Plot graph from Tree with passing name branch as argument

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;


#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();
}

why do you put extra single quotes ?
just do:

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

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

1 Like
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.

1 Like

@couet @eguiraud thanks, very helpful!

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

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

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