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