19 from simulation
import add_simulation
20 from rawdata
import add_packers
21 from softwaretrigger
import constants
23 from ROOT
import Belle2
27 """Test module for assuring correct data store content"""
30 """reimplementation of Module::event()."""
33 if not sft_trigger.isValid():
34 basf2.B2FATAL(
"SoftwareTriggerResult object not created")
35 elif len(sft_trigger.getResults()) == 0:
36 basf2.B2FATAL(
"SoftwareTriggerResult exists but has no entries")
39 basf2.B2FATAL(
"ROIs are not present")
42 def generate_input_file(run_type, location, output_file_name, exp_number, passthrough):
44 Generate an input file for usage in the test.
45 Simulate uubar for "beam" and two muons for "cosmic" setting.
47 Only raw data will be stored to the given output file.
48 :param run_type: Whether to simulate cosmic or beam
49 :param location: Whether to simulate expressreco (with ROIs) or hlt (no PXD)
50 :param output_file_name: where to store the result file
51 :param exp_number: which experiment number to simulate
52 :param passthrough: if true don't generate a trigger result in the input file
54 if os.path.exists(output_file_name):
57 basf2.set_random_seed(12345)
60 path.add_module(
'EventInfoSetter', evtNumList=[4], expList=[exp_number])
62 if run_type == constants.RunTypes.beam:
64 elif run_type == constants.RunTypes.cosmic:
68 path.add_module(
"ParticleGun", pdgCodes=[-13, 13], momentumParams=[10, 200])
70 add_simulation(path, usePXDDataReduction=(location == constants.Location.expressreco))
72 if location == constants.Location.hlt:
73 components = DEFAULT_HLT_COMPONENTS
74 elif location == constants.Location.expressreco:
75 components = DEFAULT_EXPRESSRECO_COMPONENTS
77 basf2.B2FATAL(
"Location {} for test is not supported".format(location.name))
79 components.append(
"TRG")
81 add_packers(path, components=components)
84 if location == constants.Location.expressreco
and not passthrough:
85 class FakeHLTResult(basf2.Module):
87 self.results =
Belle2.PyStoreObj(Belle2.SoftwareTriggerResult.Class(),
"SoftwareTriggerResult")
88 self.results.registerInDataStore()
95 if (self.EventMetaData.obj().getEvent() == 1):
96 self.results.addResult(
"software_trigger_cut&all&total_result", 1)
97 self.results.addResult(
"software_trigger_cut&skim&accept_mumutight", 1)
98 self.results.addResult(
"software_trigger_cut&skim&accept_dstar_1", 1)
99 self.results.addResult(
"software_trigger_cut&filter&L1_trigger", 1)
101 elif (self.EventMetaData.obj().getEvent() == 2):
102 self.results.addResult(
"software_trigger_cut&all&total_result", 1)
103 self.results.addResult(
"software_trigger_cut&filter&L1_trigger", 1)
105 elif (self.EventMetaData.obj().getEvent() == 3):
106 self.results.addResult(
"software_trigger_cut&all&total_result", 1)
107 self.results.addResult(
"software_trigger_cut&skim&accept_mumutight", 1)
108 self.results.addResult(
"software_trigger_cut&skim&accept_dstar_1", 1)
109 self.results.addResult(
"software_trigger_cut&filter&L1_trigger", 0)
111 elif (self.EventMetaData.obj().getEvent() == 4):
112 self.results.addResult(
"software_trigger_cut&all&total_result", 0)
113 self.results.addResult(
"software_trigger_cut&skim&accept_mumutight", 1)
114 self.results.addResult(
"software_trigger_cut&skim&accept_dstar_1", 1)
115 self.results.addResult(
"software_trigger_cut&filter&L1_trigger", 0)
117 path.add_module(FakeHLTResult())
120 branch_names = RAWDATA_OBJECTS + [
"EventMetaData",
"TRGSummary"]
122 branch_names += [
"SoftwareTriggerResult"]
123 if location == constants.Location.hlt:
124 branch_names.remove(
"RawPXDs")
125 branch_names.remove(
"ROIs")
128 branch_names.remove(
"RawTRGs")
130 path.add_module(
"RootOutput", outputFileName=output_file_name, branchNames=branch_names)
135 def test_script(script_location, input_file_name, temp_dir):
137 Test a script with the given file path using the given input file.
138 Raises an exception if the execution fails or if the needed output is not in
140 The results are stored in the temporary directory location.
142 :param script_location: the script to test
143 :param input_file_name: the file path of the input file
144 :param temp_dir: where to store and run
146 input_buffer =
"UNUSED"
147 output_buffer =
"UNUSED"
150 random_seed =
"".join(random.choices(
"abcdef", k=4))
152 histos_file_name = f
"{random_seed}_histos.root"
153 output_file_name = os.path.join(temp_dir, f
"{random_seed}_output.root")
155 globaltags = list(basf2.conditions.default_globaltags)
161 os.chdir(os.path.dirname(script_location))
162 cmd1 = [sys.executable, script_location,
"--central-db-tag"] + globaltags + [
163 "--input-file", os.path.abspath(input_file_name),
164 "--histo-output-file", f
"./{histos_file_name}",
165 "--output-file", os.path.abspath(output_file_name),
166 "--number-processes", str(num_processes),
167 input_buffer, output_buffer, str(histo_port)
169 subprocess.check_call(cmd1)
174 if os.path.exists(histos_file_name):
175 final_histos_file_name = os.path.join(temp_dir, histos_file_name)
176 shutil.move(histos_file_name, os.path.join(temp_dir, final_histos_file_name))
181 if "expressreco" not in script_location
and "beam_reco" in script_location:
183 test_path = basf2.Path()
184 test_path.add_module(
"RootInput", inputFileName=output_file_name)
188 cmd2 = [
"hlt-check-dqm-size", final_histos_file_name]
189 subprocess.check_call(cmd2)
192 def test_folder(location, run_type, exp_number, phase, passthrough=False):
194 Run all hlt operation scripts in a given folder
195 and test the outputs of the files.
197 Will call the test_script function on all files in the folder given
198 by the location after having created a suitable input file with the given
201 :param location: hlt or expressreco, depending on which operation files to run
202 and which input to simulate
203 :param run_type: cosmic or beam, depending on which operation files to run
204 and which input to simulate
205 :param exp_number: which experiment number to simulate
206 :param phase: where to look for the operation files (will search in the folder
207 hlt/operation/{phase}/global/{location}/evp_scripts/)
208 :param passthrough: only relevant for express reco: If true don't create a
209 software trigger result in the input file to test running
210 express reco if hlt is in passthrough mode
215 temp_dir = os.getcwd()
216 output_file_name = os.path.join(temp_dir, f
"{location.name}_{run_type.name}.root")
217 generate_input_file(run_type=run_type, location=location,
218 output_file_name=output_file_name, exp_number=exp_number,
219 passthrough=passthrough)
221 script_dir = basf2.find_file(f
"hlt/operation/{phase}/global/{location.name}/evp_scripts/")
222 run_at_least_one =
False
223 for script_location
in glob(os.path.join(script_dir, f
"run_{run_type.name}*.py")):
224 run_at_least_one =
True
225 test_script(script_location, input_file_name=output_file_name, temp_dir=temp_dir)
227 assert run_at_least_one
a (simplified) python wrapper for StoreArray.
a (simplified) python wrapper for StoreObjPtr.
def safe_process(*args, **kwargs)
def add_continuum_generator(path, finalstate, userdecfile='', *skip_on_failure=True)