1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
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 //
21 // author - cvetan.cheshkov@cern.ch //
23 // adapted to ITSupg 18/07/2012 - ruben.shahoyan@cern.ch //
25 // ATTENTION: In opposite to ols AliITSgeomTGeo, all indices start //
26 // from 0, not from 1!!! //
28 ///////////////////////////////////////////////////////////////////////////
32 #include <TGeoManager.h>
33 #include <TGeoPhysicalNode.h>
38 #include "AliITSUGeomTGeo.h"
40 #include "AliAlignObj.h"
41 #include "AliITSsegmentation.h"
42 #include "AliITSUSegmentationPix.h"
43 using namespace TMath;
45 ClassImp(AliITSUGeomTGeo)
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"};
56 TString AliITSUGeomTGeo::fgITSsegmFileName = "itsSegmentations.root";
58 //______________________________________________________________________
59 AliITSUGeomTGeo::AliITSUGeomTGeo(Bool_t build, Bool_t loadSegm)
72 for (int i=AliITSUAux::kMaxLayers;i--;) fLr2Wrapper[i] = -1;
73 if (build) BuildITS(loadSegm);
76 //______________________________________________________________________
77 AliITSUGeomTGeo::AliITSUGeomTGeo(const AliITSUGeomTGeo &src)
79 ,fVersion(src.fVersion)
80 ,fNLayers(src.fNLayers)
81 ,fNModules(src.fNModules)
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];
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);
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);
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);
125 fSegm->AddAt(sg->Clone(),i);
129 for (int i=AliITSUAux::kMaxLayers;i--;) fLr2Wrapper[i] = src.fLr2Wrapper[i];
132 //______________________________________________________________________
133 AliITSUGeomTGeo::~AliITSUGeomTGeo()
138 delete[] fNDetectors;
139 delete[] fLastModIndex;
146 //______________________________________________________________________
147 AliITSUGeomTGeo& AliITSUGeomTGeo::operator=(const AliITSUGeomTGeo &src)
153 delete[] fNDetectors;
154 delete[] fLastModIndex;
155 fNLadders = fLrDetType = fNDetectors = fLastModIndex = 0;
156 fVersion = src.fVersion;
157 fNLayers = src.fNLayers;
158 fNModules = src.fNModules;
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);
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);
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);
184 fSegm->AddAt(sg->Clone(),i);
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];
204 //______________________________________________________________________
205 Int_t AliITSUGeomTGeo::GetModuleIndex(Int_t lay,Int_t lad,Int_t det) const
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.
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
216 return GetFirstModIndex(lay) + fNDetectors[lay]*lad + det;
219 //______________________________________________________________________
220 Bool_t AliITSUGeomTGeo::GetLayer(Int_t index,Int_t &lay,Int_t &index2) const
222 // This routine computes the layer number for a
223 // given the module index. The
225 // Int_t index The module index number, starting from zero.
227 // Int_t index2 The module index inside a layer, starting from zero.
228 // Int_t lay The layer number. Starting from 0.
230 lay = GetLayer(index);
231 index2 = index - GetFirstModIndex(lay);
236 //______________________________________________________________________
237 Int_t AliITSUGeomTGeo::GetLayer(Int_t index) const
239 // Get module layer, from 0
242 while(index>fLastModIndex[lay]) lay++;
246 //______________________________________________________________________
247 Int_t AliITSUGeomTGeo::GetLadder(Int_t index) const
249 // Get module ladder, from 0
252 while(index>fLastModIndex[lay]) lay++;
253 index -= GetFirstModIndex(lay);
254 return index/fNDetectors[lay];
257 //______________________________________________________________________
258 Int_t AliITSUGeomTGeo::GetModIdInLayer(Int_t index) const
260 // Get module number within layer, from 0
263 while(index>fLastModIndex[lay]) lay++;
264 index -= GetFirstModIndex(lay);
268 //______________________________________________________________________
269 Int_t AliITSUGeomTGeo::GetModIdInLadder(Int_t index) const
271 // Get module number within ladder, from 0
274 while(index>fLastModIndex[lay]) lay++;
275 index -= GetFirstModIndex(lay);
276 return index%fNDetectors[lay];
279 //______________________________________________________________________
280 Bool_t AliITSUGeomTGeo::GetModuleId(Int_t index,Int_t &lay,Int_t &lad,Int_t &det) const
282 // The method is taken from the old AliITSgeom class by Bjorn Nilsen
284 // This routine computes the layer, ladder and detector number
285 // given the module index number.
287 // Int_t index The module index number, starting from zero.
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
293 lay = GetLayer(index);
294 index -= GetFirstModIndex(lay);
295 lad = index/fNDetectors[lay];
296 det = index%fNDetectors[lay];
300 //______________________________________________________________________
301 const char* AliITSUGeomTGeo::GetSymName(Int_t index) const
303 // Get the TGeoPNEntry symbolic name
304 // for a given module identified by 'index'
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) );
313 AliError(Form("Failed to find alignable entry with index %d: (Lr%d Mod:%d) !",index,lay,index2));
316 return pne->GetName();
319 //______________________________________________________________________
320 const char* AliITSUGeomTGeo::ComposeSymNameITS()
322 // sym name of the layer
326 //______________________________________________________________________
327 const char* AliITSUGeomTGeo::ComposeSymNameLayer(Int_t lr)
329 // sym name of the layer
330 return Form("%s/%s%d",ComposeSymNameITS(),GetITSLayerPattern(),lr);
333 //______________________________________________________________________
334 const char* AliITSUGeomTGeo::ComposeSymNameLadder(Int_t lr, Int_t ladder)
336 // sym name of the ladder at given layer
337 return Form("%s/%s%d",ComposeSymNameLayer(lr),GetITSLadderPattern(),ladder);
340 //______________________________________________________________________
341 const char* AliITSUGeomTGeo::ComposeSymNameModule(Int_t lr, Int_t lad, int det)
343 // sym name of the module
344 return Form("%s/%s%d",ComposeSymNameLadder(lr,lad),GetITSModulePattern(),det);
347 //______________________________________________________________________
348 TGeoHMatrix* AliITSUGeomTGeo::GetMatrix(Int_t index) const
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;
356 TGeoPhysicalNode *pnode = pne->GetPhysicalNode();
357 if (pnode) return pnode->GetMatrix();
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));
366 matTmp = *gGeoManager->GetCurrentMatrix();
367 gGeoManager->PopPath();
371 //______________________________________________________________________
372 Bool_t AliITSUGeomTGeo::GetTranslation(Int_t index, Double_t t[3]) const
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;
379 Double_t *trans = m->GetTranslation();
380 for (Int_t i = 0; i < 3; i++) t[i] = trans[i];
385 //______________________________________________________________________
386 Bool_t AliITSUGeomTGeo::GetRotation(Int_t index, Double_t r[9]) const
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;
393 Double_t *rot = m->GetRotationMatrix();
394 for (Int_t i = 0; i < 9; i++) r[i] = rot[i];
399 //______________________________________________________________________
400 Bool_t AliITSUGeomTGeo::GetOrigMatrix(Int_t index, TGeoHMatrix &m) const
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
408 const char *symname = GetSymName(index);
409 if (!symname) return kFALSE;
411 return AliGeomManager::GetOrigGlobalMatrix(symname,m);
414 //______________________________________________________________________
415 Bool_t AliITSUGeomTGeo::GetOrigTranslation(Int_t index, Double_t t[3]) const
417 // Get the original translation vector (ideal geometry)
418 // for a given module 'index' by quering the TGeoManager
420 if (!GetOrigMatrix(index,m)) return kFALSE;
422 Double_t *trans = m.GetTranslation();
423 for (Int_t i = 0; i < 3; i++) t[i] = trans[i];
428 //______________________________________________________________________
429 Bool_t AliITSUGeomTGeo::GetOrigRotation(Int_t index, Double_t r[9]) const
431 // Get the original rotation matrix (ideal geometry)
432 // for a given module 'index' by quering the TGeoManager
434 if (!GetOrigMatrix(index,m)) return kFALSE;
436 Double_t *rot = m.GetRotationMatrix();
437 for (Int_t i = 0; i < 9; i++) r[i] = rot[i];
442 //______________________________________________________________________
443 TGeoHMatrix* AliITSUGeomTGeo::ExtractMatrixT2L(Int_t index) const
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;
450 TGeoHMatrix *m = (TGeoHMatrix*) pne->GetMatrix();
451 if (!m) AliError(Form("TGeoPNEntry (%s) contains no matrix !",pne->GetName()));
456 //______________________________________________________________________
457 Bool_t AliITSUGeomTGeo::GetTrackingMatrix(Int_t index, TGeoHMatrix &m)
459 // Get the matrix which transforms from the tracking r.s. to
461 // Returns kFALSE in case of error.
464 TGeoHMatrix *m1 = GetMatrix(index);
465 if (!m1) return kFALSE;
467 const TGeoHMatrix *m2 = GetMatrixT2L(index);
468 if (!m2) return kFALSE;
476 //______________________________________________________________________
477 TGeoHMatrix* AliITSUGeomTGeo::ExtractMatrixSens(Int_t index) const
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());
489 Int_t lay,ladd,detInLad;
490 GetModuleId(index,lay,ladd,detInLad);
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()));
500 } // end if !gGeoManager
501 matTmp = *gGeoManager->GetCurrentMatrix(); // matrix may change after cd
503 // printf("%d/%d/%d %s\n",lay,ladd,detInLad,path.Data());
505 // Retstore the modeler state.
506 gGeoManager->PopPath();
511 //______________________________________________________________________
512 TGeoPNEntry* AliITSUGeomTGeo::GetPNEntry(Int_t index) const
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
519 if (index >= fNModules) {
520 AliError(Form("Invalid ITS module index: %d (0 -> %d) !",index,fNModules));
524 if (!gGeoManager || !gGeoManager->IsClosed()) {
525 AliError("Can't get the matrix! gGeoManager doesn't exist or it is still opened!");
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));
535 //______________________________________________________________________
536 void AliITSUGeomTGeo::BuildITS(Bool_t loadSegm)
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");
542 fNLayers = ExtractNumberOfLayers();
543 if (!fNLayers) return;
545 fNLadders = new Int_t[fNLayers];
546 fNDetectors = new Int_t[fNLayers];
547 fLrDetType = new Int_t[fNLayers];
548 fLastModIndex = new Int_t[fNLayers];
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;
561 if (loadSegm) { // fetch segmentations
562 fSegm = new TObjArray();
563 AliITSUSegmentationPix::LoadSegmentations(fSegm,GetITSsegmentationFileName());
568 //______________________________________________________________________
569 Int_t AliITSUGeomTGeo::ExtractNumberOfLayers()
571 // Determines the number of layers in the Upgrade Geometry
573 Int_t numberOfLayers = 0;
575 TGeoVolume *itsV = gGeoManager->GetVolume(fgkITSVolName);
576 if (!itsV) AliFatal(Form("ITS volume %s is not in the geometry",fgkITSVolName));
577 SetUIDShift(itsV->GetUniqueID());
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();
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;
600 return numberOfLayers;
603 //______________________________________________________________________
604 Int_t AliITSUGeomTGeo::ExtractNumberOfLadders(Int_t lay) const
606 // Determines the number of layers in the Upgrade Geometry
609 // lay: layer number, starting from 0
612 Int_t numberOfLadders = 0;
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));
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++;
622 return numberOfLadders;
626 //______________________________________________________________________
627 Int_t AliITSUGeomTGeo::ExtractNumberOfDetectors(Int_t lay) const
629 // Determines the number of detectors per ladder in the Upgrade Geometry
632 // lay: layer number from 0
634 Int_t numberOfModules = 0;
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));
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++;
644 return numberOfModules;
648 //______________________________________________________________________
649 Int_t AliITSUGeomTGeo::ExtractLayerDetType(Int_t lay) const
651 // Determines the layer detector type the Upgrade Geometry
654 // lay: layer number from 0
658 // detector type id for the layer
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;}
665 return volLd->GetUniqueID();
669 //______________________________________________________________________
670 UInt_t AliITSUGeomTGeo::ComposeDetTypeID(UInt_t segmId)
672 if (segmId>=kMaxSegmPerDetType) AliFatalClass(Form("Id=%d is >= max.allowed %d",segmId,kMaxSegmPerDetType));
673 return segmId + kDetTypePix*kMaxSegmPerDetType;
676 //______________________________________________________________________
677 void AliITSUGeomTGeo::Print(Option_t *) const
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]);
688 //______________________________________________________________________
689 void AliITSUGeomTGeo::FetchMatrices()
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);
699 //______________________________________________________________________
700 void AliITSUGeomTGeo::CreateT2LMatrices()
702 // create tracking to local (Sensor!) matrices
703 fMatT2L = new TObjArray(fNModules);
704 fMatT2L->SetOwner(kTRUE);
706 double loc[3]={0,0,0},glo[3];
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
719 t2l->MultiplyLeft(&matSens->Inverse());
720 fMatT2L->AddAt(t2l,isn);
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;
729 // The X axis of tracking frame must always look outward
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;
735 // Transformation matrix
737 matLtoT.SetDx(-rSens*Sin(alTr)); // translation
739 matLtoT.SetDz(gtrans[2]);
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;
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);