gpt4 book ai didi

c++ - 从 ROOT(cern) 使用 AddFriend 时出现 `illegal pointer` 错误

转载 作者:行者123 更新时间:2023-11-30 03:51:20 25 4
gpt4 key购买 nike

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

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

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

到目前为止我尝试了什么;运行 this page 底部的示例后成功地,我决定将它减少到只包含阅读和添加 friend 的部分,因为我已经在第一次运行时创建了 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");
}

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

最佳答案

原来TFile的方法Get可能返回null,表示失败。你没有考虑到这一点。为什么它在您的案例中返回 null?

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

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

关于c++ - 从 ROOT(cern) 使用 AddFriend 时出现 `illegal pointer` 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31314232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com