File indexing completed on 2025-07-12 07:56:18
0001 import subprocess
0002 import os
0003
0004 def setup_environment():
0005 """
0006 Updates the LD_LIBRARY_PATH environment variable to include custom library paths.
0007 """
0008
0009 current_ld_library_path = os.environ.get('LD_LIBRARY_PATH', '')
0010
0011
0012 prefix_path = '/var/project/prefix/lib'
0013
0014
0015 new_ld_library_path = (prefix_path + ':' + current_ld_library_path) if current_ld_library_path else prefix_path
0016
0017
0018 os.environ['LD_LIBRARY_PATH'] = new_ld_library_path
0019 print("Updated LD_LIBRARY_PATH:", os.environ['LD_LIBRARY_PATH'])
0020
0021 def run_command(command):
0022 """
0023 Executes a given command in the shell and prints the output as it appears.
0024
0025 Parameters:
0026 command (list): A list containing the command and its arguments.
0027 """
0028 print("Executing:", " ".join(command))
0029 process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
0030
0031
0032 while True:
0033 output = process.stdout.readline()
0034 if output == '' and process.poll() is not None:
0035 break
0036 if output:
0037 print(output.strip())
0038
0039
0040 err = process.stderr.read()
0041 if err:
0042 print("Error:", err)
0043
0044
0045 process.wait()
0046 print("Command completed with return code:", process.returncode)
0047 print("\n" + "-"*50 + "\n")
0048
0049 def get_hepmc_path(beam, minq2):
0050 return f"root://dtn-eic.jlab.org//volatile/eic/EPIC/EVGEN/DIS/NC/{beam}/minQ2={minq2}/pythia8NCDIS_{beam}_minQ2={minq2}_beamEffects_xAngle=-0.025_hiDiv_1.hepmc3.tree.root
0051
0052 def get_base_name(beam, minq2, event_num):
0053 return f"py8_dis-cc_{beam}_minq2-{minq2}_minp-150mev_vtxcut-5m_nevt-{event_num}"
0054
0055 def run_simulation(beam, minq2, event_num, detector_path, steering_file):
0056 """
0057 Runs the simulation for a given beam, Q2 value, and event number, then converts the output file.
0058
0059 Parameters:
0060 beam (str): The energy configuration for the beam.
0061 minq2 (int): The minimum Q2 value.
0062 event_num (int): The number of events to simulate.
0063 detector_path (str): Path to the detector configuration XML file.
0064 """
0065 # Construct the input file URL
0066 url = get_hepmc_path(beam, minq2)
0067
0068 # Construct the output file name
0069 output_base = get_base_name(beam, minq2, event_num)
0070 output_edm4hep = output_base + ".edm4hep.root"
0071 output_evttxt = output_base + ".evt.txt"
0072 event_prefix = f"CC_{beam}_minq2_{minq2}"
0073
0074
0075 # Command for npsim
0076 npsim_command = [
0077 "npsim",
0078 "--compactFile", detector_path,
0079 "-N", str(event_num),
0080 "--inputFiles", url,
0081 "--random.seed", "1",
0082 "--outputFile", output_edm4hep,
0083 # "-v", "WARNING",
0084 "--steeringFile", steering_file
0085 # #"npsim",
0086 # "python3", "npsim_stepping.py"
0087 # "--compactFile", detector_path,
0088 # "-N", str(event_num),
0089 # "--inputFiles", url,
0090 # "--random.seed", "1",
0091 # "--outputFile", output_file,
0092 # "--steeringFile=steering.py"
0093 ]
0094
0095 # Run the simulation
0096 run_command(npsim_command)
0097
0098 # Command for converting the output file to JSON format
0099 reconstruction_command = [
0100 "eicrecon",
0101 f"-Pjana:debug_plugin_loading=1",
0102 f"-Pjana:nevents={event_num}",
0103 f"-Pjana:timeout=0",
0104 f"-Ppodio:output_file={output_base}.edm4eic.root",
0105 f"-Pdd4hep:xml_files={detector_path}",
0106 f"{output_base}.edm4hep.root"
0107 ]
0108
0109 # Run the conversion
0110 run_command(reconstruction_command)
0111
0112 setup_environment()
0113
0114 # Set the detector path (assuming it's predefined as an environment variable or explicitly defined here)
0115 DETECTOR_PATH = os.getenv('DETECTOR_PATH', '/opt/detector/epic-main/share/epic/')
0116
0117 # Matrix definitions for beams and Q2 values
0118 beams = ['5x41', '10x100', '18x275']
0119 minq2s = [1, 100, 1000]
0120
0121 # Iterate over each combination of beam and minq2
0122 for beam in beams:
0123 for minq2 in minq2s:
0124 print("\n---------------------------------------------------------------------------------"*5)
0125 run_simulation(beam, minq2, 5, f'{DETECTOR_PATH}/epic_full.xml', '/mnt/dd4hep-plugin/firebird_steering.py')
0126 print("\n/////////////////////////////////////////////////////////////////////////////////"*5)