Belle II Software  release-08-01-10
SVDShaperDigitSorterModule.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 <svd/modules/svdReconstruction/SVDShaperDigitSorterModule.h>
10 
11 #include <framework/datastore/StoreArray.h>
12 #include <framework/datastore/RelationArray.h>
13 #include <framework/logging/Logger.h>
14 
15 #include <mdst/dataobjects/MCParticle.h>
16 #include <svd/dataobjects/SVDTrueHit.h>
17 #include <utility>
18 #include <vector>
19 #include <algorithm>
20 
21 using namespace std;
22 using namespace Belle2;
23 using namespace Belle2::SVD;
24 
25 //-----------------------------------------------------------------
26 // Register the Module
27 //-----------------------------------------------------------------
28 REG_MODULE(SVDShaperDigitSorter);
29 
30 //-----------------------------------------------------------------
31 // Implementation
32 //-----------------------------------------------------------------
33 
34 SVDShaperDigitSorterModule::SVDShaperDigitSorterModule() : Module()
35 {
36  //
37  //Set module properties
38  setDescription("This module sorts an existing SVDShaperDigits collection and also "
39  "updates the corresponding relations to MCParticles and TrueHits. This is "
40  "needed for background overlay as the Clusterizer expects sorted input. "
41  "The module is otherwise not required for simulation or reconstruction, "
42  "as both SVDDigitzer ond SVDUnpacker output sorted digit collections.");
44  addParam("digits", m_storeShaperDigitsName, "SVDShaperDigit collection name", string(""));
45  addParam("truehits", m_storeTrueHitsName, "SVDTrueHit collection name", string(""));
46  addParam("particles", m_storeMCParticlesName, "MCParticle collection name", string(""));
47  addParam("digitsToTrueHits", m_relShaperDigitTrueHitName, "ShaperDigits to TrueHit relation name",
48  string(""));
49  addParam("digitsToMCParticles", m_relShaperDigitMCParticleName, "ShaperDigits to MCParticles relation name",
50  string(""));
51 }
52 
54 {
55  //Register collections
59  storeShaperDigits.isRequired();
60  storeMCParticles.isOptional();
61  storeTrueHits.isOptional();
62 
63  RelationArray relShaperDigitMCParticles(storeShaperDigits, storeMCParticles, m_relShaperDigitMCParticleName);
64  RelationArray relShaperDigitTrueHits(storeShaperDigits, storeTrueHits, m_relShaperDigitTrueHitName);
65  relShaperDigitMCParticles.isOptional();
66  relShaperDigitTrueHits.isOptional();
67 
68  m_storeShaperDigitsName = storeShaperDigits.getName();
69  m_storeTrueHitsName = storeTrueHits.getName();
70  m_storeMCParticlesName = storeMCParticles.getName();
71 
72  m_relShaperDigitTrueHitName = relShaperDigitTrueHits.getName();
73  m_relShaperDigitMCParticleName = relShaperDigitMCParticles.getName();
74 
75 }
76 
78 {
80  // If not digits, nothing to do
81  if (!storeShaperDigits || !storeShaperDigits.getEntries()) return;
82  B2DEBUG(29, "Initial size of StoreShaperDigits array: " << storeShaperDigits.getEntries());
83 
84  RelationArray relShaperDigitMCParticle(m_relShaperDigitMCParticleName);
85  RelationArray relShaperDigitTrueHit(m_relShaperDigitTrueHitName);
86 
87  //List of mappings from old indices to new indices
88  std::vector<RelationArray::consolidation_type> relationIndices(storeShaperDigits.getEntries());
89 
90  // Sort digits by channelID
91  typedef std::pair<size_t, unsigned int> indexElement;
92 
93  std::vector<indexElement> digitIndex;
94 
95  const unsigned int nSamples = storeShaperDigits.getEntries();
96  for (size_t i = 0; i < nSamples; i++)
97  digitIndex.emplace_back(i, storeShaperDigits[i]->getUniqueChannelID());
98 
99  std::sort(digitIndex.begin(), digitIndex.end(),
100  [](const indexElement & a, const indexElement & b)->bool { return (a.second < b.second);}
101  );
102 
103  // Now we loop over sensors and reorder the digits list
104  // To do this, we create a copy of the existing digits
105  m_digitcopy.clear();
106  m_digitcopy.insert(end(m_digitcopy), begin(storeShaperDigits), end(storeShaperDigits));
107 
108  for (size_t i = 0; i < nSamples; ++i) {
109  *storeShaperDigits[i] = m_digitcopy[digitIndex[i].first];
110  relationIndices[digitIndex[i].first] = std::make_pair(i, false);
111  }
112 
113  // Finally we just need to reorder the RelationArrays
114  RelationArray::ReplaceVec<> from(relationIndices);
116  if (relShaperDigitMCParticle) relShaperDigitMCParticle.consolidate(from, to, RelationArray::c_deleteElement);
117  if (relShaperDigitTrueHit) relShaperDigitTrueHit.consolidate(from, to, RelationArray::c_deleteElement);
118  B2DEBUG(29, "Final size of StoreShaperDigits store array: " << storeShaperDigits.getEntries());
119 
120 }
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
Struct to replace indices based on a sequential container.
Low-level class to create/modify relations between StoreArrays.
Definition: RelationArray.h:62
void consolidate()
Consolidate Relation Elements.
@ c_deleteElement
Delete the whole relation element if the original element got re-attributed.
Definition: RelationArray.h:81
std::string m_relShaperDigitMCParticleName
Name of the relation between SVDShaperDigits and MCParticles.
virtual void initialize() override
Initialize the module.
std::string m_storeShaperDigitsName
Name of the collection to use for the SVDShaperDigits.
virtual void event() override
do the sorting
std::string m_storeTrueHitsName
Name of the collection to use for the SVDTrueHits.
std::string m_storeMCParticlesName
Name of the collection to use for the MCParticles.
std::string m_relShaperDigitTrueHitName
Name of the relation between SVDShaperDigits and SVDTrueHits.
std::vector< SVDShaperDigit > m_digitcopy
Copy of the ShaperDigits needed for sorting.
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.
const std::string & getName() const
Return name under which the object is saved in the DataStore.
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:216
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
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Namespace to encapsulate code needed for simulation and reconstrucion of the SVD.
Definition: GeoSVDCreator.h:23
Abstract base class for different kinds of events.
Struct for identity transformation on indices.
Definition: RelationArray.h:94