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