Removing Duplicate Entries in a ROOT TTree
-
26-06-2021 - |
質問
I have three .root
files that I need to merge together. Normally I would use hadd
to merge the files, but the files contain duplicate entries which I need to remove. I can't just delete the duplicated entries because TTrees are read-only. Is there a simple way to merge the files while ensuring that only unique entries are saved?
解決
I did manage to find a way to produce histograms that contain only unique entries using TEntryList
. This allows you to specify which tree entries you wish to use. In my case, each entry has an event number which identifies it. So, I generated an entry list with the entry numbers corresponding to only unique event numbers.
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");
所属していません StackOverflow