diff --git a/data/jsonpog-integration b/data/jsonpog-integration index ea5e2a97..773fd624 160000 --- a/data/jsonpog-integration +++ b/data/jsonpog-integration @@ -1 +1 @@ -Subproject commit ea5e2a97c3cba402658e58156b4f076075c1afab +Subproject commit 773fd624aa67c3f742c85530e1a3979bce78b179 diff --git a/include/reweighting.hxx b/include/reweighting.hxx index ec3f4290..9aea6e54 100644 --- a/include/reweighting.hxx +++ b/include/reweighting.hxx @@ -10,6 +10,11 @@ Pileup(ROOT::RDF::RNode df, const std::string &outputname, const std::string &true_pileup_number, const std::string &corr_file, const std::string &corr_name, const std::string &variation); +ROOT::RDF::RNode PUWeightROOT(ROOT::RDF::RNode df, const std::string &weightname, + const std::string &truePUMean, + const std::string &datafilename, + const std::string &mcfilename, + const std::string &histname); ROOT::RDF::RNode PartonShower(ROOT::RDF::RNode df, const std::string &outputname, const std::string &ps_weights, diff --git a/src/reweighting.cxx b/src/reweighting.cxx index 3777e53b..c399989f 100644 --- a/src/reweighting.cxx +++ b/src/reweighting.cxx @@ -56,6 +56,56 @@ Pileup(ROOT::RDF::RNode df, return df1; } +/** + * @brief Function used to read out pileup weights from root files + * + * @param df The input dataframe + * @param weightname name of the derived weight + * @param truePUMean name of the column containing the true PU mean of simulated + * events + * @param filename path to the rootfile + * @param histogramname name of the histogram stored in the rootfile + * @return a new dataframe containing the new column + */ +ROOT::RDF::RNode PUWeightROOT(ROOT::RDF::RNode df, const std::string &weightname, + const std::string &truePUMean, + const std::string &datafilename, + const std::string &mcfilename, + const std::string &histname) { + // Open files and get histograms + TFile* datafile = TFile::Open(datafilename.c_str(), "READ"); + TFile* mcfile = TFile::Open(mcfilename.c_str(), "READ"); + TH1D* datahist = (TH1D*)datafile->Get(histname.c_str()); + TH1D* mchist = (TH1D*)mcfile->Get(histname.c_str()); + datahist->SetDirectory(0); + mchist->SetDirectory(0); + datahist->Scale(1.0 / datahist->Integral()); + mchist->Scale(1.0 / mchist->Integral()); + datafile->Close(); + mcfile->Close(); + delete datafile; + delete mcfile; + + // Define lambda for event weight + auto puweightlambda = [datahist, mchist](const float truePUMean) { + int dataBin = datahist->GetXaxis()->FindBin(truePUMean); + int mcBin = mchist->GetXaxis()->FindBin(truePUMean); + float data = datahist->GetBinContent(dataBin); + float mc = mchist->GetBinContent(mcBin); + if (mc > 0.0) { + float ratio = data / mc; + if (ratio > 5.0) return 5.0f; + return ratio; + } else { + return 1.0f; + } + }; + + // Add new column to DataFrame + auto df1 = df.Define(weightname, puweightlambda, {truePUMean}); + return df1; +} + /** * @brief This function is used to evaluate the parton shower (PS) weight of an event. * The weights are stored in the nanoAOD files and defined as @@ -507,6 +557,9 @@ ZBosonPt(ROOT::RDF::RNode df, * @param argset additional arguments that are needed for the function * * @return a new dataframe containing the new column + * + * @note The function is intended for Run 2 analysis. In Run 3 Zpt corrections are + * handled through correctionlib, see the function below. */ ROOT::RDF::RNode ZPtMass(ROOT::RDF::RNode df, const std::string &outputname, @@ -542,6 +595,7 @@ ROOT::RDF::RNode ZPtMass(ROOT::RDF::RNode df, gen_boson + "_pt"); return df3; } + } // end namespace reweighting } // end namespace event #endif /* GUARD_REWEIGHTING_H */