An introductional notebook to HEP analysis in C++


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.

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.

all done with less that 20 lines of code!

The library used is ROOT, 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 #1

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

In [1]:
#include <iostream>
#include <string>
#include <stdio.h>

Cell #2

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

In [2]:
%jsroot on

Cell #3

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!

In [3]:
TFile *file = TFile::Open("http://opendata.atlas.cern/release/samples/MC/mc_147770.Zee.root");

Cell #4

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

In [4]:
TTree *tree = (TTree*) file->Get("mini");

Cell #5

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

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

In [5]:
UInt_t lepton_n = -1;
tree->SetBranchAddress("lep_n", &lepton_n);

Cell #6

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:

In [6]:
TCanvas *canvas = new TCanvas("Canvas","a first way to plot a variable",800,600);

Cell #7

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.

In [7]:
TH1F *hist = new TH1F("variable","Example plot: Number of leptons",4,0,4);

Cell #8

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

Done!.

In [8]:
int nentries, nbytes, i;
nentries = (Int_t)tree->GetEntries();

for (i = 0; i < nentries; i++)
{
    nbytes = tree->GetEntry(i);
    hist->Fill(lepton_n);
}

std::cout << "Done!" << std::endl;
Done!

Cell #9

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:

In [9]:
hist->Draw();
canvas->Draw();