Saving a TNtuple tree as a histogram

Hi everyone! I have a TNtuple tree with two variables (say, X and Y). I can draw a histogram of X versus Y using the command:

(t is the name of the tree variable).

I would like to save it in the TH1F format (therefore I could fit it, etc.,etc.) How can I do this?

Thanks!

A TH1 histogram is a “one-dimensional” object in the sense that it only needs one variable. You can get one for X by doing t.Draw(“X”) as you know. Making a “histogram of X versus Y” would be a two-dimensional TH2, which you can get by doing t.Draw(“X:Y”) (or Y:X). Actually the type of object made by t.Draw(“X:Y”) depends on whether you add " >> myhist" type of commands in the string, so it’s a bit confusing. Sometimes it’s a TGraph, sometimes a TH2. See this thread: TGraph saved to file shows up as TH2F

Jean-François

Merci, I’ve already solved the problem :smiley:

I now have a different problem: I need to read from a ROOT file, where I’ve saved the histogram, and then fit it.

It works OK like this:

TFile f("900V.root"); TH1D *h=(TH1D*)f.Get("c1"); h->Draw();

But then, when I add the line

or something like that, it just gives me an “Unhandled exception [5988]”. The Fit Panel works perfectly fine, but I would like to do it in the code.

Maybe, you have to write

instead of

Already tried that, result is the same. I even tried doing this in an older version of ROOT and on a different PC.

And if you define a TF1 like that

TF1 *f = new TF1("f","gaus",-5,5); h->Fit("f")
Does it work?

No result. It seems to have a problem with the “h->Fit(…)” no matter what sort of function I place in the brackets. Here is my file, perhaps the problem is with the file?
900V.root (14.8 KB)

Ah ok, the problem is that “c1” is not a TH1D but a TCanvas
So, you have to write something like

TFile *f = new TFile("900V.root"); TCanvas *c1 = (TCanvas*)f->Get("c1"); TH1D *h1 = (TH1D*)c1->FindObject("htemp"); h1->Fit("gaus")

Thanks a lot, it worked! :smiley: :smiley: Spent a couple of days trying to fix this…