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