Belle II Software  release-05-02-19
RemoveMCParticlesModule.cc
1 /**************************************************************************
2  * Belle II detector background library *
3  * Copyright(C) 2011-2012 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter, Marko Petric *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <generators/modules/RemoveMCParticlesModule.h>
12 
13 #include <framework/logging/Logger.h>
14 #include <framework/gearbox/Unit.h>
15 #include <framework/datastore/StoreArray.h>
16 
17 using namespace Belle2;
18 
19 //-----------------------------------------------------------------
20 // Register the Module
21 //-----------------------------------------------------------------
22 REG_MODULE(RemoveMCParticles)
23 
24 //-----------------------------------------------------------------
25 // Implementation
26 //-----------------------------------------------------------------
27 
28 namespace {
30  inline bool alwaysCut(double min, double max, double value)
31  {
32  return (value < min || value >= max);
33  }
35  inline bool optionalCut(double min, double max, double value)
36  {
37  if (min < max) return (value < min || value >= max);
38  return false;
39  }
40 }
41 
43 {
44  //Set module properties
45  setDescription(R"DOC(
46 Remove particles from the MCParticle Collection.
47 
48 Warning:
49  At the moment, Relations to that MCParticle collection will become invalid
50  and are not fixed automatically
51 )DOC");
52 
53  //Parameter definition
54  addParam("collectionName", m_particleList, "Collection to perform the cuts on", std::string(""));
55  addParam("minZ", m_minZ,
56  "Minimum Z value of Particles to be kept. If bigger or equal to maxZ, no cut on z is performed", 0.0);
57  addParam("maxZ", m_maxZ,
58  "Maximum Z value of Particles to be kept. If smaller or equal to minZ, no cut on z is performed", 0.0);
59  addParam("minR", m_minR,
60  "Minimum Rphi value of Particles to be kept. If bigger or equal to maxR, no cut on Rphi is performed", 0.0);
61  addParam("maxR", m_maxR,
62  "Maximum Rphi value of Particles to be kept. If smaller or equal to minR, no cut on Rphi is performed", 0.0);
63  addParam("minTheta", m_minTheta,
64  "Minimum theta value of Particles to be kept", 0.0);
65  addParam("maxTheta", m_maxTheta,
66  "Maximum theta value of Particles to be kept", 180.0);
67  addParam("minPt", m_minPt,
68  "Minimum Pt value of Particles to be kept. If bigger or equal to maxPt, no cut on Pt is performed", 0.0);
69  addParam("maxPt", m_maxPt,
70  "Maximum Pt value of Particles to be kept. If smaller or equal to minPt, no cut on Pt is performed", 0.0);
71  addParam("alsoChildren", m_alsoChildren,
72  "If true, all children of a particle are removed together with the particle, otherwise children are kept", true);
73  addParam("pdgCodes", m_pdgCodes,
74  "If not empty, cuts will only be performed on particles matching the given PDG codes. "
75  "To remove all particles with a given code just set maxTheta to 0 and fill this list "
76  "with all codes to be removed", m_pdgCodes);
77 }
78 
80 {
81  StoreArray<MCParticle> mcparticle;
82  mcparticle.registerInDataStore();
83 
84  std::sort(m_pdgCodes.begin(), m_pdgCodes.end());
87 }
88 
90 {
92  if (!mcParticles) {
93  B2WARNING("No MCParticle collection with name \"" << m_particleList << "\", cannot remove anything");
94  return;
95  }
96  unsigned nPart = mcParticles.getEntries();
97 
98  m_mpg.clear();
100  //We know that all MCParticles in the list are also 1:1 in the graph with the
101  //same index. This helps also removing children if neccessary. No we apply
102  //cuts to all particles having no mother and the applyCuts method will then
103  //do all the children
104  for (unsigned i = 0; i < nPart; ++i) {
105  MCParticle& mcp = *mcParticles[i];
106  if (!mcp.getMother()) applyCuts(mcp, false);
107  }
108 
110 
111  //FIXME: Update all Relations pointing to this MCParticle List?
112  //We would need to create an old index -> new index list and than call
113  //consolidate on all affected relations. It would be a good idea to implement
114  //in a generic way (maybe as part of MCParticleGraph) to also use the same
115  //code after Simulation
116 }
117 
118 void RemoveMCParticlesModule::applyCuts(const MCParticle& particle, bool cut)
119 {
120  //If we don't remove all children, the cut does not propagate
121  if (!m_alsoChildren) cut = false;
122 
123  //Only apply cuts if list of pdg codes is empty or pdg code of current particle is in list
124  if (m_pdgCodes.empty() || std::binary_search(m_pdgCodes.begin(), m_pdgCodes.end(), particle.getPDG())) {
125  cut |= optionalCut(m_minZ, m_maxZ, particle.getProductionVertex().Z());
126  cut |= optionalCut(m_minR, m_maxR, particle.getProductionVertex().Perp());
127  cut |= optionalCut(m_minPt, m_maxPt, particle.getMomentum().Pt());
128  cut |= alwaysCut(m_minTheta, m_maxTheta, particle.getMomentum().Theta());
129  }
130 
131  //If cut is true, then we remove this particle from the list by ignoring it in the graph
132  if (cut) m_mpg[particle.getArrayIndex()].setIgnore(true);
133 
134  //Then we look at all daughters
135  for (MCParticle* daughter : particle.getDaughters()) {
136  applyCuts(*daughter, cut);
137  }
138 }
139 
141 {
142 }
Belle2::MCParticleGraph::generateList
void generateList(const std::string &name="", int options=c_setNothing)
Generates the MCParticle list and stores it in the StoreArray with the given name.
Definition: MCParticleGraph.cc:211
Belle2::RemoveMCParticlesModule::m_alsoChildren
bool m_alsoChildren
If true, also remove all children of a particle if it fails any cut.
Definition: RemoveMCParticlesModule.h:78
Belle2::Module::setDescription
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:216
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::RemoveMCParticlesModule::m_minZ
double m_minZ
Minimum Z value of particles to keep.
Definition: RemoveMCParticlesModule.h:70
Belle2::RemoveMCParticlesModule::terminate
virtual void terminate() override
Terminates the module.
Definition: RemoveMCParticlesModule.cc:140
Belle2::MCParticleGraph::loadList
void loadList(const std::string &name="")
Load the MCParticle list given by name into the Graph.
Definition: MCParticleGraph.cc:241
Belle2::RemoveMCParticlesModule::m_minPt
double m_minPt
Minimum Pt value of particles to keep.
Definition: RemoveMCParticlesModule.h:76
Belle2::RemoveMCParticlesModule::m_mpg
MCParticleGraph m_mpg
ParticleGraph used for reformatting MCParticle collection.
Definition: RemoveMCParticlesModule.h:81
Belle2::RemoveMCParticlesModule::event
virtual void event() override
Method is called for each event.
Definition: RemoveMCParticlesModule.cc:89
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::RemoveMCParticlesModule::m_maxTheta
double m_maxTheta
Maximum Theta value of particles to keep.
Definition: RemoveMCParticlesModule.h:75
Belle2::RemoveMCParticlesModule::RemoveMCParticlesModule
RemoveMCParticlesModule()
Constructor.
Definition: RemoveMCParticlesModule.cc:42
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RemoveMCParticlesModule::applyCuts
void applyCuts(const MCParticle &particle, bool cut=false)
Apply cuts on a Particle and call for all daugthers recursively.
Definition: RemoveMCParticlesModule.cc:118
Belle2::MCParticle::getMother
MCParticle * getMother() const
Returns a pointer to the mother particle.
Definition: MCParticle.h:593
Belle2::RemoveMCParticlesModule::m_maxZ
double m_maxZ
Maximum Z value of particles to keep.
Definition: RemoveMCParticlesModule.h:71
Belle2::Unit::deg
static const double deg
degree to radians
Definition: Unit.h:119
Belle2::MCParticleGraph::c_clearParticles
@ c_clearParticles
Clear the particle list before adding the graph.
Definition: MCParticleGraph.h:76
Belle2::RemoveMCParticlesModule::m_minTheta
double m_minTheta
Minimum Theta value of particles to keep.
Definition: RemoveMCParticlesModule.h:74
Belle2::Module::addParam
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:562
Belle2::RemoveMCParticlesModule::m_minR
double m_minR
Minimum Rphi value of particles to keep.
Definition: RemoveMCParticlesModule.h:72
Belle2::RemoveMCParticlesModule::initialize
virtual void initialize() override
Initializes the module.
Definition: RemoveMCParticlesModule.cc:79
Belle2::RemoveMCParticlesModule::m_maxR
double m_maxR
Maximum Rphi value of particles to keep.
Definition: RemoveMCParticlesModule.h:73
Belle2::MCParticle
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:43
Belle2::StoreArray< MCParticle >
Belle2::MCParticleGraph::clear
void clear()
Reset particles and decay information to make the class reusable.
Definition: MCParticleGraph.h:298
Belle2::StoreArray::getEntries
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:226
Belle2::RemoveMCParticlesModule::m_maxPt
double m_maxPt
Maximum Pt value of particles to keep.
Definition: RemoveMCParticlesModule.h:77
Belle2::RemoveMCParticlesModule::m_pdgCodes
std::vector< int > m_pdgCodes
List of pdgCodes wo apply cuts on.
Definition: RemoveMCParticlesModule.h:79
Belle2::RemoveMCParticlesModule::m_particleList
std::string m_particleList
Name of the MCParticle collection to work on.
Definition: RemoveMCParticlesModule.h:69