I am trying to slim an ntuple as follows, in analogy to how I would do this in C++ using a class generated via TChain::MakeClass():
ch = TChain("qcd")
ch.Add("*qcd.root*")
nEntries = ch.GetEntries()
#--- Throw away some branches
ch.SetBranchStatus("el_*", 0)
ch.SetBranchStatus("mu_*", 0)
ch_new = ch.CloneTree(0)
for i in range(nEntries):
ch.GetEntry(i)
#--- Only write out certain events that pass some cut
if passCut(ch.RunNumber, ch.lbn):
ch_new.Fill()
#--- Write to new file
outFile = "qcd.slim.root"
newFile = TFile(outFile,"RECREATE")
newFile.cd()
ch_new.Write()
newFile.Close()
Unfortunately this leaves me with a new chain ch_new that has no events. I guess when I call TChain::Fill() its branches are not associated with addresses of the variables whose values to fill, unlike the C++ case. How can I resolve this?
I have verified btw that TChain::CloneTree() is working properly, e.g. if I just do:
You need to make sure the new TTree is associate either with no file or better yet with the output file before starting to fill it. The following should work nicely:[code]ch = TChain(“qcd”)
ch.Add(“qcd.root”)
nEntries = ch.GetEntries()
#— Throw away some branches
ch.SetBranchStatus(“el_", 0)
ch.SetBranchStatus("mu_”, 0)
#— Write to new file
outFile = "qcd.slim.root"
newFile = TFile(outFile,“RECREATE”)
ch_new = ch.CloneTree(0)
for i in range(nEntries):
ch.GetEntry(i)
#— Only write out certain events that pass some cut
if passCut(ch.RunNumber, ch.lbn):
ch_new.Fill()
You need to make sure the new TTree is associate either with no file or better yet with the output file before starting to fill it. The following should work nicely:[code]ch = TChain(“qcd”)
ch.Add(“qcd.root”)
nEntries = ch.GetEntries()
#— Throw away some branches
ch.SetBranchStatus(“el_", 0)
ch.SetBranchStatus("mu_”, 0)
#— Write to new file
outFile = "qcd.slim.root"
newFile = TFile(outFile,“RECREATE”)
ch_new = ch.CloneTree(0)
for i in range(nEntries):
ch.GetEntry(i)
#— Only write out certain events that pass some cut
if passCut(ch.RunNumber, ch.lbn):
ch_new.Fill()
I assume you double check that the condition:if passCut(ch.RunNumber, ch.lbn):was sometimes true …
If it is, then we would need a complete running example reproducing the problem.
Cheers,
Philippe[/quote]
I apologize Philippe, this function works properly but was not selecting anything because I was using a special ntuple for testing. It works now after correcting this – sorry about this dumb mistake.
Following the similar line. I try to slim the data.
What I see is the following problem:
I get 2 trees with same name both are slimmed.
One tree has slightly less events than the other.
any suggestion that why should I get the 2 trees with same name ?
In principle, one would expect the slimmed version of a tree.
Thanks in advance,
—Saleem
Here is piece of code:
[code]#get main tree
ch = TChain(“physics”) #set max size = 10 times of default ~20 GB
ch.SetMaxTreeSize(1900000000*10)
for file in inputFiles:
ch.Add(file)
[quote]these are 2 cycles (cycles should not have different entries )[/quote]Why not? Each cycle is a copy/snapshot in time of the meta-data of the TTree/Object and they should in most case have different number entries …
[quote]The annoying part is you get 2 cycles with different entries in the output (final) root file.[/quote]I am a bit confused of ‘why’ it would be annoying or even surprising … the lower number cycles is a copy of one of the previous state of the TTree object and as thus it should have a different number of entries. Even more so, most of the retrieval tool automatic pick the highest cycle unless explicitly requested otherwise. In addition (see User’s Guide for detail) you can disable the extra safety of having those cycles …
[quote]I think this is because clonetree is associated with the chain of input root file as well as with the output root file. (my guess…)[/quote]A TTree can only be associated with one file … (however switching the association in mid-stream is very likely to cause severe problems).
[quote]In principle, clonetree has to be associated with the output root file.[/quote]Yes, and this is usually achieve by making sure that the ‘output’ file is the current ROOT file (i.e. gFile == ouput_file) when you call CloneTree.