<commit_before>#ifndef __CINT__
#endif
#include "triggerInfo.C"

// TODO read number of bits from AliVEvent?
#define NBITS 29
#define NMAXCLASSES 100
TString bitNames[NBITS] = {
    "kINT1",
    "kINT7",
    "kMUON",
    "kHighMultSPD",
    "kEMC1",
    "kINT5",
    "kINT7inMUON",
    "kMuonSingleHighPt7",
    "kMuonLikeLowPt7",
    "kMuonUnlikeLowPt7",
    "kEMC7",
    "kMuonSingleLowPt7",
    "kPHI1",
    "kPHI78",
    "kEMCEJE",
    "kEMCEGA",
    "kHighMultV0",
    "kSemiCentral",
    "kDG",
    "kZED",
    "kSPI78",
    "kINT8",
    "kMuonSingleLowPt8",
    "kMuonSingleHighPt8",
    "kMuonLikeLowPt8",
    "kMuonUnlikeLowPt8",
    "kMuonUnlikeLowPt0",
    "kUserDefined",
    "kTRD"
};

void writeTree(TFile* fout, TTree* t){
  fout->cd();
  t->Fill();
  t->Write();
  fout->Close();
}

//Int_t runLevelEventStatQA(TString qafilename="event_stat.root", Int_t run=254422, TString ocdbStorage = "raw://"){
//Int_t runLevelEventStatQA(TString qafilename="event_stat.root", Int_t run=255042, TString ocdbStorage = "raw://"){
Int_t runLevelEventStatQA(TString qafilename="EventStat_temp.root", Int_t run=256676, TString ocdbStorage = "local:///cvmfs/alice.cern.ch/calibration/data/2016/OCDB"){
  
  gStyle->SetOptStat(0);
  gStyle->SetLineScalePS(1.5);
  gStyle->SetPadBottomMargin(0.08);
  gStyle->SetPadRightMargin(0.02);
  gStyle->SetPadTopMargin(0.07);
  gStyle->SetPadLeftMargin(0.07);
  gStyle->SetLegendBorderSize(0);
  // tree variables
  TObjArray classes = TObjArray();
  TObjString activeDetectors = TObjString();
  TObjString objPartition = TObjString();
  TObjString objLhcState = TObjString();
  TObjString objLhcPeriod = TObjString();
  Int_t fill               = 0;
  Double_t run_duration    = 0;
  Int_t nBCsPerOrbit       = 0;
  Double_t refCounts       = 0;
  Double_t refTmaskCounts  = 0;
  Double_t mu              = 0;
  Double_t lumi_seen       = 0;
  Double_t interactionRate = 0;
  ULong64_t class_lMb[NMAXCLASSES]         = {0};
  ULong64_t class_lMa[NMAXCLASSES]         = {0};
  ULong64_t class_l0b[NMAXCLASSES]         = {0};
  ULong64_t class_l0a[NMAXCLASSES]         = {0};
  ULong64_t class_l1b[NMAXCLASSES]         = {0};
  ULong64_t class_l1a[NMAXCLASSES]         = {0};
  ULong64_t class_l2b[NMAXCLASSES]         = {0};
  ULong64_t class_l2a[NMAXCLASSES]         = {0};
  ULong64_t class_duration[NMAXCLASSES]    = {0};
  Double_t  class_lifetime[NMAXCLASSES]    = {0};
  Double_t  class_lumi[NMAXCLASSES]        = {0};
  Double_t  class_ds[NMAXCLASSES]          = {0};
  ULong64_t alias_recorded[NBITS]          = {0};
  ULong64_t alias_reconstructed[NBITS]     = {0};
  ULong64_t alias_accepted[NBITS]          = {0};
  ULong64_t alias_acc_step1[NBITS]         = {0};
  ULong64_t alias_acc_step2[NBITS]         = {0};
  ULong64_t alias_acc_step3[NBITS]         = {0};
  ULong64_t alias_acc_step4[NBITS]         = {0};
  ULong64_t alias_acc_step5[NBITS]         = {0};
  ULong64_t alias_acc_step6[NBITS]         = {0};
  ULong64_t alias_acc_step7[NBITS]         = {0};
  ULong64_t alias_acc_step8[NBITS]         = {0};
  ULong64_t alias_acc_step9[NBITS]         = {0};
  Double_t alias_l0b_rate[NBITS]           = {0};
  Double_t alias_lifetime[NBITS]           = {0};
  Double_t alias_lumi_recorded[NBITS]      = {0};
  Double_t alias_lumi_reconstructed[NBITS] = {0};
  Double_t alias_lumi_accepted[NBITS]      = {0};
  Int_t timeStart = 0;
  Int_t timeEnd = 0;
  Double_t meanV0MOn = 0;
  Double_t meanV0MOf = 0;
  Double_t meanOFO = 0;
  Double_t meanTKL = 0;
  Double_t meanErrV0MOn = 0;
  Double_t meanErrV0MOf = 0;
  Double_t meanErrOFO = 0;
  Double_t meanErrTKL = 0;
  Double_t meanV0MOnHM = 0;
  Double_t meanV0MOfHM = 0;
  Double_t meanOFOHM = 0;
  Double_t meanTKLHM = 0;
  Double_t meanErrV0MOnHM = 0;
  Double_t meanErrV0MOfHM = 0;
  Double_t meanErrOFOHM = 0;
  Double_t meanErrTKLHM = 0;
  Double_t thresholdV0M = 0;
  Double_t aV0MOnVsOfVal = 0;
  Double_t bV0MOnVsOfVal = 0;
  Double_t aV0MOnVsOfErr = 0;
  Double_t bV0MOnVsOfErr = 0;
  Double_t aSPDOnVsOfVal = 0;
  Double_t bSPDOnVsOfVal = 0;
  Double_t aSPDOnVsOfErr = 0;
  Double_t bSPDOnVsOfErr = 0;
  Double_t aV0MOnVsOfOADB = 0;
  Double_t bV0MOnVsOfOADB = 0;
  Double_t aSPDOnVsOfOADB = 0;
  Double_t bSPDOnVsOfOADB = 0;

  TH2F* hHistStat = new TH2F();

  TFile* fout = new TFile("trending.root","recreate");
  TTree* t = new TTree("trending","tree of trending variables");
  t->Branch("run",&run);
  t->Branch("fill",&fill);
  t->Branch("bcs",&nBCsPerOrbit);
  t->Branch("run_duration",&run_duration);
  t->Branch("mu",&mu);
  t->Branch("interactionRate",&interactionRate);
  t->Branch("refCounts",&refCounts);
  t->Branch("lumi_seen",&lumi_seen);
  t->Branch("classes",&classes);
  t->Branch("class_lMb",&class_lMb,Form("class_lMb[%i]/l",NMAXCLASSES));
  t->Branch("class_lMa",&class_lMa,Form("class_lMa[%i]/l",NMAXCLASSES));
  t->Branch("class_l0b",&class_l0b,Form("class_l0b[%i]/l",NMAXCLASSES));
  t->Branch("class_l0a",&class_l0a,Form("class_l0a[%i]/l",NMAXCLASSES));
  t->Branch("class_l1b",&class_l1b,Form("class_l1b[%i]/l",NMAXCLASSES));
  t->Branch("class_l1a",&class_l1a,Form("class_l1a[%i]/l",NMAXCLASSES));
  t->Branch("class_l2b",&class_l2b,Form("class_l2b[%i]/l",NMAXCLASSES));
  t->Branch("class_l2a",&class_l2a,Form("class_l2a[%i]/l",NMAXCLASSES));
  t->Branch("class_duration",&class_duration,Form("class_duration[%i]/l",NMAXCLASSES));
  t->Branch("class_lifetime",&class_lifetime,Form("class_lifetime[%i]/D",NMAXCLASSES));
  t->Branch("class_lumi",&class_lumi,Form("class_lumi[%i]/D",NMAXCLASSES));
  t->Branch("class_ds",&class_ds,Form("class_ds[%i]/D",NMAXCLASSES));
  t->Branch("alias_recorded",&alias_recorded,Form("alias_recorded[%i]/l",NBITS));
  t->Branch("alias_reconstructed",&alias_reconstructed,Form("alias_reconstructed[%i]/l",NBITS));
  t->Branch("alias_accepted",&alias_accepted,Form("alias_accepted[%i]/l",NBITS));
  t->Branch("alias_acc_step1",&alias_acc_step1,Form("alias_acc_step1[%i]/l",NBITS));
  t->Branch("alias_acc_step2",&alias_acc_step2,Form("alias_acc_step2[%i]/l",NBITS));
  t->Branch("alias_acc_step3",&alias_acc_step3,Form("alias_acc_step3[%i]/l",NBITS));
  t->Branch("alias_acc_step4",&alias_acc_step4,Form("alias_acc_step4[%i]/l",NBITS));
  t->Branch("alias_acc_step5",&alias_acc_step5,Form("alias_acc_step5[%i]/l",NBITS));
  t->Branch("alias_acc_step6",&alias_acc_step6,Form("alias_acc_step6[%i]/l",NBITS));
  t->Branch("alias_acc_step7",&alias_acc_step7,Form("alias_acc_step7[%i]/l",NBITS));
  t->Branch("alias_acc_step8",&alias_acc_step6,Form("alias_acc_step8[%i]/l",NBITS));
  t->Branch("alias_acc_step9",&alias_acc_step7,Form("alias_acc_step9[%i]/l",NBITS));
  t->Branch("alias_l0b_rate",&alias_lifetime,Form("alias_l0b_rate[%i]/D",NBITS));
  t->Branch("alias_lifetime",&alias_lifetime,Form("alias_lifetime[%i]/D",NBITS));
  t->Branch("alias_lumi_recorded",&alias_lumi_recorded,Form("alias_lumi_recorded[%i]/D",NBITS));
  t->Branch("alias_lumi_reconstructed",&alias_lumi_reconstructed,Form("alias_lumi_reconstructed[%i]/D",NBITS));
  t->Branch("alias_lumi_accepted",&alias_lumi_accepted,Form("alias_lumi_accepted[%i]/D",NBITS));
  t->Branch("activeDetectors",&activeDetectors);
  t->Branch("lhcState",&objLhcState);
  t->Branch("lhcPeriod",&objLhcPeriod);
  t->Branch("partition",&objPartition);
  t->Branch("timeStart",&timeStart);
  t->Branch("timeEnd",&timeEnd);
  t->Branch("meanV0MOn",&meanV0MOn);
  t->Branch("meanV0MOf",&meanV0MOf);
  t->Branch("meanOFO",&meanOFO);
  t->Branch("meanTKL",&meanTKL);
  t->Branch("meanErrV0MOn",&meanErrV0MOn);
  t->Branch("meanErrV0MOf",&meanErrV0MOf);
  t->Branch("meanErrOFO",&meanErrOFO);
  t->Branch("meanErrTKL",&meanErrTKL);
  t->Branch("meanV0MOnHM",&meanV0MOnHM);
  t->Branch("meanV0MOfHM",&meanV0MOfHM);
  t->Branch("meanOFOHM",&meanOFOHM);
  t->Branch("meanTKLHM",&meanTKLHM);
  t->Branch("meanErrV0MOnHM",&meanErrV0MOnHM);
  t->Branch("meanErrV0MOfHM",&meanErrV0MOfHM);
  t->Branch("meanErrOFOHM",&meanErrOFOHM);
  t->Branch("meanErrTKLHM",&meanErrTKLHM);
  t->Branch("thresholdV0M",&thresholdV0M);
  t->Branch("aV0MOnVsOfVal",&aV0MOnVsOfVal);
  t->Branch("bV0MOnVsOfVal",&bV0MOnVsOfVal);
  t->Branch("aV0MOnVsOfErr",&aV0MOnVsOfErr);
  t->Branch("bV0MOnVsOfErr",&bV0MOnVsOfErr);
  t->Branch("aSPDOnVsOfVal",&aSPDOnVsOfVal);
  t->Branch("bSPDOnVsOfVal",&bSPDOnVsOfVal);
  t->Branch("aSPDOnVsOfErr",&aSPDOnVsOfErr);
  t->Branch("bSPDOnVsOfErr",&bSPDOnVsOfErr);
  t->Branch("aV0MOnVsOfOADB",&aV0MOnVsOfOADB);
  t->Branch("bV0MOnVsOfOADB",&bV0MOnVsOfOADB);
  t->Branch("aSPDOnVsOfOADB",&aSPDOnVsOfOADB);
  t->Branch("bSPDOnVsOfOADB",&bSPDOnVsOfOADB);
  
  t->Branch("hHistStat",&hHistStat);
  
  TString refClass="";
  Double_t refSigma=-1;
  Double_t refEff = 1.;
  Double_t refMu = 1.e-11;
  if      (               run<=118501) { refSigma=  62.; refEff = 1.00; refClass = "CINT1B-ABCE-NOPF-ALL";   } // pp_7.00: 62mb=54.3mb*1.15=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=118502 && run<=118561) { refSigma=  47.; refEff = 1.00; refClass = "CINT1B-ABCE-NOPF-ALL";   } // pp_0.90: 47mb=52 mb *0.91=sigma(INEL)*R(INT1/INEL) (arxiv: 1208.4968, fig.10 + table 3)
  else if (run>=118903 && run<=120829) { refSigma=  62.; refEff = 1.00; refClass = "CINT1B-ABCE-NOPF-ALL";   } // pp_7.00: 62mb=54.3mb*1.15=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=121039 && run<=121040) { refSigma=  47.; refEff = 1.00; refClass = "CINT1B-ABCE-NOPF-ALL";   } // pp_0.90: 47mb=52 mb *0.91=sigma(INEL)*R(INT1/INEL) (arxiv: 1208.4968, fig.10 + table 3)
  else if (run>=121041 && run<=126437) { refSigma=  62.; refEff = 1.00; refClass = "CINT1B-ABCE-NOPF-ALL";   } // pp_7.00: 62mb=54.3mb*1.15=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=126438 && run<=127718) { refSigma=  62.; refEff = 1.00; refClass = "CINT1-B-NOPF-ALLNOTRD";  } // pp_7.00: 62mb=54.3mb*1.15=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=127719 && run<=127730) { refSigma=  62.; refEff = 1.00; refClass = "CINT1B-ABCE-NOPF-ALL";   } // pp_7.00: 62mb=54.3mb*1.15=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=127731 && run<=136848) { refSigma=  62.; refEff = 1.00; refClass = "CINT1-B-NOPF-ALLNOTRD";  } // pp_7.00: 62mb=54.3mb*1.15=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=136849 && run<=139316) { refSigma=5970.; refEff = 0.78; refClass = "C0SMH-B-NOPF-ALL";       } // PbPb_2.76: (Oyama,2011-05-20,RunCond), sigma_hardronic = 7.64 b
  else if (run>=139328 && run<=139517) { refSigma=5970.; refEff = 0.78; refClass = "C0SMH-B-NOPF-ALLNOTRD";  } // PbPb_2.76: (Oyama,2011-05-20,RunCond), sigma_hardronic = 7.64 b
  else if (run>=145289 && run<=146860) { refSigma=  57.; refEff = 1.00; refClass = "CINT1-B-NOPF-ALLNOTRD";  } // pp_2.76: 57mb=47.7mb*1.20=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=146808 && run<=146814) { refSigma=  57.; refEff = 1.00; refClass = "CINT1-B-NOPF-ALL";       } // pp_2.76: 57mb=47.7mb*1.20=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=145815 && run<=146856) { refSigma=  57.; refEff = 1.00; refClass = "CINT1-B-NOPF-ALLNOTRD";  } // pp_2.76: 57mb=47.7mb*1.20=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=146857 && run<=146857) { refSigma=  57.; refEff = 1.00; refClass = "CINT1-B-NOPF-ALL";       } // pp_2.76: 57mb=47.7mb*1.20=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=146858 && run<=146860) { refSigma=  57.; refEff = 1.00; refClass = "CINT1-B-NOPF-ALLNOTRD";  } // pp_2.76: 57mb=47.7mb*1.20=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=148370 && run<=157078) { refSigma=  54.; refEff = 1.00; refClass = "CVBAND-B-NOPF-ALLNOTRD"; } // pp_7.00: 54.3mb (Martino,2012-03-12,RunCond)
  else if (run>=157079 && run<=165746) { refSigma=  24.; refEff = 0.44; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_7.00: 24mb=54.3mb*0.44=sigma(VBAND)*R(0TVX/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=166477 && run<=170593) { refSigma=4100.; refEff = 0.54; refClass = "CVLN-B-NOPF-ALLNOTRD";   } // PbPb_2.76: (Martino,2013-03-15,RunCond)
  else if (run>=176658 && run<=177143) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177146 && run<=177147) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177148 && run<=177149) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177150 && run<=177165) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177166 && run<=177166) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177167 && run<=177167) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177168 && run<=177168) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177169 && run<=177172) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177173 && run<=177173) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177174 && run<=177506) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177507 && run<=178017) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=178018 && run<=178029) { refSigma=  67.; refEff = 0.90; refClass = "CINT1-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), CINT1/C0TVX=2.7 from 178052
  else if (run>=178030 && run<=178053) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=178055 && run<=178062) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALL";       } // pp_8.00: (Artem, 2013-10-04,RunCond), vdM
  else if (run>=178062 && run<=178220) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=179444 && run<=180715) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-S-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=180716 && run<=180720) { refSigma=  56.; refEff = 0.75; refClass = "CINT7-S-NOPF-ALLNOTRD";  } // no C0TVX in these runs, taking VBAND cross section
  else if (run>=180721 && run<=184844) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-S-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=184845 && run<=184990) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=184991 && run<=188229) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-S-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=188230 && run<=188366) { refSigma=1590.; refEff = 0.76; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pPb_5.02: pilot. arxiv:1405.1849
  else if (run>=188367 && run<=193692) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-S-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=193693 && run<=193766) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=195344 && run<=197388) { refSigma=1590.; refEff = 0.76; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pPb_5.02: arxiv:1405.1849
  else if (run>=197470 && run<=197692) { refSigma=  18.; refEff = 0.39; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_2.76: 18mb=47.7mb*0.39=sigma(VBAND)*R(0TVX/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=221835 && run<=223669) { refSigma= 16.8; refEff = 0.32; refClass = "CADAND-B-NOPF-ALLNOTRD"; } // estimates from Martino
  else if (run>=221670 && run<=223983) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // estimates from Martino and MC
  else if (run>=223984 && run<=223984) { refSigma= 50.0; refEff = 0.66; refClass = "CADAND-B-NOPF-ALLNOTRD"; } // estimates from Martino and MC
  else if (run>=223985 && run<=226110) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // estimates from Martino and MC
  else if (run>=226111 && run<=226115) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=226116 && run<=228909) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // estimates from Martino and MC
  else if (run>=228910 && run<=229376) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=229386 && run<=229398) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-MUON";      } // estimates from Martino and MC
  else if (run>=229409 && run<=229410) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=229416 && run<=229893) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-MUON";      } // estimates from Martino and MC
  else if (run>=229894 && run<=229899) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // estimates from Martino and MC
  else if (run>=229942 && run<=231321) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-MUON";      } // estimates from Martino and MC
  else if (run>=232914 && run<=233858) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENT";      } // estimates from Martino and MC
  else if (run>=233910 && run<=234050) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // estimates from Martino and MC
  else if (run>=234051 && run<=238669) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENT";      } // estimates from Martino and MC
  else if (run>=238670 && run<=240150) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=240151 && run<=240151) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-MUON";      } // estimates from Martino and MC
  else if (run>=240152 && run<=243373) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=243374 && run<=243398) { refSigma= 21.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=243399 && run<=243984) { refSigma=6700.; refEff = 0.90; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=243985 && run<=244912) { refSigma= 21.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=244913 && run<=246994) { refSigma=4600.; refEff = 0.60; refClass = "C0V0M-B-NOPF-CENTNOTRD"; } // estimates from Cvetan and Alberica
  else if (run>=246995 && run<=256147) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=256148 && run<=256157) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENT";      } // estimates from Martino and MC
  else if (run>=256158 && run<=265301) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=265304 && run<=265586) { refSigma=1590.; refEff = 0.76; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // taken from old p-Pb
  else if (run>=265587 && run<=267131) { refSigma=1715.; refEff = 0.82; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // T0 efficiency estimate from MC, refSigma assuming cs(INEL) = 2092
  else if (run>=267132 && run<=267166) { refSigma=1590.; refEff = 0.76; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // taken from old p-Pb
  else if (run>=267167 && run<=275923) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // back to pp at 13TeV
  else if (run>=275924 && run<=276096) { refSigma= 64.0; refEff = 0.85; refClass = "CINT7-B-NOPF-CENTNOTRD"; } // T0 is not operational, using V0AND
  else if (run>=276097 )               { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // back to pp at 13TeV

  
  Double_t orbitRate = 11245.;
  TString partition;
  TString lhcState;
  TString lhcPeriod;
  TString activeDetectorsString;
  //Int_t run, TString ocdbStorage, TString &partition, TString &activeDetectorsString, Double_t& run_duration, 
  classes = GetClasses(run,ocdbStorage,partition,activeDetectorsString,run_duration,class_lMb,class_lMa,class_l0b,class_l0a,class_l1b,class_l1a,class_l2b,class_l2a,class_duration);
  activeDetectors.SetString(activeDetectorsString.Data());
  objPartition.SetString(partition.Data());
  Int_t status = triggerInfo(run,ocdbStorage,lhcPeriod,lhcState,fill,nBCsPerOrbit,timeStart,timeEnd);
  if (status>0) { writeTree(fout,t); return 1; }
  objLhcState.SetString(lhcState.Data());
  objLhcPeriod.SetString(lhcPeriod.Data());

  AliTriggerClass* refClassObject = (AliTriggerClass*) classes.FindObject(refClass);
  if (!refClassObject) { writeTree(fout,t); return 2; }
  Int_t refId = classes.IndexOf(refClassObject);
  TString refCluster = refClassObject->GetCluster()->GetName();
  refCounts = (activeDetectorsString.Contains("TRD") && (refCluster.EqualTo("CENT") || refCluster.EqualTo("ALL") || refCluster.EqualTo("FAST"))) ? class_lMb[refId] : class_l0b[refId];
  if (refClass.Contains("1B-ABCE-")){
    Int_t emptyClassId = classes.IndexOf(classes.FindObject("CBEAMB-ABCE-NOPF-ALL"));
    Int_t emptyL0B = class_l0b[emptyClassId];
    if (nBCsPerOrbit<0 && emptyL0B>0) nBCsPerOrbit = Double_t(emptyL0B)/orbitRate/run_duration;
  } else {
    nBCsPerOrbit= refClassObject->GetBCMask()->GetNUnmaskedBCs();
  }
  
  Double_t totalBCs = orbitRate*run_duration*nBCsPerOrbit;
  if (totalBCs<1 || refCounts<1) { writeTree(fout,t); return 3; }
  refMu = -TMath::Log(1-Double_t(refCounts)/totalBCs); // mu
  Double_t refInteractionRate = (run_duration>1e-10) ? (refMu>1.e-10 ? refMu/(1-TMath::Exp(-refMu)):1)*refCounts/run_duration : 0;
  interactionRate = refInteractionRate/refEff; // INEL interaction rate 
  mu              = refMu/refEff;              // INEL mu value
  if (refSigma>1.e-10) lumi_seen = run_duration*refInteractionRate/refSigma/1000; //[ub-1]

  for (Int_t i=0;i<classes.GetEntriesFast();i++){
    // printf("%30s %12lli %10lli %10lli %10lli %10lli %10lli\n",classes.At(i)->GetName(),class_l0b[i],class_l0a[i],class_l1b[i],class_l1a[i],class_l2b[i],class_l2a[i]);
    class_lifetime[i] = class_lMb[i]>0 ? Double_t(class_lMa[i])/class_lMb[i]: 0;
    class_lifetime[i]*= class_l0b[i]>0 ? Double_t(class_l0a[i])/class_l0b[i]: 0;
    class_lifetime[i]*= class_l1b[i]>0 ? Double_t(class_l1a[i])/class_l1b[i]: 0;
    class_lifetime[i]*= class_l2b[i]>0 ? Double_t(class_l2a[i])/class_l2b[i]: 0;
    class_lumi[i] = lumi_seen*class_lifetime[i];
    AliTriggerClass* cl = (AliTriggerClass*) classes.At(i);
    cl->GetDownscaleFactor(class_ds[i]);
  }

  // special treatment for Pb-Pb
  if (run>=244917 && run<=246994) {
    for (Int_t i=0;i<classes.GetEntriesFast();i++){
      AliTriggerClass* cl = (AliTriggerClass*) classes.At(i);
      TObjArray* tokens = TString(cl->GetName()).Tokenize("-");
      TString cluster = tokens->At(3)->GetName();
      TString lifetimeClassName = Form("C0VHM-B-NOPF-%s",cluster.Data());
      if (run<245256) lifetimeClassName = Form("C0V0M-B-NOPF-%s",cluster.Data());
      TObject* lifetimeClass = classes.FindObject(lifetimeClassName.Data());
      if (lifetimeClass) {
        Int_t index = classes.IndexOf(lifetimeClass);
        if (class_ds[index]>0) {
          printf("%s %f %f\n",lifetimeClassName.Data(),class_ds[i],class_ds[index]);
          Float_t ds_ratio = class_ds[i]/class_ds[index];
          class_lifetime[i]=class_lifetime[index]*ds_ratio;
          class_lumi[i]=class_lumi[index]*ds_ratio;
        }
      }
      tokens->Delete();
      delete tokens;
    }
  }

  // special treatment for INT11
  if (run>=252235 && run<=255011 && run!=253434 && run!=253437) {
    AliTriggerClass* clINT11 = (AliTriggerClass*) classes.FindObject("CINT11-B-NOPF-CENTNOTRD");
    AliTriggerClass* cl0TVX  = (AliTriggerClass*) classes.FindObject("C0TVX-B-NOPF-CENTNOTRD");
    if (clINT11 && cl0TVX) {
      Int_t iCINT11 = classes.IndexOf(clINT11);
      Int_t iC0TVX  = classes.IndexOf(cl0TVX);
      class_lifetime[iCINT11] = class_lifetime[iC0TVX];
      class_lumi[iCINT11]     = class_lumi[iC0TVX];
    }
  }
  
  // special treatment for CUP2
  if (run>=255276 && run<=256418) {
    Int_t iCUP2=-1;
    Int_t iCTRUE_NOPF=-1;
    Int_t iCTRUE_SPD1=-1;
    Int_t iCVHMV0M=-1;
    for (Int_t i=0;i<classes.GetEntriesFast();i++){
      AliTriggerClass* cl = (AliTriggerClass*) classes.At(i);
      if (TString(cl->GetName()).EqualTo("CCUP2-B-SPD1-CENTNOTRD")   ) iCUP2=i;
      if (TString(cl->GetName()).EqualTo("CTRUE-B-SPD1-CENTNOTRD")   ) iCTRUE_SPD1=i;
      if (TString(cl->GetName()).EqualTo("CTRUE-B-NOPF-CENTNOTRD")   ) iCTRUE_NOPF=i;
      if (TString(cl->GetName()).EqualTo("CVHMV0M-B-NOPF-CENTNOTRD") ) iCVHMV0M=i;
    }
    Int_t iREF = iCVHMV0M>=0 ? iCVHMV0M : iCTRUE_NOPF;
    if (iCUP2>=0 && iCTRUE_NOPF>=0 && iCTRUE_SPD1>=0 && iREF>=0) {
      class_lifetime[iCUP2] = (class_ds[iREF]>1e-7 && class_l0b[iREF]>0) ? (Double_t(class_l0a[iREF])/class_l0b[iREF]/class_ds[iREF]) : (Double_t(class_l0a[iCTRUE_NOPF])/class_l0b[iCTRUE_NOPF]/class_ds[iCTRUE_NOPF]);
      class_lifetime[iCUP2]*=class_l2a[iCTRUE_SPD1];
      class_lifetime[iCUP2]*=class_l2a[iCTRUE_NOPF]>0 ? 1./class_l2a[iCTRUE_NOPF] : 0;
      class_lifetime[iCUP2]*=class_ds[iCUP2];
      class_lumi[iCUP2] = lumi_seen*class_lifetime[iCUP2];
    }
    if (iCUP2>=0) printf("lumi=%f\n",class_lumi[iCUP2]);
  }
  
  // special treatment for CUP13
  if (run>=256504) {
    Int_t iCUP13=-1;
    Int_t iCTRUE_NOPF=-1;
    Int_t iCTRUE_SPD1=-1;
    Int_t iCEMC7=-1;
    Int_t iCPHI7=-1;
    for (Int_t i=0;i<classes.GetEntriesFast();i++){
      AliTriggerClass* cl = (AliTriggerClass*) classes.At(i);
      if (TString(cl->GetName()).EqualTo("CCUP13-B-SPD1-CENTNOTRD")  ) iCUP13=i;
      if (TString(cl->GetName()).EqualTo("CTRUE-B-SPD1-CENTNOTRD")   ) iCTRUE_SPD1=i;
      if (TString(cl->GetName()).EqualTo("CTRUE-B-NOPF-CENTNOTRD")   ) iCTRUE_NOPF=i;
      if (TString(cl->GetName()).EqualTo("CEMC7EG1-B-NOPF-CENTNOTRD")) iCEMC7=i;
      if (TString(cl->GetName()).EqualTo("CPHI7-B-NOPF-CENTNOTRD"))    iCPHI7=i;
    }
    Int_t iREF = iCEMC7>=0 ? iCEMC7 : (iCPHI7>=0 ? iCPHI7 : iCTRUE_NOPF);
    if (iCUP13>=0 && iCTRUE_NOPF>=0 && iCTRUE_SPD1>=0 && iREF>=0) {
      class_lifetime[iCUP13] = (class_ds[iREF]>1e-7 && class_l0b[iREF]>0) ? (Double_t(class_l0a[iREF])/class_l0b[iREF]/class_ds[iREF]) : (Double_t(class_l0a[iCTRUE_NOPF])/class_l0b[iCTRUE_NOPF]/class_ds[iCTRUE_NOPF]);
      class_lifetime[iCUP13]*=class_l2a[iCTRUE_SPD1]>0 ? class_l2a[iCTRUE_SPD1] : 1;
      class_lifetime[iCUP13]*=class_l2a[iCTRUE_NOPF]>0 ? 1./class_l2a[iCTRUE_NOPF] : 0;
      class_lifetime[iCUP13]*=1-TMath::Power(1-class_ds[iCUP13],4.);
      class_lumi[iCUP13] = lumi_seen*class_lifetime[iCUP13];
    }
  }
  
  // special treatment for CUP25
  if (run>=272151) {
    Int_t iCUP25=-1;
    Int_t iCTRUE_NOPF=-1;
    Int_t iCTRUE_SPD1=-1;
    Int_t iCEMC7=-1;
    Int_t iCPHI7=-1;
    for (Int_t i=0;i<classes.GetEntriesFast();i++){
      AliTriggerClass* cl = (AliTriggerClass*) classes.At(i);
      if (TString(cl->GetName()).EqualTo("CCUP25-B-SPD1-CENTNOTRD")  ) iCUP25=i;
      if (TString(cl->GetName()).EqualTo("CTRUE-B-SPD1-CENTNOTRD")   ) iCTRUE_SPD1=i;
      if (TString(cl->GetName()).EqualTo("CTRUE-B-NOPF-CENTNOTRD")   ) iCTRUE_NOPF=i;
      if (TString(cl->GetName()).EqualTo("CEMC7EG1-B-NOPF-CENTNOTRD")) iCEMC7=i;
      if (TString(cl->GetName()).EqualTo("CPHI7-B-NOPF-CENTNOTRD"))    iCPHI7=i;
    }
    Int_t iREF = iCEMC7>=0 ? iCEMC7 : (iCPHI7>=0 ? iCPHI7 : iCTRUE_NOPF);
    if (iCUP25>=0 && iCTRUE_NOPF>=0 && iCTRUE_SPD1>=0 && iREF>=0) {
      class_lifetime[iCUP25] = (class_ds[iREF]>1e-7 && class_l0b[iREF]>0) ? (Double_t(class_l0a[iREF])/class_l0b[iREF]/class_ds[iREF]) : (Double_t(class_l0a[iCTRUE_NOPF])/class_l0b[iCTRUE_NOPF]/class_ds[iCTRUE_NOPF]);
      class_lifetime[iCUP25]*=class_l2a[iCTRUE_SPD1]>0 ? class_l2a[iCTRUE_SPD1] : 1;
      class_lifetime[iCUP25]*=class_l2a[iCTRUE_NOPF]>0 ? 1./class_l2a[iCTRUE_NOPF] : 0;
      class_lifetime[iCUP25]*=class_ds[iCUP25];
      class_lumi[iCUP25] = lumi_seen*class_lifetime[iCUP25];
    }
  }
  
  // special treatment for UPC central barrel in p-Pb
  if (run>=265589 && run<=267166) {
    for (Int_t i=0;i<classes.GetEntriesFast();i++){
      AliTriggerClass* cl = (AliTriggerClass*) classes.At(i);
      if (!cl) continue;
      TString name = cl->GetName();
      if (!(name.Contains("CCUP") && name.Contains("-CENTNOTRD"))) continue;
      Bool_t pf = name.Contains("-SPD2-");
      TObject* emc = classes.FindObject(pf ? "CEMC7EG1-B-SPD2-CENTNOTRD" : "CEMC7EG1-B-NOPF-CENTNOTRD");
      TObject* phs = classes.FindObject(pf ? "CPHI7PHL-B-SPD2-CENTNOTRD" : "CPHI7PHL-B-NOPF-CENTNOTRD");
      if (!emc && !phs) continue;
      Int_t ilt = emc!=0 ? classes.IndexOf(emc) : classes.IndexOf(phs);
      if (ilt<0) continue;
      class_lifetime[i] = class_lifetime[ilt];
      class_lumi[i] = lumi_seen*class_lifetime[i]*class_ds[i];
    }
  }

  // special treatment for T-Mask
  if (run>=276135) {
    AliTriggerClass* refTmaskClassObject = (AliTriggerClass*) classes.FindObject("C0TVX-T-NOPF-CENTNOTRD");
    if (refClass.Contains("CINT7")) refTmaskClassObject = (AliTriggerClass*) classes.FindObject("CINT7-T-NOPF-CENTNOTRD");
    Int_t refTmaskId = classes.IndexOf(refTmaskClassObject);
    TString
    refTmaskCluster = refTmaskClassObject->GetCluster()->GetName();
    refTmaskCounts = (activeDetectorsString.Contains("TRD") && (refTmaskCluster.EqualTo("CENT") || refTmaskCluster.EqualTo("ALL") || refTmaskCluster.EqualTo("FAST"))) ? class_lMb[refTmaskId] : class_l0b[refTmaskId];
    for (Int_t i=0;i<classes.GetEntriesFast();i++){
      AliTriggerClass* cl = (AliTriggerClass*) classes.At(i);
      if (TString(cl->GetName()).Contains("-T-")){
        class_lumi[i] = class_lumi[i]*TMath::Log(1-(Double_t)(refTmaskCounts)/totalBCs)/TMath::Log(1-(Double_t)(refCounts)/totalBCs);
      }
    }
  }


  
  TFile* fin = new TFile(qafilename);
  if (!fin) {
    printf("qa file not found");
    writeTree(fout,t);
    return 4;
  }
  
  AliPhysicsSelection* ps = 0;
  
  if (qafilename.Contains("event_stat")) {
    hHistStat = (TH2F*) fin->Get("fHistStat");
  } else {
    TList* statsout = (TList*) fin->Get("cstatsout");
    ps = statsout ? (AliPhysicsSelection*) statsout->FindObject("AliPhysicsSelection") : 0;
    hHistStat = ps ? (TH2F*) ps->GetStatistics("") : 0;
  }
  
  if (!hHistStat) {
     printf("fHistStat not found\n");
     writeTree(fout,t);
     return 5; 
  }
  
  fout->cd();

  meanV0MOn = 0;
  meanV0MOf = 0;
  meanOFO   = 0;
  meanTKL   = 0;
  meanErrV0MOn = 0;
  meanErrV0MOf = 0;
  meanErrOFO   = 0;
  meanErrTKL   = 0;
  meanV0MOnHM = 0;
  meanV0MOfHM = 0;
  meanOFOHM   = 0;
  meanTKLHM   = 0;
  meanErrV0MOnHM = 0;
  meanErrV0MOfHM = 0;
  meanErrOFOHM   = 0;
  meanErrTKLHM   = 0;
  thresholdV0M   = 0;
  Double_t* abV0M;
  Double_t* abSPD;

  for (Int_t j=1;j<=hHistStat->GetNbinsY();j++){
    TString label = hHistStat->GetYaxis()->GetBinLabel(j);
    // kINT7
    TList* list = NULL;
    if (qafilename.Contains("event_stat")) {
      list = (TList*) fin->Get(Form("trigger_histograms_%s/histos",label.Data()));
    } else {
      AliTriggerAnalysis* ta = ps->GetTriggerAnalysis(j-1);
      list = ta->GetHistList();
    }
    if (!list) continue;
    TH1F* hV0MOnAcc = (TH1F*) list->FindObject("fHistV0MOnAcc");
    TH1F* hV0MOfAcc = (TH1F*) list->FindObject("fHistV0MOfAcc");
    TH1F* hOFOAcc   = (TH1F*) list->FindObject("fHistOFOAcc");
    TH1F* hTKLAcc   = (TH1F*) list->FindObject("fHistTKLAcc");
    TH2F* hV0MOnVsOfCln = (TH2F*) list->FindObject("fHistV0MOnVsOfCln");
    TH2F* hSPDOnVsOfCln = (TH2F*) list->FindObject("fHistSPDOnVsOfCln");
    if (label.Contains(" &2 ")) {
      meanV0MOn    = hV0MOnAcc ? hV0MOnAcc->GetMean()      : 0;
      meanV0MOf    = hV0MOfAcc ? hV0MOfAcc->GetMean()      : 0;
      meanOFO      = hOFOAcc   ? hOFOAcc->GetMean()        : 0;
      meanTKL      = hTKLAcc   ? hTKLAcc->GetMean()        : 0;
      meanErrV0MOn = hV0MOnAcc ? hV0MOnAcc->GetMeanError() : 0;
      meanErrV0MOf = hV0MOfAcc ? hV0MOfAcc->GetMeanError() : 0;
      meanErrOFO   = hOFOAcc   ? hOFOAcc->GetMeanError()   : 0;
      meanErrTKL   = hTKLAcc   ? hTKLAcc->GetMeanError()   : 0;
      aV0MOnVsOfOADB = ((TF1*) hV0MOnVsOfCln->GetListOfFunctions()->FindObject("fFuncV0MOnVsOf"))->GetParameter(0);
      bV0MOnVsOfOADB = ((TF1*) hV0MOnVsOfCln->GetListOfFunctions()->FindObject("fFuncV0MOnVsOf"))->GetParameter(1);
      aSPDOnVsOfOADB = ((TF1*) hSPDOnVsOfCln->GetListOfFunctions()->FindObject("fFuncSPDOnVsOf"))->GetParameter(0);
      bSPDOnVsOfOADB = ((TF1*) hSPDOnVsOfCln->GetListOfFunctions()->FindObject("fFuncSPDOnVsOf"))->GetParameter(1);
      
      abV0M = getCutParametersOnlineVsOffline(hV0MOnVsOfCln, "V0M");
      aV0MOnVsOfVal = abV0M ? abV0M[0] : 0;
      aV0MOnVsOfErr = abV0M ? abV0M[1] : 0;
      bV0MOnVsOfVal = abV0M ? abV0M[2] : 0;
      bV0MOnVsOfErr = abV0M ? abV0M[3] : 0;
      abSPD = getCutParametersOnlineVsOffline(hSPDOnVsOfCln, "SPD");
      aSPDOnVsOfVal = abSPD ? abSPD[0] : 0;
      aSPDOnVsOfErr = abSPD ? abSPD[1] : 0;
      bSPDOnVsOfVal = abSPD ? abSPD[2] : 0;
      bSPDOnVsOfErr = abSPD ? abSPD[3] : 0;
    } else if (label.Contains(" &65536 ")) {
      meanV0MOnHM    = hV0MOnAcc ? hV0MOnAcc->GetMean()      : 0;
      meanV0MOfHM    = hV0MOfAcc ? hV0MOfAcc->GetMean()      : 0;
      meanOFOHM      = hOFOAcc   ? hOFOAcc->GetMean()        : 0;
      meanTKLHM      = hTKLAcc   ? hTKLAcc->GetMean()        : 0;
      meanErrV0MOnHM = hV0MOnAcc ? hV0MOnAcc->GetMeanError() : 0;
      meanErrV0MOfHM = hV0MOfAcc ? hV0MOfAcc->GetMeanError() : 0;
      meanErrOFOHM   = hOFOAcc   ? hOFOAcc->GetMeanError()   : 0;
      meanErrTKLHM   = hTKLAcc   ? hTKLAcc->GetMeanError()   : 0;
      thresholdV0M   = hV0MOnAcc->GetBinLowEdge(hV0MOnAcc->FindFirstBinAbove(9));
      printf("thresholdV0M=%f\n",thresholdV0M);
    }
  }

  for (Int_t j=1;j<=hHistStat->GetNbinsY();j++){
    TString label = hHistStat->GetYaxis()->GetBinLabel(j);
    if (label.Length()<2) continue;
    // skip background triggers
    // TODO introduce identifier to filter-out background triggers
    if      (label.Contains("-A-"))      continue;
    else if (label.Contains("-C-"))      continue;
    else if (label.Contains("-E-"))      continue;
    else if (label.Contains("-AC-"))     continue;
    else if (label.Contains("-ACE-"))    continue;
    else if (label.Contains("-GA-"))     continue;
    else if (label.Contains("-GC-"))     continue;
    else if (label.Contains("1A-ABCE-")) continue;
    else if (label.Contains("1C-ABCE-")) continue;
    else if (label.Contains("C0LSR-ABCE-")) continue;

    // Read mask
    // TODO think how to propagate mask with TBit aliases
    UInt_t mask = 0;
    TString classList = ""; // list of classes for given PS bit
    TObjArray* array = label.Tokenize(" ");
    for (Int_t itoken=0;itoken<array->GetEntries();itoken++){
      TString token = array->At(itoken)->GetName();
      if (itoken==0) classList = token; 
      if (token[0]!='&') continue;
      token.Remove(0,1);
      mask = token.Atoi();
      break;
    }
    array->Delete();
    delete array;
    printf("%s\n",label.Data());
    printf("%i\n",mask);
    if (!mask) continue;
    // Fill all and accepted counters for the most significant bit
    Int_t ibit = TMath::Nint(TMath::Log2(mask));

    // FIXME
    // if (!bitNames[ibit].EqualTo("kINT7") && !bitNames[ibit].EqualTo("kINT7inMUON") && !bitNames[ibit].EqualTo("kMuonUnlikeLowPt7")) continue;

    if (ibit>=NBITS) continue;
//    if (alias_recorded[ibit]) break; 

    alias_reconstructed[ibit] = Int_t(hHistStat->GetBinContent(1,j));
    alias_accepted[ibit]      = Int_t(hHistStat->GetBinContent(2,j));
    alias_acc_step1[ibit]     = Int_t(hHistStat->GetBinContent(3,j));
    alias_acc_step2[ibit]     = Int_t(hHistStat->GetBinContent(4,j));
    alias_acc_step3[ibit]     = Int_t(hHistStat->GetBinContent(5,j));
    alias_acc_step4[ibit]     = Int_t(hHistStat->GetBinContent(6,j));
    alias_acc_step5[ibit]     = Int_t(hHistStat->GetBinContent(7,j));
    alias_acc_step6[ibit]     = Int_t(hHistStat->GetBinContent(8,j));
    alias_acc_step7[ibit]     = Int_t(hHistStat->GetBinContent(9,j));
    alias_acc_step8[ibit]     = Int_t(hHistStat->GetBinContent(10,j));
    alias_acc_step9[ibit]     = Int_t(hHistStat->GetBinContent(11,j));
    //printf("%4i %8i %8i\n",ibit,alias_reconstructed[ibit],alias_accepted[ibit]);
    
    classList.Remove(0,1); // remove +
    array = classList.Tokenize(",");
    // if trigger bit corresponds to several active classes, just take the last one
    // example: kTRD
    // TODO think about more elegant solution
    for (Int_t i=0;i<array->GetEntriesFast();i++){
      TString token = array->At(i)->GetName();
      AliTriggerClass* cl = (AliTriggerClass*) classes.FindObject(token.Data());
      if (!cl) continue;
      Int_t iclass = classes.IndexOf(cl);
      printf(" %30s",token.Data());
      printf(" %12lli",class_l0b[iclass]);
      printf(" %12lli",class_l0a[iclass]);
      printf(" %12lli",class_l1b[iclass]);
      printf(" %12lli",class_l1a[iclass]);
      printf(" %12lli",class_l2b[iclass]);
      printf(" %12lli",class_l2a[iclass]);
      printf("\n");
      alias_recorded[ibit]      = class_l2a[iclass];
      alias_lifetime[ibit]      = class_lifetime[iclass];
      alias_lumi_recorded[ibit] = class_lumi[iclass];
      if (!alias_recorded[ibit]) continue;
      alias_lumi_reconstructed[ibit] = alias_lumi_recorded[ibit]/alias_recorded[ibit]*alias_reconstructed[ibit];
      alias_lumi_accepted[ibit]      = alias_lumi_recorded[ibit]/alias_recorded[ibit]*alias_accepted[ibit];
    }
    array->Delete();<commit_msg>manual merge of if condition on old stat histo without ZDC background monitoring<commit_after>#ifndef __CINT__
#endif
#include "triggerInfo.C"

// TODO read number of bits from AliVEvent?
#define NBITS 29
#define NMAXCLASSES 100
TString bitNames[NBITS] = {
    "kINT1",
    "kINT7",
    "kMUON",
    "kHighMultSPD",
    "kEMC1",
    "kINT5",
    "kINT7inMUON",
    "kMuonSingleHighPt7",
    "kMuonLikeLowPt7",
    "kMuonUnlikeLowPt7",
    "kEMC7",
    "kMuonSingleLowPt7",
    "kPHI1",
    "kPHI78",
    "kEMCEJE",
    "kEMCEGA",
    "kHighMultV0",
    "kSemiCentral",
    "kDG",
    "kZED",
    "kSPI78",
    "kINT8",
    "kMuonSingleLowPt8",
    "kMuonSingleHighPt8",
    "kMuonLikeLowPt8",
    "kMuonUnlikeLowPt8",
    "kMuonUnlikeLowPt0",
    "kUserDefined",
    "kTRD"
};

void writeTree(TFile* fout, TTree* t){
  fout->cd();
  t->Fill();
  t->Write();
  fout->Close();
}

//Int_t runLevelEventStatQA(TString qafilename="event_stat.root", Int_t run=254422, TString ocdbStorage = "raw://"){
//Int_t runLevelEventStatQA(TString qafilename="event_stat.root", Int_t run=255042, TString ocdbStorage = "raw://"){
Int_t runLevelEventStatQA(TString qafilename="EventStat_temp.root", Int_t run=256676, TString ocdbStorage = "local:///cvmfs/alice.cern.ch/calibration/data/2016/OCDB"){
  
  gStyle->SetOptStat(0);
  gStyle->SetLineScalePS(1.5);
  gStyle->SetPadBottomMargin(0.08);
  gStyle->SetPadRightMargin(0.02);
  gStyle->SetPadTopMargin(0.07);
  gStyle->SetPadLeftMargin(0.07);
  gStyle->SetLegendBorderSize(0);
  // tree variables
  TObjArray classes = TObjArray();
  TObjString activeDetectors = TObjString();
  TObjString objPartition = TObjString();
  TObjString objLhcState = TObjString();
  TObjString objLhcPeriod = TObjString();
  Int_t fill               = 0;
  Double_t run_duration    = 0;
  Int_t nBCsPerOrbit       = 0;
  Double_t refCounts       = 0;
  Double_t refTmaskCounts  = 0;
  Double_t mu              = 0;
  Double_t lumi_seen       = 0;
  Double_t interactionRate = 0;
  ULong64_t class_lMb[NMAXCLASSES]         = {0};
  ULong64_t class_lMa[NMAXCLASSES]         = {0};
  ULong64_t class_l0b[NMAXCLASSES]         = {0};
  ULong64_t class_l0a[NMAXCLASSES]         = {0};
  ULong64_t class_l1b[NMAXCLASSES]         = {0};
  ULong64_t class_l1a[NMAXCLASSES]         = {0};
  ULong64_t class_l2b[NMAXCLASSES]         = {0};
  ULong64_t class_l2a[NMAXCLASSES]         = {0};
  ULong64_t class_duration[NMAXCLASSES]    = {0};
  Double_t  class_lifetime[NMAXCLASSES]    = {0};
  Double_t  class_lumi[NMAXCLASSES]        = {0};
  Double_t  class_ds[NMAXCLASSES]          = {0};
  ULong64_t alias_recorded[NBITS]          = {0};
  ULong64_t alias_reconstructed[NBITS]     = {0};
  ULong64_t alias_accepted[NBITS]          = {0};
  ULong64_t alias_acc_step1[NBITS]         = {0};
  ULong64_t alias_acc_step2[NBITS]         = {0};
  ULong64_t alias_acc_step3[NBITS]         = {0};
  ULong64_t alias_acc_step4[NBITS]         = {0};
  ULong64_t alias_acc_step5[NBITS]         = {0};
  ULong64_t alias_acc_step6[NBITS]         = {0};
  ULong64_t alias_acc_step7[NBITS]         = {0};
  ULong64_t alias_acc_step8[NBITS]         = {0};
  ULong64_t alias_acc_step9[NBITS]         = {0};
  Double_t alias_l0b_rate[NBITS]           = {0};
  Double_t alias_lifetime[NBITS]           = {0};
  Double_t alias_lumi_recorded[NBITS]      = {0};
  Double_t alias_lumi_reconstructed[NBITS] = {0};
  Double_t alias_lumi_accepted[NBITS]      = {0};
  Int_t timeStart = 0;
  Int_t timeEnd = 0;
  Double_t meanV0MOn = 0;
  Double_t meanV0MOf = 0;
  Double_t meanOFO = 0;
  Double_t meanTKL = 0;
  Double_t meanErrV0MOn = 0;
  Double_t meanErrV0MOf = 0;
  Double_t meanErrOFO = 0;
  Double_t meanErrTKL = 0;
  Double_t meanV0MOnHM = 0;
  Double_t meanV0MOfHM = 0;
  Double_t meanOFOHM = 0;
  Double_t meanTKLHM = 0;
  Double_t meanErrV0MOnHM = 0;
  Double_t meanErrV0MOfHM = 0;
  Double_t meanErrOFOHM = 0;
  Double_t meanErrTKLHM = 0;
  Double_t thresholdV0M = 0;
  Double_t aV0MOnVsOfVal = 0;
  Double_t bV0MOnVsOfVal = 0;
  Double_t aV0MOnVsOfErr = 0;
  Double_t bV0MOnVsOfErr = 0;
  Double_t aSPDOnVsOfVal = 0;
  Double_t bSPDOnVsOfVal = 0;
  Double_t aSPDOnVsOfErr = 0;
  Double_t bSPDOnVsOfErr = 0;
  Double_t aV0MOnVsOfOADB = 0;
  Double_t bV0MOnVsOfOADB = 0;
  Double_t aSPDOnVsOfOADB = 0;
  Double_t bSPDOnVsOfOADB = 0;

  TH2F* hHistStat = new TH2F();

  TFile* fout = new TFile("trending.root","recreate");
  TTree* t = new TTree("trending","tree of trending variables");
  t->Branch("run",&run);
  t->Branch("fill",&fill);
  t->Branch("bcs",&nBCsPerOrbit);
  t->Branch("run_duration",&run_duration);
  t->Branch("mu",&mu);
  t->Branch("interactionRate",&interactionRate);
  t->Branch("refCounts",&refCounts);
  t->Branch("lumi_seen",&lumi_seen);
  t->Branch("classes",&classes);
  t->Branch("class_lMb",&class_lMb,Form("class_lMb[%i]/l",NMAXCLASSES));
  t->Branch("class_lMa",&class_lMa,Form("class_lMa[%i]/l",NMAXCLASSES));
  t->Branch("class_l0b",&class_l0b,Form("class_l0b[%i]/l",NMAXCLASSES));
  t->Branch("class_l0a",&class_l0a,Form("class_l0a[%i]/l",NMAXCLASSES));
  t->Branch("class_l1b",&class_l1b,Form("class_l1b[%i]/l",NMAXCLASSES));
  t->Branch("class_l1a",&class_l1a,Form("class_l1a[%i]/l",NMAXCLASSES));
  t->Branch("class_l2b",&class_l2b,Form("class_l2b[%i]/l",NMAXCLASSES));
  t->Branch("class_l2a",&class_l2a,Form("class_l2a[%i]/l",NMAXCLASSES));
  t->Branch("class_duration",&class_duration,Form("class_duration[%i]/l",NMAXCLASSES));
  t->Branch("class_lifetime",&class_lifetime,Form("class_lifetime[%i]/D",NMAXCLASSES));
  t->Branch("class_lumi",&class_lumi,Form("class_lumi[%i]/D",NMAXCLASSES));
  t->Branch("class_ds",&class_ds,Form("class_ds[%i]/D",NMAXCLASSES));
  t->Branch("alias_recorded",&alias_recorded,Form("alias_recorded[%i]/l",NBITS));
  t->Branch("alias_reconstructed",&alias_reconstructed,Form("alias_reconstructed[%i]/l",NBITS));
  t->Branch("alias_accepted",&alias_accepted,Form("alias_accepted[%i]/l",NBITS));
  t->Branch("alias_acc_step1",&alias_acc_step1,Form("alias_acc_step1[%i]/l",NBITS));
  t->Branch("alias_acc_step2",&alias_acc_step2,Form("alias_acc_step2[%i]/l",NBITS));
  t->Branch("alias_acc_step3",&alias_acc_step3,Form("alias_acc_step3[%i]/l",NBITS));
  t->Branch("alias_acc_step4",&alias_acc_step4,Form("alias_acc_step4[%i]/l",NBITS));
  t->Branch("alias_acc_step5",&alias_acc_step5,Form("alias_acc_step5[%i]/l",NBITS));
  t->Branch("alias_acc_step6",&alias_acc_step6,Form("alias_acc_step6[%i]/l",NBITS));
  t->Branch("alias_acc_step7",&alias_acc_step7,Form("alias_acc_step7[%i]/l",NBITS));
  t->Branch("alias_acc_step8",&alias_acc_step6,Form("alias_acc_step8[%i]/l",NBITS));
  t->Branch("alias_acc_step9",&alias_acc_step7,Form("alias_acc_step9[%i]/l",NBITS));
  t->Branch("alias_l0b_rate",&alias_lifetime,Form("alias_l0b_rate[%i]/D",NBITS));
  t->Branch("alias_lifetime",&alias_lifetime,Form("alias_lifetime[%i]/D",NBITS));
  t->Branch("alias_lumi_recorded",&alias_lumi_recorded,Form("alias_lumi_recorded[%i]/D",NBITS));
  t->Branch("alias_lumi_reconstructed",&alias_lumi_reconstructed,Form("alias_lumi_reconstructed[%i]/D",NBITS));
  t->Branch("alias_lumi_accepted",&alias_lumi_accepted,Form("alias_lumi_accepted[%i]/D",NBITS));
  t->Branch("activeDetectors",&activeDetectors);
  t->Branch("lhcState",&objLhcState);
  t->Branch("lhcPeriod",&objLhcPeriod);
  t->Branch("partition",&objPartition);
  t->Branch("timeStart",&timeStart);
  t->Branch("timeEnd",&timeEnd);
  t->Branch("meanV0MOn",&meanV0MOn);
  t->Branch("meanV0MOf",&meanV0MOf);
  t->Branch("meanOFO",&meanOFO);
  t->Branch("meanTKL",&meanTKL);
  t->Branch("meanErrV0MOn",&meanErrV0MOn);
  t->Branch("meanErrV0MOf",&meanErrV0MOf);
  t->Branch("meanErrOFO",&meanErrOFO);
  t->Branch("meanErrTKL",&meanErrTKL);
  t->Branch("meanV0MOnHM",&meanV0MOnHM);
  t->Branch("meanV0MOfHM",&meanV0MOfHM);
  t->Branch("meanOFOHM",&meanOFOHM);
  t->Branch("meanTKLHM",&meanTKLHM);
  t->Branch("meanErrV0MOnHM",&meanErrV0MOnHM);
  t->Branch("meanErrV0MOfHM",&meanErrV0MOfHM);
  t->Branch("meanErrOFOHM",&meanErrOFOHM);
  t->Branch("meanErrTKLHM",&meanErrTKLHM);
  t->Branch("thresholdV0M",&thresholdV0M);
  t->Branch("aV0MOnVsOfVal",&aV0MOnVsOfVal);
  t->Branch("bV0MOnVsOfVal",&bV0MOnVsOfVal);
  t->Branch("aV0MOnVsOfErr",&aV0MOnVsOfErr);
  t->Branch("bV0MOnVsOfErr",&bV0MOnVsOfErr);
  t->Branch("aSPDOnVsOfVal",&aSPDOnVsOfVal);
  t->Branch("bSPDOnVsOfVal",&bSPDOnVsOfVal);
  t->Branch("aSPDOnVsOfErr",&aSPDOnVsOfErr);
  t->Branch("bSPDOnVsOfErr",&bSPDOnVsOfErr);
  t->Branch("aV0MOnVsOfOADB",&aV0MOnVsOfOADB);
  t->Branch("bV0MOnVsOfOADB",&bV0MOnVsOfOADB);
  t->Branch("aSPDOnVsOfOADB",&aSPDOnVsOfOADB);
  t->Branch("bSPDOnVsOfOADB",&bSPDOnVsOfOADB);
  
  t->Branch("hHistStat",&hHistStat);
  
  TString refClass="";
  Double_t refSigma=-1;
  Double_t refEff = 1.;
  Double_t refMu = 1.e-11;
  if      (               run<=118501) { refSigma=  62.; refEff = 1.00; refClass = "CINT1B-ABCE-NOPF-ALL";   } // pp_7.00: 62mb=54.3mb*1.15=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=118502 && run<=118561) { refSigma=  47.; refEff = 1.00; refClass = "CINT1B-ABCE-NOPF-ALL";   } // pp_0.90: 47mb=52 mb *0.91=sigma(INEL)*R(INT1/INEL) (arxiv: 1208.4968, fig.10 + table 3)
  else if (run>=118903 && run<=120829) { refSigma=  62.; refEff = 1.00; refClass = "CINT1B-ABCE-NOPF-ALL";   } // pp_7.00: 62mb=54.3mb*1.15=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=121039 && run<=121040) { refSigma=  47.; refEff = 1.00; refClass = "CINT1B-ABCE-NOPF-ALL";   } // pp_0.90: 47mb=52 mb *0.91=sigma(INEL)*R(INT1/INEL) (arxiv: 1208.4968, fig.10 + table 3)
  else if (run>=121041 && run<=126437) { refSigma=  62.; refEff = 1.00; refClass = "CINT1B-ABCE-NOPF-ALL";   } // pp_7.00: 62mb=54.3mb*1.15=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=126438 && run<=127718) { refSigma=  62.; refEff = 1.00; refClass = "CINT1-B-NOPF-ALLNOTRD";  } // pp_7.00: 62mb=54.3mb*1.15=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=127719 && run<=127730) { refSigma=  62.; refEff = 1.00; refClass = "CINT1B-ABCE-NOPF-ALL";   } // pp_7.00: 62mb=54.3mb*1.15=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=127731 && run<=136848) { refSigma=  62.; refEff = 1.00; refClass = "CINT1-B-NOPF-ALLNOTRD";  } // pp_7.00: 62mb=54.3mb*1.15=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=136849 && run<=139316) { refSigma=5970.; refEff = 0.78; refClass = "C0SMH-B-NOPF-ALL";       } // PbPb_2.76: (Oyama,2011-05-20,RunCond), sigma_hardronic = 7.64 b
  else if (run>=139328 && run<=139517) { refSigma=5970.; refEff = 0.78; refClass = "C0SMH-B-NOPF-ALLNOTRD";  } // PbPb_2.76: (Oyama,2011-05-20,RunCond), sigma_hardronic = 7.64 b
  else if (run>=145289 && run<=146860) { refSigma=  57.; refEff = 1.00; refClass = "CINT1-B-NOPF-ALLNOTRD";  } // pp_2.76: 57mb=47.7mb*1.20=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=146808 && run<=146814) { refSigma=  57.; refEff = 1.00; refClass = "CINT1-B-NOPF-ALL";       } // pp_2.76: 57mb=47.7mb*1.20=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=145815 && run<=146856) { refSigma=  57.; refEff = 1.00; refClass = "CINT1-B-NOPF-ALLNOTRD";  } // pp_2.76: 57mb=47.7mb*1.20=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=146857 && run<=146857) { refSigma=  57.; refEff = 1.00; refClass = "CINT1-B-NOPF-ALL";       } // pp_2.76: 57mb=47.7mb*1.20=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=146858 && run<=146860) { refSigma=  57.; refEff = 1.00; refClass = "CINT1-B-NOPF-ALLNOTRD";  } // pp_2.76: 57mb=47.7mb*1.20=sigma(VBAND)*R(INT1/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=148370 && run<=157078) { refSigma=  54.; refEff = 1.00; refClass = "CVBAND-B-NOPF-ALLNOTRD"; } // pp_7.00: 54.3mb (Martino,2012-03-12,RunCond)
  else if (run>=157079 && run<=165746) { refSigma=  24.; refEff = 0.44; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_7.00: 24mb=54.3mb*0.44=sigma(VBAND)*R(0TVX/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=166477 && run<=170593) { refSigma=4100.; refEff = 0.54; refClass = "CVLN-B-NOPF-ALLNOTRD";   } // PbPb_2.76: (Martino,2013-03-15,RunCond)
  else if (run>=176658 && run<=177143) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177146 && run<=177147) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177148 && run<=177149) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177150 && run<=177165) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177166 && run<=177166) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177167 && run<=177167) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177168 && run<=177168) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177169 && run<=177172) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177173 && run<=177173) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177174 && run<=177506) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=177507 && run<=178017) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=178018 && run<=178029) { refSigma=  67.; refEff = 0.90; refClass = "CINT1-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), CINT1/C0TVX=2.7 from 178052
  else if (run>=178030 && run<=178053) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=178055 && run<=178062) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALL";       } // pp_8.00: (Artem, 2013-10-04,RunCond), vdM
  else if (run>=178062 && run<=178220) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=179444 && run<=180715) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-S-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=180716 && run<=180720) { refSigma=  56.; refEff = 0.75; refClass = "CINT7-S-NOPF-ALLNOTRD";  } // no C0TVX in these runs, taking VBAND cross section
  else if (run>=180721 && run<=184844) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-S-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=184845 && run<=184990) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=184991 && run<=188229) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-S-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=188230 && run<=188366) { refSigma=1590.; refEff = 0.76; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pPb_5.02: pilot. arxiv:1405.1849
  else if (run>=188367 && run<=193692) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-S-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=193693 && run<=193766) { refSigma=  25.; refEff = 0.33; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_8.00: (Artem, 2013-10-04,RunCond), TOTEM INEL = 74.7+/-1.7 mb
  else if (run>=195344 && run<=197388) { refSigma=1590.; refEff = 0.76; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pPb_5.02: arxiv:1405.1849
  else if (run>=197470 && run<=197692) { refSigma=  18.; refEff = 0.39; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // pp_2.76: 18mb=47.7mb*0.39=sigma(VBAND)*R(0TVX/VBAND) (Martino,2012-03-12,RunCond)
  else if (run>=221835 && run<=223669) { refSigma= 16.8; refEff = 0.32; refClass = "CADAND-B-NOPF-ALLNOTRD"; } // estimates from Martino
  else if (run>=221670 && run<=223983) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // estimates from Martino and MC
  else if (run>=223984 && run<=223984) { refSigma= 50.0; refEff = 0.66; refClass = "CADAND-B-NOPF-ALLNOTRD"; } // estimates from Martino and MC
  else if (run>=223985 && run<=226110) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // estimates from Martino and MC
  else if (run>=226111 && run<=226115) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=226116 && run<=228909) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // estimates from Martino and MC
  else if (run>=228910 && run<=229376) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=229386 && run<=229398) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-MUON";      } // estimates from Martino and MC
  else if (run>=229409 && run<=229410) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=229416 && run<=229893) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-MUON";      } // estimates from Martino and MC
  else if (run>=229894 && run<=229899) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // estimates from Martino and MC
  else if (run>=229942 && run<=231321) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-MUON";      } // estimates from Martino and MC
  else if (run>=232914 && run<=233858) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENT";      } // estimates from Martino and MC
  else if (run>=233910 && run<=234050) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-ALLNOTRD";  } // estimates from Martino and MC
  else if (run>=234051 && run<=238669) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENT";      } // estimates from Martino and MC
  else if (run>=238670 && run<=240150) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=240151 && run<=240151) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-MUON";      } // estimates from Martino and MC
  else if (run>=240152 && run<=243373) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=243374 && run<=243398) { refSigma= 21.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=243399 && run<=243984) { refSigma=6700.; refEff = 0.90; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=243985 && run<=244912) { refSigma= 21.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=244913 && run<=246994) { refSigma=4600.; refEff = 0.60; refClass = "C0V0M-B-NOPF-CENTNOTRD"; } // estimates from Cvetan and Alberica
  else if (run>=246995 && run<=256147) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=256148 && run<=256157) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENT";      } // estimates from Martino and MC
  else if (run>=256158 && run<=265301) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // estimates from Martino and MC
  else if (run>=265304 && run<=265586) { refSigma=1590.; refEff = 0.76; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // taken from old p-Pb
  else if (run>=265587 && run<=267131) { refSigma=1715.; refEff = 0.82; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // T0 efficiency estimate from MC, refSigma assuming cs(INEL) = 2092
  else if (run>=267132 && run<=267166) { refSigma=1590.; refEff = 0.76; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // taken from old p-Pb
  else if (run>=267167 && run<=275923) { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // back to pp at 13TeV
  else if (run>=275924 && run<=276096) { refSigma= 64.0; refEff = 0.85; refClass = "CINT7-B-NOPF-CENTNOTRD"; } // T0 is not operational, using V0AND
  else if (run>=276097 )               { refSigma= 30.0; refEff = 0.40; refClass = "C0TVX-B-NOPF-CENTNOTRD"; } // back to pp at 13TeV

  
  Double_t orbitRate = 11245.;
  TString partition;
  TString lhcState;
  TString lhcPeriod;
  TString activeDetectorsString;
  //Int_t run, TString ocdbStorage, TString &partition, TString &activeDetectorsString, Double_t& run_duration, 
  classes = GetClasses(run,ocdbStorage,partition,activeDetectorsString,run_duration,class_lMb,class_lMa,class_l0b,class_l0a,class_l1b,class_l1a,class_l2b,class_l2a,class_duration);
  activeDetectors.SetString(activeDetectorsString.Data());
  objPartition.SetString(partition.Data());
  Int_t status = triggerInfo(run,ocdbStorage,lhcPeriod,lhcState,fill,nBCsPerOrbit,timeStart,timeEnd);
  if (status>0) { writeTree(fout,t); return 1; }
  objLhcState.SetString(lhcState.Data());
  objLhcPeriod.SetString(lhcPeriod.Data());

  AliTriggerClass* refClassObject = (AliTriggerClass*) classes.FindObject(refClass);
  if (!refClassObject) { writeTree(fout,t); return 2; }
  Int_t refId = classes.IndexOf(refClassObject);
  TString refCluster = refClassObject->GetCluster()->GetName();
  refCounts = (activeDetectorsString.Contains("TRD") && (refCluster.EqualTo("CENT") || refCluster.EqualTo("ALL") || refCluster.EqualTo("FAST"))) ? class_lMb[refId] : class_l0b[refId];
  if (refClass.Contains("1B-ABCE-")){
    Int_t emptyClassId = classes.IndexOf(classes.FindObject("CBEAMB-ABCE-NOPF-ALL"));
    Int_t emptyL0B = class_l0b[emptyClassId];
    if (nBCsPerOrbit<0 && emptyL0B>0) nBCsPerOrbit = Double_t(emptyL0B)/orbitRate/run_duration;
  } else {
    nBCsPerOrbit= refClassObject->GetBCMask()->GetNUnmaskedBCs();
  }
  
  Double_t totalBCs = orbitRate*run_duration*nBCsPerOrbit;
  if (totalBCs<1 || refCounts<1) { writeTree(fout,t); return 3; }
  refMu = -TMath::Log(1-Double_t(refCounts)/totalBCs); // mu
  Double_t refInteractionRate = (run_duration>1e-10) ? (refMu>1.e-10 ? refMu/(1-TMath::Exp(-refMu)):1)*refCounts/run_duration : 0;
  interactionRate = refInteractionRate/refEff; // INEL interaction rate 
  mu              = refMu/refEff;              // INEL mu value
  if (refSigma>1.e-10) lumi_seen = run_duration*refInteractionRate/refSigma/1000; //[ub-1]

  for (Int_t i=0;i<classes.GetEntriesFast();i++){
    // printf("%30s %12lli %10lli %10lli %10lli %10lli %10lli\n",classes.At(i)->GetName(),class_l0b[i],class_l0a[i],class_l1b[i],class_l1a[i],class_l2b[i],class_l2a[i]);
    class_lifetime[i] = class_lMb[i]>0 ? Double_t(class_lMa[i])/class_lMb[i]: 0;
    class_lifetime[i]*= class_l0b[i]>0 ? Double_t(class_l0a[i])/class_l0b[i]: 0;
    class_lifetime[i]*= class_l1b[i]>0 ? Double_t(class_l1a[i])/class_l1b[i]: 0;
    class_lifetime[i]*= class_l2b[i]>0 ? Double_t(class_l2a[i])/class_l2b[i]: 0;
    class_lumi[i] = lumi_seen*class_lifetime[i];
    AliTriggerClass* cl = (AliTriggerClass*) classes.At(i);
    cl->GetDownscaleFactor(class_ds[i]);
  }

  // special treatment for Pb-Pb
  if (run>=244917 && run<=246994) {
    for (Int_t i=0;i<classes.GetEntriesFast();i++){
      AliTriggerClass* cl = (AliTriggerClass*) classes.At(i);
      TObjArray* tokens = TString(cl->GetName()).Tokenize("-");
      TString cluster = tokens->At(3)->GetName();
      TString lifetimeClassName = Form("C0VHM-B-NOPF-%s",cluster.Data());
      if (run<245256) lifetimeClassName = Form("C0V0M-B-NOPF-%s",cluster.Data());
      TObject* lifetimeClass = classes.FindObject(lifetimeClassName.Data());
      if (lifetimeClass) {
        Int_t index = classes.IndexOf(lifetimeClass);
        if (class_ds[index]>0) {
          printf("%s %f %f\n",lifetimeClassName.Data(),class_ds[i],class_ds[index]);
          Float_t ds_ratio = class_ds[i]/class_ds[index];
          class_lifetime[i]=class_lifetime[index]*ds_ratio;
          class_lumi[i]=class_lumi[index]*ds_ratio;
        }
      }
      tokens->Delete();
      delete tokens;
    }
  }

  // special treatment for INT11
  if (run>=252235 && run<=255011 && run!=253434 && run!=253437) {
    AliTriggerClass* clINT11 = (AliTriggerClass*) classes.FindObject("CINT11-B-NOPF-CENTNOTRD");
    AliTriggerClass* cl0TVX  = (AliTriggerClass*) classes.FindObject("C0TVX-B-NOPF-CENTNOTRD");
    if (clINT11 && cl0TVX) {
      Int_t iCINT11 = classes.IndexOf(clINT11);
      Int_t iC0TVX  = classes.IndexOf(cl0TVX);
      class_lifetime[iCINT11] = class_lifetime[iC0TVX];
      class_lumi[iCINT11]     = class_lumi[iC0TVX];
    }
  }
  
  // special treatment for CUP2
  if (run>=255276 && run<=256418) {
    Int_t iCUP2=-1;
    Int_t iCTRUE_NOPF=-1;
    Int_t iCTRUE_SPD1=-1;
    Int_t iCVHMV0M=-1;
    for (Int_t i=0;i<classes.GetEntriesFast();i++){
      AliTriggerClass* cl = (AliTriggerClass*) classes.At(i);
      if (TString(cl->GetName()).EqualTo("CCUP2-B-SPD1-CENTNOTRD")   ) iCUP2=i;
      if (TString(cl->GetName()).EqualTo("CTRUE-B-SPD1-CENTNOTRD")   ) iCTRUE_SPD1=i;
      if (TString(cl->GetName()).EqualTo("CTRUE-B-NOPF-CENTNOTRD")   ) iCTRUE_NOPF=i;
      if (TString(cl->GetName()).EqualTo("CVHMV0M-B-NOPF-CENTNOTRD") ) iCVHMV0M=i;
    }
    Int_t iREF = iCVHMV0M>=0 ? iCVHMV0M : iCTRUE_NOPF;
    if (iCUP2>=0 && iCTRUE_NOPF>=0 && iCTRUE_SPD1>=0 && iREF>=0) {
      class_lifetime[iCUP2] = (class_ds[iREF]>1e-7 && class_l0b[iREF]>0) ? (Double_t(class_l0a[iREF])/class_l0b[iREF]/class_ds[iREF]) : (Double_t(class_l0a[iCTRUE_NOPF])/class_l0b[iCTRUE_NOPF]/class_ds[iCTRUE_NOPF]);
      class_lifetime[iCUP2]*=class_l2a[iCTRUE_SPD1];
      class_lifetime[iCUP2]*=class_l2a[iCTRUE_NOPF]>0 ? 1./class_l2a[iCTRUE_NOPF] : 0;
      class_lifetime[iCUP2]*=class_ds[iCUP2];
      class_lumi[iCUP2] = lumi_seen*class_lifetime[iCUP2];
    }
    if (iCUP2>=0) printf("lumi=%f\n",class_lumi[iCUP2]);
  }
  
  // special treatment for CUP13
  if (run>=256504) {
    Int_t iCUP13=-1;
    Int_t iCTRUE_NOPF=-1;
    Int_t iCTRUE_SPD1=-1;
    Int_t iCEMC7=-1;
    Int_t iCPHI7=-1;
    for (Int_t i=0;i<classes.GetEntriesFast();i++){
      AliTriggerClass* cl = (AliTriggerClass*) classes.At(i);
      if (TString(cl->GetName()).EqualTo("CCUP13-B-SPD1-CENTNOTRD")  ) iCUP13=i;
      if (TString(cl->GetName()).EqualTo("CTRUE-B-SPD1-CENTNOTRD")   ) iCTRUE_SPD1=i;
      if (TString(cl->GetName()).EqualTo("CTRUE-B-NOPF-CENTNOTRD")   ) iCTRUE_NOPF=i;
      if (TString(cl->GetName()).EqualTo("CEMC7EG1-B-NOPF-CENTNOTRD")) iCEMC7=i;
      if (TString(cl->GetName()).EqualTo("CPHI7-B-NOPF-CENTNOTRD"))    iCPHI7=i;
    }
    Int_t iREF = iCEMC7>=0 ? iCEMC7 : (iCPHI7>=0 ? iCPHI7 : iCTRUE_NOPF);
    if (iCUP13>=0 && iCTRUE_NOPF>=0 && iCTRUE_SPD1>=0 && iREF>=0) {
      class_lifetime[iCUP13] = (class_ds[iREF]>1e-7 && class_l0b[iREF]>0) ? (Double_t(class_l0a[iREF])/class_l0b[iREF]/class_ds[iREF]) : (Double_t(class_l0a[iCTRUE_NOPF])/class_l0b[iCTRUE_NOPF]/class_ds[iCTRUE_NOPF]);
      class_lifetime[iCUP13]*=class_l2a[iCTRUE_SPD1]>0 ? class_l2a[iCTRUE_SPD1] : 1;
      class_lifetime[iCUP13]*=class_l2a[iCTRUE_NOPF]>0 ? 1./class_l2a[iCTRUE_NOPF] : 0;
      class_lifetime[iCUP13]*=1-TMath::Power(1-class_ds[iCUP13],4.);
      class_lumi[iCUP13] = lumi_seen*class_lifetime[iCUP13];
    }
  }
  
  // special treatment for CUP25
  if (run>=272151) {
    Int_t iCUP25=-1;
    Int_t iCTRUE_NOPF=-1;
    Int_t iCTRUE_SPD1=-1;
    Int_t iCEMC7=-1;
    Int_t iCPHI7=-1;
    for (Int_t i=0;i<classes.GetEntriesFast();i++){
      AliTriggerClass* cl = (AliTriggerClass*) classes.At(i);
      if (TString(cl->GetName()).EqualTo("CCUP25-B-SPD1-CENTNOTRD")  ) iCUP25=i;
      if (TString(cl->GetName()).EqualTo("CTRUE-B-SPD1-CENTNOTRD")   ) iCTRUE_SPD1=i;
      if (TString(cl->GetName()).EqualTo("CTRUE-B-NOPF-CENTNOTRD")   ) iCTRUE_NOPF=i;
      if (TString(cl->GetName()).EqualTo("CEMC7EG1-B-NOPF-CENTNOTRD")) iCEMC7=i;
      if (TString(cl->GetName()).EqualTo("CPHI7-B-NOPF-CENTNOTRD"))    iCPHI7=i;
    }
    Int_t iREF = iCEMC7>=0 ? iCEMC7 : (iCPHI7>=0 ? iCPHI7 : iCTRUE_NOPF);
    if (iCUP25>=0 && iCTRUE_NOPF>=0 && iCTRUE_SPD1>=0 && iREF>=0) {
      class_lifetime[iCUP25] = (class_ds[iREF]>1e-7 && class_l0b[iREF]>0) ? (Double_t(class_l0a[iREF])/class_l0b[iREF]/class_ds[iREF]) : (Double_t(class_l0a[iCTRUE_NOPF])/class_l0b[iCTRUE_NOPF]/class_ds[iCTRUE_NOPF]);
      class_lifetime[iCUP25]*=class_l2a[iCTRUE_SPD1]>0 ? class_l2a[iCTRUE_SPD1] : 1;
      class_lifetime[iCUP25]*=class_l2a[iCTRUE_NOPF]>0 ? 1./class_l2a[iCTRUE_NOPF] : 0;
      class_lifetime[iCUP25]*=class_ds[iCUP25];
      class_lumi[iCUP25] = lumi_seen*class_lifetime[iCUP25];
    }
  }
  
  // special treatment for UPC central barrel in p-Pb
  if (run>=265589 && run<=267166) {
    for (Int_t i=0;i<classes.GetEntriesFast();i++){
      AliTriggerClass* cl = (AliTriggerClass*) classes.At(i);
      if (!cl) continue;
      TString name = cl->GetName();
      if (!(name.Contains("CCUP") && name.Contains("-CENTNOTRD"))) continue;
      Bool_t pf = name.Contains("-SPD2-");
      TObject* emc = classes.FindObject(pf ? "CEMC7EG1-B-SPD2-CENTNOTRD" : "CEMC7EG1-B-NOPF-CENTNOTRD");
      TObject* phs = classes.FindObject(pf ? "CPHI7PHL-B-SPD2-CENTNOTRD" : "CPHI7PHL-B-NOPF-CENTNOTRD");
      if (!emc && !phs) continue;
      Int_t ilt = emc!=0 ? classes.IndexOf(emc) : classes.IndexOf(phs);
      if (ilt<0) continue;
      class_lifetime[i] = class_lifetime[ilt];
      class_lumi[i] = lumi_seen*class_lifetime[i]*class_ds[i];
    }
  }

  // special treatment for T-Mask
  if (run>=276135) {
    AliTriggerClass* refTmaskClassObject = (AliTriggerClass*) classes.FindObject("C0TVX-T-NOPF-CENTNOTRD");
    if (refClass.Contains("CINT7")) refTmaskClassObject = (AliTriggerClass*) classes.FindObject("CINT7-T-NOPF-CENTNOTRD");
    Int_t refTmaskId = classes.IndexOf(refTmaskClassObject);
    TString refTmaskCluster = refTmaskClassObject->GetCluster()->GetName();
    refTmaskCounts = (activeDetectorsString.Contains("TRD") && (refTmaskCluster.EqualTo("CENT") || refTmaskCluster.EqualTo("ALL") || refTmaskCluster.EqualTo("FAST"))) ? class_lMb[refTmaskId] : class_l0b[refTmaskId];
    for (Int_t i=0;i<classes.GetEntriesFast();i++){
      AliTriggerClass* cl = (AliTriggerClass*) classes.At(i);
      if (TString(cl->GetName()).Contains("-T-")){
        class_lumi[i] = class_lumi[i]*TMath::Log(1-(Double_t)(refTmaskCounts)/totalBCs)/TMath::Log(1-(Double_t)(refCounts)/totalBCs);
      }
    }
  }


  
  TFile* fin = new TFile(qafilename);
  if (!fin) {
    printf("qa file not found");
    writeTree(fout,t);
    return 4;
  }
  
  AliPhysicsSelection* ps = 0;
  
  if (qafilename.Contains("event_stat")) {
    hHistStat = (TH2F*) fin->Get("fHistStat");
  } else {
    TList* statsout = (TList*) fin->Get("cstatsout");
    ps = statsout ? (AliPhysicsSelection*) statsout->FindObject("AliPhysicsSelection") : 0;
    hHistStat = ps ? (TH2F*) ps->GetStatistics("") : 0;
  }
  
  if (!hHistStat) {
     printf("fHistStat not found\n");
     writeTree(fout,t);
     return 5; 
  }
  
  fout->cd();

  meanV0MOn = 0;
  meanV0MOf = 0;
  meanOFO   = 0;
  meanTKL   = 0;
  meanErrV0MOn = 0;
  meanErrV0MOf = 0;
  meanErrOFO   = 0;
  meanErrTKL   = 0;
  meanV0MOnHM = 0;
  meanV0MOfHM = 0;
  meanOFOHM   = 0;
  meanTKLHM   = 0;
  meanErrV0MOnHM = 0;
  meanErrV0MOfHM = 0;
  meanErrOFOHM   = 0;
  meanErrTKLHM   = 0;
  thresholdV0M   = 0;
  Double_t* abV0M;
  Double_t* abSPD;

  for (Int_t j=1;j<=hHistStat->GetNbinsY();j++){
    TString label = hHistStat->GetYaxis()->GetBinLabel(j);
    // kINT7
    TList* list = NULL;
    if (qafilename.Contains("event_stat")) {
      list = (TList*) fin->Get(Form("trigger_histograms_%s/histos",label.Data()));
    } else {
      AliTriggerAnalysis* ta = ps->GetTriggerAnalysis(j-1);
      list = ta->GetHistList();
    }
    if (!list) continue;
    TH1F* hV0MOnAcc = (TH1F*) list->FindObject("fHistV0MOnAcc");
    TH1F* hV0MOfAcc = (TH1F*) list->FindObject("fHistV0MOfAcc");
    TH1F* hOFOAcc   = (TH1F*) list->FindObject("fHistOFOAcc");
    TH1F* hTKLAcc   = (TH1F*) list->FindObject("fHistTKLAcc");
    TH2F* hV0MOnVsOfCln = (TH2F*) list->FindObject("fHistV0MOnVsOfCln");
    TH2F* hSPDOnVsOfCln = (TH2F*) list->FindObject("fHistSPDOnVsOfCln");
    if (label.Contains(" &2 ")) {
      meanV0MOn    = hV0MOnAcc ? hV0MOnAcc->GetMean()      : 0;
      meanV0MOf    = hV0MOfAcc ? hV0MOfAcc->GetMean()      : 0;
      meanOFO      = hOFOAcc   ? hOFOAcc->GetMean()        : 0;
      meanTKL      = hTKLAcc   ? hTKLAcc->GetMean()        : 0;
      meanErrV0MOn = hV0MOnAcc ? hV0MOnAcc->GetMeanError() : 0;
      meanErrV0MOf = hV0MOfAcc ? hV0MOfAcc->GetMeanError() : 0;
      meanErrOFO   = hOFOAcc   ? hOFOAcc->GetMeanError()   : 0;
      meanErrTKL   = hTKLAcc   ? hTKLAcc->GetMeanError()   : 0;
      aV0MOnVsOfOADB = ((TF1*) hV0MOnVsOfCln->GetListOfFunctions()->FindObject("fFuncV0MOnVsOf"))->GetParameter(0);
      bV0MOnVsOfOADB = ((TF1*) hV0MOnVsOfCln->GetListOfFunctions()->FindObject("fFuncV0MOnVsOf"))->GetParameter(1);
      aSPDOnVsOfOADB = ((TF1*) hSPDOnVsOfCln->GetListOfFunctions()->FindObject("fFuncSPDOnVsOf"))->GetParameter(0);
      bSPDOnVsOfOADB = ((TF1*) hSPDOnVsOfCln->GetListOfFunctions()->FindObject("fFuncSPDOnVsOf"))->GetParameter(1);
      
      abV0M = getCutParametersOnlineVsOffline(hV0MOnVsOfCln, "V0M");
      aV0MOnVsOfVal = abV0M ? abV0M[0] : 0;
      aV0MOnVsOfErr = abV0M ? abV0M[1] : 0;
      bV0MOnVsOfVal = abV0M ? abV0M[2] : 0;
      bV0MOnVsOfErr = abV0M ? abV0M[3] : 0;
      abSPD = getCutParametersOnlineVsOffline(hSPDOnVsOfCln, "SPD");
      aSPDOnVsOfVal = abSPD ? abSPD[0] : 0;
      aSPDOnVsOfErr = abSPD ? abSPD[1] : 0;
      bSPDOnVsOfVal = abSPD ? abSPD[2] : 0;
      bSPDOnVsOfErr = abSPD ? abSPD[3] : 0;
    } else if (label.Contains(" &65536 ")) {
      meanV0MOnHM    = hV0MOnAcc ? hV0MOnAcc->GetMean()      : 0;
      meanV0MOfHM    = hV0MOfAcc ? hV0MOfAcc->GetMean()      : 0;
      meanOFOHM      = hOFOAcc   ? hOFOAcc->GetMean()        : 0;
      meanTKLHM      = hTKLAcc   ? hTKLAcc->GetMean()        : 0;
      meanErrV0MOnHM = hV0MOnAcc ? hV0MOnAcc->GetMeanError() : 0;
      meanErrV0MOfHM = hV0MOfAcc ? hV0MOfAcc->GetMeanError() : 0;
      meanErrOFOHM   = hOFOAcc   ? hOFOAcc->GetMeanError()   : 0;
      meanErrTKLHM   = hTKLAcc   ? hTKLAcc->GetMeanError()   : 0;
      thresholdV0M   = hV0MOnAcc->GetBinLowEdge(hV0MOnAcc->FindFirstBinAbove(9));
      printf("thresholdV0M=%f\n",thresholdV0M);
    }
  }

  for (Int_t j=1;j<=hHistStat->GetNbinsY();j++){
    TString label = hHistStat->GetYaxis()->GetBinLabel(j);
    if (label.Length()<2) continue;
    // skip background triggers
    // TODO introduce identifier to filter-out background triggers
    if      (label.Contains("-A-"))      continue;
    else if (label.Contains("-C-"))      continue;
    else if (label.Contains("-E-"))      continue;
    else if (label.Contains("-AC-"))     continue;
    else if (label.Contains("-ACE-"))    continue;
    else if (label.Contains("-GA-"))     continue;
    else if (label.Contains("-GC-"))     continue;
    else if (label.Contains("1A-ABCE-")) continue;
    else if (label.Contains("1C-ABCE-")) continue;
    else if (label.Contains("C0LSR-ABCE-")) continue;

    // Read mask
    // TODO think how to propagate mask with TBit aliases
    UInt_t mask = 0;
    TString classList = ""; // list of classes for given PS bit
    TObjArray* array = label.Tokenize(" ");
    for (Int_t itoken=0;itoken<array->GetEntries();itoken++){
      TString token = array->At(itoken)->GetName();
      if (itoken==0) classList = token; 
      if (token[0]!='&') continue;
      token.Remove(0,1);
      mask = token.Atoi();
      break;
    }
    array->Delete();
    delete array;
    printf("%s\n",label.Data());
    printf("%i\n",mask);
    if (!mask) continue;
    // Fill all and accepted counters for the most significant bit
    Int_t ibit = TMath::Nint(TMath::Log2(mask));

    // FIXME
    // if (!bitNames[ibit].EqualTo("kINT7") && !bitNames[ibit].EqualTo("kINT7inMUON") && !bitNames[ibit].EqualTo("kMuonUnlikeLowPt7")) continue;

    if (ibit>=NBITS) continue;
//    if (alias_recorded[ibit]) break; 

    alias_reconstructed[ibit] = Int_t(hHistStat->GetBinContent(1,j));
    alias_accepted[ibit]      = Int_t(hHistStat->GetBinContent(2,j));
    if (hHistStat->GetNbinsX()!=19) {
      // old stat histo without ZDC background monitoring
      // setting ZDCBG step equal to V0AND and take other steps shifted by 1
      alias_acc_step1[ibit]     = Int_t(hHistStat->GetBinContent(3,j));
      alias_acc_step2[ibit]     = Int_t(hHistStat->GetBinContent(3,j));
      alias_acc_step3[ibit]     = Int_t(hHistStat->GetBinContent(4,j));
      alias_acc_step4[ibit]     = Int_t(hHistStat->GetBinContent(5,j));
      alias_acc_step5[ibit]     = Int_t(hHistStat->GetBinContent(6,j));
      alias_acc_step6[ibit]     = Int_t(hHistStat->GetBinContent(7,j));
      alias_acc_step7[ibit]     = Int_t(hHistStat->GetBinContent(8,j));
      alias_acc_step8[ibit]     = Int_t(hHistStat->GetBinContent(9,j));
      alias_acc_step9[ibit]     = Int_t(hHistStat->GetBinContent(10,j));
    } else {
      alias_acc_step1[ibit]     = Int_t(hHistStat->GetBinContent(3,j));
      alias_acc_step2[ibit]     = Int_t(hHistStat->GetBinContent(4,j));
      alias_acc_step3[ibit]     = Int_t(hHistStat->GetBinContent(5,j));
      alias_acc_step4[ibit]     = Int_t(hHistStat->GetBinContent(6,j));
      alias_acc_step5[ibit]     = Int_t(hHistStat->GetBinContent(7,j));
      alias_acc_step6[ibit]     = Int_t(hHistStat->GetBinContent(8,j));
      alias_acc_step7[ibit]     = Int_t(hHistStat->GetBinContent(9,j));
      alias_acc_step8[ibit]     = Int_t(hHistStat->GetBinContent(10,j));
      alias_acc_step9[ibit]     = Int_t(hHistStat->GetBinContent(11,j));
    }
    //printf("%4i %8i %8i\n",ibit,alias_reconstructed[ibit],alias_accepted[ibit]);
    
    classList.Remove(0,1); // remove +
    array = classList.Tokenize(",");
    // if trigger bit corresponds to several active classes, just take the last one
    // example: kTRD
    // TODO think about more elegant solution
    for (Int_t i=0;i<array->GetEntriesFast();i++){
      TString token = array->At(i)->GetName();
      AliTriggerClass* cl = (AliTriggerClass*) classes.FindObject(token.Data());
      if (!cl) continue;
      Int_t iclass = classes.IndexOf(cl);
      printf(" %30s",token.Data());
      printf(" %12lli",class_l0b[iclass]);
      printf(" %12lli",class_l0a[iclass]);
      printf(" %12lli",class_l1b[iclass]);
      printf(" %12lli",class_l1a[iclass]);
      printf(" %12lli",class_l2b[iclass]);
      printf(" %12lli",class_l2a[iclass]);
      printf("\n");
      alias_recorded[ibit]      = class_l2a[iclass];
      alias_lifetime[ibit]      = class_lifetime[iclass];
      alias_lumi_recorded[ibit] = class_lumi[iclass];
      if (!alias_recorded[ibit]) continue;
      alias_lumi_reconstructed[ibit] = alias_lumi_recorded[ibit]/alias_recorded[ibit]*alias_reconstructed[ibit];
      alias_lumi_accepted[ibit]      = alias_lumi_recorded[ibit]/alias_recorded[ibit]*alias_accepted[ibit];
    }
    array->Delete();
{'fraction_non_alphanumeric': 0.1775975015234613, 'fraction_numerical': 0.07656662604103188, 'mean_word_length': 4.046383496700622, 'pattern_counts': {'":': 0, '<': 191, '<?xml version=': 0, '>': 617, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4868191', 'n_tokens_mistral': 37657, 'n_tokens_neox': 34399, 'n_words': 5062}