CMS 3D CMS Logo

cropTnPTrees.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import ROOT
3 ROOT.gROOT.SetBatch(True)
4 
5 import sys
6 
7 from argparse import ArgumentParser
8 parser = ArgumentParser()
9 parser.add_argument("inputFile", type=str)
10 parser.add_argument("fraction", type=float)
11 parser.add_argument("outputFile", type=str)
12 options = parser.parse_args()
13 
14 frac = options.fraction
15 input = ROOT.TFile(options.inputFile)
16 output = ROOT.TFile(options.outputFile, "RECREATE")
17 for k in input.GetListOfKeys():
18  print(k.GetName(), k.GetClassName())
19  if k.GetClassName() == "TDirectoryFile":
20  print(" processing directory ",k.GetName())
21  din = input.Get(k.GetName())
22  dout = output.mkdir(k.GetName())
23  for i in din.GetListOfKeys():
24  if i.GetClassName() == "TTree":
25  src = din.Get(i.GetName()) #i.ReadObj(); # ReadObj doesn't work!!!
26  newEntries = int(src.GetEntries()*frac)
27  print(" cropped TTree",i.GetName(),", original entries",src.GetEntries(), ", new entries",newEntries)
28  cloned = src.CloneTree(newEntries)
29  dout.WriteTObject(cloned, i.GetName())
30  elif i.GetClassName() != "TDirectory":
31  dout.WriteTObject(i.ReadObj(), i.GetName())
32  print(" copied ",i.GetClassName(),i.GetName())
33 
34 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47