{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Df017_VecOpsHEP\n",
"This tutorial shows how VecOps can be used to slim down the programming\n",
"model typically adopted in HEP for analysis.\n",
"In this case we have a dataset containing the kinematic properties of\n",
"particles stored in individual arrays.\n",
"We want to plot the transverse momentum of these particles if the energy is\n",
"greater than 100.\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Danilo Piparo, Andre Vieira Silva \n",
"This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Wednesday, November 06, 2019 at 11:01 PM."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"auto filename = gROOT->GetTutorialDir() + \"/dataframe/df017_vecOpsHEP.root\";\n",
"auto treename = \"myDataset\";\n",
"using doubles = ROOT::VecOps::RVec;\n",
"using RDF = ROOT::RDataFrame;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" A helper function is created: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%cpp -d\n",
"void WithTTreeReader()\n",
"{\n",
" TFile f(filename);\n",
" TTreeReader tr(treename, &f);\n",
" TTreeReaderArray px(tr, \"px\");\n",
" TTreeReaderArray py(tr, \"py\");\n",
" TTreeReaderArray E(tr, \"E\");\n",
"\n",
" TH1F h(\"pt\", \"pt\", 16, 0, 4);\n",
"\n",
" while (tr.Next()) {\n",
" for (auto i=0U;i < px.GetSize(); ++i) {\n",
" if (E[i] > 100) h.Fill(sqrt(px[i]*px[i] + py[i]*py[i]));\n",
" }\n",
" }\n",
" h.DrawCopy();\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" A helper function is created: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%cpp -d\n",
"void WithRDataFrame()\n",
"{\n",
" RDF f(treename, filename.Data());\n",
" auto CalcPt = [](doubles &px, doubles &py, doubles &E) {\n",
" doubles v;\n",
" for (auto i=0U;i < px.size(); ++i) {\n",
" if (E[i] > 100) {\n",
" v.emplace_back(sqrt(px[i]*px[i] + py[i]*py[i]));\n",
" }\n",
" }\n",
" return v;\n",
" };\n",
" f.Define(\"pt\", CalcPt, {\"px\", \"py\", \"E\"})\n",
" .Histo1D({\"pt\", \"pt\", 16, 0, 4}, \"pt\")->DrawCopy();\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" A helper function is created: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%cpp -d\n",
"void WithRDataFrameVecOps()\n",
"{\n",
" RDF f(treename, filename.Data());\n",
" auto CalcPt = [](doubles &px, doubles &py, doubles &E) {\n",
" auto pt = sqrt(px*px + py*py);\n",
" return pt[E>100];\n",
" };\n",
" f.Define(\"good_pt\", CalcPt, {\"px\", \"py\", \"E\"})\n",
" .Histo1D({\"pt\", \"pt\", 16, 0, 4}, \"good_pt\")->DrawCopy();\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" A helper function is created: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%cpp -d\n",
"void WithRDataFrameVecOpsJit()\n",
"{\n",
" RDF f(treename, filename.Data());\n",
" f.Define(\"good_pt\", \"sqrt(px*px + py*py)[E>100]\")\n",
" .Histo1D({\"pt\", \"pt\", 16, 0, 4}, \"good_pt\")->DrawCopy();\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We plot four times the same quantity, the key is to look into the implementation\n",
" of the functions above"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"auto c = new TCanvas();\n",
"c->Divide(2,2);\n",
"c->cd(1);\n",
"WithTTreeReader();\n",
"c->cd(2);\n",
"WithRDataFrame();\n",
"c->cd(3);\n",
"WithRDataFrameVecOps();\n",
"c->cd(4);\n",
"WithRDataFrameVecOpsJit();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%jsroot on\n",
"gROOT->GetListOfCanvases()->Draw()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "ROOT C++",
"language": "c++",
"name": "root"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".C",
"mimetype": " text/x-c++src",
"name": "c++"
}
},
"nbformat": 4,
"nbformat_minor": 2
}