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