import ROOT

"""
Explanation: <CENTER>
    <a href="http://opendata.atlas.cern" class="icons"><img src="../images/opendata-top-transblack.png" style="width:40%"></a>
</CENTER>
A simple introductional notebook to HEP analysis in python
<p> In this notebook you can find an easy set of commands that show the basic computing techniques commonly used in high energy  physics (HEP) analyzes. It also shows how to create a histogram, fill it and draw it. Moreover it is an introduction to [ROOT](https://root.cern.ch/) too. At the end you get a plot with the number of leptons.</p>

<CENTER><h1>Simple pyROOT notebook example</h1></CENTER>
The library used is ROOT - a scientific software framework that provides all the functionalities needed to deal with big data processing, statistical analysis, visualisation and storage.
First of all ROOT is imported to read the files in the .root data format. A .root file consists of a tree having branches and leaves. At this point you could also import further programs that contain other formulas that you maybe use more often. But here we don't import other programs to keep it simple.
End of explanation
"""


## %jsroot on

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


##f = ROOT.TFile.Open("http://opendata.atlas.cern/release/samples/MC/mc_147770.Zee.root")
f = ROOT.TFile.Open("/home/student/datasets/MC/mc_105987.WZ.root")

"""
Explanation: Next we have to open the data that we want to analyze. As described above the data is stored in a *.root file.
End of explanation
"""


canvas = ROOT.TCanvas("Canvas","a first way to plot a variable",800,600)

"""
Explanation: 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.
End of explanation
"""


tree = f.Get("mini")

"""
Explanation: The next step is to define a tree named tree to get the data out of the *.root file.
End of explanation
"""


hist = ROOT.TH1F("variable","Example plot: Number of leptons",4,0,4)

"""
Explanation: 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.
End of explanation
"""


for event in tree:
    hist.Fill(tree.lep_n)
    
print "Done!"

"""
Explanation: 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!.
End of explanation
"""


hist.Draw()

canvas.Draw()

scale = hist.Integral()
hist.Scale(1/scale)

hist.Draw()
canvas.Draw()

"""
Explanation: 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.
End of explanation
"""


