TGraph constructor with STL vectors?


since TGraph seems to me the only way to draw exact 2D histos (without the points beeing randomly distributed in the bins), I frequently use them.

And I am missing all the time the possibility to create TGraph from vectors <>, avoiding the use of staticly defined arrays.

Is there a way to use dynamic memory objects with Tgraphs?



You can already use a vector with a TGraph:

vector<double> x,y; TGraph gr( v.size(), &(x[0]), &(y[0]) );

[quote]Is there a way to use dynamic memory objects with Tgraphs? [/quote]What do you mean?


1 Like


I think he meant if you can define arrays dynamically like this:

Double_t *time = new Double_t [npoints];
Double_t *xpos = new Double_t [npoints];

And later use them with TGraph like this:

TGraph *x_vs_t = new TGraph( npoints , time , xpos  );

I tried your suggestion for the STL vectors with this kind of dynamically defined array, but it didn’t work. I get no apparent errors (with either your syntax or mine) but my canvas is empty!..and I’ve checked my arrays contain the data I need.

I guess I can try using vectors instead, but they might be slower…

EDIT: typo

UPDATA/EDIT: pcanal’s method using STL vectors works indeed.

Using the new operator also works fine, only there is no need to use &(x[0]) . Merely typing x works.