DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
MaterialManager.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
11 //
12 //==========================================================================
13 #include "DDRec/MaterialManager.h"
14 #include "DD4hep/Exceptions.h"
15 #include "DD4hep/Detector.h"
16 
17 #include "TGeoVolume.h"
18 #include "TGeoManager.h"
19 #include "TGeoNode.h"
20 #include "TVirtualGeoTrack.h"
21 
22 #define MINSTEP 1.e-5
23 
24 namespace dd4hep {
25  namespace rec {
26 
27  MaterialManager::MaterialManager(Volume world) : _mV(0), _m( Material() ), _p0(),_p1(),_pos() {
28  _tgeoMgr = world->GetGeoManager();
29  }
30 
32 
33  }
34 
37  return _placeV;
38  }
39 
40  const MaterialVec& MaterialManager::materialsBetween(const Vector3D& p0, const Vector3D& p1 , double epsilon) {
41  if( ( p0 != _p0 ) || ( p1 != _p1 ) ) {
42  //---------------------------------------
43  _mV.clear() ;
44  _placeV.clear();
45  //
46  // algorithm copied from TGeoGearDistanceProperties.cc (A.Munnich):
47  //
48 
49  double startpoint[3], endpoint[3], direction[3];
50  double L=0;
51  for(unsigned int i=0; i<3; i++) {
52  startpoint[i] = p0[i];
53  endpoint[i] = p1[i];
54  direction[i] = endpoint[i] - startpoint[i];
55  L+=direction[i]*direction[i];
56  }
57  double totDist = sqrt( L ) ;
58 
59  //normalize direction
60  for(unsigned int i=0; i<3; i++)
61  direction[i]=direction[i]/totDist;
62 
63  _tgeoMgr->AddTrack(0, 12 ) ; // electron neutrino
64 
65  TGeoNode *node1 = _tgeoMgr->InitTrack(startpoint, direction);
66 
67  //check if there is a node at startpoint
68  if(!node1)
69  throw std::runtime_error("No geometry node found at given location. Either there is no node placed here or position is outside of top volume.");
70 
71  while ( !_tgeoMgr->IsOutside() ) {
72 
73  // TGeoNode *node2;
74  // TVirtualGeoTrack *track;
75 
76  // step to (and over) the next Boundary
77  TGeoNode * node2 = _tgeoMgr->FindNextBoundaryAndStep( 500, 1) ;
78 
79  if( !node2 || _tgeoMgr->IsOutside() )
80  break;
81 
82  const double *position = _tgeoMgr->GetCurrentPoint();
83  const double *previouspos = _tgeoMgr->GetLastPoint();
84 
85  double length = _tgeoMgr->GetStep();
86 
87  TVirtualGeoTrack *track = _tgeoMgr->GetLastTrack();
88 
89  //protection against infinitive loop in root which should not happen, but well it does...
90  //work around until solution within root can be found when the step gets very small e.g. 1e-10
91  //and the next boundary is never reached
92 
93 #if 1 //fg: is this still needed ?
94  if( length < MINSTEP ) {
95 
96  _tgeoMgr->SetCurrentPoint( position[0] + MINSTEP * direction[0],
97  position[1] + MINSTEP * direction[1],
98  position[2] + MINSTEP * direction[2] );
99 
100  length = _tgeoMgr->GetStep();
101  node2 = _tgeoMgr->FindNextBoundaryAndStep(500, 1) ;
102 
103  position = _tgeoMgr->GetCurrentPoint();
104  previouspos = _tgeoMgr->GetLastPoint();
105  }
106 #endif
107  // printf( " -- step length : %1.8e %1.8e %1.8e %1.8e %1.8e %1.8e %1.8e - %s \n" , length ,
108  // position[0], position[1], position[2], previouspos[0], previouspos[1], previouspos[2] , node1->GetMedium()->GetMaterial()->GetName() ) ;
109 
110  Vector3D posV( position ) ;
111 
112  double currDistance = ( posV - p0 ).r() ;
113 
114  // //if the next boundary is further than end point
115  // if(fabs(position[0])>fabs(endpoint[0]) || fabs(position[1])>fabs(endpoint[1])
116  // || fabs(position[2])>fabs(endpoint[2]))
117 
118  //if we travelled too far:
119  if( currDistance > totDist ) {
120 
121  length = sqrt( pow(endpoint[0]-previouspos[0],2) +
122  pow(endpoint[1]-previouspos[1],2) +
123  pow(endpoint[2]-previouspos[2],2) );
124 
125  track->AddPoint( endpoint[0], endpoint[1], endpoint[2], 0. );
126 
127 
128  if( length > epsilon ) {
129  _mV.emplace_back(node1->GetMedium(), length );
130  _placeV.emplace_back(node1,length);
131  }
132  break;
133  }
134 
135  track->AddPoint( position[0], position[1], position[2], 0.);
136 
137  if( length > epsilon ) {
138  _mV.emplace_back(node1->GetMedium(), length);
139  _placeV.emplace_back(node1,length);
140  }
141  node1 = node2;
142  }
143 
144 
145  //fg: protect against empty list:
146  if( _mV.empty() ){
147  _mV.emplace_back(node1->GetMedium(), totDist);
148  _placeV.emplace_back(node1,totDist);
149  }
150 
151 
152  _tgeoMgr->ClearTracks();
153 
154  _tgeoMgr->CleanGarbage();
155 
156  //---------------------------------------
157 
158  _p0 = p0 ;
159  _p1 = p1 ;
160  }
161 
162  return _mV ;
163  }
164 
165 
167  if( pos != _pos ) {
168  TGeoNode *node = _tgeoMgr->FindNode( pos[0], pos[1], pos[2] ) ;
169  if( ! node ) {
170  std::stringstream err ;
171  err << " MaterialManager::material: No geometry node found at location: " << pos ;
172  throw std::runtime_error( err.str() );
173  }
174  _m = Material( node->GetMedium() );
175  _pv = node;
176  _pos = pos ;
177  }
178  return _m ;
179  }
180 
182  if( pos != _pos ) {
183  TGeoNode *node = _tgeoMgr->FindNode( pos[0], pos[1], pos[2] ) ;
184  if( ! node ) {
185  std::stringstream err ;
186  err << " MaterialManager::material: No geometry node found at location: " << pos ;
187  throw std::runtime_error( err.str() );
188  }
189  _m = Material( node->GetMedium() );
190  _pv = node;
191  _pos = pos;
192  }
193  return _pv;
194  }
195 
197 
198  std::stringstream sstr ;
199 
200  double sum_l = 0 ;
201  double sum_rho_l = 0 ;
202  double sum_rho_l_over_A = 0 ;
203  double sum_rho_l_Z_over_A = 0 ;
204  //double sum_rho_l_over_x = 0 ;
205  double sum_l_over_x = 0 ;
206  //double sum_rho_l_over_lambda = 0 ;
207  double sum_l_over_lambda = 0 ;
208 
209  for(unsigned i=0,n=materials.size(); i<n ; ++i){
210 
211  Material mat = materials[i].first ;
212  double l = materials[i].second ;
213 
214  if( i != 0 ) sstr << "_" ;
215  sstr << mat.name() << "_" << l ;
216 
217  double rho = mat.density() ;
218  double A = mat.A() ;
219  double Z = mat.Z() ;
220  double x = mat.radLength() ;
221  double lambda = mat.intLength() ;
222 
223  sum_l += l ;
224  sum_rho_l += rho * l ;
225  sum_rho_l_over_A += rho * l / A ;
226  sum_rho_l_Z_over_A += rho * l * Z / A ;
227  sum_l_over_x += l / x ;
228  sum_l_over_lambda += l / lambda ;
229  // sum_rho_l_over_x += rho * l / x ;
230  // sum_rho_l_over_lambda += rho * l / lambda ;
231  }
232 
233  double rho = sum_rho_l / sum_l ;
234 
235  double A = sum_rho_l / sum_rho_l_over_A ;
236  double Z = sum_rho_l_Z_over_A / sum_rho_l_over_A ;
237 
238  // radiation and interaction lengths already given in cm - average by length
239 
240  // double x = sum_rho_l / sum_rho_l_over_x ;
241  double x = sum_l / sum_l_over_x ;
242 
243  // double lambda = sum_rho_l / sum_rho_l_over_lambda ;
244  double lambda = sum_l / sum_l_over_lambda ;
245 
246 
247  return MaterialData( sstr.str() , Z, A, rho, x, lambda ) ;
248 
249  }
250 
251  } /* namespace rec */
252 } /* namespace dd4hep */
dd4hep::rec::MaterialManager::materialAt
const Material & materialAt(const Vector3D &pos)
Definition: MaterialManager.cpp:166
dd4hep::rec::MaterialManager::_mV
MaterialVec _mV
Cached materials.
Definition: MaterialManager.h:83
dd4hep::rec::Vector3D
Definition: Vector3D.h:32
Detector.h
dd4hep::rec::MaterialManager::createAveragedMaterial
MaterialData createAveragedMaterial(const MaterialVec &materials)
Definition: MaterialManager.cpp:196
dd4hep::PlacedVolume
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:163
dd4hep::rec::MaterialManager::_p1
Vector3D _p1
Definition: MaterialManager.h:89
dd4hep::rec::MaterialVec
std::vector< std::pair< Material, double > > MaterialVec
Definition: MaterialManager.h:30
dd4hep::Handle::name
const char * name() const
Access the object name (or "" if not supported by the object)
dd4hep::Material::intLength
double intLength() const
Access the interaction length of the underlying material.
Definition: Objects.cpp:221
dd4hep::rec::MaterialManager::_pv
PlacedVolume _pv
Cached nodes.
Definition: MaterialManager.h:86
dd4hep::Material::A
double A() const
atomic number of the underlying material
Definition: Objects.cpp:188
dd4hep::rec::MaterialManager::_pos
Vector3D _pos
Definition: MaterialManager.h:89
epsilon
const double epsilon
Definition: test_cellid_position_converter.cpp:41
dd4hep::Material::density
double density() const
density of the underlying material
Definition: Objects.cpp:199
dd4hep::rec::MaterialManager::~MaterialManager
~MaterialManager()
Definition: MaterialManager.cpp:31
dd4hep::Material
Handle class describing a material.
Definition: Objects.h:272
dd4hep::Volume
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:370
dd4hep::rec::MaterialManager::_m
Material _m
Definition: MaterialManager.h:84
dd4hep::Material::Z
double Z() const
proton number of the underlying material
Definition: Objects.cpp:176
dd4hep::rec::MaterialManager::placementsBetween
const PlacementVec & placementsBetween(const Vector3D &p0, const Vector3D &p1, double epsilon=1e-4)
Definition: MaterialManager.cpp:35
dd4hep::rec::MaterialManager::_p0
Vector3D _p0
cached last points
Definition: MaterialManager.h:89
dd4hep::rec::MaterialManager::_placeV
PlacementVec _placeV
Definition: MaterialManager.h:87
dd4hep::rec::MaterialManager::placementAt
PlacedVolume placementAt(const Vector3D &pos)
Definition: MaterialManager.cpp:181
dd4hep::rec::MaterialManager::materialsBetween
const MaterialVec & materialsBetween(const Vector3D &p0, const Vector3D &p1, double epsilon=1e-4)
Definition: MaterialManager.cpp:40
dd4hep::rec::MaterialData
Definition: Material.h:33
MaterialManager.h
dd4hep::rec::PlacementVec
std::vector< std::pair< PlacedVolume, double > > PlacementVec
Definition: MaterialManager.h:31
dd4hep::rec::MaterialManager::MaterialManager
MaterialManager()=delete
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::rec::MaterialManager::_tgeoMgr
TGeoManager * _tgeoMgr
Reference to the TGeoManager.
Definition: MaterialManager.h:91
dd4hep::Material::radLength
double radLength() const
Access the radiation length of the underlying material.
Definition: Objects.cpp:210
MINSTEP
#define MINSTEP
Definition: MaterialManager.cpp:22
Exceptions.h