+Bool_t AliTOFcalib::ReadParOnlineStatusFromCDB(const Char_t *sel, Int_t nrun)
+{
+ //Read calibration parameters from the CDB -------> new calib objs!!!!!
+ AliCDBManager *man = AliCDBManager::Instance();
+ const Char_t *sel1 = "Status" ;
+ Char_t out[100];
+ sprintf(out,"%s/%s",sel,sel1);
+ AliCDBEntry *entry = man->Get(out,nrun);
+ if (!entry) {
+ AliFatal("Exiting, no CDB object (Status) found!!!");
+ exit(0);
+ }
+ if(!entry->GetObject()){
+ AliFatal("Exiting, no CDB object (Status) found!!!");
+ exit(0);
+ }
+
+ fStatus =(AliTOFChannelOnlineStatusArray*)entry->GetObject();
+
+ return kTRUE;
+
+}
+//_____________________________________________________________________________
+
+Bool_t AliTOFcalib::ReadParOnlineFromCDB(const Char_t *sel, Int_t nrun)
+{
+ //Read calibration parameters from the CDB
+ AliCDBManager *man = AliCDBManager::Instance();
+ const Char_t *sel1 = "ParOnline" ;
+ Char_t out[100];
+ sprintf(out,"%s/%s",sel,sel1);
+ AliCDBEntry *entry = man->Get(out,nrun);
+ if (!entry) {
+ AliFatal("Exiting, no CDB object (ParOnline) found!!!");
+ exit(0);
+ }
+ if(!entry->GetObject()){
+ AliFatal("Exiting, no CDB object (ParOnline) found!!!");
+ exit(0);
+ }
+
+ fTOFCalOnline =(TObjArray*)entry->GetObject();
+
+ return kTRUE;
+
+}
+//_____________________________________________________________________________
+
+Bool_t AliTOFcalib::ReadParOnlinePulserFromCDB(const Char_t *sel, Int_t nrun)
+{
+ //Read calibration parameters from pulser from the CDB
+ AliCDBManager *man = AliCDBManager::Instance();
+ const Char_t *sel1 = "Pulser" ;
+ Char_t out[100];
+ sprintf(out,"%s/%s",sel,sel1);
+ AliCDBEntry *entry = man->Get(out,nrun);
+ if (!entry) {
+ AliFatal("Exiting, no CDB object (Pulser) found!!!");
+ exit(0);
+ }
+ if(!entry->GetObject()){
+ AliFatal("Exiting, no CDB object (Pulser) found!!!");
+ exit(0);
+ }
+
+ fTOFCalOnlinePulser =(TObjArray*)entry->GetObject();
+
+ return kTRUE;
+
+}
+//_____________________________________________________________________________
+
+Bool_t AliTOFcalib::ReadParOnlineNoiseFromCDB(const Char_t *sel, Int_t nrun)
+{
+ //Read calibration parameters from noise from the CDB
+ AliCDBManager *man = AliCDBManager::Instance();
+ const Char_t *sel1 = "Noise" ;
+ Char_t out[100];
+ sprintf(out,"%s/%s",sel,sel1);
+ AliCDBEntry *entry = man->Get(out,nrun);
+ if (!entry) {
+ AliFatal("Exiting, no CDB object (Noise) found!!!");
+ exit(0);
+ }
+ if(!entry->GetObject()){
+ AliFatal("Exiting, no CDB object (Noise) found!!!");
+ exit(0);
+ }
+
+ fTOFCalOnlineNoise =(TObjArray*)entry->GetObject();
+
+ return kTRUE;
+
+}
+//_____________________________________________________________________________
+
+Bool_t AliTOFcalib::ReadParOnlineHWFromCDB(const Char_t *sel, Int_t nrun)
+{
+ //Read calibration parameters from hardware from the CDB
+ AliCDBManager *man = AliCDBManager::Instance();
+ const Char_t *sel1 = "HW" ;
+ Char_t out[100];
+ sprintf(out,"%s/%s",sel,sel1);
+ AliCDBEntry *entry = man->Get(out,nrun);
+ if (!entry) {
+ AliFatal("Exiting, no CDB object (HW map) found!!!");
+ exit(0);
+ }
+ if(!entry->GetObject()){
+ AliFatal("Exiting, no CDB object (HW map) found!!!");
+ exit(0);
+ }
+
+ fTOFCalOnlineHW =(TObjArray*)entry->GetObject();
+
+ return kTRUE;
+
+}
+//_____________________________________________________________________________
+
+Bool_t AliTOFcalib::ReadParOfflineFromCDB(const Char_t *sel, Int_t nrun)
+{
+ //Read calibration parameters from the CDB
+ AliCDBManager *man = AliCDBManager::Instance();
+ const Char_t *sel1 = "ParOffline" ;
+ Char_t out[100];
+ sprintf(out,"%s/%s",sel,sel1);
+ AliCDBEntry *entry = man->Get(out,nrun);
+ if (!entry) {
+ AliFatal("Exiting, no CDB object (ParOffline) found!!!");
+ exit(0);
+ }
+ if(!entry->GetObject()){
+ AliFatal("Exiting, no CDB object (ParOffline) found!!!");
+ exit(0);
+ }
+ AliCDBMetaData * md = entry->GetMetaData();
+ fkValidity = md->GetComment();
+ fTOFCalOffline =(TObjArray*)entry->GetObject();
+
+ return kTRUE;
+
+}
+//_____________________________________________________________________________
+void AliTOFcalib::WriteSimHistoOnCDB(const Char_t *sel, Int_t minrun, Int_t maxrun, TH1F *histo){
+ //Write Sim miscalibration parameters to the CDB
+
+ fTOFSimToT=histo;
+ AliCDBManager *man = AliCDBManager::Instance();
+ const Char_t *sel1 = "SimHisto" ;
+ Char_t out[100];
+ sprintf(out,"%s/%s",sel,sel1);
+ AliCDBMetaData *mdhisto = new AliCDBMetaData();
+ mdhisto->SetResponsible("Chiara Zampolli");
+ AliCDBId id(out,minrun,maxrun);
+ man->Put(fTOFSimToT,id,mdhisto);
+ delete mdhisto;
+}
+//_____________________________________________________________________________
+Bool_t AliTOFcalib::ReadSimHistoFromCDB(const Char_t *sel, Int_t nrun)
+{
+ //Read miscalibration parameters from the CDB
+ AliCDBManager *man = AliCDBManager::Instance();
+
+ // The Tot Histo
+
+ const Char_t *sel1 = "SimHisto" ;
+ Char_t out[100];
+ sprintf(out,"%s/%s",sel,sel1);
+ AliCDBEntry *entry = man->Get(out,nrun);
+ if (!entry) {
+ AliFatal("Exiting, no CDB object (SimHisto) found!!!");
+ exit(0);
+ }
+ if(!entry->GetObject()){
+ AliFatal("Exiting, no CDB object (SimHisto) found!!!");
+ exit(0);
+ }
+ TH1F *histo =(TH1F*)entry->GetObject();
+ fTOFSimToT=histo;
+ return kTRUE;
+}
+//_____________________________________________________________________________
+void AliTOFcalib::WriteRecParOnCDB(const Char_t *sel, Int_t minrun, Int_t maxrun, AliTOFRecoParam *param){
+ //Write reconstruction parameters to the CDB
+
+ AliCDBManager *man = AliCDBManager::Instance();
+ AliCDBMetaData *md = new AliCDBMetaData();
+ md->SetResponsible("Silvia Arcelli");
+ const Char_t *sel1 = "RecoParam" ;
+ Char_t out[100];
+ sprintf(out,"%s/%s",sel,sel1);
+ AliCDBId id(out,minrun,maxrun);
+ man->Put(param,id,md);
+ delete md;
+}
+//_____________________________________________________________________________
+AliTOFRecoParam * AliTOFcalib::ReadRecParFromCDB(const Char_t *sel, Int_t nrun)
+{
+ //Read reconstruction parameters from the CDB
+ AliCDBManager *man = AliCDBManager::Instance();
+ const Char_t *sel1 = "RecoParam" ;
+ Char_t out[100];
+ sprintf(out,"%s/%s",sel,sel1);
+ AliCDBEntry *entry = man->Get(out,nrun);
+ if (!entry) {
+ AliFatal("Exiting, no CDB object (RecoParam) found!!!");
+ exit(0);
+ }
+ if(!entry->GetObject()){
+ AliFatal("Exiting, no CDB object (RecoParam) found!!!");
+ exit(0);
+ }
+
+ AliTOFRecoParam *param=(AliTOFRecoParam*)entry->GetObject();
+ return param;
+}
+//-----------------------------------------------------------------------------
+// Calibration methods
+//-----------------------------------------------------------------------------
+void AliTOFcalib::CreateTreeFromCDB(Int_t minrun, Int_t maxrun){
+
+ // creating the chain with the trees for calibration
+ // collecting them from reference data
+ // from minrun to maxrun
+
+ Float_t p[CHENTRIESSMALL];
+ Int_t nentries;
+ fTree = new TTree("TOFCalib","Tree for TOF Calibration");
+ fTree->Branch("nentries",&nentries,"nentries/I");
+ fTree->Branch("TOFentries",p,"TOFentries[nentries]/F");
+ AliCDBManager *man = AliCDBManager::Instance();
+ AliCDBStorage *aStorage = man->GetStorage("local://$ALICE_ROOT/OCDB");
+ for (Int_t irun = minrun;irun<=maxrun;irun++){
+ AliCDBEntry *entry = aStorage->Get("TOF/RefData/TreeForCalib",irun);
+ if (!entry){
+ AliInfo(Form("No entry found for run %i",irun));
+ }
+ else{
+ TTree *tree = new TTree();
+ tree = (TTree*)entry->GetObject();
+ tree->SetBranchAddress("nentries",&nentries);
+ tree->SetBranchAddress("TOFentries",p);
+ fTree->CopyEntries(tree);
+ delete tree;
+ fNruns++;
+ }
+ }
+ AliInfo(Form("Number of runs being analyzed %i",fNruns));
+}
+//-----------------------------------------------------------------------------
+void AliTOFcalib::CreateTreeFromGrid(Int_t minrun, Int_t maxrun){
+
+ // creating the chain with the trees for calibration
+ // collecting them from the Grid
+ // from minrun to maxrun
+
+ Float_t p[CHENTRIESSMALL];
+ Int_t nentries;
+ fTree = new TTree("TOFCalib","Tree for TOF Calibration");
+ fTree->SetDirectory(0);
+ fTree->Branch("nentries",&nentries,"nentries/I");
+ fTree->Branch("TOFentries",p,"TOFentries[nentries]/F");
+ AliInfo("connected to alien");
+ TGrid::Connect("alien://");
+
+ Char_t filename[100];
+ for (Int_t irun = minrun;irun<=maxrun;irun++){
+ sprintf(filename,"alien:///alice/cern.ch/user/c/czampolli/TOFCalibReference_%i.root",irun);
+ TFile *filegrid = TFile::Open(filename,"READ");
+ TTree *tree = (TTree*)filegrid->Get("T");
+ tree->SetBranchAddress("nentries",&nentries);
+ tree->SetBranchAddress("TOFentries",p);
+ fTree->CopyEntries(tree);
+ delete tree;
+ fNruns++;
+ }
+
+ AliInfo(Form("Number of runs being analyzed %i",fNruns));
+}
+//-----------------------------------------------------------------------------
+void AliTOFcalib::CreateTreeFromFile(Int_t minrun, Int_t maxrun){
+
+ // creating the tree with the trees for calibration
+ // collecting them from reference data (from file)
+ // from minrun to maxrun
+
+ Float_t p[CHENTRIESSMALL];
+ Int_t nentries;
+ fTree = new TTree("TOFCalib","Tree for TOF Calibration");
+ fTree->SetDirectory(0);
+ fTree->Branch("nentries",&nentries,"nentries/I");
+ fTree->Branch("TOFentries",p,"TOFentries[nentries]/F");
+ Char_t filename[100];
+ for (Int_t irun = minrun;irun<=maxrun;irun++){
+ sprintf(filename,"$ALICE_ROOT/TOF/RefData/TreeForCalib/fileout_%i.root",irun);
+ TFile *file = new TFile(filename,"READ");
+ TTree *tree = (TTree*)file->Get("T");
+ tree->SetBranchAddress("nentries",&nentries);
+ tree->SetBranchAddress("TOFentries",p);
+ fTree->CopyEntries(tree);
+ delete tree;
+ delete file;
+ file = 0x0;
+ fNruns++;
+ }
+
+ AliInfo(Form("Number of runs being analyzed %i",fNruns));
+}
+//-----------------------------------------------------------------------------
+void AliTOFcalib::CreateChainFromGrid(Int_t minrun, Int_t maxrun){
+
+ // creating the chain with the trees for calibration
+ // collecting them from the Grid
+ // from minrun to maxrun
+
+ fChain = new TChain("T");
+ AliInfo("connected to alien");
+ TGrid::Connect("alien://");
+
+ Char_t filename[100];
+ for (Int_t irun = minrun;irun<=maxrun;irun++){
+ sprintf(filename,"alien:///alice/cern.ch/user/c/czampolli/TOFCalibReference_%i.root",irun);
+ fChain->Add(filename);
+ fNruns++;
+ }
+
+ AliInfo(Form("Number of runs being analyzed %i",fNruns));
+}
+//-----------------------------------------------------------------------------
+Int_t AliTOFcalib::Calibrate(Int_t ichmin, Int_t ichmax, Option_t *optionSave, Option_t *optionFit){
+
+ // calibrating summing more than one channels
+ // computing calibration parameters
+ // Returning codes:
+ // 0 -> everything was ok
+ // 1 -> no tree for calibration found
+ // 2 -> not enough statistics to perform calibration
+ // 3 -> problems with arrays
+
+ TH1::AddDirectory(0);
+
+ AliInfo(Form("*** Calibrating Histograms %s, summing more channels, from channel %i, to channel %i, storing Calib Pars in channel %i", GetName(),ichmin,ichmax,ichmin)) ;
+ AliInfo(Form("Option for Saving histos = %s",optionSave )) ;
+ AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ;
+
+ Float_t p[CHENTRIESSMALL];
+ Int_t nentries;
+ //fTree->SetBranchAddress("nentries",&nentries);
+ //fTree->SetBranchAddress("TOFentries",p);
+ fChain->SetBranchAddress("nentries",&nentries);
+ fChain->SetBranchAddress("TOFentries",p);
+
+ Float_t ntracksTotalmean =0;
+ for (Int_t i=ichmin; i<ichmax; i++){
+ Int_t ientry = -1;
+ for (Int_t irun=0;irun<fNruns;irun++){
+ ientry = i+irun*fNChannels;
+ //fTree->GetEntry(ientry);
+ fChain->GetEntry(ientry);
+ Int_t ntracksRun=nentries/3;
+ ntracksTotalmean+=ntracksRun;
+ }
+ }
+
+ if (ntracksTotalmean < MEANENTRIES) {
+ AliInfo(Form(" Too small mean number of entires per channel (mean number = %f) not calibrating and exiting.....",ntracksTotalmean));
+ return 2;
+ }
+
+ //filling ToT and Time arrays
+
+ Int_t nbinToT = 100; // ToT bin width in Profile = 48.8 ps
+ Float_t minToT = 0; // ns
+ Float_t maxToT = 4.88; // ns
+
+ TH1F *hToT = new TH1F("htot","htot",nbinToT, minToT, maxToT);
+ TH1F *hdeltaTime = new TH1F("hdeltaTime","hdeltaTime",200,2,4);
+ Int_t ntracksTotal = 0;
+ Int_t ntracksRun = 0;
+ Double_t binsProfile[101]; // sized larger than necessary, the correct
+ // dim being set in the booking of the profile
+ Int_t nusefulbins=0;
+ Float_t meantime=0;
+ for (Int_t i = ichmin;i<ichmax;i++){
+ Int_t ientry = -1;
+ for (Int_t irun=0;irun<fNruns;irun++){
+ ientry = i+irun*fNChannels;
+ //fTree->GetEntry(ientry);
+ fChain->GetEntry(ientry);
+ ntracksTotal+=nentries/3;
+ ntracksRun=nentries/3;
+ AliDebug(2,Form("run %i, channel %i, nentries = %i, ntracks = %i",irun,i,nentries, ntracksRun));
+ for (Int_t j=0;j<ntracksRun;j++){
+ Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT;
+ Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME;
+ Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID;
+ Float_t tot = p[idxexToT];
+ hdeltaTime->Fill(p[idxexTime]-p[idxexExTime]);
+ meantime+=p[idxexTime]-p[idxexExTime];
+ hToT->Fill(tot);
+ }
+ }
+ }
+ nusefulbins = FindBins(hToT,&binsProfile[0]);
+ meantime/=ntracksTotal;
+ AliDebug(2, Form("meantime = %f",meantime));
+
+ for (Int_t j=1;j<=nusefulbins;j++) {
+ AliDebug(2,Form(" summing channels from %i to %i, nusefulbins = %i, bin %i = %f",ichmin,ichmax,nusefulbins,j,binsProfile[j]));
+ }
+
+ TProfile* hSlewingProf = new TProfile("hSlewingProf", "hSlewingProf",nusefulbins, binsProfile, "G"); // CHECK THE BUILD OPTION, PLEASE!!!!!!
+ TH2F * htimetot = new TH2F("htimetot","htimetot",nbinToT, minToT, maxToT,600,-5,10);
+
+ for (Int_t irun=0;irun<fNruns;irun++){
+ Int_t ientry = -1;
+ for (Int_t i=ichmin; i<ichmax; i++){
+ ientry = i+irun*fNChannels;
+ //fTree->GetEntry(ientry);
+ fChain->GetEntry(ientry);
+ ntracksRun=nentries/3;
+ for (Int_t j=0;j<ntracksRun;j++){
+ Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT;
+ Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME;
+ Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID;
+ Float_t tot = p[idxexToT];
+ Float_t time = p[idxexTime]-p[idxexExTime];
+ AliDebug (2, Form("track = %i, time = %f, tot = %f, time-meantime = %f",j,time, tot, time-meantime));
+ hSlewingProf->Fill(tot,time); // if meantime is not used, the fill may be moved in the loop above
+ htimetot->Fill(tot,time-meantime); // if meantime is not used, the fill may be moved in the loop above
+ }
+ }
+ }
+
+ hSlewingProf->Fit("pol5",optionFit,"",0,4);
+ TF1 * calibfunc = (TF1*)hSlewingProf->GetFunction("pol5");
+ Float_t par[6];
+ for(Int_t kk=0;kk<6;kk++){
+ par[kk]=calibfunc->GetParameter(kk);
+ AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
+ }
+
+ if(strstr(optionSave,"save")){
+ TFile * fileProf = new TFile("TOFCalibSave.root","recreate");
+ fileProf->cd();
+ TString profName=Form("Profile%06i_%06i",ichmin,ichmax);
+ TString timeTotName=Form("TimeTot%06i_%06i",ichmin,ichmax);
+ TString totName=Form("Tot%06i_%06i",ichmin,ichmax);
+ TString deltaName=Form("Delta%06i_%06i",ichmin,ichmax);
+ hSlewingProf->Write(profName);
+ htimetot->Write(timeTotName);
+ hToT->Write(totName);
+ hdeltaTime->Write(deltaName);
+ fileProf->Close();
+ delete fileProf;
+ fileProf=0x0;
+ }
+
+ delete hToT;
+ hToT=0x0;
+ delete hSlewingProf;
+ hSlewingProf=0x0;
+ delete htimetot;
+ htimetot=0x0;
+ delete hdeltaTime;
+ hdeltaTime=0x0;
+
+ AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(ichmin);
+ calChannel->SetSlewPar(par);
+ WriteParOfflineOnCDB("TOF/Calib","valid");
+ return 0;
+}
+//----------------------------------------------------------------------------
+Int_t AliTOFcalib::Calibrate(Int_t i, Option_t *optionSave, Option_t *optionFit){
+
+ // computing calibration parameters for channel i
+ // Returning codes:
+ // 0 -> everything was ok
+ // 1 -> no tree for calibration found
+ // 2 -> not enough statistics to perform calibration
+ // 3 -> problems with arrays
+
+ TH1::AddDirectory(0);
+
+ AliInfo(Form("*** Calibrating Histograms (one channel) %s", GetName())) ;
+ AliInfo(Form("Option for Saving histos = %s",optionSave )) ;
+ AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ;
+
+ Float_t p[MAXCHENTRIESSMALL];
+ Int_t nentries;
+ //fTree->SetBranchAddress("nentries",&nentries);
+ //fTree->SetBranchAddress("TOFentries",p);
+ fChain->SetBranchAddress("nentries",&nentries);
+ fChain->SetBranchAddress("TOFentries",p);
+
+ Float_t ntracksTotal =0;
+ for (Int_t irun=0;irun<fNruns;irun++){
+ Int_t ientry = -1;
+ ientry = i+irun*fNChannels;
+ //fTree->GetEntry(ientry);
+ fChain->GetEntry(ientry);
+ ntracksTotal+=nentries/3;
+ }
+
+ if (ntracksTotal < MEANENTRIES) {
+ AliInfo(Form(" Too small mean number of entires per channel (mean number = %f) not calibrating and exiting.....",ntracksTotal));
+ return 2;
+ }
+
+ //filling ToT and Time arrays
+
+ Int_t nbinToT = 100; // ToT bin width in Profile = 48.8 ps
+ Float_t minToT = 0; // ns
+ Float_t maxToT = 4.88; // ns
+
+ TH1F *hToT = new TH1F("htot","htot",nbinToT, minToT, maxToT);
+ TH1F *hdeltaTime = new TH1F("hdeltaTime","hdeltaTime",200,2,4);
+ Int_t ntracksRun = 0;
+ Double_t binsProfile[101]; // sized larger than necessary, the correct
+ // dim being set in the booking of the profile
+ Int_t nusefulbins=0;
+ Float_t meantime=0;
+ for (Int_t irun=0;irun<fNruns;irun++){
+ Int_t ientry = -1;
+ ientry = i+irun*fNChannels;
+ //fTree->GetEntry(ientry);
+ fChain->GetEntry(ientry);
+ ntracksRun=nentries/3;
+ AliDebug(2,Form("run %i, channel %i, nentries = %i, ntracksRun = %i",irun, i ,nentries, ntracksRun));
+ for (Int_t j=0;j<ntracksRun;j++){
+ Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT;
+ Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME;
+ Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID;
+ Float_t tot = p[idxexToT];
+ meantime+=p[idxexTime]-p[idxexExTime];
+ hdeltaTime->Fill(p[idxexTime]-p[idxexExTime]);
+ hToT->Fill(tot);
+ }
+ }
+
+ nusefulbins = FindBins(hToT,&binsProfile[0]);
+ meantime/=ntracksTotal;
+ AliDebug(2,Form("meantime = %f",meantime));
+
+ for (Int_t j=1;j<=nusefulbins;j++) {
+ AliDebug(2,Form(" channel %i, nusefulbins = %i, bin %i = %f",i,nusefulbins,j,binsProfile[j]));
+ }
+
+ TProfile* hSlewingProf = new TProfile("hSlewingProf", "hSlewingProf",nusefulbins, binsProfile, "G"); // CHECK THE BUILD OPTION, PLEASE!!!!!!
+ TH2F * htimetot = new TH2F("htimetot","htimetot",nbinToT, minToT, maxToT,600,-5,10);
+ for (Int_t irun=0;irun<fNruns;irun++){
+ Int_t ientry = -1;
+ ientry = i+irun*fNChannels;
+ //fTree->GetEntry(ientry);
+ fChain->GetEntry(ientry);
+ ntracksRun=nentries/3;
+ for (Int_t j=0;j<ntracksRun;j++){
+ Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT;
+ Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME;
+ Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID;
+ Float_t tot = p[idxexToT];
+ Float_t time = p[idxexTime]-p[idxexExTime];
+ AliDebug (2,Form("track = %i, time = %f, tot = %f, time-meantime = %f",j,time, tot, time-meantime));
+ hSlewingProf->Fill(tot,time); // if meantime is not used, the fill may be moved in the loop above
+ htimetot->Fill(tot,time-meantime); // if meantime is not used, the fill may be moved in the loop above
+ }
+ }
+
+ hSlewingProf->Fit("pol5",optionFit,"",0,4);
+ TF1 * calibfunc = (TF1*)hSlewingProf->GetFunction("pol5");
+ Float_t par[6];
+ for(Int_t kk=0;kk<6;kk++){
+ par[kk]=calibfunc->GetParameter(kk);
+ AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
+ }
+
+
+ if(strstr(optionSave,"save")){
+ TFile * fileProf = new TFile("TOFCalibSave.root","recreate");
+ fileProf->cd();
+ TString profName=Form("Profile%06i",i);
+ TString timeTotName=Form("TimeTot%06i",i);
+ TString totName=Form("Tot%06i",i);
+ TString deltaName=Form("Delta%06i",i);
+ hSlewingProf->Write(profName);
+ htimetot->Write(timeTotName);
+ hToT->Write(totName);
+ hdeltaTime->Write(deltaName);
+ fileProf->Close();
+ delete fileProf;
+ fileProf=0x0;
+ }
+
+ delete hToT;
+ hToT=0x0;
+ delete hSlewingProf;
+ hSlewingProf=0x0;
+ delete htimetot;
+ htimetot=0x0;
+ delete hdeltaTime;
+ hdeltaTime=0x0;
+
+ AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(i);
+ calChannel->SetSlewPar(par);
+ WriteParOfflineOnCDB("TOF/Calib","valid");
+ return 0;
+}
+//----------------------------------------------------------------------------
+Int_t AliTOFcalib::Calibrate(Int_t nch, Int_t *ch, Option_t *optionSave, Option_t *optionFit){
+
+ // calibrating an array of channels
+ // computing calibration parameters
+ // Returning codes:
+ // 0 -> everything was ok
+ // 1 -> no tree for calibration found
+ // 2 -> not enough statistics to perform calibration
+ // 3 -> problems with arrays
+
+ TH1::AddDirectory(0);
+
+ AliInfo(Form("*** Calibrating Histograms %s, number of channels = %i", GetName(),nch)) ;
+ AliInfo(Form("Option for Saving histos = %s",optionSave )) ;
+ AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ;
+ for (Int_t ich=0; ich<nch; ich++){
+ Int_t i = ch[ich];
+ AliInfo(Form("Calibrating channel = %i",i )) ;
+ }
+ Float_t p[MAXCHENTRIESSMALL];
+ Int_t nentries;
+ //fTree->SetBranchAddress("nentries",&nentries);
+ //fTree->SetBranchAddress("TOFentries",p);
+ fChain->SetBranchAddress("nentries",&nentries);
+ fChain->SetBranchAddress("TOFentries",p);
+
+ Float_t ntracksTotalmean =0;
+ for (Int_t ich=0; ich<nch; ich++){
+ Int_t ientry = -1;
+ Int_t i = ch[ich];
+ for (Int_t irun=0;irun<fNruns;irun++){
+ ientry = i+irun*fNChannels;
+ //fTree->GetEntry(ientry);
+ fChain->GetEntry(ientry);
+ ntracksTotalmean+=nentries/3;
+ }
+ }
+
+ ntracksTotalmean/=nch;
+ if (ntracksTotalmean < MEANENTRIES) {
+ AliInfo(Form(" Too small mean number of entires per channel (mean number = %f) not calibrating and exiting.....",ntracksTotalmean));
+ return 2;
+ }
+
+ //filling ToT and Time arrays
+
+ Int_t nbinToT = 100; // ToT bin width in Profile = 48.8 ps
+ Float_t minToT = 0; // ns
+ Float_t maxToT = 4.88; // ns
+ TFile * fileProf=0x0;
+ if(strstr(optionSave,"save")){
+ fileProf = new TFile("TOFCalibSave.root","recreate");
+ }
+ for (Int_t ich=0; ich<nch; ich++) {
+ TH1F *hToT = new TH1F("htot","htot",nbinToT, minToT, maxToT);
+ TH1F *hdeltaTime = new TH1F("hdeltaTime","hdeltaTime",200,2,4);
+ Double_t binsProfile[101]; // sized larger than necessary, the correct
+ // dim being set in the booking of the profile
+ TH2F * htimetot = new TH2F("htimetot","htimetot",nbinToT, minToT, maxToT,600,-5,10);
+ Int_t ntracksTotal = 0;
+ Int_t ntracksRun = 0;
+ Int_t nusefulbins=0;
+ Float_t meantime=0;
+ Int_t i=-1;
+ for (Int_t irun=0;irun<fNruns;irun++){
+ i = ch[ich]+irun*fNChannels;
+ AliDebug(2,Form("Calibrating channel %i",i));
+ //fTree->GetEntry(i);
+ fChain->GetEntry(i);
+ ntracksTotal+=nentries/3;
+ }
+ if (ntracksTotal < MEANENTRIES) {
+ AliInfo(Form(" Too small mean number of entires in channel %i (number of tracks = %d), not calibrating channel and continuing.....",i,ntracksTotal));
+ continue;
+ }
+
+ for (Int_t irun=0;irun<fNruns;irun++){
+ i = ch[ich]+irun*fNChannels;
+ //fTree->GetEntry(i);
+ fChain->GetEntry(i);
+ ntracksRun=nentries/3;
+ for (Int_t j=0;j<ntracksRun;j++){
+ Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT;
+ Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME;
+ Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID;
+ Float_t tot = p[idxexToT];
+ hdeltaTime->Fill(p[idxexTime]-p[idxexExTime]);
+ meantime+=p[idxexTime]-p[idxexExTime];
+ hToT->Fill(tot);
+ }
+ }
+
+ nusefulbins = FindBins(hToT,&binsProfile[0]);
+ meantime/=ntracksTotal;
+ for (Int_t j=1;j<=nusefulbins;j++) {
+ AliDebug(2,Form(" channel %i, nusefulbins = %i, bin %i = %f",i,nusefulbins,j,binsProfile[j]));
+ }
+
+ TProfile* hSlewingProf = new TProfile("hSlewingProf", "hSlewingProf",nusefulbins, binsProfile, "G"); // CHECK THE BUILD OPTION, PLEASE!!!!!!
+ for (Int_t irun=0;irun<fNruns;irun++){
+ i = ch[ich]+irun*fNChannels;
+ //fTree->GetEntry(i);
+ fChain->GetEntry(i);
+ ntracksRun=nentries/3;
+ for (Int_t j=0;j<ntracksRun;j++){
+ Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT;
+ Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME;
+ Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID;
+ Float_t tot = p[idxexToT];
+ Float_t time = p[idxexTime]-p[idxexExTime];
+ AliDebug(2,Form("track = %i, time = %f, tot = %f, time-meantime = %f",j,time, tot, time-meantime));
+ hSlewingProf->Fill(tot,time); // if meantime is not used, the fill may be moved in the loop above
+ htimetot->Fill(tot,time-meantime); // if meantime is not used, the fill may be moved in the loop above
+ }
+ }
+
+ hSlewingProf->Fit("pol5",optionFit,"",1,4);
+ TF1 * calibfunc = (TF1*)hSlewingProf->GetFunction("pol5");
+ Float_t par[6];
+ for(Int_t kk=0;kk<6;kk++){
+ par[kk]=calibfunc->GetParameter(kk);
+ AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
+ }
+
+ if(strstr(optionSave,"save") && fileProf){
+ TString profName=Form("Profile%06i",i);
+ TString timeTotName=Form("TimeTot%06i",i);
+ TString totName=Form("Tot%06i",i);
+ TString deltaName=Form("Delta%06i",i);
+ fileProf->cd();
+ hSlewingProf->Write(profName);
+ htimetot->Write(timeTotName);
+ hToT->Write(totName);
+ hdeltaTime->Write(deltaName);
+ }
+
+ AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(i);
+ calChannel->SetSlewPar(par);
+ delete hToT;
+ hToT=0x0;
+ delete hSlewingProf;
+ hSlewingProf=0x0;
+ delete htimetot;
+ htimetot=0x0;
+ delete hdeltaTime;
+ hdeltaTime=0x0;
+ }
+
+ if(strstr(optionSave,"save") && fileProf){
+ fileProf->Close();
+ delete fileProf;
+ fileProf=0x0;
+ }
+ WriteParOfflineOnCDB("TOF/Calib","valid");
+
+ return 0;
+}
+//----------------------------------------------------------------------------
+Int_t AliTOFcalib::CalibrateFromProfile(Int_t ich, Option_t *optionSave, Option_t *optionFit){
+
+ // computing calibration parameters using the old profiling algo
+ // Returning codes:
+ // 0 -> everything was ok
+ // 1 -> no tree for calibration found
+ // 2 -> not enough statistics to perform calibration
+ // 3 -> problems with arrays
+
+ TH1::AddDirectory(0);
+
+ AliInfo(Form("*** Calibrating Histograms From Profile %s", GetName())) ;
+ AliInfo(Form("Option for Saving histos = %s",optionSave )) ;
+ AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ;
+ Float_t p[MAXCHENTRIESSMALL];
+ Int_t nentries;
+ Int_t ntracksTotal=0;
+ //fTree->SetBranchAddress("nentries",&nentries);
+ //fTree->SetBranchAddress("TOFentries",p);
+ fChain->SetBranchAddress("nentries",&nentries);
+ fChain->SetBranchAddress("TOFentries",p);
+
+ for (Int_t irun=0;irun<fNruns;irun++){
+ Int_t i = ich+irun*fNChannels;
+ //fTree->GetEntry(i);
+ fChain->GetEntry(i);
+ ntracksTotal+=nentries/3;
+ }
+
+ if (ntracksTotal < MEANENTRIES) {
+ AliInfo(Form(" Too small mean number of entires per channel (mean number = %d) not calibrating and exiting.....",ntracksTotal));
+ return 2;
+ }
+
+ TH1F * hProf = new TH1F();
+ hProf = Profile(ich);
+ hProf->Fit("pol5",optionFit,"",0,4);
+ TF1 * calibfunc = (TF1*)hProf->GetFunction("pol5");
+ Float_t par[6];
+ for(Int_t kk=0;kk<6;kk++){
+ par[kk]=calibfunc->GetParameter(kk);
+ AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
+ }
+
+ if(strstr(optionSave,"save")){
+ TFile * fileProf = new TFile("TOFCalibSave.root","recreate");
+ fileProf->cd();
+ TString profName=Form("Profile%06i",ich);
+ hProf->Write(profName);
+ fileProf->Close();
+ delete fileProf;
+ fileProf=0x0;
+ }
+
+ delete hProf;
+ hProf=0x0;
+ AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(ich);
+ calChannel->SetSlewPar(par);
+ WriteParOfflineOnCDB("TOF/Calib","valid");
+ return 0;
+}
+//----------------------------------------------------------------------------
+Int_t AliTOFcalib::Calibrate(Option_t *optionSave, Option_t *optionFit){
+
+ // calibrating the whole TOF
+ // computing calibration parameters
+ // Returning codes:
+ // 0 -> everything was ok
+ // 1 -> no tree for calibration found
+ // 2 -> not enough statistics to perform calibration
+ // 3 -> problems with arrays
+
+ TH1::AddDirectory(0);
+
+ AliInfo(Form("*** Calibrating Histograms %s, all channels", GetName())) ;
+ AliInfo(Form("Option for Saving histos = %s",optionSave )) ;
+ AliInfo(Form("Option for Fitting Profile histos = %s",optionFit )) ;
+
+ TFile * fileProf=0x0;
+ if(strstr(optionSave,"save")){
+ fileProf = new TFile("TOFCalibSave.root","recreate");
+ }
+
+ Float_t p[MAXCHENTRIESSMALL];
+ Int_t nentries;
+ //fTree->SetBranchAddress("nentries",&nentries);
+ //fTree->SetBranchAddress("TOFentries",p);
+ fChain->SetBranchAddress("nentries",&nentries);
+ fChain->SetBranchAddress("TOFentries",p);
+
+ Float_t ntracksTotalmean =0;
+ for (Int_t ii=0; ii<fNChannels; ii++){
+ for (Int_t irun=0;irun<fNruns;irun++){
+ Int_t i = ii+irun*fNChannels;
+ //fTree->GetEntry(i);
+ fChain->GetEntry(i);
+ ntracksTotalmean+=nentries/3;
+ }
+ }
+
+ ntracksTotalmean/=fNChannels;
+ if (ntracksTotalmean < MEANENTRIES) {
+ AliInfo(Form(" Too small mean number of entires per channel (mean number = %f) not calibrating and exiting.....",ntracksTotalmean));
+ return 2;
+ }
+
+ //filling ToT and Time arrays
+
+ Int_t nbinToT = 100; // ToT bin width in Profile = 50.0 ps
+ Float_t minToT = 0; // ns
+ Float_t maxToT = 4.88;// ns
+ for (Int_t ii=0; ii<fNChannels; ii++) {
+ TH1F *hToT = new TH1F("htot","htot",nbinToT, minToT, maxToT);
+ TH1F *hdeltaTime = new TH1F("hdeltaTime","hdeltaTime",200,2,4);
+ TH2F * htimetot = new TH2F("htimetot","htimetot",nbinToT, minToT, maxToT,600,-5,10);
+ if (ii%1000 == 0) AliDebug(1,Form("Calibrating channel %i ",ii));
+ //Int_t i = 3;
+ Int_t nusefulbins=0;
+ Double_t binsProfile[101]; // sized larger than necessary, the correct
+ // dim being set in the booking of the profile
+ Int_t ntracksRun = 0;
+ Int_t ntracksTotal = 0;
+ for (Int_t irun=0;irun<fNruns;irun++){
+ Int_t i = ii+irun*fNChannels;
+ //fTree->GetEntry(i);
+ fChain->GetEntry(i);
+ ntracksTotal+=nentries/3;
+ }
+ if (ntracksTotal < MEANENTRIES) {
+ AliInfo(Form(" Too small mean number of entires in channel %i (number of tracks = %d), not calibrating channel and continuing.....",ii,ntracksTotal));
+ continue;
+ }
+ Float_t meantime=0;
+ for (Int_t irun=0;irun<fNruns;irun++){
+ Int_t i = ii+irun*fNChannels;
+ //fTree->GetEntry(i);
+ fChain->GetEntry(i);
+ ntracksRun=nentries/3;
+ for (Int_t j=0;j<ntracksRun;j++){
+ Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT;
+ Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME;
+ Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID;
+ Float_t tot = p[idxexToT];
+ hdeltaTime->Fill(p[idxexTime]-p[idxexExTime]);
+ meantime+=p[idxexTime]-p[idxexExTime];
+ hToT->Fill(tot);
+ }
+ }
+ nusefulbins = FindBins(hToT,&binsProfile[0]);
+ meantime/=ntracksTotal;
+ for (Int_t j=0;j<nusefulbins;j++) {
+ AliDebug(2,Form(" channel %i, usefulbin = %i, low edge = %f",ii,j,binsProfile[j]));
+ }
+ TProfile* hSlewingProf = new TProfile("hSlewingProf", "hSlewingProf",nusefulbins, binsProfile, "G"); // CHECK THE BUILD OPTION, PLEASE!!!!!!
+ for (Int_t irun=0;irun<fNruns;irun++){
+ Int_t i = ii+irun*fNChannels;
+ //fTree->GetEntry(i);
+ fChain->GetEntry(i);
+ ntracksRun=nentries/3;
+ for (Int_t j=0;j<ntracksRun;j++){
+ Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT;
+ Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME;
+ Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID;
+ Float_t tot = p[idxexToT];
+ Float_t time = p[idxexTime]-p[idxexExTime];
+ AliDebug (2,Form("track = %i, time = %f, tot = %f, time-meantime = %f",j,time, tot, time-meantime));
+ hSlewingProf->Fill(tot,time); // if meantime is not used, the fill may be moved in the loop above
+ htimetot->Fill(tot,time-meantime); // if meantime is not used, the fill may be moved in the loop above
+ }
+ }
+ hSlewingProf->Fit("pol5",optionFit,"",1,4);
+ TF1 * calibfunc = (TF1*)hSlewingProf->GetFunction("pol5");
+ Float_t par[6];
+ for(Int_t kk=0;kk<6;kk++){
+ par[kk]=calibfunc->GetParameter(kk);
+ AliDebug(2,Form("parameter %i = %f",kk,par[kk]));
+ }
+
+ if(strstr(optionSave,"save") && fileProf){
+ TString profName=Form("Profile%06i",ii);
+ TString timeTotName=Form("TimeTot%06i",ii);
+ TString totName=Form("Tot%06i",ii);
+ TString deltaName=Form("Delta%06i",ii);
+ fileProf->cd();
+ hSlewingProf->Write(profName);
+ htimetot->Write(timeTotName);
+ hToT->Write(totName);
+ hdeltaTime->Write(deltaName);
+ }
+ AliTOFChannelOffline * calChannel = (AliTOFChannelOffline*)fTOFCalOffline->At(ii);
+ calChannel->SetSlewPar(par);
+
+ delete hToT;
+ hToT=0x0;
+ delete hSlewingProf;
+ hSlewingProf=0x0;
+ delete htimetot;
+ htimetot=0x0;
+ delete hdeltaTime;
+ hdeltaTime=0x0;
+ }
+
+ if(strstr(optionSave,"save")){
+ fileProf->Close();
+ delete fileProf;
+ fileProf=0x0;
+ }
+ WriteParOfflineOnCDB("TOF/Calib","valid");
+ return 0;
+}
+
+//-----------------------------------------------------------------------
+TH1F* AliTOFcalib::Profile(Int_t ich)
+{
+ // profiling algo
+
+ Float_t p[MAXCHENTRIESSMALL];
+ Int_t nentries;
+ //fTree->SetBranchAddress("nentries",&nentries);
+ //fTree->SetBranchAddress("TOFentries",p);
+ fChain->SetBranchAddress("nentries",&nentries);
+ fChain->SetBranchAddress("TOFentries",p);
+
+ //Prepare histograms for Slewing Correction
+ const Int_t knbinToT = 100;
+ Int_t nbinTime = 200;
+ Float_t minTime = -5.5; //ns
+ Float_t maxTime = 5.5; //ns
+ Float_t minToT = 0; //ns
+ Float_t maxToT = 5.; //ns
+ Float_t deltaToT = (maxToT-minToT)/knbinToT;
+ 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];
+ Int_t n[knbinToT+1], nentrx[knbinToT+1];
+ Double_t sigmaToT[knbinToT+1];
+ for (Int_t i = 0; i < knbinToT+1 ; i++){
+ mTime[i]=0;
+ mToT[i]=0;
+ n[i]=0;
+ meanTime[i]=0;
+ meanTime2[i]=0;
+ vToT[i]=0;
+ vToT2[i]=0;
+ meanToT[i]=0;
+ meanToT2[i]=0;
+ vTime[i]=0;
+ vTime2[i]=0;
+ xlow[i]=0;
+ sigmaTime[i]=0;
+ sigmaToT[i]=0;
+ n[i]=0;
+ nentrx[i]=0;
+ }
+ TH2F* hSlewing = new TH2F("hSlewing", "hSlewing", knbinToT, minToT, maxToT, nbinTime, minTime, maxTime);
+ Int_t ntracksRun = 0;
+ TH1F *histo = new TH1F("histo", "1D Time vs ToT", knbinToT, minToT, maxToT);
+ for (Int_t irun=0;irun<fNruns;irun++){
+ Int_t i = ich+irun*fNChannels;
+ //fTree->GetEntry(i);
+ fChain->GetEntry(i);
+ ntracksRun=nentries/3;
+ for (Int_t j=0;j<ntracksRun;j++){
+ Int_t idxexToT = (j* NIDXSMALL)+DELTAIDXTOT;
+ Int_t idxexTime = (j* NIDXSMALL)+DELTAIDXTIME;
+ Int_t idxexExTime = (j* NIDXSMALL)+DELTAIDXPID;
+ Float_t tot = p[idxexToT];
+ Float_t time = p[idxexTime]-p[idxexExTime];
+ Int_t nx = (Int_t)((tot-minToT)/deltaToT)+1;
+ if ((tot != 0) && ( time!= 0)){
+ vTime[nx]+=time;
+ vTime2[nx]+=time*time;
+ vToT[nx]+=tot;
+ vToT2[nx]+=tot*tot;
+ nentrx[nx]++;
+ hSlewing->Fill(tot,time);
+ }
+ }
+ }
+ Int_t nbinsToT=hSlewing->GetNbinsX();
+ if (nbinsToT != knbinToT) {
+ AliError("Profile :: incompatible numbers of bins");
+ return 0x0;
+ }
+
+ Int_t usefulBins=0;
+ for (Int_t i=1;i<=nbinsToT;i++){
+ if (nentrx[i]!=0){
+ n[usefulBins]+=nentrx[i];
+ if (n[usefulBins]==0 && i == nbinsToT) {
+ break;
+ }
+ meanTime[usefulBins]+=vTime[i];
+ meanTime2[usefulBins]+=vTime2[i];
+ meanToT[usefulBins]+=vToT[i];
+ meanToT2[usefulBins]+=vToT2[i];
+ if (n[usefulBins]<10 && i!=nbinsToT) continue;
+ mTime[usefulBins]=meanTime[usefulBins]/n[usefulBins];
+ mToT[usefulBins]=meanToT[usefulBins]/n[usefulBins];
+ sigmaTime[usefulBins]=TMath::Sqrt(1./n[usefulBins]/n[usefulBins]
+ *(meanTime2[usefulBins]-meanTime[usefulBins]
+ *meanTime[usefulBins]/n[usefulBins]));
+ if ((1./n[usefulBins]/n[usefulBins]
+ *(meanToT2[usefulBins]-meanToT[usefulBins]
+ *meanToT[usefulBins]/n[usefulBins]))< 0) {
+ AliError(" too small radical" );
+ sigmaToT[usefulBins]=0;
+ }
+ else{
+ sigmaToT[usefulBins]=TMath::Sqrt(1./n[usefulBins]/n[usefulBins]
+ *(meanToT2[usefulBins]-meanToT[usefulBins]
+ *meanToT[usefulBins]/n[usefulBins]));
+ }
+ usefulBins++;
+ }
+ }
+ for (Int_t i=0;i<usefulBins;i++){
+ Int_t binN = (Int_t)((mToT[i]-minToT)/deltaToT)+1;
+ histo->Fill(mToT[i],mTime[i]);
+ histo->SetBinError(binN,sigmaTime[i]);
+ }
+ delete hSlewing;
+ hSlewing=0x0;
+
+ return histo;
+}
+//----------------------------------------------------------------------------
+Int_t AliTOFcalib::FindBins(TH1F* h, Double_t *binsProfile) const{
+
+ // to determine the bins for ToT histo
+
+ Int_t cont = 0;
+ Int_t startBin = 1;
+ Int_t nbin = h->GetNbinsX();
+ Int_t nentries = (Int_t)h->GetEntries();
+ Float_t max = h->GetBinLowEdge(nbin);
+ Int_t nusefulbins=0;
+ Int_t maxcont=0;
+ // setting maxvalue of entries per bin
+ if (nentries <= 60) maxcont = 2;
+ else if (nentries <= 100) maxcont = 5;
+ else if (nentries <= 500) maxcont = 10;
+ else maxcont = 20;
+ for (Int_t j=1;j<=nbin;j++) {
+ cont += (Int_t)h->GetBinContent(j);
+ if (j<nbin){
+ if (cont>=maxcont){
+ nusefulbins++;
+ binsProfile[nusefulbins-1]=h->GetBinLowEdge(startBin);
+ cont=0;
+ startBin=j+1;
+ continue;
+ }
+ }
+ else{
+ if (cont>=maxcont){
+ nusefulbins++;
+ binsProfile[nusefulbins-1]=h->GetBinLowEdge(startBin);
+ binsProfile[nusefulbins]=max;
+ }
+ else {
+ binsProfile[nusefulbins]=h->GetBinLowEdge(startBin);
+ }
+ }
+ }
+ return nusefulbins;
+}
+
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::CreateDeltaBCOffset()
+{
+ /*
+ * create deltaBC offset
+ */
+
+ if (fDeltaBCOffset) {
+ AliWarning("DeltaBCOffset object already defined, cannot create a new one");
+ return;
+ }
+ fDeltaBCOffset = new AliTOFDeltaBCOffset();
+}
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::CreateCTPLatency()
+{
+ /*
+ * create CTP latency
+ */
+
+ if (fCTPLatency) {
+ AliWarning("CTPLatency object already defined, cannot create a new one");
+ return;
+ }
+ fCTPLatency = new AliTOFCTPLatency();
+}
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::CreateT0Fill()
+{
+ /*
+ * create event-time
+ */
+
+ if (fT0Fill) {
+ AliWarning("T0Fill object already defined, cannot create a new one");
+ return;
+ }
+ fT0Fill = new AliTOFT0Fill();
+}
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::CreateRunParams()
+{
+ /*
+ * create run params
+ */
+
+ if (fRunParams) {
+ AliWarning("RunParams object already defined, cannot create a new one");
+ return;
+ }
+ fRunParams = new AliTOFRunParams();
+}
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::WriteDeltaBCOffsetOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
+{
+ /*
+ * deltaBC offset on CDB
+ */
+
+ if (!fDeltaBCOffset) return;
+ AliCDBId id(Form("%s/DeltaBCOffset", sel), minrun, maxrun);
+ AliCDBMetaData *md = new AliCDBMetaData();
+ md->SetResponsible("Roberto Preghenella");
+ AliCDBManager *man = AliCDBManager::Instance();
+ man->Put(fDeltaBCOffset, id, md);
+ AliDebug(2,Form("DeltaBCOffset written on CDB with run range [%i, %i] ",minrun ,maxrun));
+ delete md;
+}
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::WriteCTPLatencyOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
+{
+ /*
+ * write CTP latency on CDB
+ */
+
+ if (!fCTPLatency) return;
+ AliCDBId id(Form("%s/CTPLatency", sel), minrun, maxrun);
+ AliCDBMetaData *md = new AliCDBMetaData();
+ md->SetResponsible("Roberto Preghenella");
+ AliCDBManager *man = AliCDBManager::Instance();
+ man->Put(fCTPLatency, id, md);
+ AliDebug(2,Form("CTPLatency written on CDB with run range [%i, %i] ",minrun ,maxrun));
+ delete md;
+}
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::WriteT0FillOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
+{
+ /*
+ * write event-time on CDB
+ */
+
+ if (!fT0Fill) return;
+ AliCDBId id(Form("%s/T0Fill", sel), minrun, maxrun);
+ AliCDBMetaData *md = new AliCDBMetaData();
+ md->SetResponsible("Roberto Preghenella");
+ AliCDBManager *man = AliCDBManager::Instance();
+ man->Put(fT0Fill, id, md);
+ AliDebug(2,Form("T0Fill written on CDB with run range [%i, %i] ",minrun ,maxrun));
+ delete md;
+}
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::WriteRunParamsOnCDB(const Char_t *sel , Int_t minrun, Int_t maxrun)
+{
+ /*
+ * write run params on CDB
+ */
+
+ if (!fRunParams) return;
+ AliCDBId id(Form("%s/RunParams", sel), minrun, maxrun);
+ AliCDBMetaData *md = new AliCDBMetaData();
+ md->SetResponsible("Roberto Preghenella");
+ AliCDBManager *man = AliCDBManager::Instance();
+ man->Put(fRunParams, id, md);
+ AliDebug(2,Form("RunParams written on CDB with run range [%i, %i] ",minrun ,maxrun));
+ delete md;
+}
+
+//----------------------------------------------------------------------------
+
+Bool_t
+AliTOFcalib::ReadDeltaBCOffsetFromCDB(const Char_t *sel , Int_t nrun)
+{
+ /*
+ * read deltaBC offset from CDB
+ */
+
+ AliCDBManager *man = AliCDBManager::Instance();
+ AliCDBEntry *entry = man->Get(Form("%s/DeltaBCOffset", sel),nrun);
+ if (!entry) {
+ AliFatal("No DeltaBCOffset entry found in CDB");
+ exit(0);
+ }
+ fDeltaBCOffset =(AliTOFDeltaBCOffset *)entry->GetObject();
+ if(!fDeltaBCOffset){
+ AliFatal("No DeltaBCOffset object found in CDB entry");
+ exit(0);
+ }
+ return kTRUE;
+}
+
+//----------------------------------------------------------------------------
+
+Bool_t
+AliTOFcalib::ReadCTPLatencyFromCDB(const Char_t *sel , Int_t nrun)
+{
+ /*
+ * read CTP latency from CDB
+ */
+
+ AliCDBManager *man = AliCDBManager::Instance();
+ AliCDBEntry *entry = man->Get(Form("%s/CTPLatency", sel),nrun);
+ if (!entry) {
+ AliFatal("No CTPLatency entry found in CDB");
+ exit(0);
+ }
+ fCTPLatency =(AliTOFCTPLatency *)entry->GetObject();
+ if(!fCTPLatency){
+ AliFatal("No CTPLatency object found in CDB entry");
+ exit(0);
+ }
+ return kTRUE;
+}
+
+//----------------------------------------------------------------------------
+
+Bool_t
+AliTOFcalib::ReadT0FillFromCDB(const Char_t *sel , Int_t nrun)
+{
+ /*
+ * read event-time from CDB
+ */
+
+ AliCDBManager *man = AliCDBManager::Instance();
+ AliCDBEntry *entry = man->Get(Form("%s/T0Fill", sel),nrun);
+ if (!entry) {
+ AliFatal("No T0Fill entry found in CDB");
+ exit(0);
+ }
+ fT0Fill =(AliTOFT0Fill *)entry->GetObject();
+ if(!fT0Fill){
+ AliFatal("No T0Fill object found in CDB entry");
+ exit(0);
+ }
+ return kTRUE;
+}
+
+//----------------------------------------------------------------------------
+
+Bool_t
+AliTOFcalib::ReadRunParamsFromCDB(const Char_t *sel , Int_t nrun)
+{
+ /*
+ * read run params from CDB
+ */
+
+ AliCDBManager *man = AliCDBManager::Instance();
+ AliCDBEntry *entry = man->Get(Form("%s/RunParams", sel),nrun);
+ if (!entry) {
+ AliFatal("No RunParams entry found in CDB");
+ exit(0);
+ }
+ fRunParams =(AliTOFRunParams *)entry->GetObject();
+ if(!fRunParams){
+ AliFatal("No RunParams object found in CDB entry");
+ exit(0);
+ }
+ return kTRUE;
+}
+
+//----------------------------------------------------------------------------
+
+Bool_t
+AliTOFcalib::Init(Int_t run)
+{
+ /*
+ * init
+ */
+
+ if (fInitFlag) {
+ AliWarning("the class was already initialized, re-initialize it");
+ fInitFlag = kFALSE;
+ }
+
+ /* read channel status array */
+ if (!ReadParOnlineStatusFromCDB("TOF/Calib", run)) {
+ AliError("cannot get \"Status\" object from OCDB");
+ return kFALSE;
+ }
+ /* get par offline array */
+ if (!ReadParOfflineFromCDB("TOF/Calib", run)) {
+ AliError("cannot get \"ParOffline\" object from OCDB");
+ return kFALSE;
+ }
+ /* get deltaBC offset obj */
+ if (!ReadDeltaBCOffsetFromCDB("TOF/Calib", run)) {
+ AliError("cannot get \"DeltaBCOffset\" object from OCDB");
+ return kFALSE;
+ }
+ /* get CTP latency obj */
+ if (!ReadCTPLatencyFromCDB("TOF/Calib", run)) {
+ AliError("cannot get \"CTPLatency\" object from OCDB");
+ return kFALSE;
+ }
+ /* get run params obj */
+ if (!ReadRunParamsFromCDB("TOF/Calib", run)) {
+ AliError("cannot get \"RunParams\" object from OCDB");
+ return kFALSE;
+ }
+ /* get response params */
+ if (fCorrectTExp) {
+ TFile *responseFile = TFile::Open("$ALICE_ROOT/TOF/data/AliTOFresponsePar.root");
+ if (!responseFile || !responseFile->IsOpen()) {
+ AliError("cannot open \"ResponseParams\" local file");
+ return kFALSE;
+ }
+ fResponseParams = (AliTOFResponseParams *)responseFile->Get("ResponseParams");
+ if (!fResponseParams) {
+ AliError("cannot get \"ResponseParams\" object from local file");
+ return kFALSE;
+ }
+ responseFile->Close();
+ }
+
+ /* all done */
+ fInitFlag = kTRUE;
+ return kTRUE;
+
+}
+
+//----------------------------------------------------------------------------
+
+Double_t
+AliTOFcalib::GetTimeCorrection(Int_t index, Double_t tot, Int_t deltaBC, Int_t l0l1, UInt_t timestamp)
+{
+ /*
+ * get time correction
+ */
+
+ if (!fInitFlag) {
+ AliError("class not yet initialized. Initialize it before.");
+ return 0.;
+ }
+
+ /* get calibration params */
+ AliTOFChannelOffline *parOffline = (AliTOFChannelOffline *)fTOFCalOffline->At(index);
+ Int_t deltaBCOffset = fDeltaBCOffset->GetDeltaBCOffset();
+ Float_t ctpLatency = fCTPLatency->GetCTPLatency();
+ Float_t tdcLatencyWindow = fStatus->GetLatencyWindow(index) * 1.e3;
+ Float_t timezero = fRunParams->EvalT0(timestamp);
+ /* check whether to remove mean T0.
+ * useful when one wants to compute mean T0 */
+ if (!fRemoveMeanT0) timezero = 0.;
+
+ /* compute correction */
+ Double_t corr = 0.;
+ /* deltaBC correction */
+ deltaBC = deltaBCOffset; /* inhibit deltaBC correction for the time being */
+ corr += (deltaBC - deltaBCOffset) * AliTOFGeometry::BunchCrossingBinWidth();
+ /* L0-L1 latency correction */
+ corr -= l0l1 * AliTOFGeometry::BunchCrossingBinWidth();
+ /* CTP latency correction */
+ corr -= ctpLatency;
+ /* TDC latency window correction */
+ corr += tdcLatencyWindow;
+ /* time-zero correction */
+ corr += timezero;
+ /* time calibration correction */
+ if (tot < AliTOFGeometry::SlewTOTMin())
+ tot = AliTOFGeometry::SlewTOTMin();
+ if (tot > AliTOFGeometry::SlewTOTMax())
+ tot = AliTOFGeometry::SlewTOTMax();
+ for (Int_t islew = 0; islew < 6; islew++)
+ corr += parOffline->GetSlewPar(islew) * TMath::Power(tot, islew) * 1.e3;
+
+ /* return correction */
+ return corr;
+}
+
+//----------------------------------------------------------------------------
+
+void
+AliTOFcalib::CalibrateESD(AliESDEvent *event)
+{
+ /*
+ * calibrate ESD
+ */
+
+ if (!fInitFlag) {
+ AliError("class not yet initialized. Initialize it before.");
+ return;
+ }
+
+ /* loop over tracks */
+ AliESDtrack *track = NULL;
+ Int_t index, l0l1, deltaBC;
+ Double_t time, tot, corr, texp[AliPID::kSPECIES];
+ UInt_t timestamp = event->GetTimeStamp();
+ for (Int_t itrk = 0; itrk < event->GetNumberOfTracks(); itrk++) {
+
+ /* get track */
+ track = event->GetTrack(itrk);
+ if (!track || !(track->GetStatus() & AliESDtrack::kTOFout)) continue;
+
+ /* calibrate TOF signal */
+ if (fCalibrateTOFsignal) {
+ /* get info */
+ index = track->GetTOFCalChannel();
+ time = track->GetTOFsignalRaw();
+ tot = track->GetTOFsignalToT();
+ l0l1 = track->GetTOFL0L1();
+ deltaBC = track->GetTOFDeltaBC();
+ /* get correction */
+ corr = GetTimeCorrection(index, tot, deltaBC, l0l1, timestamp);
+ /* apply correction */
+ time -= corr;
+ /* set new TOF signal */
+ track->SetTOFsignal(time);
+ }
+
+ /* correct expected time */
+ if (fCorrectTExp) {
+ /* get integrated times */
+ track->GetIntegratedTimes(texp);
+ /* loop over particle types and correct expected time */
+ for (Int_t ipart = 0; ipart < AliPID::kSPECIES; ipart++)
+ texp[ipart] += fResponseParams->EvalTExpCorr(ipart, track->P());
+ /* set integrated times */
+ track->SetIntegratedTimes(texp);
+ }
+
+ }
+
+}
+
+//----------------------------------------------------------------------------
+
+Bool_t
+AliTOFcalib::IsChannelEnabled(Int_t index)
+{
+ /*
+ * is channel enabled
+ */
+
+ if (!fInitFlag) {
+ AliError("class not yet initialized. Initialize it before.");
+ return kTRUE;
+ }
+
+ /* check bad status */
+ if (fStatus->GetPulserStatus(index) == AliTOFChannelOnlineStatusArray::kTOFPulserBad) return kFALSE;
+ if (fStatus->GetNoiseStatus(index) == AliTOFChannelOnlineStatusArray::kTOFNoiseBad) return kFALSE;
+ if (fStatus->GetHWStatus(index) == AliTOFChannelOnlineStatusArray::kTOFHWBad) return kFALSE;
+
+ /* good status */
+ return kTRUE;
+
+}