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