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