Draw the Graph of average of two leaves

Hello all,

I want to draw [color=#FF4000]an average[/color] of two leaves from two different Trees. I tried :

TFile *f1 = TFile::Open(“ABC.root”);
TTree T1 = (TTree)f1->Get(“firstTree”);
T1->AddFriend(“secondTree”, “ABC.root”);

T1->Draw("(a0+secondTree.a0)/2:(Time+secondTree.Time)/2", “”, “”); //I want to find an average of leave a0 of firstTree and a0 of secondTree to plot against an average of Time of firstTree and Time of secondTree.

TGraph *gr = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1());
gr->SetLineColor(kRed);

Its giving an error. Please guide me how I can do it correctly? OR in root do we have any function to find an average?

Thank you.

Hi,

It would have been helpful if you had included the actual error (both so that
people can better help you and also for anybody with the same problem in
the future).

I would guess that your problem was that you gave a filename argument here:

This file is already opened, but if you give TTree::AddFriend a filename
argument it will open the file again and attempt to read the tree. This should then
lead to error messages stating that the file wasn’t closed correctly etc.

What you should do instead is give it no filename argument since in this particular case
the file was already open.

T1->AddFriend("secondTree");

Hello,

Thanks for the reply. I have made some modifications in my code. It is as follows :

TFile *f1 = TFile::Open(“ABC.root”);
std::vectorstd::string treeNameVector; //Different TTree are created in the ABC.root file. Those TTree names are stored in treeNameVector

treeNameVector.push_back(firstTree); //firstTree and secondTree are already created in ABC.root
treeNameVector.push_back(secondTree);

TTree T1 = (TTree)f1->Get(treeNameVector.at(i).c_str()); //Assign T1 pointer to “firstTree"
T1->AddFriend(treeNameVector.at(i+1).c_str()); //T1 add a friend to “secondTree"
T1->Draw(”((treeNameVector.at(i+1).a0)+a0)/2:Time”, “”, “same”); [color=#FF4000]//Plot a graph of average of “ao” leave of “secondTree” and “firstTree” VS “Time” of “firstTree”. Here getting an error as “Bad numerical experssion”[/color]
TGraph *gr1 = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1
gr1->SetLineColor(55);

How could I referred to different Trees whose names are stored in “vector” for plotting a graph in ROOT? Please guide me.

Thank you.

Did you try to build the argument for T1->Draw? You should be able to do that
with combinations of TTree::GetName and std::string or TString.

Hello,

Thanks for the reply. Yes, I tried to build an argument with TString. I have added it in the code but still its giving the same error as “Bad numerical experssion”.

TFile *f1 = TFile::Open(“ABC.root”);
std::vectorstd::string treeNameVector; //Different TTree are created in the ABC.root file. Those TTree names are stored in treeNameVector

treeNameVector.push_back(firstTree); //firstTree and secondTree are already created in ABC.root
treeNameVector.push_back(secondTree);
[color=#4000FF]TString treeNameVector2;[/color]
TTree T1 = (TTree)f1->Get(treeNameVector.at(i).c_str()); //Assign T1 pointer to “firstTree”
[color=#4000FF] treeNameVector2 = treeNameVector.at(i+1).c_str();[/color]
cout << “treeNameVector2 : " << treeNameVector2 << endl; //Its taking a correct value
T1->AddFriend(treeNameVector2, “resin1.root”);
T1->Draw(”(a0+treeNameVector2.a0)/2:(Time+treeNameVector2.Time)/2", “”, “same”); [color=#FF4000]//Plot a graph of average of “ao” leave of “secondTree” and “firstTree” VS “Time” of “firstTree”. Here getting an error as “Bad numerical experssion” for treeNameVector2.ao and treeNameVector2.Time[/color]

  TGraph *gr1 = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1
  gr1->SetLineColor(55);

I am still looking for an answer. Thank you.

Hi,

You expect a little too much. treeNameVector2 is the name of C++ variable (here a TString), and you expect TTree::Draw to magically know the value it contains. This doesn’t work here (it often does for types inheriting from TNamed though).

When I said “build the argument for T1->Draw” I literally meant that you should build the const char* it expects, e.g. since I don’t know the ROOT string formatting functions

std::string tree2 = treeNameVector.at(i+1);
std::string arg = std::string("(a0+") + tree2 + ".a0)/2:(Time+" + tree2 + ".Time)/2";
T1->Draw(arg.c_str(), "", "same");

std::string has a + operator which makes joining strings easy. I explicilty create a std::string for the first part – then all other parts are implicitly converted to std::strings.

Hello,

Thanks for the reply. But its not working.

It is drawing a Graph for T1->Draw(“a0:Time”, “”, “same”); Means graph for firstTree only. Its not drawing a Graph of average of two leaves, if we use std::string arg = std::string("(a0+") + tree2 + “.a0)/2:(Time+” + tree2 + “.Time)/2”; T1->Draw(arg.c_str(), “”, “same”);.