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