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