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