Belle II Software development
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/database/DBObjPtr.h>
14#include <framework/dataobjects/EventMetaData.h>
15#include <framework/core/Environment.h>
16#include <framework/core/ModuleParam.templateDetails.h>
17#include <framework/logging/Logger.h>
18
19#include <analysis/VariableManager/Manager.h>
20#include <analysis/dataobjects/Particle.h>
21#include <analysis/dataobjects/ParticleList.h>
22#include <analysis/dbobjects/ParticleWeightingLookUpTable.h>
23
24#include <TRandom.h>
25#include <Math/Vector4D.h>
26
27#include <cmath>
28#include <map>
29
30using namespace Belle2;
31
32REG_MODULE(TrackingMomentumScaleFactors);
33
35{
37 R"DOC(Module to modify momentum of tracks from the lists. Include in your code as
38
39.. code:: python
40
41 mypath.add_module("TrackingMomentumScaleFactors", particleLists=['pi+:cut'], scale=0.999)
42
43The module modifies the input particleLists by scaling track momenta as given by the parameter scale
44
45 )DOC");
47 // Parameter definitions
48 addParam("particleLists", m_ParticleLists, "input particle lists");
49 addParam("scale", m_scale, "scale factor to be applied to 3-momentum", nan(""));
50 addParam("payloadName", m_payloadName,
51 "Base name of the table used for reweighing. The suffix '_data' or '_MC' is appended automatically "
52 "depending on whether the module runs on data or MC.", std::string(""));
53 addParam("scalingFactorName", m_scalingFactorName, "Label for the scale factor in the look up table", std::string(""));
54 addParam("smearingFactorName", m_smearingFactorName, "Label for the smearing factor in the look up table", std::string(""));
55}
56
58{
59 if (!std::isnan(m_scale) && !m_payloadName.empty()) {
60 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!");
61 } else if (std::isnan(m_scale) && m_payloadName.empty()) {
62 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!");
63 } else if (!m_scalingFactorName.empty() && !m_smearingFactorName.empty()) {
64 B2FATAL("It's not allowed to provide both a valid value for the scalingFactorName and smearingFactorName. Please set (exactly) one of the two options!");
65 } else if (!m_payloadName.empty()) {
66 // For the momentum scaling, let basf2 choose the appropriate (data or MC) payload.
67 // For the smearing no suffix is added.
68 if (!m_scalingFactorName.empty()) {
69 m_payloadName += Environment::Instance().isMC() ? "_MC" : "_data";
70 }
71 m_ParticleWeightingLookUpTable = std::make_unique<DBObjPtr<ParticleWeightingLookUpTable>>(m_payloadName);
72 }
73}
74
76{
77 if (m_ParticleWeightingLookUpTable != nullptr) {
78 if (not(*m_ParticleWeightingLookUpTable.get()).isValid()) {
79 StoreObjPtr<EventMetaData> evt;
80 B2FATAL("There is no valid payload for this run!"
81 << LogVar("payload", m_payloadName)
82 << LogVar("experiment", evt->getExperiment())
83 << LogVar("run", evt->getRun())
84 );
85 }
86
87 std::vector<std::string> variables = Variable::Manager::Instance().resolveCollections((
88 *m_ParticleWeightingLookUpTable.get())->getAxesNames());
89 for (const auto& i_variable : variables) {
91 if (!var) {
92 B2FATAL("Variable '" << i_variable << "' is not available in Variable::Manager!");
93 }
94 }
95 }
96}
97
99{
100 for (const auto& iList : m_ParticleLists) {
101 StoreObjPtr<ParticleList> particleList(iList);
102
103 //check particle List exists and has particles
104 if (!particleList) {
105 B2ERROR("ParticleList " << iList << " not found");
106 continue;
107 }
108
109 size_t nPart = particleList->getListSize();
110 for (size_t iPart = 0; iPart < nPart; iPart++) {
111 auto particle = particleList->getParticle(iPart);
112 setMomentumScalingFactor(particle);
113 }
114 }
115}
116
117
118// Getting LookUp info for given particle in given event
120{
121 std::vector<std::string> variables = Variable::Manager::Instance().resolveCollections((
122 *m_ParticleWeightingLookUpTable.get())->getAxesNames());
123
124 std::map<std::string, double> values;
125 for (const auto& i_variable : variables) {
126 const Variable::Manager::Var* var = Variable::Manager::Instance().getVariable(i_variable);
127 double value = std::get<double>(var->function(particle));
128 values.insert(std::make_pair(i_variable, value));
129 }
130
131 WeightInfo info = (*m_ParticleWeightingLookUpTable.get())->getInfo(values);
132 for (const auto& entry : info) {
133 particle->writeExtraInfo(m_payloadName + "_" + entry.first, entry.second);
134 }
135
136 return particle->getExtraInfo(m_payloadName + "_" + m_scalingFactorName);
137}
138
139
140
142{
143 std::vector<std::string> variables = Variable::Manager::Instance().resolveCollections((
144 *m_ParticleWeightingLookUpTable.get())->getAxesNames());
145
146 std::map<std::string, double> values;
147 for (const auto& i_variable : variables) {
149 double value = std::get<double>(var->function(particle));
150 values.insert(std::make_pair(i_variable, value));
151 }
152
153 WeightInfo info = (*m_ParticleWeightingLookUpTable.get())->getInfo(values);
154 for (const auto& entry : info) {
155 particle->writeExtraInfo(m_payloadName + "_" + entry.first, gRandom->Gaus(1, entry.second));
156 }
157 return particle->getExtraInfo(m_payloadName + "_" + m_smearingFactorName);
158}
159
160
161
163{
164 if (particle->getParticleSource() == Particle::EParticleSourceObject::c_Composite or
165 particle->getParticleSource() == Particle::EParticleSourceObject::c_V0) {
166 for (auto daughter : particle->getDaughters()) {
167 setMomentumScalingFactor(daughter);
168 }
169 double px = 0;
170 double py = 0;
171 double pz = 0;
172 double E = 0;
173 for (const auto daughter : particle->getDaughters()) {
174 px += daughter->getPx();
175 py += daughter->getPy();
176 pz += daughter->getPz();
177 E += daughter->getEnergy();
178 }
179 const ROOT::Math::PxPyPzEVector vec(px, py, pz, E);
180 particle->set4Vector(vec);
181 } else if (particle->getParticleSource() == Particle::EParticleSourceObject::c_Track) {
182 if (!std::isnan(m_scale)) {
183 particle->setMomentumScalingFactor(m_scale);
184 } else if (!m_scalingFactorName.empty()) {
185 particle->setMomentumScalingFactor(getScalingFactor(particle));
186 } else if (!m_smearingFactorName.empty()) {
187 particle->setMomentumSmearingFactor(getSmearingFactor(particle));
188 }
189 }
190}
R E
internal precision of FFTW codelets
bool isMC() const
Do we have generated, not real data?
static Environment & Instance()
Static method to get a reference to the Environment instance.
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition Module.cc:208
Module()
Constructor.
Definition Module.cc:30
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition Module.h:80
Class to store reconstructed particles.
Definition Particle.h:76
std::vector< std::string > m_ParticleLists
input particle lists
void initialize() override
Initializes the modules and checks the validity of the input parameters.
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
void beginRun() override
Function to be executed at each beginning of a run.
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:180
const Var * getVariable(std::string name)
Get the variable belonging to the given key.
Definition Manager.cc:58
static Manager & Instance()
get singleton instance.
Definition Manager.cc:26
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:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
Abstract base class for different kinds of events.
A variable returning a floating-point value for a given Particle.
Definition Manager.h:146