Belle II Software  release-08-01-10
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 
22 using namespace Belle2;
23 
24 //-----------------------------------------------------------------
25 // Register module
26 //-----------------------------------------------------------------
27 
28 REG_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 
43 The module modifies the input particleLists by scaling energy as given by the scale in the LookUpTable
44 
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  }
62  values.insert(std::make_pair(i_variable, std::get<double>(var->function(particle))));
63  }
64 
65  return (*m_ParticleWeightingLookUpTable.get())->getInfo(values);
66 }
67 
69 {
70  //check if this module is used only for data
71  if (Environment::Instance().isMC()) {
72  B2ERROR("Attempting to run EnergyBiasCorrection on MC but this is only for data");
73  }
74 
75  m_ParticleWeightingLookUpTable = std::make_unique<DBObjPtr<ParticleWeightingLookUpTable>>(m_tableName);
76 }
77 
78 
80 {
81  //check if this module is used only for data
82  if (Environment::Instance().isMC()) {
83  return;
84  }
85 
86  for (auto& iList : m_ParticleLists) {
87  StoreObjPtr<ParticleList> particleList(iList);
88 
89  //check particle List exists and has particles
90  if (!particleList) {
91  B2ERROR("ParticleList " << iList << " not found");
92  continue;
93  }
94 
95  size_t nPart = particleList->getListSize();
96  for (size_t iPart = 0; iPart < nPart; iPart++) {
97  auto particle = particleList->getParticle(iPart);
98  setEnergyScalingFactor(particle);
99  }
100  }
101 
102 }
103 
105 {
106  if (particle->getParticleSource() == Particle::EParticleSourceObject::c_Composite) {
107  for (auto daughter : particle->getDaughters()) {
108  // if composite then apply scaling for daughters then recalculate (this particle) parent momentum
109  setEnergyScalingFactor(daughter);
110  }
111  double px = 0;
112  double py = 0;
113  double pz = 0;
114  double E = 0;
115  for (auto daughter : particle->getDaughters()) {
116  px += daughter->getPx();
117  py += daughter->getPy();
118  pz += daughter->getPz();
119  E += daughter->getEnergy();
120  }
121  const ROOT::Math::PxPyPzEVector vec(px, py, pz, E);
122  particle->set4Vector(vec);
123  } else if (particle->getParticleSource() == Particle::EParticleSourceObject::c_ECLCluster
124  && particle->getPDGCode() == Const::photon.getPDGCode()) {
125  //particle is photon reconstructed from ECL cluster
126  WeightInfo info = getInfo(particle);
127  for (const auto& entry : info) {
128  particle->writeExtraInfo(m_tableName + "_" + entry.first, entry.second);
129  }
130  particle->setMomentumScalingFactor(particle->getExtraInfo(m_tableName + "_Weight"));
131  particle->updateJacobiMatrix();
132  }
133  B2DEBUG(10, "Called setMomentumScalingFactor for an unspecified, track-based or KLM cluster-based particle");
134 }
135 
R E
internal precision of FFTW codelets
int getPDGCode() const
PDG code.
Definition: Const.h:464
static const ParticleType photon
photon particle
Definition: Const.h:664
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.
REG_MODULE(arichBtest)
Register the Module.
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
Abstract base class for different kinds of events.
A variable returning a floating-point value for a given Particle.
Definition: Manager.h:146