DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
test_surfaces.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 //==========================================================================
11 
12 // Framework include files
13 #include "DD4hep/Detector.h"
14 
15 #include "DDRec/Surface.h"
16 #include "DDRec/DetectorSurfaces.h"
17 #include "DDRec/SurfaceManager.h"
18 #include "DDRec/SurfaceHelper.h"
19 #include "DD4hep/DDTest.h"
20 
21 #include "DD4hep/DD4hepUnits.h"
22 
23 #include "lcio.h"
24 #include "IO/LCReader.h"
25 #include "EVENT/LCEvent.h"
26 #include "EVENT/LCCollection.h"
27 #include "EVENT/SimTrackerHit.h"
28 #include "UTIL/ILDConf.h"
29 
30 #include <map>
31 #include <sstream>
32 
33 using namespace std ;
34 using namespace dd4hep ;
35 using namespace dd4hep::detail;
36 using namespace dd4hep::rec ;
37 
38 
39 static DDTest test( "surfaces" ) ;
40 
41 //=============================================================================
42 
43 int main_wrapper(int argc, char** argv ){
44 
45  if( argc < 3 ) {
46  std::cout << " usage: test_surfaces compact.xml lcio_file.slcio" << std::endl ;
47  exit(1) ;
48  }
49 
50  std::string inFile = argv[1] ;
51 
52  Detector& description = Detector::getInstance();
53 
54  description.fromCompact( inFile );
55 
56 
57 #if 0
58  // create a list of all surfaces in the detector:
59  DetElement world = description.world() ;
60 
61  SurfaceHelper surfMan( world ) ;
62 
63  const SurfaceList& sL = surfMan.surfaceList() ;
64 
65  // map of surfaces
66  std::map< dd4hep::long64, Surface* > surfMap ;
67 
68  for( SurfaceList::const_iterator it = sL.begin() ; it != sL.end() ; ++it ){
69 
70  Surface* surf = *it ;
71 
72  // std::cout << " ------------------------- "
73  // << " surface: " << *surf << std::endl
74  // << " ------------------------- " << std::endl ;
75 
76 
77  surfMap[ surf->id() ] = surf ;
78  }
79 #else
80 
81  SurfaceManager& surfMan = *description.extension< SurfaceManager >() ;
82  const SurfaceMap& surfMap = *surfMan.map( "world" ) ;
83 
84 #endif
85 
86  //---------------------------------------------------------------------
87  // open lcio file with SimTrackerHits
88  //---------------------------------------------------------------------
89 
90  std::string lcioFileName = argv[2] ;
91 
92  IO::LCReader* rdr = IOIMPL::LCFactory::getInstance()->createLCReader() ;
93  rdr->open( lcioFileName ) ;
94 
95  EVENT::LCEvent* evt = 0 ;
96 
97 
98  while( ( evt = rdr->readNextEvent() ) != 0 ){
99 
100  const std::vector< std::string >& colNames = *evt->getCollectionNames() ;
101 
102  for(unsigned icol=0, ncol = colNames.size() ; icol < ncol ; ++icol ){
103 
104  EVENT::LCCollection* col = evt->getCollection( colNames[ icol ] ) ;
105 
106  std::string typeName = col->getTypeName() ;
107 
108  if( typeName != lcio::LCIO::SIMTRACKERHIT )
109  continue ;
110 
111  std::cout << " -- testing collection : " << colNames[ icol ] << std::endl ;
112 
113  std::string cellIDEcoding = col->getParameters().getStringVal("CellIDEncoding") ;
114 
115  lcio::BitField64 idDecoder( cellIDEcoding ) ;
116 
117  int nHit = col->getNumberOfElements() ;
118 
119  for(int i=0 ; i< nHit ; ++i){
120 
121  EVENT::SimTrackerHit* sHit = (EVENT::SimTrackerHit*) col->getElementAt(i) ;
122 
123  dd4hep::FieldID id = sHit->getCellID0() ;
124 
125  idDecoder.setValue( id ) ;
126  // std::cout << " simhit with cellid : " << idDecoder << std::endl ;
127 
128 #if 0
129  Surface* surf = surfMap[ id ] ;
130 #else
131  SurfaceMap::const_iterator si = surfMap.find( id ) ;
132  // Surface* surf = dynamic_cast<Surface*> ( ( si != surfMap.end() ? si->second : 0 ) ) ;
133  ISurface* surf = ( si != surfMap.end() ? si->second : 0 ) ;
134 #endif
135 
136  std::stringstream sst ;
137  sst << " surface found for id : " << std::hex << id << std::dec << " " << idDecoder.valueString() << std ::endl ;
138 
139 
140  // ===== test that we have a surface with the correct ID for every hit ======================
141 
142  test( surf != 0 , true , sst.str() ) ;
143 
144 
145  if( surf != 0 ){
146 
147  // std::cout << " found surface " << *surf << std::endl ;
148 
149  Vector3D point( sHit->getPosition()[0]* dd4hep::mm , sHit->getPosition()[1]* dd4hep::mm , sHit->getPosition()[2]* dd4hep::mm ) ;
150 
151  double dist = surf->distance( point ) ;
152 
153  bool isInside = surf->insideBounds( point ) ;
154 
155 
156  sst.str("") ;
157  sst << " point " << point << " is on surface " ;
158 
159  // ====== test that hit points are inside their surface ================================
160 
161  test( isInside , true , sst.str() ) ;
162 
163  if( ! isInside ) {
164 
165  std::cout << " found surface " << *surf << std::endl
166  << " id : " << idDecoder.valueString()
167  << " point : " << point
168  << " is inside : " << isInside
169  << " distance from surface : " << dist << " (units: cm) " << std::endl
170  << std::endl ;
171  }
172 
173  // ====== test that slightly moved hit points are inside their surface ================================
174 
175  // this test does not make too much sense, depending on the position of the surface within the volume
176 
177  // Vector3D point2 = point + 1e-5 * surf->normal() ;
178  // sst.str("") ;
179  // sst << " point2 " << point2 << " is on surface " ;
180  // isInside = surf->insideBounds( point2 ) ;
181  // test( isInside , true , sst.str() ) ;
182 
183  // if( ! isInside ) {
184 
185  // std::cout << " found surface " << *surf << std::endl
186  // << " id : " << idDecoder.valueString()
187  // << " point : " << point
188  // << " is inside : " << isInside
189  // << " distance from surface : " << dist/dd4hep::mm << std::endl
190  // << std::endl ;
191 
192  // }
193 
194  // ====== test that moved hit points are outside their surface ================================
195 
196  Vector3D point3 = point + 1e-3 * surf->normal() ;
197  sst.str("") ;
198  sst << " point3 " << point3 << " is not on surface " ;
199  isInside = surf->insideBounds( point3) ;
200  test( isInside , false , sst.str() ) ;
201 
202 
203 
204  } else {
205 
206  std::cout << "ERROR: no surface found for id: " << idDecoder << std::endl ;
207  }
208 
209  }
210 
211  }
212 
213 
214  }
215  return 0;
216 }
217 
218 //=============================================================================
219 #include "main.h"
dd4hep::Detector::world
virtual DetElement world() const =0
Return reference to the top-most (world) detector element.
SurfaceManager.h
dd4hep::rec
Namespace for the reconstruction part of the AIDA detector description toolkit.
Definition: SurfaceInstaller.h:28
dd4hep::rec::ISurface::normal
virtual Vector3D normal(const Vector3D &point=Vector3D()) const =0
Access to the normal direction at the given point.
dd4hep::rec::Vector3D
Definition: Vector3D.h:32
Detector.h
dd4hep::rec::ISurface
Definition: ISurface.h:39
dd4hep::rec::ISurface::distance
virtual double distance(const Vector3D &point) const =0
main_wrapper
int main_wrapper(int argc, char **argv)
Definition: test_surfaces.cpp:43
dd4hep::rec::SurfaceMap
std::multimap< unsigned long, ISurface * > SurfaceMap
typedef for surface maps, keyed by the cellID
Definition: SurfaceManager.h:25
SurfaceHelper.h
dd4hep::rec::Surface::id
virtual long64 id() const
The id of this surface - corresponds to DetElement id.
Definition: Surface.cpp:611
SurfaceManager
Plugin that creates a SurfaceManager object and attaches it to description as a user extension object...
dd4hep::DDSegmentation::FieldID
int64_t FieldID
Definition: BitFieldCoder.h:25
dd4hep::rec::ISurface::insideBounds
virtual bool insideBounds(const Vector3D &point, double epsilon=1.e-4) const =0
Checks if the given point lies within the surface.
Surface.h
dd4hep::DDTest
Simple class for defining unit tests.
Definition: DDTest.h:21
dd4hep::DetElement
Handle class describing a detector element.
Definition: DetElement.h:188
dd4hep::rec::SurfaceList
Definition: Surface.h:681
dd4hep::detail
DD4hep internal namespace.
Definition: Alignments.h:32
dd4hep::rec::SurfaceHelper::surfaceList
const SurfaceList & surfaceList()
Definition: SurfaceHelper.h:40
DetectorSurfaces.h
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.
main.h
std
Definition: Plugins.h:30
dist
double dist(const Position &p0, const Position &p1)
Definition: test_cellid_position_converter.cpp:45
dd4hep::Detector::extension
IFACE * extension(bool alert=true) const
Access extension element by the type.
Definition: Detector.h:342
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
dd4hep::rec::Surface
Definition: Surface.h:498
DD4hepUnits.h
dd4hep::rec::SurfaceHelper
Definition: SurfaceHelper.h:29
dd4hep::rec::SurfaceManager::map
const SurfaceMap * map(const std::string name) const
Definition: SurfaceManager.cpp:43