{
"cells": [
{
"cell_type": "markdown",
"id": "ad381259",
"metadata": {},
"source": [
"# ntpl004_dimuon\n",
"Mini-Analysis on CMS OpenData with RDataFrame.\n",
"This tutorial illustrates that analyzing data with RDataFrame works the same\n",
"for both TTree data and RNTuple data. The RNTuple data are converted from the Events tree\n",
"in http://root.cern/files/NanoAOD_DoubleMuon_CMS2011OpenData.root\n",
"Based on RDataFrame's df102_NanoAODDimuonAnalysis.C\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** The ROOT Team \n",
"This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:14 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "897e4a64",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:14:48.302743Z",
"iopub.status.busy": "2026-05-19T20:14:48.302626Z",
"iopub.status.idle": "2026-05-19T20:14:48.312198Z",
"shell.execute_reply": "2026-05-19T20:14:48.311626Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \n",
"\n",
"#include \n",
"#include \n",
"#include \n",
"#include \n",
"\n",
"#include \n",
"#include \n",
"#include \n",
"#include \n",
"#include \n",
"#include \n",
"#include \n",
"\n",
"constexpr char const *kNTupleFileName = \"http://root.cern/files/tutorials/ntpl004_dimuon_v1.root\";\n",
"\n",
"using namespace ROOT::VecOps;"
]
},
{
"cell_type": "markdown",
"id": "ea407e18",
"metadata": {},
"source": [
"Use all available CPU cores"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f47693ae",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:14:48.313391Z",
"iopub.status.busy": "2026-05-19T20:14:48.313277Z",
"iopub.status.idle": "2026-05-19T20:14:48.924208Z",
"shell.execute_reply": "2026-05-19T20:14:48.923463Z"
}
},
"outputs": [],
"source": [
"ROOT::EnableImplicitMT();\n",
"\n",
"ROOT::RDataFrame df(\"Events\", kNTupleFileName);"
]
},
{
"cell_type": "markdown",
"id": "bacbf0a6",
"metadata": {},
"source": [
"The tutorial is identical to df102_NanoAODDimuonAnalysis except the use of RNTuple."
]
},
{
"cell_type": "markdown",
"id": "6e81bbc3",
"metadata": {},
"source": [
"For simplicity, select only events with exactly two muons and require opposite charge"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "410069f1",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:14:48.926182Z",
"iopub.status.busy": "2026-05-19T20:14:48.926046Z",
"iopub.status.idle": "2026-05-19T20:14:49.129898Z",
"shell.execute_reply": "2026-05-19T20:14:49.129170Z"
}
},
"outputs": [],
"source": [
"auto df_2mu = df.Filter(\"nMuon == 2\", \"Events with exactly two muons\");\n",
"auto df_os = df_2mu.Filter(\"Muon_charge[0] != Muon_charge[1]\", \"Muons with opposite charge\");"
]
},
{
"cell_type": "markdown",
"id": "f453a4fc",
"metadata": {},
"source": [
"Compute invariant mass of the dimuon system"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "eb42a4a3",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:14:49.131966Z",
"iopub.status.busy": "2026-05-19T20:14:49.131846Z",
"iopub.status.idle": "2026-05-19T20:14:49.724896Z",
"shell.execute_reply": "2026-05-19T20:14:49.724119Z"
}
},
"outputs": [],
"source": [
"auto df_mass = df_os.Define(\"Dimuon_mass\", InvariantMass, {\"Muon_pt\", \"Muon_eta\", \"Muon_phi\", \"Muon_mass\"});"
]
},
{
"cell_type": "markdown",
"id": "966f910a",
"metadata": {},
"source": [
"Make histogram of dimuon mass spectrum"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "73a5392e",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:14:49.726716Z",
"iopub.status.busy": "2026-05-19T20:14:49.726578Z",
"iopub.status.idle": "2026-05-19T20:14:49.930386Z",
"shell.execute_reply": "2026-05-19T20:14:49.929671Z"
}
},
"outputs": [],
"source": [
"auto h = df_mass.Histo1D({\"Dimuon_mass\", \"Dimuon_mass\", 30000, 0.25, 300}, \"Dimuon_mass\");"
]
},
{
"cell_type": "markdown",
"id": "42257435",
"metadata": {},
"source": [
"Request cut-flow report"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "750f0de1",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:14:49.932200Z",
"iopub.status.busy": "2026-05-19T20:14:49.932075Z",
"iopub.status.idle": "2026-05-19T20:14:50.707459Z",
"shell.execute_reply": "2026-05-19T20:14:50.706777Z"
}
},
"outputs": [],
"source": [
"auto report = df_mass.Report();"
]
},
{
"cell_type": "markdown",
"id": "c0932d3f",
"metadata": {},
"source": [
"Produce plot"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "f0d176b4",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:14:50.709587Z",
"iopub.status.busy": "2026-05-19T20:14:50.709467Z",
"iopub.status.idle": "2026-05-19T20:14:54.045664Z",
"shell.execute_reply": "2026-05-19T20:14:54.044844Z"
}
},
"outputs": [],
"source": [
"gStyle->SetOptStat(0); gStyle->SetTextFont(42);\n",
"auto c = new TCanvas(\"c\", \"\", 800, 700);\n",
"c->SetLogx(); c->SetLogy();\n",
"\n",
"h->SetTitle(\"\");\n",
"h->GetXaxis()->SetTitle(\"m_{#mu#mu} (GeV)\"); h->GetXaxis()->SetTitleSize(0.04);\n",
"h->GetYaxis()->SetTitle(\"N_{Events}\"); h->GetYaxis()->SetTitleSize(0.04);\n",
"h->DrawCopy();\n",
"\n",
"TLatex label; label.SetNDC(true);\n",
"label.DrawLatex(0.175, 0.740, \"#eta\");\n",
"label.DrawLatex(0.205, 0.775, \"#rho,#omega\");\n",
"label.DrawLatex(0.270, 0.740, \"#phi\");\n",
"label.DrawLatex(0.400, 0.800, \"J/#psi\");\n",
"label.DrawLatex(0.415, 0.670, \"#psi'\");\n",
"label.DrawLatex(0.485, 0.700, \"Y(1,2,3S)\");\n",
"label.DrawLatex(0.755, 0.680, \"Z\");\n",
"label.SetTextSize(0.040); label.DrawLatex(0.100, 0.920, \"#bf{CMS Open Data}\");\n",
"label.SetTextSize(0.030); label.DrawLatex(0.630, 0.920, \"#sqrt{s} = 8 TeV, L_{int} = 11.6 fb^{-1}\");"
]
},
{
"cell_type": "markdown",
"id": "b0527963",
"metadata": {},
"source": [
"Print cut-flow report"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "bb957fd9",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:14:54.047382Z",
"iopub.status.busy": "2026-05-19T20:14:54.047263Z",
"iopub.status.idle": "2026-05-19T20:14:54.253022Z",
"shell.execute_reply": "2026-05-19T20:14:54.252411Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Events with exactly two muons: pass=1973491 all=4000000 -- eff=49.34 % cumulative eff=49.34 %\n",
"Muons with opposite charge: pass=1501030 all=1973491 -- eff=76.06 % cumulative eff=37.53 %\n"
]
}
],
"source": [
"report->Print();"
]
},
{
"cell_type": "markdown",
"id": "683e9bff",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "9b075869",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:14:54.254695Z",
"iopub.status.busy": "2026-05-19T20:14:54.254556Z",
"iopub.status.idle": "2026-05-19T20:14:54.459356Z",
"shell.execute_reply": "2026-05-19T20:14:54.458598Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"gROOT->GetListOfCanvases()->Draw()"
]
}
],
"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": 5
}