TTree Draw and TH1F

Hi everyone,

I have a simple question: I define histograms with TH1F ,then I call a variable from a tree and I redirect to histograms defined before with " >> h" but it doesn’t work. It works only if I don’t define histograms, but I have to do this.

These are the first lines of my code: [code]
h1 = TH1F(“dphi”,“dphi”,10,-3.2,3.2)
h2 = TH1F(“dphi_2”,“dphi_2”,10,-3.2,3.2)
h4 = TH1F(“dphi_4”,“dphi_4”,10,-3.2,3.2)
h1.Sumw2()
h2.Sumw2()
h4.Sumw2()

infile_1 = TFile (str(sys.argv[1]),“READ”)
infile_2 = TFile(str(sys.argv[2]),“READ”)
intree_1 = infile_1.Get(“tree;1”)
intree_2 = infile_2.Get(“tree;1”)

intree_1.Draw(“truth_dphi >> h1”,“truth_dphi > -5”)
intree_1.Draw(“truth_dphi >> h4”,“truth_dphi > -5”)
intree_2.Draw(“truth_dphi >> h2”,“truth_dphi > -5”)
[/code]

Thank you!

try

h1 = TH1F("h1","dphi",10,-3.2,3.2)
h2 = TH1F("h2","dphi_2",10,-3.2,3.2)
h4 = TH1F("h4","dphi_4",10,-3.2,3.2)

Unforunately it doesn’t work, it doesn’t draw any histograms. This is the complete code if it can helps:

[code]from ROOT import *
import ROOT
import sys

gROOT.LoadMacro(“AtlasStyle.C”)
SetAtlasStyle()

h1 = TH1F(“h1”,“dphi”,10,-3.2,3.2)
h2 = TH1F(“h2”,“dphi_2”,10,-3.2,3.2)
h4 = TH1F(“h4”,“dphi_4”,10,-3.2,3.2)
h1.Sumw2()
h2.Sumw2()
h4.Sumw2()

infile_1 = TFile (str(sys.argv[1]),“READ”)
infile_2 = TFile(str(sys.argv[2]),“READ”)
intree_1 = infile_1.Get(“tree;1”)
intree_2 = infile_2.Get(“tree;1”)

intree_1.Draw(“truth_dphi >> h1”,“truth_dphi > -5”)
intree_1.Draw(“truth_dphi >> h4”,“truth_dphi > -5”)
intree_2.Draw(“truth_dphi >> h2”,“truth_dphi > -5”)

c1.Clear()
c1.Divide(1,2,0,0)

c1_1.SetPad(.005,.305,.995,.995)
c1_2.SetPad(.005,.005,.995,.295)

int1 = h1.Integral(“weight”)
int2 = h2.Integral(“weight”)
int4 = h4.Integral(“weight”)
h1.Scale(1./int1)
h2.Scale(1./int2)
h4.Scale(1./int4)

h1.SetTitle(“Dphi”)
h4.SetTitle("")

c1.cd(1)
h1.GetYaxis().SetTitle(“Entries”)
h1.GetYaxis().SetLabelSize(0.05)
h1.SetLineColor(kGreen)
h2.SetLineColor(kRed)

h1.Draw()
h2.Draw(“same”)

c1.cd(2)
h4.Divide(h2)
h4.GetXaxis().SetTitle(“Delta_phi”)
h4.GetXaxis().SetTitleSize(0.12)
h4.GetXaxis().SetTitleOffset(0.65)
h4.GetXaxis().SetLabelSize(0.12)
h4.GetYaxis().SetTitle(“Ratio”)
h4.GetYaxis().SetTitleSize(0.12)
h4.GetYaxis().SetTitleOffset(0.6)
h4.GetYaxis().SetLabelSize(0.12)
h4.SetLineColor(kBlue)
h4.Draw()

c1.Draw()
c1.SaveAs(“prova.root”)
[/code]

Try to add “gROOT.cd()” right before the line “intree_1.Draw(…)”.

Ok, it works. Thanks a lot!