Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:46

0001 #!/usr/bin/env python3
0002 from PhysicsTools.NanoAODTools.postprocessing.framework.postprocessor import PostProcessor
0003 from importlib import import_module
0004 import os
0005 import sys
0006 import ROOT
0007 ROOT.PyConfig.IgnoreCommandLineOptions = True
0008 
0009 if __name__ == "__main__":
0010     from argparse import ArgumentParser
0011     parser = ArgumentParser()
0012     parser.add_argument("-s", "--postfix", dest="postfix", type=str, default=None,
0013                         help="Postfix which will be appended to the file name (default: _Friend for friends, _Skim for skims)")
0014     parser.add_argument("-J", "--json", dest="json", type=str,
0015                         default=None, help="Select events using this JSON file")
0016     parser.add_argument("-c", "--cut", dest="cut", type=str,
0017                         default=None, help="Cut string")
0018     parser.add_argument("-b", "--branch-selection", dest="branchsel",
0019                         type=str, default=None, help="Branch selection")
0020     parser.add_argument("--bi", "--branch-selection-input", dest="branchsel_in",
0021                         type=str, default=None, help="Branch selection input")
0022     parser.add_argument("--bo", "--branch-selection-output", dest="branchsel_out",
0023                         type=str, default=None, help="Branch selection output")
0024     parser.add_argument("--friend", dest="friend", action="store_true", default=False,
0025                         help="Produce friend trees in output (current default is to produce full trees)")
0026     parser.add_argument("--full", dest="friend", action="store_false", default=False,
0027                         help="Produce full trees in output (this is the current default)")
0028     parser.add_argument("--noout", dest="noOut", action="store_true",
0029                         default=False, help="Do not produce output, just run modules")
0030     parser.add_argument("-P", "--prefetch", dest="prefetch", action="store_true", default=False,
0031                         help="Prefetch input files locally instead of accessing them via xrootd")
0032     parser.add_argument("--long-term-cache", dest="longTermCache", action="store_true", default=False,
0033                         help="Keep prefetched files across runs instead of deleting them at the end")
0034     parser.add_argument("-N", "--max-entries", dest="maxEntries", type=int, default=None,
0035                         help="Maximum number of entries to process from any single given input tree")
0036     parser.add_argument("--first-entry", dest="firstEntry", type=int, default=0,
0037                         help="First entry to process in the three (to be used together with --max-entries)")
0038     parser.add_argument("--justcount", dest="justcount", default=False,
0039                         action="store_true", help="Just report the number of selected events")
0040     parser.add_argument("-I", "--import", dest="imports", type=str, default=[], action="append",
0041                         nargs=2, help="Import modules (python package, comma-separated list of ")
0042     parser.add_argument("-z", "--compression", dest="compression", type=str,
0043                         default=("LZMA:9"), help="Compression: none, or (algo):(level) ")
0044     parser.add_argument("outputDir", type=str)
0045     parser.add_argument("inputFile", type=str, nargs='+')
0046     options = parser.parse_args()
0047 
0048     if options.friend:
0049         if options.cut or options.json:
0050             raise RuntimeError(
0051                 "Can't apply JSON or cut selection when producing friends")
0052 
0053     modules = []
0054     for mod, names in options.imports:
0055         import_module(mod)
0056         obj = sys.modules[mod]
0057         selnames = names.split(",")
0058         mods = dir(obj)
0059         for name in selnames:
0060             if name in mods:
0061                 print("Loading %s from %s " % (name, mod))
0062                 if type(getattr(obj, name)) == list:
0063                     for mod in getattr(obj, name):
0064                         modules.append(mod())
0065                 else:
0066                     modules.append(getattr(obj, name)())
0067     if options.noOut:
0068         if len(modules) == 0:
0069             raise RuntimeError(
0070                 "Running with --noout and no modules does nothing!")
0071     if options.branchsel != None:
0072         options.branchsel_in = options.branchsel
0073         options.branchsel_out = options.branchsel
0074     p = PostProcessor(options.outputDir, options.inputFile,
0075                       cut=options.cut,
0076                       branchsel=options.branchsel_in,
0077                       modules=modules,
0078                       compression=options.compression,
0079                       friend=options.friend,
0080                       postfix=options.postfix,
0081                       jsonInput=options.json,
0082                       noOut=options.noOut,
0083                       justcount=options.justcount,
0084                       prefetch=options.prefetch,
0085                       longTermCache=options.longTermCache,
0086                       maxEntries=options.maxEntries,
0087                       firstEntry=options.firstEntry,
0088                       outputbranchsel=options.branchsel_out)
0089     p.run()