Convert from TGraph to histogram

I see, in the last example you sent, that you are not using GetBinCenter as suggested before. Here is another version of the macro using GetBinCenter:

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 = 100;
   double x1 = 18. , x2 = 152.;
   double bc;
   auto   h  = new TH1D("h","h",nb,x1,x2);

   for (int b=1; b<=nb; b++) {
      bc = h->GetBinCenter(b);
      h->SetBinContent(b, 25.*gr->Eval(bc));
   }

   h->Draw("hist");
}

Dear Olivier,
Please, is there a way we can sum up the bin content and divide it by the number of bins?

Regards,
Gideon

  [...]
   double bcont, ab = 0;
   for (int b=1; b<=nb; b++) {
      bc = h->GetBinCenter(b);
      bcont = 25.*gr->Eval(bc);
      ab = ab + bcont;
      h->SetBinContent(b, bcont);
   }
   printf("Sum of the bin content divided by the number of bins = %g\n",ab/nb);
   [...]

Dear Olivier,
Thanks for your help.

With kind regards,
Gideon

Dear Olivier,
Sorry to bother you again.
Please, how do I do bin-by-bin multiplication for the previous code?

With kind regards,
Gideon

That’s what is already done when we do:

bcont = 25.*gr->Eval(bc);
h->SetBinContent(b, bcont);

Each bin is multiplied by 25.

Dear Olivier,
No. I want to multiply each of the bins together and sum up and multiply by 25

Regards,
Gideon

something like: \prod_{i=1}^{n} b_i
where:
n is the number of bin.
b_i the content of the bin number i

?

something like: 25\sum_{i=1}^{n} b_i
where:
n is the number of bin.
b_i the content of the bin number i

?

can you explain, if that’s what you want, how to combined these 2 formulae ?

Dear Olivier,
The bin gives a value on the cross-section axis with its corresponding energy value at the energy axis.
I want to multiply each of them do a summation of them sum and multiply the result by 25.

With kind regards
Gideon

You said:

I want to multiply each of them do a summation of them sum and multiply the result by 25.

Let me try to identify the various steps to be done:

I want to multiply each of them \rightarrow by what ?

do a summation of them sum \rightarrow \sum_{i=1}^{n} b_i ?

and multiply the result by 25. \rightarrow this I understand

Does that mean you get a single number at the end of the process?

Dear Olivier,
Yes, please. I have to get a single number at the end.
Each bin is supposed to multiply its corresponding energies and sum up to get a number and multiply by 25.

Regards,
Gideon

you said:
Each bin is supposed to multiply its corresponding energies

This is still unclear. What multiplication should we do for each bin ?

I suppose we have to multiply each bin’s content by something?
What is the something ?
Is it the corresponding energy ?
If that’s the case, where should we find it ? is it bins’ centers on the x-axis ?

If that’s the case (to be checked), the loop should be something like that:

  [...]
   double bincenter, bincontent, sum = 0;

   for (int b=1; b<=nb; b++) {
      bincenter  = h->GetBinCenter(b);
      bincontent = gr->Eval(bincenter);
      h->SetBinContent(b, bincontent);
      sum = sum + bincontent*bincenter;
   }
   double result = sum*25.;
   [...]

Dear Olivier,
Let me try this and see if it will do what I am looking for.
The attached formula is what I am trying to do using the energy and the cross-section data

Ok. The question is now: “Where to find the various parts of this formula in your data ?”.
I think that’s only you who know that. That’s your data you know them…

Dear Oliver,
The effective cross-section for carbon is 17.3388 mb
phi experiment is 5.39 x10^10. I am not adding PN.

Ok, encode that is C++. You should know where to find the various members of this formula. Again that’s your data not mine…

Okay. I will check

Thanks

Dear Olivier,
Please, the information is clear now. we have to multiply 17.3388 mb by each of the bins and sum them up. then the result can be multiplied by 25. I hope this is clear now.

Regards,
Gideon

void gideon(){
   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);

   int    NumberOfBins = 50;
   double XLow = 18. , XHigh = 152.;
   auto   h  = new TH1D("h","h",NumberOfBins,XLow,XHigh);

   double BinContent, BinCenter, Result = 0;

   for (int Bin=1; Bin<=NumberOfBins; Bin++) {
      BinCenter  = h->GetBinCenter(Bin);
      BinContent = gr->Eval(BinCenter);
      Result = Result + 17.3388*BinContent;
      h->SetBinContent(Bin, BinContent);
   }
   Result = 25*Result;
   printf("Result = %g\n",Result);

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

root [0] .x gideon.C
Result = 8.86793

Dear Olivier,
Thanks

I appreciate it

Regards,
Gideon