DD4hep  1.28.0
Detector Description Toolkit for High Energy Physics
test_cellid_position_converter.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // @author F.Gaede, DESY
11 // @date May, 2017
12 //==========================================================================
13 
14 // Framework include files
15 #include "DD4hep/Detector.h"
16 #include "DD4hep/DDTest.h"
17 
18 #include "DD4hep/DD4hepUnits.h"
19 #include "DD4hep/BitFieldCoder.h"
21 
22 #include "lcio.h"
23 #include "IO/LCReader.h"
24 #include "EVENT/LCEvent.h"
25 #include "EVENT/LCCollection.h"
26 #include "EVENT/SimCalorimeterHit.h"
27 
28 #include <sstream>
29 
30 using namespace std ;
31 using namespace dd4hep ;
32 using namespace dd4hep::detail;
33 using namespace dd4hep::rec ;
34 
35 using namespace lcio;
36 
37 
38 static DDTest test( "cellid_position_converter" ) ;
39 
40 //=============================================================================
41 
42 const double epsilon = dd4hep::micrometer ;
43 const int maxHit = 100 ;
44 
45 
46 double dist( const Position& p0, const Position& p1 ){
47  Position p2 = p1 - p0 ;
48  return p2.r() ;
49 }
50 
51 
52 struct TestCounter{
53  unsigned passed{} ;
54  unsigned failed{} ;
55 } ;
56 
57 struct TestCounters{
58  TestCounter position{} ;
59  TestCounter cellid{} ;
60 };
61 
62 typedef std::map<std::string, TestCounters > TestMap ;
63 
64 
65 
66 int main_wrapper(int argc, char** argv ){
67 
68  if( argc < 3 ) {
69  std::cout << " usage: test_cellid_position_converter compact.xml lcio_file.slcio" << std::endl ;
70  exit(1) ;
71  }
72 
73  std::string inFile = argv[1] ;
74 
75  Detector& description = Detector::getInstance();
76 
77  description.fromCompact( inFile );
78 
79  CellIDPositionConverter idposConv( description ) ;
80 
81 
82  //---------------------------------------------------------------------
83  // open lcio file with SimCalorimeterHits
84  //---------------------------------------------------------------------
85 
86  std::string lcioFileName = argv[2] ;
87 
88  LCReader* rdr = LCFactory::getInstance()->createLCReader() ;
89  rdr->open( lcioFileName ) ;
90 
91  LCEvent* evt = 0 ;
92 
93 
94  // use only hits from these collections
95  std::set< std::string > subset = {} ;
96  //{"BeamCalCollection" } ; //{"EcalBarrelCollection" } ; //{"HcalBarrelRegCollection"} ;
97 
98 
99  // ignore all hits from these collections
100  std::set< std::string > subsetIgnore = {"HCalBarrelRPCHits","HCalECRingRPCHits","HCalEndcapRPCHits" } ;
101 
102  TestMap tMap ;
103 
104  while( ( evt = rdr->readNextEvent() ) != 0 ){
105 
106  const std::vector< std::string >& colNames = *evt->getCollectionNames() ;
107 
108  for(unsigned icol=0, ncol = colNames.size() ; icol < ncol ; ++icol ){
109 
110  LCCollection* col = evt->getCollection( colNames[ icol ] ) ;
111 
112  std::string typeName = col->getTypeName() ;
113 
114  if( typeName != lcio::LCIO::SIMCALORIMETERHIT )
115  continue ;
116 
117  if( !subset.empty() && subset.find( colNames[icol] ) == subset.end() )
118  continue ;
119 
120  if( !subsetIgnore.empty() && subsetIgnore.find( colNames[icol] ) != subsetIgnore.end() )
121  continue ;
122 
123  std::cout << " -- testing collection : " << colNames[ icol ] << std::endl ;
124 
125  std::string cellIDEcoding = col->getParameters().getStringVal("CellIDEncoding") ;
126 
127  dd4hep::BitFieldCoder idDecoder0( cellIDEcoding ) ;
128  dd4hep::BitFieldCoder idDecoder1( cellIDEcoding ) ;
129 
130  int nHit = std::min( col->getNumberOfElements(), maxHit ) ;
131 
132 
133  for(int i=0 ; i< nHit ; ++i){
134 
135  SimCalorimeterHit* sHit = (SimCalorimeterHit*) col->getElementAt(i) ;
136 
137  dd4hep::CellID id0 = sHit->getCellID0() ;
138  dd4hep::CellID id1 = sHit->getCellID1() ;
139 
140  dd4hep::CellID id = idDecoder0.toLong( id0 , id1 ) ;
141 
142  Position point( sHit->getPosition()[0]* dd4hep::mm , sHit->getPosition()[1]* dd4hep::mm , sHit->getPosition()[2]* dd4hep::mm ) ;
143 
144 
145  // ====== test cellID to position and position to cellID conversion ================================
146  DetElement det = idposConv.findDetElement( point ) ;
147 
148  CellID idFromDecoder = idposConv.cellID( point ) ;
149 
150  std::stringstream sst ;
151  sst << " compare ids: " << det.name() << " " << idDecoder0.valueString(id) << " - " << idDecoder1.valueString(idFromDecoder) ;
152 
153  test( id, idFromDecoder, sst.str() ) ;
154 
155  if( ! strcmp( test.last_test_status() , "PASSED" ) )
156  tMap[ colNames[icol] ].cellid.passed++ ;
157  else
158  tMap[ colNames[icol] ].cellid.failed++ ;
159 
160  Position pointFromDecoder = idposConv.position( id ) ;
161 
162  double d = dist(pointFromDecoder, point) ;
163  std::stringstream sst1 ;
164  sst1 << " dist " << d << " ( " << point << " ) - ( " << pointFromDecoder << " ) - detElement: "
165  << det.name() ;
166 
167  test( d < epsilon , true , sst1.str() ) ;
168 
169  if( ! strcmp( test.last_test_status() , "PASSED" ) )
170  tMap[ colNames[icol] ].position.passed++ ;
171  else
172  tMap[ colNames[icol] ].position.failed++ ;
173 
174  }
175  }
176 
177  }
178 
179  // print summary
180 
181  std::cout << "\n ----------------------- summary ---------------------- " << std::endl ;
182 
183 
184  for( auto res : tMap ){
185 
186  std::string name = res.first ;
187  unsigned total = res.second.position.passed+res.second.position.failed ;
188  unsigned pos_failed = res.second.position.failed ;
189  unsigned id_failed = res.second.cellid.failed ;
190 
191 
192  printf(" %-30s \t failed position: %5d failed cellID: %5d of total: %5d \n",
193  name.c_str(), pos_failed , id_failed, total ) ;
194 
195  }
196  std::cout << "\n -------------------------------------------------------- " << std::endl ;
197 
198 
199  return 0;
200 }
201 
202 //=============================================================================
203 #include "main.h"
dd4hep::DDTest::last_test_status
const char * last_test_status()
Definition: DDTest.h:164
dd4hep::rec
Namespace for the reconstruction part of the AIDA detector description toolkit.
Definition: SurfaceInstaller.h:28
TestMap
std::map< std::string, TestCounters > TestMap
Definition: test_cellid_position_converter.cpp:62
Detector.h
dd4hep::DDSegmentation::BitFieldCoder
Helper class for decoding and encoding a bit field of 64bits for convenient declaration.
Definition: BitFieldCoder.h:114
dd4hep::DDSegmentation::BitFieldCoder::toLong
static CellID toLong(unsigned low_Word, unsigned high_Word)
Definition: BitFieldCoder.h:151
dd4hep::rec::CellIDPositionConverter::cellID
CellID cellID(const Position &global) const
Definition: CellIDPositionConverter.cpp:87
TestCounters
Definition: test_cellid_position_converter.cpp:57
dd4hep::DDSegmentation::BitFieldCoder::valueString
std::string valueString(CellID bitfield) const
Definition: BitFieldCoder.cpp:99
epsilon
const double epsilon
Definition: test_cellid_position_converter.cpp:42
TestCounter
Definition: test_cellid_position_converter.cpp:52
main_wrapper
int main_wrapper(int argc, char **argv)
Definition: test_cellid_position_converter.cpp:66
dd4hep::DDTest
Simple class for defining unit tests.
Definition: DDTest.h:21
CellIDPositionConverter.h
dd4hep::DetElement
Handle class describing a detector element.
Definition: DetElement.h:188
dd4hep::detail
DD4hep internal namespace.
Definition: Alignments.h:32
dd4hep::rec::CellIDPositionConverter::findDetElement
DetElement findDetElement(const Position &global, const DetElement &det=DetElement()) const
Definition: CellIDPositionConverter.cpp:209
dd4hep::Detector::fromCompact
virtual void fromCompact(const std::string &fname, DetectorBuildType type=BUILD_DEFAULT)=0
Deprecated call (use fromXML): Read compact geometry description or alignment file.
dd4hep::Position
ROOT::Math::XYZVector Position
Definition: Objects.h:81
main.h
std
Definition: Plugins.h:30
dist
double dist(const Position &p0, const Position &p1)
Definition: test_cellid_position_converter.cpp:46
maxHit
const int maxHit
Definition: test_cellid_position_converter.cpp:43
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
det
DetElement::Object * det
Definition: AlignmentsCalculator.cpp:66
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
DD4hepUnits.h
dd4hep::rec::CellIDPositionConverter::position
Position position(const CellID &cellID) const
Definition: CellIDPositionConverter.cpp:32
dd4hep::rec::CellIDPositionConverter
Definition: CellIDPositionConverter.h:39
BitFieldCoder.h
CellID
dd4hep::DDSegmentation::CellID CellID
Definition: SegmentationDictionary.h:49