Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TDF][VecOps] Add tutorial with a real analysis usecase #1795

Merged
merged 2 commits into from Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
87 changes: 87 additions & 0 deletions tutorials/dataframe/tdf017_vecOpsHEP.C
@@ -0,0 +1,87 @@
/// \file
/// \ingroup tutorial_tdataframe
/// \notebook -draw
/// This tutorial shows how VecOps can be used to slim down the programming
/// model typically adopted in HEP for analysis.
/// In this case we have a dataset containing the kinematic properties of
/// particles stored in individual arrays.
/// We want to plot the transverse momentum of these particles if the energy is
/// greater than 100.
///
/// \macro_code
///
/// \date March 2018
/// \author Danilo Piparo, Andr�e Vieira Silva

auto filename = gROOT->GetTutorialDir() + "/dataframe/tdf017_vecOpsHEP.root";
auto treename = "myDataset";
using doubles = ROOT::Experimental::VecOps::TVec<double>;
using TDF = ROOT::Experimental::TDataFrame;

void WithTTreeReader()
{
TFile f(filename);
TTreeReader tr(treename, &f);
TTreeReaderArray<double> px(tr, "px");
TTreeReaderArray<double> py(tr, "py");
TTreeReaderArray<double> E(tr, "E");

TH1F h("pt", "pt", 16, 0, 4);

while (tr.Next()) {
for (auto i=0U;i < px.GetSize(); ++i) {
if (E[i] > 100) h.Fill(sqrt(px[i]*px[i] + py[i]*py[i]));
}
}
h.DrawCopy();
}

void WithTDataFrame()
{
TDF f(treename, filename.Data());
auto CalcPt = [](doubles &px, doubles &py, doubles &E) {
doubles v;
for (auto i=0U;i < px.size(); ++i) {
if (E[i] > 100) {
v.emplace_back(sqrt(px[i]*px[i] + py[i]*py[i]));
}
}
return v;
};
f.Define("pt", CalcPt, {"px", "py", "E"})
.Histo1D<doubles>({"pt", "pt", 16, 0, 4}, "pt")->DrawCopy();
}

void WithTDataFrameVecOps()
{
TDF f(treename, filename.Data());
auto CalcPt = [](doubles &px, doubles &py, doubles &E) {
auto pt = sqrt(px*px + py*py);
return pt[E>100];
};
f.Define("good_pt", CalcPt, {"px", "py", "E"})
.Histo1D<doubles>({"pt", "pt", 16, 0, 4}, "good_pt")->DrawCopy();
}

void WithTDataFrameVecOpsJit()
{
TDF f(treename, filename.Data());
f.Define("good_pt", "sqrt(px*px + py*py)[E>100]")
.Histo1D({"pt", "pt", 16, 0, 4}, "good_pt")->DrawCopy();
}

void tdf017_vecOpsHEP()
{
// We plot four times the same quantity, the key is to look into the implementation
// of the functions above
auto c = new TCanvas();
c->Divide(2,2);
c->cd(1);
WithTTreeReader();
c->cd(2);
WithTDataFrame();
c->cd(3);
WithTDataFrameVecOps();
c->cd(4);
WithTDataFrameVecOpsJit();
}
40 changes: 40 additions & 0 deletions tutorials/dataframe/tdf017_vecOpsHEP.py
@@ -0,0 +1,40 @@
## \file
## \ingroup tutorial_tdataframe
## \notebook -draw
## This tutorial shows how VecOps can be used to slim down the programming
## model typically adopted in HEP for analysis.
## \macro_code
##
## \date March 2018
## \author Danilo Piparo, Andre Vieira Silva

import ROOT
from math import sqrt

filename = ROOT.gROOT.GetTutorialDir().Data() + "/dataframe/tdf017_vecOpsHEP.root"
treename = "myDataset"
TDF = ROOT.ROOT.Experimental.TDataFrame

def WithPyROOT():
f = ROOT.TFile(filename)
h = ROOT.TH1F("pt", "pt", 16, 0, 4)
for event in f.myDataset:
for E, px, py in zip(event.E, event.px, event.py):
if (E > 100):
h.Fill(sqrt(px*px + py*py))
h.DrawCopy()

def WithTDataFrameVecOpsJit():
f = TDF(treename, filename)
h = f.Define("good_pt", "sqrt(px*px + py*py)[E>100]")\
.Histo1D(("pt", "pt", 16, 0, 4), "good_pt")
h.DrawCopy()

## We plot twice the same quantity, the key is to look into the implementation
## of the functions above
c = ROOT.TCanvas()
c.Divide(2,1)
c.cd(1)
WithPyROOT()
c.cd(2)
WithTDataFrameVecOpsJit()
Binary file added tutorials/dataframe/tdf017_vecOpsHEP.root
Binary file not shown.