# Convert from TGraph to histogram

I have theory prediction with TGraph. And The data with TH1F … I want to convert from TGraph toTH1F ,then change bins ,and then divide the TH1F (data). How can I make this? I found the TGraph::GetHistogram methots, but I don’t get it … Could you help me for this?

Thanks…

Hi,

what you could do is to create a new “theory predicted” histogram and then divide. This histogram has a binning identical to the data one. The bin content is set via the TGraph::Eval method which takes care of the interpolation.

Danilo

Hi Danilo,

Thanks your answer. I create new histogram and this solve my problem. if you look very deep you can not see simple solutions Thanks…

Ayse,

[quote=“dpiparo”]Hi,

what you could do is to create a new “theory predicted” histogram and then divide. This histogram has a binning identical to the data one. The bin content is set via the TGraph::Eval method which takes care of the interpolation.

Danilo[/quote]

Hi Danilo,
Thanks

Dear @gbentum ,

There was no code in any of the previous post messages. If you want some references, feel free to look at the TH1 docs and TGraph docs. In particular, the methods mentioned were TH1::Divide, TGraph::Eval.

Cheers,
Vincenzo

Thanks. I am new to using ROOT. Could you please help me in using the eval method to loop over my
tgraph_array.C (1.4 KB)
points in the Tgraph and fill them into a histogram? I have
hist.txt (1.8 KB)
Attached is my Tgraph code and the data.

Hi @gbentum ,

I found this macro in my examples. That may help you maybe.

``````void graph2hist(){
double x[20], y[20];
int n = 20;
for (int i=0;i<n;i++) {
x[i] = i*0.1;
y[i] = 10*sin(x[i]+0.2);
}
auto g = new TGraph(n,x,y);
g->SetTitle(";X;Y");
g->Draw("AL");

int nb = 50;
double x1=0, x2=2.;
double dx = (x2-x1)/nb;
auto h = new TH1D("h","h",nb,x1,x2);
for (int b=1; b<=nb; b++) {
h->SetBinContent(b,g->Eval(x1+b*dx));
}
h->Draw("same hist");
}
``````

If I adapt the example I sent you to your case I get this:

``````void tgraph_array(){
double x[] = {18.722, 19, 19.5, 20, 20.5, 21, 21.5, 22 , 23, 23.5, 24, 24.5, 25, 25.5, 26, 26.5, 27, 27.5, 28, 28.5, 29, 29.5, 30, 32.5, 35, 37.5, 40, 45, 47.5, 50, 52.5, 55, 57.5, 60, 62.5, 65, 72.5, 75, 77.5, 80, 85, 87.5, 90, 100, 102.5, 105, 107.5, 110, 112.5, 115, 117.5, 120, 122.5, 125, 127.5, 130, 132.5, 135, 137.5, 140, 142.5, 145, 147.5, 150.};
double y[] = {0.0, 0.000093273, 0.000354167, 0.0006875, 0.00137322, 0.00278194, 0.00496428, 0.00567111, 0.00650744, 0.00676991, 0.00562713, 0.00297917, 0.00366927, 0.004125, 0.003355, 0.00271212, 0.00231861, 0.00235417, 0.0020875, 0.00176833, 0.00163426, 0.00165357, 0.00157744, 0.00127633, 0.00104167, 0.000895011, 0.000775807, 0.000566122, 0.000458931, 0.000357328, 0.000294003, 0.000251503, 0.000199038, 0.000149, 0.00011862, 0.000105919, 0.0000712972, 0.0000599773,  0.0000507858, 0.000045, 0.0000362928, 0.0000315124, 0.0000275856, 0.000017, 0.0000146941, 0.0000129529, 0.0000116025, 0.0000104687, 0.00000941879, 0.00000846238, 0.00000763881, 0.00000698742, 0.00000643988, 0.00000592057, 0.00000545785, 0.0000050801, 0.00000475711, 0.00000444586, 0.00000415553, 0.00000389529, 0.00000365601, 0.00000342634, 0.00000321208, 0.00000301905};

TGraph* gr =new TGraph(64,x,y);

gr->Draw("AL*");
gr->SetTitle(";Energy [MeV];Cross Section [barns]");

int nb = 100;
double x1=18., x2=152.;
double dx = (x2-x1)/nb;
auto h = new TH1D("h","h",nb,x1,x2);
for (int b=1; b<=nb; b++) {
h->SetBinContent(b,gr->Eval(x1+b*dx));
}
h->Draw("same hist");
}
``````

Hello Vincenzo,
Thanks. Problem solved.

Hello Vincenzo,
Sorry to bother you again.
Please, is there a way that I can get the points at the bin centre of the histogram?

Hi Vincenzo,
Please from my previous code, there is another thing I want to do.
Please I want to multiply each bin of the histogram by a constant value of 25.

Hi @gbentum
Simply do:

``````      h->SetBinContent(b, 25*gr->Eval(x1+b*dx));
``````

Cheers,
Olivier

Dear Olivier,
Thanks

Regards,
Gideon

Dear Olivier,

From the code I posted, I want a loop that will multiply each of the bins by 25.
Please, I have attached here with the output graph.

Thanks

Regards,
Gideon

TgraphHis.pdf (14.5 KB)

So it is not multiplied ? right ?
can you post the new code ?

Dear Olivier,
Please, attached here is the code.

With kind regards,
Gideon

tgraph_array.C (1.63 KB)

Here is the histogram multiplied by 25.

``````void tgraph_array(){
double x[] = {18.722, 19, 19.5, 20, 20.5, 21, 21.5, 22 , 23, 23.5, 24, 24.5, 25, 25.5, 26, 26.5, 27, 27.5, 28, 28.5, 29, 29.5, 30, 32.5, 35, 37.5, 40, 45, 47.5, 50, 52.5, 55, 57.5, 60, 62.5, 65, 72.5, 75, 77.5, 80, 85, 87.5, 90, 100, 102.5, 105, 107.5, 110, 112.5, 115, 117.5, 120, 122.5, 125, 127.5, 130, 132.5, 135, 137.5, 140, 142.5, 145, 147.5, 150.};
double y[] = {0.0, 0.000093273, 0.000354167, 0.0006875, 0.00137322, 0.00278194, 0.00496428, 0.00567111, 0.00650744, 0.00676991, 0.00562713, 0.00297917, 0.00366927, 0.004125, 0.003355, 0.00271212, 0.00231861, 0.00235417, 0.0020875, 0.00176833, 0.00163426, 0.00165357, 0.00157744, 0.00127633, 0.00104167, 0.000895011, 0.000775807, 0.000566122, 0.000458931, 0.000357328, 0.000294003, 0.000251503, 0.000199038, 0.000149, 0.00011862, 0.000105919, 0.0000712972, 0.0000599773,  0.0000507858, 0.000045, 0.0000362928, 0.0000315124, 0.0000275856, 0.000017, 0.0000146941, 0.0000129529, 0.0000116025, 0.0000104687, 0.00000941879, 0.00000846238, 0.00000763881, 0.00000698742, 0.00000643988, 0.00000592057, 0.00000545785, 0.0000050801, 0.00000475711, 0.00000444586, 0.00000415553, 0.00000389529, 0.00000365601, 0.00000342634, 0.00000321208, 0.00000301905};

auto gr =new TGraph(64,x,y);

gr->SetTitle(";Energy [MeV];Cross Section [barns]");

int    nb = 50;
double x1 = 18. , x2 = 152.;
double dx = (x2-x1)/nb;
auto   h  = new TH1D("h","h",nb,x1,x2);

for (int b=0; b<=nb; b++) {
h->SetBinContent(b, 25.*gr->Eval(x1+b*dx-0.1222));
}

h->Draw("hist");
gr->Draw("L*");
}
``````

Dear Olivier,
Thanks. Please my number is +233541048442.

We can talk. You can kindly share your number.

Regards,
Gideon

I guess we can sort out this issue here. What is wrong in what I sent ? In the new example I sent, the contain of each bin is multiplied by 25 as you asked.