CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_4/src/PhysicsTools/TagAndProbe/python/cropTnPTrees.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import ROOT
00003 ROOT.gROOT.SetBatch(True)
00004 
00005 import sys
00006 
00007 from optparse import OptionParser
00008 parser = OptionParser(usage = "usage: %prog [options] inputFile fraction outputFile",
00009                       version = "%prog $Id: cropTnPTrees.py,v 1.1 2010/05/07 14:22:37 gpetrucc Exp $")
00010 (options, args) = parser.parse_args()
00011 
00012 if len(args) <= 2: 
00013     parser.print_usage()
00014     sys.exit(2)
00015     
00016 try:
00017     frac = float(args[1])
00018 except TypeError:
00019     parser.print_usage()
00020     print "fraction must be a floating point number (e.g. 0.5)"
00021     sys.exit(2)
00022 
00023 input  = ROOT.TFile(args[0])
00024 output = ROOT.TFile(args[2], "RECREATE")
00025 for k in input.GetListOfKeys():
00026     print k.GetName(), k.GetClassName()
00027     if k.GetClassName() == "TDirectoryFile":
00028         print "  processing directory ",k.GetName()
00029         din  = input.Get(k.GetName())
00030         dout = output.mkdir(k.GetName())
00031         for i in din.GetListOfKeys():
00032             if i.GetClassName() == "TTree":
00033                 src = din.Get(i.GetName()) #i.ReadObj(); # ReadObj doesn't work!!!
00034                 newEntries = int(src.GetEntries()*frac)
00035                 print "    cropped TTree",i.GetName(),", original entries",src.GetEntries(), ", new entries",newEntries
00036                 cloned = src.CloneTree(newEntries)
00037                 dout.WriteTObject(cloned, i.GetName())       
00038             elif i.GetClassName() != "TDirectory":
00039                 dout.WriteTObject(i.ReadObj(), i.GetName())
00040                 print "    copied ",i.GetClassName(),i.GetName()
00041 
00042