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