Belle II Software  release-05-01-25
PXDDigitSorterModule.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 *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <pxd/modules/pxdReconstruction/PXDDigitSorterModule.h>
12 #include <framework/datastore/StoreArray.h>
13 #include <framework/datastore/RelationArray.h>
14 #include <framework/logging/Logger.h>
15 #include <vxd/geometry/GeoCache.h>
16 
17 #include <mdst/dataobjects/MCParticle.h>
18 #include <pxd/dataobjects/PXDTrueHit.h>
19 #include <pxd/reconstruction/Pixel.h>
20 #include <pxd/reconstruction/PXDPixelMasker.h>
21 
22 using namespace std;
23 using namespace Belle2;
24 using namespace Belle2::PXD;
25 using namespace boost::python;
26 
27 
28 //-----------------------------------------------------------------
29 // Register the Module
30 //-----------------------------------------------------------------
31 REG_MODULE(PXDDigitSorter);
32 
33 //-----------------------------------------------------------------
34 // Implementation
35 //-----------------------------------------------------------------
36 
37 PXDDigitSorterModule::PXDDigitSorterModule() : Module()
38 {
39  //Set module properties
40  setDescription("This module sorts the existing PXDDigits collection and also "
41  "updates the corresponding Relation to MCParticles and TrueHits. This is "
42  "needed for unsorted pixel data as the Clusterizer expects sorted input.");
44  addParam("merge", m_mergeDuplicates, "If true, merge Pixel information if more than one digit exists for the same address", true);
45  addParam("trimDigits", m_trimDigits, "If true, pixel data will be checked to detect malformed pixels. Such pixels will be scarded.",
46  true);
47  addParam("digits", m_storeDigitsName, "PXDDigit collection name", string(""));
48  addParam("truehits", m_storeTrueHitsName, "PXDTrueHit collection name", string(""));
49  addParam("particles", m_storeMCParticlesName, "MCParticle collection name", string(""));
50  addParam("digitsToTrueHits", m_relDigitTrueHitName, "Digits to TrueHit relation name",
51  string(""));
52  addParam("digitsToMCParticles", m_relDigitMCParticleName, "Digits to MCParticles relation name",
53  string(""));
54 }
55 
57 {
58  //Register collections
62  storeDigits.isRequired();
63  storeMCParticles.isOptional();
64  storeTrueHits.isOptional();
65 
66  RelationArray relDigitMCParticles(storeDigits, storeMCParticles, m_relDigitMCParticleName);
67  RelationArray relDigitTrueHits(storeDigits, storeTrueHits, m_relDigitTrueHitName);
68  relDigitMCParticles.isOptional();
69  relDigitTrueHits.isOptional();
70 
71  m_storeDigitsName = storeDigits.getName();
72  m_storeTrueHitsName = storeTrueHits.getName();
73  m_storeMCParticlesName = storeMCParticles.getName();
74 
75  m_relDigitTrueHitName = relDigitTrueHits.getName();
76  m_relDigitMCParticleName = relDigitMCParticles.getName();
77 }
78 
80 {
82  // If not digits, nothing to do
83  if (!storeDigits || !storeDigits.getEntries()) return;
84 
86 
87  RelationArray relDigitMCParticle(m_relDigitMCParticleName);
88  RelationArray relDigitTrueHit(m_relDigitTrueHitName);
89 
90  //List of mappings from old indices to new indices
91  std::vector<RelationArray::consolidation_type> relationIndices(storeDigits.getEntries());
92  //Mapping of Pixel information to sort according to VxdID, row, column
93  std::map<VxdID, std::multiset<Pixel>> sensors;
94 
95  // Fill sensor information to get sorted PXDDigit indices
96  const int nPixels = storeDigits.getEntries();
97  for (int i = 0; i < nPixels; i++) {
98  const PXDDigit* const storeDigit = storeDigits[i];
99  VxdID sensorID = storeDigit->getSensorID();
100  if (!geo.validSensorID(sensorID)) {
101  B2DEBUG(20, "Malformed PXDDigit, VxdID $" << hex << sensorID.getID() << ", dropping. (" << sensorID << ")");
102  continue;
103  }
104 
105  if (PXDPixelMasker::getInstance().pixelOK(storeDigit->getSensorID(), storeDigit->getUCellID(), storeDigit->getVCellID())) {
106  // Trim digits
107  if (!m_trimDigits || goodDigit(storeDigit)) {
108  Pixel px(storeDigit, i);
109  sensors[sensorID].insert(px);
110  } else {
111  B2DEBUG(20, "Encountered a malformed digit in PXDDigit sorter: " << endl
112  << "VxdID: " << sensorID.getLayerNumber() << "/"
113  << sensorID.getLadderNumber() << "/"
114  << sensorID.getSensorNumber() << " u = " << storeDigit->getUCellID() << "v = " << storeDigit->getVCellID()
115  << " DISCARDED.");
116  }
117  }
118  }
119 
120  // Now we loop over sensors and reorder the digits list
121  // To do this, we create a copy of the existing digits
122  m_digitcopy.clear();
123  m_digitcopy.insert(end(m_digitcopy), begin(storeDigits), end(storeDigits));
124  // and a vector to remember which index changed into what
125  unsigned int index(0);
126  // And just loop over the sensors and assign the digits at the correct position
127  for (const auto& sensor : sensors) {
128  const PXD::Pixel* lastpx(0);
129  for (const PXD::Pixel& px : sensor.second) {
130  //Normal case: pixel has different address
131  if (!lastpx || px > *lastpx) {
132  //Overwrite the digit
133  *storeDigits[index] = m_digitcopy[px.getIndex()];
134  //Remember old and new index
135  relationIndices[px.getIndex()] = std::make_pair(index, false);
136  //increment next index
137  ++index;
138  } else {
139  //We already have a pixel at this address, see if we merge or drop the new one
140  if (m_mergeDuplicates) {
141  //Merge the two pixels. As the PXDDigit does not have setters we have to create a new object.
142  const PXDDigit& old = *storeDigits[index - 1];
143  // FIXME: Does it really make sense to add the charge of duplicate pixels?
144  *storeDigits[index - 1] = PXDDigit(old.getSensorID(), old.getUCellID(), old.getVCellID(),
145  old.getCharge() + m_digitcopy[px.getIndex()].getCharge());
146  relationIndices[px.getIndex()] = std::make_pair(index - 1, false);
147  } else {
148  //Otherwise delete the second pixel by omitting it here and removing relation elements on consolidation
149  relationIndices[px.getIndex()] = std::make_pair(0, true);
150  }
151  }
152  lastpx = &px;
153  }
154  }
155  //Resize if we omitted/merged one or more elements
156  storeDigits.getPtr()->ExpandCreate(index);
157 
158  // Finally we just need to reorder the RelationArrays and delete elements
159  // referencing pixels which were ignored because that adress was set more
160  // than once
161  RelationArray::ReplaceVec<> from(relationIndices);
163  if (relDigitMCParticle) relDigitMCParticle.consolidate(from, to, RelationArray::c_deleteElement);
164  if (relDigitTrueHit) relDigitTrueHit.consolidate(from, to, RelationArray::c_deleteElement);
165 }
Belle2::RelationArray
Low-level class to create/modify relations between StoreArrays.
Definition: RelationArray.h:72
Belle2::PXD::PXDDigitSorterModule::initialize
virtual void initialize() override
Initialize the module.
Definition: PXDDigitSorterModule.cc:56
Belle2::VxdID
Class to uniquely identify a any structure of the PXD and SVD.
Definition: VxdID.h:43
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
Belle2::PXD::PXDDigitSorterModule::m_relDigitMCParticleName
std::string m_relDigitMCParticleName
Name of the relation between PXDDigits and MCParticles.
Definition: PXDDigitSorterModule.h:74
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::PXD::PXDDigitSorterModule::m_storeTrueHitsName
std::string m_storeTrueHitsName
Name of the collection to use for the PXDTrueHits.
Definition: PXDDigitSorterModule.h:70
Belle2::VxdID::getLadderNumber
baseType getLadderNumber() const
Get the ladder id.
Definition: VxdID.h:108
Belle2::StoreAccessorBase::isOptional
bool isOptional(const std::string &name="")
Tell the DataStore about an optional input.
Definition: StoreAccessorBase.h:95
Belle2::PXD::Pixel
Class to represent one pixel, used in clustering for fast access.
Definition: Pixel.h:47
Belle2::VxdID::getID
baseType getID() const
Get the unique id.
Definition: VxdID.h:104
Belle2::StoreAccessorBase::getName
const std::string & getName() const
Return name under which the object is saved in the DataStore.
Definition: StoreAccessorBase.h:130
Belle2::PXD::PXDDigitSorterModule::m_storeDigitsName
std::string m_storeDigitsName
Name of the collection to use for the PXDDigits.
Definition: PXDDigitSorterModule.h:68
Belle2::PXD::PXDDigitSorterModule::m_relDigitTrueHitName
std::string m_relDigitTrueHitName
Name of the relation between PXDDigits and PXDTrueHits.
Definition: PXDDigitSorterModule.h:76
Belle2::PXD::PXDDigitSorterModule::goodDigit
bool goodDigit(const PXDDigit *const digit) const
Utility function to check pixel coordinates.
Definition: PXDDigitSorterModule.h:58
Belle2::PXDDigit
The PXD digit class.
Definition: PXDDigit.h:38
Belle2::PXD::PXDDigitSorterModule::m_digitcopy
std::vector< PXDDigit > m_digitcopy
Copy of the Digits needed for sorting.
Definition: PXDDigitSorterModule.h:78
Belle2::VXD::GeoCache::validSensorID
bool validSensorID(Belle2::VxdID id) const
Check that id is a valid sensor number.
Definition: GeoCache.cc:53
Belle2::PXDDigit::getUCellID
unsigned short getUCellID() const
Get cell ID in u.
Definition: PXDDigit.h:80
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::PXD::PXDDigitSorterModule::event
virtual void event() override
do the sorting
Definition: PXDDigitSorterModule.cc:79
Belle2::Module::setPropertyFlags
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:210
Belle2::PXDDigit::getSensorID
VxdID getSensorID() const
Get the sensor ID.
Definition: PXDDigit.h:75
Belle2::PXD
Namespace to encapsulate code needed for simulation and reconstrucion of the PXD.
Definition: PXDCalibrationUtilities.h:28
Belle2::VXD::GeoCache::getInstance
static GeoCache & getInstance()
Return a reference to the singleton instance.
Definition: GeoCache.cc:215
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::PXD::PXDDigitSorterModule::m_trimDigits
bool m_trimDigits
if true, check digit data and discard malformed digits.
Definition: PXDDigitSorterModule.h:82
Belle2::PXD::Pixel::getIndex
unsigned int getIndex() const
Return the Index of the digit.
Definition: Pixel.h:83
Belle2::RelationArray::ReplaceVec
Struct to replace indices based on a sequential container.
Definition: RelationArray.h:142
Belle2::PXD::PXDDigitSorterModule::m_mergeDuplicates
bool m_mergeDuplicates
Mode: if true, merge duplicate pixels, otherwise delete all but the first occurence.
Definition: PXDDigitSorterModule.h:80
Belle2::VxdID::getSensorNumber
baseType getSensorNumber() const
Get the sensor id.
Definition: VxdID.h:110
Belle2::StoreArray::getPtr
TClonesArray * getPtr() const
Raw access to the underlying TClonesArray.
Definition: StoreArray.h:321
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::VXD::GeoCache
Class to faciliate easy access to sensor information of the VXD like coordinate transformations or pi...
Definition: GeoCache.h:41
Belle2::StoreArray
Accessor to arrays stored in the data store.
Definition: ECLMatchingPerformanceExpertModule.h:33
Belle2::VxdID::getLayerNumber
baseType getLayerNumber() const
Get the layer id.
Definition: VxdID.h:106
Belle2::PXD::PXDDigitSorterModule::m_storeMCParticlesName
std::string m_storeMCParticlesName
Name of the collection to use for the MCParticles.
Definition: PXDDigitSorterModule.h:72
Belle2::StoreArray::getEntries
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:226
Belle2::PXDDigit::getVCellID
unsigned short getVCellID() const
Get cell ID in v.
Definition: PXDDigit.h:85
Belle2::PXD::PXDPixelMasker::getInstance
static PXDPixelMasker & getInstance()
Main (and only) way to access the PXDPixelMasker.
Definition: PXDPixelMasker.cc:37