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