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:
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):
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')
tree_filtered.Write()
file_filtered.Close() # automatically deletes "tree_filtered", too
file.Close() # automatically deletes "tree", too