]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALGeometry.cxx
fixed bug in AliSTARTTrigger
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALGeometry.cxx
1  /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$*/
17
18 //_________________________________________________________________________
19 // Geometry class  for EMCAL : singleton  
20 // EMCAL consists of layers of scintillator and lead
21 // Places the the Barrel Geometry of The EMCAL at Midrapidity
22 // between 80 and 180(or 190) degrees of Phi and
23 // -0.7 to 0.7 in eta 
24 // Number of Modules and Layers may be controlled by 
25 // the name of the instance defined               
26 //*-- Author: Sahal Yacoob (LBL / UCT)
27 //     and  : Yves Schutz (SUBATECH)
28 //     and  : Jennifer Klay (LBL)
29 //     SHASHLYK : Aleksei Pavlinov (WSU)
30 //     SuperModules -> module(or tower) -> cell
31
32 // --- AliRoot header files ---
33 #include <assert.h>
34 #include "Riostream.h"
35
36 #include <TMath.h>
37 #include <TVector3.h>
38 #include <TArrayD.h>
39 #include <TRegexp.h>
40 #include <TObjArray.h>
41 #include <TObjString.h>
42 #include <TGeoManager.h>
43 #include <TGeoNode.h>
44 #include <TGeoMatrix.h>
45 #include <TMatrixD.h>
46 #include <TClonesArray.h>
47
48 // -- ALICE Headers.
49 //#include "AliConst.h"
50
51 // --- EMCAL headers
52 #include "AliEMCALGeometry.h"
53 #include "AliEMCALShishKebabTrd1Module.h"
54 //#include "AliRecPoint.h"
55 #include "AliEMCALRecPoint.h"
56 #include "AliEMCALDigit.h"
57
58 ClassImp(AliEMCALGeometry)
59
60 AliEMCALGeometry *AliEMCALGeometry::fgGeom = 0;
61 Bool_t            AliEMCALGeometry::fgInit = kFALSE;
62 TString name; // contains name of geometry
63
64 char *additionalOpts[]={"nl=",   // number of sampling layers
65                        "pbTh=", // cm, Thickness of the Pb
66                        "scTh="  // cm, Thickness of the Sc
67 };
68 int  nAdditionalOpts = sizeof(additionalOpts) / sizeof(char*);
69
70 //______________________________________________________________________
71 AliEMCALGeometry::~AliEMCALGeometry(void){
72     // dtor
73 }
74 //______________________________________________________________________
75 void AliEMCALGeometry::Init(void){
76   // Initializes the EMCAL parameters
77   // naming convention : GUV_WX_N_ gives the composition of a tower
78   // WX inform about the composition of the EM calorimeter section: 
79   //   thickness in mm of Pb radiator (W) and of scintillator (X), and number of scintillator layers (N)
80   // New geometry: EMCAL_55_25
81   // 24-aug-04 for shish-kebab
82   // SHISH_25 or SHISH_62
83   // 11-oct-05   - correction for pre final design
84   // Feb 06,2006 - decrease the weight of EMCAL
85   fgInit = kFALSE; // Assume failed until proven otherwise.
86   name   = GetName();
87   name.ToUpper();
88   fKey110DEG = 0;
89   if(name.Contains("110DEG")) fKey110DEG = 1; // for GetAbsCellId
90   fShishKebabTrd1Modules = 0;
91   fTrd2AngleY = f2Trd2Dy2 = fEmptySpace = fTubsR = fTubsTurnAngle = 0;
92
93   fNZ             = 114;        // granularity along Z (eta) 
94   fNPhi           = 168;        // granularity in phi (azimuth)
95   fArm1PhiMin     = 60.0;       // degrees, Starting EMCAL Phi position
96   fArm1PhiMax     = 180.0;      // degrees, Ending EMCAL Phi position
97   fArm1EtaMin     = -0.7;       // pseudorapidity, Starting EMCAL Eta position
98   fArm1EtaMax     = +0.7;       // pseudorapidity, Ending EMCAL Eta position
99   fIPDistance     = 454.0;      // cm, Radial distance to inner surface of EMCAL
100   fPhiGapForSM    = 0.;         // cm, only for final TRD1 geometry
101   for(int i=0; i<12; i++) fMatrixOfSM[i] = 0;
102
103   // geometry
104   if(name.Contains("SHISH")){ // Only shahslyk now
105     // 7-sep-05; integration issue
106     fArm1PhiMin     = 80.0;     // 60  -> 80
107     fArm1PhiMax     = 180.0;    // 180 -> 190
108
109     fNumberOfSuperModules = 10; // 12 = 6 * 2 (6 in phi, 2 in Z);
110     fSteelFrontThick = 2.54;    //  9-sep-04
111     fIPDistance      = 460.0;
112     fFrontSteelStrip = fPassiveScintThick = 0.0; // 13-may-05
113     fLateralSteelStrip = 0.025; // before MAY 2005 
114     fPhiModuleSize   = fEtaModuleSize   = 11.4;
115     fPhiTileSize = fEtaTileSize      = 5.52; // (11.4-5.52*2)/2. = 0.18 cm (wall thickness)
116     fNPhi            = 14;
117     fNZ              = 30;
118     fAlFrontThick    = fGap2Active = 0;
119     fNPHIdiv = fNETAdiv = 2;
120
121     fNECLayers       = 62;
122     fECScintThick    = fECPbRadThickness = 0.2;
123     fSampling        = 1.;  // 30-aug-04 - should be calculated
124     if(name.Contains("TWIST")) { // all about EMCAL module
125       fNZ             = 27;  // 16-sep-04
126     } else if(name.Contains("TRD")) {
127       fIPDistance      = 428.0;  //  11-may-05
128       fSteelFrontThick = 0.0;    // 3.17 -> 0.0; 28-mar-05 : no stell plate
129       fNPhi            = 12;
130       fSampling       = 12.327;
131       fPhiModuleSize = fEtaModuleSize = 12.26;
132       fNZ            = 26;     // 11-oct-04
133       fTrd1Angle     = 1.3;    // in degree
134 // 18-nov-04; 1./0.08112=12.327
135 // http://pdsfweb01.nersc.gov/~pavlinov/ALICE/SHISHKEBAB/RES/linearityAndResolutionForTRD1.html
136       if(name.Contains("TRD1")) {       // 30-jan-05
137         // for final design
138         fPhiGapForSM    = 2.;         // cm, only for final TRD1 geometry
139         if(name.Contains("MAY05") || name.Contains("WSUC") || name.Contains("FINAL")){
140           fNumberOfSuperModules = 12; // 20-may-05
141           if(name.Contains("WSUC")) fNumberOfSuperModules = 1; // 27-may-05
142           fNECLayers     = 77;       // (13-may-05 from V.Petrov)
143           fPhiModuleSize = 12.5;     // 20-may-05 - rectangular shape
144           fEtaModuleSize = 11.9;
145           fECScintThick  = fECPbRadThickness = 0.16;// (13-may-05 from V.Petrov)
146           fFrontSteelStrip   = 0.025;// 0.025cm = 0.25mm  (13-may-05 from V.Petrov)
147           fLateralSteelStrip = 0.01; // 0.01cm  = 0.1mm   (13-may-05 from V.Petrov) - was 0.025
148           fPassiveScintThick = 0.8;  // 0.8cm   = 8mm     (13-may-05 from V.Petrov)
149           fNZ                = 24;
150           fTrd1Angle         = 1.5;  // 1.3 or 1.5
151
152           if(name.Contains("FINAL")) { // 9-sep-05
153             fNumberOfSuperModules = 10;
154             if(name.Contains("110DEG")) {
155               fNumberOfSuperModules = 12;// last two modules have size 10 degree in phi (180<phi<190)
156               fArm1PhiMax = 200.0; // for XEN1 and turn angle of super modules
157             }
158             fPhiModuleSize = 12.26 - fPhiGapForSM / Float_t(fNPhi); // first assumption
159             fEtaModuleSize = fPhiModuleSize;
160             if(name.Contains("HUGE")) fNECLayers *= 3; // 28-oct-05 for analysing leakage    
161           }
162         }
163       } else if(name.Contains("TRD2")) {       // 30-jan-05
164         fSteelFrontThick = 0.0;         // 11-mar-05
165         fIPDistance+= fSteelFrontThick; // 1-feb-05 - compensate absence of steel plate
166         fTrd1Angle  = 1.64;             // 1.3->1.64
167         fTrd2AngleY = fTrd1Angle;       //  symmetric case now
168         fEmptySpace    = 0.2; // 2 mm
169         fTubsR         = fIPDistance; // 31-jan-05 - as for Fred case
170
171         fPhiModuleSize  = fTubsR*2.*TMath::Tan(fTrd2AngleY*TMath::DegToRad()/2.);
172         fPhiModuleSize -= fEmptySpace/2.; // 11-mar-05  
173         fEtaModuleSize  = fPhiModuleSize; // 20-may-05 
174         fTubsTurnAngle  = 3.;
175       }
176       fNPHIdiv = fNETAdiv  = 2;   // 13-oct-04 - division again
177       if(name.Contains("3X3")) {   // 23-nov-04
178         fNPHIdiv = fNETAdiv  = 3;
179       } else if(name.Contains("4X4")) {
180         fNPHIdiv = fNETAdiv  = 4;
181       }
182     }
183     fPhiTileSize = fPhiModuleSize/2. - fLateralSteelStrip; // 13-may-05 
184     fEtaTileSize = fEtaModuleSize/2. - fLateralSteelStrip; // 13-may-05 
185
186     if(name.Contains("25")){
187       fNECLayers     = 25;
188       fECScintThick  = fECPbRadThickness = 0.5;
189     }
190     if(name.Contains("WSUC")){ // 18-may-05 - about common structure
191       fShellThickness = 30.; // should be change 
192       fNPhi = fNZ = 4; 
193     }
194
195     CheckAditionalOptions();
196
197     // constant for transition absid <--> indexes
198     fNCellsInTower  = fNPHIdiv*fNETAdiv;
199     fNCellsInSupMod = fNCellsInTower*fNPhi*fNZ;
200     fNCells         = fNCellsInSupMod*fNumberOfSuperModules;
201     if(name.Contains("110DEG")) fNCells -= fNCellsInSupMod;
202
203     fLongModuleSize = fNECLayers*(fECScintThick + fECPbRadThickness);
204     if(name.Contains("MAY05")) fLongModuleSize += (fFrontSteelStrip + fPassiveScintThick);
205
206     // 30-sep-04
207     if(name.Contains("TRD")) {
208       f2Trd1Dx2 = fEtaModuleSize + 2.*fLongModuleSize*TMath::Tan(fTrd1Angle*TMath::DegToRad()/2.);
209       if(name.Contains("TRD2")) {  // 27-jan-05
210         f2Trd2Dy2 = fPhiModuleSize + 2.*fLongModuleSize*TMath::Tan(fTrd2AngleY*TMath::DegToRad()/2.);
211       }
212     }
213   } else Fatal("Init", "%s is an undefined geometry!", name.Data()) ; 
214
215   fNPhiSuperModule = fNumberOfSuperModules/2;
216   if(fNPhiSuperModule<1) fNPhiSuperModule = 1;
217   //There is always one more scintillator than radiator layer because of the first block of aluminium
218   fShellThickness = fAlFrontThick + fGap2Active + fNECLayers*GetECScintThick()+(fNECLayers-1)*GetECPbRadThick();
219   if(name.Contains("SHISH")) {
220     fShellThickness = fSteelFrontThick + fLongModuleSize;
221     if(name.Contains("TWIST")) { // 13-sep-04
222       fShellThickness  = TMath::Sqrt(fLongModuleSize*fLongModuleSize + fPhiModuleSize*fEtaModuleSize);
223       fShellThickness += fSteelFrontThick;
224     } else if(name.Contains("TRD")) { // 1-oct-04
225       fShellThickness  = TMath::Sqrt(fLongModuleSize*fLongModuleSize + f2Trd1Dx2*f2Trd1Dx2);
226       fShellThickness += fSteelFrontThick;
227       // Local coordinates
228       fParSM[0] = GetShellThickness()/2.;        
229       fParSM[1] = GetPhiModuleSize() * GetNPhi()/2.;
230       fParSM[2] = 350./2.;
231     }
232   }
233
234   fZLength        = 2.*ZFromEtaR(fIPDistance+fShellThickness,fArm1EtaMax); // Z coverage
235   fEnvelop[0]     = fIPDistance; // mother volume inner radius
236   fEnvelop[1]     = fIPDistance + fShellThickness; // mother volume outer r.
237   fEnvelop[2]     = 1.00001*fZLength; // add some padding for mother volume. 
238   
239   fgInit = kTRUE; 
240   
241   if (kTRUE) {
242     printf("Init: geometry of EMCAL named %s is as follows:\n", name.Data());
243     printf( "               ECAL      : %d x (%f cm Pb, %f cm Sc) \n", 
244     GetNECLayers(), GetECPbRadThick(), GetECScintThick() ) ; 
245     printf("                fSampling %5.2f \n",  fSampling );
246     if(name.Contains("SHISH")){
247       printf(" fIPDistance       %6.3f cm \n", fIPDistance);
248       if(fSteelFrontThick>0.) 
249       printf(" fSteelFrontThick  %6.3f cm \n", fSteelFrontThick);
250       printf(" fNPhi %i   |  fNZ %i \n", fNPhi, fNZ);
251       printf(" fNCellsInTower %i : fNCellsInSupMod %i : fNCells %i\n",fNCellsInTower, fNCellsInSupMod, fNCells);
252       if(name.Contains("MAY05")){
253         printf(" fFrontSteelStrip         %6.4f cm (thickness of front steel strip)\n", 
254         fFrontSteelStrip);
255         printf(" fLateralSteelStrip       %6.4f cm (thickness of lateral steel strip)\n", 
256         fLateralSteelStrip);
257         printf(" fPassiveScintThick  %6.4f cm (thickness of front passive Sc tile)\n",
258         fPassiveScintThick);
259       }
260       printf(" X:Y module size     %6.3f , %6.3f cm \n", fPhiModuleSize, fEtaModuleSize);
261       printf(" X:Y   tile size     %6.3f , %6.3f cm \n", fPhiTileSize, fEtaTileSize);
262       printf(" #of sampling layers %i(fNECLayers) \n", fNECLayers);
263       printf(" fLongModuleSize     %6.3f cm \n", fLongModuleSize);
264       printf(" #supermodule in phi direction %i \n", fNPhiSuperModule );
265     }
266     if(name.Contains("TRD")) {
267       printf(" fTrd1Angle %7.4f\n", fTrd1Angle);
268       printf(" f2Trd1Dx2  %7.4f\n",  f2Trd1Dx2);
269       if(name.Contains("TRD2")) {
270         printf(" fTrd2AngleY     %7.4f\n", fTrd2AngleY);
271         printf(" f2Trd2Dy2       %7.4f\n", f2Trd2Dy2);
272         printf(" fTubsR          %7.2f cm\n", fTubsR);
273         printf(" fTubsTurnAngle  %7.4f\n", fTubsTurnAngle);
274         printf(" fEmptySpace     %7.4f cm\n", fEmptySpace);
275       } else if(name.Contains("TRD1") && name.Contains("FINAL")){
276         printf("SM dimensions(TRD1) : dx %7.2f dy %7.2f dz %7.2f (SMOD, BOX)\n", 
277         fParSM[0],fParSM[1],fParSM[2]);
278         printf(" fPhiGapForSM  %7.4f cm \n",  fPhiGapForSM);
279         if(name.Contains("110DEG"))printf(" Last two modules have size 10 degree in  phi (180<phi<190)\n");
280       }
281     }
282     printf("Granularity: %d in eta and %d in phi\n", GetNZ(), GetNPhi()) ;
283     printf("Layout: phi = (%7.1f, %7.1f), eta = (%5.2f, %5.2f), IP = %7.2f\n",  
284            GetArm1PhiMin(), GetArm1PhiMax(),GetArm1EtaMin(), GetArm1EtaMax(), GetIPDistance() );
285   }
286   //TRU parameters. These parameters values are not the final ones.
287   fNTRU    = 3 ;
288   fNTRUEta = 3 ;
289   fNTRUPhi = 1 ;
290 }
291
292 //______________________________________________________________________
293
294 void AliEMCALGeometry::CheckAditionalOptions()
295 { // Feb 06,2006
296   fArrayOpts = new TObjArray;
297   Int_t nopt = ParseString(name, *fArrayOpts);
298   if(nopt==1) { // no aditional option(s)
299     fArrayOpts->Delete();
300     delete fArrayOpts;
301     fArrayOpts = 0; 
302     return;
303   }              
304   for(Int_t i=1; i<nopt; i++){
305     TObjString *o = (TObjString*)fArrayOpts->At(i); 
306
307     TString addOpt = o->String();
308     Int_t indj=-1;
309     for(Int_t j=0; j<nAdditionalOpts; j++) {
310       TString opt = additionalOpts[j];
311       if(addOpt.Contains(opt,TString::kIgnoreCase)) {
312           indj = j;
313         break;
314       }
315     }
316     if(indj<0) {
317       printf("<E> option |%s| unavailable : ** look to the file AliEMCALGeometry.h **\n", 
318       addOpt.Data());
319       assert(0);
320     } else {
321       printf("<I> option |%s| is valid : number %i : |%s|\n", 
322              addOpt.Data(), indj, additionalOpts[indj]);
323       if       (addOpt.Contains("NL=",TString::kIgnoreCase))   {// number of sampling layers
324         sscanf(addOpt.Data(),"NL=%i", &fNECLayers);
325         printf(" fNECLayers %i (new) \n", fNECLayers);
326       } else if(addOpt.Contains("PBTH=",TString::kIgnoreCase)) {//Thickness of the Pb
327         sscanf(addOpt.Data(),"PBTH=%f", &fECPbRadThickness);
328       } else if(addOpt.Contains("SCTH=",TString::kIgnoreCase)) {//Thickness of the Sc
329         sscanf(addOpt.Data(),"SCTH=%f", &fECScintThick);
330       }
331     }
332   }
333 }
334
335 //____________________________________________________________________________
336 TClonesArray *  AliEMCALGeometry::FillTRU(const TClonesArray * digits) {
337
338
339   //Orders digits ampitudes list in fNTRU TRUs (384 cells) per supermodule. 
340   //Each TRU is a TMatrixD, and they are kept in TClonesArrays. The number of 
341   //TRU in phi is fNTRUPhi, and the number of TRU in eta is fNTRUEta. For the 
342   //moment the TRU of the 2 smaller supermodules are considered to be equal 
343   //to the rest.  
344
345   //Check data members
346
347   if(fNTRUEta*fNTRUPhi != fNTRU)
348     Error("FillTRU"," Wrong number of TRUS per Eta or Phi");
349
350   //Initilize variables
351   //List of TRU matrices initialized to 0.
352   Int_t nCellsPhi = fNPhi*2/fNTRUPhi;
353   Int_t nCellsEta = fNZ*2/fNTRUEta;
354   TClonesArray * matrix = new TClonesArray("TMatrixD",1000);
355  
356   for(Int_t k = 0; k < fNTRU*fNumberOfSuperModules; k++){
357     TMatrixD  * trus = new TMatrixD(nCellsPhi,nCellsEta) ;
358     for(Int_t i = 0; i < nCellsPhi; i++)
359       for(Int_t j = 0; j < nCellsEta; j++)
360         (*trus)(i,j) = 0.0;
361
362     new((*matrix)[k]) TMatrixD(*trus) ;
363   }
364
365   AliEMCALDigit * dig ;
366   
367   //Declare variables
368   Int_t id      = -1; 
369   Float_t amp   = -1;
370   Int_t iSupMod = -1;
371   Int_t nTower  = -1;
372   Int_t nIphi   = -1;
373   Int_t nIeta   = -1;
374   Int_t iphi    = -1;
375   Int_t ieta    = -1;
376   
377   //Digits loop to fill TRU matrices with amplitudes.
378
379   for(Int_t idig = 0 ; idig < digits->GetEntriesFast() ; idig++){
380     
381     dig = dynamic_cast<AliEMCALDigit *>(digits->At(idig)) ;
382     amp = dig->GetAmp() ; //Energy of the digit (arbitrary units)
383     id  = dig->GetId() ;  //Id label of the cell
384     //cout<<"idig "<<idig<<" Amp "<<amp<<" Id "<<id<<endl;
385    
386     //Get eta and phi cell position in supermodule
387     Bool_t bCell = GetCellIndex(id, iSupMod, nTower, nIphi, nIeta) ;
388     if(!bCell)
389       Error("FillTRU","Wrong cell id number") ;
390     
391     GetCellPhiEtaIndexInSModule(iSupMod,nTower,nIphi, nIeta,iphi,ieta);
392
393     //Check to which TRU in the supermodule belongs the cell. 
394     //Supermodules are divided in a TRU matrix of dimension 
395     //(fNTRUPhi,fNTRUEta).
396     //Each TRU is a cell matrix of dimension (nCellsPhi,nCellsEta)
397
398     //First calculate the row and column in the supermodule 
399     //of the TRU to which the cell belongs.
400
401     Int_t col   = (ieta-1)/nCellsEta+1; 
402     Int_t row   = (iphi-1)/nCellsPhi+1; 
403     Int_t itru  = col*row + (iSupMod-1)*fNTRU - 1; //Label number of the TRU
404 //     Info("FillTRU","SM %d, cell: phi %d, eta %d",iSupMod,iphi,ieta);
405 //     Info("FillTRU","SM TRU: SMrow %d, SMcol %d, SMtru %d,",row,col,itru); 
406     
407  
408     //Fill TRU matrix with cell values
409     
410     TMatrixD * trus = dynamic_cast<TMatrixD *>(matrix->At(itru)) ;
411     
412     //Calculate row and column of the cell inside the TRU with number itru
413
414     Int_t irow = (iphi-1) - (row-1) *  nCellsPhi;       
415     Int_t icol = (ieta-1) - (col-1) *  nCellsEta;
416     
417     (*trus)(irow,icol) = amp ;
418
419
420     //     Info("FillTRU","TRU: row %d, col %d",irow,icol);
421
422   }
423   return matrix;
424 }
425
426
427 //______________________________________________________________________
428 AliEMCALGeometry *  AliEMCALGeometry::GetInstance(){ 
429   // Returns the pointer of the unique instance
430   
431   AliEMCALGeometry * rv = static_cast<AliEMCALGeometry *>( fgGeom );
432   return rv; 
433 }
434
435 //______________________________________________________________________
436 AliEMCALGeometry* AliEMCALGeometry::GetInstance(const Text_t* name,
437                                                 const Text_t* title){
438     // Returns the pointer of the unique instance
439
440     AliEMCALGeometry * rv = 0; 
441     if ( fgGeom == 0 ) {
442         if ( strcmp(name,"") == 0 ) rv = 0;
443         else {    
444             fgGeom = new AliEMCALGeometry(name, title);
445             if ( fgInit ) rv = (AliEMCALGeometry * ) fgGeom;
446             else {
447                 rv = 0; 
448                 delete fgGeom; 
449                 fgGeom = 0; 
450             } // end if fgInit
451         } // end if strcmp(name,"")
452     }else{
453         if ( strcmp(fgGeom->GetName(), name) != 0 ) {
454           printf("\ncurrent geometry is ") ;  
455           printf(fgGeom->GetName());
456           printf("\n                      you cannot call     "); 
457           printf(name);  
458         }else{
459           rv = (AliEMCALGeometry *) fgGeom; 
460         } // end 
461     }  // end if fgGeom
462     return rv; 
463 }
464
465 // These methods are obsolete but use in AliEMCALRecPoint - keep it now
466 //______________________________________________________________________
467 Int_t AliEMCALGeometry::TowerIndex(Int_t ieta,Int_t iphi) const {
468   // Returns the tower index number from the based on the Z and Phi
469   // index numbers.
470   // Inputs:
471   //   Int_t ieta    // index along z axis [1-fNZ]
472   //   Int_t iphi  // index along phi axis [1-fNPhi]
473   // Outputs:
474   //   none.
475   // Returned
476   //   Int_t index // Tower index number 
477   
478   if ( (ieta <= 0 || ieta>GetNEta()) || 
479        (iphi <= 0 || iphi>GetNPhi())) {
480     Error("TowerIndex", "Unexpected parameters eta = %d phi = %d!", ieta, iphi) ; 
481     return -1;
482   }
483   return ( (iphi - 1)*GetNEta() + ieta ); 
484 }
485
486 //______________________________________________________________________
487 void AliEMCALGeometry::TowerIndexes(Int_t index,Int_t &ieta,Int_t &iphi) const {
488   // Inputs:
489   //   Int_t index // Tower index number [1-fNZ*fNPhi]
490   // Outputs:
491   //   Int_t ieta    // index allong z axis [1-fNZ]
492   //   Int_t iphi  // index allong phi axis [1-fNPhi]
493   // Returned
494   //   none.
495
496   Int_t nindex = 0;
497
498   if ( IsInECA(index) ) { // ECAL index
499     nindex = index ;
500   }
501   else {
502     Error("TowerIndexes", "Unexpected Id number!") ;
503     ieta = -1;
504     iphi = -1;
505     return;
506   }   
507
508   if (nindex%GetNZ()) 
509     iphi = nindex / GetNZ() + 1 ; 
510   else 
511     iphi = nindex / GetNZ() ; 
512   ieta = nindex - (iphi - 1) * GetNZ() ; 
513
514   if (gDebug==2)
515     printf("TowerIndexes: index=%d,%d, ieta=%d, iphi = %d", index, nindex,ieta, iphi) ; 
516   return;
517   
518 }
519
520 //______________________________________________________________________
521 void AliEMCALGeometry::EtaPhiFromIndex(Int_t index,Float_t &eta,Float_t &phi) const {
522     // given the tower index number it returns the based on the eta and phi
523     // of the tower.
524     // Inputs:
525     //   Int_t index // Tower index number [1-fNZ*fNPhi]
526     // Outputs:
527     //   Float_t eta  // eta of center of tower in pseudorapidity
528     //   Float_t phi  // phi of center of tower in degrees
529     // Returned
530     //   none.
531     Int_t ieta, iphi;
532     Float_t deta, dphi ;
533
534     TowerIndexes(index,ieta,iphi);
535     
536     if (gDebug == 2) 
537       printf("EtaPhiFromIndex: index = %d, ieta = %d, iphi = %d", index, ieta, iphi) ;
538
539     deta = (GetArm1EtaMax()-GetArm1EtaMin())/(static_cast<Float_t>(GetNEta()));
540     eta  = GetArm1EtaMin() + ((static_cast<Float_t>(ieta) - 0.5 ))*deta;
541
542     dphi = (GetArm1PhiMax() - GetArm1PhiMin())/(static_cast<Float_t>(GetNPhi()));  // in degrees.
543     phi  = GetArm1PhiMin() + dphi*(static_cast<Float_t>(iphi) - 0.5);//iphi range [1-fNphi].
544 }
545
546 //______________________________________________________________________
547 Int_t AliEMCALGeometry::TowerIndexFromEtaPhi(Float_t eta,Float_t phi) const {
548     // returns the tower index number based on the eta and phi of the tower.
549     // Inputs:
550     //   Float_t eta  // eta of center of tower in pseudorapidity
551     //   Float_t phi  // phi of center of tower in degrees
552     // Outputs:
553     //   none.
554     // Returned
555     //   Int_t index // Tower index number [1-fNZ*fNPhi]
556
557     Int_t ieta,iphi;
558
559     ieta = static_cast<Int_t> ( 1 + (static_cast<Float_t>(GetNEta()) * (eta - GetArm1EtaMin()) / (GetArm1EtaMax() - GetArm1EtaMin())) ) ;
560
561     if( ieta <= 0 || ieta > GetNEta() ) { 
562       Error("TowerIndexFromEtaPhi", "Unexpected (eta, phi) = (%f, %f) value, outside of EMCAL!", eta, phi) ; 
563       return -1 ; 
564     }
565
566     iphi = static_cast<Int_t> ( 1 + (static_cast<Float_t>(GetNPhi()) * (phi - GetArm1PhiMin()) / (GetArm1PhiMax() - GetArm1PhiMin())) ) ;
567
568     if( iphi <= 0 || iphi > GetNPhi() ) { 
569       Error("TowerIndexFromEtaPhi", "Unexpected (eta, phi) = (%f, %f) value, outside of EMCAL!", eta, phi) ; 
570       return -1 ; 
571     }
572
573     return TowerIndex(ieta,iphi);
574 }
575
576 //______________________________________________________________________
577 Bool_t AliEMCALGeometry::AbsToRelNumbering(Int_t AbsId, Int_t *relid) const {
578     // Converts the absolute numbering into the following array/
579     //  relid[0] = Row number inside EMCAL
580     //  relid[1] = Column number inside EMCAL
581     // Input:
582     //   Int_t AbsId // Tower index number [1-2*fNZ*fNPhi]
583     // Outputs:
584     //   Int_t *relid // array of 2. Described above.
585     Bool_t rv  = kTRUE ;
586     Int_t ieta=0,iphi=0,index=AbsId;
587
588     TowerIndexes(index,ieta,iphi);
589     relid[0] = ieta;
590     relid[1] = iphi;
591
592     return rv;
593 }
594
595 //______________________________________________________________________
596 void AliEMCALGeometry::PosInAlice(const Int_t *relid, Float_t &theta, Float_t &phi) const 
597 {
598   // Converts the relative numbering into the local EMCAL-module (x, z)
599   // coordinates
600   Int_t ieta = relid[0]; // offset along x axis
601   Int_t iphi = relid[1]; // offset along z axis
602   Int_t index;
603   Float_t eta;
604   
605   index = TowerIndex(ieta,iphi);
606   EtaPhiFromIndex(index,eta,phi);
607   //theta = 180.*(2.0*TMath::ATan(TMath::Exp(-eta)))/TMath::Pi();
608   theta = 2.0*TMath::ATan(TMath::Exp(-eta));
609
610   // correct for distance to IP
611   Float_t d = GetIP2ECASection() - GetIPDistance() ;  
612
613   Float_t correction = 1 + d/GetIPDistance() ; 
614   Float_t tantheta = TMath::Tan(theta) * correction ; 
615   theta = TMath::ATan(tantheta) * TMath::RadToDeg() ; 
616   if (theta < 0 ) 
617     theta += 180. ; 
618   
619   return;
620 }
621
622 //______________________________________________________________________
623 void AliEMCALGeometry::PosInAlice(Int_t absid, Float_t &theta, Float_t &phi) const 
624 {
625   // Converts the relative numbering into the local EMCAL-module (x, z)
626   // coordinates
627   Int_t relid[2] ; 
628   AbsToRelNumbering(absid, relid) ;
629   Int_t ieta = relid[0]; // offset along x axis
630   Int_t iphi = relid[1]; // offset along z axis
631   Int_t index;
632   Float_t eta;
633   
634   index = TowerIndex(ieta,iphi);
635   EtaPhiFromIndex(index,eta,phi);
636   theta = 2.0*TMath::ATan(TMath::Exp(-eta)) ;
637   
638   // correct for distance to IP
639   Float_t d = 0. ; 
640   if (IsInECA(absid))
641     d = GetIP2ECASection() - GetIPDistance() ; 
642   else {
643     Error("PosInAlice", "Unexpected id # %d!", absid) ; 
644     return;
645   }
646
647   Float_t correction = 1 + d/GetIPDistance() ; 
648   Float_t tantheta = TMath::Tan(theta) * correction ; 
649   theta = TMath::ATan(tantheta) * TMath::RadToDeg() ; 
650   if (theta < 0 ) 
651     theta += 180. ; 
652   
653   return;
654 }
655
656 //______________________________________________________________________
657 void AliEMCALGeometry::XYZFromIndex(const Int_t *relid,Float_t &x,Float_t &y, Float_t &z) const {
658     // given the tower relative number it returns the X, Y and Z
659     // of the tower.
660     
661     // Outputs:
662     //   Float_t x  // x of center of tower in cm
663     //   Float_t y  // y of center of tower in cm
664     //   Float_t z  // z of centre of tower in cm
665     // Returned
666     //   none.
667     
668     Float_t eta,theta, phi,cylradius=0. ;
669     
670     Int_t ieta = relid[0]; // offset along x axis
671     Int_t iphi = relid[1]; // offset along z axis.
672     Int_t index;
673     
674     index = TowerIndex(ieta,iphi);
675     EtaPhiFromIndex(index,eta,phi);
676     theta = 180.*(2.0*TMath::ATan(TMath::Exp(-eta)))/TMath::Pi();
677     
678     cylradius = GetIP2ECASection() ;  
679
680     Double_t  kDeg2Rad = TMath::DegToRad() ; 
681     x =  cylradius * TMath::Cos(phi * kDeg2Rad ) ;
682     y =  cylradius * TMath::Sin(phi * kDeg2Rad ) ; 
683     z =  cylradius / TMath::Tan(theta * kDeg2Rad ) ; 
684  
685  return;
686
687
688 //______________________________________________________________________
689 void AliEMCALGeometry::XYZFromIndex(Int_t absid,  TVector3 &v) const {
690     // given the tower relative number it returns the X, Y and Z
691     // of the tower.
692     
693     // Outputs:
694     //   Float_t x  // x of center of tower in cm
695     //   Float_t y  // y of center of tower in cm
696     //   Float_t z  // z of centre of tower in cm
697     // Returned
698     //   none.
699     
700     Float_t theta, phi,cylradius=0. ;
701         
702     PosInAlice(absid, theta, phi) ; 
703     
704     if ( IsInECA(absid) ) 
705       cylradius = GetIP2ECASection() ;
706     else {
707       Error("XYZFromIndex", "Unexpected Tower section") ;
708       return;
709     }
710
711     Double_t  kDeg2Rad = TMath::DegToRad() ; 
712     v.SetX(cylradius * TMath::Cos(phi * kDeg2Rad ) );
713     v.SetY(cylradius * TMath::Sin(phi * kDeg2Rad ) ); 
714     v.SetZ(cylradius / TMath::Tan(theta * kDeg2Rad ) ) ; 
715  
716  return;
717
718
719 Bool_t AliEMCALGeometry::IsInEMCAL(Double_t x, Double_t y, Double_t z) const {
720   // Checks whether point is inside the EMCal volume
721   //
722   // Code uses cylindrical approximation made of inner radius (for speed)
723   //
724   // Points behind EMCAl, i.e. R > outer radius, but eta, phi in acceptance 
725   // are considered to inside
726
727   Double_t r=sqrt(x*x+y*y);
728
729   if ( r > fEnvelop[0] ) {
730      Double_t theta;
731      theta  =    TMath::ATan2(r,z);
732      Double_t eta;
733      if(theta == 0) 
734        eta = 9999;
735      else 
736        eta    =   -TMath::Log(TMath::Tan(theta/2.));
737      if (eta < fArm1EtaMin || eta > fArm1EtaMax)
738        return 0;
739  
740      Double_t phi = TMath::ATan2(y,x) * 180./TMath::Pi();
741      if (phi > fArm1PhiMin && phi < fArm1PhiMax)
742        return 1;
743   }
744   return 0;
745 }
746 // ==
747
748 //
749 // == Shish-kebab cases ==
750 //
751 Int_t AliEMCALGeometry::GetAbsCellId(Int_t nSupMod, Int_t nTower, Int_t nIphi, Int_t nIeta) const
752 { // 27-aug-04; 
753   // corr. 21-sep-04; 
754   //       13-oct-05; 110 degree case
755   // 1 <= nSupMod <= fNumberOfSuperModules
756   // 1 <= nTower  <= fNPHI * fNZ ( fNPHI * fNZ/2 for fKey110DEG=1)
757   // 1 <= nIphi   <= fNPHIdiv
758   // 1 <= nIeta   <= fNETAdiv
759   // 1 <= absid   <= fNCells
760   static Int_t id=0; // have to change from 1 to fNCells
761   if(fKey110DEG == 1 && nSupMod > 10) { // 110 degree case; last two supermodules
762     id  = fNCellsInSupMod*10 + (fNCellsInSupMod/2)*(nSupMod-11);
763   } else {
764     id  = fNCellsInSupMod*(nSupMod-1);
765   }
766   id += fNCellsInTower *(nTower-1);
767   id += fNPHIdiv *(nIphi-1);
768   id += nIeta;
769   if(id<=0 || id > fNCells) {
770 //     printf(" wrong numerations !!\n");
771 //     printf("    id      %6i(will be force to -1)\n", id);
772 //     printf("    fNCells %6i\n", fNCells);
773 //     printf("    nSupMod %6i\n", nSupMod);
774 //     printf("    nTower  %6i\n", nTower);
775 //     printf("    nIphi   %6i\n", nIphi);
776 //     printf("    nIeta   %6i\n", nIeta);
777     id = -TMath::Abs(id);
778   }
779   return id;
780 }
781
782 Bool_t  AliEMCALGeometry::CheckAbsCellId(Int_t ind) const
783 { // 17-niv-04 - analog of IsInECA
784    if(name.Contains("TRD")) {
785      if(ind<=0 || ind > fNCells) return kFALSE;
786      else                        return kTRUE;
787    } else return IsInECA(ind);
788 }
789
790 Bool_t AliEMCALGeometry::GetCellIndex(Int_t absId,Int_t &nSupMod,Int_t &nTower,Int_t &nIphi,Int_t &nIeta) const
791 { // 21-sep-04
792   // 19-oct-05;
793   static Int_t tmp=0, sm10=0;
794   if(absId<=0 || absId>fNCells) {
795 //     Info("GetCellIndex"," wrong abs Id %i !! \n", absId); 
796     return kFALSE;
797   }
798   sm10 = fNCellsInSupMod*10;
799   if(fKey110DEG == 1 && absId > sm10) { // 110 degree case; last two supermodules  
800     nSupMod = (absId-1-sm10) / (fNCellsInSupMod/2) + 11;
801     tmp     = (absId-1-sm10) % (fNCellsInSupMod/2);
802   } else {
803     nSupMod = (absId-1) / fNCellsInSupMod + 1;
804     tmp     = (absId-1) % fNCellsInSupMod;
805   }
806
807   nTower  = tmp / fNCellsInTower + 1;
808   tmp     = tmp % fNCellsInTower;
809   nIphi   = tmp / fNPHIdiv + 1;
810   nIeta   = tmp % fNPHIdiv + 1;
811
812   return kTRUE;
813 }
814
815 void AliEMCALGeometry::GetTowerPhiEtaIndexInSModule(Int_t nSupMod, Int_t nTower,  int &iphit, int &ietat) const
816 { // added nSupMod; have to check  - 19-oct-05 ! 
817   static Int_t nphi;
818
819   if(fKey110DEG == 1 && nSupMod>=11) nphi = fNPhi/2;
820   else                               nphi = fNPhi;
821
822   ietat = (nTower-1)/nphi + 1; // have to change from 1 to fNZ
823   iphit = (nTower-1)%nphi + 1; // have to change from 1 to fNPhi
824 }
825
826 void AliEMCALGeometry::GetCellPhiEtaIndexInSModule(Int_t nSupMod, Int_t nTower, Int_t nIphi, Int_t nIeta, 
827 int &iphi, int &ieta) const
828 { // added nSupMod; Nov 25, 05
829   static Int_t iphit, ietat;
830
831   GetTowerPhiEtaIndexInSModule(nSupMod,nTower, iphit, ietat); 
832   // have to change from 1 to fNZ*fNETAdiv
833   ieta  = (ietat-1)*fNETAdiv + (3-nIeta); // x(module) = -z(SM) 
834   // iphi - have to change from 1 to fNPhi*fNPHIdiv
835   iphi  = (iphit-1)*fNPHIdiv + nIphi;     // y(module) =  y(SM) 
836 }
837
838 Int_t  AliEMCALGeometry::GetSuperModuleNumber(Int_t absId)  const
839 {
840   static Int_t nSupMod, nTower, nIphi, nIeta;
841   GetCellIndex(absId, nSupMod, nTower, nIphi, nIeta);
842   return nSupMod;
843
844
845 // Methods for AliEMCALRecPoint - Feb 19, 2006
846 Bool_t AliEMCALGeometry::RelPosCellInSModule(Int_t absId, Double_t &xr, Double_t &yr, Double_t &zr)
847 {
848   static Int_t nSupMod, nTower, nIphi, nIeta, iphi, ieta;
849   if(!CheckAbsCellId(absId)) return kFALSE;
850
851   GetCellIndex(absId, nSupMod, nTower, nIphi, nIeta);
852   GetCellPhiEtaIndexInSModule(nSupMod,nTower,nIphi,nIeta, iphi, ieta); 
853  
854   xr = fXCentersOfCells->At(ieta-1);
855   zr = fEtaCentersOfCells->At(ieta-1);
856
857   yr = fPhiCentersOfCells->At(iphi-1);
858
859   //  cout<<" absId "<<absId<<" iphi "<<iphi<<"ieta"<<ieta;
860   // cout<< " xr " << xr << " yr " << yr << " zr " << zr <<endl;
861   return kTRUE;
862 }
863
864 void AliEMCALGeometry::CreateListOfTrd1Modules()
865 {
866   cout<< endl<< " AliEMCALGeometry::CreateListOfTrd1Modules() started " << endl;
867   AliEMCALShishKebabTrd1Module *mod=0, *mTmp=0; // current module
868   if(fShishKebabTrd1Modules == 0) {
869     fShishKebabTrd1Modules = new TList;
870     for(int iz=0; iz< GetNZ(); iz++) { 
871       if(iz==0) { 
872         mod  = new AliEMCALShishKebabTrd1Module(TMath::Pi()/2.,this);
873       } else {
874         mTmp  = new AliEMCALShishKebabTrd1Module(*mod);
875         mod   = mTmp;
876       }
877       fShishKebabTrd1Modules->Add(mod);
878     }
879   } else {
880     cout<<" Already exits : ";
881   }
882   cout<<" fShishKebabTrd1Modules "<< fShishKebabTrd1Modules << " has " 
883   << fShishKebabTrd1Modules->GetSize() << " modules" <<endl << endl;
884   // Feb 20,2006;
885   // define grid for cells in eta(z) and x directions in local coordinates system of SM
886   fEtaCentersOfCells = new TArrayD(fNZ *fNETAdiv);
887   fXCentersOfCells = new TArrayD(fNZ *fNETAdiv);
888   printf(" Cells grid in eta directions : size %i\n", fEtaCentersOfCells->GetSize());
889   Int_t iphi=0, ieta=0, nTower=0;
890   Double_t xr, zr;
891   for(Int_t it=0; it<fNZ; it++) { // array index
892     AliEMCALShishKebabTrd1Module *trd1 = GetShishKebabModule(it);
893     nTower = fNPhi*it + 1;
894     for(Int_t ic=0; ic<fNETAdiv; ic++) { // array index
895       trd1->GetCenterOfCellInLocalCoordinateofSM(ic+1, xr, zr);
896       GetCellPhiEtaIndexInSModule(1, nTower, 1, ic+1, iphi, ieta); // don't depend from phi
897       fXCentersOfCells->AddAt(float(xr) - fParSM[0],ieta-1);
898       fEtaCentersOfCells->AddAt(float(zr) - fParSM[2],ieta-1);
899     }
900   }
901   for(Int_t i=0; i<fEtaCentersOfCells->GetSize(); i++) {
902     printf(" ind %2.2i : z %8.3f : x %8.3f", i+1, fEtaCentersOfCells->At(i),fXCentersOfCells->At(i));
903     if(i%2 != 0) printf("\n"); 
904   }
905   printf("\n"); 
906  // define grid for cells in phi(y) direction in local coordinates system of SM
907   fPhiCentersOfCells = new TArrayD(fNPhi*fNPHIdiv);
908   printf(" Cells grid in phi directions : size %i\n", fPhiCentersOfCells->GetSize());
909   Int_t ind=0;
910   for(Int_t it=0; it<fNPhi; it++) { // array index
911     Float_t ytLeftCenterModule = -fParSM[1] + fPhiModuleSize*(2*it+1)/2;         // module
912     for(Int_t ic=0; ic<fNPHIdiv; ic++) { // array index
913       Float_t ytLeftCenterCell = ytLeftCenterModule + fPhiTileSize *(2*ic-1)/2.; // tower(cell) 
914       fPhiCentersOfCells->AddAt(ytLeftCenterCell,ind);
915       printf(" ind %2.2i : y %8.3f ", ind, fPhiCentersOfCells->At(ind++));
916       if(ic == fNPHIdiv-1) printf("\n"); 
917     }
918   }
919   printf("\n"); 
920 }
921
922 void  AliEMCALGeometry::GetTransformationForSM()
923 {
924   static Bool_t transInit=kFALSE;
925   if(transInit) return;
926
927   int i=0;
928   if(gGeoManager == 0) {
929     Info("CreateTransformationForSM() "," Load geometry : TGeoManager::Import()");
930     assert(0);
931   }
932   TGeoNode *tn = gGeoManager->GetTopNode();
933   TGeoNode *node=0, *XEN1 = 0;
934   for(i=0; i<tn->GetNdaughters(); i++) {
935     node = tn->GetDaughter(i);
936     TString ns(node->GetName());
937     if(ns.Contains(GetNameOfEMCALEnvelope())) {
938       XEN1 = node;
939       break;
940     }
941   }
942   if(!XEN1) {
943     Info("CreateTransformationForSM() "," geometry has not EMCAL envelope with name %s", 
944     GetNameOfEMCALEnvelope());
945     assert(0);
946   }
947   printf(" i %i : EMCAL Envelope is %s : #SM %i \n", i, XEN1->GetName(), XEN1->GetNdaughters());
948   for(i=0; i<XEN1->GetNdaughters(); i++) {
949     TGeoNodeMatrix *sm = (TGeoNodeMatrix*)XEN1->GetDaughter(i);
950     fMatrixOfSM[i] = sm->GetMatrix();
951     //Compiler doesn't like this syntax...
952     //    printf(" %i : matrix %x \n", i, fMatrixOfSM[i]);
953   }
954   transInit = kTRUE;
955 }
956
957 void AliEMCALGeometry::GetGlobal(const Double_t *loc, Double_t *glob, int nsm) const
958 {
959   //  if(fMatrixOfSM[0] == 0) GetTransformationForSM();
960   static int ind;
961   ind = nsm-1;
962   if(ind>=0 && ind < GetNumberOfSuperModules()) {
963     fMatrixOfSM[ind]->LocalToMaster(loc, glob);
964   }
965 }
966
967 void AliEMCALGeometry::GetGlobal(const Int_t /* absId */, TVector3 & /* vglob */) const
968 { // have to be defined  
969 }
970
971 void AliEMCALGeometry::GetGlobal(const TVector3 &vloc, TVector3 &vglob, int nsm) const
972 {
973   static Double_t tglob[3], tloc[3];
974   vloc.GetXYZ(tloc);
975   GetGlobal(tloc, tglob, nsm);
976   vglob.SetXYZ(tglob[0], tglob[1], tglob[2]);
977 }
978
979 void AliEMCALGeometry::GetGlobal(const AliRecPoint *rp, TVector3 &vglob) const
980 {
981   static TVector3 vloc;
982   static Int_t nSupMod, nTower, nIphi, nIeta;
983
984   AliRecPoint *rpTmp = (AliRecPoint*)rp; // const_cast ??
985   if(!rpTmp) return;
986   AliEMCALRecPoint *rpEmc = (AliEMCALRecPoint*)rpTmp;
987
988   GetCellIndex(rpEmc->GetAbsId(0), nSupMod, nTower, nIphi, nIeta);
989   rpTmp->GetLocalPosition(vloc);
990   GetGlobal(vloc, vglob, nSupMod);
991 }
992
993 // Service routine 
994 int  AliEMCALGeometry::ParseString(const TString &topt, TObjArray &Opt)
995 { // Feb 06, 2006
996   Ssiz_t begin, index, end, end2;
997   begin = index = end = end2 = 0;
998   TRegexp separator("[^ ;,\\t\\s/]+");
999   while ( (begin < topt.Length()) && (index != kNPOS) ) {
1000     // loop over given options
1001     index = topt.Index(separator,&end,begin);
1002     if (index >= 0 && end >= 1) {
1003       TString substring(topt(index,end));
1004       Opt.Add(new TObjString(substring.Data()));
1005     }
1006     begin += end+1;
1007   }
1008   return Opt.GetEntries();
1009 }