58 def run(self, iov, iteration, queue):
60 Runs the algorithm machine over the collected data and
63 if not self.is_valid():
64 raise StrategyError(
'The strategy KLMStripEfficiency was not '
69 basf2.B2INFO(f
'Setting up {self.__class__.__name__} strategy '
70 f
'for {self.algorithm.name}')
73 machine_params[
'database_chain'] = self.database_chain
74 machine_params[
'dependent_databases'] = self.dependent_databases
75 machine_params[
'output_dir'] = self.output_dir
76 machine_params[
'output_database_dir'] = self.output_database_dir
77 machine_params[
'input_files'] = self.input_files
79 self.
machine.setup_from_dict(machine_params)
81 basf2.B2INFO(f
'Starting AlgorithmMachine of {self.algorithm.name}')
82 self.algorithm.algorithm.setCalibrationStage(
83 KLMStripEfficiencyAlgorithm.c_EfficiencyMeasurement)
86 self.
machine.setup_algorithm(iteration=iteration)
88 basf2.B2INFO(f
'Beginning execution of {self.algorithm.name} using '
89 f
'strategy {self.__class__.__name__}')
92 runs = self.algorithm.algorithm.getRunListFromAllData()
93 all_runs_collected = set(runs_from_vector(runs))
96 runs_to_execute = runs_overlapping_iov(iov, all_runs_collected)
98 runs_to_execute = all_runs_collected
101 basf2.B2INFO(f
'Removing the ignored_runs from the runs '
102 f
'to execute for {self.algorithm.name}')
103 runs_to_execute.difference_update(set(self.
ignored_runs))
106 runs_to_execute = sorted(runs_to_execute)
107 runs_to_execute = split_runs_by_exp(runs_to_execute)
111 if 'iov_coverage' in self.algorithm.params:
112 iov_coverage = self.algorithm.params[
'iov_coverage']
115 number_of_experiments = len(runs_to_execute)
116 for i_exp, run_list
in enumerate(runs_to_execute, start=1):
117 lowest_exprun = get_lowest_exprun(number_of_experiments, i_exp,
118 run_list, iov_coverage)
119 highest_exprun = get_highest_exprun(number_of_experiments, i_exp,
120 run_list, iov_coverage)
122 lowest_exprun, highest_exprun)
125 self.send_result(self.
machine.result)
126 if (self.
machine.result.result == AlgResult.ok.value)
or \
127 (self.
machine.result.result == AlgResult.iterate.value):
130 self.send_final_state(self.
FAILED)
155 lowest_exprun, highest_exprun):
157 Process runs from experiment.
165 for exp_run
in experiment_runs:
167 [exp_run], iteration,
False,
168 KLMStripEfficiencyAlgorithm.c_MeasurablePlaneCheck,
None)
169 result = self.
machine.result.result
170 algorithm_results = KLMStripEfficiencyAlgorithm.Results(
171 self.
machine.algorithm.algorithm.getResults())
174 if (algorithm_results.getExtHits() > 0):
175 run_data.append([exp_run.run, result, [exp_run],
176 algorithm_results,
'',
None])
177 result_str = calibration_result_string(result)
178 basf2.B2INFO(f
'Run {int(exp_run.run)}: {result_str}.')
181 run_data.sort(key=
lambda x: x[0])
186 while (i < len(run_data)):
187 if (run_data[i][1] == 2):
189 while (run_data[j][1] == 2):
191 if (j >= len(run_data)):
193 run_ranges.append([i, j])
200 def can_merge(run_data, run_not_enough_data, run_normal):
201 return run_data[run_not_enough_data][3].newExtHitsPlanes(
202 run_data[run_normal][3].getExtHitsPlane()) == 0
204 for run_range
in run_ranges:
205 next_run = run_range[1]
209 if (next_run < len(run_data)):
210 while (i >= run_range[0]):
211 if (can_merge(run_data, i, next_run)):
212 basf2.B2INFO(f
'Run {int(run_data[i][0])} (not enough data) can be merged into the next normal run ' +
213 f
'{int(run_data[next_run][0])}.')
214 run_data[i][4] =
'next'
216 basf2.B2INFO(f
'Run {int(run_data[i][0])} (not enough data) cannot be merged into the next normal run ' +
217 f
'{int(run_data[next_run][0])}, will try the previous one.')
220 if (i < run_range[0]):
222 previous_run = run_range[0] - 1
223 if (previous_run >= 0):
225 if (can_merge(run_data, j, previous_run)):
226 basf2.B2INFO(f
'Run {int(run_data[j][0])} (not enough data) can be merged into the previous normal run ' +
227 f
'{int(run_data[previous_run][0])}.')
228 run_data[j][4] =
'previous'
230 basf2.B2INFO(f
'Run {int(run_data[j][0])} (not enough data) cannot be merged into the previous normal ' +
231 f
'run {int(run_data[previous_run][0])}.')
236 basf2.B2INFO(
'A range of runs with not enough data is found that cannot be merged into neither previous nor ' +
237 f
'next normal run: from {int(run_data[j][0])} to {int(run_data[i][0])}.')
239 run_data[j][4] =
'none'
246 while (i < len(run_data) - 1):
247 while ((run_data[i][1] == 2)
and (run_data[i + 1][1] == 2)):
248 if (run_data[i][4] != run_data[i + 1][4]):
250 basf2.B2INFO(f
'Merging run {int(run_data[i + 1][0])} (not enough data) into run {int(run_data[i][0])} ' +
251 '(not enough data).')
252 run_data[i][2].extend(run_data[i + 1][2])
255 run_data[i][2], iteration,
False,
256 KLMStripEfficiencyAlgorithm.c_MeasurablePlaneCheck,
None)
257 run_data[i][1] = self.
machine.result.result
258 run_data[i][3] = KLMStripEfficiencyAlgorithm.Results(
259 self.
machine.algorithm.algorithm.getResults())
260 result_str = calibration_result_string(run_data[i][1])
261 basf2.B2INFO(f
'Run {int(run_data[i][0])}: {result_str}.')
262 if (i >= len(run_data) - 1):
267 def merge_runs(run_data, run_not_enough_data, run_normal, forced):
268 basf2.B2INFO(f
'Merging run {int(run_data[run_not_enough_data][0])} (not enough data) into run ' +
269 f
'{int(run_data[run_normal][0])} (normal).')
270 run_data[run_normal][2].extend(run_data[run_not_enough_data][2])
272 run_data[run_normal][2], iteration, forced,
273 KLMStripEfficiencyAlgorithm.c_MeasurablePlaneCheck,
None)
274 run_data[run_normal][1] = self.
machine.result.result
275 run_data[run_normal][3] = KLMStripEfficiencyAlgorithm.Results(
276 self.
machine.algorithm.algorithm.getResults())
277 result_str = calibration_result_string(run_data[run_normal][1])
278 basf2.B2INFO(f
'Run {int(run_data[run_normal][0])}: {result_str}.')
279 if (run_data[run_normal][1] != 0):
280 basf2.B2FATAL(f
'Merging run {int(run_data[run_not_enough_data][0])} into ' +
281 f
'run {int(run_data[run_normal][0])} failed.')
282 del run_data[run_not_enough_data]
285 while (i < len(run_data)):
286 if (run_data[i][1] == 2):
287 if (run_data[i][4] ==
'next'):
288 merge_runs(run_data, i, i + 1,
False)
289 elif (run_data[i][4] ==
'previous'):
290 merge_runs(run_data, i, i - 1,
False)
296 while (i < len(run_data)):
297 if (run_data[i][1] == 2
and run_data[i][4] ==
'none'):
298 new_planes_previous = -1
300 if (i < len(run_data) - 1):
301 new_planes_next = run_data[i][3].newExtHitsPlanes(
302 run_data[i + 1][3].getExtHitsPlane())
303 basf2.B2INFO(f
'There are {int(new_planes_next)} new active modules in run {int(run_data[i][0])} ' +
304 f
'relatively to run {int(run_data[i + 1][0])}.')
306 new_planes_previous = run_data[i][3].newExtHitsPlanes(
307 run_data[i - 1][3].getExtHitsPlane())
308 basf2.B2INFO(f
'There are {int(new_planes_previous)} new active modules in run {int(run_data[i][0])} ' +
309 f
'relatively to run {int(run_data[i - 1][0])}.')
318 if (new_planes_previous >= 0
and new_planes_next < 0):
319 run_for_merging = i - 1
320 elif (new_planes_previous < 0
and new_planes_next >= 0):
321 run_for_merging = i + 1
322 elif (new_planes_previous >= 0
and new_planes_next >= 0):
323 if (new_planes_previous < new_planes_next):
324 run_for_merging = i - 1
326 run_for_merging = i + 1
328 basf2.B2INFO(f
'Cannot determine run for merging for run {int(run_data[i][0])}, performing its' +
329 ' forced calibration.')
331 run_data[i][2], iteration,
True,
332 KLMStripEfficiencyAlgorithm.c_MeasurablePlaneCheck,
334 run_data[i][1] = self.
machine.result.result
335 run_data[i][3] = KLMStripEfficiencyAlgorithm.Results(
336 self.
machine.algorithm.algorithm.getResults())
337 result_str = calibration_result_string(run_data[i][1])
338 basf2.B2INFO(f
'Run {int(run_data[i][0])}: {result_str}.')
339 if (run_data[i][1] != 0):
340 basf2.B2FATAL(f
'Forced calibration of run {int(run_data[i][0])} failed.')
341 if (run_for_merging >= 0):
342 merge_runs(run_data, i, run_for_merging,
True)
351 while (i < len(run_data)):
353 while (j < len(run_data)):
354 planes_differ =
False
355 if (run_data[j][3].newMeasuredPlanes(
356 run_data[i][3].getEfficiency()) != 0):
358 if (run_data[i][3].newMeasuredPlanes(
359 run_data[j][3].getEfficiency()) != 0):
362 basf2.B2INFO(f
'Run {int(run_data[j][0])}: the set of planes is different from run {int(run_data[i][0])}.')
365 basf2.B2INFO(f
'Run {int(run_data[j][0])}: the set of planes is the same as for run {int(run_data[i][0])}.')
367 run_ranges.append([i, j])
373 if (
not os.path.isdir(
'efficiency')):
374 os.mkdir(
'efficiency')
377 def merge_runs_2(run_data, run_1, run_2, forced):
378 basf2.B2INFO(f
'Merging run {int(run_data[run_2][0])} into run {int(run_data[run_1][0])}.')
379 run_data[run_1][2].extend(run_data[run_2][2])
380 output_file = f
'efficiency/efficiency_{int(run_data[run_1][2][0].exp)}_{int(run_data[run_1][2][0].run)}.root'
382 run_data[run_1][2], iteration, forced,
383 KLMStripEfficiencyAlgorithm.c_EfficiencyMeasurement,
385 run_data[run_1][1] = self.
machine.result.result
386 run_data[run_1][3] = KLMStripEfficiencyAlgorithm.Results(
387 self.
machine.algorithm.algorithm.getResults())
388 run_data[run_1][5] = \
389 self.
machine.algorithm.algorithm.getPayloadValues()
390 result_str = calibration_result_string(run_data[run_1][1])
391 basf2.B2INFO(f
'Run {int(run_data[run_1][0])}: {result_str}; requested precision {0.02:f}, achieved precision ' +
392 f
'{run_data[run_1][3].getAchievedPrecision():f}.')
394 for run_range
in run_ranges:
396 while (i < run_range[1]):
397 output_file = f
'efficiency/efficiency_{int(run_data[i][2][0].exp)}_{int(run_data[i][2][0].run)}.root'
399 if (i == run_range[1] - 1):
400 forced_calibration =
True
402 forced_calibration =
False
404 run_data[i][2], iteration, forced_calibration,
405 KLMStripEfficiencyAlgorithm.c_EfficiencyMeasurement,
407 run_data[i][1] = self.
machine.result.result
408 run_data[i][3] = KLMStripEfficiencyAlgorithm.Results(
409 self.
machine.algorithm.algorithm.getResults())
411 self.
machine.algorithm.algorithm.getPayloadValues()
412 result_str = calibration_result_string(run_data[i][1])
413 basf2.B2INFO(f
'Run {int(run_data[i][0])}: {result_str}; requested precision {0.02:f}, achieved precision ' +
414 f
'{run_data[i][3].getAchievedPrecision():f}.')
415 if (run_data[i][1] == 2):
417 while (j < run_range[1]):
420 if (j == run_range[1] - 1):
421 forced_calibration =
True
423 forced_calibration =
False
424 merge_runs_2(run_data, i, j, forced_calibration)
427 if (run_data[i][1] == 0):
434 while (i < len(run_data)):
435 if (run_data[i][1] == -1):
441 def commit_payload(run_data, run):
442 basf2.B2INFO(f
'Writing run {int(run_data[run][0])}.')
443 self.
machine.algorithm.algorithm.commit(run_data[run][5])
445 for i
in range(0, len(run_data)):
447 run_data[i][2].sort(key=
lambda x: x.run)
448 first_run = run_data[i][2][0].run
451 run_data[i][5].front().iov = \
455 iov = run_data[previous_run][5].front().iov
456 if (previous_run == 0):
457 run_data[previous_run][5].front().iov = \
459 lowest_exprun.exp, lowest_exprun.run,
460 experiment, first_run - 1)
462 run_data[previous_run][5].front().iov = \
464 experiment, first_run - 1)
465 commit_payload(run_data, previous_run)
470 if (i == len(run_data) - 1):
471 iov = run_data[i][5].front().iov
472 run_data[i][5].front().iov = \
474 experiment, iov.getRunLow(),
475 highest_exprun.exp, highest_exprun.run)
476 commit_payload(run_data, i)