Belle II Software development
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#include <unordered_set>
11
12using namespace Belle2;
13
14//-----------------------------------------------------------------
15// Register the Module
16//-----------------------------------------------------------------
17REG_MODULE(UdstListFilter)
18
19
22void storeRelatedToTrack(const Track* p,
23 std::map < Particle::EParticleSourceObject, std::unordered_set<unsigned int>>& indicesToKeep)
24{
25 for (const ECLCluster& cluster : p->getRelationsTo<ECLCluster>()) {
26 indicesToKeep[Particle::EParticleSourceObject::c_ECLCluster].insert(cluster.getArrayIndex());
27 };
28 for (const KLMCluster& cluster : p->getRelationsTo<KLMCluster>()) {
29 indicesToKeep[Particle::EParticleSourceObject::c_KLMCluster].insert(cluster.getArrayIndex());
30 };
31}
32
33
37void keepObject(const Particle* p, std::map < Particle::EParticleSourceObject, std::unordered_set<unsigned int>>& indicesToKeep)
38{
39 auto source = p->getParticleSource();
40 if (source == Particle::EParticleSourceObject::c_Track ||
41 source == Particle::EParticleSourceObject::c_KLMCluster ||
42 source == Particle::EParticleSourceObject::c_ECLCluster ||
43 source == Particle::EParticleSourceObject::c_V0) {
44 unsigned mdstIndex = p->getMdstArrayIndex();
45 indicesToKeep[source].insert(mdstIndex);
46 // trace relations:
47 if (source == Particle::EParticleSourceObject::c_Track) {
48 storeRelatedToTrack(p->getTrack(), indicesToKeep);
49 }
50 }
51
52
53 unsigned int n = p->getNDaughters();
54 for (unsigned int i = 0; i < n; i++) {
55 keepObject(p->getDaughter(i), indicesToKeep);
56 }
57}
58
59//-----------------------------------------------------------------
60// Implementation
61//-----------------------------------------------------------------
62
64{
65 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.");
67 addParam("listName", m_listName, "name of particle list.", std::string(""));
68 addParam("keepNotInList", m_reverse,
69 "if true, keep mdst objects which are not used in the particle list, otherwise keep objects which are used in the list.", false);
70}
71
73{
74 m_plist.isRequired(m_listName);
75
76 m_tracks.isRequired();
77 m_track_selector.registerSubset(m_tracks, "myudst_tracks");
78 m_track_selector.inheritAllRelations();
79
80 m_eclclusters.isRequired();
82
83 m_klmclusters.isRequired();
84 m_klm_selector.registerSubset(m_klmclusters);
85
86 m_v0s.isRequired();
87 m_v0_selector.registerSubset(m_v0s);
88
89 m_selectedV0s = new StoreArray<V0>("myudst_V0s");
91
92}
93
95{
96 std::map < Particle::EParticleSourceObject, std::unordered_set<unsigned int>> mdstIndices;
97 // Select objects to keep/remove:
98 for (auto& p : *m_plist) {
99 keepObject(&p, mdstIndices);
100 }
101
102 for (const auto& mI : mdstIndices) {
103 // for (const auto& [source, indices] : mdstIndices) {
104
105 const auto& source = mI.first;
106 const auto& indices = mI.second;
107
108 // Selector
109 auto selector = [indices, this](const RelationsObject * p) -> bool {
110 int idx = p->getArrayIndex();
111 return (indices.count(idx) == 1)^ m_reverse;
112 };
113
114 // Do removal
115 if (source == Particle::EParticleSourceObject::c_Track) {
116 m_track_selector.select(selector);
117 // Update V0s...
118 // Also overwrite the original tracks:
119 swapV0s();
120 m_track_selector.swapSetsAndDestroyOriginal();
121 }
122
123 else if (source == Particle::EParticleSourceObject::c_V0) {
124 // already dealt with
125 }
126
127 else if (source == Particle::EParticleSourceObject::c_ECLCluster) {
128 m_ecl_selector.select(selector);
129 }
130
131 else if (source == Particle::EParticleSourceObject::c_KLMCluster) {
132 m_klm_selector.select(selector);
133 }
134 }
135}
136
138{
139 for (auto& v0 : m_v0s) {
140 const auto tracks = v0.getTracks();
141 const auto fitResults = v0.getTrackFitResults();
142 const Track* r1 = tracks.first->getRelated<Track>("myudst_tracks");
143 const Track* r2 = tracks.second->getRelated<Track>("myudst_tracks");
144 if ((r1 != nullptr) and (r2 != nullptr)) {
145 // New V0 with new track relations:
146 m_selectedV0s->appendNew(std::make_pair(r1, fitResults.first), std::make_pair(r2, fitResults.second),
147 v0.getFittedVertexX(), v0.getFittedVertexY(), v0.getFittedVertexZ());
148 }
149 }
150
153 if (!fromEntry->ptr) {
154 if (toEntry->isArray)
155 toEntry->getPtrAsArray()->Delete(); // This seems to be needed too.
156 toEntry->ptr = nullptr;
157 } else {
159 }
160}
@ c_WriteOut
Object/array should be saved by output modules.
Definition: DataStore.h:70
@ 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:53
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:293
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:342
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
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
Class to store reconstructed particles.
Definition: Particle.h:76
EParticleSourceObject
particle source enumerators
Definition: Particle.h:83
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.
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:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:649
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 whether 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