Could use std::vector<double> to build a graphs


ROOT Version: 6.28
Platform: Windows 10
Compiler: visual studio


Could use std::vector to build a graphs, and give me a example?

TGraph constructors are listed here.

Yes you can definitely do this!
A TGraph has constructors which take arrays as arguments.
For example:

TGraph::TGraph ( Int_t n,
const Double_t * x,
const Double_t * y
)

So if you had:
vector<double> v = {7,8,9};
Then you could use those points as y-values in a TGraph like this:
TGraph g(v.size(), v.data()); // create a TGraph with default x-values
which would make a TGraph with the points (0,7), (1,8), and (2,9).

If you want to specify the x and y values, you would need 2 vectors x and y
TGraph g(x.size(),x.data(), y.data()); //assuming x.size() == y.size()

Hope this helps!
Best,
Andrew

1 Like

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