Vector and TGraph

Hello,

I have a question that haunts me since a while about TVectorT for a TGraph utilisation.
I would like to create TGraph without having to declare the size so I thought the TVectorT constructor was what I was looking for.
However it seems that you need to specify the size of your TVectorT. At least it seems since I get the error:
Error in <TVectorT::operator()>: Request index(0) outside vector range of 0 - 0

So my question is:
What is the goal of TVectorT if it doesn’t have the features of the classic vector object. I mean without having the possibility to use the handy push_back function and of not specifying sizes of the vectors.
(Another way of formulating the question is: Is there a handy “push_back” method and a way of not specifying sizes of the vectors ??)
I’m under the feeling that I missed some tricks about the use of this class.

Thank you very much.
Benoit

I do not see any problem using TVectorF or TVectorD (concrete instances of TVectorT) with TGraph.
see example below

Rene

#include "TVectorD.h" #include "TGraph.h" void tgv() { TVectorD x(2); x[0]=1.1; x[1]=1.2; TVectorD y(2); y[0]=2.1; y[1]=2.2; TGraph *g = new TGraph(x,y); g->SetMarkerStyle(21); g->Draw("alp"); }

Thank you for the answer and sorry if I wasn’t clear.
In your example, you could as well has used classic arrays (double x[2]:wink:
For me the interest of TVectorF was that it suppress the need to indicate the size of the vector( as does a vector array). If it doesn’t, then I don’t understand why to use them instead of classic arrays.

I do not understand your remark. Where do you see the vector length specified in the TGraph constructor using TVectorD ?

Rene

The problem is not TGraph but TVectorF. You specify the size of your vector before ( TVectorD x(2):wink: If you need to know the exact size of your vector, I do not understand the value of the vector comparing to array.

Hi,

TVectorF and TVectorD primary purpose is within the context of matrix algebra and are indeed fixed sizes.

Philippe.

Ok I understand. TVectorT is not the equivalent of “classic” vector in c++. Main problem with TGraph is it does not support vector, so we need to convert our vector in array with something like that

vector<float> vx; vector<float> vy; (one fills both vectors) int N = vx.size(); float x[N]; float y[N]; for (int i=0 ; i<N ; i++) { x[i]=vx[i]; y[i]=vy[i]; } TGraph *g = new TGraph(N,x,y);
Is there any method to write directly

?
Thanks

Hi,

vector a;
vector b;
//fill your vectors

TGraph g(a.size(), &(a[0]), &(b[0]));

a std::vector is at its heart an array. To get the array just get the address of the first element.

Cheers,
Justin

Ok, thanks a lot. :smiley:

Thanks for the hint
I could then define a way to convert a C++ vector into a TVectorD

std::vector x;

… fill-in x;

TVectorD VX; VX.Use(x.size(),&(x[0]));