CopyTree with selection

Hi experts,

I am trying to filter a tree using CopyTree(), but this does not seem to work. I have searched through the forum but nothing gives me a working solution.

Here’s a little snippet to reproduce my problem which I wrote using pyROOT:

import ROOT

file = ROOT.TFile('/afs/')
tree = file.Get("CollectionTree")
tree_filtered = tree.CopyTree("HGamEventInfoAuxDyn.isPassed == 1")

This gives me segmentation error.

Hoping the experts here can help!

Best regards,

_ROOT Version: 6.16

Hi @yosse_andrean,

To reproduce the problem, we would need access to the file mc16a.PowhegPy8_ZH125J.root. Could you please attach it to a post here?

OTOH, have you tried that also in the ROOT prompt (C++) or only in PyROOT?


Try adding gROOT->cd(); (its PyROOT equivalent, rather), or creating a new TFile, between getting the original tree and doing CopyTree. See this (even the title of this post is the same as yours, so you would have found it searching the Forum):

Hi @jalopezg ,

You can find the sample here: CERNBox
I have also tried it in a ROOT prompt but the result is the same.


HI @dastudillo ,
I have tried both suggestions and it still does not work, which is the motivation of making this post.

import ROOT
# warning: you should first load dictionaries for all classes used by the "tree"
file = ROOT.TFile('mc16a.PowhegPy8_ZH125J.root')
tree = file.Get('CollectionTree')
# currently, two branches generate an error, so we need to disable them
tree.SetBranchStatus('HGamElectrons', 0) # read too few bytes: 6 instead of 12
tree.SetBranchStatus('HGamMuons', 0) # read too few bytes: 6 instead of 12
file_filtered = ROOT.TFile('mc16a.PowhegPy8_ZH125J.filtered.root', 'recreate')
tree_filtered = tree.CopyTree('HGamEventInfoAuxDyn.isPassed == 1')
file_filtered.Close() # automatically deletes "tree_filtered", too
file.Close() # automatically deletes "tree", too

Thanks @Wile_E_Coyote. I should have mentioned the error I got on those two branches. Works like a charm!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.