# Plot graph from Tree with passing name branch as argument

**URL:** <https://root-forum.cern.ch/t/plot-graph-from-tree-with-passing-name-branch-as-argument/32222>\
**Category:** Newbie\
**Created:** [January 9, 2019, 1:59pm UTC](https://root-forum.cern.ch/t/plot-graph-from-tree-with-passing-name-branch-as-argument/32222 "2019-01-09T13:59:34Z")\
**Posts on this page:** 7\
**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:** [January 9, 2019, 1:59pm UTC](https://root-forum.cern.ch/t/plot-graph-from-tree-with-passing-name-branch-as-argument/32222/1 "2019-01-09T13:59:34Z")

</div>

I have some problem with passing arguments to my script.

I won’t plot graph from some tree and name of branch passing like arguments.

I know that can do it like that

> .x ‘anylize.cxx(“BSOuterVoltage”,“NimplantTotalCurrent”)’

but

> Error in TApplication::ExecuteFile: macro 'anylize.cxx not found in path .;C:\root\_v5.34.36/macros;

where I could make a mistake?

**my macro is in folder /macros;**

```auto

#include "Riostream.h"
#include "TString.h"
#include "TFile.h"
#include "TTree.h"
#include "TSystem.h"

TGraph * DrawGraph(string xname, string yname, TTree* tree)
{
    float first = 0.0;//
    float second = 0.0;//
    float third = 0.0;//
    float fourth = 0.0;//
  

    tree->SetBranchAddress("first",&first);
    tree->SetBranchAddress("second ",&second);
    tree->SetBranchAddress("third",&third);
    tree->SetBranchAddress("fourth",&fourth);
  

    // 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(first);
        yvector.push_back(second);
    }
    Float_t *x= &xvector[0];
    Float_t *y= &yvector[0];

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

void anylize(const char *dirname="output/", const char *ext=".csv", string xname, string yname)
 {
    new TBrowser();// dont start without it

    auto c1 = new TCanvas("canvas_01","spektrum",550,10,800,550);

    // genered a frame with an empty histogram
    TH1F *hframe = new TH1F("hframe","", 220, 0, 220);
    hframe->SetXTitle("First");
    hframe->SetYTitle("Second");
    hframe->GetYaxis()->SetTitleOffset(1.2);
    hframe->SetMinimum(0.0);
    hframe->SetMaximum(1500);
    hframe->SetStats(0);
    hframe->Draw();

    // Read all file from dir
    TSystemDirectory dir(dirname, dirname);
    TList *files = dir.GetListOfFiles();
    if (files) {
        TSystemFile *file;
        TString fname;
        TIter next(files);
        int j = 0;
        string xname;
        string yname;

        while ((file=(TSystemFile*)next())) {
            fname = file->GetName();
           cout << "Name " << fname << endl;// print name all files

            if (!file->IsDirectory() && fname.EndsWith(ext)) {
                TFile *f = new TFile(fname+TString(".root"), "RECREATE");// new root file
                TTree *tree = new TTree("ntuple", "data from csv file");
                tree->ReadFile((dirname + fname).Data(), "", ','); // read file, branch from firs line of file
                if(j == 0)
                {
                    tree->Print();

                }

                TGraph * g = DrawGraph(xname,yname, tree); // plot graph 
                g->SetMarkerStyle(20);
                g->SetMarkerColor(j+50);
                g->SetLineColor(j+20);

                if (j ==0 )
                {
                    g->Draw("LP");
                    cout << "HERE" << endl; // test
                }
                else
                {
                    g->Draw("LPsame");
                    cout << "HEREsame" << endl;// test
                }

                //c2->Print(fname.Data() + TString(".pdf"));
                j++;
            }
        }
    }

    c1->Print("graph.pdf");
    f->Write();
}

```

---

<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:** [January 9, 2019, 2:06pm UTC](https://root-forum.cern.ch/t/plot-graph-from-tree-with-passing-name-branch-as-argument/32222/2 "2019-01-09T14:06:09Z")

</div>

why do you put extra single quotes ?  
just do:

```auto
root[0] .x anylize.cxx("BSOuterVoltage","NimplantTotalCurrent","???","???")

```

note also your method has 4 parameters… not only 2.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [January 9, 2019, 2:26pm UTC](https://root-forum.cern.ch/t/plot-graph-from-tree-with-passing-name-branch-as-argument/32222/3 "2019-01-09T14:26:08Z")

</div>

```auto
void anylize(const char *dirname="output/", const char *ext=".csv", string xname, string yname)

```

In addition to what @couet mentioned, note that this is not valid C++: optional arguments must come after required arguments.

---

<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:** [January 10, 2019, 2:16pm UTC](https://root-forum.cern.ch/t/plot-graph-from-tree-with-passing-name-branch-as-argument/32222/4 "2019-01-10T14:16:12Z")

</div>

@couet @eguiraud thanks, very helpful!

---

<div class="post-metadata">

**Author:** ![fjh](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/54ee81/32.png) [@fjh](https://root-forum.cern.ch/u/fjh)\
**Post date:** [November 11, 2020, 9:24am UTC](https://root-forum.cern.ch/t/plot-graph-from-tree-with-passing-name-branch-as-argument/32222/5 "2020-11-11T09:24:58Z")

</div>

Excuse me, I have a question, why do you start your tree at -1? I hope to hear from you, thank you.

---

<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:** [November 24, 2020, 5:25pm UTC](https://root-forum.cern.ch/t/plot-graph-from-tree-with-passing-name-branch-as-argument/32222/6 "2020-11-24T17:25:20Z")

</div>

> [@fjh](#):
>
> why do you start your tree at -1?

Are you sure this question is related to what was discussed in that post ?  
Can you be more precise ?

---

<div class="post-metadata">

**Author:** ![fjh](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/54ee81/32.png) [@fjh](https://root-forum.cern.ch/u/fjh)\
**Post date:** [November 27, 2020, 9:49am UTC](https://root-forum.cern.ch/t/plot-graph-from-tree-with-passing-name-branch-as-argument/32222/7 "2020-11-27T09:49:34Z")

</div>

> [@anhelina](#):
>
> ```auto
> for (Int_t i = -1; i <entries ; i++)
> {
> tree->GetEntry(i);
> xvector.push_back(first);
> yvector.push_back(second);
> }
> Float_t *x= &xvector[0];
> Float_t *y= &yvector[0];
> 
> ```

I’m very sorry, my description is not accurate enough, I wanted to ask why i starts at -1 in GetEntry(i)?
