{ "cells": [ { "cell_type": "markdown", "id": "a8cef08d", "metadata": {}, "source": [ "# df102_NanoAODDimuonAnalysis\n", "Show how NanoAOD files can be processed with RDataFrame.\n", "\n", "This tutorial illustrates how NanoAOD files can be processed with ROOT\n", "dataframes. The NanoAOD-like input files are filled with 66 mio. events\n", "from CMS OpenData containing muon candidates part of 2012 dataset\n", "([DOI: 10.7483/OPENDATA.CMS.YLIC.86ZZ](http://opendata.cern.ch/record/6004)\n", "and [DOI: 10.7483/OPENDATA.CMS.M5AD.Y3V3](http://opendata.cern.ch/record/6030)).\n", "The macro matches muon pairs and produces an histogram of the dimuon mass\n", "spectrum showing resonances up to the Z mass.\n", "Note that the bump at 30 GeV is not a resonance but a trigger effect.\n", "\n", "More details about the dataset can be found on [the CERN Open Data portal](http://opendata.web.cern.ch/record/12341).\n", "\n", "\n", "\n", "\n", "**Author:** Stefan Wunsch (KIT, CERN) \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:10 PM." ] }, { "cell_type": "code", "execution_count": null, "id": "8037c613", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"ROOT/RDataFrame.hxx\"\n", "#include \"ROOT/RDFHelpers.hxx\"\n", "#include \"ROOT/RVec.hxx\"\n", "#include \"TCanvas.h\"\n", "#include \"TH1D.h\"\n", "#include \"TLatex.h\"\n", "#include \"TStyle.h\"\n", "\n", "using namespace ROOT::VecOps;" ] }, { "cell_type": "markdown", "id": "8d6236ea", "metadata": {}, "source": [ "Enable multi-threading" ] }, { "cell_type": "code", "execution_count": null, "id": "eee61895", "metadata": { "collapsed": false }, "outputs": [], "source": [ "ROOT::EnableImplicitMT();" ] }, { "cell_type": "markdown", "id": "d032046d", "metadata": {}, "source": [ "Create dataframe from NanoAOD files" ] }, { "cell_type": "code", "execution_count": null, "id": "8ae5da34", "metadata": { "collapsed": false }, "outputs": [], "source": [ "ROOT::RDataFrame df(\"Events\", \"root://eospublic.cern.ch//eos/opendata/cms/derived-data/AOD2NanoAODOutreachTool/\"\n", " \"Run2012BC_DoubleMuParked_Muons.root\");" ] }, { "cell_type": "markdown", "id": "79576a68", "metadata": {}, "source": [ "Add ProgressBar" ] }, { "cell_type": "code", "execution_count": null, "id": "14a968c9", "metadata": { "collapsed": false }, "outputs": [], "source": [ "ROOT::RDF::Experimental::AddProgressBar(df);" ] }, { "cell_type": "markdown", "id": "9234cfeb", "metadata": {}, "source": [ "For simplicity, select only events with exactly two muons and require opposite charge" ] }, { "cell_type": "code", "execution_count": null, "id": "3163a00c", "metadata": { "collapsed": false }, "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": "84a4aa74", "metadata": {}, "source": [ "Compute invariant mass of the dimuon system" ] }, { "cell_type": "code", "execution_count": null, "id": "884badb0", "metadata": { "collapsed": false }, "outputs": [], "source": [ "auto df_mass = df_os.Define(\"Dimuon_mass\", InvariantMass, {\"Muon_pt\", \"Muon_eta\", \"Muon_phi\", \"Muon_mass\"});" ] }, { "cell_type": "markdown", "id": "7adeef69", "metadata": {}, "source": [ "Make histogram of dimuon mass spectrum. Note how we can set title and axis labels in one go" ] }, { "cell_type": "code", "execution_count": null, "id": "d8ab64e0", "metadata": { "collapsed": false }, "outputs": [], "source": [ "auto h = df_mass.Histo1D({\"Dimuon_mass\", \"Dimuon mass;m_{#mu#mu} (GeV);N_{Events}\", 30000, 0.25, 300}, \"Dimuon_mass\");" ] }, { "cell_type": "markdown", "id": "7ceb9688", "metadata": {}, "source": [ "Request cut-flow report" ] }, { "cell_type": "code", "execution_count": null, "id": "afbe888c", "metadata": { "collapsed": false }, "outputs": [], "source": [ "auto report = df.Report();" ] }, { "cell_type": "markdown", "id": "26957e6e", "metadata": {}, "source": [ "Produce plot" ] }, { "cell_type": "code", "execution_count": null, "id": "201ffc36", "metadata": { "collapsed": false }, "outputs": [], "source": [ "gStyle->SetOptStat(0); gStyle->SetTextFont(42);\n", "auto c = new TCanvas(\"c\", \"\", 800, 700);\n", "c->SetLogx(); c->SetLogy();\n", "\n", "h->GetXaxis()->SetTitleSize(0.04);\n", "h->GetYaxis()->SetTitleSize(0.04);\n", "h->DrawClone();\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}\");\n", "\n", "c->SaveAs(\"dimuon_spectrum.pdf\");" ] }, { "cell_type": "markdown", "id": "85ad2613", "metadata": {}, "source": [ "Print cut-flow report" ] }, { "cell_type": "code", "execution_count": null, "id": "1331d5e8", "metadata": { "collapsed": false }, "outputs": [], "source": [ "report->Print();" ] }, { "cell_type": "markdown", "id": "48336159", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": null, "id": "709d598a", "metadata": { "collapsed": false }, "outputs": [], "source": [ "%jsroot on\n", "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 }