Belle II Software  release-05-02-19
PXDClusterCheckModule.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/PXDClusterCheckModule.h>
12 
13 #include <framework/datastore/StoreArray.h>
14 #include <framework/datastore/RelationArray.h>
15 #include <framework/datastore/RelationIndex.h>
16 #include <framework/logging/Logger.h>
17 
18 #include <mdst/dataobjects/MCParticle.h>
19 #include <pxd/dataobjects/PXDDigit.h>
20 #include <pxd/dataobjects/PXDCluster.h>
21 #include <pxd/dataobjects/PXDTrueHit.h>
22 
23 using namespace std;
24 using namespace Belle2;
25 using namespace Belle2::PXD;
26 
27 
28 #define assert_float(A,B) if(!isClose((A),(B)))\
29  B2FATAL("Assertion failed: " << #A << " (" << (A) << ") != " << #B << " (" << (B) << ")");
30 #define assert_eq(A,B) if((A)!=(B))\
31  B2FATAL("Assertion failed: " << #A << " (" << (A) << ") != " << #B << " (" << (B) << ")");
32 
33 namespace {
35  bool isClose(double a, double b, double epsilon = 1e-6)
36  {
37  return a == b || fabs(a - b) < epsilon || (fabs(a / b) - 1.0) < epsilon;
38  }
39 
41  template<class range> void checkRelation(range a, range b)
42  {
43  //They should have the same number of elements
44  assert_eq(std::distance(begin(a), end(a)), std::distance(begin(b), end(b)));
45  //Loop over both ranges and compare index and weights
46  for (decltype(begin(a)) ita = begin(a), itb = begin(b); ita != end(a) && itb != end(b); ++ita, ++itb) {
47  assert_eq(ita->indexTo, itb->indexTo);
48  assert_float(ita->weight, itb->weight);
49  }
50  }
51 }
52 
53 //-----------------------------------------------------------------
54 // Register the Module
55 //-----------------------------------------------------------------
56 REG_MODULE(PXDClusterCheck);
57 
58 //-----------------------------------------------------------------
59 // Implementation
60 //-----------------------------------------------------------------
61 
62 PXDClusterCheckModule::PXDClusterCheckModule() : Module()
63 {
64  //Set module properties
65  setDescription("This Modules compares to sets of clusters and their relations "
66  "to make sure they are identical. Intended to cross check Clusterizer. "
67  "Default Collection names are assumed for MCParticles, PXDTrueHits and "
68  "PXDDigits");
70  addParam("clustersOld", m_clustersOld, "Digits collection name", string(""));
71  addParam("clustersNew", m_clustersNew, "Digits collection name", string(""));
72 }
73 
75 {
76  //Mark all StoreArrays as required
77  StoreArray<MCParticle> storeMCParticles;
78  StoreArray<PXDDigit> storeDigits;
79  StoreArray<PXDTrueHit> storeTrueHits;
80  StoreArray<PXDCluster> storeClustersOld(m_clustersOld);
81  StoreArray<PXDCluster> storeClustersNew(m_clustersNew);
82  storeMCParticles.isRequired();
83  storeDigits.isRequired();
84  storeTrueHits.isRequired();
85  storeClustersOld.isRequired();
86  storeClustersNew.isRequired();
87 
88  //And also all relations
89  RelationArray relClustersMCParticlesOld(storeClustersOld, storeMCParticles);
90  RelationArray relClustersTrueHitsOld(storeClustersOld, storeTrueHits);
91  RelationArray relClustersDigitsOld(storeClustersOld, storeDigits);
92  relClustersMCParticlesOld.isRequired();
93  relClustersTrueHitsOld.isRequired();
94  relClustersDigitsOld.isRequired();
95 
96  RelationArray relClustersMCParticlesNew(storeClustersNew, storeMCParticles);
97  RelationArray relClustersTrueHitsNew(storeClustersNew, storeTrueHits);
98  RelationArray relClustersDigitsNew(storeClustersNew, storeDigits);
99  relClustersMCParticlesNew.isRequired();
100  relClustersTrueHitsNew.isRequired();
101  relClustersDigitsNew.isRequired();
102 }
103 
105 {
106  //Obtain all StoreArrays
107  const StoreArray<MCParticle> storeMCParticles;
108  const StoreArray<PXDDigit> storeDigits;
109  const StoreArray<PXDTrueHit> storeTrueHits;
110  const StoreArray<PXDCluster> storeClustersOld(m_clustersOld);
111  const StoreArray<PXDCluster> storeClustersNew(m_clustersNew);
112  //Obtain all relations
113  RelationArray relCDOld(storeClustersOld, storeDigits);
114  RelationArray relCDNew(storeClustersNew, storeDigits);
115  //There might be differences in the order of the pixels so we sort the cluster relation
116  if (relCDOld) relCDOld.consolidate();
117  if (relCDNew) relCDNew.consolidate();
118  //The rest should be sorted anyway so let's just assume they are
119  const RelationIndex<PXDCluster, MCParticle> relClustersMCParticlesOld(storeClustersOld, storeMCParticles);
120  const RelationIndex<PXDCluster, PXDTrueHit> relClustersTrueHitsOld(storeClustersOld, storeTrueHits);
121  const RelationIndex<PXDCluster, MCParticle> relClustersMCParticlesNew(storeClustersNew, storeMCParticles);
122  const RelationIndex<PXDCluster, PXDTrueHit> relClustersTrueHitsNew(storeClustersNew, storeTrueHits);
123 
124  //We want the same number of Clusters
125  assert_eq(storeClustersOld.getEntries(), storeClustersNew.getEntries());
126  const unsigned int nCls = storeClustersOld.getEntries();
127  //And for each cluster we want one relation to digits, so size should be equal
128  assert_eq(relCDOld.getEntries(), (int)nCls);
129  assert_eq(relCDNew.getEntries(), (int)nCls);
130  for (unsigned int i = 0; i < nCls; ++i) {
131  //Loop through clusters and compare all members. Since we want to allow for
132  //small deviations we do not use == on the whole cluster but compare the
133  //members individually
134  const PXDCluster& clsOld = *storeClustersOld[i];
135  const PXDCluster& clsNew = *storeClustersNew[i];
136  assert_eq(clsOld.getSensorID(), clsNew.getSensorID());
137  assert_float(clsOld.getU(), clsNew.getU());
138  assert_float(clsOld.getV(), clsNew.getV());
139  assert_float(clsOld.getUSigma(), clsNew.getUSigma());
140  assert_float(clsOld.getVSigma(), clsNew.getVSigma());
141  assert_float(clsOld.getCharge(), clsNew.getCharge());
142  assert_float(clsOld.getSeedCharge(), clsNew.getSeedCharge());
143  assert_eq(clsOld.getSize(), clsNew.getSize());
144  assert_eq(clsOld.getUSize(), clsNew.getUSize());
145  assert_eq(clsOld.getVSize(), clsNew.getVSize());
146  assert_eq(clsOld.getUStart(), clsNew.getUStart());
147  assert_eq(clsOld.getVStart(), clsNew.getVStart());
148  assert_float(clsOld.getRho(), clsNew.getRho());
149 
150  //Get the corresponding relation element
151  const RelationElement reOld = relCDOld[i];
152  const RelationElement reNew = relCDNew[i];
153  //The relation is sorted so the index should be correct
154  assert_eq(reOld.getFromIndex(), i);
155  assert_eq(reNew.getFromIndex(), i);
156  //And the number of relations should be equal to the cluster size
157  assert_eq(reOld.getSize(), clsOld.getSize());
158  assert_eq(reNew.getSize(), clsNew.getSize());
159  //And the entries should be identical
160  for (unsigned int j = 0; j < reOld.getSize(); ++j) {
161  assert_eq(reOld.getToIndex(j), reNew.getToIndex(j));
162  assert_eq(reOld.getWeight(j), reNew.getWeight(j));
163  }
164  //Now let's compare the MCParticle and TrueHit relations
165  checkRelation(relClustersMCParticlesOld.getElementsFrom(clsOld), relClustersMCParticlesNew.getElementsFrom(clsNew));
166  checkRelation(relClustersTrueHitsOld.getElementsFrom(clsOld), relClustersTrueHitsNew.getElementsFrom(clsNew));
167  }
168 }
Belle2::PXDCluster::getUSigma
float getUSigma() const
Get error of u coordinate of hit position.
Definition: PXDCluster.h:146
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
Belle2::PXD::PXDClusterCheckModule::event
virtual void event() override
do the clustering
Definition: PXDClusterCheckModule.cc:104
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::PXDCluster::getU
float getU() const
Get u coordinate of hit position.
Definition: PXDCluster.h:136
Belle2::RelationIndex
Provides access to fast ( O(log n) ) bi-directional lookups on a specified relation.
Definition: RelationIndex.h:84
Belle2::PXD::PXDClusterCheckModule::m_clustersOld
std::string m_clustersOld
Name of the first PXDCluster StoreArray.
Definition: PXDClusterCheckModule.h:54
Belle2::PXDCluster::getSeedCharge
unsigned short getSeedCharge() const
Get seed charge.
Definition: PXDCluster.h:166
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::RelationElement
Class to store a single element of a relation.
Definition: RelationElement.h:33
Belle2::PXD::PXDClusterCheckModule::initialize
virtual void initialize() override
Initialize the module.
Definition: PXDClusterCheckModule.cc:74
Belle2::PXDCluster::getCharge
unsigned short getCharge() const
Get collected charge.
Definition: PXDCluster.h:161
Belle2::StoreAccessorBase::isRequired
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
Definition: StoreAccessorBase.h:80
Belle2::Module::setPropertyFlags
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:210
Belle2::PXDCluster::getRho
float getRho() const
Get hit position error covariance coefficient.
Definition: PXDCluster.h:156
Belle2::PXDCluster::getSize
unsigned short getSize() const
Get cluster size.
Definition: PXDCluster.h:171
Belle2::RelationIndex::getElementsFrom
range_from getElementsFrom(const FROM *from) const
Return a range of all elements pointing from the given object.
Definition: RelationIndex.h:157
Belle2::PXD
Namespace to encapsulate code needed for simulation and reconstrucion of the PXD.
Definition: PXDCalibrationUtilities.h:28
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::PXD::PXDClusterCheckModule::m_clustersNew
std::string m_clustersNew
Name of the second PXDCluster StoreArray.
Definition: PXDClusterCheckModule.h:56
Belle2::PXDCluster
The PXD Cluster class This class stores all information about reconstructed PXD clusters The position...
Definition: PXDCluster.h:41
Belle2::RelationElement::getSize
size_t getSize() const
Get number of indices we points to.
Definition: RelationElement.h:81
Belle2::PXDCluster::getSensorID
VxdID getSensorID() const
Get the sensor ID.
Definition: PXDCluster.h:131
Belle2::RelationElement::getToIndex
index_type getToIndex(size_t n=0) const
Get nth index we point to.
Definition: RelationElement.h:87
Belle2::RelationArray::getEntries
int getEntries() const
Get the number of elements.
Definition: RelationArray.h:252
Belle2::PXDCluster::getVSigma
float getVSigma() const
Get error in v coordinate of hit position.
Definition: PXDCluster.h:151
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::RelationElement::getFromIndex
index_type getFromIndex() const
Get index we point from.
Definition: RelationElement.h:78
Belle2::RelationElement::getWeight
weight_type getWeight(size_t n=0) const
Get nth weight we point to.
Definition: RelationElement.h:90
Belle2::StoreArray< MCParticle >
Belle2::PXDCluster::getUStart
unsigned short getUStart() const
Get cluster start cell in u direction.
Definition: PXDCluster.h:186
Belle2::PXDCluster::getVSize
unsigned short getVSize() const
Get cluster size in v direction.
Definition: PXDCluster.h:181
Belle2::PXDCluster::getV
float getV() const
Get v coordinate of hit position.
Definition: PXDCluster.h:141
Belle2::PXDCluster::getVStart
unsigned short getVStart() const
Get cluster start cell in v direction.
Definition: PXDCluster.h:191
Belle2::StoreArray::getEntries
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:226
Belle2::PXDCluster::getUSize
unsigned short getUSize() const
Get cluster size in u direction.
Definition: PXDCluster.h:176