]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFcalib.cxx
Corrected t0spread stored in AliTOFHeader object and changed log infos
[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
907   TObjArray *arr=new TObjArray(1);
908   arr->AddLast(param);
909   man->Put(arr,id,md);
910   //man->Put(param,id,md);
911   delete md;
912 }
913 //_____________________________________________________________________________
914 void AliTOFcalib::WriteRecParOnCDB(const Char_t *sel, Int_t minrun, Int_t maxrun, TObjArray *arr){
915   //Write reconstruction parameters to the CDB
916
917   AliCDBManager *man = AliCDBManager::Instance();
918   AliCDBMetaData *md = new AliCDBMetaData();
919   md->SetResponsible("Silvia Arcelli");
920   const Char_t *sel1 = "RecoParam" ;
921   TString out(Form("%s/%s",sel,sel1));
922   AliCDBId id(out,minrun,maxrun);
923   man->Put(arr,id,md);
924   delete md;
925 }
926 //_____________________________________________________________________________
927 AliTOFRecoParam * AliTOFcalib::ReadRecParFromCDB(const Char_t *sel, Int_t nrun, Int_t eventType)
928 {
929   //Read reconstruction parameters from the CDB
930   AliCDBManager *man = AliCDBManager::Instance();
931   const Char_t *sel1 = "RecoParam" ;
932   TString out(Form("%s/%s",sel,sel1));
933   AliCDBEntry *entry = man->Get(out,nrun);
934   if (!entry) { 
935     AliFatal("Exiting, no CDB object (RecoParam) found!!!");
936     exit(0);  
937   }  
938   if(!entry->GetObject()){
939     AliFatal("Exiting, no CDB object (RecoParam) found!!!");
940     exit(0);  
941   }  
942
943   TObjArray *array = (TObjArray*)entry->GetObject();
944   AliTOFRecoParam *param=0x0;
945   if (eventType>=0 || eventType<array->GetEntries())
946     param=(AliTOFRecoParam*)array->At(eventType);
947   return param;
948
949 }
950 //-----------------------------------------------------------------------------
951 // Calibration methods
952 //-----------------------------------------------------------------------------
953 void AliTOFcalib::CreateTreeFromCDB(Int_t minrun, Int_t maxrun){
954
955   // creating the chain with the trees for calibration
956   // collecting them from reference data 
957   // from minrun to maxrun
958
959   Float_t p[CHENTRIESSMALL];
960   Int_t nentries;
961   fTree = new TTree("TOFCalib","Tree for TOF Calibration");
962   fTree->Branch("nentries",&nentries,"nentries/I");
963   fTree->Branch("TOFentries",p,"TOFentries[nentries]/F");
964   AliCDBManager *man = AliCDBManager::Instance();
965   AliCDBStorage *aStorage = man->GetStorage("local://$ALICE_ROOT/OCDB");
966   for (Int_t irun = minrun;irun<=maxrun;irun++){
967     AliCDBEntry *entry = aStorage->Get("TOF/RefData/TreeForCalib",irun);
968     if (!entry){
969       AliInfo(Form("No entry found for run %i",irun));
970     }
971     else{
972       TTree *tree = (TTree*)entry->GetObject();
973       tree->SetBranchAddress("nentries",&nentries);
974       tree->SetBranchAddress("TOFentries",p);      
975       fTree->CopyEntries(tree);
976       fNruns++;
977     }
978   }
979   AliInfo(Form("Number of runs being analyzed %i",fNruns));
980 }
981 //-----------------------------------------------------------------------------
982 void AliTOFcalib::CreateTreeFromGrid(Int_t minrun, Int_t maxrun){
983
984   // creating the chain with the trees for calibration
985   // collecting them from the Grid 
986   // from minrun to maxrun
987
988   Float_t p[CHENTRIESSMALL];
989   Int_t nentries;
990   fTree = new TTree("TOFCalib","Tree for TOF Calibration");
991   fTree->SetDirectory(0);
992   fTree->Branch("nentries",&nentries,"nentries/I");
993   fTree->Branch("TOFentries",p,"TOFentries[nentries]/F");
994   AliInfo("connected to alien");
995   TGrid::Connect("alien://");
996   
997   TString filename;
998   for (Int_t irun = minrun;irun<=maxrun;irun++){
999     filename = Form("alien:///alice/cern.ch/user/c/czampolli/TOFCalibReference_%i.root",irun);
1000     TFile *filegrid = TFile::Open(filename.Data(),"READ");
1001     TTree *tree = (TTree*)filegrid->Get("T");
1002     tree->SetBranchAddress("nentries",&nentries);
1003     tree->SetBranchAddress("TOFentries",p);      
1004     fTree->CopyEntries(tree);
1005     delete tree;
1006     fNruns++;    
1007   }
1008   
1009   AliInfo(Form("Number of runs being analyzed %i",fNruns));
1010 }
1011 //-----------------------------------------------------------------------------
1012 void AliTOFcalib::CreateTreeFromFile(Int_t minrun, Int_t maxrun){
1013
1014   // creating the tree with the trees for calibration
1015   // collecting them from reference data (from file)
1016   // from minrun to maxrun
1017
1018   Float_t p[CHENTRIESSMALL];
1019   Int_t nentries;
1020   fTree = new TTree("TOFCalib","Tree for TOF Calibration");
1021   fTree->SetDirectory(0);
1022   fTree->Branch("nentries",&nentries,"nentries/I");
1023   fTree->Branch("TOFentries",p,"TOFentries[nentries]/F");
1024   TString filename;
1025   for (Int_t irun = minrun;irun<=maxrun;irun++){
1026     filename = Form("$ALICE_ROOT/TOF/RefData/TreeForCalib/fileout_%i.root",irun);
1027     TFile *file = new TFile(filename.Data(),"READ");
1028     TTree *tree = (TTree*)file->Get("T");
1029     tree->SetBranchAddress("nentries",&nentries);
1030     tree->SetBranchAddress("TOFentries",p);      
1031     fTree->CopyEntries(tree);
1032     delete tree;
1033     delete file;
1034     file = 0x0;
1035     fNruns++;
1036   }
1037
1038   AliInfo(Form("Number of runs being analyzed %i",fNruns));
1039 }
1040 //-----------------------------------------------------------------------------
1041 void AliTOFcalib::CreateChainFromGrid(Int_t minrun, Int_t maxrun){
1042
1043   // creating the chain with the trees for calibration
1044   // collecting them from the Grid 
1045   // from minrun to maxrun
1046
1047   fChain = new TChain("T");
1048   AliInfo("connected to alien");
1049   TGrid::Connect("alien://");
1050   
1051   TString filename;
1052   for (Int_t irun = minrun;irun<=maxrun;irun++){
1053     filename = Form("alien:///alice/cern.ch/user/c/czampolli/TOFCalibReference_%i.root",irun);
1054     fChain->Add(filename.Data());
1055     fNruns++;    
1056   }
1057   
1058   AliInfo(Form("Number of runs being analyzed %i",fNruns));
1059 }
1060 //-----------------------------------------------------------------------------
1061 Int_t AliTOFcalib::Calibrate(Int_t ichmin, Int_t ichmax, Option_t *optionSave, Option_t *optionFit){
1062
1063   // calibrating summing more than one channels
1064   // computing calibration parameters
1065   // Returning codes:
1066   // 0 -> everything was ok
1067   // 1 -> no tree for calibration found
1068   // 2 -> not enough statistics to perform calibration
1069   // 3 -> problems with arrays
1070   
1071   TH1::AddDirectory(0);
1072
1073   AliInfo(Form("*** Calibrating Histograms %s, summing more channels, from channel %i, to channel %i, storing Calib Pars in channel %i", GetName(),ichmin,ichmax,ichmin)) ; 
1074   AliInfo(Form("Option for Saving histos = %s",optionSave )) ; 
1075   AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ; 
1076
1077   Float_t p[CHENTRIESSMALL];
1078   Int_t nentries;
1079   //fTree->SetBranchAddress("nentries",&nentries);
1080   //fTree->SetBranchAddress("TOFentries",p);
1081   fChain->SetBranchAddress("nentries",&nentries);
1082   fChain->SetBranchAddress("TOFentries",p);
1083
1084   Float_t ntracksTotalmean =0;
1085   for (Int_t i=ichmin; i<ichmax; i++){
1086     Int_t ientry = -1;
1087     for (Int_t irun=0;irun<fNruns;irun++){
1088       ientry = i+irun*fNChannels;
1089       //fTree->GetEntry(ientry);
1090       fChain->GetEntry(ientry);
1091       Int_t ntracksRun=nentries/3;
1092       ntracksTotalmean+=ntracksRun;
1093     }
1094   }
1095   
1096   if (ntracksTotalmean < MEANENTRIES) {
1097     AliInfo(Form(" Too small mean number of entires per channel (mean number = %f) not calibrating and exiting.....",ntracksTotalmean));
1098     return 2;
1099   }
1100
1101   //filling ToT and Time arrays
1102
1103   Int_t nbinToT = 100;  // ToT bin width in Profile = 48.8 ps 
1104   Float_t minToT = 0;   // ns
1105   Float_t maxToT = 4.88;  // ns
1106
1107   TH1F *hToT = new TH1F("htot","htot",nbinToT, minToT, maxToT);
1108   TH1F *hdeltaTime = new TH1F("hdeltaTime","hdeltaTime",200,2,4);
1109   Int_t ntracksTotal = 0;
1110   Int_t ntracksRun = 0;
1111   Double_t binsProfile[101]; // sized larger than necessary, the correct 
1112                              // dim being set in the booking of the profile
1113   Int_t nusefulbins=0;
1114   Float_t meantime=0;
1115   for (Int_t i = ichmin;i<ichmax;i++){
1116     Int_t ientry = -1;
1117     for (Int_t irun=0;irun<fNruns;irun++){
1118       ientry = i+irun*fNChannels;
1119       //fTree->GetEntry(ientry);
1120       fChain->GetEntry(ientry);
1121       ntracksTotal+=nentries/3;
1122       ntracksRun=nentries/3;
1123       AliDebug(2,Form("run %i, channel %i, nentries = %i, ntracks = %i",irun,i,nentries, ntracksRun));
1124       for (Int_t j=0;j<ntracksRun;j++){
1125         Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1126         Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1127         Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1128         Float_t tot = p[idxexToT];
1129         hdeltaTime->Fill(p[idxexTime]-p[idxexExTime]);
1130         meantime+=p[idxexTime]-p[idxexExTime];
1131         hToT->Fill(tot);
1132       }
1133     }
1134   }
1135   nusefulbins = FindBins(hToT,&binsProfile[0]);
1136   meantime/=ntracksTotal;
1137   AliDebug(2, Form("meantime = %f",meantime));
1138   
1139   for (Int_t j=1;j<=nusefulbins;j++) {
1140     AliDebug(2,Form(" summing channels from %i to %i, nusefulbins = %i, bin %i = %f",ichmin,ichmax,nusefulbins,j,binsProfile[j])); 
1141   }
1142
1143   TProfile* hSlewingProf = new TProfile("hSlewingProf", "hSlewingProf",nusefulbins, binsProfile, "G");  // CHECK THE BUILD OPTION, PLEASE!!!!!!
1144   TH2F * htimetot = new TH2F("htimetot","htimetot",nbinToT, minToT, maxToT,600,-5,10);
1145
1146   for (Int_t irun=0;irun<fNruns;irun++){
1147     Int_t ientry = -1;
1148     for (Int_t i=ichmin; i<ichmax; i++){
1149       ientry = i+irun*fNChannels;
1150       //fTree->GetEntry(ientry);
1151       fChain->GetEntry(ientry);
1152       ntracksRun=nentries/3;
1153       for (Int_t j=0;j<ntracksRun;j++){
1154         Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1155         Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1156         Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1157         Float_t tot = p[idxexToT];
1158         Float_t time = p[idxexTime]-p[idxexExTime];
1159         AliDebug (2, Form("track = %i, time = %f, tot = %f, time-meantime = %f",j,time, tot, time-meantime));
1160         hSlewingProf->Fill(tot,time);  // if meantime is not used, the fill may be moved in the loop above
1161         htimetot->Fill(tot,time-meantime);  // if meantime is not used, the fill may be moved in the loop above
1162       }
1163     }
1164   }
1165
1166   hSlewingProf->Fit("pol5",optionFit,"",0,4);
1167   TF1 * calibfunc = (TF1*)hSlewingProf->GetFunction("pol5");
1168   Float_t par[6];    
1169   for(Int_t kk=0;kk<6;kk++){
1170     par[kk]=calibfunc->GetParameter(kk);
1171     AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
1172   }
1173
1174   if(strstr(optionSave,"save")){
1175     TFile * fileProf = new TFile("TOFCalibSave.root","recreate");
1176     fileProf->cd(); 
1177     TString profName=Form("Profile%06i_%06i",ichmin,ichmax);
1178     TString timeTotName=Form("TimeTot%06i_%06i",ichmin,ichmax);
1179     TString totName=Form("Tot%06i_%06i",ichmin,ichmax);
1180     TString deltaName=Form("Delta%06i_%06i",ichmin,ichmax);
1181     hSlewingProf->Write(profName);
1182     htimetot->Write(timeTotName);
1183     hToT->Write(totName);
1184     hdeltaTime->Write(deltaName);
1185     fileProf->Close();
1186     delete fileProf;
1187     fileProf=0x0;
1188   }
1189
1190   delete hToT;
1191   hToT=0x0;
1192   delete hSlewingProf;
1193   hSlewingProf=0x0;
1194   delete htimetot;
1195   htimetot=0x0;
1196   delete hdeltaTime;
1197   hdeltaTime=0x0;
1198
1199   AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(ichmin);
1200   calChannel->SetSlewPar(par);
1201   WriteParOfflineOnCDB("TOF/Calib","valid");
1202   return 0;
1203 }
1204 //----------------------------------------------------------------------------
1205 Int_t AliTOFcalib::Calibrate(Int_t i, Option_t *optionSave, Option_t *optionFit){
1206
1207   // computing calibration parameters for channel i
1208   // Returning codes:
1209   // 0 -> everything was ok
1210   // 1 -> no tree for calibration found
1211   // 2 -> not enough statistics to perform calibration
1212   // 3 -> problems with arrays
1213
1214   TH1::AddDirectory(0);
1215   
1216   AliInfo(Form("*** Calibrating Histograms (one channel) %s", GetName())) ; 
1217   AliInfo(Form("Option for Saving histos = %s",optionSave )) ; 
1218   AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ; 
1219
1220   Float_t p[MAXCHENTRIESSMALL];
1221   Int_t nentries;
1222   //fTree->SetBranchAddress("nentries",&nentries);
1223   //fTree->SetBranchAddress("TOFentries",p);
1224   fChain->SetBranchAddress("nentries",&nentries);
1225   fChain->SetBranchAddress("TOFentries",p);
1226
1227   Float_t ntracksTotal =0;
1228   for (Int_t irun=0;irun<fNruns;irun++){
1229     Int_t ientry = -1;
1230     ientry = i+irun*fNChannels;
1231     //fTree->GetEntry(ientry);
1232     fChain->GetEntry(ientry);
1233     ntracksTotal+=nentries/3;    
1234   }
1235   
1236   if (ntracksTotal < MEANENTRIES) {  
1237     AliInfo(Form(" Too small mean number of entires per channel (mean number = %f) not calibrating and exiting.....",ntracksTotal));
1238     return 2;
1239   }
1240
1241   //filling ToT and Time arrays
1242
1243   Int_t nbinToT = 100;  // ToT bin width in Profile = 48.8 ps 
1244   Float_t minToT = 0;   // ns
1245   Float_t maxToT = 4.88;  // ns
1246
1247   TH1F *hToT = new TH1F("htot","htot",nbinToT, minToT, maxToT);
1248   TH1F *hdeltaTime = new TH1F("hdeltaTime","hdeltaTime",200,2,4);
1249   Int_t ntracksRun = 0;
1250   Double_t binsProfile[101]; // sized larger than necessary, the correct 
1251                              // dim being set in the booking of the profile
1252   Int_t nusefulbins=0;
1253   Float_t meantime=0;
1254   for (Int_t irun=0;irun<fNruns;irun++){
1255     Int_t ientry = -1;
1256     ientry = i+irun*fNChannels;
1257     //fTree->GetEntry(ientry);
1258     fChain->GetEntry(ientry);
1259     ntracksRun=nentries/3;
1260     AliDebug(2,Form("run %i, channel %i, nentries = %i, ntracksRun = %i",irun, i ,nentries, ntracksRun));
1261     for (Int_t j=0;j<ntracksRun;j++){
1262       Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1263       Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1264       Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1265       Float_t tot = p[idxexToT];
1266       meantime+=p[idxexTime]-p[idxexExTime];
1267       hdeltaTime->Fill(p[idxexTime]-p[idxexExTime]);
1268       hToT->Fill(tot);
1269     }
1270   }
1271
1272   nusefulbins = FindBins(hToT,&binsProfile[0]);
1273   meantime/=ntracksTotal;
1274   AliDebug(2,Form("meantime = %f",meantime));
1275   
1276   for (Int_t j=1;j<=nusefulbins;j++) {
1277     AliDebug(2,Form(" channel %i, nusefulbins = %i, bin %i = %f",i,nusefulbins,j,binsProfile[j])); 
1278   }
1279
1280   TProfile* hSlewingProf = new TProfile("hSlewingProf", "hSlewingProf",nusefulbins, binsProfile, "G");  // CHECK THE BUILD OPTION, PLEASE!!!!!!
1281   TH2F * htimetot = new TH2F("htimetot","htimetot",nbinToT, minToT, maxToT,600,-5,10);
1282   for (Int_t irun=0;irun<fNruns;irun++){
1283     Int_t ientry = -1;
1284     ientry = i+irun*fNChannels;
1285     //fTree->GetEntry(ientry);
1286     fChain->GetEntry(ientry);
1287     ntracksRun=nentries/3;
1288     for (Int_t j=0;j<ntracksRun;j++){
1289       Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1290       Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1291       Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1292       Float_t tot = p[idxexToT];
1293       Float_t time = p[idxexTime]-p[idxexExTime];
1294       AliDebug (2,Form("track = %i, time = %f, tot = %f, time-meantime = %f",j,time, tot, time-meantime));
1295       hSlewingProf->Fill(tot,time);  // if meantime is not used, the fill may be moved in the loop above
1296       htimetot->Fill(tot,time-meantime);  // if meantime is not used, the fill may be moved in the loop above
1297     }
1298   }
1299
1300   hSlewingProf->Fit("pol5",optionFit,"",0,4);
1301   TF1 * calibfunc = (TF1*)hSlewingProf->GetFunction("pol5");
1302   Float_t par[6];    
1303   for(Int_t kk=0;kk<6;kk++){
1304     par[kk]=calibfunc->GetParameter(kk);
1305     AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
1306   }
1307
1308
1309   if(strstr(optionSave,"save")){
1310     TFile * fileProf = new TFile("TOFCalibSave.root","recreate");
1311     fileProf->cd();   
1312     TString profName=Form("Profile%06i",i);
1313     TString timeTotName=Form("TimeTot%06i",i);
1314     TString totName=Form("Tot%06i",i);
1315     TString deltaName=Form("Delta%06i",i);
1316     hSlewingProf->Write(profName);
1317     htimetot->Write(timeTotName);
1318     hToT->Write(totName);
1319     hdeltaTime->Write(deltaName);
1320     fileProf->Close();
1321     delete fileProf;
1322     fileProf=0x0;
1323   }
1324
1325   delete hToT;
1326   hToT=0x0; 
1327   delete hSlewingProf;
1328   hSlewingProf=0x0;
1329   delete htimetot;
1330   htimetot=0x0;
1331   delete hdeltaTime;
1332   hdeltaTime=0x0;
1333
1334   AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(i);
1335   calChannel->SetSlewPar(par);
1336   WriteParOfflineOnCDB("TOF/Calib","valid");
1337   return 0;
1338 }
1339 //----------------------------------------------------------------------------
1340 Int_t AliTOFcalib::Calibrate(Int_t nch, Int_t *ch, Option_t *optionSave, Option_t *optionFit){
1341
1342   // calibrating an array of channels
1343   // computing calibration parameters
1344   // Returning codes:
1345   // 0 -> everything was ok
1346   // 1 -> no tree for calibration found
1347   // 2 -> not enough statistics to perform calibration
1348   // 3 -> problems with arrays
1349   
1350   TH1::AddDirectory(0);
1351
1352   AliInfo(Form("*** Calibrating Histograms %s, number of channels = %i", GetName(),nch)) ; 
1353   AliInfo(Form("Option for Saving histos = %s",optionSave )) ; 
1354   AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ; 
1355   for (Int_t ich=0; ich<nch; ich++){
1356     Int_t i = ch[ich];
1357     AliInfo(Form("Calibrating channel = %i",i )) ; 
1358   }
1359   Float_t p[MAXCHENTRIESSMALL];
1360   Int_t nentries;
1361   //fTree->SetBranchAddress("nentries",&nentries);
1362   //fTree->SetBranchAddress("TOFentries",p);
1363   fChain->SetBranchAddress("nentries",&nentries);
1364   fChain->SetBranchAddress("TOFentries",p);
1365
1366   Float_t ntracksTotalmean =0;
1367   for (Int_t ich=0; ich<nch; ich++){
1368     Int_t ientry = -1;
1369       Int_t i = ch[ich];
1370       for (Int_t irun=0;irun<fNruns;irun++){
1371       ientry = i+irun*fNChannels;
1372       //fTree->GetEntry(ientry);
1373       fChain->GetEntry(ientry);
1374       ntracksTotalmean+=nentries/3;
1375     }
1376   }
1377
1378   ntracksTotalmean/=nch;
1379   if (ntracksTotalmean < MEANENTRIES) { 
1380     AliInfo(Form(" Too small mean number of entires per channel (mean number = %f) not calibrating and exiting.....",ntracksTotalmean));
1381     return 2;
1382   }
1383
1384   //filling ToT and Time arrays
1385
1386   Int_t nbinToT = 100;  // ToT bin width in Profile = 48.8 ps 
1387   Float_t minToT = 0;   // ns
1388   Float_t maxToT = 4.88;  // ns
1389   TFile * fileProf=0x0;
1390   if(strstr(optionSave,"save")){
1391     fileProf = new TFile("TOFCalibSave.root","recreate");
1392   }
1393   for (Int_t ich=0; ich<nch; ich++) {
1394     TH1F *hToT = new TH1F("htot","htot",nbinToT, minToT, maxToT);
1395     TH1F *hdeltaTime = new TH1F("hdeltaTime","hdeltaTime",200,2,4);
1396     Double_t binsProfile[101]; // sized larger than necessary, the correct 
1397     // dim being set in the booking of the profile
1398     TH2F * htimetot = new TH2F("htimetot","htimetot",nbinToT, minToT, maxToT,600,-5,10);
1399     Int_t ntracksTotal = 0;
1400     Int_t ntracksRun = 0;
1401     Int_t nusefulbins=0;
1402     Float_t meantime=0;
1403     Int_t i=-1;
1404     for (Int_t irun=0;irun<fNruns;irun++){
1405       i = ch[ich]+irun*fNChannels;
1406       AliDebug(2,Form("Calibrating channel %i",i));
1407       //fTree->GetEntry(i);
1408       fChain->GetEntry(i);
1409       ntracksTotal+=nentries/3;
1410     }
1411     if (ntracksTotal < MEANENTRIES) {
1412       AliInfo(Form(" Too small mean number of entires in channel %i (number of tracks = %d), not calibrating channel and continuing.....",i,ntracksTotal));
1413       continue;
1414     }
1415   
1416     for (Int_t irun=0;irun<fNruns;irun++){
1417       i = ch[ich]+irun*fNChannels;
1418       //fTree->GetEntry(i);
1419       fChain->GetEntry(i);
1420       ntracksRun=nentries/3;
1421       for (Int_t j=0;j<ntracksRun;j++){
1422         Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1423         Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1424         Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1425         Float_t tot = p[idxexToT];
1426         hdeltaTime->Fill(p[idxexTime]-p[idxexExTime]);
1427         meantime+=p[idxexTime]-p[idxexExTime];
1428         hToT->Fill(tot);
1429       }
1430     }
1431
1432     nusefulbins = FindBins(hToT,&binsProfile[0]);
1433     meantime/=ntracksTotal;
1434     for (Int_t j=1;j<=nusefulbins;j++) {
1435       AliDebug(2,Form(" channel %i, nusefulbins = %i, bin %i = %f",i,nusefulbins,j,binsProfile[j])); 
1436     }
1437
1438     TProfile* hSlewingProf = new TProfile("hSlewingProf", "hSlewingProf",nusefulbins, binsProfile, "G");  // CHECK THE BUILD OPTION, PLEASE!!!!!!
1439     for (Int_t irun=0;irun<fNruns;irun++){
1440       i = ch[ich]+irun*fNChannels;
1441       //fTree->GetEntry(i);
1442       fChain->GetEntry(i);
1443       ntracksRun=nentries/3;
1444       for (Int_t j=0;j<ntracksRun;j++){
1445         Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1446         Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1447         Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1448         Float_t tot = p[idxexToT];
1449         Float_t time = p[idxexTime]-p[idxexExTime];
1450         AliDebug(2,Form("track = %i, time = %f, tot = %f, time-meantime = %f",j,time, tot, time-meantime));
1451         hSlewingProf->Fill(tot,time);  // if meantime is not used, the fill may be moved in the loop above
1452         htimetot->Fill(tot,time-meantime);  // if meantime is not used, the fill may be moved in the loop above
1453       }
1454     }
1455     
1456     hSlewingProf->Fit("pol5",optionFit,"",1,4);
1457     TF1 * calibfunc = (TF1*)hSlewingProf->GetFunction("pol5");
1458     Float_t par[6];    
1459     for(Int_t kk=0;kk<6;kk++){
1460       par[kk]=calibfunc->GetParameter(kk);
1461       AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
1462     }
1463     
1464     if(strstr(optionSave,"save") && fileProf){
1465       TString profName=Form("Profile%06i",i);
1466       TString timeTotName=Form("TimeTot%06i",i);
1467       TString totName=Form("Tot%06i",i);
1468       TString deltaName=Form("Delta%06i",i);
1469       fileProf->cd();
1470       hSlewingProf->Write(profName);
1471       htimetot->Write(timeTotName);
1472       hToT->Write(totName);
1473       hdeltaTime->Write(deltaName);
1474     }
1475
1476     AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(i);
1477     calChannel->SetSlewPar(par);
1478     delete hToT;
1479     hToT=0x0;
1480     delete hSlewingProf;
1481     hSlewingProf=0x0;
1482     delete htimetot;
1483     htimetot=0x0;
1484     delete hdeltaTime;
1485     hdeltaTime=0x0;
1486   }
1487
1488   if(strstr(optionSave,"save") && fileProf){
1489     fileProf->Close();
1490     delete fileProf;
1491     fileProf=0x0;
1492   }
1493   WriteParOfflineOnCDB("TOF/Calib","valid");
1494
1495   return 0;
1496 }
1497 //----------------------------------------------------------------------------
1498 Int_t AliTOFcalib::CalibrateFromProfile(Int_t ich, Option_t *optionSave, Option_t *optionFit){
1499
1500   // computing calibration parameters using the old profiling algo
1501   // Returning codes:
1502   // 0 -> everything was ok
1503   // 1 -> no tree for calibration found
1504   // 2 -> not enough statistics to perform calibration
1505   // 3 -> problems with arrays
1506
1507   TH1::AddDirectory(0);
1508
1509   AliInfo(Form("*** Calibrating Histograms From Profile %s", GetName())) ; 
1510   AliInfo(Form("Option for Saving histos = %s",optionSave )) ; 
1511   AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ; 
1512   Float_t p[MAXCHENTRIESSMALL];
1513   Int_t nentries;
1514   Int_t ntracksTotal=0;
1515   //fTree->SetBranchAddress("nentries",&nentries);
1516   //fTree->SetBranchAddress("TOFentries",p);
1517   fChain->SetBranchAddress("nentries",&nentries);
1518   fChain->SetBranchAddress("TOFentries",p);
1519
1520   for (Int_t irun=0;irun<fNruns;irun++){
1521     Int_t i = ich+irun*fNChannels;
1522     //fTree->GetEntry(i);
1523     fChain->GetEntry(i);
1524     ntracksTotal+=nentries/3;
1525   }
1526
1527   if (ntracksTotal < MEANENTRIES) {  
1528     AliInfo(Form(" Too small mean number of entires per channel (mean number = %d) not calibrating and exiting.....",ntracksTotal));
1529     return 2;
1530   }
1531
1532   TH1F * hProf = Profile(ich);
1533   hProf->Fit("pol5",optionFit,"",0,4);
1534   TF1 * calibfunc = (TF1*)hProf->GetFunction("pol5");
1535   Float_t par[6];    
1536   for(Int_t kk=0;kk<6;kk++){
1537     par[kk]=calibfunc->GetParameter(kk);
1538     AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
1539   }
1540
1541   if(strstr(optionSave,"save")){
1542     TFile * fileProf = new TFile("TOFCalibSave.root","recreate");
1543     fileProf->cd(); 
1544     TString profName=Form("Profile%06i",ich);
1545     hProf->Write(profName);
1546     fileProf->Close();
1547     delete fileProf;
1548     fileProf=0x0;
1549   }
1550
1551   delete hProf;
1552   hProf=0x0;
1553   AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(ich);
1554   calChannel->SetSlewPar(par);
1555   WriteParOfflineOnCDB("TOF/Calib","valid");
1556   return 0;
1557 }
1558 //----------------------------------------------------------------------------
1559 Int_t AliTOFcalib::Calibrate(Option_t *optionSave, Option_t *optionFit){
1560
1561   // calibrating the whole TOF
1562   // computing calibration parameters
1563   // Returning codes:
1564   // 0 -> everything was ok
1565   // 1 -> no tree for calibration found
1566   // 2 -> not enough statistics to perform calibration
1567   // 3 -> problems with arrays
1568
1569   TH1::AddDirectory(0);
1570
1571   AliInfo(Form("*** Calibrating Histograms %s, all channels", GetName())) ; 
1572   AliInfo(Form("Option for Saving histos = %s",optionSave )) ; 
1573   AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ; 
1574
1575   TFile * fileProf=0x0;
1576   if(strstr(optionSave,"save")){
1577     fileProf = new TFile("TOFCalibSave.root","recreate");
1578   }
1579
1580   Float_t p[MAXCHENTRIESSMALL];
1581   Int_t nentries;
1582   //fTree->SetBranchAddress("nentries",&nentries);
1583   //fTree->SetBranchAddress("TOFentries",p);
1584   fChain->SetBranchAddress("nentries",&nentries);
1585   fChain->SetBranchAddress("TOFentries",p);
1586
1587   Float_t ntracksTotalmean =0;
1588   for (Int_t ii=0; ii<fNChannels; ii++){
1589     for (Int_t irun=0;irun<fNruns;irun++){
1590       Int_t i = ii+irun*fNChannels;
1591       //fTree->GetEntry(i);
1592       fChain->GetEntry(i);
1593       ntracksTotalmean+=nentries/3;
1594     }
1595   }
1596
1597   ntracksTotalmean/=fNChannels;
1598   if (ntracksTotalmean < MEANENTRIES) {
1599     AliInfo(Form(" Too small mean number of entires per channel (mean number = %f) not calibrating and exiting.....",ntracksTotalmean));
1600     return 2;
1601   }
1602
1603   //filling ToT and Time arrays
1604
1605   Int_t nbinToT = 100;  // ToT bin width in Profile = 50.0 ps 
1606   Float_t minToT = 0;   // ns
1607   Float_t maxToT = 4.88;// ns
1608   for (Int_t ii=0; ii<fNChannels; ii++) {
1609     TH1F *hToT = new TH1F("htot","htot",nbinToT, minToT, maxToT);
1610     TH1F *hdeltaTime = new TH1F("hdeltaTime","hdeltaTime",200,2,4);
1611     TH2F * htimetot = new TH2F("htimetot","htimetot",nbinToT, minToT, maxToT,600,-5,10);
1612     if (ii%1000 == 0) AliDebug(1,Form("Calibrating channel %i ",ii));
1613     //Int_t i = 3;
1614     Int_t nusefulbins=0;
1615     Double_t binsProfile[101]; // sized larger than necessary, the correct 
1616                               // dim being set in the booking of the profile
1617     Int_t ntracksRun = 0;
1618     Int_t ntracksTotal = 0;
1619     for (Int_t irun=0;irun<fNruns;irun++){
1620       Int_t i = ii+irun*fNChannels;
1621       //fTree->GetEntry(i);
1622       fChain->GetEntry(i);
1623       ntracksTotal+=nentries/3;
1624     }
1625     if (ntracksTotal < MEANENTRIES) {
1626       AliInfo(Form(" Too small mean number of entires in channel %i (number of tracks = %d), not calibrating channel and continuing.....",ii,ntracksTotal));
1627       continue;
1628     }
1629     Float_t meantime=0;
1630     for (Int_t irun=0;irun<fNruns;irun++){
1631       Int_t i = ii+irun*fNChannels;
1632       //fTree->GetEntry(i);
1633       fChain->GetEntry(i);
1634       ntracksRun=nentries/3;
1635       for (Int_t j=0;j<ntracksRun;j++){
1636         Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1637         Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1638         Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1639         Float_t tot = p[idxexToT];
1640         hdeltaTime->Fill(p[idxexTime]-p[idxexExTime]);
1641         meantime+=p[idxexTime]-p[idxexExTime];
1642         hToT->Fill(tot);
1643       }
1644     }
1645     nusefulbins = FindBins(hToT,&binsProfile[0]);
1646     meantime/=ntracksTotal;
1647     for (Int_t j=0;j<nusefulbins;j++) {
1648       AliDebug(2,Form(" channel %i, usefulbin = %i, low edge = %f",ii,j,binsProfile[j])); 
1649     }
1650     TProfile* hSlewingProf = new TProfile("hSlewingProf", "hSlewingProf",nusefulbins, binsProfile, "G");  // CHECK THE BUILD OPTION, PLEASE!!!!!!
1651     for (Int_t irun=0;irun<fNruns;irun++){
1652       Int_t i = ii+irun*fNChannels;
1653       //fTree->GetEntry(i);
1654       fChain->GetEntry(i);
1655       ntracksRun=nentries/3;
1656       for (Int_t j=0;j<ntracksRun;j++){
1657         Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1658         Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1659         Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1660         Float_t tot = p[idxexToT];
1661         Float_t time = p[idxexTime]-p[idxexExTime];
1662         AliDebug (2,Form("track = %i, time = %f, tot = %f, time-meantime = %f",j,time, tot, time-meantime));
1663         hSlewingProf->Fill(tot,time);  // if meantime is not used, the fill may be moved in the loop above
1664         htimetot->Fill(tot,time-meantime);  // if meantime is not used, the fill may be moved in the loop above
1665       }
1666     }
1667     hSlewingProf->Fit("pol5",optionFit,"",1,4);
1668     TF1 * calibfunc = (TF1*)hSlewingProf->GetFunction("pol5");
1669     Float_t par[6];    
1670     for(Int_t kk=0;kk<6;kk++){
1671       par[kk]=calibfunc->GetParameter(kk);
1672       AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
1673     }
1674
1675     if(strstr(optionSave,"save") && fileProf){
1676       TString profName=Form("Profile%06i",ii);
1677       TString timeTotName=Form("TimeTot%06i",ii);
1678       TString totName=Form("Tot%06i",ii);
1679       TString deltaName=Form("Delta%06i",ii);
1680       fileProf->cd();
1681       hSlewingProf->Write(profName);
1682       htimetot->Write(timeTotName);
1683       hToT->Write(totName);
1684       hdeltaTime->Write(deltaName);
1685     }
1686     AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(ii);
1687     calChannel->SetSlewPar(par);
1688
1689     delete hToT;
1690     hToT=0x0;
1691     delete hSlewingProf;
1692     hSlewingProf=0x0;
1693     delete htimetot;
1694     htimetot=0x0;
1695     delete hdeltaTime;
1696     hdeltaTime=0x0;
1697   }
1698
1699   if(strstr(optionSave,"save")){
1700     fileProf->Close();
1701     delete fileProf;
1702     fileProf=0x0;
1703   }
1704   WriteParOfflineOnCDB("TOF/Calib","valid");
1705   return 0;
1706 }
1707
1708 //-----------------------------------------------------------------------
1709 TH1F* AliTOFcalib::Profile(Int_t ich)
1710 {
1711   // profiling algo
1712
1713   Float_t p[MAXCHENTRIESSMALL];
1714   Int_t nentries;
1715   //fTree->SetBranchAddress("nentries",&nentries);
1716   //fTree->SetBranchAddress("TOFentries",p);
1717   fChain->SetBranchAddress("nentries",&nentries);
1718   fChain->SetBranchAddress("TOFentries",p);
1719
1720   //Prepare histograms for Slewing Correction
1721   const Int_t knbinToT = 100;
1722   Int_t nbinTime = 200;
1723   Float_t minTime = -5.5; //ns
1724   Float_t maxTime = 5.5; //ns
1725   Float_t minToT = 0; //ns
1726   Float_t maxToT = 5.; //ns
1727   Float_t deltaToT = (maxToT-minToT)/knbinToT;
1728   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];
1729   Int_t n[knbinToT+1], nentrx[knbinToT+1];
1730   Double_t sigmaToT[knbinToT+1];
1731   for (Int_t i = 0; i < knbinToT+1 ; i++){
1732     mTime[i]=0;
1733     mToT[i]=0;
1734     n[i]=0;
1735     meanTime[i]=0;
1736     meanTime2[i]=0;
1737     vToT[i]=0;
1738     vToT2[i]=0;
1739     meanToT[i]=0;
1740     meanToT2[i]=0;
1741     vTime[i]=0;
1742     vTime2[i]=0;
1743     xlow[i]=0;
1744     sigmaTime[i]=0;
1745     sigmaToT[i]=0;
1746     n[i]=0;
1747     nentrx[i]=0;
1748   }
1749   TH2F* hSlewing = new TH2F("hSlewing", "hSlewing", knbinToT, minToT, maxToT, nbinTime, minTime, maxTime);
1750   Int_t ntracksRun = 0;
1751   TH1F *histo = new TH1F("histo", "1D Time vs ToT", knbinToT, minToT, maxToT);
1752   for (Int_t irun=0;irun<fNruns;irun++){
1753     Int_t i = ich+irun*fNChannels;
1754     //fTree->GetEntry(i);
1755     fChain->GetEntry(i);
1756     ntracksRun=nentries/3;
1757     for (Int_t j=0;j<ntracksRun;j++){
1758       Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1759       Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1760       Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1761       Float_t tot = p[idxexToT];
1762       Float_t time = p[idxexTime]-p[idxexExTime];
1763       Int_t nx = (Int_t)((tot-minToT)/deltaToT)+1;
1764       if ((tot != 0) && ( time!= 0)){
1765         vTime[nx]+=time;
1766         vTime2[nx]+=time*time;
1767         vToT[nx]+=tot;
1768         vToT2[nx]+=tot*tot;
1769         nentrx[nx]++;
1770         hSlewing->Fill(tot,time);
1771       }
1772     }
1773   }
1774   Int_t nbinsToT=hSlewing->GetNbinsX();
1775   if (nbinsToT != knbinToT) {
1776     AliError("Profile :: incompatible numbers of bins");
1777     return 0x0;
1778   }
1779   
1780   Int_t usefulBins=0;
1781   for (Int_t i=1;i<=nbinsToT;i++){
1782     if (nentrx[i]!=0){
1783       n[usefulBins]+=nentrx[i];
1784       if (n[usefulBins]==0 && i == nbinsToT) {
1785         break;
1786       }
1787       meanTime[usefulBins]+=vTime[i];
1788       meanTime2[usefulBins]+=vTime2[i];
1789       meanToT[usefulBins]+=vToT[i];
1790       meanToT2[usefulBins]+=vToT2[i];
1791       if (n[usefulBins]<10 && i!=nbinsToT) continue; 
1792       mTime[usefulBins]=meanTime[usefulBins]/n[usefulBins];
1793       mToT[usefulBins]=meanToT[usefulBins]/n[usefulBins];
1794       sigmaTime[usefulBins]=TMath::Sqrt(1./n[usefulBins]/n[usefulBins]
1795                             *(meanTime2[usefulBins]-meanTime[usefulBins]
1796                             *meanTime[usefulBins]/n[usefulBins]));
1797       if ((1./n[usefulBins]/n[usefulBins]
1798            *(meanToT2[usefulBins]-meanToT[usefulBins]
1799              *meanToT[usefulBins]/n[usefulBins]))< 0) {
1800         AliError(" too small radical" );
1801         sigmaToT[usefulBins]=0;
1802       }
1803       else{       
1804         sigmaToT[usefulBins]=TMath::Sqrt(1./n[usefulBins]/n[usefulBins]
1805                              *(meanToT2[usefulBins]-meanToT[usefulBins]
1806                              *meanToT[usefulBins]/n[usefulBins]));
1807       }
1808       usefulBins++;
1809     }
1810   }
1811   for (Int_t i=0;i<usefulBins;i++){
1812     Int_t binN = (Int_t)((mToT[i]-minToT)/deltaToT)+1;
1813     histo->Fill(mToT[i],mTime[i]);
1814     histo->SetBinError(binN,sigmaTime[i]);
1815   } 
1816   delete hSlewing;
1817   hSlewing=0x0;
1818
1819   return histo;
1820 }
1821 //----------------------------------------------------------------------------
1822 Int_t AliTOFcalib::FindBins(TH1F* h, Double_t *binsProfile) const{
1823
1824   // to determine the bins for ToT histo
1825
1826   Int_t cont = 0;
1827   Int_t startBin = 1;
1828   Int_t nbin = h->GetNbinsX();
1829   Int_t nentries = (Int_t)h->GetEntries();
1830   Float_t max = h->GetBinLowEdge(nbin);
1831   Int_t nusefulbins=0;
1832   Int_t maxcont=0;
1833   // setting maxvalue of entries per bin
1834   if (nentries <= 60) maxcont = 2;
1835   else  if (nentries <= 100) maxcont = 5;
1836   else  if (nentries <= 500) maxcont = 10;
1837   else  maxcont = 20;
1838   for (Int_t j=1;j<=nbin;j++) {
1839     cont += (Int_t)h->GetBinContent(j);
1840     if (j<nbin){
1841       if (cont>=maxcont){
1842         nusefulbins++;
1843         binsProfile[nusefulbins-1]=h->GetBinLowEdge(startBin);
1844         cont=0;
1845         startBin=j+1;
1846         continue;
1847       }
1848     }
1849     else{
1850       if (cont>=maxcont){
1851         nusefulbins++;
1852         binsProfile[nusefulbins-1]=h->GetBinLowEdge(startBin);
1853         binsProfile[nusefulbins]=max;
1854       }
1855       else {
1856         binsProfile[nusefulbins]=h->GetBinLowEdge(startBin);
1857       }
1858     }
1859   }
1860   return nusefulbins;
1861 }
1862
1863
1864 //----------------------------------------------------------------------------
1865
1866 void
1867 AliTOFcalib::CreateDeltaBCOffset()
1868 {
1869   /*
1870    * create deltaBC offset
1871    */
1872
1873   if (fDeltaBCOffset) {
1874     AliWarning("DeltaBCOffset object already defined, cannot create a new one");
1875     return;
1876   }
1877   fDeltaBCOffset = new AliTOFDeltaBCOffset();
1878 }
1879   
1880 //----------------------------------------------------------------------------
1881
1882 void
1883 AliTOFcalib::CreateCTPLatency()
1884 {
1885   /*
1886    * create CTP latency
1887    */
1888
1889   if (fCTPLatency) {
1890     AliWarning("CTPLatency object already defined, cannot create a new one");
1891     return;
1892   }
1893   fCTPLatency = new AliTOFCTPLatency();
1894 }
1895   
1896 //----------------------------------------------------------------------------
1897
1898 void
1899 AliTOFcalib::CreateT0Fill()
1900 {
1901   /*
1902    * create event-time
1903    */
1904
1905   if (fT0Fill) {
1906     AliWarning("T0Fill object already defined, cannot create a new one");
1907     return;
1908   }
1909   fT0Fill = new AliTOFT0Fill();
1910 }
1911   
1912 //----------------------------------------------------------------------------
1913
1914 void
1915 AliTOFcalib::CreateRunParams()
1916 {
1917   /*
1918    * create run params
1919    */
1920
1921   if (fRunParams) {
1922     AliWarning("RunParams object already defined, cannot create a new one");
1923     return;
1924   }
1925   fRunParams = new AliTOFRunParams();
1926 }
1927   
1928 //----------------------------------------------------------------------------
1929
1930 void
1931 AliTOFcalib::WriteDeltaBCOffsetOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
1932 {
1933   /*
1934    * deltaBC offset on CDB 
1935    */
1936   
1937   if (!fDeltaBCOffset) return;
1938   AliCDBId id(Form("%s/DeltaBCOffset", sel), minrun, maxrun);
1939   AliCDBMetaData *md = new AliCDBMetaData();
1940   md->SetResponsible("Roberto Preghenella");
1941   AliCDBManager *man = AliCDBManager::Instance();
1942   man->Put(fDeltaBCOffset, id, md);
1943   AliDebug(2,Form("DeltaBCOffset written on CDB with run range [%i, %i] ",minrun ,maxrun));
1944   delete md;
1945 }
1946
1947 //----------------------------------------------------------------------------
1948
1949 void
1950 AliTOFcalib::WriteCTPLatencyOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
1951 {
1952   /*
1953    * write CTP latency on CDB 
1954    */
1955   
1956   if (!fCTPLatency) return;
1957   AliCDBId id(Form("%s/CTPLatency", sel), minrun, maxrun);
1958   AliCDBMetaData *md = new AliCDBMetaData();
1959   md->SetResponsible("Roberto Preghenella");
1960   AliCDBManager *man = AliCDBManager::Instance();
1961   man->Put(fCTPLatency, id, md);
1962   AliDebug(2,Form("CTPLatency written on CDB with run range [%i, %i] ",minrun ,maxrun));
1963   delete md;
1964 }
1965
1966 //----------------------------------------------------------------------------
1967
1968 void
1969 AliTOFcalib::WriteT0FillOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
1970 {
1971   /*
1972    * write event-time on CDB 
1973    */
1974   
1975   if (!fT0Fill) return;
1976   AliCDBId id(Form("%s/T0Fill", sel), minrun, maxrun);
1977   AliCDBMetaData *md = new AliCDBMetaData();
1978   md->SetResponsible("Roberto Preghenella");
1979   AliCDBManager *man = AliCDBManager::Instance();
1980   man->Put(fT0Fill, id, md);
1981   AliDebug(2,Form("T0Fill written on CDB with run range [%i, %i] ",minrun ,maxrun));
1982   delete md;
1983 }
1984
1985 //----------------------------------------------------------------------------
1986
1987 void
1988 AliTOFcalib::WriteRunParamsOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
1989 {
1990   /*
1991    * write run params on CDB 
1992    */
1993   
1994   if (!fRunParams) return;
1995   AliCDBId id(Form("%s/RunParams", sel), minrun, maxrun);
1996   AliCDBMetaData *md = new AliCDBMetaData();
1997   md->SetResponsible("Roberto Preghenella");
1998   AliCDBManager *man = AliCDBManager::Instance();
1999   man->Put(fRunParams, id, md);
2000   AliDebug(2,Form("RunParams written on CDB with run range [%i, %i] ",minrun ,maxrun));
2001   delete md;
2002 }
2003
2004 //----------------------------------------------------------------------------
2005
2006 Bool_t
2007 AliTOFcalib::ReadDeltaBCOffsetFromCDB(const Char_t *sel , Int_t nrun)
2008 {
2009   /*
2010    * read deltaBC offset from CDB
2011    */
2012   
2013   AliCDBManager *man = AliCDBManager::Instance();
2014   AliCDBEntry *entry = man->Get(Form("%s/DeltaBCOffset", sel),nrun);
2015   if (!entry) { 
2016     AliFatal("No DeltaBCOffset entry found in CDB");
2017     exit(0);  
2018   }
2019   fDeltaBCOffset =(AliTOFDeltaBCOffset *)entry->GetObject();
2020   if(!fDeltaBCOffset){
2021     AliFatal("No DeltaBCOffset object found in CDB entry");
2022     exit(0);  
2023   }  
2024   return kTRUE; 
2025 }
2026
2027 //----------------------------------------------------------------------------
2028
2029 Bool_t
2030 AliTOFcalib::ReadCTPLatencyFromCDB(const Char_t *sel , Int_t nrun)
2031 {
2032   /*
2033    * read CTP latency from CDB
2034    */
2035   
2036   AliCDBManager *man = AliCDBManager::Instance();
2037   AliCDBEntry *entry = man->Get(Form("%s/CTPLatency", sel),nrun);
2038   if (!entry) { 
2039     AliFatal("No CTPLatency entry found in CDB");
2040     exit(0);  
2041   }
2042   fCTPLatency =(AliTOFCTPLatency *)entry->GetObject();
2043   if(!fCTPLatency){
2044     AliFatal("No CTPLatency object found in CDB entry");
2045     exit(0);  
2046   }  
2047   return kTRUE; 
2048 }
2049
2050 //----------------------------------------------------------------------------
2051
2052 Bool_t
2053 AliTOFcalib::ReadT0FillFromCDB(const Char_t *sel , Int_t nrun)
2054 {
2055   /*
2056    * read event-time from CDB
2057    */
2058   
2059   AliCDBManager *man = AliCDBManager::Instance();
2060   AliCDBEntry *entry = man->Get(Form("%s/T0Fill", sel),nrun);
2061   if (!entry) { 
2062     AliFatal("No T0Fill entry found in CDB");
2063     exit(0);  
2064   }
2065   fT0Fill =(AliTOFT0Fill *)entry->GetObject();
2066   if(!fT0Fill){
2067     AliFatal("No T0Fill object found in CDB entry");
2068     exit(0);  
2069   }  
2070   return kTRUE; 
2071 }
2072
2073 //----------------------------------------------------------------------------
2074
2075 Bool_t
2076 AliTOFcalib::ReadRunParamsFromCDB(const Char_t *sel , Int_t nrun)
2077 {
2078   /*
2079    * read run params from CDB
2080    */
2081   
2082   AliCDBManager *man = AliCDBManager::Instance();
2083   AliCDBEntry *entry = man->Get(Form("%s/RunParams", sel),nrun);
2084   if (!entry) { 
2085     AliFatal("No RunParams entry found in CDB");
2086     exit(0);  
2087   }
2088   fRunParams =(AliTOFRunParams *)entry->GetObject();
2089   if(!fRunParams){
2090     AliFatal("No RunParams object found in CDB entry");
2091     exit(0);  
2092   }  
2093   return kTRUE; 
2094 }
2095
2096 //----------------------------------------------------------------------------
2097
2098 Bool_t 
2099 AliTOFcalib::Init(Int_t run)
2100 {
2101   /*
2102    * init
2103    */
2104
2105   if (fInitFlag) {
2106     AliWarning("the class was already initialized, re-initialize it");
2107     fInitFlag = kFALSE;
2108   }
2109   
2110   /* read channel status array */
2111   if (!ReadParOnlineStatusFromCDB("TOF/Calib", run)) {
2112     AliError("cannot get \"Status\" object from OCDB");
2113     return kFALSE;
2114   }
2115   /* get par offline array */
2116   if (!ReadParOfflineFromCDB("TOF/Calib", run)) {
2117     AliError("cannot get \"ParOffline\" object from OCDB");
2118     return kFALSE;
2119   }
2120   /* get deltaBC offset obj */
2121   if (!ReadDeltaBCOffsetFromCDB("TOF/Calib", run)) {
2122     AliError("cannot get \"DeltaBCOffset\" object from OCDB");
2123     return kFALSE;
2124   }
2125   /* get CTP latency obj */
2126   if (!ReadCTPLatencyFromCDB("TOF/Calib", run)) {
2127     AliError("cannot get \"CTPLatency\" object from OCDB");
2128     return kFALSE;
2129   }
2130   /* get run params obj */
2131   if (!ReadRunParamsFromCDB("TOF/Calib", run)) {
2132     AliError("cannot get \"RunParams\" object from OCDB");
2133     return kFALSE;
2134   }
2135   /* get response params */
2136   if (fCorrectTExp) {
2137     TFile *responseFile = TFile::Open("$ALICE_ROOT/TOF/data/AliTOFresponsePar.root");
2138     if (!responseFile || !responseFile->IsOpen()) {
2139       AliError("cannot open \"ResponseParams\" local file");
2140       return kFALSE;
2141     }
2142     fResponseParams = (AliTOFResponseParams *)responseFile->Get("ResponseParams");
2143     if (!fResponseParams) {
2144       AliError("cannot get \"ResponseParams\" object from local file");
2145       return kFALSE;
2146     }
2147     responseFile->Close();
2148   }
2149
2150   /* all done */
2151   fInitFlag = kTRUE;
2152   return kTRUE;
2153
2154 }
2155
2156 //----------------------------------------------------------------------------
2157
2158 Double_t
2159 AliTOFcalib::GetTimeCorrection(Int_t index, Double_t tot, Int_t deltaBC, Int_t l0l1, UInt_t timestamp)
2160 {
2161   /*
2162    * get time correction
2163    */
2164
2165   if (!fInitFlag) {
2166     AliError("class not yet initialized. Initialize it before.");
2167     return 0.;
2168   }
2169
2170   /* get calibration params */
2171   AliTOFChannelOffline *parOffline = (AliTOFChannelOffline *)fTOFCalOffline->At(index);
2172   Int_t deltaBCOffset = fDeltaBCOffset->GetDeltaBCOffset();
2173   Float_t ctpLatency = fCTPLatency->GetCTPLatency();
2174   Float_t tdcLatencyWindow = fStatus->GetLatencyWindow(index) * 1.e3;
2175   Float_t timezero = fRunParams->EvalT0(timestamp);
2176   /* check whether to remove mean T0.
2177    * useful when one wants to compute mean T0 */
2178   if (!fRemoveMeanT0) timezero = 0.;
2179
2180   /* compute correction */
2181   Double_t corr = 0.;
2182   /* deltaBC correction */
2183   deltaBC = deltaBCOffset; /* inhibit deltaBC correction for the time being */
2184   corr += (deltaBC - deltaBCOffset) * AliTOFGeometry::BunchCrossingBinWidth();
2185   /* L0-L1 latency correction */
2186   corr -= l0l1 * AliTOFGeometry::BunchCrossingBinWidth();
2187   /* CTP latency correction */
2188   corr -= ctpLatency;
2189   /* TDC latency window correction */
2190   corr += tdcLatencyWindow;
2191   /* time-zero correction */
2192   corr += timezero;
2193   /* time calibration correction */
2194   if (tot < AliTOFGeometry::SlewTOTMin()) 
2195     tot = AliTOFGeometry::SlewTOTMin();
2196   if (tot > AliTOFGeometry::SlewTOTMax()) 
2197     tot = AliTOFGeometry::SlewTOTMax();
2198   for (Int_t islew = 0; islew < 6; islew++)
2199     corr += parOffline->GetSlewPar(islew) * TMath::Power(tot, islew) * 1.e3;
2200
2201   /* return correction */
2202   return corr;
2203 }
2204
2205 //----------------------------------------------------------------------------
2206
2207 void
2208 AliTOFcalib::CalibrateESD(AliESDEvent *event)
2209 {
2210   /*
2211    * calibrate ESD
2212    */
2213
2214   if (!fInitFlag) {
2215     AliError("class not yet initialized. Initialize it before.");
2216     return;
2217   }
2218
2219   /* loop over tracks */
2220   AliESDtrack *track = NULL;
2221   Int_t index, l0l1, deltaBC;
2222   Double_t time, tot, corr, texp[AliPID::kSPECIES];
2223   UInt_t timestamp = event->GetTimeStamp();
2224   for (Int_t itrk = 0; itrk < event->GetNumberOfTracks(); itrk++) {
2225
2226     /* get track */
2227     track = event->GetTrack(itrk);
2228     if (!track || !(track->GetStatus() & AliESDtrack::kTOFout)) continue;
2229
2230     /* calibrate TOF signal */
2231     if (fCalibrateTOFsignal) {
2232       /* get info */
2233       index = track->GetTOFCalChannel();
2234       time = track->GetTOFsignalRaw();
2235       tot = track->GetTOFsignalToT();
2236       l0l1 = track->GetTOFL0L1();
2237       deltaBC = track->GetTOFDeltaBC();
2238       /* get correction */
2239       corr = GetTimeCorrection(index, tot, deltaBC, l0l1, timestamp);
2240       /* apply correction */
2241       time -= corr;
2242       /* set new TOF signal */
2243       track->SetTOFsignal(time);
2244     }
2245
2246     /* correct expected time */
2247     if (fCorrectTExp) {
2248       /* get integrated times */
2249       track->GetIntegratedTimes(texp);
2250       /* loop over particle types and correct expected time */
2251       for (Int_t ipart = 0; ipart < AliPID::kSPECIES; ipart++)
2252         texp[ipart] += fResponseParams->EvalTExpCorr(ipart, track->P());
2253       /* set integrated times */
2254       track->SetIntegratedTimes(texp);
2255     }
2256
2257   }
2258
2259 }
2260
2261 //----------------------------------------------------------------------------
2262
2263 Bool_t
2264 AliTOFcalib::IsChannelEnabled(Int_t index)
2265 {
2266   /*
2267    * is channel enabled
2268    */
2269
2270   if (!fInitFlag) {
2271     AliError("class not yet initialized. Initialize it before.");
2272     return kTRUE;
2273   }
2274
2275   /* check bad status */
2276   if (fStatus->GetPulserStatus(index) == AliTOFChannelOnlineStatusArray::kTOFPulserBad) return kFALSE;
2277   if (fStatus->GetNoiseStatus(index) == AliTOFChannelOnlineStatusArray::kTOFNoiseBad) return kFALSE;
2278   if (fStatus->GetHWStatus(index) == AliTOFChannelOnlineStatusArray::kTOFHWBad) return kFALSE;
2279   
2280   /* good status */
2281   return kTRUE;
2282
2283 }