Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-07 07:50:50

0001 # docker run -it --rm -v/home/romanov/dev/firebird:/mnt eicweb/eic_xl:nightly
0002 # cd /mnt/dd4hep-plugin/
0003 # mkdir build && cd build
0004 # This will create prefix/lib folder after the install
0005 # cmake .. && make && make install
0006 # cd .. && ls prefix/lib  # <= Ensure libfirebird-dd4hep.so is there
0007 # export LD_LIBRARY_PATH="/mnt/dd4hep-plugin/prefix/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
0008 # mkdir -p tmp && cd tmp
0009 # source /opt/detector/epic-main/bin/thisepic.sh
0010 # python3 /mnt/test/run_many_energies.py
0011 
0012 import subprocess
0013 import os
0014 
0015 def setup_environment():
0016     """
0017     Updates the LD_LIBRARY_PATH environment variable to include custom library paths.
0018     """
0019 
0020     current_ld_library_path = os.environ.get('LD_LIBRARY_PATH', '')
0021 
0022     # Define the prefix you want to add
0023     prefix_path = '/var/project/prefix/lib'
0024 
0025     # Prepend the prefix to the LD_LIBRARY_PATH
0026     new_ld_library_path = (prefix_path + ':' + current_ld_library_path) if current_ld_library_path else prefix_path
0027 
0028     # Set the new LD_LIBRARY_PATH in the environment
0029     os.environ['LD_LIBRARY_PATH'] = new_ld_library_path
0030     print("Updated LD_LIBRARY_PATH:", os.environ['LD_LIBRARY_PATH'])
0031 
0032 def run_command(command):
0033     """
0034     Executes a given command in the shell and prints the output as it appears.
0035 
0036     Parameters:
0037         command (list): A list containing the command and its arguments.
0038     """
0039     print("Executing:", " ".join(command))
0040     process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
0041 
0042     # Print output as it appears
0043     while True:
0044         output = process.stdout.readline()
0045         if output == '' and process.poll() is not None:
0046             break
0047         if output:
0048             print(output.strip())
0049 
0050     # Handle errors if there are any
0051     err = process.stderr.read()
0052     if err:
0053         print("Error:", err)
0054 
0055     # Check the process return code
0056     process.wait()
0057     print("Command completed with return code:", process.returncode)
0058     print("\n" + "-"*50 + "\n")
0059 
0060 def get_hepmc_path(beam, minq2):
0061     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"
0062 
0063 def get_base_name(beam, minq2, event_num):
0064     return f"py8dis-nc_{beam}_minq2-{minq2}_minp-250mev_nevt-{event_num}"
0065 
0066 def run_simulation(beam, minq2, event_num, detector_path, steering_file):
0067     """
0068     Runs the simulation for a given beam, Q2 value, and event number, then converts the output file.
0069 
0070     Parameters:
0071         beam (str): The energy configuration for the beam.
0072         minq2 (int): The minimum Q2 value.
0073         event_num (int): The number of events to simulate.
0074         detector_path (str): Path to the detector configuration XML file.
0075     """
0076     # Construct the input file URL
0077     url = get_hepmc_path(beam, minq2)
0078 
0079     # Construct the output file name
0080     output_base = get_base_name(beam, minq2, event_num)
0081     output_edm4hep = output_base + ".edm4hep.root"
0082     output_evttxt = output_base + ".evt.txt"
0083     event_prefix = f"CC_{beam}_minq2_{minq2}"
0084 
0085 
0086     # Command for npsim
0087     npsim_command = [
0088         "npsim",
0089         "--compactFile", detector_path,
0090         "-N", str(event_num),
0091         "--inputFiles", url,
0092         "--random.seed", "1",
0093         "--outputFile", output_edm4hep,
0094         # "-v", "WARNING",
0095         "--steeringFile", steering_file
0096         # #"npsim",
0097         # "python3", "npsim_stepping.py"
0098         # "--compactFile", detector_path,
0099         # "-N", str(event_num),
0100         # "--inputFiles", url,
0101         # "--random.seed", "1",
0102         # "--outputFile", output_file,
0103         # "--steeringFile=steering.py"
0104     ]
0105 
0106     # Run the simulation
0107     run_command(npsim_command)
0108 
0109     # Command for converting the output file to JSON format
0110     reconstruction_command = [
0111     "eicrecon",
0112       f"-Pjana:debug_plugin_loading=1",
0113       f"-Pjana:nevents={event_num}",
0114       f"-Pjana:timeout=0",
0115       f"-Ppodio:output_file={output_base}.edm4eic.root",
0116       f"-Pdd4hep:xml_files={detector_path}",
0117       f"{output_base}.edm4hep.root"
0118     ]
0119 
0120     # Run the conversion
0121     run_command(reconstruction_command)
0122 
0123 
0124 
0125 setup_environment()
0126 
0127 # Set the detector path (assuming it's predefined as an environment variable or explicitly defined here)
0128 DETECTOR_PATH = os.getenv('DETECTOR_PATH', '/opt/detector/epic-main/share/epic/')
0129 
0130 # Matrix definitions for beams and Q2 values
0131 beams = ['5x41', '10x100', '18x275']
0132 minq2s = [1, 100, 1000]
0133 
0134 # Iterate over each combination of beam and minq2
0135 for beam in beams:
0136     for minq2 in minq2s:
0137         print("\n---------------------------------------------------------------------------------"*5)
0138         run_simulation(beam, minq2, 5, f'{DETECTOR_PATH}/epic_craterlake_{beam}.xml', '/mnt/dd4hep-plugin/firebird_steering.py')
0139         print("\n/////////////////////////////////////////////////////////////////////////////////"*5)