]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/UPGRADE/AliITSUGeomTGeo.cxx
Why the h*ll do we make a remote commit when pulling?
[u/mrichter/AliRoot.git] / ITS / UPGRADE / AliITSUGeomTGeo.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 ///////////////////////////////////////////////////////////////////////////
17 //    AliITSUGeomTGeo is a simple interface class to TGeoManager       //
18 //    It is used in the simulation and reconstruction in order to        //
19 //    query the TGeo ITS geometry                                        //
20 //                                                                       //
21 //    author - cvetan.cheshkov@cern.ch                                   //
22 //    15/02/2007                                                         //
23 //    adapted to ITSupg 18/07/2012 - ruben.shahoyan@cern.ch              //
24 //                                                                       //
25 //    ATTENTION: In opposite to ols AliITSgeomTGeo, all indices start    //
26 //    from 0, not from 1!!!                                              //
27 //                                                                       //
28 ///////////////////////////////////////////////////////////////////////////
29
30 #include <TClass.h>
31 #include <TString.h>
32 #include <TGeoManager.h>
33 #include <TGeoPhysicalNode.h>
34 #include <TDatime.h>
35 #include <TMath.h>
36 #include <TSystem.h>
37
38 #include "AliITSUGeomTGeo.h"
39 #include "AliLog.h"
40 #include "AliAlignObj.h"
41 #include "AliITSsegmentation.h"
42 #include "AliITSUSegmentationPix.h"
43 using namespace TMath;
44
45 ClassImp(AliITSUGeomTGeo)
46
47 UInt_t AliITSUGeomTGeo::fgUIDShift = 16;                // bit shift to go from mod.id to modUUID for TGeo
48 const char* AliITSUGeomTGeo::fgkITSVolName = "ITSV";
49 const char* AliITSUGeomTGeo::fgkITSLrName  = "ITSULayer";
50 const char* AliITSUGeomTGeo::fgkITSLadName = "ITSULadder";
51 const char* AliITSUGeomTGeo::fgkITSModName = "ITSUModule";
52 const char* AliITSUGeomTGeo::fgkITSSensName ="ITSUSensor";
53 const char* AliITSUGeomTGeo::fgkITSWrapVolName = "ITSUWrapVol";
54 const char* AliITSUGeomTGeo::fgkITSDetTypeName[AliITSUGeomTGeo::kNDetTypes] = {"Pix"};
55 //
56 TString     AliITSUGeomTGeo::fgITSsegmFileName = "itsSegmentations.root";
57
58 //______________________________________________________________________
59 AliITSUGeomTGeo::AliITSUGeomTGeo(Bool_t build, Bool_t loadSegm)
60   :fVersion(kITSVNA)
61   ,fNLayers(0)
62   ,fNModules(0)
63   ,fNLadders(0)
64   ,fLrDetType(0)
65   ,fNDetectors(0)
66   ,fLastModIndex(0)
67   ,fMatSens(0)
68   ,fMatT2L(0)
69   ,fSegm(0)
70 {
71   // default c-tor
72   for (int i=AliITSUAux::kMaxLayers;i--;) fLr2Wrapper[i] = -1;
73   if (build) BuildITS(loadSegm);
74 }
75
76 //______________________________________________________________________
77 AliITSUGeomTGeo::AliITSUGeomTGeo(const AliITSUGeomTGeo &src)
78   :TObject(src)
79   ,fVersion(src.fVersion)
80   ,fNLayers(src.fNLayers)
81   ,fNModules(src.fNModules)
82   ,fNLadders(0)
83   ,fLrDetType(0)
84   ,fNDetectors(0)
85   ,fLastModIndex(0)
86   ,fMatSens(0)
87   ,fMatT2L(0)
88   ,fSegm(0)
89 {
90   // copy c-tor
91   if (fNLayers) {
92     fNLadders   = new Int_t[fNLayers];
93     fNDetectors = new Int_t[fNLayers];
94     fLrDetType  = new Int_t[fNLayers];
95     fLastModIndex   = new Int_t[fNLayers];
96     for (int i=fNLayers;i--;) {
97       fNLadders[i] = src.fNLadders[i];
98       fNDetectors[i] = src.fNDetectors[i];
99       fLrDetType[i]  = src.fLrDetType[i];
100       fLastModIndex[i] = src.fLastModIndex[i];
101     }
102     if (src.fMatSens) {
103       fMatSens = new TObjArray(fNModules);
104       fMatSens->SetOwner(kTRUE);
105       for (int i=0;i<fNModules;i++) {
106         const TGeoHMatrix* mat = (TGeoHMatrix*)src.fMatSens->At(i);
107         fMatSens->AddAt(new TGeoHMatrix(*mat),i);
108       }
109     }
110     if (src.fMatT2L) {
111       fMatT2L = new TObjArray(fNModules);
112       fMatT2L->SetOwner(kTRUE);
113       for (int i=0;i<fNModules;i++) {
114         const TGeoHMatrix* mat =(TGeoHMatrix*) src.fMatT2L->At(i);
115         fMatT2L->AddAt(new TGeoHMatrix(*mat),i);
116       }
117     }
118     if (src.fSegm) {
119       int sz = src.fSegm->GetEntriesFast();
120       fSegm = new TObjArray(sz);
121       fSegm->SetOwner(kTRUE);
122       for (int i=0;i<sz;i++) {
123         AliITSsegmentation* sg = (AliITSsegmentation*)src.fSegm->UncheckedAt(i);
124         if (!sg) continue;
125         fSegm->AddAt(sg->Clone(),i);
126       }
127     }
128   }
129   for (int i=AliITSUAux::kMaxLayers;i--;) fLr2Wrapper[i] = src.fLr2Wrapper[i];
130 }
131
132 //______________________________________________________________________
133 AliITSUGeomTGeo::~AliITSUGeomTGeo()
134 {
135   //d-tor
136   delete[] fNLadders;
137   delete[] fLrDetType;
138   delete[] fNDetectors;
139   delete[] fLastModIndex;
140   delete fMatT2L;
141   delete fMatSens;
142   delete fSegm;
143 }
144
145
146 //______________________________________________________________________
147 AliITSUGeomTGeo& AliITSUGeomTGeo::operator=(const AliITSUGeomTGeo &src)
148 {
149   // cp op.
150   if (this!=&src) {
151     delete[] fNLadders;
152     delete[] fLrDetType;
153     delete[] fNDetectors;
154     delete[] fLastModIndex;
155     fNLadders = fLrDetType = fNDetectors = fLastModIndex = 0;
156     fVersion = src.fVersion;
157     fNLayers = src.fNLayers;
158     fNModules = src.fNModules;
159     if (src.fMatSens) {
160       delete fMatSens; 
161       fMatSens = new TObjArray(fNModules);
162       fMatSens->SetOwner(kTRUE);
163       for (int i=0;i<fNModules;i++) {
164         const TGeoHMatrix* mat = (TGeoHMatrix*) src.fMatSens->At(i);
165         fMatSens->AddAt(new TGeoHMatrix(*mat),i);
166       }
167     }
168     if (src.fMatT2L) {
169       delete fMatT2L; 
170       fMatT2L = new TObjArray(fNModules);
171       fMatT2L->SetOwner(kTRUE);
172       for (int i=0;i<fNModules;i++) {
173         const TGeoHMatrix* mat = (TGeoHMatrix*) src.fMatT2L->At(i);
174         fMatT2L->AddAt(new TGeoHMatrix(*mat),i);
175       }
176     }
177     if (src.fSegm) {
178       int sz = src.fSegm->GetEntriesFast();
179       fSegm = new TObjArray(sz);
180       fSegm->SetOwner(kTRUE);
181       for (int i=0;i<sz;i++) {
182         AliITSsegmentation* sg = (AliITSsegmentation*)src.fSegm->UncheckedAt(i);
183         if (!sg) continue;
184         fSegm->AddAt(sg->Clone(),i);
185       }
186     }
187     //
188     if (fNLayers) {
189       fNLadders   = new Int_t[fNLayers];
190       fNDetectors = new Int_t[fNLayers];
191       fLrDetType  = new Int_t[fNLayers];
192       fLastModIndex   = new Int_t[fNLayers];
193       for (int i=fNLayers;i--;) {
194         fNLadders[i] = src.fNLadders[i];
195         fNDetectors[i] = src.fNDetectors[i];
196         fLrDetType[i]  = src.fLrDetType[i];
197         fLastModIndex[i] = src.fLastModIndex[i];
198       }
199     }    
200   }
201   return *this;
202 }
203
204 //______________________________________________________________________
205 Int_t AliITSUGeomTGeo::GetModuleIndex(Int_t lay,Int_t lad,Int_t det) const
206 {
207   // This routine computes the module index number from the layer,
208   // ladder, and detector numbers. The number of ladders and detectors
209   // per layer is set statically
210   // see above for details.
211   // Inputs:
212   //    Int_t lay  The layer number. Starting from 0.
213   //    Int_t lad  The ladder number. Starting from 0
214   //    Int_t det  The detector number in the ladder. Starting from 0
215   //
216   return GetFirstModIndex(lay) + fNDetectors[lay]*lad + det;
217 }
218
219 //______________________________________________________________________
220 Bool_t AliITSUGeomTGeo::GetLayer(Int_t index,Int_t &lay,Int_t &index2)  const
221 {
222   // This routine computes the layer number for a
223   // given the module index. The 
224   // Inputs:
225   //     Int_t index  The module index number, starting from zero.
226   // Outputs:
227   //     Int_t index2 The module index inside a layer, starting from zero.
228   //     Int_t lay    The layer number. Starting from 0.
229   //
230   lay = GetLayer(index);
231   index2 = index - GetFirstModIndex(lay);
232   return kTRUE;
233   //
234 }
235
236 //______________________________________________________________________
237 Int_t AliITSUGeomTGeo::GetLayer(Int_t index) const
238 {
239   // Get module layer, from 0
240   //
241   int lay = 0;
242   while(index>fLastModIndex[lay]) lay++;
243   return lay;
244 }
245
246 //______________________________________________________________________
247 Int_t AliITSUGeomTGeo::GetLadder(Int_t index) const
248 {
249   // Get module ladder, from 0
250   //
251   int lay = 0;
252   while(index>fLastModIndex[lay]) lay++;
253   index -= GetFirstModIndex(lay);
254   return index/fNDetectors[lay];
255 }
256
257 //______________________________________________________________________
258 Int_t AliITSUGeomTGeo::GetModIdInLayer(Int_t index) const
259 {
260   // Get module number within layer, from 0
261   //
262   int lay = 0;
263   while(index>fLastModIndex[lay]) lay++;
264   index -= GetFirstModIndex(lay);
265   return index;
266 }
267
268 //______________________________________________________________________
269 Int_t AliITSUGeomTGeo::GetModIdInLadder(Int_t index) const
270 {
271   // Get module number within ladder, from 0
272   //
273   int lay = 0;
274   while(index>fLastModIndex[lay]) lay++;
275   index -= GetFirstModIndex(lay);
276   return index%fNDetectors[lay];
277 }
278
279 //______________________________________________________________________
280 Bool_t AliITSUGeomTGeo::GetModuleId(Int_t index,Int_t &lay,Int_t &lad,Int_t &det)  const
281 {
282   // The method is taken from the old AliITSgeom class by Bjorn Nilsen
283   //
284   // This routine computes the layer, ladder and detector number 
285   // given the module index number. 
286   // Inputs:
287   //     Int_t index  The module index number, starting from zero.
288   // Outputs:
289   //     Int_t lay    The layer number. Starting from 0
290   //     Int_t lad    The ladder number. Starting from 0
291   //     Int_t det    The detector number. Starting from 0
292   //
293   lay  = GetLayer(index);
294   index -= GetFirstModIndex(lay);
295   lad  = index/fNDetectors[lay];
296   det  = index%fNDetectors[lay];
297   return kTRUE;
298 }
299
300 //______________________________________________________________________
301 const char* AliITSUGeomTGeo::GetSymName(Int_t index)  const
302 {
303   // Get the TGeoPNEntry symbolic name
304   // for a given module identified by 'index'
305   //
306   Int_t lay, index2;
307   if (!GetLayer(index,lay,index2)) return NULL;
308   // return AliGeomManager::SymName((AliGeomManager::ELayerID)((lay-1)+AliGeomManager::kSPD1),index2);
309   // RS: this is not optimal, but we cannod access directly AliGeomManager, since the latter has hardwired layers 
310   //  TGeoPNEntry* pne = gGeoManager->GetAlignableEntryByUID( AliGeomManager::LayerToVolUID(lay+1,index2) );
311   TGeoPNEntry* pne = gGeoManager->GetAlignableEntryByUID( ModuleVolUID(index) );
312   if (!pne) {
313     AliError(Form("Failed to find alignable entry with index %d: (Lr%d Mod:%d) !",index,lay,index2));
314     return NULL;
315   }
316   return pne->GetName();
317 }
318
319 //______________________________________________________________________
320 const char* AliITSUGeomTGeo::ComposeSymNameITS()
321 {
322   // sym name of the layer
323   return "ITS";
324 }
325
326 //______________________________________________________________________
327 const char* AliITSUGeomTGeo::ComposeSymNameLayer(Int_t lr)
328 {
329   // sym name of the layer
330   return Form("%s/%s%d",ComposeSymNameITS(),GetITSLayerPattern(),lr);
331 }
332
333 //______________________________________________________________________
334 const char* AliITSUGeomTGeo::ComposeSymNameLadder(Int_t lr, Int_t ladder)
335 {
336   // sym name of the ladder at given layer
337   return Form("%s/%s%d",ComposeSymNameLayer(lr),GetITSLadderPattern(),ladder);
338 }
339
340 //______________________________________________________________________
341 const char* AliITSUGeomTGeo::ComposeSymNameModule(Int_t lr, Int_t lad, int det)
342 {
343   // sym name of the module
344   return Form("%s/%s%d",ComposeSymNameLadder(lr,lad),GetITSModulePattern(),det);
345 }
346
347 //______________________________________________________________________
348 TGeoHMatrix* AliITSUGeomTGeo::GetMatrix(Int_t index)  const
349 {
350   // Get the transformation matrix for a given module 'index'
351   // by quering the TGeoManager
352   static TGeoHMatrix matTmp;
353   TGeoPNEntry *pne = GetPNEntry(index);
354   if (!pne) return NULL;
355
356   TGeoPhysicalNode *pnode = pne->GetPhysicalNode();
357   if (pnode) return pnode->GetMatrix();
358
359   const char* path = pne->GetTitle();
360   gGeoManager->PushPath(); // Preserve the modeler state.
361   if (!gGeoManager->cd(path)) {
362     gGeoManager->PopPath();
363     AliError(Form("Volume path %s not valid!",path));
364     return NULL;
365   }
366   matTmp = *gGeoManager->GetCurrentMatrix();
367   gGeoManager->PopPath();
368   return &matTmp;
369 }
370
371 //______________________________________________________________________
372 Bool_t AliITSUGeomTGeo::GetTranslation(Int_t index, Double_t t[3])  const
373 {
374   // Get the translation vector for a given module 'index'
375   // by quering the TGeoManager
376   TGeoHMatrix *m = GetMatrix(index);
377   if (!m) return kFALSE;
378
379   Double_t *trans = m->GetTranslation();
380   for (Int_t i = 0; i < 3; i++) t[i] = trans[i];
381
382   return kTRUE;
383 }
384
385 //______________________________________________________________________
386 Bool_t AliITSUGeomTGeo::GetRotation(Int_t index, Double_t r[9])  const
387 {
388   // Get the rotation matrix for a given module 'index'
389   // by quering the TGeoManager
390   TGeoHMatrix *m = GetMatrix(index);
391   if (!m) return kFALSE;
392
393   Double_t *rot = m->GetRotationMatrix();
394   for (Int_t i = 0; i < 9; i++) r[i] = rot[i];
395
396   return kTRUE;
397 }
398
399 //______________________________________________________________________
400 Bool_t AliITSUGeomTGeo::GetOrigMatrix(Int_t index, TGeoHMatrix &m) const
401 {
402   // Get the original (ideal geometry) TGeo matrix for
403   // a given module identified by 'index'.
404   // The method is slow, so it should be used
405   // with great care.
406   m.Clear();
407
408   const char *symname = GetSymName(index);
409   if (!symname) return kFALSE;
410
411   return AliGeomManager::GetOrigGlobalMatrix(symname,m);
412 }
413
414 //______________________________________________________________________
415 Bool_t AliITSUGeomTGeo::GetOrigTranslation(Int_t index, Double_t t[3])  const
416 {
417   // Get the original translation vector (ideal geometry)
418   // for a given module 'index' by quering the TGeoManager
419   TGeoHMatrix m;
420   if (!GetOrigMatrix(index,m)) return kFALSE;
421
422   Double_t *trans = m.GetTranslation();
423   for (Int_t i = 0; i < 3; i++) t[i] = trans[i];
424
425   return kTRUE;
426 }
427
428 //______________________________________________________________________
429 Bool_t AliITSUGeomTGeo::GetOrigRotation(Int_t index, Double_t r[9])  const
430 {
431   // Get the original rotation matrix (ideal geometry)
432   // for a given module 'index' by quering the TGeoManager
433   TGeoHMatrix m;
434   if (!GetOrigMatrix(index,m)) return kFALSE;
435
436   Double_t *rot = m.GetRotationMatrix();
437   for (Int_t i = 0; i < 9; i++) r[i] = rot[i];
438
439   return kTRUE;
440 }
441
442 //______________________________________________________________________
443 TGeoHMatrix* AliITSUGeomTGeo::ExtractMatrixT2L(Int_t index) const
444 {
445   // Get the matrix which transforms from the tracking to local r.s.
446   // The method queries directly the TGeoPNEntry
447   TGeoPNEntry *pne = GetPNEntry(index);
448   if (!pne) return NULL;
449
450   TGeoHMatrix *m = (TGeoHMatrix*) pne->GetMatrix();
451   if (!m) AliError(Form("TGeoPNEntry (%s) contains no matrix !",pne->GetName()));
452
453   return m;
454 }
455
456 //______________________________________________________________________
457 Bool_t AliITSUGeomTGeo::GetTrackingMatrix(Int_t index, TGeoHMatrix &m)
458 {
459   // Get the matrix which transforms from the tracking r.s. to
460   // the global one.
461   // Returns kFALSE in case of error.
462   m.Clear();
463
464   TGeoHMatrix *m1 = GetMatrix(index);
465   if (!m1) return kFALSE;
466
467   const TGeoHMatrix *m2 = GetMatrixT2L(index);
468   if (!m2) return kFALSE;
469
470   m = *m1;
471   m.Multiply(m2);
472
473   return kTRUE;
474 }
475
476 //______________________________________________________________________
477 TGeoHMatrix* AliITSUGeomTGeo::ExtractMatrixSens(Int_t index) const
478 {
479   // Get the transformation matrix of the SENSOR (not ncessary the same as the module) 
480   // for a given module 'index' by quering the TGeoManager
481   static TGeoHMatrix matTmp;
482   const TString kPathBase = Form("/ALIC_1/%s_2/",AliITSUGeomTGeo::GetITSVolPattern());
483   const TString kNames = Form("%%s%%s%s%%d_1/%s%%d_%%d/%s%%d_%%d/%s%%d_%%d"
484                               ,AliITSUGeomTGeo::GetITSLayerPattern()
485                               ,AliITSUGeomTGeo::GetITSLadderPattern()
486                               ,AliITSUGeomTGeo::GetITSModulePattern()
487                               ,AliITSUGeomTGeo::GetITSSensorPattern());
488   TString path;
489   Int_t lay,ladd,detInLad;
490   GetModuleId(index,lay,ladd,detInLad);
491   //
492   int wrID = fLr2Wrapper[lay];
493   path.Form(kNames.Data(),kPathBase.Data(),wrID>=0 ? Form("%s%d_1/",GetITSWrapVolPattern(),wrID) : "",
494             lay,lay,ladd,lay,detInLad,lay,1);
495   gGeoManager->PushPath();
496   if (!gGeoManager->cd(path.Data())) {
497     gGeoManager->PopPath();
498     AliError(Form("Error in cd-ing to %s",path.Data()));
499     return 0;
500   } // end if !gGeoManager
501   matTmp = *gGeoManager->GetCurrentMatrix(); // matrix may change after cd
502   //RSS
503   //  printf("%d/%d/%d %s\n",lay,ladd,detInLad,path.Data());
504   //  mat->Print();
505   // Retstore the modeler state.
506   gGeoManager->PopPath();
507   return &matTmp;
508 }
509
510
511 //______________________________________________________________________
512 TGeoPNEntry* AliITSUGeomTGeo::GetPNEntry(Int_t index) const
513 {
514   // Get a pointer to the TGeoPNEntry of a module
515   // identified by 'index'
516   // Returns NULL in case of invalid index,
517   // missing TGeoManager or invalid symbolic name
518   //
519   if (index >= fNModules) {
520     AliError(Form("Invalid ITS module index: %d (0 -> %d) !",index,fNModules));
521     return NULL;
522   }
523   
524   if (!gGeoManager || !gGeoManager->IsClosed()) {
525     AliError("Can't get the matrix! gGeoManager doesn't exist or it is still opened!");
526     return NULL;
527   }
528   TGeoPNEntry* pne = gGeoManager->GetAlignableEntryByUID( ModuleVolUID(index) );
529   //  TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(GetSymName(index));
530   if (!pne) AliError(Form("The index %d does not correspond to a physical entry!",index));
531   //
532   return pne;
533 }
534
535 //______________________________________________________________________
536 void AliITSUGeomTGeo::BuildITS(Bool_t loadSegm)
537 {
538   // exract upg ITS parameters from TGeo
539   if (fVersion!=kITSVNA) {AliWarning("Already built"); return; // already initialized}
540     if (!gGeoManager) AliFatal("Geometry is not loaded");
541   }
542   fNLayers    = ExtractNumberOfLayers();
543   if (!fNLayers) return;
544   //
545   fNLadders   = new Int_t[fNLayers];
546   fNDetectors = new Int_t[fNLayers];
547   fLrDetType  = new Int_t[fNLayers];
548   fLastModIndex   = new Int_t[fNLayers];
549   fNModules = 0;
550   for (int i=0;i<fNLayers;i++) {
551     fNLadders[i]   = ExtractNumberOfLadders(i);
552     fNDetectors[i] = ExtractNumberOfDetectors(i);
553     fLrDetType[i]  = ExtractLayerDetType(i);
554     fNModules     += fNLadders[i]*fNDetectors[i];
555     fLastModIndex[i]   = fNModules-1;
556   }
557   //
558   FetchMatrices();
559   fVersion = kITSVUpg;
560   //
561   if (loadSegm) {  // fetch segmentations
562     fSegm = new TObjArray();
563     AliITSUSegmentationPix::LoadSegmentations(fSegm,GetITSsegmentationFileName());
564   }
565   //
566 }
567
568 //______________________________________________________________________
569 Int_t AliITSUGeomTGeo::ExtractNumberOfLayers()
570 {
571   // Determines the number of layers in the Upgrade Geometry
572   //
573   Int_t numberOfLayers = 0;
574   //
575   TGeoVolume *itsV = gGeoManager->GetVolume(fgkITSVolName);
576   if (!itsV) AliFatal(Form("ITS volume %s is not in the geometry",fgkITSVolName));
577   SetUIDShift(itsV->GetUniqueID());
578   //
579   // Loop on all ITSV nodes, count Layer volumes by checking names
580   // Build on the fly layer - wrapper correspondence
581   TObjArray* nodes = itsV->GetNodes();
582   Int_t nNodes = nodes->GetEntriesFast();
583   //
584   int nWrp = 0;
585   for (Int_t j=0; j<nNodes; j++) {
586     TGeoNode* nd = (TGeoNode*)nodes->At(j);
587     const char* name = nd->GetName();
588     if      (strstr(name,fgkITSLrName)) numberOfLayers++;
589     else if (strstr(name,fgkITSWrapVolName)) { // this is a wrapper volume, may cointain layers
590       TObjArray* nodesW = nd->GetNodes();
591       int nNodesW = nodesW->GetEntriesFast();
592       for (Int_t jw=0; jw<nNodesW; jw++) {
593         TGeoNode* ndW = (TGeoNode*)nodesW->At(jw);
594         if (strstr(ndW->GetName(),fgkITSLrName)) fLr2Wrapper[numberOfLayers++] = nWrp;
595       }
596       nWrp++;
597     }
598   }
599   //  
600   return numberOfLayers;
601 }
602
603 //______________________________________________________________________
604 Int_t AliITSUGeomTGeo::ExtractNumberOfLadders(Int_t lay) const
605 {
606   // Determines the number of layers in the Upgrade Geometry
607   //
608   // Inputs:
609   //   lay: layer number, starting from 0
610   //
611   // MS
612   Int_t numberOfLadders = 0;
613   char laynam[30];
614   snprintf(laynam, 30, "%s%d",fgkITSLrName,lay);
615   TGeoVolume* volLr = gGeoManager->GetVolume(laynam);
616   if (!volLr) AliFatal(Form("can't find %s volume",laynam));
617   //
618   // Loop on all layer nodes, count Ladder volumes by checking names
619   Int_t nNodes = volLr->GetNodes()->GetEntries();
620   for (Int_t j=0; j<nNodes; j++) if (strstr(volLr->GetNodes()->At(j)->GetName(),fgkITSLadName)) numberOfLadders++;
621   //
622   return numberOfLadders;
623   //
624 }
625
626 //______________________________________________________________________
627 Int_t AliITSUGeomTGeo::ExtractNumberOfDetectors(Int_t lay)  const
628 {
629   // Determines the number of detectors per ladder in the Upgrade Geometry
630   //
631   // Inputs:
632   //   lay: layer number from 0
633   // MS
634   Int_t numberOfModules = 0;
635   char laddnam[30];
636   snprintf(laddnam, 30, "%s%d", fgkITSLadName,lay);
637   TGeoVolume* volLd = gGeoManager->GetVolume(laddnam);
638   if (!volLd) AliFatal(Form("can't find %s volume",laddnam));
639   //
640   // Loop on all ladder nodes, count Module volumes by checking names
641   Int_t nNodes = volLd->GetNodes()->GetEntries();
642   for (Int_t j=0; j<nNodes; j++) if (strstr(volLd->GetNodes()->At(j)->GetName(),fgkITSModName)) numberOfModules++;
643   //
644   return numberOfModules;
645   //
646 }
647
648 //______________________________________________________________________
649 Int_t AliITSUGeomTGeo::ExtractLayerDetType(Int_t lay)  const
650 {
651   // Determines the layer detector type the Upgrade Geometry
652   //
653   // Inputs:
654   //   lay: layer number from 0
655   // Outputs:
656   //   none
657   // Return:
658   //   detector type id for the layer
659   // MS
660   char laddnam[30];
661   snprintf(laddnam, 30, "%s%d", fgkITSLrName,lay);
662   TGeoVolume* volLd = gGeoManager->GetVolume(laddnam);
663   if (!volLd) {AliFatal(Form("can't find %s volume",laddnam)); return -1;}
664   //
665   return volLd->GetUniqueID();
666   //
667 }
668
669 //______________________________________________________________________
670 UInt_t AliITSUGeomTGeo::ComposeDetTypeID(UInt_t segmId)
671 {
672   if (segmId>=kMaxSegmPerDetType) AliFatalClass(Form("Id=%d is >= max.allowed %d",segmId,kMaxSegmPerDetType));
673   return segmId + kDetTypePix*kMaxSegmPerDetType;
674 }
675
676 //______________________________________________________________________
677 void AliITSUGeomTGeo::Print(Option_t *) const
678 {
679   // print
680   printf("Geometry version %d, NLayers:%d NModules:%d\n",fVersion,fNLayers,fNModules);
681   if (fVersion==kITSVNA) return;
682   for (int i=0;i<fNLayers;i++) {
683     printf("Lr%2d\tNLadd:%2d\tNDet:%2d\tDetType:%3d\tMod#:%4d:%4d\tWrapVol:%d\n",
684            i,fNLadders[i],fNDetectors[i],fLrDetType[i],GetFirstModIndex(i),GetLastModIndex(i),fLr2Wrapper[i]);
685   }
686 }
687
688 //______________________________________________________________________
689 void AliITSUGeomTGeo::FetchMatrices()
690 {
691   // store pointer on often used matrices for faster access
692   if (!gGeoManager) AliFatal("Geometry is not loaded");
693   fMatSens = new TObjArray(fNModules);
694   fMatSens->SetOwner(kTRUE);
695   for (int i=0;i<fNModules;i++) fMatSens->AddAt(new TGeoHMatrix(*ExtractMatrixSens(i)),i);
696   CreateT2LMatrices();
697 }
698
699 //______________________________________________________________________
700 void AliITSUGeomTGeo::CreateT2LMatrices()
701 {
702   // create tracking to local (Sensor!) matrices
703   fMatT2L  = new TObjArray(fNModules);  
704   fMatT2L->SetOwner(kTRUE);
705   TGeoHMatrix matLtoT;
706   double loc[3]={0,0,0},glo[3];
707   const double *rotm;
708   for (int isn=0;isn<fNModules;isn++) {
709     const TGeoHMatrix* matSens = GetMatrixSens(isn);
710     if (!matSens) {AliFatal(Form("Failed to get matrix for sensor %d",isn)); return;}
711     matSens->LocalToMaster(loc,glo);
712     rotm = matSens->GetRotationMatrix();
713     Double_t al = -ATan2(rotm[1],rotm[0]);
714     double sn=Sin(al), cs=Cos(al), r=glo[0]*sn-glo[1]*cs, x=r*sn, y=-r*cs; // sensor plane PCA to origin
715     TGeoHMatrix* t2l = new TGeoHMatrix();
716     t2l->RotateZ(ATan2(y,x)*RadToDeg()); // rotate in direction of normal to the sensor plane
717     t2l->SetDx(x);
718     t2l->SetDy(y);
719     t2l->MultiplyLeft(&matSens->Inverse());
720     fMatT2L->AddAt(t2l,isn);
721     /*
722     const double *gtrans = matSens->GetTranslation();
723     memcpy(&rotMatrix[0], matSens->GetRotationMatrix(), 9*sizeof(Double_t));
724     Double_t al = -ATan2(rotMatrix[1],rotMatrix[0]);
725     Double_t rSens = Sqrt(gtrans[0]*gtrans[0] + gtrans[1]*gtrans[1]);
726     Double_t tanAl = ATan2(gtrans[1],gtrans[0]) - Pi()/2; //angle of tangent
727     Double_t alTr = tanAl - al;
728     //
729     // The X axis of tracking frame must always look outward
730     loc[1] = rSens/2;
731     matSens->LocalToMaster(loc,glo);
732     double rPos = Sqrt(glo[0]*glo[0] + glo[1]*glo[1]);
733     Bool_t rotOutward = rPos>rSens ? kFALSE : kTRUE;
734     //
735     // Transformation matrix
736     matLtoT.Clear();
737     matLtoT.SetDx(-rSens*Sin(alTr)); // translation
738     matLtoT.SetDy(0.);
739     matLtoT.SetDz(gtrans[2]);
740     // Rotation matrix
741     rotMatrix[0]= 0;  rotMatrix[1]= 1;  rotMatrix[2]= 0; // + rotation
742     rotMatrix[3]=-1;  rotMatrix[4]= 0;  rotMatrix[5]= 0;
743     rotMatrix[6]= 0;  rotMatrix[7]= 0;  rotMatrix[8]= 1;
744     //
745     TGeoRotation rot;
746     rot.SetMatrix(rotMatrix);
747     matLtoT.MultiplyLeft(&rot);
748     if (rotOutward) matLtoT.RotateZ(180.);
749     // Inverse transformation Matrix
750     fMatT2L->AddAt(new TGeoHMatrix(matLtoT.Inverse()),isn);
751     */
752   }
753   //
754 }
755