148 lowest_exprun, highest_exprun):
149 """
150 Process runs from experiment.
151 """
152
153
154
155 run_data = []
156
157
158 for exp_run in experiment_runs:
159 self.execute_over_run_list(
160 [exp_run], iteration, False,
161 KLMStripEfficiencyAlgorithm.c_MeasurablePlaneCheck, None)
162 result = self.machine.result.result
163 algorithm_results = KLMStripEfficiencyAlgorithm.Results(
164 self.machine.algorithm.algorithm.getResults())
165
166
167 if (algorithm_results.getExtHits() > 0):
168 run_data.append([exp_run.run, result, [exp_run],
169 algorithm_results, '', None])
170 result_str = calibration_result_string(result)
171 basf2.B2INFO(f'Run {int(exp_run.run)}: {result_str}.')
172
173
174 run_data.sort(key=lambda x: x[0])
175
176
177 run_ranges = []
178 i = 0
179 while (i < len(run_data)):
180 if (run_data[i][1] == 2):
181 j = i
182 while (run_data[j][1] == 2):
183 j += 1
184 if (j >= len(run_data)):
185 break
186 run_ranges.append([i, j])
187 i = j
188 else:
189 i += 1
190
191
192
193 def can_merge(run_data, run_not_enough_data, run_normal):
194 return run_data[run_not_enough_data][3].newExtHitsPlanes(
195 run_data[run_normal][3].getExtHitsPlane()) == 0
196
197 for run_range in run_ranges:
198 next_run = run_range[1]
199
200 j = run_range[0]
201 i = next_run - 1
202 if (next_run < len(run_data)):
203 while (i >= run_range[0]):
204 if (can_merge(run_data, i, next_run)):
205 basf2.B2INFO(f'Run {int(run_data[i][0])} (not enough data) can be merged into the next normal run ' +
206 f'{int(run_data[next_run][0])}.')
207 run_data[i][4] = 'next'
208 else:
209 basf2.B2INFO(f'Run {int(run_data[i][0])} (not enough data) cannot be merged into the next normal run ' +
210 f'{int(run_data[next_run][0])}, will try the previous one.')
211 break
212 i -= 1
213 if (i < run_range[0]):
214 continue
215 previous_run = run_range[0] - 1
216 if (previous_run >= 0):
217 while (j <= i):
218 if (can_merge(run_data, j, previous_run)):
219 basf2.B2INFO(f'Run {int(run_data[j][0])} (not enough data) can be merged into the previous normal run ' +
220 f'{int(run_data[previous_run][0])}.')
221 run_data[j][4] = 'previous'
222 else:
223 basf2.B2INFO(f'Run {int(run_data[j][0])} (not enough data) cannot be merged into the previous normal ' +
224 f'run {int(run_data[previous_run][0])}.')
225 break
226 j += 1
227 if (j > i):
228 continue
229 basf2.B2INFO('A range of runs with not enough data is found that cannot be merged into neither previous nor ' +
230 f'next normal run: from {int(run_data[j][0])} to {int(run_data[i][0])}.')
231 while (j <= i):
232 run_data[j][4] = 'none'
233 j += 1
234
235
236
237 i = 0
238 j = 0
239 while (i < len(run_data) - 1):
240 while ((run_data[i][1] == 2) and (run_data[i + 1][1] == 2)):
241 if (run_data[i][4] != run_data[i + 1][4]):
242 break
243 basf2.B2INFO(f'Merging run {int(run_data[i + 1][0])} (not enough data) into run {int(run_data[i][0])} ' +
244 '(not enough data).')
245 run_data[i][2].extend(run_data[i + 1][2])
246 del run_data[i + 1]
247 self.execute_over_run_list(
248 run_data[i][2], iteration, False,
249 KLMStripEfficiencyAlgorithm.c_MeasurablePlaneCheck, None)
250 run_data[i][1] = self.machine.result.result
251 run_data[i][3] = KLMStripEfficiencyAlgorithm.Results(
252 self.machine.algorithm.algorithm.getResults())
253 result_str = calibration_result_string(run_data[i][1])
254 basf2.B2INFO(f'Run {int(run_data[i][0])}: {result_str}.')
255 if (i >= len(run_data) - 1):
256 break
257 i += 1
258
259
260 def merge_runs(run_data, run_not_enough_data, run_normal, forced):
261 basf2.B2INFO(f'Merging run {int(run_data[run_not_enough_data][0])} (not enough data) into run ' +
262 f'{int(run_data[run_normal][0])} (normal).')
263 run_data[run_normal][2].extend(run_data[run_not_enough_data][2])
264 self.execute_over_run_list(
265 run_data[run_normal][2], iteration, forced,
266 KLMStripEfficiencyAlgorithm.c_MeasurablePlaneCheck, None)
267 run_data[run_normal][1] = self.machine.result.result
268 run_data[run_normal][3] = KLMStripEfficiencyAlgorithm.Results(
269 self.machine.algorithm.algorithm.getResults())
270 result_str = calibration_result_string(run_data[run_normal][1])
271 basf2.B2INFO(f'Run {int(run_data[run_normal][0])}: {result_str}.')
272 if (run_data[run_normal][1] != 0):
273 basf2.B2FATAL(f'Merging run {int(run_data[run_not_enough_data][0])} into ' +
274 f'run {int(run_data[run_normal][0])} failed.')
275 del run_data[run_not_enough_data]
276
277 i = 0
278 while (i < len(run_data)):
279 if (run_data[i][1] == 2):
280 if (run_data[i][4] == 'next'):
281 merge_runs(run_data, i, i + 1, False)
282 elif (run_data[i][4] == 'previous'):
283 merge_runs(run_data, i, i - 1, False)
284 else:
285 i += 1
286 else:
287 i += 1
288 i = 0
289 while (i < len(run_data)):
290 if (run_data[i][1] == 2 and run_data[i][4] == 'none'):
291 new_planes_previous = -1
292 new_planes_next = -1
293 if (i < len(run_data) - 1):
294 new_planes_next = run_data[i][3].newExtHitsPlanes(
295 run_data[i + 1][3].getExtHitsPlane())
296 basf2.B2INFO(f'There are {int(new_planes_next)} new active modules in run {int(run_data[i][0])} ' +
297 f'relatively to run {int(run_data[i + 1][0])}.')
298 if (i > 0):
299 new_planes_previous = run_data[i][3].newExtHitsPlanes(
300 run_data[i - 1][3].getExtHitsPlane())
301 basf2.B2INFO(f'There are {int(new_planes_previous)} new active modules in run {int(run_data[i][0])} ' +
302 f'relatively to run {int(run_data[i - 1][0])}.')
303 run_for_merging = -1
304
305
306
307
308
309
310
311 if (new_planes_previous >= 0 and new_planes_next < 0):
312 run_for_merging = i - 1
313 elif (new_planes_previous < 0 and new_planes_next >= 0):
314 run_for_merging = i + 1
315 elif (new_planes_previous >= 0 and new_planes_next >= 0):
316 if (new_planes_previous < new_planes_next):
317 run_for_merging = i - 1
318 else:
319 run_for_merging = i + 1
320 else:
321 basf2.B2INFO(f'Cannot determine run for merging for run {int(run_data[i][0])}, performing its' +
322 ' forced calibration.')
323 self.execute_over_run_list(
324 run_data[i][2], iteration, True,
325 KLMStripEfficiencyAlgorithm.c_MeasurablePlaneCheck,
326 None)
327 run_data[i][1] = self.machine.result.result
328 run_data[i][3] = KLMStripEfficiencyAlgorithm.Results(
329 self.machine.algorithm.algorithm.getResults())
330 result_str = calibration_result_string(run_data[i][1])
331 basf2.B2INFO(f'Run {int(run_data[i][0])}: {result_str}.')
332 if (run_data[i][1] != 0):
333 basf2.B2FATAL(f'Forced calibration of run {int(run_data[i][0])} failed.')
334 if (run_for_merging >= 0):
335 merge_runs(run_data, i, run_for_merging, True)
336 else:
337 i += 1
338
339
340
341
342 run_ranges.clear()
343 i = 0
344 while (i < len(run_data)):
345 j = i + 1
346 while (j < len(run_data)):
347 planes_differ = False
348 if (run_data[j][3].newMeasuredPlanes(
349 run_data[i][3].getEfficiency()) != 0):
350 planes_differ = True
351 if (run_data[i][3].newMeasuredPlanes(
352 run_data[j][3].getEfficiency()) != 0):
353 planes_differ = True
354 if (planes_differ):
355 basf2.B2INFO(f'Run {int(run_data[j][0])}: the set of planes is different from run {int(run_data[i][0])}.')
356 break
357 else:
358 basf2.B2INFO(f'Run {int(run_data[j][0])}: the set of planes is the same as for run {int(run_data[i][0])}.')
359 j = j + 1
360 run_ranges.append([i, j])
361 i = j
362
363
364
365
366 if (not os.path.isdir('efficiency')):
367 os.mkdir('efficiency')
368
369
370 def merge_runs_2(run_data, run_1, run_2, forced):
371 basf2.B2INFO(f'Merging run {int(run_data[run_2][0])} into run {int(run_data[run_1][0])}.')
372 run_data[run_1][2].extend(run_data[run_2][2])
373 output_file = f'efficiency/efficiency_{int(run_data[run_1][2][0].exp)}_{int(run_data[run_1][2][0].run)}.root'
374 self.execute_over_run_list(
375 run_data[run_1][2], iteration, forced,
376 KLMStripEfficiencyAlgorithm.c_EfficiencyMeasurement,
377 output_file)
378 run_data[run_1][1] = self.machine.result.result
379 run_data[run_1][3] = KLMStripEfficiencyAlgorithm.Results(
380 self.machine.algorithm.algorithm.getResults())
381 run_data[run_1][5] = \
382 self.machine.algorithm.algorithm.getPayloadValues()
383 result_str = calibration_result_string(run_data[run_1][1])
384 basf2.B2INFO(f'Run {int(run_data[run_1][0])}: {result_str}; requested precision {0.02:f}, achieved precision ' +
385 f'{run_data[run_1][3].getAchievedPrecision():f}.')
386
387 for run_range in run_ranges:
388 i = run_range[0]
389 while (i < run_range[1]):
390 output_file = f'efficiency/efficiency_{int(run_data[i][2][0].exp)}_{int(run_data[i][2][0].run)}.root'
391
392 if (i == run_range[1] - 1):
393 forced_calibration = True
394 else:
395 forced_calibration = False
396 self.execute_over_run_list(
397 run_data[i][2], iteration, forced_calibration,
398 KLMStripEfficiencyAlgorithm.c_EfficiencyMeasurement,
399 output_file)
400 run_data[i][1] = self.machine.result.result
401 run_data[i][3] = KLMStripEfficiencyAlgorithm.Results(
402 self.machine.algorithm.algorithm.getResults())
403 run_data[i][5] = \
404 self.machine.algorithm.algorithm.getPayloadValues()
405 result_str = calibration_result_string(run_data[i][1])
406 basf2.B2INFO(f'Run {int(run_data[i][0])}: {result_str}; requested precision {0.02:f}, achieved precision ' +
407 f'{run_data[i][3].getAchievedPrecision():f}.')
408 if (run_data[i][1] == 2):
409 j = i + 1
410 while (j < run_range[1]):
411
412
413 if (j == run_range[1] - 1):
414 forced_calibration = True
415 else:
416 forced_calibration = False
417 merge_runs_2(run_data, i, j, forced_calibration)
418 run_data[j][1] = -1
419 j = j + 1
420 if (run_data[i][1] == 0):
421 break
422 i = j
423 else:
424 i = i + 1
425
426 i = 0
427 while (i < len(run_data)):
428 if (run_data[i][1] == -1):
429 del run_data[i]
430 else:
431 i = i + 1
432
433
434 def commit_payload(run_data, run):
435 basf2.B2INFO(f'Writing run {int(run_data[run][0])}.')
436 self.machine.algorithm.algorithm.commit(run_data[run][5])
437
438 for i in range(0, len(run_data)):
439
440 run_data[i][2].sort(key=lambda x: x.run)
441 first_run = run_data[i][2][0].run
442
443
444 run_data[i][5].front().iov = \
446
447 if (i > 0):
448 iov = run_data[previous_run][5].front().iov
449 if (previous_run == 0):
450 run_data[previous_run][5].front().iov = \
452 lowest_exprun.exp, lowest_exprun.run,
453 experiment, first_run - 1)
454 else:
455 run_data[previous_run][5].front().iov = \
457 experiment, first_run - 1)
458 commit_payload(run_data, previous_run)
459 previous_run = i
460 if (i == 0):
461 previous_run = 0
462
463 if (i == len(run_data) - 1):
464 iov = run_data[i][5].front().iov
465 run_data[i][5].front().iov = \
467 experiment, iov.getRunLow(),
468 highest_exprun.exp, highest_exprun.run)
469 commit_payload(run_data, i)
A class that describes the interval of experiments/runs for which an object in the database is valid.