import ROOT

rndm = ROOT.TRandom3(1)

f = ROOT.TFile("histos.root","RECREATE")

hg = ROOT.TH1F("gaus","Gaussian numbers", 64, -4, 4)
he = ROOT.TH1F("expo","Exponential numbers", 64, -4, 4)
hu = ROOT.TH1F("unif","Uniform numbers", 64, -4, 4)
for i in xrange(1024):
    hg.Fill(rndm.Gaus())
    he.Fill(rndm.Exp(1))
    hu.Fill(rndm.Uniform(-4,4))

for h in (hg, he, hu): h.Write()
    
f.Close()

"""
Explanation: <span style="color:red">
Wait before looking at the solution! Try harder if you can!
</span>
Writing on files
This is a Python notebook in which you will practice the concepts learned during the lectures.
Writing histograms
Create a TFile containing three histograms filled with random numbers distributed according to a Gaus, an exponential and a uniform distribution.
Close the file: you will reopen it later.
End of explanation
"""


! ls .
! echo Now listing the content of the file
! rootls -l ./histos.root

"""
Explanation: Now, you can invoke the ls command from within the notebook to list the files in this directory. Check that the file is there. You can invoke the rootls command to see what's inside the file.
End of explanation
"""


%jsroot on
f = ROOT.TFile("histos.root")
c = ROOT.TCanvas()
c.Divide(2,2)
c.cd(1)
f.gaus.Draw()
c.cd(2)
f.expo.Draw()
c.cd(3)
f.unif.Draw()
c.Draw() # Draw the Canvas

"""
Explanation: Access the histograms and draw them in Python. Remember that you need to create a TCanvas before and draw it too in order to inline the plots in the notebooks.
You can switch to the interactive JavaScript visualisation using the %jsroot on "magic" command.
End of explanation
"""


%%cpp
TFile f("histos.root");
TH1F *hg, *he, *hu;
f.GetObject("gaus", hg);
f.GetObject("expo", he);
f.GetObject("unif", hu);
TCanvas c;
c.Divide(2,2);
c.cd(1);
hg->Draw();
c.cd(2);
he->Draw();
c.cd(3);
hu->Draw();
c.Draw(); // Draw the Canvas

"""
Explanation: You can now repeat the exercise above using C++. Transform the cell in a C++ cell using the %%cpp "magic".
End of explanation
"""


f = ROOT.TXMLFile("histos.xml","RECREATE")

hg = ROOT.TH1F("gaus","Gaussian numbers", 64, -4, 4)
he = ROOT.TH1F("expo","Exponential numbers", 64, -4, 4)
hu = ROOT.TH1F("unif","Uniform numbers", 64, -4, 4)
for i in xrange(1024):
    hg.Fill(rndm.Gaus())
    he.Fill(rndm.Exp(1))
    hu.Fill(rndm.Uniform(-4,4))

for h in (hg, he, hu): h.Write()
    
f.Close()

! ls -l histos.xml histos.root

! cat histos.xml

"""
Explanation: Inspect the content of the file: TXMLFile
ROOT provides a different kind of TFile, TXMLFile. It has the same interface and it's very useful to better understand how objects are written in files by ROOT.
Repeat the exercise above, either on Python or C++ - your choice, using a TXMLFILE rather than a TFile and then display its content with the cat command. Can you see how the content of the individual bins of the histograms is stored? And the colour of its markers?
Do you understand why the xml file is bigger than the root one even if they have the same content?
End of explanation
"""


