# Histogram from TTree from TFile

**URL:** <https://root-forum.cern.ch/t/histogram-from-ttree-from-tfile/38178>\
**Category:** Newbie\
**Tags:** tree\
**Created:** [February 27, 2020, 4:59pm UTC](https://root-forum.cern.ch/t/histogram-from-ttree-from-tfile/38178 "2020-02-27T16:59:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevinlinxc](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kevinlinxc/32/21030_2.png) [@kevinlinxc](https://root-forum.cern.ch/u/kevinlinxc)\
**Post date:** [February 27, 2020, 4:59pm UTC](https://root-forum.cern.ch/t/histogram-from-ttree-from-tfile/38178/1 "2020-02-27T16:59:57Z")

</div>

It’s my first couple of days using ROOT.  
I’ve been instructed to make a histogram (I think it’s a histogram), make a gaussian fit to a peak, and then integrate it to get a value.  
Here’s what I’ve tried:  
TFile\* f = new TFile(“file\_name”)  
TTree\* tree = (TTree\*)f-\>Get(“r”)  
tree-\>Draw(“fadc\_channel:ftimestamp\>\>hist”,"",“colz”)  
Doing that gives me this histogram:  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/9/0/90a59ca5784e7b5d1b09a6308046a83c04bf89d0.png)  
However, what I’m looking for is something more like this:  
[https://imgur.com/JfL1eYw](https://imgur.com/JfL1eYw), which I can see in the browser but not reach programatically. The second picture looks more like something I can curve fit and integrate over. How can I get there?

_ROOT Version:_ 6.18/04  
_Platform:_ Windows  
_Compiler:_

* * *

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 27, 2020, 6:20pm UTC](https://root-forum.cern.ch/t/histogram-from-ttree-from-tfile/38178/2 "2020-02-27T18:20:50Z")

</div>

Try:

```cpp
{
  gROOT->cd(); // make sure the "h_fadc" histogram will be created here
  TH1D *h = new TH1D("h_fadc", "fadc;channel;#entries", 4096, 0., 4096.);
  TFile *f = TFile::Open("some_file_name.root");
  TTree *t; f->GetObject("r", t);
  gROOT->cd(); // where the "h_fadc" histogram resides
  t->Project("h_fadc", "fadc_channel");
  delete f; // no longer needed (automatically deletes "t", too)
  h->Draw("HIST"); // e.g. "" or "HIST"
}

```

---

<div class="post-metadata">

**Author:** ![kevinlinxc](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kevinlinxc/32/21030_2.png) [@kevinlinxc](https://root-forum.cern.ch/u/kevinlinxc)\
**Post date:** [March 4, 2020, 12:07pm UTC](https://root-forum.cern.ch/t/histogram-from-ttree-from-tfile/38178/3 "2020-03-04T12:07:12Z")

</div>

Can you explain what gROOT-\>cd(); does? When I run this histogram creation code on multiple files, every histogram past the first is blank, and if I delete this line, all histograms are blank, leading me to believe that this line changes something thatI need to “reset” between files

---

<div class="post-metadata">

**Author:** ![kevinlinxc](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kevinlinxc/32/21030_2.png) [@kevinlinxc](https://root-forum.cern.ch/u/kevinlinxc)\
**Post date:** [March 4, 2020, 12:24pm UTC](https://root-forum.cern.ch/t/histogram-from-ttree-from-tfile/38178/4 "2020-03-04T12:24:47Z")

</div>

I got it: ![image](https://root-forum.cern.ch/uploads/default/original/3X/2/7/27448cbf6696357533b8717320af1ebb904fd022.png)  
This screenshot shows that it’s stuck in the last file’s directory for some reason, so the hist is made there so when I use gROOT-\> cd, the hist isn’t there. I just added a gROOT-\>cd at the beginning too so that the hist would be guaranteed to be made there.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 4, 2020, 1:39pm UTC](https://root-forum.cern.ch/t/histogram-from-ttree-from-tfile/38178/5 "2020-03-04T13:39:35Z")

</div>

“Warning in \<Fit\>: Fit data is empty” usually means that the histogram is empty (at least in the range in which you try to fit it).

---

<div class="post-metadata">

**Author:** ![kevinlinxc](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kevinlinxc/32/21030_2.png) [@kevinlinxc](https://root-forum.cern.ch/u/kevinlinxc)\
**Post date:** [March 4, 2020, 1:42pm UTC](https://root-forum.cern.ch/t/histogram-from-ttree-from-tfile/38178/6 "2020-03-04T13:42:53Z")

</div>

In this case, the histogram was empty because it didn’t exist or it just didn’t exist in the right directory… I think

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 4, 2020, 1:56pm UTC](https://root-forum.cern.ch/t/histogram-from-ttree-from-tfile/38178/7 "2020-03-04T13:56:38Z")

</div>

I think it exists but all “relevant” bins have contents and / or errors 0 … try: `h->Print("all");`

---

<div class="post-metadata">

**Author:** ![kevinlinxc](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kevinlinxc/32/21030_2.png) [@kevinlinxc](https://root-forum.cern.ch/u/kevinlinxc)\
**Post date:** [March 4, 2020, 2:10pm UTC](https://root-forum.cern.ch/t/histogram-from-ttree-from-tfile/38178/8 "2020-03-04T14:10:09Z")

</div>

The data is there (nonzero) and can be seen in TBrowser, the problem, at least in my observations, was that the histogram wasn’t in the right place to be fit to. I fixed the problem by putting gROOT-\>cd a the beginning of the histogram macro
