Belle II Software  release-05-02-19
RestOfEventBuilderModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2013 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Anze Zupanc, Sviatoslav Bilokin *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <analysis/modules/RestOfEventBuilder/RestOfEventBuilderModule.h>
12 
13 #include <analysis/dataobjects/ParticleList.h>
14 #include <analysis/dataobjects/Particle.h>
15 #include <mdst/dataobjects/MCParticle.h>
16 
17 #include <mdst/dataobjects/Track.h>
18 #include <mdst/dataobjects/ECLCluster.h>
19 #include <mdst/dataobjects/KLMCluster.h>
20 
21 #include <framework/datastore/StoreArray.h>
22 #include <framework/datastore/StoreObjPtr.h>
23 
24 #include <framework/logging/Logger.h>
25 
26 using namespace Belle2;
27 
28 //-----------------------------------------------------------------
29 // Register the Module
30 //-----------------------------------------------------------------
31 REG_MODULE(RestOfEventBuilder)
32 
33 //-----------------------------------------------------------------
34 // Implementation
35 //-----------------------------------------------------------------
36 
38 {
39  // Set module properties
40  setDescription("Creates for each Particle in the given ParticleList a RestOfEvent dataobject and makes BASF2 relation between them.");
41  setPropertyFlags(c_ParallelProcessingCertified);
42 
43  // Parameter definitions
44  std::vector<std::string> emptyList;
45  addParam("particleList", m_particleList, "Name of the ParticleList");
46  addParam("particleListsInput", m_particleListsInput, "List of the particle lists, which serve as a source of particles", emptyList);
47  addParam("createNestedROE", m_createNestedROE, "A switch to create nested ROE", false);
48  addParam("nestedROEMask", m_nestedMask, "A switch to create nested ROE", std::string(""));
49  addParam("fromMC", m_fromMC, "A switch to create MC ROE", false);
50  m_nestedROEArrayName = "NestedRestOfEvents";
51 }
52 
54 {
55  // input
57  StoreArray<Particle> particles;
58  particles.isRequired();
59 
60  // output
61  StoreArray<RestOfEvent> roeArray;
62  roeArray.registerInDataStore();
63  particles.registerRelationTo(roeArray);
64  if (m_createNestedROE) {
66  nestedROEArray.registerInDataStore();
67  particles.registerRelationTo(nestedROEArray);
68  roeArray.registerRelationTo(nestedROEArray);
69  }
70 }
71 
73 {
74  if (!m_createNestedROE) {
75  createROE();
76  } else {
78  }
79 
80 }
81 
83 {
84  // input target Particle
87  StoreObjPtr<RestOfEvent> hostROE("RestOfEvent");
88  if (!hostROE.isValid()) {
89  B2WARNING("ROE list is not valid somehow, nested ROE is not created!");
90  return;
91  }
92  auto outerROEParticles = hostROE->getParticles(m_nestedMask);
93  unsigned int nParticles = plist->getListSize();
94  for (unsigned i = 0; i < nParticles; i++) {
95  const Particle* particle = plist->getParticle(i);
96  // check if a Particle object is already related to a RestOfEvent object
97  auto* check_roe = particle->getRelated<RestOfEvent>();
98  if (check_roe != nullptr) {
99  return;
100  }
101  // create nested RestOfEvent object:
102  RestOfEvent* nestedROE = nestedROEArray.appendNew(particle->getPDGCode(), true);
103  // create relation: Particle <-> RestOfEvent
104  particle->addRelationTo(nestedROE);
105  // create relation: host ROE <-> nested ROE
106  hostROE->addRelationTo(nestedROE);
107  auto fsdaughters = particle->getFinalStateDaughters();
108  std::vector<const Particle* > particlesToAdd;
109  for (auto* outerROEParticle : outerROEParticles) {
110  bool toAdd = true;
111  for (auto* daughter : fsdaughters) {
112  if (outerROEParticle->isCopyOf(daughter, true)) {
113  toAdd = false;
114  break;
115  }
116  }
117  if (toAdd) {
118  particlesToAdd.push_back(outerROEParticle);
119  }
120  }
121  nestedROE->addParticles(particlesToAdd);
122  }
123 }
124 
126 {
127  // input target Particle
129 
130  // output
131  StoreArray<RestOfEvent> roeArray;
132  unsigned int nParts = plist->getListSize();
133  for (unsigned i = 0; i < nParts; i++) {
134  const Particle* particle = plist->getParticle(i);
135 
136  // check if a Particle object is already related to a RestOfEvent object
137  auto* check_roe = particle->getRelated<RestOfEvent>();
138  if (check_roe != nullptr)
139  return;
140 
141  // create RestOfEvent object
142  RestOfEvent* roe = roeArray.appendNew(particle->getPDGCode(), false, m_fromMC);
143 
144  // create relation: Particle <-> RestOfEvent
145  particle->addRelationTo(roe);
146 
147  // fill RestOfEvent with content
148  addRemainingParticles(particle, roe);
149  }
150 }
151 
153 {
154  StoreArray<Particle> particlesArray;
155  auto fsdaughters = particle->getFinalStateDaughters();
156  int nParticleLists = m_particleListsInput.size();
157  B2DEBUG(10, "Particle has " + std::to_string(fsdaughters.size()) + " daughters");
158  for (auto* daughter : fsdaughters) {
159  B2DEBUG(10, "\t" << daughter->getArrayIndex() << ": pdg " << daughter->getPDGCode());
160  B2DEBUG(10, "\t\t Store array particle: " << particlesArray[daughter->getArrayIndex()]->getPDGCode());
161  }
162  unsigned int nExcludedParticles = 0;
163  std::vector<const Particle* > particlesToAdd;
164  B2DEBUG(10, "nLists: " << nParticleLists);
165  for (int i_pl = 0; i_pl != nParticleLists; ++i_pl) {
166 
167  std::string particleListName = m_particleListsInput[i_pl];
168  B2DEBUG(10, "ParticleList: " << particleListName);
169  StoreObjPtr<ParticleList> plist(particleListName);
170  int m_part = plist->getListSize();
171  for (int i = 0; i < m_part; i++) {
172  Particle* storedParticle = plist->getParticle(i);
173 
174  std::vector<const Particle*> storedParticleDaughters = storedParticle->getFinalStateDaughters();
175  for (auto* storedParticleDaughter : storedParticleDaughters) {
176  bool toAdd = true;
177  if ((m_fromMC and storedParticleDaughter->getParticleSource() != Particle::EParticleSourceObject::c_MCParticle)
178  or (!m_fromMC and storedParticleDaughter->getParticleSource() == Particle::EParticleSourceObject::c_MCParticle)) {
179  B2FATAL("The value of fromMC parameter is not consisted with the type of provided particles, MC vs Reco");
180  }
181  // Remove non primary MCParticles
182  if (m_fromMC and !storedParticleDaughter->getMCParticle()->hasStatus(MCParticle::c_PrimaryParticle)) {
183  nExcludedParticles++;
184  continue;
185  }
186  for (auto* daughter : fsdaughters) {
187  if (storedParticleDaughter->isCopyOf(daughter, true)) {
188  B2DEBUG(10, "Ignoring Particle with PDG " << daughter->getPDGCode() << " index " <<
189  storedParticleDaughter->getArrayIndex() << " to " << daughter->getArrayIndex());
190  B2DEBUG(10, "Is copy " << storedParticleDaughter->isCopyOf(daughter));
191  toAdd = false;
192  nExcludedParticles++;
193  break;
194  }
195  }
196  if (toAdd) {
197  particlesToAdd.push_back(storedParticleDaughter);
198  }
199  }
200  }
201  }
202  if (fsdaughters.size() > nExcludedParticles) {
203  B2WARNING("Number of excluded particles do not coincide with the number of target FSP daughters! Provided lists must be incomplete");
204  }
205  roe->addParticles(particlesToAdd);
206 }
208 {
209  StoreArray<ECLCluster> eclClusters;
210  StoreArray<KLMCluster> klmClusters;
211  StoreArray<Track> tracks;
212 
213  B2INFO("[RestOfEventBuilderModule] *** Print Event ***");
214  B2INFO("[RestOfEventBuilderModule] Tracks: " << tracks.getEntries());
215  for (int i = 0; i < tracks.getEntries(); i++) {
216  const Track* track = tracks[i];
217  const ECLCluster* trackECLCluster = track->getRelated<ECLCluster>();
218  const KLMCluster* trackKLMCluster = track->getRelated<KLMCluster>();
219  if (trackECLCluster) {
220  B2INFO("[RestOfEventBuilderModule] -> track " << track->getArrayIndex() << " -> ECLCluster " << trackECLCluster->getArrayIndex());
221  } else {
222  B2INFO("[RestOfEventBuilderModule] -> track " << track->getArrayIndex() << " -> ECLCluster (NO RELATION)");
223  }
224  if (trackKLMCluster) {
225  B2INFO("[RestOfEventBuilderModule] -> track " << track->getArrayIndex() << " -> KLMCluster " << trackKLMCluster->getArrayIndex());
226  } else {
227  B2INFO("[RestOfEventBuilderModule] -> track " << track->getArrayIndex() << " -> KLMCluster (NO RELATION)");
228  }
229  }
230 
231  B2INFO("[RestOfEventBuilderModule] ECLCluster: " << eclClusters.getEntries());
232  for (int i = 0; i < eclClusters.getEntries(); i++) {
233  const ECLCluster* eclCluster = eclClusters[i];
234 
235  B2INFO("[RestOfEventBuilderModule] -> cluster " << eclCluster->getArrayIndex());
236  }
237 
238  B2INFO("[RestOfEventBuilderModule] KLMCluster: " << klmClusters.getEntries());
239  for (int i = 0; i < klmClusters.getEntries(); i++) {
240  const KLMCluster* klmCluster = klmClusters[i];
241 
242  B2INFO("[RestOfEventBuilderModule] -> cluster " << klmCluster->getArrayIndex());
243  }
244 }
245 
247 {
248  std::vector<int> eclFSPs = particle->getMdstArrayIndices(Particle::EParticleSourceObject::c_ECLCluster);
249  std::vector<int> klmFSPs = particle->getMdstArrayIndices(Particle::EParticleSourceObject::c_KLMCluster);
250  std::vector<int> trackFSPs = particle->getMdstArrayIndices(Particle::EParticleSourceObject::c_Track);
251 
252  B2INFO("[RestOfEventBuilderModule] tracks : ");
253 
254  std::string printout;
255  for (int trackFSP : trackFSPs)
256  printout += std::to_string(trackFSP) + " ";
257  B2INFO(printout);
258 
259  printout.clear();
260 
261  B2INFO("[RestOfEventBuilderModule] eclFSPs : ");
262  for (int eclFSP : eclFSPs)
263  printout += std::to_string(eclFSP) + " ";
264  B2INFO(printout);
265 
266  printout.clear();
267 
268  B2INFO("[RestOfEventBuilderModule] klmFSPs : ");
269  for (int klmFSP : klmFSPs)
270  printout += std::to_string(klmFSP) + " ";
271  B2INFO(printout);
272 
273 }
Belle2::StoreArray::appendNew
T * appendNew()
Construct a new T object at the end of the array.
Definition: StoreArray.h:256
Belle2::StoreArray::registerRelationTo
bool registerRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut, const std::string &namedRelation="") const
Register a relation to the given StoreArray.
Definition: StoreArray.h:150
Belle2::ECLCluster
ECL cluster data.
Definition: ECLCluster.h:39
Belle2::RestOfEventBuilderModule::createROE
void createROE()
create usual (host) ROE
Definition: RestOfEventBuilderModule.cc:125
Belle2::RestOfEventBuilderModule::addRemainingParticles
void addRemainingParticles(const Particle *particle, RestOfEvent *roe)
Adds all particles from input particle lists that are not used in reconstruction of given particle.
Definition: RestOfEventBuilderModule.cc:152
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::RestOfEventBuilderModule::initialize
virtual void initialize() override
Overriden initialize method.
Definition: RestOfEventBuilderModule.cc:53
Belle2::RestOfEvent
This is a general purpose class for collecting reconstructed MDST data objects that are not used in r...
Definition: RestOfEvent.h:59
Belle2::RestOfEventBuilderModule::printEvent
void printEvent()
for debugging purposes
Definition: RestOfEventBuilderModule.cc:207
Belle2::RestOfEventBuilderModule::m_fromMC
bool m_fromMC
Should we create MC ROE?
Definition: RestOfEventBuilderModule.h:69
Belle2::RestOfEventBuilderModule::createNestedROE
void createNestedROE()
create nested ROE
Definition: RestOfEventBuilderModule.cc:82
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::Particle::getFinalStateDaughters
std::vector< const Belle2::Particle * > getFinalStateDaughters() const
Returns a vector of pointers to Final State daughter particles.
Definition: Particle.cc:613
Belle2::RestOfEventBuilderModule::event
virtual void event() override
Overriden event method.
Definition: RestOfEventBuilderModule.cc:72
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::RelationsInterface::getArrayIndex
int getArrayIndex() const
Returns this object's array index (in StoreArray), or -1 if not found.
Definition: RelationsObject.h:387
Belle2::KLMCluster
KLM cluster data.
Definition: KLMCluster.h:38
Belle2::Particle
Class to store reconstructed particles.
Definition: Particle.h:77
Belle2::RestOfEventBuilderModule::m_nestedROEArrayName
std::string m_nestedROEArrayName
Name of the nested ROE.
Definition: RestOfEventBuilderModule.h:67
Belle2::RestOfEventBuilderModule::m_particleListsInput
std::vector< std::string > m_particleListsInput
Name of the input particle lists of pi+ gamma and Klongs.
Definition: RestOfEventBuilderModule.h:66
Belle2::RestOfEventBuilderModule::m_particleList
std::string m_particleList
Name of the ParticleList.
Definition: RestOfEventBuilderModule.h:64
Belle2::Track
Class that bundles various TrackFitResults.
Definition: Track.h:35
Belle2::StoreArray< Particle >
Belle2::RestOfEvent::addParticles
void addParticles(const std::vector< const Particle * > &particle)
Add StoreArray indices of given Particles to the list of unused particles in the event.
Definition: RestOfEvent.cc:27
Belle2::RestOfEventBuilderModule::m_createNestedROE
bool m_createNestedROE
Should we create nested ROE?
Definition: RestOfEventBuilderModule.h:68
Belle2::RestOfEventBuilderModule
Creates for each Particle in given ParticleList an RestOfEvent (ROE) dataobject and makes BASF2 relat...
Definition: RestOfEventBuilderModule.h:44
Belle2::StoreArray::getEntries
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:226
Belle2::RestOfEventBuilderModule::printParticle
void printParticle(const Particle *particle)
for debugging purposes
Definition: RestOfEventBuilderModule.cc:246
Belle2::MCParticle::c_PrimaryParticle
@ c_PrimaryParticle
bit 0: Particle is primary particle.
Definition: MCParticle.h:58
Belle2::RestOfEventBuilderModule::m_nestedMask
std::string m_nestedMask
Name of the ParticleList.
Definition: RestOfEventBuilderModule.h:65
Belle2::StoreObjPtr::isValid
bool isValid() const
Check whether the object was created.
Definition: StoreObjPtr.h:120