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