Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-03-03 02:23:10

0001 #!/usr/bin/env python3
0002 
0003 from __future__ import print_function
0004 import os
0005 import sys
0006 
0007 if "CMSSW_BASE" not in os.environ:
0008     print("You need to source the CMSSW environment first.")
0009     sys.exit(1)
0010 
0011 required_version = (2,7)
0012 if sys.version_info < required_version:
0013     print("Your Python interpreter is too old. Need version 2.7 or higher.")
0014     sys.exit(1)
0015 
0016 import argparse
0017 
0018 import HLTrigger.Tools.rrapi as rrapi
0019 from FWCore.PythonUtilities.LumiList import LumiList
0020 
0021 
0022 def main(argv = None):
0023     """Main routine of the script.
0024 
0025     Arguments:
0026     - `argv`: arguments passed to the main routine
0027     """
0028 
0029     if argv == None:
0030         argv = sys.argv[1:]
0031 
0032     parser = argparse.ArgumentParser(
0033         description="Create JSON selection for a given magnetic field.")
0034     parser.add_argument("-i", "--input", dest="input", metavar="JSON",
0035                         type=str, help="input JSON file")
0036     parser.add_argument("-o", "--output", dest="output", metavar="JSON",
0037                         type=str, help="output JSON file")
0038     parser.add_argument("--min", dest="min", metavar="RUN", type=int,
0039                         help="first run to be considered in the selection")
0040     parser.add_argument("--max", dest="max", metavar="RUN", type=int,
0041                         help="last run to be considered in the selection")
0042     parser.add_argument("--epsilon", dest="epsilon", metavar="TESLA",
0043                         default=0.1, type=float,
0044                         help="precision of the filter (default: %(default)s T)")
0045     parser.add_argument("--debug", dest="debug", action="store_true",
0046                         help="show more verbose output")
0047     required = parser.add_argument_group("required arguments")
0048     required.add_argument("--b-field", dest="bfield", metavar="TESLA",
0049                           required=True, type=float,
0050                           help="magnetic field to filter")
0051     args = parser.parse_args(argv)
0052 
0053 
0054     try:
0055         if args.input == None and (args.min == None or args.max == None):
0056             msg = ("If no input JSON file ('--input') is provided, you have to "
0057                    "explicitly provide the first ('--min') and last ('--max') "
0058                    "run.")
0059             raise RuntimeError(msg)
0060 
0061         if args.min != None and args.max != None and args.min > args.max:
0062             msg = "First run ({min:d}) is after last run ({max:d})."
0063             msg = msg.format(**args.__dict__)
0064             raise RuntimeError(msg)
0065 
0066         if args.max != None and args.max <= 0:
0067             msg = "Last run must be greater than zero: max = {0:d} <= 0."
0068             msg = msg.format(args.max)
0069             raise RuntimeError(msg)
0070     except RuntimeError as e:
0071         if args.debug: raise
0072         print(">>>", os.path.splitext(os.path.basename(__file__))[0]+":", str(e))
0073         sys.exit(1)
0074 
0075 
0076     lumi_list = None if not args.input else LumiList(filename = args.input)
0077     input_runs = None if not lumi_list else [int(r) for r in lumi_list.getRuns()]
0078 
0079     # Run registry API: https://twiki.cern.ch/twiki/bin/viewauth/CMS/DqmRrApi
0080     URL = "http://runregistry.web.cern.ch/runregistry/"
0081     api = rrapi.RRApi(URL, debug = args.debug)
0082 
0083     if api.app != "user": return
0084 
0085     column_list = ("number",)
0086     min_run = args.min if args.min != None else input_runs[0]
0087     max_run = args.max if args.max != None else input_runs[-1]
0088     bfield_min = args.bfield - args.epsilon
0089     bfield_max = args.bfield + args.epsilon
0090     constraints = {
0091         "datasetExists": "= true",
0092         "number": ">= {0:d} and <= {1:d}".format(min_run, max_run),
0093         "bfield": "> {0:f} and < {1:f}".format(bfield_min, bfield_max)
0094         }
0095 
0096     run_list = [item["number"] for item in
0097                 api.data(workspace = "GLOBAL", table = "runsummary",
0098                          template = "json", columns = column_list,
0099                          filter = constraints)]
0100 
0101     if lumi_list != None:
0102         runs_to_remove = []
0103         for run in input_runs:
0104             if run not in run_list: runs_to_remove.append(run)
0105         lumi_list.removeRuns(runs_to_remove)
0106     else:
0107         lumi_list = LumiList(runs = run_list)
0108 
0109     if args.output != None:
0110         lumi_list.writeJSON(args.output)
0111         with open(args.output+".args", "w") as f:
0112             f.write(" ".join(argv)+"\n")
0113     else:
0114         print(lumi_list)
0115 
0116 
0117 
0118 if __name__ == "__main__":
0119     main()