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