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