Fixing big memory leak. The parameters should NOT be read from CDB inside the loop...
[u/mrichter/AliRoot.git] / T0 / AliT0Parameters.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 //                                                                          
20 // T0 - T0. 
21 //
22 // This class is a singleton that handles various parameters of
23 // the T0 detectors.  
24 // Eventually, this class will use the Conditions DB to get the
25 // various parameters, which code can then request from here.
26 //                                                       
27 #include "AliLog.h"               
28 #include "AliT0Parameters.h"      
29 #include "AliT0CalibData.h"   
30 #include "AliT0LookUpValue.h"
31 #include <AliCDBManager.h>        
32 #include <AliCDBEntry.h>          
33 #include <AliCDBStorage.h>  
34 #include <TMath.h>
35 #include <TSystem.h>
36 #include <Riostream.h>
37 #include <TGeoManager.h>
38 #include <TGeoPhysicalNode.h>
39
40 AliT0CalibData* AliT0Parameters::fgCalibData = 0;
41 AliT0CalibData* AliT0Parameters::fgLookUp = 0;
42 AliT0CalibData* AliT0Parameters::fgSlewCorr =0;
43 //====================================================================
44 ClassImp(AliT0Parameters)
45 #if 0
46   ; // This is here to keep Emacs for indenting the next line
47 #endif
48
49 //____________________________________________________________________
50 AliT0Parameters* AliT0Parameters::fgInstance = 0;
51 //____________________________________________________________________
52 AliT0Parameters* 
53 AliT0Parameters::Instance() 
54 {
55   // Get static instance 
56   if (!fgInstance) {
57     fgInstance = new AliT0Parameters;
58     fgInstance->Init();
59   }
60   return fgInstance;
61 }
62
63 //____________________________________________________________________
64 AliT0Parameters::AliT0Parameters()
65   :fIsInit(kFALSE),
66    fPh2Mip(0),fmV2Mip(0),
67    fChannelWidth(0),fmV2Channel(0),
68    fQTmin(0),fQTmax(0),
69    fSlewingLED(),fSlewingRec(),
70    fPMTeff(),
71    fTimeDelayLED(0),fTimeDelayCFD(0),fTimeDelayTVD(0),
72    fCalibentry(), fLookUpentry(),fSlewCorr()
73 {
74   // Default constructor 
75
76   for (Int_t ipmt=0; ipmt<24; ipmt++)
77     {
78       SetSlewingLED(ipmt);
79       SetSlewingRec(ipmt);
80       SetWalk(ipmt);
81       SetPh2Mip();      
82       SetmV2Mip();      
83       SetChannelWidth();
84       SetmV2channel();
85       SetQTmin();
86       SetQTmax();
87       SetPMTeff(ipmt);
88
89    }
90   SetTimeDelayTVD();
91   SetZposition();
92   SetNumberOfTRMs(2);
93   
94 }
95
96 //__________________________________________________________________
97 void
98 AliT0Parameters::Init()
99 {
100   // Initialize the parameters manager.  We need to get stuff from the
101   // CDB here. 
102   //   if (fIsInit) return;
103   
104
105   AliCDBStorage *stor =AliCDBManager::Instance()->GetStorage("local://$ALICE_ROOT");
106   //time equalizing
107   AliCDBEntry* fCalibentry  = stor->Get("T0/Calib/TimeDelay",0);
108   if (fCalibentry)
109    fgCalibData  = (AliT0CalibData*)fCalibentry->GetObject();
110   else 
111     AliError(" ALARM !!!! No time delays in CDB "); 
112  //slewing correction
113   AliCDBEntry* fSlewCorr  = stor->Get("T0/Calib/Slewing_Walk",0);
114   if (fSlewCorr){
115     fgSlewCorr  = (AliT0CalibData*)fSlewCorr->GetObject();
116   }
117   fLookUpentry  = stor->Get("T0/Calib/LookUp_Table",0);
118   if (fLookUpentry){
119     fgLookUp  = (AliT0CalibData*)fLookUpentry->GetObject();
120   }
121   else {
122   const char * filename = gSystem->ExpandPathName("$ALICE_ROOT/T0/lookUpTable.txt");
123   ifstream inFile(filename);
124   fgLookUp->ReadAsciiLookup(filename);
125   }
126
127   fIsInit = kTRUE;
128 }
129
130
131 //__________________________________________________________________
132 Float_t
133 AliT0Parameters::GetTimeDelayLED(Int_t ipmt) 
134 {
135   // return time delay for LED channel
136   // 
137   if (!fCalibentry) {
138     fTimeDelayLED = 0;
139     return  fTimeDelayLED;
140   } 
141   return fgCalibData ->GetTimeDelayLED(ipmt);
142 }
143 //__________________________________________________________________
144 Float_t
145 AliT0Parameters::GetTimeDelayCFD(Int_t ipmt) 
146 {
147   // return time delay for CFD channel
148    // 
149   if (!fCalibentry) 
150     {
151       fTimeDelayCFD = 1000+ipmt*100;
152       return fTimeDelayCFD;
153     }
154    
155   return fgCalibData->GetTimeDelayCFD(ipmt);
156 }
157
158 //__________________________________________________________________
159
160 void 
161 AliT0Parameters::SetSlewingLED(Int_t ipmt)
162 {
163   //  Set Slweing Correction for LED channel 
164      Float_t mv[23] = {25, 30,40,60, 80,100,150,200,250,300,
165                        400,500,600,800,1000,1500, 2000, 3000, 4000, 5500,
166                        6000, 7000,8000};
167       Float_t y[23] = {5044, 4719, 3835, 3224, 2847, 2691,2327, 2067, 1937, 1781,
168                        1560, 1456 ,1339, 1163.5, 1027, 819, 650, 520, 370.5, 234,
169                        156, 78, 0};
170       
171       TGraph* gr = new TGraph(23,mv,y);
172       fSlewingLED.AddAtAndExpand(gr,ipmt);
173   }
174 //__________________________________________________________________
175
176 Float_t AliT0Parameters::GetSlewingLED(Int_t ipmt, Float_t mv) const
177 {
178   if (!fCalibentry) {
179     return ((TGraph*)fSlewingLED.At(ipmt))->Eval(mv); 
180   } 
181   return fgCalibData->GetSlewingLED(ipmt, mv) ;
182 }
183
184
185 //__________________________________________________________________
186
187 TGraph *AliT0Parameters::GetSlew(Int_t ipmt) const
188 {
189   if (!fCalibentry) {
190     return  (TGraph*)fSlewingLED.At(ipmt); 
191   } 
192   return fgCalibData -> GetSlew(ipmt) ;
193 }
194
195 //__________________________________________________________________
196
197
198 void 
199 AliT0Parameters::SetSlewingRec(Int_t ipmt)
200 {
201   //  Set Slweing Correction for LED channel 
202       Float_t mv[23] = {25, 30, 40,60, 80,100,150,200,250,300,
203                         400,500,600,800,1000,1500, 2000, 3000, 4000, 5500,
204                         6000, 7000,8000};
205       Float_t y[23] = {5044, 4719, 3835, 3224, 2847, 2691,2327, 2067, 1937, 1781, 
206                        1560, 1456 ,1339, 1163.5, 1027, 819, 650, 520, 370.5, 234, 
207                        156, 78, 0};
208       Float_t y1[23], mv1[23];
209       for (Int_t i=0; i<23; i++){
210         y1[i] = y[22-i]; mv1[i] = mv[22-i];}
211
212       TGraph* gr = new TGraph(23,y1,mv1);
213       fSlewingRec.AddAtAndExpand(gr,ipmt);
214
215 }
216 //__________________________________________________________________
217
218 Float_t AliT0Parameters::GetSlewingRec(Int_t ipmt, Float_t mv) const
219 {
220   if (!fCalibentry) {
221     return ((TGraph*)fSlewingRec.At(ipmt))->Eval(mv); 
222   } 
223   return fgCalibData -> GetSlewingRec(ipmt, mv) ;
224 }
225
226 //__________________________________________________________________
227
228 TGraph *AliT0Parameters::GetSlewRec(Int_t ipmt) const
229 {
230   if (!fCalibentry) {
231     return  (TGraph*)fSlewingRec.At(ipmt); 
232   } 
233   return fgCalibData -> GetSlewRec(ipmt) ;
234 }
235
236
237 //________________________________________________________________
238 void AliT0Parameters::SetWalk(Int_t ipmt)
239 {
240
241  Int_t mv, ps; 
242   Int_t x[70000], y[70000], index[70000];
243   Float_t time[10000],amplitude[10000];
244   string buffer;
245   Bool_t down=false;
246   
247   const char * filename = gSystem->ExpandPathName("$ALICE_ROOT/T0/data/CFD-Amp.txt");
248   ifstream inFile(filename);
249   //  if(!inFile) AliError(Form("Cannot open file %s !",filename));
250   
251   Int_t i=0;
252   while(getline(inFile,buffer)){
253     inFile >> ps >> mv;
254
255     x[i]=ps; y[i]=mv;
256     i++;
257   }
258   inFile.close();
259  
260   TMath::Sort(i, y, index,down);
261   Int_t amp=0, iin=0, isum=0, sum=0;
262   Int_t ind=0;
263   for (Int_t ii=0; ii<i; ii++)
264     {
265       ind=index[ii];
266       if(y[ind] == amp)
267         {
268           sum +=x[ind];
269           iin++;
270         }
271       else
272         {
273           if(iin>0)
274             time[isum] = Float_t (sum/(iin));
275           else
276             time[isum] =Float_t (x[ind]);
277           amplitude[isum] = Float_t (amp);
278           amp=y[ind];
279           //      cout<<ii<<" "<<ind<<" "<<y[ind]<<" "<<x[ind]<<" iin "<<iin<<" mean "<<time[isum]<<" amp "<< amplitude[isum]<<" "<<isum<<endl;
280           iin=0;
281           isum++;
282           sum=0;
283         }
284
285
286     }
287
288   inFile.close();
289
290    TGraph* gr = new TGraph(isum, amplitude, time);
291   fWalk.AddAtAndExpand(gr,ipmt);
292   
293   
294 }
295 //__________________________________________________________________
296
297 TGraph *AliT0Parameters::GetWalk(Int_t ipmt) const
298 {
299   if (!fCalibentry) {
300     return  (TGraph*)fWalk.At(ipmt); 
301   } 
302   return fgCalibData -> GetWalk(ipmt) ;
303 }
304
305 //__________________________________________________________________
306
307 Float_t AliT0Parameters::GetWalkVal(Int_t ipmt, Float_t mv) const
308 {
309   if (!fCalibentry) {
310     return ((TGraph*)fWalk.At(ipmt))->Eval(mv); 
311   } 
312   return fgCalibData -> GetWalkVal(ipmt, mv) ;
313 }
314
315
316 //__________________________________________________________________
317 void 
318 AliT0Parameters::SetPMTeff(Int_t ipmt)
319 {
320   Float_t lambda[50];
321   Float_t eff[50 ] = {0,        0,       0.23619,  0.202909, 0.177913, 
322                     0.175667, 0.17856, 0.190769, 0.206667, 0.230286,
323                     0.252276, 0.256267,0.26,     0.27125,  0.281818,
324                     0.288118, 0.294057,0.296222, 0.301622, 0.290421, 
325                     0.276615, 0.2666,  0.248,    0.23619,  0.227814, 
326                     0.219818, 0.206667,0.194087, 0.184681, 0.167917, 
327                     0.154367, 0.1364,  0.109412, 0.0834615,0.0725283, 
328                     0.0642963,0.05861, 0.0465,   0.0413333,0.032069, 
329                     0.0252203,0.02066, 0.016262, 0.012,    0.00590476,
330                     0.003875, 0.00190, 0,        0,        0          } ;
331   for (Int_t i=0; i<50; i++) lambda[i]=200+10*i; 
332
333   TGraph* gr = new TGraph(50,lambda,eff);
334   fPMTeff.AddAtAndExpand(gr,ipmt);
335 }
336 //________________________________________________________________
337
338 Int_t 
339 AliT0Parameters::GetChannel(Int_t trm,  Int_t tdc, Int_t chain, Int_t channel)
340 {
341
342   
343   AliT0LookUpKey * lookkey;  //= new AliT0LookUpKey();
344   AliT0LookUpValue * lookvalue= new AliT0LookUpValue(trm,tdc,chain,channel);
345     
346    lookkey = (AliT0LookUpKey*) fgLookUp->GetMapLookup()->GetValue((TObject*)lookvalue);
347   if (!lookkey ) {
348     cout<<" no such address "<<endl; return -1;
349   }
350   
351
352   //cout<<"AliT0Parameters:: key "<<lookkey->GetKey()<<endl;
353   return lookkey->GetKey();
354   
355
356 }
357 //__________________________________________________________________
358 Int_t
359 AliT0Parameters::GetNumberOfTRMs() 
360 {
361   // return number of trms
362   // 
363   if (!fgLookUp) {
364     fNumberOfTRMs = 2;
365     return  fNumberOfTRMs;
366   } 
367   return  fgLookUp ->GetNumberOfTRMs();
368 }
369 //________________________________________________________________________________
370 Double_t AliT0Parameters::GetZPosition(const char* symname){
371 // Get the global z coordinate of the given T0 alignable volume
372 //
373   Double_t *tr;
374   
375   cout<<symname<<endl;
376   TGeoPNEntry *pne = gGeoManager->GetAlignableEntry(symname);
377   if (!pne) return 0;
378   
379
380   TGeoPhysicalNode *pnode = pne->GetPhysicalNode();
381   if(pnode){
382           TGeoHMatrix* hm = pnode->GetMatrix();
383            tr = hm->GetTranslation();
384   }else{
385           const char* path = pne->GetTitle();
386           if(!gGeoManager->cd(path)){
387                   AliErrorClass(Form("Volume path %s not valid!",path));
388                   return 0;
389           }
390          tr = gGeoManager->GetCurrentMatrix()->GetTranslation();
391   }
392   return tr[2];
393
394 }
395