Method "AddPoint" for TGraph doesn't work


this is my simple code and when I try compile it I get "no member named “AddPoint” to "“TGraph” ". Where is the error hiding?

TGraph* gr1 = new TGraph();

TCanvas* c1 = new TCanvas("c1","Paint", 1000, 10, 1000, 700);

TRandom3* r = new TRandom3;

double GetX(double& v1, double& v2);
double GetY();
bool Neumann(double& x, double& y);

void DrawAll();
void DrawFunctions();

vector<double> Walls;

int NumberPoint = 10000;

void QuestOne()
  cout << "Start main function" << endl;
  double FirstWall, SecondWall;
  int N = 1;
  cout << "Walls start build" << endl;
  while( Walls.back() != 10 )
    Walls.push_back(Walls.back() + 20./N);
  cout << "Walls are built\nStart to born points" << endl;
  for ( int i = 0; i < N; i++ )
      int NShoot = 0;
      while( NShoot < NumberPoint/N )
          double x = GetX( Walls[i], Walls[i+1] );
          double y = GetY();
          if ( Neumann( x, y ) )
  cout << "End main function" << endl;

double GetX(double& v1, double& v2)
  return v1 + (v2 - v1)*r->Rndm();

double GetY()
  return 4 - 4*r->Rndm();

bool Neumann(double& x, double& y)
  return y < 1/sqrt(2*TMath::Pi())*exp(-pow(x,2)/2);

void DrawAll()

void DrawFunctions()
  TF1* MyGausn = new TF1("MyGausn","1/sqrt(2*TMath::Pi())*exp(-pow(x,2)/2)", -10, 10);


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.19/01
Platform: Ubuntu
Compiler: Not Provided

Hi Svyatoslav,

TGraph::AddPoint method introduced recently and will be available first in 6.24 release begin of next year. Of course, you can use it already now, but with current master branch of ROOT.


If you using older ROOT version, you can use TGraph::SetPoint method, providing appropriate index. Necessary number of points will be automatically add to TGraph

Do I have to keep track of the indexes myself? And more. How can I put a dot on the graph? In principle, I don’t need to store points, I just need them to be plotted on the graph after generation.

Yes, you need to keep track of index yourself - but it is very simple.
To draw TGraph as markers, call gr1->Draw("AP"), see documentation

Do I have to keep track of the indexes myself?

Typically you can just pass graph.GetN() as the index.

This solution have a problem. I don’t know how mane points there will be. SetPoint requires point number.

“point number” is the number of that point, not the total number of points.

g.SetPoint(g.GetN(), 0, 0);
g.SetPoint(g.GetN(), 1, 2);

should work.

It work. thanks

And one more question. What do I need to make Rndm () give a new number every time the program starts?

You have to initialize the seed of the random number generator to a different number every time, e.g. based on the linux epoch. The call is gRandom->SetSeed(...), see e.g.

Yes, I know it and use out Root time(0), but I don’t know how it use with Rndm(). I add gRandom->SetSedd(time(0)) in my code, but it isn’t work.

What doesn’t work exactly? What’s the error?

This works:

$ cat set_seed.C
void set_seed() {
   std::cout << gRandom->Rndm() << '\n';

$ root -l -b -q set_seed.C
Processing set_seed.C...

$ root -l -b -q set_seed.C
Processing set_seed.C...

$ root -l -b -q set_seed.C
Processing set_seed.C...

I don’t get an error. Simple get same numbers.

And I use a different scheme. Look my code: in this TRandom3* r = new TRandom3 and then r->Rndm(). And for my scheme adding gRandom->SetSeed(time(0)) doesn’t work.

If you don’t use gRandom to extract random numbers, but r, then you probably want r->SetSeed(...) :slight_smile:

It works :upside_down_face: thanks :relaxed:

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