Convert TGraph to TH1 histogram

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 :slight_smile:
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); // ?
}
4 Likes