Hi ROOTers!
When drawing to a canvas i also Draw a TGaxis to the same Canvas and it vanishes somehow when Saving. I have provided some boiled down version of the programm:
import ROOT
import os
from array import array
def save(canvas,plotpath, filename,extensions = ["png", "pdf", "root"]):
if not os.path.exists(plotpath):
os.makedirs(plotpath)
for ext in extensions:
savePath = os.path.join(plotpath,filename)
canvas.Print("%s.%s"%(savePath,ext))
def drawObjs(canvas,objList,xMin = None,xMax = None,yMin = None,yMax = None,caption = "Spectrum", xAxis = "xAxisName", yAxis = "yAxisName",yAxis2 = None,frameBool = True,drawOptions = []):
#yAxis2 Can be initialized like: yAxis2 = {"obj":obj,"title",title"yMin":yMin,"yMax":yMax}
#drawOptions Can be [[obj1,"P"],[obj3,""]]
if frameBool:
canvas.DrawFrame(xMin,yMin,xMax,yMax,"%s;%s;%s" % (caption,xAxis,yAxis))
for obj,i in zip(objList,range(len(objList))):
#Set Drawoption
drawOption = "P"
for drawObject,drawOptionTemp in drawOptions:
if drawObject == obj:
drawOption = drawOptionTemp
break
if not frameBool and obj == objList[0]:
obj.Draw(drawOption)
else:
obj.Draw("SAME" + drawOption)
#Second yAxis
if not yAxis2 is None and (issubclass(obj.__class__,ROOT.TGraph) or issubclass(obj.__class__,ROOT.TF1) or issubclass(obj.__class__,ROOT.TH1)):
if obj == yAxis2["obj"]:
axis = ROOT.TGaxis( ROOT.gPad.GetUxmax(),
ROOT.gPad.GetUymin(),
ROOT.gPad.GetUxmax(),
ROOT.gPad.GetUymax(),
yAxis2["yMin"],
yAxis2["yMax"],
50510,"+L")
axis.SetTitle(yAxis2["title"])
axis.Draw("SAME") #Doesnt matter if i say "same" here or not
raw_input()
return canvas
if __name__ == "__main__":
xMin = -5
xMax = 5
hist = ROOT.TH1F("hist","hist",100,xMin,xMax)
hist.SetMarkerStyle(2)
gaus = ROOT.TF1("g","gaus",xMin,xMax)
gaus.SetParameters(1,2,3)
hist.FillRandom("g")
canvas = ROOT.TCanvas("canvas","CAPTION",2048,1024)
#canvas.Range(-10,-1,10,1)
canvas = drawObjs(canvas,[gaus,hist],yAxis = "Gaus",yAxis2 = {"title":"Histo","obj":hist,"yMin":0.,"yMax":100},xMin = xMin,xMax = xMax,yMin = 0,yMax = 100)
raw_input()
save(canvas,"/user/tmp","Test")
Just before saving i have the canvas as wished but the right axis is gone when opening the pdf. I hope someone can solve this
Cheers,
Daniel