# Suggestion for new TGraph Constructor

**URL:** <https://root-forum.cern.ch/t/suggestion-for-new-tgraph-constructor/15198>\
**Category:** ROOT\
**Created:** [November 5, 2012, 6:09pm UTC](https://root-forum.cern.ch/t/suggestion-for-new-tgraph-constructor/15198 "2012-11-05T18:09:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfcaron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jfcaron](https://root-forum.cern.ch/u/jfcaron)\
**Post date:** [November 5, 2012, 6:09pm UTC](https://root-forum.cern.ch/t/suggestion-for-new-tgraph-constructor/15198/1 "2012-11-05T18:09:08Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![honk](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/honk/32/11273_2.png) [@honk](https://root-forum.cern.ch/u/honk)\
**Post date:** [November 6, 2012, 8:27am UTC](https://root-forum.cern.ch/t/suggestion-for-new-tgraph-constructor/15198/2 "2012-11-06T08:27:45Z")

</div>

Hi jfcaron,

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

```auto
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.

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

---

<div class="post-metadata">

**Author:** ![jfcaron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jfcaron](https://root-forum.cern.ch/u/jfcaron)\
**Post date:** [November 6, 2012, 4:56pm UTC](https://root-forum.cern.ch/t/suggestion-for-new-tgraph-constructor/15198/3 "2012-11-06T16:56:13Z")

</div>

Right, if I made a simple function like

```auto
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
