Belle II Software  release-08-01-10
TrackingSystematics.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/TrackingSystematics.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 
21 using namespace Belle2;
22 
23 REG_MODULE(TrackingEfficiency);
24 REG_MODULE(TrackingMomentum);
25 
27 {
29  R"DOC(Module to remove tracks from the lists at random. Include in your code as
30 
31  .. code:: python
32 
33  mypath.add_module("TrackingEfficiency", particleLists=['pi+:cut'], frac=0.01)
34 
35 The module modifies the input particleLists by randomly removing tracks with the probability frac.
36 
37  )DOC");
38  // Parameter definitions
39  addParam("particleLists", m_ParticleLists, "input particle lists");
40  addParam("frac", m_frac, "probability to remove the particle", 0.0);
41 }
42 
44 {
45  // map from mdstIndex to decision
46  std::map <unsigned, bool> indexToRemove;
47 
48  // determine list of mdst tracks:
49  for (auto& iList : m_ParticleLists) {
50  StoreObjPtr<ParticleList> particleList(iList);
51  //check particle List exists and has particles
52  if (!particleList) {
53  B2ERROR("ParticleList " << iList << " not found");
54  continue;
55  }
56 
57  if (!Const::chargedStableSet.contains(Const::ParticleType(abs(particleList->getPDGCode())))) {
58  B2ERROR("The provided particlelist " << iList << " does not contain track-based particles.");
59  }
60 
61  std::vector<unsigned int> toRemove;
62  size_t nPart = particleList->getListSize();
63  for (size_t iPart = 0; iPart < nPart; iPart++) {
64  auto particle = particleList->getParticle(iPart);
65  unsigned mdstIndex = particle->getMdstArrayIndex();
66  bool remove;
67  if (indexToRemove.find(mdstIndex) != indexToRemove.end()) {
68  // found, use entry
69  remove = indexToRemove.at(mdstIndex);
70  } else {
71  // not found, generate and store it
72  auto prob = gRandom->Uniform();
73  remove = prob < m_frac;
74  indexToRemove.insert(std::pair{mdstIndex, remove});
75  }
76  if (remove) toRemove.push_back(particle->getArrayIndex());
77  }
78  particleList->removeParticles(toRemove);
79  }
80 }
81 
83 {
85  R"DOC(Module to modify momentum of tracks from the lists. Include in your code as
86 
87  .. code:: python
88 
89  mypath.add_module("TrackingMomentum", particleLists=['pi+:cut'], scale=0.999)
90 
91 The module modifies the input particleLists by scaling track momenta as given by the parameter scale
92 
93  )DOC");
94  // Parameter definitions
95  addParam("particleLists", m_ParticleLists, "input particle lists");
96  addParam("scale", m_scale, "scale factor to be applied to 3-momentum", nan(""));
97  addParam("payloadName", m_payloadName, "ID of table used for reweighing", std::string(""));
98  addParam("scalingFactorName", m_scalingFactorName, "Label for the scale factor in the look up table", std::string(""));
99  addParam("smearingFactorName", m_smearingFactorName, "Label for the smearing factor in the look up table", std::string(""));
100 }
101 
103 {
104  if (!isnan(m_scale) && !m_payloadName.empty()) {
105  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!");
106  } else if (isnan(m_scale) && m_payloadName.empty()) {
107  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!");
108  } else if (!m_scalingFactorName.empty() && !m_smearingFactorName.empty()) {
109  B2FATAL("It's not allowed to provide both a valid value for the scalingFactorName and smearingFactorName. Please set (exactly) one of the two options!");
110  } else if (!m_payloadName.empty()) {
111  m_ParticleWeightingLookUpTable = std::make_unique<DBObjPtr<ParticleWeightingLookUpTable>>(m_payloadName);
112 
113  std::vector<std::string> variables = Variable::Manager::Instance().resolveCollections((
114  *m_ParticleWeightingLookUpTable.get())->getAxesNames());
115  for (const auto& i_variable : variables) {
117  if (!var) {
118  B2FATAL("Variable '" << i_variable << "' is not available in Variable::Manager!");
119  }
120  }
121  }
122 }
123 
125 {
126  for (auto& iList : m_ParticleLists) {
127  StoreObjPtr<ParticleList> particleList(iList);
128 
129  //check particle List exists and has particles
130  if (!particleList) {
131  B2ERROR("ParticleList " << iList << " not found");
132  continue;
133  }
134 
135  size_t nPart = particleList->getListSize();
136  for (size_t iPart = 0; iPart < nPart; iPart++) {
137  auto particle = particleList->getParticle(iPart);
138  setMomentumScalingFactor(particle);
139  }
140  }
141 }
142 
143 
144 // Getting LookUp info for given particle in given event
146 {
147  std::vector<std::string> variables = Variable::Manager::Instance().resolveCollections((
148  *m_ParticleWeightingLookUpTable.get())->getAxesNames());
149 
150  std::map<std::string, double> values;
151  for (const auto& i_variable : variables) {
153  double value = std::get<double>(var->function(particle));
154  values.insert(std::make_pair(i_variable, value));
155  }
156 
157  WeightInfo info = (*m_ParticleWeightingLookUpTable.get())->getInfo(values);
158  for (const auto& entry : info) {
159  particle->writeExtraInfo(m_payloadName + "_" + entry.first, entry.second);
160  }
161 
162  return particle->getExtraInfo(m_payloadName + "_" + m_scalingFactorName);
163 }
164 
165 
166 
168 {
169  std::vector<std::string> variables = Variable::Manager::Instance().resolveCollections((
170  *m_ParticleWeightingLookUpTable.get())->getAxesNames());
171 
172  std::map<std::string, double> values;
173  for (const auto& i_variable : variables) {
175  double value = std::get<double>(var->function(particle));
176  values.insert(std::make_pair(i_variable, value));
177  }
178 
179  WeightInfo info = (*m_ParticleWeightingLookUpTable.get())->getInfo(values);
180  for (const auto& entry : info) {
181  particle->writeExtraInfo(m_payloadName + "_" + entry.first, gRandom->Gaus(1, entry.second));
182  }
183  return particle->getExtraInfo(m_payloadName + "_" + m_smearingFactorName);
184 }
185 
186 
187 
189 {
190  if (particle->getParticleSource() == Particle::EParticleSourceObject::c_Composite or
191  particle->getParticleSource() == Particle::EParticleSourceObject::c_V0) {
192  for (auto daughter : particle->getDaughters()) {
193  setMomentumScalingFactor(daughter);
194  }
195  double px = 0;
196  double py = 0;
197  double pz = 0;
198  double E = 0;
199  for (auto daughter : particle->getDaughters()) {
200  px += daughter->getPx();
201  py += daughter->getPy();
202  pz += daughter->getPz();
203  E += daughter->getEnergy();
204  }
205  const ROOT::Math::PxPyPzEVector vec(px, py, pz, E);
206  particle->set4Vector(vec);
207  } else if (particle->getParticleSource() == Particle::EParticleSourceObject::c_Track) {
208  if (!isnan(m_scale)) {
209  particle->setMomentumScalingFactor(m_scale);
210  } else if (!m_scalingFactorName.empty()) {
211  particle->setMomentumScalingFactor(getScalingFactor(particle));
212  } else if (!m_smearingFactorName.empty()) {
213  particle->setMomentumSmearingFactor(getSmearingFactor(particle));
214  }
215  }
216 
217 
218 }
R E
internal precision of FFTW codelets
The ParticleType class for identifying different particle types.
Definition: Const.h:399
static const ParticleSet chargedStableSet
set of charged stable particles
Definition: Const.h:609
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 event() override
Function to be executed at each event.
double m_frac
fraction of particles to be removed from the particlelist
TrackingEfficiencyModule()
Constructor: Sets the description, the properties and the parameters of the module.
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::string m_payloadName
Name of the table
std::unique_ptr< DBObjPtr< ParticleWeightingLookUpTable > > m_ParticleWeightingLookUpTable
Pointer to the table in DB.
void setMomentumScalingFactor(Particle *particle)
function to set momentum scaling factor
double m_scale
input momentum scale modifier
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.
TrackingMomentumModule()
Constructor: Sets the description, the properties and the parameters of the module.
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.
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