How to push data to vector from branch tree

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

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

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

how push data from the needed branch?

#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;
}
1 Like