Belle II Software development
BiasCorrection.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9// Own header.
10#include <analysis/modules/BiasCorrection/BiasCorrection.h>
11#include <iostream>
12
13#include <framework/datastore/StoreObjPtr.h>
14#include <framework/core/ModuleParam.templateDetails.h>
15#include <framework/core/Environment.h>
16#include <analysis/VariableManager/Manager.h>
17
18#include <Math/Vector4D.h>
19
20#include <map>
21
22using namespace Belle2;
23
24//-----------------------------------------------------------------
25// Register module
26//-----------------------------------------------------------------
27
28REG_MODULE(EnergyBiasCorrection);
29
30//-----------------------------------------------------------------
31// Implementation
32//-----------------------------------------------------------------
33
35{
37 R"DOC(Module to modify energy from the lists. Include in your code as
38
39.. code:: python
40
41 mypath.add_module("EnergyBiasCorrection", particleLists=['gamma:cut'], scale=tableName_Weight)
42
43The module modifies the input particleLists by scaling energy as given by the scale in the LookUpTable
45 )DOC");
46 // Parameter definitions
47 addParam("particleLists", m_ParticleLists, "input particle lists");
48 addParam("tableName", m_tableName, "ID of table used for reweighing");
49}
50
51// Getting LookUp info for given particle in given event
53{
54 std::vector<std::string> variables = Variable::Manager::Instance().resolveCollections((
55 *m_ParticleWeightingLookUpTable.get())->getAxesNames());
56 std::map<std::string, double> values;
57 for (const auto& i_variable : variables) {
59 if (!var) {
60 B2ERROR("Variable '" << i_variable << "' is not available in Variable::Manager!");
61 } else {
62 values.insert(std::make_pair(i_variable, std::get<double>(var->function(particle))));
63 }
64 }
65
66 return (*m_ParticleWeightingLookUpTable.get())->getInfo(values);
67}
68
70{
71 //check if this module is used only for data
72 if (Environment::Instance().isMC()) {
73 B2ERROR("Attempting to run EnergyBiasCorrection on MC but this is only for data");
74 }
75
76 m_ParticleWeightingLookUpTable = std::make_unique<DBObjPtr<ParticleWeightingLookUpTable>>(m_tableName);
77}
78
79
81{
82 //check if this module is used only for data
83 if (Environment::Instance().isMC()) {
84 return;
85 }
86
87 for (auto& iList : m_ParticleLists) {
88 StoreObjPtr<ParticleList> particleList(iList);
89
90 //check particle List exists and has particles
91 if (!particleList) {
92 B2ERROR("ParticleList " << iList << " not found");
93 continue;
94 }
95
96 size_t nPart = particleList->getListSize();
97 for (size_t iPart = 0; iPart < nPart; iPart++) {
98 auto particle = particleList->getParticle(iPart);
99 setEnergyScalingFactor(particle);
100 }
101 }
102
103}
104
106{
107 if (particle->getParticleSource() == Particle::EParticleSourceObject::c_Composite) {
108 for (auto daughter : particle->getDaughters()) {
109 // if composite then apply scaling for daughters then recalculate (this particle) parent momentum
110 setEnergyScalingFactor(daughter);
111 }
112 double px = 0;
113 double py = 0;
114 double pz = 0;
115 double E = 0;
116 for (auto daughter : particle->getDaughters()) {
117 px += daughter->getPx();
118 py += daughter->getPy();
119 pz += daughter->getPz();
120 E += daughter->getEnergy();
121 }
122 const ROOT::Math::PxPyPzEVector vec(px, py, pz, E);
123 particle->set4Vector(vec);
124 } else if (particle->getParticleSource() == Particle::EParticleSourceObject::c_ECLCluster
125 && particle->getPDGCode() == Const::photon.getPDGCode()) {
126 //particle is photon reconstructed from ECL cluster
127 WeightInfo info = getInfo(particle);
128 for (const auto& entry : info) {
129 particle->writeExtraInfo(m_tableName + "_" + entry.first, entry.second);
130 }
131 particle->setMomentumScalingFactor(particle->getExtraInfo(m_tableName + "_Weight"));
132 particle->updateJacobiMatrix();
133 }
134 B2DEBUG(10, "Called setMomentumScalingFactor for an unspecified, track-based or KLM cluster-based particle");
135}
136
R E
internal precision of FFTW codelets
int getPDGCode() const
PDG code.
Definition: Const.h:473
static const ParticleType photon
photon particle
Definition: Const.h:673
std::vector< std::string > m_ParticleLists
input particle lists
void setEnergyScalingFactor(Particle *particle)
function to set scaling factor
virtual void event() override
Function to be executed at each event.
WeightInfo getInfo(const Particle *particle)
Get LookUp information for the particle.
std::unique_ptr< DBObjPtr< ParticleWeightingLookUpTable > > m_ParticleWeightingLookUpTable
Pointer to the table in DB.
virtual void beginRun() override
Nothing so far.
std::string m_tableName
Name of the table.
EnergyBiasCorrectionModule()
Constructor: Sets the description, the properties and the parameters of the module.
static Environment & Instance()
Static method to get a reference to the Environment instance.
Definition: Environment.cc:28
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
Class to store reconstructed particles.
Definition: Particle.h:75
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
std::vector< std::string > resolveCollections(const std::vector< std::string > &variables)
Resolve Collection Returns variable names corresponding to the given collection or if it is not a col...
Definition: Manager.cc:179
const Var * getVariable(std::string name)
Get the variable belonging to the given key.
Definition: Manager.cc:57
static Manager & Instance()
get singleton instance.
Definition: Manager.cc:25
std::map< std::string, double > WeightInfo
Weight information: a line from the weight lookup table.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.
A variable returning a floating-point value for a given Particle.
Definition: Manager.h:146