DD4hep  1.30.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 using namespace lcio;
35 
36 
37 static DDTest test( "cellid_position_converter" ) ;
38 
39 //=============================================================================
40 
41 const double epsilon = dd4hep::micrometer ;
42 const int maxHit = 100 ;
43 
44 
45 double dist( const Position& p0, const Position& p1 ){
46  Position p2 = p1 - p0 ;
47  return p2.r() ;
48 }
49 
50 
51 struct TestCounter{
52  unsigned passed{} ;
53  unsigned failed{} ;
54 } ;
55 
56 struct TestCounters{
57  TestCounter position{} ;
58  TestCounter cellid{} ;
59 };
60 
61 typedef std::map<std::string, TestCounters > TestMap ;
62 
63 
64 
65 int main_wrapper(int argc, char** argv ){
66 
67  if( argc < 3 ) {
68  std::cout << " usage: test_cellid_position_converter compact.xml lcio_file.slcio" << std::endl ;
69  exit(1) ;
70  }
71 
72  std::string inFile = argv[1] ;
73 
74  Detector& description = Detector::getInstance();
75 
76  description.fromCompact( inFile );
77 
78  CellIDPositionConverter idposConv( description ) ;
79 
80 
81  //---------------------------------------------------------------------
82  // open lcio file with SimCalorimeterHits
83  //---------------------------------------------------------------------
84 
85  std::string lcioFileName = argv[2] ;
86 
87  LCReader* rdr = LCFactory::getInstance()->createLCReader() ;
88  rdr->open( lcioFileName ) ;
89 
90  LCEvent* evt = 0 ;
91 
92 
93  // use only hits from these collections
94  std::set< std::string > subset = {} ;
95  //{"BeamCalCollection" } ; //{"EcalBarrelCollection" } ; //{"HcalBarrelRegCollection"} ;
96 
97 
98  // ignore all hits from these collections
99  std::set< std::string > subsetIgnore = {"HCalBarrelRPCHits","HCalECRingRPCHits","HCalEndcapRPCHits" } ;
100 
101  TestMap tMap ;
102 
103  while( ( evt = rdr->readNextEvent() ) != 0 ){
104 
105  const std::vector< std::string >& colNames = *evt->getCollectionNames() ;
106 
107  for(unsigned icol=0, ncol = colNames.size() ; icol < ncol ; ++icol ){
108 
109  LCCollection* col = evt->getCollection( colNames[ icol ] ) ;
110 
111  std::string typeName = col->getTypeName() ;
112 
113  if( typeName != lcio::LCIO::SIMCALORIMETERHIT )
114  continue ;
115 
116  if( !subset.empty() && subset.find( colNames[icol] ) == subset.end() )
117  continue ;
118 
119  if( !subsetIgnore.empty() && subsetIgnore.find( colNames[icol] ) != subsetIgnore.end() )
120  continue ;
121 
122  std::cout << " -- testing collection : " << colNames[ icol ] << std::endl ;
123 
124  std::string cellIDEcoding = col->getParameters().getStringVal("CellIDEncoding") ;
125 
126  dd4hep::BitFieldCoder idDecoder0( cellIDEcoding ) ;
127  dd4hep::BitFieldCoder idDecoder1( cellIDEcoding ) ;
128 
129  int nHit = std::min( col->getNumberOfElements(), maxHit ) ;
130 
131 
132  for(int i=0 ; i< nHit ; ++i){
133 
134  SimCalorimeterHit* sHit = (SimCalorimeterHit*) col->getElementAt(i) ;
135 
136  dd4hep::CellID id0 = sHit->getCellID0() ;
137  dd4hep::CellID id1 = sHit->getCellID1() ;
138 
139  dd4hep::CellID id = idDecoder0.toLong( id0 , id1 ) ;
140 
141  Position point( sHit->getPosition()[0]* dd4hep::mm , sHit->getPosition()[1]* dd4hep::mm , sHit->getPosition()[2]* dd4hep::mm ) ;
142 
143 
144  // ====== test cellID to position and position to cellID conversion ================================
145  DetElement det = idposConv.findDetElement( point ) ;
146 
147  CellID idFromDecoder = idposConv.cellID( point ) ;
148 
149  std::stringstream sst ;
150  sst << " compare ids: " << det.name() << " " << idDecoder0.valueString(id) << " - " << idDecoder1.valueString(idFromDecoder) ;
151 
152  test( id, idFromDecoder, sst.str() ) ;
153 
154  if( ! strcmp( test.last_test_status() , "PASSED" ) )
155  tMap[ colNames[icol] ].cellid.passed++ ;
156  else
157  tMap[ colNames[icol] ].cellid.failed++ ;
158 
159  Position pointFromDecoder = idposConv.position( id ) ;
160 
161  double d = dist(pointFromDecoder, point) ;
162  std::stringstream sst1 ;
163  sst1 << " dist " << d << " ( " << point << " ) - ( " << pointFromDecoder << " ) - detElement: "
164  << det.name() ;
165 
166  test( d < epsilon , true , sst1.str() ) ;
167 
168  if( ! strcmp( test.last_test_status() , "PASSED" ) )
169  tMap[ colNames[icol] ].position.passed++ ;
170  else
171  tMap[ colNames[icol] ].position.failed++ ;
172 
173  }
174  }
175 
176  }
177 
178  // print summary
179 
180  std::cout << "\n ----------------------- summary ---------------------- " << std::endl ;
181 
182 
183  for( const auto& res : tMap ) {
184  const std::string& name = res.first;
185  unsigned total = res.second.position.passed+res.second.position.failed ;
186  unsigned pos_failed = res.second.position.failed ;
187  unsigned id_failed = res.second.cellid.failed ;
188 
189 
190  printf(" %-30s \t failed position: %5d failed cellID: %5d of total: %5d \n",
191  name.c_str(), pos_failed , id_failed, total ) ;
192 
193  }
194  std::cout << "\n -------------------------------------------------------- " << std::endl ;
195 
196 
197  return 0;
198 }
199 
200 //=============================================================================
201 #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:61
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:56
dd4hep::DDSegmentation::BitFieldCoder::valueString
std::string valueString(CellID bitfield) const
Definition: BitFieldCoder.cpp:109
epsilon
const double epsilon
Definition: test_cellid_position_converter.cpp:41
TestCounter
Definition: test_cellid_position_converter.cpp:51
main_wrapper
int main_wrapper(int argc, char **argv)
Definition: test_cellid_position_converter.cpp:65
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:45
maxHit
const int maxHit
Definition: test_cellid_position_converter.cpp:42
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:50