Hello,
I want to convert a TGraph to a TH1 so that I can multiply two histograms bin by bin. What is the best way of accomplishing that?
Thank you
Hello,
I want to convert a TGraph to a TH1 so that I can multiply two histograms bin by bin. What is the best way of accomplishing that?
Thank you
What do you mean “converting a TGraph to a TH1”? I don’t think there is one clear meaning attached to this conversion: a point in a TGraph has x and y coordinates, a histogram is a collection of bins and their number of entries.
Also, in principle it would be much easier to just fill an histogram as you fill the TGraph
If your starting point must be a TGraph, then you can loop over all its points and fill a TH1 with those.
Assuming you want to fill TH1 with the x coordinate as histogram value and the y coordinate as weight (not tested):
TH1 h; // the histogram (you should set the number of bins, the title etc)
auto nPoints = graph.GetN(); // number of points in your TGraph
for(int i=0; i < nPoints; ++i) {
double x,y;
graph.GetPoint(i, x, y);
h->Fill(x,y); // ?
}
Thank you for your answer.
I have some data in the form of a TGraph and some data in the form of a TH1 histogram, but i want to multiply the TGraph data with the TH1 data, and so I was wondering how to make a histogram from the TGraph.
I’m getting an error when i try to run the for loop, it says:
<Error: Can’t call TGraph::GetPoint(i,&x,&y) in current scope (tmpfile):2:
Possible candidates are…
(in TGraph)
/mnt/research/NuInt/NUISANCE_new/genie_v2_10_10/lamp/GENIESupport/root/lib/libHist.so -1:-1 0 public: virtual Int_t TGraph::GetPoint(Int_t i,Double_t& x,Double_t& y) const;
*** Interpreter error recovered ***>
Any idea how to fix this?
Call instead graph.GetPoint(i, x, y);
It worked!
Thank you both
Ah yeah my snippet had two errors: the first is the one the compiler complained about: x
and not &x
(the address of x
) must be passed to TGraph::GetPoint
.
The other problem is that I created x
and y
as int
but you really want to make them double
!!!
I edited my original snippet to reflect this changes.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.