156 solenoid_current_override=None, toroids_current_override=None):
157 """
158 Create RunParams by reading from IS via the WEBDAQ REST API.
159
160 This uses the webis_server REST API to fetch run parameters, avoiding
161 direct dependencies on TDAQ libraries. The API endpoint is determined by:
162 1. The webdaq_base parameter if provided
163 2. The TDAQ_WEBDAQ_BASE environment variable
164
165 The IS objects accessed are:
166 - RunParams.RunParams: run_number, det_mask, timeSOR, trigger_type, etc.
167 - Magnets.Magnets: SolenoidCurrent, ToroidsCurrent
168
169 Args:
170 partition: The partition name (default: from TDAQ_PARTITION env var)
171 webdaq_base: Base URL for webis_server (default: from TDAQ_WEBDAQ_BASE env var)
172 strict: If True, raise an exception if IS read fails (for --online-environment)
173 solenoid_current_override: If provided, skip IS fetch for solenoid (command-line override)
174 toroids_current_override: If provided, skip IS fetch for toroids (command-line override)
175
176 Returns:
177 RunParams instance with values from IS, or defaults if unavailable
178
179 Raises:
180 RuntimeError: If strict=True and IS read fails
181 """
182 import requests
183
184
185 if webdaq_base is None:
186 webdaq_base = os.environ.get('TDAQ_WEBDAQ_BASE')
187
188 if not webdaq_base:
189 msg = "TDAQ_WEBDAQ_BASE not set, cannot read from IS"
190 if strict:
191 raise RuntimeError(msg + " (required for --online-environment)")
192 log.warning(msg + ". Using defaults.")
193 return cls()
194
195
196 if partition is None:
197 partition = os.environ.get('TDAQ_PARTITION', 'ATLAS')
198
199 log.info("Reading run parameters from IS via WEBDAQ: %s (partition=%s)", webdaq_base, partition)
200
201 params = {}
202
203
204
205
206 try:
207 url = f"{webdaq_base}/info/current/{partition}/is/RunParams/RunParams.RunParams?format=compact"
208 log.debug("Fetching RunParams from: %s", url)
209
210 response = requests.get(url, timeout=10)
211 if response.status_code == 200:
212 response_data = response.json()
213 log.debug("RunParams response from IS: %s", response_data)
214
215
216 if isinstance(response_data, list) and len(response_data) >= 4:
217 runparams = response_data[3]
218 else:
219 runparams = response_data
220
221 log.debug("RunParams data: %s", runparams)
222
223
224 if 'run_number' in runparams:
225 params['run_number'] = int(runparams['run_number'])
226 if 'lumiblock' in runparams:
227 params['lb_number'] = int(runparams['lumiblock'])
228 if 'det_mask' in runparams:
229 params['detector_mask'] = runparams['det_mask']
230 if 'timeSOR' in runparams:
231 sor_time = runparams['timeSOR']
232
233 if '.' not in sor_time:
234 sor_time += '.000000'
235 params['sor_time'] = sor_time
236 if 'beam_type' in runparams:
237 params['beam_type'] = int(runparams['beam_type'])
238 if 'beam_energy' in runparams:
239 params['beam_energy'] = int(runparams['beam_energy'])
240 if 'run_type' in runparams:
241 params['run_type'] = runparams['run_type']
242 if 'trigger_type' in runparams:
243 params['trigger_type'] = int(runparams['trigger_type'])
244 if 'recording_enabled' in runparams:
245 params['recording_enabled'] = runparams['recording_enabled'] in ('1', 'true', 'True', True, 1)
246
247 log.info("Got run parameters from IS: run=%s, lb=%s",
248 params.get('run_number'), params.get('lb_number'))
249 else:
250 msg = f"Failed to fetch RunParams from IS: HTTP {response.status_code}"
251 if strict:
252 raise RuntimeError(msg + " (required for --online-environment)")
253 log.warning(msg)
254
255 except requests.exceptions.RequestException as e:
256 msg = f"Error fetching RunParams from IS: {e}"
257 if strict:
258 raise RuntimeError(msg + " (required for --online-environment)")
259 log.warning(msg)
260 except (ValueError, KeyError) as e:
261 msg = f"Error parsing RunParams from IS: {e}"
262 if strict:
263 raise RuntimeError(msg + " (required for --online-environment)")
264 log.warning(msg)
265
266
267
268
269 have_solenoid_override = solenoid_current_override is not None
270 have_toroids_override = toroids_current_override is not None
271
272 if have_solenoid_override:
273 params['solenoid_current'] = solenoid_current_override
274 log.info("Using solenoid_current=%.1f from command line override", solenoid_current_override)
275 if have_toroids_override:
276 params['toroids_current'] = toroids_current_override
277 log.info("Using toroids_current=%.1f from command line override", toroids_current_override)
278
279
280 if not (have_solenoid_override and have_toroids_override):
281 try:
282 url = f"{webdaq_base}/info/current/{partition}/is/Magnets/Magnets.Magnets?format=compact"
283 log.debug("Fetching Magnets from: %s", url)
284
285 response = requests.get(url, timeout=10)
286 if response.status_code == 200:
287 response_data = response.json()
288 log.debug("Magnets response from IS: %s", response_data)
289
290 magnets = response_data[3] if isinstance(response_data, list) and len(response_data) >= 4 else response_data
291 log.debug("Magnets data: %s", magnets)
292
293
294
295 if not have_solenoid_override:
296 params['solenoid_current'] = float(magnets['SolenoidCurrent']['value'])
297 if not have_toroids_override:
298 params['toroids_current'] = float(magnets['ToroidsCurrent']['value'])
299
300 log.info("Got magnet currents from IS: solenoid=%s, toroids=%s",
301 params.get('solenoid_current'), params.get('toroids_current'))
302 elif strict:
303 raise RuntimeError(f"Magnets not available from IS: HTTP {response.status_code} "
304 "(required for --online-environment, use --solenoid-current and --toroids-current to override)")
305 else:
306 log.debug("Magnets not available from IS: HTTP %d", response.status_code)
307
308 except requests.exceptions.RequestException as e:
309 if strict:
310 raise RuntimeError(f"Error fetching Magnets from IS: {e} "
311 "(required for --online-environment, use --solenoid-current and --toroids-current to override)")
312 log.debug("Error fetching Magnets from IS: %s", e)
313 except (ValueError, KeyError, TypeError) as e:
314 if strict:
315 raise RuntimeError(f"Error parsing Magnets from IS: {e} "
316 "(required for --online-environment, use --solenoid-current and --toroids-current to override)")
317 log.debug("Error parsing Magnets from IS: %s", e)
318
319
320 if strict and 'run_number' not in params:
321 raise RuntimeError("Failed to get run_number from IS (required for --online-environment)")
322
323 return cls(**params)
324
325