# When we can create a Tgraph with Tree

**URL:** <https://root-forum.cern.ch/t/when-we-can-create-a-tgraph-with-tree/45193>\
**Category:** Newbie\
**Tags:** tree\
**Created:** [June 1, 2021, 2:10pm UTC](https://root-forum.cern.ch/t/when-we-can-create-a-tgraph-with-tree/45193 "2021-06-01T14:10:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![IKoro](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/3e96dc/32.png) [@IKoro](https://root-forum.cern.ch/u/IKoro)\
**Post date:** [June 1, 2021, 2:10pm UTC](https://root-forum.cern.ch/t/when-we-can-create-a-tgraph-with-tree/45193/1 "2021-06-01T14:10:20Z")

</div>

Hello,  
I don’t understand why my program didn’t function.  
It is possible to create a graphe with TGraphe with Tree data ?

```auto
void OMline()
{
  gStyle->SetOptStat(0);
  gStyle->SetOptFit(1111);
  //gStyle->SetMarkerSize(3);

TCanvas *c1 = new TCanvas("c1","",0,10,0,500000);

TFile *input = new TFile("/home/kdupuy/Documents/fichier_root/valeurspics2.root","READ");

TTree *tree = (TTree*)input->Get("valeurspics2_tree;1");

double Constant1, Mean1, Sigma1, om_number1;
int pics, intensity;
Double_t error1, error2, error3;
Double_t x[6], y[6],ex[6],ey[6];
Int_t n = 6;

tree->SetBranchAddress("om_number1", &om_number1);
tree->SetBranchAddress("Constant1", &Constant1);
tree->SetBranchAddress("Mean1", &Mean1);
tree->SetBranchAddress("Sigma1", &Sigma1);
tree->SetBranchAddress("pics", &pics);
tree->SetBranchAddress("intensity", &intensity);
tree->SetBranchAddress("error1", &error1);
tree->SetBranchAddress("error2", &error2);
tree->SetBranchAddress("error3", &error3);

int entries = tree->GetEntries();

for (int j = 301; j <= 301; j++ )

{
  //TH2D *hist7 = new TH2D("hist7", "",1000, 60, 130, 1000, 0, 500000);

  TF1 *fit7 = new TF1("fit7", "pol1", 0, 400000);

  TGraph* gr = new TGraphErrors(n,x,y,ex,ey);

for(int i=0; i < entries; i++) // rentre les valeurs de la branche dans notre variable

    {
      tree->GetEntry(i);

      if (om_number1 == j)
      {
      //cout << Mean1 << pics << endl;
      //hist7->Fill(intensity,Mean1);
      for (Int_t i=0;i<n;i++)
      {
        cout << intensity << endl;
        x[i] = intensity;
        y[i] = Mean1;
        ex[i] = error2;
        ey[i] = 0;
      }
      }
    }

  //TGraph* gr = new TGraph(n,x,y,ex,ey);

  gr->Draw("");

  //gr->Fit("fit7","R");
//hist7->SetErrorX(0.5);
// hist7->SetMarkerStyle(20);
// hist7->GetXaxis()->SetTitle("Intensite LED (U.A)");
// hist7->GetYaxis()->SetTitle("Charge moyenne (U.A)");
// hist7->GetXaxis()->SetTitleSize(0.04);
// hist7->GetYaxis()->SetTitleSize(0.03);
// hist7->GetXaxis()->SetLabelSize(0.05);
// hist7->GetYaxis()->SetLabelSize(0.05);
// hist7->Draw("");

//c1->Print(Form("/home/kdupuy/Documents/Line/%03i1.png", j));

	//delete hist7;
}
}

```

[valeurspics2.root](https://root-forum.cern.ch/uploads/short-url/6xID52tdVbYwolMdQIVTt7rPlRJ.root) (35.7 KB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 1, 2021, 2:31pm UTC](https://root-forum.cern.ch/t/when-we-can-create-a-tgraph-with-tree/45193/2 "2021-06-01T14:31:29Z")

</div>

Your macro cannot work as it was. I clean up a bit as follow:

```auto
void OMline() {
   gStyle->SetOptStat(0);
   gStyle->SetOptFit(1111);

   TFile *input = new TFile("valeurspics2.root","READ");

   TTree *tree = (TTree*)input->Get("valeurspics2_tree;1");

   double Constant1, Mean1, Sigma1, om_number1;
   int pics, intensity;
   Double_t error1, error2, error3;
   Double_t x[6], y[6],ex[6],ey[6];
   Int_t n = 6;

   tree->SetBranchAddress("om_number1", &om_number1);
   tree->SetBranchAddress("Constant1", &Constant1);
   tree->SetBranchAddress("Mean1", &Mean1);
   tree->SetBranchAddress("Sigma1", &Sigma1);
   tree->SetBranchAddress("pics", &pics);
   tree->SetBranchAddress("intensity", &intensity);
   tree->SetBranchAddress("error1", &error1);
   tree->SetBranchAddress("error2", &error2);
   tree->SetBranchAddress("error3", &error3);

   int entries = tree->GetEntries();

   for (int j = 301; j <= 301; j++ ) {
      TF1 *fit7 = new TF1("fit7", "pol1", 0, 400000);
      for (int i=0; i < entries; i++) {
         tree->GetEntry(i);
         if (om_number1 == j) {
            for (Int_t i=0;i<n;i++) {
               cout << intensity << endl;
               x[i] = intensity;
               y[i] = Mean1;
               ex[i] = error2;
               ey[i] = 0;
            }
         }
      }
      TGraph* gr = new TGraphErrors(n,x,y,ex,ey);
      gr->Draw("AL");
   }
}

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 1, 2021, 3:27pm UTC](https://root-forum.cern.ch/t/when-we-can-create-a-tgraph-with-tree/45193/3 "2021-06-01T15:27:24Z")

</div>

Indeed this macro makes no sense because only the last graph is plotted I propose you a new version using TMultiGraph which make more sense.

```auto
void OMline() {
   gStyle->SetOptStat(0);
   gStyle->SetOptFit(1111);

   TFile *input = new TFile("valeurspics2.root","READ");

   TTree *tree = (TTree*)input->Get("valeurspics2_tree;1");

   double Constant1, Mean1, Sigma1, om_number1;
   int pics, intensity;
   Double_t error1, error2, error3;
   Double_t x[6], y[6],ex[6],ey[6];
   Int_t n = 6;

   tree->SetBranchAddress("om_number1", &om_number1);
   tree->SetBranchAddress("Constant1", &Constant1);
   tree->SetBranchAddress("Mean1", &Mean1);
   tree->SetBranchAddress("Sigma1", &Sigma1);
   tree->SetBranchAddress("pics", &pics);
   tree->SetBranchAddress("intensity", &intensity);
   tree->SetBranchAddress("error1", &error1);
   tree->SetBranchAddress("error2", &error2);
   tree->SetBranchAddress("error3", &error3);

   int entries = tree->GetEntries();

   auto mg = new TMultiGraph();

   int j = 301;
   TF1 *fit7 = new TF1("fit7", "pol1", 0, 400000);
   for (int i=0; i < entries; i++) {
      tree->GetEntry(i);
      if (om_number1 == j) {
         for (Int_t i=0;i<n;i++) {
            x[i] = intensity;
            y[i] = Mean1;
            ex[i] = error2;
            ey[i] = 0;
         }
         TGraph* gr = new TGraphErrors(n,x,y,ex,ey);
         mg->Add(gr);
      }
   }
   mg->Draw("AL");
}

```

 ![Screenshot 2021-06-01 at 17.24.28](https://root-forum.cern.ch/uploads/default/original/3X/e/2/e2b602d6365fae20baf568cdad0fe2836b95cb2d.jpeg)

---

<div class="post-metadata">

**Author:** ![KevinWang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/ebca7d/32.png) [@KevinWang](https://root-forum.cern.ch/u/KevinWang)\
**Post date:** [June 2, 2021, 6:08am UTC](https://root-forum.cern.ch/t/when-we-can-create-a-tgraph-with-tree/45193/4 "2021-06-02T06:08:41Z")

</div>

What about this:

```auto
void OMline()
{
    gStyle->SetOptStat(0);
    gStyle->SetOptFit(1111);

    TCanvas *c1 = new TCanvas("c1","",0,10,0,500000);

    TFile *input = new 
    TFile("/home/kdupuy/Documents/fichier_root/valeurspics2.root","READ");

    TTree *tree = (TTree*)input->Get("valeurspics2_tree;1");
    tree->SetEstimate(-1);
    for(int j=301; j<=301;j++)
    {
        int nData = tree->Draw("intensity:Mean1:error2", ("om_number1=="+to_string(j)).c_str(), "goff");
        double *x=tree->GetVal(0);
        double *y=tree->GetVal(1);
        double *ex=tree->GetVal(2);
        TGraphErrors *gr = new TGraphErrors(nData, x, y, ex);
        gr->DrawClone("AP*");
        gr->Fit("pol1");

       // TH2D *hist7 = new TH2D("hist7", "",1000, 60, 130, 1000, 0, 500000);
       // for(int i=0; i<nData; i++)
       // hist7->Fill(x[i], y[i]);
    }
    

```

---

<div class="post-metadata">

**Author:** ![IKoro](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/3e96dc/32.png) [@IKoro](https://root-forum.cern.ch/u/IKoro)\
**Post date:** [June 2, 2021, 8:33am UTC](https://root-forum.cern.ch/t/when-we-can-create-a-tgraph-with-tree/45193/5 "2021-06-02T08:33:16Z")

</div>

I don’t understand why, with my precedent programm, just the last graph is plotted ?  
But thank you for the new version

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 2, 2021, 8:40am UTC](https://root-forum.cern.ch/t/when-we-can-create-a-tgraph-with-tree/45193/6 "2021-06-02T08:40:00Z")

</div>

> [@IKoro](#):
>
> I don’t understand why, with my precedent programm, just the last graph is plotted ?

Because you were drawing the graph in a loop and only the last “Draw” was visible. They were overwriting each other.
