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