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