]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFcalibHisto.cxx
Extra library EMCALUtils needed for particle correlations task.
[u/mrichter/AliRoot.git] / TOF / AliTOFcalibHisto.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 /*************************************************************************
17  *
18  * AliTOFcalibHisto - class to handle TOF calibration histograms,
19  *                    map histograms and more
20  *
21  *
22  * autors:   Roberto Preghenella (R+)
23  * concacts: preghenella@bo.infn.it
24  *
25  *************************************************************************/
26
27 #include "AliTOFcalibHisto.h"
28 #include "AliLog.h"
29 #include "TH1F.h"
30 #include "TFile.h"
31 #include "AliTOFRawStream.h"
32 #include "AliTOFCableLengthMap.h"
33 #include "AliESDtrack.h"
34
35 ClassImp(AliTOFcalibHisto)
36
37 //__________________________________________________________________________
38
39 TFile *AliTOFcalibHisto::fgCalibHistoFile = NULL;
40 TFile *AliTOFcalibHisto::fgCalibParFile = NULL;
41
42 //__________________________________________________________________________
43
44 TString AliTOFcalibHisto::fgCalibHistoFileName = "$ALICE_ROOT/TOF/data/AliTOFcalibHisto.root";
45 TString AliTOFcalibHisto::fgCalibParFileName = "$ALICE_ROOT/TOF/data/AliTOFcalibPar.root";
46
47 //__________________________________________________________________________
48
49 const TString AliTOFcalibHisto::fgkCalibConstName[kNcalibConsts] = {
50   "LHCperiod",
51   "AmphenolCableDelay",
52   "FlatCableDelay",
53   "InterfaceCardDelay"
54 };
55
56 //__________________________________________________________________________
57
58 const TString AliTOFcalibHisto::fgkCalibMapName[kNcalibMaps] = {
59   /* main index */
60   "Index",
61   /* EO indices */
62   "DDL",
63   "TRM", 
64   "Chain", 
65   "TDC", 
66   "Channel", 
67   /* DO indices */
68   "Sector", 
69   "Plate", 
70   "Strip", 
71   "SectorStrip", 
72   "PadZ", 
73   "PadX", 
74   "Pad",
75   "InterfaceCardIndex",
76   /* calib constants */
77   "DDLBCshift",
78   "FlatCableLength",
79   "InterfaceCardLength",
80   "AmphenolCableLength"
81 };
82
83 //__________________________________________________________________________
84
85 const TString AliTOFcalibHisto::fgkCalibParName[kNcalibPars] = {
86   "hDDLDelay",
87   "hHPTDCDelay",
88   "hLeftFEAchDelay",
89   "hRightFEAchDelay",
90   "hFEADelay",
91   "hTRMDelay",
92   "hSlew"
93 };
94
95 //__________________________________________________________________________
96
97 /* LHC clock period [ns] */
98 const Float_t AliTOFcalibHisto::fgkLHCperiod = (24.4e-3 * 1024); /* ns */
99
100 //__________________________________________________________________________
101
102 /* Amphenol cable delay [ns/cm] */
103 const Float_t AliTOFcalibHisto::fgkAmphenolCableDelay = 5.13e-2; /* from measurement */
104
105 //__________________________________________________________________________
106
107 /* flat cable delay [ns/cm] */
108 //const Float_t AliTOFcalibHisto::fgkFlatCableDelay = 5.3e-2; /* from Amphenol 132-2829 series data-sheet */
109 const Float_t AliTOFcalibHisto::fgkFlatCableDelay = 5.124e-2; /* from LHC08d calibration */
110
111 //__________________________________________________________________________
112
113 /* interface card delay [ns/cm] */
114 //const Float_t AliTOFcalibHisto::fgkInterfaceCardDelay = 6.9e-2; /* from HyperLinx simulation */
115 const Float_t AliTOFcalibHisto::fgkInterfaceCardDelay = 5.7898e-2; /* from LHC08d calibration */
116
117 //__________________________________________________________________________
118
119 /* number of readout channels (DO/EO) */
120 const Int_t AliTOFcalibHisto::fgkNchannels = 157248;
121 const Int_t AliTOFcalibHisto::fgkNchannelsEO = 172800;
122
123 //__________________________________________________________________________
124
125 /* DDL BC shifts due to TTC fibers [LHCperiod] */
126 const Int_t AliTOFcalibHisto::fgkDDLBCshift[72] = {
127   2, 2, -1, -1,
128   2, 2, 0, 0,
129   2, 2, 0, 0,
130   2, 2, 0, 0,
131   2, 2, 0, 0,
132   2, 2, 0, 0,
133   2, 2, 0, 0,
134   2, 2, 0, 0,
135   2, 2, 0, 0,
136   2, 2, 0, 0,
137   2, 2, -1, -1,
138   2, 2, -1, -1,
139   2, 2, -2, -2,
140   2, 2, -2, -2,
141   2, 2, -2, -2,
142   2, 2, -1, -1,
143   2, 2, -1, -1,
144   2, 2, -1, -1
145 };
146
147 //__________________________________________________________________________
148
149 /* strip flat-cable length (preliminary) [cm] */
150 const Float_t AliTOFcalibHisto::fgkFlatCableLength[91] = {
151   18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 17.,
152   21., 21., 21., 21., 21., 17., 17., 21., 21., 17., 21., 21., 21., 17., 21., 21., 17., 21., 23.,
153   17., 19., 17., 19., 17., 19., 17., 19., 17., 19., 17., 19., 17., 19., 17.,
154   23., 21., 17., 21., 21., 17., 21., 21., 21., 17., 21., 21., 17., 17., 21., 21., 21., 21., 21.,
155   17., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18., 18.
156 };
157
158 //__________________________________________________________________________
159
160 /* interface card lenght (preliminary) [cm] */
161 const Float_t AliTOFcalibHisto::fgkInterfaceCardLength[48] = {
162   13.97, 12.57, 14.52, 13.10, 15.44, 13.60, 10.58, 9.14, 
163   11.21, 9.76, 12.11, 10.76, 8.67, 7.58, 9.32, 8.09,
164   10.24, 8.4, 5.51, 4.31, 6.54, 5.23, 7.48, 6.28,
165   10.43, 8.76, 11.05, 9.43, 11.72, 10.14, 7.2, 5.69,
166   7.71, 6.26, 8.36, 7.19, 4.85, 4.09, 5.57, 4.35, 
167   6.59, 5.12, 2.49, 2.96, 2.70, 2.76, 2.91, 2.55
168 };
169
170 //__________________________________________________________________________
171
172 Bool_t AliTOFcalibHisto::fgCableCorrectionFlag[kNcorrections] = {
173   kFALSE, // kDDLBCcorr
174   kTRUE, // kAmphenolCableCorr
175   kTRUE, // kFlatCableCorr
176   kTRUE, // kInterfaceCardCorr
177   kFALSE, // kDDLdelayCorr
178   kFALSE, // kHPTDCdelayCorr
179   kFALSE, // kFEAchDelayCorr
180   kFALSE, // kFEAdelayCorr
181   kFALSE, // kTRMdelayCorr
182   kFALSE, // kTimeSlewingCorr
183 };
184
185 //__________________________________________________________________________
186
187 Bool_t AliTOFcalibHisto::fgFullCorrectionFlag[kNcorrections] = {
188   kFALSE, // kDDLBCcorr
189   kTRUE, // kAmphenolCableCorr
190   kTRUE, // kFlatCableCorr
191   kTRUE, // kInterfaceCardCorr
192   kTRUE, // kDDLdelayCorr
193   kTRUE, // kHPTDCdelayCorr
194   kTRUE, // kFEAchDelayCorr
195   kTRUE, // kFEAdelayCorr
196   kTRUE, // kTRMdelayCorr
197   kFALSE, // kTimeSlewingCorr
198 };
199
200 //__________________________________________________________________________
201
202 AliTOFcalibHisto::AliTOFcalibHisto() :
203   TObject(),
204   fCalibConst(),
205   fCalibMap(),
206   fCalibPar()
207 {
208   /* default constructor */
209 }
210
211 //__________________________________________________________________________
212
213 AliTOFcalibHisto::~AliTOFcalibHisto()
214 {
215   /* default destructor */
216 }
217
218 //__________________________________________________________________________
219
220 void 
221 AliTOFcalibHisto::LoadHisto(TFile* file, TH1F **histo, const Char_t *name) 
222 {
223   /* load histo */
224   *histo = (TH1F *)file->Get(name);
225   if (!*histo)
226     AliWarning(Form("error while getting %s histo", name));
227 }
228
229 //__________________________________________________________________________
230
231 void 
232 AliTOFcalibHisto::CreateHisto(TH1F **histo, const Char_t *name, Int_t size) 
233 {
234   /* create histo */
235   *histo = new TH1F(name, Form(";index;%s", name), size, 0, size);
236   if (!*histo)
237     AliWarning(Form("error while creating %s histo", name));
238 }
239
240 //__________________________________________________________________________
241
242 void 
243 AliTOFcalibHisto::WriteHisto(TFile *file, TH1F *histo) 
244 {
245   /* write histo */
246   if (!file || !file->IsOpen() || !histo)
247     return;
248   file->cd(); 
249   histo->Write();
250 }
251
252 //__________________________________________________________________________
253
254 void
255 AliTOFcalibHisto::SetHisto(TH1F *histo, Int_t index, Float_t value)
256 {
257   /* set histo */
258   if (!histo)
259     return;
260   histo->SetBinContent(index + 1, value);
261 }
262
263 //__________________________________________________________________________
264
265 Float_t
266 AliTOFcalibHisto::GetHisto(TH1F *histo, Int_t index)
267 {
268   /* get histo */
269   if (!histo) {
270     AliWarning("cannot get histo");
271     return 0.;
272   }
273   return histo->GetBinContent(index + 1);
274 }
275
276 //__________________________________________________________________________
277
278 void
279 AliTOFcalibHisto::LoadCalibHisto()
280 {
281   /* load calib histo */
282
283   if (fgCalibHistoFile && fgCalibHistoFile->IsOpen())
284     AliWarning("calib histo file already open: reloading"); 
285
286   /* open input file */
287   TFile *fileIn = TFile::Open(GetCalibHistoFileName());
288   if (!fileIn || !fileIn->IsOpen())
289     AliFatal(Form("cannot open input file %s", GetCalibHistoFileName()));
290
291   /* set calib histo file */
292   fgCalibHistoFile = fileIn;
293
294   /* load consts */
295   for (Int_t iConst = 0; iConst < kNcalibConsts; iConst++)
296     LoadHisto(fileIn, &fCalibConst[iConst], fgkCalibConstName[iConst].Data());
297   /* load maps */
298   for (Int_t iMap = 0; iMap < kNcalibMaps; iMap++)
299     LoadHisto(fileIn, &fCalibMap[iMap], fgkCalibMapName[iMap].Data());
300 }
301
302 //__________________________________________________________________________
303
304 void
305 AliTOFcalibHisto::LoadCalibPar()
306 {
307   /* load calib par */
308
309   if (fgCalibParFile && fgCalibParFile->IsOpen())
310     AliWarning("calib par file already open: reloading"); 
311
312   /* load calib histo */
313   LoadCalibHisto();
314
315   /* open input file */
316   TFile *fileIn = TFile::Open(GetCalibParFileName());
317   if (!fileIn || !fileIn->IsOpen())
318     AliError(Form("cannot open input file %s", GetCalibParFileName()));
319
320   /* set calib par file */
321   fgCalibParFile = fileIn;
322
323   /* load pars */
324   for (Int_t i = 0; i < kNcalibPars; i++)
325     LoadHisto(fileIn, &fCalibPar[i], fgkCalibParName[i].Data());
326
327 }
328
329 //__________________________________________________________________________
330
331 void
332 AliTOFcalibHisto::WriteCalibHisto()
333 {
334   /* write calib histo */
335
336   /* open output file */
337   TFile *fileOut = TFile::Open(GetCalibHistoFileName(), "RECREATE");
338   if (!fileOut || !fileOut->IsOpen())
339     AliFatal(Form("cannot open output file %s", GetCalibHistoFileName()));
340
341   /* create consts */
342   for (Int_t iConst = 0; iConst < kNcalibConsts; iConst++)
343     CreateHisto(&fCalibConst[iConst], fgkCalibConstName[iConst].Data(), 1);
344   /* create maps */
345   for (Int_t iMap = 0; iMap < kNcalibMaps; iMap++)
346     if (iMap == kIndex)
347       CreateHisto(&fCalibMap[iMap], fgkCalibMapName[iMap].Data(), fgkNchannelsEO);
348     else
349       CreateHisto(&fCalibMap[iMap], fgkCalibMapName[iMap].Data(), fgkNchannels);
350
351   /*** SETUP CONSTS ***/
352
353   SetHisto(fCalibConst[kLHCperiod], 0, fgkLHCperiod);
354   SetHisto(fCalibConst[kAmphenolCableDelay], 0, fgkAmphenolCableDelay);
355   SetHisto(fCalibConst[kFlatCableDelay], 0, fgkFlatCableDelay);
356   SetHisto(fCalibConst[kInterfaceCardDelay], 0, fgkInterfaceCardDelay);
357   
358   /***  SETUP MAPS  ***/
359
360   AliTOFRawStream rawStream;
361   Int_t indexEO, det[5], dummy, index, sector, plate, strip, sectorStrip, padz, padx, pad, icIndex;
362
363   /* temporarly disable warnings */
364   AliLog::EType_t logLevel = (AliLog::EType_t)AliLog::GetGlobalLogLevel();
365   AliLog::SetGlobalLogLevel(AliLog::kError);
366
367   /* loop over electronics oriented (EO) indices */
368   for (Int_t ddl = 0; ddl < 72; ddl++)
369     for (Int_t trm = 0; trm < 10; trm++)
370       for (Int_t chain = 0; chain < 2; chain++)
371         for (Int_t tdc = 0; tdc < 15; tdc++)
372           for (Int_t channel = 0; channel < 8; channel++) {
373             
374             /* compute index EO */
375             indexEO = GetIndexEO(ddl, trm, chain, tdc, channel);
376
377             /* convert EO indices into detector oriented (DO) indices
378                (this call causes some warnings because the loop includes
379                EO indices which are not connected to physical channels) */
380             rawStream.EquipmentId2VolumeId(ddl, trm + 3, chain, tdc, channel, det);
381             
382             /* swap det[3] and det[4] */
383             dummy = det[3]; det[3] = det[4]; det[4] = dummy;
384             
385             /* check detector indices */
386             if (det[0] < 0 || det[0] > 71 ||
387                 det[1] < 0 || det[1] > 4 ||
388                 det[2] < 0 || det[2] > 18 ||
389                 det[3] < 0 || det[3] > 1 ||
390                 det[4] < 0 || det[4] > 47) {
391               SetHisto(fCalibMap[kIndex], indexEO, -1);
392               continue;
393             }
394             
395             /* setup information */
396             index = AliTOFGeometry::GetIndex(det);
397             sector = det[0];
398             plate = det[1];
399             strip = det[2];
400             sectorStrip = plate < 3 ? plate * 19 + strip : plate * 19 - 4 + strip;
401             padz = det[3];
402             padx = det[4];
403             pad = padz + 2 * padx;
404             icIndex = pad < 48 ? pad : 95 - pad;
405
406             /* set maps */
407
408             /* main index */
409             SetHisto(fCalibMap[kIndex], indexEO, index);
410             /* EO indices */
411             SetHisto(fCalibMap[kDDL], index, ddl);
412             SetHisto(fCalibMap[kTRM], index, trm);
413             SetHisto(fCalibMap[kChain], index, chain);
414             SetHisto(fCalibMap[kTDC], index, tdc);
415             SetHisto(fCalibMap[kChannel], index, channel);
416             /* DO indices */
417             SetHisto(fCalibMap[kSector], index, sector);
418             SetHisto(fCalibMap[kPlate], index, plate);
419             SetHisto(fCalibMap[kStrip], index, strip);
420             SetHisto(fCalibMap[kSectorStrip], index, sectorStrip);
421             SetHisto(fCalibMap[kPadZ], index, padz);
422             SetHisto(fCalibMap[kPadX], index, padx);
423             SetHisto(fCalibMap[kPad], index, pad);
424             SetHisto(fCalibMap[kInterfaceCardIndex], index, icIndex);
425             /* calib constants */
426             SetHisto(fCalibMap[kDDLBCshift], index, fgkDDLBCshift[ddl]);
427             SetHisto(fCalibMap[kFlatCableLength], index, fgkFlatCableLength[sectorStrip]);
428             SetHisto(fCalibMap[kInterfaceCardLength], index, fgkInterfaceCardLength[icIndex]);
429             SetHisto(fCalibMap[kAmphenolCableLength], index, AliTOFCableLengthMap::GetCableLength(ddl, trm + 3, chain, tdc));
430             
431           } /* loop over electronics oriented (EO) indices */
432
433   /* re-enable warnings */
434   AliLog::SetGlobalLogLevel(logLevel);
435
436   /* write consts */
437   for (Int_t iConst = 0; iConst < kNcalibConsts; iConst++)
438     WriteHisto(fileOut, fCalibConst[iConst]);
439   /* write maps */
440   for (Int_t iMap = 0; iMap < kNcalibMaps; iMap++)
441     WriteHisto(fileOut, fCalibMap[iMap]);
442
443   /* close output file */
444   fileOut->Close();
445 }
446
447 //__________________________________________________________________________
448
449 Float_t
450 AliTOFcalibHisto::GetCorrection(Int_t corr, Int_t index, Float_t tot)
451 {
452   /* apply correction */
453
454   Int_t ddl, chain, tdc, channel, hptdc, pbCh, feaIndex, sector, plate, strip, padx, trm;
455   Float_t slewing;
456   
457   switch (corr) {
458   case kDDLBCcorr:
459     return -GetCalibConst(kLHCperiod) * GetCalibMap(kDDLBCshift, index);
460   case kAmphenolCableCorr:
461     return GetCalibConst(kAmphenolCableDelay) * GetCalibMap(kAmphenolCableLength, index);
462   case kFlatCableCorr:
463     return GetCalibConst(kFlatCableDelay) * GetCalibMap(kFlatCableLength, index);
464   case kInterfaceCardCorr:
465     return GetCalibConst(kInterfaceCardDelay) * GetCalibMap(kInterfaceCardLength, index);
466   case kDDLdelayCorr:
467     ddl = (Int_t)GetCalibMap(kDDL, index);
468     return GetCalibPar(kDDLdelayPar, ddl);
469   case kHPTDCdelayCorr:
470     chain = (Int_t)GetCalibMap(kChain, index);
471     tdc = (Int_t)GetCalibMap(kTDC, index);
472     hptdc = tdc + 15 * chain;
473     return GetCalibPar(kHPTDCdelayPar, hptdc);
474   case kFEAchDelayCorr:
475     ddl = (Int_t)GetCalibMap(kDDL, index);
476     tdc = (Int_t)GetCalibMap(kTDC, index);
477     channel = (Int_t)GetCalibMap(kChannel, index);
478     pbCh = channel + 8 * (tdc % 3);
479     if (ddl % 2 == 0)
480       return GetCalibPar(kRightFEAchDelayPar, pbCh);
481     else
482       return GetCalibPar(kLeftFEAchDelayPar, pbCh);
483   case kFEAdelayCorr:
484     sector = (Int_t)GetCalibMap(kSector, index);
485     plate = (Int_t)GetCalibMap(kPlate, index);
486     strip = (Int_t)GetCalibMap(kStrip, index);
487     padx = (Int_t)GetCalibMap(kPadX, index);
488     feaIndex = padx / 12 + 4 * strip + 4 * 19 * plate + 4 * 19 * 5 * sector;      
489     return GetCalibPar(kFEAdelayPar, feaIndex);
490   case kTRMdelayCorr:
491     trm = (Int_t)GetCalibMap(kTRM, index);
492     return GetCalibPar(kTRMdelayPar, trm);
493   case kTimeSlewingCorr:
494     slewing = 0.;
495     for (Int_t i = 0; i < fCalibPar[kTimeSlewingPar]->GetNbinsX(); i++)
496       slewing += GetCalibPar(kTimeSlewingPar, i) * TMath::Power(tot, i);
497     return slewing;
498   default:
499     AliWarning(Form("unknown correction flag (%d)", corr));
500     return 0.;
501   }
502 }
503
504 //__________________________________________________________________________
505
506 Float_t
507 AliTOFcalibHisto::GetNominalCorrection(Int_t index)
508 {
509   /* get nominal correction */
510   Float_t corr = 0;
511   for (Int_t iCorr = 0; iCorr < kNcorrections; iCorr++)
512     corr += GetCorrection(iCorr, index);
513   return corr;
514 }
515
516 //__________________________________________________________________________
517
518 void
519 AliTOFcalibHisto::ApplyNominalCorrection(AliESDtrack *track)
520 {
521   /* apply nominal correction */
522   
523   Double_t rawTime = track->GetTOFsignalRaw();
524   Int_t index = track->GetTOFCalChannel();
525   Double_t time = rawTime - 1.e3 * GetNominalCorrection(index);
526   track->SetTOFsignal(time);
527 }
528
529 //__________________________________________________________________________
530
531 Float_t
532 AliTOFcalibHisto::GetCableCorrection(Int_t index)
533 {
534   /* get cable correction */
535   Float_t corr = 0;
536   for (Int_t iCorr = 0; iCorr < kNcorrections; iCorr++)
537     if (fgCableCorrectionFlag[iCorr])
538       corr += GetCorrection(iCorr, index);
539   return corr;
540 }
541
542 //__________________________________________________________________________
543
544 Float_t
545 AliTOFcalibHisto::GetFullCorrection(Int_t index)
546 {
547   /* get full correction */
548   Float_t corr = 0;
549   for (Int_t iCorr = 0; iCorr < kNcorrections; iCorr++)
550     if (fgFullCorrectionFlag[iCorr])
551       corr += GetCorrection(iCorr, index);
552   return corr;
553 }
554