TGraph2D: need number of bins less than 4 x 4

How can I bypass limitation in TGraph2D, that the number of bins along each direction cannot be less than 4?
I’m using 6.08/06 on Fedora 24.
Thanks for your recipe!

Hi Anatoly,

TGraph2D does not have bins, you can set as many points as you wish: does this answer your question? What am I missing?


Hi Danilo,
There are bins in TGraph2D. You can set number of bins by

    TGraph2D* gr2D = new TGraph2D(npx*npy);

and each of them is depicted in its own colour when you

The problem is that inside the code of TGraph2D.cxx
SetNpx (and SetNpy) contains restriction:

 1649   if (npx < 4) {
 1650       Warning("SetNpx", "Number of points must be >4 && < 500, fNpx set to 4");
 1651       fNpx = 4;

How can I replace with my own version of that function in my code?

You are interested in: TGraph2D::GetN
Try: { TGraph2D *g = new TGraph2D(); std::cout << g->GetN() << std::endl; g->SetPoint(g->GetN(), 1, 1, 1); std::cout << g->GetN() << std::endl; // uncomment lines below one by one and see what you get // g->SetPoint(g->GetN(), 2, 2, 2); std::cout << g->GetN() << std::endl; // g->SetPoint(g->GetN(), 3, 3, 3); std::cout << g->GetN() << std::endl; // g->SetPoint(g->GetN(), 4, 4, 4); std::cout << g->GetN() << std::endl; // g->SetPoint(g->GetN(), 5, 5, 5); std::cout << g->GetN() << std::endl; g->SetMarkerStyle(20); g->Draw("LINE PCOL Z"); }
BTW. See the “TGraph2D Class Reference” for descriptions of methods.

1 Like

Following your suggestion, the plot consists of enormous amount of tiny squares, while what I need is only four such squares. It could be achieved by setting that number by hand (see above), but the problem is that it works only down to 4. You cannot set g->SetNpx(2), for example. Try it with your small example code and you will see what happens.
Also please see lines 1649 … of TGraph2D.cxx.

In other words, a workaround could be to replace SetNpx with my own version, which naively could be

#include <TGraph2D.h>
void SetNpx(Int_t npx)
    fNpx = npx;
void test_code_replacement()
    TGraph2D* gr2d = new TGraph2D();

But that obviously does not compile.

So what is correct way to do a replacement of such kind?

Let me clarify that my use case is absolutely real: I need to depict degree of misalignment of four mirror segments of a RICH optical system in one of the LHC experiments for the shifters’ awareness… For that I need a graph consisting of four bins (2x2).


Use a 2D histogram instead.
For example: { TH2D *h = new TH2D("h", "my beautiful 4 bins;X axis;Y axis;Z axis", 2, -10, 10, 2, -10, 10); h->SetStats(kFALSE); // no statistics box h->SetBinContent(1, 1, 1.0); h->SetBinContent(1, 2, 2.0); h->SetBinContent(2, 1, 3.0); h->SetBinContent(2, 2, 4.0); TCanvas *c = new TCanvas("c", "rich harvest"); c->SetRightMargin(0.12); // so that we see the "Z" color palette description h->Draw("LEGO2Z"); // "LEGO2Z" or "COLZ" }

This is the clue, indeed. Thank you very much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.