1

目标; 我希望比较两个具有相同结构的ROOT TTree对象的内容(但显然不是相同的内容)。最好的方法似乎是使用AddFriend

问题; 我收到此错误消息;

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

到目前为止我已经尝试过什么;在成功运行此页面 底部的示例后,我决定将其缩减为仅阅读和添加朋友部分,因为我已经创建tree3.roottree3f.root在第一次运行中。所以我有一个名为 tree3.C 的文件,其中包含;

// 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");
}

当从根提示符加载 ( root[] .L tree3.C) 并运行 ( )时,这按预期工作。root[] tree3r()

因此,我将副本放在包含我的两个根文件plainMaskOutput.root和, 的文件夹中DNMaskOutput.root,并更改了副本中的字符串以匹配我的文件名。所以我有;

// 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");
}

这给出了上面的错误。我不明白为什么这些事情的行为不同?为什么他们不能只是朋友?

4

2 回答 2

1

问题是这plainMaskOutput.root是一个文件名,Get()括号内的字符串是树名。调用的文件plainMaskOutput.root不包含一棵同名的树,t3它包含一棵同名的树HitsTree。所以这条线应该是;

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

同样,添加朋友命令需要将树的名称存储在 中DNMaskOutput.root,但由于它们具有相同的名称,因此应该使用别名

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

这只是我这次遇到的问题,它可能并不总是与此错误相关的问题。我在这方面的知识不足,无法说出可能存在的其他问题。

于 2015-07-09T12:35:39.117 回答
1

原来TFiles方法Get可能返回null,说明失败。你没有考虑到这一点。为什么它在您的情况下返回 null ?

根据我在评论中提供的链接(https://root.cern.ch/phpBB3/viewtopic.php?t=12407),这是因为您的文件不包含具有您提供的名称的树。

最好添加对 from 的返回值的显式检查Get。如果文件稍后更改,您的程序将再次开始崩溃。

于 2015-07-09T13:11:08.020 回答