3 from __future__
import with_statement
5 from multiprocessing.pool
import Pool
7 import logging; log = logging.getLogger(
"DCSCalculator2.main")
9 from DQUtils.db
import fetch_iovs
10 from DQUtils.iov_arrangement
import inverse_lblb, run_iovs_from_lblb
11 from DQUtils.general
import timer
12 from DQUtils.utils
import pprint_objects
13 from DQUtils.logger
import init_logger
14 from DQUtils.sugar
import RunLumi, IOVSet
16 from DQDefects
import DefectsDB
18 from DCSCalculator2.subdetectors
import ALL_SYSTEMS, SYSTEM_MAP
19 from DCSCalculator2.variable
import DefectIOV, DefectIOVFull
21 import DCSCalculator2.config
as config
23 from contextlib
import nullcontext
30 hours = seconds / 3600; seconds %= 3600
31 minutes = seconds / 60; seconds %= 60
32 return "%ih%im%is" % (hours, minutes, seconds)
36 log.info(
"Empty Result.")
48 with timer(
"Run DCS calculator 2 for %s" % system):
49 return system.run(lbtime, run_iovs)
51 log.warning(
"DCS Calculator failed to run for %s.", system)
52 if config.opts.dont_ignore_system_exceptions:
54 log.exception(
"Continuing. Use -e -X commandline to investigate")
55 if config.opts.email_on_failure:
56 from DataQualityUtils.panic
import panic
57 from traceback
import format_exc
58 runnum = lbtime[0].Run
if len(lbtime)>0
else '??????'
59 panicmsg =
"DCS Calculator failed to execute in run %s for system %s.\n\n%s" % (runnum, system, format_exc())
62 except (KeyboardInterrupt, SystemExit):
66 pool = Pool(N
if N > 0
else len(systems))
68 for system
in systems:
69 result = pool.apply_async(run_one, (system, lbtime, run_iovs))
70 results.append(result)
72 all_results = IOVSet()
73 for system, result
in zip(systems, map(
lambda x: x.get(), results)):
74 log.info(
" -- result for %s", system)
77 all_results.extend(result)
82 result_iovs = IOVSet()
84 for system
in systems:
85 log.info(
" -- running for %s", system)
86 system_result =
run_one(system, lbtime, run_iovs)
89 result_iovs.extend(system_result)
93 def go(iov, systems, db, indb, timewise=False, use_flask=False):
95 Run the DCS calculator for `run` on the `systems` specified, saving the
96 result to the `database`.
100 with timer(
"Read LBLB"):
102 lblb =
fetch_iovs(
"LBLB", since, until, with_channel=
False, database=(indb
if indb.startswith(
'sqlite')
else 'COOLONL_TRIGGER/%s' % indb))
103 assert lblb,
"No luminosity blocks for desired range. [%s, %s)" % (since, until)
114 log.debug(
"Will process %i LBs over %i runs", len(lblb), len(run_iovs))
116 log.info(
"DCSC2 will run over range: %r %s", tuple(run_iovs.range_iov),
"(%i lbs)" % len(lblb))
120 systems = [system()
for system
in systems
121 if not getattr(system,
"__DISABLED__",
False)]
130 if log.isEnabledFor(logging.DEBUG):
133 log.info(
"Calculation complete")
137 with timer(
"write result (%i iovs)" % len(result_iovs)):
138 log.debug(
"Writing result (%i iovs)", len(result_iovs))
139 defect_iovs =
list(
filter(
lambda iov: isinstance(iov, DefectIOV), result_iovs))
140 defect_iovs_full = [
DefectIOVFull(recoverable=
False, user=
'sys:defectcalculator', **_._asdict())
141 for _
in defect_iovs]
142 defect_iovs_full.append(
DefectIOVFull(recoverable=
False, user=
'sys:defectcalculator',
143 channel=
'GLOBAL_DCS_UNCHECKED', present=
False,
144 comment=
'Calculator did run',
145 since=since, until=until))
146 if len(defect_iovs) > 0:
147 log.warning(f
'db {db}, read_only {not use_flask}')
148 ddb = DefectsDB(db, read_only=use_flask, create=
not use_flask)
149 defect_names =
set(i.channel
for i
in defect_iovs_full)
150 for defect
in defect_names:
151 if defect
in ddb.defect_id_map:
153 ddb.create_defect(defect,
"Created by DCSCalculator2")
154 with ddb.storage_buffer
if not use_flask
else nullcontext():
158 secret_path = os.environ.get(
'COOLFLASK_SECRET',
'/afs/cern.ch/user/a/atlasdqm/private/coolflask_secret/coolflask_secret.json')
159 auth = json.loads(
open(secret_path).
read())
162 ddb.insert_multiple(defect_iovs_full, use_flask=use_flask, flask_auth=auth)
164 log.warning(
"DCS Calculator failed to upload defects to DB")
165 if config.opts.email_on_failure:
166 from DataQualityUtils.panic
import panic
167 from traceback
import format_exc
168 runnum = lbtime[0].Run
if len(lbtime)>0
else '??????'
169 panicmsg =
"DCS Calculator failed to upload defects to database for run %s\n\n%s" % (runnum, format_exc())
174 args = len(result_iovs),
hash(result_iovs)
175 log.info(
"Success. Calculated %i iovs. Result hash: 0x%0x8.", *args)
179 optp, opts, args = config.parse_options(argv)
183 log.debug(
"Commandline arguments: %s", argv)
184 log.debug(
"Current configuration: %s", (opts))
186 if opts.shell_on_exception:
188 from IPython.core
import ultratb
189 sys.excepthook = ultratb.FormattedTB(call_pdb=
True)
193 if opts.systems
is None:
194 systems = ALL_SYSTEMS
199 for system
in opts.systems:
200 if system
not in SYSTEM_MAP:
201 invalid_systems.append(system)
203 systems.append(SYSTEM_MAP[system])
206 optp.error(
"Invalid system(s) specified: {0}. "
207 "Available systems: {1}".
format(invalid_systems, SYSTEM_MAP.keys()))
210 if (opts.run
and opts.range)
or (
not opts.run
and not opts.range):
211 optp.error(
"Specify either -r or -R")
213 since, until = opts.run, opts.run
215 since, until = map(int, opts.range.split(
"-"))
219 optp.error(
"--lbs and --range are incompatible. "
220 "Use --lbs with --run")
221 sincelb, untillb = map(int, opts.lbs.split(
"-"))
226 go(iov, systems, opts.output_database, opts.input_database, opts.timewise, opts.flask)