Belle II Software  light-2403-persian
TrackingEfficiency.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/TrackingEfficiency.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 
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 
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
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.
REG_MODULE(B2BIIConvertBeamParams)
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.
Definition: ClusterUtils.h:24