Home | News | Documentation | Download

How to make TH1F from my TTree file


#1

Hi, I have a question with respect to transfering TTree into TH1F.

I have a TTree ROOT file, that I want to convert into TH1F to do Gaussian fit!

I attached the code of my pyROOT with my TTree Root file in this writing for consultation.

WIth my attempt however, I got into several problems.

1.In my pyROOT script below, I want to get number of entries for “chg” branch but it says
AttributeError: ‘ROOT.PyUShortBuffer’ object has no attribute ‘GetEntries’. How can I get number of entries for the specific branch?
2. When I try to write on existing B8D5.root, it gives me an error with message that it’s not writable. How can I fix the problem?
3. Any other or general advice for my newbie code please!

from ROOT import gSystem, TFile, TH1F, TCanvas, gRandom
from histutil import *
from array import array
import random
rootfile = '1FB8D5.root'
def main():       
  myfile =  TFile("B8D5.root")
  t2 = myfile.Get("mppc")    
  hdestep   = TH1F("ADC Values","ADC Values",800,0,799)

  now = getattr(t2,'chg')
  nentries = now.GetEntries()
  print(nentries)
  for i in range(nentries):       
      npw = now.GetEntry(i)
      hdestep.Fill(npw)
  hfile = TFile(rootfile, "recreate")
  hfile.cd()
  hdestep.Write() 
  hfile.Close()
  myfile.Close()

#-------------------------------------------------------------------------------
try:
   main()
except KeyboardInterrupt:
   print 'ciao!'

B8D5.root (88.5 KB)

Thank you.


#2

Hi,

what about

rdf = ROOT.ROOT.RDataFrame("mppc", "B8D5.root")
h = rdf.Histo1D("chg")
h.Fit("gaus")  

Cheers,
D


#3

It has the message AttributeError: type object ‘ROOT’ has no attribute ‘RDataFrame’


#4

Hi,

what is your root version?

Cheers,
D


#5

6.13.02 is my root version. I guess my question mainly consists of two big chunks.

one for how to access a branch in PyROOT.

and how to obtain the number of entries for the specific branch, not a tree


#6

6.13 is a bit old, it does not contain RDataFrame. Perhaps you can move to 6.14?