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