Skip to content
Snippets Groups Projects
Commit d515184f authored by Chao Peng's avatar Chao Peng
Browse files

change the make_dawn_view script to flexibly run the simulation

parent aa05845f
No related branches found
No related tags found
1 merge request!110change the make_dawn_view script to flexibly run the simulation
...@@ -82,13 +82,14 @@ compile: ...@@ -82,13 +82,14 @@ compile:
stage: docs stage: docs
before_script: before_script:
- source .local/bin/env.sh - source .local/bin/env.sh
- sed -i 's?<support inside?<\!--support inside?' compact/ecal_barrel_hybrid.xml # - sed -i 's?<support inside?<\!--support inside?' compact/ecal_barrel_hybrid.xml
- sed -i 's?</support>?</support-->?' compact/ecal_barrel_hybrid.xml # - sed -i 's?</support>?</support-->?' compact/ecal_barrel_hybrid.xml
- sed -i 's?<fiber material?<\!--fiber material?' compact/ecal_barrel_hybrid.xml # - sed -i 's?<fiber material?<\!--fiber material?' compact/ecal_barrel_interlayers.xml
- sed -i 's?</fiber>?</fiber-->?' compact/ecal_barrel_hybrid.xml # - sed -i 's?</fiber>?</fiber-->?' compact/ecal_barrel_interlayers.xml
- echo $DETECTOR_PATH # - echo $DETECTOR_PATH
- cp compact/ecal_barrel_hybrid.xml ${DETECTOR_PATH}/compact/ecal_barrel_hybrid.xml # - cp compact/ecal_barrel_interlayers.xml ${DETECTOR_PATH}/compact/ecal_barrel_interlayers.xml
- env - env
- pip3 install psutil
needs: needs:
- ["common:detector"] - ["common:detector"]
......
#!/bin/bash #!/usr/local/bin/python
#git clone --depth 1 https://eicweb.phy.anl.gov/EIC/datasets.git # get dawn view of detectors
export DAWN_PS_PREVIEWER="ls " # W. Armstrong (ANL), original bash script
export G4DAWNFILE_VIEWER="ls -lrth " # C. Peng (ANL), translate to python and add flexible run time for simulation
export G4DAWNFILE_PS_VIEWER="ls "
import os
function print_the_help { import signal
echo "USAGE: ${0} [-s <skip_events>] -d <dawn_run_dir> " import subprocess
echo " OPTIONS: " import argparse
echo " -D,--detector-only Only generate the prim files for the detector geometry." import atexit
echo " -s,--skip <skip_events> Required run number" import time
echo " -i,--input <input_file> Input hepmc file" from datetime import datetime
echo " -d,--dawn-dir <dawn_run_dir> Directory with the .DAWN files and a generate script " import fcntl
echo " -t,--tag <file_tag> Output file tag" import psutil
exit
}
def readline_nonblocking(output):
function yes_or_no { fd = output.fileno()
while true; do fl = fcntl.fcntl(fd, fcntl.F_GETFL)
read -p "$* [y/n]: " yn fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
case $yn in try:
[Yy]*) return 0 ;; return output.readline()
[Nn]*) echo "No entered" ; return 1 ;; except:
esac return ''
done
}
# arguments
if [[ $# -eq 0 ]] ; then parser = argparse.ArgumentParser()
print_the_help
exit parser.add_argument('-c', '--compact-file', type=str, dest='compact',
fi default=os.path.join(os.environ.get('DETECTOR_PATH', '.'), 'athena.xml'),
help='Top level compact file for detectors')
SKIP_EVENTS=0
DETECTOR_ONLY=0 parser.add_argument('-s', '--skip', type=int,
FILE_TAG="view" default=0,
DAWN_RUN_DIR="scripts/view1" help='Number of events number to skip in the input')
DETECTOR_FILE="${DETECTOR_PATH}/athena.xml"
parser.add_argument('-i', '--input', type=str,
POSITIONAL=() default='scripts/input_data/few_events.hepmc',
while [[ $# -gt 0 ]] help='Input hepmc file')
do
key="$1" parser.add_argument('-o', '--output-dir', type=str, dest='out_dir',
default='images',
case $key in help='output directory')
-h|--help)
shift # past argument parser.add_argument('-D', '--detector-only', action='store_true', dest='detector_only',
print_the_help help='only generate the prim files for the detector geometry')
;;
-c|--compact-file) parser.add_argument('-d', '--dawn-dir', type=str, dest='dawn_dir',
DETECTOR_FILE="$2" default='scripts/view1',
shift # past argument help='Directory to dawn script dir (with .DAWN files and a generate_eps script)')
shift # past value
;; parser.add_argument('-t', '--tag', type=str,
-s|--skip) default='view',
SKIP_EVENTS="$2" help='Output file tag')
shift # past argument
shift # past value parser.add_argument('--timeout', type=int,
;; default=60,
-i|--input) help='Timeout in seconds')
INPUT_FILE="$2"
shift # past argument args = parser.parse_args()
shift # past value
;; macro = 'macro/dawn_picture.mac' if args.detector_only else 'macro/dawn_picture2.mac'
-o|--ouput-dir)
OUTPUT_DIR="$2" # raise error if cannot create a temporary working dir
shift # past argument # os.makedirs('dawn_view_tmp', exist_ok=False)
shift # past value os.makedirs(args.out_dir, exist_ok=True)
;;
-D|--detector-only) # use absolute path so the chdir does not affect them
DETECTOR_ONLY=1 args.input = os.path.abspath(args.input)
shift # past argument args.out_dir = os.path.abspath(args.out_dir)
#shift # past value args.compact = os.path.abspath(args.compact)
;; macro = os.path.abspath(macro)
-d|--dawn-dir)
DAWN_RUN_DIR=$2 prim_file = 'g4_0000.prim'
shift # past argument dawn_env = os.environ.copy()
shift # past value dawn_env['DAWN_BATCH'] = 'a'
;; # sdir = os.path.dirname(os.path.realpath(__file__))
-t|--tag)
FILE_TAG=$2 # Using a python warpper such as npsim introduces some problem in managing the subprocess.
shift # past argument # The process1 managed by subprocess will generate another process with proc2_pid = proc1_pid + 1, which will not
shift # past value # be terminated by terminating or killing the process1.
;; # In addition, running Geant4 with vis mode will never exit automatically (it waits for input).
*) # unknown option # Thus the created process 2 will occupy the system resources.
#POSITIONAL+=("$1") # save it in an array for later sim_cmd = ['npsim', '--runType', 'vis',
echo "unknown option $1" '--compact', args.compact,
print_the_help '--inputFiles', args.input,
shift # past argument '--outputFile', 'derp.root',
;; '--numberOfEvents', '1',
esac '--skipNEvents', str(args.skip),
done '--macroFile', macro]
set -- "${POSITIONAL[@]}" # restore positional parameters
start = datetime.now()
elapse = datetime.now() - start
rm -f *.prim last_update = datetime.now()
finished = False
if [ "${DETECTOR_ONLY}" -eq "1" ] ; then
# run simulation
# timeout --preserve-status --signal=SIGTERM 120s \ print(' '.join(sim_cmd))
./scripts/run_detector_simulation.py \ p = subprocess.Popen(args=sim_cmd, env=dawn_env,
--compact ${DETECTOR_PATH}/athena.xml \ stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
-i scripts/input_data/few_events.hepmc \ __child_pid = p.pid
-o derp.root -n 1 \ while elapse.seconds < args.timeout:
--ui csh --vis -b -m macro/dawn_picture.mac & line = readline_nonblocking(p.stdout)
elapse = datetime.now() - start
timeout=200 time_left = args.timeout - elapse.seconds
while [ $timeout -ge 0 ] && [ ! -f "g4_0000.prim" ] time_str = '[{:02d}:{:02d}]'.format(elapse.seconds // 60, elapse.seconds % 60)
do
echo "terminating in $timeout secs ..." if time_left < 10:
sleep 5 print('{} === TIMEOUT ===: Terminating in {:d} seconds'.format(time_str, time_left))
((timeout=timeout-5))
done if line:
kill %1 decoded_line = line.decode('utf-8').strip()
print('{} {}'.format(time_str, decoded_line))
else # what we are looking for
if decoded_line == 'File {} is generated.'.format(prim_file):
echo " Running simulation for tracks" print('{} === FINISHED ===: Got the prim file, terminating.'.format(time_str))
# timeout --preserve-status --signal=SIGTERM 120s \ finished = True
./scripts/run_detector_simulation.py \ break
--compact ${DETECTOR_PATH}/athena.xml \ if decoded_line == 'Idle>':
-i scripts/input_data/few_events.hepmc \ p.stdin.write(b'exit')
-o derp.root -s ${SKIP_EVENTS} -n 1 \ break
--ui csh --vis -b -m macro/dawn_picture2.mac & # do not sleep
continue
sleep 40
kill %1 # ended early before file
if p.poll() is not None:
fi print(p.poll())
break
#./scripts/run_detector_simulation.py -i scripts/input_data/few_events.hepmc \
# -o derp.root -s 2 -n 1 \ time.sleep(1)
# --ui csh --vis -b -m macro/dawn_picture2.mac &
# p.kill()
#sleep 20 # use to kill the subprocess generated from the python wrapper
#kill %1 # this is unsafe so maybe more checks required
for proc in psutil.process_iter():
#./scripts/run_detector_simulation.py -i scripts/input_data/few_events.hepmc \ pinfo = proc.as_dict(attrs=['pid', 'name', 'create_time'])
# -o derp.root -s 5 -n 1 \ if pinfo['pid'] == p.pid + 1 and pinfo['name'] == 'python':
# --ui csh --vis -b -m macro/dawn_picture2.mac & print('kill {}, generated from {}'.format(pinfo, p.pid))
# os.kill(pinfo['pid'], signal.SIGTERM)
#sleep 20
#kill %1 line = b'stderr outputs:\n'
# while line:
#./scripts/run_detector_simulation.py -i scripts/input_data/few_events.hepmc \ print(line.decode('utf-8'), end='')
# -o derp.root -s 6 -n 1 \ line = readline_nonblocking(p.stderr)
# --ui csh --vis -b -m macro/dawn_picture2.mac &
# if finished:
#sleep 20 print('Simulation finished')
#kill %1 else:
print('Simulation failed')
[[ -f "g4_0000.prim" ]] || exit -1 exit(1)
echo "simulating done"
#npsim --runType vis \ # generate DAWN images
# -v WARNING \ prim_file = os.path.abspath(prim_file)
# --macroFile macro/dawn_picture.mac \ owd = os.getcwd()
# --numberOfEvents 1 \ os.chdir(args.dawn_dir)
# --compactFile topside.xml \ subprocess.run(['./generate_eps', '-t', args.tag, '-i', prim_file])
# --inputFiles scripts/input_data/few_events.hepmc \ subprocess.run(['ls', '-lrth'])
# --outputFile derp.root \
# --dumpSteeringFile >> derp.out # upload the results
os.system('cp *.pdf {}'.format(args.out_dir))
os.system('cp *.png {}'.format(args.out_dir))
ls -lrth os.chdir(owd)
mkdir -p images
#mkdir -p images/prim
pushd ${DAWN_RUN_DIR}
if [ "${DETECTOR_ONLY}" -eq "1" ] ; then
./generate_eps -t ${FILE_TAG} -i ../../g4_0000.prim
else
./generate_eps -t ${FILE_TAG} -i ../../g4_0000.prim
fi
ls -lrth
cp *.pdf ../../images/.
cp *.png ../../images/.
#cp *.prim ../../images/prim/.
popd
...@@ -127,7 +127,8 @@ ...@@ -127,7 +127,8 @@
sensitive="yes" sensitive="yes"
radius="EcalBarrel_FiberRadius" radius="EcalBarrel_FiberRadius"
spacing_x="EcalBarrel_FiberXSpacing" spacing_x="EcalBarrel_FiberXSpacing"
spacing_z="EcalBarrel_FiberZSpacing"/> spacing_z="EcalBarrel_FiberZSpacing">
</fiber>
</slice> </slice>
<slice material="CarbonFiber" thickness="EcalBarrel_CarbonThickness" vis="AnlGold"/> <slice material="CarbonFiber" thickness="EcalBarrel_CarbonThickness" vis="AnlGold"/>
<slice material="Air" thickness="EcalBarrel_LayerSpacing " vis="AnlGold"/> <slice material="Air" thickness="EcalBarrel_LayerSpacing " vis="AnlGold"/>
...@@ -141,7 +142,8 @@ ...@@ -141,7 +142,8 @@
sensitive="yes" sensitive="yes"
radius="EcalBarrel_FiberRadius" radius="EcalBarrel_FiberRadius"
spacing_x="EcalBarrel_FiberXSpacing" spacing_x="EcalBarrel_FiberXSpacing"
spacing_z="EcalBarrel_FiberZSpacing"/> spacing_z="EcalBarrel_FiberZSpacing">
</fiber>
</slice> </slice>
</layer> </layer>
</detector> </detector>
......
...@@ -60,7 +60,7 @@ pdftoppm ${FILE_TAG}.pdf ${FILE_TAG} -png -singlefile -cropbox ...@@ -60,7 +60,7 @@ pdftoppm ${FILE_TAG}.pdf ${FILE_TAG} -png -singlefile -cropbox
# Top view # Top view
dawncut 0 1 0 1 ${INPUT_FILE} ${FILE_TAG}_temp0.prim dawncut 0 1 0 1 ${INPUT_FILE} ${FILE_TAG}_temp0.prim
dawncut 0 -1 0 1 ${FILE_TAG}_temp0.prim ${FILE_TAG}.prim dawncut 0 -1 0 1 ${FILE_TAG}_temp0.prim ${FILE_TAG}.prim
../../bin/dawn_tweak --theta 270 --mag 20 ../../bin/dawn_tweak --theta 270 --mag 10
dawn -d ${FILE_TAG}.prim dawn -d ${FILE_TAG}.prim
ps2pdf ${FILE_TAG}.eps ${FILE_TAG}_top_full.pdf ps2pdf ${FILE_TAG}.eps ${FILE_TAG}_top_full.pdf
gs -o ${FILE_TAG}_top.pdf -sDEVICE=pdfwrite \ gs -o ${FILE_TAG}_top.pdf -sDEVICE=pdfwrite \
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment