]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALGeometry.cxx
Pass the name of the MC model (Geant3, Geant4, Fluka), and title (G4 Physics list...
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALGeometry.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$*/
17
18 //_________________________________________________________________________
19 // Geometry class  for EMCAL : singleton 
20 // EMCAL consists of layers of scintillator and lead
21 // with scintillator fiber arranged as "shish-kebab" skewers 
22 // Places the the Barrel Geometry of The EMCAL at Midrapidity
23 // between 80 and 180(or 190) degrees of Phi and
24 // -0.7 to 0.7 in eta 
25 //
26 //     EMCAL geometry tree:
27 //     EMCAL -> superModule -> module -> tower(cell)
28 //     Indexes
29 //     absId -> nSupMod     -> nModule -> (nIphi,nIeta)
30 //
31 //   Name choices: 
32 //   EMCAL_PDC06 (geometry used for PDC06 simulations, kept for backward compatibility)
33 //      = equivalent to SHISH_77_TRD1_2X2_FINAL_110DEG in old notation
34 //   EMCAL_COMPLETE (geometry for expected complete detector)
35 //      = equivalent to SHISH_77_TRD1_2X2_FINAL_110DEG scTh=0.176 pbTh=0.144
36 //          in old notation
37 //   EMCAL_FIRSTYEARV1 - geometry for December 2009 to December 2010 run period; 
38 //                fixed bug for positions of modules inside SM
39 //                (first module has tilt 0.75 degree);
40 //                the sizes updated with last information from production
41 //                drawing (end of October 2010). 
42 //      
43 //   EMCAL_COMPLETEV1: Same fixes as FIRSTYEAR and 10 SM instead of 10+2 half SM
44 //
45 //   EMCAL_WSUC (Wayne State test stand)
46 //      = no definite equivalent in old notation, was only used by
47 //          Aleksei, but kept for testing purposes
48 //
49 //   etc.
50
51 //
52 // Usage: 
53 //        You can create the AliEMCALGeometry object independently from anything.
54 //        You have to use just the correct name of geometry. If name is empty string the
55 //        default name of geometry will be used.
56 //         
57 //  AliEMCALGeometry* g = AliEMCALGeometry::GetInstance(name,title); // first time
58 //  ..
59 //  g = AliEMCALGeometry::GetInstance();                             // after first time
60 //
61 //  MC:   If you work with MC data you have to get geometry the next way: 
62 //  ==                                      =============================
63 //  AliRunLoader    *rl   = AliRunLoader::Instance();
64 //  AliEMCALGeometry *geom = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
65 //  TGeoManager::Import("geometry.root");
66 //
67 //*-- Author: Sahal Yacoob (LBL / UCT)
68 //     and  : Yves Schutz (SUBATECH)
69 //     and  : Jennifer Klay (LBL)
70 //     and  : Alexei Pavlinov (WSU) 
71 //
72 //  Implementation for analysis usage, before AliEMCALGeometry now (06/2011) merged again
73 //  in AliEMCALGeometry
74 //                  
75 // -- Author: Magali Estienne (magali.estienne@subatech.in2p3.fr)
76 //
77 //
78 // Usage: 
79 //        You can create the AliEMCALGeometry object independently from anything.
80 //        You have to use just the correct name of geometry. If name is empty string the
81 //        default name of geometry will be used.
82 //         
83 //  AliEMCALGeometry* geom = new AliEMCALGeometry("EMCAL_COMPLETEV1","EMCAL");
84 //  TGeoManager::Import("geometry.root");
85 //
86 //  MC:   If you work with MC data you have to get geometry the next way: 
87 //  ==                                      =============================
88 // !!!!!!!!! This part has to be modified
89 //  AliRunLoader    *rl   = AliRunLoader::GetRunLoader();
90 //  AliEMCALEMCGeometry *geom = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
91 //  TGeoManager::Import("geometry.root");
92
93
94 // --- ROOT system ---
95
96 #include <TParticle.h>
97 #include <TGeoManager.h>
98 #include <TGeoMatrix.h>
99 #include <TGeoBBox.h>
100 #include <TList.h>
101 #include <TBrowser.h>
102
103 // --- Standard library ---
104 //#include <Riostream.h>
105
106 // --- AliRoot header files ---
107 #include "AliEMCALGeometry.h"
108 #include "AliEMCALShishKebabTrd1Module.h"
109
110 ClassImp(AliEMCALGeometry)
111
112 // these initialisations are needed for a singleton
113 AliEMCALGeometry  *AliEMCALGeometry::fgGeom      = 0;
114 const Char_t*      AliEMCALGeometry::fgkDefaultGeometryName = "EMCAL_COMPLETEV1";
115
116 //____________________________________________________________________________
117 AliEMCALGeometry::AliEMCALGeometry():
118   fEMCGeometry(0x0),fGeoName(0),
119   fKey110DEG(0),fNCellsInSupMod(0),fNETAdiv(0),fNPHIdiv(0),
120   fNCellsInModule(0),fPhiBoundariesOfSM(0x0),fPhiCentersOfSM(0x0),
121   fPhiCentersOfCells(0x0),fCentersOfCellsEtaDir(0x0),
122   fCentersOfCellsPhiDir(0x0),fEtaCentersOfCells(0x0),
123   fNCells(0),fNPhi(0),fCentersOfCellsXDir(0x0),fArm1EtaMin(0),
124   fArm1EtaMax(0),fArm1PhiMin(0),fArm1PhiMax(0),fEtaMaxOfTRD1(0),
125   fShishKebabTrd1Modules(0),fPhiModuleSize(0.),
126   fEtaModuleSize(0.),fPhiTileSize(0.),fEtaTileSize(0.),fNZ(0),
127   fIPDistance(0.),fLongModuleSize(0.),fShellThickness(0.),
128   fZLength(0.),fSampling(0.),fUseExternalMatrices(kFALSE)
129 {
130   // default ctor 
131   // must be kept public for root persistency purposes, but should never be called by the outside world
132   fEnvelop[0] = 0.;
133   fEnvelop[1] = 0.;
134   fEnvelop[2] = 0.;
135   fParSM[0]   = 0.;
136   fParSM[1]   = 0.;
137   fParSM[2]   = 0.;
138   for (Int_t i=0;i<AliEMCALGeoParams::fgkEMCALModules;i++)
139     fkSModuleMatrix[i]=0 ;
140
141   for (Int_t i = 0; i < 48; i++)
142         for (Int_t j = 0; j < 64; j++) fFastOR2DMap[i][j] = -1;
143 }  
144
145 //____________________________________________________________________________
146 AliEMCALGeometry::AliEMCALGeometry(const AliEMCALGeometry & geo)
147   : TNamed(geo),
148     fEMCGeometry(geo.fEMCGeometry),fGeoName(geo.fGeoName),
149     fKey110DEG(geo.fKey110DEG),fNCellsInSupMod(geo.fNCellsInSupMod),fNETAdiv(geo.fNETAdiv),fNPHIdiv(geo.fNPHIdiv),
150     fNCellsInModule(geo.fNCellsInModule),fPhiBoundariesOfSM(geo.fPhiBoundariesOfSM),fPhiCentersOfSM(geo.fPhiCentersOfSM),
151     fPhiCentersOfCells(geo.fPhiCentersOfCells),fCentersOfCellsEtaDir(geo.fCentersOfCellsEtaDir),
152     fCentersOfCellsPhiDir(geo.fCentersOfCellsPhiDir),fEtaCentersOfCells(geo.fEtaCentersOfCells),
153     fNCells(geo.fNCells),fNPhi(geo.fNPhi),fCentersOfCellsXDir(geo.fCentersOfCellsXDir),fArm1EtaMin(geo.fArm1EtaMin),
154     fArm1EtaMax(geo.fArm1EtaMax),fArm1PhiMin(geo.fArm1PhiMin),fArm1PhiMax(geo.fArm1PhiMax),fEtaMaxOfTRD1(geo.fEtaMaxOfTRD1),
155     fShishKebabTrd1Modules(geo.fShishKebabTrd1Modules),fPhiModuleSize(geo.fPhiModuleSize),
156     fEtaModuleSize(geo.fEtaModuleSize),fPhiTileSize(geo.fPhiTileSize),fEtaTileSize(geo.fEtaTileSize),fNZ(geo.fNZ),
157     fIPDistance(geo.fIPDistance),fLongModuleSize(geo.fLongModuleSize),fShellThickness(geo.fShellThickness),
158     fZLength(geo.fZLength),fSampling(geo.fSampling),fUseExternalMatrices(geo.fUseExternalMatrices)
159 {
160   fEnvelop[0] = geo.fEnvelop[0];
161   fEnvelop[1] = geo.fEnvelop[1];
162   fEnvelop[2] = geo.fEnvelop[2];
163   fParSM[0]   = geo.fParSM[0];
164   fParSM[1]   = geo.fParSM[1];
165   fParSM[2]   = geo.fParSM[2];
166   for (Int_t i=0;i<AliEMCALGeoParams::fgkEMCALModules;i++)
167     fkSModuleMatrix[i]=0 ;
168   
169   for (Int_t i = 0; i < 48; i++)
170         for (Int_t j = 0; j < 64; j++) fFastOR2DMap[i][j] = geo.fFastOR2DMap[i][j];
171 }
172
173 //____________________________________________________________________________
174 AliEMCALGeometry::AliEMCALGeometry(const Text_t* name,   const Text_t* title,
175                                    const Text_t* mcname, const Text_t* mctitle) 
176   : TNamed(name, title),
177     fEMCGeometry(0x0),fGeoName(0),
178     fKey110DEG(0),fNCellsInSupMod(0),fNETAdiv(0),fNPHIdiv(0),
179     fNCellsInModule(0),fPhiBoundariesOfSM(0x0),fPhiCentersOfSM(0x0),
180     fPhiCentersOfCells(0x0),fCentersOfCellsEtaDir(0x0),
181     fCentersOfCellsPhiDir(0x0),fEtaCentersOfCells(0x0),
182     fNCells(0),fNPhi(0),fCentersOfCellsXDir(0x0),fArm1EtaMin(0),
183     fArm1EtaMax(0),fArm1PhiMin(0),fArm1PhiMax(0),fEtaMaxOfTRD1(0),
184     fShishKebabTrd1Modules(0),fPhiModuleSize(0.),
185     fEtaModuleSize(0.),fPhiTileSize(0.),fEtaTileSize(0.),fNZ(0),
186     fIPDistance(0.),fLongModuleSize(0.),fShellThickness(0.),
187     fZLength(0.),fSampling(0.), fUseExternalMatrices(kFALSE)
188
189
190   // ctor only for normal usage 
191   
192   fEMCGeometry = new AliEMCALEMCGeometry(name,title,mcname,mctitle);
193
194   fGeoName = fEMCGeometry->GetGeoName();
195   fKey110DEG = fEMCGeometry->GetKey110DEG();
196   fNCellsInSupMod = fEMCGeometry->GetNCellsInSupMod();
197   fNETAdiv = fEMCGeometry->GetNETAdiv();
198   fNPHIdiv = fEMCGeometry->GetNPHIdiv();
199   fNCellsInModule = fNPHIdiv*fNETAdiv;
200   static int i=0;
201   Int_t nSMod = fEMCGeometry->GetNumberOfSuperModules();
202   fPhiBoundariesOfSM.Set(nSMod);
203   fPhiCentersOfSM.Set(nSMod/2);
204   for(Int_t sm=0; sm<nSMod; sm++) {
205     i = sm/2;
206     fEMCGeometry->GetPhiBoundariesOfSM(sm,fPhiBoundariesOfSM[2*i],fPhiBoundariesOfSM[2*i+1]);
207   }
208
209   Double_t phiMin =  0.;
210   Double_t phiMax =  0.;
211   for(Int_t sm=0; sm<nSMod; sm++) {
212     fEMCGeometry->GetPhiBoundariesOfSM(sm,phiMin,phiMax);
213     i=sm/2;
214     fPhiCentersOfSM[i] = fEMCGeometry->GetPhiCenterOfSM(sm);
215   }
216   fNCells = fEMCGeometry->GetNCells();
217   fNPhi = fEMCGeometry->GetNPhi();
218   fEnvelop[0] = fEMCGeometry->GetEnvelop(0);
219   fEnvelop[1] = fEMCGeometry->GetEnvelop(1);
220   fEnvelop[2] = fEMCGeometry->GetEnvelop(2);
221   fParSM[0]   = fEMCGeometry->GetSuperModulesPar(0);
222   fParSM[1]   = fEMCGeometry->GetSuperModulesPar(1);
223   fParSM[2]   = fEMCGeometry->GetSuperModulesPar(2);
224   fArm1EtaMin = fEMCGeometry->GetArm1EtaMin();
225   fArm1EtaMax = fEMCGeometry->GetArm1EtaMax();
226   fArm1PhiMin = fEMCGeometry->GetArm1PhiMin();
227   fArm1PhiMax = fEMCGeometry->GetArm1PhiMax();
228   fShellThickness = fEMCGeometry->GetShellThickness();
229   fZLength    = fEMCGeometry->GetZLength();
230   fSampling   = fEMCGeometry->GetSampling();
231   fEtaModuleSize = fEMCGeometry->GetEtaModuleSize();
232   fPhiModuleSize = fEMCGeometry->GetPhiModuleSize();
233   fEtaTileSize = fEMCGeometry->GetEtaTileSize();
234   fPhiTileSize = fEMCGeometry->GetPhiTileSize();
235   fNZ          = fEMCGeometry->GetNZ();
236   fIPDistance  = fEMCGeometry->GetIPDistance();
237   fLongModuleSize = fEMCGeometry->GetLongModuleSize();
238
239   CreateListOfTrd1Modules();
240
241   for(Int_t smod=0; smod < AliEMCALGeoParams::fgkEMCALModules; smod++)
242     fkSModuleMatrix[smod]=0 ;   
243         
244   if (AliDebugLevel()>=2) {
245     fEMCGeometry->Print();
246     PrintGeometryGeoUtils();
247   }
248
249   for (Int_t ix = 0; ix < 48; ix++)
250         for (Int_t jx = 0; jx < 64; jx++) fFastOR2DMap[ix][jx] = -1;
251
252   BuildFastOR2DMap();
253 }
254
255 //____________________________________________________________________________
256 AliEMCALGeometry & AliEMCALGeometry::operator = (const AliEMCALGeometry  & /*rvalue*/) { 
257   //assing operator
258   Fatal("assignment operator", "not implemented") ; 
259   return *this ;
260 }
261
262 //____________________________________________________________________________
263 AliEMCALGeometry::~AliEMCALGeometry(void)
264 {
265   // dtor
266   if (this==fgGeom) {
267     AliError("Do not call delete on me");
268     return;
269   }
270   if (fEMCGeometry){ 
271     for(Int_t smod = 0 ; smod < fEMCGeometry->GetNumberOfSuperModules(); smod++){
272       if(fkSModuleMatrix[smod])
273         delete fkSModuleMatrix[smod] ;
274         fkSModuleMatrix[smod]=0 ;
275     }
276     delete fEMCGeometry; fEMCGeometry = 0 ;
277   }
278 }
279
280 //______________________________________________________________________
281 AliEMCALGeometry *  AliEMCALGeometry::GetInstance(){ 
282   // Returns the pointer of the unique instance
283   
284   AliEMCALGeometry * rv = static_cast<AliEMCALGeometry *>( fgGeom );
285   return rv; 
286 }
287
288 //______________________________________________________________________
289 AliEMCALGeometry* AliEMCALGeometry::GetInstance(const Text_t* name,   const Text_t* title,
290                                                 const Text_t* mcname, const Text_t* mctitle ){
291   // Returns the pointer of the unique instance
292     
293   AliEMCALGeometry * rv = 0; 
294   if ( fgGeom == 0 ) {
295     if ( strcmp(name,"") == 0 ) { // get default geometry
296       fgGeom = new AliEMCALGeometry(fgkDefaultGeometryName, title,mcname,mctitle);
297     } else {
298       fgGeom = new AliEMCALGeometry(name, title,mcname,mctitle);
299     }  // end if strcmp(name,"")
300     if ( AliEMCALEMCGeometry::fgInit ) rv = (AliEMCALGeometry * ) fgGeom;
301     else {
302       rv = 0; 
303       delete fgGeom; 
304       fgGeom = 0; 
305     } // end if fgInit
306   }else{
307     if ( strcmp(fgGeom->GetName(), name) != 0) {
308       printf("\ncurrent geometry is %s : ", fgGeom->GetName());
309       printf(" you cannot call %s ",name);  
310     }else{
311       rv = (AliEMCALGeometry *) fgGeom; 
312     } // end 
313   }  // end if fgGeom
314   return rv; 
315 }
316
317 //________________________________________________________________________________________________
318 void AliEMCALGeometry::Browse(TBrowser* b)
319 {
320   //Browse the modules
321   if(fShishKebabTrd1Modules) b->Add(fShishKebabTrd1Modules);
322 }
323
324 //________________________________________________________________________________________________
325 Bool_t AliEMCALGeometry::IsFolder() const
326 {
327   //Check if fShishKebabTrd1Modules is in folder
328   if(fShishKebabTrd1Modules) return kTRUE;
329   else                       return kFALSE;
330 }
331
332 //________________________________________________________________________________________________
333 void AliEMCALGeometry::GetGlobal(const Double_t *loc, Double_t *glob, int ind) const
334 {
335   // Figure out the global numbering
336   // of a given supermodule from the
337   // local numbering and the transformation
338   // matrix stored by the geometry manager (allows for misaligned
339   // geometry)
340         
341         const TGeoHMatrix* m = GetMatrixForSuperModule(ind);
342     if(m) {
343       m->LocalToMaster(loc, glob);
344     } else {
345       AliFatal("Geo matrixes are not loaded \n") ;
346     }
347 }
348
349 //________________________________________________________________________________________________
350 void AliEMCALGeometry::GetGlobal(const TVector3 &vloc, TVector3 &vglob, int ind) const
351 {
352   //Figure out the global numbering
353   //of a given supermodule from the
354   //local numbering given a 3-vector location
355
356   static Double_t tglob[3], tloc[3];
357   vloc.GetXYZ(tloc);
358   GetGlobal(tloc, tglob, ind);
359   vglob.SetXYZ(tglob[0], tglob[1], tglob[2]);
360 }
361
362 //________________________________________________________________________________________________
363 void AliEMCALGeometry::GetGlobal(Int_t absId , double glob[3]) const
364 {
365   // Alice numbering scheme - Jun 03, 2006
366   static Int_t nSupMod=-1, nModule=-1, nIphi=-1, nIeta=-1;
367   static double loc[3];
368
369   glob[0]=glob[1]=glob[2]=0.0; // bad case
370   if(RelPosCellInSModule(absId, loc)) {
371     GetCellIndex(absId, nSupMod, nModule, nIphi, nIeta);
372
373           const TGeoHMatrix* m = GetMatrixForSuperModule(nSupMod);
374           if(m) {
375       m->LocalToMaster(loc, glob);
376     } else {
377       AliFatal("Geo matrixes are not loaded \n") ;
378     }
379   }
380 }
381
382 //___________________________________________________________________
383 void AliEMCALGeometry::GetGlobal(Int_t absId , TVector3 &vglob) const
384 {
385   // Alice numbering scheme - Jun 03, 2006
386   static Double_t glob[3];
387
388   GetGlobal(absId, glob);
389   vglob.SetXYZ(glob[0], glob[1], glob[2]);
390
391 }
392
393
394 //______________________________________________________________________
395 void AliEMCALGeometry::PrintCellIndexes(Int_t absId, int pri, const char *tit) const
396 {
397   // Service methods
398   Int_t nSupMod, nModule, nIphi, nIeta;
399   Int_t iphi, ieta;
400   TVector3 vg;
401
402   GetCellIndex(absId,  nSupMod, nModule, nIphi, nIeta);
403   printf(" %s | absId : %i -> nSupMod %i nModule %i nIphi %i nIeta %i \n", tit, absId,  nSupMod, nModule, nIphi, nIeta);
404   if(pri>0) {
405     GetCellPhiEtaIndexInSModule(nSupMod,nModule,nIphi,nIeta, iphi,ieta);
406     printf(" local SM index : iphi %i : ieta %i \n", iphi,ieta);
407     GetGlobal(absId, vg);
408     printf(" vglob : mag %7.2f : perp %7.2f : z %7.2f : eta %6.4f : phi %6.4f(%6.2f) \n", 
409            vg.Mag(), vg.Perp(), vg.Z(), vg.Eta(), vg.Phi(), vg.Phi()*TMath::RadToDeg());
410   }
411 }
412
413 void AliEMCALGeometry::PrintLocalTrd1(Int_t pri) const
414 {
415   // For comparing with numbers from drawing
416   for(Int_t i=0; i<GetShishKebabTrd1Modules()->GetSize(); i++){
417     printf(" %s | ", GetShishKebabModule(i)->GetName());
418     if(i==0 && pri<1) GetShishKebabModule(i)->PrintShish(1);
419     else     GetShishKebabModule(i)->PrintShish(pri);
420   }
421 }
422
423 //________________________________________________________________________________________________
424 void AliEMCALGeometry::EtaPhiFromIndex(Int_t absId,Double_t &eta,Double_t &phi) const
425 {
426   // Nov 16, 2006- float to double
427   // version for TRD1 only
428   static TVector3 vglob;
429   GetGlobal(absId, vglob);
430   eta = vglob.Eta();
431   phi = vglob.Phi();
432 }
433
434 //________________________________________________________________________________________________
435 void AliEMCALGeometry::EtaPhiFromIndex(Int_t absId,Float_t &eta,Float_t &phi) const
436 {
437   // Nov 16,2006 - should be discard in future
438   static TVector3 vglob;
439   GetGlobal(absId, vglob);
440   eta = float(vglob.Eta());
441   phi = float(vglob.Phi());
442 }
443
444 //
445 // == Shish-kebab cases ==
446 //
447 //________________________________________________________________________________________________
448 Int_t AliEMCALGeometry::GetAbsCellId(Int_t nSupMod, Int_t nModule, Int_t nIphi, Int_t nIeta) const
449
450   // 27-aug-04; 
451   // corr. 21-sep-04; 
452   //       13-oct-05; 110 degree case
453   // May 31, 2006; ALICE numbering scheme:
454   // 0 <= nSupMod < fNumberOfSuperModules
455   // 0 <= nModule  < fNPHI * fNZ ( fNPHI * fNZ/2 for fKey110DEG=1)
456   // 0 <= nIphi   < fNPHIdiv
457   // 0 <= nIeta   < fNETAdiv
458   // 0 <= absid   < fNCells
459   static Int_t id=0; // have to change from 0 to fNCells-1
460   if(fKey110DEG == 1 && nSupMod >= 10) { // 110 degree case; last two supermodules
461     id  = fNCellsInSupMod*10 + (fNCellsInSupMod/2)*(nSupMod-10);
462   } else {
463     id  = fNCellsInSupMod*nSupMod;
464   }
465   id += fNCellsInModule *nModule;
466   id += fNPHIdiv *nIphi;
467   id += nIeta;
468   if(id<0 || id >= fNCells) {
469 //     printf(" wrong numerations !!\n");
470 //     printf("    id      %6i(will be force to -1)\n", id);
471 //     printf("    fNCells %6i\n", fNCells);
472 //     printf("    nSupMod %6i\n", nSupMod);
473 //     printf("    nModule  %6i\n", nModule);
474 //     printf("    nIphi   %6i\n", nIphi);
475 //     printf("    nIeta   %6i\n", nIeta);
476     id = -TMath::Abs(id); // if negative something wrong
477   }
478   return id;
479 }
480
481 //________________________________________________________________________________________________
482 void  AliEMCALGeometry::GetModuleIndexesFromCellIndexesInSModule(Int_t nSupMod, Int_t iphi, Int_t ieta, 
483                         Int_t &iphim, Int_t &ietam, Int_t &nModule) const
484 {
485   // Transition from cell indexes (ieta,iphi) to module indexes (ietam,iphim, nModule)
486   static Int_t nphi=-1;
487   nphi  = GetNumberOfModuleInPhiDirection(nSupMod);  
488
489   ietam  = ieta/fNETAdiv;
490   iphim  = iphi/fNPHIdiv;
491   nModule = ietam * nphi + iphim; 
492 }
493
494 //________________________________________________________________________________________________
495 Int_t  AliEMCALGeometry::GetAbsCellIdFromCellIndexes(Int_t nSupMod, Int_t iphi, Int_t ieta) const
496 {
497   // Transition from super module number(nSupMod) and cell indexes (ieta,iphi) to absId
498   static Int_t ietam=-1, iphim=-1, nModule=-1;
499   static Int_t nIeta=-1, nIphi=-1; // cell indexes in module
500
501   GetModuleIndexesFromCellIndexesInSModule(nSupMod, iphi, ieta, ietam, iphim, nModule);
502
503   nIeta = ieta%fNETAdiv;
504   nIeta = fNETAdiv - 1 - nIeta;
505   nIphi = iphi%fNPHIdiv;
506
507   return GetAbsCellId(nSupMod, nModule, nIphi, nIeta);
508 }
509
510 //________________________________________________________________________________________________
511 Bool_t AliEMCALGeometry::SuperModuleNumberFromEtaPhi(Double_t eta, Double_t phi, Int_t &nSupMod) const
512
513   // Return false if phi belongs a phi cracks between SM
514  
515   static Int_t i=0;
516
517   if(TMath::Abs(eta) > fEtaMaxOfTRD1) return kFALSE;
518
519   phi = TVector2::Phi_0_2pi(phi); // move phi to (0,2pi) boundaries
520   for(i=0; i<6; i++) {
521         
522         //Check if it is not the complete geometry
523         if (i >= fEMCGeometry->GetNumberOfSuperModules()/2) return kFALSE;
524
525     if(phi>=fPhiBoundariesOfSM[2*i] && phi<=fPhiBoundariesOfSM[2*i+1]) {
526       nSupMod = 2*i;
527       if(eta < 0.0) nSupMod++;
528       AliDebug(1,Form("eta %f phi %f(%5.2f) : nSupMod %i : #bound %i", eta,phi,phi*TMath::RadToDeg(), nSupMod,i));
529       return kTRUE;
530     }
531   }
532   return kFALSE;
533 }
534
535
536 //________________________________________________________________________________________________
537 Bool_t AliEMCALGeometry::GetAbsCellIdFromEtaPhi(Double_t eta, Double_t phi, Int_t &absId) const
538 {
539   // Nov 17,2006
540   // stay here - phi problem as usual 
541   static Int_t nSupMod=-1, i=0, ieta=-1, iphi=-1, etaShift=0, nphi=-1;
542   static Double_t absEta=0.0, d=0.0, dmin=0.0, phiLoc=0;
543   absId = nSupMod = - 1;
544   if(SuperModuleNumberFromEtaPhi(eta, phi, nSupMod)) {
545     // phi index first
546     phi    = TVector2::Phi_0_2pi(phi);
547     phiLoc = phi - fPhiCentersOfSM[nSupMod/2];
548     nphi   = fPhiCentersOfCells.GetSize();
549     if(nSupMod>=10) {
550       phiLoc = phi - 190.*TMath::DegToRad();
551       nphi  /= 2;
552     }
553
554     dmin   = TMath::Abs(fPhiCentersOfCells[0]-phiLoc);
555     iphi   = 0;
556     for(i=1; i<nphi; i++) {
557       d = TMath::Abs(fPhiCentersOfCells[i] - phiLoc);
558       if(d < dmin) {
559         dmin = d;
560         iphi = i;
561       }
562       //      printf(" i %i : d %f : dmin %f : fPhiCentersOfCells[i] %f \n", i, d, dmin, fPhiCentersOfCells[i]);
563     }
564     // odd SM are turned with respect of even SM - reverse indexes
565     AliDebug(2,Form(" iphi %i : dmin %f (phi %f, phiLoc %f ) ", iphi, dmin, phi, phiLoc));
566     // eta index
567     absEta   = TMath::Abs(eta);
568     etaShift = iphi*fCentersOfCellsEtaDir.GetSize();
569     dmin     = TMath::Abs(fEtaCentersOfCells[etaShift]-absEta);
570     ieta     = 0;
571     for(i=1; i<fCentersOfCellsEtaDir.GetSize(); i++) {
572       d = TMath::Abs(fEtaCentersOfCells[i+etaShift] - absEta);
573       if(d < dmin) {
574         dmin = d;
575         ieta = i;
576       }
577     }
578     AliDebug(2,Form(" ieta %i : dmin %f (eta=%f) : nSupMod %i ", ieta, dmin, eta, nSupMod));
579
580     if(eta<0) iphi = (nphi-1) - iphi;
581           
582         //patch for mapping following alice convention  
583         if(nSupMod%2 == 0)                
584                   ieta = (fCentersOfCellsEtaDir.GetSize()-1)-ieta;// 47-ieta, revert the ordering on A side in order to keep convention.
585         else {
586                 if(nSupMod<10) 
587                                 iphi = (fCentersOfCellsPhiDir.GetSize()-1)  -iphi;// 23-iphi, revert the ordering on C side in order to keep convention.
588                 else 
589                                 iphi = (fCentersOfCellsPhiDir.GetSize()/2-1)-iphi;// 11-iphi, revert the ordering on C side in order to keep convention.
590         }
591   
592     absId = GetAbsCellIdFromCellIndexes(nSupMod, iphi, ieta);
593
594     return kTRUE;
595   }
596   return kFALSE;
597 }
598
599 //________________________________________________________________________________________________
600 Bool_t  AliEMCALGeometry::CheckAbsCellId(Int_t absId) const
601
602   // May 31, 2006; only trd1 now
603   if(absId<0 || absId >= fNCells) return kFALSE;
604   else                            return kTRUE;
605 }
606
607 //________________________________________________________________________________________________
608 Bool_t AliEMCALGeometry::GetCellIndex(Int_t absId,Int_t &nSupMod,Int_t &nModule,Int_t &nIphi,Int_t &nIeta) const
609
610   // 21-sep-04; 19-oct-05;
611   // May 31, 2006; ALICE numbering scheme:
612   // 
613   // In:
614   // absId   - cell is as in Geant,     0<= absId   < fNCells;
615   // Out:
616   // nSupMod - super module(SM) number, 0<= nSupMod < fNumberOfSuperModules;
617   // nModule  - module number in SM,     0<= nModule  < fNCellsInSupMod/fNCellsInSupMod or(/2) for tow last SM (10th and 11th);
618   // nIphi   - cell number in phi driection inside module; 0<= nIphi < fNPHIdiv; 
619   // nIeta   - cell number in eta driection inside module; 0<= nIeta < fNETAdiv; 
620   // 
621   static Int_t tmp=0, sm10=0;
622   if(!CheckAbsCellId(absId)) return kFALSE;
623
624   sm10 = fNCellsInSupMod*10;
625   if(fKey110DEG == 1 && absId >= sm10) { // 110 degree case; last two supermodules  
626     nSupMod = (absId-sm10) / (fNCellsInSupMod/2) + 10;
627     tmp     = (absId-sm10) % (fNCellsInSupMod/2);
628   } else {
629     nSupMod = absId / fNCellsInSupMod;
630     tmp     = absId % fNCellsInSupMod;
631   }
632
633   nModule  = tmp / fNCellsInModule;
634   tmp     = tmp % fNCellsInModule;
635   nIphi   = tmp / fNPHIdiv;
636   nIeta   = tmp % fNPHIdiv;
637
638   return kTRUE;
639 }
640
641 //________________________________________________________________________________________________
642 Int_t  AliEMCALGeometry::GetSuperModuleNumber(Int_t absId)  const
643 {
644   // Return the number of the  supermodule given the absolute
645   // ALICE numbering id
646
647   static Int_t nSupMod=-1, nModule=-1, nIphi=-1, nIeta=-1;
648   GetCellIndex(absId, nSupMod, nModule, nIphi, nIeta);
649   return nSupMod;
650
651
652 //________________________________________________________________________________________________
653 void AliEMCALGeometry::GetModulePhiEtaIndexInSModule(Int_t nSupMod, Int_t nModule,  int &iphim, int &ietam) const
654
655   // added nSupMod; - 19-oct-05 !
656   // Alice numbering scheme        - Jun 01,2006 
657   // ietam, iphi - indexes of module in two dimensional grid of SM
658   // ietam - have to change from 0 to fNZ-1
659   // iphim - have to change from 0 to nphi-1 (fNPhi-1 or fNPhi/2-1)
660   static Int_t nphi=-1;
661
662   if(fKey110DEG == 1 && nSupMod>=10) nphi = fNPhi/2;
663   else                               nphi = fNPhi;
664
665   ietam = nModule/nphi;
666   iphim = nModule%nphi;
667 }
668
669 //________________________________________________________________________________________________
670 void AliEMCALGeometry::GetCellPhiEtaIndexInSModule(Int_t nSupMod, Int_t nModule, Int_t nIphi, Int_t nIeta, 
671 int &iphi, int &ieta) const
672
673   // 
674   // Added nSupMod; Nov 25, 05
675   // Alice numbering scheme  - Jun 01,2006 
676   // IN:
677   // nSupMod - super module(SM) number, 0<= nSupMod < fNumberOfSuperModules;
678   // nModule  - module number in SM,     0<= nModule  < fNCellsInSupMod/fNCellsInSupMod or(/2) for tow last SM (10th and 11th);
679   // nIphi   - cell number in phi driection inside module; 0<= nIphi < fNPHIdiv; 
680   // nIeta   - cell number in eta driection inside module; 0<= nIeta < fNETAdiv; 
681   // 
682  // OUT:
683   // ieta, iphi - indexes of cell(tower) in two dimensional grid of SM
684   // ieta - have to change from 0 to (fNZ*fNETAdiv-1)
685   // iphi - have to change from 0 to (fNPhi*fNPHIdiv-1 or fNPhi*fNPHIdiv/2-1)
686   //
687   static Int_t iphim=-1, ietam=-1;
688
689   GetModulePhiEtaIndexInSModule(nSupMod,nModule, iphim, ietam); 
690   //  ieta  = ietam*fNETAdiv + (1-nIeta); // x(module) = -z(SM) 
691   ieta  = ietam*fNETAdiv + (fNETAdiv - 1 - nIeta); // x(module) = -z(SM) 
692   iphi  = iphim*fNPHIdiv + nIphi;     // y(module) =  y(SM) 
693
694   if(iphi<0 || ieta<0)
695   AliDebug(1,Form(" nSupMod %i nModule %i nIphi %i nIeta %i => ieta %i iphi %i\n", 
696   nSupMod, nModule, nIphi, nIeta, ieta, iphi));
697 }
698
699
700 // Methods for AliEMCALRecPoint - Feb 19, 2006
701 //________________________________________________________________________________________________
702 Bool_t AliEMCALGeometry::RelPosCellInSModule(Int_t absId, Double_t &xr, Double_t &yr, Double_t &zr) const
703 {
704   // Look to see what the relative
705   // position inside a given cell is
706   // for a recpoint.
707   // Alice numbering scheme - Jun 08, 2006
708   // In:
709   // absId   - cell is as in Geant,     0<= absId   < fNCells;
710   // OUT:
711   // xr,yr,zr - x,y,z coordinates of cell with absId inside SM 
712
713   // Shift index taking into account the difference between standard SM 
714   // and SM of half size in phi direction
715   const Int_t kphiIndexShift = fCentersOfCellsPhiDir.GetSize()/4; // Nov 22, 2006; was 6 for cas 2X2
716   static Int_t nSupMod=-1, nModule=-1, nIphi=-1, nIeta=-1, iphi=-1, ieta=-1;
717   if(!CheckAbsCellId(absId)) return kFALSE;
718
719   GetCellIndex(absId, nSupMod, nModule, nIphi, nIeta);
720   GetCellPhiEtaIndexInSModule(nSupMod,nModule,nIphi,nIeta, iphi, ieta); 
721         
722   //Get eta position. Careful with ALICE conventions (increase index decrease eta)      
723   Int_t ieta2 = ieta;
724   if(nSupMod%2 == 0)              
725           ieta2 = (fCentersOfCellsEtaDir.GetSize()-1)-ieta;// 47-ieta, revert the ordering on A side in order to keep convention.
726   zr = fCentersOfCellsEtaDir.At(ieta2); 
727   xr = fCentersOfCellsXDir.At(ieta2);
728
729   //Get phi position. Careful with ALICE conventions (increase index increase phi)
730   Int_t iphi2 = iphi;
731   if(nSupMod<10) { 
732                 if(nSupMod%2 != 0) 
733                         iphi2 = (fCentersOfCellsPhiDir.GetSize()-1)-iphi;// 23-iphi, revert the ordering on C side in order to keep convention.
734                 yr = fCentersOfCellsPhiDir.At(iphi2);
735           
736   } else {
737                 if(nSupMod%2 != 0) 
738                         iphi2 = (fCentersOfCellsPhiDir.GetSize()/2-1)-iphi;// 11-iphi, revert the ordering on C side in order to keep convention.
739                 yr = fCentersOfCellsPhiDir.At(iphi2 + kphiIndexShift);
740   }
741   AliDebug(1,Form("absId %i nSupMod %i iphi %i ieta %i xr %f yr %f zr %f ",absId,nSupMod,iphi,ieta,xr,yr,zr));
742
743   return kTRUE;
744 }
745
746 //________________________________________________________________________________________________
747 Bool_t AliEMCALGeometry::RelPosCellInSModule(Int_t absId, Double_t loc[3]) const
748 {
749   // Look to see what the relative
750   // position inside a given cell is
751   // for a recpoint.    // Alice numbering scheme - Jun 03, 2006
752   loc[0] = loc[1] = loc[2]=0.0;
753   if(RelPosCellInSModule(absId, loc[0],loc[1],loc[2])) {
754     return kTRUE;
755   }
756   return kFALSE;
757 }
758
759 //________________________________________________________________________________________________
760 Bool_t AliEMCALGeometry::RelPosCellInSModule(Int_t absId, TVector3 &vloc) const
761 {
762   // Look to see what the relative
763   // position inside a given cell is
764   // for a recpoint.  
765   // Alice numbering scheme - Jun 03, 2006
766   static Double_t loc[3];
767   if(RelPosCellInSModule(absId,loc)) {
768     vloc.SetXYZ(loc[0], loc[1], loc[2]);
769     return kTRUE;
770   } else {
771     vloc.SetXYZ(0,0,0);
772     return kFALSE;
773   }
774 }
775
776 //________________________________________________________________________________________________
777 Bool_t AliEMCALGeometry::RelPosCellInSModule(Int_t absId, Double_t distEff, Double_t &xr, Double_t &yr, Double_t &zr) const
778 {
779   // Jul 30, 2007 - taking into account position of shower max
780   // Look to see what the relative
781   // position inside a given cell is
782   // for a recpoint.
783   // In:
784   // absId   - cell is as in Geant,     0<= absId   < fNCells;
785   // e       - cluster energy
786   // OUT:
787   // xr,yr,zr - x,y,z coordinates of cell with absId inside SM 
788   
789   // Shift index taking into account the difference between standard SM 
790   // and SM of half size in phi direction
791   const  Int_t kphiIndexShift = fCentersOfCellsPhiDir.GetSize()/4; // Nov 22, 2006; was 6 for cas 2X2
792   static Int_t nSupMod=0, nModule=-1, nIphi=-1, nIeta=-1, iphi=-1, ieta=-1;
793   static Int_t iphim=-1, ietam=-1;
794   static AliEMCALShishKebabTrd1Module *mod = 0;
795   static TVector2 v;
796   if(!CheckAbsCellId(absId)) return kFALSE;
797   
798   GetCellIndex(absId, nSupMod, nModule, nIphi, nIeta);
799   GetModulePhiEtaIndexInSModule(nSupMod, nModule, iphim, ietam);
800   GetCellPhiEtaIndexInSModule(nSupMod,nModule,nIphi,nIeta, iphi, ieta); 
801   
802   //Get eta position. Careful with ALICE conventions (increase index decrease eta)      
803   if(nSupMod%2 == 0) {             
804     ietam = (fCentersOfCellsEtaDir.GetSize()/2-1)-ietam;// 47-ietam, revert the ordering on A side in order to keep convention.
805     if(nIeta == 0) nIeta = 1;
806     else           nIeta = 0;
807   }
808   mod = GetShishKebabModule(ietam);
809   mod ->GetPositionAtCenterCellLine(nIeta, distEff, v); 
810   xr = v.Y() - fParSM[0];
811   zr = v.X() - fParSM[2];
812   
813   //Get phi position. Careful with ALICE conventions (increase index increase phi)
814   Int_t iphi2 = iphi;
815   if(nSupMod<10) { 
816     if(nSupMod%2 != 0) 
817       iphi2 = (fCentersOfCellsPhiDir.GetSize()-1)-iphi;// 23-iphi, revert the ordering on C side in order to keep convention.
818     yr = fCentersOfCellsPhiDir.At(iphi2);
819     
820   } else {
821     if(nSupMod%2 != 0) 
822       iphi2 = (fCentersOfCellsPhiDir.GetSize()/2-1)-iphi;// 11-iphi, revert the ordering on C side in order to keep convention.
823     yr = fCentersOfCellsPhiDir.At(iphi2 + kphiIndexShift);
824   }
825   
826   AliDebug(1,Form("absId %i nSupMod %i iphi %i ieta %i xr %f yr %f zr %f ",absId,nSupMod,iphi,ieta,xr,yr,zr));
827   
828   return kTRUE;
829 }
830
831
832 //________________________________________________________________________________________________
833 void AliEMCALGeometry::CreateListOfTrd1Modules()
834 {
835   // Generate the list of Trd1 modules
836   // which will make up the EMCAL
837   // geometry
838   // key: look to the AliEMCALShishKebabTrd1Module::
839
840   AliDebug(2,Form(" AliEMCALGeometry::CreateListOfTrd1Modules() started "));
841
842   AliEMCALShishKebabTrd1Module *mod=0, *mTmp=0; // current module
843   if(fShishKebabTrd1Modules == 0) {
844     fShishKebabTrd1Modules = new TList;
845     fShishKebabTrd1Modules->SetName("ListOfTRD1");
846     for(int iz=0; iz< fEMCGeometry->GetNZ(); iz++) {
847       if(iz==0) {
848         //        mod  = new AliEMCALShishKebabTrd1Module(TMath::Pi()/2.,this);
849         mod  = new AliEMCALShishKebabTrd1Module(TMath::Pi()/2.,fEMCGeometry);
850       } else {
851         mTmp  = new AliEMCALShishKebabTrd1Module(*mod);
852         mod   = mTmp;
853       }
854       fShishKebabTrd1Modules->Add(mod);
855     }
856   } else {
857     AliDebug(2,Form(" Already exits : "));
858   }
859   mod = (AliEMCALShishKebabTrd1Module*)fShishKebabTrd1Modules->At(fShishKebabTrd1Modules->GetSize()-1);
860   fEtaMaxOfTRD1 = mod->GetMaxEtaOfModule(0);
861
862   AliDebug(2,Form(" fShishKebabTrd1Modules has %i modules : max eta %5.4f \n",
863                   fShishKebabTrd1Modules->GetSize(),fEtaMaxOfTRD1));
864   // Feb 20,2006;
865   // Jun 01, 2006 - ALICE numbering scheme
866   // define grid for cells in eta(z) and x directions in local coordinates system of SM
867   // Works just for 2x2 case only -- ?? start here
868   //
869   //
870   // Define grid for cells in phi(y) direction in local coordinates system of SM
871   // as for 2X2 as for 3X3 - Nov 8,2006
872   //
873   AliDebug(2,Form(" Cells grid in phi directions : size %i\n", fCentersOfCellsPhiDir.GetSize()));
874   Int_t ind=0; // this is phi index
875   Int_t ieta=0, nModule=0, iphiTemp;
876   Double_t xr=0., zr=0., theta=0., phi=0., eta=0., r=0., x=0.,y=0.;
877   TVector3 vglob;
878   Double_t ytCenterModule=0.0, ytCenterCell=0.0;
879
880   fCentersOfCellsPhiDir.Set(fNPhi*fNPHIdiv);
881   fPhiCentersOfCells.Set(fNPhi*fNPHIdiv);
882
883   Double_t r0 = fIPDistance + fLongModuleSize/2.;
884   for(Int_t it=0; it<fNPhi; it++) { // cycle on modules
885     ytCenterModule = -fParSM[1] + fPhiModuleSize*(2*it+1)/2;  // center of module
886     for(Int_t ic=0; ic<fNPHIdiv; ic++) { // cycle on cells in module
887       if(fNPHIdiv==2) {
888         ytCenterCell = ytCenterModule + fPhiTileSize *(2*ic-1)/2.;
889       } else if(fNPHIdiv==3){
890         ytCenterCell = ytCenterModule + fPhiTileSize *(ic-1);
891       } else if(fNPHIdiv==1){
892         ytCenterCell = ytCenterModule;
893       }
894       fCentersOfCellsPhiDir.AddAt(ytCenterCell,ind);
895       // Define grid on phi direction
896       // Grid is not the same for different eta bin;
897       // Effect is small but is still here
898       phi = TMath::ATan2(ytCenterCell, r0);
899       fPhiCentersOfCells.AddAt(phi, ind);
900
901       AliDebug(2,Form(" ind %2.2i : y %8.3f ", ind, fCentersOfCellsPhiDir.At(ind)));
902       ind++;
903     }
904   }
905
906   fCentersOfCellsEtaDir.Set(fNZ *fNETAdiv);
907   fCentersOfCellsXDir.Set(fNZ *fNETAdiv);
908   fEtaCentersOfCells.Set(fNZ *fNETAdiv * fNPhi*fNPHIdiv);
909   AliDebug(2,Form(" Cells grid in eta directions : size %i\n", fCentersOfCellsEtaDir.GetSize()));
910   for(Int_t it=0; it<fNZ; it++) {
911     AliEMCALShishKebabTrd1Module *trd1 = GetShishKebabModule(it);
912     nModule = fNPhi*it;
913     for(Int_t ic=0; ic<fNETAdiv; ic++) {
914       if(fNPHIdiv==2) {
915         trd1->GetCenterOfCellInLocalCoordinateofSM(ic, xr, zr);      // case of 2X2
916         GetCellPhiEtaIndexInSModule(0, nModule, 0, ic, iphiTemp, ieta);
917       } if(fNPHIdiv==3) {
918         trd1->GetCenterOfCellInLocalCoordinateofSM3X3(ic, xr, zr);  // case of 3X3
919         GetCellPhiEtaIndexInSModule(0, nModule, 0, ic, iphiTemp, ieta);
920       } if(fNPHIdiv==1) {
921         trd1->GetCenterOfCellInLocalCoordinateofSM1X1(xr, zr);      // case of 1X1
922         GetCellPhiEtaIndexInSModule(0, nModule, 0, ic, iphiTemp, ieta);
923       }
924       fCentersOfCellsXDir.AddAt(float(xr) - fParSM[0],ieta);
925       fCentersOfCellsEtaDir.AddAt(float(zr) - fParSM[2],ieta);
926       // Define grid on eta direction for each bin in phi
927       for(int iphi=0; iphi<fCentersOfCellsPhiDir.GetSize(); iphi++) {
928         x = xr + trd1->GetRadius();
929         y = fCentersOfCellsPhiDir[iphi];
930         r = TMath::Sqrt(x*x + y*y + zr*zr);
931         theta = TMath::ACos(zr/r);
932         eta   = AliEMCALShishKebabTrd1Module::ThetaToEta(theta);
933         //        ind   = ieta*fCentersOfCellsPhiDir.GetSize() + iphi;
934         ind   = iphi*fCentersOfCellsEtaDir.GetSize() + ieta;
935         fEtaCentersOfCells.AddAt(eta, ind);
936       }
937       //printf(" ieta %i : xr + trd1->GetRadius() %f : zr %f : eta %f \n", ieta, xr + trd1->GetRadius(), zr, eta);
938     }
939   }
940   for(Int_t i=0; i<fCentersOfCellsEtaDir.GetSize(); i++) {
941     AliDebug(2,Form(" ind %2.2i : z %8.3f : x %8.3f", i+1,
942                     fCentersOfCellsEtaDir.At(i),fCentersOfCellsXDir.At(i)));
943   }
944
945 }
946
947
948 //________________________________________________________________________________________________
949 AliEMCALShishKebabTrd1Module* AliEMCALGeometry::GetShishKebabModule(Int_t neta) const
950 {
951   //This method was too long to be
952   //included in the header file - the
953   //rule checker complained about it's
954   //length, so we move it here.  It returns the
955   //shishkebabmodule at a given eta index point.
956
957   static AliEMCALShishKebabTrd1Module* trd1=0;
958   if(fShishKebabTrd1Modules && neta>=0 && neta<fShishKebabTrd1Modules->GetSize()) {
959     trd1 = (AliEMCALShishKebabTrd1Module*)fShishKebabTrd1Modules->At(neta);
960   } else trd1 = 0;
961   return trd1;
962 }
963
964 //___________________________________________________________________
965 void AliEMCALGeometry::PrintGeometryGeoUtils()
966 {
967   //Print information from geometry
968   fEMCGeometry->PrintGeometry();
969
970   printf(" fShishKebabTrd1Modules has %i modules : max eta %5.4f \n", 
971          fShishKebabTrd1Modules->GetSize(),fEtaMaxOfTRD1);
972   
973   printf("\n Cells grid in eta directions : size %i\n", fCentersOfCellsEtaDir.GetSize());
974   for(Int_t i=0; i<fCentersOfCellsEtaDir.GetSize(); i++) {
975     printf(" ind %2.2i : z %8.3f : x %8.3f \n", i, 
976            fCentersOfCellsEtaDir.At(i),fCentersOfCellsXDir.At(i));
977     int ind=0; // Nov 21,2006
978     for(Int_t iphi=0; iphi<fCentersOfCellsPhiDir.GetSize(); iphi++) {
979       ind = iphi*fCentersOfCellsEtaDir.GetSize() + i;
980       printf("%6.4f ", fEtaCentersOfCells[ind]);
981       if((iphi+1)%12 == 0) printf("\n");
982     }
983     printf("\n");
984     
985   }
986
987   printf("\n Cells grid in phi directions : size %i\n", fCentersOfCellsPhiDir.GetSize());
988   for(Int_t i=0; i<fCentersOfCellsPhiDir.GetSize(); i++) {
989     double phi=fPhiCentersOfCells.At(i);
990     printf(" ind %2.2i : y %8.3f : phi %7.5f(%6.2f) \n", i, fCentersOfCellsPhiDir.At(i), 
991            phi, phi*TMath::RadToDeg());
992   }
993
994 }
995
996 //____________________________________________________________________________
997 Bool_t  AliEMCALGeometry::Impact(const TParticle * particle) const 
998 {
999   // Tells if a particle enters EMCAL
1000   Bool_t in=kFALSE;
1001   Int_t absID=0;
1002   TVector3 vtx(particle->Vx(),particle->Vy(),particle->Vz());
1003   TVector3 vimpact(0,0,0);
1004   ImpactOnEmcal(vtx,particle->Theta(),particle->Phi(),absID,vimpact);
1005   if(absID>=0) 
1006     in=kTRUE;
1007   return in;
1008 }
1009 //____________________________________________________________________________
1010 void AliEMCALGeometry::ImpactOnEmcal(TVector3 vtx, Double_t theta, Double_t phi, 
1011                                      Int_t & absId, TVector3 & vimpact) const
1012 {
1013   // calculates the impact coordinates on EMCAL (centre of a tower/not on EMCAL surface) 
1014   // of a neutral particle  
1015   // emitted in the vertex vtx[3] with direction theta and phi in the ALICE global coordinate system
1016
1017   TVector3 p(TMath::Sin(theta)*TMath::Cos(phi),TMath::Sin(theta)*TMath::Sin(phi),TMath::Cos(theta)) ;
1018
1019   vimpact.SetXYZ(0,0,0);
1020   absId=-1;
1021   if(phi==0 || theta==0) return;
1022
1023   TVector3 direction;
1024   Double_t factor = (fIPDistance-vtx[1])/p[1];
1025   direction = vtx + factor*p;
1026
1027   //from particle direction -> tower hitted
1028   GetAbsCellIdFromEtaPhi(direction.Eta(),direction.Phi(),absId);
1029   
1030   //tower absID hitted -> tower/module plane (evaluated at the center of the tower)
1031   Int_t nSupMod=-1, nModule=-1, nIphi=-1, nIeta=-1;
1032   Double_t loc[3],loc2[3],loc3[3];
1033   Double_t glob[3]={},glob2[3]={},glob3[3]={};
1034   
1035   if(!RelPosCellInSModule(absId,loc)) return;
1036   
1037   //loc is cell center of tower
1038   GetCellIndex(absId, nSupMod, nModule, nIphi, nIeta);
1039
1040   //look at 2 neighbours-s cell using nIphi={0,1} and nIeta={0,1}
1041   Int_t nIphi2=-1,nIeta2=-1,absId2=-1,absId3=-1;
1042   if(nIeta==0) nIeta2=1;
1043   else nIeta2=0;
1044   absId2=GetAbsCellId(nSupMod,nModule,nIphi,nIeta2);  
1045   if(nIphi==0) nIphi2=1;
1046   else nIphi2=0;
1047   absId3=GetAbsCellId(nSupMod,nModule,nIphi2,nIeta);
1048
1049   //2nd point on emcal cell plane
1050   if(!RelPosCellInSModule(absId2,loc2)) return;
1051     
1052   //3rd point on emcal cell plane
1053   if(!RelPosCellInSModule(absId3,loc3)) return;
1054     
1055   // Get Matrix
1056   const TGeoHMatrix* m = GetMatrixForSuperModule(nSupMod);
1057   if(m) {
1058     m->LocalToMaster(loc, glob);
1059     m->LocalToMaster(loc2, glob2);
1060     m->LocalToMaster(loc3, glob3);
1061   } else {
1062     AliFatal("Geo matrixes are not loaded \n") ;
1063   }
1064
1065   //Equation of Plane from glob,glob2,glob3 (Ax+By+Cz+D=0)
1066   Double_t a = glob[1]*(glob2[2]-glob3[2]) + glob2[1]*(glob3[2]-glob[2]) + glob3[1]*(glob[2]-glob2[2]);
1067   Double_t b = glob[2]*(glob2[0]-glob3[0]) + glob2[2]*(glob3[0]-glob[0]) + glob3[2]*(glob[0]-glob2[0]);
1068   Double_t c = glob[0]*(glob2[1]-glob3[1]) + glob2[0]*(glob3[1]-glob[1]) + glob3[0]*(glob[1]-glob2[1]);
1069   Double_t d = glob[0]*(glob2[1]*glob3[2]-glob3[1]*glob2[2]) + glob2[0]*(glob3[1]*glob[2]-glob[1]*glob3[2]) + glob3[0]*(glob[1]*glob2[2]-glob2[1]*glob[2]);
1070   d=-d;
1071   
1072   //shift equation of plane from tower/module center to surface along vector (A,B,C) normal to tower/module plane
1073   Double_t dist = fLongModuleSize/2.;
1074   Double_t norm = TMath::Sqrt(a*a+b*b+c*c);
1075   Double_t glob4[3]={};
1076   TVector3 dir(a,b,c);
1077   TVector3 point(glob[0],glob[1],glob[2]); 
1078   if(point.Dot(dir)<0) dist*=-1;
1079   glob4[0]=glob[0]-dist*a/norm;
1080   glob4[1]=glob[1]-dist*b/norm;
1081   glob4[2]=glob[2]-dist*c/norm;
1082   d = glob4[0]*a +  glob4[1]*b +  glob4[2]*c ;
1083   d = -d;
1084
1085   //Line determination (2 points for equation of line : vtx and direction)
1086   //impact between line (particle) and plane (module/tower plane)
1087   Double_t den = a*(vtx(0)-direction(0)) + b*(vtx(1)-direction(1)) + c*(vtx(2)-direction(2));
1088   if(den==0){
1089     printf("ImpactOnEmcal() No solution :\n");
1090     return;
1091   }
1092   
1093   Double_t length = a*vtx(0)+b*vtx(1)+c*vtx(2)+d;
1094   length /=den;
1095   
1096   vimpact.SetXYZ(vtx(0)+length*(direction(0)-vtx(0)),vtx(1)+length*(direction(1)-vtx(1)),vtx(2)+length*(direction(2)-vtx(2)));
1097   
1098   //shift vimpact from tower/module surface to center along vector (A,B,C) normal to tower/module plane
1099   vimpact.SetXYZ(vimpact(0)+dist*a/norm,vimpact(1)+dist*b/norm,vimpact(2)+dist*c/norm);
1100   
1101   return;
1102 }
1103
1104 //_____________________________________________________________________________
1105 Bool_t AliEMCALGeometry::IsInEMCAL(Double_t x, Double_t y, Double_t z) const {
1106   // Checks whether point is inside the EMCal volume, used in AliEMCALv*.cxx
1107   //
1108   // Code uses cylindrical approximation made of inner radius (for speed)
1109   //
1110   // Points behind EMCAl, i.e. R > outer radius, but eta, phi in acceptance 
1111   // are considered to inside
1112
1113   Double_t r=sqrt(x*x+y*y);
1114
1115   if ( r > fEnvelop[0] ) {
1116      Double_t theta;
1117      theta  =    TMath::ATan2(r,z);
1118      Double_t eta;
1119      if(theta == 0) 
1120        eta = 9999;
1121      else 
1122        eta    =   -TMath::Log(TMath::Tan(theta/2.));
1123      if (eta < fArm1EtaMin || eta > fArm1EtaMax)
1124        return 0;
1125  
1126      Double_t phi = TMath::ATan2(y,x) * 180./TMath::Pi();
1127      if (phi < 0) phi += 360;  // phi should go from 0 to 360 in this case
1128      if (phi > fArm1PhiMin && phi < fArm1PhiMax)
1129        return 1;
1130   }
1131   return 0;
1132 }
1133
1134 //________________________________________________________________________________________________
1135 Int_t AliEMCALGeometry::GetAbsTRUNumberFromNumberInSm(const Int_t row, const Int_t col, const Int_t sm) const
1136 { // Nov 6, 2007
1137   // Get TRU absolute number from column, row and Super Module number
1138   Int_t itru = row + col*fEMCGeometry->GetNModulesInTRUPhi() + sm*fEMCGeometry->GetNTRU();
1139   // printf("  GetAbsTRUNumberFromNumberInSm : row %2i col %2i sm %2i -> itru %2i\n", row, col, sm, itru); 
1140   return itru;
1141 }
1142
1143 //________________________________________________________________________________________________
1144 Bool_t AliEMCALGeometry::GetAbsFastORIndexFromTRU(const Int_t iTRU, const Int_t iADC, Int_t& id) const
1145 {
1146         //Trigger mapping method, get  FastOr Index from TRU
1147
1148   if (iTRU > 31 || iTRU < 0 || iADC > 95 || iADC < 0) 
1149         {
1150                 AliError("TRU out of range!");
1151                 return kFALSE;
1152         }
1153         
1154         id  = ( iTRU % 2 ) ? iADC%4 + 4 * (23 - int(iADC/4)) : (3 - iADC%4) + 4 * int(iADC/4);
1155
1156         id += iTRU * 96;
1157         
1158         return kTRUE;
1159 }
1160
1161 //________________________________________________________________________________________________
1162 Bool_t AliEMCALGeometry::GetTRUFromAbsFastORIndex(const Int_t id, Int_t& iTRU, Int_t& iADC) const
1163 {
1164
1165         //Trigger mapping method, get TRU number from FastOr Index
1166
1167         if (id > 3071 || id < 0)
1168         {
1169                 AliError("Id out of range!");
1170                 return kFALSE;
1171         }
1172         
1173         iTRU = id / 96;
1174         
1175         iADC = id % 96;
1176         
1177         iADC = ( iTRU % 2 ) ? iADC%4 + 4 * (23 - int(iADC/4)) : (3 - iADC%4) + 4 * int(iADC/4);
1178         
1179         return kTRUE;
1180 }
1181
1182 //________________________________________________________________________________________________
1183 Bool_t AliEMCALGeometry::GetPositionInTRUFromAbsFastORIndex(const Int_t id, Int_t& iTRU, Int_t& iEta, Int_t& iPhi) const
1184 {
1185         //Trigger mapping method, get position in TRU from FasOr Index
1186         
1187         Int_t iADC=-1;  
1188         if (!GetTRUFromAbsFastORIndex(id, iTRU, iADC)) return kFALSE;
1189         
1190         Int_t x = iADC / 4;
1191         Int_t y = iADC % 4;
1192         
1193         if ( iTRU % 2 ) // C side 
1194         {
1195                 iEta = 23 - x;
1196                 iPhi =      y;
1197         }
1198         else            // A side
1199         {
1200                 iEta =      x;
1201                 iPhi =  3 - y;
1202         }
1203         
1204         return kTRUE;
1205 }
1206
1207 //________________________________________________________________________________________________
1208 Bool_t AliEMCALGeometry::GetPositionInSMFromAbsFastORIndex(const Int_t id, Int_t& iSM, Int_t& iEta, Int_t& iPhi) const
1209 {
1210         //Trigger mapping method, get position in Super Module from FasOr Index
1211
1212         Int_t iTRU=-1;
1213                 
1214         if (!GetPositionInTRUFromAbsFastORIndex(id, iTRU, iEta, iPhi)) return kFALSE;
1215         
1216         if (iTRU % 2) // C side
1217         {
1218                 iSM  = 2 * ( int( int(iTRU / 2) / 3 ) ) + 1;
1219         }
1220         else            // A side
1221         {
1222                 iSM  = 2 * ( int( int(iTRU / 2) / 3 ) );
1223         }
1224
1225         iPhi += 4 * int((iTRU % 6) / 2);
1226         
1227         return kTRUE;
1228 }
1229
1230 //________________________________________________________________________________________________
1231 Bool_t AliEMCALGeometry::GetPositionInEMCALFromAbsFastORIndex(const Int_t id, Int_t& iEta, Int_t& iPhi) const
1232 {
1233   //Trigger mapping method, get position in EMCAL from FastOR index
1234
1235         Int_t iSM=-1;
1236         
1237         if (GetPositionInSMFromAbsFastORIndex(id, iSM, iEta, iPhi))
1238         {
1239                 if (iSM % 2) iEta += 24; 
1240                 
1241                 iPhi += 12 * int(iSM / 2);
1242                 
1243                 return kTRUE;
1244         }
1245         
1246         return kFALSE;
1247 }
1248
1249 //________________________________________________________________________________________________
1250 Bool_t AliEMCALGeometry::GetAbsFastORIndexFromPositionInTRU(const Int_t iTRU, const Int_t iEta, const Int_t iPhi, Int_t& id) const
1251 {
1252         //Trigger mapping method, get Index if FastOr from Position in TRU
1253
1254         if (iTRU < 0 || iTRU > 31 || iEta < 0 || iEta > 23 || iPhi < 0 || iPhi > 3) 
1255         {
1256                 AliError("Out of range!");      
1257                 return kFALSE;
1258         }
1259         
1260         id =  iPhi  + 4 * iEta + iTRU * 96;
1261         
1262         return kTRUE;
1263 }
1264
1265 //________________________________________________________________________________________________
1266 Bool_t AliEMCALGeometry::GetAbsFastORIndexFromPositionInSM(const Int_t  iSM, const Int_t iEta, const Int_t iPhi, Int_t& id) const
1267 {
1268   //Trigger mapping method, from position in SM Index get FastOR index 
1269
1270         if (iSM < 0 || iSM > 11 || iEta < 0 || iEta > 23 || iPhi < 0 || iPhi > 11) 
1271         {
1272                 AliError("Out of range!");
1273                 return kFALSE;
1274         }
1275         
1276         Int_t x = iEta;
1277         Int_t y = iPhi % 4;     
1278         
1279         Int_t iOff = (iSM % 2) ? 1 : 0;
1280         Int_t iTRU = 2 * int(iPhi / 4) + 6 * int(iSM / 2) + iOff;
1281
1282         if (GetAbsFastORIndexFromPositionInTRU(iTRU, x, y, id))
1283         {
1284                 return kTRUE;
1285         }
1286         
1287         return kFALSE;
1288 }
1289
1290 //________________________________________________________________________________________________
1291 Bool_t AliEMCALGeometry::GetAbsFastORIndexFromPositionInEMCAL(const Int_t iEta, const Int_t iPhi, Int_t& id) const
1292 {
1293   //Trigger mapping method, from position in EMCAL Index get FastOR index 
1294
1295         if (iEta < 0 || iEta > 47 || iPhi < 0 || iPhi > 63 ) 
1296         {
1297                 AliError("Out of range!");
1298                 return kFALSE;
1299         }
1300         
1301         if (fFastOR2DMap[iEta][iPhi] == -1) 
1302         {
1303                 AliError("Invalid index!");
1304                 return kFALSE;
1305         }
1306         
1307         id = fFastOR2DMap[iEta][iPhi];
1308         
1309         return kTRUE;
1310 }
1311
1312 //________________________________________________________________________________________________
1313 Bool_t AliEMCALGeometry::GetFastORIndexFromCellIndex(const Int_t id, Int_t& idx) const
1314 {
1315   //Trigger mapping method, from cell index get FastOR index 
1316
1317         Int_t iSupMod, nModule, nIphi, nIeta, iphim, ietam;
1318         
1319         Bool_t isOK = GetCellIndex( id, iSupMod, nModule, nIphi, nIeta );
1320         
1321         GetModulePhiEtaIndexInSModule( iSupMod, nModule, iphim, ietam );
1322         
1323         if (isOK && GetAbsFastORIndexFromPositionInSM(iSupMod, ietam, iphim, idx))
1324         {
1325                 return kTRUE;
1326         }
1327         
1328         return kFALSE;
1329 }
1330
1331 //________________________________________________________________________________________________
1332 Bool_t AliEMCALGeometry::GetCellIndexFromFastORIndex(const Int_t id, Int_t idx[4]) const
1333 {
1334   //Trigger mapping method, from FASTOR index get cell index 
1335
1336   Int_t iSM=-1, iEta=-1, iPhi=-1;
1337         if (GetPositionInSMFromAbsFastORIndex(id, iSM, iEta, iPhi))
1338         {
1339                 Int_t ix = 2 * iEta;
1340                 Int_t iy = 2 * iPhi;
1341                 
1342                 for (Int_t i=0; i<2; i++)
1343                 {
1344                         for (Int_t j=0; j<2; j++)
1345                         {
1346                                 idx[2*i+j] = GetAbsCellIdFromCellIndexes(iSM, iy + i, ix + j);
1347                         }
1348                 }
1349                 
1350                 return kTRUE;
1351         }
1352         
1353         return kFALSE;
1354 }
1355
1356 //________________________________________________________________________________________________
1357 Bool_t AliEMCALGeometry::GetTRUIndexFromSTUIndex(const Int_t id, Int_t& idx) const
1358 {
1359   //Trigger mapping method, from STU index get TRU index 
1360
1361         if (id > 31 || id < 0) 
1362         {
1363                 AliError(Form("TRU index out of range: %d",id));
1364                 return kFALSE;
1365         }
1366         
1367         idx = (id > 15) ? 2 * (31 - id) : 2 * (15 - id) + 1;
1368         
1369         return kTRUE;
1370 }
1371
1372 //________________________________________________________________________________________________
1373 Int_t AliEMCALGeometry::GetTRUIndexFromSTUIndex(const Int_t id) const
1374 {
1375   //Trigger mapping method, from STU index get TRU index 
1376
1377         if (id > 31 || id < 0) 
1378         {
1379                 AliError(Form("TRU index out of range: %d",id));
1380         }
1381         
1382         Int_t idx = (id > 15) ? 2 * (31 - id) : 2 * (15 - id) + 1;
1383         
1384         return idx;
1385 }
1386
1387 //________________________________________________________________________________________________
1388 void AliEMCALGeometry::BuildFastOR2DMap()
1389 {
1390         // Needed by STU
1391         for (Int_t i = 0; i < 32; i++)
1392         {
1393                 for (Int_t j = 0; j < 24; j++)
1394                 {
1395                         for (Int_t k = 0; k < 4; k++)
1396                         {
1397                                 Int_t id;
1398                                 if (GetAbsFastORIndexFromPositionInTRU(i, j, k, id))
1399                                 {
1400                                         Int_t x = j, y = k + 4 * int(i / 2);
1401                                 
1402                                         if (i % 2) x += 24;
1403                                 
1404                                         fFastOR2DMap[x][y] = id;
1405                                 }
1406                         }                       
1407                 }
1408         }
1409 }
1410
1411 //________________________________________________________________________________________________
1412 Bool_t AliEMCALGeometry::GetFastORIndexFromL0Index(const Int_t iTRU, const Int_t id, Int_t idx[], const Int_t size) const
1413 {
1414   //Trigger mapping method, from L0 index get FastOR index 
1415         if (size <= 0 ||size > 4)
1416         {
1417                 AliError("Size not supported!");
1418                 return kFALSE;
1419         }
1420                 
1421         Int_t motif[4] = {0, 1, 4, 5};
1422         
1423         switch (size)
1424         {
1425                 case 1: // Cosmic trigger
1426                         if (!GetAbsFastORIndexFromTRU(iTRU, id, idx[1])) return kFALSE;
1427                         break;
1428                 case 4: // 4 x 4
1429                         for (Int_t k = 0; k < 4; k++)
1430                         {
1431                                 Int_t iADC = motif[k] + 4 * int(id / 3) + (id % 3);
1432                                 
1433                                 if (!GetAbsFastORIndexFromTRU(iTRU, iADC, idx[k])) return kFALSE;
1434                         }
1435                         break;
1436                 default:
1437                         break;
1438         }
1439         
1440         return kTRUE;
1441 }
1442
1443 //____________________________________________________________________________
1444 const TGeoHMatrix * AliEMCALGeometry::GetMatrixForSuperModule(Int_t smod) const {
1445
1446         //Provides shift-rotation matrix for EMCAL
1447         
1448         if(smod < 0 || smod > fEMCGeometry->GetNumberOfSuperModules()) 
1449                 AliFatal(Form("Wrong supermodule index -> %d",smod));
1450                 
1451         //If GeoManager exists, take matrixes from it
1452         
1453         //
1454         //    if(fKey110DEG && ind>=10) {
1455         //    }
1456         //
1457         //    if(!gGeoManager->cd(volpath.Data()))
1458         //      AliFatal(Form("AliEMCALGeometry::GeoManager cannot find path %s!",volpath.Data()));
1459         //
1460         //    TGeoHMatrix* m = gGeoManager->GetCurrentMatrix();
1461   
1462   //Use matrices set externally
1463         if(!gGeoManager || (gGeoManager && fUseExternalMatrices)){
1464     if(fkSModuleMatrix[smod]){
1465       return fkSModuleMatrix[smod] ;
1466     }
1467     else{
1468       AliInfo("Stop:");
1469       printf("\t Can not find EMCAL misalignment matrixes\n") ;
1470       printf("\t Either import TGeoManager from geometry.root or \n");
1471       printf("\t read stored matrixes from AliESD Header:  \n") ;   
1472       printf("\t AliEMCALGeometry::SetMisalMatrixes(header->GetEMCALMisalMatrix()) \n") ;
1473       abort() ;
1474     }  
1475   }//external matrices
1476   
1477         if(gGeoManager){
1478     const Int_t buffersize = 255;
1479                 char path[buffersize] ;
1480                 snprintf(path,buffersize,"/ALIC_1/XEN1_1/SMOD_%d",smod+1) ;
1481                 //TString volpath = "ALIC_1/XEN1_1/SMOD_";
1482             //volpath += smod+1;
1483
1484                 if(fKey110DEG && smod >= 10){
1485                           snprintf(path,buffersize,"/ALIC_1/XEN1_1/SM10_%d",smod-10+1) ;
1486                         //volpath = "ALIC_1/XEN1_1/SM10_";
1487                         //volpath += smod-10+1;
1488                 }
1489                 if (!gGeoManager->cd(path)){
1490                         AliFatal(Form("Geo manager can not find path %s!\n",path));
1491                 }
1492                 return gGeoManager->GetCurrentMatrix();
1493         }
1494
1495         return 0 ;
1496 }
1497
1498 //______________________________________________________________________
1499 void AliEMCALGeometry::GetModulePhiEtaIndexInSModuleFromTRUIndex(Int_t itru, Int_t iphitru, Int_t ietatru, Int_t &iphiSM, Int_t &ietaSM) const 
1500 {
1501   
1502   // This method transforms the (eta,phi) index of module in a 
1503   // TRU matrix into Super Module (eta,phi) index.
1504   
1505   // Calculate in which row and column where the TRU are 
1506   // ordered in the SM
1507
1508   Int_t col = itru/fEMCGeometry->GetNTRUPhi() ; // indexes of TRU in SM
1509   Int_t row = itru - col*fEMCGeometry->GetNTRUPhi();
1510    
1511   iphiSM = fEMCGeometry->GetNModulesInTRUPhi()*row + iphitru  ;
1512   ietaSM = fEMCGeometry->GetNModulesInTRUEta()*col + ietatru  ; 
1513   //printf(" GetModulePhiEtaIndexInSModuleFromTRUIndex : itru %2i iphitru %2i ietatru %2i iphiSM %2i ietaSM %2i \n", 
1514   // itru, iphitru, ietatru, iphiSM, ietaSM);
1515 }
1516
1517 //__________________________________________________________________________________________________________________
1518 void AliEMCALGeometry::RecalculateTowerPosition(Float_t drow, Float_t dcol, const Int_t sm, const Float_t depth,
1519                                                 const Float_t misaligTransShifts[15], const Float_t misaligRotShifts[15], Float_t global[3]) const
1520 { //Transform clusters cell position into global with alternative method, taking into account the depth calculation.
1521   //Input are: the tower indeces, 
1522   //           supermodule, 
1523   //           particle type (photon 0, electron 1, hadron 2 )
1524   //           misalignment shifts to global position in case of need.
1525   // Federico.Ronchetti@cern.ch
1526   
1527     
1528   // To use in a print later
1529   Float_t droworg = drow;
1530   Float_t dcolorg = dcol;
1531   
1532   if(gGeoManager){
1533     //Recover some stuff
1534
1535     const Int_t nSMod = fEMCGeometry->GetNumberOfSuperModules();
1536  
1537     gGeoManager->cd("ALIC_1/XEN1_1");
1538     TGeoNode        *geoXEn1 = gGeoManager->GetCurrentNode();
1539     TGeoNodeMatrix  *geoSM[nSMod];        
1540     TGeoVolume      *geoSMVol[nSMod];     
1541     TGeoShape       *geoSMShape[nSMod];    
1542     TGeoBBox        *geoBox[nSMod];        
1543     TGeoMatrix      *geoSMMatrix[nSMod];       
1544     
1545     for(int iSM = 0; iSM < nSMod; iSM++) {  
1546       geoSM[iSM]       = dynamic_cast<TGeoNodeMatrix *>(geoXEn1->GetDaughter(iSM));
1547       geoSMVol[iSM]    = geoSM[iSM]->GetVolume(); 
1548       geoSMShape[iSM]  = geoSMVol[iSM]->GetShape();
1549       geoBox[iSM]      = dynamic_cast<TGeoBBox *>(geoSMShape[iSM]);
1550       geoSMMatrix[iSM] = geoSM[iSM]->GetMatrix();
1551     }
1552     
1553     if(sm % 2 == 0) {
1554       dcol = 47. - dcol;
1555       drow = 23. - drow;
1556     }
1557     
1558     Int_t istrip = 0;
1559     Float_t z0   = 0;
1560     Float_t zb   = 0;
1561     Float_t z_is = 0;
1562     
1563     Float_t x,y,z; // return variables in terry's RF
1564     
1565     //***********************************************************
1566     //Do not like this: too many hardcoded values, is it not already stored somewhere else?
1567     //                : need more comments in the code 
1568     //***********************************************************
1569     
1570     Float_t dz = 6.0;   // base cell width in eta
1571     Float_t dx = 6.004; // base cell width in phi
1572     
1573     
1574     //Float_t L = 26.04; // active tower length for hadron (lead+scint+paper)
1575     // we use the geant numbers 13.87*2=27.74
1576     Float_t teta1 = 0.;
1577       
1578     //Do some basic checks
1579     if (dcol >= 47.5 || dcol<-0.5) {
1580       AliError(Form("Bad tower coordinate dcol=%f, where dcol >= 47.5 || dcol<-0.5; org: %f", dcol, dcolorg));
1581       return;
1582     }
1583     if (drow >= 23.5 || drow<-0.5) {
1584       AliError(Form("Bad tower coordinate drow=%f, where drow >= 23.5 || drow<-0.5; org: %f", drow, droworg));
1585       return;
1586     }
1587     if (sm >= nSMod || sm < 0) {
1588       AliError(Form("Bad SM number sm=%d, where sm >= %d || sm < 0", nSMod, sm));
1589       return;
1590     }    
1591     
1592     istrip = int ((dcol+0.5)/2);
1593     
1594     // tapering angle
1595     teta1 = TMath::DegToRad() * istrip * 1.5;
1596     
1597     // calculation of module corner along z 
1598     // as a function of strip
1599     
1600     for (int is=0; is<= istrip; is++) {
1601       
1602       teta1 = TMath::DegToRad() * (is*1.5 + 0.75);
1603       if(is==0)
1604         z_is = z_is + 2*dz*TMath::Cos(teta1);
1605       else
1606         z_is = z_is + 2*dz*TMath::Cos(teta1) + 2*dz*TMath::Sin(teta1)*TMath::Tan(teta1-0.75*TMath::DegToRad());
1607       
1608     }
1609     
1610     z0 = dz*(dcol-2*istrip+0.5);
1611     zb = (2*dz-z0-depth*TMath::Tan(teta1));
1612     
1613     z = z_is - zb*TMath::Cos(teta1);
1614     y = depth/TMath::Cos(teta1) + zb*TMath::Sin(teta1);
1615     
1616     x = (drow + 0.5)*dx;
1617     
1618     // moving the origin from terry's RF
1619     // to the GEANT one
1620     
1621     double xx =  y - geoBox[sm]->GetDX();
1622     double yy = -x + geoBox[sm]->GetDY(); 
1623     double zz =  z - geoBox[sm]->GetDZ(); 
1624     const double localIn[3] = {xx, yy, zz};
1625     double dglobal[3];
1626     //geoSMMatrix[sm]->Print();
1627     //printf("TFF Local    (row = %d, col = %d, x = %3.2f,  y = %3.2f, z = %3.2f)\n", iroworg, icolorg, localIn[0], localIn[1], localIn[2]);
1628     geoSMMatrix[sm]->LocalToMaster(localIn, dglobal);
1629     //printf("TFF Global   (row = %2.0f, col = %2.0f, x = %3.2f,  y = %3.2f, z = %3.2f)\n", drow, dcol, dglobal[0], dglobal[1], dglobal[2]);
1630     
1631     //apply global shifts
1632     if(sm == 2 || sm == 3) {//sector 1
1633       global[0] = dglobal[0] + misaligTransShifts[3] + misaligRotShifts[3]*TMath::Sin(TMath::DegToRad()*20) ; 
1634       global[1] = dglobal[1] + misaligTransShifts[4] + misaligRotShifts[4]*TMath::Cos(TMath::DegToRad()*20) ; 
1635       global[2] = dglobal[2] + misaligTransShifts[5];
1636     }
1637     else if(sm == 0 || sm == 1){//sector 0
1638       global[0] = dglobal[0] + misaligTransShifts[0]; 
1639       global[1] = dglobal[1] + misaligTransShifts[1]; 
1640       global[2] = dglobal[2] + misaligTransShifts[2];
1641     }
1642     else {
1643       AliInfo("Careful, correction not implemented yet!");
1644       global[0] = dglobal[0] ;
1645       global[1] = dglobal[1] ;
1646       global[2] = dglobal[2] ;
1647     }
1648     
1649     
1650   }
1651   else{
1652     AliFatal("Geometry boxes information, check that geometry.root is loaded\n");
1653   }
1654   
1655 }