void plotVariation(TH1F *nom, TH1F *up, TH1F *down, string phoCat, string name){

	TCanvas *canv = new TCanvas();
	canv->SetWindowSize(750,750);
	canv->SetCanvasSize(750,750);
	gStyle->SetOptStat(0);
	nom->SetLineWidth(3);
	nom->SetLineColor(kBlack);
	up->SetLineWidth(2);
	up->SetLineColor(kBlue);
	down->SetLineWidth(2);
	down->SetLineColor(kRed);

	double max=0.;
	max = nom->Integral()/(sqrt(2*TMath::Pi())*0.7)*nom->GetBinWidth(1);
	/// max = TMath::Max(max,nom->GetMaximum());
	/// max = TMath::Max(max,up->GetMaximum());
	/// max = TMath::Max(max,down->GetMaximum());

	nom->GetYaxis()->SetRangeUser(0,max*1.1);
	up->GetYaxis()->SetRangeUser(0,max*1.1);
	down->GetYaxis()->SetRangeUser(0,max*1.1);

	nom->GetXaxis()->SetRangeUser(mh_-10,mh_+10);
	up->GetXaxis()->SetRangeUser(mh_-10,mh_+10);
	down->GetXaxis()->SetRangeUser(mh_-10,mh_+10);

	nom->GetXaxis()->SetTitle("m_{#gamma#gamma} (GeV)");
	up->GetXaxis()->SetTitle("m_{#gamma#gamma} (GeV)");
	down->GetXaxis()->SetTitle("m_{#gamma#gamma} (GeV)");

	nom->SetTitle(Form("%s_%s",name.c_str(),phoCat.c_str()));
	up->SetTitle(Form("%s_%s",name.c_str(),phoCat.c_str()));
	down->SetTitle(Form("%s_%s",name.c_str(),phoCat.c_str()));

	TLegend *leg = new TLegend(0.6,0.6,0.89,0.89);
	leg->SetFillColor(0);
	leg->SetLineColor(0);
	leg->AddEntry(nom,"Nominal","L");
	if (quadInterpolate_!=0) {
		leg->AddEntry(up,Form("+%d#sigma",quadInterpolate_),"L");
		leg->AddEntry(down,Form("-%d#sigma",quadInterpolate_),"L");
	}
	else {
		leg->AddEntry(up,"+1#sigma","L");
		leg->AddEntry(down,"-1#sigma","L");
	}

	nom->Draw("HIST");
	up->Draw("HISTsame");
	down->Draw("HISTsame");
	nom->Draw("HISTsame");
	leg->Draw();

	canv->Print(Form("%s/systematics/%s_%s.pdf",plotDir_.c_str(),name.c_str(),phoCat.c_str()));
	canv->Print(Form("%s/systematics/%s_%s.png",plotDir_.c_str(),name.c_str(),phoCat.c_str()));
}
示例#2
0
void corrLaser()
{
  const Int_t kXtals = 1700;
  gStyle->SetTitleFontSize(0.06);
  //gStyle->SetStatFontSize(0.04);    
  gStyle->SetStatColor(10);
  gStyle->SetTitleFillColor(10);
  gStyle->SetCanvasColor(10);
  //  gStyle->SetOptStat("e");
  gStyle->SetOptStat(111111);
  gStyle->SetOptFit(1001);
  gStyle->SetPaperSize(20,26);
  //gStyle->SetStatH(0.10);  
  //gStyle->SetStatW(0.08);
  gStyle->SetStatH(0.15);  
  gStyle->SetStatW(0.25);

  TFile* fout = new TFile("corrBeam.root","recreate");

  hRatio126laser  = new TH1F("hRatio126laser","SM6, Gain ratio x12/x6", 
			      50, 1.85, 2.05);
  hRatio126TP = new TH1F("hRatio126TP","SM6, Gain ratio x12/x6", 
			   50, 1.85, 2.05);

  hCorr126 = new TH2F("hCorr126","SM6, Gain ratio x12/x6 corr.", 
			50,1.9, 2.02,50, 1.9, 2.02);
  
  hDiff126 = new TH1F("hDiff126","SM6, Gain ratio x12/x6 diff.", 
			50, -0.015,0.025);

  TCanvas cPrint("cPrint");
 
  //  Double_t ratio121TP[kXtals];
  Double_t ratio126laser[kXtals];
  Double_t ratio126TP[kXtals];
  Double_t ratio121TP[kXtals];
  for(Int_t iXtal = 0; iXtal < kXtals; iXtal++) {
    ratio126laser[iXtal] = 0.;
    ratio126TP[iXtal] = 0.;
  }
  // Open laser ratio file
  ifstream fRatioInput;
  fRatioInput.open("relgain_laser.out");
  if(!fRatioInput.is_open()) {
    cout  << "can't open laser Ratio file " << endl;
    return;
  }

  for(Int_t i = 0; !fRatioInput.eof(); i++) {
    Int_t Xtal,iXtal;
    Double_t ratio126, error;
    fRatioInput >> Xtal >> iXtal >> ratio126 >> error;
    ratio126laser[iXtal - 1] = ratio126;
    //    cout << iXtal << " " << ratio126laser[iXtal - 1] << endl;
  }
  fRatioInput.close();

  // Open Test Pulse ratio file
  ifstream fRatioInput;
  fRatioInput.open("TestPulse_gainratio_SM06.txt");
  if(!fRatioInput.is_open()) {
    cout  << "can't open old Ratio file " << endl;
    return;
  }
  
  for(Int_t iXtal = 0; !fRatioInput.eof(); iXtal++) {
    Int_t Xtal;
    fRatioInput >>  Xtal >> ratio121TP[iXtal] >> ratio126TP[iXtal];
    if(Xtal != iXtal + 1) cout << " pb Xtal " << Xtal << " " << iXtal << endl;
    cout << iXtal << " " << ratio126TP[iXtal] << endl;
  }
  fRatioInput.close();

  for(Int_t iXtal = 0; iXtal < kXtals; iXtal++) {
    if (ratio126laser[iXtal] != 0) {
      hRatio126laser->Fill(ratio126laser[iXtal]);
      hRatio126TP->Fill(ratio126TP[iXtal]);
      hCorr126->Fill(ratio126laser[iXtal], ratio126TP[iXtal]);
      hDiff126->
	  Fill((ratio126laser[iXtal]-ratio126TP[iXtal])/ratio126laser[iXtal]);
      //      cout << iXtal << " " << ratio126laser[iXtal] << " " << ratio126TP[iXtal]
      //	   << endl;
    }
  }

  TLegend* legend1 = new TLegend(0.15,0.75,0.35,0.85);
  legend1->SetBorderSize(1); 
  legend1->SetFillColor(0); 
  legend1->SetTextSize(0.03); 
  legend1->SetTextFont(42);
 
  TCanvas* cRatio = new TCanvas("cRatio", "cRatio");
  cRatio->SetCanvasSize(630,885);
  cRatio->Divide(1,2);
  cRatio->cd(1);
  hRatio126laser->Draw();
  gPad->Update();
  TPaveStats* st1 = 
    (TPaveStats*)hRatio126laser->GetListOfFunctions()->FindObject("stats");
  st1->SetLineColor(3);
  hRatio126laser->SetFillColor(3);
  hRatio126laser->GetYaxis()->SetTitle("# Xtals");
  legend1->AddEntry(hRatio126laser, "laser","f");
  hRatio126laser->Write();

  hRatio126TP->Draw("sames");
  gPad->Update();
  TPaveStats* st2 = 
    (TPaveStats*)hRatio126TP->GetListOfFunctions()->FindObject("stats");
  st2->SetLineColor(4);
  hRatio126TP->SetFillColor(4);
  hRatio126TP->GetYaxis()->SetTitle("# Xtals");
  legend1->AddEntry(hRatio126TP, "TestPulse","f");
  legend1->Draw();
  cRatio->Modified();
  hRatio126TP->Write();
  gPad->Print("hRatio126TP.eps");

  cRatio->Write("cRatio");
  // cRatio->Print("corrBeam.ps(");

  TCanvas* cRatio = new TCanvas("cRatio","Gain Ratio");
  cRatio->SetCanvasSize(630,885);
  cRatio->Divide(1,2);
  cRatio->cd(1);
  hCorr126->SetMarkerStyle(20);
  hCorr126->Draw();
  hCorr126->GetXaxis()->SetTitle("laser");
  hCorr126->GetYaxis()->SetTitle("TestPulse");
  TLine* l = new TLine(1.9,1.9,2.02,2.02);
  l->SetLineColor(2);
  l->SetLineWidth(3);
  l->Draw();
  cRatio->Update();
  gPad->Print("hCorr126.eps");

  cRatio->cd(2);
  hDiff126->Draw();
  //  hDiff126->GetXaxis()->SetTitle("(Weights 6 - Weights 12)/Weights 6");
  hDiff126->GetXaxis()->SetTitle("(laser - TestPulse)/laser");
  hDiff126->GetYaxis()->SetTitle("# Xtals");
  hDiff126->SetFillColor(5);
  gPad->Print("hDiff126.eps");
  cRatio->Print("corrBeam.ps");

  fout->Write();
  fout->Close();
 
  gROOT->ProcessLine(".q");
}
void determineWorkingPoint(TString algo="csv",TString baseURL="~/scratch0/top-nosyst/plotter.root")
{
  float workPoint(0.244);
  float sfb(1.020),    sfberr(0.04);      //from BTV-11-003
  float sflight(1.08), sflighterr(0.13);  //from BTV-11-002
  //  float sfb(0.99),        sfberr(0.099);      //from BTV-11-001
  //  float sflight(1.07882), sflighterr(0.244);  //from BTV-11-001
  if(algo=="tche")
    {
      workPoint=1.7;
      sfb=0.95; sfberr=0.095;
      sflight=1.08018; sflighterr=0.1125;
    }

  TObjArray bjets=getDistributionFromPlotter(algo+"b",baseURL);
  TObjArray lightjets=getDistributionFromPlotter(algo+"light",baseURL);
  
  TH1F *bDisc=(TH1F *) bjets.At(1);
  TH1F *lightDisc=(TH1F *) lightjets.At(1);
  
  setStyle();
  gStyle->SetOptFit(0);

  TCanvas *cnv = getNewCanvas("c","c",false);
  cnv->Clear();
  cnv->SetCanvasSize(1200,1200);
  cnv->SetWindowSize(1200,1200);
  cnv->Divide(2,2);

  //draw the discriminator
  TPad *p = (TPad *)cnv->cd(1); 
  bDisc->SetTitle("b");
  bDisc->SetLineColor(1);
  bDisc->SetMarkerColor(1);
  bDisc->SetMarkerStyle(20);
  bDisc->SetFillStyle(0);
  bDisc->DrawNormalized("histe1");
  lightDisc->SetLineColor(1);
  lightDisc->SetMarkerColor(1);
  lightDisc->SetMarkerStyle(24);
  lightDisc->SetFillStyle(0);
  lightDisc->SetTitle("udcsg");
  lightDisc->DrawNormalized("histe1same");
  TLegend *leg=p->BuildLegend();
  formatForCmsPublic(p,leg,"CMS simulation",2);

  //draw the b/light efficiencies
  p=(TPad *)cnv->cd(2);
  p->SetLogy();
  TGraphAsymmErrors *bEff     = getEfficiencyFrom(bDisc);
  TGraphAsymmErrors *lightEff = getEfficiencyFrom(lightDisc);
  bEff->SetMarkerStyle(20);
  bEff->SetFillStyle(0);
  bEff->Draw("ap");
  bEff->GetXaxis()->SetTitle(bDisc->GetXaxis()->GetTitle());
  bEff->GetYaxis()->SetTitle("Efficiency");
  lightEff->SetMarkerStyle(24);
  lightEff->SetFillStyle(0);
  lightEff->Draw("p");

  //draw relatively to a given working point
  p=(TPad *)cnv->cd(3);
  Double_t baseBEff=bEff->Eval(workPoint);
  TGraphAsymmErrors *relBEff=new TGraphAsymmErrors;
  relBEff->SetMarkerStyle(20);
  relBEff->SetFillStyle(0);
  Double_t baseLightEff=lightEff->Eval(workPoint);
  TGraphAsymmErrors *relLightEff=new TGraphAsymmErrors;
  relLightEff->SetMarkerStyle(24);
  relLightEff->SetFillStyle(0);
  for(int ip=0; ip<bEff->GetN(); ip++)
    {
      Double_t cut, y,ey; 
      bEff->GetPoint(ip,cut,y);     ey = bEff->GetErrorY(ip);
      Double_t relEff(y/baseBEff);
      if(relEff<sfb+2*sfberr && relEff>sfb-2*sfberr)
	{
	  int ipt=relBEff->GetN();
	  relBEff->SetPoint(ipt,cut,relEff);
	  relBEff->SetPointError(ipt,0,0,ey/baseBEff,ey/baseBEff);
	}

      lightEff->GetPoint(ip,cut,y);     ey = lightEff->GetErrorY(ip);
      relEff=y/baseLightEff;
      //      if(relEff<sflight+7*sflighterr && relEff>sflight-7*sflighterr)
      if(relEff<sflight+3*sflighterr && relEff>sflight-3*sflighterr)
	{
	  int ipt=relLightEff->GetN();
	  relLightEff->SetPoint(ipt,cut,relEff);
	  relLightEff->SetPointError(ipt,0,0,ey/baseLightEff,ey/baseLightEff);
	}
    }

  relLightEff->Draw("ap");
  relLightEff->GetXaxis()->SetTitle( bDisc->GetXaxis()->GetTitle() );
  relLightEff->GetYaxis()->SetTitle( "#varepsilon/#varepsilon_{0}" );
  relLightEff->Fit("expo","Q+");
  TF1 *ffunc=relLightEff->GetFunction("expo");
  float newLightCut=(TMath::Log(sflight)-ffunc->GetParameter(0))/ffunc->GetParameter(1);
  float newLightCutErrPlus=(TMath::Log(sflight+sflighterr)-ffunc->GetParameter(0))/ffunc->GetParameter(1)-newLightCut;
  float newLightCutErrMinus=(TMath::Log(sflight-sflighterr)-ffunc->GetParameter(0))/ffunc->GetParameter(1)-newLightCut;
  TArrow *lightArrow = new TArrow(newLightCut, sflight-3*sflighterr, newLightCut, sflight-3*sflighterr*0.8, 0.02, "<|");
  lightArrow->SetLineColor(kGray+2);
  lightArrow->SetFillColor(kGray+2);
  lightArrow->Draw("SAME <|");

  relBEff->Draw("p");
  relBEff->Fit("expo","Q+");
  ffunc=relBEff->GetFunction("expo");
  float newBCut=(TMath::Log(sfb)-ffunc->GetParameter(0))/ffunc->GetParameter(1);
  float newBCutErrPlus=(TMath::Log(sfb+sfberr)-ffunc->GetParameter(0))/ffunc->GetParameter(1)-newBCut;
  float newBCutErrMinus=(TMath::Log(sfb-sfberr)-ffunc->GetParameter(0))/ffunc->GetParameter(1)-newBCut;
  cout <<  sfb << " " << sfb+sfberr << " " << sfb-sfberr << endl;
  TArrow *bArrow    = new TArrow(newBCut, sflight-3*sflighterr, newBCut, sflight-3*sflighterr*0.8, 0.02, "<|");
  bArrow->Draw("SAME <|");

  //draw epsilon_b vs epsilon_q
  p=(TPad *)cnv->cd(4);
  p->SetLogy();

  TGraphAsymmErrors *perf=new TGraphAsymmErrors;
  perf->SetName("algoperformance");
  perf->SetMarkerStyle(20);
  perf->SetFillStyle(0);
  for(int ip=0; ip<bEff->GetN(); ip++)
    {
      Double_t cut;
      Double_t x,ex; bEff->GetPoint(ip,cut,x);     ex = bEff->GetErrorY(ip);
      Double_t y,ey; lightEff->GetPoint(ip,cut,y); ey = lightEff->GetErrorY(ip);
      perf->SetPoint(ip,x,y);
      perf->SetPointError(ip,ex,ex,ey,ey);
    }
  perf->Draw("ap");
  perf->GetXaxis()->SetTitle( bEff->GetTitle() );
  perf->GetYaxis()->SetTitle( lightEff->GetTitle() );

  //new performance expected after applying the new cuts
  TGraphAsymmErrors *newperf=new TGraphAsymmErrors;
  newperf->SetName("newalgoperformance");
  newperf->SetMarkerStyle(24);
  newperf->SetFillStyle(0);
  newperf->SetLineWidth(2);
  newperf->SetLineColor(kRed);
  newperf->SetLineColor(kRed);
  newperf->SetPoint(0,baseBEff*sfb,baseLightEff*sflight);
  newperf->SetPointError(0,baseBEff*sfberr,baseBEff*sfberr,baseLightEff*sflighterr,baseLightEff*sflighterr);
  newperf->Draw("p");

  cnv->Modified();
  cnv->Update();
  cnv->SaveAs("discFlavor.C");  
  cnv->SaveAs("discFlavor.png");  


  cout << "[determineWorkingPoint]" << endl
       << "To emulate the measured scale-factors you can use the following new cuts per jet flavor" << endl
       << "SF-b : "     << newBCut << " +" << newBCutErrPlus << " " << newBCutErrMinus << endl
       << "SF-light : " << newLightCut << " +" << newLightCutErrPlus << " " << newLightCutErrMinus << endl;
}
示例#4
0
int main(int argc, char* argv[]) {

  TH1::SetDefaultSumw2();

  ProgramOptions options(argc, argv);

  double lumi = options.lumi;

  std::cout << "Integrated luminosity : " << lumi << " pb-1" << std::endl;

  Datasets datasets(options.iDir);
  datasets.readFile(options.datasetFile);

  LeptonWeights lw;

  // cuts
  Cuts cuts;
  TCut puWeight("puWeight");
  TCut trigCorr( "(trigCorrWeight>0) ? trigCorrWeight : 1." );

  // For lepton weights
  TCut elTightWeight     = cuts.elTightWeight(options.leptCorr);
  TCut muTightWeight     = cuts.muTightWeight(options.leptCorr);
  TCut leptonTightWeight = elTightWeight * muTightWeight;
  // ok to use muon and electron tight weights in control regions, since we use a lepton veto, so you would have a muon weight = 1 
  
  TCut elVetoWeight      = cuts.elVetoWeight(options.leptCorr);
  TCut muVetoWeight      = cuts.muVetoWeight(options.leptCorr);
  TCut leptonVetoWeight  = elVetoWeight * muVetoWeight;

  TCut otherCutsTight = puWeight * trigCorr * leptonTightWeight;
  TCut otherCutsVeto  = puWeight * trigCorr * leptonVetoWeight;

  TCut cutSignalNoMETNoDPhi = cuts.HLTandMETFilters() + cuts.leptonVeto() + cuts.vbf();
  
  // Tau cuts
  // Get puWeight etc added below if necessary
  // standard TightMjj selection - essentially signal selection but no DPhiJJ and no cjv
  TCut cutTightMjj_basic = cuts.HLTandMETFilters() + cuts.cutWTau("lVeto") + cuts.cutWTau("dijet") + cuts.cutWTau("dEtaJJ") + cuts.cutWTau("MET") + cuts.cutWTau("Mjj"); 
  TCut cutwElnoMjj       = cuts.cutWEl("trigger")+ cuts.cutWEl("wEl")+cuts.cutWEl("lVeto")+cuts.cutWEl("dijet")+cuts.cutWEl("dEtaJJ")+cuts.cutWEl("MET")+cuts.cutWEl("CJV")+cuts.cutWEl("dPhiJJ"); //for W->el regions
  TCut cutTightMjj(""); // used to add in PU, trig corr, wWeight etc

  // double dphiEdges[4] = { 0., 1.0, 2.6, TMath::Pi() };
  double dphiEdges[5]     = { 0., 1.0, 1.8, 2.6, TMath::Pi() };
  double MjjEdges[5]      = { 800., 1000., 1200., 1800., 3000. };
  double METEdges[5]      = { 100., 150., 200., 250., 300. };
  //double CenJetEtEdges[5] = { 10., 40., 70., 120. ,200};
  double CenJetEtEdges[5] = { 10., 30., 60., 100., 150.};

  TH1D* hWEl_MCGen_DPhi  = new TH1D("hWEl_MCGen_DPhi", "", 1, 0,1);   // W+jets MC gen level el
  TH1D* hWTau_MCGen_DPhi = new TH1D("hWTau_MCGen_DPhi", "", 1, 0,1);   // W+jets MC gen level Tau - for ID 

  // DPhiJJ histograms
  // WEl histograms
  TH1D* hWEl_MCC_DPhi    = new TH1D("hWEl_MCC_DPhi", "", 4, dphiEdges);  // W+jets MC el ctrl region
  
  TH1D* hWEl_BGC_DPhi    = new TH1D("hWEl_BGC_DPhi", "", 4, dphiEdges);  // background MC ctrl region
  TH1D* hWEl_DataC_DPhi  = new TH1D("hWEl_DataC_DPhi", "", 4, dphiEdges);  // Data ctrl region
  
  // WTau histograms
  TH1D* hWTau_MCC_DPhi   = new TH1D("hWTau_MCC_DPhi", "", 4, dphiEdges);  // W+jets MC tau ctrl region - for CJV ratio
  
  TH1D* hWTau_BGC_DPhi   = new TH1D("hWTau_BGC_DPhi", "", 4, dphiEdges);  // background MC ctrl region
  TH1D* hWTau_DataC_DPhi = new TH1D("hWTau_DataC_DPhi", "", 4, dphiEdges);  // Data ctrl region

  // Mjj histograms
  // WEl histograms
  TH1D* hWEl_MCC_Mjj    = new TH1D("hWEl_MCC_Mjj", "", 4, MjjEdges);  // W+jets MC el ctrl region
  TH1D* hWEl_BGC_Mjj    = new TH1D("hWEl_BGC_Mjj", "", 4, MjjEdges);  // background MC ctrl region
  TH1D* hWEl_DataC_Mjj  = new TH1D("hWEl_DataC_Mjj", "", 4, MjjEdges);  // Data ctrl region
  
  // WTau histograms
  TH1D* hWTau_MCC_Mjj   = new TH1D("hWTau_MCC_Mjj", "", 4, MjjEdges);  // W+jets MC tau ctrl region - for CJV ratio
  TH1D* hWTau_BGC_Mjj   = new TH1D("hWTau_BGC_Mjj", "", 4, MjjEdges);  // background MC ctrl region
  TH1D* hWTau_DataC_Mjj = new TH1D("hWTau_DataC_Mjj", "", 4, MjjEdges);  // Data ctrl region  // Mjj histograms
 

  // MET histograms
  // WEl histograms
  TH1D* hWEl_MCC_MET    = new TH1D("hWEl_MCC_MET", "", 4, METEdges);  // W+jets MC el ctrl region
  TH1D* hWEl_BGC_MET    = new TH1D("hWEl_BGC_MET", "", 4, METEdges);  // background MC ctrl region
  TH1D* hWEl_DataC_MET  = new TH1D("hWEl_DataC_MET", "", 4, METEdges);  // Data ctrl region
  
  // WTau histograms
  TH1D* hWTau_MCC_MET   = new TH1D("hWTau_MCC_MET", "", 4, METEdges);  // W+jets MC tau ctrl region - for CJV ratio
  TH1D* hWTau_BGC_MET   = new TH1D("hWTau_BGC_MET", "", 4, METEdges);  // background MC ctrl region
  TH1D* hWTau_DataC_MET = new TH1D("hWTau_DataC_MET", "", 4, METEdges);  // Data ctrl region  // MET histograms
 
  // Central Jet Et histograms 
  // WEl histograms
  TH1D* hWEl_MCC_CenJetEt    = new TH1D("hWEl_MCC_CenJetEt", "", 4, CenJetEtEdges);  // W+jets MC el ctrl region
  TH1D* hWEl_BGC_CenJetEt    = new TH1D("hWEl_BGC_CenJetEt", "", 4, CenJetEtEdges);  // background MC ctrl region
  TH1D* hWEl_DataC_CenJetEt  = new TH1D("hWEl_DataC_CenJetEt", "", 4, CenJetEtEdges);  // Data ctrl region
  
  // WTau histograms
  TH1D* hWTau_MCC_CenJetEt   = new TH1D("hWTau_MCC_CenJetEt", "", 4, CenJetEtEdges);  // W+jets MC tau ctrl region - for CJV ratio
  TH1D* hWTau_BGC_CenJetEt   = new TH1D("hWTau_BGC_CenJetEt", "", 4, CenJetEtEdges);  // background MC ctrl region
  TH1D* hWTau_DataC_CenJetEt = new TH1D("hWTau_DataC_CenJetEt", "", 4, CenJetEtEdges);  // Data ctrl region

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

    Dataset dataset = datasets.getDataset(i);

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

    // setup cuts
    TCut wWeight("");

    TCut cutD = cuts.cutDataset(dataset.name);
    TCut cutWEl_Gen           = cuts.wElGen();
    TCut cutWTau_Gen          = cuts.wTauGen();

    TCut cutWEl_MCC_DPhi      = cuts.wElGen() + cuts.wElVBF() + cuts.cutWEl("MET");
    TCut cutWEl_C_DPhi        = cutD + cuts.wElVBF() + cuts.cutWEl("MET"); // WEl MC control region; WEl MC gen only; control region for data/bg - all for DPhiJJ
    TCut cutWTau_MCC_DPhi     = cuts.wTauGen() + cutTightMjj_basic + cuts.cutWTau("wTau");
    TCut cutWTau_C_DPhi       = cutD + cutTightMjj_basic + cuts.cutWTau("wTau"); // WTau MC control region; WTau MC gen only; control region for data/bg - all for DPhiJJ

    TCut cutWEl_MCC_Mjj       = cuts.wElGen() + cutwElnoMjj;
    TCut cutWEl_C_Mjj         = cutD + cutwElnoMjj;
    TCut cutWTau_MCC_Mjj      = cuts.wTauGen() + cuts.cutWTau("wTau") + cuts.cutWTau("trigger") + cuts.cutWTau("lVeto") + cuts.cutWTau("dijet") + cuts.cutWTau("dEtaJJ") + cuts.cutWTau("MET") + cuts.cutWTau("dPhiJJ");
    TCut cutWTau_C_Mjj        = cutD + cuts.cutWTau("wTau") + cuts.cutWTau("trigger") + cuts.cutWTau("lVeto") + cuts.cutWTau("dijet") + cuts.cutWTau("dEtaJJ") + cuts.cutWTau("MET") + cuts.cutWTau("dPhiJJ");
    
    TCut cutWEl_MCC_MET       = cuts.wElGen() + cuts.wElVBF() + cuts.cutWEl("dPhiJJ");
    TCut cutWEl_C_MET         = cutD + cuts.wElVBF() + cuts.cutWEl("dPhiJJ");
    TCut cutWTau_MCC_MET      = cuts.wTauGen() + cuts.cutWTau("wTau") + cuts.cutWTau("trigger") + cuts.cutWTau("lVeto") + cuts.cutWTau("dijet") + cuts.cutWTau("dEtaJJ") + cuts.cutWTau("Mjj") + cuts.cutWTau("dPhiJJ");
    TCut cutWTau_C_MET        = cutD + cuts.cutWTau("wTau") + cuts.cutWTau("trigger") + cuts.cutWTau("lVeto") + cuts.cutWTau("dijet") + cuts.cutWTau("dEtaJJ") + cuts.cutWTau("Mjj") + cuts.cutWTau("dPhiJJ");
    
    TCut cutWEl_MCC_CenJetEt  = cuts.wElGen() + cuts.cutWEl("trigger") + cuts.cutWEl("lVeto") + cuts.cutWEl("wEl") + cuts.cutWEl("dijet") + cuts.cutWEl("dEtaJJ") + cuts.cutWEl("Mjj") + cuts.cutWEl("MET") + cuts.cutWEl("dPhiJJ");
    TCut cutWEl_C_CenJetEt    = cutD + cuts.cutWEl("trigger") + cuts.cutWEl("lVeto") + cuts.cutWEl("wEl") + cuts.cutWEl("dijet") + cuts.cutWEl("dEtaJJ") + cuts.cutWEl("Mjj") + cuts.cutWEl("MET") + cuts.cutWEl("dPhiJJ");
    TCut cutWTau_MCC_CenJetEt = cuts.wTauGen() + cutTightMjj_basic + cuts.cutWTau("wTau") + cuts.cutWTau("dPhiJJ");
    TCut cutWTau_C_CenJetEt   = cutD + cutTightMjj_basic + cuts.cutWTau("wTau") + cuts.cutWTau("dPhiJJ");

    // TEMP HISTS - DPhiJJ
    TH1D* hWEl_MCGen_DPhi_tmp      = new TH1D("hWEl_MCGen_DPhi_tmp", "",1, 0,1);  // W+jets El MC gen
    TH1D* hWTau_MCGen_DPhi_tmp     = new TH1D("hWTau_MCGen_DPhi_tmp", "",1, 0,1);  // W+jets Tau MC gen

    // tmp histograms - WEl
    TH1D* hWEl_MCC_DPhi_tmp        = new TH1D("hWEl_MCC_DPhi_tmp",   "",4,dphiEdges);  // W+jets MC ctrl region
    TH1D* hWEl_DataC_DPhi_tmp      = new TH1D("hWEl_DataC_DPhi_tmp", "",4,dphiEdges); // Data in WEl ctrl region
    TH1D* hWEl_BGC_DPhi_tmp        = new TH1D("hWEl_BGC_DPhi_tmp",   "",4,dphiEdges); //BG in WEl ctrl region
    
    // tmp histograms - wTau
    TH1D* hWTau_MCC_DPhi_tmp       = new TH1D("hWTau_MCC_DPhi_tmp",   "",4,dphiEdges);  // W+jets MC ctrl region
    TH1D* hWTau_DataC_DPhi_tmp     = new TH1D("hWTau_DataC_DPhi_tmp", "",4,dphiEdges); // Data in WTau ctrl region
    TH1D* hWTau_BGC_DPhi_tmp       = new TH1D("hWTau_BGC_DPhi_tmp",   "",4,dphiEdges); // BG in WTau ctrl region

    // TEMP HISTS - Mjj
    // tmp histograms - WEl
    TH1D* hWEl_MCC_Mjj_tmp         = new TH1D("hWEl_MCC_Mjj_tmp",   "",4,MjjEdges);  // W+jets MC ctrl region
    TH1D* hWEl_DataC_Mjj_tmp       = new TH1D("hWEl_DataC_Mjj_tmp", "",4,MjjEdges);
    TH1D* hWEl_BGC_Mjj_tmp         = new TH1D("hWEl_BGC_Mjj_tmp",   "",4,MjjEdges);
    
    // tmp histograms - wTau
    TH1D* hWTau_MCC_Mjj_tmp        = new TH1D("hWTau_MCC_Mjj_tmp",   "",4,MjjEdges);  // W+jets MC ctrl region
    TH1D* hWTau_DataC_Mjj_tmp      = new TH1D("hWTau_DataC_Mjj_tmp", "",4,MjjEdges);
    TH1D* hWTau_BGC_Mjj_tmp        = new TH1D("hWTau_BGC_Mjj_tmp",   "",4,MjjEdges);
    
    // TEMP HISTS - MET
    // tmp histograms - WEl
    TH1D* hWEl_MCC_MET_tmp         = new TH1D("hWEl_MCC_MET_tmp",   "",4,METEdges);  // W+jets MC ctrl region
    TH1D* hWEl_DataC_MET_tmp       = new TH1D("hWEl_DataC_MET_tmp", "",4,METEdges);
    TH1D* hWEl_BGC_MET_tmp         = new TH1D("hWEl_BGC_MET_tmp",   "",4,METEdges);
    
    // tmp histograms - wTau
    TH1D* hWTau_MCC_MET_tmp        = new TH1D("hWTau_MCC_MET_tmp",   "",4,METEdges);  // W+jets MC ctrl region
    TH1D* hWTau_DataC_MET_tmp      = new TH1D("hWTau_DataC_MET_tmp", "",4,METEdges);
    TH1D* hWTau_BGC_MET_tmp        = new TH1D("hWTau_BGC_MET_tmp",   "",4,METEdges);
    
    // TEMP HISTS - CenJetEt
    // tmp histograms - WEl
    TH1D* hWEl_MCC_CenJetEt_tmp    = new TH1D("hWEl_MCC_CenJetEt_tmp",   "",4,CenJetEtEdges);  // W+jets MC ctrl region
    TH1D* hWEl_DataC_CenJetEt_tmp  = new TH1D("hWEl_DataC_CenJetEt_tmp", "",4,CenJetEtEdges);
    TH1D* hWEl_BGC_CenJetEt_tmp    = new TH1D("hWEl_BGC_CenJetEt_tmp",   "",4,CenJetEtEdges);
    
    // tmp histograms - wTau
    TH1D* hWTau_MCC_CenJetEt_tmp   = new TH1D("hWTau_MCC_CenJetEt_tmp",   "",4,CenJetEtEdges);  // W+jets MC ctrl region
    TH1D* hWTau_DataC_CenJetEt_tmp = new TH1D("hWTau_DataC_CenJetEt_tmp", "",4,CenJetEtEdges);
    TH1D* hWTau_BGC_CenJetEt_tmp   = new TH1D("hWTau_BGC_CenJetEt_tmp",   "",4,CenJetEtEdges);

    // Weight to lumi
    double weight = (dataset.isData) ? 1. : (lumi * dataset.sigma / dataset.nEvents);
    if(dataset.name == "EWK_ZvvFake") weight *= constants::ratioZToNuNuZToLL;

    // check it's  W+Jets
    bool isWJets = false;
    bool isEwkW  = false;
    if (dataset.name == "WJets" ||
        dataset.name == "W1Jets" || 
        dataset.name == "W2Jets" || 
        dataset.name == "W3Jets" || 
        dataset.name == "W4Jets" ||
        dataset.name == "EWK_Wp2Jets" ||
        dataset.name == "EWK_Wm2Jets") {

      std::cout << "Analysing W MC     : " << dataset.name << std::endl;
      std::cout << "  weight : " << weight << std::endl;

      if (dataset.name == "EWK_Wp2Jets" || dataset.name == "EWK_Wm2Jets") isEwkW = true;
      else isWJets = true;

      if(isWJets) wWeight =  cuts.wWeight();

      // El cuts
      tree->Draw("vbfDPhi>>hWEl_MCC_DPhi_tmp", otherCutsTight * wWeight * cutWEl_MCC_DPhi);
      tree->Draw("0.5>>hWEl_MCGen_DPhi_tmp", otherCutsTight * wWeight * cutWEl_Gen);
      tree->Draw("vbfM>>hWEl_MCC_Mjj_tmp", otherCutsTight * wWeight * cutWEl_MCC_Mjj);
      tree->Draw("metNoWLepton>>hWEl_MCC_MET_tmp", otherCutsTight * wWeight * cutWEl_MCC_MET);
      tree->Draw("cenJetEt>>hWEl_MCC_CenJetEt_tmp", otherCutsTight * wWeight * cutWEl_MCC_CenJetEt);

      hWEl_MCC_DPhi_tmp->Scale(weight);
      hWEl_MCGen_DPhi_tmp->Scale(weight);
      hWEl_MCC_Mjj_tmp->Scale(weight);
      hWEl_MCC_MET_tmp->Scale(weight);
      hWEl_MCC_CenJetEt_tmp->Scale(weight);
      
      // add to final histogram
      hWEl_MCC_DPhi->Add(hWEl_MCC_DPhi_tmp);
      hWEl_MCGen_DPhi->Add(hWEl_MCGen_DPhi_tmp);
      hWEl_MCC_Mjj->Add(hWEl_MCC_Mjj_tmp);
      hWEl_MCC_MET->Add(hWEl_MCC_MET_tmp);
      hWEl_MCC_CenJetEt->Add(hWEl_MCC_CenJetEt_tmp);

      tree->Draw("vbfDPhi>>hWTau_MCC_DPhi_tmp", otherCutsVeto * wWeight * cutWTau_MCC_DPhi);
      tree->Draw("0.5>>hWTau_MCGen_DPhi_tmp", otherCutsVeto * wWeight * cutWTau_Gen);
      tree->Draw("vbfM>>hWTau_MCC_Mjj_tmp", otherCutsVeto * wWeight * cutWTau_MCC_Mjj);
      tree->Draw("met>>hWTau_MCC_MET_tmp", otherCutsVeto * wWeight * cutWTau_MCC_MET);
      tree->Draw("cenJetEt>>hWTau_MCC_CenJetEt_tmp", otherCutsVeto * wWeight * cutWTau_MCC_CenJetEt);

      hWTau_MCC_DPhi_tmp->Scale(weight);
      hWTau_MCGen_DPhi_tmp->Scale(weight);
      hWTau_MCC_Mjj_tmp->Scale(weight);
      hWTau_MCC_MET_tmp->Scale(weight);
      hWTau_MCC_CenJetEt_tmp->Scale(weight);
      
      // add to final histogram
      hWTau_MCC_DPhi->Add(hWTau_MCC_DPhi_tmp);
      hWTau_MCGen_DPhi->Add(hWTau_MCGen_DPhi_tmp);
      hWTau_MCC_Mjj->Add(hWTau_MCC_Mjj_tmp);
      hWTau_MCC_MET->Add(hWTau_MCC_MET_tmp);
      hWTau_MCC_CenJetEt->Add(hWTau_MCC_CenJetEt_tmp);

    } else if (dataset.isData) {
     
      std::cout << "Analysing Data     : " << dataset.name << std::endl;

      // El region
      tree->Draw("vbfDPhi>>hWEl_DataC_DPhi_tmp", cutWEl_C_DPhi);
      hWEl_DataC_DPhi->Add(hWEl_DataC_DPhi_tmp);

      tree->Draw("vbfM>>hWEl_DataC_Mjj_tmp", cutWEl_C_Mjj);
      hWEl_DataC_Mjj->Add(hWEl_DataC_Mjj_tmp);

      tree->Draw("metNoWLepton>>hWEl_DataC_MET_tmp", cutWEl_C_MET);
      hWEl_DataC_MET->Add(hWEl_DataC_MET_tmp);

      tree->Draw("cenJetEt>>hWEl_DataC_CenJetEt_tmp", cutWEl_C_CenJetEt);
      hWEl_DataC_CenJetEt->Add(hWEl_DataC_CenJetEt_tmp);

      // Tau region
      tree->Draw("vbfDPhi>>hWTau_DataC_DPhi_tmp", cutWTau_C_DPhi);
      hWTau_DataC_DPhi->Add(hWTau_DataC_DPhi_tmp);

      tree->Draw("vbfM>>hWTau_DataC_Mjj_tmp", cutWTau_C_Mjj);
      hWTau_DataC_Mjj->Add(hWTau_DataC_Mjj_tmp);

      tree->Draw("met>>hWTau_DataC_MET_tmp", cutWTau_C_MET);
      hWTau_DataC_MET->Add(hWTau_DataC_MET_tmp);

      tree->Draw("cenJetEt>>hWTau_DataC_CenJetEt_tmp", cutWTau_C_CenJetEt);
      hWTau_DataC_CenJetEt->Add(hWTau_DataC_CenJetEt_tmp);
    
      std::cout << "  N ctrl region (dphi<1) : " << hWTau_DataC_DPhi_tmp->GetBinContent(1) << " +/- " << hWTau_DataC_DPhi_tmp->GetBinError(1) << std::endl;// debug output

    } else {
    
      std::cout << "Analysing BG MC    : " << dataset.name << std::endl;
      std::cout << "  weight : " << weight << std::endl;

      if (dataset.name.compare(0,17,"SignalM125_POWHEG")== 0 || dataset.name.compare(0,3,"QCD") == 0) continue;

      if(dataset.name.compare(0,3,"Zvv") != 0 && dataset.name != "EWK_ZvvFake"){ // Don't need Zvv in WEl estimates
        tree->Draw("vbfDPhi>>hWEl_BGC_DPhi_tmp", otherCutsTight * cutWEl_C_DPhi);
        tree->Draw("vbfM>>hWEl_BGC_Mjj_tmp", otherCutsTight * cutWEl_C_Mjj);
        tree->Draw("metNoWLepton>>hWEl_BGC_MET_tmp", otherCutsTight * cutWEl_C_MET);
        tree->Draw("cenJetEt>>hWEl_BGC_CenJetEt_tmp", otherCutsTight * cutWEl_C_CenJetEt);
        
        hWEl_BGC_DPhi_tmp->Scale(weight);
    	hWEl_BGC_Mjj_tmp->Scale(weight);
        hWEl_BGC_MET_tmp->Scale(weight);
        hWEl_BGC_CenJetEt_tmp->Scale(weight);
        
        hWEl_BGC_DPhi->Add(hWEl_BGC_DPhi_tmp);
        hWEl_BGC_Mjj->Add(hWEl_BGC_Mjj_tmp);
        hWEl_BGC_MET->Add(hWEl_BGC_MET_tmp);
        hWEl_BGC_CenJetEt->Add(hWEl_BGC_CenJetEt_tmp);
      } 

      // Count number of tau in control region in bg mc
      tree->Draw("vbfDPhi>>hWTau_BGC_DPhi_tmp", otherCutsVeto * cutWTau_C_DPhi);
      tree->Draw("vbfM>>hWTau_BGC_Mjj_tmp", otherCutsVeto * cutWTau_C_Mjj);
      tree->Draw("met>>hWTau_BGC_MET_tmp", otherCutsVeto * cutWTau_C_MET);
      tree->Draw("cenJetEt>>hWTau_BGC_CenJetEt_tmp", otherCutsVeto * cutWTau_C_CenJetEt);

      hWTau_BGC_DPhi_tmp->Scale(weight);
      hWTau_BGC_Mjj_tmp->Scale(weight);
      hWTau_BGC_MET_tmp->Scale(weight);
      hWTau_BGC_CenJetEt_tmp->Scale(weight);

      hWTau_BGC_DPhi->Add(hWTau_BGC_DPhi_tmp);
      hWTau_BGC_Mjj->Add(hWTau_BGC_Mjj_tmp);
      hWTau_BGC_MET->Add(hWTau_BGC_MET_tmp);
      hWTau_BGC_CenJetEt->Add(hWTau_BGC_CenJetEt_tmp);

      // debug output
      std::cout << "  N ctrl region (dphi<1) : " << hWTau_BGC_DPhi_tmp->GetBinContent(1) << " +/- " << hWTau_BGC_DPhi_tmp->GetBinError(1) << std::endl;
      std::cout << "  N ctrl region (MET)    : " << hWTau_BGC_MET_tmp->GetBinContent(1) << " +/- " << hWTau_BGC_MET_tmp->GetBinError(1) << std::endl;

    }

        
    // debug output
    // std::cout << "  N ctrl region (dphi<1) : " << hWEl_C_DPhi->GetBinContent(1) << " +/- " << hWEl_C_DPhi->GetBinError(1) << std::endl;
    
    delete hWEl_MCC_DPhi_tmp;
    delete hWEl_MCGen_DPhi_tmp;
    delete hWEl_DataC_DPhi_tmp;
    delete hWEl_BGC_DPhi_tmp;

    delete hWTau_MCC_DPhi_tmp;
    delete hWTau_MCGen_DPhi_tmp;
    delete hWTau_DataC_DPhi_tmp;
    delete hWTau_BGC_DPhi_tmp;

    delete hWEl_MCC_Mjj_tmp;
    delete hWEl_DataC_Mjj_tmp;
    delete hWEl_BGC_Mjj_tmp;
    
    delete hWTau_MCC_Mjj_tmp;
    delete hWTau_DataC_Mjj_tmp;
    delete hWTau_BGC_Mjj_tmp;

    delete hWEl_MCC_MET_tmp;
    delete hWEl_DataC_MET_tmp;
    delete hWEl_BGC_MET_tmp;
    
    delete hWTau_MCC_MET_tmp;
    delete hWTau_DataC_MET_tmp;
    delete hWTau_BGC_MET_tmp;

    delete hWEl_MCC_CenJetEt_tmp;
    delete hWEl_DataC_CenJetEt_tmp;
    delete hWEl_BGC_CenJetEt_tmp;
    
    delete hWTau_MCC_CenJetEt_tmp;
    delete hWTau_DataC_CenJetEt_tmp;
    delete hWTau_BGC_CenJetEt_tmp;

    file->Close();
   
  } // end of datasets loop

  //for (int i=1; i<=hWTau_BGC_DPhi->GetNbinsX(); ++i) hWTau_BGC_DPhi->SetBinError(i,0.); // no stat error from mc
  //for (int i=1; i<=hWEl_BGC_DPhi->GetNbinsX(); ++i) hWEl_BGC_DPhi->SetBinError(i,0.); // no stat error from mc

  //for (int i=1; i<=hWTau_BGC_MET->GetNbinsX(); ++i) hWTau_BGC_MET->SetBinError(i,0.); // no stat error from mc
  //for (int i=1; i<=hWEl_BGC_MET->GetNbinsX(); ++i) hWEl_BGC_MET->SetBinError(i,0.); // no stat error from mc

  //for (int i=1; i<=hWTau_BGC_CenJetEt->GetNbinsX(); ++i) hWTau_BGC_CenJetEt->SetBinError(i,0.); // no stat error from mc
  //for (int i=1; i<=hWEl_BGC_CenJetEt->GetNbinsX(); ++i) hWEl_BGC_CenJetEt->SetBinError(i,0.); // no stat error from mc

  // W->el
  TH1D* hWEl_EstC_DPhi = new TH1D("hWEl_EstC_DPhi", "", 4, dphiEdges); // n_data - n_bg in WEl control
  hWEl_EstC_DPhi->Add(hWEl_DataC_DPhi, hWEl_BGC_DPhi, 1., -1.);
  TH1D* hWEl_EstC_Mjj = new TH1D("hWEl_EstC_Mjj", "", 4, MjjEdges); // n_data - n_bg in WEl control
  hWEl_EstC_Mjj->Add(hWEl_DataC_Mjj, hWEl_BGC_Mjj, 1., -1.);
  TH1D* hWEl_EstC_MET = new TH1D("hWEl_EstC_MET", "", 4, METEdges); // n_data - n_bg in WEl control
  hWEl_EstC_MET->Add(hWEl_DataC_MET, hWEl_BGC_MET, 1., -1.);
  TH1D* hWEl_EstC_CenJetEt = new TH1D("hWEl_EstC_CenJetEt", "", 4, CenJetEtEdges); // n_data - n_bg in WEl control
  hWEl_EstC_CenJetEt->Add(hWEl_DataC_CenJetEt, hWEl_BGC_CenJetEt, 1., -1.);

  // W->Tau
  TH1D* hWTau_EstC_DPhi = new TH1D("hWTau_EstC_DPhi", "", 4, dphiEdges); // n_data - n_bg in WTau control
  hWTau_EstC_DPhi->Add(hWTau_DataC_DPhi, hWTau_BGC_DPhi, 1., -1.);
  TH1D* hWTau_EstC_Mjj = new TH1D("hWTau_EstC_Mjj", "", 4, MjjEdges); // n_data - n_bg in WTau control
  hWTau_EstC_Mjj->Add(hWTau_DataC_Mjj, hWTau_BGC_Mjj, 1., -1.);
  TH1D* hWTau_EstC_MET = new TH1D("hWTau_EstC_MET", "", 4, METEdges); // n_data - n_bg in WTau control
  hWTau_EstC_MET->Add(hWTau_DataC_MET, hWTau_BGC_MET, 1., -1.);
  TH1D* hWTau_EstC_CenJetEt = new TH1D("hWTau_EstC_CenJetEt", "", 4, CenJetEtEdges); // n_data - n_bg in WTau control
  hWTau_EstC_CenJetEt->Add(hWTau_DataC_CenJetEt, hWTau_BGC_CenJetEt, 1., -1.);

  TH1D* h_RGEN = new TH1D("h_RGEN","",1,0,1.);
  h_RGEN->Divide(hWEl_MCGen_DPhi,hWTau_MCGen_DPhi,1.,1.);

  TH1D* h_RVBF_DPhi = new TH1D("h_RVBF_DPhi","",4,dphiEdges);
  h_RVBF_DPhi->Divide(hWTau_MCC_DPhi,hWEl_MCC_DPhi,1.,1.);
  TH1D* h_RVBF_Mjj = new TH1D("h_RVBF_Mjj","",4,MjjEdges);
  h_RVBF_Mjj->Divide(hWTau_MCC_Mjj,hWEl_MCC_Mjj,1.,1.);
  TH1D* h_RVBF_MET = new TH1D("h_RVBF_MET","",4,METEdges);
  h_RVBF_MET->Divide(hWTau_MCC_MET,hWEl_MCC_MET,1.,1.);
  TH1D* h_RVBF_CenJetEt = new TH1D("h_RVBF_CenJetEt","",4,CenJetEtEdges);
  h_RVBF_CenJetEt->Divide(hWTau_MCC_CenJetEt,hWEl_MCC_CenJetEt,1.,1.);

  //for (int i=1; i<=h_RGEN->GetNbinsX(); ++i)h_RGEN->SetBinError(i,0.); // no stat error from mc
  //for (int i=1; i<=h_RVBF_DPhi->GetNbinsX(); ++i) h_RVBF_DPhi->SetBinError(i,0.); // no stat error from mc
  //for (int i=1; i<=h_RVBF_MET->GetNbinsX(); ++i) h_RVBF_MET->SetBinError(i,0.); // no stat error from mc
  //for (int i=1; i<=h_RVBF_CenJetEt->GetNbinsX(); ++i) h_RVBF_CenJetEt->SetBinError(i,0.); // no stat error from mc
  
  TH1D* h_R_DPhi     = new TH1D("h_R_DPhi","",4,dphiEdges);
  TH1D* h_R_Mjj      = new TH1D("h_R_Mjj","",4,MjjEdges);
  TH1D* h_R_MET      = new TH1D("h_R_MET","",4,METEdges);
  TH1D* h_R_CenJetEt = new TH1D("h_R_CenJetEt","",4,CenJetEtEdges);
 
  for (int ibin = 1; ibin <= h_R_DPhi->GetNbinsX(); ++ibin){
    h_R_DPhi->SetBinContent(ibin, h_RVBF_DPhi->GetBinContent(ibin) * h_RGEN->GetBinContent(1));
    h_R_DPhi->SetBinError(ibin, h_R_DPhi->GetBinContent(ibin) * sqrt(pow(h_RVBF_DPhi->GetBinError(ibin)/h_RVBF_DPhi->GetBinContent(ibin),2) + pow(h_RGEN->GetBinError(1)/ h_RGEN->GetBinContent(1),2)));
  }
  for (int ibin = 1; ibin <= h_R_Mjj->GetNbinsX(); ++ibin){
    h_R_Mjj->SetBinContent(ibin, h_RVBF_Mjj->GetBinContent(ibin) * h_RGEN->GetBinContent(1));
    h_R_Mjj->SetBinError(ibin, h_R_Mjj->GetBinContent(ibin) * sqrt(pow(h_RVBF_Mjj->GetBinError(ibin)/h_RVBF_Mjj->GetBinContent(ibin),2) + pow(h_RGEN->GetBinError(1)/ h_RGEN->GetBinContent(1),2)));
  }
  for (int ibin = 1; ibin <= h_R_MET->GetNbinsX(); ++ibin){
    h_R_MET->SetBinContent(ibin, h_RVBF_MET->GetBinContent(ibin) * h_RGEN->GetBinContent(1));
    h_R_MET->SetBinError(ibin, h_R_MET->GetBinContent(ibin) * sqrt(pow(h_RVBF_MET->GetBinError(ibin)/h_RVBF_MET->GetBinContent(ibin),2) + pow(h_RGEN->GetBinError(1)/ h_RGEN->GetBinContent(1),2)));
  }
  for (int ibin = 1; ibin <= h_R_CenJetEt->GetNbinsX(); ++ibin){
    h_R_CenJetEt->SetBinContent(ibin, h_RVBF_CenJetEt->GetBinContent(ibin) * h_RGEN->GetBinContent(1));
    h_R_CenJetEt->SetBinError(ibin, h_R_CenJetEt->GetBinContent(ibin) * sqrt(pow(h_RVBF_CenJetEt->GetBinError(ibin)/h_RVBF_CenJetEt->GetBinContent(ibin),2) + pow(h_RGEN->GetBinError(1)/ h_RGEN->GetBinContent(1),2)));
  }

  // lets calculate some stuff
  TH1D* hWTau_Prediction_DPhi = new TH1D("hWTau_Prediction_DPhi","",4,dphiEdges);
  hWTau_Prediction_DPhi->Add(hWEl_EstC_DPhi,1.);
  hWTau_Prediction_DPhi->Multiply(h_R_DPhi);

  TH1D* hWTau_Prediction_Mjj = new TH1D("hWTau_Prediction_Mjj","",4,MjjEdges);
  hWTau_Prediction_Mjj->Add(hWEl_EstC_Mjj,1.);
  hWTau_Prediction_Mjj->Multiply(h_R_Mjj);

  TH1D* hWTau_Prediction_MET = new TH1D("hWTau_Prediction_MET","",4,METEdges);
  hWTau_Prediction_MET->Add(hWEl_EstC_MET,1.);
  hWTau_Prediction_MET->Multiply(h_R_MET);

  TH1D* hWTau_Prediction_CenJetEt = new TH1D("hWTau_Prediction_CenJetEt","",4,CenJetEtEdges);
  hWTau_Prediction_CenJetEt->Add(hWEl_EstC_CenJetEt,1.);
  hWTau_Prediction_CenJetEt->Multiply(h_R_CenJetEt);

  double xsecRatio = (0.1125*0.6476)/(0.1057+(0.1125*0.1741)); // Ratio of w->Tau cross-section to w-?el cross section (includes w->el and w->tau->el)
  hWTau_Prediction_DPhi->Scale(xsecRatio);
  hWTau_Prediction_Mjj->Scale(xsecRatio);
  hWTau_Prediction_MET->Scale(xsecRatio);
  hWTau_Prediction_CenJetEt->Scale(xsecRatio);

  ///////////////////////////
  // Lots of screen output //
  ///////////////////////////
  std::cout << std::endl;
  std::cout << "***************** W Tau closure test from W El *****************" << std::endl;
  std::cout << std::endl;
  std::cout << "--- Binned by DPhiJJ ---" << std::endl;
  for (int a = 0; a < 4; a++){
    std::cout << std::endl;
    std::cout << "Bin: " << dphiEdges[a] << " < dphi < " << dphiEdges[a+1] << "" << std::endl;
    std::cout << "W->el channel" << std::endl;
    std::cout << "  W+jets reco'd in ctrl                                  : " << hWEl_MCC_DPhi->GetBinContent(a+1) << " +/- " << hWEl_MCC_DPhi->GetBinError(a+1) << std::endl;
    std::cout << "  W+jets at gen level                                    : " << hWEl_MCGen_DPhi->GetBinContent(a+1) << " +/- " << hWEl_MCGen_DPhi->GetBinError(a+1) << std::endl;
    std::cout << "  Background ctrl region                                 : " << hWEl_BGC_DPhi->GetBinContent(a+1) << " +/- " << hWEl_BGC_DPhi->GetBinError(a+1) << std::endl;
    std::cout << "  Data ctrl region                                       : " << hWEl_DataC_DPhi->GetBinContent(a+1) << " +/- " << hWEl_DataC_DPhi->GetBinError(a+1) << std::endl;
    std::cout << "  Data-bg ctrl region                                    : " << hWEl_EstC_DPhi->GetBinContent(a+1) << " +/- " << hWEl_EstC_DPhi->GetBinError(a+1) << std::endl;
    std::cout << std::endl;
    std::cout << "W->tau channel" << std::endl;
    std::cout << "  W+jets reco'd in ctrl (MC estimate in ctrl)            : " << hWTau_MCC_DPhi->GetBinContent(a+1) << " +/- " << hWTau_MCC_DPhi->GetBinError(a+1) << std::endl;
    std::cout << "  W+jets at gen level                                    : " << hWTau_MCGen_DPhi->GetBinContent(a+1) << " +/- " << hWTau_MCGen_DPhi->GetBinError(a+1) << std::endl;
    std::cout << "  Background ctrl region                                 : " << hWTau_BGC_DPhi->GetBinContent(a+1) << " +/- " << hWTau_BGC_DPhi->GetBinError(a+1) << std::endl;
    std::cout << "  Data ctrl region                                       : " << hWTau_DataC_DPhi->GetBinContent(a+1) << " +/- " << hWTau_DataC_DPhi->GetBinError(a+1) << std::endl;
    std::cout << std::endl;
    std::cout << "  WTau in ctrl region observed                           : " << hWTau_EstC_DPhi->GetBinContent(a+1) << " +/- " << hWTau_EstC_DPhi->GetBinError(a+1) << std::endl;
    std::cout << std::endl;
    std::cout << "  Closure test: WTau from WEl                            : " << hWTau_Prediction_DPhi->GetBinContent(a+1) << " +/- " << hWTau_Prediction_DPhi->GetBinError(a+1) << std::endl;
    std::cout << std::endl;
  }

  std::cout << std::endl;
  std::cout << "--- Binned by Mjj ---" << std::endl;
  for (int b = 0; b < 4; b++) {
    std::cout << std::endl;
    std::cout << "Bin: " << MjjEdges[b] << " < Mjj < " << MjjEdges[b+1] << "" << std::endl;
    std::cout << "W->el channel" << std::endl;
    std::cout << "  W+jets reco'd in ctrl                                  : " << hWEl_MCC_Mjj->GetBinContent(b+1) << " +/- " << hWEl_MCC_Mjj->GetBinError(b+1) << std::endl;
    std::cout << "  Background ctrl region                                 : " << hWEl_BGC_Mjj->GetBinContent(b+1) << " +/- " << hWEl_BGC_Mjj->GetBinError(b+1) << std::endl;
    std::cout << "  Data ctrl region                                       : " << hWEl_DataC_Mjj->GetBinContent(b+1) << " +/- " << hWEl_DataC_Mjj->GetBinError(b+1) << std::endl;
    std::cout << "  Data-bg ctrl region                                    : " << hWEl_EstC_Mjj->GetBinContent(b+1) << " +/- " << hWEl_EstC_Mjj->GetBinError(b+1) << std::endl;
    std::cout << std::endl;
    std::cout << "W->tau channel" << std::endl;
    std::cout << "  W+jets reco'd in ctrl (MC estimate in ctrl)            : " << hWTau_MCC_Mjj->GetBinContent(b+1) << " +/- " << hWTau_MCC_Mjj->GetBinError(b+1) << std::endl;
    std::cout << "  Background ctrl region                                 : " << hWTau_BGC_Mjj->GetBinContent(b+1) << " +/- " << hWTau_BGC_Mjj->GetBinError(b+1) << std::endl;
    std::cout << "  Data ctrl region                                       : " << hWTau_DataC_Mjj->GetBinContent(b+1) << " +/- " << hWTau_DataC_Mjj->GetBinError(b+1) << std::endl;
    std::cout << std::endl;
    std::cout << "  WTau in ctrl region observed                           : " << hWTau_EstC_Mjj->GetBinContent(b+1) << " +/- " << hWTau_EstC_Mjj->GetBinError(b+1) << std::endl;
    std::cout << std::endl;
    std::cout << "  Closure test: WTau from WEl                            : " << hWTau_Prediction_Mjj->GetBinContent(b+1) << " +/- " << hWTau_Prediction_Mjj->GetBinError(b+1) << std::endl;
    std::cout << std::endl;
  }

  std::cout << std::endl;
  std::cout << "--- Binned by MET ---" << std::endl;
  for (int b = 0; b < 4; b++) {
    std::cout << std::endl;
    std::cout << "Bin: " << METEdges[b] << " < MET < " << METEdges[b+1] << "" << std::endl;
    std::cout << "W->el channel" << std::endl;
    std::cout << "  W+jets reco'd in ctrl                                  : " << hWEl_MCC_MET->GetBinContent(b+1) << " +/- " << hWEl_MCC_MET->GetBinError(b+1) << std::endl;
    std::cout << "  Background ctrl region                                 : " << hWEl_BGC_MET->GetBinContent(b+1) << " +/- " << hWEl_BGC_MET->GetBinError(b+1) << std::endl;
    std::cout << "  Data ctrl region                                       : " << hWEl_DataC_MET->GetBinContent(b+1) << " +/- " << hWEl_DataC_MET->GetBinError(b+1) << std::endl;
    std::cout << "  Data-bg ctrl region                                    : " << hWEl_EstC_MET->GetBinContent(b+1) << " +/- " << hWEl_EstC_MET->GetBinError(b+1) << std::endl;
    std::cout << std::endl;
    std::cout << "W->tau channel" << std::endl;
    std::cout << "  W+jets reco'd in ctrl (MC estimate in ctrl)            : " << hWTau_MCC_MET->GetBinContent(b+1) << " +/- " << hWTau_MCC_MET->GetBinError(b+1) << std::endl;
    std::cout << "  Background ctrl region                                 : " << hWTau_BGC_MET->GetBinContent(b+1) << " +/- " << hWTau_BGC_MET->GetBinError(b+1) << std::endl;
    std::cout << "  Data ctrl region                                       : " << hWTau_DataC_MET->GetBinContent(b+1) << " +/- " << hWTau_DataC_MET->GetBinError(b+1) << std::endl;
    std::cout << std::endl;
    std::cout << "  WTau in ctrl region observed                           : " << hWTau_EstC_MET->GetBinContent(b+1) << " +/- " << hWTau_EstC_MET->GetBinError(b+1) << std::endl;
    std::cout << std::endl;
    std::cout << "  Closure test: WTau from WEl                            : " << hWTau_Prediction_MET->GetBinContent(b+1) << " +/- " << hWTau_Prediction_MET->GetBinError(b+1) << std::endl;
    std::cout << std::endl;
  }

  std::cout << std::endl;
  std::cout << "--- Binned by CenJetEt ---" << std::endl;
  for (int c = 0; c<4; c++){
    std::cout << std::endl;
    std::cout << "Bin: " << CenJetEtEdges[c] << " < Central Jet Et < " << CenJetEtEdges[c+1] << "" << std::endl;
    std::cout << "W->el channel" << std::endl;
    std::cout << "  W+jets reco'd in ctrl                                  : " << hWEl_MCC_CenJetEt->GetBinContent(c+1) << " +/- " << hWEl_MCC_CenJetEt->GetBinError(c+1) << std::endl;
    std::cout << "  Background ctrl region                                 : " << hWEl_BGC_CenJetEt->GetBinContent(c+1) << " +/- " << hWEl_BGC_CenJetEt->GetBinError(c+1) << std::endl;
    std::cout << "  Data ctrl region                                       : " << hWEl_DataC_CenJetEt->GetBinContent(c+1) << " +/- " << hWEl_DataC_CenJetEt->GetBinError(c+1) << std::endl;
    std::cout << "  Data-bg ctrl region                                    : " << hWEl_EstC_CenJetEt->GetBinContent(c+1) << " +/- " << hWEl_EstC_CenJetEt->GetBinError(c+1) << std::endl;
    std::cout << std::endl;
    std::cout << "W->tau channel" << std::endl;
    std::cout << "  W+jets reco'd in ctrl (MC estimate in ctrl)            : " << hWTau_MCC_CenJetEt->GetBinContent(c+1) << " +/- " << hWTau_MCC_CenJetEt->GetBinError(c+1) << std::endl;
    std::cout << "  Background ctrl region                                 : " << hWTau_BGC_CenJetEt->GetBinContent(c+1) << " +/- " << hWTau_BGC_CenJetEt->GetBinError(c+1) << std::endl;
    std::cout << "  Data ctrl region                                       : " << hWTau_DataC_CenJetEt->GetBinContent(c+1) << " +/- " << hWTau_DataC_CenJetEt->GetBinError(c+1) << std::endl;
    std::cout << std::endl;
    std::cout << "  WTau in ctrl region observed                           : " << hWTau_EstC_CenJetEt->GetBinContent(c+1) << " +/- " << hWTau_EstC_CenJetEt->GetBinError(c+1) << std::endl;
    std::cout << std::endl;
    std::cout << "  Closure test: WTau from WEl                            : " << hWTau_Prediction_CenJetEt->GetBinContent(c+1) << " +/- " << hWTau_Prediction_CenJetEt->GetBinError(c+1) << std::endl;
    std::cout << std::endl;
  }

  ////////////////////////
  // draw closure plots //
  ////////////////////////
  std::string pdfName;

  gStyle->SetOptStat(0);
  gStyle->SetOptFit(111111111);

  double x_dPhi[4]  = {0.5, 1.4, 2.2, 2.6 + (TMath::Pi()-2.6)/2};
  double ex_dPhi[4] = {0.5, 0.4, 0.4, (TMath::Pi()-2.6)/2};
  double y_dPhi1[4],ey_dPhi1[4],y_dPhi2[4],ey_dPhi2[4],y_dPhi3[4],ey_dPhi3[4]; // For raw numbers - prediceted (data), observed, predicted (MC)
  double frac_dPhi[4],efrac_dPhi[4]; // For Frac diff

  double x_Mjj[4]   = {900., 1100., 1500., 2400.};
  double ex_Mjj[4]  = {100, 100, 300, 600};
  double y_Mjj1[4],ey_Mjj1[4],y_Mjj2[4],ey_Mjj2[4],y_Mjj3[4],ey_Mjj3[4]; // For raw numbers - prediceted (data), observed, predicted (MC)
  double frac_Mjj[4],efrac_Mjj[4]; // For Frac diff

  double x_MET[4]   = {125., 175., 225., 275.};
  double ex_MET[4]  = {25, 25, 25, 25};
  double y_MET1[4],ey_MET1[4],y_MET2[4],ey_MET2[4],y_MET3[4],ey_MET3[4]; // For raw numbers - prediceted (data), observed, predicted (MC)
  double frac_MET[4],efrac_MET[4]; // For Frac diff

  //double x_CenJetEt[4]  = {25, 55, 95, 160};
  //double ex_CenJetEt[4] = {15, 15, 25, 40};
  double x_CenJetEt[4]   = {20., 45., 80., 125.};
  double ex_CenJetEt[4]  = {10, 15, 20, 25};  
  double y_CenJetEt1[4],ey_CenJetEt1[4],y_CenJetEt2[4],ey_CenJetEt2[4],y_CenJetEt3[4],ey_CenJetEt3[4]; // For raw numbers - prediceted (data), observed, predicted (MC)
  double frac_CenJetEt[4],efrac_CenJetEt[4]; // For Frac diff

  double y_syst[4],e_syst[4]; // for systematic bands

  for(int i=0; i<4; ++i) {
        y_syst[i] = 0.;
        //e_syst[i] = sqrt( pow(0.2,2) + pow(constants::syst_WTau,2) ); // 20% from MC stats, plus whatever the current value of Tau syst from TAU ID, w-e contamination, JER etc
	e_syst[i] = constants::syst_WTau;

        y_dPhi1[i]  = hWTau_Prediction_DPhi->GetBinContent(i+1);  //Predicted WTau
        ey_dPhi1[i] = hWTau_Prediction_DPhi->GetBinError(i+1);
        y_dPhi2[i]  = hWTau_EstC_DPhi->GetBinContent(i+1);        //Observed WTau
        ey_dPhi2[i] = hWTau_EstC_DPhi->GetBinError(i+1);  
        y_dPhi3[i]  = hWTau_MCC_DPhi->GetBinContent(i+1);         //MC prediction
        ey_dPhi3[i] = hWTau_MCC_DPhi->GetBinError(i+1);
        frac_dPhi[i]  = (y_dPhi1[i]/y_dPhi2[i])-1.;
        efrac_dPhi[i] = (y_dPhi1[i]/y_dPhi2[i])*sqrt(pow(ey_dPhi1[i]/y_dPhi1[i],2) + pow(ey_dPhi2[i]/y_dPhi2[i],2));

        y_Mjj1[i]  = hWTau_Prediction_Mjj->GetBinContent(i+1);  //Predicted WTau
        ey_Mjj1[i] = hWTau_Prediction_Mjj->GetBinError(i+1);
        y_Mjj2[i]  = hWTau_EstC_Mjj->GetBinContent(i+1);        //Observed WTau
        ey_Mjj2[i] = hWTau_EstC_Mjj->GetBinError(i+1);  
        y_Mjj3[i]  = hWTau_MCC_Mjj->GetBinContent(i+1);         //MC prediction
        ey_Mjj3[i] = hWTau_MCC_Mjj->GetBinError(i+1);
        frac_Mjj[i]  = (y_Mjj1[i]/y_Mjj2[i])-1.;
        efrac_Mjj[i] = (y_Mjj1[i]/y_Mjj2[i])*sqrt(pow(ey_Mjj1[i]/y_Mjj1[i],2) + pow(ey_Mjj2[i]/y_Mjj2[i],2));

        y_MET1[i]  = hWTau_Prediction_MET->GetBinContent(i+1);  //Predicted WTau
        ey_MET1[i] = hWTau_Prediction_MET->GetBinError(i+1);
        y_MET2[i]  = hWTau_EstC_MET->GetBinContent(i+1);        //Observed WTau
        ey_MET2[i] = hWTau_EstC_MET->GetBinError(i+1);  
        y_MET3[i]  = hWTau_MCC_MET->GetBinContent(i+1);         //MC prediction
        ey_MET3[i] = hWTau_MCC_MET->GetBinError(i+1);
        frac_MET[i]  = (y_MET1[i]/y_MET2[i])-1.;
        efrac_MET[i] = (y_MET1[i]/y_MET2[i])*sqrt(pow(ey_MET1[i]/y_MET1[i],2) + pow(ey_MET2[i]/y_MET2[i],2));

        y_CenJetEt1[i]  = hWTau_Prediction_CenJetEt->GetBinContent(i+1);  //Predicted WTau
        ey_CenJetEt1[i] = hWTau_Prediction_CenJetEt->GetBinError(i+1);
        y_CenJetEt2[i]  = hWTau_EstC_CenJetEt->GetBinContent(i+1);        //Observed WTau
        ey_CenJetEt2[i] = hWTau_EstC_CenJetEt->GetBinError(i+1);  
        y_CenJetEt3[i]  = hWTau_MCC_CenJetEt->GetBinContent(i+1);         //MC prediction
        ey_CenJetEt3[i] = hWTau_MCC_CenJetEt->GetBinError(i+1);
        frac_CenJetEt[i]  = (y_CenJetEt1[i]/y_CenJetEt2[i])-1.;
        efrac_CenJetEt[i] = (y_CenJetEt1[i]/y_CenJetEt2[i])*sqrt(pow(ey_CenJetEt1[i]/y_CenJetEt1[i],2) + pow(ey_CenJetEt2[i]/y_CenJetEt2[i],2));

  }

  TGraphErrors *gp_dPhi1 = new TGraphErrors(4,x_dPhi,y_dPhi1,ex_dPhi,ey_dPhi1);
  TGraphErrors *gp_dPhi2 = new TGraphErrors(4,x_dPhi,y_dPhi2,ex_dPhi,ey_dPhi2);
  TGraphErrors *gp_dPhi3 = new TGraphErrors(4,x_dPhi,y_dPhi3,ex_dPhi,ey_dPhi3);
  TGraphErrors *gp_dPhiF = new TGraphErrors(4,x_dPhi,frac_dPhi,ex_dPhi,efrac_dPhi);
  TGraphErrors *gp_dPhiS = new TGraphErrors(4,x_dPhi,y_syst,ex_dPhi,e_syst); // systematic error bands

  TGraphErrors *gp_Mjj1 = new TGraphErrors(4,x_Mjj,y_Mjj1,ex_Mjj,ey_Mjj1);
  TGraphErrors *gp_Mjj2 = new TGraphErrors(4,x_Mjj,y_Mjj2,ex_Mjj,ey_Mjj2);
  TGraphErrors *gp_Mjj3 = new TGraphErrors(4,x_Mjj,y_Mjj3,ex_Mjj,ey_Mjj3);
  TGraphErrors *gp_MjjF = new TGraphErrors(4,x_Mjj,frac_Mjj,ex_Mjj,efrac_Mjj);
  TGraphErrors *gp_MjjS = new TGraphErrors(4,x_Mjj,y_syst,ex_Mjj,e_syst); // systematic error bands

  TGraphErrors *gp_MET1 = new TGraphErrors(4,x_MET,y_MET1,ex_MET,ey_MET1);
  TGraphErrors *gp_MET2 = new TGraphErrors(4,x_MET,y_MET2,ex_MET,ey_MET2);
  TGraphErrors *gp_MET3 = new TGraphErrors(4,x_MET,y_MET3,ex_MET,ey_MET3);
  TGraphErrors *gp_METF = new TGraphErrors(4,x_MET,frac_MET,ex_MET,efrac_MET);
  TGraphErrors *gp_METS = new TGraphErrors(4,x_MET,y_syst,ex_MET,e_syst); // systematic error bands

  TGraphErrors *gp_CenJetEt1 = new TGraphErrors(4,x_CenJetEt,y_CenJetEt1,ex_CenJetEt,ey_CenJetEt1);
  TGraphErrors *gp_CenJetEt2 = new TGraphErrors(4,x_CenJetEt,y_CenJetEt2,ex_CenJetEt,ey_CenJetEt2);
  TGraphErrors *gp_CenJetEt3 = new TGraphErrors(4,x_CenJetEt,y_CenJetEt3,ex_CenJetEt,ey_CenJetEt3);
  TGraphErrors *gp_CenJetEtF = new TGraphErrors(4,x_CenJetEt,frac_CenJetEt,ex_CenJetEt,efrac_CenJetEt);
  TGraphErrors *gp_CenJetEtS = new TGraphErrors(4,x_CenJetEt,y_syst,ex_CenJetEt,e_syst); // systematic error bands

  TH1D *h_dPhi     = new TH1D("h_dPhi", "", 1, 0, TMath::Pi()); // For axes
  TH1D *h_Mjj      = new TH1D("h_Mjj", "", 1, 800, 3000); // For axes
  TH1D *h_MET      = new TH1D("h_MET", "", 1, 100, 300); // For axes
  TH1D *h_CenJetEt = new TH1D("h_CenJetEt", "", 1, 10, 150); // For axes

  TPaveText *cms = new TPaveText(0.12, 0.68, 0.58 , 0.88, "NDC");
  cms->SetFillColor(0);
  cms->SetFillStyle(4000);
  cms->SetBorderSize(0);
  cms->SetLineColor(0);
  cms->SetTextAlign(12);
  cms->AddText("CMS Preliminary");
  cms->AddText("");
  cms->AddText("#sqrt{s} = 8 TeV L = 19.6 fb^{-1}");
  cms->AddText("");

  TCanvas canvas; 
  canvas.SetCanvasSize(canvas.GetWindowWidth(), 1.2*canvas.GetWindowHeight());

  ///////////////////
  // DPhiJJ graphs //
  ///////////////////
  // Absolute numbers
  gp_dPhi1->SetTitle("");
  gp_dPhi1->SetMarkerStyle(20);
  gp_dPhi1->SetMarkerSize(0.9);
  gp_dPhi1->SetLineColor(kRed);
  gp_dPhi1->SetMarkerColor(kRed);
  gp_dPhi1->GetXaxis()->SetTitle("#Delta #phi_{jj}");
  gp_dPhi1->GetXaxis()->SetRangeUser(0,TMath::Pi());
  gp_dPhi1->GetYaxis()->SetTitle("N(W#rightarrow #tau#nu)");
  gp_dPhi1->GetYaxis()->SetTitleOffset(1.2);
  gp_dPhi1->GetYaxis()->SetRangeUser(0,80);
  gp_dPhi1->Draw("AP");
  gp_dPhi2->SetMarkerStyle(20);
  gp_dPhi2->SetMarkerSize(0.9);
  gp_dPhi2->SetLineColor(kBlue);
  gp_dPhi2->SetMarkerColor(kBlue);
  gp_dPhi2->Draw("P same");
  gp_dPhi3->SetMarkerStyle(20);
  gp_dPhi3->SetMarkerSize(0.9);
  gp_dPhi3->SetLineColor(kViolet);
  gp_dPhi3->SetMarkerColor(kViolet);
  gp_dPhi3->Draw("P same");

  TLegend leg(0.12,0.67,0.32,0.87);
  leg.SetBorderSize(0);
  leg.SetFillColor(0);
  leg.AddEntry(gp_dPhi1,"predicted (data)","P");
  leg.AddEntry(gp_dPhi2,"observed (data)","P");
  leg.AddEntry(gp_dPhi3,"predicted (MC)","P");
  leg.Draw();

  pdfName= options.oDir + std::string("/Wtaunu_dphi_num.pdf");
  canvas.Print(pdfName.c_str());

   // Fractional diff
  h_dPhi->Draw();
  h_dPhi->GetXaxis()->SetTitle("#Delta #phi_{jj}");
  h_dPhi->GetYaxis()->SetTitle("#frac{Predicted - Observed}{Observed}");
  h_dPhi->GetYaxis()->SetTitleOffset(1.2);
  h_dPhi->GetYaxis()->SetRangeUser(-1.0,2.0);
  h_dPhi->SetLineColor(kBlue);
  h_dPhi->SetLineWidth(2);
  h_dPhi->Draw();
  gp_dPhiS->SetLineColor(kGray);
  gp_dPhiS->SetLineWidth(0);
  gp_dPhiS->SetFillColor(kGray);
  gp_dPhiS->SetFillStyle(3001);
  gp_dPhiF->SetMarkerStyle(20);
  gp_dPhiF->SetMarkerSize(1.4);
  gp_dPhiF->SetMarkerColor(kBlue);
  gp_dPhiF->SetLineWidth(2);
  TF1 *f_dPhi = new TF1("f_dPhi","pol0",0,2.6); //To do a fit in first 3 bins only
  gp_dPhiF->Fit("f_dPhi","R");
  h_dPhi->Draw();
  gp_dPhiS->Draw("2 same");
  gp_dPhiF->Draw("P same");

  TLegend leg2_dphi(0.12,0.67,0.40,0.87);
  leg2_dphi.SetBorderSize(0);
  leg2_dphi.SetFillColor(0);
  leg2_dphi.AddEntry(f_dPhi,"pol0 fit (0 < #Delta #phi_{jj} < 2.6)","l");
  leg2_dphi.AddEntry(gp_dPhiS,"Systematic error","f");
  leg2_dphi.Draw();
  pdfName= options.oDir + std::string("/Wtaunu_dphi_frac.pdf");
  canvas.Print(pdfName.c_str());

  ////////////////
  // Mjj graphs //
  ////////////////
  gp_Mjj1->SetTitle("");
  gp_Mjj1->SetMarkerStyle(20);
  gp_Mjj1->SetMarkerSize(0.9);
  gp_Mjj1->SetLineColor(kRed);
  gp_Mjj1->SetMarkerColor(kRed);
  gp_Mjj1->GetXaxis()->SetTitle("M_{jj} [GeV]");
  gp_Mjj1->GetXaxis()->SetRangeUser(800.,3000.);
  gp_Mjj1->GetYaxis()->SetTitle("N(W#rightarrow #tau#nu)");
  gp_Mjj1->GetYaxis()->SetTitleOffset(1.2);
  gp_Mjj1->GetYaxis()->SetRangeUser(0.,50.);
  gp_Mjj1->Draw("AP");
  gp_Mjj2->SetMarkerStyle(20);
  gp_Mjj2->SetMarkerSize(0.9);
  gp_Mjj2->SetLineColor(kBlue);
  gp_Mjj2->SetMarkerColor(kBlue);
  gp_Mjj2->Draw("P same");
  gp_Mjj3->SetMarkerStyle(20);
  gp_Mjj3->SetMarkerSize(0.9);
  gp_Mjj3->SetLineColor(kViolet);
  gp_Mjj3->SetMarkerColor(kViolet);
  gp_Mjj3->Draw("P same");

  leg.Draw();

  pdfName= options.oDir + std::string("/Wtaunu_mjj_num.pdf");
  canvas.Print(pdfName.c_str());
 
  h_Mjj->Draw();
  h_Mjj->GetXaxis()->SetTitle("M_{jj} [GeV]");
  h_Mjj->GetYaxis()->SetTitle("#frac{Predicted - Observed}{Observed}");
  h_Mjj->GetYaxis()->SetTitleOffset(1.2);
  h_Mjj->GetYaxis()->SetRangeUser(-3.0,10.0);
  h_Mjj->SetLineColor(kBlue);
  h_Mjj->SetLineWidth(2);
  h_Mjj->Draw();
  gp_MjjS->SetLineColor(kGray);
  gp_MjjS->SetLineWidth(0);
  gp_MjjS->SetFillColor(kGray);
  gp_MjjS->SetFillStyle(3001);
  gp_MjjF->SetMarkerStyle(20);
  gp_MjjF->SetMarkerSize(1.4);
  gp_MjjF->SetMarkerColor(kBlue);
  gp_MjjF->SetLineWidth(2);
  TF1 *f_Mjj = new TF1("f_Mjj","pol0",800,3000);
  gp_MjjF->Fit("f_Mjj","R");
  h_Mjj->Draw();
  gp_MjjS->Draw("2 same");
  gp_MjjF->Draw("P same");

  TLegend leg2_mjj(0.12,0.67,0.40,0.87);
  leg2_mjj.SetBorderSize(0);
  leg2_mjj.SetFillColor(0);
  leg2_mjj.AddEntry(f_Mjj,"pol0 fit","l");
  leg2_mjj.AddEntry(gp_MjjS,"Systematic error","f");
  leg2_mjj.Draw();
  pdfName= options.oDir + std::string("/Wtaunu_mjj_frac.pdf");
  canvas.Print(pdfName.c_str());

  ////////////////
  // MET graphs //
  ////////////////
  gp_MET1->SetTitle("");
  gp_MET1->SetMarkerStyle(20);
  gp_MET1->SetMarkerSize(0.9);
  gp_MET1->SetLineColor(kRed);
  gp_MET1->SetMarkerColor(kRed);
  gp_MET1->GetXaxis()->SetTitle("E_{T}^{miss} [GeV]");
  gp_MET1->GetXaxis()->SetRangeUser(100,300);
  gp_MET1->GetYaxis()->SetTitle("N(W#rightarrow #tau#nu)");
  gp_MET1->GetYaxis()->SetTitleOffset(1.2);
  gp_MET1->GetYaxis()->SetRangeUser(0,40);
  gp_MET1->Draw("AP");
  gp_MET2->SetMarkerStyle(20);
  gp_MET2->SetMarkerSize(0.9);
  gp_MET2->SetLineColor(kBlue);
  gp_MET2->SetMarkerColor(kBlue);
  gp_MET2->Draw("P same");
  gp_MET3->SetMarkerStyle(20);
  gp_MET3->SetMarkerSize(0.9);
  gp_MET3->SetLineColor(kViolet);
  gp_MET3->SetMarkerColor(kViolet);
  gp_MET3->Draw("P same");

  leg.Draw();

  pdfName= options.oDir + std::string("/Wtaunu_met_num.pdf");
  canvas.Print(pdfName.c_str());
 
  h_MET->Draw();
  h_MET->GetXaxis()->SetTitle("E_{T}^{miss} [GeV]");
  h_MET->GetYaxis()->SetTitle("#frac{Predicted - Observed}{Observed}");
  h_MET->GetYaxis()->SetTitleOffset(1.2);
  h_MET->GetYaxis()->SetRangeUser(-3.0,5.0);
  h_MET->SetLineColor(kBlue);
  h_MET->SetLineWidth(2);
  h_MET->Draw();
  gp_METS->SetLineColor(kGray);
  gp_METS->SetLineWidth(0);
  gp_METS->SetFillColor(kGray);
  gp_METS->SetFillStyle(3001);
  gp_METF->SetMarkerStyle(20);
  gp_METF->SetMarkerSize(1.4);
  gp_METF->SetMarkerColor(kBlue);
  gp_METF->SetLineWidth(2);
  TF1 *f_MET = new TF1("f_MET","pol0",100,300);
  gp_METF->Fit("f_MET","R");
  h_MET->Draw();
  gp_METS->Draw("2 same");
  gp_METF->Draw("P same");

  TLegend leg2_met(0.12,0.67,0.40,0.87);
  leg2_met.SetBorderSize(0);
  leg2_met.SetFillColor(0);
  leg2_met.AddEntry(f_MET,"pol0 fit","l");
  leg2_met.AddEntry(gp_METS,"Systematic error","f");
  leg2_met.Draw();
  pdfName= options.oDir + std::string("/Wtaunu_met_frac.pdf");
  canvas.Print(pdfName.c_str());

  /////////////////////
  // CenJetEt graphs //
  /////////////////////
  gp_CenJetEt1->SetTitle("");
  gp_CenJetEt1->SetMarkerStyle(20);
  gp_CenJetEt1->SetMarkerSize(0.9);
  gp_CenJetEt1->SetLineColor(kRed);
  gp_CenJetEt1->SetMarkerColor(kRed);
  gp_CenJetEt1->GetXaxis()->SetTitle("Central Jet E_{T} [Gev]");
  gp_CenJetEt1->GetXaxis()->SetRangeUser(10,150);
  gp_CenJetEt1->GetYaxis()->SetTitle("N(W#rightarrow #tau#nu)");
  gp_CenJetEt1->GetYaxis()->SetTitleOffset(1.2);
  gp_CenJetEt1->GetYaxis()->SetRangeUser(0,40);
  gp_CenJetEt1->Draw("AP");
  gp_CenJetEt2->SetMarkerStyle(20);
  gp_CenJetEt2->SetMarkerSize(0.9);
  gp_CenJetEt2->SetLineColor(kBlue);
  gp_CenJetEt2->SetMarkerColor(kBlue);
  gp_CenJetEt2->Draw("P same");
  gp_CenJetEt3->SetMarkerStyle(20);
  gp_CenJetEt3->SetMarkerSize(0.9);
  gp_CenJetEt3->SetLineColor(kViolet);
  gp_CenJetEt3->SetMarkerColor(kViolet);
  gp_CenJetEt3->Draw("P same");

  leg.Draw();

  pdfName= options.oDir + std::string("/Wtaunu_CenJetEt_num.pdf");
  canvas.Print(pdfName.c_str());
 
  h_CenJetEt->Draw();
  h_CenJetEt->GetXaxis()->SetTitle("Central Jet E_{T} [Gev]");
  h_CenJetEt->GetYaxis()->SetTitle("#frac{Predicted - Observed}{Observed}");
  h_CenJetEt->GetYaxis()->SetTitleOffset(1.2);
  h_CenJetEt->GetYaxis()->SetRangeUser(-3.0,5.0);
  h_CenJetEt->SetLineColor(kBlue);
  h_CenJetEt->SetLineWidth(2);
  h_CenJetEt->Draw();
  gp_CenJetEtS->SetLineColor(kGray);
  gp_CenJetEtS->SetLineWidth(0);
  gp_CenJetEtS->SetFillColor(kGray);
  gp_CenJetEtS->SetFillStyle(3001);
  gp_CenJetEtF->SetMarkerStyle(20);
  gp_CenJetEtF->SetMarkerSize(1.4);
  gp_CenJetEtF->SetMarkerColor(kBlue);
  gp_CenJetEtF->SetLineWidth(2);
  TF1 *f_CenJetEt = new TF1("f_CenJetEt","pol0",10,150); 
  gp_CenJetEtF->Fit("f_CenJetEt","R");
  h_CenJetEt->Draw();
  gp_CenJetEtS->Draw("2 same");
  gp_CenJetEtF->Draw("P same");

  TLegend leg2_cenjet(0.12,0.67,0.40,0.87);
  leg2_cenjet.SetBorderSize(0);
  leg2_cenjet.SetFillColor(0);
  leg2_cenjet.AddEntry(f_CenJetEt,"pol0 fit","l");
  leg2_cenjet.AddEntry(gp_CenJetEtS,"Systematic error","f");
  leg2_cenjet.Draw();

  pdfName= options.oDir + std::string("/Wtaunu_CenJetEt_frac.pdf");
  canvas.Print(pdfName.c_str());


}
示例#5
0
int electronCompare()
 {
  TString CMP_DIR          = gSystem->Getenv( "CMP_DIR"          ) ;
  TString CMP_URL          = gSystem->Getenv( "CMP_URL"          ) ;
  TString CMP_TITLE        = gSystem->Getenv( "CMP_TITLE"        ) ;
  TString CMP_RED_FILE     = gSystem->Getenv( "CMP_RED_FILE"     ) ;
  TString CMP_BLUE_FILE    = gSystem->Getenv( "CMP_BLUE_FILE"    ) ;
  TString CMP_RED_NAME     = gSystem->Getenv( "CMP_RED_NAME"     ) ;
  TString CMP_BLUE_NAME    = gSystem->Getenv( "CMP_BLUE_NAME"    ) ;
  TString CMP_RED_COMMENT  = gSystem->Getenv( "CMP_RED_COMMENT"  ) ;
  TString CMP_BLUE_COMMENT = gSystem->Getenv( "CMP_BLUE_COMMENT" ) ;
  TString CMP_CONFIG       = gSystem->Getenv( "CMP_CONFIG"       ) ;
  TString CMP_RED_RELEASE  = gSystem->Getenv( "CMP_RED_RELEASE"  ) ;
  TString CMP_BLUE_RELEASE = gSystem->Getenv( "CMP_BLUE_RELEASE" ) ;

//-----
// AC
//  std::cout << "red_file : C : " << CMP_RED_FILE << std::endl;
//  std::cout << "blue_file : C : " << CMP_BLUE_FILE << std::endl;
  std::cout << "red_release : C : " << CMP_RED_RELEASE << std::endl;
  std::cout << "blue_release : C : " << CMP_BLUE_RELEASE << std::endl;
//-----
  
// style:
  TStyle *eleStyle = new TStyle("eleStyle","Style for electron validation");
  eleStyle->SetCanvasBorderMode(0);
  eleStyle->SetCanvasColor(kWhite);
  eleStyle->SetCanvasDefH(600);
  eleStyle->SetCanvasDefW(800);
  eleStyle->SetCanvasDefX(0);
  eleStyle->SetCanvasDefY(0);
  eleStyle->SetPadBorderMode(0);
  eleStyle->SetPadColor(kWhite);
  eleStyle->SetPadGridX(false);
  eleStyle->SetPadGridY(false);
  eleStyle->SetGridColor(0);
  eleStyle->SetGridStyle(3);
  eleStyle->SetGridWidth(1);
  eleStyle->SetOptStat(1);
  eleStyle->SetPadTickX(1);
  eleStyle->SetPadTickY(1);
  eleStyle->SetHistLineColor(1);
  eleStyle->SetHistLineStyle(0);
  eleStyle->SetHistLineWidth(2);
  eleStyle->SetEndErrorSize(2);
  eleStyle->SetErrorX(0.);
  eleStyle->SetOptStat(1);
  eleStyle->SetTitleColor(1, "XYZ");
  eleStyle->SetTitleFont(42, "XYZ");
  eleStyle->SetTitleXOffset(1.0);
  eleStyle->SetTitleYOffset(1.0);
  eleStyle->SetLabelOffset(0.005, "XYZ");
  eleStyle->SetTitleSize(0.05, "XYZ");
  eleStyle->SetTitleFont(22,"X");
  eleStyle->SetTitleFont(22,"Y");
  eleStyle->SetHistLineWidth(2);
  eleStyle->SetPadBottomMargin(0.13);
  eleStyle->SetPadLeftMargin(0.15);
  eleStyle->SetMarkerStyle(21);
  eleStyle->SetMarkerSize(0.8);
  //-- AC --
  eleStyle->SetPadRightMargin(0.2) ; 

  eleStyle->cd();

  gROOT->ForceStyle();

  TString internal_path("DQMData/Run 1/EgammaV/Run summary/") ;
  TString old_internal_path("DQMData/EgammaV/") ;

  TString file_ref_dir ;
  TFile * file_ref = 0 ;
  if ( CMP_BLUE_FILE != "" )
   {
    file_ref = TFile::Open(CMP_BLUE_FILE) ;
    if (file_ref!=0)
     {
      std::cout<<"open "<<CMP_BLUE_FILE<<std::endl ;
      if (file_ref->cd(internal_path)==kTRUE)
       {
        std::cerr<<"cd "<<internal_path<<std::endl ;
        file_ref_dir = internal_path ;
        file_ref->cd() ;
       }
      else if (file_ref->cd(old_internal_path)==kTRUE)
       {
        std::cerr<<"cd "<<old_internal_path<<std::endl ;
        file_ref_dir = old_internal_path ;
        file_ref->cd() ;
       }
      else
       {
        std::cerr<<"Failed move to: "<<internal_path<<" and "<<old_internal_path<<std::endl ;
        file_ref_dir = "" ;
       }
     }
    else
     { std::cerr<<"Failed to open: "<<CMP_BLUE_FILE<<std::endl ; }
   }

  TString file_new_dir = internal_path  ;
  TFile * file_new = 0 ;
  if ( CMP_RED_FILE != "" )
   {
    file_new = TFile::Open(CMP_RED_FILE) ;
    if (file_new!=0)
     {
      std::cout<<"open "<<CMP_RED_FILE<<std::endl ;
      if (file_new->cd(internal_path)==kTRUE)
       {
        std::cerr<<"cd "<<internal_path<<std::endl ;
        file_new_dir = internal_path ;
        file_new->cd() ;
       }
      else if (file_new->cd(old_internal_path)==kTRUE)
       {
        std::cerr<<"cd "<<old_internal_path<<std::endl ;
        file_new_dir = old_internal_path ;
        file_new->cd() ;
       }
      else
       {
        std::cerr<<"Failed move to: "<<internal_path<<" and "<<old_internal_path<<std::endl ;
        file_new_dir = "" ;
       }
     }
    else
     { std::cerr<<"Failed to open: "<<CMP_RED_FILE<<std::endl ; }
   }

  TCanvas * canvas ;
  TH1 * histo_ref, * histo_new ;
  TPaveStats * st_ref, * st_new ;

  std::ofstream web_page(CMP_DIR+"/index.html") ;

  web_page
    <<"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n"
    <<"<html>\n"
    <<"<head>\n"
    <<"<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />\n"
    <<"<title>"<<CMP_TITLE<<"</title>\n"
    <<"</head>\n"
    <<"<h1><a href=\"../\"><img border=0 width=\"22\" height=\"22\" src=\"../../../../img/up.gif\" alt=\"Up\"/></a>&nbsp;"<<CMP_TITLE<<"</h1>\n" ;

  if (file_ref==0)
   {
    web_page
     <<"<p>In all plots below"
     <<", there was no reference histograms to compare with"
     <<", and the "<<CMP_RED_NAME<<" histograms are in red." ;
   }
  else
   {
/*    web_page
     <<"<p>In all plots below"
     <<", the "<<CMP_RED_NAME<<" histograms are in red"
     <<", and the "<<CMP_BLUE_NAME<<" histograms are in blue." ;*/
    web_page
     <<"<p>In all plots below"
     <<", the "<<CMP_RED_RELEASE<<" histograms are in red"
     <<", and the "<<CMP_BLUE_RELEASE<<" histograms are in blue." ;
/*	std::cout <<"<p>In all plots below "
     <<", the "<<CMP_RED_RELEASE<<" histograms are in red"
     <<", and the "<<CMP_BLUE_RELEASE<<" histograms are in blue." << std::endl ;*/
   }

  std::ifstream red_comment_file(CMP_RED_COMMENT) ;
  std::string red_comment ;
  std::getline(red_comment_file,red_comment) ;
  red_comment_file.close() ;

  std::ifstream blue_comment_file(CMP_BLUE_COMMENT) ;
  std::string blue_comment ;
  std::getline(blue_comment_file,blue_comment) ;
  blue_comment_file.close() ;

  web_page<<" "<<red_comment<<" "<<blue_comment
    <<" Some more details"
    <<": <a href=\"electronCompare.C\">script</a> used to make the plots"
    <<", <a href=\""<<CMP_CONFIG<<"\">specification</a> of histograms"
    <<", <a href=\"gifs/\">images</a> of histograms"
    <<"." ;
  web_page<<"</p>\n" ;

  // canvas_name std::string => TString
  TString canvas_name, histo_name, histo_full_path, gif_name, gif_path ;
  TString short_histo_name ;
  TString first_short_histo_name, first_histo_name ;
  TString dl_short_histo_name, dl_histo_name ;
  TString num_ref, denom_ref, num_full, denom_full ;
  Int_t n_ele_charge ;
  int scaled, err ;
  int divide;
  std::string cat, line, histo_path, num, denom ;
  int eol ; // end of line
  int eoc ; // enf of category
  double rescale_factor = 0. ;

  std::ifstream histo_file1(CMP_CONFIG) ;

  web_page
    <<"<br><table border=\"1\" cellpadding=\"5\" width=\"100%\">"
    <<"<tr valign=\"top\"><td width=\"20%\">\n" ;
  int cat_num = 0 ;

  cat = "" ;
  do
   {
    std::getline(histo_file1,cat) ;
   } while (cat.find_first_not_of(" \t")==std::string::npos) ;

  web_page<<"<b>"<<cat<<"</b><br><br>" ;

  while (std::getline(histo_file1,line))
   {
    if (line.empty()) continue ;
    std::size_t first = line.find_first_not_of(" \t") ;
    if (first==std::string::npos) continue ;
    if (line[first]=='#') continue ;

    std::istringstream linestream(line) ;
    divide = 0 ; num = denom = "" ;
    linestream >> histo_path >> scaled >> err >> eol >> eoc >> divide >> num >> denom ;

    histo_name = histo_path ;
    Ssiz_t pos = histo_name.Last('/') ;
    if (pos!=kNPOS) histo_name.Remove(0,pos+1) ;
    short_histo_name = histo_name ;
    short_histo_name.Remove(0,2) ;
    if (short_histo_name.BeginsWith("ele_"))
     { short_histo_name.Remove(0,4) ; }

    if (first_histo_name.IsNull())
     {
      first_short_histo_name = short_histo_name ;
      first_histo_name = histo_name ;
      dl_short_histo_name = short_histo_name ;
      dl_histo_name = histo_name ;
     }
    else
     {
      TString common ;
      TString first_specific ;
      TString second_specific ;
      CompareHistoNames(first_short_histo_name,short_histo_name,common,first_specific,second_specific) ;
      if (!dl_histo_name.IsNull())
       {
        if (first_specific.IsNull())
         { web_page<<"<a href=\"#"<<first_histo_name<<"\">"<<first_short_histo_name<<"</a>" ; }
        else
         { web_page<<common<<"&nbsp;|&nbsp;<a href=\"#"<<first_histo_name<<"\">"<<first_specific<<"</a>" ; }
        dl_short_histo_name = dl_histo_name = "" ;
       }
      web_page<<"&nbsp;|&nbsp;<a href=\"#"<<histo_name<<"\">"<<second_specific<<"</a>" ;
     }

    if ((eol)||(eoc))
     {
      if (!dl_histo_name.IsNull())
       { web_page<<"<a href=\"#"<<dl_histo_name<<"\">"<<dl_short_histo_name<<"</a>" ; }
      web_page<<"<br>\n" ;
      first_histo_name = first_short_histo_name = "" ;
     }

    if (eoc)
     {
      cat_num++ ;
      if ((cat_num%5)==0)
       { web_page<<"<br></td></tr>\n<tr valign=\"top\"><td width=\"20%\">" ; }
      else
       { web_page<<"<br></td><td width=\"20%\">\n" ; }
      cat = "" ;
      do
       {
        std::getline(histo_file1,cat) ;
       } while (cat.find_first_not_of(" \t")==std::string::npos) ;
      web_page<<"<b>"<<cat<<"</b><br><br>" ;
     }
   }
  web_page<<"<br></td></tr></table>\n" ;
  histo_file1.close() ;

  web_page<<"<br><br><table cellpadding=\"5\"><tr valign=\"top\"><td><a href=\""<<CMP_URL<<"/\"><img width=\"18\" height=\"18\" border=\"0\" align=\"middle\" src=\"../../../../img/up.gif\" alt=\"Top\"/></a></td><td>\n" ;
  std::ifstream histo_file2(CMP_CONFIG) ;

  n_ele_charge = 0 ;
  cat = "" ;
  do
   {
    std::getline(histo_file2,cat) ;
   } while (cat.empty()) ;

  while (std::getline(histo_file2,line))
   {
    if (line.empty()) continue ;
    std::size_t first = line.find_first_not_of(" \t") ;
    if (first==std::string::npos) continue ;
    if (line[first]=='#') continue ;

    std::istrstream linestream(line) ;
    divide = 0 ; num = denom = "" ;
    linestream >> histo_path >> scaled >> err >> eol >> eoc >> divide >> num >> denom ;

    histo_name = histo_path.c_str() ;
    histo_ref = 0 ;
    histo_new = 0 ;
    st_ref = 0 ;
    st_new = 0 ;

    Ssiz_t pos = histo_name.Last('/') ;
    if (pos!=kNPOS) histo_name.Remove(0,pos+1) ;
    short_histo_name = histo_name ;
    short_histo_name.Remove(0,2) ;

    gif_name = "gifs/" ; gif_name += histo_name ; gif_name += ".gif" ;
    gif_path = CMP_DIR ; gif_path += "/" ; gif_path += gif_name ;
    canvas_name = "c" ; canvas_name += histo_name ;
    canvas = new TCanvas(canvas_name) ;
    canvas->SetFillColor(10) ;
	//std::cout << "canvas width " << canvas->GetWindowWidth() << std::endl; // 800 default
	//std::cout << "canvas height " << canvas->GetWindowHeight() << std::endl; // 600 default
	//std::cout << "canvas Y real " << canvas->GetYsizeReal() << std::endl;
	//std::cout << "canvas Y user " << canvas->GetYsizeUser() << std::endl;

    web_page<<"<a id=\""<<histo_name<<"\" name=\""<<short_histo_name<<"\"></a>" ;

    // search histo_ref
    if ( file_ref != 0 )
     {
      if (file_ref_dir.IsNull())
       { histo_full_path = histo_name ; }
      else
       { histo_full_path = file_ref_dir ; histo_full_path += histo_path.c_str() ; }
      histo_ref = (TH1 *)file_ref->Get(histo_full_path) ;
      if (histo_ref!=0)
       {
        // renaming those histograms avoid very strange bugs because they
        // have the same names as the ones loaded from the new file
        histo_ref->SetName(TString(histo_ref->GetName())+"_ref") ;
       }
      else
       {
        web_page<<"No <b>"<<histo_path<<"</b> for "<<CMP_BLUE_NAME<<".<br>" ;
       }
     }

    // search histo_new
    histo_full_path = file_new_dir ; histo_full_path += histo_path.c_str() ;
    histo_new = (TH1 *)file_new->Get(histo_full_path) ;
	//std::cout << "size " << histo_new->GetSize() << std::endl ;

    // special treatments
    if ((scaled==1)&&(histo_new!=0)&&(histo_ref!=0)&&(histo_ref->GetEntries()!=0))
     {
      Int_t new_entries = histo_new->GetEntries() ;
      if (new_entries==0) { new_entries = n_ele_charge ; }
      if (new_entries==0)
       { std::cerr<<"DO NOT KNOW HOW TO RESCALE "<<histo_name<<std::endl ; }
      else
       {
        // we want to reuse the rescale factor of the first histogram
        // for all the subsequent histograms.
        if (rescale_factor==0.) { rescale_factor = new_entries/histo_ref->GetEntries() ; }
        histo_ref->Scale(rescale_factor) ;
       }
     }
    if ((histo_new!=0)&&(histo_ref!=0)&&(histo_ref->GetMaximum()>histo_new->GetMaximum()))
     { histo_new->SetMaximum(histo_ref->GetMaximum()*1.1) ; }

    if (histo_new==0)
     {
      web_page<<"No <b>"<<histo_path<<"</b> for "<<CMP_RED_NAME<<".<br>" ;
     }
    else
     {
      // catch n_ele_charge
      if (histo_name=="h_ele_charge")
       { n_ele_charge = histo_new->GetEntries() ; }

      // draw histo_new
      TString newDrawOptions(err==1?"E1 P":"hist") ;
      gErrorIgnoreLevel = kWarning ;
      if (divide!=0)
       {
        num_full = file_new_dir ; num_full += num.c_str() ;
        denom_full = file_new_dir ; denom_full += denom.c_str() ;
        histo_new = DivideHistos(file_new,histo_new,num_full,denom_full) ;
       }
      histo_new->SetLineColor(kRed) ;
      histo_new->SetMarkerColor(2) ;
      histo_new->SetLineWidth(3) ;
      RenderHisto(histo_new,canvas) ;
      histo_new->Draw(newDrawOptions) ;
//	  std::cout << "SIZE : " << canvas->GetWw() << std::endl ; // 796 default
//	  std::cout << "SIZE : " << canvas->GetWh() << std::endl ; // 572 default
      //canvas->Update() ;
	  //canvas->SetWindowSize(440, 600);
	  canvas->SetCanvasSize(960, 600);
      canvas->Update() ;
      st_new = (TPaveStats*)histo_new->FindObject("stats");
      st_new->SetTextColor(kRed) ;

      // draw histo_ref
      if (histo_ref!=0)
       {
        if (divide!=0)
         {
          num_ref = num ;
          denom_ref = denom ;
          if (file_ref_dir.IsNull())
           {
            pos = num_ref.Last('/') ;
            if (pos!=kNPOS) num_ref.Remove(0,pos+1) ;
            pos = denom_ref.Last('/') ;
            if (pos!=kNPOS) denom_ref.Remove(0,pos+1) ;
           }
          histo_ref = DivideHistos(file_ref,histo_ref,file_ref_dir+num_ref,file_ref_dir+denom_ref) ;
         }
        RenderHisto(histo_ref,canvas) ;
        histo_ref->SetLineColor(kBlue) ;
        histo_ref->SetLineWidth(3) ;
        histo_ref->Draw("sames hist") ;
        canvas->Update() ;
        st_ref = (TPaveStats*)histo_ref->FindObject("stats");
        st_ref->SetTextColor(kBlue) ;
        Double_t y1 = st_ref->GetY1NDC() ;
        Double_t y2 = st_ref->GetY2NDC() ;
        st_ref->SetY1NDC(2*y1-y2) ;
        st_ref->SetY2NDC(y1) ;
        //Double_t x1 = st_ref->GetX1NDC() ;
        //Double_t x2 = st_ref->GetX2NDC() ;
		//std::cout << "position s x1 = " << x1 << std::endl ; // 0.78 par defaut
		//std::cout << "position s x2 = " << x2 << std::endl ; // 0.98 par defaut
		//std::cout << "position s y1 = " << y1 << std::endl ; // 0.755 ou 0.835 par defaut
		//std::cout << "position s y2 = " << y2 << std::endl ; // 0.995 par defaut
       }

      // Redraws
      newDrawOptions = "sames " ;
      newDrawOptions += (err==1?"E1 P":"hist") ;
      histo_new->Draw(newDrawOptions) ;
      if (st_ref!=0) st_ref->Draw() ;
      if (st_new!=0) st_new->Draw() ;

      // eventual log scale
      //if ( (log==1) && ( (histo_new->GetEntries()>0) || ( (histo_ref!=0) && (histo_ref->GetEntries()!=0) ) ) )
      // { canvas->SetLogy(1) ; }


// ne pas oublier de decommenter les 4 lignes suivantes
      std::cout<<histo_name
        <<" has "<<histo_new->GetEffectiveEntries()<<" entries"
        <<" of mean value "<<histo_new->GetMean()
        <<std::endl ;
      canvas->SaveAs(gif_path.Data()) ;
      web_page<<"<a href=\""<<gif_name<<"\"><img border=\"0\" class=\"image\" width=\"440\" src=\""<<gif_name<<"\"></a><br>" ;
     }

//    else if ((file_ref!=0)&&(histo_ref!=0))
//     {
//      std::cout<<histo_path<<" NOT FOUND"<<std::endl ;
//      web_page<<"<br>(no such histo)" ;
//      canvas->SaveAs(gif_path.Data()) ;
//      web_page<<"<a href=\""<<gif_name<<"\"><img border=\"0\" class=\"image\" width=\"440\" src=\""<<gif_name<<"\"></a><br>" ;
//     }

    if (eol)
     { web_page<<"</td></tr>\n<tr valign=\"top\"><td><a href=\""<<CMP_URL<<"/\"><img width=\"18\" height=\"18\" border=\"0\" align=\"middle\" src=\"../../../../img/up.gif\" alt=\"Top\"/></a></td><td>" ; }
    else
     { web_page<<"</td><td>" ; }
    if (eoc)
     {
      cat = "" ;
      do
       {
        std::getline(histo_file2,cat) ;
       } while (cat.empty()) ;
     }
   }
  histo_file2.close() ;
  web_page<<"</td></tr></table>\n" ;

  web_page<<"\n</html>"<<std::endl ;
  web_page.close() ;

 }
示例#6
0
void flagNoiseMatrixGraphs(TString myFileName){  
gSystem->cd("images/NoiseMatrix");
gSystem->cd(myFileName);

TCanvas *flagNoiseMatrixCanv = new TCanvas("flagNoiseMatrixCanv", "flagNoiseMatrixCanv", 200,10,800,800);
flagNoiseMatrixCanv->SetCanvasSize(1200,800);
flagNoiseMatrixCanv->cd();
flagNoiseMatrixGraph_1D = new TH1F("flagNoiseMatrixGraph_1D", "flagNoiseMatrixGraph_1D", 5, 0, 5);
flagNoiseMatrixGraph_1D->GetYaxis()->SetTitle("Number of flag of each type");
flagNoiseMatrixGraph_1D->GetYaxis()->SetLabelSize(0.035);

TLegend *LegNoiseMatrix = new TLegend(0.7,0.5,0.89,0.7);
LegNoiseMatrix->SetHeader("Noise Matrix Flags Definitions");
LegNoiseMatrix->SetFillColor(0);
LegNoiseMatrix->SetTextSize(0);

Calibration->UseCurrentStyle(); 
Calibration->Draw("flagMatrix>>flagNoiseMatrixGraph_1D"); 

LegNoiseMatrix->AddEntry("", "1: Good");
LegNoiseMatrix->AddEntry("", "2: High Noise");
LegNoiseMatrix->AddEntry("", "3: Low Noise" );
LegNoiseMatrix->Draw("same");

flagNoiseMatrixCanv->Update();

PrintAsGif(flagNoiseMatrixCanv, "flagNoiseMatrixGraph_1D");

///////// CHAMBER flag Graph ///////////////////////
gStyle->SetOptStat(0);
TCanvas *flagNoiseMatrixChamberCanv = new TCanvas("flagNoiseMatrixChamberCanv", "flagNoiseMatrixChamberCanv", 200,10,800,800);
flagNoiseMatrixChamberCanv->SetCanvasSize(1200,800);

//create legend 
TLegend *LegNoiseMatrixChamber = new TLegend(0.85,0.8,0.98,0.98);
LegNoiseMatrixChamber->SetHeader("Noise Matrix Flags Definitions");
LegNoiseMatrixChamber->SetFillColor(0);
LegNoiseMatrixChamber->SetTextSize(0);

//final histogram for display
flagNoiseMatrixGraph_2D_Chamber = new TH2F("flagNoiseMatrixGraph_2D_Chamber", "flagNoiseMatrixGraph_2D_Chamber", 9, 0, 9, 4, 0, 4);
///dummy histo to get bin maximum
flagNoiseMatrixGraph_2D_Chamber0 = new TH2F("flagNoiseMatrixGraph_2D_Chamber0", "flagNoiseMatrixGraph_2D_Chamber0", 9, 0, 9, 4, 0, 4);
//one histo for each flag value. 
flagNoiseMatrixGraph_2D_Chamber1 = new TH2F("flagNoiseMatrixGraph_2D_Chamber1", "flagNoiseMatrixGraph_2D_Chamber1", 9, 0, 9, 4, 0, 4);
flagNoiseMatrixGraph_2D_Chamber2 = new TH2F("flagNoiseMatrixGraph_2D_Chamber2", "flagNoiseMatrixGraph_2D_Chamber2", 9, 0, 9, 4, 0, 4);
flagNoiseMatrixGraph_2D_Chamber3 = new TH2F("flagNoiseMatrixGraph_2D_Chamber3", "flagNoiseMatrixGraph_2D_Chamber3", 9, 0, 9, 4, 0, 4);
flagNoiseMatrixGraph_2D_Chamber4 = new TH2F("flagNoiseMatrixGraph_2D_Chamber4", "flagNoiseMatrixGraph_2D_Chamber4", 9, 0, 9, 4, 0, 4);

//fill completley, get bin maximum, set it for overall graph
Calibration->Project("flagNoiseMatrixGraph_2D_Chamber0", "flagMatrix:cham"); 
Double_t binMaxValCham = flagNoiseMatrixGraph_2D_Chamber0->GetMaximum();
//normalize each box appropriately, with respect to the most filled box
flagNoiseMatrixGraph_2D_Chamber->SetMaximum(binMaxValCham);

//fill each "bin"
Calibration->Project("flagNoiseMatrixGraph_2D_Chamber1","flagMatrix:cham", "flagMatrix==1", "box"); 
Calibration->Project("flagNoiseMatrixGraph_2D_Chamber2","flagMatrix:cham", "flagMatrix==2", "box"); 
Calibration->Project("flagNoiseMatrixGraph_2D_Chamber3","flagMatrix:cham", "flagMatrix==3", "box"); 

//set appropriate colors
flagNoiseMatrixGraph_2D_Chamber1->SetFillColor(1);//Black for eveything is OK
flagNoiseMatrixGraph_2D_Chamber2->SetFillColor(2);//red for VERY BAD
flagNoiseMatrixGraph_2D_Chamber3->SetFillColor(3);//Green for pretty good

int idArray[9];
GetChamberIDs(idArray);
for (int chamNum = 0; chamNum<9; ++chamNum){
int chamNumPlus = chamNum + 1; //for bin access

Int_t chamber_id_int = idArray[chamNum]; //set individual id as int
std::stringstream chamber_id_stream;     //define variable in intermediate format
chamber_id_stream << chamber_id_int;     //convert from int to intermediate "stringstream" format
TString chamber_id_str = chamber_id_stream.str();  //convert from stream into string
 if (chamber_id_str.BeginsWith("220")==0 ){  //binary check, i.e. if the string doesn't begin with 220
   chamber_id_str=0;                         //clean out; set to 0.
 }else{
   chamber_id_str.Remove(8,8);               //remove 0 at end
   chamber_id_str.Remove(0,3);               //remove 220 at beginning 
 }
flagNoiseMatrixGraph_2D_Chamber->GetXaxis()->SetBinLabel(chamNumPlus,chamber_id_str); //set bins to have chamber names
}

flagNoiseMatrixGraph_2D_Chamber->GetYaxis()->SetTitle("Flag");
flagNoiseMatrixGraph_2D_Chamber->GetXaxis()->SetTitle("Chamber");

flagNoiseMatrixChamberCanv->cd();  
//draw original histogram, empty
flagNoiseMatrixGraph_2D_Chamber->Draw("box");
//overlay the individual "bin" graphs
flagNoiseMatrixGraph_2D_Chamber1->Draw("samebox");
flagNoiseMatrixGraph_2D_Chamber2->Draw("samebox");
flagNoiseMatrixGraph_2D_Chamber3->Draw("samebox");
flagNoiseMatrixGraph_2D_Chamber4->Draw("samebox");

//set legend entries appropriately
LegNoiseMatrixChamber->AddEntry(flagNoiseMatrixGraph_2D_Chamber1, "Good", "f");
LegNoiseMatrixChamber->AddEntry(flagNoiseMatrixGraph_2D_Chamber2, "High Noise", "f");
LegNoiseMatrixChamber->AddEntry(flagNoiseMatrixGraph_2D_Chamber3, "Low noise", "f");
LegNoiseMatrixChamber->Draw("same");

//print as gif
PrintAsGif(flagNoiseMatrixChamberCanv, "flagNoiseMatrixChamber");
gStyle->SetOptStat(0);gSystem->cd("../../../");
directoryCheck();
}
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);
 
  // output file
  TFile* ofile = TFile::Open( (options.oDir+std::string("/ZMuMufromW.root")).c_str(), "RECREATE");
  std::string oDir = options.oDir + std::string("/ZmumuFromW");

  if (oDir!="") {
    boost::filesystem::path opath(oDir);
    if (!exists(opath)) {
      std::cout << "Creating output directory : " << oDir << std::endl;
      boost::filesystem::create_directory(opath);
    }
    std::cout << "Writing results to " << oDir << std::endl;
  }

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

  TCut puWeight("puWeight");
  TCut trigCorr( "(trigCorrWeight>0) ? trigCorrWeight : 1." );

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

  TCut METNo2Muon130("metNo2Muon>130.");
  TCut cutLoDPhi = cuts.cut("dPhiJJ");
  TCut cutHiDPhi("vbfDPhi>2.6");
  TCut cutMe1DPhi("vbfDPhi>1.0 && vbfDPhi<=1.8");
  TCut cutMe2DPhi("vbfDPhi>1.8 && vbfDPhi<=2.6");

  // histograms
  double dphiEdges[5] = { 0., 1.0, 1.8, 2.6, TMath::Pi() };

  // Observed signal MET>130
  TH1D* hZ_DY_C_DPhi 	= new TH1D("hZ_DY_C_DPhi", "", 4, dphiEdges);  // Z+jets MC ctrl region
  TH1D* hZ_BG_ZC_DPhi 	= new TH1D("hZ_BG_ZC_DPhi", "", 4, dphiEdges);  // background MC ctrl region
  TH1D* hZ_Data_ZC_DPhi = new TH1D("hZ_Data_ZC_DPhi", "", 4, dphiEdges);  // Data ctrl region
  // Predicted from Wmunu
  TH1D* hZ_W_C_DPhi 	= new TH1D("hZ_W_C_DPhi", "", 4, dphiEdges);  	// W+jets MC ctrl region
  TH1D* hZ_BG_WC_DPhi 	= new TH1D("hZ_BG_WC_DPhi", "", 4, dphiEdges);  	// background MC ctrl region
  TH1D* hZ_Data_WC_DPhi = new TH1D("hZ_Data_WC_DPhi", "", 4, dphiEdges);  // Data W ctrl region

  TH1D* hZ_W_EffMu_D 	= new TH1D("hZ_W_EffMu_D", "", 1, 0., 1.);	// denominator of MuMu efficiency from DY(pT<100) + DY(pT>100) + DY_EWK samples
  TH1D* hZ_W_EffMu_N 	= new TH1D("hZ_W_EffMu_N", "", 1, 0., 1.);	// numerator of MuMu efficiency from DY(pT<100) + DY(pT>100) + DY_EWK samples

  TH1D* hZ_DY_EffMuMu_D = new TH1D("hZ_DY_EffMuMu_D", "", 1, 0., 1.);   // denominator of MuMu efficiency from DY + DY_EWK samples
  TH1D* hZ_DY_EffMuMu_N = new TH1D("hZ_DY_EffMuMu_N", "", 1, 0., 1.);   // numerator of MuMu efficiency from DY + DY_EWK samples 
  
  TH1D* hZ_DY_EffVBFS_D = new TH1D("hZ_DY_EffVBFS_D", "", 1, 0., 1.);   // denominator of VBF(S) efficiency from DY(pT<100) + DY(pT>100) + DY_EWK samples
  //TH1D* hZ_DY_EffVBFS_N  = new TH1D("hZ_DY_EffVBFS_N", "", 1, 0., 1.);   // numerator of VBF(S) efficiency from DY(pT<100) + DY(pT>100) + DY_EWK samples  
  TH1D* hZ_DY_EffVBFS_NLo  = new TH1D("hZ_DY_EffVBFS_NLo", "", 1, 0., 1.);   
  TH1D* hZ_DY_EffVBFS_NHi  = new TH1D("hZ_DY_EffVBFS_NHi", "", 1, 0., 1.);
  TH1D* hZ_DY_EffVBFS_NMe1 = new TH1D("hZ_DY_EffVBFS_NMe1", "", 1, 0., 1.); 
  TH1D* hZ_DY_EffVBFS_NMe2 = new TH1D("hZ_DY_EffVBFS_NMe2", "", 1, 0., 1.);  

  TH1D* hZ_W_EffVBFC_D 	   = new TH1D("hZ_W_EffVBFC_D", "", 1, 0., 1.);   // denominator of VBF(C) efficiency from DY(pT<100) + DY(pT>100) + DY_EWK samples
  //TH1D* hZ_W_EffVBFC_N   = new TH1D("hZ_W_EffVBFC_N", "", 1, 0., 1.);   // numerator of VBF(C) efficiency from DY(pT<100) + DY(pT>100) + DY_EWK samples 
  TH1D* hZ_W_EffVBFC_NLo   = new TH1D("hZ_W_EffVBFC_NLo", "", 1, 0., 1.);  
  TH1D* hZ_W_EffVBFC_NHi   = new TH1D("hZ_W_EffVBFC_NHi", "", 1, 0., 1.);
  TH1D* hZ_W_EffVBFC_NMe1  = new TH1D("hZ_W_EffVBFC_NMe1", "", 1, 0., 1.);
  TH1D* hZ_W_EffVBFC_NMe2  = new TH1D("hZ_W_EffVBFC_NMe2", "", 1, 0., 1.);

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

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

    // bit of a fudge for mutight weight - data doesn't have the var in it, so need to add an exception (see below)
    // and then restore it for all other MC
    muTightWeight=cuts.muTightWeight(options.leptCorr);

    // check if it's DYJets
    bool isDY = false;
    bool isWJets = false;
    bool isQCD = false;
    bool isEwkW = false;
    if (dataset.name.compare(0,2,"DY")==0) {
      isDY = true;
      std::cout << "Analysing DY->ll MC : " << dataset.name << std::endl;
    }
    else if (dataset.name == "WJets" ||
        dataset.name == "W1Jets" ||
        dataset.name == "W2Jets" ||
        dataset.name == "W3Jets" ||
        dataset.name == "W4Jets" ||
        dataset.name == "EWK_Wp2Jets" ||
        dataset.name == "EWK_Wm2Jets") {

      if (dataset.name == "EWK_Wp2Jets" || dataset.name == "EWK_Wm2Jets") isEwkW = true;
      else isWJets = true;

      if(isWJets) wWeight =  cuts.wWeight();
      std::cout << "Analysing W MC     : " << dataset.name << std::endl;
    }
    else if (dataset.name.compare(0,3,"QCD")==0) {
      isQCD = true;
      std::cout << "Analysing QCD : " << dataset.name << std::endl;
    }
    else if (dataset.isData) {
      muTightWeight="";
      std::cout << "Analysing Data      : " << dataset.name << std::endl;
    }
    else {
      std::cout << "Analysing BG MC     : " << dataset.name << std::endl;
    }

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

    // set up cuts
    TCut otherCuts = puWeight * trigCorr * wWeight;

    TCut cutZMuMu_C    	      = otherCuts * muTightWeight * (cutD + cuts.zMuMuVBF() + METNo2Muon130);
    TCut cutWMuNu_C    	      = otherCuts * muTightWeight * (cutD + cuts.wMuVBF() + cuts.cutWMu("MET"));
    // eps_mu from W trigger samples
    TCut cutEfficiencyMu_D    = otherCuts * (cutD + cuts.HLTandMETFilters() + cuts.wMuGen());
    TCut cutEfficiencyMu_N    = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.wMuGen() + cuts.cutWMu("wMu"));

    TCut cutEfficiencyMuMu_D    = otherCuts * (cutD + cuts.HLTandMETFilters() + cuts.zMuMuGen());
    TCut cutEfficiencyMuMu_N    = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.zMuMuGen() + cuts.zMuMuReco());
    // eps_VBFS from DY-Trig
    TCut cutEfficiencyVBFS_D    = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.zMuMuGen() + cuts.zMuMuReco());
    //TCut cutEfficiencyVBFS_N  = otherCuts * (cutD + cuts.HLTandMETFilters() + cuts.zMuMuGen() + cuts.zMuMuReco() + cuts.vbf() + METNo2Muon130);
    TCut cutEfficiencyVBFS_NLoDPhi  = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.zMuMuGen() + cuts.zMuMuReco() + cuts.vbf() + METNo2Muon130 + cutLoDPhi);
    TCut cutEfficiencyVBFS_NHiDPhi  = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.zMuMuGen() + cuts.zMuMuReco() + cuts.vbf() + METNo2Muon130 + cutHiDPhi);
    TCut cutEfficiencyVBFS_NMe1DPhi = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.zMuMuGen() + cuts.zMuMuReco() + cuts.vbf() + METNo2Muon130 + cutMe1DPhi);
    TCut cutEfficiencyVBFS_NMe2DPhi = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.zMuMuGen() + cuts.zMuMuReco() + cuts.vbf() + METNo2Muon130 + cutMe2DPhi);

    // eps_VBFC from W
    TCut cutEfficiencyVBFC_D    = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.wMuGen() + cuts.cutWMu("wMu"));
    //TCut cutEfficiencyVBFC_N  = otherCuts * (cutD + cuts.HLTandMETFilters() + cuts.wMuGen() + cuts.cutWMu("wMu") + cuts.vbf() + cuts.cutWMu("MET")); 
    TCut cutEfficiencyVBFC_NLoDPhi  = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.wMuGen() + cuts.cutWMu("wMu") + cuts.vbf() + cuts.cutWMu("MET") + cutLoDPhi);
    TCut cutEfficiencyVBFC_NHiDPhi  = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.wMuGen() + cuts.cutWMu("wMu") + cuts.vbf() + cuts.cutWMu("MET") + cutHiDPhi);
    TCut cutEfficiencyVBFC_NMe1DPhi = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.wMuGen() + cuts.cutWMu("wMu") + cuts.vbf() + cuts.cutWMu("MET") + cutMe1DPhi);
    TCut cutEfficiencyVBFC_NMe2DPhi = otherCuts * muTightWeight * (cutD + cuts.HLTandMETFilters() + cuts.wMuGen() + cuts.cutWMu("wMu") + cuts.vbf() + cuts.cutWMu("MET") + cutMe2DPhi);

    // fill tmp histograms for BG estimation
    //observed
    //TH1D* hZ_ZC_noDPhi  	= new TH1D("hZ_ZC_noDPhi", "", 1, 0., 1.);
    TH1D* hZ_ZC_DPhi  		= new TH1D("hZ_ZC_DPhi", "", 4, dphiEdges);  
    //predicted
    //TH1D* hZ_WC_noDPhi  	= new TH1D("hZ_WC_noDPhi", "", 1, 0., 1.);
    TH1D* hZ_WC_DPhi  		= new TH1D("hZ_WC_DPhi", "", 4, dphiEdges);  // this is for the actual BG estimation
    // fill tmp histograms for efficiency calculation
    TH1D* hZ_EffMuMu_D  	= new TH1D("hZ_EffMuMu_D",   "", 1, 0., 1.);
    TH1D* hZ_EffMuMu_N  	= new TH1D("hZ_EffMuMu_N",   "", 1, 0., 1.);
    TH1D* hZ_EffMu_D  		= new TH1D("hZ_EffMu_D",     "", 1, 0., 1.);
    TH1D* hZ_EffMu_N  		= new TH1D("hZ_EffMu_N",     "", 1, 0., 1.);
    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_EffVBFS_NLo  	= new TH1D("hZ_EffVBFS_NLo", "", 1, 0., 1.);
    TH1D* hZ_EffVBFS_NHi  	= new TH1D("hZ_EffVBFS_NHi", "", 1, 0., 1.);
    TH1D* hZ_EffVBFS_NMe1  	= new TH1D("hZ_EffVBFS_NMe1","", 1, 0., 1.);
    TH1D* hZ_EffVBFS_NMe2  	= new TH1D("hZ_EffVBFS_NMe2","", 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_EffVBFC_NLo  	= new TH1D("hZ_EffVBFC_NLo", "", 1, 0., 1.);
    TH1D* hZ_EffVBFC_NHi  	= new TH1D("hZ_EffVBFC_NHi", "", 1, 0., 1.);
    TH1D* hZ_EffVBFC_NMe1  	= new TH1D("hZ_EffVBFC_NMe1","", 1, 0., 1.);
    TH1D* hZ_EffVBFC_NMe2  	= new TH1D("hZ_EffVBFC_NMe2","", 1, 0., 1.);

    // W control region DY is BG
    if (isDY) { 
      //tree->Draw("0.5>>hZ_ZC_noDPhi",    cutZMuMu_C);
      tree->Draw("vbfDPhi>>hZ_ZC_DPhi",    cutZMuMu_C);

      //tree->Draw("0.5>>hZ_WC_noDPhi",    cutWMuNu_C);
      tree->Draw("vbfDPhi>>hZ_WC_DPhi",    cutWMuNu_C);
      tree->Draw("0.5>>hZ_EffMuMu_D",      cutEfficiencyMuMu_D);
      tree->Draw("0.5>>hZ_EffMuMu_N",      cutEfficiencyMuMu_N);
      tree->Draw("0.5>>hZ_EffVBFS_D",      cutEfficiencyVBFS_D);
      //tree->Draw("0.5>>hZ_EffVBFS_N",    cutEfficiencyVBFS_N);
      tree->Draw("0.5>>hZ_EffVBFS_NLo",    cutEfficiencyVBFS_NLoDPhi);
      tree->Draw("0.5>>hZ_EffVBFS_NHi",    cutEfficiencyVBFS_NHiDPhi);
      tree->Draw("0.5>>hZ_EffVBFS_NMe1",   cutEfficiencyVBFS_NMe1DPhi);
      tree->Draw("0.5>>hZ_EffVBFS_NMe2",   cutEfficiencyVBFS_NMe2DPhi);
    }
    else if(isWJets || isEwkW) {
      //tree->Draw("0.5>>hZ_WC_noDPhi",    cutWMuNu_C);
      tree->Draw("vbfDPhi>>hZ_WC_DPhi",    cutWMuNu_C);
      tree->Draw("0.5>>hZ_EffMu_D",        cutEfficiencyMu_D);  
      tree->Draw("0.5>>hZ_EffMu_N",        cutEfficiencyMu_N);    
      tree->Draw("0.5>>hZ_EffVBFC_D",      cutEfficiencyVBFC_D);
      //tree->Draw("0.5>>hZ_EffVBFC_N",    cutEfficiencyVBFC_N);
      tree->Draw("0.5>>hZ_EffVBFC_NLo",    cutEfficiencyVBFC_NLoDPhi);
      tree->Draw("0.5>>hZ_EffVBFC_NHi",    cutEfficiencyVBFC_NHiDPhi);
      tree->Draw("0.5>>hZ_EffVBFC_NMe1",   cutEfficiencyVBFC_NMe1DPhi);
      tree->Draw("0.5>>hZ_EffVBFC_NMe2",   cutEfficiencyVBFC_NMe2DPhi);
    }
    else if(isQCD) {
      //tree->Draw("0.5>>hZ_C_noDPhi",     cutWMuNu_C);
      tree->Draw("vbfDPhi>>hZ_WC_DPhi",    cutWMuNu_C);
    }
    else {
      tree->Draw("vbfDPhi>>hZ_ZC_DPhi",    cutZMuMu_C);
      tree->Draw("vbfDPhi>>hZ_WC_DPhi",    cutWMuNu_C);
    }

    // weight  to lumi
    double weight = (dataset.isData) ? 1. : lumi * dataset.sigma / dataset.nEvents;

    hZ_ZC_DPhi->Scale(weight);
    hZ_WC_DPhi->Scale(weight);
    hZ_EffVBFS_D->Scale(weight);
    //hZ_EffVBFS_N->Scale(weight);
    hZ_EffVBFS_NLo->Scale(weight);
    hZ_EffVBFS_NHi->Scale(weight);
    hZ_EffVBFS_NMe1->Scale(weight);
    hZ_EffVBFS_NMe2->Scale(weight);
    hZ_EffVBFC_D->Scale(weight);
    //hZ_EffVBFC_N->Scale(weight);
    hZ_EffVBFC_NLo->Scale(weight);
    hZ_EffVBFC_NHi->Scale(weight);
    hZ_EffVBFC_NMe1->Scale(weight);
    hZ_EffVBFC_NMe2->Scale(weight);
    hZ_EffMu_D->Scale(weight);
    hZ_EffMu_N->Scale(weight);
    hZ_EffMuMu_D->Scale(weight);
    hZ_EffMuMu_N->Scale(weight);

    // add to output histograms
    if (dataset.isData) {
      hZ_Data_ZC_DPhi->Add(hZ_ZC_DPhi);
      hZ_Data_WC_DPhi->Add(hZ_WC_DPhi);
    }
    else if (isWJets || isEwkW) {
      //hZ_W_C_noDPhi->Add(hZ_C_noDPhi);
      hZ_W_C_DPhi->Add(hZ_WC_DPhi);
      hZ_W_EffMu_D->Add(hZ_EffMu_D);
      hZ_W_EffMu_N->Add(hZ_EffMu_N);
      hZ_W_EffVBFC_D->Add(hZ_EffVBFC_D);
      //hZ_W_EffVBFC_N->Add(hZ_EffVBFC_N);
      hZ_W_EffVBFC_NLo->Add(hZ_EffVBFC_NLo);
      hZ_W_EffVBFC_NHi->Add(hZ_EffVBFC_NHi);
      hZ_W_EffVBFC_NMe1->Add(hZ_EffVBFC_NMe1);
      hZ_W_EffVBFC_NMe2->Add(hZ_EffVBFC_NMe2);
    }
    else if (isDY) {
      hZ_DY_C_DPhi->Add(hZ_ZC_DPhi);
      hZ_BG_WC_DPhi->Add(hZ_WC_DPhi);

      hZ_DY_EffMuMu_D->Add(hZ_EffMuMu_D);
      hZ_DY_EffMuMu_N->Add(hZ_EffMuMu_N);
      hZ_DY_EffVBFS_D->Add(hZ_EffVBFS_D);
      //hZ_DY_EffVBFS_N->Add(hZ_EffVBFS_N);
      hZ_DY_EffVBFS_NLo->Add(hZ_EffVBFS_NLo);
      hZ_DY_EffVBFS_NHi->Add(hZ_EffVBFS_NHi);
      hZ_DY_EffVBFS_NMe1->Add(hZ_EffVBFS_NMe1);
      hZ_DY_EffVBFS_NMe2->Add(hZ_EffVBFS_NMe2);
    }
    else if (isQCD) {
      //hZ_BG_WC_DPhi->Add(hZ_WC_DPhi);
    }
    else {
      hZ_BG_WC_DPhi->Add(hZ_WC_DPhi);
      hZ_BG_ZC_DPhi->Add(hZ_ZC_DPhi);
    }

    std::cout << "  N_Z ctrl (dphi<1.0) : " << hZ_ZC_DPhi->GetBinContent(1) << " +/- " << hZ_ZC_DPhi->GetBinError(1) << std::endl;
    std::cout << "  N_W ctrl (dphi<1.0) : " << hZ_WC_DPhi->GetBinContent(1) << " +/- " << hZ_WC_DPhi->GetBinError(1) << std::endl;
    
    delete hZ_WC_DPhi;
    delete hZ_ZC_DPhi;
    delete hZ_EffMuMu_D;
    delete hZ_EffMuMu_N;
    delete hZ_EffMu_D;
    delete hZ_EffMu_N;
    delete hZ_EffVBFS_D;
    //delete hZ_EffVBFS_N;
    delete hZ_EffVBFS_NLo;
    delete hZ_EffVBFS_NMe1;
    delete hZ_EffVBFS_NMe2;
    delete hZ_EffVBFS_NHi;
    delete hZ_EffVBFC_D;
    //delete hZ_EffVBFC_N;
    delete hZ_EffVBFC_NLo;
    delete hZ_EffVBFC_NMe1;
    delete hZ_EffVBFC_NMe2;
    delete hZ_EffVBFC_NHi;

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

  // numbers - calculate these from MC in this program later!
  //double ratioBF = 5.626;  //  MCFM + NLO
  double ratioBF = 1144./14428.;

  TH1D* hZ_Est_ZC_DPhi = new TH1D("hZ_Est_ZC_DPhi", "", 4, dphiEdges); 

  // bins dPhi
  TH1D* hZ_Est_WC_DPhi = new TH1D("hZ_Est_WC_DPhi", "", 4, dphiEdges); 
  TH1D* hZ_Est_WS_DPhi = new TH1D("hZ_Est_WS_DPhi", "", 4, dphiEdges); 
  TH1D* hZ_Eff_WS_DPhi = new TH1D("hZ_Eff_WS_DPhi", "", 4, dphiEdges);
 
  TH1D* hZ_W_EffMu    = new TH1D("hZ_W_EffMu", "", 1, 0., 1.);     	// epsilon mumu
  hZ_W_EffMu->Add(hZ_W_EffMu_N);
  hZ_W_EffMu->Divide(hZ_W_EffMu_D);
  //double mu_syst = 0.025*hZ_W_EffMu->GetBinContent(1);      //2.5% Muon ID/Iso efficiency uncertainty from EWK-10-002
  //hZ_W_EffMu->SetBinError(1,TMath::Sqrt(hZ_W_EffMu->GetBinError(1)*hZ_W_EffMu->GetBinError(1) + mu_syst*mu_syst));

  TH1D* hZ_DY_EffMuMu = new TH1D("hZ_DY_EffMuMu", "", 1, 0., 1.);       // epsilon mumu
  hZ_DY_EffMuMu->Add(hZ_DY_EffMuMu_N);
  hZ_DY_EffMuMu->Divide(hZ_DY_EffMuMu_D);
  //mu_syst = 0.025*hZ_DY_EffMuMu->GetBinContent(1);      //2.5% Muon ID/Iso efficiency uncertainty from EWK-10-002
  //hZ_DY_EffMuMu->SetBinError(1,TMath::Sqrt(hZ_DY_EffMuMu->GetBinError(1)*hZ_DY_EffMuMu->GetBinError(1) + mu_syst*mu_syst));

  //TH1D* hZ_DY_EffVBFS  = new TH1D("hZ_DY_EffVBFS", "", 1, 0., 1.);  	// epsilon_s_vbf
  TH1D* hZ_DY_EffVBFSLo  = new TH1D("hZ_DY_EffVBFSLo", "", 1, 0., 1.);
  TH1D* hZ_DY_EffVBFSHi  = new TH1D("hZ_DY_EffVBFSHi", "", 1, 0., 1.);
  TH1D* hZ_DY_EffVBFSMe1 = new TH1D("hZ_DY_EffVBFSMe1", "", 1, 0., 1.);
  TH1D* hZ_DY_EffVBFSMe2 = new TH1D("hZ_DY_EffVBFSMe2", "", 1, 0., 1.);

  //TH1D* hZ_W_EffVBFC   = new TH1D("hZ_W_EffVBFC", "", 1, 0., 1.);       // epsilon_c_vbf
  TH1D* hZ_W_EffVBFCLo   = new TH1D("hZ_W_EffVBFCLo", "", 1, 0., 1.);
  TH1D* hZ_W_EffVBFCHi   = new TH1D("hZ_W_EffVBFCHi", "", 1, 0., 1.);
  TH1D* hZ_W_EffVBFCMe1  = new TH1D("hZ_W_EffVBFCMe1", "", 1, 0., 1.);
  TH1D* hZ_W_EffVBFCMe2  = new TH1D("hZ_W_EffVBFCMe2", "", 1, 0., 1.);

  //TH1D* hZ_W_RatioVBF  = new TH1D("hZ_W_RatioVBF", "", 1, 0., 1.);	// epsilon_s_vbf/epsilon_c_vbf
  TH1D* hZ_W_RatioVBFLo  = new TH1D("hZ_W_RatioVBFLo", "", 1, 0., 1.); 
  TH1D* hZ_W_RatioVBFHi  = new TH1D("hZ_W_RatioVBFHi", "", 1, 0., 1.); 
  TH1D* hZ_W_RatioVBFMe1 = new TH1D("hZ_W_RatioVBFMe1", "", 1, 0., 1.); 
  TH1D* hZ_W_RatioVBFMe2 = new TH1D("hZ_W_RatioVBFMe2", "", 1, 0., 1.); 

  //TH1D* hZ_W_TotalEff  = new TH1D("hZ_W_TotalEff", "", 1, 0., 1.); 
  TH1D* hZ_W_TotalEffLo  = new TH1D("hZ_W_TotalEffLo", "", 1, 0., 1.);
  TH1D* hZ_W_TotalEffMe1 = new TH1D("hZ_W_TotalEffMe1", "", 1, 0., 1.);
  TH1D* hZ_W_TotalEffMe2 = new TH1D("hZ_W_TotalEffMe2", "", 1, 0., 1.);
  TH1D* hZ_W_TotalEffHi  = new TH1D("hZ_W_TotalEffHi", "", 1, 0., 1.);
  
  //hZ_DY_EffVBFS->Add(hZ_DY_EffVBFS_N);
  //hZ_DY_EffVBFS->Divide(hZ_DY_EffVBFS_D);
  hZ_DY_EffVBFSLo->Add(hZ_DY_EffVBFS_NLo);
  hZ_DY_EffVBFSLo->Divide(hZ_DY_EffVBFS_D);
  hZ_DY_EffVBFSHi->Add(hZ_DY_EffVBFS_NHi);
  hZ_DY_EffVBFSHi->Divide(hZ_DY_EffVBFS_D);
  hZ_DY_EffVBFSMe1->Add(hZ_DY_EffVBFS_NMe1);
  hZ_DY_EffVBFSMe1->Divide(hZ_DY_EffVBFS_D);
  hZ_DY_EffVBFSMe2->Add(hZ_DY_EffVBFS_NMe2);
  hZ_DY_EffVBFSMe2->Divide(hZ_DY_EffVBFS_D);

  //hZ_W_EffVBFC->Add(hZ_W_EffVBFC_N);
  //hZ_W_EffVBFC->Divide(hZ_W_EffVBFC_D);
  hZ_W_EffVBFCLo->Add(hZ_W_EffVBFC_NLo);
  hZ_W_EffVBFCLo->Divide(hZ_W_EffVBFC_D);
  hZ_W_EffVBFCMe1->Add(hZ_W_EffVBFC_NMe1);
  hZ_W_EffVBFCMe1->Divide(hZ_W_EffVBFC_D);
  hZ_W_EffVBFCMe2->Add(hZ_W_EffVBFC_NMe2);
  hZ_W_EffVBFCMe2->Divide(hZ_W_EffVBFC_D);
  hZ_W_EffVBFCHi->Add(hZ_W_EffVBFC_NHi);
  hZ_W_EffVBFCHi->Divide(hZ_W_EffVBFC_D);

  //hZ_W_RatioVBF->Add(hZ_DY_EffVBFS);
  //hZ_W_RatioVBF->Divide(hZ_W_EffVBFC);
  hZ_W_RatioVBFLo->Add(hZ_DY_EffVBFSLo);
  hZ_W_RatioVBFLo->Divide(hZ_W_EffVBFCLo);
  hZ_W_RatioVBFMe1->Add(hZ_DY_EffVBFSMe1);
  hZ_W_RatioVBFMe1->Divide(hZ_W_EffVBFCMe1);
  hZ_W_RatioVBFMe2->Add(hZ_DY_EffVBFSMe2);
  hZ_W_RatioVBFMe2->Divide(hZ_W_EffVBFCMe2);
  hZ_W_RatioVBFHi->Add(hZ_DY_EffVBFSHi);
  hZ_W_RatioVBFHi->Divide(hZ_W_EffVBFCHi);

  hZ_W_TotalEffLo->Add(hZ_W_RatioVBFLo);
  hZ_W_TotalEffLo->Multiply(hZ_DY_EffMuMu);
  hZ_W_TotalEffLo->Divide(hZ_W_EffMu);
  hZ_W_TotalEffMe1->Add(hZ_W_RatioVBFMe1);
  hZ_W_TotalEffMe1->Multiply(hZ_DY_EffMuMu);
  hZ_W_TotalEffMe1->Divide(hZ_W_EffMu);
  hZ_W_TotalEffMe2->Add(hZ_W_RatioVBFMe2);
  hZ_W_TotalEffMe2->Multiply(hZ_DY_EffMuMu);
  hZ_W_TotalEffMe2->Divide(hZ_W_EffMu);
  hZ_W_TotalEffHi->Add(hZ_W_RatioVBFHi);
  hZ_W_TotalEffHi->Multiply(hZ_DY_EffMuMu);
  hZ_W_TotalEffHi->Divide(hZ_W_EffMu);

  //hZ_Eff_S_noDPhi->Add(hZ_W_RatioVBF);  
  //hZ_Eff_S_noDPhi->Divide(hZ_W_EffMu);
  hZ_Eff_WS_DPhi->SetBinContent(1,hZ_W_TotalEffLo->GetBinContent(1));
  hZ_Eff_WS_DPhi->SetBinError(1,hZ_W_TotalEffLo->GetBinError(1));
  hZ_Eff_WS_DPhi->SetBinContent(2,hZ_W_TotalEffMe1->GetBinContent(1));  
  hZ_Eff_WS_DPhi->SetBinError(2,hZ_W_TotalEffMe1->GetBinError(1));
  hZ_Eff_WS_DPhi->SetBinContent(3,hZ_W_TotalEffMe2->GetBinContent(1));
  hZ_Eff_WS_DPhi->SetBinError(3,hZ_W_TotalEffMe2->GetBinError(1));
  hZ_Eff_WS_DPhi->SetBinContent(4,hZ_W_TotalEffHi->GetBinContent(1));
  hZ_Eff_WS_DPhi->SetBinError(4,hZ_W_TotalEffHi->GetBinError(1));

  //for(int ibin = 1; ibin <= hZ_Eff_S_DPhi->GetNbinsX(); ++ibin) {
  //  hZ_Eff_S_DPhi->SetBinContent(ibin,hZ_W_TotalEff->GetBinContent(1));
  //  hZ_Eff_S_DPhi->SetBinError  (ibin,hZ_W_TotalEff->GetBinError(1));
  //}
  // Observed 
  hZ_Est_ZC_DPhi->Add(hZ_Data_ZC_DPhi, hZ_BG_ZC_DPhi, 1., -1.);
  // Predicted
  hZ_Est_WC_DPhi->Add(hZ_Data_WC_DPhi, hZ_BG_WC_DPhi, 1., -1.);
  hZ_Est_WS_DPhi->Add(hZ_Est_WC_DPhi,ratioBF);
  hZ_Est_WS_DPhi->Multiply(hZ_Eff_WS_DPhi);

  // print out

  std::cout << std::endl;
  std::cout << "##################################### Cross-check with W mu ctrl region #####################################" << std::endl;
  std::cout << std::endl;
  std::cout << "  eps_mumu by histogram  : " << hZ_DY_EffMuMu->GetBinContent(1) << " +/- " << hZ_DY_EffMuMu->GetBinError(1) << std::endl;
  std::cout << "  eps_mu by histogram    : " << hZ_W_EffMu->GetBinContent(1) << " +/- " << hZ_W_EffMu->GetBinError(1) << std::endl;
  std::cout << std::endl;
  std::cout << "dphi < 1.0" << std::endl;
  std::cout << std::endl;
  std::cout << "  eps_s_vbf by histogram : " << hZ_DY_EffVBFSLo->GetBinContent(1) << " +/- " << hZ_DY_EffVBFSLo->GetBinError(1) << std::endl;
  std::cout << "  eps_c_vbf by histogram : " << hZ_W_EffVBFCLo->GetBinContent(1) << " +/- " << hZ_W_EffVBFCLo->GetBinError(1) << std::endl;
  std::cout << "  ratio_vbf by histogram : " << hZ_W_RatioVBFLo->GetBinContent(1) << " +/- " << hZ_W_RatioVBFLo->GetBinError(1) << std::endl;
  std::cout << "  total eff by histogram : " << hZ_Eff_WS_DPhi->GetBinContent(1) << " +/- " << hZ_Eff_WS_DPhi->GetBinError(1) << std::endl;
  std::cout << std::endl;
  std::cout << "  W+jets MC ctrl region  : " << hZ_W_C_DPhi->GetBinContent(1) << " +/- " << hZ_W_C_DPhi->GetBinError(1) << std::endl;
  std::cout << "  Background ctrl region : " << hZ_BG_WC_DPhi->GetBinContent(1) << " +/- " << hZ_BG_WC_DPhi->GetBinError(1) << std::endl;
  std::cout << "  Data ctrl region       : " << hZ_Data_WC_DPhi->GetBinContent(1) << " +/- " << hZ_Data_WC_DPhi->GetBinError(1) << std::endl;
  std::cout << "  Data - BG              : " << hZ_Est_WC_DPhi->GetBinContent(1) << " +/- " << hZ_Est_WC_DPhi->GetBinError(1) << std::endl;
  std::cout << std::endl;
  std::cout << "  Predicted Zmumu        : " << hZ_Est_WS_DPhi->GetBinContent(1) << " +/- " << hZ_Est_WS_DPhi->GetBinError(1) << std::endl;
  std::cout << "  Observed Zmumu         : " << hZ_Est_ZC_DPhi->GetBinContent(1) << " +/- " << hZ_Est_ZC_DPhi->GetBinError(1) << std::endl;
  std::cout << std::endl;
  std::cout << "dphi > 1.0 dphi < 1.8" << std::endl;
  std::cout << std::endl;
  std::cout << "  eps_s_vbf by histogram : " << hZ_DY_EffVBFSMe1->GetBinContent(1) << " +/- " << hZ_DY_EffVBFSMe1->GetBinError(1) << std::endl;
  std::cout << "  eps_c_vbf by histogram : " << hZ_W_EffVBFCMe1->GetBinContent(1) << " +/- " << hZ_W_EffVBFCMe1->GetBinError(1) << std::endl;
  std::cout << "  ratio_vbf by histogram : " << hZ_W_RatioVBFMe1->GetBinContent(1) << " +/- " << hZ_W_RatioVBFMe1->GetBinError(1) << std::endl;
  std::cout << "  total eff by histogram : " << hZ_Eff_WS_DPhi->GetBinContent(2) << " +/- " << hZ_Eff_WS_DPhi->GetBinError(2) << std::endl;
  std::cout << std::endl;
  std::cout << "  W+jets MC ctrl region  : " << hZ_W_C_DPhi->GetBinContent(2) << " +/- " << hZ_W_C_DPhi->GetBinError(2) << std::endl;
  std::cout << "  Background ctrl region : " << hZ_BG_WC_DPhi->GetBinContent(2) << " +/- " << hZ_BG_WC_DPhi->GetBinError(2) << std::endl;
  std::cout << "  Data ctrl region       : " << hZ_Data_WC_DPhi->GetBinContent(2) << " +/- " << hZ_Data_WC_DPhi->GetBinError(2) << std::endl;
  std::cout << "  Data - BG     	 : " << hZ_Est_WC_DPhi->GetBinContent(2) << " +/- " << hZ_Est_WC_DPhi->GetBinError(2) << std::endl;
  std::cout << std::endl;
  std::cout << "  Predicted Zmumu        : " << hZ_Est_WS_DPhi->GetBinContent(2) << " +/- " << hZ_Est_WS_DPhi->GetBinError(2) << std::endl;
  std::cout << "  Observed Zmumu         : " << hZ_Est_ZC_DPhi->GetBinContent(2) << " +/- " << hZ_Est_ZC_DPhi->GetBinError(2) << std::endl;
  std::cout << std::endl;
  std::cout << "dphi > 1.8 dphi < 2.6" << std::endl;
  std::cout << std::endl;
  std::cout << "  eps_s_vbf by histogram : " << hZ_DY_EffVBFSMe2->GetBinContent(1) << " +/- " << hZ_DY_EffVBFSMe2->GetBinError(1) << std::endl;
  std::cout << "  eps_c_vbf by histogram : " << hZ_W_EffVBFCMe2->GetBinContent(1) << " +/- " << hZ_W_EffVBFCMe2->GetBinError(1) << std::endl;
  std::cout << "  ratio_vbf by histogram : " << hZ_W_RatioVBFMe2->GetBinContent(1) << " +/- " << hZ_W_RatioVBFMe2->GetBinError(1) << std::endl;
  std::cout << "  total eff by histogram : " << hZ_Eff_WS_DPhi->GetBinContent(3) << " +/- " << hZ_Eff_WS_DPhi->GetBinError(3) << std::endl;
  std::cout << std::endl;
  std::cout << "  W+jets MC ctrl region  : " << hZ_W_C_DPhi->GetBinContent(3) << " +/- " << hZ_W_C_DPhi->GetBinError(3) << std::endl;
  std::cout << "  Background ctrl region : " << hZ_BG_WC_DPhi->GetBinContent(3) << " +/- " << hZ_BG_WC_DPhi->GetBinError(3) << std::endl;
  std::cout << "  Data ctrl region       : " << hZ_Data_WC_DPhi->GetBinContent(3) << " +/- " << hZ_Data_WC_DPhi->GetBinError(3) << std::endl;
  std::cout << "  Data - BG       	 : " << hZ_Est_WC_DPhi->GetBinContent(3) << " +/- " << hZ_Est_WC_DPhi->GetBinError(3) << std::endl;
  std::cout << std::endl;
  std::cout << "  Predicted Zmumu        : " << hZ_Est_WS_DPhi->GetBinContent(3) << " +/- " << hZ_Est_WS_DPhi->GetBinError(3) << std::endl;
  std::cout << "  Observed Zmumu       	 : " << hZ_Est_ZC_DPhi->GetBinContent(3) << " +/- " << hZ_Est_ZC_DPhi->GetBinError(3) << std::endl;
  std::cout << std::endl;
  std::cout << "dphi > 2.6" << std::endl;
  std::cout << std::endl;
  std::cout << "  eps_s_vbf by histogram : " << hZ_DY_EffVBFSHi->GetBinContent(1) << " +/- " << hZ_DY_EffVBFSHi->GetBinError(1) << std::endl;
  std::cout << "  eps_c_vbf by histogram : " << hZ_W_EffVBFCHi->GetBinContent(1) << " +/- " << hZ_W_EffVBFCHi->GetBinError(1) << std::endl;
  std::cout << "  ratio_vbf by histogram : " << hZ_W_RatioVBFHi->GetBinContent(1) << " +/- " << hZ_W_RatioVBFHi->GetBinError(1) << std::endl;
  std::cout << "  total eff by histogram : " << hZ_Eff_WS_DPhi->GetBinContent(4) << " +/- " << hZ_Eff_WS_DPhi->GetBinError(4) << std::endl;
  std::cout << std::endl;
  std::cout << "  W+jets MC ctrl region  : " << hZ_W_C_DPhi->GetBinContent(4) << " +/- " << hZ_W_C_DPhi->GetBinError(4) << std::endl;
  std::cout << "  Background ctrl region : " << hZ_BG_WC_DPhi->GetBinContent(4) << " +/- " << hZ_BG_WC_DPhi->GetBinError(4) << std::endl;
  std::cout << "  Data ctrl region       : " << hZ_Data_WC_DPhi->GetBinContent(4) << " +/- " << hZ_Data_WC_DPhi->GetBinError(4) << std::endl;
  std::cout << "  Data - BG region       : " << hZ_Est_WC_DPhi->GetBinContent(4) << " +/- " << hZ_Est_WC_DPhi->GetBinError(4) << std::endl;
  std::cout << std::endl;
  std::cout << "  Predicted Zmumu        : " << hZ_Est_WS_DPhi->GetBinContent(4) << " +/- " << hZ_Est_WS_DPhi->GetBinError(4) << std::endl;
  std::cout << "  Observed Zmumu         : " << hZ_Est_ZC_DPhi->GetBinContent(4) << " +/- " << hZ_Est_ZC_DPhi->GetBinError(4) << std::endl;
  std::cout << std::endl;
  std::cout << "#####################################################################################" << std::endl;
  std::cout << std::endl;

  // draw control plots
  std::string pdfName;

  gStyle->SetOptStat(0);
  gStyle->SetOptFit(111111111);

  double x1[4]  = {0.5, 1.4, 2.2, 2.6 + (TMath::Pi()-2.6)/2};
  double ex1[4] = {0.5, 0.4, 0.4, (TMath::Pi()-2.6)/2};
  double y1[4],ey1[4],y2[4],ey2[4],y3[4],ey3[4];
  double diff[4],ediff[4];
  double frac[4],efrac[4];
  double y_syst[4],e_syst[4];

  for(int i=0; i<4; ++i) {
        y1[i]  = hZ_Est_WS_DPhi->GetBinContent(i+1);     //Prediction
        ey1[i] = hZ_Est_WS_DPhi->GetBinError(i+1);
        y2[i]  = hZ_Est_ZC_DPhi->GetBinContent(i+1);     //Observation
        ey2[i] = hZ_Est_ZC_DPhi->GetBinError(i+1); 
	y3[i]  = hZ_DY_C_DPhi->GetBinContent(i+1);       //MC Prediction
        ey3[i] = hZ_DY_C_DPhi->GetBinError(i+1);

	diff[i]  = y1[i]-y2[i];
	ediff[i] = sqrt(ey1[i]*ey1[i] + ey2[i]*ey2[i]);
        y_syst[i] = 0.;
        e_syst[i] = 0.21;

	if(y1[i] > 0) frac[i]  = (y1[i]-y2[i])/y2[i];
	efrac[i] = (y1[i]/y2[i])*sqrt(pow(ey1[i]/y1[i],2) + pow(ey2[i]/y2[i],2));
  }

  TGraphErrors *graph1 = new TGraphErrors(4,x1,y1,ex1,ey1);
  TGraphErrors *graph2 = new TGraphErrors(4,x1,y2,ex1,ey2);
  TGraphErrors *graph6 = new TGraphErrors(4,x1,y3,ex1,ey3);
  TGraphErrors *graph3 = new TGraphErrors(4,x1,diff,ex1,ediff);
  TGraphErrors *graph4 = new TGraphErrors(4,x1,frac,ex1,efrac);
  TGraphErrors *graph5 = new TGraphErrors(4,x1,y_syst,ex1,e_syst);
  TH1D *h = new TH1D("h", "", 1, 0, TMath::Pi());

  TCanvas canvas;
  canvas.SetCanvasSize(canvas.GetWindowWidth(), 1.2*canvas.GetWindowHeight());

  graph1->SetTitle("");
  graph1->SetMarkerStyle(20);
  graph1->SetMarkerSize(0.9);
  graph1->SetLineColor(kRed);
  graph1->SetMarkerColor(kRed);  
  graph1->GetXaxis()->SetTitle("#Delta #phi_{jj}");
  graph1->GetXaxis()->SetRangeUser(0,TMath::Pi());
  graph1->GetYaxis()->SetTitle("N(Z#rightarrow #mu#mu)");
  graph1->GetYaxis()->SetRangeUser(0,45);
  graph1->Draw("AP");  
  graph2->SetMarkerStyle(20);
  graph2->SetMarkerSize(0.9);
  graph2->SetLineColor(kBlue);
  graph2->SetMarkerColor(kBlue);
  graph2->Draw("P same");
  graph6->SetMarkerStyle(20);
  graph6->SetMarkerSize(0.9);
  graph6->SetLineColor(kViolet);
  graph6->SetMarkerColor(kViolet);
  graph6->Draw("P same");

  TLegend leg(0.12,0.67,0.37,0.88);
  leg.SetBorderSize(0);
  leg.SetFillColor(0);
  leg.AddEntry(graph1,"predicted (data)","P");
  leg.AddEntry(graph2,"observed (data)","P");
  leg.AddEntry(graph6,"predicted (MC)","P");
  leg.Draw();

  pdfName= oDir + std::string("/Zmumu_num.pdf");
  canvas.Print(pdfName.c_str());

  h->GetXaxis()->SetTitle("#Delta #phi_{jj}");
  h->GetYaxis()->SetTitle("Predicted - Observed");
  h->GetYaxis()->SetRangeUser(-20,20);
  h->SetLineColor(kBlue);
  h->Draw();
  graph3->SetMarkerStyle(20);
  graph3->SetMarkerSize(0.9);
  graph3->SetMarkerColor(kGreen-2);
  TF1 *f1 = new TF1("f1","pol0",0,TMath::Pi());
  graph3->Fit("f1","R");
  h->Draw();
  graph3->Draw("SAMEP");

  pdfName= oDir + std::string("/Zmumu_diff.pdf");
  canvas.Print(pdfName.c_str());

  h->GetXaxis()->SetTitle("#Delta #phi_{jj}");
  h->GetYaxis()->SetTitle("#frac{Predicted - Observed}{Observed}");
  h->GetYaxis()->SetTitleOffset(1.2);
  h->GetYaxis()->SetRangeUser(-2,2);
  h->SetLineColor(kBlue);
  h->SetLineWidth(2);
  h->Draw();
  graph5->SetLineColor(kGray+2);
  graph5->SetLineWidth(0);
  graph5->SetFillColor(kGray+2);
  graph5->SetFillStyle(3002);
  graph4->SetMarkerStyle(20);
  graph4->SetMarkerSize(1.2);
  graph4->SetMarkerColor(kGreen-2);
  graph4->Fit("f1","R");
  h->Draw();
  graph5->Draw("2 same");
  graph4->Draw("P same");

  TLegend leg2(0.12,0.67,0.40,0.87);
  leg2.SetBorderSize(0);
  leg2.SetFillColor(0);
  leg2.AddEntry(f1,"pol0 fit (0 < #Delta #phi_{jj} < #pi)","l");
  leg2.AddEntry(graph5,"Systematic error","f");
  leg2.Draw();
  pdfName= oDir + std::string("/Zmumu_frac.pdf");
  canvas.Print(pdfName.c_str());

  //store histograms
  ofile->cd();
  hZ_DY_C_DPhi->Write("",TObject::kOverwrite);
  hZ_BG_ZC_DPhi->Write("",TObject::kOverwrite);
  hZ_Data_ZC_DPhi->Write("",TObject::kOverwrite);
  hZ_W_C_DPhi->Write("",TObject::kOverwrite);
  hZ_BG_WC_DPhi->Write("",TObject::kOverwrite);
  hZ_Data_WC_DPhi->Write("",TObject::kOverwrite);
  hZ_Est_ZC_DPhi->Write("",TObject::kOverwrite);
  hZ_Est_WC_DPhi->Write("",TObject::kOverwrite);
  hZ_Est_WS_DPhi->Write("",TObject::kOverwrite);
  hZ_Eff_WS_DPhi->Write("",TObject::kOverwrite);
  hZ_DY_EffMuMu_D->Write("",TObject::kOverwrite);
  hZ_DY_EffMuMu_N->Write("",TObject::kOverwrite);
  hZ_W_EffMu_D->Write("",TObject::kOverwrite);
  hZ_W_EffMu_N->Write("",TObject::kOverwrite);
  hZ_DY_EffVBFS_D->Write("",TObject::kOverwrite);
  hZ_DY_EffVBFS_NLo->Write("",TObject::kOverwrite);
  hZ_DY_EffVBFS_NMe1->Write("",TObject::kOverwrite);
  hZ_DY_EffVBFS_NMe2->Write("",TObject::kOverwrite);
  hZ_DY_EffVBFS_NHi->Write("",TObject::kOverwrite);
  hZ_W_EffVBFC_D->Write("",TObject::kOverwrite);
  hZ_W_EffVBFC_NLo->Write("",TObject::kOverwrite);
  hZ_W_EffVBFC_NMe1->Write("",TObject::kOverwrite);
  hZ_W_EffVBFC_NMe2->Write("",TObject::kOverwrite);
  hZ_W_EffVBFC_NHi->Write("",TObject::kOverwrite);
  hZ_W_EffMu->Write("",TObject::kOverwrite);
  //hZ_DY_EffVBFS->Write("",TObject::kOverwrite);
  //hZ_W_EffVBFC->Write("",TObject::kOverwrite);
  //hZ_W_RatioVBF->Write("",TObject::kOverwrite);
  //hZ_W_TotalEff->Write("",TObject::kOverwrite);
 
  ofile->Close();    

}