Make 1D histogram from a 2D one by pyroot

hello experts
I have a 2D histogram in the tree and i want only the y-axis and z-axis information to make a 1D plot about pT.

the code is here, to compare histograms from two root files. I used ProfileY() but it doesnt work, and gives error AttributeError: 'TH2F' object has no attribute 'GetHistogram', I’m wondering how to solve the problem? thanks a lot

Blockquote
#!/usr/bin/env python
import sys,os
from math import sqrt
import ROOT as rt
import libPython.CMS_lumi, libPython.tdrstyle
libPython.tdrstyle.setTDRStyle()
listOfFile = [
{ ‘path’ : ‘results/UL2016_postVFP/tnpEleID/passingCutBasedLoose94XV2/egammaEffi.txt_EGM2D.root’, ‘legend’ : ‘run2016postVFP_passingCutBasedLoose94XV2’ , ‘color’ : rt.kAzure},
{ ‘path’ : ‘/eos/user/r/ryi/TagandProbe/TnP2016post/egammaEffi.txt_Ele_Loose_postVFP_EGM2D.root’ , ‘legend’ : ‘official_run2016postVFP’ , ‘color’ : rt.kRed-2},
]
grNames = [ ‘EGamma_SF2D’ ]
c = rt.TCanvas( ‘SFs’, ‘SFs’, 800, 500 )
c.SetTopMargin(0.10)
c.SetBottomMargin(0.15)
c.SetLeftMargin(0.12)
sfMin = 0.68
sfMax = 1.32
for igr in range(len(grNames)):
legend = rt.TLegend(0.6,0.62,0.95,0.86)
legend.SetFillColor(0)
legend.SetBorderSize(0)

for ifile in range(len(listOfFile)):
    print 'Opening TFile: ', listOfFile[ifile]['path']
    rfile = rt.TFile.Open(listOfFile[ifile]['path'],'read')
    rfile.Print()
    rgr   = rfile.Get(grNames[igr])

    drawOption = "P"
    if igr == 0 and ifile == 0 :
        drawOption = "AP"
        rgr.ProfileY()
        rgr.GetHistogram().SetMinimum(sfMin)
        rgr.GetHistogram().SetMaximum(sfMax)

    rgr.SetLineColor(   listOfFile[ifile]['color'] )
    rgr.SetMarkerColor( listOfFile[ifile]['color'] )
    c.cd()
    legend.AddEntry( rgr, listOfFile[ifile]['legend'],'PL' )
    rgr.Draw( drawOption )

Blockquote
xMin = 0
xMax = 2.6
lineAtOne = rt.TLine(xMin,1,xMax,1)
lineAtOne.SetLineStyle(rt.kDashed)
lineAtOne.SetLineWidth(2)
lineAtOne.Draw()
legend.Draw()
libPython.CMS_lumi.CMS_lumi(c, 4, 10)
c.Print(‘outEleID_SFs.pdf’)

Hi there,

Using ProfileY() is a correct thing to do for your purpose.

As your error tells you, the object you are reading from the file already a histogram (TH2F), so you don’t need to call GetHistogram() on (already) a histogram.

// rgr.GetHistogram().SetMinimum(sfMin)
// rgr.GetHistogram().SetMaximum(sfMax)
// change to
rgr.SetMinimum(sfMin)
rgr.SetMaximum(sfMax)

this should do the trick.

cheers

thanks a lot. i modified the code, but it plot the x and y axis, i was expecting it plot y and z axis

i’m not sure which part went wrong?

I think you have to save computed profile in some variable and draw it.

    # ...    
    prof = rgr.ProfileY()        
    # ...    
    prof.Draw( "AP" )

i modified the part to:

for ifile in range(len(listOfFile)):
    print 'Opening TFile: ', listOfFile[ifile]['path']
    rfile = rt.TFile.Open(listOfFile[ifile]['path'],'read')
    rfile.Print()
    rgr   = rfile.Get(grNames[igr])

    drawOption = "P"
    if igr == 0 and ifile == 0 :
        drawOption = "AP"
        prof = rgr.ProfileY()
        prof.SetMinimum(sfMin)
        prof.SetMaximum(sfMax)
    rgr.SetLineColor(   listOfFile[ifile]['color'] )
    rgr.SetMarkerColor( listOfFile[ifile]['color'] )
    c.cd()
    legend.AddEntry( rgr, listOfFile[ifile]['legend'],'PL' )
    prof.Draw( drawOption )

i’m not sure how to save the prof and use it outside of the loop? this one seems not working…

If you need prof outside the loop, using python list to save all of them should work.

profiles = []
for ifile in range(len(listOfFile)):
    prof = rgr.ProfileY()
    profiles.append(prof)
# end of the loop
profiles[0].Draw()

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.