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