Skip to content

Instantly share code, notes, and snippets.

@pieterdavid
Last active April 7, 2024 19:16
Show Gist options
  • Save pieterdavid/a560e65658386d70a1720cb5afe4d3e9 to your computer and use it in GitHub Desktop.
Save pieterdavid/a560e65658386d70a1720cb5afe4d3e9 to your computer and use it in GitHub Desktop.
correctionlib RDataFrame examples
# Example commands to build the C++ example (df.cc):
# cmake $(correction config --cmake) .
# make
cmake_minimum_required(VERSION 3.11)
project(correctionlibexample LANGUAGES CXX)
find_package(correctionlib REQUIRED)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads)
find_package(ROOT REQUIRED COMPONENTS ROOTDataFrame)
add_executable(test_df df.cc)
target_link_libraries(test_df PRIVATE correctionlib ROOT::ROOTDataFrame)
#include "TFile.h"
#include "TCanvas.h"
#include "ROOT/RDataFrame.hxx"
#include "correction.h"
using correction::CorrectionSet;
using ROOT::VecOps::RVec;
int main(int argc, char** argv) {
auto f = TFile::Open("DY_M50_2016.root");
auto df = ROOT::RDataFrame(*(f->Get<TTree>("Events")));
auto cset = CorrectionSet::from_file("electron.json");
auto cset_2016preID = cset->at("UL-Electron-ID-SF");
auto df1 = df.Filter(
[] (unsigned int n, const RVec<float>& pt) { return n >= 1 && pt[0] > 10.; }, {"nElectron", "Electron_pt"}
).Define("leadElSF",
[cset_2016preID] (const RVec<float>& pt, const RVec<float>& eta) {
return cset_2016preID->evaluate({"2016preVFP", "sf", "wp90iso", std::abs(eta[0]), pt[0]});
}, {"Electron_pt", "Electron_eta"});
auto h = df1.Histo1D("leadElSF");
TCanvas cv{"c1"};
h->Draw();
cv.Update();
cv.SaveAs("df_compiled.png");
return 1;
}
import ROOT as gbl
import correctionlib
correctionlib.register_pyroot_binding()
f = gbl.TFile.Open("DY_M50_2016.root")
df = gbl.ROOT.RDataFrame(f.Get("Events"))
gbl.gInterpreter.Declare('auto csetEl = correction::CorrectionSet::from_file("electron.json");')
gbl.gInterpreter.Declare('auto csetEl_2016preID = csetEl->at("UL-Electron-ID-SF");')
df = df.Filter("nElectron >= 1 && Electron_pt[0] > 10.").Define(
"leadElSF",
('csetEl_2016preID->evaluate({"2016preVFP", "sf", "wp90iso", '
'std::abs(Electron_eta[0]), Electron_pt[0]})'))
h = df.Histo1D("leadElSF")
h.Draw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment