我之前在官方根(CERN)论坛上问过这个问题,但是至今问题没有解决。也许这里的任何人都可以通过指出我的错误或建议替代方法来提供帮助?
我有一个包含事件的 TTree; TTree 有一个分支,其中包含每个事件的 UNIX 时间和一些其他分支。我想根据时间间隔选择事件的一个子集,以便我可以单独分析这些事件。为了进行选择,我创建了另一棵树并复制了正确时间间隔内的所有条目。
下面的代码工作得很好并将所有事件从树复制到子树:
void tree_time_filter(TTree* tree, TTree** subtree, Int_t time_i, Int_t time_f){
*subtree = tree->CloneTree(0);
Int_t t;
tree->SetBranchAddress("UNIX time", &t);
for(Long64_t i = 0; i<tree->GetEntries(); i++){
tree->GetEntry(i);
if (true)
{
(*subtree)->Fill();
}
}
cout<<"Tree filtered. "<<(*subtree)->GetEntries()<< " entries were selected.\n";
return;
}
当我用实际条件替换 if(true) 时出现问题:
void tree_time_filter(TTree* tree, TTree** subtree, Int_t time_i, Int_t time_f){
*subtree = tree->CloneTree(0);
Int_t t;
tree->SetBranchAddress("UNIX time", &t);
for(Long64_t i = 0; i<tree->GetEntries(); i++){
tree->GetEntry(i);
if (t > time_i && t < time_f) //-> the condition
{
(*subtree)->Fill(); //-> this line now gives an error
}
}
cout<<"Tree filtered. "<<(*subtree)->GetEntries()<< " entries were selected.\n";
return;
}
我收到错误:“错误:指向类对象子树 0x0 3084 c:/.... 的非法指针。* 解释器错误已恢复 *”
错误行指的是 (*subtree) -> Fill(),与第一个示例中运行良好的相同代码。对于任何不涉及 t 的条件或任何不引用子树的 if-body,代码都有效。谁能解释这里出了什么问题?
谢谢!
(供引用,原始问题的链接:http://root.cern.ch/phpBB3/posting.php?mode=edit&f=3&p=79722)
最佳答案
我认为这是解释器的问题,在我看来是一个错误。
当宏第一次通过 (*subtree)->Fill();
时,我可以重现你的错误,它没有填充任何东西。很奇怪,因为它没有进入那里,但是......
我能够通过制作两个循环来解决它:
第一个循环循环直到它找到第一个通过剪切的事件然后停止。
第二,(*subtree)->Fill();
以此事件开始,确保处理的第一个事件通过剪切并被填充。
关于c++ - CERN 根 - 根据时间过滤子树事件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26308325/