Jekowl
Jekowl

Reputation: 317

Getting `illegal pointer` error when using AddFriend from ROOT(cern)

Aim; I wish to compare the contents of two ROOT TTree objects that have identical structure (but not identical contents obviously). The best way to do this seems to be using AddFriend.

Problem; I am getting this error message;

Error: illegal pointer to class object t3 0x0 1681  makeFriends.C:6:
*** Interpreter error recovered ***

What I've tried so far; After running the example at the bottom of this page successfully I decided to reduce it down to just the reading and friend adding section, as I had already created tree3.root and tree3f.root in the first run. So I had a file called tree3.C containing;

// Function to read the two files and add the friend
void tree3r()         {
  TFile *f = new TFile("tree3.root");
  TTree *t3 = (TTree*)f->Get("t3");
  // Add the second tree to the first tree as a friend
  t3->AddFriend("t3f","tree3f.root");
  // Draw pz which is in the first tree and use pt 
  // in the condition. pt is in the friend tree.
  //t3->Draw("pz","pt>5");
}

This worked as expected, when loaded (root[] .L tree3.C) and run (root[] tree3r()) from the root prompt.

So I put a copy in a folder containing both of my root files, plainMaskOutput.root and DNMaskOutput.root, and changed strings in the copy to match the names of my files. So I have;

// Function to read the two files and add the friend
void tree3r()         {
TFile *f = new TFile("plainMaskOutput.root");
TTree *t3 = (TTree*)f->Get("t3");
   // Add the second tree to the first tree as a friend
t3->AddFriend("t3f","DNMaskOutput.root");
   // Draw pz which is in the first tree and use pt 
   // in the condition. pt is in the friend tree.
//t3->Draw("pz","pt>5");
}

Which gives the error above. I dont understand why these things are behaving diffrently? Why can't they just be friends?

Upvotes: 1

Views: 683

Answers (2)

varnie
varnie

Reputation: 2595

It turned out that TFiles method Get might return null, indicating the failure. You were not taking that into account. Why does it return null in your case?

According to the link I provided in the comments (https://root.cern.ch/phpBB3/viewtopic.php?t=12407) it is because your file doesn't contain a tree having a name you provided.

It will be better to add an explicit check of the returned value from Get. if the file gets changed later, your program will start to crash again.

Upvotes: 1

Jekowl
Jekowl

Reputation: 317

The problem is that plainMaskOutput.root is a file name and the string inside the Get() parenthesis is the tree name. The file called plainMaskOutput.root did not contain a tree by the name t3 it contained one by the name HitsTree. So the line should have been;

TTree *foo = (TTree*)f->Get("HitsTree");

Similarly, the add friend command needed to have the name of the tree stored in DNMaskOutput.root, but as they have the same name it should be aliased;

foo->AddFriend("DNHitsTree = HitsTree","DNMaskOutput.root");

This is just the problem I was having this time, it may not always be the problem associated with this error. I am not knowledgeable enough in this area to say what other problems are possible.

Upvotes: 1

Related Questions