Select/merge root files, keep content

Dear ROOTers,

I have a number of .root files all having the same architecture: they contain a tree (let’s call it “events”) and a directory (“Meta”), itself containing a number of trees.

What I want to do is to make a selection process on the “events” and merge all files together while keeping the “Meta” directory. Hence I want to create a .root file containing a subselection of “events” from all inputs, and all the “Meta”.

But I do not really know how to make that. I cannot use ROOT “hadd” (for some reason, doesn’t work on that data). Basic code to get the idea:

# setup, import, etc

chain = TChain("Events")

for file in filelist:	#filelist loaded previously
	chain.add(file)
	
for i in range(0,chain.GetEntries()):
	# selection rules
	
# Here: find a way to load the "Meta" directory inside the TChain and write TChain to a file

The different examples found while Google-ing around only dealt with a single tree. Any hint would be greatly appreciated!

Have you thought of making a Chain with this “events” tree ?
root.cern.ch/doc/master/classTChain.html

Hi,

I would recommend something like:

# setup, import, etc

chain = TChain("Events")
chain2 = TChain("Meta/Tree1");
chain3 = TChain("Meta/Tree2");

for file in filelist:   #filelist loaded previously
   chain.add(file)
   chain2.add(file)
   chain3.add(file)
   
outputfile.cd("Meta")
ot2 = chain2.CloneTree(-1,"fast");
ot3 = chain.CloneTree(-1,"fast");

outputfile.cd();
ot1 = chain.CloneTree(0);

for i in range(0,chain.GetEntries()):
   # selection rules
   ...
   

Thanks a lot, this method worked perfectly fine!