Writing a Histograms as a branch of tree in PyROOT


_ROOT Version: 6.08
Platform: Not Provided
Compiler: Not Provided


Hi Experts,
I want to write a histogram to a branch of a tree in PyROOT.

// Opens input file
f =ROOT.TFile.Open(“~/Sharma/TuningMC/RootFiles/MC_pidfix/BIN1/performanceHisto.root”)
//Get NDim Histo
nHis1=f.Get(“RAAhisTPC_NcrossedRows_v_qPt_tgl_phi”)
//Create Output file
fileOut=ROOT.TFile.Open(“fileOut.root”,“RECREATE”)
//Create TTree
tree = ROOT.TTree( ‘tree’, ‘tree’ )
//extract TH1D histo
h1=nHis1.Projection(0)
//Writing as a branch
tree.Branch(“h1”,“TH1D”,hex(id(h1))) // Here I am giving the address, but I am no getting histogrm
tree.Write()

Is there anything I am doing wrong?
Could you please help in this?

Thanks in Advance

Himanshu

Hi @HIMANSHU_SHARMA

Can you try with

 tree.Branch('h1', h1)

This should create a branch called h1 associated with the h1 histogram, and when you run tree.Write() it will write an entry in the tree for that branch.

Thanks!
It works. There is a branch in the tree with a lot of information.
I correct myself, I need a single leaf which contains the histogram (h1) inside the tree.

Right Now we have this –

tree->Branch (h1)-> … … several leaves and plenty of information ***but NOT the h1 distribution

I expect —

tree->leaf (h1) ***** No extra Info, just h1 distribution

h1 is a 1D histogram

Thanks!

Hi @HIMANSHU_SHARMA

So you just want to store a single histogram in your file, is that correct?

If that is so, you just need to

    # Opens input file
    f =ROOT.TFile.Open("~/Sharma/TuningMC/RootFiles/MC_pidfix/BIN1/performanceHisto.root")
    # Get NDim Histo
    nHis1=f.Get(“RAAhisTPC_NcrossedRows_v_qPt_tgl_phi”)
    # Create Output file
    fileOut=ROOT.TFile.Open(“fileOut.root”,“RECREATE”)
    # extract TH1D histo
    h1=nHis1.Projection(0)
    # Write as single object in file
    h1.Write()

Yes! this does work.

But I want to write as an object inside tree.
I need this-

fileOut → tree → h1

What you were suggesting is -

fileOut → h1

Please note that the tree is (sort of) a table, so if you write the histogram as a branch (column), you will have N copies of that histogram stored in the tree (where N is the number of rows, called entries in the tree).

Okay!
Thanks for the reply…
I understood

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