First, sorry for the long post, but it’s very simple. This problem is not a difficult problem, but I’ve a big problem to work with ROOT. I simplify the problem, I hope someone can find an easier solution.
Let t1 and t2 be two TTree class. They have the same variables called “A”, “B”, … but t1 and t2 are different, in particular they have different entries number.
Let el1, el2, el3, … be TEntryLists that can be applied to t1 and to t2.
Let a list like: variables_to_draw=[“A”,“B”,“A:B”,…] describe what we want to plot.
The problem is: draw the histogram normalized of the variables specified in variables_to_draw. In this example I want to draw 2 TH1F (A,B) and 1 TH2F (A:B). I want to draw all the histograms in the same canvas, and I want to superimpose the variable of the two TTree. Redo all applying el1,el2,el3, …
The code is a python code, but it’s not this the problem
import copy
import random
def do(canvas,variables_to_draw,t1,t2):
...
canvas.Divide(nx,ny)
histos = []
seed = random.random()
for (n,var) in enumerate(variables_to_draw):
canvas.cd(n+1)
name1 = "1_%s%s" % (n,seed)
name2 = "2_%s%s" % (n,seed)
t1.Draw(var+">>"+name1,"","goff")
t2.Draw(var+">>"+name2,"","goff")
h1 = copy.copy(gDirectory.Get(name1))
h2 = copy.copy(gDirectory.Get(name2))
try:
h1.Scale(1./h1.GetEntries())
except(ZeroDivisionError):
pass
try:
h2.Scale(1./h2.GetEntries())
except(ZeroDivisionError):
pass
histos.append(h1)
histos.append(h2)
h1.Draw()
h2.Draw("same")
canvas.Update()
canvas.histos = histos
def main(canvas,variables_to_draw,t1,t2,el1,el2,el3):
els = [el1,el2,el3]
for e in els:
t1.SetEntryList(e)
t2.SetEntryList(e)
do(canvas,variables_to_draw,t1,t2)
wait_key_pressed()
the random seed is necessary, because the name of the histogram need to be different for all the els loop
This is my dream:
def do(canvas,variables_to_draw,t1,t2):
...
canvas.Divide(nx,ny)
for var in variables_to_draw:
h1 = Data2Histogram(t1,var)
h2 = Data2Histogram(t2,var)
h1.NormalizeArea(1)
h2.NormalizeArea(1)
subcanvas = canvas.NextSubCanvas()
subcanvas.Draw(h1)
subcanvas.Draw(h2,"same") #no persistency problem, all automatic
# or better:
subcanvas.DrawHistogram([h1,h2],NormalizedArea=True)
# ok, this is too much for C++