Retrieving Histogram from TGraph2D - Binning problem


Retrieving a TH2D histogram from a TGraph2D, how can I fix the bin number of the histogram?

It seems that by default the fHistogram from TGraph2d merges several points into one bin but I would like to have 1 value per bin (number of bins = equal to the number of points in my graph)

For example, in the image I join, the left plot corresponds to an histogram filled with the variables of my tree and the right plot corresponds to the histogram from the TGraph2d which is initially filled with the same variables as the histogram. Is the difference be coming only from this binning difference?

I would like to get the exact same histogram in both cases. The reason is I then need to apply a rotation which can not be applied to an histogram.

Thank you for your help,

Pierre Saouter
Diff_Histo_HistoFromGraph.pdf (47.4 KB)

This is explained here:

SetNpx and SetNpy

Thank you.

I always read carefully the literature but missed it this time. Sorry.

However, I am still getting very small differences in the two histograms, is this something I should expect?

On another hand, when I apply a rotation on the variables of my graph, I completely loose the properties of my source (shape distorted). Obviously I am doing something wrong…

I join a piece of code where I define and fill my two graphs, one without any rotation and the second with a rotation.


Piere Saouter


    TGraph2D *GraphNoOrientation = new TGraph2D();
TGraph2D *GraphOrientation = new TGraph2D();

GraphNoOrientation -> SetNpx(100);
GraphNoOrientation -> SetNpy(30);

GraphOrientation -> SetNpx(100);
GraphOrientation -> SetNpy(30);

// t is my tree containing the variables RawX,RawY,Intensity

     int nentries = (int)t->GetEntries(); 

    double theta = -0.495; // (TMath::Pi()/180)*(28.4°); 
    double costet = TMath::Cos(theta); 
    double sintet = TMath::Sin(theta); 

for (int i=0; i<=nentries; i++)

RawX2 = RawX*costet-RawY*sintet;
RawY2 = RawX*sintet+RawY*costet;


TH2D *Source0 = new TH2D(“Source0”,“Histo From Graph”,100,0,99,30,0,29);
TH2D *Source = new TH2D(“Source”,“Histo From Graph with Rotation”,100,0,99,30,0,29);

Source0 = (TH2D*)GraphNoOrientation->GetHistogram(“fHistogram”);
Source = (TH2D*)GraphOrientation->GetHistogram(“fHistogram”);


Have you tried to plot the graph2d using TRI1 ? in that case the graph is drawn directly (not the resulting histogram) and you see exactly the points you put in the graph.

With the Drawing option TRI1, the result of the rotation seems again to be more than just a rotation but also some change in the z and y scale. It looks like the problem comes from some binning issue, as if the binning was not the same for the rotated graph.

Anyhow, I need to get an histogram at the end to be able to use projections on the axis. Or is this a feature allowed on a TGraph2D?



With TRI1 there is no bin involved…
Can you send me a small macro (which I can run) reproducing what you describe here ?

Here is a small code GraphToHisto.c that compiles, and the root file used, PWN.root
PWNData.root (9.57 KB)
GraphToHisto.c (3.27 KB)

With tri1 I get the attached picture.
I see you do the rotation yourself on the individual point.
Is that picture correct ? it looks ok for me.
Then the interpolation on the histogram is just done by looking at the Z value of the triangle above each bin.

BTW I get:

Warning in TGraphDelaunay::Interpolate: Two of these three points are coincident 3001 2970 3000
Warning in TGraphDelaunay::Interpolate: Two of these three points are coincident 3001 2970 3000

Also one of the include file is wrong. You typed TGraph2d instead of TGraph2D

Yes indeed, I get the same result for the graphs. Perhaps it is just an effect of perspective when looking at the graphs but it seemed to me, for instance, that the height of the biggest peak was not the same after the rotation. The z scale has changed as well.

But even though the differences for the graphs are perhaps minor, the retrieved histograms are very different (I join the plot).

I also get the warnings but for different points:

Warning in TGraphDelaunay::Interpolate: Two of these three points are coincident 2969 3001 3000
Warning in TGraphDelaunay::Interpolate: Two of these three points are coincident 2970 3001 3000

I’ll try fixing these.

Perhaps I should try adopting another method for what I want to do. The “source” you see in the contz plot is not aligned to the axis and I need to get it aligned in order to use ProjectionX/Y to perform 1d histogram fits. The final goal is to perform a 2 dimensional fit of this source.

Does the method adopted until now seem reasonable to you? Is there another way to apply a rotation than to do it manually?

Thank you,

Pierre Saouter
OrientationCompar_contz.pdf (38.1 KB)

Actually one things which might be wrong is the Z scale because the graph exit in the case of rotated view. See the attached image. I’ll checked… but the content looks the same.

I suggest you do


I think I found the solution for the z scale. It has to do with setting the number of points in the graphs for the binning of the retrieved histograms.


GraphOrientation -> SetNpx(200);
GraphOrientation -> SetNpy(60);

instead of

GraphOrientation -> SetNpx(100);
GraphOrientation -> SetNpy(30);

, i.e, twice the number of points for the oriented graph, you end up with much closer results.

Now, I just have to figure out what is the rule of thumb to choose the correct SetNpx/Npy() values after the rotation.

Thanks for your help!