Ejemplo n.º 1
0
Archivo: SSTop.C Proyecto: fgolf/pac
void PrintSystematics(const std::string& filename, const unsigned int sr_num, const float scale_den = 1.0)
{
    const std::string sr = GetSRLabel(static_cast<ss::SignalRegion::value_type>(sr_num)); 

    // acceptance and systematic plots
    rt::TH1Container hc(filename);

    const float den = rt::Integral(hc[sr+"nGenerated"])*(scale_den);
    value_t num     = IntegralAndError(hc[sr+"nPassing"]);
    value_t eff(num.value/den, num.value/den * (rt::Integral(hc[sr+"effErrStat"])/num.value));
/*     eff.error       = eff.value * (rt::Integral(hc[sr+"effErrStat"])/num.value); */
/*     eff.error       = sqrt(eff.value*(1.0 - eff.value)/den); */
    value_t eff_per = eff;
    eff_per.value   *= 100;
    eff_per.error   *= 100;

    // systematics (percentage) 
    const float jer     = GetSyst(rt::Integral(hc[sr+"nJER"      ]), num.value);
    const float jes     = GetSyst(rt::Integral(hc[sr+"nJESUP"    ]), rt::Integral(hc[sr+"nJESDN"    ]), num.value);
    const float jes_up  = GetSyst(rt::Integral(hc[sr+"nJESUP"    ]), num.value);
    const float jes_dn  = GetSyst(rt::Integral(hc[sr+"nJESDN"    ]), num.value);
    const float beff    = GetSyst(rt::Integral(hc[sr+"nBTAUP"    ]), rt::Integral(hc[sr+"nBTADN"    ]), num.value);
    const float beff_up = GetSyst(rt::Integral(hc[sr+"nBTAUP"    ]), num.value);
    const float beff_dn = GetSyst(rt::Integral(hc[sr+"nBTADN"    ]), num.value);
    const float met     = GetSyst(rt::Integral(hc[sr+"nMETUP"    ]), rt::Integral(hc[sr+"nMETDN"    ]), num.value);
    const float met_up  = GetSyst(rt::Integral(hc[sr+"nMETUP"    ]), num.value);
    const float met_dn  = GetSyst(rt::Integral(hc[sr+"nMETDN"    ]), num.value);
    const float lep     = GetSyst(rt::Integral(hc[sr+"nLepEffUP" ]), rt::Integral(hc[sr+"nLepEffDN" ]), num.value);
    const float trig    = GetSyst(rt::Integral(hc[sr+"nTrigEffUP"]), rt::Integral(hc[sr+"nTrigEffDN"]), num.value);
    const float lumi    = 4.4;
    const float pdf     = 2.0;
    const float stat    = eff.error;
    const float total   = sqrt(pow(jes,2) + pow(jer,2) + pow(beff,2) + pow(met,2) + pow(lep,2) + pow(trig,2) + pow(lumi,2) + pow(pdf,2) + pow(stat,2));

    CTable t;
    t.useTitle();
    t.setTitle(Form("TTBar Acceptance SR %d", sr_num));
    t.setTable() (                    "value"  , "rel unc (1 + dx)"  )
        ("# generated"    , int(den)           , "NA"               )
        ("# passing"      , pm(num, "1.0f")    , "NA"               )
        ("acceptance"     , pm(eff, "1.5f")    , "NA"               )
        ("acceptance"     , pm(eff    , "1.2e"), "NA"               )
        ("acceptance (%)" , pm(eff_per, "1.5f"), "NA"               )
        ("JES        (%)" , jes                , 1.0 + 0.01*jes     )
        ("JES+       (%)" , jes_up             , 1.0 + 0.01*jes_up  )
        ("JES-       (%)" , jes_dn             , 1.0 + 0.01*jes_dn  )
        ("JER        (%)" , jer                , 1.0 + 0.01*jer     )
        ("Btag       (%)" , beff               , 1.0 + 0.01*beff    )
        ("Btag+      (%)" , beff_up            , 1.0 + 0.01*beff_up )
        ("Btag-      (%)" , beff_dn            , 1.0 + 0.01*beff_dn )
        ("MET        (%)" , met                , 1.0 + 0.01*met     )
        ("MET+       (%)" , met_up             , 1.0 + 0.01*met_up  )
        ("MET-       (%)" , met_dn             , 1.0 + 0.01*met_dn  )
        ("Trig       (%)" , trig               , 1.0 + 0.01*trig    )
        ("Lep        (%)" , lep                , 1.0 + 0.01*lep     )
        ("Lumi       (%)" , lumi               , 1.0 + 0.01*lumi    )
        ("Pdf        (%)" , pdf                , 1.0 + 0.01*pdf     )
        ("Stat       (%)" , Form("%1.2e", stat), Form("%1.2e", stat))
        ("Total      (%)" , total              , 1.0 + 0.01*total   )
        ;
    t.print();
    cout << endl;
}
Ejemplo n.º 2
0
void dataDrivenFromCR(TFile* fdata, TFile* fmc, TFile* fout, TString ddtype, TString gentype) {
  // Additional hists to consider: dataStats, MCstats, impurity

  TList* listOfDirs = fmc->GetListOfKeys();
  for (auto k : *listOfDirs) {
    TString srname = k->GetName();
    if (!srname.Contains("sr")) continue;
    if (srname.Contains("base") || srname.Contains("incl") || srname.Contains("sb")) continue;
    if (ddtype == "cr0b" && (srname.EndsWith("2") || srname.EndsWith("3"))) continue;

    TString crname = srname;
    crname.ReplaceAll("sr", ddtype);

    TString hname_data_CR = crname + "/h_metbins";
    TString hname_MC_SR = srname + "/h_metbins" + gentype;
    TString hname_MC_CR = crname + "/h_metbins";

    auto outdir = (TDirectory*) fout->mkdir(srname);

    auto hist_data_CR = (TH1D*) fdata->Get(hname_data_CR);
    auto hist_MC_CR = (TH1D*) fmc->Get(hname_MC_CR);
    auto hist_MC_SR = (TH1D*) fmc->Get(hname_MC_SR);

    if (!fmc->Get(hname_MC_CR)) {
      cout << "Couldn't find yield hist for " << hist_MC_CR << " in " << fmc->GetName() << "!!" << endl;
      cout << "This should not happend! Can not use data driven on this region! Use MC yields directly use TF from other SR!" << endl;
      continue;  // <-- actions to be added
    }
    if (!fmc->Get(hname_MC_SR)) {
      cout << "Couldn't find yield hist for " << hist_MC_SR << " in " << fmc->GetName() << ". Cannot define TF!" << endl;
      continue;  // <-- actions to be added
    }
    if (!fdata->Get(hname_data_CR)) {
      cout << "Couldn't find yield hist for " << hist_data_CR << " in " << fdata->GetName() << ". Please use yield from MC!" << endl;
      continue;  // <-- actions to be added
    }

    int lastbin = hist_data_CR->GetNbinsX();
    int extr_start_bin = lastbin; // the bin to start extrapolation, if == lastbin means no MET extrapolation is needed

    auto combineYieldsInExtrBins = [&](TH1D* hist) {
      double err = 0;
      double ylds = hist->IntegralAndError(extr_start_bin, -1, err);
      for (int ibin = extr_start_bin; ibin <= lastbin; ++ibin) {
        hist->SetBinContent(ibin, ylds);
        hist->SetBinError(ibin, err);
      }
    };

    if (useMetExtrapolation) {
      double yldCR(0.0), err(0.0);
      for (; extr_start_bin > 1; --extr_start_bin) {
        yldCR = hist_MC_CR->IntegralAndError(extr_start_bin, -1, err);
        double TFval = hist_MC_SR->GetBinContent(extr_start_bin) / yldCR;
        if (yldCR > extr_threshold && (TFval < extr_TFcap)) break;
      }
      // if (gentype == "_2lep" && (srname == "srE2" || srname == "srG2")) extr_start_bin = lastbin; // temporary hack for 2016
      // if (gentype == "_2lep" && (srname == "srH")) extr_start_bin = 1; // temporary hack for 2016
      if (extr_start_bin != lastbin) {
        cout << "Doing MET extrapolation for  " << crname << "  from bin " << lastbin << " (last bin) to bin " << extr_start_bin << "!" << endl;
        hist_data_CR->Clone("h_datayields_CR_raw")->Write();
        combineYieldsInExtrBins(hist_data_CR);
      }
    }

    TH1D* centralHist;

    auto crdir = (TDirectoryFile*) fmc->Get(crname);
    for (auto h : *(crdir->GetListOfKeys())) {
      TString hname = h->GetName();
      if (!hname.BeginsWith("h_metbins")) continue;
      // hardcode genclass skipping for now
      if (hname.Contains("_2lep") || hname.Contains("_1lep") || hname.Contains("_Znunu") || hname.Contains("_unclass")) continue;

      TString hnameSR = hname;
      hnameSR.ReplaceAll("h_metbins", "h_metbins" + gentype);

      // Not using fraction
      auto hist_MC_CR = (TH1D*) crdir->Get(hname)->Clone(hname+"_cr");
      auto hist_MC_SR = (TH1D*) fmc->Get(srname + "/" + hnameSR);

      if (!hist_MC_SR) {
        if (!hnameSR.Contains("cr2lTriggerSF"))
          cout << "Couldn't find yield hist for " << (srname + "/" + hnameSR) << " in " << fmc->GetName() << ". Use centralHist!" << endl;
        hist_MC_SR = (TH1D*) fmc->Get(hname_MC_SR)->Clone(hnameSR);
      }

      auto alphaHist = (TH1D*) hist_MC_SR->Clone(hname+"_alpha");
      if (useMetExtrapolation && extr_start_bin != lastbin) {
        // To take the MET distribution from the CR
        double cerr_SR = 0;
        double cyld_SR = alphaHist->IntegralAndError(extr_start_bin, -1, cerr_SR);
        double cyld_CR = hist_MC_CR->Integral(extr_start_bin, -1);
        for (int ibin = extr_start_bin; ibin <= lastbin; ++ibin) {
          double metfrac = hist_MC_CR->GetBinContent(ibin) / cyld_CR;
          alphaHist->SetBinContent(ibin, metfrac * cyld_SR);
          alphaHist->SetBinError(ibin, metfrac * cerr_SR);
        }
        combineYieldsInExtrBins(hist_MC_CR);
      }
      alphaHist->Divide(hist_MC_CR);

      for (int i = 1; i <= alphaHist->GetNbinsX(); ++i) {
        // zero out negative yields
        if (alphaHist->GetBinContent(i) < 0) {
          alphaHist->SetBinContent(i, 0);
          alphaHist->SetBinError(i, 0);
        }
      }

      outdir->cd();
      TH1D* hout = (TH1D*) alphaHist->Clone(hname);
      hout->Multiply(hist_data_CR);
      hout->Write();

      if (yearSeparateSyst && (hname.EndsWith("Up") || hname.EndsWith("Dn"))) {
        for (int i = 1; i < 4; ++i) {

          auto hcen_MC_CR = (TH1D*) fbkgs[i]->Get(crname+"/h_metbins");
          auto hcen_MC_SR = (TH1D*) fbkgs[i]->Get(srname+"/h_metbins"+gentype);
          auto hsys_MC_CR = (TH1D*) fbkgs[i]->Get(crname+"/"+hname);
          auto hsys_MC_SR = (TH1D*) fbkgs[i]->Get(srname+"/"+hnameSR);

          if (!hist_MC_SR) {
            if (!hnameSR.Contains("cr2lTriggerSF"))
              cout << "Couldn't find yield hist for " << (srname + "/" + hnameSR) << " in " << fmc->GetName() << ". Use centralHist!" << endl;
            hist_MC_SR = (TH1D*) fmc->Get(hname_MC_SR)->Clone(hnameSR);
          }

          auto alphaHist_yi = (TH1D*) fmc->Get(hname_MC_SR)->Clone(TString(hname).Insert(hname.Length()-2, Form("%d", 15+i)));
          auto h_MC_CR_yi = (TH1D*) fmc->Get(hname_MC_CR)->Clone(Form("%s_den_%d", hname.Data(), 15+i));
          if (hcen_MC_SR) alphaHist_yi->Add(hcen_MC_SR, -1);
          if (hsys_MC_SR) alphaHist_yi->Add(hsys_MC_SR);
          if (hcen_MC_CR) h_MC_CR_yi->Add(hcen_MC_CR, -1);
          if (hsys_MC_CR) h_MC_CR_yi->Add(hsys_MC_CR);

          if (useMetExtrapolation && extr_start_bin != lastbin) {
            // To take the MET distribution from the CR
            double cerr_SR = 0;
            double cyld_SR = alphaHist_yi->IntegralAndError(extr_start_bin, -1, cerr_SR);
            double cyld_CR = h_MC_CR_yi->Integral(extr_start_bin, -1);
            for (int ibin = extr_start_bin; ibin <= lastbin; ++ibin) {
              double metfrac = h_MC_CR_yi->GetBinContent(ibin) / cyld_CR;
              alphaHist_yi->SetBinContent(ibin, metfrac * cyld_SR);
              alphaHist_yi->SetBinError(ibin, metfrac * cerr_SR);
            }
            combineYieldsInExtrBins(h_MC_CR_yi);
          }
          alphaHist_yi->Divide(h_MC_CR_yi);

          for (int i = 1; i <= alphaHist_yi->GetNbinsX(); ++i) {
            // zero out negative yields
            if (alphaHist_yi->GetBinContent(i) < 0) {
              alphaHist_yi->SetBinContent(i, 0);
              alphaHist_yi->SetBinError(i, 0);
            }
          }
          outdir->cd();
          alphaHist_yi->Multiply(hist_data_CR);
          alphaHist_yi->Write();
        }
      }

      if (hname.EndsWith("h_metbins")) {
        centralHist = hout;
        // Store the central alpha hist and extr_start_bin for signal contamination
        alphaHist->Write("h_alphaHist");
        if (useMetExtrapolation && extr_start_bin < lastbin) {
          TH1D* h_extrstart = new TH1D("h_extrstart", "MET extrapolation start bin", 1, 0, 1);
          h_extrstart->SetBinContent(1, extr_start_bin);
          h_extrstart->Write();
        }
        if (doCRPurityError) {
          auto hist_MC_CR_pure = (TH1D*) fmc->Get(hname_MC_CR + gentype);
          auto hout_purityUp = (TH1D*) hout->Clone(hname+"_CRpurityUp");
          auto hout_purityDn = (TH1D*) hout->Clone(hname+"_CRpurityDn");
          if (useMetExtrapolation && extr_start_bin < lastbin)
            combineYieldsInExtrBins(hist_MC_CR_pure);
          for (int ibin = 1; ibin <= lastbin; ++ibin) {
            double crpurityerr = 0.5 * (hist_MC_CR->GetBinContent(ibin) - hist_MC_CR_pure->GetBinContent(ibin)) / hist_MC_CR->GetBinContent(ibin);
            hout_purityUp->SetBinContent(ibin, hout->GetBinContent(ibin) / ( 1 - crpurityerr));
            hout_purityDn->SetBinContent(ibin, hout->GetBinContent(ibin) / ( 1 + crpurityerr));
          }
          hout_purityUp->Write();
          hout_purityDn->Write();

          auto purityHist = (TH1D*) hist_MC_CR_pure->Clone("h_CRpurity");
          purityHist->Divide(hist_MC_CR_pure, hist_MC_CR, 1, 1, "B");
          purityHist->Write();
        }
      }
    }

    // Create alphaHist for dataStats
    auto h_dataStats = (TH1D*) centralHist->Clone("h_metbins_dataStats");
    auto h_MCStats = (TH1D*) centralHist->Clone("h_metbins_MCStats");
    for (int ibin = 1; ibin <= extr_start_bin; ++ibin) {
      // If not doing met extrapolation, extr_start_bin will equal to lastbin
      double data_error_thisbin = (hist_data_CR->GetBinContent(ibin) < 0.01)? 0 : hist_data_CR->GetBinError(ibin) / hist_data_CR->GetBinContent(ibin);
      h_dataStats->SetBinError(ibin, data_error_thisbin * h_dataStats->GetBinContent(ibin));

      double MC_SR_error_thisbin = (hist_MC_SR->GetBinContent(ibin) < 1e-5)? 0 : hist_MC_SR->GetBinError(ibin) / hist_MC_SR->GetBinContent(ibin);
      double MC_CR_error_thisbin = (hist_MC_CR->GetBinContent(ibin) < 1e-5)? 0 : hist_MC_CR->GetBinError(ibin) / hist_MC_CR->GetBinContent(ibin);
      double MC_error_thisbin = sqrt(MC_SR_error_thisbin*MC_SR_error_thisbin + MC_CR_error_thisbin*MC_CR_error_thisbin);

      h_MCStats->SetBinError(ibin, MC_error_thisbin * h_MCStats->GetBinContent(ibin));
    }
    for (int ibin = extr_start_bin+1; ibin <= lastbin; ++ibin) {
      // If doing met extrapolation, the bins following extr_start_bin for data and MC CR will be set to have 0 stat error
      // TODO: verify that this is the right thing to do
      h_dataStats->SetBinError(ibin, 0);
      double MC_SR_error_thisbin = (hist_MC_SR->GetBinContent(ibin) < 1e-5)? 0 : hist_MC_SR->GetBinError(ibin) / hist_MC_SR->GetBinContent(ibin);
      h_MCStats->SetBinError(ibin, MC_SR_error_thisbin * h_MCStats->GetBinContent(ibin));
    }

    h_dataStats->Write();
    h_MCStats->Write();

    hist_data_CR->Clone("h_datayields_CR")->Write();
    hist_MC_CR->Clone("h_MCyields_CR")->Write();
    hist_MC_SR->Clone("h_MCyields_SR")->Write();
  }
}