]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TOF/AliTOFcalib.cxx
reducing macro to minimum AliReconstruction functionality, disable QA and TriggerESD...
[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   AliCDBEntry *entry = man->Get(out,nrun);
464   if (!entry) { 
465     AliFatal("Exiting, no CDB object (ParOnline) found!!!");
466     exit(0);  
467   }
468   if(!entry->GetObject()){
469     AliFatal("Exiting, no CDB object (ParOnline) found!!!");
470     exit(0);  
471   }  
472   
473   fTOFCalOnline =(TObjArray*)entry->GetObject();
474
475   return kTRUE; 
476    
477 }
478 //_____________________________________________________________________________
479
480 Bool_t AliTOFcalib::ReadParOnlinePulserFromCDB(Char_t *sel, Int_t nrun)
481 {
482   //Read calibration parameters from pulser from the CDB
483   AliCDBManager *man = AliCDBManager::Instance();
484   Char_t *sel1 = "Pulser" ;
485   Char_t  out[100];
486   sprintf(out,"%s/%s",sel,sel1); 
487   AliCDBEntry *entry = man->Get(out,nrun);
488   if (!entry) { 
489     AliFatal("Exiting, no CDB object (Pulser) found!!!");
490     exit(0);  
491   }
492   if(!entry->GetObject()){
493     AliFatal("Exiting, no CDB object (Pulser) found!!!");
494     exit(0);  
495   }  
496   
497   fTOFCalOnlinePulser =(TObjArray*)entry->GetObject();
498
499   return kTRUE; 
500    
501 }
502 //_____________________________________________________________________________
503
504 Bool_t AliTOFcalib::ReadParOnlineNoiseFromCDB(Char_t *sel, Int_t nrun)
505 {
506   //Read calibration parameters from noise from the CDB
507   AliCDBManager *man = AliCDBManager::Instance();
508   Char_t *sel1 = "Noise" ;
509   Char_t  out[100];
510   sprintf(out,"%s/%s",sel,sel1); 
511   AliCDBEntry *entry = man->Get(out,nrun);
512   if (!entry) { 
513     AliFatal("Exiting, no CDB object (Noise) found!!!");
514     exit(0);  
515   }
516   if(!entry->GetObject()){
517     AliFatal("Exiting, no CDB object (Noise) found!!!");
518     exit(0);  
519   }  
520   
521   fTOFCalOnlineNoise =(TObjArray*)entry->GetObject();
522
523   return kTRUE; 
524    
525 }
526 //_____________________________________________________________________________
527
528 Bool_t AliTOFcalib::ReadParOnlineHWFromCDB(Char_t *sel, Int_t nrun)
529 {
530   //Read calibration parameters from hardware from the CDB
531   AliCDBManager *man = AliCDBManager::Instance();
532   Char_t *sel1 = "HW" ;
533   Char_t  out[100];
534   sprintf(out,"%s/%s",sel,sel1); 
535   AliCDBEntry *entry = man->Get(out,nrun);
536   if (!entry) { 
537     AliFatal("Exiting, no CDB object (HW map) found!!!");
538     exit(0);  
539   }
540   if(!entry->GetObject()){
541     AliFatal("Exiting, no CDB object (HW map) found!!!");
542     exit(0);  
543   }  
544   
545   fTOFCalOnlineHW =(TObjArray*)entry->GetObject();
546
547   return kTRUE; 
548    
549 }
550 //_____________________________________________________________________________
551
552 Bool_t AliTOFcalib::ReadParOfflineFromCDB(Char_t *sel, Int_t nrun)
553 {
554   //Read calibration parameters from the CDB
555   AliCDBManager *man = AliCDBManager::Instance();
556   Char_t *sel1 = "ParOffline" ;
557   Char_t  out[100];
558   sprintf(out,"%s/%s",sel,sel1); 
559   AliCDBEntry *entry = man->Get(out,nrun);
560   if (!entry) { 
561     AliFatal("Exiting, no CDB object (ParOffline) found!!!");
562     exit(0);  
563   }
564   if(!entry->GetObject()){
565     AliFatal("Exiting, no CDB object (ParOffline) found!!!");
566     exit(0);  
567   }  
568   AliCDBMetaData * md = entry->GetMetaData();
569   fkValidity = md->GetComment();  
570   fTOFCalOffline =(TObjArray*)entry->GetObject();
571
572   return kTRUE; 
573    
574 }
575 //_____________________________________________________________________________
576 void AliTOFcalib::WriteSimHistoOnCDB(Char_t *sel, Int_t minrun, Int_t maxrun, TH1F *histo){
577   //Write Sim miscalibration parameters to the CDB
578
579   fTOFSimToT=histo;
580   AliCDBManager *man = AliCDBManager::Instance();
581   Char_t *sel1 = "SimHisto" ;
582   Char_t  out[100];
583   sprintf(out,"%s/%s",sel,sel1); 
584   AliCDBMetaData *mdhisto = new AliCDBMetaData();
585   mdhisto->SetResponsible("Chiara Zampolli");
586   AliCDBId id(out,minrun,maxrun);
587   man->Put(fTOFSimToT,id,mdhisto);
588   delete mdhisto;
589 }
590 //_____________________________________________________________________________
591 Bool_t AliTOFcalib::ReadSimHistoFromCDB(Char_t *sel, Int_t nrun)
592 {
593   //Read miscalibration parameters from the CDB
594   AliCDBManager *man = AliCDBManager::Instance();
595
596   // The Tot Histo
597
598   Char_t *sel1 = "SimHisto" ;
599   Char_t  out[100];
600   sprintf(out,"%s/%s",sel,sel1); 
601   AliCDBEntry *entry = man->Get(out,nrun);
602   if (!entry) { 
603     AliFatal("Exiting, no CDB object (SimHisto) found!!!");
604     exit(0);  
605   }
606   if(!entry->GetObject()){
607     AliFatal("Exiting, no CDB object (SimHisto) found!!!");
608     exit(0);  
609   }  
610   TH1F *histo =(TH1F*)entry->GetObject();
611   fTOFSimToT=histo;
612   return kTRUE;
613 }
614 //_____________________________________________________________________________
615 void AliTOFcalib::WriteRecParOnCDB(Char_t *sel, Int_t minrun, Int_t maxrun, AliTOFRecoParam *param){
616   //Write reconstruction parameters to the CDB
617
618   AliCDBManager *man = AliCDBManager::Instance();
619   AliCDBMetaData *md = new AliCDBMetaData();
620   md->SetResponsible("Silvia Arcelli");
621   Char_t *sel1 = "RecPar" ;
622   Char_t  out[100];
623   sprintf(out,"%s/%s",sel,sel1); 
624   AliCDBId id(out,minrun,maxrun);
625   man->Put(param,id,md);
626   delete md;
627 }
628 //_____________________________________________________________________________
629 AliTOFRecoParam * AliTOFcalib::ReadRecParFromCDB(Char_t *sel, Int_t nrun)
630 {
631   //Read reconstruction parameters from the CDB
632   AliCDBManager *man = AliCDBManager::Instance();
633   Char_t *sel1 = "RecPar" ;
634   Char_t  out[100];
635   sprintf(out,"%s/%s",sel,sel1); 
636   AliCDBEntry *entry = man->Get(out,nrun);
637   if (!entry) { 
638     AliFatal("Exiting, no CDB object (RecPar) found!!!");
639     exit(0);  
640   }  
641   if(!entry->GetObject()){
642     AliFatal("Exiting, no CDB object (RecPar) found!!!");
643     exit(0);  
644   }  
645
646   AliTOFRecoParam *param=(AliTOFRecoParam*)entry->GetObject();
647   return param;
648 }
649 //-----------------------------------------------------------------------------
650 // Calibration methods
651 //-----------------------------------------------------------------------------
652 void AliTOFcalib::CreateTreeFromCDB(Int_t minrun, Int_t maxrun){
653
654   // creating the chain with the trees for calibration
655   // collecting them from reference data 
656   // from minrun to maxrun
657
658   Float_t p[CHENTRIESSMALL];
659   Int_t nentries;
660   fTree = new TTree("TOFCalib","Tree for TOF Calibration");
661   fTree->Branch("nentries",&nentries,"nentries/I");
662   fTree->Branch("TOFentries",p,"TOFentries[nentries]/F");
663   AliCDBManager *man = AliCDBManager::Instance();
664   AliCDBStorage *aStorage = man->GetStorage("local://$ALICE_ROOT");
665   for (Int_t irun = minrun;irun<=maxrun;irun++){
666     AliCDBEntry *entry = aStorage->Get("TOF/RefData/TreeForCalib",irun);
667     if (!entry){
668       AliInfo(Form("No entry found for run %i",irun));
669     }
670     else{
671       TTree *tree = new TTree();
672       tree = (TTree*)entry->GetObject();
673       tree->SetBranchAddress("nentries",&nentries);
674       tree->SetBranchAddress("TOFentries",p);      
675       fTree->CopyEntries(tree);
676       delete tree;
677       fNruns++;
678     }
679   }
680   AliInfo(Form("Number of runs being analyzed %i",fNruns));
681 }
682 //-----------------------------------------------------------------------------
683 void AliTOFcalib::CreateTreeFromGrid(Int_t minrun, Int_t maxrun){
684
685   // creating the chain with the trees for calibration
686   // collecting them from the Grid 
687   // from minrun to maxrun
688
689   Float_t p[CHENTRIESSMALL];
690   Int_t nentries;
691   fTree = new TTree("TOFCalib","Tree for TOF Calibration");
692   fTree->SetDirectory(0);
693   fTree->Branch("nentries",&nentries,"nentries/I");
694   fTree->Branch("TOFentries",p,"TOFentries[nentries]/F");
695   AliInfo("connected to alien");
696   TGrid::Connect("alien://");
697   
698   Char_t filename[100];
699   for (Int_t irun = minrun;irun<=maxrun;irun++){
700     sprintf(filename,"alien:///alice/cern.ch/user/c/czampolli/TOFCalibReference_%i.root",irun);
701     TFile *filegrid = TFile::Open(filename,"READ");
702     TTree *tree = (TTree*)filegrid->Get("T");
703     tree->SetBranchAddress("nentries",&nentries);
704     tree->SetBranchAddress("TOFentries",p);      
705     fTree->CopyEntries(tree);
706     delete tree;
707     fNruns++;    
708   }
709   
710   AliInfo(Form("Number of runs being analyzed %i",fNruns));
711 }
712 //-----------------------------------------------------------------------------
713 void AliTOFcalib::CreateTreeFromFile(Int_t minrun, Int_t maxrun){
714
715   // creating the tree with the trees for calibration
716   // collecting them from reference data (from file)
717   // from minrun to maxrun
718
719   Float_t p[CHENTRIESSMALL];
720   Int_t nentries;
721   fTree = new TTree("TOFCalib","Tree for TOF Calibration");
722   fTree->SetDirectory(0);
723   fTree->Branch("nentries",&nentries,"nentries/I");
724   fTree->Branch("TOFentries",p,"TOFentries[nentries]/F");
725   Char_t filename[100];
726   for (Int_t irun = minrun;irun<=maxrun;irun++){
727     sprintf(filename,"$ALICE_ROOT/TOF/RefData/TreeForCalib/fileout_%i.root",irun);
728     TFile *file = new TFile(filename,"READ");
729     TTree *tree = (TTree*)file->Get("T");
730     tree->SetBranchAddress("nentries",&nentries);
731     tree->SetBranchAddress("TOFentries",p);      
732     fTree->CopyEntries(tree);
733     delete tree;
734     delete file;
735     file = 0x0;
736     fNruns++;
737   }
738
739   AliInfo(Form("Number of runs being analyzed %i",fNruns));
740 }
741 //-----------------------------------------------------------------------------
742 void AliTOFcalib::CreateChainFromGrid(Int_t minrun, Int_t maxrun){
743
744   // creating the chain with the trees for calibration
745   // collecting them from the Grid 
746   // from minrun to maxrun
747
748   fChain = new TChain("T");
749   AliInfo("connected to alien");
750   TGrid::Connect("alien://");
751   
752   Char_t filename[100];
753   for (Int_t irun = minrun;irun<=maxrun;irun++){
754     sprintf(filename,"alien:///alice/cern.ch/user/c/czampolli/TOFCalibReference_%i.root",irun);
755     fChain->Add(filename);
756     fNruns++;    
757   }
758   
759   AliInfo(Form("Number of runs being analyzed %i",fNruns));
760 }
761 //-----------------------------------------------------------------------------
762 Int_t AliTOFcalib::Calibrate(Int_t ichmin, Int_t ichmax, Option_t *optionSave, Option_t *optionFit){
763
764   // calibrating summing more than one channels
765   // computing calibration parameters
766   // Returning codes:
767   // 0 -> everything was ok
768   // 1 -> no tree for calibration found
769   // 2 -> not enough statistics to perform calibration
770   // 3 -> problems with arrays
771   
772   TH1::AddDirectory(0);
773
774   AliInfo(Form("*** Calibrating Histograms %s, summing more channels, from channel %i, to channel %i, storing Calib Pars in channel %i", GetName(),ichmin,ichmax,ichmin)) ; 
775   AliInfo(Form("Option for Saving histos = %s",optionSave )) ; 
776   AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ; 
777
778   Float_t p[CHENTRIESSMALL];
779   Int_t nentries;
780   //fTree->SetBranchAddress("nentries",&nentries);
781   //fTree->SetBranchAddress("TOFentries",p);
782   fChain->SetBranchAddress("nentries",&nentries);
783   fChain->SetBranchAddress("TOFentries",p);
784
785   Float_t ntracksTotalmean =0;
786   for (Int_t i=ichmin; i<ichmax; i++){
787     Int_t ientry = -1;
788     for (Int_t irun=0;irun<fNruns;irun++){
789       ientry = i+irun*fNChannels;
790       //fTree->GetEntry(ientry);
791       fChain->GetEntry(ientry);
792       Int_t ntracksRun=nentries/3;
793       ntracksTotalmean+=ntracksRun;
794     }
795   }
796   
797   if (ntracksTotalmean < MEANENTRIES) {
798     AliInfo(Form(" Too small mean number of entires per channel (mean number = %f) not calibrating and exiting.....",ntracksTotalmean));
799     return 2;
800   }
801
802   //filling ToT and Time arrays
803
804   Int_t nbinToT = 100;  // ToT bin width in Profile = 48.8 ps 
805   Float_t minToT = 0;   // ns
806   Float_t maxToT = 4.88;  // ns
807
808   TH1F *hToT = new TH1F("htot","htot",nbinToT, minToT, maxToT);
809   TH1F *hdeltaTime = new TH1F("hdeltaTime","hdeltaTime",200,2,4);
810   Int_t ntracksTotal = 0;
811   Int_t ntracksRun = 0;
812   Double_t binsProfile[101]; // sized larger than necessary, the correct 
813                              // dim being set in the booking of the profile
814   Int_t nusefulbins=0;
815   Float_t meantime=0;
816   for (Int_t i = ichmin;i<ichmax;i++){
817     Int_t ientry = -1;
818     for (Int_t irun=0;irun<fNruns;irun++){
819       ientry = i+irun*fNChannels;
820       //fTree->GetEntry(ientry);
821       fChain->GetEntry(ientry);
822       ntracksTotal+=nentries/3;
823       ntracksRun=nentries/3;
824       AliDebug(2,Form("run %i, channel %i, nentries = %i, ntracks = %i",irun,i,nentries, ntracksRun));
825       for (Int_t j=0;j<ntracksRun;j++){
826         Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
827         Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
828         Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
829         Float_t tot = p[idxexToT];
830         hdeltaTime->Fill(p[idxexTime]-p[idxexExTime]);
831         meantime+=p[idxexTime]-p[idxexExTime];
832         hToT->Fill(tot);
833       }
834     }
835   }
836   nusefulbins = FindBins(hToT,&binsProfile[0]);
837   meantime/=ntracksTotal;
838   AliDebug(2, Form("meantime = %f",meantime));
839   
840   for (Int_t j=1;j<=nusefulbins;j++) {
841     AliDebug(2,Form(" summing channels from %i to %i, nusefulbins = %i, bin %i = %f",ichmin,ichmax,nusefulbins,j,binsProfile[j])); 
842   }
843
844   TProfile* hSlewingProf = new TProfile("hSlewingProf", "hSlewingProf",nusefulbins, binsProfile, "G");  // CHECK THE BUILD OPTION, PLEASE!!!!!!
845   TH2F * htimetot = new TH2F("htimetot","htimetot",nbinToT, minToT, maxToT,600,-5,10);
846
847   for (Int_t irun=0;irun<fNruns;irun++){
848     Int_t ientry = -1;
849     for (Int_t i=ichmin; i<ichmax; i++){
850       ientry = i+irun*fNChannels;
851       //fTree->GetEntry(ientry);
852       fChain->GetEntry(ientry);
853       ntracksRun=nentries/3;
854       for (Int_t j=0;j<ntracksRun;j++){
855         Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
856         Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
857         Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
858         Float_t tot = p[idxexToT];
859         Float_t time = p[idxexTime]-p[idxexExTime];
860         AliDebug (2, Form("track = %i, time = %f, tot = %f, time-meantime = %f",j,time, tot, time-meantime));
861         hSlewingProf->Fill(tot,time);  // if meantime is not used, the fill may be moved in the loop above
862         htimetot->Fill(tot,time-meantime);  // if meantime is not used, the fill may be moved in the loop above
863       }
864     }
865   }
866
867   hSlewingProf->Fit("pol5",optionFit,"",0,4);
868   TF1 * calibfunc = (TF1*)hSlewingProf->GetFunction("pol5");
869   Float_t par[6];    
870   for(Int_t kk=0;kk<6;kk++){
871     par[kk]=calibfunc->GetParameter(kk);
872     AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
873   }
874
875   if(strstr(optionSave,"save")){
876     TFile * fileProf = new TFile("TOFCalibSave.root","recreate");
877     fileProf->cd(); 
878     TString profName=Form("Profile%06i_%06i",ichmin,ichmax);
879     TString timeTotName=Form("TimeTot%06i_%06i",ichmin,ichmax);
880     TString totName=Form("Tot%06i_%06i",ichmin,ichmax);
881     TString deltaName=Form("Delta%06i_%06i",ichmin,ichmax);
882     hSlewingProf->Write(profName);
883     htimetot->Write(timeTotName);
884     hToT->Write(totName);
885     hdeltaTime->Write(deltaName);
886     fileProf->Close();
887     delete fileProf;
888     fileProf=0x0;
889   }
890
891   delete hToT;
892   hToT=0x0;
893   delete hSlewingProf;
894   hSlewingProf=0x0;
895   delete htimetot;
896   htimetot=0x0;
897   delete hdeltaTime;
898   hdeltaTime=0x0;
899
900   AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(ichmin);
901   calChannel->SetSlewPar(par);
902   WriteParOfflineOnCDB("TOF/Calib","valid");
903   return 0;
904 }
905 //----------------------------------------------------------------------------
906 Int_t AliTOFcalib::Calibrate(Int_t i, Option_t *optionSave, Option_t *optionFit){
907
908   // computing calibration parameters for channel i
909   // Returning codes:
910   // 0 -> everything was ok
911   // 1 -> no tree for calibration found
912   // 2 -> not enough statistics to perform calibration
913   // 3 -> problems with arrays
914
915   TH1::AddDirectory(0);
916   
917   AliInfo(Form("*** Calibrating Histograms (one channel) %s", GetName())) ; 
918   AliInfo(Form("Option for Saving histos = %s",optionSave )) ; 
919   AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ; 
920
921   Float_t p[MAXCHENTRIESSMALL];
922   Int_t nentries;
923   //fTree->SetBranchAddress("nentries",&nentries);
924   //fTree->SetBranchAddress("TOFentries",p);
925   fChain->SetBranchAddress("nentries",&nentries);
926   fChain->SetBranchAddress("TOFentries",p);
927
928   Float_t ntracksTotal =0;
929   for (Int_t irun=0;irun<fNruns;irun++){
930     Int_t ientry = -1;
931     ientry = i+irun*fNChannels;
932     //fTree->GetEntry(ientry);
933     fChain->GetEntry(ientry);
934     ntracksTotal+=nentries/3;    
935   }
936   
937   if (ntracksTotal < MEANENTRIES) {  
938     AliInfo(Form(" Too small mean number of entires per channel (mean number = %f) not calibrating and exiting.....",ntracksTotal));
939     return 2;
940   }
941
942   //filling ToT and Time arrays
943
944   Int_t nbinToT = 100;  // ToT bin width in Profile = 48.8 ps 
945   Float_t minToT = 0;   // ns
946   Float_t maxToT = 4.88;  // ns
947
948   TH1F *hToT = new TH1F("htot","htot",nbinToT, minToT, maxToT);
949   TH1F *hdeltaTime = new TH1F("hdeltaTime","hdeltaTime",200,2,4);
950   Int_t ntracksRun = 0;
951   Double_t binsProfile[101]; // sized larger than necessary, the correct 
952                              // dim being set in the booking of the profile
953   Int_t nusefulbins=0;
954   Float_t meantime=0;
955   for (Int_t irun=0;irun<fNruns;irun++){
956     Int_t ientry = -1;
957     ientry = i+irun*fNChannels;
958     //fTree->GetEntry(ientry);
959     fChain->GetEntry(ientry);
960     ntracksRun=nentries/3;
961     AliDebug(2,Form("run %i, channel %i, nentries = %i, ntracksRun = %i",irun, i ,nentries, ntracksRun));
962     for (Int_t j=0;j<ntracksRun;j++){
963       Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
964       Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
965       Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
966       Float_t tot = p[idxexToT];
967       meantime+=p[idxexTime]-p[idxexExTime];
968       hdeltaTime->Fill(p[idxexTime]-p[idxexExTime]);
969       hToT->Fill(tot);
970     }
971   }
972
973   nusefulbins = FindBins(hToT,&binsProfile[0]);
974   meantime/=ntracksTotal;
975   AliDebug(2,Form("meantime = %f",meantime));
976   
977   for (Int_t j=1;j<=nusefulbins;j++) {
978     AliDebug(2,Form(" channel %i, nusefulbins = %i, bin %i = %f",i,nusefulbins,j,binsProfile[j])); 
979   }
980
981   TProfile* hSlewingProf = new TProfile("hSlewingProf", "hSlewingProf",nusefulbins, binsProfile, "G");  // CHECK THE BUILD OPTION, PLEASE!!!!!!
982   TH2F * htimetot = new TH2F("htimetot","htimetot",nbinToT, minToT, maxToT,600,-5,10);
983   for (Int_t irun=0;irun<fNruns;irun++){
984     Int_t ientry = -1;
985     ientry = i+irun*fNChannels;
986     //fTree->GetEntry(ientry);
987     fChain->GetEntry(ientry);
988     ntracksRun=nentries/3;
989     for (Int_t j=0;j<ntracksRun;j++){
990       Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
991       Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
992       Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
993       Float_t tot = p[idxexToT];
994       Float_t time = p[idxexTime]-p[idxexExTime];
995       AliDebug (2,Form("track = %i, time = %f, tot = %f, time-meantime = %f",j,time, tot, time-meantime));
996       hSlewingProf->Fill(tot,time);  // if meantime is not used, the fill may be moved in the loop above
997       htimetot->Fill(tot,time-meantime);  // if meantime is not used, the fill may be moved in the loop above
998     }
999   }
1000
1001   hSlewingProf->Fit("pol5",optionFit,"",0,4);
1002   TF1 * calibfunc = (TF1*)hSlewingProf->GetFunction("pol5");
1003   Float_t par[6];    
1004   for(Int_t kk=0;kk<6;kk++){
1005     par[kk]=calibfunc->GetParameter(kk);
1006     AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
1007   }
1008
1009
1010   if(strstr(optionSave,"save")){
1011     TFile * fileProf = new TFile("TOFCalibSave.root","recreate");
1012     fileProf->cd();   
1013     TString profName=Form("Profile%06i",i);
1014     TString timeTotName=Form("TimeTot%06i",i);
1015     TString totName=Form("Tot%06i",i);
1016     TString deltaName=Form("Delta%06i",i);
1017     hSlewingProf->Write(profName);
1018     htimetot->Write(timeTotName);
1019     hToT->Write(totName);
1020     hdeltaTime->Write(deltaName);
1021     fileProf->Close();
1022     delete fileProf;
1023     fileProf=0x0;
1024   }
1025
1026   delete hToT;
1027   hToT=0x0; 
1028   delete hSlewingProf;
1029   hSlewingProf=0x0;
1030   delete htimetot;
1031   htimetot=0x0;
1032   delete hdeltaTime;
1033   hdeltaTime=0x0;
1034
1035   AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(i);
1036   calChannel->SetSlewPar(par);
1037   WriteParOfflineOnCDB("TOF/Calib","valid");
1038   return 0;
1039 }
1040 //----------------------------------------------------------------------------
1041 Int_t AliTOFcalib::Calibrate(Int_t nch, Int_t *ch, Option_t *optionSave, Option_t *optionFit){
1042
1043   // calibrating an array of channels
1044   // computing calibration parameters
1045   // Returning codes:
1046   // 0 -> everything was ok
1047   // 1 -> no tree for calibration found
1048   // 2 -> not enough statistics to perform calibration
1049   // 3 -> problems with arrays
1050   
1051   TH1::AddDirectory(0);
1052
1053   AliInfo(Form("*** Calibrating Histograms %s, number of channels = %i", GetName(),nch)) ; 
1054   AliInfo(Form("Option for Saving histos = %s",optionSave )) ; 
1055   AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ; 
1056   for (Int_t ich=0; ich<nch; ich++){
1057     Int_t i = ch[ich];
1058     AliInfo(Form("Calibrating channel = %i",i )) ; 
1059   }
1060   Float_t p[MAXCHENTRIESSMALL];
1061   Int_t nentries;
1062   //fTree->SetBranchAddress("nentries",&nentries);
1063   //fTree->SetBranchAddress("TOFentries",p);
1064   fChain->SetBranchAddress("nentries",&nentries);
1065   fChain->SetBranchAddress("TOFentries",p);
1066
1067   Float_t ntracksTotalmean =0;
1068   for (Int_t ich=0; ich<nch; ich++){
1069     Int_t ientry = -1;
1070       Int_t i = ch[ich];
1071       for (Int_t irun=0;irun<fNruns;irun++){
1072       ientry = i+irun*fNChannels;
1073       //fTree->GetEntry(ientry);
1074       fChain->GetEntry(ientry);
1075       ntracksTotalmean+=nentries/3;
1076     }
1077   }
1078
1079   ntracksTotalmean/=nch;
1080   if (ntracksTotalmean < MEANENTRIES) { 
1081     AliInfo(Form(" Too small mean number of entires per channel (mean number = %f) not calibrating and exiting.....",ntracksTotalmean));
1082     return 2;
1083   }
1084
1085   //filling ToT and Time arrays
1086
1087   Int_t nbinToT = 100;  // ToT bin width in Profile = 48.8 ps 
1088   Float_t minToT = 0;   // ns
1089   Float_t maxToT = 4.88;  // ns
1090   TFile * fileProf=0x0;
1091   if(strstr(optionSave,"save")){
1092     fileProf = new TFile("TOFCalibSave.root","recreate");
1093   }
1094   for (Int_t ich=0; ich<nch; ich++) {
1095     TH1F *hToT = new TH1F("htot","htot",nbinToT, minToT, maxToT);
1096     TH1F *hdeltaTime = new TH1F("hdeltaTime","hdeltaTime",200,2,4);
1097     Double_t binsProfile[101]; // sized larger than necessary, the correct 
1098     // dim being set in the booking of the profile
1099     TH2F * htimetot = new TH2F("htimetot","htimetot",nbinToT, minToT, maxToT,600,-5,10);
1100     Int_t ntracksTotal = 0;
1101     Int_t ntracksRun = 0;
1102     Int_t nusefulbins=0;
1103     Float_t meantime=0;
1104     Int_t i=-1;
1105     for (Int_t irun=0;irun<fNruns;irun++){
1106       i = ch[ich]+irun*fNChannels;
1107       AliDebug(2,Form("Calibrating channel %i",i));
1108       //fTree->GetEntry(i);
1109       fChain->GetEntry(i);
1110       ntracksTotal+=nentries/3;
1111     }
1112     if (ntracksTotal < MEANENTRIES) {
1113       AliInfo(Form(" Too small mean number of entires in channel %i (number of tracks = %f), not calibrating channel and continuing.....",i,ntracksTotal));
1114       continue;
1115     }
1116   
1117     for (Int_t irun=0;irun<fNruns;irun++){
1118       Int_t i = ch[ich]+irun*fNChannels;
1119       //fTree->GetEntry(i);
1120       fChain->GetEntry(i);
1121       ntracksRun=nentries/3;
1122       for (Int_t j=0;j<ntracksRun;j++){
1123         Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1124         Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1125         Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1126         Float_t tot = p[idxexToT];
1127         hdeltaTime->Fill(p[idxexTime]-p[idxexExTime]);
1128         meantime+=p[idxexTime]-p[idxexExTime];
1129         hToT->Fill(tot);
1130       }
1131     }
1132
1133     nusefulbins = FindBins(hToT,&binsProfile[0]);
1134     meantime/=ntracksTotal;
1135     for (Int_t j=1;j<=nusefulbins;j++) {
1136       AliDebug(2,Form(" channel %i, nusefulbins = %i, bin %i = %f",i,nusefulbins,j,binsProfile[j])); 
1137     }
1138
1139     TProfile* hSlewingProf = new TProfile("hSlewingProf", "hSlewingProf",nusefulbins, binsProfile, "G");  // CHECK THE BUILD OPTION, PLEASE!!!!!!
1140     for (Int_t irun=0;irun<fNruns;irun++){
1141       Int_t i = ch[ich]+irun*fNChannels;
1142       //fTree->GetEntry(i);
1143       fChain->GetEntry(i);
1144       ntracksRun=nentries/3;
1145       for (Int_t j=0;j<ntracksRun;j++){
1146         Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1147         Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1148         Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1149         Float_t tot = p[idxexToT];
1150         Float_t time = p[idxexTime]-p[idxexExTime];
1151         AliDebug(2,Form("track = %i, time = %f, tot = %f, time-meantime = %f",j,time, tot, time-meantime));
1152         hSlewingProf->Fill(tot,time);  // if meantime is not used, the fill may be moved in the loop above
1153         htimetot->Fill(tot,time-meantime);  // if meantime is not used, the fill may be moved in the loop above
1154       }
1155     }
1156     
1157     hSlewingProf->Fit("pol5",optionFit,"",1,4);
1158     TF1 * calibfunc = (TF1*)hSlewingProf->GetFunction("pol5");
1159     Float_t par[6];    
1160     for(Int_t kk=0;kk<6;kk++){
1161       par[kk]=calibfunc->GetParameter(kk);
1162       AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
1163     }
1164     
1165     if(strstr(optionSave,"save") && fileProf){
1166       TString profName=Form("Profile%06i",i);
1167       TString timeTotName=Form("TimeTot%06i",i);
1168       TString totName=Form("Tot%06i",i);
1169       TString deltaName=Form("Delta%06i",i);
1170       fileProf->cd();
1171       hSlewingProf->Write(profName);
1172       htimetot->Write(timeTotName);
1173       hToT->Write(totName);
1174       hdeltaTime->Write(deltaName);
1175     }
1176
1177     AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(i);
1178     calChannel->SetSlewPar(par);
1179     delete hToT;
1180     hToT=0x0;
1181     delete hSlewingProf;
1182     hSlewingProf=0x0;
1183     delete htimetot;
1184     htimetot=0x0;
1185     delete hdeltaTime;
1186     hdeltaTime=0x0;
1187   }
1188
1189   if(strstr(optionSave,"save") && fileProf){
1190     fileProf->Close();
1191     delete fileProf;
1192     fileProf=0x0;
1193   }
1194   WriteParOfflineOnCDB("TOF/Calib","valid");
1195
1196   return 0;
1197 }
1198 //----------------------------------------------------------------------------
1199 Int_t AliTOFcalib::CalibrateFromProfile(Int_t ich, Option_t *optionSave, Option_t *optionFit){
1200
1201   // computing calibration parameters using the old profiling algo
1202   // Returning codes:
1203   // 0 -> everything was ok
1204   // 1 -> no tree for calibration found
1205   // 2 -> not enough statistics to perform calibration
1206   // 3 -> problems with arrays
1207
1208   TH1::AddDirectory(0);
1209
1210   AliInfo(Form("*** Calibrating Histograms From Profile %s", GetName())) ; 
1211   AliInfo(Form("Option for Saving histos = %s",optionSave )) ; 
1212   AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ; 
1213   Float_t p[MAXCHENTRIESSMALL];
1214   Int_t nentries;
1215   Int_t ntracksTotal=0;
1216   //fTree->SetBranchAddress("nentries",&nentries);
1217   //fTree->SetBranchAddress("TOFentries",p);
1218   fChain->SetBranchAddress("nentries",&nentries);
1219   fChain->SetBranchAddress("TOFentries",p);
1220
1221   for (Int_t irun=0;irun<fNruns;irun++){
1222     Int_t i = ich+irun*fNChannels;
1223     //fTree->GetEntry(i);
1224     fChain->GetEntry(i);
1225     ntracksTotal+=nentries/3;
1226   }
1227
1228   if (ntracksTotal < MEANENTRIES) {  
1229     AliInfo(Form(" Too small mean number of entires per channel (mean number = %f) not calibrating and exiting.....",ntracksTotal));
1230     return 2;
1231   }
1232
1233   TH1F * hProf = new TH1F();
1234   hProf = Profile(ich);
1235   hProf->Fit("pol5",optionFit,"",0,4);
1236   TF1 * calibfunc = (TF1*)hProf->GetFunction("pol5");
1237   Float_t par[6];    
1238   for(Int_t kk=0;kk<6;kk++){
1239     par[kk]=calibfunc->GetParameter(kk);
1240     AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
1241   }
1242
1243   if(strstr(optionSave,"save")){
1244     TFile * fileProf = new TFile("TOFCalibSave.root","recreate");
1245     fileProf->cd(); 
1246     TString profName=Form("Profile%06i",ich);
1247     hProf->Write(profName);
1248     fileProf->Close();
1249     delete fileProf;
1250     fileProf=0x0;
1251   }
1252
1253   delete hProf;
1254   hProf=0x0;
1255   AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(ich);
1256   calChannel->SetSlewPar(par);
1257   WriteParOfflineOnCDB("TOF/Calib","valid");
1258   return 0;
1259 }
1260 //----------------------------------------------------------------------------
1261 Int_t AliTOFcalib::Calibrate(Option_t *optionSave, Option_t *optionFit){
1262
1263   // calibrating the whole TOF
1264   // computing calibration parameters
1265   // Returning codes:
1266   // 0 -> everything was ok
1267   // 1 -> no tree for calibration found
1268   // 2 -> not enough statistics to perform calibration
1269   // 3 -> problems with arrays
1270
1271   TH1::AddDirectory(0);
1272
1273   AliInfo(Form("*** Calibrating Histograms %s, all channels", GetName())) ; 
1274   AliInfo(Form("Option for Saving histos = %s",optionSave )) ; 
1275   AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ; 
1276
1277   TFile * fileProf=0x0;
1278   if(strstr(optionSave,"save")){
1279     fileProf = new TFile("TOFCalibSave.root","recreate");
1280   }
1281
1282   Float_t p[MAXCHENTRIESSMALL];
1283   Int_t nentries;
1284   //fTree->SetBranchAddress("nentries",&nentries);
1285   //fTree->SetBranchAddress("TOFentries",p);
1286   fChain->SetBranchAddress("nentries",&nentries);
1287   fChain->SetBranchAddress("TOFentries",p);
1288
1289   Float_t ntracksTotalmean =0;
1290   for (Int_t ii=0; ii<fNChannels; ii++){
1291     for (Int_t irun=0;irun<fNruns;irun++){
1292       Int_t i = ii+irun*fNChannels;
1293       //fTree->GetEntry(i);
1294       fChain->GetEntry(i);
1295       ntracksTotalmean+=nentries/3;
1296     }
1297   }
1298
1299   ntracksTotalmean/=fNChannels;
1300   if (ntracksTotalmean < MEANENTRIES) {
1301     AliInfo(Form(" Too small mean number of entires per channel (mean number = %f) not calibrating and exiting.....",ntracksTotalmean));
1302     return 2;
1303   }
1304
1305   //filling ToT and Time arrays
1306
1307   Int_t nbinToT = 100;  // ToT bin width in Profile = 50.0 ps 
1308   Float_t minToT = 0;   // ns
1309   Float_t maxToT = 4.88;// ns
1310   for (Int_t ii=0; ii<fNChannels; ii++) {
1311     TH1F *hToT = new TH1F("htot","htot",nbinToT, minToT, maxToT);
1312     TH1F *hdeltaTime = new TH1F("hdeltaTime","hdeltaTime",200,2,4);
1313     TH2F * htimetot = new TH2F("htimetot","htimetot",nbinToT, minToT, maxToT,600,-5,10);
1314     if (ii%1000 == 0) AliDebug(1,Form("Calibrating channel %i ",ii));
1315     //Int_t i = 3;
1316     Int_t nusefulbins=0;
1317     Double_t binsProfile[101]; // sized larger than necessary, the correct 
1318                               // dim being set in the booking of the profile
1319     Int_t ntracksRun = 0;
1320     Int_t ntracksTotal = 0;
1321     for (Int_t irun=0;irun<fNruns;irun++){
1322       Int_t i = ii+irun*fNChannels;
1323       //fTree->GetEntry(i);
1324       fChain->GetEntry(i);
1325       ntracksTotal+=nentries/3;
1326     }
1327     if (ntracksTotal < MEANENTRIES) {
1328       AliInfo(Form(" Too small mean number of entires in channel %i (number of tracks = %f), not calibrating channel and continuing.....",ii,ntracksTotal));
1329       continue;
1330     }
1331     Float_t meantime=0;
1332     for (Int_t irun=0;irun<fNruns;irun++){
1333       Int_t i = ii+irun*fNChannels;
1334       //fTree->GetEntry(i);
1335       fChain->GetEntry(i);
1336       ntracksRun=nentries/3;
1337       for (Int_t j=0;j<ntracksRun;j++){
1338         Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1339         Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1340         Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1341         Float_t tot = p[idxexToT];
1342         hdeltaTime->Fill(p[idxexTime]-p[idxexExTime]);
1343         meantime+=p[idxexTime]-p[idxexExTime];
1344         hToT->Fill(tot);
1345       }
1346     }
1347     nusefulbins = FindBins(hToT,&binsProfile[0]);
1348     meantime/=ntracksTotal;
1349     for (Int_t j=0;j<nusefulbins;j++) {
1350       AliDebug(2,Form(" channel %i, usefulbin = %i, low edge = %f",ii,j,binsProfile[j])); 
1351     }
1352     TProfile* hSlewingProf = new TProfile("hSlewingProf", "hSlewingProf",nusefulbins, binsProfile, "G");  // CHECK THE BUILD OPTION, PLEASE!!!!!!
1353     for (Int_t irun=0;irun<fNruns;irun++){
1354       Int_t i = ii+irun*fNChannels;
1355       //fTree->GetEntry(i);
1356       fChain->GetEntry(i);
1357       ntracksRun=nentries/3;
1358       for (Int_t j=0;j<ntracksRun;j++){
1359         Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1360         Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1361         Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1362         Float_t tot = p[idxexToT];
1363         Float_t time = p[idxexTime]-p[idxexExTime];
1364         AliDebug (2,Form("track = %i, time = %f, tot = %f, time-meantime = %f",j,time, tot, time-meantime));
1365         hSlewingProf->Fill(tot,time);  // if meantime is not used, the fill may be moved in the loop above
1366         htimetot->Fill(tot,time-meantime);  // if meantime is not used, the fill may be moved in the loop above
1367       }
1368     }
1369     hSlewingProf->Fit("pol5",optionFit,"",1,4);
1370     TF1 * calibfunc = (TF1*)hSlewingProf->GetFunction("pol5");
1371     Float_t par[6];    
1372     for(Int_t kk=0;kk<6;kk++){
1373       par[kk]=calibfunc->GetParameter(kk);
1374       AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
1375     }
1376
1377     if(strstr(optionSave,"save") && fileProf){
1378       TString profName=Form("Profile%06i",ii);
1379       TString timeTotName=Form("TimeTot%06i",ii);
1380       TString totName=Form("Tot%06i",ii);
1381       TString deltaName=Form("Delta%06i",ii);
1382       fileProf->cd();
1383       hSlewingProf->Write(profName);
1384       htimetot->Write(timeTotName);
1385       hToT->Write(totName);
1386       hdeltaTime->Write(deltaName);
1387     }
1388     AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(ii);
1389     calChannel->SetSlewPar(par);
1390
1391     delete hToT;
1392     hToT=0x0;
1393     delete hSlewingProf;
1394     hSlewingProf=0x0;
1395     delete htimetot;
1396     htimetot=0x0;
1397     delete hdeltaTime;
1398     hdeltaTime=0x0;
1399   }
1400
1401   if(strstr(optionSave,"save")){
1402     fileProf->Close();
1403     delete fileProf;
1404     fileProf=0x0;
1405   }
1406   WriteParOfflineOnCDB("TOF/Calib","valid");
1407   return 0;
1408 }
1409
1410 //-----------------------------------------------------------------------
1411 TH1F* AliTOFcalib::Profile(Int_t ich)
1412 {
1413   // profiling algo
1414
1415   Float_t p[MAXCHENTRIESSMALL];
1416   Int_t nentries;
1417   //fTree->SetBranchAddress("nentries",&nentries);
1418   //fTree->SetBranchAddress("TOFentries",p);
1419   fChain->SetBranchAddress("nentries",&nentries);
1420   fChain->SetBranchAddress("TOFentries",p);
1421
1422   //Prepare histograms for Slewing Correction
1423   const Int_t knbinToT = 100;
1424   Int_t nbinTime = 200;
1425   Float_t minTime = -5.5; //ns
1426   Float_t maxTime = 5.5; //ns
1427   Float_t minToT = 0; //ns
1428   Float_t maxToT = 5.; //ns
1429   Float_t deltaToT = (maxToT-minToT)/knbinToT;
1430   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];
1431   Int_t n[knbinToT+1], nentrx[knbinToT+1];
1432   Double_t sigmaToT[knbinToT+1];
1433   for (Int_t i = 0; i < knbinToT+1 ; i++){
1434     mTime[i]=0;
1435     mToT[i]=0;
1436     n[i]=0;
1437     meanTime[i]=0;
1438     meanTime2[i]=0;
1439     vToT[i]=0;
1440     vToT2[i]=0;
1441     meanToT[i]=0;
1442     meanToT2[i]=0;
1443     vTime[i]=0;
1444     vTime2[i]=0;
1445     xlow[i]=0;
1446     sigmaTime[i]=0;
1447     sigmaToT[i]=0;
1448     n[i]=0;
1449     nentrx[i]=0;
1450   }
1451   TH2F* hSlewing = new TH2F("hSlewing", "hSlewing", knbinToT, minToT, maxToT, nbinTime, minTime, maxTime);
1452   Int_t ntracksRun = 0;
1453   TH1F *histo = new TH1F("histo", "1D Time vs ToT", knbinToT, minToT, maxToT);
1454   for (Int_t irun=0;irun<fNruns;irun++){
1455     Int_t i = ich+irun*fNChannels;
1456     //fTree->GetEntry(i);
1457     fChain->GetEntry(i);
1458     ntracksRun=nentries/3;
1459     for (Int_t j=0;j<ntracksRun;j++){
1460       Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT; 
1461       Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME; 
1462       Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID; 
1463       Float_t tot = p[idxexToT];
1464       Float_t time = p[idxexTime]-p[idxexExTime];
1465       Int_t nx = (Int_t)((tot-minToT)/deltaToT)+1;
1466       if ((tot != 0) && ( time!= 0)){
1467         vTime[nx]+=time;
1468         vTime2[nx]+=time*time;
1469         vToT[nx]+=tot;
1470         vToT2[nx]+=tot*tot;
1471         nentrx[nx]++;
1472         hSlewing->Fill(tot,time);
1473       }
1474     }
1475   }
1476   Int_t nbinsToT=hSlewing->GetNbinsX();
1477   if (nbinsToT != knbinToT) {
1478     AliError("Profile :: incompatible numbers of bins");
1479     return 0x0;
1480   }
1481   
1482   Int_t usefulBins=0;
1483   for (Int_t i=1;i<=nbinsToT;i++){
1484     if (nentrx[i]!=0){
1485       n[usefulBins]+=nentrx[i];
1486       if (n[usefulBins]==0 && i == nbinsToT) {
1487         break;
1488       }
1489       meanTime[usefulBins]+=vTime[i];
1490       meanTime2[usefulBins]+=vTime2[i];
1491       meanToT[usefulBins]+=vToT[i];
1492       meanToT2[usefulBins]+=vToT2[i];
1493       if (n[usefulBins]<10 && i!=nbinsToT) continue; 
1494       mTime[usefulBins]=meanTime[usefulBins]/n[usefulBins];
1495       mToT[usefulBins]=meanToT[usefulBins]/n[usefulBins];
1496       sigmaTime[usefulBins]=TMath::Sqrt(1./n[usefulBins]/n[usefulBins]
1497                             *(meanTime2[usefulBins]-meanTime[usefulBins]
1498                             *meanTime[usefulBins]/n[usefulBins]));
1499       if ((1./n[usefulBins]/n[usefulBins]
1500            *(meanToT2[usefulBins]-meanToT[usefulBins]
1501              *meanToT[usefulBins]/n[usefulBins]))< 0) {
1502         AliError(" too small radical" );
1503         sigmaToT[usefulBins]=0;
1504       }
1505       else{       
1506         sigmaToT[usefulBins]=TMath::Sqrt(1./n[usefulBins]/n[usefulBins]
1507                              *(meanToT2[usefulBins]-meanToT[usefulBins]
1508                              *meanToT[usefulBins]/n[usefulBins]));
1509       }
1510       usefulBins++;
1511     }
1512   }
1513   for (Int_t i=0;i<usefulBins;i++){
1514     Int_t binN = (Int_t)((mToT[i]-minToT)/deltaToT)+1;
1515     histo->Fill(mToT[i],mTime[i]);
1516     histo->SetBinError(binN,sigmaTime[i]);
1517   } 
1518   delete hSlewing;
1519   hSlewing=0x0;
1520
1521   return histo;
1522 }
1523 //----------------------------------------------------------------------------
1524 Int_t AliTOFcalib::FindBins(TH1F* h, Double_t *binsProfile) const{
1525
1526   // to determine the bins for ToT histo
1527
1528   Int_t cont = 0;
1529   Int_t startBin = 1;
1530   Int_t nbin = h->GetNbinsX();
1531   Int_t nentries = (Int_t)h->GetEntries();
1532   Float_t max = h->GetBinLowEdge(nbin);
1533   Int_t nusefulbins=0;
1534   Int_t maxcont=0;
1535   // setting maxvalue of entries per bin
1536   if (nentries <= 60) maxcont = 2;
1537   else  if (nentries <= 100) maxcont = 5;
1538   else  if (nentries <= 500) maxcont = 10;
1539   else  maxcont = 20;
1540   for (Int_t j=1;j<=nbin;j++) {
1541     cont += (Int_t)h->GetBinContent(j);
1542     if (j<nbin){
1543       if (cont>=maxcont){
1544         nusefulbins++;
1545         binsProfile[nusefulbins-1]=h->GetBinLowEdge(startBin);
1546         cont=0;
1547         startBin=j+1;
1548         continue;
1549       }
1550     }
1551     else{
1552       if (cont>=maxcont){
1553         nusefulbins++;
1554         binsProfile[nusefulbins-1]=h->GetBinLowEdge(startBin);
1555         binsProfile[nusefulbins]=max;
1556       }
1557       else {
1558         binsProfile[nusefulbins]=h->GetBinLowEdge(startBin);
1559       }
1560     }
1561   }
1562   return nusefulbins;
1563 }