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