Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion data/jsonpog-integration
Submodule jsonpog-integration updated from ea5e2a to 773fd6
5 changes: 5 additions & 0 deletions include/reweighting.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
54 changes: 54 additions & 0 deletions src/reweighting.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Comment on lines +76 to +79
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File and histogram retrieval lack null pointer checks. If files fail to open or histograms don't exist, subsequent operations will cause segmentation faults. Add validation after each TFile::Open and Get call.

Copilot uses AI. Check for mistakes.
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;
Comment on lines +80 to +87
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Histograms are detached from their files with SetDirectory(0) but never deleted, causing a memory leak. The histograms captured by the lambda will persist for the lifetime of the DataFrame. Consider using std::unique_ptr or document that this is intentional for the lambda's lifetime.

Copilot uses AI. Check for mistakes.

// Define lambda for event weight
auto puweightlambda = [datahist, mchist](const float truePUMean) {
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lambda function name 'puweightlambda' is redundant since it's clearly a lambda. Consider renaming to 'calculatePUWeight' or 'getPUWeight' for clarity.

Copilot uses AI. Check for mistakes.
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;
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic number 5.0 for the maximum weight ratio lacks explanation. Add a comment explaining why this cap is necessary or define it as a named constant.

Copilot uses AI. Check for mistakes.
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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 */
Loading