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