From 831b0faa3b9c8e819de05d88bead0b03737e9b25 Mon Sep 17 00:00:00 2001 From: Kevin Pedro Date: Tue, 24 Oct 2023 08:13:20 -0500 Subject: [PATCH] argparse migration for PhysicsTools/TagAndProbe --- .../TagAndProbe/python/cropTnPTrees.py | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/PhysicsTools/TagAndProbe/python/cropTnPTrees.py b/PhysicsTools/TagAndProbe/python/cropTnPTrees.py index 1b7de44f2bf07..4cced842b2422 100644 --- a/PhysicsTools/TagAndProbe/python/cropTnPTrees.py +++ b/PhysicsTools/TagAndProbe/python/cropTnPTrees.py @@ -1,28 +1,19 @@ #!/usr/bin/env python -from __future__ import print_function import ROOT ROOT.gROOT.SetBatch(True) import sys -from optparse import OptionParser -parser = OptionParser(usage = "usage: %prog [options] inputFile fraction outputFile", - version = "%prog $Id:$") -(options, args) = parser.parse_args() +from argparse import ArgumentParser +parser = ArgumentParser() +parser.add_argument("inputFile", type=str) +parser.add_argument("fraction", type=float) +parser.add_argument("outputFile", type=str) +options = parser.parse_args() -if len(args) <= 2: - parser.print_usage() - sys.exit(2) - -try: - frac = float(args[1]) -except TypeError: - parser.print_usage() - print("fraction must be a floating point number (e.g. 0.5)") - sys.exit(2) - -input = ROOT.TFile(args[0]) -output = ROOT.TFile(args[2], "RECREATE") +frac = options.fraction +input = ROOT.TFile(options.inputFile) +output = ROOT.TFile(options.outputFile, "RECREATE") for k in input.GetListOfKeys(): print(k.GetName(), k.GetClassName()) if k.GetClassName() == "TDirectoryFile":