# 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:** 1\
**Showing post:** 2

<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;
}

```

---

_[View the full topic](https://root-forum.cern.ch/t/how-to-push-data-to-vector-from-branch-tree/32616)._
