# How to make TH1F from my TTree file

**URL:** <https://root-forum.cern.ch/t/how-to-make-th1f-from-my-ttree-file/31460>\
**Category:** Newbie\
**Created:** [November 6, 2018, 6:36am UTC](https://root-forum.cern.ch/t/how-to-make-th1f-from-my-ttree-file/31460 "2018-11-06T06:36:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![trenta\_coollime](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/trenta_coollime/32/19159_2.png) [@trenta\_coollime](https://root-forum.cern.ch/u/trenta_coollime)\
**Post date:** [November 6, 2018, 6:36am UTC](https://root-forum.cern.ch/t/how-to-make-th1f-from-my-ttree-file/31460/1 "2018-11-06T06:36:38Z")

</div>

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!

```auto
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](https://root-forum.cern.ch/uploads/default/original/3X/e/1/e1b0554dbedef5209512e2ede383fc0a51b8183a.root) (88.5 KB)

Thank you.

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [November 6, 2018, 8:52am UTC](https://root-forum.cern.ch/t/how-to-make-th1f-from-my-ttree-file/31460/2 "2018-11-06T08:52:42Z")

</div>

Hi,

what about

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

```

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![trenta\_coollime](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/trenta_coollime/32/19159_2.png) [@trenta\_coollime](https://root-forum.cern.ch/u/trenta_coollime)\
**Post date:** [November 6, 2018, 4:01pm UTC](https://root-forum.cern.ch/t/how-to-make-th1f-from-my-ttree-file/31460/3 "2018-11-06T16:01:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [November 6, 2018, 4:12pm UTC](https://root-forum.cern.ch/t/how-to-make-th1f-from-my-ttree-file/31460/4 "2018-11-06T16:12:45Z")

</div>

Hi,

what is your root version?

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![trenta\_coollime](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/trenta_coollime/32/19159_2.png) [@trenta\_coollime](https://root-forum.cern.ch/u/trenta_coollime)\
**Post date:** [November 6, 2018, 4:15pm UTC](https://root-forum.cern.ch/t/how-to-make-th1f-from-my-ttree-file/31460/5 "2018-11-06T16:15:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [November 6, 2018, 4:23pm UTC](https://root-forum.cern.ch/t/how-to-make-th1f-from-my-ttree-file/31460/6 "2018-11-06T16:23:18Z")

</div>

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