Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:19

0001 #!/usr/bin/env python
0002 from __future__ import print_function
0003 import ROOT
0004 ROOT.gROOT.SetBatch(True)
0005 
0006 import sys
0007 
0008 from optparse import OptionParser
0009 parser = OptionParser(usage = "usage: %prog [options] inputFile fraction outputFile",
0010                       version = "%prog $Id:$")
0011 (options, args) = parser.parse_args()
0012 
0013 if len(args) <= 2: 
0014     parser.print_usage()
0015     sys.exit(2)
0016     
0017 try:
0018     frac = float(args[1])
0019 except TypeError:
0020     parser.print_usage()
0021     print("fraction must be a floating point number (e.g. 0.5)")
0022     sys.exit(2)
0023 
0024 input  = ROOT.TFile(args[0])
0025 output = ROOT.TFile(args[2], "RECREATE")
0026 for k in input.GetListOfKeys():
0027     print(k.GetName(), k.GetClassName())
0028     if k.GetClassName() == "TDirectoryFile":
0029         print("  processing directory ",k.GetName())
0030         din  = input.Get(k.GetName())
0031         dout = output.mkdir(k.GetName())
0032         for i in din.GetListOfKeys():
0033             if i.GetClassName() == "TTree":
0034                 src = din.Get(i.GetName()) #i.ReadObj(); # ReadObj doesn't work!!!
0035                 newEntries = int(src.GetEntries()*frac)
0036                 print("    cropped TTree",i.GetName(),", original entries",src.GetEntries(), ", new entries",newEntries)
0037                 cloned = src.CloneTree(newEntries)
0038                 dout.WriteTObject(cloned, i.GetName())       
0039             elif i.GetClassName() != "TDirectory":
0040                 dout.WriteTObject(i.ReadObj(), i.GetName())
0041                 print("    copied ",i.GetClassName(),i.GetName())
0042 
0043