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