CMS 3D CMS Logo

magneticFieldFilterJSON.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from __future__ import print_function
4 import os
5 import sys
6 
7 if "CMSSW_BASE" not in os.environ:
8  print("You need to source the CMSSW environment first.")
9  sys.exit(1)
10 
11 required_version = (2,7)
12 if sys.version_info < required_version:
13  print("Your Python interpreter is too old. Need version 2.7 or higher.")
14  sys.exit(1)
15 
16 import argparse
17 
18 import HLTrigger.Tools.rrapi as rrapi
19 from FWCore.PythonUtilities.LumiList import LumiList
20 
21 
22 def main(argv = None):
23  """Main routine of the script.
24 
25  Arguments:
26  - `argv`: arguments passed to the main routine
27  """
28 
29  if argv == None:
30  argv = sys.argv[1:]
31 
32  parser = argparse.ArgumentParser(
33  description="Create JSON selection for a given magnetic field.")
34  parser.add_argument("-i", "--input", dest="input", metavar="JSON",
35  type=str, help="input JSON file")
36  parser.add_argument("-o", "--output", dest="output", metavar="JSON",
37  type=str, help="output JSON file")
38  parser.add_argument("--min", dest="min", metavar="RUN", type=int,
39  help="first run to be considered in the selection")
40  parser.add_argument("--max", dest="max", metavar="RUN", type=int,
41  help="last run to be considered in the selection")
42  parser.add_argument("--epsilon", dest="epsilon", metavar="TESLA",
43  default=0.1, type=float,
44  help="precision of the filter (default: %(default)s T)")
45  parser.add_argument("--debug", dest="debug", action="store_true",
46  help="show more verbose output")
47  required = parser.add_argument_group("required arguments")
48  required.add_argument("--b-field", dest="bfield", metavar="TESLA",
49  required=True, type=float,
50  help="magnetic field to filter")
51  args = parser.parse_args(argv)
52 
53 
54  try:
55  if args.input == None and (args.min == None or args.max == None):
56  msg = ("If no input JSON file ('--input') is provided, you have to "
57  "explicitly provide the first ('--min') and last ('--max') "
58  "run.")
59  raise RuntimeError(msg)
60 
61  if args.min != None and args.max != None and args.min > args.max:
62  msg = "First run ({min:d}) is after last run ({max:d})."
63  msg = msg.format(**args.__dict__)
64  raise RuntimeError(msg)
65 
66  if args.max != None and args.max <= 0:
67  msg = "Last run must be greater than zero: max = {0:d} <= 0."
68  msg = msg.format(args.max)
69  raise RuntimeError(msg)
70  except RuntimeError as e:
71  if args.debug: raise
72  print(">>>", os.path.splitext(os.path.basename(__file__))[0]+":", str(e))
73  sys.exit(1)
74 
75 
76  lumi_list = None if not args.input else LumiList(filename = args.input)
77  input_runs = None if not lumi_list else [int(r) for r in lumi_list.getRuns()]
78 
79  # Run registry API: https://twiki.cern.ch/twiki/bin/viewauth/CMS/DqmRrApi
80  URL = "http://runregistry.web.cern.ch/runregistry/"
81  api = rrapi.RRApi(URL, debug = args.debug)
82 
83  if api.app != "user": return
84 
85  column_list = ("number",)
86  min_run = args.min if args.min != None else input_runs[0]
87  max_run = args.max if args.max != None else input_runs[-1]
88  bfield_min = args.bfield - args.epsilon
89  bfield_max = args.bfield + args.epsilon
90  constraints = {
91  "datasetExists": "= true",
92  "number": ">= {0:d} and <= {1:d}".format(min_run, max_run),
93  "bfield": "> {0:f} and < {1:f}".format(bfield_min, bfield_max)
94  }
95 
96  run_list = [item["number"] for item in
97  api.data(workspace = "GLOBAL", table = "runsummary",
98  template = "json", columns = column_list,
99  filter = constraints)]
100 
101  if lumi_list != None:
102  runs_to_remove = []
103  for run in input_runs:
104  if run not in run_list: runs_to_remove.append(run)
105  lumi_list.removeRuns(runs_to_remove)
106  else:
107  lumi_list = LumiList(runs = run_list)
108 
109  if args.output != None:
110  lumi_list.writeJSON(args.output)
111  with open(args.output+".args", "w") as f:
112  f.write(" ".join(argv)+"\n")
113  else:
114  print(lumi_list)
115 
116 
117 
118 if __name__ == "__main__":
119  main()
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
Definition: main.py:1
#define str(s)