All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
VXDGeometry.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 #include "VXDGeometry.h"
3 #include <gear/VXDParameters.h>
4 #include <gear/VXDLayerLayout.h>
5 
6 #include "streamlog/streamlog.h"
7 // #include <iostream>
8 
9 #include <gsl/gsl_randist.h>
10 
11 #include <cmath>
12 #include <math.h>
13 
14 
15 bool isEqual(double d0 , double d1 ) {
16 
17  static double EPSILON = 1e-12 ;
18 
19  if( d1 == d0 )
20  return true ;
21 
22  if( std::abs( d1 ) < EPSILON && std::abs( d0 ) < EPSILON )
23  return true ;
24 
25  if( std::abs( d0 - d1 ) < EPSILON ) // absolute difference small
26  return true ;
27 
28  // return relative difference smaller than epsilon
29  return ( 2. * std::abs ( d0 - d1 ) / (std::abs( d1 ) + std::abs( d0 )) ) < EPSILON ;
30 
31 
32 }
34 
35  return
36  isEqual( v0[0] , v1[0] ) &&
37  isEqual( v0[1] , v1[1] ) &&
38  isEqual( v0[2] , v1[2] ) ;
39 }
40 
41 
42 
43 VXDGeometry::VXDGeometry(gear::GearMgr* gearMgr) :
44  _gearMgr( gearMgr ) {
45 
46  init() ;
47 
48 }
49 
51 
52  // init VXD parameters ....
53  const gear::VXDParameters& gearVXD = _gearMgr->getVXDParameters() ;
54  const gear::VXDLayerLayout& layerVXD = gearVXD.getVXDLayerLayout();
55 
56 
57  streamlog_out( DEBUG0 ) << " initializing VXD ladder geometry from gear ... "
58  << std::endl ;
59 
60  _vxdLadders.resize( layerVXD.getNLayers() ) ;
61  _vxdLayers.resize( layerVXD.getNLayers() ) ;
62 
63 
64  // shell gap - same for all layers
65  double gap = gearVXD.getShellGap () ;
66 
67  for( int i=0 ; i < layerVXD.getNLayers() ; i++ ) {
68 
69  double phi0 = layerVXD.getPhi0 (i) ;
70  double dist = layerVXD.getSensitiveDistance (i) ;
71 
72  double thick = layerVXD.getSensitiveThickness (i) ;
73  double offs = layerVXD.getSensitiveOffset (i) ;
74  double width = layerVXD.getSensitiveWidth (i) ;
75 
76  // -----fg: gear length is half length really !!!!!
77  double len = layerVXD.getSensitiveLength (i) ;
78 
79 
80  int nLad = layerVXD.getNLadders(i) ;
81 
82  // double rMin ;
83  // double rMax ;
84  // double length ;
85  // double width ;
86  // double ladderArea ;
87  // int nLadders ;
88 
89  _vxdLayers[i].width = width ;
90  _vxdLayers[i].length = layerVXD.getSensitiveLength (i) ;
91 
92  _vxdLayers[i].gap = gap ;
93  _vxdLayers[i].thickness = thick ;
94 
95  _vxdLayers[i].ladderArea = 2 * width * len ;
96  _vxdLayers[i].nLadders = nLad ;
97 
98  double locA = dist+thick ;
99  double locB = width/2. + std::abs(offs) ;
100 
101  _vxdLayers[i].rMax = std::sqrt( locA*locA + locB*locB ) ;
102  _vxdLayers[i].rMin = dist ;
103 
104  streamlog_out( DEBUG4 ) << " layer: " << i
105  << " phi0 : " << phi0
106  << " offs : " << offs
107  << " width : " << width
108  << " length : " << _vxdLayers[i].length
109  << " ladderArea : " << _vxdLayers[i].ladderArea
110  << " rMin : " << _vxdLayers[i].rMin
111  << " rMax : " << _vxdLayers[i].rMax
112  << std::endl ;
113 
114  _vxdLadders[i].resize( nLad ) ;
115 
116  for( int j=0 ; j < nLad ; j++ ) {
117 
118  // rotation around z-axis
119  double phi = phi0 + j * ( 2 * M_PI ) / nLad ;
120 
121 
122  // translation vector
123  gear::Vector3D t0( dist + thick / 2. , phi, 0., gear::Vector3D::cylindrical ) ;
124 
125  double sign = offs / std::abs( offs ) ;
126 
127  gear::Vector3D t1( std::abs( offs ),
128  phi + sign * M_PI / 2.,
129  0, // keep z coordinate
130  gear::Vector3D::cylindrical) ;
131 
132  VXDLadder& l = _vxdLadders[i][j] ;
133  l.phi = phi ;
134  l.trans = t0 + t1 ;
135 
136  }
137  }
138 }
139 
140 
141 gear::Vector3D VXDGeometry::lab2LadderPos( gear::Vector3D labPos, int layerID, int ladderID) {
142 
143  gear::Vector3D u = labPos - _vxdLadders[layerID][ladderID].trans ;
144 
145  return gear::Vector3D( u.rho(),
146  u.phi() - _vxdLadders[layerID][ladderID].phi,
147  u.z(),
148  gear::Vector3D::cylindrical ) ;
149 }
150 
151 gear::Vector3D VXDGeometry::ladder2LabPos( gear::Vector3D ladderPos, int layerID, int ladderID){
152 
153  gear::Vector3D r( ladderPos.rho(),
154  ladderPos.phi() + _vxdLadders[layerID][ladderID].phi,
155  ladderPos.z(),
156  gear::Vector3D::cylindrical ) ;
157 
158  return r + _vxdLadders[layerID][ladderID].trans ;
159 
160 }
161 
162 gear::Vector3D VXDGeometry::lab2LadderDir( gear::Vector3D labDir, int layerID, int ladderID) {
163 
164  return gear::Vector3D( labDir.rho(),
165  labDir.phi() - _vxdLadders[layerID][ladderID].phi,
166  labDir.z(),
167  gear::Vector3D::cylindrical ) ;
168 }
169 
170 gear::Vector3D VXDGeometry::ladder2LabDir( gear::Vector3D ladderDir, int layerID, int ladderID){
171 
172  return gear::Vector3D( ladderDir.rho(),
173  ladderDir.phi() + _vxdLadders[layerID][ladderID].phi,
174  ladderDir.z(),
175  gear::Vector3D::cylindrical ) ;
176 
177 }
178 
179 
180 
181 std::pair<int,int> VXDGeometry::getLadderID( gear::Vector3D labPos, int layerID ) {
182 
183  if( layerID < 0 ) { // need to search the layer first....
184 
185  double r = labPos.rho() ;
186  unsigned n = _vxdLayers.size() ;
187 
188  for(unsigned i = 0 ; i < n ; ++i ) { // loop over all layers
189 
190  if( _vxdLayers[i].rMin <= r && r <= _vxdLayers[i].rMax ) { // candidate layer
191 
192  // bool isSensitive = false ;
193  unsigned m = _vxdLadders[i].size() ;
194 
195  for(unsigned j = 0 ; j < m ; ++j ) { // check
196 
197  gear::Vector3D l = lab2LadderPos( labPos , i , j ) ;
198 
199  double z_abs = std::abs( l.z() ) - _vxdLayers[i].gap / 2. ;
200 
201  if( ( 0 < z_abs && z_abs < _vxdLayers[i].length ) &&
202  ( std::abs( l.y() ) < _vxdLayers[i].width / 2. ) &&
203  ( std::abs( l.x() ) < _vxdLayers[i].thickness / 2. ) ) {
204 
205  return std::make_pair( i , j ) ;
206  }
207  }
208  }
209  }
210  return std::make_pair( -1, -1 ) ;
211  }
212 
213  unsigned m = _vxdLadders[layerID].size() ;
214 
215  for(unsigned j = 0 ; j < m ; ++j ) { // check
216 
217  gear::Vector3D l = lab2LadderPos( labPos , layerID , j ) ;
218 
219  double z_abs = std::abs( l.z() ) - _vxdLayers[layerID].gap / 2. ;
220 
221  if( ( 0 < z_abs && z_abs < _vxdLayers[layerID].length ) &&
222  ( std::abs( l.y() ) < _vxdLayers[layerID].width / 2. ) &&
223  ( std::abs( l.x() ) < _vxdLayers[layerID].thickness / 2. ) ) {
224 
225  return std::make_pair( layerID , j ) ;
226  }
227  }
228  return std::make_pair( layerID , -1 ) ;
229 }
230 
231 
232 
233 
235 
236  int nHit = 1000 ;
237 
238  // initialize gsl random generator
239  // ranlux algorithm of Lüscher, which produces 'luxury random numbers'
240  gsl_rng* r = gsl_rng_alloc(gsl_rng_ranlxs2) ;
241 
242 
243  const gear::VXDParameters& gearVXD = _gearMgr->getVXDParameters() ;
244  double hgap = gearVXD.getShellGap() / 2. ;
245 
246  for(unsigned i=0 ; i< _vxdLayers.size() ; ++i) {
247 
248  for(unsigned j=0 ; j< _vxdLadders[i].size() ; ++j) {
249 
250 
251  double width = _vxdLayers[i].width ;
252  double len = _vxdLayers[i].length ;
253 
254  // FIXME: what about the sensitive gap ?
255 
256  streamlog_out( MESSAGE ) << " testing " << nHit << " random points for ladder "
257  << j << " of layer " << i << std::endl ;
258 
259  bool isOK = true ;
260 
261  for( int k=0 ; k< nHit ; k++){
262 
263  double l1 = gsl_rng_uniform( r ) ;
264  double l2 = gsl_rng_uniform( r ) ;
265 
266  // ------ compute a coordinate in the labframe
267  // - (x,y) origin is in the center of the ladder:
268  double k1 = 0.5 - l1 ;
269  // - z origin is at z==0 in the lab
270  double k2 = ( -1. + (k % 2) * 2. ) * l2 ; // -1. k even , +1. k odd
271 
272 
273  gear::Vector3D ladRnd( 0 , k1 * width, k2 * len + hgap ) ;
274 
275  gear::Vector3D lab = ladder2LabPos( ladRnd, i , j ) ;
276 
277  gear::Vector3D ladder = lab2LadderPos( lab, i , j ) ;
278 
279 
280 
281  if( ! isEqual( ladRnd , ladder ) ) {
282 
283 
284  isOK = false ;
285 
286  streamlog_out( WARNING ) << " l1,l2: " << l1 << ", " << l2 << std::endl
287  << " ladRnd: " << ladRnd
288  << " lab : " << lab
289  << " ladder: " << ladder
290  << std::endl ;
291 
292 
293  streamlog_out( DEBUG4 ) << "isEqual(ladRnd[0],ladder[0]): "<< isEqual(ladRnd[0],ladder[0] )
294  << std::endl
295  << "isEqual(ladRnd[1],ladder[1]): "<< isEqual(ladRnd[1],ladder[1] )
296  << std::endl
297  << "isEqual(ladRnd[2],ladder[2]): "<< isEqual(ladRnd[2],ladder[2] )
298  << std::endl ;
299 
300  }
301 
302 
303  if( ! gearVXD.isPointInSensitive( lab ) ){
304 
305  isOK = false ;
306 
307  streamlog_out( WARNING ) << " created hit outside sensitve volume " << lab << std::endl ;
308  }
309 
310 
311  std::pair<int,int> id = getLadderID( lab ) ;
312  if( (unsigned)id.first != i ){
313 
314  isOK = false ;
315 
316  streamlog_out( WARNING ) << " getLadderID layer wrong : " << id.first
317  << " instead of " << i << std::endl ;
318  }
319  if( (unsigned)id.second != j ){
320 
321  isOK = false ;
322 
323  streamlog_out( WARNING ) << " getLadderID ladder wrong : " << id.second
324  << " instead of " << j << " layer " << i << std::endl ;
325  }
326 
327 
328 
329  if( id != getLadderID( lab , i ) ){
330 
331  isOK = false ;
332 
333  streamlog_out( WARNING ) << " getLadderID with ID=" <<i << " - ladder wrong : "
334  << id.second<< std::endl ;
335  }
336 
337 
338 
339  streamlog_out( DEBUG ) << " ladRnd: " << ladRnd
340  << " lab : " << lab
341  << " ladder: " << ladder
342  << std::endl ;
343 
344  }
345  if (isOK )
346  streamlog_out( MESSAGE ) << " tested " << nHit << " random points for ladder "
347  << j << " of layer " << i << " OK ! " << std::endl ;
348 
349  }
350  } // ------ loop over layers
351 
352 }
bool isEqual(double d0, double d1)
Definition: VXDGeometry.cc:15
double phi
Definition: VXDGeometry.h:18
static const float m
gear::Vector3D trans
Definition: VXDGeometry.h:19
gear::Vector3D ladder2LabPos(gear::Vector3D ladderPos, int layerID, int ladderID)
Convert a position in local ladder coordinates (x_ladder==0 is the middle of the sensitive) to the la...
Definition: VXDGeometry.cc:151
CLHEP::Hep3Vector Vector3D
static const float k
gear::Vector3D lab2LadderDir(gear::Vector3D labPos, int layerID, int ladderID)
Convert a direction in the lab frame to local ladder coordinates (x_ladder==0 is the middle of the se...
Definition: VXDGeometry.cc:162
gear::Vector3D lab2LadderPos(gear::Vector3D labPos, int layerID, int ladderID)
Convert a position in the lab frame to local ladder coordinates (x_ladder==0 is the middle of the sen...
Definition: VXDGeometry.cc:141
static const float e
VXDLayers _vxdLayers
Definition: VXDGeometry.h:98
void init()
Definition: VXDGeometry.cc:50
gear::GearMgr * _gearMgr
Definition: VXDGeometry.h:96
gear::Vector3D ladder2LabDir(gear::Vector3D ladderPos, int layerID, int ladderID)
Convert a direction in local ladder coordinates (x_ladder==0 is the middle of the sensitive) to the l...
Definition: VXDGeometry.cc:170
std::pair< int, int > getLadderID(gear::Vector3D labPos, int layerID=-1)
Return the pair (layerID, ladderID) for the given position, (-1,-1) if not in sensitive volume (in th...
Definition: VXDGeometry.cc:181
VXDLadders _vxdLadders
Definition: VXDGeometry.h:97