删除ROOT TTree中的重复条目

时间:2012-08-17 08:10:42

标签: root-framework

我需要合并三个.root个文件。通常我会使用hadd来合并文件,但文件包含我需要删除的重复条目。我不能只删除重复的条目,因为TTree是只读的。有没有一种简单的方法来合并文件,同时确保只保存唯一的条目?

1 个答案:

答案 0 :(得分:1)

我确实找到了一种方法,可以使用TEntryList生成仅包含唯一条目的直方图。这允许您指定要使用的树条目。就我而言,每个条目都有一个标识它的事件编号。因此,我生成了一个条目列表,其条目号仅对应于唯一的事件编号。

set<int> eventIds; // keep track of already seen event numbers
int EVENT;
int nEntries = tree->GetEntries();

tree->SetBranchAddress("EVENT",&EVENT); // grab the event number from the tree

TEntryList *tlist = new TEntryList(tree); // initialize entry list for 'TTree* tree'

// loop over the entries in 'tree'
for (int j = 0; j < nEntries; ++j)
{
    tree->GetEvent(j);

    // if we have not seen this event yet, add it to the set
    // and to the entry list
    if (eventIds.count(EVENT) == 0)
    {
        eventIds.insert(EVENT);
        tlist->Enter(j,tree);
    }
}

// apply the entry list to the tree
tree->SetEntryList(tlist);

// histogram of the variable 'var' will be drawn only with the
// entries specified by the entry list.
tree->Draw("var");