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