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