]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFcalib.cxx
Bug fix (Sergey)
[u/mrichter/AliRoot.git] / TOF / AliTOFcalib.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 $Log: AliTOFcalib.cxx,v $
18 Revision 1.21  2007/11/02 15:41:49  hristov
19 Provide return value if the function is not void
20
21 Revision 1.20  2007/10/26 15:13:50  zampolli
22 Using a TChain instead of a TTree
23
24 Revision 1.19  2007/10/23 15:27:38  zampolli
25 Rearrangement of Calibration objects for simulation
26
27 Revision 1.16  2007/10/08 10:13:26  zampolli
28 First Run and Last Run members added, infinite validity of calib obj implemented.
29
30 Revision 1.15  2007/10/04 13:23:28  zampolli
31 Updates to handle functionalities in TOF online/offline calibration according to the latest schema
32
33 Revision 1.14  2007/06/06 16:26:30  arcelli
34 remove fall-back call to local CDB storage
35
36 Revision 1.13  2007/04/20 13:59:40  arcelli
37 make protections agains failed retrieval of the CDB object in a proper way
38
39 Revision 1.12  2007/03/23 11:31:16  arcelli
40 CDB Entry for TOF Reconstruction Parameters
41
42 Revision 1.11  2007/02/28 18:08:26  arcelli
43 Add protection against failed retrieval of the CDB cal object
44
45 Revision 1.10  2006/08/22 13:30:49  arcelli
46 removal of effective c++ warnings (C.Zampolli)
47
48 Revision 1.9  2006/04/20 22:30:50  hristov
49 Coding conventions (Annalisa)
50
51 Revision 1.8  2006/04/16 22:29:05  hristov
52 Coding conventions (Annalisa)
53
54 Revision 1.7  2006/04/16 20:12:46  hristov
55 Removing memory leak in case of cached CDB entries
56
57 Revision 1.6  2006/04/11 15:28:32  hristov
58 Checks on cache status before deleting calibration objects (A.Colla)
59
60 Revision 1.5  2006/04/05 08:35:38  hristov
61 Coding conventions (S.Arcelli, C.Zampolli)
62
63 Revision 1.4  2006/03/31 11:26:46  arcelli
64  changing CDB Ids according to standard convention
65
66 Revision 1.3  2006/03/28 14:57:02  arcelli
67 updates to handle new V5 geometry & some re-arrangements
68
69 Revision 1.2  2006/02/13 17:22:26  arcelli
70 just Fixing Log info
71
72 Revision 1.1  2006/02/13 16:10:48  arcelli
73 Add classes for TOF Calibration (C.Zampolli)
74
75 author: Chiara Zampolli, zampolli@bo.infn.it
76 */  
77
78 ///////////////////////////////////////////////////////////////////////////////
79 //                                                                           //
80 // class for TOF calibration                                                 //
81 //                                                                           //
82 ///////////////////////////////////////////////////////////////////////////////
83
84 #include "TF1.h"
85 #include "TFile.h"
86 #include "TH1F.h"
87 #include "TH2F.h"
88 //#include "TList.h"
89 //#include "TROOT.h"
90 //#include "TStyle.h"
91 #include "TTree.h"
92 #include "TChain.h"
93 #include "TProfile.h"
94 #include "TGrid.h"
95 #include "TMath.h"
96 #include "TMap.h"
97
98 #include "AliCDBEntry.h"
99 #include "AliCDBRunRange.h"
100 #include "AliCDBId.h"
101 #include "AliCDBManager.h"
102 #include "AliCDBStorage.h"
103 #include "AliCDBMetaData.h"
104 //#include "AliESDtrack.h"
105 //#include "AliESD.h"
106 #include "AliLog.h"
107
108 #include "AliTOFcalib.h"
109 #include "AliTOFChannelOnlineArray.h"
110 #include "AliTOFChannelOnline.h"
111 #include "AliTOFChannelOnlineStatus.h"
112 #include "AliTOFChannelOnlineStatusArray.h"
113 #include "AliTOFChannelOffline.h"
114 #include "AliTOFGeometry.h"
115 #include "AliTOFRecoParam.h"
116 #include "AliTOFDeltaBCOffset.h"
117 #include "AliTOFCTPLatency.h"
118 #include "AliTOFT0Fill.h"
119 #include "AliTOFRunParams.h"
120 #include "AliTOFResponseParams.h"
121 #include "AliESDEvent.h"
122 #include "AliESDtrack.h"
123
124 class TROOT;
125 class TStyle;
126
127 extern TROOT *gROOT;
128 extern TStyle *gStyle;
129
130 ClassImp(AliTOFcalib)
131
132 //_______________________________________________________________________
133 AliTOFcalib::AliTOFcalib():
134   TTask("AliTOFcalib",""),
135   fNChannels(-1),
136   fTOFCalOnline(0x0),
137   fTOFCalOnlinePulser(0x0),
138   fTOFCalOnlineNoise(0x0),
139   fTOFCalOnlineHW(0x0),
140   fTOFCalOffline(0x0),
141   fCal(0x0),
142   fStatus(0x0),
143   fTOFSimToT(0x0),
144   fkValidity(0x0),
145   fTree(0x0),
146   fChain(0x0),
147   fNruns(0),
148   fFirstRun(0),
149   fLastRun(AliCDBRunRange::Infinity()),
150   fConfigMap(new TMap),
151   fDeltaBCOffset(NULL),
152   fCTPLatency(NULL),
153   fT0Fill(NULL),
154   fRunParams(NULL),
155   fResponseParams(NULL),
156   fInitFlag(kFALSE),
157   fRemoveMeanT0(kTRUE),
158   fCalibrateTOFsignal(kTRUE),
159   fCorrectTExp(kFALSE)
160
161   //TOF Calibration Class ctor
162   fNChannels = AliTOFGeometry::NSectors()*(2*(AliTOFGeometry::NStripC()+AliTOFGeometry::NStripB())+AliTOFGeometry::NStripA())*AliTOFGeometry::NpadZ()*AliTOFGeometry::NpadX();
163 }
164 //____________________________________________________________________________ 
165
166 AliTOFcalib::AliTOFcalib(const AliTOFcalib & calib):
167   TTask(calib),
168   fNChannels(calib.fNChannels),
169   fTOFCalOnline(0x0),
170   fTOFCalOnlinePulser(0x0),
171   fTOFCalOnlineNoise(0x0),
172   fTOFCalOnlineHW(0x0),
173   fTOFCalOffline(0x0),
174   fCal(calib.fCal),
175   fStatus(calib.fStatus),
176   fTOFSimToT(calib.fTOFSimToT),
177   fkValidity(calib.fkValidity),
178   fTree(calib.fTree),
179   fChain(calib.fChain),
180   fNruns(calib.fNruns),
181   fFirstRun(calib.fFirstRun),
182   fLastRun(calib.fLastRun),
183   fConfigMap(calib.fConfigMap),
184   fDeltaBCOffset(NULL),
185   fCTPLatency(NULL),
186   fT0Fill(NULL),
187   fRunParams(NULL),
188   fResponseParams(NULL),
189   fInitFlag(calib.fInitFlag),
190   fRemoveMeanT0(calib.fRemoveMeanT0),
191   fCalibrateTOFsignal(calib.fCalibrateTOFsignal),
192   fCorrectTExp(calib.fCorrectTExp)
193 {
194   //TOF Calibration Class copy ctor
195   for (Int_t iarray = 0; iarray<fNChannels; iarray++){
196     AliTOFChannelOnline * calChOnline = (AliTOFChannelOnline*)calib.fTOFCalOnline->At(iarray);
197     AliTOFChannelOnlineStatus * calChOnlineStPulser = (AliTOFChannelOnlineStatus*)calib.fTOFCalOnlinePulser->At(iarray);
198     AliTOFChannelOnlineStatus * calChOnlineStNoise = (AliTOFChannelOnlineStatus*)calib.fTOFCalOnlineNoise->At(iarray);
199     AliTOFChannelOnlineStatus * calChOnlineStHW = (AliTOFChannelOnlineStatus*)calib.fTOFCalOnlineHW->At(iarray);
200     AliTOFChannelOffline * calChOffline = (AliTOFChannelOffline*)calib.fTOFCalOffline->At(iarray);
201     fTOFCalOnline->AddAt(calChOnline,iarray);
202     fTOFCalOnlinePulser->AddAt(calChOnlineStPulser,iarray);
203     fTOFCalOnlineNoise->AddAt(calChOnlineStNoise,iarray);
204     fTOFCalOnlineHW->AddAt(calChOnlineStHW,iarray);
205     fTOFCalOffline->AddAt(calChOffline,iarray);
206   }
207
208   if (calib.fDeltaBCOffset) fDeltaBCOffset = new AliTOFDeltaBCOffset(*calib.fDeltaBCOffset);
209   if (calib.fCTPLatency) fCTPLatency = new AliTOFCTPLatency(*calib.fCTPLatency);
210   if (calib.fT0Fill) fT0Fill = new AliTOFT0Fill(*calib.fT0Fill);
211   if (calib.fRunParams) fRunParams = new AliTOFRunParams(*calib.fRunParams);
212   if (calib.fResponseParams) fResponseParams = new AliTOFResponseParams(*calib.fResponseParams);
213 }
214
215 //____________________________________________________________________________ 
216
217 AliTOFcalib& AliTOFcalib::operator=(const AliTOFcalib &calib)
218 {
219   //TOF Calibration Class assignment operator
220
221   if (this == &calib)
222     return *this;
223   
224   TTask::operator=(calib);
225   fNChannels = calib.fNChannels;
226   fCal = calib.fCal;
227   fStatus = calib.fStatus;
228   fTOFSimToT = calib.fTOFSimToT;
229   fkValidity = calib.fkValidity;
230   fTree = calib.fTree;
231   fChain = calib.fChain;
232   fNruns = calib.fNruns;
233   fFirstRun = calib.fFirstRun;
234   fLastRun = calib.fLastRun;
235   for (Int_t iarray = 0; iarray<fNChannels; iarray++){
236     AliTOFChannelOnline * calChOnline = (AliTOFChannelOnline*)calib.fTOFCalOnline->At(iarray);
237     AliTOFChannelOffline * calChOffline = (AliTOFChannelOffline*)calib.fTOFCalOffline->At(iarray);
238     AliTOFChannelOnlineStatus * calChOnlineStPulser = (AliTOFChannelOnlineStatus*)calib.fTOFCalOnlinePulser->At(iarray);
239     AliTOFChannelOnlineStatus * calChOnlineStNoise = (AliTOFChannelOnlineStatus*)calib.fTOFCalOnlineNoise->At(iarray);
240     AliTOFChannelOnlineStatus * calChOnlineStHW = (AliTOFChannelOnlineStatus*)calib.fTOFCalOnlineHW->At(iarray);
241     fTOFCalOnline->AddAt(calChOnline,iarray);
242     fTOFCalOnlinePulser->AddAt(calChOnlineStPulser,iarray);
243     fTOFCalOnlineNoise->AddAt(calChOnlineStNoise,iarray);
244     fTOFCalOnlineHW->AddAt(calChOnlineStHW,iarray);
245     fTOFCalOffline->AddAt(calChOffline,iarray);
246   }
247
248   if (calib.fDeltaBCOffset) {
249     if (fDeltaBCOffset) *fDeltaBCOffset = *calib.fDeltaBCOffset;
250     else fDeltaBCOffset = new AliTOFDeltaBCOffset(*calib.fDeltaBCOffset);
251   }
252
253   if (calib.fCTPLatency) {
254     if (fCTPLatency) *fCTPLatency = *calib.fCTPLatency;
255     else fCTPLatency = new AliTOFCTPLatency(*calib.fCTPLatency);
256   }
257
258   if (calib.fT0Fill) {
259     if (fT0Fill) *fT0Fill = *calib.fT0Fill;
260     else fT0Fill = new AliTOFT0Fill(*calib.fT0Fill);
261   }
262   if (calib.fRunParams) {
263     if (fRunParams) *fRunParams = *calib.fRunParams;
264     else fRunParams = new AliTOFRunParams(*calib.fRunParams);
265   }
266   if (calib.fResponseParams) {
267     if (fResponseParams) *fResponseParams = *calib.fResponseParams;
268     else fResponseParams = new AliTOFResponseParams(*calib.fResponseParams);
269   }
270   fInitFlag = calib.fInitFlag;
271   fRemoveMeanT0 = calib.fRemoveMeanT0;
272   fCalibrateTOFsignal = calib.fCalibrateTOFsignal;
273   fCorrectTExp = calib.fCorrectTExp;
274
275   return *this;
276 }
277
278 //____________________________________________________________________________ 
279
280 AliTOFcalib::~AliTOFcalib()
281 {
282   //TOF Calibration Class dtor
283   if(!(AliCDBManager::Instance()->GetCacheFlag())){ // CDB objects must NOT be deleted if cache is active!
284     if (fTOFCalOnline){
285       delete fTOFCalOnline;
286     }
287     if (fTOFCalOnlinePulser){
288       delete fTOFCalOnlinePulser;
289     }
290     if (fTOFCalOnlineNoise){
291       delete fTOFCalOnlineNoise;
292     }
293     if (fTOFCalOnlineHW){
294       delete fTOFCalOnlineHW;
295     }
296     if (fTOFCalOffline){
297       delete fTOFCalOffline;
298     }
299     if (fCal){
300       delete fCal;
301     }
302     if (fStatus){
303       delete fStatus;
304     }
305     if (fConfigMap){
306       delete fConfigMap;
307     }
308     if (fDeltaBCOffset) delete fDeltaBCOffset;
309     if (fCTPLatency) delete fCTPLatency;
310     if (fT0Fill) delete fT0Fill;
311     if (fRunParams) delete fRunParams;
312   }
313   if (fTree!=0x0) delete fTree;
314   if (fChain!=0x0) delete fChain;
315
316 }
317 //_____________________________________________________________________________
318 void AliTOFcalib::CreateCalArrays(){
319
320   // creating arrays for online/offline calibration objs
321
322   fTOFCalOnline = new TObjArray(fNChannels);
323   fTOFCalOnlinePulser = new TObjArray(fNChannels);
324   fTOFCalOnlineNoise = new TObjArray(fNChannels);
325   fTOFCalOnlineHW = new TObjArray(fNChannels);
326   fTOFCalOffline = new TObjArray(fNChannels);
327   fTOFCalOnline->SetOwner();
328   fTOFCalOnlinePulser->SetOwner();
329   fTOFCalOnlineNoise->SetOwner();
330   fTOFCalOnlineHW->SetOwner();
331   fTOFCalOffline->SetOwner();
332   for (Int_t iarray = 0; iarray<fNChannels; iarray++){
333     AliTOFChannelOnline * calChOnline = new AliTOFChannelOnline();
334     AliTOFChannelOnlineStatus * calChOnlineStPulser = new AliTOFChannelOnlineStatus();
335     AliTOFChannelOnlineStatus * calChOnlineStNoise = new AliTOFChannelOnlineStatus();
336     AliTOFChannelOnlineStatus * calChOnlineStHW = new AliTOFChannelOnlineStatus();
337     AliTOFChannelOffline * calChOffline = new AliTOFChannelOffline();
338     fTOFCalOnline->AddAt(calChOnline,iarray);
339     fTOFCalOnlinePulser->AddAt(calChOnlineStPulser,iarray);
340     fTOFCalOnlineNoise->AddAt(calChOnlineStNoise,iarray);
341     fTOFCalOnlineHW->AddAt(calChOnlineStHW,iarray);
342     fTOFCalOffline->AddAt(calChOffline,iarray);
343   }
344   fCal = new AliTOFChannelOnlineArray(fNChannels);
345   fStatus = new AliTOFChannelOnlineStatusArray(fNChannels);
346 }
347 //_____________________________________________________________________________
348 void AliTOFcalib::CreateCalObjects(){
349
350   // creating arrays for online/offline calibration objs
351
352   fTOFCalOffline = new TObjArray(fNChannels);
353   fTOFCalOffline->SetOwner();
354   for (Int_t iarray = 0; iarray<fNChannels; iarray++){
355     AliTOFChannelOffline * calChOffline = new AliTOFChannelOffline();
356     fTOFCalOffline->AddAt(calChOffline,iarray);
357   }
358   fCal = new AliTOFChannelOnlineArray(fNChannels);
359   fStatus = new AliTOFChannelOnlineStatusArray(fNChannels);
360 }
361 //_____________________________________________________________________________
362 void AliTOFcalib::WriteConfigMapOnCDB(const Char_t *sel, Int_t minrun, Int_t maxrun)
363 {
364   //Write calibration parameters to the CDB
365   SetFirstRun(minrun);
366   SetLastRun(maxrun);
367   AliCDBManager *man = AliCDBManager::Instance();
368   const Char_t *sel1 = "Config" ;  // to be consistent with TOFPreprocessor
369   Char_t  out[100];
370   sprintf(out,"%s/%s",sel,sel1); 
371   AliDebug(2,Form("Writing TOF configuration map for online calib on CDB with run range [%i, %i] ",fFirstRun,fLastRun));
372   AliCDBId id(out,fFirstRun,fLastRun);
373   AliCDBMetaData *md = new AliCDBMetaData();
374   md->SetResponsible("Chiara Zampolli");
375   if (!fConfigMap) {
376     // deve uscire!!
377   }
378   man->Put(fConfigMap,id,md);
379   delete md;
380 }
381 //_____________________________________________________________________________
382
383 void AliTOFcalib::WriteConfigMapOnCDB(const Char_t *sel)
384 {
385   //Write calibration parameters to the CDB with infinite validity
386   AliCDBManager *man = AliCDBManager::Instance();
387   const Char_t *sel1 = "Config" ;  // to be consistent with TOFPreprocessor
388   Char_t  out[100];
389   sprintf(out,"%s/%s",sel,sel1); 
390   AliCDBRunRange runrange(fFirstRun,fLastRun);
391   AliDebug(2,Form("Writing TOF config map for online calib on CDB with run range [%i, %i] ",runrange.GetFirstRun(),runrange.GetLastRun()));
392   AliCDBId id(out,runrange);
393   AliCDBMetaData *md = new AliCDBMetaData();
394   md->SetResponsible("Chiara Zampolli");
395   if (!fConfigMap) {
396     // deve uscire!!
397   }
398   man->Put(fConfigMap,id,md);
399   delete md;
400 }
401 //_____________________________________________________________________________
402 void AliTOFcalib::WriteParOnlineDelayOnCDB(const Char_t *sel, Int_t minrun, Int_t maxrun)
403 {
404   //Write calibration parameters to the CDB -------> new calib objs!!!!!
405   SetFirstRun(minrun);
406   SetLastRun(maxrun);
407   AliCDBManager *man = AliCDBManager::Instance();
408   const Char_t *sel1 = "ParOnlineDelay" ;  // to be consistent with TOFPreprocessor
409   Char_t  out[100];
410   sprintf(out,"%s/%s",sel,sel1); 
411   AliDebug(2,Form("Writing TOF online calib obj on CDB with run range [%i, %i] ",fFirstRun,fLastRun));
412   AliCDBId id(out,fFirstRun,fLastRun);
413   AliCDBMetaData *md = new AliCDBMetaData();
414   md->SetResponsible("Chiara Zampolli");
415   if (!fCal) {
416     // deve uscire!!
417   }
418   man->Put(fCal,id,md);
419   delete md;
420 }
421 //_____________________________________________________________________________
422 void AliTOFcalib::WriteParOnlineStatusOnCDB(const Char_t *sel, Int_t minrun, Int_t maxrun)
423 {
424   //Write calibration parameters to the CDB -------> new calib objs!!!!!
425   SetFirstRun(minrun);
426   SetLastRun(maxrun);
427   AliCDBManager *man = AliCDBManager::Instance();
428   const Char_t *sel1 = "Status" ;  // to be consistent with TOFPreprocessor
429   Char_t  out[100];
430   sprintf(out,"%s/%s",sel,sel1); 
431   AliDebug(2,Form("Writing TOF online status calib obj on CDB with run range [%i, %i] ",fFirstRun,fLastRun));
432   AliCDBId id(out,fFirstRun,fLastRun);
433   AliCDBMetaData *md = new AliCDBMetaData();
434   md->SetResponsible("Chiara Zampolli");
435   if (!fStatus) {
436     // deve uscire!!
437   }
438   man->Put(fStatus,id,md);
439   delete md;
440 }
441 //_____________________________________________________________________________
442
443 void AliTOFcalib::WriteParOnlineDelayOnCDB(const Char_t *sel)
444 {
445   //Write calibration parameters to the CDB with infinite validity -------> new calib objs!!!!!
446   AliCDBManager *man = AliCDBManager::Instance();
447   const Char_t *sel1 = "ParOnlineDelay" ;  // to be consistent with TOFPreprocessor
448   Char_t  out[100];
449   sprintf(out,"%s/%s",sel,sel1); 
450   AliCDBRunRange runrange(fFirstRun,fLastRun);
451   AliDebug(2,Form("Writing TOF online calib obj on CDB with run range [%i, %i] ",runrange.GetFirstRun(),runrange.GetLastRun()));
452   AliCDBId id(out,runrange);
453   AliCDBMetaData *md = new AliCDBMetaData();
454   md->SetResponsible("Chiara Zampolli");
455   if (!fCal) {
456     // deve uscire!!
457   }
458   man->Put(fCal,id,md);
459   delete md;
460 }
461 //_____________________________________________________________________________
462
463 void AliTOFcalib::WriteParOnlineStatusOnCDB(const Char_t *sel)
464 {
465   //Write calibration parameters to the CDB with infinite validity -------> new calib objs!!!!!
466   AliCDBManager *man = AliCDBManager::Instance();
467   const Char_t *sel1 = "Status" ;  // to be consistent with TOFPreprocessor
468   Char_t  out[100];
469   sprintf(out,"%s/%s",sel,sel1); 
470   AliCDBRunRange runrange(fFirstRun,fLastRun);
471   AliDebug(2,Form("Writing TOF online status calib obj on CDB with run range [%i, %i] ",runrange.GetFirstRun(),runrange.GetLastRun()));
472   AliCDBId id(out,runrange);
473   AliCDBMetaData *md = new AliCDBMetaData();
474   md->SetResponsible("Chiara Zampolli");
475   if (!fStatus) {
476     // deve uscire!!
477   }
478   man->Put(fStatus,id,md);
479   delete md;
480 }
481 //_____________________________________________________________________________
482 void AliTOFcalib::WriteParOnlineOnCDB(const Char_t *sel, Int_t minrun, Int_t maxrun)
483 {
484   //Write calibration parameters to the CDB
485   SetFirstRun(minrun);
486   SetLastRun(maxrun);
487   AliCDBManager *man = AliCDBManager::Instance();
488   const Char_t *sel1 = "ParOnline" ;  // to be consistent with TOFPreprocessor
489   Char_t  out[100];
490   sprintf(out,"%s/%s",sel,sel1); 
491   AliDebug(2,Form("Writing TOF online calib obj on CDB with run range [%i, %i] ",fFirstRun,fLastRun));
492   AliCDBId id(out,fFirstRun,fLastRun);
493   AliCDBMetaData *md = new AliCDBMetaData();
494   md->SetResponsible("Chiara Zampolli");
495   if (!fTOFCalOnline) {
496     // deve uscire!!
497   }
498   man->Put(fTOFCalOnline,id,md);
499   delete md;
500 }
501 //_____________________________________________________________________________
502 void AliTOFcalib::WriteParOnlinePulserOnCDB(const Char_t *sel, Int_t minrun, Int_t maxrun)
503 {
504   //Write calibration parameters from pulser to the CDB
505   SetFirstRun(minrun);
506   SetLastRun(maxrun);
507   AliCDBManager *man = AliCDBManager::Instance();
508   const Char_t *sel1 = "Pulser" ;  // to be consistent with TOFPreprocessor
509   Char_t  out[100];
510   sprintf(out,"%s/%s",sel,sel1); 
511   AliDebug(2,Form("Writing TOF online calib obj from pulser on CDB with run range [%i, %i] ",fFirstRun,fLastRun));
512   AliCDBId id(out,fFirstRun,fLastRun);
513   AliCDBMetaData *md = new AliCDBMetaData();
514   md->SetResponsible("Chiara Zampolli");
515   if (!fTOFCalOnlinePulser) {
516     // deve uscire!!
517   }
518   man->Put(fTOFCalOnlinePulser,id,md);
519   delete md;
520 }
521 //_____________________________________________________________________________
522 void AliTOFcalib::WriteParOnlineNoiseOnCDB(const Char_t *sel, Int_t minrun, Int_t maxrun)
523 {
524   //Write calibration parameters from noise to the CDB
525   SetFirstRun(minrun);
526   SetLastRun(maxrun);
527   AliCDBManager *man = AliCDBManager::Instance();
528   const Char_t *sel1 = "Noise" ;  // to be consistent with TOFPreprocessor
529   Char_t  out[100];
530   sprintf(out,"%s/%s",sel,sel1); 
531   AliDebug(2,Form("Writing TOF online calib obj from noise on CDB with run range [%i, %i] ",fFirstRun,fLastRun));
532   AliCDBId id(out,fFirstRun,fLastRun);
533   AliCDBMetaData *md = new AliCDBMetaData();
534   md->SetResponsible("Chiara Zampolli");
535   if (!fTOFCalOnlineNoise) {
536     // deve uscire!!
537   }
538   man->Put(fTOFCalOnlineNoise,id,md);
539   delete md;
540 }
541 //_____________________________________________________________________________
542 void AliTOFcalib::WriteParOnlineHWOnCDB(const Char_t *sel, Int_t minrun, Int_t maxrun)
543 {
544   //Write calibration parameters from hardware to the CDB
545   SetFirstRun(minrun);
546   SetLastRun(maxrun);
547   AliCDBManager *man = AliCDBManager::Instance();
548   const Char_t *sel1 = "HW" ;  // to be consistent with TOFPreprocessor
549   Char_t  out[100];
550   sprintf(out,"%s/%s",sel,sel1); 
551   AliDebug(2,Form("Writing TOF online calib obj from hardware on CDB with run range [%i, %i] ",fFirstRun,fLastRun));
552   AliCDBId id(out,fFirstRun,fLastRun);
553   AliCDBMetaData *md = new AliCDBMetaData();
554   md->SetResponsible("Chiara Zampolli");
555   if (!fTOFCalOnlineHW) {
556     // deve uscire!!
557   }
558   man->Put(fTOFCalOnlineHW,id,md);
559   delete md;
560 }
561 //_____________________________________________________________________________
562
563 void AliTOFcalib::WriteParOnlineOnCDB(const Char_t *sel)
564 {
565   //Write calibration parameters to the CDB with infinite validity
566   AliCDBManager *man = AliCDBManager::Instance();
567   const Char_t *sel1 = "ParOnline" ;  // to be consistent with TOFPreprocessor
568   Char_t  out[100];
569   sprintf(out,"%s/%s",sel,sel1); 
570   AliCDBRunRange runrange(fFirstRun,fLastRun);
571   AliDebug(2,Form("Writing TOF online calib obj on CDB with run range [%i, %i] ",runrange.GetFirstRun(),runrange.GetLastRun()));
572   AliCDBId id(out,runrange);
573   AliCDBMetaData *md = new AliCDBMetaData();
574   md->SetResponsible("Chiara Zampolli");
575   if (!fTOFCalOnline) {
576     // deve uscire!!
577   }
578   man->Put(fTOFCalOnline,id,md);
579   delete md;
580 }
581 //_____________________________________________________________________________
582
583 void AliTOFcalib::WriteParOnlinePulserOnCDB(const Char_t *sel)
584 {
585   //Write calibration parameters from pulser to the CDB with infinite validity
586   AliCDBManager *man = AliCDBManager::Instance();
587   const Char_t *sel1 = "Pulser" ;  // to be consistent with TOFPreprocessor
588   Char_t  out[100];
589   sprintf(out,"%s/%s",sel,sel1); 
590   AliCDBRunRange runrange(fFirstRun,fLastRun);
591   AliDebug(2,Form("Writing TOF online calib obj from pulser on CDB with run range [%i, %i] ",runrange.GetFirstRun(),runrange.GetLastRun()));
592   AliCDBId id(out,runrange);
593   AliCDBMetaData *md = new AliCDBMetaData();
594   md->SetResponsible("Chiara Zampolli");
595   if (!fTOFCalOnlinePulser) {
596     // deve uscire!!
597   }
598   man->Put(fTOFCalOnlinePulser,id,md);
599   delete md;
600 }
601 //_____________________________________________________________________________
602
603 void AliTOFcalib::WriteParOnlineNoiseOnCDB(const Char_t *sel)
604 {
605   //Write calibration parameters from noise to the CDB with infinite validity
606   AliCDBManager *man = AliCDBManager::Instance();
607   const Char_t *sel1 = "Noise" ;  // to be consistent with TOFPreprocessor
608   Char_t  out[100];
609   sprintf(out,"%s/%s",sel,sel1); 
610   AliCDBRunRange runrange(fFirstRun,fLastRun);
611   AliDebug(2,Form("Writing TOF online calib obj from noise on CDB with run range [%i, %i] ",runrange.GetFirstRun(),runrange.GetLastRun()));
612   AliCDBId id(out,runrange);
613   AliCDBMetaData *md = new AliCDBMetaData();
614   md->SetResponsible("Chiara Zampolli");
615   if (!fTOFCalOnlineNoise) {
616     // deve uscire!!
617   }
618   man->Put(fTOFCalOnlineNoise,id,md);
619   delete md;
620 }
621 //_____________________________________________________________________________
622
623 void AliTOFcalib::WriteParOnlineHWOnCDB(const Char_t *sel)
624 {
625   //Write calibration parameters from hardware to the CDB with infinite validity
626   AliCDBManager *man = AliCDBManager::Instance();
627   const Char_t *sel1 = "HW" ;  // to be consistent with TOFPreprocessor
628   Char_t  out[100];
629   sprintf(out,"%s/%s",sel,sel1); 
630   AliCDBRunRange runrange(fFirstRun,fLastRun);
631   AliDebug(2,Form("Writing TOF online calib obj from harware on CDB with run range [%i, %i] ",runrange.GetFirstRun(),runrange.GetLastRun()));
632   AliCDBId id(out,runrange);
633   AliCDBMetaData *md = new AliCDBMetaData();
634   md->SetResponsible("Chiara Zampolli");
635   if (!fTOFCalOnlineHW) {
636     // deve uscire!!
637   }
638   man->Put(fTOFCalOnlineHW,id,md);
639   delete md;
640 }
641 //_____________________________________________________________________________
642
643 void AliTOFcalib::WriteParOfflineOnCDB(const Char_t *sel, const Char_t *validity, Int_t minrun, Int_t maxrun)
644 {
645   //Write calibration parameters to the CDB
646   SetFirstRun(minrun);
647   SetLastRun(maxrun);
648   AliCDBManager *man = AliCDBManager::Instance();
649   const Char_t *sel1 = "ParOffline" ;
650   Char_t  out[100];
651   sprintf(out,"%s/%s",sel,sel1); 
652   AliDebug(2,Form("Writing TOF offline calib obj on CDB with run range [%i, %i] ",fFirstRun,fLastRun));
653   AliCDBId id(out,fFirstRun,fLastRun);
654   AliCDBMetaData *md = new AliCDBMetaData();
655   md->SetResponsible("Chiara Zampolli");
656   md->SetComment(validity);
657   man->Put(fTOFCalOffline,id,md);
658   delete md;
659 }
660 //_____________________________________________________________________________
661
662 void AliTOFcalib::WriteParOfflineOnCDB(const Char_t *sel, const Char_t *validity)
663 {
664   //Write calibration parameters to the CDB with infinite validity
665   AliCDBManager *man = AliCDBManager::Instance();
666   const Char_t *sel1 = "ParOffline" ;
667   Char_t  out[100];
668   sprintf(out,"%s/%s",sel,sel1); 
669   AliCDBRunRange runrange(fFirstRun,fLastRun);
670   AliDebug(2,Form("Writing TOF offline calib obj on CDB with run range [%i, %i] ",runrange.GetFirstRun(),runrange.GetLastRun()));
671   AliCDBId id(out,runrange);
672   AliCDBMetaData *md = new AliCDBMetaData();
673   md->SetResponsible("Chiara Zampolli");
674   md->SetComment(validity);
675   man->Put(fTOFCalOffline,id,md);
676   delete md;
677 }
678 //_____________________________________________________________________________
679
680 Bool_t AliTOFcalib::ReadConfigMapFromCDB(const Char_t *sel, Int_t nrun)
681 {
682   //Read calibration parameters from the CDB
683   AliCDBManager *man = AliCDBManager::Instance();
684   const Char_t *sel1 = "Config" ;
685   Char_t  out[100];
686   sprintf(out,"%s/%s",sel,sel1); 
687   AliCDBEntry *entry = man->Get(out,nrun);
688   if (!entry) { 
689     AliFatal("Exiting, no CDB object (ConfigMap) found!!!");
690     exit(0);  
691   }
692   if(!entry->GetObject()){
693     AliFatal("Exiting, no CDB object (ConfigMap) found!!!");
694     exit(0);  
695   }  
696   
697   fConfigMap =(TMap*)entry->GetObject();
698
699   return kTRUE; 
700    
701 }
702 //_____________________________________________________________________________
703
704 Bool_t AliTOFcalib::ReadParOnlineDelayFromCDB(const Char_t *sel, Int_t nrun)
705 {
706   //Read calibration parameters from the CDB -------> new calib objs!!!!!
707   AliCDBManager *man = AliCDBManager::Instance();
708   const Char_t *sel1 = "ParOnlineDelay" ;
709   Char_t  out[100];
710   sprintf(out,"%s/%s",sel,sel1); 
711   AliCDBEntry *entry = man->Get(out,nrun);
712   if (!entry) { 
713     AliFatal("Exiting, no CDB object (ParOnlineDelay) found!!!");
714     exit(0);  
715   }
716   if(!entry->GetObject()){
717     AliFatal("Exiting, no CDB object (ParOnlineDelay) found!!!");
718     exit(0);  
719   }  
720   
721   fCal =(AliTOFChannelOnlineArray*)entry->GetObject();
722
723   return kTRUE; 
724    
725 }
726 //_____________________________________________________________________________
727
728 Bool_t AliTOFcalib::ReadParOnlineStatusFromCDB(const Char_t *sel, Int_t nrun)
729 {
730   //Read calibration parameters from the CDB -------> new calib objs!!!!!
731   AliCDBManager *man = AliCDBManager::Instance();
732   const Char_t *sel1 = "Status" ;
733   Char_t  out[100];
734   sprintf(out,"%s/%s",sel,sel1); 
735   AliCDBEntry *entry = man->Get(out,nrun);
736   if (!entry) { 
737     AliFatal("Exiting, no CDB object (Status) found!!!");
738     exit(0);  
739   }
740   if(!entry->GetObject()){
741     AliFatal("Exiting, no CDB object (Status) found!!!");
742     exit(0);  
743   }  
744   
745   fStatus =(AliTOFChannelOnlineStatusArray*)entry->GetObject();
746
747   return kTRUE; 
748    
749 }
750 //_____________________________________________________________________________
751
752 Bool_t AliTOFcalib::ReadParOnlineFromCDB(const Char_t *sel, Int_t nrun)
753 {
754   //Read calibration parameters from the CDB
755   AliCDBManager *man = AliCDBManager::Instance();
756   const Char_t *sel1 = "ParOnline" ;
757   Char_t  out[100];
758   sprintf(out,"%s/%s",sel,sel1); 
759   AliCDBEntry *entry = man->Get(out,nrun);
760   if (!entry) { 
761     AliFatal("Exiting, no CDB object (ParOnline) found!!!");
762     exit(0);  
763   }
764   if(!entry->GetObject()){
765     AliFatal("Exiting, no CDB object (ParOnline) found!!!");
766     exit(0);  
767   }  
768   
769   fTOFCalOnline =(TObjArray*)entry->GetObject();
770
771   return kTRUE; 
772    
773 }
774 //_____________________________________________________________________________
775
776 Bool_t AliTOFcalib::ReadParOnlinePulserFromCDB(const Char_t *sel, Int_t nrun)
777 {
778   //Read calibration parameters from pulser from the CDB
779   AliCDBManager *man = AliCDBManager::Instance();
780   const Char_t *sel1 = "Pulser" ;
781   Char_t  out[100];
782   sprintf(out,"%s/%s",sel,sel1); 
783   AliCDBEntry *entry = man->Get(out,nrun);
784   if (!entry) { 
785     AliFatal("Exiting, no CDB object (Pulser) found!!!");
786     exit(0);  
787   }
788   if(!entry->GetObject()){
789     AliFatal("Exiting, no CDB object (Pulser) found!!!");
790     exit(0);  
791   }  
792   
793   fTOFCalOnlinePulser =(TObjArray*)entry->GetObject();
794
795   return kTRUE; 
796    
797 }
798 //_____________________________________________________________________________
799
800 Bool_t AliTOFcalib::ReadParOnlineNoiseFromCDB(const Char_t *sel, Int_t nrun)
801 {
802   //Read calibration parameters from noise from the CDB
803   AliCDBManager *man = AliCDBManager::Instance();
804   const Char_t *sel1 = "Noise" ;
805   Char_t  out[100];
806   sprintf(out,"%s/%s",sel,sel1); 
807   AliCDBEntry *entry = man->Get(out,nrun);
808   if (!entry) { 
809     AliFatal("Exiting, no CDB object (Noise) found!!!");
810     exit(0);  
811   }
812   if(!entry->GetObject()){
813     AliFatal("Exiting, no CDB object (Noise) found!!!");
814     exit(0);  
815   }  
816   
817   fTOFCalOnlineNoise =(TObjArray*)entry->GetObject();
818
819   return kTRUE; 
820    
821 }
822 //_____________________________________________________________________________
823
824 Bool_t AliTOFcalib::ReadParOnlineHWFromCDB(const Char_t *sel, Int_t nrun)
825 {
826   //Read calibration parameters from hardware from the CDB
827   AliCDBManager *man = AliCDBManager::Instance();
828   const Char_t *sel1 = "HW" ;
829   Char_t  out[100];
830   sprintf(out,"%s/%s",sel,sel1); 
831   AliCDBEntry *entry = man->Get(out,nrun);
832   if (!entry) { 
833     AliFatal("Exiting, no CDB object (HW map) found!!!");
834     exit(0);  
835   }
836   if(!entry->GetObject()){
837     AliFatal("Exiting, no CDB object (HW map) found!!!");
838     exit(0);  
839   }  
840   
841   fTOFCalOnlineHW =(TObjArray*)entry->GetObject();
842
843   return kTRUE; 
844    
845 }
846 //_____________________________________________________________________________
847
848 Bool_t AliTOFcalib::ReadParOfflineFromCDB(const Char_t *sel, Int_t nrun)
849 {
850   //Read calibration parameters from the CDB
851   AliCDBManager *man = AliCDBManager::Instance();
852   const Char_t *sel1 = "ParOffline" ;
853   Char_t  out[100];
854   sprintf(out,"%s/%s",sel,sel1); 
855   AliCDBEntry *entry = man->Get(out,nrun);
856   if (!entry) { 
857     AliFatal("Exiting, no CDB object (ParOffline) found!!!");
858     exit(0);  
859   }
860   if(!entry->GetObject()){
861     AliFatal("Exiting, no CDB object (ParOffline) found!!!");
862     exit(0);  
863   }  
864   AliCDBMetaData * md = entry->GetMetaData();
865   fkValidity = md->GetComment();  
866   fTOFCalOffline =(TObjArray*)entry->GetObject();
867
868   return kTRUE; 
869    
870 }
871 //_____________________________________________________________________________
872 void AliTOFcalib::WriteSimHistoOnCDB(const Char_t *sel, Int_t minrun, Int_t maxrun, TH1F *histo){
873   //Write Sim miscalibration parameters to the CDB
874
875   fTOFSimToT=histo;
876   AliCDBManager *man = AliCDBManager::Instance();
877   const Char_t *sel1 = "SimHisto" ;
878   Char_t  out[100];
879   sprintf(out,"%s/%s",sel,sel1); 
880   AliCDBMetaData *mdhisto = new AliCDBMetaData();
881   mdhisto->SetResponsible("Chiara Zampolli");
882   AliCDBId id(out,minrun,maxrun);
883   man->Put(fTOFSimToT,id,mdhisto);
884   delete mdhisto;
885 }
886 //_____________________________________________________________________________
887 Bool_t AliTOFcalib::ReadSimHistoFromCDB(const Char_t *sel, Int_t nrun)
888 {
889   //Read miscalibration parameters from the CDB
890   AliCDBManager *man = AliCDBManager::Instance();
891
892   // The Tot Histo
893
894   const Char_t *sel1 = "SimHisto" ;
895   Char_t  out[100];
896   sprintf(out,"%s/%s",sel,sel1); 
897   AliCDBEntry *entry = man->Get(out,nrun);
898   if (!entry) { 
899     AliFatal("Exiting, no CDB object (SimHisto) found!!!");
900     exit(0);  
901   }
902   if(!entry->GetObject()){
903     AliFatal("Exiting, no CDB object (SimHisto) found!!!");
904     exit(0);  
905   }  
906   TH1F *histo =(TH1F*)entry->GetObject();
907   fTOFSimToT=histo;
908   return kTRUE;
909 }
910 //_____________________________________________________________________________
911 void AliTOFcalib::WriteRecParOnCDB(const Char_t *sel, Int_t minrun, Int_t maxrun, AliTOFRecoParam *param){
912   //Write reconstruction parameters to the CDB
913
914   AliCDBManager *man = AliCDBManager::Instance();
915   AliCDBMetaData *md = new AliCDBMetaData();
916   md->SetResponsible("Silvia Arcelli");
917   const Char_t *sel1 = "RecoParam" ;
918   Char_t  out[100];
919   sprintf(out,"%s/%s",sel,sel1); 
920   AliCDBId id(out,minrun,maxrun);
921   man->Put(param,id,md);
922   delete md;
923 }
924 //_____________________________________________________________________________
925 AliTOFRecoParam * AliTOFcalib::ReadRecParFromCDB(const Char_t *sel, Int_t nrun)
926 {
927   //Read reconstruction parameters from the CDB
928   AliCDBManager *man = AliCDBManager::Instance();
929   const Char_t *sel1 = "RecoParam" ;
930   Char_t  out[100];
931   sprintf(out,"%s/%s",sel,sel1); 
932   AliCDBEntry *entry = man->Get(out,nrun);
933   if (!entry) { 
934     AliFatal("Exiting, no CDB object (RecoParam) found!!!");
935     exit(0);  
936   }  
937   if(!entry->GetObject()){
938     AliFatal("Exiting, no CDB object (RecoParam) found!!!");
939     exit(0);  
940   }  
941
942   AliTOFRecoParam *param=(AliTOFRecoParam*)entry->GetObject();
943   return param;
944 }
945 //-----------------------------------------------------------------------------
946 // Calibration methods
947 //-----------------------------------------------------------------------------
948 void AliTOFcalib::CreateTreeFromCDB(Int_t minrun, Int_t maxrun){
949
950   // creating the chain with the trees for calibration
951   // collecting them from reference data 
952   // from minrun to maxrun
953
954   Float_t p[CHENTRIESSMALL];
955   Int_t nentries;
956   fTree = new TTree("TOFCalib","Tree for TOF Calibration");
957   fTree->Branch("nentries",&nentries,"nentries/I");
958   fTree->Branch("TOFentries",p,"TOFentries[nentries]/F");
959   AliCDBManager *man = AliCDBManager::Instance();
960   AliCDBStorage *aStorage = man->GetStorage("local://$ALICE_ROOT/OCDB");
961   for (Int_t irun = minrun;irun<=maxrun;irun++){
962     AliCDBEntry *entry = aStorage->Get("TOF/RefData/TreeForCalib",irun);
963     if (!entry){
964       AliInfo(Form("No entry found for run %i",irun));
965     }
966     else{
967       TTree *tree = new TTree();
968       tree = (TTree*)entry->GetObject();
969       tree->SetBranchAddress("nentries",&nentries);
970       tree->SetBranchAddress("TOFentries",p);      
971       fTree->CopyEntries(tree);
972       delete tree;
973       fNruns++;
974     }
975   }
976   AliInfo(Form("Number of runs being analyzed %i",fNruns));
977 }
978 //-----------------------------------------------------------------------------
979 void AliTOFcalib::CreateTreeFromGrid(Int_t minrun, Int_t maxrun){
980
981   // creating the chain with the trees for calibration
982   // collecting them from the Grid 
983   // from minrun to maxrun
984
985   Float_t p[CHENTRIESSMALL];
986   Int_t nentries;
987   fTree = new TTree("TOFCalib","Tree for TOF Calibration");
988   fTree->SetDirectory(0);
989   fTree->Branch("nentries",&nentries,"nentries/I");
990   fTree->Branch("TOFentries",p,"TOFentries[nentries]/F");
991   AliInfo("connected to alien");
992   TGrid::Connect("alien://");
993   
994   Char_t filename[100];
995   for (Int_t irun = minrun;irun<=maxrun;irun++){
996     sprintf(filename,"alien:///alice/cern.ch/user/c/czampolli/TOFCalibReference_%i.root",irun);
997     TFile *filegrid = TFile::Open(filename,"READ");
998     TTree *tree = (TTree*)filegrid->Get("T");
999     tree->SetBranchAddress("nentries",&nentries);
1000     tree->SetBranchAddress("TOFentries",p);      
1001     fTree->CopyEntries(tree);
1002     delete tree;
1003     fNruns++;    
1004   }
1005   
1006   AliInfo(Form("Number of runs being analyzed %i",fNruns));
1007 }
1008 //-----------------------------------------------------------------------------
1009 void AliTOFcalib::CreateTreeFromFile(Int_t minrun, Int_t maxrun){
1010
1011   // creating the tree with the trees for calibration
1012   // collecting them from reference data (from file)
1013   // from minrun to maxrun
1014
1015   Float_t p[CHENTRIESSMALL];
1016   Int_t nentries;
1017   fTree = new TTree("TOFCalib","Tree for TOF Calibration");
1018   fTree->SetDirectory(0);
1019   fTree->Branch("nentries",&nentries,"nentries/I");
1020   fTree->Branch("TOFentries",p,"TOFentries[nentries]/F");
1021   Char_t filename[100];
1022   for (Int_t irun = minrun;irun<=maxrun;irun++){
1023     sprintf(filename,"$ALICE_ROOT/TOF/RefData/TreeForCalib/fileout_%i.root",irun);
1024     TFile *file = new TFile(filename,"READ");
1025     TTree *tree = (TTree*)file->Get("T");
1026     tree->SetBranchAddress("nentries",&nentries);
1027     tree->SetBranchAddress("TOFentries",p);      
1028     fTree->CopyEntries(tree);
1029     delete tree;
1030     delete file;
1031     file = 0x0;
1032     fNruns++;
1033   }
1034
1035   AliInfo(Form("Number of runs being analyzed %i",fNruns));
1036 }
1037 //-----------------------------------------------------------------------------
1038 void AliTOFcalib::CreateChainFromGrid(Int_t minrun, Int_t maxrun){
1039
1040   // creating the chain with the trees for calibration
1041   // collecting them from the Grid 
1042   // from minrun to maxrun
1043
1044   fChain = new TChain("T");
1045   AliInfo("connected to alien");
1046   TGrid::Connect("alien://");
1047   
1048   Char_t filename[100];
1049   for (Int_t irun = minrun;irun<=maxrun;irun++){
1050     sprintf(filename,"alien:///alice/cern.ch/user/c/czampolli/TOFCalibReference_%i.root",irun);
1051     fChain->Add(filename);
1052     fNruns++;    
1053   }
1054   
1055   AliInfo(Form("Number of runs being analyzed %i",fNruns));
1056 }
1057 //-----------------------------------------------------------------------------
1058 Int_t AliTOFcalib::Calibrate(Int_t ichmin, Int_t ichmax, Option_t *optionSave, Option_t *optionFit){
1059
1060   // calibrating summing more than one channels
1061   // computing calibration parameters
1062   // Returning codes:
1063   // 0 -> everything was ok
1064   // 1 -> no tree for calibration found
1065   // 2 -> not enough statistics to perform calibration
1066   // 3 -> problems with arrays
1067   
1068   TH1::AddDirectory(0);
1069
1070   AliInfo(Form("*** Calibrating Histograms %s, summing more channels, from channel %i, to channel %i, storing Calib Pars in channel %i", GetName(),ichmin,ichmax,ichmin)) ; 
1071   AliInfo(Form("Option for Saving histos = %s",optionSave )) ; 
1072   AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ; 
1073
1074   Float_t p[CHENTRIESSMALL];
1075   Int_t nentries;
1076   //fTree->SetBranchAddress("nentries",&nentries);
1077   //fTree->SetBranchAddress("TOFentries",p);
1078   fChain->SetBranchAddress("nentries",&nentries);
1079   fChain->SetBranchAddress("TOFentries",p);
1080
1081   Float_t ntracksTotalmean =0;
1082   for (Int_t i=ichmin; i<ichmax; i++){
1083     Int_t ientry = -1;
1084     for (Int_t irun=0;irun<fNruns;irun++){
1085       ientry = i+irun*fNChannels;
1086       //fTree->GetEntry(ientry);
1087       fChain->GetEntry(ientry);
1088       Int_t ntracksRun=nentries/3;
1089       ntracksTotalmean+=ntracksRun;
1090     }
1091   }
1092   
1093   if (ntracksTotalmean < MEANENTRIES) {
1094     AliInfo(Form(" Too small mean number of entires per channel (mean number = %f) not calibrating and exiting.....",ntracksTotalmean));
1095     return 2;
1096   }
1097
1098   //filling ToT and Time arrays
1099
1100   Int_t nbinToT = 100;  // ToT bin width in Profile = 48.8 ps 
1101   Float_t minToT = 0;   // ns
1102   Float_t maxToT = 4.88;  // ns
1103
1104   TH1F *hToT = new TH1F("htot","htot",nbinToT, minToT, maxToT);
1105   TH1F *hdeltaTime = new TH1F("hdeltaTime","hdeltaTime",200,2,4);
1106   Int_t ntracksTotal = 0;
1107   Int_t ntracksRun = 0;
1108   Double_t binsProfile[101]; // sized larger than necessary, the correct 
1109                              // dim being set in the booking of the profile
1110   Int_t nusefulbins=0;
1111   Float_t meantime=0;
1112   for (Int_t i = ichmin;i<ichmax;i++){
1113     Int_t ientry = -1;
1114     for (Int_t irun=0;irun<fNruns;irun++){
1115       ientry = i+irun*fNChannels;
1116       //fTree->GetEntry(ientry);
1117       fChain->GetEntry(ientry);
1118       ntracksTotal+=nentries/3;
1119       ntracksRun=nentries/3;
1120       AliDebug(2,Form("run %i, channel %i, nentries = %i, ntracks = %i",irun,i,nentries, ntracksRun));
1121       for (Int_t j=0;j<ntracksRun;j++){
1122         Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1123         Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1124         Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1125         Float_t tot = p[idxexToT];
1126         hdeltaTime->Fill(p[idxexTime]-p[idxexExTime]);
1127         meantime+=p[idxexTime]-p[idxexExTime];
1128         hToT->Fill(tot);
1129       }
1130     }
1131   }
1132   nusefulbins = FindBins(hToT,&binsProfile[0]);
1133   meantime/=ntracksTotal;
1134   AliDebug(2, Form("meantime = %f",meantime));
1135   
1136   for (Int_t j=1;j<=nusefulbins;j++) {
1137     AliDebug(2,Form(" summing channels from %i to %i, nusefulbins = %i, bin %i = %f",ichmin,ichmax,nusefulbins,j,binsProfile[j])); 
1138   }
1139
1140   TProfile* hSlewingProf = new TProfile("hSlewingProf", "hSlewingProf",nusefulbins, binsProfile, "G");  // CHECK THE BUILD OPTION, PLEASE!!!!!!
1141   TH2F * htimetot = new TH2F("htimetot","htimetot",nbinToT, minToT, maxToT,600,-5,10);
1142
1143   for (Int_t irun=0;irun<fNruns;irun++){
1144     Int_t ientry = -1;
1145     for (Int_t i=ichmin; i<ichmax; i++){
1146       ientry = i+irun*fNChannels;
1147       //fTree->GetEntry(ientry);
1148       fChain->GetEntry(ientry);
1149       ntracksRun=nentries/3;
1150       for (Int_t j=0;j<ntracksRun;j++){
1151         Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1152         Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1153         Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1154         Float_t tot = p[idxexToT];
1155         Float_t time = p[idxexTime]-p[idxexExTime];
1156         AliDebug (2, Form("track = %i, time = %f, tot = %f, time-meantime = %f",j,time, tot, time-meantime));
1157         hSlewingProf->Fill(tot,time);  // if meantime is not used, the fill may be moved in the loop above
1158         htimetot->Fill(tot,time-meantime);  // if meantime is not used, the fill may be moved in the loop above
1159       }
1160     }
1161   }
1162
1163   hSlewingProf->Fit("pol5",optionFit,"",0,4);
1164   TF1 * calibfunc = (TF1*)hSlewingProf->GetFunction("pol5");
1165   Float_t par[6];    
1166   for(Int_t kk=0;kk<6;kk++){
1167     par[kk]=calibfunc->GetParameter(kk);
1168     AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
1169   }
1170
1171   if(strstr(optionSave,"save")){
1172     TFile * fileProf = new TFile("TOFCalibSave.root","recreate");
1173     fileProf->cd(); 
1174     TString profName=Form("Profile%06i_%06i",ichmin,ichmax);
1175     TString timeTotName=Form("TimeTot%06i_%06i",ichmin,ichmax);
1176     TString totName=Form("Tot%06i_%06i",ichmin,ichmax);
1177     TString deltaName=Form("Delta%06i_%06i",ichmin,ichmax);
1178     hSlewingProf->Write(profName);
1179     htimetot->Write(timeTotName);
1180     hToT->Write(totName);
1181     hdeltaTime->Write(deltaName);
1182     fileProf->Close();
1183     delete fileProf;
1184     fileProf=0x0;
1185   }
1186
1187   delete hToT;
1188   hToT=0x0;
1189   delete hSlewingProf;
1190   hSlewingProf=0x0;
1191   delete htimetot;
1192   htimetot=0x0;
1193   delete hdeltaTime;
1194   hdeltaTime=0x0;
1195
1196   AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(ichmin);
1197   calChannel->SetSlewPar(par);
1198   WriteParOfflineOnCDB("TOF/Calib","valid");
1199   return 0;
1200 }
1201 //----------------------------------------------------------------------------
1202 Int_t AliTOFcalib::Calibrate(Int_t i, Option_t *optionSave, Option_t *optionFit){
1203
1204   // computing calibration parameters for channel i
1205   // Returning codes:
1206   // 0 -> everything was ok
1207   // 1 -> no tree for calibration found
1208   // 2 -> not enough statistics to perform calibration
1209   // 3 -> problems with arrays
1210
1211   TH1::AddDirectory(0);
1212   
1213   AliInfo(Form("*** Calibrating Histograms (one channel) %s", GetName())) ; 
1214   AliInfo(Form("Option for Saving histos = %s",optionSave )) ; 
1215   AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ; 
1216
1217   Float_t p[MAXCHENTRIESSMALL];
1218   Int_t nentries;
1219   //fTree->SetBranchAddress("nentries",&nentries);
1220   //fTree->SetBranchAddress("TOFentries",p);
1221   fChain->SetBranchAddress("nentries",&nentries);
1222   fChain->SetBranchAddress("TOFentries",p);
1223
1224   Float_t ntracksTotal =0;
1225   for (Int_t irun=0;irun<fNruns;irun++){
1226     Int_t ientry = -1;
1227     ientry = i+irun*fNChannels;
1228     //fTree->GetEntry(ientry);
1229     fChain->GetEntry(ientry);
1230     ntracksTotal+=nentries/3;    
1231   }
1232   
1233   if (ntracksTotal < MEANENTRIES) {  
1234     AliInfo(Form(" Too small mean number of entires per channel (mean number = %f) not calibrating and exiting.....",ntracksTotal));
1235     return 2;
1236   }
1237
1238   //filling ToT and Time arrays
1239
1240   Int_t nbinToT = 100;  // ToT bin width in Profile = 48.8 ps 
1241   Float_t minToT = 0;   // ns
1242   Float_t maxToT = 4.88;  // ns
1243
1244   TH1F *hToT = new TH1F("htot","htot",nbinToT, minToT, maxToT);
1245   TH1F *hdeltaTime = new TH1F("hdeltaTime","hdeltaTime",200,2,4);
1246   Int_t ntracksRun = 0;
1247   Double_t binsProfile[101]; // sized larger than necessary, the correct 
1248                              // dim being set in the booking of the profile
1249   Int_t nusefulbins=0;
1250   Float_t meantime=0;
1251   for (Int_t irun=0;irun<fNruns;irun++){
1252     Int_t ientry = -1;
1253     ientry = i+irun*fNChannels;
1254     //fTree->GetEntry(ientry);
1255     fChain->GetEntry(ientry);
1256     ntracksRun=nentries/3;
1257     AliDebug(2,Form("run %i, channel %i, nentries = %i, ntracksRun = %i",irun, i ,nentries, ntracksRun));
1258     for (Int_t j=0;j<ntracksRun;j++){
1259       Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1260       Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1261       Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1262       Float_t tot = p[idxexToT];
1263       meantime+=p[idxexTime]-p[idxexExTime];
1264       hdeltaTime->Fill(p[idxexTime]-p[idxexExTime]);
1265       hToT->Fill(tot);
1266     }
1267   }
1268
1269   nusefulbins = FindBins(hToT,&binsProfile[0]);
1270   meantime/=ntracksTotal;
1271   AliDebug(2,Form("meantime = %f",meantime));
1272   
1273   for (Int_t j=1;j<=nusefulbins;j++) {
1274     AliDebug(2,Form(" channel %i, nusefulbins = %i, bin %i = %f",i,nusefulbins,j,binsProfile[j])); 
1275   }
1276
1277   TProfile* hSlewingProf = new TProfile("hSlewingProf", "hSlewingProf",nusefulbins, binsProfile, "G");  // CHECK THE BUILD OPTION, PLEASE!!!!!!
1278   TH2F * htimetot = new TH2F("htimetot","htimetot",nbinToT, minToT, maxToT,600,-5,10);
1279   for (Int_t irun=0;irun<fNruns;irun++){
1280     Int_t ientry = -1;
1281     ientry = i+irun*fNChannels;
1282     //fTree->GetEntry(ientry);
1283     fChain->GetEntry(ientry);
1284     ntracksRun=nentries/3;
1285     for (Int_t j=0;j<ntracksRun;j++){
1286       Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1287       Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1288       Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1289       Float_t tot = p[idxexToT];
1290       Float_t time = p[idxexTime]-p[idxexExTime];
1291       AliDebug (2,Form("track = %i, time = %f, tot = %f, time-meantime = %f",j,time, tot, time-meantime));
1292       hSlewingProf->Fill(tot,time);  // if meantime is not used, the fill may be moved in the loop above
1293       htimetot->Fill(tot,time-meantime);  // if meantime is not used, the fill may be moved in the loop above
1294     }
1295   }
1296
1297   hSlewingProf->Fit("pol5",optionFit,"",0,4);
1298   TF1 * calibfunc = (TF1*)hSlewingProf->GetFunction("pol5");
1299   Float_t par[6];    
1300   for(Int_t kk=0;kk<6;kk++){
1301     par[kk]=calibfunc->GetParameter(kk);
1302     AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
1303   }
1304
1305
1306   if(strstr(optionSave,"save")){
1307     TFile * fileProf = new TFile("TOFCalibSave.root","recreate");
1308     fileProf->cd();   
1309     TString profName=Form("Profile%06i",i);
1310     TString timeTotName=Form("TimeTot%06i",i);
1311     TString totName=Form("Tot%06i",i);
1312     TString deltaName=Form("Delta%06i",i);
1313     hSlewingProf->Write(profName);
1314     htimetot->Write(timeTotName);
1315     hToT->Write(totName);
1316     hdeltaTime->Write(deltaName);
1317     fileProf->Close();
1318     delete fileProf;
1319     fileProf=0x0;
1320   }
1321
1322   delete hToT;
1323   hToT=0x0; 
1324   delete hSlewingProf;
1325   hSlewingProf=0x0;
1326   delete htimetot;
1327   htimetot=0x0;
1328   delete hdeltaTime;
1329   hdeltaTime=0x0;
1330
1331   AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(i);
1332   calChannel->SetSlewPar(par);
1333   WriteParOfflineOnCDB("TOF/Calib","valid");
1334   return 0;
1335 }
1336 //----------------------------------------------------------------------------
1337 Int_t AliTOFcalib::Calibrate(Int_t nch, Int_t *ch, Option_t *optionSave, Option_t *optionFit){
1338
1339   // calibrating an array of channels
1340   // computing calibration parameters
1341   // Returning codes:
1342   // 0 -> everything was ok
1343   // 1 -> no tree for calibration found
1344   // 2 -> not enough statistics to perform calibration
1345   // 3 -> problems with arrays
1346   
1347   TH1::AddDirectory(0);
1348
1349   AliInfo(Form("*** Calibrating Histograms %s, number of channels = %i", GetName(),nch)) ; 
1350   AliInfo(Form("Option for Saving histos = %s",optionSave )) ; 
1351   AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ; 
1352   for (Int_t ich=0; ich<nch; ich++){
1353     Int_t i = ch[ich];
1354     AliInfo(Form("Calibrating channel = %i",i )) ; 
1355   }
1356   Float_t p[MAXCHENTRIESSMALL];
1357   Int_t nentries;
1358   //fTree->SetBranchAddress("nentries",&nentries);
1359   //fTree->SetBranchAddress("TOFentries",p);
1360   fChain->SetBranchAddress("nentries",&nentries);
1361   fChain->SetBranchAddress("TOFentries",p);
1362
1363   Float_t ntracksTotalmean =0;
1364   for (Int_t ich=0; ich<nch; ich++){
1365     Int_t ientry = -1;
1366       Int_t i = ch[ich];
1367       for (Int_t irun=0;irun<fNruns;irun++){
1368       ientry = i+irun*fNChannels;
1369       //fTree->GetEntry(ientry);
1370       fChain->GetEntry(ientry);
1371       ntracksTotalmean+=nentries/3;
1372     }
1373   }
1374
1375   ntracksTotalmean/=nch;
1376   if (ntracksTotalmean < MEANENTRIES) { 
1377     AliInfo(Form(" Too small mean number of entires per channel (mean number = %f) not calibrating and exiting.....",ntracksTotalmean));
1378     return 2;
1379   }
1380
1381   //filling ToT and Time arrays
1382
1383   Int_t nbinToT = 100;  // ToT bin width in Profile = 48.8 ps 
1384   Float_t minToT = 0;   // ns
1385   Float_t maxToT = 4.88;  // ns
1386   TFile * fileProf=0x0;
1387   if(strstr(optionSave,"save")){
1388     fileProf = new TFile("TOFCalibSave.root","recreate");
1389   }
1390   for (Int_t ich=0; ich<nch; ich++) {
1391     TH1F *hToT = new TH1F("htot","htot",nbinToT, minToT, maxToT);
1392     TH1F *hdeltaTime = new TH1F("hdeltaTime","hdeltaTime",200,2,4);
1393     Double_t binsProfile[101]; // sized larger than necessary, the correct 
1394     // dim being set in the booking of the profile
1395     TH2F * htimetot = new TH2F("htimetot","htimetot",nbinToT, minToT, maxToT,600,-5,10);
1396     Int_t ntracksTotal = 0;
1397     Int_t ntracksRun = 0;
1398     Int_t nusefulbins=0;
1399     Float_t meantime=0;
1400     Int_t i=-1;
1401     for (Int_t irun=0;irun<fNruns;irun++){
1402       i = ch[ich]+irun*fNChannels;
1403       AliDebug(2,Form("Calibrating channel %i",i));
1404       //fTree->GetEntry(i);
1405       fChain->GetEntry(i);
1406       ntracksTotal+=nentries/3;
1407     }
1408     if (ntracksTotal < MEANENTRIES) {
1409       AliInfo(Form(" Too small mean number of entires in channel %i (number of tracks = %d), not calibrating channel and continuing.....",i,ntracksTotal));
1410       continue;
1411     }
1412   
1413     for (Int_t irun=0;irun<fNruns;irun++){
1414       i = ch[ich]+irun*fNChannels;
1415       //fTree->GetEntry(i);
1416       fChain->GetEntry(i);
1417       ntracksRun=nentries/3;
1418       for (Int_t j=0;j<ntracksRun;j++){
1419         Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1420         Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1421         Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1422         Float_t tot = p[idxexToT];
1423         hdeltaTime->Fill(p[idxexTime]-p[idxexExTime]);
1424         meantime+=p[idxexTime]-p[idxexExTime];
1425         hToT->Fill(tot);
1426       }
1427     }
1428
1429     nusefulbins = FindBins(hToT,&binsProfile[0]);
1430     meantime/=ntracksTotal;
1431     for (Int_t j=1;j<=nusefulbins;j++) {
1432       AliDebug(2,Form(" channel %i, nusefulbins = %i, bin %i = %f",i,nusefulbins,j,binsProfile[j])); 
1433     }
1434
1435     TProfile* hSlewingProf = new TProfile("hSlewingProf", "hSlewingProf",nusefulbins, binsProfile, "G");  // CHECK THE BUILD OPTION, PLEASE!!!!!!
1436     for (Int_t irun=0;irun<fNruns;irun++){
1437       i = ch[ich]+irun*fNChannels;
1438       //fTree->GetEntry(i);
1439       fChain->GetEntry(i);
1440       ntracksRun=nentries/3;
1441       for (Int_t j=0;j<ntracksRun;j++){
1442         Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1443         Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1444         Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1445         Float_t tot = p[idxexToT];
1446         Float_t time = p[idxexTime]-p[idxexExTime];
1447         AliDebug(2,Form("track = %i, time = %f, tot = %f, time-meantime = %f",j,time, tot, time-meantime));
1448         hSlewingProf->Fill(tot,time);  // if meantime is not used, the fill may be moved in the loop above
1449         htimetot->Fill(tot,time-meantime);  // if meantime is not used, the fill may be moved in the loop above
1450       }
1451     }
1452     
1453     hSlewingProf->Fit("pol5",optionFit,"",1,4);
1454     TF1 * calibfunc = (TF1*)hSlewingProf->GetFunction("pol5");
1455     Float_t par[6];    
1456     for(Int_t kk=0;kk<6;kk++){
1457       par[kk]=calibfunc->GetParameter(kk);
1458       AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
1459     }
1460     
1461     if(strstr(optionSave,"save") && fileProf){
1462       TString profName=Form("Profile%06i",i);
1463       TString timeTotName=Form("TimeTot%06i",i);
1464       TString totName=Form("Tot%06i",i);
1465       TString deltaName=Form("Delta%06i",i);
1466       fileProf->cd();
1467       hSlewingProf->Write(profName);
1468       htimetot->Write(timeTotName);
1469       hToT->Write(totName);
1470       hdeltaTime->Write(deltaName);
1471     }
1472
1473     AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(i);
1474     calChannel->SetSlewPar(par);
1475     delete hToT;
1476     hToT=0x0;
1477     delete hSlewingProf;
1478     hSlewingProf=0x0;
1479     delete htimetot;
1480     htimetot=0x0;
1481     delete hdeltaTime;
1482     hdeltaTime=0x0;
1483   }
1484
1485   if(strstr(optionSave,"save") && fileProf){
1486     fileProf->Close();
1487     delete fileProf;
1488     fileProf=0x0;
1489   }
1490   WriteParOfflineOnCDB("TOF/Calib","valid");
1491
1492   return 0;
1493 }
1494 //----------------------------------------------------------------------------
1495 Int_t AliTOFcalib::CalibrateFromProfile(Int_t ich, Option_t *optionSave, Option_t *optionFit){
1496
1497   // computing calibration parameters using the old profiling algo
1498   // Returning codes:
1499   // 0 -> everything was ok
1500   // 1 -> no tree for calibration found
1501   // 2 -> not enough statistics to perform calibration
1502   // 3 -> problems with arrays
1503
1504   TH1::AddDirectory(0);
1505
1506   AliInfo(Form("*** Calibrating Histograms From Profile %s", GetName())) ; 
1507   AliInfo(Form("Option for Saving histos = %s",optionSave )) ; 
1508   AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ; 
1509   Float_t p[MAXCHENTRIESSMALL];
1510   Int_t nentries;
1511   Int_t ntracksTotal=0;
1512   //fTree->SetBranchAddress("nentries",&nentries);
1513   //fTree->SetBranchAddress("TOFentries",p);
1514   fChain->SetBranchAddress("nentries",&nentries);
1515   fChain->SetBranchAddress("TOFentries",p);
1516
1517   for (Int_t irun=0;irun<fNruns;irun++){
1518     Int_t i = ich+irun*fNChannels;
1519     //fTree->GetEntry(i);
1520     fChain->GetEntry(i);
1521     ntracksTotal+=nentries/3;
1522   }
1523
1524   if (ntracksTotal < MEANENTRIES) {  
1525     AliInfo(Form(" Too small mean number of entires per channel (mean number = %d) not calibrating and exiting.....",ntracksTotal));
1526     return 2;
1527   }
1528
1529   TH1F * hProf = new TH1F();
1530   hProf = Profile(ich);
1531   hProf->Fit("pol5",optionFit,"",0,4);
1532   TF1 * calibfunc = (TF1*)hProf->GetFunction("pol5");
1533   Float_t par[6];    
1534   for(Int_t kk=0;kk<6;kk++){
1535     par[kk]=calibfunc->GetParameter(kk);
1536     AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
1537   }
1538
1539   if(strstr(optionSave,"save")){
1540     TFile * fileProf = new TFile("TOFCalibSave.root","recreate");
1541     fileProf->cd(); 
1542     TString profName=Form("Profile%06i",ich);
1543     hProf->Write(profName);
1544     fileProf->Close();
1545     delete fileProf;
1546     fileProf=0x0;
1547   }
1548
1549   delete hProf;
1550   hProf=0x0;
1551   AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(ich);
1552   calChannel->SetSlewPar(par);
1553   WriteParOfflineOnCDB("TOF/Calib","valid");
1554   return 0;
1555 }
1556 //----------------------------------------------------------------------------
1557 Int_t AliTOFcalib::Calibrate(Option_t *optionSave, Option_t *optionFit){
1558
1559   // calibrating the whole TOF
1560   // computing calibration parameters
1561   // Returning codes:
1562   // 0 -> everything was ok
1563   // 1 -> no tree for calibration found
1564   // 2 -> not enough statistics to perform calibration
1565   // 3 -> problems with arrays
1566
1567   TH1::AddDirectory(0);
1568
1569   AliInfo(Form("*** Calibrating Histograms %s, all channels", GetName())) ; 
1570   AliInfo(Form("Option for Saving histos = %s",optionSave )) ; 
1571   AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ; 
1572
1573   TFile * fileProf=0x0;
1574   if(strstr(optionSave,"save")){
1575     fileProf = new TFile("TOFCalibSave.root","recreate");
1576   }
1577
1578   Float_t p[MAXCHENTRIESSMALL];
1579   Int_t nentries;
1580   //fTree->SetBranchAddress("nentries",&nentries);
1581   //fTree->SetBranchAddress("TOFentries",p);
1582   fChain->SetBranchAddress("nentries",&nentries);
1583   fChain->SetBranchAddress("TOFentries",p);
1584
1585   Float_t ntracksTotalmean =0;
1586   for (Int_t ii=0; ii<fNChannels; ii++){
1587     for (Int_t irun=0;irun<fNruns;irun++){
1588       Int_t i = ii+irun*fNChannels;
1589       //fTree->GetEntry(i);
1590       fChain->GetEntry(i);
1591       ntracksTotalmean+=nentries/3;
1592     }
1593   }
1594
1595   ntracksTotalmean/=fNChannels;
1596   if (ntracksTotalmean < MEANENTRIES) {
1597     AliInfo(Form(" Too small mean number of entires per channel (mean number = %f) not calibrating and exiting.....",ntracksTotalmean));
1598     return 2;
1599   }
1600
1601   //filling ToT and Time arrays
1602
1603   Int_t nbinToT = 100;  // ToT bin width in Profile = 50.0 ps 
1604   Float_t minToT = 0;   // ns
1605   Float_t maxToT = 4.88;// ns
1606   for (Int_t ii=0; ii<fNChannels; ii++) {
1607     TH1F *hToT = new TH1F("htot","htot",nbinToT, minToT, maxToT);
1608     TH1F *hdeltaTime = new TH1F("hdeltaTime","hdeltaTime",200,2,4);
1609     TH2F * htimetot = new TH2F("htimetot","htimetot",nbinToT, minToT, maxToT,600,-5,10);
1610     if (ii%1000 == 0) AliDebug(1,Form("Calibrating channel %i ",ii));
1611     //Int_t i = 3;
1612     Int_t nusefulbins=0;
1613     Double_t binsProfile[101]; // sized larger than necessary, the correct 
1614                               // dim being set in the booking of the profile
1615     Int_t ntracksRun = 0;
1616     Int_t ntracksTotal = 0;
1617     for (Int_t irun=0;irun<fNruns;irun++){
1618       Int_t i = ii+irun*fNChannels;
1619       //fTree->GetEntry(i);
1620       fChain->GetEntry(i);
1621       ntracksTotal+=nentries/3;
1622     }
1623     if (ntracksTotal < MEANENTRIES) {
1624       AliInfo(Form(" Too small mean number of entires in channel %i (number of tracks = %d), not calibrating channel and continuing.....",ii,ntracksTotal));
1625       continue;
1626     }
1627     Float_t meantime=0;
1628     for (Int_t irun=0;irun<fNruns;irun++){
1629       Int_t i = ii+irun*fNChannels;
1630       //fTree->GetEntry(i);
1631       fChain->GetEntry(i);
1632       ntracksRun=nentries/3;
1633       for (Int_t j=0;j<ntracksRun;j++){
1634         Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1635         Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1636         Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1637         Float_t tot = p[idxexToT];
1638         hdeltaTime->Fill(p[idxexTime]-p[idxexExTime]);
1639         meantime+=p[idxexTime]-p[idxexExTime];
1640         hToT->Fill(tot);
1641       }
1642     }
1643     nusefulbins = FindBins(hToT,&binsProfile[0]);
1644     meantime/=ntracksTotal;
1645     for (Int_t j=0;j<nusefulbins;j++) {
1646       AliDebug(2,Form(" channel %i, usefulbin = %i, low edge = %f",ii,j,binsProfile[j])); 
1647     }
1648     TProfile* hSlewingProf = new TProfile("hSlewingProf", "hSlewingProf",nusefulbins, binsProfile, "G");  // CHECK THE BUILD OPTION, PLEASE!!!!!!
1649     for (Int_t irun=0;irun<fNruns;irun++){
1650       Int_t i = ii+irun*fNChannels;
1651       //fTree->GetEntry(i);
1652       fChain->GetEntry(i);
1653       ntracksRun=nentries/3;
1654       for (Int_t j=0;j<ntracksRun;j++){
1655         Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1656         Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1657         Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1658         Float_t tot = p[idxexToT];
1659         Float_t time = p[idxexTime]-p[idxexExTime];
1660         AliDebug (2,Form("track = %i, time = %f, tot = %f, time-meantime = %f",j,time, tot, time-meantime));
1661         hSlewingProf->Fill(tot,time);  // if meantime is not used, the fill may be moved in the loop above
1662         htimetot->Fill(tot,time-meantime);  // if meantime is not used, the fill may be moved in the loop above
1663       }
1664     }
1665     hSlewingProf->Fit("pol5",optionFit,"",1,4);
1666     TF1 * calibfunc = (TF1*)hSlewingProf->GetFunction("pol5");
1667     Float_t par[6];    
1668     for(Int_t kk=0;kk<6;kk++){
1669       par[kk]=calibfunc->GetParameter(kk);
1670       AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
1671     }
1672
1673     if(strstr(optionSave,"save") && fileProf){
1674       TString profName=Form("Profile%06i",ii);
1675       TString timeTotName=Form("TimeTot%06i",ii);
1676       TString totName=Form("Tot%06i",ii);
1677       TString deltaName=Form("Delta%06i",ii);
1678       fileProf->cd();
1679       hSlewingProf->Write(profName);
1680       htimetot->Write(timeTotName);
1681       hToT->Write(totName);
1682       hdeltaTime->Write(deltaName);
1683     }
1684     AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(ii);
1685     calChannel->SetSlewPar(par);
1686
1687     delete hToT;
1688     hToT=0x0;
1689     delete hSlewingProf;
1690     hSlewingProf=0x0;
1691     delete htimetot;
1692     htimetot=0x0;
1693     delete hdeltaTime;
1694     hdeltaTime=0x0;
1695   }
1696
1697   if(strstr(optionSave,"save")){
1698     fileProf->Close();
1699     delete fileProf;
1700     fileProf=0x0;
1701   }
1702   WriteParOfflineOnCDB("TOF/Calib","valid");
1703   return 0;
1704 }
1705
1706 //-----------------------------------------------------------------------
1707 TH1F* AliTOFcalib::Profile(Int_t ich)
1708 {
1709   // profiling algo
1710
1711   Float_t p[MAXCHENTRIESSMALL];
1712   Int_t nentries;
1713   //fTree->SetBranchAddress("nentries",&nentries);
1714   //fTree->SetBranchAddress("TOFentries",p);
1715   fChain->SetBranchAddress("nentries",&nentries);
1716   fChain->SetBranchAddress("TOFentries",p);
1717
1718   //Prepare histograms for Slewing Correction
1719   const Int_t knbinToT = 100;
1720   Int_t nbinTime = 200;
1721   Float_t minTime = -5.5; //ns
1722   Float_t maxTime = 5.5; //ns
1723   Float_t minToT = 0; //ns
1724   Float_t maxToT = 5.; //ns
1725   Float_t deltaToT = (maxToT-minToT)/knbinToT;
1726   Double_t mTime[knbinToT+1],mToT[knbinToT+1],meanTime[knbinToT+1], meanTime2[knbinToT+1],vToT[knbinToT+1], vToT2[knbinToT+1],meanToT[knbinToT+1],meanToT2[knbinToT+1],vTime[knbinToT+1],vTime2[knbinToT+1],xlow[knbinToT+1],sigmaTime[knbinToT+1];
1727   Int_t n[knbinToT+1], nentrx[knbinToT+1];
1728   Double_t sigmaToT[knbinToT+1];
1729   for (Int_t i = 0; i < knbinToT+1 ; i++){
1730     mTime[i]=0;
1731     mToT[i]=0;
1732     n[i]=0;
1733     meanTime[i]=0;
1734     meanTime2[i]=0;
1735     vToT[i]=0;
1736     vToT2[i]=0;
1737     meanToT[i]=0;
1738     meanToT2[i]=0;
1739     vTime[i]=0;
1740     vTime2[i]=0;
1741     xlow[i]=0;
1742     sigmaTime[i]=0;
1743     sigmaToT[i]=0;
1744     n[i]=0;
1745     nentrx[i]=0;
1746   }
1747   TH2F* hSlewing = new TH2F("hSlewing", "hSlewing", knbinToT, minToT, maxToT, nbinTime, minTime, maxTime);
1748   Int_t ntracksRun = 0;
1749   TH1F *histo = new TH1F("histo", "1D Time vs ToT", knbinToT, minToT, maxToT);
1750   for (Int_t irun=0;irun<fNruns;irun++){
1751     Int_t i = ich+irun*fNChannels;
1752     //fTree->GetEntry(i);
1753     fChain->GetEntry(i);
1754     ntracksRun=nentries/3;
1755     for (Int_t j=0;j<ntracksRun;j++){
1756       Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1757       Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1758       Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1759       Float_t tot = p[idxexToT];
1760       Float_t time = p[idxexTime]-p[idxexExTime];
1761       Int_t nx = (Int_t)((tot-minToT)/deltaToT)+1;
1762       if ((tot != 0) && ( time!= 0)){
1763         vTime[nx]+=time;
1764         vTime2[nx]+=time*time;
1765         vToT[nx]+=tot;
1766         vToT2[nx]+=tot*tot;
1767         nentrx[nx]++;
1768         hSlewing->Fill(tot,time);
1769       }
1770     }
1771   }
1772   Int_t nbinsToT=hSlewing->GetNbinsX();
1773   if (nbinsToT != knbinToT) {
1774     AliError("Profile :: incompatible numbers of bins");
1775     return 0x0;
1776   }
1777   
1778   Int_t usefulBins=0;
1779   for (Int_t i=1;i<=nbinsToT;i++){
1780     if (nentrx[i]!=0){
1781       n[usefulBins]+=nentrx[i];
1782       if (n[usefulBins]==0 && i == nbinsToT) {
1783         break;
1784       }
1785       meanTime[usefulBins]+=vTime[i];
1786       meanTime2[usefulBins]+=vTime2[i];
1787       meanToT[usefulBins]+=vToT[i];
1788       meanToT2[usefulBins]+=vToT2[i];
1789       if (n[usefulBins]<10 && i!=nbinsToT) continue; 
1790       mTime[usefulBins]=meanTime[usefulBins]/n[usefulBins];
1791       mToT[usefulBins]=meanToT[usefulBins]/n[usefulBins];
1792       sigmaTime[usefulBins]=TMath::Sqrt(1./n[usefulBins]/n[usefulBins]
1793                             *(meanTime2[usefulBins]-meanTime[usefulBins]
1794                             *meanTime[usefulBins]/n[usefulBins]));
1795       if ((1./n[usefulBins]/n[usefulBins]
1796            *(meanToT2[usefulBins]-meanToT[usefulBins]
1797              *meanToT[usefulBins]/n[usefulBins]))< 0) {
1798         AliError(" too small radical" );
1799         sigmaToT[usefulBins]=0;
1800       }
1801       else{       
1802         sigmaToT[usefulBins]=TMath::Sqrt(1./n[usefulBins]/n[usefulBins]
1803                              *(meanToT2[usefulBins]-meanToT[usefulBins]
1804                              *meanToT[usefulBins]/n[usefulBins]));
1805       }
1806       usefulBins++;
1807     }
1808   }
1809   for (Int_t i=0;i<usefulBins;i++){
1810     Int_t binN = (Int_t)((mToT[i]-minToT)/deltaToT)+1;
1811     histo->Fill(mToT[i],mTime[i]);
1812     histo->SetBinError(binN,sigmaTime[i]);
1813   } 
1814   delete hSlewing;
1815   hSlewing=0x0;
1816
1817   return histo;
1818 }
1819 //----------------------------------------------------------------------------
1820 Int_t AliTOFcalib::FindBins(TH1F* h, Double_t *binsProfile) const{
1821
1822   // to determine the bins for ToT histo
1823
1824   Int_t cont = 0;
1825   Int_t startBin = 1;
1826   Int_t nbin = h->GetNbinsX();
1827   Int_t nentries = (Int_t)h->GetEntries();
1828   Float_t max = h->GetBinLowEdge(nbin);
1829   Int_t nusefulbins=0;
1830   Int_t maxcont=0;
1831   // setting maxvalue of entries per bin
1832   if (nentries <= 60) maxcont = 2;
1833   else  if (nentries <= 100) maxcont = 5;
1834   else  if (nentries <= 500) maxcont = 10;
1835   else  maxcont = 20;
1836   for (Int_t j=1;j<=nbin;j++) {
1837     cont += (Int_t)h->GetBinContent(j);
1838     if (j<nbin){
1839       if (cont>=maxcont){
1840         nusefulbins++;
1841         binsProfile[nusefulbins-1]=h->GetBinLowEdge(startBin);
1842         cont=0;
1843         startBin=j+1;
1844         continue;
1845       }
1846     }
1847     else{
1848       if (cont>=maxcont){
1849         nusefulbins++;
1850         binsProfile[nusefulbins-1]=h->GetBinLowEdge(startBin);
1851         binsProfile[nusefulbins]=max;
1852       }
1853       else {
1854         binsProfile[nusefulbins]=h->GetBinLowEdge(startBin);
1855       }
1856     }
1857   }
1858   return nusefulbins;
1859 }
1860
1861
1862 //----------------------------------------------------------------------------
1863
1864 void
1865 AliTOFcalib::CreateDeltaBCOffset()
1866 {
1867   /*
1868    * create deltaBC offset
1869    */
1870
1871   if (fDeltaBCOffset) {
1872     AliWarning("DeltaBCOffset object already defined, cannot create a new one");
1873     return;
1874   }
1875   fDeltaBCOffset = new AliTOFDeltaBCOffset();
1876 }
1877   
1878 //----------------------------------------------------------------------------
1879
1880 void
1881 AliTOFcalib::CreateCTPLatency()
1882 {
1883   /*
1884    * create CTP latency
1885    */
1886
1887   if (fCTPLatency) {
1888     AliWarning("CTPLatency object already defined, cannot create a new one");
1889     return;
1890   }
1891   fCTPLatency = new AliTOFCTPLatency();
1892 }
1893   
1894 //----------------------------------------------------------------------------
1895
1896 void
1897 AliTOFcalib::CreateT0Fill()
1898 {
1899   /*
1900    * create event-time
1901    */
1902
1903   if (fT0Fill) {
1904     AliWarning("T0Fill object already defined, cannot create a new one");
1905     return;
1906   }
1907   fT0Fill = new AliTOFT0Fill();
1908 }
1909   
1910 //----------------------------------------------------------------------------
1911
1912 void
1913 AliTOFcalib::CreateRunParams()
1914 {
1915   /*
1916    * create run params
1917    */
1918
1919   if (fRunParams) {
1920     AliWarning("RunParams object already defined, cannot create a new one");
1921     return;
1922   }
1923   fRunParams = new AliTOFRunParams();
1924 }
1925   
1926 //----------------------------------------------------------------------------
1927
1928 void
1929 AliTOFcalib::WriteDeltaBCOffsetOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
1930 {
1931   /*
1932    * deltaBC offset on CDB 
1933    */
1934   
1935   if (!fDeltaBCOffset) return;
1936   AliCDBId id(Form("%s/DeltaBCOffset", sel), minrun, maxrun);
1937   AliCDBMetaData *md = new AliCDBMetaData();
1938   md->SetResponsible("Roberto Preghenella");
1939   AliCDBManager *man = AliCDBManager::Instance();
1940   man->Put(fDeltaBCOffset, id, md);
1941   AliDebug(2,Form("DeltaBCOffset written on CDB with run range [%i, %i] ",minrun ,maxrun));
1942   delete md;
1943 }
1944
1945 //----------------------------------------------------------------------------
1946
1947 void
1948 AliTOFcalib::WriteCTPLatencyOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
1949 {
1950   /*
1951    * write CTP latency on CDB 
1952    */
1953   
1954   if (!fCTPLatency) return;
1955   AliCDBId id(Form("%s/CTPLatency", sel), minrun, maxrun);
1956   AliCDBMetaData *md = new AliCDBMetaData();
1957   md->SetResponsible("Roberto Preghenella");
1958   AliCDBManager *man = AliCDBManager::Instance();
1959   man->Put(fCTPLatency, id, md);
1960   AliDebug(2,Form("CTPLatency written on CDB with run range [%i, %i] ",minrun ,maxrun));
1961   delete md;
1962 }
1963
1964 //----------------------------------------------------------------------------
1965
1966 void
1967 AliTOFcalib::WriteT0FillOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
1968 {
1969   /*
1970    * write event-time on CDB 
1971    */
1972   
1973   if (!fT0Fill) return;
1974   AliCDBId id(Form("%s/T0Fill", sel), minrun, maxrun);
1975   AliCDBMetaData *md = new AliCDBMetaData();
1976   md->SetResponsible("Roberto Preghenella");
1977   AliCDBManager *man = AliCDBManager::Instance();
1978   man->Put(fT0Fill, id, md);
1979   AliDebug(2,Form("T0Fill written on CDB with run range [%i, %i] ",minrun ,maxrun));
1980   delete md;
1981 }
1982
1983 //----------------------------------------------------------------------------
1984
1985 void
1986 AliTOFcalib::WriteRunParamsOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
1987 {
1988   /*
1989    * write run params on CDB 
1990    */
1991   
1992   if (!fRunParams) return;
1993   AliCDBId id(Form("%s/RunParams", sel), minrun, maxrun);
1994   AliCDBMetaData *md = new AliCDBMetaData();
1995   md->SetResponsible("Roberto Preghenella");
1996   AliCDBManager *man = AliCDBManager::Instance();
1997   man->Put(fRunParams, id, md);
1998   AliDebug(2,Form("RunParams written on CDB with run range [%i, %i] ",minrun ,maxrun));
1999   delete md;
2000 }
2001
2002 //----------------------------------------------------------------------------
2003
2004 Bool_t
2005 AliTOFcalib::ReadDeltaBCOffsetFromCDB(const Char_t *sel , Int_t nrun)
2006 {
2007   /*
2008    * read deltaBC offset from CDB
2009    */
2010   
2011   AliCDBManager *man = AliCDBManager::Instance();
2012   AliCDBEntry *entry = man->Get(Form("%s/DeltaBCOffset", sel),nrun);
2013   if (!entry) { 
2014     AliFatal("No DeltaBCOffset entry found in CDB");
2015     exit(0);  
2016   }
2017   fDeltaBCOffset =(AliTOFDeltaBCOffset *)entry->GetObject();
2018   if(!fDeltaBCOffset){
2019     AliFatal("No DeltaBCOffset object found in CDB entry");
2020     exit(0);  
2021   }  
2022   return kTRUE; 
2023 }
2024
2025 //----------------------------------------------------------------------------
2026
2027 Bool_t
2028 AliTOFcalib::ReadCTPLatencyFromCDB(const Char_t *sel , Int_t nrun)
2029 {
2030   /*
2031    * read CTP latency from CDB
2032    */
2033   
2034   AliCDBManager *man = AliCDBManager::Instance();
2035   AliCDBEntry *entry = man->Get(Form("%s/CTPLatency", sel),nrun);
2036   if (!entry) { 
2037     AliFatal("No CTPLatency entry found in CDB");
2038     exit(0);  
2039   }
2040   fCTPLatency =(AliTOFCTPLatency *)entry->GetObject();
2041   if(!fCTPLatency){
2042     AliFatal("No CTPLatency object found in CDB entry");
2043     exit(0);  
2044   }  
2045   return kTRUE; 
2046 }
2047
2048 //----------------------------------------------------------------------------
2049
2050 Bool_t
2051 AliTOFcalib::ReadT0FillFromCDB(const Char_t *sel , Int_t nrun)
2052 {
2053   /*
2054    * read event-time from CDB
2055    */
2056   
2057   AliCDBManager *man = AliCDBManager::Instance();
2058   AliCDBEntry *entry = man->Get(Form("%s/T0Fill", sel),nrun);
2059   if (!entry) { 
2060     AliFatal("No T0Fill entry found in CDB");
2061     exit(0);  
2062   }
2063   fT0Fill =(AliTOFT0Fill *)entry->GetObject();
2064   if(!fT0Fill){
2065     AliFatal("No T0Fill object found in CDB entry");
2066     exit(0);  
2067   }  
2068   return kTRUE; 
2069 }
2070
2071 //----------------------------------------------------------------------------
2072
2073 Bool_t
2074 AliTOFcalib::ReadRunParamsFromCDB(const Char_t *sel , Int_t nrun)
2075 {
2076   /*
2077    * read run params from CDB
2078    */
2079   
2080   AliCDBManager *man = AliCDBManager::Instance();
2081   AliCDBEntry *entry = man->Get(Form("%s/RunParams", sel),nrun);
2082   if (!entry) { 
2083     AliFatal("No RunParams entry found in CDB");
2084     exit(0);  
2085   }
2086   fRunParams =(AliTOFRunParams *)entry->GetObject();
2087   if(!fRunParams){
2088     AliFatal("No RunParams object found in CDB entry");
2089     exit(0);  
2090   }  
2091   return kTRUE; 
2092 }
2093
2094 //----------------------------------------------------------------------------
2095
2096 Bool_t 
2097 AliTOFcalib::Init(Int_t run)
2098 {
2099   /*
2100    * init
2101    */
2102
2103   if (fInitFlag) {
2104     AliWarning("the class was already initialized, re-initialize it");
2105     fInitFlag = kFALSE;
2106   }
2107   
2108   /* read channel status array */
2109   if (!ReadParOnlineStatusFromCDB("TOF/Calib", run)) {
2110     AliError("cannot get \"Status\" object from OCDB");
2111     return kFALSE;
2112   }
2113   /* get par offline array */
2114   if (!ReadParOfflineFromCDB("TOF/Calib", run)) {
2115     AliError("cannot get \"ParOffline\" object from OCDB");
2116     return kFALSE;
2117   }
2118   /* get deltaBC offset obj */
2119   if (!ReadDeltaBCOffsetFromCDB("TOF/Calib", run)) {
2120     AliError("cannot get \"DeltaBCOffset\" object from OCDB");
2121     return kFALSE;
2122   }
2123   /* get CTP latency obj */
2124   if (!ReadCTPLatencyFromCDB("TOF/Calib", run)) {
2125     AliError("cannot get \"CTPLatency\" object from OCDB");
2126     return kFALSE;
2127   }
2128   /* get run params obj */
2129   if (!ReadRunParamsFromCDB("TOF/Calib", run)) {
2130     AliError("cannot get \"RunParams\" object from OCDB");
2131     return kFALSE;
2132   }
2133   /* get response params */
2134   if (fCorrectTExp) {
2135     TFile *responseFile = TFile::Open("$ALICE_ROOT/TOF/data/AliTOFresponsePar.root");
2136     if (!responseFile || !responseFile->IsOpen()) {
2137       AliError("cannot open \"ResponseParams\" local file");
2138       return kFALSE;
2139     }
2140     fResponseParams = (AliTOFResponseParams *)responseFile->Get("ResponseParams");
2141     if (!fResponseParams) {
2142       AliError("cannot get \"ResponseParams\" object from local file");
2143       return kFALSE;
2144     }
2145     responseFile->Close();
2146   }
2147
2148   /* all done */
2149   fInitFlag = kTRUE;
2150   return kTRUE;
2151
2152 }
2153
2154 //----------------------------------------------------------------------------
2155
2156 Double_t
2157 AliTOFcalib::GetTimeCorrection(Int_t index, Double_t tot, Int_t deltaBC, Int_t l0l1, UInt_t timestamp)
2158 {
2159   /*
2160    * get time correction
2161    */
2162
2163   if (!fInitFlag) {
2164     AliError("class not yet initialized. Initialize it before.");
2165     return 0.;
2166   }
2167
2168   /* get calibration params */
2169   AliTOFChannelOffline *parOffline = (AliTOFChannelOffline *)fTOFCalOffline->At(index);
2170   Int_t deltaBCOffset = fDeltaBCOffset->GetDeltaBCOffset();
2171   Float_t ctpLatency = fCTPLatency->GetCTPLatency();
2172   Float_t tdcLatencyWindow = fStatus->GetLatencyWindow(index) * 1.e3;
2173   Float_t timezero = fRunParams->EvalT0(timestamp);
2174   /* check whether to remove mean T0.
2175    * useful when one wants to compute mean T0 */
2176   if (!fRemoveMeanT0) timezero = 0.;
2177
2178   /* compute correction */
2179   Double_t corr = 0.;
2180   /* deltaBC correction */
2181   deltaBC = deltaBCOffset; /* inhibit deltaBC correction for the time being */
2182   corr += (deltaBC - deltaBCOffset) * AliTOFGeometry::BunchCrossingBinWidth();
2183   /* L0-L1 latency correction */
2184   corr -= l0l1 * AliTOFGeometry::BunchCrossingBinWidth();
2185   /* CTP latency correction */
2186   corr -= ctpLatency;
2187   /* TDC latency window correction */
2188   corr += tdcLatencyWindow;
2189   /* time-zero correction */
2190   corr += timezero;
2191   /* time calibration correction */
2192   if (tot < AliTOFGeometry::SlewTOTMin()) 
2193     tot = AliTOFGeometry::SlewTOTMin();
2194   if (tot > AliTOFGeometry::SlewTOTMax()) 
2195     tot = AliTOFGeometry::SlewTOTMax();
2196   for (Int_t islew = 0; islew < 6; islew++)
2197     corr += parOffline->GetSlewPar(islew) * TMath::Power(tot, islew) * 1.e3;
2198
2199   /* return correction */
2200   return corr;
2201 }
2202
2203 //----------------------------------------------------------------------------
2204
2205 void
2206 AliTOFcalib::CalibrateESD(AliESDEvent *event)
2207 {
2208   /*
2209    * calibrate ESD
2210    */
2211
2212   if (!fInitFlag) {
2213     AliError("class not yet initialized. Initialize it before.");
2214     return;
2215   }
2216
2217   /* loop over tracks */
2218   AliESDtrack *track = NULL;
2219   Int_t index, l0l1, deltaBC;
2220   Double_t time, tot, corr, texp[AliPID::kSPECIES];
2221   UInt_t timestamp = event->GetTimeStamp();
2222   for (Int_t itrk = 0; itrk < event->GetNumberOfTracks(); itrk++) {
2223
2224     /* get track */
2225     track = event->GetTrack(itrk);
2226     if (!track || !(track->GetStatus() & AliESDtrack::kTOFout)) continue;
2227
2228     /* calibrate TOF signal */
2229     if (fCalibrateTOFsignal) {
2230       /* get info */
2231       index = track->GetTOFCalChannel();
2232       time = track->GetTOFsignalRaw();
2233       tot = track->GetTOFsignalToT();
2234       l0l1 = track->GetTOFL0L1();
2235       deltaBC = track->GetTOFDeltaBC();
2236       /* get correction */
2237       corr = GetTimeCorrection(index, tot, deltaBC, l0l1, timestamp);
2238       /* apply correction */
2239       time -= corr;
2240       /* set new TOF signal */
2241       track->SetTOFsignal(time);
2242     }
2243
2244     /* correct expected time */
2245     if (fCorrectTExp) {
2246       /* get integrated times */
2247       track->GetIntegratedTimes(texp);
2248       /* loop over particle types and correct expected time */
2249       for (Int_t ipart = 0; ipart < AliPID::kSPECIES; ipart++)
2250         texp[ipart] += fResponseParams->EvalTExpCorr(ipart, track->P());
2251       /* set integrated times */
2252       track->SetIntegratedTimes(texp);
2253     }
2254
2255   }
2256
2257 }
2258
2259 //----------------------------------------------------------------------------
2260
2261 Bool_t
2262 AliTOFcalib::IsChannelEnabled(Int_t index)
2263 {
2264   /*
2265    * is channel enabled
2266    */
2267
2268   if (!fInitFlag) {
2269     AliError("class not yet initialized. Initialize it before.");
2270     return kTRUE;
2271   }
2272
2273   /* check bad status */
2274   if (fStatus->GetPulserStatus(index) == AliTOFChannelOnlineStatusArray::kTOFPulserBad) return kFALSE;
2275   if (fStatus->GetNoiseStatus(index) == AliTOFChannelOnlineStatusArray::kTOFNoiseBad) return kFALSE;
2276   if (fStatus->GetHWStatus(index) == AliTOFChannelOnlineStatusArray::kTOFHWBad) return kFALSE;
2277   
2278   /* good status */
2279   return kTRUE;
2280
2281 }