Belle II Software light-2406-ragdoll
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 }
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
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
void updateJacobiMatrix()
Propagate the photon energy scaling to jacobian elements that were calculated using energy.
Definition: Particle.cc:257
void writeExtraInfo(const std::string &name, const double value)
Sets the user defined extraInfo.
Definition: Particle.cc:1308
void setMomentumScalingFactor(double momentumScalingFactor)
Sets momentum scaling.
Definition: Particle.h:334
int getPDGCode(void) const
Returns PDG code.
Definition: Particle.h:454
std::vector< Belle2::Particle * > getDaughters() const
Returns a vector of pointers to daughter particles.
Definition: Particle.cc:637
void set4Vector(const ROOT::Math::PxPyPzEVector &p4)
Sets Lorentz vector.
Definition: Particle.h:271
EParticleSourceObject getParticleSource() const
Returns particle source as defined with enum EParticleSourceObject.
Definition: Particle.h:478
double getExtraInfo(const std::string &name) const
Return given value if set.
Definition: Particle.cc:1289
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.
Definition: ClusterUtils.h:24
A variable returning a floating-point value for a given Particle.
Definition: Manager.h:146