CMS 3D CMS Logo

cropTnPTrees.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 from __future__ import print_function
3 import ROOT
4 ROOT.gROOT.SetBatch(True)
5 
6 import sys
7 
8 from optparse import OptionParser
9 parser = OptionParser(usage = "usage: %prog [options] inputFile fraction outputFile",
10  version = "%prog $Id:$")
11 (options, args) = parser.parse_args()
12 
13 if len(args) <= 2:
14  parser.print_usage()
15  sys.exit(2)
16 
17 try:
18  frac = float(args[1])
19 except TypeError:
20  parser.print_usage()
21  print("fraction must be a floating point number (e.g. 0.5)")
22  sys.exit(2)
23 
24 input = ROOT.TFile(args[0])
25 output = ROOT.TFile(args[2], "RECREATE")
26 for k in input.GetListOfKeys():
27  print(k.GetName(), k.GetClassName())
28  if k.GetClassName() == "TDirectoryFile":
29  print(" processing directory ",k.GetName())
30  din = input.Get(k.GetName())
31  dout = output.mkdir(k.GetName())
32  for i in din.GetListOfKeys():
33  if i.GetClassName() == "TTree":
34  src = din.Get(i.GetName()) #i.ReadObj(); # ReadObj doesn't work!!!
35  newEntries = int(src.GetEntries()*frac)
36  print(" cropped TTree",i.GetName(),", original entries",src.GetEntries(), ", new entries",newEntries)
37  cloned = src.CloneTree(newEntries)
38  dout.WriteTObject(cloned, i.GetName())
39  elif i.GetClassName() != "TDirectory":
40  dout.WriteTObject(i.ReadObj(), i.GetName())
41  print(" copied ",i.GetClassName(),i.GetName())
42 
43 
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
createfilelist.int
int
Definition: createfilelist.py:10
edm::print
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66