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