Esempio n. 1
0
void plotDataMC(const TString outdir   = "Data/extra",
                const TString mcfname  = "MC/eff.root",
	        const TString datfname = "Data/eff.root"
) {
  gBenchmark->Start("plotDataMC");
  
  //--------------------------------------------------------------------------------------------------------------
  // Settings 
  //============================================================================================================== 

  vector<TString> etalabelv;
  vector<TString> ptlabelv;
  
  // eta bins
  etalabelv.push_back("0 < |#eta| < 1.5");
  etalabelv.push_back("1.5 < |#eta| < 2.5");
  
  CPlot::sOutDir = outdir;
  TString format = "png";

  // y-axis ranges
  Double_t efflow   = 0.10, effhigh   = 1.20;
  Double_t scalelow = 0.95, scalehigh = 1.05;
  
  
  //--------------------------------------------------------------------------------------------------------------
  // Main analysis code 
  //==============================================================================================================   
  
  TFile mcfile(mcfname);      
  TFile datafile(datfname);
  
  TH2F *hMCEff=0,   *hMCErrl=0,   *hMCErrh=0;
  TH2F *hDataEff=0, *hDataErrl=0, *hDataErrh=0;
  
  TGraphAsymmErrors *grMCEffEta=0, *grDataEffEta=0, *grScaleEta=0;
  TGraphAsymmErrors *grMCEffPt=0,  *grDataEffPt=0,  *grScalePt=0;
  
  vector<TGraphAsymmErrors*> mceff_vs_pt_per_etav;
  vector<TGraphAsymmErrors*> eff_vs_pt_per_etav;
  vector<TGraphAsymmErrors*> mceff_vs_eta_per_ptv;
  vector<TGraphAsymmErrors*> eff_vs_eta_per_ptv;
  vector<TGraphAsymmErrors*> scale_vs_pt_per_etav;
  vector<TGraphAsymmErrors*> scale_vs_eta_per_ptv;
  
  grMCEffEta = (TGraphAsymmErrors*)mcfile.Get("grEffEta");
  grMCEffPt  = (TGraphAsymmErrors*)mcfile.Get("grEffPt");
  if(grMCEffPt) {
    grMCEffPt->SetPoint(grMCEffPt->GetN()-1, 125, grMCEffPt->GetY()[grMCEffPt->GetN()-1]);
    grMCEffPt->SetPointError(grMCEffPt->GetN()-1,25,25, 
                             grMCEffPt->GetErrorYlow(grMCEffPt->GetN()-1),
			     grMCEffPt->GetErrorYhigh(grMCEffPt->GetN()-1));
  }  			               
  hMCEff  = (TH2F*)mcfile.Get("hEffEtaPt");
  hMCErrl = (TH2F*)mcfile.Get("hErrlEtaPt");
  hMCErrh = (TH2F*)mcfile.Get("hErrhEtaPt");  

  grDataEffEta = (TGraphAsymmErrors*)datafile.Get("grEffEta");
  grDataEffPt  = (TGraphAsymmErrors*)datafile.Get("grEffPt");
  if(grDataEffPt) {
    grDataEffPt->SetPoint(grDataEffPt->GetN()-1, 125, grDataEffPt->GetY()[grDataEffPt->GetN()-1]);
    grDataEffPt->SetPointError(grDataEffPt->GetN()-1,25,25, 
                                grDataEffPt->GetErrorYlow(grDataEffPt->GetN()-1),
			        grDataEffPt->GetErrorYhigh(grDataEffPt->GetN()-1));
  }
  
  hDataEff  = (TH2F*)datafile.Get("hEffEtaPt");
  hDataErrl = (TH2F*)datafile.Get("hErrlEtaPt");
  hDataErrh = (TH2F*)datafile.Get("hErrhEtaPt");
    
  if(grMCEffEta && grDataEffEta) {
    grScaleEta = new TGraphAsymmErrors(grMCEffEta->GetN());
    for(Int_t i=0; i<grMCEffEta->GetN(); i++) {
      Double_t mcval   = grMCEffEta->GetY()[i];
      Double_t dataval = grDataEffEta->GetY()[i];
      Double_t scale   = dataval/mcval;
      grScaleEta->SetPoint(i,grMCEffEta->GetX()[i],scale);
      
      Double_t mcerrl   = grMCEffEta->GetErrorYlow(i);
      Double_t mcerrh   = grMCEffEta->GetErrorYhigh(i);
      Double_t dataerrl = grDataEffEta->GetErrorYlow(i);
      Double_t dataerrh = grDataEffEta->GetErrorYhigh(i);
      grScaleEta->SetPointError(i, 0, 0,
	  		        scale*sqrt(mcerrl*mcerrl/mcval/mcval + dataerrl*dataerrl/dataval/dataval),
			        scale*sqrt(mcerrh*mcerrh/mcval/mcval + dataerrh*dataerrh/dataval/dataval));
    }
  }
  
  if(grMCEffPt && grDataEffPt) {
    grScalePt = new TGraphAsymmErrors(grMCEffPt->GetN());
    for(Int_t i=0; i<grMCEffPt->GetN(); i++) {
      Double_t mcval   = grMCEffPt->GetY()[i];
      Double_t dataval = grDataEffPt->GetY()[i];
      Double_t scale   = dataval/mcval;
      grScalePt->SetPoint(i, grMCEffPt->GetX()[i],scale);
      if(i==grMCEffPt->GetN()-1)
        grScalePt->SetPoint(i,165,scale);
    
      Double_t mcerrl   = grMCEffPt->GetErrorYlow(i);
      Double_t mcerrh   = grMCEffPt->GetErrorYhigh(i);
      Double_t dataerrl = grDataEffPt->GetErrorYlow(i);
      Double_t dataerrh = grDataEffPt->GetErrorYhigh(i);
      grScalePt->SetPointError(i, 0, 0,
			        scale*sqrt(mcerrl*mcerrl/mcval/mcval + dataerrl*dataerrl/dataval/dataval),
			        scale*sqrt(mcerrh*mcerrh/mcval/mcval + dataerrh*dataerrh/dataval/dataval));
    }
  }
  
  if(hMCEff->GetEntries()>0 && hDataEff->GetEntries()>0) {
    const Int_t nx = hMCEff->GetNbinsX();
    const Int_t ny = hMCEff->GetNbinsY(); 
/*    
    for(Int_t iy=1; iy<=ny; iy++) {
      Double_t xval[nx], xerr[nx];
      Double_t effval[nx],   efferrl[nx],   efferrh[nx];
      Double_t scaleval[nx], scaleerrl[nx], scaleerrh[nx];
      
      for(Int_t ix=1; ix<=nx; ix++) {
        xval[ix-1] = 0.5*(hMCEff->GetXaxis()->GetBinLowEdge(ix) + hMCEff->GetXaxis()->GetBinLowEdge(ix+1));
        xerr[ix-1] = 0;
        
        Double_t mceff  = hMCEff->GetCellContent(ix,iy);
        Double_t mcerrl = hMCErrl->GetCellContent(ix,iy);
        Double_t mcerrh = hMCErrh->GetCellContent(ix,iy);
        
        Double_t dataeff  = hDataEff->GetCellContent(ix,iy);
        Double_t dataerrl = hDataErrl->GetCellContent(ix,iy);
        Double_t dataerrh = hDataErrh->GetCellContent(ix,iy);
        effval[ix-1]  = dataeff;
        efferrl[ix-1] = dataerrl;
        efferrh[ix-1] = dataerrh;
        
        Double_t scale = dataeff/mceff;
        scaleval[ix-1]  = scale;
        scaleerrl[ix-1] = scale*sqrt(mcerrl*mcerrl/mceff/mceff + dataerrl*dataerrl/dataeff/dataeff);
        scaleerrh[ix-1] = scale*sqrt(mcerrh*mcerrh/mceff/mceff + dataerrh*dataerrh/dataeff/dataeff);
      }
      
      eff_vs_eta_per_ptv.push_back(new TGraphAsymmErrors(nx,xval,effval,xerr,xerr,efferrl,efferrh));
      scale_vs_eta_per_ptv.push_back(new TGraphAsymmErrors(nx,xval,scaleval,xerr,xerr,scaleerrl,scaleerrh));
    }
*/    
    for(Int_t ix=1; ix<=nx; ix++) {
      Double_t xval[ny], xerr[ny];
      Double_t mceffval[ny], mcefferrl[ny], mcefferrh[ny];
      Double_t effval[ny],   efferrl[ny],   efferrh[ny];
      Double_t scaleval[ny], scaleerrl[ny], scaleerrh[ny];
      
      for(Int_t iy=1; iy<=ny; iy++) {
        xval[iy-1] = 0.5*(hMCEff->GetYaxis()->GetBinLowEdge(iy+1) + hMCEff->GetYaxis()->GetBinLowEdge(iy));
        xerr[iy-1] = 0.5*(hMCEff->GetYaxis()->GetBinLowEdge(iy+1) - hMCEff->GetYaxis()->GetBinLowEdge(iy));
	if(iy==ny) {
	  xval[iy-1] = 125;
          xerr[iy-1] = 25;
        }
	
        Double_t mceff  = hMCEff->GetCellContent(ix,iy);
        Double_t mcerrl = hMCErrl->GetCellContent(ix,iy);
        Double_t mcerrh = hMCErrh->GetCellContent(ix,iy);
	mceffval[iy-1]  = mceff;
        mcefferrl[iy-1] = mcerrl;
        mcefferrh[iy-1] = mcerrh;
        
        Double_t dataeff  = hDataEff->GetCellContent(ix,iy);
        Double_t dataerrl = hDataErrl->GetCellContent(ix,iy);
        Double_t dataerrh = hDataErrh->GetCellContent(ix,iy);
        effval[iy-1]  = dataeff;
        efferrl[iy-1] = dataerrl;
        efferrh[iy-1] = dataerrh;
        
        Double_t scale = dataeff/mceff;
        scaleval[iy-1]  = scale;
        scaleerrl[iy-1] = (scale>0) ? scale*sqrt(mcerrl*mcerrl/mceff/mceff + dataerrl*dataerrl/dataeff/dataeff) : 0;
        scaleerrh[iy-1] = (scale>0) ? scale*sqrt(mcerrh*mcerrh/mceff/mceff + dataerrh*dataerrh/dataeff/dataeff) : 0;
      }
      
      mceff_vs_pt_per_etav.push_back(new TGraphAsymmErrors(ny,xval,mceffval,xerr,xerr,mcefferrl,mcefferrh));
      eff_vs_pt_per_etav.push_back(new TGraphAsymmErrors(ny,xval,effval,xerr,xerr,efferrl,efferrh));
      scale_vs_pt_per_etav.push_back(new TGraphAsymmErrors(ny,xval,scaleval,0,0,scaleerrl,scaleerrh));
    }  
  }

    
  //--------------------------------------------------------------------------------------------------------------
  // Make plots
  //==============================================================================================================
  TCanvas *c = MakeCanvas("c","c",800,600);
  
  if(grMCEffEta && grDataEffEta) {
    CPlot plotEffEta("effeta","","|#eta|","#varepsilon");
    plotEffEta.AddGraph(grMCEffEta,   "MC","",  kRed, kOpenSquare);
    plotEffEta.AddGraph(grDataEffEta,"data","",kBlue,kFullDotLarge);
    plotEffEta.SetYRange(efflow,effhigh);
    plotEffEta.TransLegend(0,-0.5);
    plotEffEta.Draw(c,kTRUE,format);
    
    CPlot plotScaleEta("scaleeta","","|#eta|","scale factor");
    plotScaleEta.AddGraph(grScaleEta,"",kBlue,kFullDotLarge);
    plotScaleEta.AddLine(0,1.0,2.7,1.0,kBlack,7);
    plotScaleEta.SetYRange(scalelow,scalehigh);
    plotScaleEta.SetXRange(0,2.7);
    plotScaleEta.Draw(c,kTRUE,format);
  }
  
  if(grMCEffPt && grDataEffPt) {
    CPlot plotEffPt("effpt","","p_{T} [GeV/c]","#varepsilon");
    plotEffPt.AddGraph(grMCEffPt,   "MC","",  kRed, kOpenSquare);
    plotEffPt.AddGraph(grDataEffPt,"data","",kBlue,kFullDotLarge);
    plotEffPt.SetYRange(efflow,effhigh);
    plotEffPt.SetXRange(0,150);
    plotEffPt.TransLegend(0,-0.5);
    plotEffPt.Draw(c,kTRUE,format);
  
    CPlot plotScalePt("scalept","","p_{T} [GeV/c]","scale factor");
    plotScalePt.AddGraph(grScalePt,"",kBlue,kFullDotLarge);
    plotScalePt.AddLine(0,1.0,150,1.0,kBlack,7);
    plotScalePt.SetXRange(0,150);
    plotScalePt.SetYRange(scalelow,scalehigh);
    plotScalePt.Draw(c,kTRUE,format);
  }
      
  if(mceff_vs_pt_per_etav.size()>0 && eff_vs_pt_per_etav.size()>0) {
    for(UInt_t ig=0; ig<mceff_vs_pt_per_etav.size(); ig++) {
      char pname[100];
      sprintf(pname,"effpt_eta%i",ig);
      CPlot plotEffPt_perEta(pname,"","p_{T} [GeV/c]","#varepsilon");
      plotEffPt_perEta.AddGraph(mceff_vs_pt_per_etav[ig],"MC","",   kRed,  kOpenSquare);
      plotEffPt_perEta.AddGraph(eff_vs_pt_per_etav[ig], "data","", kBlue, kFullDotLarge);
      plotEffPt_perEta.AddTextBox(etalabelv[ig],0.2,0.82,0.4,0.88,0);
      plotEffPt_perEta.TransLegend(0,-0.5); 
      plotEffPt_perEta.SetXRange(0,150);
      plotEffPt_perEta.SetYRange(efflow,effhigh);
      plotEffPt_perEta.Draw(c,kTRUE,format);
    }    
    
    for(UInt_t ig=0; ig<scale_vs_pt_per_etav.size(); ig++) {
      char pname[100];
      sprintf(pname,"scalept_eta%i",ig);
      CPlot plotScalePt_perEta(pname,"","p_{T} [GeV/c]","scale factor");
      plotScalePt_perEta.AddGraph(scale_vs_pt_per_etav[ig],"",kBlue,kFullDotLarge);
      plotScalePt_perEta.AddTextBox(etalabelv[ig],0.7,0.82,0.9,0.88,0);
      plotScalePt_perEta.AddLine(0,1,150,1,kBlack,7);
      plotScalePt_perEta.SetXRange(0,150);
      plotScalePt_perEta.SetYRange(scalelow,scalehigh);
      plotScalePt_perEta.Draw(c,kTRUE,format);
    }
  }

  if(mceff_vs_eta_per_ptv.size()>0 && eff_vs_eta_per_ptv.size()>0) {
    for(UInt_t ig=0; ig<mceff_vs_eta_per_ptv.size(); ig++) {
      char pname[100];
      sprintf(pname,"effeta_pt%i",ig);
      CPlot plotEffEta_perPt(pname,"","|#eta|","#varepsilon");
      plotEffEta_perPt.AddGraph(mceff_vs_eta_per_ptv[ig],"MC","",  kRed,  kOpenSquare);
      plotEffEta_perPt.AddGraph(eff_vs_eta_per_ptv[ig], "data","", kBlue, kFullDotLarge);
      plotEffEta_perPt.AddTextBox(ptlabelv[ig],0.2,0.82,0.4,0.88,0);
      plotEffEta_perPt.SetYRange(efflow,effhigh);
      plotEffEta_perPt.Draw(c,kTRUE,format);
    }
    
    for(UInt_t ig=0; ig<scale_vs_eta_per_ptv.size(); ig++) {
      char pname[100];
      sprintf(pname,"scaleeta_pt%i",ig);
      CPlot plotScaleEta_perPt(pname,"","|#eta|","scale factor");
      plotScaleEta_perPt.AddGraph(scale_vs_eta_per_ptv[ig],"", kBlue, kFullDotLarge);
      plotScaleEta_perPt.AddLine(-2.7,1.0,2.7,1.0,kBlack,7);
      plotScaleEta_perPt.AddTextBox(ptlabelv[ig],0.7,0.82,0.9,0.88,0);
      plotScaleEta_perPt.SetYRange(scalelow,scalehigh);
      plotScaleEta_perPt.SetXRange(-2.7,2.7);
      plotScaleEta_perPt.Draw(c,kTRUE,format);
    }
  }
  
  gBenchmark->Show("plotDataMC");
}
void makeEfficiencyScaleFactors(string DataFilename = "data_eleWPEffTP/basic_76_106/eff.root", 
                   string MCFilename = "s11_zee_eleWPEffTP/basic_76_106/eff.root" , 
                   string outputDir = "data_eleWPEffTP/basic_76_106/", 
                   string histName = "h2_results_ele_selection",
                   string Label = "htt")
{
  gBenchmark->Start("printMuonWPEff");
    
  string label = Label;
  if (Label != "") label = "_" + Label;

  //--------------------------------------------------------------------------------------------------------------
  // Settings 
  //============================================================================================================== 
  
  TString outfname = (outputDir + "/eff_table.txt").c_str();
  TFile mcfile(MCFilename.c_str());
  TFile datafile(DataFilename.c_str());
  
  //--------------------------------------------------------------------------------------------------------------
  // Main analysis code 
  //==============================================================================================================   
  
  TH2F *hMCEff=0,    *hMCErrl=0,    *hMCErrh=0;
  TH2F *hDataEff1=0, *hDataErrl1=0, *hDataErrh1=0;
  TH2F *hDataEff2=0, *hDataErrl2=0, *hDataErrh2=0;
  
  hMCEff  = (TH2F*)mcfile.Get("hEffEtaPt");
  hMCErrl = (TH2F*)mcfile.Get("hErrlEtaPt");
  hMCErrh = (TH2F*)mcfile.Get("hErrhEtaPt");
  
  hDataEff  = (TH2F*)datafile.Get("hEffEtaPt");
  hDataErrl = (TH2F*)datafile.Get("hErrlEtaPt");
  hDataErrh = (TH2F*)datafile.Get("hErrhEtaPt");
  
  TGraphAsymmErrors* MCEffVsRho   = ( TGraphAsymmErrors*)mcfile.Get("grEffRho");
  TGraphAsymmErrors* DataEffVsRho = ( TGraphAsymmErrors*)datafile.Get("grEffRho");
 
  TGraphAsymmErrors* MCEffVsNVtx   = ( TGraphAsymmErrors*)mcfile.Get("grEffNPV");
  TGraphAsymmErrors* DataEffVsNVtx = ( TGraphAsymmErrors*)datafile.Get("grEffNPV");


  //--------------------------------------------------------------------------------------------------------------
  // Update root file histograms
  //==============================================================================================================   
  const Int_t nx = hMCEff->GetNbinsX();
  const Int_t ny = hMCEff->GetNbinsY();

  TFile *outputFile = new TFile(("efficiency_results"+label+".root").c_str(), "UPDATE");

  //Do Binning
  Double_t *ptbins = new Double_t[ny+1];
  Double_t *etabins = new Double_t[nx+1];
  for(Int_t iy=1; iy<=ny; iy++) {
      ptbins[iy-1] = hMCEff->GetYaxis()->GetBinLowEdge(iy);
  }
  for(Int_t ix=1; ix<=nx; ix++) {
    etabins[ix-1]= hMCEff->GetXaxis()->GetBinLowEdge(ix);
  }
  ptbins[ny] = 60;
  etabins[nx] = 2.5;
    
  TH2F *h2_results_selection = new TH2F(histName.c_str(),"",ny,ptbins,nx,etabins);
  for(Int_t iy=0; iy<=ny+2; iy++) {
    for(Int_t ix=0; ix<=nx+2; ix++) {
      h2_results_selection->SetCellContent(iy,ix, 1.0);
      h2_results_selection->SetCellError(iy,ix, 0.0);
    }
  }

  cout << "ptbins : ";
  for(Int_t i=0; i<ny+1;++i) {
    cout << ptbins[i] << " ";
  }
  cout << endl;
  cout << "etabins : ";
  for(Int_t i=0; i<nx+1;++i) {
    cout << etabins[i] << " ";
  }
  cout << endl;

  //--------------------------------------------------------------------------------------------------------------
  // Produce Text file table
  //==============================================================================================================   

  ofstream txtfile;
  txtfile.open(outfname.Data());
  assert(txtfile.is_open());
    
  
  txtfile << " pT        ";
  txtfile << " eta           ";
  txtfile << "    MC T&P           ";
  txtfile << "    Data T&P         ";  
  txtfile << "    Scale factor     ";
  txtfile << endl;
  txtfile << "----------------------------------------------------------------------------------------------------------------------------------------------------";
  txtfile << endl;
  for(Int_t iy=1; iy<=ny; iy++) {
    for(Int_t ix=1; ix<=nx; ix++) {
      txtfile << "[" << setw(4) << hMCEff->GetYaxis()->GetBinLowEdge(iy) << "," << setw(4) << hMCEff->GetYaxis()->GetBinLowEdge(iy+1) << "]";
      txtfile << "[" << setw(6) << hMCEff->GetXaxis()->GetBinLowEdge(ix) << "," << setw(6) << hMCEff->GetXaxis()->GetBinLowEdge(ix+1) << "]";      
      ios_base::fmtflags flags = txtfile.flags();
      txtfile.precision(4);
      
      Double_t mceff  = hMCEff->GetCellContent(ix,iy);
      Double_t mcerrl = hMCErrl->GetCellContent(ix,iy);
      Double_t mcerrh = hMCErrh->GetCellContent(ix,iy);
      txtfile << " " << setw(9) << fixed << mceff << " +/- " << TMath::Max(mcerrl,mcerrh);

      Double_t dataeff  = hDataEff->GetCellContent(ix,iy);
      Double_t dataerrl = hDataErrl->GetCellContent(ix,iy);
      Double_t dataerrh = hDataErrh->GetCellContent(ix,iy);
      txtfile << " " << setw(9) << fixed << dataeff << " +/- " << TMath::Max(dataerrl,dataerrh);
      
      Double_t scale     = (hDataEff->GetCellContent(ix,iy))/(hMCEff->GetCellContent(ix,iy));
      Double_t scaleErrl = scale*sqrt(mcerrl*mcerrl/mceff/mceff + dataerrl*dataerrl/dataeff/dataeff);
      Double_t scaleErrh = scale*sqrt(mcerrh*mcerrh/mceff/mceff + dataerrh*dataerrh/dataeff/dataeff);
      txtfile << " " << setw(9) << fixed << scale << " +/- " << TMath::Max(scaleErrl,scaleErrh);

      txtfile << endl;
      txtfile.flags(flags);

      cout << "Set " << iy << " " << ix << " " << scale << endl;
      h2_results_selection->SetCellContent(iy,ix, scale);
      h2_results_selection->SetCellError(iy,ix, (scaleErrl + scaleErrh)/2);
      //fill overflow bins with the same values as last bin
      if (ix == nx) {
      cout << "Set " << iy << " " << ix+1 << " " << scale << endl;
        h2_results_selection->SetCellContent(iy,nx+1, scale);
        h2_results_selection->SetCellError(iy,nx+1, (scaleErrl + scaleErrh)/2);
      }
    }
    if (iy == ny) {
      for(Int_t ix=1; ix<=nx; ix++) {
        Double_t mceff  = hMCEff->GetCellContent(ix,iy);
        Double_t mcerrl = hMCErrl->GetCellContent(ix,iy);
        Double_t mcerrh = hMCErrh->GetCellContent(ix,iy);
        Double_t dataeff  = hDataEff->GetCellContent(ix,iy);
        Double_t dataerrl = hDataErrl->GetCellContent(ix,iy);
        Double_t dataerrh = hDataErrh->GetCellContent(ix,iy);
        Double_t scale     = (hDataEff->GetCellContent(ix,iy))/(hMCEff->GetCellContent(ix,iy));
        Double_t scaleErrl = scale*sqrt(mcerrl*mcerrl/mceff/mceff + dataerrl*dataerrl/dataeff/dataeff);
        Double_t scaleErrh = scale*sqrt(mcerrh*mcerrh/mceff/mceff + dataerrh*dataerrh/dataeff/dataeff);
        
        cout << "Set " << iy+1 << " " << ix << " " << scale << endl;
        h2_results_selection->SetCellContent(iy+1,ix, scale);
        h2_results_selection->SetCellError(iy+1,ix, (scaleErrl + scaleErrh)/2);
        //fill overflow bins with the same values as last bin
        if (ix == nx) {
          cout << "Set " << iy+1 << " " << ix+1 << " " << scale << endl;
          h2_results_selection->SetCellContent(iy+1,nx+1, scale);
          h2_results_selection->SetCellError(iy+1,nx+1, (scaleErrl + scaleErrh)/2);
        }
      }
    }

    txtfile << endl;
  }
  txtfile.close();
  
  cout << outfname << " created!" << endl;
  



  //--------------------------------------------------------------------------------------------------------------
  // Create TEX table
  //==============================================================================================================   

  ofstream texfile;
  texfile.open((outputDir + "/eff_table.tex").c_str());
  assert(texfile.is_open());

  texfile << " \\begin{table}[!ht]" << endl;
  texfile << " \\begin{center} " << endl;
  texfile << " \\begin{tabular}{|c|c|c|c|}" << endl;
  texfile << " \\hline\n";


  texfile << " $p_{T}$ / $\\eta$ bin    &  Monte Carlo Efficiency    &  Data Efficiency   &  MC to Data Scale Factor \\\\  ";
  texfile << " \\hline           ";
  texfile << endl;
  for(Int_t iy=1; iy<=ny; iy++) {
    for(Int_t ix=1; ix<=nx; ix++) {

      string binLabel = Form("$%5.1f < p_{T} \\le %5.1f$ , $%5.1f  \\le |\\eta| < %5.1f$", 
                             hMCEff->GetYaxis()->GetBinLowEdge(iy), hMCEff->GetYaxis()->GetBinLowEdge(iy+1),
                             hMCEff->GetXaxis()->GetBinLowEdge(ix), hMCEff->GetXaxis()->GetBinLowEdge(ix+1));
      if (iy == ny) {
        binLabel = Form("$%5.1f < p_{T} $ , $%5.1f  \\le |\\eta| < %5.1f$", 
                        hMCEff->GetYaxis()->GetBinLowEdge(iy), 
                        hMCEff->GetXaxis()->GetBinLowEdge(ix), hMCEff->GetXaxis()->GetBinLowEdge(ix+1));
      }
      
      texfile << binLabel;
      texfile << "   &   ";

      ios_base::fmtflags flags = texfile.flags();
      texfile.precision(4);
      
      Double_t mceff  = hMCEff->GetCellContent(ix,iy);
      Double_t mcerrl = hMCErrl->GetCellContent(ix,iy);
      Double_t mcerrh = hMCErrh->GetCellContent(ix,iy);
      texfile << " " << setw(9) << fixed << mceff << " +/- " << TMath::Max(mcerrl,mcerrh);
      texfile << "   &   ";

      Double_t dataeff  = hDataEff->GetCellContent(ix,iy);
      Double_t dataerrl = hDataErrl->GetCellContent(ix,iy);
      Double_t dataerrh = hDataErrh->GetCellContent(ix,iy);
      texfile << " " << setw(9) << fixed << dataeff << " +/- " << TMath::Max(dataerrl,dataerrh);
      texfile << "   &   ";
     
      Double_t scale     = (hDataEff->GetCellContent(ix,iy))/(hMCEff->GetCellContent(ix,iy));
      Double_t scaleErrl = scale*sqrt(mcerrl*mcerrl/mceff/mceff + dataerrl*dataerrl/dataeff/dataeff);
      Double_t scaleErrh = scale*sqrt(mcerrh*mcerrh/mceff/mceff + dataerrh*dataerrh/dataeff/dataeff);
      texfile << " " << setw(9) << fixed << scale << " +/- " << TMath::Max(scaleErrl,scaleErrh);
      texfile << "   \\\\   ";
      texfile << endl;
      texfile << "\\hline";
      texfile << endl;

    }
  }
  texfile << "\\end{tabular}" << endl;
  texfile << "\\caption{CAPTION.}" << endl;
  texfile << "\\label{tab:eff_ele_offline}" << endl;
  texfile << "\\end{center}" << endl;
  texfile << "\\end{table}" << endl;
  texfile.close();
  cout << outputDir + "/eff_table.tex" << " created!" << endl;




  //--------------------------------------------------------------------------------------------------------------
  // Create TWIKI table
  //==============================================================================================================   

  ofstream twikifile;
  twikifile.open((outputDir + "/eff_table.twiki").c_str());
  assert(twikifile.is_open());
  

  twikifile << "| *pT bin* | *eta bin* | *MC Efficiency* | *Data Efficiency* | *ScaleFactor* | \n";
  
  for(Int_t iy=1; iy<=ny; iy++) {
    for(Int_t ix=1; ix<=nx; ix++) {

      string binLabel = Form("| *%4.1f < pT \\le %4.1f* | *%3.1f  <= eta < %3.1f* | ", 
                             hMCEff->GetYaxis()->GetBinLowEdge(iy), hMCEff->GetYaxis()->GetBinLowEdge(iy+1),
                             hMCEff->GetXaxis()->GetBinLowEdge(ix), hMCEff->GetXaxis()->GetBinLowEdge(ix+1));
      if (iy == ny) {
        binLabel = Form("| *%4.1f < pT* | *%3.1f  <= eta < %3.1f* | ", 
                        hMCEff->GetYaxis()->GetBinLowEdge(iy), 
                        hMCEff->GetXaxis()->GetBinLowEdge(ix), hMCEff->GetXaxis()->GetBinLowEdge(ix+1));
      }
      
      twikifile << binLabel ;

      ios_base::fmtflags flags = twikifile.flags();
      twikifile.precision(4);
      
      Double_t mceff  = hMCEff->GetCellContent(ix,iy);
      Double_t mcerrl = hMCErrl->GetCellContent(ix,iy);
      Double_t mcerrh = hMCErrh->GetCellContent(ix,iy);
      twikifile << " " << setw(9) << fixed << mceff << " +/- " << TMath::Max(mcerrl,mcerrh);
      twikifile << " | ";

      Double_t dataeff  = hDataEff->GetCellContent(ix,iy);
      Double_t dataerrl = hDataErrl->GetCellContent(ix,iy);
      Double_t dataerrh = hDataErrh->GetCellContent(ix,iy);
      twikifile << " " << setw(9) << fixed << dataeff << " +/- " << TMath::Max(dataerrl,dataerrh);
      twikifile << " | ";
     
      Double_t scale     = (hDataEff->GetCellContent(ix,iy))/(hMCEff->GetCellContent(ix,iy));
      Double_t scaleErrl = scale*sqrt(mcerrl*mcerrl/mceff/mceff + dataerrl*dataerrl/dataeff/dataeff);
      Double_t scaleErrh = scale*sqrt(mcerrh*mcerrh/mceff/mceff + dataerrh*dataerrh/dataeff/dataeff);
      twikifile << " " << setw(9) << fixed << scale << " +/- " << TMath::Max(scaleErrl,scaleErrh);
      twikifile << " |\n";

    }
  }
  twikifile.close();
  cout << outputDir + "/eff_table.twiki" << " created!" << endl;





  outputFile->WriteTObject(h2_results_selection, h2_results_selection->GetName(), "WriteDelete");
  outputFile->Close();

  gBenchmark->Show("printMuonWPEff"); 
}
Esempio n. 3
0
void printEff(const TString outdir   = "Data/extra",
              const TString mcfname  = "MC/eff.root",
	      const TString datfname = "Data/eff.root"
) {
  gBenchmark->Start("printEff");

    
  //--------------------------------------------------------------------------------------------------------------
  // Main analysis code 
  //==============================================================================================================   

  TFile mcfile(mcfname);
  TFile datafile(datfname);
  
  gSystem->mkdir(outdir,true);
    
  TH2F *hMCEff=0,   *hMCErrl=0,   *hMCErrh=0;
  TH2F *hDataEff=0, *hDataErrl=0, *hDataErrh=0;
  
  hMCEff  = (TH2F*)mcfile.Get("hEffEtaPt");
  hMCErrl = (TH2F*)mcfile.Get("hErrlEtaPt");
  hMCErrh = (TH2F*)mcfile.Get("hErrhEtaPt");  
  
  hDataEff  = (TH2F*)datafile.Get("hEffEtaPt");
  hDataErrl = (TH2F*)datafile.Get("hErrlEtaPt");
  hDataErrh = (TH2F*)datafile.Get("hErrhEtaPt");
    
  //
  // Print out text file
  //
  ofstream txtfile;
  TString outfname = outdir + TString("/eff_table.txt");
  txtfile.open(outfname.Data());
  assert(txtfile.is_open());
    
  const Int_t nx = hMCEff->GetNbinsX();
  const Int_t ny = hMCEff->GetNbinsY();
  
  txtfile << " pT        ";
  txtfile << " eta           ";
  txtfile << "    MC T&P           ";
  txtfile << "    Data T&P         ";  
  txtfile << "    Scale factor     ";
  txtfile << endl;
  txtfile << "----------------------------------------------------------------------------------------------------------------------------------------------------";
  txtfile << endl;
  
  for(Int_t ix=1; ix<=nx; ix++) {
    for(Int_t iy=1; iy<=ny; iy++) {    

      Double_t mceff  = hMCEff->GetBinContent(hMCEff->GetBin(ix,iy));
      Double_t mcerrl = hMCErrl->GetBinContent(hMCErrl->GetBin(ix,iy));
      Double_t mcerrh = hMCErrh->GetBinContent(hMCErrh->GetBin(ix,iy));
      
      Double_t dataeff  = hDataEff->GetBinContent(hDataEff->GetBin(ix,iy));
      Double_t dataerrl = hDataErrl->GetBinContent(hDataErrl->GetBin(ix,iy));
      Double_t dataerrh = hDataErrh->GetBinContent(hDataErrh->GetBin(ix,iy));
            
      Double_t scale     = (hDataEff->GetBinContent(hDataEff->GetBin(ix,iy)))/(hMCEff->GetBinContent(hMCEff->GetBin(ix,iy)));
      Double_t scaleerrl = scale*sqrt(mcerrl*mcerrl/mceff/mceff + dataerrl*dataerrl/dataeff/dataeff);
      Double_t scaleerrh = scale*sqrt(mcerrh*mcerrh/mceff/mceff + dataerrh*dataerrh/dataeff/dataeff);

      txtfile << "[" << setw(4) << hMCEff->GetYaxis()->GetBinLowEdge(iy) << "," << setw(4) << hMCEff->GetYaxis()->GetBinLowEdge(iy+1) << "]";
      txtfile << "[" << setw(6) << hMCEff->GetXaxis()->GetBinLowEdge(ix) << "," << setw(6) << hMCEff->GetXaxis()->GetBinLowEdge(ix+1) << "]";       
      ios_base::fmtflags flags = txtfile.flags();
      txtfile.precision(4);
      txtfile << " " << setw(9) << fixed << mceff << " +/- " << TMath::Max(mcerrl,mcerrh);
      txtfile << " " << setw(9) << fixed << dataeff << " +/- " << TMath::Max(dataerrl,dataerrh);
      txtfile << " " << setw(9) << fixed << scale << " +/- " << TMath::Max(scaleerrl,scaleerrh);
      txtfile << endl;
      txtfile.flags(flags);
    }
    txtfile << endl;
  }
  txtfile.close();
  
  //
  // Print out LaTeX tables
  //
  ofstream mc_latex;
  outfname = outdir + TString("/mc_eff_table.tex");
  mc_latex.open(outfname.Data());
  assert(mc_latex.is_open());
  
  ofstream dat_latex;
  outfname = outdir + TString("/dat_eff_table.tex");
  dat_latex.open(outfname.Data());
  assert(dat_latex.is_open());  
  
  ofstream sf_latex;
  outfname = outdir + TString("/sf_table.tex");
  sf_latex.open(outfname.Data());
  assert(sf_latex.is_open());
  
  mc_latex << "\\begin{table}[!ht]" << endl;
  mc_latex << "\\begin{center}" << endl;
  mc_latex << "\\begin{tabular}{c";
  for(int ix=0; ix<nx; ix++) mc_latex << "|c";
  mc_latex << "}" << endl;
  mc_latex << "\\hline";
  for(int ix=1; ix<=nx; ix++)
    mc_latex << " & $" << hDataEff->GetXaxis()->GetBinLowEdge(ix) << " < |\\eta| < " << hDataEff->GetXaxis()->GetBinLowEdge(ix+1) << "$";
  mc_latex << "  \\\\" << endl; 
  mc_latex << "\\hline" << endl;  

  dat_latex << "\\begin{table}[!ht]" << endl;
  dat_latex << "\\begin{center}" << endl;
  dat_latex << "\\begin{tabular}{c";
  for(int ix=0; ix<nx; ix++) dat_latex << "|c";
  dat_latex << "}" << endl;
  dat_latex << "\\hline";
  for(int ix=1; ix<=nx; ix++)
    dat_latex << " & $" << hDataEff->GetXaxis()->GetBinLowEdge(ix) << " < |\\eta| < " << hDataEff->GetXaxis()->GetBinLowEdge(ix+1) << "$";
  dat_latex << "  \\\\" << endl; 
  dat_latex << "\\hline" << endl;  
  
  sf_latex << "\\begin{table}[!ht]" << endl;
  sf_latex << "\\begin{center}" << endl;
  sf_latex << "\\begin{tabular}{c";
  for(int ix=0; ix<nx; ix++) sf_latex << "|c";
  sf_latex << "}" << endl;
  sf_latex << "\\hline";
  for(int ix=1; ix<=nx; ix++)
    sf_latex << " & $" << hDataEff->GetXaxis()->GetBinLowEdge(ix) << " < |\\eta| < " << hDataEff->GetXaxis()->GetBinLowEdge(ix+1) << "$";
  sf_latex << "  \\\\" << endl; 
  sf_latex << "\\hline" << endl;  
    
  for(Int_t iy=1; iy<=ny; iy++) {
    mc_latex  << "$" << setw(3) << hMCEff->GetYaxis()->GetBinLowEdge(iy) << " < p_T < " << setw(3) << hMCEff->GetYaxis()->GetBinLowEdge(iy+1) << "$";
    dat_latex << "$" << setw(3) << hMCEff->GetYaxis()->GetBinLowEdge(iy) << " < p_T < " << setw(3) << hMCEff->GetYaxis()->GetBinLowEdge(iy+1) << "$";
    sf_latex  << "$" << setw(3) << hMCEff->GetYaxis()->GetBinLowEdge(iy) << " < p_T < " << setw(3) << hMCEff->GetYaxis()->GetBinLowEdge(iy+1) << "$";
    for(Int_t ix=1; ix<=nx; ix++) {
      Double_t mceff  = hMCEff->GetBinContent(hMCEff->GetBin(ix,iy));
      Double_t mcerrl = hMCErrl->GetBinContent(hMCEffl->GetBin(ix,iy));
      Double_t mcerrh = hMCErrh->GetBinContent(hMCEffh->GetBin(ix,iy));
      
      Double_t dataeff  = hDataEff->GetBinContent(hDataEff->GetBin(ix,iy));
      Double_t dataerrl = hDataErrl->GetBinContent(hDataEffl->GetBin(ix,iy));
      Double_t dataerrh = hDataErrh->GetBinContent(hDataEffh->GetBin(ix,iy));
            
      Double_t scale     = (hDataEff->GetBinContent(hDataEff->GetBin(ix,iy)))/(hMCEff->GetBinContent(hMCEff->GetBin(ix,iy)));
      Double_t scaleerrl = scale*sqrt(mcerrl*mcerrl/mceff/mceff + dataerrl*dataerrl/dataeff/dataeff);
      Double_t scaleerrh = scale*sqrt(mcerrh*mcerrh/mceff/mceff + dataerrh*dataerrh/dataeff/dataeff);
                     
      ios_base::fmtflags flags = mc_latex.flags();
      mc_latex.precision(4);
      mc_latex << " & $" << setw(6) << fixed << mceff << " \\pm " << TMath::Max(mcerrl,mcerrh) << "$";
      mc_latex.flags(flags);

      flags = dat_latex.flags();
      dat_latex.precision(4);
      dat_latex << " & $" << setw(6) << fixed << dataeff << " \\pm " << TMath::Max(dataerrl,dataerrh) << "$";
      dat_latex.flags(flags);
      
      flags = sf_latex.flags();
      sf_latex.precision(4);
      sf_latex << " & $" << setw(6) << fixed << scale << " \\pm " << TMath::Max(scaleerrl,scaleerrh) << "$";
      sf_latex.flags(flags);
    }
    mc_latex  << "  \\\\" << endl;
    dat_latex << "  \\\\" << endl;
    sf_latex  << "  \\\\" << endl;
  }    
  mc_latex << "\\hline" << endl;
  mc_latex << "\\end{tabular}" << endl;
  mc_latex << "\\caption{Write some stuff}" << endl;
  mc_latex << "\\label{tab:mytable}" << endl;
  mc_latex << "\\end{center}" << endl;
  mc_latex << "\\end{table}" << endl;
  mc_latex.close();

  dat_latex << "\\hline" << endl;
  dat_latex << "\\end{tabular}" << endl;
  dat_latex << "\\caption{Write some stuff}" << endl;
  dat_latex << "\\label{tab:mytable}" << endl;
  dat_latex << "\\end{center}" << endl;
  dat_latex << "\\end{table}" << endl;
  dat_latex.close();
  
  sf_latex << "\\hline" << endl;
  sf_latex << "\\end{tabular}" << endl;
  sf_latex << "\\caption{Write some stuff}" << endl;
  sf_latex << "\\label{tab:mytable}" << endl;
  sf_latex << "\\end{center}" << endl;
  sf_latex << "\\end{table}" << endl;
  sf_latex.close();
    
  cout << " <> Output saved to " << outdir << endl;
  
  gBenchmark->Show("printEff"); 
}