Belle II Software  release-08-01-10
UdstListFilterModule.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/UdstListFilter/UdstListFilterModule.h>
10 // framework aux
11 #include <framework/logging/Logger.h>
12 #include <unordered_set>
13 
14 using namespace Belle2;
15 
16 //-----------------------------------------------------------------
17 // Register the Module
18 //-----------------------------------------------------------------
19 REG_MODULE(UdstListFilter)
20 
21 
24 void storeRelatedToTrack(const Track* p,
25  std::map < Particle::EParticleSourceObject, std::unordered_set<unsigned int>>& indicesToKeep)
26 {
27  for (const ECLCluster& cluster : p->getRelationsTo<ECLCluster>()) {
28  indicesToKeep[Particle::EParticleSourceObject::c_ECLCluster].insert(cluster.getArrayIndex());
29  };
30  for (const KLMCluster& cluster : p->getRelationsTo<KLMCluster>()) {
31  indicesToKeep[Particle::EParticleSourceObject::c_KLMCluster].insert(cluster.getArrayIndex());
32  };
33 }
34 
35 
39 void keepObject(const Particle* p, std::map < Particle::EParticleSourceObject, std::unordered_set<unsigned int>>& indicesToKeep)
40 {
41  auto source = p->getParticleSource();
42  if (source == Particle::EParticleSourceObject::c_Track ||
43  source == Particle::EParticleSourceObject::c_KLMCluster ||
44  source == Particle::EParticleSourceObject::c_ECLCluster ||
45  source == Particle::EParticleSourceObject::c_V0) {
46  unsigned mdstIndex = p->getMdstArrayIndex();
47  indicesToKeep[source].insert(mdstIndex);
48  // trace relations:
49  if (source == Particle::EParticleSourceObject::c_Track) {
50  storeRelatedToTrack(p->getTrack(), indicesToKeep);
51  }
52  }
53 
54 
55  unsigned int n = p->getNDaughters();
56  for (unsigned int i = 0; i < n; i++) {
57  keepObject(p->getDaughter(i), indicesToKeep);
58  }
59 }
60 
61 //-----------------------------------------------------------------
62 // Implementation
63 //-----------------------------------------------------------------
64 
66 {
67  setDescription("Filter udst file content based on a particle list. As a result of the module, all object which are (not) associated with the list are removed. The module is used for signal embedding.");
68  addParam("listName", m_listName, "name of particle list.", std::string(""));
69  addParam("keepNotInList", m_reverse,
70  "if true, keep mdst objects which are not used in the particle list, otherwise keep objects which are used in the list.", false);
71 }
72 
74 {
75  m_plist.isRequired(m_listName);
76 
77  m_tracks.isRequired();
78  m_track_selector.registerSubset(m_tracks, "myudst_tracks");
79  m_track_selector.inheritAllRelations();
80 
81  m_eclclusters.isRequired();
82  m_ecl_selector.registerSubset(m_eclclusters, DataStore::EStoreFlags::c_WriteOut);
83 
84  m_klmclusters.isRequired();
85  m_klm_selector.registerSubset(m_klmclusters);
86 
87  m_v0s.isRequired();
88  m_v0_selector.registerSubset(m_v0s);
89 
90  m_selectedV0s = new StoreArray<V0>("myudst_V0s");
92 
93 }
94 
96 {
97  std::map < Particle::EParticleSourceObject, std::unordered_set<unsigned int>> mdstIndices;
98  // Select objects to keep/remove:
99  for (auto& p : *m_plist) {
100  keepObject(&p, mdstIndices);
101  }
102 
103  for (const auto& mI : mdstIndices) {
104  // for (const auto& [source, indices] : mdstIndices) {
105 
106  const auto& source = mI.first;
107  const auto& indices = mI.second;
108 
109  // Selector
110  auto selector = [indices, this](const RelationsObject * p) -> bool {
111  int idx = p->getArrayIndex();
112  return (indices.count(idx) == 1)^ m_reverse;
113  };
114 
115  // Do removal
116  if (source == Particle::EParticleSourceObject::c_Track) {
117  m_track_selector.select(selector);
118  // Update V0s...
119  // Also overwrite the original tracks:
120  swapV0s();
121  m_track_selector.swapSetsAndDestroyOriginal();
122  }
123 
124  else if (source == Particle::EParticleSourceObject::c_V0) {
125  // already dealt with
126  }
127 
128  else if (source == Particle::EParticleSourceObject::c_ECLCluster) {
129  m_ecl_selector.select(selector);
130  }
131 
132  else if (source == Particle::EParticleSourceObject::c_KLMCluster) {
133  m_klm_selector.select(selector);
134  }
135  }
136 }
137 
139 {
140  for (auto& v0 : m_v0s) {
141  const auto tracks = v0.getTracks();
142  const auto fitResults = v0.getTrackFitResults();
143  const Track* r1 = tracks.first->getRelated<Track>("myudst_tracks");
144  const Track* r2 = tracks.second->getRelated<Track>("myudst_tracks");
145  if ((r1 != nullptr) and (r2 != nullptr)) {
146  // New V0 with new track relations:
147  m_selectedV0s->appendNew(std::make_pair(r1, fitResults.first), std::make_pair(r2, fitResults.second),
148  v0.getFittedVertexX(), v0.getFittedVertexY(), v0.getFittedVertexZ());
149  }
150  }
151 
154  if (!fromEntry->ptr) {
155  if (toEntry->isArray)
156  toEntry->getPtrAsArray()->Delete(); // This seems to be needed too.
157  toEntry->ptr = nullptr;
158  } else {
160  }
161 }
@ c_ErrorIfAlreadyRegistered
If the object/array was already registered, produce an error (aborting initialisation).
Definition: DataStore.h:72
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
StoreEntry * getEntry(const StoreAccessorBase &accessor)
Check whether an entry with the correct type is registered in the DataStore map and return it.
Definition: DataStore.cc:294
void replaceData(const StoreAccessorBase &from, const StoreAccessorBase &to)
For two StoreAccessors of same type, move all data in 'from' into 'to', discarding previous contents ...
Definition: DataStore.cc:343
ECL cluster data.
Definition: ECLCluster.h:27
KLM cluster data.
Definition: KLMCluster.h:28
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
Class to store reconstructed particles.
Definition: Particle.h:75
EParticleSourceObject
particle source enumerators
Definition: Particle.h:82
Defines interface for accessing relations of objects in StoreArray.
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
Class that bundles various TrackFitResults.
Definition: Track.h:25
StoreArray< V0 > * m_selectedV0s
New selected V0 array.
void swapV0s()
Update V0 mdst datastore object.
virtual void initialize() override
Initialize the Module.
StoreArray< KLMCluster > m_klmclusters
StoreArray of KLMCluster.
StoreArray< V0 > m_v0s
StoreArray of V0s.
virtual void event() override
Event processor.
std::string m_listName
Breco particle list name.
SelectSubset< Track > m_track_selector
Selector of sub-set of tracks.
SelectSubset< KLMCluster > m_klm_selector
Selector of sub-set of KLM clusters.
StoreArray< Track > m_tracks
StoreArray of Tracks.
SelectSubset< ECLCluster > m_ecl_selector
Selector of sub-set of ECL clusters.
SelectSubset< V0 > m_v0_selector
Selector of sub-set of V0s.
StoreArray< ECLCluster > m_eclclusters
StoreArray of ECLCluster.
StoreObjPtr< ParticleList > m_plist
list used for cleaning
bool m_reverse
Reverse selection: keep objects NOT mentioned in the list.
REG_MODULE(arichBtest)
Register the Module.
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
Abstract base class for different kinds of events.
Wraps a stored array/object, stored under unique (name, durability) key.
Definition: StoreEntry.h:22
TObject * ptr
The pointer to the returned object, either equal to 'object' or null, depending on wether the object ...
Definition: StoreEntry.h:51
TClonesArray * getPtrAsArray() const
Return ptr cast to TClonesArray.
Definition: StoreEntry.cc:83
bool isArray
Flag that indicates whether the object is a TClonesArray.
Definition: StoreEntry.h:39