TObjArray Delete() method not working?

Hello all,

I am trying to create an array of TTree branches. For those branches, I am trying to draw a TGraph. The reason for doing this is to write a code for drawing a TGraph onces and that will create as many TGraphs as of branches in TTree.

My code is as follows:

std::vectorstd::string treeNameVector; //In this I am storing all TTree from .root file
treeNameVector.push_back(“ABC”); // ABC,DEF,GHI… are different TTree in one .root file
treeNameVector.push_back(“DEF”);
treeNameVector.push_back(“GHI”);

void Graph()
{
TFile *f1 = new TFile(“Tree.root”);
for(int i=0;i<treeNameVector.size();i++)
{
TTree T1 = (TTree)f1->Get(treeNameVector.at(i).c_str());
T1->AddFriend(“DEF”, “Tree.root”);
T1->AddFriend(“GHI”, “Tree.root”);

TCanvas *c = new TCanvas(treeNameVector.at(i).c_str(), treeNameVector.at(i).c_str(), 0, 0, 1000, 1000);

TObjArray branches = new TObjArray(0); //For getting branches of different TTree’s
branches = T1->GetListOfBranches();
int nbranches = branches->GetEntries();
cout << "Number of Branches = " << nbranches << endl;
for (int i = 0; i < nbranches; ++i)
{
string branchname = ((TBranch
)(*branches)[i])->GetName();
cout << "Branch Name = " << branchname << endl;
}
[color=#FF0000]// This works fine for only 1st TTree. But not working for other TTree as TObjArray is already filled with Branch name’s of 1st TTree. At the end of this loop I am using Deleting the TObjArray , So that it will work as a Dynamic array. But it is not woking.
//This will be useful if TTree have many branches e.g: above 500. Then insted of writing code for drawing a TGraph for all 500 branches will increase code length.[/color]

T1->Draw(branchname.at(i+1).c_str():branchname.at(i).c_str(), cut, “same”);
TGraph *gr = new TGraph(T1->GetSelectedRows(),T1->GetV2(), T1->GetV1());
gr->SetLineColor(55);

TMultiGraph *mg = new TMultiGraph();
mg -> Add(gr);

delete branches;
[color=#FF0000]// I am trying to flush-off the TObjArray for the use of next TTree branches but it is not working. [/color]

}
}

If anybody have an idea of [color=#FF0000]how to use TObjArray to be work as a dynamic array[/color] then please guide me.

thank you.

Wouldn’t storing the names of all branches you care about before your plotting loop work? Create a vector branchname and just copy what you get from iterating over the branches from GetListOfBranches.

Your code has a memory leak btw

TObjArray *branches = new TObjArray(0); // dummy object allocated here
branches = T1->GetListOfBranches();     // reference to initial object lost

Instead you could just have done

TObjArray *branches = T1->GetListOfBranches();

That array should be owned by the tree and you shouldn’t delete it yourself.

Also, you cannot concatenate the outputs of std::string::c_str like you did,
instead do

std::string a, b;
(a+b).c_str();

Hello,

Thanks for the reply.

You said, Create a vector branchname and just copy what you get from iterating over the branches from GetListOfBranches. But for the next TTree, TObjArray is already filled with listOfBranches of first TTree from .root file. So it is giving an error.

Is it possible to do store T1->GetListOfBranches(); list directly into the vector? Because return type is TObjArray. How we can do this? Please guide me further.