# Get a TH1F from a TGraph

Hello!

Is there a way to get a TH1F from a TGraph?

I have a TGraph that has points scattered in varying intervals of X, so this makes it impossible to do a simple SetBinContent.

• Sujeewa

Could you explain what is your problem and what you want to do?

Rene

Hi Rene,

So, I have a TGraph *gr of scattered points in varying intervals of X. For ex., (x,y)={(0.01,5.), (2.50, 6.), (1.68, 1.1), ( 3.2, 5.}

Since the x intervals for the points are varying, I have to make a TGraph, instead of a TH1F since I can not use TH1F->SetBinContent. So I make TGraph *gr.

But now I want to do things like
GetMaximumBinContent(),
hh->GetBinContent(hh->FindBin(xx))
etc, all of which I could do had I made a TH1F instead of a TGraph; but I could not have made a TH1F from my varying intervals of x data set. (at least I am not aware of how to do this).

So now I am thinking, is there a way that I can extract, or convert a my gr to a TH1F?

At the end what I want to do is to fit one TGraph, say TGraph *data, to another TGraph, say TGraph *pred, like your example in \$ROOTSYS/tutorials/fit/fithist.C (thanks for that earlier reply too, btw!), but instead of fitting histos I want to fit graphs.

Thanks!

• Sujeewa

Use TMath::LocMax to locate the maximum element of an array, eg

``` TGraph *gr; Long64_t locmax = TMath::LocMax(gr->GetN(),gr->GetY()); //the corresponding x value can be obtained with double xmax = gr->GetX()[locmax]; ```
Rene

Thanks Rene!

Dear Roort experts;

I have a problem about getting a TH1F from a TGraph.
I have a file which is read a …dat file and draw a TGraph. But I want to convert this TGraph to TH1F and I want to have an output graph as TH1F.
Is it possible? I am new at ROOT so can you help and explain me how can I do this?
Thank you so much from now.

[quote] But I want to convert this TGraph to TH1F [/quote]To what purpose do you need to do this?

[quote]I have a file which is read a …dat file and draw a TGraph[/quote]Rather than going from the text file to a TGraph, I recommend that you go from the text to a TTree (TTree::ReadFile) from which you will easily be able to extract either the TGraph or the TH1 as needed.

Cheers,
Philippe.