Belle II Software  release-06-02-00
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 include
10 #include <analysis/modules/TrackingSystematics/TrackingSystematics.h>
11 
12 // dataobjects
13 #include <analysis/dataobjects/ParticleList.h>
14 
15 #include <framework/datastore/StoreObjPtr.h>
16 
17 #include <map>
18 #include <TRandom.h>
19 
20 using namespace Belle2;
21 
22 REG_MODULE(TrackingEfficiency);
23 REG_MODULE(TrackingMomentum);
24 
26 {
28  R"DOC(Module to remove tracks from the lists at random. Include in your code as
29 
30  .. code:: python
31 
32  mypath.add_module("TrackingEfficiency", particleLists=['pi+:cut'], frac=0.01)
33 
34 The module modifies the input particleLists by randomly removing tracks with the probability frac.
35 
36  )DOC");
37  // Parameter definitions
38  addParam("particleLists", m_ParticleLists, "input particle lists");
39  addParam("frac", m_frac, "probability to remove the particle", 0.0);
40 }
41 
43 {
44  // map from mdstIndex to decision
45  std::map <unsigned, bool> indexToRemove;
46 
47  // determine list of mdst tracks:
48  for (auto& iList : m_ParticleLists) {
49  StoreObjPtr<ParticleList> particleList(iList);
50  //check particle List exists and has particles
51  if (!particleList) {
52  B2ERROR("ParticleList " << iList << " not found");
53  continue;
54  }
55 
56  if (!Const::chargedStableSet.contains(Const::ParticleType(abs(particleList->getPDGCode())))) {
57  B2ERROR("The provided particlelist " << iList << " does not contain track-based particles.");
58  }
59 
60  std::vector<unsigned int> toRemove;
61  size_t nPart = particleList->getListSize();
62  for (size_t iPart = 0; iPart < nPart; iPart++) {
63  auto particle = particleList->getParticle(iPart);
64  unsigned mdstIndex = particle->getMdstArrayIndex();
65  bool remove;
66  if (indexToRemove.find(mdstIndex) != indexToRemove.end()) {
67  // found, use entry
68  remove = indexToRemove.at(mdstIndex);
69  } else {
70  // not found, generate and store it
71  auto prob = gRandom->Uniform();
72  remove = prob < m_frac;
73  indexToRemove.insert(std::pair{mdstIndex, remove});
74  }
75  if (remove) toRemove.push_back(particle->getArrayIndex());
76  }
77  particleList->removeParticles(toRemove);
78  }
79 }
80 
82 {
83  setDescription(
84  R"DOC(Module to modify momentum of tracks from the lists. Include in your code as
85 
86  .. code:: python
87 
88  mypath.add_module("TrackingMomentum", particleLists=['pi+:cut'], scale=0.999)
89 
90 The module modifies the input particleLists by scaling track momenta as given by the parameter scale
91 
92  )DOC");
93  // Parameter definitions
94  addParam("particleLists", m_ParticleLists, "input particle lists");
95  addParam("scale", m_scale, "scale factor to be applied to 3-momentum", 0.999);
96 }
97 
99 {
100  for (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 
118 {
119  if (particle->getParticleSource() == Particle::EParticleSourceObject::c_Composite or
120  particle->getParticleSource() == Particle::EParticleSourceObject::c_V0) {
121  for (auto daughter : particle->getDaughters()) {
122  setMomentumScalingFactor(daughter);
123  }
124  double px = 0;
125  double py = 0;
126  double pz = 0;
127  double E = 0;
128  for (auto daughter : particle->getDaughters()) {
129  px += daughter->getPx();
130  py += daughter->getPy();
131  pz += daughter->getPz();
132  E += daughter->getEnergy();
133  }
134  const TLorentzVector vec(px, py, pz, E);
135  particle->set4Vector(vec);
136  } else if (particle->getParticleSource() == Particle::EParticleSourceObject::c_Track) {
137  particle->setMomentumScalingFactor(m_scale);
138  }
139 }
The ParticleType class for identifying different particle types.
Definition: Const.h:289
static const ParticleSet chargedStableSet
set of charged stable particles
Definition: Const.h:499
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:74
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:95
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 event() override
Function to be executed at each event.
void setMomentumScalingFactor(Particle *particle)
function to set momentum scaling factor
double m_scale
input momentum scale modifier
TrackingMomentumModule()
Constructor: Sets the description, the properties and the parameters of 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
#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.