Suggestion for new TGraph Constructor

Hello, I notice that TVector objects have a .Draw() method that makes a graph for visualization, with automatic X-values from the index, and Y-values from the values in the TVector itself.

I am hoping that we can add a constructor for TGraph that duplicates this behavior, so that one can do

TVectorD tv(10);
for(Int_t i = 0;i<10;i++){tv[i] = i*i+3;}
TGraph tg(10,tv);

and that the resulting TGraph would look like the result of tv.Draw().

Is this possible?

Jean-François

Hi jfcaron,

did you know that a TGraph can already be constructed from the TH1 produced by TVector::Draw()?

TVector tv(10);
tv.Draw();

h = static_cast<TH1*>(gPad->FindObject("TVectorF");
TGraph gr(h);

Getting the object out of the gPad isn’t really pretty, but at least it also works in batch mode. If there is an easy way to construct an index vector of tv in CINT (not sure) this could be even easier since a TGraph can be constructed from the x and y coordinates, and here the x coordinates would just be the vector indices.

TVector tv(10);
TGraph gr(magic_index_tvector(tv.GetNoElements()), tv);

Right, if I made a simple function like

TVectorD index_vector(TVectorD tv)
{
  Int_t N = tv.GetNoElements();
  TVectorD index(N);
  for(Int_t i = 0;i<N;i++)
  {
    index[i] = i;
  }
  return index;
}

Then I could just feed it into the expected X parameter of the TGraph constructor. Given the plethora of convenience functions in ROOT, and the fact that TVector.Draw() already does this, I think it’s reasonable to add a TGraph constructor for this purpose.

Jean-François