Belle II Software  release-06-00-14
RestOfEventUpdaterModule.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 <analysis/modules/RestOfEventUpdater/RestOfEventUpdaterModule.h>
10 
11 #include <framework/datastore/StoreArray.h>
12 #include <framework/datastore/StoreObjPtr.h> //
13 
14 #include <framework/logging/Logger.h>
15 
16 #include <iostream>
17 
18 using namespace std;
19 
20 namespace Belle2 {
26  //-----------------------------------------------------------------
27  // Register the Module
28  //-----------------------------------------------------------------
29  REG_MODULE(RestOfEventUpdater)
30 
31  //-----------------------------------------------------------------
32  // Implementation
33  //-----------------------------------------------------------------
34 
36  {
37  // Set module properties
38  setDescription("Updates an existing mask (map of boolean values) for tracks or eclClusters in RestOfEvent with an available property (e.g. after performing training).");
39  setPropertyFlags(c_ParallelProcessingCertified);
40 
41  // Add parameters
42  std::vector<std::string> emptyMaskVector;
43  std::string emptyCutString;
44 
45  addParam("particleList", m_inputListName, "Name of the ParticleList which contains information that will be used for updating");
46  addParam("updateMasks", m_maskNamesForUpdating, "List of all mask names which will be updated", emptyMaskVector);
47  addParam("cutString", m_selection, "Cut string which will be used for updating masks", emptyCutString);
48  addParam("discard", m_discard,
49  "Update the ROE mask by passing or discarding particles in the provided particle list, default is to pass", false);
50  }
51 
52  void RestOfEventUpdaterModule::initialize()
53  {
55  m_inputList.isRequired(m_inputListName);
56 
57  m_cut = Variable::Cut::compile(m_selection);
58 
59  B2INFO("RestOfEventUpdater updated track/eclCluster ROEMask(s) with infoList: " << m_inputListName << " and cut: " << m_selection);
60  }
61 
62  void RestOfEventUpdaterModule::event()
63  {
64  if (!m_inputList) {
65  B2WARNING("Input list " << m_inputList.getName() << " was not created?");
66  return;
67  }
68  StoreObjPtr<RestOfEvent> roe("RestOfEvent");
69  if (!roe.isValid()) {
70  B2WARNING("ROE list is not valid somehow, ROE masks are not updated!");
71  return;
72  }
73  std::set<Particle::EParticleSourceObject> encounteredSources;
74  // Particle lists can contain Particles from different mdst sources
75  // Thus, we split them based on their mdst source
76  // Only particles surviving the provided cut are considered
77  std::vector<const Particle*> particlesFromTracksToUpdate;
78  std::vector<const Particle*> particlesFromECLClustersToUpdate;
79  std::vector<const Particle*> particlesFromKLMClustersToUpdate;
80  std::vector<const Particle*> compositeParticlesToUpdate;
81  for (unsigned j = 0; j < m_inputList->getListSize(); j++) {
82  const Particle* partWithInfo = m_inputList->getParticle(j);
83  Particle::EParticleSourceObject mdstSource = partWithInfo->getParticleSource();
84  encounteredSources.insert(mdstSource);
85  if (m_cut->check(partWithInfo)) {
86  if (mdstSource == Particle::EParticleSourceObject::c_Track) {
87  particlesFromTracksToUpdate.push_back(partWithInfo);
88  } else if (mdstSource == Particle::EParticleSourceObject::c_ECLCluster) {
89  particlesFromECLClustersToUpdate.push_back(partWithInfo);
90  } else if (mdstSource == Particle::EParticleSourceObject::c_KLMCluster) {
91  particlesFromKLMClustersToUpdate.push_back(partWithInfo);
92  } else if (mdstSource == Particle::EParticleSourceObject::c_Composite or
93  mdstSource == Particle::EParticleSourceObject::c_V0) {
94  compositeParticlesToUpdate.push_back(partWithInfo);
95  }
96  }
97  }
98  if (encounteredSources.count(Particle::EParticleSourceObject::c_Track) > 0) {
99  updateMasksWithParticles(roe, particlesFromTracksToUpdate, Particle::EParticleSourceObject::c_Track);
100  } else { // If we have a track-based particle in the particle list there can not be any other mdst source
101  if (encounteredSources.count(Particle::EParticleSourceObject::c_ECLCluster) > 0) {
102  updateMasksWithParticles(roe, particlesFromECLClustersToUpdate, Particle::EParticleSourceObject::c_ECLCluster);
103  }
104  if (encounteredSources.count(Particle::EParticleSourceObject::c_KLMCluster) > 0) {
105  updateMasksWithParticles(roe, particlesFromKLMClustersToUpdate, Particle::EParticleSourceObject::c_KLMCluster);
106  }
107  updateMasksWithV0(roe, compositeParticlesToUpdate); // in updateMasksWithV0 it is checked whether the vector is empty
108  }
109  }
110 
111  void RestOfEventUpdaterModule::updateMasksWithV0(const StoreObjPtr<RestOfEvent>& roe,
112  std::vector<const Particle*>& particlesToUpdate)
113  {
114  if (particlesToUpdate.size() == 0) {
115  B2DEBUG(10, "No particles in list provided, nothing to do");
116  return;
117  }
118  for (auto& maskToUpdate : m_maskNamesForUpdating) {
119  if (maskToUpdate == "") {
120  B2FATAL("Cannot update ROE mask with no name!");
121  }
122  for (auto* particleV0 : particlesToUpdate) {
123  if (!roe->checkCompatibilityOfMaskAndV0(maskToUpdate, particleV0)) {
124  continue;
125  }
126  roe->updateMaskWithV0(maskToUpdate, particleV0);
127  }
128  }
129  }
130 
131  void RestOfEventUpdaterModule::updateMasksWithParticles(const StoreObjPtr<RestOfEvent>& roe,
132  std::vector<const Particle*>& particlesToUpdate, Particle::EParticleSourceObject listType)
133  {
134  for (auto& maskToUpdate : m_maskNamesForUpdating) {
135  if (maskToUpdate == "") {
136  B2FATAL("Cannot update ROE mask with no name!");
137  }
138  if (!roe->hasMask(maskToUpdate)) {
139  // Change name to get all ROE particles in case of new mask
140  roe->initializeMask(maskToUpdate, "ROEUpdaterModule");
141  }
142  roe->excludeParticlesFromMask(maskToUpdate, particlesToUpdate, listType, m_discard);
143 
144  }
145  }
147 }
Base class for Modules.
Definition: Module.h:72
Class to store reconstructed particles.
Definition: Particle.h:74
EParticleSourceObject
particle source enumerators
Definition: Particle.h:81
EParticleSourceObject getParticleSource() const
Returns particle source as defined with enum EParticleSourceObject.
Definition: Particle.h:416
Updates an existing mask (map of boolean values) for tracks or eclClusters in RestOfEvent with an ava...
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:95
bool isValid() const
Check whether the object was created.
Definition: StoreObjPtr.h:110
#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.