Belle II Software light-2406-ragdoll
TrackingMomentumScaleFactors.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/TrackingSystematics/TrackingMomentumScaleFactors.h>
11
12#include <framework/datastore/StoreObjPtr.h>
13#include <framework/core/ModuleParam.templateDetails.h>
14#include <analysis/VariableManager/Manager.h>
15#include <analysis/dataobjects/ParticleList.h>
16
17#include <map>
18#include <TRandom.h>
19#include <Math/Vector4D.h>
20
21using namespace Belle2;
22
23REG_MODULE(TrackingMomentumScaleFactors);
24
26{
28 R"DOC(Module to modify momentum of tracks from the lists. Include in your code as
29
30.. code:: python
31
32 mypath.add_module("TrackingMomentumScaleFactors", particleLists=['pi+:cut'], scale=0.999)
33
34The module modifies the input particleLists by scaling track momenta as given by the parameter scale
35
36 )DOC");
37 // Parameter definitions
38 addParam("particleLists", m_ParticleLists, "input particle lists");
39 addParam("scale", m_scale, "scale factor to be applied to 3-momentum", nan(""));
40 addParam("payloadName", m_payloadName, "ID of table used for reweighing", std::string(""));
41 addParam("scalingFactorName", m_scalingFactorName, "Label for the scale factor in the look up table", std::string(""));
42 addParam("smearingFactorName", m_smearingFactorName, "Label for the smearing factor in the look up table", std::string(""));
43}
44
46{
47 if (!isnan(m_scale) && !m_payloadName.empty()) {
48 B2FATAL("It's not allowed to provide both a valid value for the scale parameter and a non-empty table name. Please decide for one of the two options!");
49 } else if (isnan(m_scale) && m_payloadName.empty()) {
50 B2FATAL("Neither a valid value for the scale parameter nor a non-empty table name was provided. Please set (exactly) one of the two options!");
51 } else if (!m_scalingFactorName.empty() && !m_smearingFactorName.empty()) {
52 B2FATAL("It's not allowed to provide both a valid value for the scalingFactorName and smearingFactorName. Please set (exactly) one of the two options!");
53 } else if (!m_payloadName.empty()) {
54 m_ParticleWeightingLookUpTable = std::make_unique<DBObjPtr<ParticleWeightingLookUpTable>>(m_payloadName);
55
56 std::vector<std::string> variables = Variable::Manager::Instance().resolveCollections((
57 *m_ParticleWeightingLookUpTable.get())->getAxesNames());
58 for (const auto& i_variable : variables) {
60 if (!var) {
61 B2FATAL("Variable '" << i_variable << "' is not available in Variable::Manager!");
62 }
63 }
64 }
65}
66
68{
69 for (auto& iList : m_ParticleLists) {
70 StoreObjPtr<ParticleList> particleList(iList);
71
72 //check particle List exists and has particles
73 if (!particleList) {
74 B2ERROR("ParticleList " << iList << " not found");
75 continue;
76 }
77
78 size_t nPart = particleList->getListSize();
79 for (size_t iPart = 0; iPart < nPart; iPart++) {
80 auto particle = particleList->getParticle(iPart);
82 }
83 }
84}
85
86
87// Getting LookUp info for given particle in given event
89{
90 std::vector<std::string> variables = Variable::Manager::Instance().resolveCollections((
91 *m_ParticleWeightingLookUpTable.get())->getAxesNames());
92
93 std::map<std::string, double> values;
94 for (const auto& i_variable : variables) {
96 double value = std::get<double>(var->function(particle));
97 values.insert(std::make_pair(i_variable, value));
98 }
99
100 WeightInfo info = (*m_ParticleWeightingLookUpTable.get())->getInfo(values);
101 for (const auto& entry : info) {
102 particle->writeExtraInfo(m_payloadName + "_" + entry.first, entry.second);
103 }
104
105 return particle->getExtraInfo(m_payloadName + "_" + m_scalingFactorName);
106}
107
108
109
111{
112 std::vector<std::string> variables = Variable::Manager::Instance().resolveCollections((
113 *m_ParticleWeightingLookUpTable.get())->getAxesNames());
114
115 std::map<std::string, double> values;
116 for (const auto& i_variable : variables) {
118 double value = std::get<double>(var->function(particle));
119 values.insert(std::make_pair(i_variable, value));
120 }
121
122 WeightInfo info = (*m_ParticleWeightingLookUpTable.get())->getInfo(values);
123 for (const auto& entry : info) {
124 particle->writeExtraInfo(m_payloadName + "_" + entry.first, gRandom->Gaus(1, entry.second));
125 }
126 return particle->getExtraInfo(m_payloadName + "_" + m_smearingFactorName);
127}
128
129
130
132{
133 if (particle->getParticleSource() == Particle::EParticleSourceObject::c_Composite or
134 particle->getParticleSource() == Particle::EParticleSourceObject::c_V0) {
135 for (auto daughter : particle->getDaughters()) {
136 setMomentumScalingFactor(daughter);
137 }
138 double px = 0;
139 double py = 0;
140 double pz = 0;
141 double E = 0;
142 for (auto daughter : particle->getDaughters()) {
143 px += daughter->getPx();
144 py += daughter->getPy();
145 pz += daughter->getPz();
146 E += daughter->getEnergy();
147 }
148 const ROOT::Math::PxPyPzEVector vec(px, py, pz, E);
149 particle->set4Vector(vec);
150 } else if (particle->getParticleSource() == Particle::EParticleSourceObject::c_Track) {
151 if (!isnan(m_scale)) {
153 } else if (!m_scalingFactorName.empty()) {
154 particle->setMomentumScalingFactor(getScalingFactor(particle));
155 } else if (!m_smearingFactorName.empty()) {
156 particle->setMomentumSmearingFactor(getSmearingFactor(particle));
157 }
158 }
159
160
161}
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 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
std::vector< Belle2::Particle * > getDaughters() const
Returns a vector of pointers to daughter particles.
Definition: Particle.cc:637
void setMomentumSmearingFactor(double momentumSmearingFactor)
Sets momentum smearing.
Definition: Particle.h:344
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 > m_ParticleLists
input particle lists
virtual void initialize() override
Initializes the modules and checks the validity of the input parameters.
virtual void event() override
Function to be executed at each event.
std::string m_scalingFactorName
Name of the scale factor from table.
std::unique_ptr< DBObjPtr< ParticleWeightingLookUpTable > > m_ParticleWeightingLookUpTable
Pointer to the table in DB.
void setMomentumScalingFactor(Particle *particle)
function to set momentum scaling factor
TrackingMomentumScaleFactorsModule()
Constructor: Sets the description, the properties and the parameters of the module.
double getSmearingFactor(Particle *particle)
Returns the needed smearing factor for particle based on payloadName and smearingFactorName.
std::string m_smearingFactorName
Name of the smear factor from table.
double getScalingFactor(Particle *particle)
Returns the needed scale factor for particle based on payloadName and scalingFactorName.
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