Home | News | Documentation | Download

Histogram from TTree from TFile

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
However, what I’m looking for is something more like this:
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:


Try:

{
  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"
}

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

I got it: image
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.

“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).

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

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

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