从 ROOT(cern) 使用 AddFriend 时出现“非法指针”错误

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

瞄准; 我想比较两个ROOT TTree objects that have identical structure (but not identical contents obviously). The best way to do this seems to be using AddFriend的内容。

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

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

到目前为止我尝试了什么; 在 运行 成功完成 this page 底部的示例后,我决定将其缩减为仅阅读和添加朋友部分,因为我已经创建了 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.rootDNMaskOutput.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");
}

这给出了上面的错误。我不明白为什么这些东西表现不同?为什么他们不能只是朋友?

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

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

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

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

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

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

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

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