{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

An introductional notebook to HEP analysis in C++


\n", "
\n", "\n", "

In this notebook you can find an easy set of commands that show some basic computing techniques commonly used in High Energy Physics (HEP) analyzes.

\n", "\n", "

It also shows how to create an histogram, fill it and draw it. Moreover it is an introduction to [ROOT](https://root.cern.ch/) too. The final output is a plot with the number of leptons.

\n", "\n", "

all done with less that 20 lines of code!

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The library used is [ROOT](https://root.cern.ch/), a scientific data analysis software framework that provides a large set of functionalities needed to deal with big data processing, statistical analysis, visualisation and storage." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Cell #1

\n", "

At first we have to include several helpers that will support our analysis:

" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "#include \n", "#include \n", "#include " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Cell #2

\n", "

In order to activate the interactive visualisation of the histogram that is later created we can use the JSROOT magic:

" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "%jsroot on" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Cell #3

\n", "

Next we have to open the data that we want to analyze. It is stored in a _*.root_ file that consists of a tree having branches and leaves. As you can see , we are reading the data directly from the source!

" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "TFile *file = TFile::Open(\"https://atlas-opendata.web.cern.ch/atlas-opendata/samples/2020/1largeRjet1lep/MC/mc_361106.Zee.1largeRjet1lep.root\"); // 13 TeV sample\n", "//TFile *file = TFile::Open(\"http://opendata.atlas.cern/release/samples/MC/mc_147770.Zee.root\"); // 8 TeV sample\n", "//TFile *file = TFile::Open(\"/home/student/datasets/MC/mc_105987.WZ.root\"); // example of a local file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Cell #4

\n", "

The next step is to define a tree named (we called _tree_) to get the data out of the _*.root_ file:

" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "TTree *tree = (TTree*) file->Get(\"mini\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Cell #5

\n", "

The next step is to extract the varibles we want from the dataset. As mentioned before, we will plot the number of leptons:

\n", "

(noteto know more about the content of the ATLAS Open Data datasets, please go to our documentation)

" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "UInt_t lepton_n = -1;\n", "tree->SetBranchAddress(\"lep_n\", &lepton_n);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Cell #6

\n", "

After the data is opened we create a canvas on which we can draw a histogram. If we do not have a canvas we cannot see our histogram at the end. Its name is _Canvas_ and its header is _a first way to plot a variable_. The two following arguments define the width and the height of the canvas:

" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "TCanvas *canvas = new TCanvas(\"Canvas\",\"a first way to plot a variable\",800,600);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Cell #7

\n", "

Now we define a histogram that will later be placed on this canvas. Its name is _variable_ and the header of the histogram is _Example plot: Number of leptons_. The three following arguments indicate that this histogram contains 4 so called bins which have a range from 0 to 4.

" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "TH1F *hist = new TH1F(\"variable\",\"Example plot: Number of leptons; Number of leptons ; Events \",5,-0.5,4.5);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Cell #8

\n", "

The following lines are a loop that goes over the data that is stored in the tree and fills the histogram _h_ that we already defined. In this first notebook we don't do any cuts to keep it simple. Accordingly the loop fills the histogram for each event stored in the tree. After the program has looped over all the data it prints the word

\n", "__Done!__." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Done!\n" ] } ], "source": [ "int nentries, nbytes, i;\n", "nentries = (Int_t)tree->GetEntries();\n", "\n", "for (i = 0; i < nentries; i++)\n", "{\n", " nbytes = tree->GetEntry(i);\n", " hist->Fill(lepton_n);\n", "}\n", "\n", "std::cout << \"Done!\" << std::endl;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Cell #9

\n", "

After filling the histogram we want to see the results of the analysis. First we draw the histogram on the canvas and then the canvas on which the histogram lies:

" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "hist->SetFillColor(kRed);\n", "hist->Draw();\n", "canvas->Draw();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Done!**" ] } ], "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 }