【问题标题】:Getting `illegal pointer` error when using AddFriend from ROOT(cern)从 ROOT(cern)使用 AddFriend 时出现“非法指针”错误
【发布时间】:2015-09-27 15:16:31
【问题描述】:

瞄准; 我想比较两个具有相同结构的ROOTTTree 对象的内容(但显然内容不同)。最好的方法似乎是使用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");
}

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

【问题讨论】:

标签: c++ pointers tree root-framework


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

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

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

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

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

    【讨论】:

    • 感谢@varnie 提供解决此问题的链接。
    猜你喜欢
    • 2018-05-25
    • 2020-10-04
    • 1970-01-01
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多