{
"cells": [
{
"cell_type": "markdown",
"id": "7af82a72",
"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, March 19, 2024 at 07:07 PM."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "382f6a2b",
"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": "47e137a0",
"metadata": {},
"source": [
"Enable multi-threading"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b4be14df",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"ROOT::EnableImplicitMT();"
]
},
{
"cell_type": "markdown",
"id": "bda01ee4",
"metadata": {},
"source": [
"Create dataframe from NanoAOD files"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e0267722",
"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": "aa84158d",
"metadata": {},
"source": [
"Add ProgressBar"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f3ea82f6",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"ROOT::RDF::Experimental::AddProgressBar(df);"
]
},
{
"cell_type": "markdown",
"id": "4ac673bb",
"metadata": {},
"source": [
"For simplicity, select only events with exactly two muons and require opposite charge"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "892383da",
"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": "64c30513",
"metadata": {},
"source": [
"Compute invariant mass of the dimuon system"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b52de1d8",
"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": "dd94624e",
"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": "797a56a6",
"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": "41057d8d",
"metadata": {},
"source": [
"Request cut-flow report"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "38b65338",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"auto report = df.Report();"
]
},
{
"cell_type": "markdown",
"id": "ccb56f6e",
"metadata": {},
"source": [
"Produce plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "56bdf9dd",
"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": "ee7aa448",
"metadata": {},
"source": [
"Print cut-flow report"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a85fc61e",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"report->Print();"
]
},
{
"cell_type": "markdown",
"id": "d3bec325",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3ac2c3eb",
"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
}