]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSGeoUtils.cxx
skip AliCaloAltroMapping delete at the end to avoid segmentation violation on exit...
[u/mrichter/AliRoot.git] / PHOS / AliPHOSGeoUtils.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 /* $Id: AliPHOSGeometry.cxx 25590 2008-05-06 07:09:11Z prsnko $ */
17
18 //_________________________________________________________________________
19 // Geometry class  for PHOS 
20 // PHOS consists of the electromagnetic calorimeter (EMCA)
21 // and a charged particle veto (CPV)
22 // The EMCA/CPV modules are parametrized so that any configuration
23 // can be easily implemented 
24 // The title is used to identify the version of CPV used.
25 //                  
26 // -- Author: Yves Schutz (SUBATECH) & Dmitri Peressounko (RRC "KI" & SUBATECH)
27
28 // --- ROOT system ---
29
30 #include "TClonesArray.h"
31 #include "TVector3.h"
32 #include "TParticle.h"
33 #include <TGeoManager.h>
34 #include <TGeoMatrix.h>
35
36 // --- Standard library ---
37
38 // --- AliRoot header files ---
39 #include "AliPHOSEMCAGeometry.h"
40 #include "AliPHOSCPVGeometry.h"
41 #include "AliPHOSSupportGeometry.h"
42 #include "AliPHOSGeoUtils.h"
43
44 ClassImp(AliPHOSGeoUtils)
45
46 //____________________________________________________________________________
47 AliPHOSGeoUtils::AliPHOSGeoUtils():
48   fGeometryEMCA(0x0),fGeometryCPV(0x0),fGeometrySUPP(0x0),
49   fNModules(0),fNCristalsInModule(0),fNPhi(0),fNZ(0),
50   fNumberOfCPVPadsPhi(0),fNumberOfCPVPadsZ(0),
51   fNCellsXInStrip(0),fNCellsZInStrip(0),fNStripZ(0),
52   fCrystalShift(0.),fCryCellShift(0.),fCryStripShift(0.),fCellStep(0.),
53   fPadSizePhi(0.),fPadSizeZ(0.),fCPVBoxSizeY(0.),fMisalArray(0x0)
54  
55 {
56     // default ctor 
57     // must be kept public for root persistency purposes, but should never be called by the outside world
58 }  
59
60 //____________________________________________________________________________
61 AliPHOSGeoUtils::AliPHOSGeoUtils(const AliPHOSGeoUtils & rhs)
62   : TNamed(rhs),
63   fGeometryEMCA(0x0),fGeometryCPV(0x0),fGeometrySUPP(0x0),
64   fNModules(0),fNCristalsInModule(0),fNPhi(0),fNZ(0),
65   fNumberOfCPVPadsPhi(0),fNumberOfCPVPadsZ(0),
66   fNCellsXInStrip(0),fNCellsZInStrip(0),fNStripZ(0),
67   fCrystalShift(0.),fCryCellShift(0.),fCryStripShift(0.),fCellStep(0.),
68   fPadSizePhi(0.),fPadSizeZ(0.),fCPVBoxSizeY(0.),fMisalArray(0x0)
69 {
70   Fatal("cpy ctor", "not implemented") ; 
71 }
72
73 //____________________________________________________________________________
74 AliPHOSGeoUtils::AliPHOSGeoUtils(const Text_t* name, const Text_t* title) 
75     : TNamed(name, title),
76   fGeometryEMCA(0x0),fGeometryCPV(0x0),fGeometrySUPP(0x0),
77   fNModules(0),fNCristalsInModule(0),fNPhi(0),fNZ(0),
78   fNumberOfCPVPadsPhi(0),fNumberOfCPVPadsZ(0),
79   fNCellsXInStrip(0),fNCellsZInStrip(0),fNStripZ(0),
80   fCrystalShift(0.),fCryCellShift(0.),fCryStripShift(0.),fCellStep(0.),
81   fPadSizePhi(0.),fPadSizeZ(0.),fCPVBoxSizeY(0.),fMisalArray(0x0)
82
83   // ctor only for normal usage 
84
85   fGeometryEMCA = new AliPHOSEMCAGeometry() ;
86   fGeometryCPV  = new AliPHOSCPVGeometry() ;
87   fGeometrySUPP = new AliPHOSSupportGeometry() ;
88
89   fNModules     = 5;
90   fNPhi  = fGeometryEMCA->GetNPhi() ;
91   fNZ    = fGeometryEMCA->GetNZ() ;
92   fNCristalsInModule = fNPhi*fNZ ;
93   fNCellsXInStrip= fGeometryEMCA->GetNCellsXInStrip() ;
94   fNCellsZInStrip= fGeometryEMCA->GetNCellsZInStrip() ;
95   fNStripZ = fGeometryEMCA->GetNStripZ() ;
96   fXtlArrSize[0]=fGeometryEMCA->GetInnerThermoHalfSize()[0] ; //Wery close to the zise of the Xtl set
97   fXtlArrSize[1]=fGeometryEMCA->GetInnerThermoHalfSize()[1] ; //Wery close to the zise of the Xtl set
98   fXtlArrSize[2]=fGeometryEMCA->GetInnerThermoHalfSize()[2] ; //Wery close to the zise of the Xtl set
99
100   //calculate offset to crystal surface
101   const Float_t * inthermo = fGeometryEMCA->GetInnerThermoHalfSize() ;
102   const Float_t * strip    = fGeometryEMCA->GetStripHalfSize() ;
103   const Float_t * splate   = fGeometryEMCA->GetSupportPlateHalfSize();
104   const Float_t * crystal  = fGeometryEMCA->GetCrystalHalfSize() ;
105   const Float_t * pin      = fGeometryEMCA->GetAPDHalfSize() ;
106   const Float_t * preamp   = fGeometryEMCA->GetPreampHalfSize() ;
107   fCrystalShift=-inthermo[1]+strip[1]+splate[1]+crystal[1]-fGeometryEMCA->GetAirGapLed()/2.+pin[1]+preamp[1] ;
108   fCryCellShift=crystal[1]-(fGeometryEMCA->GetAirGapLed()-2*pin[1]-2*preamp[1])/2;
109   fCryStripShift=fCryCellShift+splate[1] ;
110   fCellStep = 2.*fGeometryEMCA->GetAirCellHalfSize()[0] ;
111
112   fNumberOfCPVPadsPhi = fGeometryCPV->GetNumberOfCPVPadsPhi() ;
113   fNumberOfCPVPadsZ   = fGeometryCPV->GetNumberOfCPVPadsZ() ;
114   fPadSizePhi = fGeometryCPV->GetCPVPadSizePhi() ;
115   fPadSizeZ   = fGeometryCPV->GetCPVPadSizeZ() ; 
116   fCPVBoxSizeY= fGeometryCPV->GetCPVBoxSize(1) ;
117
118   for(Int_t mod=0; mod<5; mod++){
119     fEMCMatrix[mod]=0 ;
120     for(Int_t istrip=0; istrip<224; istrip++)
121       fStripMatrix[mod][istrip]=0 ;
122     fCPVMatrix[mod]=0;
123     fPHOSMatrix[mod]=0 ;
124   }
125  
126 }
127
128 //____________________________________________________________________________
129 AliPHOSGeoUtils & AliPHOSGeoUtils::operator = (const AliPHOSGeoUtils  & /*rvalue*/) { 
130
131   Fatal("assignment operator", "not implemented") ; 
132     return *this ;
133 }
134
135 //____________________________________________________________________________
136 AliPHOSGeoUtils::~AliPHOSGeoUtils(void)
137 {
138   // dtor
139   if(fGeometryEMCA){
140     delete fGeometryEMCA; fGeometryEMCA = 0 ;
141   }
142   if(fGeometryCPV){
143     delete fGeometryCPV; fGeometryCPV=0 ;
144   }
145   if(fGeometrySUPP){
146     delete fGeometrySUPP ; fGeometrySUPP=0 ;
147   }
148   if(fMisalArray){
149     delete fMisalArray; fMisalArray=0 ;
150   }
151 }
152 //____________________________________________________________________________
153 Bool_t AliPHOSGeoUtils::AbsToRelNumbering(Int_t absId, Int_t * relid) const
154 {
155   // Converts the absolute numbering into the following array
156   //  relid[0] = PHOS Module number 1:fNModules 
157   //  relid[1] = 0 if PbW04
158   //           = -1 if CPV
159   //  relid[2] = Row number inside a PHOS module
160   //  relid[3] = Column number inside a PHOS module
161
162   Float_t id = absId ;
163
164   Int_t phosmodulenumber = (Int_t)TMath:: Ceil( id / fNCristalsInModule ) ; 
165   
166   if ( phosmodulenumber >  fNModules ) { // it is a CPV pad
167     
168     id -=  fNPhi * fNZ *  fNModules ; 
169     Float_t nCPV  = fNumberOfCPVPadsPhi * fNumberOfCPVPadsZ ;
170     relid[0] = (Int_t) TMath::Ceil( id / nCPV ) ;
171     relid[1] = -1 ;
172     id -= ( relid[0] - 1 ) * nCPV ; 
173     relid[2] = (Int_t) TMath::Ceil( id / fNumberOfCPVPadsZ ) ;
174     relid[3] = (Int_t) ( id - ( relid[2] - 1 ) * fNumberOfCPVPadsZ ) ; 
175   } 
176   else { // it is a PW04 crystal
177
178     relid[0] = phosmodulenumber ;
179     relid[1] = 0 ;
180     id -= ( phosmodulenumber - 1 ) *  fNPhi * fNZ ; 
181     relid[2] = (Int_t)TMath::Ceil( id / fNZ )  ;
182     relid[3] = (Int_t)( id - ( relid[2] - 1 ) * fNZ ) ; 
183   } 
184   return kTRUE ; 
185 }
186 //____________________________________________________________________________
187 Bool_t AliPHOSGeoUtils::RelToAbsNumbering(const Int_t * relid, Int_t &  absId) const
188 {
189   // Converts the relative numbering into the absolute numbering
190   // EMCA crystals:
191   //  absId = from 1 to fNModules * fNPhi * fNZ
192   // CPV pad:
193   //  absId = from N(total PHOS crystals) + 1
194   //          to NCPVModules * fNumberOfCPVPadsPhi * fNumberOfCPVPadsZ
195
196   if ( relid[1] ==  0 ) {                            // it is a Phos crystal
197     absId =
198       ( relid[0] - 1 ) * fNPhi * fNZ         // the offset of PHOS modules
199       + ( relid[2] - 1 ) * fNZ                   // the offset along phi
200       +   relid[3] ;                                 // the offset along z
201   }
202   else { // it is a CPV pad
203     absId =    fNPhi * fNZ *  fNModules         // the offset to separate EMCA crystals from CPV pads
204       + ( relid[0] - 1 ) * fNumberOfCPVPadsPhi * fNumberOfCPVPadsZ   // the pads offset of PHOS modules 
205       + ( relid[2] - 1 ) * fNumberOfCPVPadsZ                            // the pads offset of a CPV row
206       +   relid[3] ;                                                         // the column number
207   }
208   
209   return kTRUE ; 
210 }
211
212 //____________________________________________________________________________
213 void AliPHOSGeoUtils::RelPosInModule(const Int_t * relid, Float_t & x, Float_t & z) const 
214 {
215   // Converts the relative numbering into the local PHOS-module (x, z) coordinates
216
217   if(relid[1]==0){ //this is PHOS
218
219     Double_t pos[3]= {0.0,-fCryCellShift,0.}; //Position incide the crystal 
220     Double_t posC[3]={0.0,0.0,0.}; //Global position
221
222     //Shift and possibly apply misalignment corrections
223     Int_t strip=1+((Int_t) TMath::Ceil((Double_t)relid[2]/fNCellsXInStrip))*fNStripZ-
224                 (Int_t) TMath::Ceil((Double_t)relid[3]/fNCellsZInStrip) ;
225     pos[0]=((relid[2]-1)%fNCellsXInStrip-fNCellsXInStrip/2+0.5)*fCellStep ;
226     pos[2]=(-(relid[3]-1)%fNCellsZInStrip+fNCellsZInStrip/2-0.5)*fCellStep ;
227
228     Int_t mod = relid[0] ;
229     const TGeoHMatrix * m2 = GetMatrixForStrip(mod, strip) ;
230     m2->LocalToMaster(pos,posC);
231
232     //Return to PHOS local system  
233     Double_t posL2[3]={posC[0],posC[1],posC[2]};
234     const TGeoHMatrix *mPHOS2 = GetMatrixForModule(mod) ;
235     mPHOS2->MasterToLocal(posC,posL2);
236     x=posL2[0] ;
237     z=-posL2[2];
238     return ;
239   }
240   else{//CPV
241     //first calculate position with respect to CPV plain 
242     Int_t row        = relid[2] ; //offset along x axis
243     Int_t column     = relid[3] ; //offset along z axis
244     Double_t pos[3]= {0.0,0.0,0.}; //Position incide the CPV printed circuit
245     Double_t posC[3]={0.0,0.0,0.}; //Global position
246     pos[0] = - ( fNumberOfCPVPadsPhi/2. - row    - 0.5 ) * fPadSizePhi  ; // position of pad  with respect
247     pos[2] = - ( fNumberOfCPVPadsZ  /2. - column - 0.5 ) * fPadSizeZ  ; // of center of PHOS module
248
249     //now apply possible shifts and rotations
250     const TGeoHMatrix *m = GetMatrixForCPV(relid[0]) ;
251     m->LocalToMaster(pos,posC);
252     //Return to PHOS local system
253     Double_t posL[3]={0.,0.,0.,} ;
254     const TGeoHMatrix *mPHOS = GetMatrixForPHOS(relid[0]) ;
255     mPHOS->MasterToLocal(posC,posL);
256     x=posL[0] ;
257     z=posL[1];
258     return ;
259  
260   }
261   
262 }
263 //____________________________________________________________________________
264 void AliPHOSGeoUtils::RelPosToAbsId(Int_t module, Double_t x, Double_t z, Int_t & absId) const
265 {
266   // converts local PHOS-module (x, z) coordinates to absId 
267
268   //Calculate AbsId using ideal geometry. Should be sufficient for primary particles calculation
269   //(the only place where this method used currently)
270   Int_t relid[4]={module,0,1,1} ;
271   relid[2] = static_cast<Int_t>(TMath::Ceil( x/ fCellStep + fNPhi / 2.) );
272   relid[3] = static_cast<Int_t>(TMath::Ceil(-z/ fCellStep + fNZ   / 2.) ) ;
273   if(relid[2]<1)relid[2]=1 ;
274   if(relid[3]<1)relid[3]=1 ;
275   if(relid[2]>fNPhi)relid[2]=fNPhi ;
276   if(relid[3]>fNZ)relid[3]=fNZ ;
277   RelToAbsNumbering(relid,absId) ;
278
279 /*
280   //find Global position
281   if (!gGeoManager){
282     printf("Geo manager not initialized\n");
283     abort() ;
284   }
285   Double_t posL[3]={x,-fCrystalShift,-z} ; //Only for EMC!!!
286   Double_t posG[3] ;
287   char path[100] ;
288   sprintf(path,"/ALIC_1/PHOS_%d/PEMC_1/PCOL_1/PTIO_1/PCOR_1/PAGA_1/PTII_1",module) ;
289   if (!gGeoManager->cd(path)){
290     printf("Geo manager can not find path \n");
291     abort() ;
292   }
293   TGeoHMatrix *mPHOS = gGeoManager->GetCurrentMatrix();
294   if (mPHOS){
295      mPHOS->LocalToMaster(posL,posG);
296   }
297   else{
298     printf("Geo matrixes are not loaded \n") ;
299     abort() ;
300   }
301
302   Int_t relid[4] ;
303   gGeoManager->FindNode(posG[0],posG[1],posG[2]) ;
304   //Check that path contains PSTR and extract strip number
305   TString cpath(gGeoManager->GetPath()) ;
306   Int_t indx = cpath.Index("PCEL") ;
307   if(indx==-1){ //for the few events when particle hits between srips use ideal geometry
308     relid[0] = module ;
309     relid[1] = 0 ;
310     relid[2] = static_cast<Int_t>(TMath::Ceil( x/ fCellStep + fNPhi / 2.) );
311     relid[3] = static_cast<Int_t>(TMath::Ceil(-z/ fCellStep + fNZ   / 2.) ) ;
312     if(relid[2]<1)relid[2]=1 ;
313     if(relid[3]<1)relid[3]=1 ;
314     if(relid[2]>fNPhi)relid[2]=fNPhi ;
315     if(relid[3]>fNZ)relid[3]=fNZ ;
316     RelToAbsNumbering(relid,absId) ;
317   }
318   else{
319     Int_t indx2 = cpath.Index("/",indx) ;
320     if(indx2==-1)
321       indx2=cpath.Length() ;
322     TString cell=cpath(indx+5,indx2-indx-5) ;
323     Int_t icell=cell.Atoi() ;
324     indx = cpath.Index("PSTR") ;
325     indx2 = cpath.Index("/",indx) ;
326     TString strip=cpath(indx+5,indx2-indx-5) ;
327     Int_t iStrip = strip.Atoi() ; 
328
329     Int_t row = fNStripZ - (iStrip - 1) % (fNStripZ) ;
330     Int_t col = (Int_t) TMath::Ceil((Double_t) iStrip/(fNStripZ)) -1 ;
331  
332     // Absid for 8x2-strips. Looks nice :)
333     absId = (module-1)*fNCristalsInModule +
334                   row * 2 + (col*fNCellsXInStrip + (icell - 1) / 2)*fNZ - (icell & 1 ? 1 : 0);
335  
336   }
337 */
338  
339 }
340
341 //____________________________________________________________________________
342 void AliPHOSGeoUtils::RelPosToRelId(Int_t module, Double_t x, Double_t z, Int_t * relId) const
343 {
344   //Evaluates RelId of the crystall with given coordinates
345
346   Int_t absId ;
347   RelPosToAbsId(module, x,z,absId) ;
348   AbsToRelNumbering(absId,relId) ;
349 }
350
351 //____________________________________________________________________________
352 void AliPHOSGeoUtils::RelPosInAlice(Int_t id, TVector3 & pos ) const
353 {
354   // Converts the absolute numbering into the global ALICE coordinate system
355   
356   if (!gGeoManager){
357     printf("Geo manager not initialized\n");
358     abort();
359   }
360     
361   Int_t relid[4] ;
362     
363   AbsToRelNumbering(id , relid) ;
364     
365   //construct module name
366   if(relid[1]==0){ //this is EMC
367  
368     Double_t ps[3]= {0.0,-fCryStripShift,0.}; //Position incide the crystal
369     Double_t psC[3]={0.0,0.0,0.}; //Global position
370  
371     //Shift and possibly apply misalignment corrections
372     Int_t strip=1+((Int_t) TMath::Ceil((Double_t)relid[2]/fNCellsXInStrip))*fNStripZ-
373                 (Int_t) TMath::Ceil((Double_t)relid[3]/fNCellsZInStrip) ;
374     ps[0]=((relid[2]-1)%fNCellsXInStrip-fNCellsXInStrip/2+0.5)*fCellStep ;
375     ps[2]=(-(relid[3]-1)%fNCellsZInStrip+fNCellsZInStrip/2-0.5)*fCellStep ;
376  
377     Int_t mod = relid[0] ;
378     const TGeoHMatrix * m2 = GetMatrixForStrip(mod, strip) ;
379     m2->LocalToMaster(ps,psC);
380     pos.SetXYZ(psC[0],psC[1],psC[2]) ; 
381  
382   }
383   else{
384     //first calculate position with respect to CPV plain
385     Int_t row        = relid[2] ; //offset along x axis
386     Int_t column     = relid[3] ; //offset along z axis
387     Double_t ps[3]= {0.0,fCPVBoxSizeY/2.,0.}; //Position on top of CPV
388     Double_t psC[3]={0.0,0.0,0.}; //Global position
389     pos[0] = - ( fNumberOfCPVPadsPhi/2. - row    - 0.5 ) * fPadSizePhi  ; // position of pad  with respect
390     pos[2] = - ( fNumberOfCPVPadsZ  /2. - column - 0.5 ) * fPadSizeZ  ; // of center of PHOS module
391  
392     //now apply possible shifts and rotations
393     const TGeoHMatrix *m = GetMatrixForCPV(relid[0]) ;
394     m->LocalToMaster(ps,psC);
395     pos.SetXYZ(psC[0],psC[1],-psC[2]) ; 
396   }
397
398
399 //____________________________________________________________________________
400 void AliPHOSGeoUtils::Local2Global(Int_t mod, Float_t x, Float_t z,
401                                    TVector3& globalPosition) const 
402 {
403   Double_t posL[3]={x,-fCrystalShift,-z} ; //Only for EMC!!!
404   Double_t posG[3] ;
405   const TGeoHMatrix *mPHOS = GetMatrixForModule(mod) ;
406   mPHOS->LocalToMaster(posL,posG);
407   globalPosition.SetXYZ(posG[0],posG[1],posG[2]) ;
408 }
409 //____________________________________________________________________________
410 void AliPHOSGeoUtils::Global2Local(TVector3& localPosition,
411                                    const TVector3& globalPosition,
412                                    Int_t module) const
413 {
414   // Transforms a global position to the local coordinate system
415   // of the module 
416   //Return to PHOS local system
417   Double_t posG[3]={globalPosition.X(),globalPosition.Y(),globalPosition.Z()} ;
418   Double_t posL[3]={0.,0.,0.} ;
419   const TGeoHMatrix *mPHOS = GetMatrixForModule(module) ;
420   mPHOS->MasterToLocal(posG,posL);
421   localPosition.SetXYZ(posL[0],posL[1]+fCrystalShift,-posL[2]) ;  
422  
423 }
424 //____________________________________________________________________________
425 Bool_t AliPHOSGeoUtils::GlobalPos2RelId(TVector3 & global, Int_t * relId){
426   //Converts position in global ALICE coordinates to relId 
427   //returns false if x,z coordinates are beyond PHOS
428   //distande to PHOS surface is NOT calculated 
429   TVector3 loc ;
430   for(Int_t mod=1; mod<fNModules; mod++){
431     Global2Local(loc,global,mod) ;
432     //If in Acceptance
433     if((TMath::Abs(loc.Z())<fXtlArrSize[2]) && (TMath::Abs(loc.X())<fXtlArrSize[0])){
434        RelPosToRelId(mod,loc.X(),loc.Z(),relId);
435        return kTRUE ;
436     }
437   }
438   return kFALSE ; 
439
440 }
441 //____________________________________________________________________________
442 Bool_t AliPHOSGeoUtils::ImpactOnEmc(const TParticle * particle,
443        Int_t & moduleNumber, Double_t & z, Double_t & x) const
444 {
445   // Tells if a particle enters PHOS and evaluates hit position
446   Double_t vtx[3]={particle->Vx(),particle->Vy(),particle->Vz()} ;
447   return ImpactOnEmc(vtx,particle->Theta(),particle->Phi(),moduleNumber,z,x);
448 }
449  
450 //____________________________________________________________________________
451 Bool_t AliPHOSGeoUtils::ImpactOnEmc(const Double_t * vtx, Double_t theta, Double_t phi, 
452                                   Int_t & moduleNumber, Double_t & z, Double_t & x) const
453 {
454   // calculates the impact coordinates on PHOS of a neutral particle  
455   // emitted in the vertex vtx[3] with direction vec(p) in the ALICE global coordinate system
456   TVector3 p(TMath::Sin(theta)*TMath::Cos(phi),TMath::Sin(theta)*TMath::Sin(phi),TMath::Cos(theta)) ;
457   return ImpactOnEmc(vtx,p,moduleNumber,z,x) ;
458
459 }
460 //____________________________________________________________________________
461 Bool_t AliPHOSGeoUtils::ImpactOnEmc(const Double_t * vtx, const TVector3 &p,
462                                   Int_t & moduleNumber, Double_t & z, Double_t & x) const
463 {
464   // calculates the impact coordinates on PHOS of a neutral particle  
465   // emitted in the vertex vtx[3] with direction theta and phi in the ALICE global coordinate system
466   TVector3 v(vtx[0],vtx[1],vtx[2]) ;
467
468   for(Int_t imod=1; imod<=fNModules ; imod++){
469     //create vector from (0,0,0) to center of crystal surface of imod module
470     Double_t tmp[3]={0.,-fCrystalShift,0.} ;
471  
472     const TGeoHMatrix *m = GetMatrixForModule(imod) ;
473     Double_t posG[3]={0.,0.,0.} ;
474     m->LocalToMaster(tmp,posG);
475     TVector3 n(posG[0],posG[1],posG[2]) ; 
476     Double_t direction=n.Dot(p) ;
477     if(direction<=0.)
478       continue ; //momentum directed FROM module
479     Double_t fr = (n.Mag2()-n.Dot(v))/direction ;  
480     //Calculate direction in module plain
481     n-=v+fr*p ;
482     n*=-1. ;
483     if(TMath::Abs(TMath::Abs(n.Z())<fXtlArrSize[2]) && n.Pt()<fXtlArrSize[0]){
484       moduleNumber = imod ;
485       z=n.Z() ;
486       x=TMath::Sign(n.Pt(),n.X()) ;
487       //no need to return to local system since we calcilated distance from module center
488       //and tilts can not be significant.
489       return kTRUE ;
490     }
491   }
492   //Not in acceptance
493   x=0; z=0 ;
494   moduleNumber=0 ;
495   return kFALSE ;
496
497 }
498 //____________________________________________________________________________
499 void AliPHOSGeoUtils::GetIncidentVector(const TVector3 &vtx, Int_t module, Float_t x,Float_t z, TVector3 &vInc) const {
500   //Calculates vector pointing from vertex to current poisition in module local frame
501   //Note that PHOS local system and ALICE global have opposite z directions
502
503   Global2Local(vInc,vtx,module) ; 
504   vInc.SetXYZ(vInc.X()+x,vInc.Y(),vInc.Z()+z) ;
505 }
506 //____________________________________________________________________________
507 const TGeoHMatrix * AliPHOSGeoUtils::GetMatrixForModule(Int_t mod)const {
508   //Provides shift-rotation matrix for module mod
509
510   //If GeoManager exists, take matrixes from it
511   if(gGeoManager){
512     char path[255] ;
513     sprintf(path,"/ALIC_1/PHOS_%d/PEMC_1/PCOL_1/PTIO_1/PCOR_1/PAGA_1/PTII_1",mod) ;
514     //    sprintf(path,"/ALIC_1/PHOS_%d",relid[0]) ;
515     if (!gGeoManager->cd(path)){
516       printf("Geo manager can not find path \n");
517       abort();
518     }
519     return gGeoManager->GetCurrentMatrix();
520   }
521   if(fEMCMatrix[mod-1]){
522     return fEMCMatrix[mod-1] ;
523   }
524   else{
525     printf("Can not find PHOS misalignment matrixes\n") ;
526     printf("Either import TGeoManager from geometry.root or \n");
527     printf("read stored matrixes from AliESD Header: \n") ;
528     printf("AliPHOSGeoUtils::SetMisalMatrixes(header->GetPHOSMisalMatrix()) \n") ; 
529     abort() ;
530   }
531   return 0 ;
532 }
533 //____________________________________________________________________________
534 const TGeoHMatrix * AliPHOSGeoUtils::GetMatrixForStrip(Int_t mod, Int_t strip)const {
535   //Provides shift-rotation matrix for strip unit of the module mod
536
537   //If GeoManager exists, take matrixes from it
538   if(gGeoManager){
539     char path[255] ;
540     sprintf(path,"/ALIC_1/PHOS_%d/PEMC_1/PCOL_1/PTIO_1/PCOR_1/PAGA_1/PTII_1/PSTR_%d",mod,strip) ;
541     if (!gGeoManager->cd(path)){
542       printf("Geo manager can not find path \n");
543       abort() ;
544     }
545     return gGeoManager->GetCurrentMatrix();
546   } 
547   if(fStripMatrix[mod-1][strip-1]){
548     return fStripMatrix[mod-1][strip-1] ;
549   }
550   else{
551     printf("Can not find PHOS misalignment matrixes\n") ;
552     printf("Either import TGeoManager from geometry.root or \n");
553     printf("read stored matrixes from AliESD Header: \n") ; 
554     printf("AliPHOSGeoUtils::SetMisalMatrixes(header->GetPHOSMisalMatrix()) \n") ;
555     abort() ;
556   } 
557   return 0 ;
558 }
559 //____________________________________________________________________________
560 const TGeoHMatrix * AliPHOSGeoUtils::GetMatrixForCPV(Int_t mod)const {
561   //Provides shift-rotation matrix for CPV of the module mod
562
563   //If GeoManager exists, take matrixes from it
564   if(gGeoManager){ 
565     char path[255] ;
566     //now apply possible shifts and rotations
567     sprintf(path,"/ALIC_1/PHOS_%d/PCPV_1",mod) ;
568     if (!gGeoManager->cd(path)){
569       printf("Geo manager can not find path \n");
570       abort() ;
571     }
572     return gGeoManager->GetCurrentMatrix();
573   }
574   if(fCPVMatrix[mod-1]){
575     return fCPVMatrix[mod-1] ;
576   }
577   else{
578     printf("Can not find PHOS misalignment matrixes\n") ;
579     printf("Either import TGeoManager from geometry.root or \n");
580     printf("read stored matrixes from AliESD Header: \n") ;  
581     printf("AliPHOSGeoUtils::SetMisalMatrixes(header->GetPHOSMisalMatrix()) \n") ;
582     abort() ;
583   }
584   return 0 ;
585
586 //____________________________________________________________________________
587 const TGeoHMatrix * AliPHOSGeoUtils::GetMatrixForPHOS(Int_t mod)const {
588   //Provides shift-rotation matrix for PHOS (EMC+CPV) 
589
590   //If GeoManager exists, take matrixes from it
591   if(gGeoManager){
592     char path[255] ;
593     sprintf(path,"/ALIC_1/PHOS_%d",mod) ;
594     if (!gGeoManager->cd(path)){
595       printf("Geo manager can not find path \n");
596       abort() ;
597     }
598     return gGeoManager->GetCurrentMatrix();
599   }
600   if(fPHOSMatrix[mod-1]){
601     return fPHOSMatrix[mod-1] ;
602   }
603   else{
604     printf("Can not find PHOS misalignment matrixes\n") ;
605     printf("Either import TGeoManager from geometry.root or \n");
606     printf("read stored matrixes from AliESD Header:  \n") ;   
607     printf("AliPHOSGeoUtils::SetMisalMatrixes(header->GetPHOSMisalMatrix()) \n") ;
608     abort() ;
609   }
610   return 0 ;
611 }
612 //____________________________________________________________________________
613 void AliPHOSGeoUtils::SetMisalMatrix(const TGeoHMatrix * m, Int_t mod){
614   //Fills pointers to geo matrixes
615  
616   fPHOSMatrix[mod]=m ;
617
618   //If modules does not exist, make sure all its matrixes are zero
619   if(m==NULL){
620     fEMCMatrix[mod]=NULL ;
621     Int_t istrip=0 ;
622     for(Int_t irow = 0; irow < fGeometryEMCA->GetNStripX(); irow ++){
623       for(Int_t icol = 0; icol < fGeometryEMCA->GetNStripZ(); icol ++){
624         fStripMatrix[mod][istrip]=NULL ;
625       }
626     } 
627     fCPVMatrix[mod]=NULL ;
628     return ;
629   }
630
631   //Calculate maxtrixes for PTII
632   if(!fMisalArray)
633     fMisalArray = new TClonesArray("TGeoHMatrix",1120+10) ;
634   Int_t nr = fMisalArray->GetEntriesFast() ;
635   Double_t rotEMC[9]={1.,0.,0.,0.,0.,-1.,0.,1.,0.} ;
636   const Float_t * inthermo = fGeometryEMCA->GetInnerThermoHalfSize() ;
637   const Float_t * strip    = fGeometryEMCA->GetStripHalfSize() ;
638   const Float_t * covparams = fGeometryEMCA->GetAlCoverParams() ;
639   const Float_t * warmcov = fGeometryEMCA->GetWarmAlCoverHalfSize() ;
640   Float_t z = fGeometryCPV->GetCPVBoxSize(1) / 2. - warmcov[2] + covparams[3]-inthermo[1] ;
641   Double_t locTII[3]={0.,0.,z} ; 
642   Double_t globTII[3] ;
643
644   TGeoHMatrix * mTII = new((*fMisalArray)[nr])TGeoHMatrix() ;
645   nr++ ;
646   mTII->SetRotation(rotEMC) ;
647   mTII->MultiplyLeft(fPHOSMatrix[mod]) ;
648   fPHOSMatrix[mod]->LocalToMaster(locTII,globTII) ;
649   mTII->SetTranslation(globTII) ;
650   fEMCMatrix[mod]=mTII ;
651  
652   //Now calculate ideal matrixes for strip misalignment.
653   //For the moment we can not store them in ESDHeader
654
655   Double_t loc[3]={0.,inthermo[1] - strip[1],0.} ; 
656   Double_t glob[3] ;
657
658   Int_t istrip=0 ;
659   for(Int_t irow = 0; irow < fGeometryEMCA->GetNStripX(); irow ++){
660     loc[0] = (2*irow + 1 - fGeometryEMCA->GetNStripX())* strip[0] ;
661     for(Int_t icol = 0; icol < fGeometryEMCA->GetNStripZ(); icol ++){
662       loc[2] = (2*icol + 1 - fGeometryEMCA->GetNStripZ()) * strip[2] ;
663       fEMCMatrix[mod]->LocalToMaster(loc,glob) ;
664       TGeoHMatrix * mSTR = new((*fMisalArray)[nr])TGeoHMatrix(*(fEMCMatrix[mod])) ; //Use same rotation as PHOS module
665       nr++ ;
666       mSTR->SetTranslation(glob) ;
667       fStripMatrix[mod][istrip]=mSTR ;
668       istrip++;
669     }
670   }
671  
672   //Now calculate CPV matrixes
673   const Float_t * emcParams = fGeometryEMCA->GetEMCParams() ;
674   Double_t globCPV[3] ;
675   Double_t locCPV[3]={0.,0.,- emcParams[3]} ;
676   Double_t rot[9]={1.,0.,0.,0.,0.,1.,0.,-1.,0.} ;
677
678   TGeoHMatrix * mCPV = new((*fMisalArray)[nr])TGeoHMatrix() ;
679   nr++ ;
680   mCPV->SetRotation(rot) ;
681   mCPV->MultiplyLeft(fPHOSMatrix[mod]) ;
682   mCPV->ReflectY(kFALSE) ;
683   fPHOSMatrix[mod]->LocalToMaster(locCPV,globCPV) ;
684   mCPV->SetTranslation(globCPV) ;
685   fCPVMatrix[mod]=mCPV ;
686
687 }
688