28 return logging.getLogger(__name__)
36 """Read generated events or generate new events"""
39 description =
"Simulate events using various generator and detector setups from command line."
44 generator_module =
None
46 detector_setup =
"Default"
52 disable_deltas =
False
54 simulation_output =
None
57 """Convert command-line arguments to basf2 argument list"""
60 setup_argument_group = argument_parser.add_argument_group(
"Detector setup arguments")
61 setup_argument_group.add_argument(
64 dest=
'detector_setup',
65 default=argparse.SUPPRESS,
66 metavar=
'DETECTOR_SETUP_NAME',
68 help=(
'Name of the detector setup to be used')
71 setup_argument_group.add_argument(
76 default=argparse.SUPPRESS,
79 help=(
'Overrides the components of the detector setup')
82 generator_argument_group = argument_parser.add_argument_group(
"Generator arguments")
83 generator_argument_group.add_argument(
86 dest=
'generator_module',
87 default=argparse.SUPPRESS,
88 metavar=
'GENERATOR_NAME',
90 help=
'Name module or short name of the generator to be used.',
93 simulation_argument_group = argument_parser.add_argument_group(
"Simulation arguments")
94 simulation_argument_group.add_argument(
100 metavar=
'BACKGROUND_DIRECTORY',
101 help=
'Path to folder of files or to a file containing the background to be used. ' +
102 'Can be given multiple times.',
105 simulation_argument_group.add_argument(
108 help=
'Disable the generation of delta rays in the simulation'
111 simulation_argument_group.add_argument(
113 '--simulation-output',
117 dest=
'simulation_output',
118 help=
'Only generate and simulate the events and write them to the given output file. Skip rest of the path.'
121 return argument_parser
124 """Configure for basf2 job; disable ROOT input if simulating events"""
127 get_logger().info(
"Requested to simulation run. Deactivate input file")
132 """Run the basf2 job"""
138 path = ReadOrGenerateEventsRun.create_path(self)
142 """Create and configure the basf2 path"""
146 path.add_module(
"Gearbox")
147 path.add_module(
"Geometry", useDB=
True)
150 detector_setup_function = detector_setups_by_short_name[detector_setup]
151 components = detector_setup_function(path)
161 add_evtgen_generator(path, dec_file_path)
164 utilities.extend_path(path,
166 generators_by_short_name,
167 allow_function_import=
True)
171 bkg_file_paths = get_bkg_file_paths(self.
bkg_filesbkg_files)
174 components=components,
175 bkgfiles=bkg_file_paths)
178 adjustments.disable_deltas(path)
183 raise RuntimeError(
'Need at least one of root_input_file,'
184 ' generator_module or bkg_files specified.')
191 path.add_module(
'RootOutput',
198 """Generate events using the EvtGen generator"""
200 generator_module =
"EvtGenInput"
207 electron_pdg_code = 11
222 proton_pdg_code = 2212
225 def add_single_gun_generator(path):
226 """Add ParticleGun with a single muon"""
227 path.add_module(
"ParticleGun",
228 pdgCodes=[muon_pdg_code, -muon_pdg_code],
231 momentumGeneration=
'inversePt',
232 momentumParams=[0.6, 1.4],
233 thetaGeneration=
'uniform',
234 thetaParams=[17., 150.])
237 def add_transverse_gun_generator(path):
238 """Add ParticleGun to illuminate a region of the phase space with low efficiency"""
239 path.add_module(
"ParticleGun",
240 pdgCodes=[muon_pdg_code, -muon_pdg_code],
243 momentumGeneration=
'inversePt',
244 momentumParams=[0.275, 0.276],
245 thetaGeneration=
'uniform',
246 thetaParams=[89., 91.])
249 def add_simple_gun_generator(path):
250 """Add ParticleGun firing 10 muons at medium energy"""
251 path.add_module(
"ParticleGun",
252 pdgCodes=[muon_pdg_code, -muon_pdg_code],
255 momentumGeneration=
'inversePt',
256 momentumParams=[0.6, 1.4],
257 thetaGeneration=
'uniform')
260 def add_low_gun_generator(path):
261 """Add ParticleGun firing 10 muons at low energy"""
262 path.add_module(
"ParticleGun",
263 pdgCodes=[muon_pdg_code, -muon_pdg_code],
266 momentumGeneration=
'inversePt',
267 momentumParams=[0.4, 0.8],
268 thetaGeneration=
'uniform')
271 def add_gun_generator(path):
272 """Add ParticleGun firing 10 muons with wide energy range"""
273 path.add_module(
"ParticleGun",
274 pdgCodes=[muon_pdg_code, -muon_pdg_code],
277 momentumGeneration=
'inversePt',
278 thetaGeneration=
'uniform',
279 thetaParams=[17., 150.])
282 def add_eloss_gun_generator(path):
283 """Add ParticleGun particle gun for energy loss estimations"""
284 path.add_module(
"ParticleGun",
297 momentumParams=[0.3, 2],
300 thetaGeneration=
'uniform',
302 thetaParams=[89., 91],
306 def add_forward_gun_generator(path):
307 """Add ParticleGun with one muon in rather forward direction"""
308 path.add_module(
"ParticleGun",
309 pdgCodes=[muon_pdg_code, -muon_pdg_code],
312 momentumGeneration=
'inversePt',
313 thetaGeneration=
'uniform',
314 thetaParams=[30., 31.])
317 def add_evtgen_generator(path, dec_file_path=None):
318 """Add Y4S generator"""
321 path.add_module(
"EvtGenInput", userDECFile=dec_file_path)
323 path.add_module(
"EvtGenInput")
326 def add_cosmics_generator(path):
327 """Add simple cosmics generator"""
331 def add_sector_tb_generator(path, sector=1):
332 phiBounds = (240 + 60.0 * sector % 360.0, 300 + 60.0 * sector % 360.0)
333 path.add_module(
"ParticleGun",
334 pdgCodes=[muon_pdg_code, -muon_pdg_code],
337 momentumGeneration=
'uniform',
338 momentumParams=[2.0, 4.0],
339 phiGeneration=
'uniform',
341 thetaGeneration=
'uniform',
342 thetaParams=[70., 110.])
345 def add_cosmics_tb_generator(path):
346 """Add simple cosmics generator resembling the test beam setup"""
347 path.add_module(
"Cosmics",
358 cosmics_selector = path.add_module(
'CDCCosmicSelector',
367 cosmics_selector.if_false(basf2.create_path())
370 def add_cry_tb_generator(path):
371 """Add cry generator resembling the test beam setup"""
373 keep_box=[0.7, 0.3, 0.3],
374 pre_general_run_setup=
"normal")
377 def add_no_generator(path):
378 """Add no generator for e.g. background only studies"""
384 generators_by_short_name = {
385 'single_gun': add_single_gun_generator,
386 'transverse_gun': add_transverse_gun_generator,
387 'simple_gun': add_simple_gun_generator,
388 'low_gun': add_low_gun_generator,
389 'forward_gun': add_forward_gun_generator,
390 'gun': add_gun_generator,
391 'eloss_gun': add_eloss_gun_generator,
392 'generic': add_evtgen_generator,
393 "EvtGenInput": add_evtgen_generator,
394 'cosmics': add_cosmics_generator,
395 'cosmics_tb': add_cosmics_tb_generator,
396 'cry_tb': add_cry_tb_generator,
397 'sector_tb': add_sector_tb_generator,
398 'bkg': add_no_generator,
399 'none': add_no_generator,
403 valid_generator_short_names = list(generators_by_short_name.keys())
408 def setup_default_detector(path):
412 def setup_tracking_detector(path):
413 components = [
"BeamPipe",
"PXD",
"SVD",
"CDC",
"MagneticField"]
415 (
"/DetectorComponent[@name='MagneticField']//Component[@type='3d'][2]/ExcludeRadiusMax",
419 adjustments.adjust_module(path,
"Gearbox", override=override)
420 adjustments.adjust_module(path,
"Geometry", components=components)
424 def setup_tracking_detector_constant_b(path):
425 components = [
"BeamPipe",
"PXD",
"SVD",
"CDC",
"MagneticFieldConstant4LimitedRCDC"]
426 adjustments.adjust_module(path,
"Geometry", components=components)
430 def setup_cdc_cr_test(path):
434 (
"/Global/length",
"8.",
"m"),
435 (
"/Global/width",
"8.",
"m"),
436 (
"/Global/height",
"1.5",
"m"),
439 (
"/DetectorComponent[@name='CDC']//t0FileName",
"t0.dat",
""),
440 (
"/DetectorComponent[@name='CDC']//xtFileName",
"xt_noB_v1.dat",
""),
441 (
"/DetectorComponent[@name='CDC']//GlobalPhiRotation",
"1.875",
"deg"),
445 adjustments.adjust_module(path,
"Gearbox", override=override)
446 adjustments.adjust_module(path,
"Geometry", components=components)
450 def setup_cdc_top_test(path):
455 (
"/Global/length",
"8.",
"m"),
456 (
"/Global/width",
"8.",
"m"),
457 (
"/Global/height",
"1.5",
"m"),
460 (
"/DetectorComponent[@name='CDC']//t0FileName",
"t0.dat",
""),
461 (
"/DetectorComponent[@name='CDC']//xtFileName",
"xt_noB_v1.dat",
""),
463 (
"/DetectorComponent[@name='CDC']//GlobalPhiRotation",
"1.875",
"deg"),
464 (
"/DetectorComponent[@name='MagneticField']//Component/Z",
"0",
""),
467 adjustments.adjust_module(path,
"Gearbox",
469 fileName=
"geometry/CDCcosmicTests.xml"
472 adjustments.adjust_module(path,
"Geometry", components=components)
476 detector_setups_by_short_name = {
477 "Default": setup_default_detector,
478 'TrackingDetector': setup_tracking_detector,
479 'TrackingDetectorConstB': setup_tracking_detector_constant_b,
480 'CDCCRTest': setup_cdc_cr_test,
481 'CDCTOPTest': setup_cdc_top_test,
488 def is_bkg_file(bkg_file_path):
489 """Test if a file path points to a file containing background mixins.
497 Simple test only checks if file exists and ends with ".root"
499 return os.path.isfile(bkg_file_path)
and bkg_file_path.endswith(
'.root')
502 def get_bkg_file_paths(bkg_dir_or_file_paths):
503 """Unpacks the content of a single or a list of directories and/or files filtering for
504 files containing background mixins.
508 bkg_dir_or_file_paths : string or iterable of strings
509 Single file or single directory in which background files are located or
510 a list of files and/or directories.
515 A list of paths to individual background files.
518 if isinstance(bkg_dir_or_file_paths, str):
519 bkg_dir_or_file_path = bkg_dir_or_file_paths
520 bkg_dir_or_file_paths = [bkg_dir_or_file_path]
523 for bkg_dir_or_file_path
in bkg_dir_or_file_paths:
524 if is_bkg_file(bkg_dir_or_file_path):
525 bkg_file_path = bkg_dir_or_file_path
526 result.append(bkg_file_path)
527 elif os.path.isdir(bkg_dir_or_file_path):
529 bkg_dir_path = bkg_dir_or_file_path
530 bkg_dir_contents = os.listdir(bkg_dir_path)
531 for dir_or_file_name
in bkg_dir_contents:
532 dir_or_file_path = os.path.join(bkg_dir_path, dir_or_file_name)
533 if is_bkg_file(dir_or_file_path):
534 bkg_file_path = dir_or_file_path
535 result.append(bkg_file_path)
def add_beamparameters(path, name, E_cms=None, **argk)
def create_argument_parser(self, **kwds)
def configure(self, arguments)
components
By default, do specific components.
bool disable_deltas
By default, do not disable delta-ray generation.
simulation_output
By default, do no store the simulation output.
string detector_setup
By default, use the default detector setup.
root_input_file
generating events, so there is no ROOT input file
generator_module
By default, do not generate events.
list bkg_files
By default, no background overlay.
root_input_file
By default, there is no input ROOT TFile.
def add_cosmics_generator(path, components=None, global_box_size=None, accept_box=None, keep_box=None, geometry_xml_file='geometry/Beast2_phase2.xml', cosmics_data_dir='data/generators/modules/cryinput/', setup_file='generators/scripts/cry.setup', data_taking_period='gcr2017', top_in_counter=False)
def add_simulation(path, components=None, bkgfiles=None, bkgOverlay=True, forceSetPXDDataReduction=False, usePXDDataReduction=True, cleanupPXDDataReduction=True, generate_2nd_cdc_hits=False, simulateT0jitter=True, isCosmics=False, FilterEvents=False, usePXDGatedMode=False, skipExperimentCheckForBG=False, save_slow_pions_in_mc=False)