Superimposing two histograms

Hi,

I translated the sample code for “Superimposing two histograms with different scales in the same pad” given in root.cern.ch/root/html/THistPainter.html to the piece of Python code shown below.

The C version works exactly as expected, the Python code creates just an empty frame with a nice title.

What did I do wrong in Python?

Thanks & regards,
Roland

[code]import ROOT
from ROOT import TCanvas, TNtuple, TH1F, TH2F, TF1, TLegend, TFile, TTree, THStack
from ROOT import TLatex, TAxis, TPaveText, TGaxis
from ROOT import gROOT, gSystem, gStyle, gPad, gEnv, gRandom

def superTest():

c1 = TCanvas(“c1”,“c1”,600,400)

create/fill draw h1

-------------------

gStyle.SetOptStat(ROOT.kFALSE)
h1 = TH1F(“h1”,“Superimposing two histograms with different scales”,100,-3,3)

for i in range(10000):
h1.Fill(gRandom.Gaus(0,1))
h1.Draw()
c1.Update()

create hint1 filled with the bins integral of h1

------------------------------------------------

hint1 = TH1F(“hint1”,“h1 bins integral”,100,-3,3)
sum = 0.0;
for i in range(100):
sum = sum + h1.GetBinContent(i)
hint1.SetBinContent(i,sum)

scale hint1 to the pad coordinates

----------------------------------

rightmax = 1.1*hint1.GetMaximum()
scale = gPad.GetUymax()/rightmax
hint1.SetLineColor(ROOT.kRed)
hint1.Scale(scale)
hint1.Draw(“same”)

draw an axis on the right side

------------------------------

axis = TGaxis(gPad.GetUxmax(), gPad.GetUymin(),
gPad.GetUxmax(), gPad.GetUymax(),0,rightmax,510,"+L")
axis.SetLineColor(ROOT.kRed)
axis.SetTextColor(ROOT.kRed)
axis.Draw()
return c1

superTest().Print(‘super.png’)[/code]

Hi,

the various graphics objects created in the function will go out of scope at the end of it and will be gone by the time Print() is called. Instead of code like ‘h1 = …’, write ‘c1.h1 = …’ so that the lifetime of the graphics objects and the canvas is the same.

Cheers,
Wim

Wim,

This is not a good solution. a histohram may be drawn in different pads/canvases.

Rene

Rene,

that is under control of the application writer, of course, so in practice it works well as every canvas that draws the histogram can take its own reference. But one can also do the same as in C++, and give up control completely user-side:ROOT.SetOwnership( h1, False )And if such code occurs a lot, one can decide to toggle the _creates member of the function to never have python own any of the newly created objects in the first place.

Cheers,
Wim