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