# How to push data to vector from branch tree

**URL:** <https://root-forum.cern.ch/t/how-to-push-data-to-vector-from-branch-tree/32616>\
**Category:** Newbie\
**Tags:** tree\
**Created:** [February 6, 2019, 11:16am UTC](https://root-forum.cern.ch/t/how-to-push-data-to-vector-from-branch-tree/32616 "2019-02-06T11:16:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anhelina](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anhelina/32/19529_2.png) [@anhelina](https://root-forum.cern.ch/u/anhelina)\
**Post date:** [February 6, 2019, 11:16am UTC](https://root-forum.cern.ch/t/how-to-push-data-to-vector-from-branch-tree/32616/1 "2019-02-06T11:16:05Z")

</div>

Hi,  
I am creating a function for plotting graph from tree.  
Something like this:

```auto
TGraph * DrawGraph(string xname, string yname, TTree* tree)
{
    float time = 0.0;//
    float NimplantOuterVoltage = 0.0;//
    float NimplantInnerVoltage = 0.0;//
    float NimplantQuasiFermiPotential = 0.0;//
    float NimplantDisplacementCurrent = 0.0;//
    float NimplanteCurrent = 0.0;//
    float NimplanthCurrent = 0.0;//
    float NimplantTotalCurrent = 0.0;//
    float NimplantCharge = 0.0;//

    tree->SetBranchAddress("time",&time);
    tree->SetBranchAddress("NimplantOuterVoltage",&NimplantOuterVoltage);
    tree->SetBranchAddress("NimplantInnerVoltage",&NimplantInnerVoltage);
    tree->SetBranchAddress("NimplantQuasiFermiPotential",&NimplantQuasiFermiPotential);
    tree->SetBranchAddress("NimplantDisplacementCurrent",&NimplantDisplacementCurrent);
    tree->SetBranchAddress("NimplanteCurrent",&NimplanteCurrent);
    tree->SetBranchAddress("NimplanthCurrent",&NimplanthCurrent);
    tree->SetBranchAddress("NimplantTotalCurrent",&NimplantTotalCurrent);
    tree->SetBranchAddress("NimplantCharge",&NimplantCharge);

    // push data to tree
    int entries = tree->GetEntries();
    vector <float> xvector;
    vector <float> yvector;
    for (Int_t i = -1; i <entries ; i++)
    {
        tree->GetEntry(i);
        xvector.push_back(NimplantInnerVoltage);
        yvector.push_back(NimplantTotalCurrent);
    }
    Float_t *x= &xvector[0];
    Float_t *y= &yvector[0];

    // plot graph
    //c1->cd();
    TGraph *g = new TGraph(entries, x, y);
    return g;
}

void TcadChoseBranch (string xname, string yname, char *dirname="output/", const char *ext="_des.csv") {

> read file and call function

TGraph * g = DrawGraph(xname,yname, tree);
}

```

problem with passing the name of brunch like a parameter to vector, because xname and yname is string

```auto
    {
        tree->GetEntry(i);
        xvector.push_back(xname);
        yvector.push_back(yname);
    }

```

how push data from the needed branch?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 6, 2019, 1:37pm UTC](https://root-forum.cern.ch/t/how-to-push-data-to-vector-from-branch-tree/32616/2 "2019-02-06T13:37:26Z")

</div>

```auto
#include "TTree.h"
#include "TGraph.h"
#include "TString.h"
#include "TPad.h"
#include "TMath.h"
#include <iostream>
TGraph *DrawGraph(const char *xname, const char *yname, TTree *tree,
                  const char *selection = "", const char *gname = "MyGraph") {
  TGraph *g = ((TGraph*)0);
  if (!(xname && xname[0] && yname && yname[0] && tree)) return g;
  if (!selection) selection = "";
  if (!(gname && gname[0])) gname = "MyGraph";
  tree->SetEstimate(-1); // keep all results (assumes one result per entry)
#if 0 /* 0 or 1 */
  tree->Draw(TString::Format("%s : %s", yname, xname), selection);
  if (gPad) g = ((TGraph*)(gPad->GetPrimitive("Graph")));
  if (g) {
    g = ((TGraph*)(g->Clone(gname))); // always return a "clone"
    g->SetTitle(TString::Format("%s;%s;%s", gname, xname, yname));
  }
#else /* 0 or 1 */
  tree->Draw(TString::Format("%s : %s", yname, xname), selection, "goff");
  if (tree->GetSelectedRows() > 0) {
    g = new TGraph(TMath::Min(tree->GetSelectedRows(), tree->GetEstimate()),
                   tree->GetV2(), tree->GetV1());
    g->SetNameTitle(gname, TString::Format("%s;%s;%s", gname, xname, yname));
  }
#endif /* 0 or 1 */
  if (tree->GetSelectedRows() > tree->GetEstimate()) {
    std::cout << "Warning : DrawGraph : increase SetEstimate and rerun : "
              << tree->GetEstimate() << " < " << tree->GetSelectedRows()
              << std::endl;
  }
  return g;
}

```
