void MCefficiencyCent(int isPbPb=0,TString inputmc="/data/wangj/MC2015/Dntuple/pp/revised/ntD_pp_Dzero_kpi_prompt/ntD_EvtBase_20160303_Dfinder_20160302_pp_Pythia8_prompt_D0_dPt0tkPt0p5_pthatweight.root", TString selmcgen="((GisSignal==1||GisSignal==2)&&(Gy>-1&&Gy<1))",TString selmcgenacceptance="((GisSignal==1||GisSignal==2)&&(Gy>-1&&Gy<1))&&abs(Gtk1eta)<2.0&&abs(Gtk2eta)<2.0&&Gtk1pt>2.0&&Gtk2pt>2.0", TString cut_recoonly="Dy>-1.&&Dy<1.&&Dtrk1highPurity&&Dtrk2highPurity&&Dtrk1Pt>2.0&&Dtrk2Pt>2.0&&Dtrk1PtErr/Dtrk1Pt<0.1&&Dtrk2PtErr/Dtrk2Pt<0.1&&abs(Dtrk1Eta)<2.0&&abs(Dtrk2Eta)<2.0&&Dtrk1Algo>3&&Dtrk1Algo<8&&(Dtrk1PixelHit+Dtrk1StripHit)>=11", TString cut="Dy>-1.&&Dy<1.&&Dtrk1highPurity&&Dtrk2highPurity&&Dtrk1Pt>2.0&&Dtrk2Pt>2.0&&(DsvpvDistance/DsvpvDisErr)>3.5&&(DlxyBS/DlxyBSErr)>1.5&&Dchi2cl>0.05&&Dalpha<0.12&&Dtrk1PtErr/Dtrk1Pt<0.1&&Dtrk2PtErr/Dtrk2Pt<0.1&&abs(Dtrk1Eta)<2.0&&abs(Dtrk2Eta)<2.0&&Dtrk1Algo>3&&Dtrk1Algo<8&&Dtrk2Algo>3&&Dtrk2Algo<8&&(Dtrk1PixelHit+Dtrk1StripHit)>=11&&(Dtrk1Chi2ndf/(Dtrk1nStripLayer+Dtrk1nPixelLayer)<0.15)&&(Dtrk2Chi2ndf/(Dtrk2nStripLayer+Dtrk2nPixelLayer)<0.15)",TString label="PP",TString outputfile="test", int useweight=1,Float_t centmin=0., Float_t centmax=100.)
{ 
 
  hiBinMin = centmin*2;
  hiBinMax = centmax*2;
  centMin = centmin;
  centMax = centmax;
  
  if(isPbPb==1)
    {
      selmcgen = selmcgen+Form("&&hiBin>=%f&&hiBin<=%f",hiBinMin,hiBinMax);
      selmcgenacceptance=selmcgenacceptance+Form("&&hiBin>=%f&&hiBin<=%f",hiBinMin,hiBinMax);
      cut_recoonly=cut_recoonly+Form("&&hiBin>=%f&&hiBin<=%f",hiBinMin,hiBinMax);
      cut=cut+Form("&&hiBin>=%f&&hiBin<=%f",hiBinMin,hiBinMax);
    }

     std::cout<<"selmcgen="<<selmcgen<<std::endl;
     std::cout<<"selmcgenacceptance="<<selmcgenacceptance<<std::endl;
     std::cout<<"cut_recoonly"<<cut_recoonly<<std::endl;
     std::cout<<"cut="<<cut<<std::endl;

   std::cout<<"option="<<useweight<<std::endl;
  gStyle->SetOptTitle(0);
  gStyle->SetOptStat(0);
  gStyle->SetEndErrorSize(0);
  gStyle->SetMarkerStyle(20);
 
  TFile* infMC = new TFile(inputmc.Data());
  TTree* ntMC = (TTree*)infMC->Get("ntKp");
  TTree* ntGen = (TTree*)infMC->Get("ntGen");
  TTree* ntSkim = (TTree*)infMC->Get("ntSkim");
  TTree* ntmvaTree = (TTree*)infMC->Get("mvaTree");
  TTree* ntHlt = (TTree*)infMC->Get("ntHlt");

  ntMC->AddFriend(ntmvaTree);
  ntMC->AddFriend(ntGen);
  ntMC->AddFriend(ntSkim);
  ntMC->AddFriend(ntHlt);

  TTree* nthi = (TTree*)infMC->Get("ntHi");
  ntGen->AddFriend(nthi);
  ntGen->AddFriend(ntSkim);
  ntGen->AddFriend(ntHlt);
  nthi->AddFriend(ntMC);
  ntMC->AddFriend(nthi);

  // optimal weigths
  TCut weighpthat = "1";
  TCut weightGpt = "1";
  TCut weightBgenpt = "1";
  TCut weightHiBin = "1";
  if(useweight==0) {
    weightfunctiongen="1";
    weightfunctionreco="1";
    weighpthat = "pthatweight";
    weightGpt = "(pow(10,-0.094152+0.008102*Gpt+Gpt*Gpt*0.000171+Gpt*Gpt*Gpt*-0.000005+Gpt*Gpt*Gpt*Gpt*-0.000000+Gpt*Gpt*Gpt*Gpt*Gpt*0.000000))";
    weightBgenpt = "(pow(10,-0.094152+0.008102*Bgenpt+Bgenpt*Bgenpt*0.000171+Bgenpt*Bgenpt*Bgenpt*-0.000005+Bgenpt*Bgenpt*Bgenpt*Bgenpt*-0.000000+Bgenpt*Bgenpt*Bgenpt*Bgenpt*Bgenpt*0.000000))";
    }
  
  if(useweight==1) {
    weightfunctiongen="6.08582+hiBin*(-0.155739)+hiBin*hiBin*(0.00149946)+hiBin*hiBin*hiBin*(-6.41629e-06)+hiBin*hiBin*hiBin*hiBin*(1.02726e-08)";
    weightfunctionreco="6.08582+hiBin*(-0.155739)+hiBin*hiBin*(0.00149946)+hiBin*hiBin*hiBin*(-6.41629e-06)+hiBin*hiBin*hiBin*hiBin*(1.02726e-08)";
    weighpthat = "pthatweight";
    weightGpt = "(pow(10,-0.107832+0.010248*Gpt+Gpt*Gpt*0.000079+Gpt*Gpt*Gpt*-0.000003+Gpt*Gpt*Gpt*Gpt*-0.000000+Gpt*Gpt*Gpt*Gpt*Gpt*0.000000))";
    weightBgenpt = "(pow(10,-0.107832+0.010248*Bgenpt+Bgenpt*Bgenpt*0.000079+Bgenpt*Bgenpt*Bgenpt*-0.000003+Bgenpt*Bgenpt*Bgenpt*Bgenpt*-0.000000+Bgenpt*Bgenpt*Bgenpt*Bgenpt*Bgenpt*0.000000))";
    weightHiBin = "(6.08582+hiBin*(-0.155739)+hiBin*hiBin*(0.00149946)+hiBin*hiBin*hiBin*(-6.41629e-06)+hiBin*hiBin*hiBin*hiBin*(1.02726e-08))";
    }

  std::cout<<"fit function parameters="<<weightfunctiongen<<std::endl;

  TH1D* hPtMC = new TH1D("hPtMC","",_nBins,_ptBins);
  TH1D* hPtMCrecoonly = new TH1D("hPtMCrecoonly","",_nBins,_ptBins);
  TH1D* hPtGen = new TH1D("hPtGen","",_nBins,_ptBins);
  TH1D* hPtGenAcc = new TH1D("hPtGenAcc","",_nBins,_ptBins);
  TH1D* hPtGenAccWeighted = new TH1D("hPtGenAccWeighted","",_nBins,_ptBins);
  TH1D* hPthat = new TH1D("hPthat","",100,0,500);
  TH1D* hPthatweight = new TH1D("hPthatweight","",100,0,500);

  //ntMC->Project("hPtMC","hiBin",TCut(weightfunctionreco)*(TCut(cut.Data())&&"(Bgen==23333)"));
  ntMC->Project("hPtMC","hiBin",TCut(weighpthat)*TCut(weightBgenpt)*TCut(weightHiBin)*(TCut(cut.Data())&&"(Bgen==23333)"));
  divideBinWidth(hPtMC);
  //ntMC->Project("hPtMCrecoonly","hiBin",TCut(weightfunctionreco)*(TCut(cut_recoonly.Data())&&"(Bgen==23333)"));
  ntMC->Project("hPtMCrecoonly","hiBin",TCut(weighpthat)*TCut(weightBgenpt)*TCut(weightHiBin)*(TCut(cut_recoonly.Data())&&"(Bgen==23333)"));
  divideBinWidth(hPtMCrecoonly);
  //ntGen->Project("hPtGen","hiBin",(TCut(selmcgen.Data())));
  ntGen->Project("hPtGen","hiBin",TCut(weighpthat)*TCut(weightGpt)*(TCut(selmcgen.Data())));
  divideBinWidth(hPtGen);
  //ntGen->Project("hPtGenAcc","hiBin",(TCut(selmcgenacceptance.Data())));
  ntGen->Project("hPtGenAcc","hiBin",TCut(weighpthat)*TCut(weightGpt)*(TCut(selmcgenacceptance.Data())));
  divideBinWidth(hPtGenAcc);
  //ntGen->Project("hPtGenAccWeighted","hiBin",TCut(weightfunctiongen)*(TCut(selmcgenacceptance.Data())));
  ntGen->Project("hPtGenAccWeighted","hiBin",TCut(weighpthat)*TCut(weightGpt)*TCut(weightHiBin)*(TCut(selmcgenacceptance.Data())));
  divideBinWidth(hPtGenAccWeighted);

  ntMC->Project("hPthat","pthat","1");
  ntMC->Project("hPthatweight","pthat",TCut("1"));

  hPtMC->Sumw2();
  hPtGenAcc->Sumw2();
  hPtMCrecoonly->Sumw2();
  //Acceptance
  TH1D* hEffAcc = (TH1D*)hPtGenAcc->Clone("hEffAcc");
  hEffAcc->Sumw2();
  hEffAcc->Divide(hEffAcc,hPtGen,1,1,"b");
  //Selection
  TH1D* hEffSelection = (TH1D*)hPtMC->Clone("hEffSelection");
  hEffSelection->Sumw2();
  hEffSelection->Divide(hEffSelection,hPtGenAccWeighted,1,1,"b");
  //Acc * Eff (one shot)
  TH1D* hEffReco = (TH1D*)hPtMCrecoonly->Clone("hEffReco");
  hEffReco->Sumw2();
  hEffReco->Divide(hEffReco,hPtGen,1,1,"b");
  //Acc * Eff
  TH1D* hEff = (TH1D*)hEffSelection->Clone("hEff");
  hEff->Sumw2();
  //hEff->Divide(hPtMC,hPtGen,1,1,"");
  hEff->Multiply(hEff,hEffAcc,1,1);

  TH2F* hemptyEff=new TH2F("hemptyEff","",50,_ptBins[0]-5.,_ptBins[_nBins]+5.,10.,0,1.0);  
  hemptyEff->GetXaxis()->CenterTitle();
  hemptyEff->GetYaxis()->CenterTitle();
  //hemptyEff->GetYaxis()->SetTitle("acceptance x #epsilon_{reco} x #epsilon_{sel} ");
  hemptyEff->GetYaxis()->SetTitle("#alpha x #epsilon");
  hemptyEff->GetXaxis()->SetTitle("Centrality");
  hemptyEff->GetXaxis()->SetTitleOffset(0.9);
  hemptyEff->GetYaxis()->SetTitleOffset(0.95);
  hemptyEff->GetXaxis()->SetTitleSize(0.05);
  hemptyEff->GetYaxis()->SetTitleSize(0.05);
  hemptyEff->GetXaxis()->SetTitleFont(42);
  hemptyEff->GetYaxis()->SetTitleFont(42);
  hemptyEff->GetXaxis()->SetLabelFont(42);
  hemptyEff->GetYaxis()->SetLabelFont(42);
  hemptyEff->GetXaxis()->SetLabelSize(0.035);
  hemptyEff->GetYaxis()->SetLabelSize(0.035);  
  hemptyEff->SetMaximum(2);
  hemptyEff->SetMinimum(0.);
  hemptyEff->Draw();

  TH2F* hemptyEffAcc=(TH2F*)hemptyEff->Clone("hemptyEffAcc");
  TH2F* hemptyEffReco=(TH2F*)hemptyEff->Clone("hemptyEffReco");
  TH2F* hemptyEffSelection=(TH2F*)hemptyEff->Clone("hemptyEffSelection");
 

  TCanvas*canvasEff=new TCanvas("canvasEff","canvasEff",1000.,500);
  canvasEff->Divide(2,1);
  canvasEff->cd(1);
  
  hemptyEffAcc->SetYTitle("#alpha");
  hemptyEffAcc->Draw();
  hEffAcc->Draw("same");
  canvasEff->cd(2);
  hemptyEff->Draw();
  hEff->Draw("same");
  canvasEff->SaveAs(Form("plotEff/canvasEff_study%s_Cent.pdf",Form(label.Data())));
  
  TH2F* hemptyPthat=new TH2F("hemptyPthat","",50,0.,500.,10,1e-5,1e9);  
  hemptyPthat->GetXaxis()->CenterTitle();
  hemptyPthat->GetYaxis()->CenterTitle();
  hemptyPthat->GetYaxis()->SetTitle("Entries");
  hemptyPthat->GetXaxis()->SetTitle("pthat");
  hemptyPthat->GetXaxis()->SetTitleOffset(0.9);
  hemptyPthat->GetYaxis()->SetTitleOffset(0.95);
  hemptyPthat->GetXaxis()->SetTitleSize(0.05);
  hemptyPthat->GetYaxis()->SetTitleSize(0.05);
  hemptyPthat->GetXaxis()->SetTitleFont(42);
  hemptyPthat->GetYaxis()->SetTitleFont(42);
  hemptyPthat->GetXaxis()->SetLabelFont(42);
  hemptyPthat->GetYaxis()->SetLabelFont(42);
  hemptyPthat->GetXaxis()->SetLabelSize(0.035);
  hemptyPthat->GetYaxis()->SetLabelSize(0.035);  
  hemptyPthat->SetMaximum(2);
  hemptyPthat->SetMinimum(0.);


  TH2F* hemptySpectra=new TH2F("hemptySpectra","",50,0.,130.,10,1,1e5);  
  hemptySpectra->GetXaxis()->CenterTitle();
  hemptySpectra->GetYaxis()->CenterTitle();
  hemptySpectra->GetYaxis()->SetTitle("Entries");
  hemptySpectra->GetXaxis()->SetTitle("Centrality");
  hemptySpectra->GetXaxis()->SetTitleOffset(0.9);
  hemptySpectra->GetYaxis()->SetTitleOffset(0.95);
  hemptySpectra->GetXaxis()->SetTitleSize(0.05);
  hemptySpectra->GetYaxis()->SetTitleSize(0.05);
  hemptySpectra->GetXaxis()->SetTitleFont(42);
  hemptySpectra->GetYaxis()->SetTitleFont(42);
  hemptySpectra->GetXaxis()->SetLabelFont(42);
  hemptySpectra->GetYaxis()->SetLabelFont(42);
  hemptySpectra->GetXaxis()->SetLabelSize(0.035);
  hemptySpectra->GetYaxis()->SetLabelSize(0.035);  

  TH2F* hemptyPthatWeighted=(TH2F*)hemptyPthat->Clone("hemptyPthatWeighted");
  hemptyPthatWeighted->GetXaxis()->SetTitle("pthat reweighted");
  
  TCanvas*canvasPthat=new TCanvas("canvasPthat","canvasPthat",1000.,500);
  canvasPthat->Divide(2,1);
  canvasPthat->cd(1);
  gPad->SetLogy();
  hemptyPthat->Draw("same");
  hPthat->Draw("same");
  canvasPthat->cd(2);
  gPad->SetLogy();
  hemptyPthatWeighted->Draw();
  hPthatweight->Draw("same");
  canvasPthat->SaveAs(Form("plotEff/canvasPthat_%s_Cent.pdf",Form(label.Data())));
  
  TCanvas*canvasSpectra=new TCanvas("canvasSpectra","canvasSpectra",1000.,500);
  canvasSpectra->Divide(2,1);
  canvasSpectra->cd(1);
  gPad->SetLogy();
  hemptySpectra->Draw();
  hPtMC->Draw("same");
  canvasSpectra->cd(2);
  gPad->SetLogy();
  hemptySpectra->Draw();
  hPtGen->Draw("same");
  canvasSpectra->SaveAs(Form("plotEff/canvasSpectra_%s_Cent.pdf",Form(label.Data())));

  //### 1D histogram
  //hEff = hPtMC / hPtGen
  //hEffReco = hPtMCrecoonly / hPtGen
  //hEffAcc = hPtGenAcc / hPtGen
  //hEffSelection = hPtMC / hPtMCrecoonly
 
/*
  ntMC->Project("hPtMC","hiBin",TCut(weightfunctionreco)*(TCut(cut.Data())&&"(Bgen==23333)"));
  ntMC->Project("hPtMCrecoonly","hiBin",TCut(weightfunctionreco)*(TCut(cut_recoonly.Data())&&"(Bgen==23333)"));
  ntGen->Project("hPtGen","hiBin",TCut(weightfunctiongen)*(TCut(selmcgen.Data())));
  ntGen->Project("hPtGenAcc","hiBin",TCut(weightfunctiongen)*(TCut(selmcgenacceptance.Data())));
*/  
  TCanvas*canvas1D=new TCanvas("canvas1D","",600,600);
  canvas1D->cd();
  gPad->SetLogy();
  hemptySpectra->SetYTitle("Entries of hPtMC");
  hemptySpectra->Draw(); 
  hPtMC->Draw("same");
  canvas1D->SaveAs(Form("plotEff/canvas1DhPtMC_%s_Cent.pdf",Form(label.Data())));
  canvas1D->Clear();

  canvas1D=new TCanvas("canvas1D","",600,600);
  canvas1D->cd();
  gPad->SetLogy();
  hemptySpectra->SetYTitle("Entries of hPtMCrecoonly");
  hemptySpectra->Draw(); 
  hPtMCrecoonly->Draw("same");
  canvas1D->SaveAs(Form("plotEff/canvas1DhPtMCrecoonly_%s_Cent.pdf",Form(label.Data())));
  canvas1D->Clear();

  canvas1D=new TCanvas("canvas1D","",600,600);
  canvas1D->cd();
  gPad->SetLogy();
  hemptySpectra->SetYTitle("Entries of hPtGen");
  hemptySpectra->Draw(); 
  hPtGen->Draw("same");
  canvas1D->SaveAs(Form("plotEff/canvas1DhPtGen_%s_Cent.pdf",Form(label.Data())));
  canvas1D->Clear();

  canvas1D=new TCanvas("canvas1D","",600,600);
  canvas1D->cd();
  gPad->SetLogy();
  hemptySpectra->SetYTitle("Entries of hPtGenAcc");
  hemptySpectra->Draw(); 
  hPtGenAcc->Draw("same");
  canvas1D->SaveAs(Form("plotEff/canvas1DhPtGenAcc_%s_Cent.pdf",Form(label.Data())));
  canvas1D->Clear();

  canvas1D=new TCanvas("canvas1D","",600,600);
  canvas1D->cd();
  gPad->SetLogy(0);
  hemptyEff->SetYTitle("hPtMC / hPtGen");
  hemptyEff->Draw(); 
  hEff->Draw("same");
  canvas1D->SaveAs(Form("plotEff/canvas1DhEff_%s_Cent.pdf",Form(label.Data())));
  canvas1D->Clear();

  canvas1D=new TCanvas("canvas1D","",600,600);
  canvas1D->cd();
  hemptyEff->SetYTitle("hPtMCrecoonly / hPtGen");
  hemptyEff->Draw(); 
  hEffReco->Draw("same");
  canvas1D->SaveAs(Form("plotEff/canvas1DhEffReco_%s_Cent.pdf",Form(label.Data())));
  canvas1D->Clear();

  canvas1D=new TCanvas("canvas1D","",600,600);
  canvas1D->cd();
  hemptyEff->SetYTitle("hPtGenAcc / hPtGen");
  hemptyEff->Draw(); 
  hEffAcc->Draw("same");
  canvas1D->SaveAs(Form("plotEff/canvas1DhEffAcc_%s_Cent.pdf",Form(label.Data())));
  canvas1D->Clear();

  canvas1D=new TCanvas("canvas1D","",600,600);
  canvas1D->cd();
  hemptyEff->SetYTitle("hPtMC / hPtGenAcc");
  hemptyEff->Draw(); 
  hEffSelection->Draw("same");
  canvas1D->SaveAs(Form("plotEff/canvas1DhEffSelection_%s_Cent.pdf",Form(label.Data())));
  canvas1D->Clear();

  gStyle->SetPalette(55);
  TCanvas* canvas2D=new TCanvas("canvas2D","",600,600);

  TFile *fout=new TFile(outputfile.Data(),"recreate");
  fout->cd();
  hPtGen->Write();
  hEffAcc->Write();
  hEffReco->Write();
  hEffSelection->Write();
  hEff->Write();
  hPtMC->Write();
  fout->Close();  

}
void trackingStudyAroundJet(char *infname = "skim_jet_ptha120.root")
{
   HiForest *c = new HiForest(infname);
   c->tree->AddFriend("nt");

   TCut trackSelection = "(mtrkQual||mtrkAlgo<4)&&pNRec>0";
   TCut dijetSelection = "leadingJetPt>120&&subleadingJetPt>50&&acos(cos(leadingJetPhi-subleadingJetPhi))>7./8.*3.141593";
   TCut genParticleCut = "abs(pEta)<2.4";
   
   TCanvas *cleadingJetPt = new TCanvas ("cleadingJetPt","",600,600);
   
   TH1F *hEffleadingJetPt = new TH1F("hEffleadingJetPt","",10,0,0.3);
   
   makeHistTitle(hEffleadingJetPt,"","Leading Jet p_{T} (GeV/c)","Tracking Efficiency");
   
   TGraphAsymmErrors *g = getEfficiency(c->tree,"acos(cos(pPhi-leadingJetPhi))",10,0,0.3,trackSelection,TCut("pPt>20")&&dijetSelection&&genParticleCut);
   TGraphAsymmErrors *g2 = getEfficiency(c->tree,"acos(cos(pPhi-leadingJetPhi))",10,0,0.3,trackSelection,TCut("pPt>8")&&dijetSelection&&genParticleCut);
   TGraphAsymmErrors *g3 = getEfficiency(c->tree,"acos(cos(pPhi-leadingJetPhi))",10,0,0.3,trackSelection,TCut("pPt>0")&&dijetSelection&&genParticleCut);

   hEffleadingJetPt->SetAxisRange(0.6,0.9,"Y");
   hEffleadingJetPt->Draw();
   
   g->Draw("p same");
   g2->SetLineColor(2);
   g2->SetMarkerColor(2);
   g2->Draw("p same");
   
   g3->SetLineColor(4);
   g3->SetMarkerColor(4);
   g3->Draw("p same");

   TLegend *legleadingJetPt = myLegend(0.5,0.7,0.9,0.9);
   legleadingJetPt->AddEntry(g,"#hat{p}_{T} 170 GeV/c, Dijet selection","");
   legleadingJetPt->AddEntry(g,"simTrack p_{T} > 20 GeV/c","pl");
   legleadingJetPt->AddEntry(g2,"simTrack p_{T} > 8 GeV/c","pl");
   legleadingJetPt->AddEntry(g3,"simTrack p_{T} > 0 GeV/c","pl");
   
   legleadingJetPt->Draw();
   cleadingJetPt->SaveAs("effFig/EffVsleadingJetDphi-DijetSelection.gif");
   cleadingJetPt->SaveAs("effFig/EffVsleadingJetDphi-DijetSelection.C");

   
}
void ttreesToHistograms() {
//********************************************************************
//****                      Variables                           ****//
cout << "Loading variables into vectors..." << endl;

vector<TString> fileName;
fileName.push_back( "rootfiles0/PhotonJetPt15_Summer09.root"  );//file0
fileName.push_back( "rootfiles0/PhotonJetPt30_Summer09.root"  );//file1
fileName.push_back( "rootfiles0/PhotonJetPt80_Summer09.root"  );//file2
fileName.push_back( "rootfiles0/PhotonJetPt170_Summer09.root" );//file3
fileName.push_back( "rootfiles0/PhotonJetPt300_Summer09.root" );//file4
fileName.push_back( "rootfiles0/PhotonJetPt470_Summer09.root" );//file5
fileName.push_back( "rootfiles0/PhotonJetPt800_Summer09.root" );//file6

TString treeName = "TreePhotonJet";

TString outputFileName = "PhotonJetHists-2009-09-02-matchesReco.root";


//*********************************
//**** Set Scale
// The following 4 number set the scale
// example:
//   scale = (integrated luminosity (1/pb))*(cross section (pb))*(filter eff)/(events analyzed)
float invLuminosityToScaleTo = 200; // in pb-1

vector<float> crossSection;
crossSection.push_back( 2.887E5 -3.222E4 );  // in pb
crossSection.push_back( 3.222E4 -1.010E3 );
crossSection.push_back( 1.010E3 -5.143E1 );
crossSection.push_back( 5.143E1 -4.193E0 );
crossSection.push_back( 4.193E0 -4.515E-1 );
crossSection.push_back( 4.515E-1 -2.003E-2 );
crossSection.push_back( 2.003E-2 );

vector<float> filterEffeciency;
filterEffeciency.push_back( 1.0 );
filterEffeciency.push_back( 1.0 );
filterEffeciency.push_back( 1.0 );
filterEffeciency.push_back( 1.0 );
filterEffeciency.push_back( 1.0 );
filterEffeciency.push_back( 1.0 );
filterEffeciency.push_back( 1.0 );

vector<float> eventsAnalyzied;
eventsAnalyzied.push_back( 1073270 );
eventsAnalyzied.push_back( 1088546 );
eventsAnalyzied.push_back(  993509 );
eventsAnalyzied.push_back( 1483940 );
eventsAnalyzied.push_back( 1024589 );
eventsAnalyzied.push_back( 1014413 );
eventsAnalyzied.push_back( 1216320 );
// END of setting scale
//*********************************


//*********************************
//****         Set Cuts      ****//
// Variables will be plotted for each "location"
vector<TString> locationCut;
locationCut.push_back( "abs(hardGenPhoton_eta)>1.55&&abs(hardGenPhoton_eta)<2.5" );
locationCut.push_back( "abs(hardGenPhoton_eta)<1.45" );

vector<TString> locationName;
locationName.push_back( "Endcap" );
locationName.push_back( "Barrel" );

// These cuts will be merged into one giant cut, applied to all plots for all files
vector<TString> cuts;
cuts.push_back( "hardGenPhoton_et>15.0&&photon_et>15.0&&photon_matches_hardGen>0.5" );

// These cuts will be applied only to corresponding file
vector<TString> fileCuts;
fileCuts.push_back( "event_genEventScale>15&&event_genEventScale<30"    ); //file0
fileCuts.push_back( "event_genEventScale>30&&event_genEventScale<80"    ); //file1
fileCuts.push_back( "event_genEventScale>80&&event_genEventScale<170"   ); //file2
fileCuts.push_back( "event_genEventScale>170&&event_genEventScale<300"  ); //file3
fileCuts.push_back( "event_genEventScale>300&&event_genEventScale<470"  ); //file4
fileCuts.push_back( "event_genEventScale>470&&event_genEventScale<800"  ); //file5
fileCuts.push_back( "event_genEventScale>800&&event_genEventScale<1400" ); //file6
//**** END of setting cuts
//*********************************


//*********************************
int locationVariablesToPlot[2][2]; // [a][b], a=number of locations, b=2 (for min,max range of variables to plot)
locationVariablesToPlot[0][0] = 16; // Endcap
locationVariablesToPlot[0][1] = 35;
locationVariablesToPlot[1][0] = 16; // Barrel
locationVariablesToPlot[1][1] = 35;
/*locationVariablesToPlot[2][0] = 0;
locationVariablesToPlot[2][1] = 4;
locationVariablesToPlot[3][0] = 0;
locationVariablesToPlot[3][1] = 4;
locationVariablesToPlot[4][0] = 0;
locationVariablesToPlot[4][1] = 4;
locationVariablesToPlot[5][0] = 0;
locationVariablesToPlot[5][1] = 4;
locationVariablesToPlot[6][0] = 0;
locationVariablesToPlot[6][1] = 4;
locationVariablesToPlot[7][0] = 0;
locationVariablesToPlot[7][1] = 4;*/

// Variables you want plotted
vector<TString> variableToPlot;
// --- the following require gen level info
variableToPlot.push_back( "hardGenPhoton_et"  );  // 0
variableToPlot.push_back( "hardGenPhoton_eta" );
variableToPlot.push_back( "hardGenPhoton_phi" );
variableToPlot.push_back( "fmod(hardGenPhoton_phi+3.141592,20.0*3.141592/180.0)-10.0*3.141592/180.0" );
variableToPlot.push_back( "abs(hardGenPhoton_eta)" ); // 4
variableToPlot.push_back( "(recPhoton.energy-hardGenPhoton_energy)/hardGenPhoton_energy" );
variableToPlot.push_back( "(recPhoton.energy-hardGenPhoton_energy)/hardGenPhoton_energy:hardGenPhoton_energy"   );
variableToPlot.push_back( "(recPhoton.energy-hardGenPhoton_energy)/hardGenPhoton_energy:abs(hardGenPhoton_eta)" );
variableToPlot.push_back( "(recPhoton.energy-hardGenPhoton_energy)/hardGenPhoton_energy:hardGenPhoton_phiMod"   );
variableToPlot.push_back( "photon_hadronicOverEm:hardGenPhoton_et"       );
variableToPlot.push_back( "photon_hadronicOverEm:abs(hardGenPhoton_eta)" ); // 10
variableToPlot.push_back( "photon_hadronicOverEm:hardGenPhoton_phiMod"   );
variableToPlot.push_back( "photon_eta-hardGenPhoton_eta" );
variableToPlot.push_back( "photon_eta-hardGenPhoton_eta:hardGenPhoton_et" );
variableToPlot.push_back( "photon_eta-hardGenPhoton_eta:abs(hardGenPhoton_eta)" );
variableToPlot.push_back( "deltaPhiGenRecPhoton" );  // 15
// --- the following require only rec photons
variableToPlot.push_back( "photon_et"  );         // 16
variableToPlot.push_back( "photon_eta" );
variableToPlot.push_back( "photon_phi" );
variableToPlot.push_back( "fmod(photon_phi+3.141592,20.0*3.141592/180.0)-10.0*3.141592/180.0" );
variableToPlot.push_back( "abs(photon_eta)" );    // 20
variableToPlot.push_back( "photon_r9" );
variableToPlot.push_back( "photon_ecalRecHitSumEtConeDR03" );
variableToPlot.push_back( "photon_hcalTowerSumEtConeDR03"  );
variableToPlot.push_back( "photon_trkSumPtSolidConeDR03"   );
variableToPlot.push_back( "photon_trkSumPtHollowConeDR03"  ); //25
variableToPlot.push_back( "photon_nTrkSolidConeDR03"  );
variableToPlot.push_back( "photon_nTrkHollowConeDR03" );
variableToPlot.push_back( "photon_hadronicOverEm"     );
variableToPlot.push_back( "photon_r2x5" );
variableToPlot.push_back( "photon_ecalRecHitSumEtConeDR03/photon_et" ); // 30
variableToPlot.push_back( "photon_hcalTowerSumEtConeDR03/photon_et"  );
variableToPlot.push_back( "photon_trkSumPtSolidConeDR03/photon_et"   );
variableToPlot.push_back( "photon_trkSumPtHollowConeDR03/photon_et"  );
// --- the following require jets
/*variableToPlot.push_back( "calcDeltaPhi(photon_phi,jet_phi)"  );
variableToPlot.push_back( "calcDeltaPhi(photon_phi,jet2_phi)" ); // 35
variableToPlot.push_back( "calcDeltaPhi(jet_phi,jet2_phi)"    );*/
variableToPlot.push_back( "(photon_et-jet_et)/photon_et"      );
variableToPlot.push_back( "jet2_et/photon_et"                 );

// Histograms for the above variables
vector<TH1*> histogram;
// --- the following require gen level info
histogram.push_back( new TH1F("photonGenEt",     "Photon E_{T} ;E_{T} (GeV);entries per 15 GeV",  50,  0, 750)  );  // 0
histogram.push_back( new TH1F("photonGenEta",    "Photon #eta ;#eta;entries per 0.1 bin",   61, -3.05,   3.05) );
histogram.push_back( new TH1F("photonGenPhi",    "Photon #phi ;#phi;entries per bin",   62, (-1.-1./30.)*TMath::Pi(), (1.+1./30.)*TMath::Pi()) );
histogram.push_back( new TH1F("photonGenPhiMod", "Photon #phi_{mod} ", 42, (-1.-1./20)*0.1745329, (1.+1./20.)*0.1745329) );
histogram.push_back( new TH1F("photonGenAbsEta", "Photon |#eta| ", 51, 0.00,   2.55) );
histogram.push_back( new TH1F("photonDeltaE",    "(E(#gamma_{rec})-E(#gamma_{gen}))/E(#gamma_{gen}) ", 50, -0.8, 0.3) );
histogram.push_back( new TH2F("photonDeltaE_vs_E","(E(#gamma_{rec})-E(#gamma_{gen}))/E(#gamma_{gen}) vs E(#gamma_{gen}) ", 50, 0, 3000, 50, -0.8, 0.3) );
histogram.push_back( new TH2F("photonDeltaE_vs_AbsEta","(E(#gamma_{rec})-E(#gamma_{gen}))/E(#gamma_{gen}) vs |#eta(#gamma_{gen}|) ", 51, 0.0, 2.5, 50, -0.8, 0.3) );
histogram.push_back( new TH2F("photonDeltaE_vs_PhiMod","(E(#gamma_{rec})-E(#gamma_{gen}))/E(#gamma_{gen}) vs #phi_{mod}(#gamma_{gen}) ", 42, (-1.-1./20)*0.1745329, (1.+1./20.)*0.1745329, 50, -0.9, 0.2) );
histogram.push_back( new TH2F("photonHoverE_vs_Et",     "H/E vs E_{T}(#gamma_{gen}) ", 50, 0, 1000, 50, 0.0, 0.2) );
histogram.push_back( new TH2F("photonHoverE_vs_AbsEta", "H/E vs |#eta(#gamma_{gen})| ", 51, 0.0, 2.5, 50, 0.0, 0.2) );
histogram.push_back( new TH2F("photonHoverE_vs_PhiMod", "H/E vs #phi_{mod}(#gamma_{gen}) ", 42, (-1.-1./20)*0.1745329, (1.+1./20.)*0.1745329, 50, 0.0, 0.2) );
histogram.push_back( new TH1F("photonDeltaEta", "#Delta#eta(#gamma_{rec},#gamma_{gen}) ;#Delta#eta(#gamma_{rec},#gamma_{gen});entries/bin", 41, -0.01, 0.01) );
histogram.push_back( new TH2F("photonDeltaEta_vs_Et",    "#Delta#eta(#gamma_{rec},#gamma_{gen}) vs E_{T}(#gamma_{gen}) ", 50, 0, 1000, 41, -0.1, 0.1) );
histogram.push_back( new TH2F("photonDeltaEta_vs_AbsEta","#Delta#eta(#gamma_{rec},#gamma_{gen}) vs #eta(#gamma_{gen})", 51, 0.0, 2.55, 41, -0.1, 0.1) );
histogram.push_back( new TH1F("photonDeltaPhi",          "#Delta#phi(#gamma_{rec},#gamma_{gen}) ;#Delta#phi(#gamma_{rec},#gamma_{gen});entries/bin", 41, 0.0, 0.01) ); // 15
// --- the following require only rec photons
histogram.push_back( new TH1F("photonEt",        "Photon E_{T} ;E_{T} (GeV);entries per 15 GeV", 50,  0,    750   ) ); // 16
histogram.push_back( new TH1F("photonEta",       "Photon #eta ;#eta;entries per 0.1"           , 61, -3.05,   3.05) );
histogram.push_back( new TH1F("photonPhi",       "Photon #phi ;#phi;entries per bin"           , 62, (-1.-1./30.)*TMath::Pi(), (1.+1./30.)*TMath::Pi()) );
histogram.push_back( new TH1F("photonPhiMod",    "Photon #phi_{mod} "                          , 42, (-1.-1./20)*0.1745329, (1.+1./20.)*0.1745329) );
histogram.push_back( new TH1F("photonAbsEta",    "Photon |#eta| "                              , 51, 0.00,  2.55) );  // 20
histogram.push_back( new TH1F("photonR9",        "R9 = E(3x3) / E(SuperCluster)  ;R9;entries/bin"   , 50, 0.6, 1.0) );
histogram.push_back( new TH1F("photonEcalIso",   "#SigmaEcal Rec Hit E_{T} in Hollow #DeltaR cone " , 50, 0  , 15) );
histogram.push_back( new TH1F("photonHcalIso",   "#SigmaHcal Rec Hit E_{T} in Hollow #DeltaR cone " , 50, 0  , 15) );
histogram.push_back( new TH1F("photonTrackSolidIso",     "#Sigmatrack p_{T} in Solid #DeltaR cone " , 50, 0  , 15) );
histogram.push_back( new TH1F("photonTrackHollowIso",    "#Sigmatrack p{T} in Hollow #DeltaR cone " , 50, 0  , 15) );  // 25
histogram.push_back( new TH1F("photonTrackCountSolid",   "Number of tracks in Solid #DeltaR cone ;Number of Tracks;entries/bin" , 25, -0.5, 24.5) );
histogram.push_back( new TH1F("photonTrackCountHollow",  "Number of tracks in Hollow #DeltaR cone ;Number of Tracks;entries/bin", 25, -0.5, 24.5) );
histogram.push_back( new TH1F("photonHoverE",            "Hadronic / EM ", 50, 0.0, 0.2) );
histogram.push_back( new TH1F("photonScSeedE2x5over5x5", "E2x5/E5x5  "   , 50, 0.6, 1.0) );
histogram.push_back( new TH1F("photonEcalIsoOverE",        "#SigmaEcal Rec Hit E_{T} in #DeltaR cone / Photon E_{T} " , 50, -0.1, 1.0) ); // 30
histogram.push_back( new TH1F("photonHcalIsoOverE",        "#SigmaHcal Rec Hit E_{T} in #DeltaR cone / Photon E_{T} " , 50, -0.1, 1.0) );
histogram.push_back( new TH1F("photonTrackSolidIsoOverE" , "#SigmaTrack p_{T} in #DeltaR cone / Photon E_{T} "        , 50, -0.1, 1.0) );
histogram.push_back( new TH1F("photonTrackHollowIsoOverE", "#SigmaTrack p_{T} in Hollow #DeltaR cone / Photon E_{T} " , 50, -0.1, 1.0) );
// --- the following require jets
/*histogram.push_back( new TH1F("h_deltaPhi_photon_jet", "#Delta#phi between Highest E_{T} #gamma and jet;#Delta#phi(#gamma,1^{st} jet)"               , 50, 0, 3.1415926) );
histogram.push_back( new TH1F("h_deltaPhi_photon_jet2","#Delta#phi between Highest E_{T} #gamma and 2^{nd} highest jet;#Delta#phi(#gamma,2^{nd} jet)", 50, 0, 3.1415926) );
histogram.push_back( new TH1F("h_deltaPhi_jet_jet2"  , "#Delta#phi between Highest E_{T} jet and 2^{nd} jet;#Delta#phi(1^{st} jet,2^{nd} jet)"       , 50, 0, 3.1415926) );*/
histogram.push_back( new TH1F("h_deltaEt_photon_jet" , "(E_{T}(#gamma)-E_{T}(jet))/E_{T}(#gamma) when #Delta#phi(#gamma,1^{st} jet) > 2.8;#DeltaE_{T}(#gamma,1^{st} jet)/E_{T}(#gamma)", 20, -1.0, 1.0) );
histogram.push_back( new TH1F("h_jet2_etOverPhotonEt", "E_{T}(2^{nd} highest jet) / E_{T}(#gamma);E_{T}(2^{nd} Jet)/E_{T}(#gamma)", 20, 0.0, 4.0) );
//****                  END of Variables                        ****//
//********************************************************************




//********************************************************************
//****                Main part of Program                      ****//

 // Human error checking
 if (variableToPlot.size() != histogram.size() ) {
   cout << "Should have equal entries in histogram and variableToPlot vector." << endl;
   return;
 }
 if (fileName.size() > crossSection.size() ) {
   cout << "Should have equal entries in fileName and crossSection vetor." << endl;
   return;
 }
 if (fileName.size() > fileCuts.size() ) {
   cout << "Should have equal entries in fileName and fileCuts vector." << endl;
   return;
 }

 // Combine all the cuts into one
 cout << endl << "Cuts that will be applied to everything: " << endl << "  ";
 TCut allCuts = "";
 for (int i =0; i<cuts.size(); i++) {
   allCuts += cuts[i];
   if (i>0) cout << "&&";
   cout << "(" << cuts[i] << ")";
 }
 cout << endl << endl;


 // Open the files & set their scales
 cout << endl << "Histograms will be scaled to " << invLuminosityToScaleTo << "pb-1 " << endl;
 cout << "Looking for TTree named \"" << treeName << "\" in files..." << endl;
 vector<float> fileScale;
 TList *fileList = new TList();
 for (int i=0; i < fileName.size(); i++) {

   TFile* currentFile = TFile::Open(fileName[i]);
   fileList->Add(currentFile);
   float currentScale = crossSection[i]*invLuminosityToScaleTo*filterEffeciency[i]/eventsAnalyzied[i];
   fileScale.push_back( currentScale );

   // Display entries in that file's TTree
   TTree* tree;
   currentFile->GetObject(treeName, tree);
   cout << "file" << i <<": " << fileName[i] << " contains " << tree->GetEntries(allCuts) << " entries, and will be scaled by " << 
currentScale << endl;
 }
 cout << endl << endl;


 //Create output file
 TFile *outputFile = TFile::Open( outputFileName, "RECREATE" );


 //************************************************************
 //                 Core of the Script                       //
 // Loop over locations
 for (int l=0; l<locationName.size(); l++) {
   TString currentLocation = locationName[l];
   TCut currentCuts = allCuts;
   currentCuts += locationCut[l];
   cout << "Creating plots for " << currentLocation << ", " << locationCut[l] << endl;
  
   // Loop over variables to plot
   for (int i=0; i<variableToPlot.size(); i++) {
     // should we plot this variable for this location?
     if (i<locationVariablesToPlot[l][0] || i>locationVariablesToPlot[l][1]) continue;

     TString currentHistType  = histogram[i]->IsA()->GetName();
     TString currentHistName  = TString(histogram[i]->GetName())  + "_" + currentLocation;
     TString currentHistTitle = TString(histogram[i]->GetTitle()) + "(" + currentLocation + ")";
     cout << "  " << variableToPlot[i] << " >> " << currentHistName;
     TString currentHistDrawOpt;
     if (currentHistType=="TH2F") {
       currentHistDrawOpt="goffbox";
     } else {
       currentHistDrawOpt="egoff";
     }
     TH1* currentHist = (TH1*)histogram[i]->Clone(currentHistName);  // Creates clone with name currentHistName
     currentHist->Sumw2(); // store errors
     currentHist->SetTitle(currentHistTitle);
     //cout << " from file";

     // Plot from the first file
     int f = 0;
     //cout << f;
     TTree *tree;
     TFile *current_file = (TFile*)fileList->First();
     current_file->cd();
     current_file->GetObject(treeName, tree);
     tree->Draw(variableToPlot[i]+">>"+currentHistName,currentCuts+TCut(fileCuts[f]),currentHistDrawOpt);
     currentHist->Scale(fileScale[f]);
     f++;

     // Loop over files
     current_file = (TFile*)fileList->After( current_file );
     while ( current_file ) {
       current_file->cd();
       //cout << ", file" << f;
       current_file->GetObject(treeName, tree);

       TString tempHistName = currentHistName+"Temp";
       TH1* tempHist = (TH1*)currentHist->Clone(tempHistName);
       tree->Draw(variableToPlot[i]+">>"+tempHistName,currentCuts+TCut(fileCuts[f]),currentHistDrawOpt);
       tempHist->Scale(fileScale[f]);
       currentHist->Add(tempHist);
       tempHist->Delete();

       current_file = (TFile*)fileList->After( current_file );
       f++;
     } // End of loop over files

     outputFile->cd();
     currentHist->Write();
     cout << endl;
   } // End of loop over variabls to plot
 } // End of loop over locations
 //                  END of Core of Script                   //
 //************************************************************

 cout << endl;
 cout << "Wrote file " << outputFileName << endl;
 cout << endl;
 outputFile->Close();
}
int main(int argc, char* argv[]) {

  TH1::SetDefaultSumw2();

  ProgramOptions options(argc, argv);

  double lumi = options.lumi;

  // input datasets
  Datasets datasets(options.iDir);
  datasets.readFile(options.datasetFile);
 
  std::string oDir_Plot = options.oDir+std::string("/ZBackground");

  boost::filesystem::path opath(oDir_Plot);
  if (!exists(opath)) {
    std::cout << "Creating output directory : " << oDir_Plot << std::endl;
    boost::filesystem::create_directory(opath);
  }
 
  if (options.doMCFMWeights) std::cout << "Going to apply MCFM weights" << std::endl;

  // output file
  TFile* ofile = TFile::Open( (options.oDir+std::string("/ZBackground.root")).c_str(), "RECREATE");

  // cuts
  Cuts cuts;
  unsigned nCutsZMuMu = cuts.nCutsZMuMu();

  TCut puWeight("puWeight");
  TCut trigCorr( "(trigCorrWeight>0) ? trigCorrWeight : 1." );
  TCut METNoMuon130("metNoMuon>130.");	// add here later for VBF efficiency when MET>35, MET>70 (QCD estimation)
  TCut METNo2Muon130("metNo2Muon>130.");
  TCut cutLoDPhi = cuts.cut("dPhiJJ");

  // For lepton weights
  TCut lVetoWeight   = cuts.elVetoWeight(options.leptCorr) * cuts.muVetoWeight(options.leptCorr);
  TCut muTightWeight = cuts.muTightWeight(options.leptCorr);

  // histograms
  double dphiEdges[4] = { 0., 1.0, 2.6, TMath::Pi() };
  //double metEdges[13] = { 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., 100., 110., 120. };

  // signal MET>130
  TH1D* hZ_DY_C_DPhi   = new TH1D("hZ_DY_C_DPhi",   "", 3, dphiEdges);  // Z+jets MC ctrl region
  TH1D* hZ_BG_C_DPhi   = new TH1D("hZ_BG_C_DPhi",   "", 3, dphiEdges);  // background MC ctrl region
  TH1D* hZ_Data_C_DPhi = new TH1D("hZ_Data_C_DPhi", "", 3, dphiEdges);  // Data ctrl region

  TH1D* hZ_QCD_EffVBFS_D = new TH1D("hZ_QCD_EffVBFS_D", "", 1, 0., 1.);
  TH1D* hZ_QCD_EffVBFS_N = new TH1D("hZ_QCD_EffVBFS_N", "", 1, 0., 1.);
  TH1D* hZ_EWK_EffVBFS_D = new TH1D("hZ_EWK_EffVBFS_D", "", 1, 0., 1.);
  TH1D* hZ_EWK_EffVBFS_N = new TH1D("hZ_EWK_EffVBFS_N", "", 1, 0., 1.);
 
  // combine eps_VBF_C * eps_mumu which remove canceling term (denominator is Z->uu gen level cut)
  TH1D* hZ_QCD_EffVBFC_D = new TH1D("hZ_QCD_EffVBFC_D", "", 1, 0., 1.);
  TH1D* hZ_QCD_EffVBFC_N = new TH1D("hZ_QCD_EffVBFC_N", "", 1, 0., 1.);
  TH1D* hZ_EWK_EffVBFC_D = new TH1D("hZ_EWK_EffVBFC_D", "", 1, 0., 1.);
  TH1D* hZ_EWK_EffVBFC_N = new TH1D("hZ_EWK_EffVBFC_N", "", 1, 0., 1.);

  // cutflow histograms
  TH1D* hZ_CutFlow_Data       = new TH1D("hZ_CutFlow_Data",       "", nCutsZMuMu, 0., nCutsZMuMu);
  TH1D* hZ_CutFlow_DY         = new TH1D("hZ_CutFlow_DY",         "", nCutsZMuMu, 0., nCutsZMuMu);
  TH1D* hZ_CutFlow_SingleTSum = new TH1D("hZ_CutFlow_SingleTSum", "", nCutsZMuMu, 0., nCutsZMuMu);
  TH1D* hZ_CutFlow_Diboson    = new TH1D("hZ_CutFlow_Diboson",    "", nCutsZMuMu, 0., nCutsZMuMu);

  // Hists to calculate DY normalisation factor
  TH1D* hZ_DY_NoVBFNoWeight = new TH1D("hZ_DY_NoVBFNoWeight","", 1, 0., 1.); // DY MC yield after dimuon and dijet selection without y* and mjj weighting
  TH1D* hZ_DY_NoVBFWeight   = new TH1D("hZ_DY_NoVBFWeight",  "", 1, 0., 1.); // DY MC yield after dimuon and dijet selection with y* and mjj weighting

  // loop over MC datasets
  for (unsigned i=0; i<datasets.size(); ++i) {

    Dataset dataset = datasets.getDataset(i);
    
    TCut cutD = cuts.cutDataset(dataset.name);
    TCut yStarWeight("");
    TCut mjjWeight("");

    // check if it's DYJets
    bool isDY = false;
    if (dataset.name.compare(0,2,"DY")==0) {
      isDY = true;
      std::cout << "Analysing DY->ll MC : " << dataset.name << std::endl; 
      // check if it's QCD
      if ( !(dataset.name.length()>11 && dataset.name.compare(11,14,"EWK")==0) ) {
    	if (options.doMCFMWeights) {
    	  yStarWeight = TCut("(0.849667 + (0.149687*abs(log((sqrt(zgenmass*zgenmass + zgenpt*zgenpt*cosh(zgeneta)*cosh(zgeneta)) + zgenpt*sinh(zgeneta))/sqrt(zgenmass*zgenmass + zgenpt*zgenpt)) - 0.5*(genJet1Eta + genJet2Eta))))");
    	  mjjWeight   = TCut("0.392568 + (0.120734*log(genVBFM)) - (0.000255622*genVBFM)");
    	}
    	std::cout << "Analysing QCD DY MC " << std::endl;
      }
    }
    else if (dataset.isData) {
      std::cout << "Analysing Data      : " << dataset.name << std::endl;
    }
    else {
      std::cout << "Analysing BG MC     : " << dataset.name << std::endl;
    }

    TCut otherWeight = puWeight * yStarWeight * mjjWeight;

    // get file & tree
    TFile* file = datasets.getTFile(dataset.name);
    TTree* tree = (TTree*) file->Get("invHiggsInfo/InvHiggsInfo");

    // set up cuts
    TCut cutZMuMu_C           = otherWeight * trigCorr * (cutD + cuts.zMuMuVBF() + METNo2Muon130);
    
    TCut cutEfficiencyMuMu_D  = otherWeight * (cutD + cuts.zMuMuGen());
    //TCut cutEfficiencyMuMu_N  = otherWeight * (cutD + cuts.zMuMuGen() + cuts.zMuMuReco());
 
    TCut cutEfficiencyVBFS_D  = otherWeight * (cutD + cuts.zMuMuGenMass());
    TCut cutEfficiencyVBFS_N  = otherWeight * trigCorr * (cutD + cuts.HLTandMETFilters() + cuts.zMuMuGenMass() + cuts.vbf() + METNoMuon130 + cutLoDPhi);
    
    //TCut cutEfficiencyVBFC_D  = otherWeight * (cutD + cuts.zMuMuGen() + cuts.zMuMuReco());
    TCut cutEfficiencyVBFC_N  = otherWeight * trigCorr * (cutD + cuts.HLTandMETFilters() + cuts.zMuMuGen() + cuts.zMuMuReco() + cuts.vbf() + METNo2Muon130 + cutLoDPhi);

    // re-weighting
    TCut cutDYNoVBFNoWeight = puWeight * trigCorr * (cutD  + cuts.zMuMuGen() + cuts.zMuMuReco() + cuts.cutZMuMu("dijet"));
    TCut cutDYNoVBFWeight   = otherWeight * trigCorr * (cutD  + cuts.zMuMuGen() + cuts.zMuMuReco() + cuts.cutZMuMu("dijet"));

    if(!(dataset.isData)) {
	cutZMuMu_C *= muTightWeight;
	cutEfficiencyVBFC_N *= muTightWeight;
	cutDYNoVBFNoWeight  *= muTightWeight;
	cutDYNoVBFWeight    *= muTightWeight;
    }

    // fill tmp histograms for BG estimation
    TH1D* hZ_C_DPhi  = new TH1D("hZ_C_DPhi", "", 3, dphiEdges);  // this is for the actual BG estimation
    // fill tmp histograms for efficiency calculation
    TH1D* hZ_EffVBFS_D  = new TH1D("hZ_EffVBFS_D", "", 1, 0., 1.);
    TH1D* hZ_EffVBFS_N  = new TH1D("hZ_EffVBFS_N", "", 1, 0., 1.);
    TH1D* hZ_EffVBFC_D  = new TH1D("hZ_EffVBFC_D", "", 1, 0., 1.);
    TH1D* hZ_EffVBFC_N  = new TH1D("hZ_EffVBFC_N", "", 1, 0., 1.);

    TH1D* hZ_DY_NoWeight = new TH1D("hZ_DY_NoWeight", "", 1, 0, 1);
    TH1D* hZ_DY_Weight   = new TH1D("hZ_DY_Weight",   "", 1, 0, 1);
    

    if (isDY) {
      tree->Draw("vbfDPhi>>hZ_C_DPhi", cutZMuMu_C);
      tree->Draw("0.5>>hZ_EffVBFS_D",  cutEfficiencyVBFS_D);
      tree->Draw("0.5>>hZ_EffVBFS_N",  cutEfficiencyVBFS_N);
      tree->Draw("0.5>>hZ_EffVBFC_D",  cutEfficiencyMuMu_D);
      tree->Draw("0.5>>hZ_EffVBFC_N",  cutEfficiencyVBFC_N);

      tree->Draw("0.5>>hZ_DY_NoWeight", cutDYNoVBFNoWeight);
      tree->Draw("0.5>>hZ_DY_Weight",   cutDYNoVBFWeight);

//       tree->Draw("abs((log((sqrt(zgenmass*zgenmass + zgenpt*zgenpt*cosh(zgeneta)*cosh(zgeneta)) + zgenpt*sinh(zgeneta))/(sqrt(zgenmass*zgenmass + zgenpt*zgenpt)))) - 0.5*(genJet1Eta + genJet2Eta))>>hYStar", cutDYNoVBFNoWeight);
//       tree->Draw("abs((log((sqrt(zgenmass*zgenmass + zgenpt*zgenpt*cosh(zgeneta)*cosh(zgeneta)) + zgenpt*sinh(zgeneta))/(sqrt(zgenmass*zgenmass + zgenpt*zgenpt)))) - 0.5*(genJet1Eta + genJet2Eta))>>hYStarC", cutZMuMu_C);
    }
    else {
      tree->Draw("vbfDPhi>>hZ_C_DPhi", cutZMuMu_C);
      //tree->Draw("met:vbfDPhi>>hZ_C_METDPhi",  cutZMuMu_MET0_C);
    }

    // weight  to lumi
    double weight = (dataset.isData) ? 1. : lumi * dataset.sigma / dataset.nEvents;
    hZ_C_DPhi->Scale(weight);
    hZ_EffVBFS_D->Scale(weight);
    hZ_EffVBFS_N->Scale(weight);
    hZ_EffVBFC_D->Scale(weight);
    hZ_EffVBFC_N->Scale(weight);

    hZ_DY_NoWeight->Scale(weight);
    hZ_DY_Weight->Scale(weight);
    
    // add to output histograms
    if (dataset.isData) {
      hZ_Data_C_DPhi->Add(hZ_C_DPhi);
    }
    else if (isDY) {
      hZ_DY_C_DPhi->Add(hZ_C_DPhi);

      hZ_DY_NoVBFNoWeight->Add(hZ_DY_NoWeight);
      hZ_DY_NoVBFWeight->Add(hZ_DY_Weight);

      if(dataset.name == "DYJetsToLL_NoTrig" || dataset.name == "DYJetsToLL_PtZ-100_NoTrig") {
        hZ_QCD_EffVBFS_D->Add(hZ_EffVBFS_D);
        hZ_QCD_EffVBFS_N->Add(hZ_EffVBFS_N);
        hZ_QCD_EffVBFC_D->Add(hZ_EffVBFC_D);
        hZ_QCD_EffVBFC_N->Add(hZ_EffVBFC_N);
      }
      if(dataset.name == "DYJetsToLL_EWK_NoTrig") {
        hZ_EWK_EffVBFS_D->Add(hZ_EffVBFS_D);
        hZ_EWK_EffVBFS_N->Add(hZ_EffVBFS_N);
        hZ_EWK_EffVBFC_D->Add(hZ_EffVBFC_D);
        hZ_EWK_EffVBFC_N->Add(hZ_EffVBFC_N);
      }
    }
    else {
      hZ_BG_C_DPhi->Add(hZ_C_DPhi);
    }

    std::cout << "  N ctrl (dphi<1.0) : " << hZ_C_DPhi->GetBinContent(1) << " +/- " << hZ_C_DPhi->GetBinError(1) << std::endl;  
    

    delete hZ_C_DPhi;
    delete hZ_EffVBFS_D;
    delete hZ_EffVBFS_N;
    delete hZ_EffVBFC_D;
    delete hZ_EffVBFC_N;

    delete hZ_DY_NoWeight;
    delete hZ_DY_Weight;

    ofile->cd();

    // cut flow histograms
    std::string hname = std::string("hZ_CutFlow_")+dataset.name;
    TH1D* hZ_CutFlow = new TH1D(hname.c_str(), "", nCutsZMuMu, 0., nCutsZMuMu);

    for (unsigned c=0; c<nCutsZMuMu; ++c) {

      TCut cut;

      if(c == nCutsZMuMu-1) cut = otherWeight * trigCorr * (cutD + cuts.cutflowZMuMu(c));
      else cut = otherWeight * (cutD + cuts.cutflowZMuMu(c));

      if(!(dataset.isData)) cut *= muTightWeight;

      TH1D* h = new TH1D("h","", 1, 0., 1.);
      tree->Draw("0.5>>h", cut);

      hZ_CutFlow->SetBinContent(c+1, h->GetBinContent(1));
      hZ_CutFlow->SetBinError(c+1, h->GetBinError(1));

      delete h;
    }

    hZ_CutFlow->Scale(weight);

    if (dataset.isData) {
      hZ_CutFlow_Data->Add(hZ_CutFlow);
    }
    if (dataset.name.compare(0,2,"DY")==0) {
      hZ_CutFlow_DY->Add(hZ_CutFlow);
    }
    if (dataset.name.compare(0,7,"SingleT")==0) {
      hZ_CutFlow_SingleTSum->Add(hZ_CutFlow);
    }
    if (dataset.name.compare(0,2,"WW")==0 ||
  	dataset.name.compare(0,2,"WZ")==0 ||
  	dataset.name.compare(0,2,"ZZ")==0 || 
	dataset.name.compare(0,2,"WG")==0) {
      hZ_CutFlow_Diboson->Add(hZ_CutFlow);
    }

    hZ_CutFlow->Write("",TObject::kOverwrite);

    delete hZ_CutFlow;


    // Z control plots
    TCut cutPlots = otherWeight * trigCorr * (cutD + cuts.zMuMuVBFLoose());

    if(!(dataset.isData)) cutPlots *= muTightWeight;
 
    TFile* ofile_Plot = TFile::Open( (oDir_Plot+std::string("/")+dataset.name+std::string(".root")).c_str(), "RECREATE");

    TH1D* ZCtrlZMass    = new TH1D("ZCtrlZMass",    "", 30, 60., 120.);
    TH1D* ZCtrlZpT      = new TH1D("ZCtrlZpT",      "", 50, 0.,  1000.);
    TH1D* ZCtrlJet1pT   = new TH1D("ZCtrlJet1pT",   "", 50, 0.,  1000.);
    TH1D* ZCtrlJet1Eta  = new TH1D("ZCtrlJet1Eta",  "", 50, -5., 5.);
    TH1D* ZCtrlJet2pT   = new TH1D("ZCtrlJet2pT",   "", 50, 0.,  1000.);
    TH1D* ZCtrlJet2Eta  = new TH1D("ZCtrlJet2Eta",  "", 50, -5., 5.);
    TH1D* ZCtrlCenJetpT = new TH1D("ZCtrlCenJetpT", "", 50, 0.,  400.);
    TH1D* ZCtrlDEtajj   = new TH1D("ZCtrlDEtajj",   "", 50, 0.,  8.);
    TH1D* ZCtrlMjj      = new TH1D("ZCtrlMjj",      "", 30, 0.,  3000.);
    TH1D* ZCtrlMET      = new TH1D("ZCtrlMET",      "", 25, 10.,  510.);
    TH1D* ZCtrlDPhijj   = new TH1D("ZCtrlDPhijj",   "", 50, 0.,  TMath::Pi());

    TH1D* ZCtrlYStar    = new TH1D("ZCtrlYStar",    "", 50, 0., 5.);
    TH1D* ZCtrlYStarWt  = new TH1D("ZCtrlYStarWt",  "", 50, 0., 2.);
    TH1D* ZCtrlMjjWt    = new TH1D("ZCtrlMjjWt",    "", 50, 0., 2.);
    TH1D* ZCtrlYStarMjjWt    = new TH1D("ZCtrlYStarMjjWt",    "", 50, -1., 5.);    

    tree->Draw("zMass>>ZCtrlZMass"	    , cutPlots);
    tree->Draw("zPt>>ZCtrlZpT"		    , cutPlots);
    tree->Draw("jet1Pt>>ZCtrlJet1pT"	    , cutPlots);
    tree->Draw("jet1Eta>>ZCtrlJet1Eta"	    , cutPlots);
    tree->Draw("jet2Pt>>ZCtrlJet2pT"	    , cutPlots);
    tree->Draw("jet2Eta>>ZCtrlJet2Eta"	    , cutPlots);
    tree->Draw("cenJetEt>>ZCtrlCenJetpT"    , cutPlots);
    tree->Draw("vbfDEta>>ZCtrlDEtajj"	    , cutPlots);
    tree->Draw("vbfM>>ZCtrlMjj"		    , cutPlots);
    tree->Draw("metNo2Muon>>ZCtrlMET"	    , cutPlots);
    tree->Draw("vbfDPhi>>ZCtrlDPhijj"	    , cutPlots);

    if (options.doMCFMWeights) {

    tree->Draw("abs(log((sqrt(zgenmass*zgenmass + zgenpt*zgenpt*cosh(zgeneta)*cosh(zgeneta)) + zgenpt*sinh(zgeneta))/sqrt(zgenmass*zgenmass + zgenpt*zgenpt)) - 0.5*(genJet1Eta + genJet2Eta))>>ZCtrlYStar"	  , cutPlots);
    tree->Draw("(0.849667 + (0.149687*abs(log((sqrt(zgenmass*zgenmass + zgenpt*zgenpt*cosh(zgeneta)*cosh(zgeneta)) + zgenpt*sinh(zgeneta))/sqrt(zgenmass*zgenmass + zgenpt*zgenpt)) - 0.5*(genJet1Eta + genJet2Eta))))>>ZCtrlYStarWt", cutPlots);
    tree->Draw("(0.392568 + (0.120734*log(genVBFM)) - (0.000255622*genVBFM))>>ZCtrlMjjWt", cutPlots);
    tree->Draw("(0.849667 + (0.149687*abs(log((sqrt(zgenmass*zgenmass + zgenpt*zgenpt*cosh(zgeneta)*cosh(zgeneta)) + zgenpt*sinh(zgeneta))/sqrt(zgenmass*zgenmass + zgenpt*zgenpt)) - 0.5*(genJet1Eta + genJet2Eta))))*(0.392568 + (0.120734*log(genVBFM)) - (0.000255622*genVBFM))>>ZCtrlYStarMjjWt", cutPlots);

    }

    if (!dataset.isData) {
      ZCtrlZMass->Scale(weight);
      ZCtrlZpT->Scale(weight);
      ZCtrlJet1pT->Scale(weight);
      ZCtrlJet1Eta->Scale(weight);
      ZCtrlJet2pT->Scale(weight);
      ZCtrlJet2Eta->Scale(weight);
      ZCtrlCenJetpT->Scale(weight);
      ZCtrlDEtajj->Scale(weight);
      ZCtrlMjj->Scale(weight);
      ZCtrlMET->Scale(weight);
      ZCtrlDPhijj->Scale(weight);
      ZCtrlYStar->Scale(weight);
      ZCtrlYStarWt->Scale(weight);
      ZCtrlMjjWt->Scale(weight);
      ZCtrlYStarMjjWt->Scale(weight);
    }

    ofile_Plot->cd();
    ZCtrlZMass->Write("",TObject::kOverwrite);
    ZCtrlZpT->Write("",TObject::kOverwrite);
    ZCtrlJet1pT->Write("",TObject::kOverwrite);
    ZCtrlJet1Eta->Write("",TObject::kOverwrite);
    ZCtrlJet2pT->Write("",TObject::kOverwrite);
    ZCtrlJet2Eta->Write("",TObject::kOverwrite);
    ZCtrlCenJetpT->Write("",TObject::kOverwrite);
    ZCtrlDEtajj->Write("",TObject::kOverwrite);
    ZCtrlMjj->Write("",TObject::kOverwrite);
    ZCtrlMET->Write("",TObject::kOverwrite);
    ZCtrlDPhijj->Write("",TObject::kOverwrite);
    ZCtrlYStar->Write("",TObject::kOverwrite);
    ZCtrlYStarWt->Write("",TObject::kOverwrite);
    ZCtrlMjjWt->Write("",TObject::kOverwrite);
    ZCtrlYStarMjjWt->Write("",TObject::kOverwrite);

    ofile_Plot->Close();

    // clean up
    delete tree;
    file->Close();
   
  }

  // re-weighting : will be 1 if re-weighting is off
  TH1D* hZ_DYNorm = new TH1D("hZ_DYNorm", "", 1, 0, 1); 
  hZ_DYNorm->Add(hZ_DY_NoVBFNoWeight);
  hZ_DYNorm->Divide(hZ_DY_NoVBFWeight);
  
  double dyNorm = hZ_DYNorm->GetBinContent(1);

  // numbers - calculate these from MC in this program later!
  double ratioBF = constants::ratioZToNuNuZToLL;

  // efficiencies (these will all have syst uncertainty only)
  TH1D* hZ_QCD_EffVBFS      = new TH1D("hZ_QCD_EffVBFS",  "", 1, 0., 1.);
  TH1D* hZ_QCD_EffVBFC      = new TH1D("hZ_QCD_EffVBFC",  "", 1, 0., 1.);
  TH1D* hZ_EWK_EffVBFS      = new TH1D("hZ_EWK_EffVBFS",  "", 1, 0., 1.);
  TH1D* hZ_EWK_EffVBFC      = new TH1D("hZ_EWK_EffVBFC",  "", 1, 0., 1.);
 
  TH1D* hZ_DY_EffVBFS       = new TH1D("hZ_DY_EffVBFS",  "", 1, 0., 1.);  	// epsilon_s_vbf
  TH1D* hZ_DY_EffVBFC       = new TH1D("hZ_DY_EffVBFC",  "", 1, 0., 1.);        // epsilon_c_vbf
  TH1D* hZ_DY_TotalEff      = new TH1D("hZ_DY_TotalEff", "", 1, 0., 1.); 

  // for stat only calculation
  TH1D* hZ_Eff_S_DPhi       = new TH1D("hZ_Eff_S_DPhi",       "", 3, dphiEdges);
  TH1D* hZ_Est_C_DPhi       = new TH1D("hZ_Est_C_DPhi",       "", 3, dphiEdges);
  TH1D* hZ_Est_S_DPhi       = new TH1D("hZ_Est_S_DPhi",       "", 3, dphiEdges);

  // for syst only calculation
  TH1D* hZ_BG_C_DPhi_Syst   = new TH1D("hZ_BG_C_DPhi_Syst",   "", 3, dphiEdges);
  TH1D* hZ_Data_C_DPhi_Syst = new TH1D("hZ_Data_C_DPhi_Syst", "", 3, dphiEdges);
  TH1D* hZ_Eff_S_DPhi_Syst  = new TH1D("hZ_Eff_S_DPhi_Syst",  "", 3, dphiEdges);
  TH1D* hZ_Est_C_DPhi_Syst  = new TH1D("hZ_Est_C_DPhi_Syst",  "", 3, dphiEdges);
  TH1D* hZ_Est_S_DPhi_Syst  = new TH1D("hZ_Est_S_DPhi_Syst",  "", 3, dphiEdges);

  // calculate efficiencies for QCD and EWK processes
  hZ_QCD_EffVBFS->Add(hZ_QCD_EffVBFS_N);
  hZ_QCD_EffVBFS->Divide(hZ_QCD_EffVBFS_D);
  hZ_EWK_EffVBFS->Add(hZ_EWK_EffVBFS_N);
  hZ_EWK_EffVBFS->Divide(hZ_EWK_EffVBFS_D);

  hZ_QCD_EffVBFC->Add(hZ_QCD_EffVBFC_N);
  hZ_QCD_EffVBFC->Divide(hZ_QCD_EffVBFC_D);
  hZ_EWK_EffVBFC->Add(hZ_EWK_EffVBFC_N);
  hZ_EWK_EffVBFC->Divide(hZ_EWK_EffVBFC_D); 

  // combine QCD Zll and EWK Zll with correct weights
  hZ_DY_EffVBFS->Add(hZ_QCD_EffVBFS,constants::sigma_Zvv_QCD);
  hZ_DY_EffVBFS->Add(hZ_EWK_EffVBFS,constants::sigma_Zvv_EWK);
  hZ_DY_EffVBFC->Add(hZ_QCD_EffVBFC,constants::sigma_Zuu_QCD);
  hZ_DY_EffVBFC->Add(hZ_EWK_EffVBFC,constants::sigma_Zuu_EWK);

  hZ_DY_TotalEff->Add(hZ_DY_EffVBFS);
  hZ_DY_TotalEff->Divide(hZ_DY_EffVBFC);

  for(int ibin = 1; ibin <= hZ_Eff_S_DPhi->GetNbinsX(); ++ibin) {
    hZ_Eff_S_DPhi->SetBinContent(ibin,hZ_DY_TotalEff->GetBinContent(1));
    hZ_Eff_S_DPhi->SetBinError(ibin,0.);
    hZ_Eff_S_DPhi_Syst->SetBinContent(ibin,hZ_DY_TotalEff->GetBinContent(1));
    hZ_Eff_S_DPhi_Syst->SetBinError(ibin,hZ_DY_TotalEff->GetBinError(1));
  }

  // do stat only version
  hZ_BG_C_DPhi_Syst->Add(hZ_BG_C_DPhi, 1.);  // copy MC BG histogram for syst calculation
  for (int i=1; i<=hZ_BG_C_DPhi->GetNbinsX(); ++i) hZ_BG_C_DPhi->SetBinError(i,0.);  // set MC BG errors to zero for stat only
  hZ_Est_C_DPhi->Add(hZ_Data_C_DPhi, hZ_BG_C_DPhi, 1., -1.);
  hZ_Est_S_DPhi->Add(hZ_Est_C_DPhi);
  hZ_Est_S_DPhi->Multiply(hZ_Eff_S_DPhi);

  // do syst only version
  hZ_Data_C_DPhi_Syst->Add(hZ_Data_C_DPhi, 1.);
  for (int i=1; i<=hZ_Data_C_DPhi_Syst->GetNbinsX(); ++i) hZ_Data_C_DPhi_Syst->SetBinError(i,0.);

  hZ_Est_C_DPhi_Syst->Add(hZ_Data_C_DPhi_Syst, hZ_BG_C_DPhi, 1., -1.);
  hZ_Est_S_DPhi_Syst->Add(hZ_Est_C_DPhi_Syst);
  hZ_Est_S_DPhi_Syst->Multiply(hZ_Eff_S_DPhi_Syst);


  std::cout << std::endl;
  std::cout << "##################################### MET > 130 #####################################" << std::endl;
  std::cout << "dphi<1.0" << std::endl;
  std::cout << std::endl;
  std::cout << "  Numerator_s_vbf : " << hZ_QCD_EffVBFS_N->GetBinContent(1) << " +/- " << hZ_QCD_EffVBFS_N->GetBinError(1) << std::endl;
  std::cout << "  Deno_s_vbf      : " << hZ_QCD_EffVBFS_D->GetBinContent(1) << " +/- " << hZ_QCD_EffVBFS_D->GetBinError(1) << std::endl;
  std::cout << "  eps_s_vbf (QCD) : " << hZ_QCD_EffVBFS->GetBinContent(1) << " +/- " << hZ_QCD_EffVBFS->GetBinError(1) << std::endl;
  std::cout << std::endl;
  std::cout << "  Numerator_s_vbf : " << hZ_EWK_EffVBFS_N->GetBinContent(1) << " +/- " << hZ_EWK_EffVBFS_N->GetBinError(1) << std::endl;
  std::cout << "  Deno_s_vbf      : " << hZ_EWK_EffVBFS_D->GetBinContent(1) << " +/- " << hZ_EWK_EffVBFS_D->GetBinError(1) << std::endl;
  std::cout << "  eps_s_vbf (EWK) : " << hZ_EWK_EffVBFS->GetBinContent(1) << " +/- " << hZ_EWK_EffVBFS->GetBinError(1) << std::endl;
  std::cout << std::endl;
  std::cout << "  Numerator_c_vbf : " << hZ_QCD_EffVBFC_N->GetBinContent(1) << " +/- " << hZ_QCD_EffVBFC_N->GetBinError(1) << std::endl;
  std::cout << "  Deno_c_vbf      : " << hZ_QCD_EffVBFC_D->GetBinContent(1) << " +/- " << hZ_QCD_EffVBFC_D->GetBinError(1) << std::endl;
  std::cout << "  eps_c_vbf (QCD) : " << hZ_QCD_EffVBFC->GetBinContent(1) << " +/- " << hZ_QCD_EffVBFC->GetBinError(1) << std::endl;
  std::cout << std::endl;
  std::cout << "  Numerator_c_vbf : " << hZ_EWK_EffVBFC_N->GetBinContent(1) << " +/- " << hZ_EWK_EffVBFC_N->GetBinError(1) << std::endl;
  std::cout << "  Deno_c_vbf      : " << hZ_EWK_EffVBFC_D->GetBinContent(1) << " +/- " << hZ_EWK_EffVBFC_D->GetBinError(1) << std::endl;
  std::cout << "  eps_c_vbf (EWK) : " << hZ_EWK_EffVBFC->GetBinContent(1) << " +/- " << hZ_EWK_EffVBFC->GetBinError(1) << std::endl;
  std::cout << std::endl;
  std::cout << "  eps_s_vbf : " << hZ_DY_EffVBFS->GetBinContent(1) << " +/- " << hZ_DY_EffVBFS->GetBinError(1) << std::endl;
  std::cout << "  eps_c_vbf : " << hZ_DY_EffVBFC->GetBinContent(1) << " +/- " << hZ_DY_EffVBFC->GetBinError(1) << std::endl;
  std::cout << "  total eff : " << hZ_DY_TotalEff->GetBinContent(1) << " +/- " << hZ_DY_TotalEff->GetBinError(1) << std::endl;
  std::cout << std::endl;
  std::cout << "  DY+jets MC ctrl region : " << hZ_DY_C_DPhi->GetBinContent(1) << " +/- " << hZ_DY_C_DPhi->GetBinError(1) << std::endl;
  std::cout << "  Background ctrl region : " << hZ_BG_C_DPhi->GetBinContent(1) << " +/- " << hZ_BG_C_DPhi->GetBinError(1) << std::endl;
  std::cout << "  Data ctrl region       : " << hZ_Data_C_DPhi->GetBinContent(1) << " +/- " << hZ_Data_C_DPhi->GetBinError(1) << std::endl;
  std::cout << std::endl;
  std::cout << "  Z in ctrl region       : " << hZ_Est_C_DPhi->GetBinContent(1) << " +/- " << hZ_Est_C_DPhi->GetBinError(1) << " (stat.) + " << hZ_Est_C_DPhi_Syst->GetBinError(1) << " (syst)" << std::endl;
  std::cout << "  Z in sgnl region       : " << hZ_Est_S_DPhi->GetBinContent(1) << " +/- " << hZ_Est_S_DPhi->GetBinError(1) << " (stat.) + " << hZ_Est_S_DPhi_Syst->GetBinError(1) << " (syst)" << std::endl;
  std::cout << std::endl;
  //std::cout << "  N_DY^No VBF (no y* or mjj weight)  : " << hZ_DY_NoVBFNoWeight->GetBinContent(1) << " +/- " << hZ_DY_NoVBFNoWeight->GetBinError(1) << std::endl; 
  //std::cout << "  N_DY^No VBF(with y* and mjj weight): " << hZ_DY_NoVBFWeight->GetBinContent(1) << " +/- " << hZ_DY_NoVBFWeight->GetBinError(1) << std::endl;
  //std::cout << "  norm eff (unweighted DY, no VBF / weighted DY, no VBF): " << dyNorm << std::endl;
  std::cout << std::endl;
  std::cout << "#####################################################################################" << std::endl;
  std::cout << std::endl << std::endl;

  // write the cutflow table
  std::cout << "Writing cut flow TeX file" << std::endl;

  ofstream effFile;
  effFile.open(options.oDir+std::string("/cutflowZMuMu.tex"));

  effFile << "Cut & N(data) & N(DY\\rightarrow\\ell\\ell) & N($t\\bar{t}$) & N(single $t$) & N(diboson) \\\\" << std::endl;

  TH1D* hZ_CutFlow_TTBar = (TH1D*) ofile->Get("hZ_CutFlow_TTBar");

  // cutflow table
  for (unsigned i=0; i<nCutsZMuMu; ++i) {

    effFile << cuts.cutNameZMuMu(i) << " & ";
    effFile << "$" << hZ_CutFlow_Data->GetBinContent(i+1) << " \\pm " << hZ_CutFlow_Data->GetBinError(i+1) << "$ & ";
    effFile << "$" << hZ_CutFlow_DY->GetBinContent(i+1) << " \\pm " << hZ_CutFlow_DY->GetBinError(i+1) << "$ & ";
    effFile << "$" << hZ_CutFlow_TTBar->GetBinContent(i+1) << " \\pm " << hZ_CutFlow_TTBar->GetBinError(i+1) << "$ & ";
    effFile << "$" << hZ_CutFlow_SingleTSum->GetBinContent(i+1) << " \\pm " << hZ_CutFlow_SingleTSum->GetBinError(i+1) << "$ & ";
    effFile << "$" << hZ_CutFlow_Diboson->GetBinContent(i+1) << " \\pm " << hZ_CutFlow_Diboson->GetBinError(i+1) << "$ & ";
    effFile << std::endl;

  }

  effFile << std::endl << std::endl;
  effFile.close();

  // list histograms for dataset summing
  std::vector<std::string> hists;
  hists.push_back("ZCtrlZMass");
  hists.push_back("ZCtrlZpT");
  hists.push_back("ZCtrlJet1pT");
  hists.push_back("ZCtrlJet1Eta");
  hists.push_back("ZCtrlJet2pT");
  hists.push_back("ZCtrlJet2Eta");
  hists.push_back("ZCtrlCenJetpT");
  hists.push_back("ZCtrlDEtajj");
  hists.push_back("ZCtrlMjj");
  hists.push_back("ZCtrlMET");
  hists.push_back("ZCtrlDPhijj");

  // check whether we have NoTrig histograms or normal
  bool noTrig = false;
  std::string dyJetsName = oDir_Plot+std::string("/DYJetsToLL.root");
  std::string dyJetsPtZName = oDir_Plot+std::string("/DYJetsToLL_PtZ-100.root");
  struct stat buffer; 
  if (stat (dyJetsName.c_str(), &buffer) != 0) {
    noTrig = true;
    dyJetsName = oDir_Plot+std::string("/DYJetsToLL_NoTrig.root");
    dyJetsPtZName = oDir_Plot+std::string("/DYJetsToLL_PtZ-100_NoTrig.root");
  }

  std::cout << "Getting histograms for plots from " << dyJetsName << " and " << dyJetsPtZName << std::endl;

  // re-scale QCD DY histograms
  TFile* qcdDYFile = TFile::Open(dyJetsName.c_str(), "UPDATE");
  for (std::vector<std::string>::const_iterator hname=hists.begin(); hname!=hists.end(); ++hname) {
    TH1D* h = (TH1D*) qcdDYFile->Get(hname->c_str());
    //std::cout << "Integral before : " << h->Integral() << std::endl;
    h->Scale(dyNorm);
    //std::cout << "Integral after : " << h->Integral() << std::endl;
    h->Write("",TObject::kOverwrite);
  }
  qcdDYFile->Close();

  qcdDYFile = TFile::Open(dyJetsPtZName.c_str(), "UPDATE");
  for (std::vector<std::string>::const_iterator hname=hists.begin(); hname!=hists.end(); ++hname) {
    TH1D* h = (TH1D*) qcdDYFile->Get(hname->c_str());
    h->Scale(dyNorm);
    h->Write("",TObject::kOverwrite);
  }
  qcdDYFile->Close();


  // sum DY datasets
  std::vector<std::string> DYDatasets;
  if (noTrig) {
    DYDatasets.push_back(std::string("DYJetsToLL_NoTrig"));
    DYDatasets.push_back(std::string("DYJetsToLL_PtZ-100_NoTrig"));
    DYDatasets.push_back(std::string("DYJetsToLL_EWK_NoTrig"));
  }
  else {
    DYDatasets.push_back(std::string("DYJetsToLL"));
    DYDatasets.push_back(std::string("DYJetsToLL_PtZ-100"));
    DYDatasets.push_back(std::string("DYJetsToLL_EWK"));
  }
  SumDatasets(oDir_Plot, DYDatasets, hists, "DY+jets");

  // sum single top datasets
  std::vector<std::string> topDatasets;
  topDatasets.push_back(std::string("SingleT_t"));
  topDatasets.push_back(std::string("SingleTbar_t"));
  topDatasets.push_back(std::string("SingleT_s"));
  topDatasets.push_back(std::string("SingleTbar_s"));
  topDatasets.push_back(std::string("SingleT_tW"));
  topDatasets.push_back(std::string("SingleTbar_tW"));
  topDatasets.push_back(std::string("TTBar"));
  SumDatasets(oDir_Plot, topDatasets, hists, "SingleT+TTbar"); 

  // sum diboson datasets
  std::vector<std::string> dibDatasets;
  dibDatasets.push_back(std::string("WW"));
  dibDatasets.push_back(std::string("WZ"));
  dibDatasets.push_back(std::string("ZZ"));
  dibDatasets.push_back(std::string("WG"));
  SumDatasets(oDir_Plot, dibDatasets, hists, "DiBoson");

  // sum SM backgrounds
  std::vector<std::string> bgDatasets;
  bgDatasets.push_back(std::string("WW"));
  bgDatasets.push_back(std::string("WZ"));
  bgDatasets.push_back(std::string("ZZ"));
  bgDatasets.push_back(std::string("WG"));
  bgDatasets.push_back(std::string("SingleT_t"));
  bgDatasets.push_back(std::string("SingleTbar_t"));
  bgDatasets.push_back(std::string("SingleT_s"));
  bgDatasets.push_back(std::string("SingleTbar_s"));
  bgDatasets.push_back(std::string("SingleT_tW"));
  bgDatasets.push_back(std::string("SingleTbar_tW"));
  bgDatasets.push_back(std::string("TTBar"));
  SumDatasets(oDir_Plot, bgDatasets, hists, "tt+VV"); 

  // make plots
  std::cout << "Making plots" << std::endl;
  StackPlot plots(oDir_Plot);
  plots.setLegPos(0.70,0.60,0.93,0.89);
  //plots.setLegPos(0.62,0.62,0.89,0.89);

  // Note that here I've used the overloaded method addDataset, which allows you to specify a filename for the input ROOT file
  // *and* also allows you to specify what to put in the TLegend. (By default, it uses the filename as the TLegend entry)
  // This is because the Tlegend entry involves some odd characters, which are best not used in filenames for safety

  //  plots.addDataset("DiBoson", kViolet-6, 0);
  plots.addDataset("tt+VV", "t#bar{t}, tW, VV", kAzure-2, 0);
  plots.addDataset("DY+jets", "DY(ll)+jets", kPink-4, 0);
  plots.addDataset("METABCD", kBlack, 1);
  plots.setScaleMCtoData(true); //rescale MC to data

  plots.draw("ZCtrlZpT",	"Z_p_{T} [GeV]",		"N_{events}"	,1,"RATIO_Z");
  plots.draw("ZCtrlJet1pT", 	"Leading jet p_{T} [GeV]", 	"N_{events}"	,1,"RATIO_Z");
  plots.draw("ZCtrlJet1Eta", 	"Leading jet #eta", 		"N_{events}"	,1,"RATIO_Z");
  plots.draw("ZCtrlJet2pT",     "Sub-leading jet p_{T} [GeV]",  "N_{events}"	,1,"RATIO_Z");
  plots.draw("ZCtrlJet2Eta",    "Sub-leading jet #eta",         "N_{events}"	,1,"RATIO_Z");
  plots.draw("ZCtrlCenJetpT",	"Central jet p_{T} [GeV]",	"N_{events}"    ,1,"RATIO_Z");
  plots.draw("ZCtrlDEtajj",	"#Delta #eta_{jj}",		"N_{events}"	,1,"RATIO_Z");
  plots.setYMax(5.e2);
  plots.setXMin(1100.);
  plots.setXMax(2600.);
  plots.draw("ZCtrlMjj", 	"M_{jj} [GeV]",			"Events / 100 GeV"	,1,"RATIO_Z");
  plots.setYMax(5.e2);
  plots.setXMin(130.);
  plots.draw("ZCtrlMET", 	"E_{T}^{miss} [GeV]",		"Events / 20 GeV"	,1,"RATIO_Z");
  plots.draw("ZCtrlDPhijj", 	"#Delta #phi_{jj}",		"Events"	,1,"RATIO_Z");
  plots.setYMax(80.);
  plots.setYMin(0.);
  plots.draw("ZCtrlZMass",      "M_{#mu#mu}  [GeV]",      "Events / 5 GeV"    ,0,"RATIO_Z");

  //store histograms
  ofile->cd();
  hZ_DY_C_DPhi->Write("",TObject::kOverwrite);
  hZ_BG_C_DPhi->Write("",TObject::kOverwrite);
  hZ_Data_C_DPhi->Write("",TObject::kOverwrite);
  hZ_Est_C_DPhi->Write("",TObject::kOverwrite);
  hZ_Est_C_DPhi_Syst->Write("",TObject::kOverwrite);
  hZ_Est_S_DPhi->Write("",TObject::kOverwrite);
  hZ_Est_S_DPhi_Syst->Write("",TObject::kOverwrite);

  hZ_Eff_S_DPhi->Write("",TObject::kOverwrite);
  hZ_Eff_S_DPhi_Syst->Write("",TObject::kOverwrite);
  hZ_QCD_EffVBFS_D->Write("",TObject::kOverwrite);
  hZ_QCD_EffVBFS_N->Write("",TObject::kOverwrite);
  hZ_QCD_EffVBFC_D->Write("",TObject::kOverwrite);
  hZ_QCD_EffVBFC_N->Write("",TObject::kOverwrite);
  hZ_EWK_EffVBFS_D->Write("",TObject::kOverwrite);
  hZ_EWK_EffVBFS_N->Write("",TObject::kOverwrite);
  hZ_EWK_EffVBFC_D->Write("",TObject::kOverwrite);
  hZ_EWK_EffVBFC_N->Write("",TObject::kOverwrite);
  hZ_DY_EffVBFS->Write("",TObject::kOverwrite);
  hZ_DY_EffVBFC->Write("",TObject::kOverwrite);
  hZ_DY_TotalEff->Write("",TObject::kOverwrite);

  hZ_CutFlow_Data->Write("",TObject::kOverwrite);
  hZ_CutFlow_DY->Write("",TObject::kOverwrite);
  hZ_CutFlow_SingleTSum->Write("",TObject::kOverwrite);
  hZ_CutFlow_Diboson->Write("",TObject::kOverwrite);

  ofile->Close();    

}
Exemple #5
0
  TCut weight("xsecsusy * (1000./50000.) * 9.708 * sltrigweight");

  TCut presel;
  presel += rho;
  presel += goodlep;
  presel += (goodel||goodmu);
  presel += deltapt;
  presel += iso5;
  presel += njets4;
  presel += btag1;
  presel += isotrk;

  TCut   SR[7];
  string SRname[7]={"SRA","SRB","SRC","SRD","SRE","SRF","SRG"};

  SR[0]=TCut("t1metphicorrmt > 150 && t1metphicorr > 100");
  SR[1]=TCut("t1metphicorrmt > 120 && t1metphicorr > 150");
  SR[2]=TCut("t1metphicorrmt > 120 && t1metphicorr > 200");
  SR[3]=TCut("t1metphicorrmt > 120 && t1metphicorr > 250");
  SR[4]=TCut("t1metphicorrmt > 120 && t1metphicorr > 300");
  SR[5]=TCut("t1metphicorrmt > 120 && t1metphicorr > 350");
  SR[6]=TCut("t1metphicorrmt > 120 && t1metphicorr > 400");

  presel += SR[0];
  //presel += sig_150_0;
  presel += sig_150_10;

  //  TCanvas *ctemp = new TCanvas();

  TChain *scan = new TChain("t");
  // scan->Add("/tas/benhoob/testFiles/T2bw_8TeV/merged.root");
Exemple #6
0
void weightPbPbFONLL(int minfit=2,int maxfit=100,TString pthat="pthatall")
{
  TString label;
  TString selmcgen="((GisSignal==1||GisSignal==2)&&(Gy>-1&&Gy<1))";
  TString myweightfunctiongen,myweightfunctionreco;
  
  //TCut weighpthat="pow(10,-0.075415*Gpt+1.748668+Gpt*Gpt*0.000388)+pow(10,-0.166406*Gpt+2.887856+Gpt*Gpt*0.000105) +0.003157";
  TCut weighpthat="1";

  gStyle->SetOptTitle(0);
  gStyle->SetOptStat(0);
  gStyle->SetEndErrorSize(0);
  gStyle->SetMarkerStyle(20);
 
  TFile*infMC=new TFile("/data/HeavyFlavourRun2/MC2015/Dntuple/PbPb/ntD_EvtBase_20160513_DfinderMC_PbPb_20160502_dPt1tkPt0p5_D0_prompt_Dpt2Dy1p1tkPt0p7tkEta2Decay2p9Dalpha0p14Skim_pthatweight.root");
  TTree* ntGen = (TTree*)infMC->Get("ntGen");
  TTree *ntHiMC = (TTree*)infMC->Get("ntHi");
  ntGen->AddFriend(ntHiMC);
  
  TH1D* hPtGenFONLL = new TH1D("hPtGenFONLL","",nBinsReweight,ptBinsReweight);
  ntGen->Project("hPtGenFONLL","Gpt",(TCut(weighpthat)*TCut(selmcgen.Data())));
  divideBinWidth(hPtGenFONLL);
    
  TString fonll="/afs/cern.ch/work/g/ginnocen/public/output_pp_d0meson_5TeV_y1.root";
  TFile* filePPReference = new TFile(fonll.Data());  
  TGraphAsymmErrors* gaeBplusReference = (TGraphAsymmErrors*)filePPReference->Get("gaeSigmaDzero");

  TH1D* hFONLL = new TH1D("hFONLL","",nBinsReweight,ptBinsReweight);
  double x,y;
  for(int i=0;i<nBinsReweight;i++){
    gaeBplusReference->GetPoint(i,x,y);
    hFONLL->SetBinContent(i+1,y);
  }
  TH1D* hFONLLOverPt=(TH1D*)hFONLL->Clone("hFONLLOverPt");
  TH1D* hFONLLOverPtWeight=(TH1D*)hFONLL->Clone("hFONLLOverPtWeight");

  hFONLLOverPt->Divide(hPtGenFONLL);

/*
  TF1 *myfit = new TF1("myfit","[0]+[1]*x+x*x*[2]+x*x*x*[3]+x*x*x*x*[4]+x*x*x*x*x*[5]",0, 100);  
  hFONLLOverPt->Fit("myfit","","",0,100);

  double par0=myfit->GetParameter(0);
  double par1=myfit->GetParameter(1);
  double par2=myfit->GetParameter(2);
  double par3=myfit->GetParameter(3);
  double par4=myfit->GetParameter(4);
  double par5=myfit->GetParameter(5);

  std::cout<<"weight="<<par0<<"+Gpt*("<<par1<<")+Gpt*Gpt*("<<par2<<")+Gpt*Gpt*Gpt*("<<par3<<")"<<"+Gpt*Gpt*Gpt*Gpt*("<<par4<<")+Gpt*Gpt*Gpt*Gpt*Gpt*("<<par5<<")"<<endl;
  std::cout<<"weight="<<par0<<"+Dgenpt*("<<par1<<")+Dgenpt*Dgenpt*("<<par2<<")+Dgenpt*Dgenpt*Dgenpt*("<<par3<<")"<<"+Dgenpt*Dgenpt*Dgenpt*Dgenpt*("<<par4<<")+Dgenpt*Dgenpt*Dgenpt*Dgenpt*Dgenpt*("<<par5<<")"<<endl;
  
  std::cout<<myweightfunctiongen<<std::endl;
  std::cout<<myweightfunctionreco<<std::endl;
  std::cout<<"fit function parameters="<<weightfunctiongen<<std::endl;
*/
    TF1 *myfit = new TF1("myfit","pow(10,[0]*x+[1]+x*x*[2])+pow(10,[3]*x+[4]+x*x*[5])+[6]", 2, 100);
    hFONLLOverPt->Fit("myfit","","",minfit,maxfit);
    double par0=myfit->GetParameter(0);
    double par1=myfit->GetParameter(1);
    double par2=myfit->GetParameter(2);
    double par3=myfit->GetParameter(3);
    double par4=myfit->GetParameter(4);
    double par5=myfit->GetParameter(5);
    double par6=myfit->GetParameter(6);

   myweightfunctiongen=Form("pow(10,%f*Gpt+%f+Gpt*Gpt*%f)+pow(10,%f*Gpt+%f+Gpt*Gpt*%f)+%f",par0,par1,par2,par3,par4,par5,par6);
   myweightfunctionreco=Form("pow(10,%f*Gpt+%f+Gpt*Gpt*%f)+pow(10,%f*Gpt+%f+Gpt*Gpt*%f)+%f",par0,par1,par2,par3,par4,par5,par6);
   std::cout<<myweightfunctiongen<<std::endl;
   std::cout<<myweightfunctionreco<<std::endl;

  TCanvas*canvasPtReweight=new TCanvas("canvasPtReweight","canvasPtReweight",1000.,500.); 
  canvasPtReweight->Divide(3,1);
  canvasPtReweight->cd(1);
  gPad->SetLogy();
  hPtGenFONLL->SetXTitle("Gen p_{T}");
  hPtGenFONLL->SetYTitle("#entries");
  hPtGenFONLL->SetMinimum(1e-4);  
  hPtGenFONLL->SetMaximum(1e11);  
  hPtGenFONLL->GetYaxis()->SetTitleOffset(1.4);
  hPtGenFONLL->Draw();
  canvasPtReweight->cd(2);
  gPad->SetLogy();
  hFONLL->SetXTitle("p_{T}");
  hFONLL->SetYTitle("FONLL, #entries");
  hFONLL->SetMinimum(1e-4);  
  hFONLL->SetMaximum(1e11);  
  hFONLL->GetYaxis()->SetTitleOffset(1.4);
  hFONLL->Draw();
  canvasPtReweight->cd(3);
  hFONLLOverPt->SetXTitle("Gen p_{T}");
  hFONLLOverPt->SetYTitle("FONLL/PYTHIA ");
  hFONLLOverPt->SetMinimum(0.01);  
  hFONLLOverPt->SetMaximum(10.);  
  hFONLLOverPt->GetYaxis()->SetTitleOffset(1.4);
  hFONLLOverPt->Draw();

}
void plotTrigger_PbPb(char *infname="/data/jisun/PbPb2015/skim_Dntuple_crab_PbPb_HIMinimumBias2_ForestAOD_Track_AK4CaloJet_D0_tkpt0p9eta1p5_01142016_tkpt8p0fortrigstudy.root")
{

   // ============== Open file and basic settings ===============   
   // Open Dntuple file
   TFile *inf = new TFile(infname);

   TTree *ntDkpi = (TTree*)inf->Get("ntDkpi");
   TTree *ntHlt = (TTree*)inf->Get("ntHlt");
   TTree *ntSkim = (TTree*)inf->Get("ntSkim");
   
   ntDkpi->AddFriend(ntHlt);
   ntDkpi->AddFriend(ntSkim);   

   // Define bin size and bin width for trigger turnon curve histograms
   const int nBin = 15;
   Float_t bins[nBin+1]={0,5,10,15,18,20,25,30,35,40,45,55,60,65,70,80};
 
   // Templates for plotting  
   TH1D *hTmp = new TH1D ("hTmp","",nBin,bins);
   TH1D *hTmp2 = new TH1D ("hTmp2","",nBin,bins);
   
   // ============== Selection Criteria ===============

   //TCut mbCut = "(HLT_HIL1MinimumBiasHF1AND_v1||  \
                  HLT_HIL1MinimumBiasHF2AND_v1||  \
                  HLT_HIL1MinimumBiasHF2AND_part1_v1)";
   TCut mbCut = "(HLT_HIL1MinimumBiasHF1AND_v1)";
   
   // L1 trigger thresholds
   TCut l1CutMBHF1And = "L1_MinimumBiasHF1_AND==1";
   TCut l1Cut28 = "L1_SingleS1Jet28_BptxAND==1";
   TCut l1Cut44 = "L1_SingleJet44_BptxAND==1";

   // D meson selection
   TCut DmassCut             = "(abs(Dmass-1.8696)<0.03)";
   TCut DmesonCut            = "(DsvpvDistance/DsvpvDisErr)>3.5&&Dchi2cl>0.05&&Dalpha<0.12";
   //TCut DmesonDaughterTrkCut = "Dtrk1Pt>8.5&&abs(Dtrk1Eta)<2.0&&Dtrk1Algo<8&&(Dtrk1PixelHit+Dtrk1StripHit)>=10.5&&Dtrk1PtErr/Dtrk1Pt<0.1&& \
                                Dtrk2Pt>8.5&&abs(Dtrk2Eta)<2.0&&Dtrk2Algo<8&&(Dtrk2PixelHit+Dtrk2StripHit)>=10.5&&Dtrk2PtErr/Dtrk2Pt<0.1";
   //TCut DmesonDaughterTrkCut = "Dtrk1Pt>8.5&&abs(Dtrk1Eta)<2.0&&(Dtrk1Algo<8||Dtrk1Algo==11)&&(Dtrk1PixelHit+Dtrk1StripHit)>=10.5&&Dtrk1PtErr/Dtrk1Pt<0.1&& \
                                Dtrk2Pt>8.5&&abs(Dtrk2Eta)<2.0&&(Dtrk2Algo<8||Dtrk2Algo==11)&&(Dtrk2PixelHit+Dtrk2StripHit)>=10.5&&Dtrk2PtErr/Dtrk2Pt<0.1";
   TCut DmesonDaughterTrkCut = "Dtrk1Pt>8.5&&abs(Dtrk1Eta)<2.0&&(Dtrk1PixelHit+Dtrk1StripHit)>=10.5&&Dtrk1PtErr/Dtrk1Pt<0.1&& \
                                Dtrk2Pt>8.5&&abs(Dtrk2Eta)<2.0&&(Dtrk2PixelHit+Dtrk2StripHit)>=10.5&&Dtrk2PtErr/Dtrk2Pt<0.1";

   // Final selection for D candidates for trigger turnon studies
   TCut DAnaCut = DmassCut && DmesonCut && DmesonDaughterTrkCut;

   // HLT trigger thresholds
   TCut HLTCut20  = "HLT_HIDmesonHITrackingGlobal_Dpt20_v1";
   TCut HLTCut40 = "HLT_HIDmesonHITrackingGlobal_Dpt40_v1";
   TCut HLTCut60 = "HLT_HIDmesonHITrackingGlobal_Dpt60_v1";

   // ============== L1 trigger efficiency study ===============
   TCanvas *c = new TCanvas("c","",600,600);
   
   TGraphAsymmErrors* g20  = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&mbCut&&l1CutMBHF1And), HLTCut20, nBin, bins);
   TGraphAsymmErrors* g40 = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&mbCut&&l1Cut28), HLTCut40, nBin, bins);
   TGraphAsymmErrors* g60 = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&mbCut&&l1Cut44), HLTCut60, nBin, bins);

   hTmp->Draw();
   hTmp->SetXTitle("D Meson p_{T} (GeV/c)");
   hTmp->SetYTitle("D Meson HLT Efficiency");
   g20->SetLineColor(1);
   g20->SetMarkerColor(1);
   g20->Draw("pl same");
   
   g40->SetLineColor(2);
   g40->SetMarkerColor(2);
   g40->Draw("pl same");

   g60->SetLineColor(kGreen+2);
   g60->SetMarkerColor(kGreen+2);
   g60->Draw("pl same");

   TLegend *leg = new TLegend(0.53,0.2,0.93,0.6);
   leg->SetBorderSize(0);
   leg->SetFillStyle(0);
   leg->AddEntry(g20,"PbPb #sqrt{s} = 5.02 TeV","");
   leg->AddEntry(g20,"HLT D meson 20","pl");
   leg->AddEntry(g40,"HLT D meson 40","pl");
   leg->AddEntry(g60,"HLT D meson 60","pl");
   leg->Draw();

   c->SaveAs("result/Dmeson-HIHLTriggerEfficiency.pdf");
   c->SaveAs("result/Dmeson-HIHLTriggerEfficiency.png");
   c->SaveAs("result/Dmeson-HIHLTriggerEfficiency.C");

   
   // ============== L1 trigger efficiency study ===============
   TCanvas *c2 = new TCanvas("c2","",600,600);
   
   TGraphAsymmErrors* gLMBHF1And = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&mbCut&&"L1_MinimumBiasHF1_AND_Prescl==1"), l1CutMBHF1And, nBin, bins);
   TGraphAsymmErrors* gL28 = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&mbCut&&"L1_SingleS1Jet28_BptxAND_Prescl==1"), l1Cut28, nBin, bins);
   TGraphAsymmErrors* gL44 = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&mbCut&&"L1_SingleJet44_BptxAND_Prescl==1"), l1Cut44, nBin, bins);
   
   hTmp2->Draw();
   hTmp2->SetXTitle("D meson p_{T} (GeV/c)");
   hTmp2->SetYTitle("L1 Trigger Efficiency");
   
   gLMBHF1And->SetMarkerColor(1);
   gLMBHF1And->SetLineColor(1);
   gLMBHF1And->Draw("pl same");

   gL28->SetMarkerColor(4);
   gL28->SetLineColor(4);
   gL28->Draw("pl same");

   gL44->SetMarkerColor(kGreen+2);
   gL44->SetLineColor(kGreen+2);
   gL44->Draw("pl same");

   TLegend *leg2 = new TLegend(0.53,0.2,0.93,0.6);
   leg2->SetBorderSize(0);
   leg2->SetFillStyle(0);
   leg2->AddEntry(gLMBHF1And,"PbPb #sqrt{s} = 5.02 TeV","");
   leg2->AddEntry(gLMBHF1And,"Level 1 Jet 16","pl");
   leg2->AddEntry(gL28,"Level 1 Jet 28","pl");
   leg2->AddEntry(gL44,"Level 1 Jet 44","pl");
   leg2->Draw();

   c2->SaveAs("result/Dmeson-HIL1TriggerEfficiency.pdf");
   c2->SaveAs("result/Dmeson-HIL1TriggerEfficiency.png");
   c2->SaveAs("result/Dmeson-HIL1TriggerEfficiency.C");
   
   
   // ============== Plot an example D mass distribution ===============
   TCanvas *c3 = new TCanvas("c3","",600,600);
   ntDkpi->Draw("Dmass>>h(100,1.7696,1.9696)",DmesonCut&&DmesonDaughterTrkCut&&mbCut&&l1CutMBHF1And);
   
   // ..done 
}
Exemple #8
0
void trackingStudyTrkEta(char *infname = "skim_jet_pthat80Sim05.root")
{
   HiForest *c = new HiForest(infname);
   c->tree->AddFriend("nt");

   TCut trackSelection = "(mtrkQual||mtrkAlgo<4)&&pNRec>0";
   TCut genParticleCut = "abs(pEta)<2.8&&pPt>0";

   TCut weightCent = "1";//"(0.64661/(0.64661+0.00279303*hiBin+(-3.89902e-5)*hiBin*hiBin)*(hiBin<34)+(hiBin>=34)*0.64661/(-1.38822+0.123182*hiBin-0.0018135*hiBin*hiBin))";

   TCut weight = "1";//"1./(0.757356-0.030163*pEta-0.0769554*pEta**2+0.0176546*pEta**3+0.0200791*pEta**4-0.00256533*pEta**5-0.00304287*pEta**6)";    
   TCanvas *cpPt = new TCanvas ("cpPt","",600,600);
   
   TH1F *hEffpPt = new TH1F("hEffpPt","",28,-2.8,2.8);
   
   makeHistTitle(hEffpPt,"","SimTrack #eta","Tracking Efficiency");
   
   TGraphAsymmErrors *g = getEfficiency(c->tree,"pEta",28,-2.8,2.8,trackSelection,TCut("pPt>0.5")&&genParticleCut,weight*weightCent);
   TGraphAsymmErrors *g1 = getEfficiency(c->tree,"pEta",28,-2.8,2.8,trackSelection,TCut("pPt>0.5&&pPt<1")&&genParticleCut,weight*weightCent);
   TGraphAsymmErrors *g2 = getEfficiency(c->tree,"pEta",28,-2.8,2.8,trackSelection,TCut("pPt>1&&pPt<2")&&genParticleCut,weight*weightCent);
   TGraphAsymmErrors *g3 = getEfficiency(c->tree,"pEta",28,-2.8,2.8,trackSelection,TCut("pPt>2&&pPt<4")&&genParticleCut,weight*weightCent);
   TGraphAsymmErrors *g4 = getEfficiency(c->tree,"pEta",28,-2.8,2.8,trackSelection,TCut("pPt>4&&pPt<8")&&genParticleCut,weight*weightCent);
   TGraphAsymmErrors *g5 = getEfficiency(c->tree,"pEta",28,-2.8,2.8,trackSelection,TCut("pPt>8")&&genParticleCut,weight*weightCent);

   hEffpPt->SetAxisRange(0.,1,"Y");
   hEffpPt->Draw();
   
   g->Draw("p same");
   g1->SetLineColor(2);
   g1->SetMarkerColor(2);
   g1->Draw("p same");
   g2->SetLineColor(3);
   g2->SetMarkerColor(3);
   g2->Draw("p same");
   
   g3->SetLineColor(4);
   g3->SetMarkerColor(4);
   g3->Draw("p same");

   g4->SetLineColor(46);
   g4->SetMarkerColor(46);
   g4->Draw("p same");

   g5->SetLineColor(kOrange);
   g5->SetMarkerColor(kOrange);
   g5->Draw("p same");

   TLegend *legpPt = myLegend(0.2,0.7,0.6,0.9);
   legpPt->AddEntry(g,"#hat{p}_{T} 80 GeV/c, No selection","");
   legpPt->AddEntry(g,"Simtrack p_{T} > 0.5 GeV/c","pl");
   legpPt->AddEntry(g1,"Simtrack 0.5 > p_{T} > 1 GeV/c","pl");
   legpPt->AddEntry(g2,"Simtrack 1 > p_{T} > 2 GeV/c","pl");
   legpPt->AddEntry(g3,"Simtrack 2 > p_{T} > 4 GeV/c","pl");
   legpPt->AddEntry(g4,"Simtrack 4 > p_{T} > 8 GeV/c","pl");
   legpPt->AddEntry(g5,"Simtrack p_{T} > 8 GeV/c","pl");
   
   legpPt->Draw();
   cpPt->SaveAs("effFig/EffVsTrackPt.gif");
   cpPt->SaveAs("effFig/EffVsTrackPt.C");

   g->Fit("pol6")   ;
}
void CutCountTP_truth(){

/////// Are you running over data or MC ???
   TCut preCut("");
   TCut ID95("probe_passingId && probe_sc_et>20. && probe_gsfEle_ecalDrivenSeed>0 && probe_gsfEle_missingHits<=1");
   TCut NotID95("probe_sc_et>20. && probe_gsfEle_ecalDrivenSeed>0 && !(probe_passingId && probe_gsfEle_missingHits<=1)");
   TCut NotPPass("!probe_passing");
   TCut PassAll("probe_passingALL");
   TCut NotPassAll("!probe_passingALL");
   TCut ID80("probe_passingId80 && probe_sc_et>20. && probe_gsfEle_ecalDrivenSeed>0 && probe_gsfEle_missingHits<1 && !(abs(probe_gsfEle_dist)<0.02 && abs(probe_gsfEle_dcot)<0.02)");
   TCut NotID80("probe_sc_et>20.  && probe_gsfEle_ecalDrivenSeed>0 && !(probe_passingId80 && probe_gsfEle_missingHits<1 && !(abs(probe_gsfEle_dist)<0.02 && abs(probe_gsfEle_dcot)<0.02))");
   TCut EMINUS("probe_gsfEle_charge<0");
   TCut EMINUSSC("");
   TCut EPLUS("probe_gsfEle_charge>0");
   TCut EPLUSSC("");
   TCut BARREL("abs(probe_sc_eta)<1.4442");
   TCut BARRELSC("abs(probe_eta)<1.4442");
   TCut ENDCAPS("abs(probe_sc_eta)>1.566");
   TCut ENDCAPSSC("abs(probe_eta)>1.566");


// //////////////////////////////////////////////////////////
   cout << "probe type" << "         efficiency " << "       Npass" <<  
      "       Nfail" << endl;
// //////////////////////////////////////////////////////////



// //////////////////////////////////////////////////////////
//   //  Super cluster --> gsfElectron efficiency
// //////////////////////////////////////////////////////////

   TCut GsfPass = preCut && TCut("probe_sc_et>20. && probe_gsfEle_ecalDrivenSeed>0");
   TCut GsfFail = preCut && NotPPass && TCut("probe_et>20.");
   ComputeEfficiency("Gsf", GsfPass, GsfFail);
   ComputeEfficiency("Gsf", GsfPass, GsfFail, "Barrel", BARREL, BARRELSC);
   ComputeEfficiency("Gsf", GsfPass, GsfFail, "Endcap", ENDCAPS, ENDCAPSSC);
   ComputeEfficiency("Gsf", GsfPass, GsfFail, "", "", "", "_eminus", EMINUS, EMINUSSC);
   ComputeEfficiency("Gsf", GsfPass, GsfFail, "", "", "", "_eplus", EPLUS, EPLUSSC);
   ComputeEfficiency("Gsf", GsfPass, GsfFail, "Barrel", BARREL, BARRELSC, "_eminus", EMINUS, EMINUSSC);
   ComputeEfficiency("Gsf", GsfPass, GsfFail, "Barrel",  BARREL, BARRELSC, "_eplus", EPLUS, EPLUSSC);
   ComputeEfficiency("Gsf", GsfPass, GsfFail, "Endcap", ENDCAPS, ENDCAPSSC, "_eminus", EMINUS, EMINUSSC);
   ComputeEfficiency("Gsf", GsfPass, GsfFail, "Endcap", ENDCAPS, ENDCAPSSC, "_eplus", EPLUS, EPLUSSC);
   cout << "########################################" << endl;


// //////////////////////////////////////////////////////////
//   //  gsfElectron --> WP-95 selection efficiency
// //////////////////////////////////////////////////////////


   TCut Id95Pass = preCut && ID95;
   TCut Id95Fail = preCut && NotID95;
   ComputeEfficiency("Id95", Id95Pass, Id95Fail);
   ComputeEfficiency("Id95", Id95Pass, Id95Fail, "Barrel", BARREL, BARREL);
   ComputeEfficiency("Id95", Id95Pass, Id95Fail, "Endcap", ENDCAPS, ENDCAPS);
   ComputeEfficiency("Id95", Id95Pass, Id95Fail, "", "", "", "_eminus", EMINUS, EMINUS);
   ComputeEfficiency("Id95", Id95Pass, Id95Fail, "", "", "", "_eplus", EPLUS, EPLUS);
   ComputeEfficiency("Id95", Id95Pass, Id95Fail, "Barrel", BARREL, BARREL, "_eminus", EMINUS, EMINUS);
   ComputeEfficiency("Id95", Id95Pass, Id95Fail, "Barrel",  BARREL, BARREL, "_eplus", EPLUS, EPLUS);
   ComputeEfficiency("Id95", Id95Pass, Id95Fail, "Endcap", ENDCAPS, ENDCAPS, "_eminus", EMINUS, EMINUS);
   ComputeEfficiency("Id95", Id95Pass, Id95Fail, "Endcap", ENDCAPS, ENDCAPS, "_eplus", EPLUS, EPLUS);
   cout << "########################################" << endl;




// //////////////////////////////////////////////////////////
//   //  gsfElectron --> WP-80 selection efficiency
// //////////////////////////////////////////////////////////


   TCut Id80Pass = preCut && ID80;
   TCut Id80Fail = preCut && NotID80;
   ComputeEfficiency("Id80", Id80Pass, Id80Fail);
   ComputeEfficiency("Id80", Id80Pass, Id80Fail, "Barrel", BARREL, BARREL);
   ComputeEfficiency("Id80", Id80Pass, Id80Fail, "Endcap", ENDCAPS, ENDCAPS);
   ComputeEfficiency("Id80", Id80Pass, Id80Fail, "", "", "", "_eminus", EMINUS, EMINUS);
   ComputeEfficiency("Id80", Id80Pass, Id80Fail, "", "", "", "_eplus", EPLUS, EPLUS);
   ComputeEfficiency("Id80", Id80Pass, Id80Fail, "Barrel", BARREL, BARREL, "_eminus", EMINUS, EMINUS);
   ComputeEfficiency("Id80", Id80Pass, Id80Fail, "Barrel",  BARREL, BARREL, "_eplus", EPLUS, EPLUS);
   ComputeEfficiency("Id80", Id80Pass, Id80Fail, "Endcap", ENDCAPS, ENDCAPS, "_eminus", EMINUS, EMINUS);
   ComputeEfficiency("Id80", Id80Pass, Id80Fail, "Endcap", ENDCAPS, ENDCAPS, "_eplus", EPLUS, EPLUS);
   cout << "########################################" << endl;



// //////////////////////////////////////////////////////////
//   //   WP-95 --> HLT triggering efficiency
// //////////////////////////////////////////////////////////


   TCut HLT95Pass = preCut && ID95 && PassAll;
   TCut HLT95Fail = preCut && ID95 && NotPassAll;
   ComputeEfficiency("HLT95", HLT95Pass, HLT95Fail);
   ComputeEfficiency("HLT95", HLT95Pass, HLT95Fail, "Barrel", BARREL, BARREL);
   ComputeEfficiency("HLT95", HLT95Pass, HLT95Fail, "Endcap", ENDCAPS, ENDCAPS);
   ComputeEfficiency("HLT95", HLT95Pass, HLT95Fail, "", "", "", "_eminus", EMINUS, EMINUS);
   ComputeEfficiency("HLT95", HLT95Pass, HLT95Fail, "", "", "", "_eplus", EPLUS, EPLUS);
   ComputeEfficiency("HLT95", HLT95Pass, HLT95Fail, "Barrel", BARREL, BARREL, "_eminus", EMINUS, EMINUS);
   ComputeEfficiency("HLT95", HLT95Pass, HLT95Fail, "Barrel",  BARREL, BARREL, "_eplus", EPLUS, EPLUS);
   ComputeEfficiency("HLT95", HLT95Pass, HLT95Fail, "Endcap", ENDCAPS, ENDCAPS, "_eminus", EMINUS, EMINUS);
   ComputeEfficiency("HLT95", HLT95Pass, HLT95Fail, "Endcap", ENDCAPS, ENDCAPS, "_eplus", EPLUS, EPLUS);
   cout << "########################################" << endl;




// //////////////////////////////////////////////////////////
//   //   WP-80 --> HLT triggering efficiency
// //////////////////////////////////////////////////////////

 
   TCut HLT80Pass = preCut && ID80 && PassAll;
   TCut HLT80Fail = preCut && ID80 && NotPassAll;
   ComputeEfficiency("HLT80", HLT80Pass, HLT80Fail);
   ComputeEfficiency("HLT80", HLT80Pass, HLT80Fail, "Barrel", BARREL, BARREL);
   ComputeEfficiency("HLT80", HLT80Pass, HLT80Fail, "Endcap", ENDCAPS, ENDCAPS);
   ComputeEfficiency("HLT80", HLT80Pass, HLT80Fail, "", "", "", "_eminus", EMINUS, EMINUS);
   ComputeEfficiency("HLT80", HLT80Pass, HLT80Fail, "", "", "", "_eplus", EPLUS, EPLUS);
   ComputeEfficiency("HLT80", HLT80Pass, HLT80Fail, "Barrel", BARREL, BARREL, "_eminus", EMINUS, EMINUS);
   ComputeEfficiency("HLT80", HLT80Pass, HLT80Fail, "Barrel",  BARREL, BARREL, "_eplus", EPLUS, EPLUS);
   ComputeEfficiency("HLT80", HLT80Pass, HLT80Fail, "Endcap", ENDCAPS, ENDCAPS, "_eminus", EMINUS, EMINUS);
   ComputeEfficiency("HLT80", HLT80Pass, HLT80Fail, "Endcap", ENDCAPS, ENDCAPS, "_eplus", EPLUS, EPLUS);
   cout << "########################################" << endl;
}
Exemple #10
0
void fitD(TString inputdata="/data/dmeson2015/DataDntuple/nt_20160112_DfinderData_pp_20160111_dPt0tkPt1_D0Dstar3p5p_DCSJSON_v2.root", TString inputmc="/afs/cern.ch/work/w/wangj/public/Dmeson/ntD_20151110_DfinderMC_20151110_EvtMatching_Pythia_D0pt15p0_Pthat15_TuneZ2_5020GeV_GENSIM_75x_1015_20151110_ppGlobaTrackingPPmenuHFlowpuv11_MBseed_twang-Pythia_1107.root", TString trgselection="((HLT_DmesonPPTrackingGlobal_Dpt15_v1&&Dpt>25&&Dpt<40)||(HLT_DmesonPPTrackingGlobal_Dpt30_v1&&Dpt>40&&Dpt<60)||(HLT_DmesonPPTrackingGlobal_Dpt50_v1&&Dpt>60))",  TString cut="Dy>-1.&&Dy<1.&&(Dtrk1highPurity&&Dtrk2highPurity)&&(DsvpvDistance/DsvpvDisErr)>3.5&&Dchi2cl>0.05&&Dalpha<0.12&&Dtrk1Pt>1.5&&Dtrk2Pt>1.5", TString selmcgen="((GisSignal==1||GisSignal==2)&&(Gy>-1&&Gy<1))", int isMC=0, Double_t luminosity=26., int doweight=0, TString collsyst="PbPb", TString outputfile="mytest.root")
{
  collisionsystem=collsyst;
  seldata = Form("%s&&%s",trgselection.Data(),cut.Data());
  selmc = Form("%s",cut.Data());

  gStyle->SetTextSize(0.05);
  gStyle->SetTextFont(42);
  gStyle->SetPadRightMargin(0.043);
  gStyle->SetPadLeftMargin(0.18);
  gStyle->SetPadTopMargin(0.1);
  gStyle->SetPadBottomMargin(0.145);
  gStyle->SetTitleX(.0f);

  void clean0 (TH1D* h);
  TF1* fit (TTree* nt, TTree* ntMC, double ptmin, double ptmax, int isMC);

  if(!doweight) weight="1";
  TFile* inf = new TFile(inputdata.Data());
  TFile* infMC = new TFile(inputmc.Data());

  TTree* nt = (TTree*) inf->Get("ntDkpi");
  TTree* HltTree= (TTree*) inf->Get("ntHlt");
  HltTree->AddFriend(nt);
  nt->AddFriend(HltTree);
  TTree* ntHid = (TTree*) inf->Get("ntHi");
  nt->AddFriend(ntHid);
  
  TTree* ntMC = (TTree*)infMC->Get("ntDkpi");
  TTree* ntGen = (TTree*)infMC->Get("ntGen");
  TTree* ntHi = (TTree*)infMC->Get("ntHi");
  
  ntGen->AddFriend(ntMC);
  ntGen->AddFriend(ntHi);
  ntMC->AddFriend(ntGen);
  ntMC->AddFriend(ntHi);
  ntHi->AddFriend(ntMC);
  
  TH1D* hPt = new TH1D("hPt","",nBins,ptBins);
  TH1D* hPtRecoTruth = new TH1D("hPtRecoTruth","",nBins,ptBins);
  TH1D* hPtMC = new TH1D("hPtMC","",nBins,ptBins);
  TH1D* hPtGen = new TH1D("hPtGen","",nBins,ptBins);

  TH1D* hMean = new TH1D("hMean","",nBins,ptBins);                       
  TH1D* hSigmaGaus1 = new TH1D("hSigmaGaus1","",nBins,ptBins); 
  TH1D* hSigmaGaus2 = new TH1D("hSigmaGaus2","",nBins,ptBins); 
  TH1D* hRelMagnGaus1Gaus2 = new TH1D("hRelMagnGaus1Gaus2","",nBins,ptBins); 
  
  for(int i=0;i<nBins;i++)
    {
      TF1* f = fit(nt,ntMC,ptBins[i],ptBins[i+1],isMC);
      double yield = f->Integral(minhisto,maxhisto)/binwidthmass;
      double yieldErr = f->Integral(minhisto,maxhisto)/binwidthmass*f->GetParError(0)/f->GetParameter(0);
      hPt->SetBinContent(i+1,yield/(ptBins[i+1]-ptBins[i]));
      hPt->SetBinError(i+1,yieldErr/(ptBins[i+1]-ptBins[i]));
      hMean->SetBinContent(i+1,f->GetParameter(1));
      hMean->SetBinError(i+1,f->GetParError(1));
      hSigmaGaus1->SetBinContent(i+1,f->GetParameter(2));
      hSigmaGaus1->SetBinError(i+1,f->GetParError(2));
      hSigmaGaus2->SetBinContent(i+1,f->GetParameter(5));
      hSigmaGaus2->SetBinError(i+1,f->GetParError(5));
      hRelMagnGaus1Gaus2->SetBinContent(i+1,f->GetParameter(4));
      hRelMagnGaus1Gaus2->SetBinError(i+1,f->GetParError(4));
    }  

  ntMC->Project("hPtMC","Dpt",TCut(weight)*(TCut(selmc.Data())&&"(Dgen==23333)"));
  divideBinWidth(hPtMC);
  ntMC->Project("hPtRecoTruth","Dpt",TCut(selmc.Data())&&"(Dgen==23333)");
  divideBinWidth(hPtRecoTruth);
  ntGen->Project("hPtGen","Gpt",TCut(weight)*(TCut(selmcgen.Data())));
  divideBinWidth(hPtGen);

  TCanvas* cPt =  new TCanvas("cPt","",600,600);
  cPt->SetLogy();
  hPt->SetXTitle("D^{0} p_{T} (GeV/c)");
  hPt->SetYTitle("Uncorrected dN(D^{0})/dp_{T}");
  hPt->Sumw2();
  hPt->Draw();
  if(isMC)
    {
      hPtMC->Draw("same hist");
      TLegend* legPt = myLegend(0.55,0.80,0.90,0.94);
      legPt->AddEntry(hPt,"Signal extraction","pl");
      legPt->AddEntry(hPtMC,"Matched reco","lf");
      legPt->Draw("same");  
    }
  hPtMC->Sumw2();
  TH1D* hEff = (TH1D*)hPtMC->Clone("hEff");
  hEff->SetTitle(";D^{0} p_{T} (GeV/c);Efficiency");
  hEff->Sumw2();
  hEff->Divide(hPtGen);
  TCanvas* cEff = new TCanvas("cEff","",600,600);
  hEff->Draw();
  
  TH1D* hPtCor = (TH1D*)hPt->Clone("hPtCor");
  hPtCor->SetTitle(";D^{0} p_{T} (GeV/c);Corrected dN(D^{0})/dp_{T}");
  hPtCor->Divide(hEff);
  TCanvas* cPtCor=  new TCanvas("cCorResult","",600,600);
  cPtCor->SetLogy();
  hPtCor->Draw();
  if(isMC)
    {
      hPtGen->Draw("same hist");
      TLegend* legPtCor = myLegend(0.55,0.80,0.90,0.94);
      legPtCor->AddEntry(hPtCor,"Corrected signal","pl");
      legPtCor->AddEntry(hPtGen,"Generated D^{0}","lf");
      legPtCor->Draw("same");  
    }

  TH1D* hPtSigma= (TH1D*)hPtCor->Clone("hPtSigma");
  hPtSigma->SetTitle(";D^{0} p_{T} (GeV/c);d#sigma(D^{0})/dp_{T} (pb/GeV)");
  hPtSigma->Scale(1./(2*luminosity*BRchain));
  TCanvas* cPtSigma=  new TCanvas("cPtSigma","",600,600);
  cPtSigma->SetLogy();
  hPtSigma->Draw();
  
  TFile* outf = new TFile(outputfile.Data(),"recreate");
  outf->cd();
  hPt->Write();
  hEff->Write();
  hPtGen->Write();
  hPtMC->Write();
  hPtCor->Write();
  hPtSigma->Write();
  hMean->Write();
  hSigmaGaus1->Write();
  hSigmaGaus2->Write();
  hRelMagnGaus1Gaus2->Write();
  outf->Close();
}
Exemple #11
0
void printYields( vector<TChain*> chmc , vector<char*> labels , TChain* chdata , TCut sel , TCut weight , bool latex ){

  initSymbols( latex );

  TCanvas *ctemp = new TCanvas();

  printLine(latex);
  printHeader();
  printLine(latex);

  TH1F* hyield = new TH1F("hyield","yield",4,0,4);
  TH1F* hmctot = new TH1F("hmctot","hmctot",4,0,4);
  hyield->Sumw2();
  hmctot->Sumw2();

  //----------------------
  // print SM MC samples
  //----------------------

  for(unsigned int imc = 0 ; imc < chmc.size() ; imc++){

    bool correlatedError = false;

    if( TString(labels[imc]).Contains("T2") ) continue;

    // data-driven DY estimate
    if( strcmp(labels[imc],"DYdata")   == 0 ){
      //hyield = doDYestimate( chmc[imc] , sel );
      doDYestimate( chmc[imc] , sel , hyield );
    }

    // fake estimate
    else if( strcmp(labels[imc],"single fakes")   == 0 || strcmp(labels[imc],"double fakes") == 0){

      //correlatedError = true;

      TString weightstring(weight.GetTitle());
      weightstring.ReplaceAll("ndavtxweight","1");
      TCut newweight = TCut(weightstring);

      chmc[imc]->Draw("leptype>>hyield",sel*newweight);

      // SF --> SF - 2 X DF
      if( strcmp(labels[imc],"single fakes")   == 0 ){
	
	TH1F *hyielddf = new TH1F("hyielddf","hyielddf",4,0,4);

	chmc[imc+1]->Draw("leptype>>hyielddf",sel*newweight);
	hyield->Add(hyielddf,-2);
      }      

      hyield->SetBinError(1,0.5*hyield->GetBinContent(1));
      hyield->SetBinError(2,0.5*hyield->GetBinContent(2));
      
    }

    //vanilla MC
    else{
      chmc[imc]->Draw("leptype>>hyield",sel*weight);

      //do efficiency correction
      //hyield->SetBinContent  ( 2 , hyield->GetBinContent(2) * 0.90);
      //hyield->SetBinContent  ( 3 , hyield->GetBinContent(3) * 0.95);
      //hyield->SetBinError    ( 2 , hyield->GetBinError(2)   * 0.90);
      //hyield->SetBinError    ( 3 , hyield->GetBinError(3)   * 0.95);

    }

    if( imc == 0 ) hmctot = (TH1F*) hyield->Clone();
    else           hmctot->Add(hyield);
    
    print( hyield , labels[imc] , correlatedError );

    //hyield->Reset();
  }

  printLine(latex);

  //-------------------------------
  // print sum of SM MC samples
  //-------------------------------

  print( hmctot , "total SM MC" );

  printLine(latex);
 
  chdata->Draw("leptype>>hyield",sel);

  print( hyield , "data" );
    
  printLine(latex);

  //----------------------
  // print T2tt MC samples
  //----------------------

  for(unsigned int imc = 0 ; imc < chmc.size() ; imc++){

    if( !TString(labels[imc]).Contains("T2") ) continue;

    chmc[imc]->Draw("leptype>>hyield",sel*weight);

    if( TString(labels[imc]).Contains("X5")  ) hyield->Scale(5);
    if( TString(labels[imc]).Contains("X6")  ) hyield->Scale(6);
    if( TString(labels[imc]).Contains("X10") ) hyield->Scale(10);

    //do efficiency correction
    //hyield->SetBinContent  ( 2 , hyield->GetBinContent(2) * 0.90);
    //hyield->SetBinContent  ( 3 , hyield->GetBinContent(3) * 0.95);
    //hyield->SetBinError    ( 2 , hyield->GetBinError(2)   * 0.90);
    //hyield->SetBinError    ( 3 , hyield->GetBinError(3)   * 0.95);
    
    print( hyield , labels[imc] );

  }

  printLine(latex);


  
  delete ctemp;
}
Exemple #12
0
//TH1F* doDYestimate( TChain *ch , TCut sel ){
void doDYestimate( TChain *ch , TCut sel , TH1F* hyield ){

  //-------------------
  // set parameters
  //-------------------

  bool  verbose = true;
  float R       = 0.13;
  float R_err   = 0.07;
  float k       = 1.11;

  if( verbose ){
    cout << "R : " << R << " +/- " << R_err << endl;
    cout << "k : " << k << endl;
  }

  //-------------------
  // invert Z-veto
  //-------------------

  TString selstring(sel.GetTitle());
  selstring.ReplaceAll("passz == 0","dilmass>76&&dilmass<106");
  TCut newsel = TCut(selstring);

  if( verbose ){
    cout << "Pre  : " << sel.GetTitle() << endl;
    cout << "Post : " << newsel.GetTitle() << endl;
  }

  //-------------------
  // get data yields
  //-------------------

  float ndata_ee_in = ch->GetEntries(newsel+"leptype==0");
  float ndata_mm_in = ch->GetEntries(newsel+"leptype==1");
  float ndata_em_in = ch->GetEntries(newsel+"leptype==2");

  if( verbose ){
    cout << "nee     " << ndata_ee_in << endl;
    cout << "nmm     " << ndata_mm_in << endl;
    cout << "nem     " << ndata_em_in << endl;
  }

  //-------------------
  // do DY estimate
  //-------------------

  float neepred     = R     * ( ndata_ee_in - (0.5/k)  * ndata_em_in );
  float nmmpred     = R     * ( ndata_mm_in - k/2.     * ndata_em_in );

  float neeprederr  = 0.;
  float nmmprederr  = 0.;

  neeprederr  += pow( R_err * ( ndata_ee_in - (0.5/k) * ndata_em_in ) , 2 );
  nmmprederr  += pow( R_err * ( ndata_mm_in - k/2.    * ndata_em_in ) , 2 );

  neeprederr  += pow( R * sqrt( ndata_ee_in + pow(0.5/k,2)  * ndata_em_in ) , 2 );
  nmmprederr  += pow( R * sqrt( ndata_mm_in + pow(k/2.,2)   * ndata_em_in ) , 2 );

  neeprederr = sqrt(neeprederr);
  nmmprederr = sqrt(nmmprederr);

  float ntotpred    = neepred + nmmpred;
  float ntotprederr = sqrt( pow(neeprederr,2) + pow(nmmprederr,2) );

  if( verbose ){
    cout << "nee  pred " << neepred  << " +/- " << neeprederr  << endl;
    cout << "nmm  pred " << nmmpred  << " +/- " << nmmprederr  << endl;
    cout << "ntot pred " << ntotpred << " +/- " << ntotprederr << endl;
  }

  //-------------------
  // set hist contents
  //-------------------

  hyield->SetBinContent( 1 , neepred );
  hyield->SetBinContent( 2 , nmmpred );
  hyield->SetBinContent( 3 , 0 );
  hyield->SetBinContent( 4 , 0 );

  hyield->SetBinError( 1 , neeprederr );
  hyield->SetBinError( 2 , nmmprederr );
  hyield->SetBinError( 3 , 0 );
  hyield->SetBinError( 4 , 0 );

}
void MCefficiency(TString inputmc="/data/wangj/MC2015/Dntuple/pp/revised/ntD_pp_Dzero_kpi_prompt/ntD_EvtBase_20160303_Dfinder_20160302_pp_Pythia8_prompt_D0_dPt0tkPt0p5_pthatweight.root", TString selmcgen="((GisSignal==1||GisSignal==2)&&(Gy>-1&&Gy<1))",TString selmcgenacceptance="((GisSignal==1||GisSignal==2)&&(Gy>-1&&Gy<1))&&abs(Gtk1eta)<2.0&&abs(Gtk2eta)<2.0&&Gtk1pt>2.0&&Gtk2pt>2.0", TString cut_recoonly="Dy>-1.&&Dy<1.&&Dtrk1highPurity&&Dtrk2highPurity&&Dtrk1Pt>2.0&&Dtrk2Pt>2.0&&Dtrk1PtErr/Dtrk1Pt<0.1&&Dtrk2PtErr/Dtrk2Pt<0.1&&abs(Dtrk1Eta)<2.0&&abs(Dtrk2Eta)<2.0&&Dtrk1Algo>3&&Dtrk1Algo<8&&(Dtrk1PixelHit+Dtrk1StripHit)>=11", TString cut="Dy>-1.&&Dy<1.&&Dtrk1highPurity&&Dtrk2highPurity&&Dtrk1Pt>2.0&&Dtrk2Pt>2.0&&(DsvpvDistance/DsvpvDisErr)>3.5&&(DlxyBS/DlxyBSErr)>1.5&&Dchi2cl>0.05&&Dalpha<0.12&&Dtrk1PtErr/Dtrk1Pt<0.1&&Dtrk2PtErr/Dtrk2Pt<0.1&&abs(Dtrk1Eta)<2.0&&abs(Dtrk2Eta)<2.0&&Dtrk1Algo>3&&Dtrk1Algo<8&&Dtrk2Algo>3&&Dtrk2Algo<8&&(Dtrk1PixelHit+Dtrk1StripHit)>=11&&(Dtrk1Chi2ndf/(Dtrk1nStripLayer+Dtrk1nPixelLayer)<0.15)&&(Dtrk2Chi2ndf/(Dtrk2nStripLayer+Dtrk2nPixelLayer)<0.15)",TString label="PP",TString outputfile="test", int useweight=1, int minfit=2,int maxfit=100)
{
 std::cout<<"option="<<useweight<<std::endl;
 selmc=cut;
  gStyle->SetOptTitle(0);
  gStyle->SetOptStat(0);
  gStyle->SetEndErrorSize(0);
  gStyle->SetMarkerStyle(20);
 
  TFile* infMC = new TFile(inputmc.Data());
  TTree* ntMC = (TTree*)infMC->Get("ntDkpi");
  TTree* ntGen = (TTree*)infMC->Get("ntGen");
  ntMC->AddFriend(ntGen);
  

  TTree* nthi = (TTree*)infMC->Get("ntHi");
  ntGen->AddFriend(nthi);
  nthi->AddFriend(ntMC);
  ntMC->AddFriend(nthi);

  if (useweight==0){
       weightfunctiongen="1"; 
       weightfunctionreco="1";  
   }   
  if (useweight==1){
     weightfunctiongen="pthatweight";
     weightfunctionreco="pthatweight";  
  }
  
  //ptweight pp
  if (useweight==2){
    weightfunctiongen="pow(10,-0.168499*Gpt+3.872855+Gpt*Gpt*0.000556)+pow(10,-0.068599*Gpt+2.512265+Gpt*Gpt*0.000331)";  
    weightfunctionreco="pow(10,-0.168499*Dgenpt+3.872855+Dgenpt*Dgenpt*0.000556)+pow(10,-0.068599*Dgenpt+2.512265+Dgenpt*Dgenpt*0.000331)"; 
  }
  //ptweight pbpb 
  if (useweight==3){
    weightfunctiongen="pow(10,-0.091618*Gpt+2.966262+Gpt*Gpt*0.000524)+pow(10,-0.167553*Gpt+3.800779+Gpt*Gpt*-0.000798) +1.437905+-0.029141*Gpt+0.000146*Gpt*Gpt"; 
    weightfunctionreco="pow(10,-0.091618*Dgenpt+2.966262+Dgenpt*Dgenpt*0.000524)+pow(10,-0.167553*Dgenpt+3.800779+Dgenpt*Dgenpt*-0.000798) +1.437905+-0.029141*Dgenpt+0.000146*Dgenpt*Dgenpt";
  }
  
   std::cout<<"fit function parameters="<<weightfunctiongen<<std::endl;

  TH1D* hPtMC = new TH1D("hPtMC","",nBins,ptBins);
  TH1D* hPtMCrecoonly = new TH1D("hPtMCrecoonly","",nBins,ptBins);
  TH1D* hPtGen = new TH1D("hPtGen","",nBins,ptBins);
  TH1D* hPtGenAcc = new TH1D("hPtGenAcc","",nBins,ptBins);
  TH1D* hPthat = new TH1D("hPthat","",100,0,500);
  TH1D* hPthatweight = new TH1D("hPthatweight","",100,0,500);
  
  ntMC->Project("hPtMC","Dpt",TCut(weightfunctionreco)*(TCut(selmc.Data())&&"(Dgen==23333)"));
  divideBinWidth(hPtMC);
  ntMC->Project("hPtMCrecoonly","Dpt",TCut(weightfunctionreco)*(TCut(cut_recoonly.Data())&&"(Dgen==23333)"));
  divideBinWidth(hPtMCrecoonly);
  ntGen->Project("hPtGen","Gpt",TCut(weightfunctiongen)*(TCut(selmcgen.Data())));
  divideBinWidth(hPtGen);
  ntGen->Project("hPtGenAcc","Gpt",TCut(weightfunctiongen)*(TCut(selmcgenacceptance.Data())));
  divideBinWidth(hPtGenAcc);

  ntMC->Project("hPthat","pthat","1");
  ntMC->Project("hPthatweight","pthat",TCut("1"));

  hPtMC->Sumw2();
  TH1D* hEff = (TH1D*)hPtMC->Clone("hEff");
  hEff->Divide(hPtGen);

  TH1D* hEffReco = (TH1D*)hPtMCrecoonly->Clone("hEffReco");
  hEffReco->Sumw2();
  hEffReco->Divide(hPtGen);

  TH1D* hEffAcc = (TH1D*)hPtGenAcc->Clone("hEffAcc");
  hEffAcc->Sumw2();
  hEffAcc->Divide(hEffAcc,hPtGen,1,1,"b");
  
  TH1D* hEffSelection = (TH1D*)hPtMC->Clone("hEffSelection");
  hEffSelection->Sumw2();
  hEffSelection->Divide(hEffSelection,hPtMCrecoonly,1,1,"b");
  
  TH2F* hemptyEff=new TH2F("hemptyEff","",50,ptBins[0]-5.,ptBins[nBins]+5.,10.,0,1.5);  
  hemptyEff->GetXaxis()->CenterTitle();
  hemptyEff->GetYaxis()->CenterTitle();
  hemptyEff->GetYaxis()->SetTitle("acceptance x #epsilon_{reco} x #epsilon_{sel} ");
  hemptyEff->GetXaxis()->SetTitle("p_{T} (GeV/c)");
  hemptyEff->GetXaxis()->SetTitleOffset(0.9);
  hemptyEff->GetYaxis()->SetTitleOffset(0.95);
  hemptyEff->GetXaxis()->SetTitleSize(0.05);
  hemptyEff->GetYaxis()->SetTitleSize(0.05);
  hemptyEff->GetXaxis()->SetTitleFont(42);
  hemptyEff->GetYaxis()->SetTitleFont(42);
  hemptyEff->GetXaxis()->SetLabelFont(42);
  hemptyEff->GetYaxis()->SetLabelFont(42);
  hemptyEff->GetXaxis()->SetLabelSize(0.035);
  hemptyEff->GetYaxis()->SetLabelSize(0.035);  
  hemptyEff->SetMaximum(2);
  hemptyEff->SetMinimum(0.);
  hemptyEff->Draw();

  TH2F* hemptyEffAcc=(TH2F*)hemptyEff->Clone("hemptyEffAcc");
  TH2F* hemptyEffReco=(TH2F*)hemptyEff->Clone("hemptyEffReco");
  TH2F* hemptyEffSelection=(TH2F*)hemptyEff->Clone("hemptyEffSelection");
  
  TCanvas*canvasEff=new TCanvas("canvasEff","canvasEff",1000.,500);
  canvasEff->Divide(2,1);
  canvasEff->cd(1);
  
  hemptyEffAcc->SetYTitle("#alpha");
  hemptyEffAcc->Draw();
  hEffAcc->Draw("same");
  /*
  canvasEff->cd(2);
  hEffReco->GetYaxis()->SetTitleOffset(1.2);
  hemptyEffReco->SetYTitle("#alpha x #epsilon_{reco} ");
  hemptyEffReco->Draw();
  hEffReco->Draw("same");
  canvasEff->cd(3);
  hemptyEffSelection->SetYTitle(" #epsilon_{sel}");
  hemptyEffSelection->Draw();  
  hEffSelection->Draw("same");  
  */
  canvasEff->cd(2);
  hemptyEff->Draw();
  hEff->Draw("same");
  canvasEff->SaveAs(Form("canvasEff_study%s.pdf",Form(label.Data())));
  
  
  TH2F* hemptyPthat=new TH2F("hemptyPthat","",50,0.,500.,10,1e-5,1e9);  
  hemptyPthat->GetXaxis()->CenterTitle();
  hemptyPthat->GetYaxis()->CenterTitle();
  hemptyPthat->GetYaxis()->SetTitle("Entries");
  hemptyPthat->GetXaxis()->SetTitle("pthat");
  hemptyPthat->GetXaxis()->SetTitleOffset(0.9);
  hemptyPthat->GetYaxis()->SetTitleOffset(0.95);
  hemptyPthat->GetXaxis()->SetTitleSize(0.05);
  hemptyPthat->GetYaxis()->SetTitleSize(0.05);
  hemptyPthat->GetXaxis()->SetTitleFont(42);
  hemptyPthat->GetYaxis()->SetTitleFont(42);
  hemptyPthat->GetXaxis()->SetLabelFont(42);
  hemptyPthat->GetYaxis()->SetLabelFont(42);
  hemptyPthat->GetXaxis()->SetLabelSize(0.035);
  hemptyPthat->GetYaxis()->SetLabelSize(0.035);  
  hemptyPthat->SetMaximum(2);
  hemptyPthat->SetMinimum(0.);


  TH2F* hemptySpectra=new TH2F("hemptySpectra","",50,0.,130.,10,1,1e5);  
  hemptySpectra->GetXaxis()->CenterTitle();
  hemptySpectra->GetYaxis()->CenterTitle();
  hemptySpectra->GetYaxis()->SetTitle("Entries");
  hemptySpectra->GetXaxis()->SetTitle("p_{T}");
  hemptySpectra->GetXaxis()->SetTitleOffset(0.9);
  hemptySpectra->GetYaxis()->SetTitleOffset(0.95);
  hemptySpectra->GetXaxis()->SetTitleSize(0.05);
  hemptySpectra->GetYaxis()->SetTitleSize(0.05);
  hemptySpectra->GetXaxis()->SetTitleFont(42);
  hemptySpectra->GetYaxis()->SetTitleFont(42);
  hemptySpectra->GetXaxis()->SetLabelFont(42);
  hemptySpectra->GetYaxis()->SetLabelFont(42);
  hemptySpectra->GetXaxis()->SetLabelSize(0.035);
  hemptySpectra->GetYaxis()->SetLabelSize(0.035);  

  TH2F* hemptyPthatWeighted=(TH2F*)hemptyPthat->Clone("hemptyPthatWeighted");
  hemptyPthatWeighted->GetXaxis()->SetTitle("pthat reweighted");
  
  TCanvas*canvasPthat=new TCanvas("canvasPthat","canvasPthat",1000.,500);
  canvasPthat->Divide(2,1);
  canvasPthat->cd(1);
  gPad->SetLogy();
  hemptyPthat->Draw("same");
  hPthat->Draw("same");
  canvasPthat->cd(2);
  gPad->SetLogy();
  hemptyPthatWeighted->Draw();
  hPthatweight->Draw("same");
 // canvasPthat->SaveAs(Form("canvasPthat_%s.pdf",Form(label.Data())));
  
  TCanvas*canvasSpectra=new TCanvas("canvasSpectra","canvasSpectra",1000.,500);
  canvasSpectra->Divide(2,1);
  canvasSpectra->cd(1);
  gPad->SetLogy();
  hemptySpectra->Draw();
  hPtMC->Draw("same");
  canvasSpectra->cd(2);
  gPad->SetLogy();
  hemptySpectra->Draw();
  hPtGen->Draw("same");
  //canvasSpectra->SaveAs(Form("canvasSpectra_%s.pdf",Form(label.Data())));

  TFile *fout=new TFile(outputfile.Data(),"recreate");
  fout->cd();
  hPtGen->Write();
  hEffAcc->Write();
  hEffReco->Write();
  hEffSelection->Write();
  hEff->Write();
  hPtMC->Write();
  fout->Close();  

}
void TMVATraining_ch4( )
{

  TFile* outputFile = TFile::Open( "TMVA_ch4.root", "RECREATE" );
  TMVA::Factory *factory = new TMVA::Factory( "MVAnalysis", outputFile,"!V");
  TFile *signal = TFile::Open("../production/BGx0/Prod2_iptubeK0/B0_etapr-eta-3pi2pi_KS-pi+pi-_output_signal_iptubeK0.root");
  TFile *background = TFile::Open("../production/BGx0/Prod2_iptubeK0/B0_etapr-eta-3pi2pi_KS-pi+pi-_output_ccbar_iptubeK0.root");
  factory->AddSignalTree ( (TTree*)signal->Get("B0"), 1.0 );
  factory->AddBackgroundTree ( (TTree*)background->Get("B0"), 1.0 );
  sigCut = TCut("B0__isContinuumEvent==0");
  bgCut = TCut("B0__isContinuumEvent==1");

  factory->AddVariable("B0_ThrustB",'F');
  factory->AddVariable("B0_ThrustO",'F');
  factory->AddVariable("B0_CosTBTO",'F');
  factory->AddVariable("B0_CosTBz",'F');
  factory->AddVariable("B0_R2",'F');
  factory->AddVariable("B0_cc1",'F');
  factory->AddVariable("B0_cc2",'F');
  factory->AddVariable("B0_cc3",'F');
  factory->AddVariable("B0_cc4",'F');
  factory->AddVariable("B0_cc5",'F');
  factory->AddVariable("B0_cc6",'F');
  factory->AddVariable("B0_cc7",'F');
  factory->AddVariable("B0_cc8",'F');
  factory->AddVariable("B0_cc9",'F');
  factory->AddVariable("B0_mm2",'F');
  factory->AddVariable("B0_et",'F');
  factory->AddVariable("B0_hso00",'F');
  // factory->AddVariable("B0_hso01",'F');
  factory->AddVariable("B0_hso02",'F');
  //factory->AddVariable("B0_hso03",'F');
  factory->AddVariable("B0_hso04",'F');
  factory->AddVariable("B0_hso10",'F');
  factory->AddVariable("B0_hso12",'F');
  factory->AddVariable("B0_hso14",'F');
  factory->AddVariable("B0_hso20",'F');
  factory->AddVariable("B0_hso22",'F');
  factory->AddVariable("B0_hso24",'F');
  factory->AddVariable("B0_hoo0",'F');
  factory->AddVariable("B0_hoo1",'F');
  factory->AddVariable("B0_hoo2",'F');
  factory->AddVariable("B0_hoo3",'F');
  factory->AddVariable("B0_hoo4",'F');

  factory->PrepareTrainingAndTestTree(sigCut, bgCut, "!V:nTrain_Signal=10000:nTest_Signal=10000:nTrain_Background=10000:nTest_Background=10000:SplitMode=Random:NormMode=NumEvents" );

  //factory->BookMethod( TMVA::Types::kLikelihood, "Likelihood", "H:V:!TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmooth=5:NAvEvtPerBin=50:VarTransform=PCA");
  //factory->BookMethod( TMVA::Types::kMLP, "MLP", "!V:NCycles=200:HiddenLayers=N+1,N:TestRate=5" );
  factory->BookMethod( TMVA::Types::kMLP, "MLPBNN", "H:!V:NeuronType=tanh:VarTransform=N:NCycles=600:HiddenLayers=N+5:TestRate=5:TrainingMethod=BFGS:UseRegulator" );
  factory->BookMethod( TMVA::Types::kBDT, "BDT", "!H:!V:NTrees=850:MinNodeSize=2.5%:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:UseBaggedBoost:BaggedSampleFraction=0.5:SeparationType=GiniIndex:nCuts=20" );
  factory->BookMethod( TMVA::Types::kSVM, "SVM", "!H:!V:Gamma=0.25:Tol=0.001:VarTransform=Norm" );

  //factory->BookMethod( TMVA::Types::kBDT, "FastBDT", "!H:!V:CreateMVAPdfs:NbinsMVAPdf=40:NTrees=100:Shrinkage=0.10"); //:RandRatio=0.5:NCutLevel=8:NTreeLayers=3");

  factory->TrainAllMethods();
  factory->TestAllMethods();
  factory->EvaluateAllMethods();
  outputFile->Close();
  delete factory;

  // Launch the GUI for the root macros
  if (!gROOT->IsBatch()) TMVA::TMVAGui( "TMVA_ch4.root" );
}
Exemple #15
0
{

  gROOT->ProcessLine(".L scripts/plotSimple_OS.C+");



  //------------------------
  // electrons
  //------------------------

  //ID eff vs pt
  plotSimple_OS("output/baby_data_skim.root", "output/baby_dyee.root", TCut("pt1", "pt1"), 
   		TCut("pt2", "pt2"), 20, 0, 100,0,false,false);
  
  //iso eff vs. pt
  plotSimple_OS("output/baby_data_skim.root", "output/baby_dyee.root", TCut("pt1", "pt1"), 
   		TCut("pt2", "pt2"), 20, 0, 100,0,true,false);
  
  //------------------------
  // muons
  //------------------------

  //ID eff vs pt
  plotSimple_OS("output/baby_data_skim.root", "output/baby_dymm.root", TCut("pt1", "pt1"), 
   		TCut("pt2", "pt2"), 20, 0, 100,1,false,false);
  
  //iso eff vs. pt
  plotSimple_OS("output/baby_data_skim.root", "output/baby_dymm.root", TCut("pt1", "pt1"), 
   		TCut("pt2", "pt2"), 20, 0, 100,1,true,false);
  
Exemple #16
0
void fitBsSingGau(TString infname="",bool doweight = 1)
{
   if (doweight==0) weight="1";
   if (infname=="") infname=inputdata.Data();
   TFile *inf = new TFile(infname.Data());
   TTree *nt = (TTree*) inf->Get("ntphi");

   TFile *infMC = new TFile(inputmc.Data());
   TTree *ntGen = (TTree*)infMC->Get("ntGen");
   TTree *ntMC = (TTree*)infMC->Get("ntphi");

   ntGen->AddFriend(ntMC);

   const int nBins = 1;
   double ptBins[nBins+1] = {10,60};
   TH1D *hPt = new TH1D("hPt","",nBins,ptBins);
   TH1D *hPtMC = new TH1D("hPtMC","",nBins,ptBins);
   TH1D *hPtGen = new TH1D("hPtGen","",nBins,ptBins);
   TH1D *hRecoTruth = new TH1D("hRecoTruth","",nBins,ptBins);
   ntMC->Project("hPtMC","pt",TCut(weight)*(selmc.Data()));
   nt->Project("hRecoTruth","pt",TCut(seldata_2y.Data())&&"(gen==23333)");
   ntGen->Project("hPtGen","pt",TCut(weight)*(selmcgen.Data()));
   divideBinWidth(hRecoTruth);
   
   for (int i=0;i<nBins;i++)
   {
      TF1 *f = fit(nt,ntMC,ptBins[i],ptBins[i+1]);
      double yield = f->Integral(5,6)/0.04;
      double yieldErr = f->Integral(5,6)/0.04*f->GetParError(0)/f->GetParameter(0);
      hPt->SetBinContent(i+1,yield/(ptBins[i+1]-ptBins[i]));
      hPt->SetBinError(i+1,yieldErr/(ptBins[i+1]-ptBins[i]));
   }  

   /*
   divideBinWidth(hPtMC);
   divideBinWidth(hPtGen);

   TCanvas *c=  new TCanvas("cResult","",600,600);
   hPt->SetXTitle("B_{s} p_{T} (GeV/c)");
   hPt->SetYTitle("Uncorrected B_{s} dN/dp_{T}");
   hPt->Draw();
   hRecoTruth->Draw("same hist");
   
   hPtMC->Sumw2();
   TH1D *hEff = (TH1D*)hPtMC->Clone("hEff");
   hPtMC->Sumw2();
   hEff->Divide(hPtGen);
  
  TH1D *hPtCor = (TH1D*)hPt->Clone("hPtCor");
  hPtCor->Divide(hEff);
  TCanvas *cCor=  new TCanvas("cCorResult","",600,600);
  hPtCor->SetYTitle("Correctd B_{s} dN/dp_{T}");
  hPtCor->Draw();
  hPtGen->Draw("same");

  TH1D *hPtSigma= (TH1D*)hPtCor->Clone("hPtSigma");
  double BRchain=2.89977e-5;
  hPtSigma->Scale(1./(2*luminosity*BRchain));
  hPtSigma->SetYTitle("d#sigma/dp_{T} (B_{s})");

  TCanvas *cSigma=  new TCanvas("cSigma","",600,600);

  hPtSigma->Draw();
  
  TFile *outf = new TFile("ResultsBs/SigmaBs.root","recreate");
  outf->cd();
  hPt->Write();
  hEff->Write();
  hPtCor->Write();
  hPtSigma->Write();
  outf->Close();
  delete outf;
   */
}
Exemple #17
0
void fitB(bool isData,int myvariationoption)
{
  TString infname;
  if(isData)infname=inputdata.Data();
  if(!isData)infname=inputmc.Data();
  
  if(!isData)seldata_2y=seldata;
  
  TFile *inf = new TFile(infname.Data());
  TTree *nt = (TTree*) inf->Get("ntKp");

  TFile *infMC = new TFile(inputmc.Data());
  TTree *ntGen = (TTree*)infMC->Get("ntGen");
  TTree *ntMC = (TTree*)infMC->Get("ntKp");
    
  const int nBins = 1;
  double ptBins[nBins+1] = {10,60};
  TH1D *hPt = new TH1D("hPt","",nBins,ptBins);
  TH1D *hPtRecoTruth = new TH1D("hPtRecoTruth","",nBins,ptBins);
  TH1D *hGenPtSelected = new TH1D("hGenPtSelected","",nBins,ptBins);
  TH1D *hPtMC = new TH1D("hPtMC","",nBins,ptBins);
  TH1D *hPtGen = new TH1D("hPtGen","",nBins,ptBins);

  for (int i=0;i<nBins;i++)
    {
      TF1 *f = fit(nt,ntMC,ptBins[i],ptBins[i+1],isData,myvariationoption);
      double yield = f->Integral(5,6)/0.02;
      double yieldErr = f->Integral(5,6)/0.02*f->GetParError(0)/f->GetParameter(0);
      hPt->SetBinContent(i+1,yield/(ptBins[i+1]-ptBins[i]));
      hPt->SetBinError(i+1,yieldErr/(ptBins[i+1]-ptBins[i]));
    }  
  
  TCanvas *c=  new TCanvas("cResult","",600,600);
  hPt->SetXTitle("B^{+} p_{T} (GeV/c)");
  hPt->SetYTitle("Uncorrected B^{+} dN/dp_{T}");
  hPt->Sumw2();
  hPt->Draw();
  
  ntMC->Project("hPtMC","pt",TCut(weight)*(TCut(selmc.Data())&&"gen==23333"));
  nt->Project("hPtRecoTruth","pt",TCut(seldata.Data())&&"gen==23333");
  ntGen->Project("hPtGen","pt",TCut(weight)*(TCut(selmcgen.Data())));
  divideBinWidth(hPtRecoTruth);
  
  hPtRecoTruth->Draw("same hist");
  divideBinWidth(hPtMC);
  divideBinWidth(hPtGen);
  
  hPtMC->Sumw2();
  TH1D *hEff = (TH1D*)hPtMC->Clone("hEff");
  hPtMC->Sumw2();
  hEff->Divide(hPtGen);
  
  TH1D *hPtCor = (TH1D*)hPt->Clone("hPtCor");
  hPtCor->Divide(hEff);
  TCanvas *cCor=  new TCanvas("cCorResult","",600,600);
  hPtCor->SetYTitle("Corrected B^{+} dN/dp_{T}");
  hPtCor->Draw();
  hPtGen->Draw("same hist");

  TH1D *hPtSigma= (TH1D*)hPtCor->Clone("hPtSigma");
  hPtSigma->Scale(1./(2*luminosity));
  hPtSigma->SetYTitle("d#sigma (B^{+})/dp_{T}");

  TCanvas *cSigma=  new TCanvas("cSigma","",600,600);

  hPtSigma->Draw(); 
  
  TFile *outf = new TFile(Form("Results/CutId%d/SigmaBplusCutId%d_isData%d.root",myvariationoption,myvariationoption,isData),"recreate");
  outf->cd();
  hPt->Write();
  hEff->Write();
  hPtGen->Write();
  hPtCor->Write();
  hPtSigma->Write();
  outf->Close();
  delete outf;
}
Exemple #18
0
void efficiency_D(){

//	TString inPbPbMCP_pthatFilter="/data/HeavyFlavourRun2/MC2015/Dntuple/PbPb/ntD_EvtBase_20160513_DfinderMC_PbPb_20160502_dPt1tkPt0p5_D0_prompt_Dpt2Dy1p1tkPt0p7tkEta2Decay2p9Dalpha0p14Skim_pthatweight.root" ;
	
//	TString inPbPbMCNP_pthatFilter="/data/HeavyFlavourRun2/MC2015/Dntuple/PbPb/ntD_EvtBase_20160513_DfinderMC_PbPb_20160502_dPt1tkPt0p5_D0_nonprompt_Dpt2Dy1p1tkPt0p7tkEta2Decay2p9Dalpha0p14Skim_pthatweight.root";

TString inppMCP_pthatFilter="/data/HeavyFlavourRun2/MC2015/Dntuple/pp/ntD_EvtBase_20160513_DfinderMC_pp_20160502_dPt0tkPt0p5_D0Dstar_prompt_Dpt2Dy1p1tkPt0p7tkEta2Decay2p9Dalpha0p14Skim_pthatweight.root";
TString inppMCNP_pthatFilter="/data/HeavyFlavourRun2/MC2015/Dntuple/pp/ntD_EvtBase_20160513_DfinderMC_pp_20160502_dPt0tkPt0p5_D0Dstar_nonprompt_Dpt2Dy1p1tkPt0p7tkEta2Decay2p9Dalpha0p14Skim_pthatweight.root";


  TString inppMCP_DptFilter="/data/chengchi/BtoD_MC2016/pp/Dntuple_pp_prompt_mergeAll.root" ;

  TString inppMCNP_DptFilter="/data/chengchi/BtoD_MC2016/pp/Dntuple_pp_Nonprompt_mergeAll.root";


	TString outfname="2015ppMCEffHis_PthatFilter.root";

	TString trgselection="1";

	TString cut="Dy>-1.&&Dy<1.&&Dtrk1highPurity&&Dtrk2highPurity&&Dtrk1Pt>1.0&&Dtrk2Pt>1.0&&Dtrk1PtErr/Dtrk1Pt<0.3&&Dtrk2PtErr/Dtrk2Pt<0.3&&abs(Dtrk1Eta)<1.5&&abs(Dtrk2Eta)<1.5&&((DlxyBS/DlxyBSErr)>1.5&&Dalpha<0.12&&((Dpt>1&&Dpt<2&&(DsvpvDistance/DsvpvDisErr)>6.0&&Dchi2cl>0.25)||(Dpt>2&&Dpt<4&&(DsvpvDistance/DsvpvDisErr)>5.86&&Dchi2cl>0.224)||(Dpt>4&&Dpt<5&&(DsvpvDistance/DsvpvDisErr)>5.46&&Dchi2cl>0.196)||(Dpt>5&&Dpt<6&&(DsvpvDistance/DsvpvDisErr)>4.86&&Dchi2cl>0.170)||(Dpt>6&&Dpt<8&&(DsvpvDistance/DsvpvDisErr)>4.54&&Dchi2cl>0.125)||(Dpt>8&&Dpt<10&&(DsvpvDistance/DsvpvDisErr)>4.42&&Dchi2cl>0.091)||(Dpt>10&&Dpt<15&&(DsvpvDistance/DsvpvDisErr)>4.06&&Dchi2cl>0.069)||(Dpt>15&&Dpt<20&&(DsvpvDistance/DsvpvDisErr)>3.71&&Dchi2cl>0.056)||(Dpt>20&&Dpt<25&&(DsvpvDistance/DsvpvDisErr)>3.25&&Dchi2cl>0.054)||(Dpt>25&&(DsvpvDistance/DsvpvDisErr)>2.97&&Dchi2cl>0.050)))";

 TString cutmcgen="((GisSignal==1||GisSignal==2)&&(Gy>-1&&Gy<1))";

 TString outputfile="ROOTfiles/hPtSpectrumDzeroPbPbMBppMCClosureMYTEST.root";

	bool isPbPb=false;
	int doweight=0;


  if(!isPbPb)
  {
    seldata = Form("%s&&%s",trgselection.Data(),cut.Data());
    selmceff = Form("%s",cut.Data());
    selmcgen = Form("%s",cutmcgen.Data());
  }
  else
  {
    seldata = Form("%s&&%s&&hiBin>%f&&hiBin<%f",trgselection.Data(),cut.Data(),hiBinMin,hiBinMax);
    selmceff = Form("%s&&hiBin>%f&&hiBin<%f",cut.Data(),hiBinMin,hiBinMax);
    selmcgen = Form("%s&&hiBin>%f&&hiBin<%f",cutmcgen.Data(),hiBinMin,hiBinMax);
  }

  selmc = Form("%s",cut.Data());

  gStyle->SetTextSize(0.05);
  gStyle->SetTextFont(42);
  gStyle->SetPadRightMargin(0.043);
  gStyle->SetPadLeftMargin(0.18);
  gStyle->SetPadTopMargin(0.1);
  gStyle->SetPadBottomMargin(0.145);
  gStyle->SetTitleX(.0f);
	gStyle->SetOptStat(0);

  if(doweight==0) {
    weightgen="1";
    weight="1";
  }

  // pp weight
  if(doweight==1) {
    weightgen="(pthatweight)*(Max$(Gpt)<pthat/1.2)*(0.0116437+Max$(Gpt)*(0.0602697)+Max$(Gpt)*Max$(Gpt)*(-0.00226879)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(3.91035e-05)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(-3.0699e-07)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(8.73234e-10))";
    weight="(pthatweight)*(Max$(Gpt)<pthat/1.2)*(0.0116437+Max$(Gpt)*(0.0602697)+Max$(Gpt)*Max$(Gpt)*(-0.00226879)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(3.91035e-05)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(-3.0699e-07)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(8.73234e-10))";
  }
  //PbPb weight
  if(doweight==2) {
    weightgen="(pthatweight)*(Max$(Gpt)<pthat/1.2)*(6.14981+hiBin*(-0.156513)+hiBin*hiBin*(0.00149127)+hiBin*hiBin*hiBin*(-6.29087e-06)+hiBin*hiBin*hiBin*hiBin*(9.90029e-09))";
    weight="(pthatweight)*(Max$(Gpt)<pthat/1.2)*(6.14981+hiBin*(-0.156513)+hiBin*hiBin*(0.00149127)+hiBin*hiBin*hiBin*(-6.29087e-06)+hiBin*hiBin*hiBin*hiBin*(9.90029e-09))";
  }
  //PbPb low pt weight
  if(doweight==3) {
    weightgen="(pthatweight)*(Max$(Gpt)<pthat/1.2)*(6.14981+hiBin*(-0.156513)+hiBin*hiBin*(0.00149127)+hiBin*hiBin*hiBin*(-6.29087e-06)+hiBin*hiBin*hiBin*hiBin*(9.90029e-09))*(-0.00600791+Max$(Gpt)*(0.0838585)+Max$(Gpt)*Max$(Gpt)*(-0.00991096)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(0.000496019)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(-8.50065e-06))";
    weight="(pthatweight)*(Max$(Gpt)<pthat/1.2)*(6.14981+hiBin*(-0.156513)+hiBin*hiBin*(0.00149127)+hiBin*hiBin*hiBin*(-6.29087e-06)+hiBin*hiBin*hiBin*hiBin*(9.90029e-09))*(-0.00600791+Max$(Gpt)*(0.0838585)+Max$(Gpt)*Max$(Gpt)*(-0.00991096)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(0.000496019)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(-8.50065e-06))";
  }
  if(doweight<0 || doweight>4) std::cout<<"ERROR, this weighting option is not defined"<<std::endl;

  std::cout<<"we are using weight="<<weight<<std::endl;

	weight="pthatweight";
	weightgen="pthatweight";

  TFile* infppMCP_pthatFilter = new TFile(inppMCP_pthatFilter.Data());
	
  TTree* ntGenppMCP_pthatFilter = (TTree*)infppMCP_pthatFilter->Get("ntGen");
  ntGenppMCP_pthatFilter->AddFriend("ntHlt");
  ntGenppMCP_pthatFilter->AddFriend("ntHi");

  TTree* ntppMCP_pthatFilter = (TTree*)infppMCP_pthatFilter->Get("ntDkpi");
  ntppMCP_pthatFilter->AddFriend("ntHlt");
  ntppMCP_pthatFilter->AddFriend("ntHi");
  ntppMCP_pthatFilter->AddFriend("ntSkim");
  ntppMCP_pthatFilter->AddFriend(ntGenppMCP_pthatFilter);


  TH1D* hPtppMCP_pthatFilter = new TH1D("hPtppMCP_pthatFilter","",nPtBins,ptBins);
  TH1D* hPtGenppMCP_pthatFilter = new TH1D("hPtGenppMCP_pthatFilter","",nPtBins,ptBins);

  ntppMCP_pthatFilter->Project("hPtppMCP_pthatFilter","Dpt",TCut(weight)*(TCut(selmceff.Data())&&"(Dgen==23333)"));
  divideBinWidth(hPtppMCP_pthatFilter);
  ntGenppMCP_pthatFilter->Project("hPtGenppMCP_pthatFilter","Gpt",TCut(weightgen)*(TCut(selmcgen.Data())));
	divideBinWidth(hPtGenppMCP_pthatFilter);

  hPtppMCP_pthatFilter->Sumw2();
  TH1D* hEffppMCP_pthatFilter = (TH1D*)hPtppMCP_pthatFilter->Clone("hEffppMCP_pthatFilter");
  hEffppMCP_pthatFilter->SetTitle(";D^{0} p_{T} (GeV/c);Efficiency");
  hEffppMCP_pthatFilter->Sumw2();
  hEffppMCP_pthatFilter->Divide(hPtGenppMCP_pthatFilter);
  TCanvas* cEffppMCP_pthatFilter = new TCanvas("cEffppMCP_pthatFilter","",600,600);
	hEffppMCP_pthatFilter->Draw();

	cEffppMCP_pthatFilter->SaveAs("EffppMCP_pthatFilter.png");



  TFile* infppMCNP_pthatFilter = new TFile(inppMCNP_pthatFilter.Data());

  TTree* ntGenppMCNP_pthatFilter = (TTree*)infppMCNP_pthatFilter->Get("ntGen");
  ntGenppMCNP_pthatFilter->AddFriend("ntHlt");
  ntGenppMCNP_pthatFilter->AddFriend("ntHi");

  TTree* ntppMCNP_pthatFilter = (TTree*)infppMCNP_pthatFilter->Get("ntDkpi");
  ntppMCNP_pthatFilter->AddFriend("ntHlt");
  ntppMCNP_pthatFilter->AddFriend("ntHi");
  ntppMCNP_pthatFilter->AddFriend("ntSkim");
  ntppMCNP_pthatFilter->AddFriend(ntGenppMCNP_pthatFilter);


  TH1D* hPtppMCNP_pthatFilter = new TH1D("hPtppMCNP_pthatFilter","",nPtBins,ptBins);
  TH1D* hPtGenppMCNP_pthatFilter = new TH1D("hPtGenppMCNP_pthatFilter","",nPtBins,ptBins);

  ntppMCNP_pthatFilter->Project("hPtppMCNP_pthatFilter","Dpt",TCut(weight)*(TCut(selmceff.Data())&&"(Dgen==23333)"));
  divideBinWidth(hPtppMCNP_pthatFilter);
  ntGenppMCNP_pthatFilter->Project("hPtGenppMCNP_pthatFilter","Gpt",TCut(weightgen)*(TCut(selmcgen.Data())));
  divideBinWidth(hPtGenppMCNP_pthatFilter);

  hPtppMCNP_pthatFilter->Sumw2();
  TH1D* hEffppMCNP_pthatFilter = (TH1D*)hPtppMCNP_pthatFilter->Clone("hEffppMCNP_pthatFilter");
  hEffppMCNP_pthatFilter->SetTitle(";D^{0} p_{T} (GeV/c);Efficiency");
  hEffppMCNP_pthatFilter->Sumw2();
  hEffppMCNP_pthatFilter->Divide(hPtGenppMCNP_pthatFilter);
  TCanvas* cEffppMCNP_pthatFilter = new TCanvas("cEffppMCNP_pthatFilter","",600,600);
  hEffppMCNP_pthatFilter->Draw();

  cEffppMCNP_pthatFilter->SaveAs("EffppMCNP_pthatFilter.png");

// no pthat weight for new MC sample
  weight="1";
  weightgen="1";


  TFile* infppMCP_DptFilter = new TFile(inppMCP_DptFilter.Data());
	
  TTree* ntGenppMCP_DptFilter = (TTree*)infppMCP_DptFilter->Get("ntGen");
  ntGenppMCP_DptFilter->AddFriend("ntHlt");
  ntGenppMCP_DptFilter->AddFriend("ntHi");

  TTree* ntppMCP_DptFilter = (TTree*)infppMCP_DptFilter->Get("ntDkpi");
  ntppMCP_DptFilter->AddFriend("ntHlt");
  ntppMCP_DptFilter->AddFriend("ntHi");
  ntppMCP_DptFilter->AddFriend("ntSkim");
  ntppMCP_DptFilter->AddFriend(ntGenppMCP_DptFilter);


  TH1D* hPtppMCP_DptFilter = new TH1D("hPtppMCP_DptFilter","",nPtBins,ptBins);
  TH1D* hPtGenppMCP_DptFilter = new TH1D("hPtGenppMCP_DptFilter","",nPtBins,ptBins);

  ntppMCP_DptFilter->Project("hPtppMCP_DptFilter","Dpt",TCut(weight)*(TCut(selmceff.Data())&&"(Dgen==23333)"));
  divideBinWidth(hPtppMCP_DptFilter);
  ntGenppMCP_DptFilter->Project("hPtGenppMCP_DptFilter","Gpt",TCut(weightgen)*(TCut(selmcgen.Data())));
	divideBinWidth(hPtGenppMCP_DptFilter);

  hPtppMCP_DptFilter->Sumw2();
  TH1D* hEffppMCP_DptFilter = (TH1D*)hPtppMCP_DptFilter->Clone("hEffppMCP_DptFilter");
  hEffppMCP_DptFilter->SetTitle(";D^{0} p_{T} (GeV/c);Efficiency");
  hEffppMCP_DptFilter->Sumw2();
  hEffppMCP_DptFilter->Divide(hPtGenppMCP_DptFilter);
  TCanvas* cEffppMCP_DptFilter = new TCanvas("cEffppMCP_DptFilter","",600,600);
	hEffppMCP_DptFilter->Draw();

	cEffppMCP_DptFilter->SaveAs("EffppMCP_DptFilter.png");

  TFile* infppMCNP_DptFilter = new TFile(inppMCNP_DptFilter.Data());

  TTree* ntGenppMCNP_DptFilter = (TTree*)infppMCNP_DptFilter->Get("ntGen");
  ntGenppMCNP_DptFilter->AddFriend("ntHlt");
  ntGenppMCNP_DptFilter->AddFriend("ntHi");

  TTree* ntppMCNP_DptFilter = (TTree*)infppMCNP_DptFilter->Get("ntDkpi");
  ntppMCNP_DptFilter->AddFriend("ntHlt");
  ntppMCNP_DptFilter->AddFriend("ntHi");
  ntppMCNP_DptFilter->AddFriend("ntSkim");
  ntppMCNP_DptFilter->AddFriend(ntGenppMCNP_DptFilter);


  TH1D* hPtppMCNP_DptFilter = new TH1D("hPtppMCNP_DptFilter","",nPtBins,ptBins);
  TH1D* hPtGenppMCNP_DptFilter = new TH1D("hPtGenppMCNP_DptFilter","",nPtBins,ptBins);

  ntppMCNP_DptFilter->Project("hPtppMCNP_DptFilter","Dpt",TCut(weight)*(TCut(selmceff.Data())&&"(Dgen==23333)"));
  divideBinWidth(hPtppMCNP_DptFilter);
  ntGenppMCNP_DptFilter->Project("hPtGenppMCNP_DptFilter","Gpt",TCut(weightgen)*(TCut(selmcgen.Data())));
  divideBinWidth(hPtGenppMCNP_DptFilter);

  hPtppMCNP_DptFilter->Sumw2();
  TH1D* hEffppMCNP_DptFilter = (TH1D*)hPtppMCNP_DptFilter->Clone("hEffppMCNP_DptFilter");
  hEffppMCNP_DptFilter->SetTitle(";D^{0} p_{T} (GeV/c);Efficiency");
  hEffppMCNP_DptFilter->Sumw2();
  hEffppMCNP_DptFilter->Divide(hPtGenppMCNP_DptFilter);
  TCanvas* cEffppMCNP_DptFilter = new TCanvas("cEffppMCNP_DptFilter","",600,600);
  hEffppMCNP_DptFilter->Draw();

  cEffppMCNP_DptFilter->SaveAs("EffppMCNP_DptFilter.png");


	TCanvas *c_effAll = new TCanvas("c_effAll","c_effAll",1000,1000);
	c_effAll->Divide(2,2);

	TH1D *hEffppMCP_ratio=(TH1D*)hEffppMCP_DptFilter->Clone("hEffppMCP_ratio");
	hEffppMCP_ratio->Sumw2();
	hEffppMCP_ratio->Divide(hEffppMCP_pthatFilter);

  TH1D *hEffppMCNP_ratio=(TH1D*)hEffppMCNP_DptFilter->Clone("hEffppMCNP_ratio");
  hEffppMCNP_ratio->Sumw2();
  hEffppMCNP_ratio->Divide(hEffppMCNP_pthatFilter);

  c_effAll->cd(1);
	gPad->SetLogx();
	hEffppMCP_DptFilter->Draw();
	hEffppMCP_DptFilter->SetLineColor(2);
	hEffppMCP_pthatFilter->Draw("eSAME");
	hEffppMCP_pthatFilter->SetLineColor(4);

	TLegend *le_hEffppMCP = new TLegend(0.24,0.68,0.48,0.88,NULL,"brNDC");
	le_hEffppMCP->SetBorderSize(0);
  le_hEffppMCP->AddEntry((TObject*)0,"pp MC efficiency","");
	le_hEffppMCP->AddEntry(hEffppMCP_DptFilter,"Prompt new MC");
	le_hEffppMCP->AddEntry(hEffppMCP_pthatFilter,"Prompt old MC");
	le_hEffppMCP->Draw("same");

  c_effAll->cd(2);
  gPad->SetLogx();
  hEffppMCNP_DptFilter->Draw();
  hEffppMCNP_DptFilter->SetLineColor(2);
  hEffppMCNP_pthatFilter->Draw("eSAME");
  hEffppMCNP_pthatFilter->SetLineColor(4);

  TLegend *le_hEffppMCNP = new TLegend(0.24,0.68,0.48,0.88,NULL,"brNDC");
  le_hEffppMCNP->SetBorderSize(0);
	le_hEffppMCNP->AddEntry((TObject*)0,"pp MC efficiency","");
  le_hEffppMCNP->AddEntry(hEffppMCNP_DptFilter,"Nonprompt new MC");
  le_hEffppMCNP->AddEntry(hEffppMCNP_pthatFilter,"Nonrompt old MC");
  le_hEffppMCNP->Draw("same");

	TLine *line1= new TLine(0,1,100,1);
	line1->SetLineStyle(7);

	c_effAll->cd(3);
  gPad->SetLogx();
	hEffppMCP_ratio->GetYaxis()->SetTitle("Eff Ratio");
	hEffppMCP_ratio->SetMarkerStyle(20);
	hEffppMCP_ratio->SetMarkerSize(0.8);
	hEffppMCP_ratio->SetMarkerColor(4);
	hEffppMCP_ratio->SetLineColor(4);
	hEffppMCP_ratio->Draw();
	line1->Draw("SAME");	
	TLatex* texppMCPratio = new TLatex(0.4,0.4,"pp Prompt D eff ratio new/old");
	texppMCPratio->SetNDC();
	texppMCPratio->SetTextSize(0.035);
	texppMCPratio->Draw();

	
	
	c_effAll->cd(4);
  gPad->SetLogx();
  hEffppMCNP_ratio->GetYaxis()->SetTitle("Eff Ratio");
  hEffppMCNP_ratio->SetMarkerStyle(20);
	hEffppMCNP_ratio->SetMarkerSize(0.8);
  hEffppMCNP_ratio->SetMarkerColor(4);
	hEffppMCNP_ratio->SetLineColor(4);
	hEffppMCNP_ratio->Draw();
  line1->Draw("SAME");
  TLatex* texppMCNPratio = new TLatex(0.4,0.4,"pp Nonrompt D eff ratio new/old");
  texppMCNPratio->SetNDC();
  texppMCNPratio->SetTextSize(0.035);
  texppMCNPratio->Draw();


	
	c_effAll->SaveAs("effAll.png");

// continue work here		

	weight="pthatweight";

	TH1D *h_ptMCP_pthatFilter = new TH1D("h_ptMCP_pthatFilter","h_ptMCP_pthatFilter",40,2,4);
  ntppMCP_pthatFilter->Project("h_ptMCP_pthatFilter","Dpt",TCut(weight)*(TCut(selmceff.Data())&&"(Dgen==23333)"));
	h_ptMCP_pthatFilter->Sumw2();
	h_ptMCP_pthatFilter->Scale(1.0/h_ptMCP_pthatFilter->Integral());

  TH1D *h_ptMCP_DptFilter = new TH1D("h_ptMCP_DptFilter","h_ptMCP_DptFilter",40,2,4);
  ntppMCP_DptFilter->Project("h_ptMCP_DptFilter","Dpt",(TCut(selmceff.Data())&&"(Dgen==23333)"));
	h_ptMCP_DptFilter->Sumw2();
  h_ptMCP_DptFilter->Scale(1.0/h_ptMCP_DptFilter->Integral());

	TCanvas *c_Dptdis = new TCanvas("c_Dptdis","c_Dptdis",600,600);
	c_Dptdis->cd();
	h_ptMCP_pthatFilter->Draw();
	h_ptMCP_pthatFilter->SetLineColor(4);
	h_ptMCP_DptFilter->SetLineColor(2);
	h_ptMCP_DptFilter->Draw("SAME");
	c_Dptdis->SaveAs("plot/c_Dptdis.png");


  TH1D *h_yMCP_pthatFilter = new TH1D("h_yMCP_pthatFilter","h_yMCP_pthatFilter",20,-1,1);
  ntppMCP_pthatFilter->Project("h_yMCP_pthatFilter","Dy",TCut(weight)*(TCut(selmceff.Data())&&"(Dgen==23333)&&(Dpt>2 && Dpt< 4)"));
  h_yMCP_pthatFilter->Sumw2();
  h_yMCP_pthatFilter->Scale(1.0/h_yMCP_pthatFilter->Integral());

  TH1D *h_yMCP_DptFilter = new TH1D("h_yMCP_DptFilter","h_yMCP_DptFilter",20,-1,1);
  ntppMCP_DptFilter->Project("h_yMCP_DptFilter","Dy",(TCut(selmceff.Data())&&"(Dgen==23333)&&(Dpt>2 && Dpt< 4)"));
  h_yMCP_DptFilter->Sumw2();
  h_yMCP_DptFilter->Scale(1.0/h_yMCP_DptFilter->Integral());

  TCanvas *c_Dydis = new TCanvas("c_Dydis","c_Dydis",600,600);
  c_Dydis->cd();
  h_yMCP_pthatFilter->Draw();
  h_yMCP_pthatFilter->SetLineColor(4);
  h_yMCP_DptFilter->SetLineColor(2);
  h_yMCP_DptFilter->Draw("SAME");
  c_Dydis->SaveAs("plot/c_Dydis.png");


  TH1D *h_DcaMCP_pthatFilter = new TH1D("h_DcaMCP_pthatFilter","h_DcaMCP_pthatFilter",nDcaBins,dcaBins);
  ntppMCP_pthatFilter->Project("h_DcaMCP_pthatFilter","DsvpvDistance*sin(Dalpha)",TCut(weight)*(TCut(selmceff.Data())&&"(Dgen==23333)&&(Dpt>2 && Dpt< 4)"));
  h_DcaMCP_pthatFilter->Sumw2();
  h_DcaMCP_pthatFilter->Scale(1.0/h_DcaMCP_pthatFilter->Integral());

  TH1D *h_DcaMCP_DptFilter = new TH1D("h_DcaMCP_DptFilter","h_DcaMCP_DptFilter",nDcaBins,dcaBins);
  ntppMCP_DptFilter->Project("h_DcaMCP_DptFilter","DsvpvDistance*sin(Dalpha)",(TCut(selmceff.Data())&&"(Dgen==23333)&&(Dpt>2 && Dpt< 4)"));
  h_DcaMCP_DptFilter->Sumw2();
  h_DcaMCP_DptFilter->Scale(1.0/h_DcaMCP_DptFilter->Integral());

  TCanvas *c_DDcadis = new TCanvas("c_DDcadis","c_DDcadis",600,600);
  c_DDcadis->cd();
	gPad->SetLogy();
  h_DcaMCP_pthatFilter->Draw();
  h_DcaMCP_pthatFilter->SetLineColor(4);
  h_DcaMCP_DptFilter->SetLineColor(2);
  h_DcaMCP_DptFilter->Draw("SAME");
  c_DDcadis->SaveAs("plot/c_DDcadis.png");




// end of main, save all into output file

  TFile *outf = TFile::Open(outfname.Data(),"recreate");
	outf->cd();
	hEffppMCP_pthatFilter->Write();
  hEffppMCNP_pthatFilter->Write();
  hEffppMCP_DptFilter->Write();
  hEffppMCNP_DptFilter->Write();
	hEffppMCP_ratio->Write();
	hEffppMCNP_ratio->Write();
	outf->Write();
	outf->Close();


}
TF1* fit(TString variable, TString variableplot, TTree* nt, TTree* ntMC, Double_t ptmin, Double_t ptmax, int isMC)
{
  static int count=0;
  count++;

  TCanvas* c= new TCanvas(Form("c%d",count),"",600,600);
  TH1D* h = new TH1D(Form("h-%d",count),"",nbinsmasshisto,minhisto,maxhisto);
  TH1D* hMCSignal = new TH1D(Form("hMCSignal-%d",count),"",nbinsmasshisto,minhisto,maxhisto);
  TH1D* hMCSwapped = new TH1D(Form("hMCSwapped-%d",count),"",nbinsmasshisto,minhisto,maxhisto);
  
  TF1* f = new TF1(Form("f%d",count),"[0]*([7]*([9]*Gaus(x,[1],[2])/(sqrt(2*3.14159)*[2])+(1-[9])*Gaus(x,[1],[10])/(sqrt(2*3.14159)*[10]))+(1-[7])*Gaus(x,[1],[8])/(sqrt(2*3.14159)*[8]))+[3]+[4]*x+[5]*x*x+[6]*x*x*x", 1.7, 2.0);
  TString mycut=Form("(%s&&(%s)>%f&&(%s)<%f)",seldata.Data(),variable.Data(),ptmin,variable.Data(),ptmax);
  if(isMC==1) nt->Project(Form("h-%d",count),"Dmass",TCut(weight)*TCut(mycut));   
  else nt->Project(Form("h-%d",count),"Dmass",Form("(%s&&(%s)>%f&&(%s)<%f)",seldata.Data(),variable.Data(),ptmin,variable.Data(),ptmax));   
  
  ntMC->Project(Form("hMCSignal-%d",count),"Dmass",Form("%s*(%s&&(%s)>%f&&(%s)<%f&&(Dgen==23333))",weight.Data(),selmc.Data(),variable.Data(),ptmin,variable.Data(),ptmax));   
  ntMC->Project(Form("hMCSwapped-%d",count),"Dmass",Form("%s*(%s&&(%s)>%f&&(%s)<%f&&(Dgen==23344))",weight.Data(),selmc.Data(),variable.Data(),ptmin,variable.Data(),ptmax));   
  
  
  TFile *fout=new TFile(Form("FitsFiles/Fits_%s_%d.root",collisionsystem.Data(),count),"recreate");
  fout->cd();
  hMCSignal->Write();
  hMCSwapped->Write();
  h->Write();  
  fout->Close();
  
  
  f->SetParLimits(4,-1000,1000);
  f->SetParLimits(10,0.001,0.05);
  f->SetParLimits(2,0.01,0.1);
  f->SetParLimits(8,0.02,0.2);
  f->SetParLimits(7,0,1);
  f->SetParLimits(9,0,1);
  
  f->SetParameter(0,setparam0);
  f->SetParameter(1,setparam1);
  f->SetParameter(2,setparam2);
  f->SetParameter(10,setparam10);
  f->SetParameter(9,setparam9);

  f->FixParameter(8,setparam8);
  f->FixParameter(7,1);
  f->FixParameter(1,fixparam1);
  f->FixParameter(3,0);
  f->FixParameter(4,0);
  f->FixParameter(5,0);
  f->FixParameter(6,0);
  h->GetEntries();
  
  hMCSignal->Fit(Form("f%d",count),"q","",minhisto,maxhisto);
  hMCSignal->Fit(Form("f%d",count),"q","",minhisto,maxhisto);
  f->ReleaseParameter(1);
  hMCSignal->Fit(Form("f%d",count),"L q","",minhisto,maxhisto);
  hMCSignal->Fit(Form("f%d",count),"L q","",minhisto,maxhisto);
  hMCSignal->Fit(Form("f%d",count),"L m","",minhisto,maxhisto);
  
  f->FixParameter(1,f->GetParameter(1));
  f->FixParameter(2,f->GetParameter(2));
  f->FixParameter(10,f->GetParameter(10));
  f->FixParameter(9,f->GetParameter(9));
  f->FixParameter(7,0);
  f->ReleaseParameter(8);
  f->SetParameter(8,setparam8);
  
  hMCSwapped->Fit(Form("f%d",count),"L q","",minhisto,maxhisto);
  hMCSwapped->Fit(Form("f%d",count),"L q","",minhisto,maxhisto);
  hMCSwapped->Fit(Form("f%d",count),"L q","",minhisto,maxhisto);
  hMCSwapped->Fit(Form("f%d",count),"L m","",minhisto,maxhisto);
  
  f->FixParameter(7,hMCSignal->Integral(0,1000)/(hMCSwapped->Integral(0,1000)+hMCSignal->Integral(0,1000)));
  f->FixParameter(8,f->GetParameter(8));
  f->ReleaseParameter(3);
  f->ReleaseParameter(4);
  f->ReleaseParameter(5);
  f->ReleaseParameter(6);

  f->SetLineColor(kRed);
  
  h->Fit(Form("f%d",count),"q","",minhisto,maxhisto);
  h->Fit(Form("f%d",count),"q","",minhisto,maxhisto);
  f->ReleaseParameter(1);
  //f->ReleaseParameter(2);                                     // you need to release these two parameters if you want to perform studies on the sigma shape
  //f->ReleaseParameter(10);                                   // you need to release these two parameters if you want to perform studies on the sigma shape
  h->Fit(Form("f%d",count),"L q","",minhisto,maxhisto);
  h->Fit(Form("f%d",count),"L q","",minhisto,maxhisto);
  h->Fit(Form("f%d",count),"L q","",minhisto,maxhisto);
  h->Fit(Form("f%d",count),"L m","",minhisto,maxhisto);
  
  TF1* background = new TF1(Form("background%d",count),"[0]+[1]*x+[2]*x*x+[3]*x*x*x");
  background->SetParameter(0,f->GetParameter(3));
  background->SetParameter(1,f->GetParameter(4));
  background->SetParameter(2,f->GetParameter(5));
  background->SetParameter(3,f->GetParameter(6));
  background->SetLineColor(4);
  background->SetRange(minhisto,maxhisto);
  background->SetLineStyle(2);
  
  TF1* mass = new TF1(Form("fmass%d",count),"[0]*([3]*([4]*Gaus(x,[1],[2])/(sqrt(2*3.14159)*[2])+(1-[4])*Gaus(x,[1],[5])/(sqrt(2*3.14159)*[5])))");
  mass->SetParameters(f->GetParameter(0),f->GetParameter(1),f->GetParameter(2),f->GetParameter(7),f->GetParameter(9),f->GetParameter(10));
  mass->SetParError(0,f->GetParError(0));
  mass->SetParError(1,f->GetParError(1));
  mass->SetParError(2,f->GetParError(2));
  mass->SetParError(3,f->GetParError(7));
  mass->SetParError(4,f->GetParError(9));
  mass->SetParError(5,f->GetParError(10));
  mass->SetFillColor(kOrange-3);
  mass->SetFillStyle(3002);
  mass->SetLineColor(kOrange-3);
  mass->SetLineWidth(3);
  mass->SetLineStyle(2);
  
  TF1* massSwap = new TF1(Form("fmassSwap%d",count),"[0]*(1-[2])*Gaus(x,[1],[3])/(sqrt(2*3.14159)*[3])");
  massSwap->SetParameters(f->GetParameter(0),f->GetParameter(1),f->GetParameter(7),f->GetParameter(8));
  massSwap->SetParError(0,f->GetParError(0));
  massSwap->SetParError(1,f->GetParError(1));
  massSwap->SetParError(2,f->GetParError(7));
  massSwap->SetParError(3,f->GetParError(8));
  massSwap->SetFillColor(kGreen+4);
  massSwap->SetFillStyle(3005);
  massSwap->SetLineColor(kGreen+4);
  massSwap->SetLineWidth(3);
  massSwap->SetLineStyle(1);
  
  h->SetXTitle("m_{#piK} (GeV/c^{2})");
  h->SetYTitle("Entries / (5 MeV/c^{2})");
  h->GetXaxis()->CenterTitle();
  h->GetYaxis()->CenterTitle();
  h->SetAxisRange(0,h->GetMaximum()*1.4*1.2,"Y");
  h->GetXaxis()->SetTitleOffset(1.3);
  h->GetYaxis()->SetTitleOffset(1.8);
  h->GetXaxis()->SetLabelOffset(0.007);
  h->GetYaxis()->SetLabelOffset(0.007);
  h->GetXaxis()->SetTitleSize(0.045);
  h->GetYaxis()->SetTitleSize(0.045);
  h->GetXaxis()->SetTitleFont(42);
  h->GetYaxis()->SetTitleFont(42);
  h->GetXaxis()->SetLabelFont(42);
  h->GetYaxis()->SetLabelFont(42);
  h->GetXaxis()->SetLabelSize(0.04);
  h->GetYaxis()->SetLabelSize(0.04);
  h->SetMarkerSize(0.8);
  h->SetMarkerStyle(20);
  h->SetStats(0);
  h->Draw("e");

  background->Draw("same");   
  mass->SetRange(minhisto,maxhisto);	
  mass->Draw("same");
  massSwap->SetRange(minhisto,maxhisto);
  massSwap->Draw("same");
  f->Draw("same");
  
  Double_t yield = mass->Integral(minhisto,maxhisto)/binwidthmass;
  Double_t yieldErr = mass->Integral(minhisto,maxhisto)/binwidthmass*mass->GetParError(0)/mass->GetParameter(0);
  
  std::cout<<"YIELD="<<yield<<"and error"<<yieldErr<<std::endl;
  std::cout<<"relative error="<<yieldErr/yield<<std::endl;

  TLegend* leg = new TLegend(0.65,0.58,0.82,0.88,NULL,"brNDC");
  leg->SetBorderSize(0);
  leg->SetTextSize(0.04);
  leg->SetTextFont(42);
  leg->SetFillStyle(0);
  leg->AddEntry(h,"Data","pl");
  leg->AddEntry(f,"Fit","l");
  leg->AddEntry(mass,"D^{0}+#bar{D^{#lower[0.2]{0}}} Signal","f");
  leg->AddEntry(massSwap,"K-#pi swapped","f");
  leg->AddEntry(background,"Combinatorial","l");
  leg->Draw("same");

  TLatex Tl;
  Tl.SetNDC();
  Tl.SetTextAlign(12);
  Tl.SetTextSize(0.04);
  Tl.SetTextFont(42);
  Tl.DrawLatex(0.18,0.93, "#scale[1.25]{CMS} Preliminary");
  Tl.DrawLatex(0.65,0.93, Form("%s #sqrt{s_{NN}} = 5.02 TeV",collisionsystem.Data()));

  TLatex* tex;

  tex = new TLatex(0.22,0.78,Form("%.1f < %s  < %.1f",ptmin,variableplot.Data(),ptmax));
  tex->SetNDC();
  tex->SetTextFont(42);
  tex->SetTextSize(0.04);
  tex->SetLineWidth(2);
  tex->Draw();

  tex = new TLatex(0.22,0.83,"|y| < 1.0");
  tex->SetNDC();
  tex->SetTextFont(42);
  tex->SetTextSize(0.04);
  tex->SetLineWidth(2);
  tex->Draw();

  h->GetFunction(Form("f%d",count))->Delete();
  TH1F* histo_copy_nofitfun = ( TH1F * ) h->Clone("histo_copy_nofitfun");
  histo_copy_nofitfun->Draw("esame");
//
  if(nBins==1) c->SaveAs(Form("DMass-inclusive%s_%d.pdf",collisionsystem.Data(),count));
  else c->SaveAs(Form("DMass%s_%d.pdf",collisionsystem.Data(),count));
  
  return mass;
}
void MakeDsToHadronic(Int_t dataset){
  TChain chain("ntp1");
  
  TString path="DsTolnu/SP1005SigHadronic/Ntuples";
  if(dataset==1)path="DsTolnu/SP1005SigHadronic/Ntuples";
  if(dataset==2)path="DsTolnu/SP1005SigHadronic/NtuplesWS";
  if(dataset==1||dataset==2){
    TreeChain(&chain,1,24,path+"/Run1On/Ntuple");
    TreeChain(&chain,1,2,path+"/Run1Off/Ntuple");
    TreeChain(&chain,1,68,path+"/Run2On/Ntuple");
    TreeChain(&chain,1,6,path+"/Run2Off/Ntuple");
    TreeChain(&chain,1,34,path+"/Run3On/Ntuple");
    TreeChain(&chain,1,2,path+"/Run3Off/Ntuple");
    TreeChain(&chain,1,100,path+"/Run4On/Ntuple");
    TreeChain(&chain,1,6,path+"/Run4Off/Ntuple");
    TreeChain(&chain,1,148,path+"/Run5On/Ntuple");
    TreeChain(&chain,1,14,path+"/Run5Off/Ntuple");
    TreeChain(&chain,1,42,path+"/Run6On/Ntuple");
    TreeChain(&chain,1,10,path+"/Run6Off/Ntuple");
  }

  if(dataset==11)path="DsTolnu/SP1005SkimHadronic/Ntuples";
  if(dataset==12)path="DsTolnu/SP1005SkimHadronic/NtuplesWS";
  if(dataset==11||dataset==12){
    TreeChain(&chain,1,99,path+"/Run1/Ntuple");
    TreeChain(&chain,1,282,path+"/Run2/Ntuple");
    TreeChain(&chain,1,140,path+"/Run3/Ntuple");
    TreeChain(&chain,1,422,path+"/Run4/Ntuple");
    TreeChain(&chain,1,612,path+"/Run5/Ntuple");
    TreeChain(&chain,1,175,path+"/Run6/Ntuple");
  }
  


  TCut modecut="!(DsnDaus==4&&Dsd1Lund==310)";//Ds->KsKpipi has narrow mass window with no signal
  TCut pstarcut="Dsp3CM>2.5";
  TCut masscut="1.87<DsMass&&DsMass<2.07";
  TCut masssigcut="abs(DsMass-1.968)<.024";
  TCut masssidecut=".032<abs(DsMass-1.968)&&abs(DsMass-1.968)<.056";
  TCut totcut=modecut+pstarcut+masscut;
  
  //TTree* ntuple=chain->CopyTree(modecut+pstarcut+masscut);
  
  TFile HistoFile(path+"/HistosFile.root","recreate");
 
  
  //Ds mass from Hadronic reco
  TH1F HDsH("HDsH","",nbins,xmin,xmax);
  chain.Draw("DsMass>>HDsH",totcut);
  HDsH.Write();

  //Ds mass from Hadronic reco truth-matched
  TH1F HDsHTrue("HDsHTrue","",nbins,xmin,xmax);
  chain.Draw("DsMass>>HDsHTrue",totcut+TCut("DsMCIdx>0"));
  HDsHTrue.Write();

//Ds* deltam from Hadronic reco
//   C.Clear();
//   TH1F HDsStH("HDsStH","",nbins,xmin,xmax);
//   //chain.Draw("sqrt(Ds*DsDsE-(DsDsPx*DsDsPx+DsDsPy*DsDsPy+DsDsPz*DsPz))>>HDsH");
//   C.Print(filename);

  //Ds mass from Recoil
  TH1F HDsR("HDsR","",ndsmbins,dsmmin,dsmmax);
  chain.Draw("sqrt(DsDsE*DsDsE-(DsDsPx*DsDsPx+DsDsPy*DsDsPy+DsDsPz*DsDsPz))>>HDsR",totcut+masssigcut);
  HDsR.Write();

  //plot of the signal-MC 
//   TH1F HDsR("HDsR","",nbinsrec,xminrec,xmaxrec);
//   chain.Draw("sqrt(DsDsE*DsDsE-(DsDsPx*DsDsPx+DsDsPy*DsDsPy+DsDsPz*DsDsPz))>>HDsR",totcut+masssigcut);
//   HDsR.Write();

  TH1F HDsRSide("HDsRSide","",ndsmbins,dsmmin,dsmmax);
  chain.Draw("sqrt(DsDsE*DsDsE-(DsDsPx*DsDsPx+DsDsPy*DsDsPy+DsDsPz*DsDsPz))>>HDsRSide",totcut+masssidecut);
  HDsRSide.Write();

  //Hadronic mass vs Recoil mass
  Int_t nbinscoarse=25;
  
  for(Int_t b=0;b<ndsmbins;b++){
    TString hname=TString("HDsHVsReco")+(long)b;
    char text[1000];
    sprintf(text,"%.6f<sqrt(DsDsE*DsDsE-(DsDsPx*DsDsPx+DsDsPy*DsDsPy+DsDsPz*DsDsPz))&&sqrt(DsDsE*DsDsE-(DsDsPx*DsDsPx+DsDsPy*DsDsPy+DsDsPz*DsDsPz))<%.6f",dsmmin+b*dsmstep,dsmmin+(b+1)*dsmstep);
    
    TH1F HDsHVsRec(hname,"",nbinscoarse,xmin,xmax);
    TCut mslicecut=text;
    chain.Draw(TString("DsMass>>")+hname,totcut+mslicecut);
    HDsHVsRec.Write();

  }
  HistoFile.Close();

}
void fitDVariable(TString variable="DsvpvDistance/DsvpvDisErr",TString variableplot="d_{xy}/#sigma(d_{xy})", TString inputdata="/data/wangj/MC2015/Dntuple/pp/ntD_pp_Dzero_kpi/ntD_EvtBase_20160203_Dfinder_20160201_pp_Pythia8_prompt_D0_dPt0tkPt0p5_pthatweight.root", TString inputmc="/data/wangj/MC2015/Dntuple/pp/ntD_pp_Dzero_kpi/ntD_EvtBase_20160203_Dfinder_20160201_pp_Pythia8_prompt_D0_dPt0tkPt0p5_pthatweight.root", TString trgselection="1",  TString cut="Dpt>20&&Dy>-1.&&Dy<1.&&Dtrk1highPurity&&Dtrk2highPurity&&Dtrk1Pt>2.0&&Dtrk2Pt>2.0&&(DsvpvDistance/DsvpvDisErr)>3&&(DlxyBS/DlxyBSErr)>1.5&&Dchi2cl>0.05&&Dalpha<0.12&&Dtrk1PtErr/Dtrk1Pt<0.1&&Dtrk2PtErr/Dtrk2Pt<0.1&&abs(Dtrk1Eta)<2.0&&abs(Dtrk2Eta)<2.0&&Dtrk1Algo>3&&Dtrk1Algo<8&&(Dtrk1PixelHit+Dtrk1StripHit)>=11", TString selmcgen="((GisSignal==1||GisSignal==2)&&(Gy>-1&&Gy<1))", int isMC=1, Double_t luminosity=26., int doweight=0, TString collsyst="PbPb", TString outputfile="mytest.root")
{
  collisionsystem=collsyst;
  seldata = Form("%s&&%s",trgselection.Data(),cut.Data());
  selmc = Form("%s",cut.Data());

  gStyle->SetTextSize(0.05);
  gStyle->SetTextFont(42);
  gStyle->SetPadRightMargin(0.043);
  gStyle->SetPadLeftMargin(0.18);
  gStyle->SetPadTopMargin(0.1);
  gStyle->SetPadBottomMargin(0.145);
  gStyle->SetTitleX(.0f);

  void clean0 (TH1D* h);
  TF1* fit (TString variable, TString variableplot, TTree* nt, TTree* ntMC, double ptmin, double ptmax, int isMC);

  if(!doweight) weight="1";
  TFile* inf = new TFile(inputdata.Data());
  TFile* infMC = new TFile(inputmc.Data());

  TTree* nt = (TTree*) inf->Get("ntDkpi");
  TTree* HltTree= (TTree*) inf->Get("ntHlt");
  HltTree->AddFriend(nt);
  nt->AddFriend(HltTree);
  TTree* ntHid = (TTree*) inf->Get("ntHi");
  nt->AddFriend(ntHid);
  
  TTree* ntMC = (TTree*)infMC->Get("ntDkpi");
  TTree* ntGen = (TTree*)infMC->Get("ntGen");
  TTree* ntHi = (TTree*)infMC->Get("ntHi");
  
  ntGen->AddFriend(ntMC);
  ntGen->AddFriend(ntHi);
  ntMC->AddFriend(ntGen);
  ntMC->AddFriend(ntHi);
  ntHi->AddFriend(ntMC);
  
  TH1D* hPt = new TH1D("hPt","",nBins,ptBins);
  TH1D* hPtRecoTruth = new TH1D("hPtRecoTruth","",nBins,ptBins);
  TH1D* hPtMC = new TH1D("hPtMC","",nBins,ptBins);
  TH1D* hPtGen = new TH1D("hPtGen","",nBins,ptBins);

  TH1D* hMean = new TH1D("hMean","",nBins,ptBins);                       
  TH1D* hSigmaGaus1 = new TH1D("hSigmaGaus1","",nBins,ptBins); 
  TH1D* hSigmaGaus2 = new TH1D("hSigmaGaus2","",nBins,ptBins); 
  TH1D* hRelMagnGaus1Gaus2 = new TH1D("hRelMagnGaus1Gaus2","",nBins,ptBins); 
  
  for(int i=0;i<nBins;i++)
    {
      TF1* f = fit(variable,variableplot,nt,ntMC,ptBins[i],ptBins[i+1],isMC);
      double yield = f->Integral(minhisto,maxhisto)/binwidthmass;
      double yieldErr = f->Integral(minhisto,maxhisto)/binwidthmass*f->GetParError(0)/f->GetParameter(0);
      hPt->SetBinContent(i+1,yield/(ptBins[i+1]-ptBins[i]));
      hPt->SetBinError(i+1,yieldErr/(ptBins[i+1]-ptBins[i]));
      hMean->SetBinContent(i+1,f->GetParameter(1));
      hMean->SetBinError(i+1,f->GetParError(1));
      hSigmaGaus1->SetBinContent(i+1,f->GetParameter(2));
      hSigmaGaus1->SetBinError(i+1,f->GetParError(2));
      hSigmaGaus2->SetBinContent(i+1,f->GetParameter(5));
      hSigmaGaus2->SetBinError(i+1,f->GetParError(5));
      hRelMagnGaus1Gaus2->SetBinContent(i+1,f->GetParameter(4));
      hRelMagnGaus1Gaus2->SetBinError(i+1,f->GetParError(4));
    }  

  ntMC->Project("hPtMC",variable.Data(),TCut(weight)*(TCut(selmc.Data())&&"(Dgen==23333)"));
  divideBinWidth(hPtMC);
  ntMC->Project("hPtRecoTruth",variable.Data(),TCut(selmc.Data())&&"(Dgen==23333)");
  divideBinWidth(hPtRecoTruth);
  ntGen->Project("hPtGen","Gpt",TCut(weight)*(TCut(selmcgen.Data())));
  divideBinWidth(hPtGen);

  TCanvas* cPt =  new TCanvas("cPt","",600,600);
  cPt->SetLogy();
  hPt->SetXTitle(Form("D^{0} %s",variable.Data()));
  hPt->SetYTitle("Uncorrected dN(D^{0})/dp_{T}");
  hPt->Sumw2();
  hPt->Draw();
  if(isMC)
    {
      hPtMC->Draw("same hist");
      TLegend* legPt = myLegend(0.55,0.80,0.90,0.94);
      legPt->AddEntry(hPt,"Signal extraction","pl");
      legPt->AddEntry(hPtMC,"Matched reco","lf");
      legPt->Draw("same");  
    }
  hPtMC->Sumw2();
  TH1D* hEff = (TH1D*)hPtMC->Clone("hEff");
  hEff->SetTitle(Form(";D^{0} %s",variable.Data()));
  hEff->Sumw2();
  hEff->Divide(hPtGen);
  TCanvas* cEff = new TCanvas("cEff","",600,600);
  hEff->Draw();
  
  TH1D* hPtCor = (TH1D*)hPt->Clone("hPtCor");
  hPtCor->SetTitle(Form(";D^{0} %s;Corrected dN(D^{0})/dp_{T}",variable.Data()));
  hPtCor->Divide(hEff);
  TCanvas* cPtCor=  new TCanvas("cCorResult","",600,600);
  cPtCor->SetLogy();
  hPtCor->Draw();
  if(isMC)
    {
      hPtGen->Draw("same hist");
      TLegend* legPtCor = myLegend(0.55,0.80,0.90,0.94);
      legPtCor->AddEntry(hPtCor,"Corrected signal","pl");
      legPtCor->AddEntry(hPtGen,"Generated D^{0}","lf");
      legPtCor->Draw("same");  
    }

  TH1D* hPtSigma= (TH1D*)hPtCor->Clone("hPtSigma");
  hPtSigma->SetTitle(Form(";D^{0} %s;d#sigma(D^{0})/d%s (pb/{units})",variable.Data(),variable.Data()));
  hPtSigma->Scale(1./(2*luminosity*BRchain));
  TCanvas* cPtSigma=  new TCanvas("cPtSigma","",600,600);
  cPtSigma->SetLogy();
  hPtSigma->Draw();
  
  TFile* outf = new TFile(outputfile.Data(),"recreate");
  outf->cd();
  hPt->Write();
  hEff->Write();
  hPtGen->Write();
  hPtMC->Write();
  hPtCor->Write();
  hPtSigma->Write();
  hMean->Write();
  hSigmaGaus1->Write();
  hSigmaGaus2->Write();
  hRelMagnGaus1Gaus2->Write();
  outf->Close();
}
Exemple #22
0
void MCefficiency(int isPbPb=0,TString inputmc="/data/wangj/MC2015/Dntuple/pp/revised/ntD_pp_Dzero_kpi_prompt/ntD_EvtBase_20160303_Dfinder_20160302_pp_Pythia8_prompt_D0_dPt0tkPt0p5_pthatweight.root", TString selmcgen="((GisSignal==1||GisSignal==2)&&(Gy>-1&&Gy<1))",TString selmcgenacceptance="((GisSignal==1||GisSignal==2)&&(Gy>-1&&Gy<1))&&abs(Gtk1eta)<2.0&&abs(Gtk2eta)<2.0&&Gtk1pt>2.0&&Gtk2pt>2.0", TString cut_recoonly="Dy>-1.&&Dy<1.&&Dtrk1highPurity&&Dtrk2highPurity&&Dtrk1Pt>2.0&&Dtrk2Pt>2.0&&Dtrk1PtErr/Dtrk1Pt<0.1&&Dtrk2PtErr/Dtrk2Pt<0.1&&abs(Dtrk1Eta)<2.0&&abs(Dtrk2Eta)<2.0&&Dtrk1Algo>3&&Dtrk1Algo<8&&(Dtrk1PixelHit+Dtrk1StripHit)>=11", TString cut="Dy>-1.&&Dy<1.&&Dtrk1highPurity&&Dtrk2highPurity&&Dtrk1Pt>2.0&&Dtrk2Pt>2.0&&(DsvpvDistance/DsvpvDisErr)>3.5&&(DlxyBS/DlxyBSErr)>1.5&&Dchi2cl>0.05&&Dalpha<0.12&&Dtrk1PtErr/Dtrk1Pt<0.1&&Dtrk2PtErr/Dtrk2Pt<0.1&&abs(Dtrk1Eta)<2.0&&abs(Dtrk2Eta)<2.0&&Dtrk1Algo>3&&Dtrk1Algo<8&&Dtrk2Algo>3&&Dtrk2Algo<8&&(Dtrk1PixelHit+Dtrk1StripHit)>=11&&(Dtrk1Chi2ndf/(Dtrk1nStripLayer+Dtrk1nPixelLayer)<0.15)&&(Dtrk2Chi2ndf/(Dtrk2nStripLayer+Dtrk2nPixelLayer)<0.15)",TString label="PP",TString outputfile="test", int useweight=1,Float_t centmin=0., Float_t centmax=100.)
{ 
 
  hiBinMin = centmin*2;
  hiBinMax = centmax*2;
  centMin = centmin;
  centMax = centmax;
  
  if(isPbPb==1)
    {
      selmcgen = selmcgen+Form("&&hiBin>=%f&&hiBin<%f",hiBinMin,hiBinMax);
      selmcgenacceptance=selmcgenacceptance+Form("&&hiBin>=%f&&hiBin<%f",hiBinMin,hiBinMax);
      cut_recoonly=cut_recoonly+Form("&&hiBin>=%f&&hiBin<%f",hiBinMin,hiBinMax);
      cut=cut+Form("&&hiBin>=%f&&hiBin<%f",hiBinMin,hiBinMax);
    }

     std::cout<<"selmcgen="<<selmcgen<<std::endl;
     std::cout<<"selmcgenacceptance="<<selmcgenacceptance<<std::endl;
     std::cout<<"cut_recoonly"<<cut_recoonly<<std::endl;
     std::cout<<"AAAA cut="<<cut<<std::endl;

 std::cout<<"option="<<useweight<<std::endl;
  gStyle->SetOptTitle(0);
  gStyle->SetOptStat(0);
  gStyle->SetEndErrorSize(0);
  gStyle->SetMarkerStyle(20);
 
  TFile* infMC = new TFile(inputmc.Data());
  TTree* ntMC = (TTree*)infMC->Get("ntDkpi");
  TTree* ntGen = (TTree*)infMC->Get("ntGen");
  TTree* ntSkim = (TTree*)infMC->Get("ntSkim");
  ntMC->AddFriend(ntGen);
  ntMC->AddFriend(ntSkim);
  

  TTree* nthi = (TTree*)infMC->Get("ntHi");
  ntGen->AddFriend(nthi);
  ntGen->AddFriend(ntSkim);
  nthi->AddFriend(ntMC);
  ntMC->AddFriend(nthi);

// optimal weigths
if(useweight==0) {
    weightfunctiongen="1";
    weightfunctionreco="1";
  }
  // pp weight
  if(useweight==1) {
     weightfunctiongen="(pthatweight)*(Max$(Gpt)<pthat/1.2)*(0.0116437+Max$(Gpt)*(0.0602697)+Max$(Gpt)*Max$(Gpt)*(-0.00226879)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(3.91035e-05)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(-3.0699e-07)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(8.73234e-10))";
     weightfunctionreco="(pthatweight)*(Max$(Gpt)<pthat/1.2)*(0.0116437+Max$(Gpt)*(0.0602697)+Max$(Gpt)*Max$(Gpt)*(-0.00226879)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(3.91035e-05)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(-3.0699e-07)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(8.73234e-10))";
  }
  //PbPb weight
  if(useweight==2) {
    weightfunctiongen="(pthatweight)*(Max$(Gpt)<pthat/1.2)*(6.14981+hiBin*(-0.156513)+hiBin*hiBin*(0.00149127)+hiBin*hiBin*hiBin*(-6.29087e-06)+hiBin*hiBin*hiBin*hiBin*(9.90029e-09))";
    weightfunctionreco="(pthatweight)*(Max$(Gpt)<pthat/1.2)*(6.14981+hiBin*(-0.156513)+hiBin*hiBin*(0.00149127)+hiBin*hiBin*hiBin*(-6.29087e-06)+hiBin*hiBin*hiBin*hiBin*(9.90029e-09))";
  }
  //PbPb low pt weight
  if(useweight==3) {
    weightfunctiongen="(pthatweight)*(Max$(Gpt)<pthat/1.2)*(6.14981+hiBin*(-0.156513)+hiBin*hiBin*(0.00149127)+hiBin*hiBin*hiBin*(-6.29087e-06)+hiBin*hiBin*hiBin*hiBin*(9.90029e-09))*(-0.00600791+Max$(Gpt)*(0.0838585)+Max$(Gpt)*Max$(Gpt)*(-0.00991096)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(0.000496019)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(-8.50065e-06))";
    weightfunctionreco="(pthatweight)*(Max$(Gpt)<pthat/1.2)*(6.14981+hiBin*(-0.156513)+hiBin*hiBin*(0.00149127)+hiBin*hiBin*hiBin*(-6.29087e-06)+hiBin*hiBin*hiBin*hiBin*(9.90029e-09))*(-0.00600791+Max$(Gpt)*(0.0838585)+Max$(Gpt)*Max$(Gpt)*(-0.00991096)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(0.000496019)+Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*Max$(Gpt)*(-8.50065e-06))";
  }
  if(useweight==4) {
    weightfunctiongen="(pthatweight)*(Max$(Gpt)<pthat/1.2)";
    weightfunctionreco="(pthatweight)*(Max$(Gpt)<pthat/1.2)";
  }


   std::cout<<"fit function parameters="<<weightfunctiongen<<std::endl;

  TH1D* hPtMC = new TH1D("hPtMC","",nBins,ptBins);
  TH1D* hPtMCrecoonly = new TH1D("hPtMCrecoonly","",nBins,ptBins);
  TH1D* hPtGen = new TH1D("hPtGen","",nBins,ptBins);
  TH1D* hPtGenAcc = new TH1D("hPtGenAcc","",nBins,ptBins);
  TH1D* hPthat = new TH1D("hPthat","",100,0,500);
  TH1D* hPthatweight = new TH1D("hPthatweight","",100,0,500);
  
  ntMC->Project("hPtMC","Dpt",TCut(weightfunctionreco)*(TCut(cut.Data())&&"(Dgen==23333)"));
  divideBinWidth(hPtMC);
  std::cout<<"step1"<<std::endl;
  ntMC->Project("hPtMCrecoonly","Dpt",TCut(weightfunctionreco)*(TCut(cut_recoonly.Data())&&"(Dgen==23333)"));
  divideBinWidth(hPtMCrecoonly);
  std::cout<<"step2"<<std::endl;
  ntGen->Project("hPtGen","Gpt",TCut(weightfunctiongen)*(TCut(selmcgen.Data())));
  divideBinWidth(hPtGen);
  std::cout<<"step3"<<std::endl;
  ntGen->Project("hPtGenAcc","Gpt",TCut(weightfunctiongen)*(TCut(selmcgenacceptance.Data())));
  divideBinWidth(hPtGenAcc);
  std::cout<<"step4"<<std::endl;


  ntMC->Project("hPthat","pthat","1");
  ntMC->Project("hPthatweight","pthat",TCut("1"));

  hPtMC->Sumw2();
  TH1D* hEff = (TH1D*)hPtMC->Clone("hEff");
  hEff->Divide(hPtGen);

  TH1D* hEffReco = (TH1D*)hPtMCrecoonly->Clone("hEffReco");
  hEffReco->Sumw2();
  hEffReco->Divide(hPtGen);

  TH1D* hEffAcc = (TH1D*)hPtGenAcc->Clone("hEffAcc");
  hEffAcc->Sumw2();
  hEffAcc->Divide(hEffAcc,hPtGen,1,1,"b");
  
  TH1D* hEffSelection = (TH1D*)hPtMC->Clone("hEffSelection");
  hEffSelection->Sumw2();
  hEffSelection->Divide(hEffSelection,hPtMCrecoonly,1,1,"b");
  
  TH2F* hemptyEff=new TH2F("hemptyEff","",50,ptBins[0]-5.,ptBins[nBins]+5.,10.,0,1.5);  
  hemptyEff->GetXaxis()->CenterTitle();
  hemptyEff->GetYaxis()->CenterTitle();
  hemptyEff->GetYaxis()->SetTitle("acceptance x #epsilon_{reco} x #epsilon_{sel} ");
  hemptyEff->GetXaxis()->SetTitle("p_{T} (GeV/c)");
  hemptyEff->GetXaxis()->SetTitleOffset(0.9);
  hemptyEff->GetYaxis()->SetTitleOffset(0.95);
  hemptyEff->GetXaxis()->SetTitleSize(0.05);
  hemptyEff->GetYaxis()->SetTitleSize(0.05);
  hemptyEff->GetXaxis()->SetTitleFont(42);
  hemptyEff->GetYaxis()->SetTitleFont(42);
  hemptyEff->GetXaxis()->SetLabelFont(42);
  hemptyEff->GetYaxis()->SetLabelFont(42);
  hemptyEff->GetXaxis()->SetLabelSize(0.035);
  hemptyEff->GetYaxis()->SetLabelSize(0.035);  
  hemptyEff->SetMaximum(2);
  hemptyEff->SetMinimum(0.);
  hemptyEff->Draw();

  TH2F* hemptyEffAcc=(TH2F*)hemptyEff->Clone("hemptyEffAcc");
  TH2F* hemptyEffReco=(TH2F*)hemptyEff->Clone("hemptyEffReco");
  TH2F* hemptyEffSelection=(TH2F*)hemptyEff->Clone("hemptyEffSelection");
  
  TCanvas*canvasEff=new TCanvas("canvasEff","canvasEff",1000.,500);
  canvasEff->Divide(2,1);
  canvasEff->cd(1);
  
  hemptyEffAcc->SetYTitle("#alpha");
  hemptyEffAcc->Draw();
  hEffAcc->Draw("same");
  canvasEff->cd(2);
  hemptyEff->Draw();
  hEff->Draw("same");
  canvasEff->SaveAs(Form("plotEff/canvasEff_study%s.pdf",Form(label.Data())));
  
  
  TH2F* hemptyPthat=new TH2F("hemptyPthat","",50,0.,500.,10,1e-5,1e9);  
  hemptyPthat->GetXaxis()->CenterTitle();
  hemptyPthat->GetYaxis()->CenterTitle();
  hemptyPthat->GetYaxis()->SetTitle("Entries");
  hemptyPthat->GetXaxis()->SetTitle("pthat");
  hemptyPthat->GetXaxis()->SetTitleOffset(0.9);
  hemptyPthat->GetYaxis()->SetTitleOffset(0.95);
  hemptyPthat->GetXaxis()->SetTitleSize(0.05);
  hemptyPthat->GetYaxis()->SetTitleSize(0.05);
  hemptyPthat->GetXaxis()->SetTitleFont(42);
  hemptyPthat->GetYaxis()->SetTitleFont(42);
  hemptyPthat->GetXaxis()->SetLabelFont(42);
  hemptyPthat->GetYaxis()->SetLabelFont(42);
  hemptyPthat->GetXaxis()->SetLabelSize(0.035);
  hemptyPthat->GetYaxis()->SetLabelSize(0.035);  
  hemptyPthat->SetMaximum(2);
  hemptyPthat->SetMinimum(0.);


  TH2F* hemptySpectra=new TH2F("hemptySpectra","",50,0.,130.,10,1,1e5);  
  hemptySpectra->GetXaxis()->CenterTitle();
  hemptySpectra->GetYaxis()->CenterTitle();
  hemptySpectra->GetYaxis()->SetTitle("Entries");
  hemptySpectra->GetXaxis()->SetTitle("p_{T}");
  hemptySpectra->GetXaxis()->SetTitleOffset(0.9);
  hemptySpectra->GetYaxis()->SetTitleOffset(0.95);
  hemptySpectra->GetXaxis()->SetTitleSize(0.05);
  hemptySpectra->GetYaxis()->SetTitleSize(0.05);
  hemptySpectra->GetXaxis()->SetTitleFont(42);
  hemptySpectra->GetYaxis()->SetTitleFont(42);
  hemptySpectra->GetXaxis()->SetLabelFont(42);
  hemptySpectra->GetYaxis()->SetLabelFont(42);
  hemptySpectra->GetXaxis()->SetLabelSize(0.035);
  hemptySpectra->GetYaxis()->SetLabelSize(0.035);  

  TH2F* hemptyPthatWeighted=(TH2F*)hemptyPthat->Clone("hemptyPthatWeighted");
  hemptyPthatWeighted->GetXaxis()->SetTitle("pthat reweighted");
  
  TCanvas*canvasPthat=new TCanvas("canvasPthat","canvasPthat",1000.,500);
  canvasPthat->Divide(2,1);
  canvasPthat->cd(1);
  gPad->SetLogy();
  hemptyPthat->Draw("same");
  hPthat->Draw("same");
  canvasPthat->cd(2);
  gPad->SetLogy();
  hemptyPthatWeighted->Draw();
  hPthatweight->Draw("same");
 // canvasPthat->SaveAs(Form("plotEff/canvasPthat_%s.pdf",Form(label.Data())));
  
  TCanvas*canvasSpectra=new TCanvas("canvasSpectra","canvasSpectra",1000.,500);
  canvasSpectra->Divide(2,1);
  canvasSpectra->cd(1);
  gPad->SetLogy();
  hemptySpectra->Draw();
  hPtMC->Draw("same");
  canvasSpectra->cd(2);
  gPad->SetLogy();
  hemptySpectra->Draw();
  hPtGen->Draw("same");

  TFile *fout=new TFile(outputfile.Data(),"recreate");
  fout->cd();
  hPtGen->Write();
  hEffAcc->Write();
  hEffReco->Write();
  hEffSelection->Write();
  hEff->Write();
  hPtMC->Write();
  fout->Close();  

}
Exemple #23
0
void weightPbPbCentrality(){

TFile*fMC=new TFile("/data/HeavyFlavourRun2/MC2015/Bntuple/PbPb/Bntuple20160606_Pythia8_BuToJpsiK_Bpt5p0_Pthat5.root");
//TFile*fMC=new TFile("/data/HeavyFlavourRun2/MC2015/Bntuple/PbPb/Bntuple20160816_Bpt7svpv5p5Bpt10svpv3p5_BfinderMC_PbPb_Pythia8_BuToJpsiK_TuneCUEP8M1_20160816_bPt5jpsiPt0tkPt0p8_Bp_pthatweight_JingBDT.root");
TTree *ntDkpiMC = (TTree*)fMC->Get("ntKp");
TTree *ntSkimMC = (TTree*)fMC->Get("ntSkim");
TTree *ntHiMC = (TTree*)fMC->Get("ntHi");
ntDkpiMC->AddFriend(ntSkimMC);
ntDkpiMC->AddFriend(ntHiMC);

TFile*fData=new TFile("/data/HeavyFlavourRun2/Data2015/Bntuple/Bntuple20160610_crab_BfinderData_PbPb_20160607_bPt5jpsiPt0tkPt0p8_Bp.root");
//TFile*fData=new TFile("/data/HeavyFlavourRun2/Data2015/Bntuple/Bntuple20160816_Bpt7svpv5p5Bpt10svpv3p5_BfinderData_PbPb_20160816_bPt5jpsiPt0tkPt0p8_Bp_JingBDT.root");
TTree *ntDkpiData = (TTree*)fData->Get("ntKp");
TTree *ntSkimData = (TTree*)fData->Get("ntSkim");
TTree *ntHiData = (TTree*)fData->Get("ntHi");
TTree *ntHltData = (TTree*)fData->Get("ntHlt");
ntDkpiData->AddFriend(ntSkimData);
ntDkpiData->AddFriend(ntHiData);
ntDkpiData->AddFriend(ntHltData);

TH1F*hCenData=new TH1F("hCenData","hCenData",200,0,200);
TH1F*hCenMC=new TH1F("hCenMC","hCenMC",200,0,200);

//TCut weighpthat="6.14981+hiBin*(-0.156513)+hiBin*hiBin*(0.00149127)+hiBin*hiBin*hiBin*(-6.29087e-06)+hiBin*hiBin*hiBin*hiBin*(9.90029e-09)";
TCut weighpthat="1";
//TCut weighpthat="pthatweight";
TString cut="abs(PVz)<15&&pclusterCompatibilityFilter&&pprimaryVertexFilter&&phfCoincFilter3";
TString hlt="(HLT_HIL1DoubleMu0_v1 || HLT_HIL1DoubleMu0_part1_v1 || HLT_HIL1DoubleMu0_part2_v1 || HLT_HIL1DoubleMu0_part3_v1)";

ntDkpiMC->Project("hCenMC","hiBin",TCut(weighpthat)*(TCut(cut.Data())));
ntDkpiData->Project("hCenData","hiBin",(TCut(cut.Data())*TCut(hlt.Data())));

hCenMC->Scale(1./hCenMC->Integral(hCenMC->FindBin(0.),hCenMC->FindBin(200)));
hCenData->Scale(1./hCenData->Integral(hCenMC->FindBin(0.),hCenMC->FindBin(200)));

TH2F* hempty1=new TH2F("hempty1","",50,0.,200.,10,0,0.05);  
hempty1->GetXaxis()->CenterTitle();
hempty1->GetYaxis()->CenterTitle();
hempty1->GetYaxis()->SetTitle("Entries");
hempty1->GetXaxis()->SetTitle("Centrality (HiBin)");
hempty1->GetXaxis()->SetTitleOffset(0.9);
hempty1->GetYaxis()->SetTitleOffset(1.5);
hempty1->GetXaxis()->SetTitleSize(0.05);
hempty1->GetYaxis()->SetTitleSize(0.05);
hempty1->GetXaxis()->SetTitleFont(42);
hempty1->GetYaxis()->SetTitleFont(42);
hempty1->GetXaxis()->SetLabelFont(42);
hempty1->GetYaxis()->SetLabelFont(42);
hempty1->GetXaxis()->SetLabelSize(0.035);
hempty1->GetYaxis()->SetLabelSize(0.035);  

TH2F* hempty2=new TH2F("hempty2","",50,0.,200.,10,0,10);  
hempty2->GetXaxis()->CenterTitle();
hempty2->GetYaxis()->CenterTitle();
hempty2->GetYaxis()->SetTitle("Ratio Data/MC");
hempty2->GetXaxis()->SetTitle("Centrality (HiBin)");
hempty2->GetXaxis()->SetTitleOffset(0.9);
hempty2->GetYaxis()->SetTitleOffset(1.5);
hempty2->GetXaxis()->SetTitleSize(0.05);
hempty2->GetYaxis()->SetTitleSize(0.05);
hempty2->GetXaxis()->SetTitleFont(42);
hempty2->GetYaxis()->SetTitleFont(42);
hempty2->GetXaxis()->SetLabelFont(42);
hempty2->GetYaxis()->SetLabelFont(42);
hempty2->GetXaxis()->SetLabelSize(0.035);
hempty2->GetYaxis()->SetLabelSize(0.035);  

TLegend *legendSigma=new TLegend(0.5100806,0.5168644,0.8084677,0.6605932,"");
legendSigma->SetBorderSize(0);
legendSigma->SetLineColor(0);
legendSigma->SetFillColor(0);
legendSigma->SetFillStyle(1001);
legendSigma->SetTextFont(42);
legendSigma->SetTextSize(0.045);

TLegendEntry *ent_SigmaPP=legendSigma->AddEntry(hCenMC,"Monte Carlo.","pf");
ent_SigmaPP->SetTextFont(42);
ent_SigmaPP->SetLineColor(2);
ent_SigmaPP->SetMarkerColor(2);
ent_SigmaPP->SetTextSize(0.03);

TLegendEntry *ent_Sigmapp=legendSigma->AddEntry(hCenData,"Data","f");
ent_Sigmapp->SetTextFont(42);
ent_Sigmapp->SetLineColor(1);
ent_Sigmapp->SetMarkerColor(1);
ent_Sigmapp->SetTextSize(0.03);

TCanvas*canvas=new TCanvas("canvas","canvas",1000,500);
canvas->Divide(2,1);
canvas->cd(1);
hempty1->Draw();
hCenMC->SetLineColor(2);
hCenMC->SetMarkerColor(2);
hCenMC->Draw("same");
hCenData->Draw("same");
legendSigma->Draw();
canvas->cd(2);
TH1D*hRatio=(TH1D*)hCenData->Clone("hRatio");
hRatio->Divide(hCenMC);
hempty2->Draw();
hRatio->Draw("same");

TF1 *myfit = new TF1("myfit","[0]+[1]*x+x*x*[2]+x*x*x*[3]+x*x*x*x*[4]", 0, 200);  
hRatio->Fit("myfit","","",0, 200);
double par0=myfit->GetParameter(0);
double par1=myfit->GetParameter(1);
double par2=myfit->GetParameter(2);
double par3=myfit->GetParameter(3);
double par4=myfit->GetParameter(4);

TString myweight;
std::cout<<"weight="<<par0<<"+hiBin*("<<par1<<")+hiBin*hiBin*("<<par2<<")+hiBin*hiBin*hiBin*("<<par3<<")"<<"+hiBin*hiBin*hiBin*hiBin*("<<par4<<")"<<endl;
canvas->SaveAs("Reweightplots/CentralityWeight.pdf");
}
Exemple #24
0
TCut Base::Cut() const
{
    INFO0;
    return TCut();
}
Exemple #25
0
void weightPPFONLLpthat(int minfit=2,int maxfit=100,TString pthat="pthatall")
{
  TString label;
  //TString selmcgen="((GisSignal==1||GisSignal==2)&&(Gy>-1&&Gy<1))";
  TString selmcgen="TMath::Abs(Gy)<2.4&&abs(GpdgId)==521&&GisSignal==1";
  TString myweightfunctiongen,myweightfunctionreco;
  
  //TCut weighpthat="pow(10,-0.075415*Gpt+1.748668+Gpt*Gpt*0.000388)+pow(10,-0.166406*Gpt+2.887856+Gpt*Gpt*0.000105) +0.003157";
  //TCut weighpthat="1";
  //TCut weighpthat="pthatweight*(pow(10,-0.107832+0.010248*Gpt+Gpt*Gpt*0.000079+Gpt*Gpt*Gpt*-0.000003+Gpt*Gpt*Gpt*Gpt*-0.000000+Gpt*Gpt*Gpt*Gpt*Gpt*0.000000))";//cross check
  TCut weighpthat="pthatweight";
  
  TCut weightCentrality="6.08582+hiBin*(-0.155739)+hiBin*hiBin*(0.00149946)+hiBin*hiBin*hiBin*(-6.41629e-06)+hiBin*hiBin*hiBin*hiBin*(1.02726e-08)";

  gStyle->SetOptTitle(1);
  //gStyle->SetOptStat(111111);
  gStyle->SetOptStat(0);
  gStyle->SetOptFit(0);
  gStyle->SetEndErrorSize(0);
  gStyle->SetMarkerStyle(20);

  gStyle->SetStatX(0.9);
  gStyle->SetStatY(0.9);
  gStyle->SetStatW(0.30);
  gStyle->SetStatH(0.04);
  gStyle->SetStatFontSize(0.03); 
  //TFile*infMC=new TFile("/data/HeavyFlavourRun2/MC2015/Bntuple/PbPb/Bntuple20160816_Bpt7svpv5p5Bpt10svpv3p5_BfinderMC_PbPb_Pythia8_BuToJpsiK_Bpt0_Pthat5_TuneCUEP8M1_20160816_bPt5jpsiPt0tkPt0p8_Bp.root");
  TFile*infMC=new TFile("/data/HeavyFlavourRun2/MC2015/Bntuple/PbPb/Bntuple20160816_Bpt7svpv5p5Bpt10svpv3p5_BfinderMC_PbPb_Pythia8_BuToJpsiK_TuneCUEP8M1_20160816_bPt5jpsiPt0tkPt0p8_Bp_pthatweight.root");
  TTree* ntGen = (TTree*)infMC->Get("ntGen");
  TTree *ntHiMC = (TTree*)infMC->Get("ntHi");
  ntGen->AddFriend(ntHiMC);
  
  TH1D* hPtGenFONLL = new TH1D("hPtGenFONLL","",nBinsReweight,ptBinsReweight);
  ntGen->Project("hPtGenFONLL","Gpt",(TCut(weighpthat)*TCut(selmcgen.Data())));
  //ntGen->Project("hPtGenFONLL","Gpt",(TCut(weighpthat)*TCut(weightCentrality)*TCut(selmcgen.Data())));
  divideBinWidth(hPtGenFONLL);
    
  TString fonll="ROOTfiles/fonllOutput_pp_Bplus_5p03TeV_y2p4.root";
  TFile* filePPReference = new TFile(fonll.Data());  
  TGraphAsymmErrors* gaeBplusReference = (TGraphAsymmErrors*)filePPReference->Get("gaeSigmaBplus");

  TH1D* hFONLL = new TH1D("hFONLL","",nBinsReweight,ptBinsReweight);
  double x,y;
  for(int i=0;i<nBinsReweight;i++){
    gaeBplusReference->GetPoint(i,x,y);
    hFONLL->SetBinContent(i+1,y);
  }
  TH1D* hFONLLOverPt=(TH1D*)hFONLL->Clone("hFONLLOverPt");
  TH1D* hFONLLOverPtWeight=(TH1D*)hFONLL->Clone("hFONLLOverPtWeight");
  
  hFONLL->Sumw2();
  hPtGenFONLL->Sumw2();
  hFONLLOverPt->Sumw2();
  hFONLL->Scale(1/hFONLL->Integral());
  hFONLLOverPt->Scale(1/hFONLLOverPt->Integral());
  hPtGenFONLL->Scale(1/hPtGenFONLL->Integral());
  hFONLLOverPt->Divide(hPtGenFONLL);
  
  //TF1 *myfit = new TF1("myfit","pow(10,[0]+[1]*x+x*x*[2])+pow(10,[3]*x*x+[4]*x*x*x+x*[5])", 2, 100);
  TF1 *myfit = new TF1("myfit","pow(10,[0]+[1]*x+x*x*[2]+x*x*x*[3]+x*x*x*x*[4]+x*x*x*x*x*[5])",0, 100);  
  //TF1 *myfit = new TF1("myfit","pow(10,[0]+[1]*x+x*x*[2]+x*x*x*[3])",0, 100);  
  //TF1 *myfit = new TF1("myfit","[0]+[1]*x+x*x*[2]+x*x*x*[3]+x*x*x*x*[4]+x*x*x*x*x*[5]",0, 100);  
   
  TCanvas*c1=new TCanvas("c1","c1",1000.,600.);
  c1->cd();
  gPad->SetLogy();
  gStyle->SetOptStat(111111111);
  hFONLLOverPt->Fit("myfit","","",minfit,maxfit);
  hFONLLOverPt->Fit("myfit","L m","",minfit,maxfit);
  TLegend* leg0 = myLegend(0.13,0.83,0.40,0.89);
  leg0->AddEntry(hFONLLOverPt,"Pythia8 MC_2015 B^{+}","");
  leg0->Draw();
  TLegend* leg1 = myLegend(0.15,0.75,0.40,0.88);
  leg1->AddEntry(hFONLLOverPt,"PbPb #sqrt{s}= 5.02 TeV","");
  leg1->Draw();

  double par0=myfit->GetParameter(0);
  double par1=myfit->GetParameter(1);
  double par2=myfit->GetParameter(2);
  double par3=myfit->GetParameter(3);
  double par4=myfit->GetParameter(4);
  double par5=myfit->GetParameter(5);
  //double par6=myfit->GetParameter(6);

  //myweightfunctiongen=Form("pow(10,%f*Gpt+%f+Gpt*Gpt*%f)+pow(10,%f*Gpt+%f+Gpt*Gpt*%f)",par0,par1,par2,par3,par4,par5);
  //myweightfunctiongen=Form("%f+%f*x+x*x*%f+x*x*x*%f+x*x*x*x*%f+x*x*x*x*x*%f",par0,par1,par2,par3,par4,par5);
  myweightfunctiongen=Form("pow(10,%f+%f*x+x*x*%f+x*x*x*%f+x*x*x*x*%f+x*x*x*x*x*%f)",par0,par1,par2,par3,par4,par5);
  std::cout<<"myweightfunctiongen="<<myweightfunctiongen<<std::endl;

  //myweightfunctionreco=Form("pow(10,%f*Gpt+%f+Gpt*Gpt*%f)+pow(10,%f*Gpt+%f+Gpt*Gpt*%f)",par0,par1,par2,par3,par4,par5);
  //std::cout<<"myweightfunctionreco="<<myweightfunctionreco<<std::endl;
  //std::cout<<"fit function parameters="<<weightfunctiongen<<std::endl;

  TCanvas*canvasPtReweight=new TCanvas("canvasPtReweight","canvasPtReweight_PbPb_MC_B+",1253.,494.); 
  canvasPtReweight->Divide(3,1);
  canvasPtReweight->cd(1);
  gPad->SetLogy();
  gPad->SetLeftMargin(0.15);
  hPtGenFONLL->SetXTitle("Gen p_{T}(GeV)");
  hPtGenFONLL->SetYTitle("PYTHIA, #entries");
  hPtGenFONLL->SetMinimum(1e-4);  
  hPtGenFONLL->SetMaximum(1e2);  
  hPtGenFONLL->GetYaxis()->SetTitleOffset(1.2);
  hPtGenFONLL->Draw();
  canvasPtReweight->cd(2);
  gPad->SetLogy();
  gPad->SetLeftMargin(0.15);
  hFONLL->SetXTitle("Gen, p_{T}(GeV)");
  hFONLL->SetYTitle("FONLL_PbPb, #entries");
  hFONLL->SetMinimum(1e-4);  
  hFONLL->SetMaximum(1e2);  
  hFONLL->GetYaxis()->SetTitleOffset(1.2);
  hFONLL->GetYaxis()->CenterTitle();
  hFONLL->GetXaxis()->CenterTitle();
  hFONLL->Draw();
  canvasPtReweight->cd(3);
  gPad->SetLogy();
  gPad->SetLeftMargin(0.15);
  hFONLLOverPt->SetXTitle("Gen p_{T}(GeV)");
  hFONLLOverPt->SetYTitle("FONLL_PbPb/PYTHIA ");
  hFONLLOverPt->SetMinimum(0.01);  
  hFONLLOverPt->SetMaximum(10.);  
  hFONLLOverPt->GetYaxis()->SetTitleOffset(1.2);
  hFONLLOverPt->GetYaxis()->CenterTitle();
  hFONLLOverPt->GetXaxis()->CenterTitle();
  hFONLLOverPt->Draw();
  canvasPtReweight->SaveAs("Reweightplots/canvasPtReweightPbPb.pdf");
}
Exemple #26
0
void plot(TString var, TString varlatex, TString varname, Int_t nbins, Double_t vmin, Double_t vmax)
{
  cout<<"---- Processing - "<<var<<endl;
  cout<<"  -- Fill histograms"<<endl;
  TFile* ifBkg = new TFile(infnameBkg[isChannel]);
  TTree* ntBkg = (TTree*)ifBkg->Get(texNtuple[isChannel]);
  ntBkg->AddFriend("ntHlt");
  TFile* ifSgl = new TFile(infnameSgl[isChannel]);
  TTree* ntSgl = (TTree*)ifSgl->Get(texNtuple[isChannel]);
  ntSgl->AddFriend("ntHlt");
  ntSgl->AddFriend("ntHi");

  TH1D* hBkg = new TH1D(Form("hBkg_%s",varname.Data()),"",nbins,vmin,vmax);
  TH1D* hSgl = new TH1D(Form("hSgl_%s",varname.Data()),"",nbins,vmin,vmax);

  ntBkg->Project(Form("hBkg_%s",varname.Data()),var,Form("%s&&%s",selTriggerBkg[isChannel].Data(),selBkg[isChannel].Data()));
  ntSgl->Project(Form("hSgl_%s",varname.Data()),var,TCut(weight[isChannel])*Form("%s&&%s",selTriggerSgl[isChannel].Data(),selSgl[isChannel].Data()));

  cout<<"  -- Calculate normalization"<<endl;
  Double_t normBkg=0,normSgl=0;
  //normBkg = hBkg->GetEntries();
  //normSgl = hSgl->GetEntries();
  normBkg = hBkg->Integral(vmin,vmax);
  normSgl = hSgl->Integral(vmin,vmax);
  cout<<"     normBkg: "<<normBkg<<" ;   normSgl: "<<normSgl<<endl;

  cout<<"  -- Normalize histograms"<<endl;
  hBkg->Scale(1./normBkg);
  hSgl->Scale(1./normSgl); 

  cout<<"  -- Plot"<<endl;
  hBkg->SetXTitle(varlatex);
  hBkg->SetYTitle("#Probability");
  hBkg->SetTitleOffset(1.5,"Y");
  Double_t hisMax = (hBkg->GetMaximum()>hSgl->GetMaximum())?hBkg->GetMaximum():hSgl->GetMaximum();
  hBkg->SetMaximum(hisMax*1.2);

  hBkg->SetLineColor(kBlue+1);
  hBkg->SetFillStyle(1001);
  hBkg->SetFillColor(kBlue-9);
  hBkg->SetLineWidth(3);
  hBkg->SetStats(0);

  TH1D* hSglplot = new TH1D(Form("hSglplot_%s",varname.Data()),"",nbins,vmin,vmax);
  for(int ib=0;ib<nbins;ib++) hSglplot->SetBinContent(ib+1,hSgl->GetBinContent(ib+1));
  hSglplot->SetLineColor(kRed);
  hSglplot->SetFillStyle(3004);
  hSglplot->SetFillColor(kRed);
  hSglplot->SetLineWidth(3);
  hSglplot->SetStats(0);

  TCanvas* c = new TCanvas(Form("c_%s",varname.Data()),"",600,600);
  hBkg->Draw();
  hSglplot->Draw("same");

  cout<<"  -- Plot legends"<<endl;
  TLatex* tex = new TLatex(0.18,0.935,Form("5.02TeV %s",texPP[isChannel].Data()));
  tex->SetNDC();
  tex->SetTextFont(42);
  tex->SetTextSize(0.055);
  tex->Draw();
  TLatex* texp;
  texp = new TLatex(0.68,0.935,texDecay[isChannel]);
  texp->SetNDC();
  texp->SetTextFont(42);
  texp->SetTextSize(0.055);
  texp->Draw();
  TLegend* leg = new TLegend(0.56,0.70,0.86,0.86);
  leg->AddEntry(hBkg,"Background","f");
  leg->AddEntry(hSglplot,"Signal","f");
  leg->SetBorderSize(0);
  leg->SetFillStyle(0);
  leg->Draw("same");

  cout<<"  -- Save plots"<<endl;
  c->SaveAs(Form("plots/%s_%s/c_%s.pdf",texPP[isChannel].Data(),texNtuple[isChannel].Data(),varname.Data()));
  cout<<endl;
}
Exemple #27
0
void fitB_extend(bool ispPb=true,TString infname="",bool doweight = 1)
{

  
  if(ispPb==true){
  
    inputdata="/afs/cern.ch/work/w/wangj/public/nt_20140727_PAMuon_HIRun2013_Merged_y24_Using03090319Bfinder.root";
    inputmc="/afs/cern.ch/work/w/wangj/public/nt_20140801_mixed_fromQMBFinder_Kp.root";
    luminosity=34.6*1e-3;
    outputname="../ResultsBplus/SigmaBplus_extend.root";
    cut="abs(y)<2.4&&(HLT_PAMu3_v1)&&abs(mumumass-3.096916)<0.15&&mass>5&&mass<6&& isbestchi2&&trk1Pt>0.9&&chi2cl>1.32e-02&&(d0/d0Err)>3.41&&cos(dtheta)>-3.46e01&&mu1pt>1.5&&mu2pt>1.5";

  }
  else{
  
    //inputdata="/data/bmeson/data//nt_20141022_PPMuon_Run2013A_PromptReco_v1_resub20141126.root";
    //inputmc="/afs/cern.ch/work/w/wangj/public/nt_20140801_mixed_fromQMBFinder_Kp.root";
    inputdata="/data/bmeson/data/nt_20141022_PPMuon_Run2013A_PromptReco_v1_resub20141126_HLT_PAL1DoubleMu0_HighQ_v1.root";
    inputmc="/data/bmeson/MC/nt_2015251_PYTHIA6_BuJpsiK_pp_PAL1DoubleMu0_HighQ_v1.root";

    luminosity=5400*1e-3;
    outputname="../ResultsBplus_pp/SigmaBplus_extend.root";
    cut="abs(y)<2.4&&(HLT_PAL1DoubleMu0_HighQ_v1)&&abs(mumumass-3.096916)<0.15&&mass>5&&mass<6&& isbestchi2&&trk1Pt>0.9&&chi2cl>1.32e-02&&(d0/d0Err)>3.41&&cos(dtheta)>-3.46e01&&mu1pt>1.5&&mu2pt>1.5";
  }
  
  seldata_2y=Form("%s",cut.Data());
  selmc=Form("abs(y)<2.4&&gen==23333&&%s",cut.Data());
  selmcgen="abs(y)<2.4&&abs(pdgId)==521&&isSignal==1";

  if (doweight==0) weight="1";
  if (infname=="") infname=inputdata.Data();
  TFile *inf = new TFile(infname.Data());
  TTree *nt = (TTree*) inf->Get("ntKp");

  TFile *infMC = new TFile(inputmc.Data());
  TTree *ntGen = (TTree*)infMC->Get("ntGen");
  TTree *ntGen2 = (TTree*)inf->Get("ntGen");
  TTree *ntMC = (TTree*)infMC->Get("ntKp");
    
  ntGen->AddFriend(ntMC);
  ntGen2->AddFriend(ntMC);
    
  const int nBins = 5;
  double ptBins[nBins+1] = {10,15,20,25,30,60};
  //const int nBins = 1;
  //double ptBins[nBins+1] = {10,60};
  TH1D *hPt = new TH1D("hPt","",nBins,ptBins);
  TH1D *hPtRecoTruth = new TH1D("hPtRecoTruth","",nBins,ptBins);
  TH1D *hGenPtSelected = new TH1D("hGenPtSelected","",nBins,ptBins);
  TH1D *hPtMC = new TH1D("hPtMC","",nBins,ptBins);
  TH1D *hPtGen = new TH1D("hPtGen","",nBins,ptBins);
  TH1D *hPtGen2 = new TH1D("hPtGen2","",nBins,ptBins);

  TFile *outf = new TFile(outputname.Data(),"recreate");

  for (int i=0;i<nBins+1;i++)
    {
      if (i==nBins) {fit(nt,ntMC,10,60,ispPb,i);continue;}
      TF1 *f = fit(nt,ntMC,ptBins[i],ptBins[i+1],ispPb,i);
      double yield = f->Integral(5,6)/0.02;
      double yieldErr = f->Integral(5,6)/0.02*f->GetParError(0)/f->GetParameter(0);
      hPt->SetBinContent(i+1,yield/(ptBins[i+1]-ptBins[i]));
      hPt->SetBinError(i+1,yieldErr/(ptBins[i+1]-ptBins[i]));
    }  

  TCanvas *c=  new TCanvas("cResult","",600,600);
  hPt->SetXTitle("B^{+} p_{T} (GeV/c)");
  hPt->SetYTitle("Uncorrected B^{+} dN/dp_{T}");
  hPt->Sumw2();
  hPt->Draw();
  
  ntMC->Project("hPtMC","pt",TCut(weight)*(TCut(selmc.Data())&&"gen==23333"));
  nt->Project("hPtRecoTruth","pt",TCut(seldata_2y.Data())&&"gen==23333");
  ntGen->Project("hPtGen","pt",TCut(weight)*(TCut(selmcgen.Data())));
  ntGen2->Project("hPtGen2","pt",(TCut(selmcgen.Data())));
  divideBinWidth(hPtRecoTruth);
  
  hPtRecoTruth->Draw("same hist");
  divideBinWidth(hPtMC);
  divideBinWidth(hPtGen);
  divideBinWidth(hPtGen2);
  
  hPtMC->Sumw2();
  TH1D *hEff = (TH1D*)hPtMC->Clone("hEff");
  hPtMC->Sumw2();
  hEff->Divide(hPtGen);
  
  TH1D *hPtCor = (TH1D*)hPt->Clone("hPtCor");
  hPtCor->Divide(hEff);
  TCanvas *cCor=  new TCanvas("cCorResult","",600,600);
  hPtCor->SetYTitle("Corrected B^{+} dN/dp_{T}");
  hPtCor->Draw();
  hPtGen->Draw("same hist");
  hPtGen2->Draw("same hist");

  TH1D *hPtSigma= (TH1D*)hPtCor->Clone("hPtSigma");
  double BRchain=6.09604e-5;

  hPtSigma->Scale(1./(2*luminosity*BRchain));
  hPtSigma->SetYTitle("d#sigma (B^{+})/dp_{T}");

  TCanvas *cSigma=  new TCanvas("cSigma","",600,600);

  hPtSigma->Draw();
  
  outf->cd();
  hPt->Write();
  hEff->Write();
  hPtGen->Write();
  hPtCor->Write();
  hPtSigma->Write();
  outf->Close();
  delete outf;
}
void fitDstar5pMinpt(Bool_t genmatchpoint=true)
{
  gStyle->SetTextSize(0.05);
  gStyle->SetTextFont(42);
  gStyle->SetPadRightMargin(0.043);
  gStyle->SetPadLeftMargin(0.18);
  gStyle->SetPadTopMargin(0.1);
  gStyle->SetPadBottomMargin(0.145);
  gStyle->SetTitleX(.0f);

  void clean0(TH1D* h);  
  TF1* fitDstar(TTree* nt, TTree* ntMC, Float_t ptmin, Bool_t plotgenmatch);

  TFile* infData = new TFile(infnameData5p[isData].Data());
  TFile* infMC = new TFile(infnameMC5p[isData].Data());

  TTree* ntData = (TTree*)infData->Get("ntDD0kpipipipi");
  TTree* ntMC = (TTree*)infMC->Get("ntDD0kpipipipi");
  TTree* ntGen = (TTree*)infMC->Get("ntGen");
  
  ntData->AddFriend("ntHlt");
  if(isData!=Data_MB&&isData!=Data) ntData->AddFriend("ntHi");
  ntMC->AddFriend("ntHlt");
  ntMC->AddFriend("ntHi");
  ntGen->AddFriend("ntHlt");
  ntGen->AddFriend("ntHi");

  Float_t aZero[nBins];
  for(int i=0;i<nBins;i++) aZero[i]=0;
  Float_t aPt[nBins],aPtErr[nBins],aGen[nBins],aGenErr[nBins];
  TH1F* hPt = new TH1F("hPt","",nBins,ptBinsPlus);
  TH1F* hGen = new TH1F("hGen","",nBins,ptBinsPlus);

  for(int i=0;i<nBins;i++)
    {
      TF1* fData = fitDstar(ntData,ntMC,ptBins[i],genmatchpoint);
      Float_t yieldData = fData->Integral(BINMIN,BINMAX)/BINWID;
      Float_t yieldDataErr = fData->Integral(BINMIN,BINMAX)/BINWID*fData->GetParError(0)/fData->GetParameter(0);      
      aPt[i] = yieldData;
      aPtErr[i] = yieldDataErr;
      hPt->SetBinContent(i+1,aPt[i]);
      hPt->SetBinError(i+1,aPtErr[i]);
      TH1D* hGenFill = new TH1D(Form("hGenFill_%.0f",ptBins[i]),"",1,ptBins[i],1.e+3);
      hGenFill->Sumw2();
      ntGen->Project(Form("hGenFill_%.0f",ptBins[i]),"Gpt",TCut("%s",weightmc[isData].Data())*Form("%s&&%s",selgen5p.Data(),triggerselectionmc[isData].Data()));
      aGen[i] = hGenFill->GetBinContent(1);
      aGenErr[i] = hGenFill->GetBinError(1);
      hGen->SetBinContent(i+1,aGen[i]);
      hGen->SetBinError(i+1,aGenErr[i]);
    }

  TGraphErrors* gPt = new TGraphErrors(nBins,ptBins,aPt,aZero,aPtErr);
  gPt->SetName("gPt");
  TGraphErrors* gGen = new TGraphErrors(nBins,ptBins,aGen,aZero,aGenErr);
  gGen->SetName("gGen");

  TFile* outputfile = new TFile(Form("outputfiles/output_5p_%s_Minpt.root",texData[isData].Data()),"recreate");
  outputfile->cd();
  gPt->Write();
  gGen->Write();
  hPt->Write();
  hGen->Write();
  outputfile->Close();
}
Exemple #29
0
//void plotTriggerForTDR(char *infname="/data/yjlee/dmeson/2015/trigger/mb.root")
void plotTrigger(string infname="/data/jisun/ppMB2015fullstats/skim_ncand_D0Dntuple_crab_pp_ALLMinimumBias_AOD_D0_tkpt0p5_Ds_01212016.root")
{  
   bool sideband = false;

   infname="/data/jisun/ppMB2015fullstats/skim_ncand_D0Dntuple_crab_pp_ALLMinimumBias_AOD_D0_tkpt0p5_Ds_01212016.root";
   TString outf = "result";
   
   //infname="/data/wangj/Data2015/Dntuple/pp/ntD_EvtBase_20160425_HighPtJet80_DfinderData_pp_20160329_dPt0tkPt0p5_D0Dstar_skim.root";
   //TString outf = "result_ppJet80";
   
   //infname="/data/wangj/Data2015/Dntuple/pp/ntD_EvtBase_20160425_HighPtLowerJets_DfinderData_pp_20160329_dPt0tkPt0p5_D0Dstar_skim.root";
   //TString outf = "result_ppLowerJets";
   
   //infname="/data/jisun/ppMB2015fullstats/skim_ncand_D0Dntuple_crab_pp_ALLMinimumBias_AOD_D0_tkpt0p5_Ds_01212016.root";
   //sideband = true; TString outf = "result_sideband"; 
   
   // ============== Open file and basic settings ===============   
   // Open Dntuple file
   TFile *inf = new TFile(infname.c_str());

   TTree *ntDkpi = (TTree*)inf->Get("ntDkpi");
   TTree *ntHlt = (TTree*)inf->Get("ntHlt");
   TTree *ntSkim = (TTree*)inf->Get("ntSkim");
   ntDkpi->AddFriend(ntHlt);
   ntDkpi->AddFriend(ntSkim);   

   // Define bin size and bin width for trigger turnon curve histograms
   const int nBin = 8;
   Float_t bins[nBin+1]={0,6,8,10,15,20,30,50,80};
   //const int nBin = 12;
   //Float_t bins[nBin+1]={0,5,6,8,10,12,15,20,25,30,35,40,70};

 
   // Templates for plotting  
   TH1D *hTmp2 = new TH1D ("hTmp2","",nBin,bins);
   
   // ============== Selection Criteria ===============

   // This MB sample has all pp MB 
   TCut mbCut = "(HLT_L1MinimumBiasHF1OR_part0_v1||  \
                             HLT_L1MinimumBiasHF1OR_part1_v1||  \
                             HLT_L1MinimumBiasHF1OR_part2_v1||  \
                             HLT_L1MinimumBiasHF1OR_part3_v1||  \
                             HLT_L1MinimumBiasHF1OR_part4_v1||  \
                             HLT_L1MinimumBiasHF1OR_part5_v1||  \
                             HLT_L1MinimumBiasHF1OR_part6_v1||  \
                             HLT_L1MinimumBiasHF1OR_part7_v1||  \
                             HLT_L1MinimumBiasHF1OR_part8_v1||  \
                             HLT_L1MinimumBiasHF1OR_part9_v1||  \
                             HLT_L1MinimumBiasHF1OR_part10_v1||  \
                             HLT_L1MinimumBiasHF1OR_part11_v1||  \
                             HLT_L1MinimumBiasHF1OR_part12_v1||  \
                             HLT_L1MinimumBiasHF1OR_part13_v1|| \
                             HLT_L1MinimumBiasHF1OR_part14_v1|| \
                             HLT_L1MinimumBiasHF1OR_part15_v1|| \
                             HLT_L1MinimumBiasHF1OR_part16_v1|| \
                             HLT_L1MinimumBiasHF1OR_part17_v1|| \
                             HLT_L1MinimumBiasHF1OR_part18_v1|| \
                             HLT_L1MinimumBiasHF1OR_part19_v1)";
   
   // L1 trigger thresholds
   TCut l1Cut16 = "L1_SingleJet16_BptxAND==1";
   TCut l1Cut24 = "L1_SingleJet24_BptxAND==1";
   TCut l1Cut28 = "L1_SingleJet28_BptxAND==1";
   TCut l1Cut40 = "L1_SingleJet40_BptxAND==1";
   TCut l1Cut48 = "L1_SingleJet48_BptxAND==1";

   // D meson selection
   TCut DmassCut             = "(abs(Dmass-1.8696)<0.03)";
   TCut DmesonCut            = "Dy>-1.&&Dy<1&&(DsvpvDistance/DsvpvDisErr)>3.5&&(DlxyBS/DlxyBSErr)>2.0&&Dchi2cl>0.05&&Dalpha<0.12";
   TCut DmesonDaughterTrkCut = "Dtrk1highPurity&&Dtrk2highPurity&&Dtrk1Pt>2.0&&Dtrk2Pt>2.0&&Dtrk1PtErr/Dtrk1Pt<0.1&&Dtrk2PtErr/Dtrk2Pt<0.1&&abs(Dtrk1Eta)<2.0&&abs(Dtrk2Eta)<2.0&&Dtrk1Algo>3&&Dtrk1Algo<8&&Dtrk2Algo>3&&Dtrk2Algo<8&&(Dtrk1PixelHit+Dtrk1StripHit)>=11&&(Dtrk1Chi2ndf/(Dtrk1nStripLayer+Dtrk1nPixelLayer)<0.15)&&(Dtrk2Chi2ndf/(Dtrk2nStripLayer+Dtrk2nPixelLayer)<0.15)";
   if (sideband) DmassCut = "(abs(Dmass-1.8696)>0.06 && abs(Dmass-1.8696)>0.12)";

   // Final selection for D candidates for trigger turnon studies
   TCut DAnaCut = DmassCut && DmesonCut && DmesonDaughterTrkCut;

   // HLT trigger thresholds
   TCut HLTCut8  = "HLT_DmesonPPTrackingGlobal_Dpt8_v1";
   TCut HLTCut15 = "HLT_DmesonPPTrackingGlobal_Dpt15_v1";
   TCut HLTCut20 = "HLT_DmesonPPTrackingGlobal_Dpt20_v1";
   TCut HLTCut30 = "HLT_DmesonPPTrackingGlobal_Dpt30_v1";
   TCut HLTCut50 = "HLT_DmesonPPTrackingGlobal_Dpt50_v1";

   // ============== L1 trigger efficiency study ===============
   
  int W = 600;
  int H = 600;

  int H_ref = 600; 
  int W_ref = 600; 

  // references for T, B, L, R
  float T = 0.08*H_ref;
  float B = 0.14*H_ref; 
  float L = 0.14*W_ref;
  float R = 0.08*W_ref;


  TString canvName = "TRDFigure_Centrality";
  TCanvas* c = new TCanvas(canvName,canvName,50,50,W,H);
  c->cd();
  c->SetFillColor(0);
  c->SetBorderMode(0);
  c->SetFrameFillStyle(0);
  c->SetFrameBorderMode(0);
  c->SetLeftMargin( L/W );
  c->SetRightMargin( R/W );
  c->SetTopMargin( T/H );
  c->SetBottomMargin( B/H );
  
  
    TH2D *hTmp = new TH2D ("hTmp","",100,0,60,100,0,1.4);
    hTmp->GetXaxis()->SetTitle("p_{T} (D^{0}) (GeV)");  
    hTmp->GetYaxis()->SetTitleOffset(1.15);
    hTmp->GetXaxis()->SetTitleOffset(0.95);
    hTmp->GetYaxis()->SetTitle("HLT Efficiency");  
    hTmp->GetYaxis()->CenterTitle();
    hTmp->GetXaxis()->CenterTitle();
    hTmp->SetMaximum(1.4);
    hTmp->GetXaxis()->SetTitleFont(42);
    hTmp->GetXaxis()->SetLabelFont(42);
    hTmp->GetXaxis()->SetTitleSize(0.06);
    hTmp->GetXaxis()->SetLabelSize(0.05);
    hTmp->GetYaxis()->SetTitleFont(42);
    hTmp->GetYaxis()->SetLabelFont(42);
    hTmp->GetYaxis()->SetTitleSize(0.06);
    hTmp->GetYaxis()->SetLabelSize(0.05);
       

//   TGraphAsymmErrors* g8  = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&mbCut&&l1Cut16), HLTCut8, nBin, bins);
//   TGraphAsymmErrors* g15 = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&mbCut&&l1Cut24), HLTCut15, nBin, bins);
   TGraphAsymmErrors* g20 = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&mbCut&&l1Cut28), HLTCut20, nBin, bins);
   TGraphAsymmErrors* g30 = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&mbCut&&l1Cut40), HLTCut30, nBin, bins);
   TGraphAsymmErrors* g50 = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&mbCut&&l1Cut48), HLTCut50, nBin, bins);

/*
   TGraphAsymmErrors* g8  = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&l1Cut16), HLTCut8, nBin, bins);
   TGraphAsymmErrors* g15 = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&l1Cut24), HLTCut15, nBin, bins);
   TGraphAsymmErrors* g20 = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&l1Cut28), HLTCut20, nBin, bins);
   TGraphAsymmErrors* g30 = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&l1Cut40), HLTCut30, nBin, bins);
*/
    hTmp->Draw();

//   g8->SetLineColor(1);
//   g8->SetMarkerColor(1);
//   g8->Draw("p same");
   
//   g15->SetLineColor(2);
//   g15->SetMarkerColor(2);
//   g15->Draw("pl same");

   g20->SetLineColor(4);
   g20->SetMarkerColor(4);
   g20->Draw("pl same");
   
   g30->SetLineColor(kGreen+2);
   g30->SetMarkerColor(kGreen+2);
   g30->Draw("pl same");

   g50->SetLineColor(kOrange);
   g50->SetMarkerColor(kOrange);
   g50->Draw("pl same");
   
 TLegend *legend=new TLegend(0.4579866,0.2472028,0.8389262,0.4342657,"");
  legend->SetBorderSize(0);
  legend->SetLineColor(0);
  legend->SetFillColor(0);
  legend->SetFillStyle(1001);
  legend->SetTextFont(42);
  legend->SetTextSize(0.04);

//  TLegendEntry *ent_g8=legend->AddEntry(g8,"HLT D meson p_{T} #geq 8","pl");
//  ent_g8->SetTextFont(42);
//  ent_g8->SetLineColor(1);
//  ent_g8->SetMarkerColor(1);
//  TLegendEntry *ent_g15=legend->AddEntry(g15,"HLT D meson p_{T} #geq15","pl");
//  ent_g15->SetTextFont(42);
//  ent_g15->SetLineColor(1);
//  ent_g15->SetMarkerColor(1);
  TLegendEntry *ent_g20=legend->AddEntry(g20,"HLT D meson p_{T} #geq 20","pl");
  ent_g20->SetTextFont(42);
  ent_g20->SetLineColor(1);
  ent_g20->SetMarkerColor(1);
  TLegendEntry *ent_g30=legend->AddEntry(g30,"HLT D meson p_{T} #geq 30","pl");
  ent_g30->SetTextFont(42);
  ent_g30->SetLineColor(1);
  ent_g30->SetMarkerColor(1);
  TLegendEntry *ent_g50=legend->AddEntry(g50,"HLT D meson p_{T} #geq 50","pl");
  ent_g50->SetTextFont(42);
  ent_g50->SetLineColor(1);
  ent_g50->SetMarkerColor(1);
  legend->Draw("same");
   
   CMS_lumi( c, 1, 11 );
   
   
  TLatex * tlatexeff=new TLatex(0.2079866,0.70,"HLT efficiency with respect to L1 seed");
  tlatexeff->SetNDC();
  tlatexeff->SetTextColor(1);
  tlatexeff->SetTextFont(42);
  tlatexeff->SetTextSize(0.040);
 // tlatexeff->Draw("same");

   c->SaveAs(outf+"/Dmeson-HLTriggerEfficiency.pdf");
   c->SaveAs(outf+"/Dmeson-HLTriggerEfficiency.png");
//   c->SaveAs(outf+"/Dmeson-HLTriggerEfficiency.jpeg");
//   c->SaveAs(outf+"/Dmeson-HLTriggerEfficiency.eps");
   c->SaveAs(outf+"/Dmeson-HLTriggerEfficiency.C");

/*   
   // ============== HLT trigger efficiency study ===============
   TCanvas *c2 = new TCanvas("c2","",600,600);
   
   TGraphAsymmErrors* gL16 = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&mbCut&&"L1_SingleJet16_BptxAND_Prescl==1"), l1Cut16, nBin, bins);
   TGraphAsymmErrors* gL24 = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&mbCut&&"L1_SingleJet24_BptxAND_Prescl==1"), l1Cut24, nBin, bins);
   TGraphAsymmErrors* gL28 = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&mbCut&&"L1_SingleJet28_BptxAND_Prescl==1"), l1Cut28, nBin, bins);
   TGraphAsymmErrors* gL40 = getEfficiency(ntDkpi,Form("Max$(Dpt*(%s))",DAnaCut.GetTitle()), TCut(DAnaCut&&mbCut&&"L1_SingleJet40_BptxAND_Prescl==1"), l1Cut40, nBin, bins);
   
   hTmp2->Draw();
   hTmp2->SetXTitle("p_{T}  (D^{0})(GeV/c)");
   hTmp2->SetYTitle("L1 Trigger Efficiency");
   
   gL16->SetMarkerColor(1);
   gL16->SetLineColor(1);
   gL16->Draw("p same");

   gL24->SetMarkerColor(2);
   gL24->SetLineColor(2);
   gL24->Draw("p same");

   gL28->SetMarkerColor(4);
   gL28->SetLineColor(4);
   gL28->Draw("p same");

   gL40->SetMarkerColor(kGreen+2);
   gL40->SetLineColor(kGreen+2);
   gL40->Draw("p same");

   TLegend *leg2 = new TLegend(0.4916107,0.3059441,0.8322148,0.4912587);
   leg2->SetBorderSize(0);
   leg2->SetFillStyle(0); 
   leg2->AddEntry(gL16,"pp #sqrt{s} = 5.02 TeV","");
   leg2->AddEntry(gL16,"Level 1 Jet 16","pl");
   leg2->AddEntry(gL24,"Level 1 Jet 24","pl");
   leg2->AddEntry(gL28,"Level 1 Jet 28","pl");
   leg2->AddEntry(gL40,"Level 1 Jet 40","pl");
   leg2->Draw();

   c2->SaveAs(outf+"/Dmeson-L1TriggerEfficiency.pdf");
   c2->SaveAs(outf+"/Dmeson-L1TriggerEfficiency.png");
//   c2->SaveAs(outf+"/Dmeson-L1TriggerEfficiency.jpeg");
   c2->SaveAs(outf+"/Dmeson-L1TriggerEfficiency.C");
   
   
   // ============== Plot an example D mass distribution ===============
   TCanvas *c3 = new TCanvas("c3","",600,600);
   ntDkpi->Draw("Dmass>>h(100,1.7696,1.9696)",DmesonCut&&DmesonDaughterTrkCut&&mbCut&&l1Cut16);
   
   // ..done 
*/
}