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