Belle II Software  release-06-01-15
ParticleSelectorModule.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/ParticleSelector/ParticleSelectorModule.h>
11 
12 // framework aux
13 #include <framework/logging/Logger.h>
14 
15 // dataobjects
16 #include <analysis/dataobjects/Particle.h>
17 
18 using namespace std;
19 
20 namespace Belle2 {
26  //-----------------------------------------------------------------
27  // Register module
28  //-----------------------------------------------------------------
29 
30  REG_MODULE(ParticleSelector)
31 
32  //-----------------------------------------------------------------
33  // Implementation
34  //-----------------------------------------------------------------
35 
37  {
38  setDescription("Removes Particles from given ParticleList that do not pass specified selection criteria.");
39 
40  setPropertyFlags(c_ParallelProcessingCertified);
41 
42  addParam("decayString", m_decayString,
43  "Input ParticleList name (see :ref:`DecayString`).");
44 
45  addParam("cut", m_cutParameter,
46  "Selection criteria to be applied, see `cut_strings_selections`",
47  std::string(""));
48  }
49 
50  void ParticleSelectorModule::initialize()
51  {
52  // obtain the input and output particle lists from the decay string
53  bool valid = m_decaydescriptor.init(m_decayString);
54  if (!valid)
55  B2ERROR("ParticleSelectorModule::initialize Invalid input DecayString: " << m_decayString);
56 
57  int nProducts = m_decaydescriptor.getNDaughters();
58  if (nProducts > 0)
59  B2ERROR("ParticleSelectorModule::initialize Invalid input DecayString " << m_decayString
60  << ". DecayString should not contain any daughters, only the mother particle.");
61 
62  // Mother particle
63  const DecayDescriptorParticle* mother = m_decaydescriptor.getMother();
64 
65  const int pdgCode = mother->getPDGCode();
66  string listLabel = mother->getLabel();
67  m_listName = mother->getFullName();
68  // Some labels are reserved for the particle loader which loads all particles of the corresponding type.
69  // If people applied cuts on these particle lists, very dangerous bugs could be introduced.
70  // An exception is made for the gamma:all list. This can be limited to photons from the ECL only.
71  if (Const::finalStateParticlesSet.contains(Const::ParticleType(abs(pdgCode))) and listLabel == "all"
72  and not(abs(pdgCode) == Const::photon.getPDGCode() and m_cutParameter == "isFromECL")) {
73  B2FATAL("You are trying to apply a cut on the list " << m_listName <<
74  " but the label 'all' is protected for lists of final-state particles." <<
75  " It could introduce *very* dangerous bugs.");
76  } else if (listLabel == "MC" or listLabel == "V0") {
77  // the labels MC and V0 are also protected
78  B2FATAL("You are trying to apply a cut on the list " << m_listName <<
79  " but the label " << listLabel << " is protected and can not be reduced.");
80  }
81 
82  m_particleList.isRequired(m_listName);
83 
84  m_cut = Variable::Cut::compile(m_cutParameter);
85 
86  B2INFO("ParticleSelector: " << m_listName);
87  B2INFO(" -> With cuts : " << m_cutParameter);
88  }
89 
90  void ParticleSelectorModule::event()
91  {
92  // loop over list only if cuts should be applied
93  if (!m_cutParameter.empty()) {
94  std::vector<unsigned int> toRemove;
95  unsigned int n = m_particleList->getListSize();
96  for (unsigned i = 0; i < n; i++) {
97  const Particle* part = m_particleList->getParticle(i);
98  if (!m_cut->check(part)) toRemove.push_back(part->getArrayIndex());
99  }
100  m_particleList->removeParticles(toRemove);
101  }
102  }
104 } // end Belle2 namespace
105 
The ParticleType class for identifying different particle types.
Definition: Const.h:289
Represents a particle in the DecayDescriptor.
Base class for Modules.
Definition: Module.h:72
Loops over all Particles in the ParticleList and removes those Particles from the ParticleList that d...
Class to store reconstructed particles.
Definition: Particle.h:74
#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.