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