Belle II Software  release-08-01-10
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 header.
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 using namespace Belle2;
20 
21 //-----------------------------------------------------------------
22 // Register module
23 //-----------------------------------------------------------------
24 
25 REG_MODULE(ParticleSelector);
26 
27 //-----------------------------------------------------------------
28 // Implementation
29 //-----------------------------------------------------------------
30 
31 ParticleSelectorModule::ParticleSelectorModule() : Module()
32 {
33  setDescription("Removes Particles from given ParticleList that do not pass specified selection criteria.");
34 
36 
37  addParam("decayString", m_decayString,
38  "Input ParticleList name (see :ref:`DecayString`).");
39 
40  addParam("cut", m_cutParameter,
41  "Selection criteria to be applied, see `cut_strings_selections`",
42  std::string(""));
43 }
44 
46 {
47  // obtain the input and output particle lists from the decay string
48  bool valid = m_decaydescriptor.init(m_decayString);
49  if (!valid)
50  B2ERROR("ParticleSelectorModule::initialize Invalid input DecayString: " << m_decayString);
51 
52  int nProducts = m_decaydescriptor.getNDaughters();
53  if (nProducts > 0)
54  B2ERROR("ParticleSelectorModule::initialize Invalid input DecayString " << m_decayString
55  << ". DecayString should not contain any daughters, only the mother particle.");
56 
57  // Mother particle
59 
60  const int pdgCode = mother->getPDGCode();
61  string listLabel = mother->getLabel();
62  m_listName = mother->getFullName();
63  // Some labels are reserved for the particle loader which loads all particles of the corresponding type.
64  // If people applied cuts on these particle lists, very dangerous bugs could be introduced.
65  // An exception is made for the gamma:all list. This can be limited to photons from the ECL only.
66  if (Const::finalStateParticlesSet.contains(Const::ParticleType(abs(pdgCode))) and listLabel == "all") {
67  if (abs(pdgCode) == Const::photon.getPDGCode() and m_cutParameter == "isFromECL")
69  else
70  B2FATAL("You are trying to apply a cut on the list " << m_listName <<
71  " but the label 'all' is protected for lists of final-state particles." <<
72  " It could introduce *very* dangerous bugs.");
73  } else if (listLabel == "V0") {
74  // the label V0 is also protected
75  B2FATAL("You are trying to apply a cut on the list " << m_listName <<
76  " but the label " << listLabel << " is protected and can not be reduced.");
77  }
78 
79  m_particleList.isRequired(m_listName);
80 
82 
83  B2INFO("ParticleSelector: " << m_listName);
84  B2INFO(" -> With cuts : " << m_cutParameter);
85 }
86 
88 {
90  m_particleList->setEditable(true);
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  m_particleList->setEditable(false);
105 }
The ParticleType class for identifying different particle types.
Definition: Const.h:399
static const ParticleSet finalStateParticlesSet
set of final set particles that can be created by the ParticleLoader
Definition: Const.h:648
static const ParticleType photon
photon particle
Definition: Const.h:664
Represents a particle in the DecayDescriptor.
bool init(const std::string &str)
Initialise the DecayDescriptor from given string.
const DecayDescriptorParticle * getMother() const
return mother.
int getNDaughters() const
return number of direct daughters.
static std::unique_ptr< GeneralCut > compile(const std::string &cut)
Creates an instance of a cut and returns a unique_ptr to it, if you need a copy-able object instead y...
Definition: GeneralCut.h:84
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 setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:80
virtual void initialize() override
Initialize the Module.
virtual void event() override
Event processor.
std::string m_decayString
Input DecayString specifying the particle being selected.
std::string m_listName
output particle list name
std::unique_ptr< Variable::Cut > m_cut
cut object which performs the cuts
bool m_exceptionForGammaAll
true if the particleList is gamma:all and cut is isFromECL
std::string m_cutParameter
selection criteria
DecayDescriptor m_decaydescriptor
Decay descriptor of the particle being selected.
StoreObjPtr< ParticleList > m_particleList
particle list
Class to store reconstructed particles.
Definition: Particle.h:75
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.