Belle II Software release-09-00-00
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 <framework/core/Environment.h>
15#include <analysis/VariableManager/Manager.h>
16#include <analysis/dataobjects/ParticleList.h>
17
18#include <map>
19#include <TRandom.h>
20#include <Math/Vector4D.h>
21
22using namespace Belle2;
23
24REG_MODULE(TrackingMomentumScaleFactors);
25
27{
29 R"DOC(Module to modify momentum of tracks from the lists. Include in your code as
30
31.. code:: python
32
33 mypath.add_module("TrackingMomentumScaleFactors", particleLists=['pi+:cut'], scale=0.999)
34
35The module modifies the input particleLists by scaling track momenta as given by the parameter scale
36
37 )DOC");
38 // Parameter definitions
39 addParam("particleLists", m_ParticleLists, "input particle lists");
40 addParam("scale", m_scale, "scale factor to be applied to 3-momentum", nan(""));
41 addParam("payloadName", m_payloadName, "ID of table used for reweighing", std::string(""));
42 addParam("scalingFactorName", m_scalingFactorName, "Label for the scale factor in the look up table", std::string(""));
43 addParam("smearingFactorName", m_smearingFactorName, "Label for the smearing factor in the look up table", std::string(""));
44}
45
47{
48 if (!isnan(m_scale) && !m_payloadName.empty()) {
49 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!");
50 } else if (isnan(m_scale) && m_payloadName.empty()) {
51 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!");
52 } else if (!m_scalingFactorName.empty() && !m_smearingFactorName.empty()) {
53 B2FATAL("It's not allowed to provide both a valid value for the scalingFactorName and smearingFactorName. Please set (exactly) one of the two options!");
54 } else if (!m_payloadName.empty()) {
55 if (Environment::Instance().isMC()) {
56 m_payloadName += "_MC";
57 } else {
58 m_payloadName += "_Data";
59 }
60 m_ParticleWeightingLookUpTable = std::make_unique<DBObjPtr<ParticleWeightingLookUpTable>>(m_payloadName);
61
62 std::vector<std::string> variables = Variable::Manager::Instance().resolveCollections((
63 *m_ParticleWeightingLookUpTable.get())->getAxesNames());
64 for (const auto& i_variable : variables) {
66 if (!var) {
67 B2FATAL("Variable '" << i_variable << "' is not available in Variable::Manager!");
68 }
69 }
70 }
71}
72
74{
75 for (auto& iList : m_ParticleLists) {
76 StoreObjPtr<ParticleList> particleList(iList);
77
78 //check particle List exists and has particles
79 if (!particleList) {
80 B2ERROR("ParticleList " << iList << " not found");
81 continue;
82 }
83
84 size_t nPart = particleList->getListSize();
85 for (size_t iPart = 0; iPart < nPart; iPart++) {
86 auto particle = particleList->getParticle(iPart);
88 }
89 }
90}
91
92
93// Getting LookUp info for given particle in given event
95{
96 std::vector<std::string> variables = Variable::Manager::Instance().resolveCollections((
97 *m_ParticleWeightingLookUpTable.get())->getAxesNames());
98
99 std::map<std::string, double> values;
100 for (const auto& i_variable : variables) {
102 double value = std::get<double>(var->function(particle));
103 values.insert(std::make_pair(i_variable, value));
104 }
105
106 WeightInfo info = (*m_ParticleWeightingLookUpTable.get())->getInfo(values);
107 for (const auto& entry : info) {
108 particle->writeExtraInfo(m_payloadName + "_" + entry.first, entry.second);
109 }
110
111 return particle->getExtraInfo(m_payloadName + "_" + m_scalingFactorName);
112}
113
114
115
117{
118 std::vector<std::string> variables = Variable::Manager::Instance().resolveCollections((
119 *m_ParticleWeightingLookUpTable.get())->getAxesNames());
120
121 std::map<std::string, double> values;
122 for (const auto& i_variable : variables) {
124 double value = std::get<double>(var->function(particle));
125 values.insert(std::make_pair(i_variable, value));
126 }
127
128 WeightInfo info = (*m_ParticleWeightingLookUpTable.get())->getInfo(values);
129 for (const auto& entry : info) {
130 particle->writeExtraInfo(m_payloadName + "_" + entry.first, gRandom->Gaus(1, entry.second));
131 }
132 return particle->getExtraInfo(m_payloadName + "_" + m_smearingFactorName);
133}
134
135
136
138{
139 if (particle->getParticleSource() == Particle::EParticleSourceObject::c_Composite or
140 particle->getParticleSource() == Particle::EParticleSourceObject::c_V0) {
141 for (auto daughter : particle->getDaughters()) {
142 setMomentumScalingFactor(daughter);
143 }
144 double px = 0;
145 double py = 0;
146 double pz = 0;
147 double E = 0;
148 for (auto daughter : particle->getDaughters()) {
149 px += daughter->getPx();
150 py += daughter->getPy();
151 pz += daughter->getPz();
152 E += daughter->getEnergy();
153 }
154 const ROOT::Math::PxPyPzEVector vec(px, py, pz, E);
155 particle->set4Vector(vec);
156 } else if (particle->getParticleSource() == Particle::EParticleSourceObject::c_Track) {
157 if (!isnan(m_scale)) {
158 particle->setMomentumScalingFactor(m_scale);
159 } else if (!m_scalingFactorName.empty()) {
160 particle->setMomentumScalingFactor(getScalingFactor(particle));
161 } else if (!m_smearingFactorName.empty()) {
162 particle->setMomentumSmearingFactor(getSmearingFactor(particle));
163 }
164 }
165
166
167}
R E
internal precision of FFTW codelets
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 > 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
TString getInfo(const TObject *obj)
Get object info HTML (e.g.
Definition: ObjectInfo.cc:55
Abstract base class for different kinds of events.
A variable returning a floating-point value for a given Particle.
Definition: Manager.h:146