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());


}
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();    

}