Commit a91c0886 authored by Breno Rilho Lemos's avatar Breno Rilho Lemos 💬
Browse files

Create self-sufficient script with progress log for PCAD running (UFRGS)

parent f248344d
......@@ -3,8 +3,8 @@
#
# For console only rendering (example):
# $ blender -noaudio --background -P animate_particles.py -- -radius=0.05 -duration=1 -cameras="BarrelCamera OverviewCamera" \
# -datafile="esd-detail.dat" -n_event=0 -simulated_t=0.02 -fps=24 -resolution=100 -transparency=1.2 \
# -stamp_note="Texto no canto" -its=1 -tpc=0 -trd=1 -detailed_tpc=1 -emcal=0 -blendersave=0 -picpct=5 -tpc_blender_path="/home/files/blender"
# -datafile="esd-detail.dat" -n_event=0 -simulated_t=0.02 -fps=24 -resolution=100 -transparency=1.2 -stamp_note="Texto no canto"\
# -its=1 -tpc=0 -trd=1 -detailed_tpc=1 -emcal=0 -blendersave=0 -picpct=5 -tpc_blender_path="/home/files/blender" -output_path="/tmp/blender"
#
import os
......@@ -46,6 +46,7 @@ parser.add_argument('-blendersave','--blendersave')
parser.add_argument('-picpct','--picpct')
parser.add_argument('-tpc_blender_path','--tpc_blender_path')
parser.add_argument('-detailed_tpc','--detailed_tpc')
parser.add_argument('-output_path','--output_path')
args = parser.parse_args()
bpy.context.user_preferences.view.show_splash = False
......@@ -85,11 +86,11 @@ for i in range(j,len(cams_string)):
cam += cams_string[i]
cams_list.append(cam)
#configure output
outputPath = "/tmp/alice_blender/"
# Configure Output
outputPath = str(args.output_path)+"/"
fileIdentifier = "PhysicalTrajectories_"
renderAnimation = True # True
renderAnimation = False # True
saveBlenderFile = blendersave # False
"""
......
# -*- coding: utf-8 -*-
# animate_particles.py - Animate HEP events
#
# For console only rendering (example):
# $ blender -noaudio --background -P animate_particles.py -- -cam "OverviewCamera" -datafile "Run139038_Orbit7548473_BunchCross1534_2.dat" \
# -n_event 2 -pic_pct 30 -output_path "/home"
#
import bpy
import argparse
import sys
# Pass on command line arguments to script:
class ArgumentParserForBlender(argparse.ArgumentParser):
def _get_argv_after_doubledash(self):
try:
idx = sys.argv.index("--")
return sys.argv[idx+1:] # the list after '--'
except ValueError as e: # '--' not in the list:
return []
# overrides superclass
def parse_args(self):
return super().parse_args(args=self._get_argv_after_doubledash())
parser = ArgumentParserForBlender()
parser.add_argument('-cam','--render_cam')
parser.add_argument('-datafile','--datafile')
parser.add_argument('-n_event','--n_event')
parser.add_argument('-pic_pct','--pic_pct')
parser.add_argument('-output_path','--output_path')
args = parser.parse_args()
render_cam = str(args.render_cam)
datafile = str(args.datafile)
n_event = str(args.n_event)
pic_pct = int(args.pic_pct)
outputPath = str(args.output_path)+"/"
fileIdentifier = "PhysicalTrajectories_"
bpy.ops.wm.open_mainfile(filepath=outputPath+fileIdentifier+"AlirootFileGenerator_"+datafile+"_Event_"+n_event+".blend")
bcs = bpy.context.scene
# Set specific output info
output_prefix = "PhysicalTrajectories_1920px_AlirootFileGenerator_"+datafile+"_Event_"+n_event+"_"+render_cam
bcs.render.filepath = outputPath+output_prefix
bcs.camera = bpy.data.objects[render_cam]
# Take picture of animation
bcs.frame_current = int(bcs.frame_end * pic_pct/100)
bpy.ops.render.render()
bpy.data.images['Render Result'].save_render(filepath=bcs.render.filepath+".png")
# Render actual animation
bpy.ops.render.render(animation=True)
......@@ -16,6 +16,9 @@ export ALIENV_ID=AliPhysics/latest-aliroot5-user
# Put Blender 2.79b in the PATH env var
export PATH="/home/schnorr/install/blender-2.79-linux-glibc219-x86_64/:$PATH"
# Progress log file
export PROGRESS_LOG=$(pwd)/progress.log
##############################
# Command-line options #
##############################
......@@ -74,7 +77,7 @@ TPC=1
DETAILED_TPC=0
TRD=1
EMCAL=1
BLENDERSAVE=0
BLENDERSAVE=1
PICPCT=80
# now enjoy the options in order and nicely split until we see --
while true; do
......@@ -312,6 +315,11 @@ else
fi
# Get number of frames
FPS_DUR="$FPS $DURATION"
FPS_DUR=$(echo $FPS_DUR | awk '{print $1*$2}')
# handle non-option arguments
if [[ $# -ne 0 ]]; then
echo "$0: non-option arguments ($#, $*) are ignored."
......@@ -319,6 +327,7 @@ if [[ $# -ne 0 ]]; then
exit
fi
##############################
# Download Dataset #
##############################
......@@ -328,8 +337,12 @@ if [ "$DOWNLOAD" = "true" ]; then
usage
exit
fi
if ! grep -q "DOWNLOAD NOT DONE" $PROGRESS_LOG; then
echo "Downloading data."
wget $URL
echo "ESD DOWNLOAD DONE" >> $PROGRESS_LOG
fi
######################################
# Established Unique ID based on URL #
......@@ -351,20 +364,21 @@ if [ "$SAMPLE" = "true" ]; then
##############################
# Phase 1: Blender animate #
##############################
BLENDER_OUTPUT=$(pwd)/sample
mkdir --verbose -p ${BLENDER_OUTPUT}
pushd ${BLENDER_SCRIPT_DIR}
echo "Preparing sample animation in Blender"
blender -noaudio --background -P animate_particles.py -- -radius=0.05 \
-duration=${DURATION} -cameras="${CAMERAS}" -datafile="d-esd-detail.dat" -simulated_t=0.03\
-fps=${FPS} -resolution=${RESOLUTION} -transparency=${TRANSPARENCY} -stamp_note=\
"opendata.cern.ch_record_1102_alice_2010_LHC10h_000139038_ESD_0001_2" -its=${ITS} \
-fps=${FPS} -resolution=${RESOLUTION} -transparency=${TRANSPARENCY} \
-stamp_note="opendata.cern.ch_record_1102_alice_2010_LHC10h_000139038_ESD_0001_2" -its=${ITS}\
-tpc=${TPC} -trd=${TRD} -emcal=${EMCAL} -detailed_tpc=${DETAILED_TPC} \
-blendersave=${BLENDERSAVE} -picpct=${PICPCT} -tpc_blender_path=${BLENDER_SCRIPT_DIR}
-blendersave=${BLENDERSAVE} -picpct=${PICPCT} -tpc_blender_path=${BLENDER_SCRIPT_DIR} \
-output_path="${BLENDER_OUTPUT}"
popd
BLENDER_OUTPUT=.
mkdir --verbose -p ${BLENDER_OUTPUT}
mv --verbose /tmp/alice_blender ${BLENDER_OUTPUT}
echo "Done."
##############################
......@@ -372,6 +386,8 @@ if [ "$SAMPLE" = "true" ]; then
##############################
elif [ "$SAMPLE" = "false" ]; then
if ! grep -q "DATA ANALYSIS for" $PROGRESS_LOG; then
# Verify if AliESDs.root is here
ALIESD_ROOT_FILE=$(pwd)/AliESDs.root
if ! [[ -f "$ALIESD_ROOT_FILE" ]]
......@@ -396,73 +412,133 @@ elif [ "$SAMPLE" = "false" ]; then
UNIQUEID=$(more uniqueid.txt)
echo "The unique ID is $UNIQUEID."
rm uniqueid.txt
fi
# Create directory where animations will be saved
popd
echo "DATA ANALYSIS for ${UNIQUEID} FINISHED" >> $PROGRESS_LOG
else
if [ "$DOWNLOAD" = "false" ]; then
pushd ${ALIROOT_SCRIPT_DIR}
UNIQUEID=$(more uniqueid.txt)
echo "The unique ID is $UNIQUEID."
popd
fi
fi
if ! grep -q "ANIMATION DIRECTORY" $PROGRESS_LOG ; then
# Create directory where animations will be saved
BLENDER_OUTPUT=$(pwd)/$UNIQUEID
mkdir --verbose -p ${BLENDER_OUTPUT}
echo "ANIMATION DIRECTORY ${UNIQUEID} CREATED" >> $PROGRESS_LOG
else
BLENDER_OUTPUT=$(pwd)/$UNIQUEID
fi
pushd ${ALIROOT_SCRIPT_DIR} # push back to aliroot directory
if ! grep -q "MOVED to animation directory" $PROGRESS_LOG; then
#################################################
# Phase 1: iteration for every event identifier #
#################################################
# Get all extracted files
EXTRACTED_FILES=$(ls -1 esd_detail-event_*.dat | sort --version-sort)
for FILE_WITH_DATA in $EXTRACTED_FILES; do
if ! [[ -s $FILE_WITH_DATA ]]; then
echo "File $FILE_WITH_DATA has zero size. Ignore and continue."
rm $FILE_WITH_DATA
continue
fi
mv ${ALIROOT_SCRIPT_DIR}/$FILE_WITH_DATA \
${BLENDER_SCRIPT_DIR}
done
echo "DATA ANALYSIS FILES of ${UNIQUEID} MOVED to animation directory" >> $PROGRESS_LOG
fi
popd
pushd ${BLENDER_SCRIPT_DIR}
if ! grep -q "CREATED EVENT COUNTER FILE" $PROGRESS_LOG; then
# Event counter for animating no more events than the informed amount
EVENT_COUNTER=0
echo "$EVENT_COUNTER" > event_counter.txt
echo "CREATED EVENT COUNTER FILE" >> $PROGRESS_LOG
fi
if ! grep -q "DATA FILES RENAMED according to UNIQUEID" $PROGRESS_LOG; then
# Get all extracted files
EXTRACTED_FILES=$(ls -1 esd_detail-event_*.dat | sort --version-sort)
for FILE_WITH_DATA in $EXTRACTED_FILES; do
EVENT_ID=$(echo $FILE_WITH_DATA | \
sed -e "s#esd_detail-event_##" \
-e "s#\.dat##")
EVENT_UNIQUE_ID=${UNIQUEID}_${EVENT_ID}
if ! [[ -s $FILE_WITH_DATA ]]; then
echo "File $FILE_WITH_DATA has zero size. Ignore and continue."
rm $FILE_WITH_DATA
continue
LOCAL_FILE_WITH_DATA=${EVENT_UNIQUE_ID}.dat
mv $FILE_WITH_DATA $LOCAL_FILE_WITH_DATA
done
echo "DATA FILES RENAMED according to UNIQUEID ${UNIQUEID}" >> $PROGRESS_LOG
fi
EXTRACTED_FILES=$(ls -1 ${UNIQUEID}_*.dat | sort --version-sort)
for LOCAL_FILE_WITH_DATA in $EXTRACTED_FILES; do
pushd ${BLENDER_SCRIPT_DIR}
EVENT_ID=$(echo $LOCAL_FILE_WITH_DATA | \
sed -e "s#${UNIQUEID}_##" \
-e "s#\.dat##")
EVENT_UNIQUE_ID=${UNIQUEID}_${EVENT_ID}
##############################
# Phase 2: blender animate #
##############################
LOCAL_FILE_WITH_DATA=${EVENT_UNIQUE_ID}.dat
cp ${ALIROOT_SCRIPT_DIR}/$FILE_WITH_DATA \
${BLENDER_SCRIPT_DIR}/${LOCAL_FILE_WITH_DATA}
rm $FILE_WITH_DATA
NUMBER_OF_PARTICLES=$(wc -l ${BLENDER_SCRIPT_DIR}/$LOCAL_FILE_WITH_DATA | \
NUMBER_OF_PARTICLES=$(wc -l $LOCAL_FILE_WITH_DATA | \
awk '{ print $1 }')
AVERAGE_PZ=$(awk 'BEGIN {pzsum=0;n=0} {pzsum+=$8;n++} END {print sqrt(pzsum*pzsum/n/n)}'\
${BLENDER_SCRIPT_DIR}/${LOCAL_FILE_WITH_DATA})
${LOCAL_FILE_WITH_DATA})
AVERAGE_PT=$(awk 'BEGIN {ptsum=0;n=0} {ptsum+=$9;n++} END {print ptsum/n}' \
${BLENDER_SCRIPT_DIR}/${LOCAL_FILE_WITH_DATA})
${LOCAL_FILE_WITH_DATA})
echo "File $LOCAL_FILE_WITH_DATA has $NUMBER_OF_PARTICLES particles."
echo "Average Z momentum: $AVERAGE_PZ"
echo "Average transversal momentum $AVERAGE_PT"
EVENT_COUNTER=$(more event_counter.txt)
if (( $(echo "$AVERAGE_PT >= $MIN_AVG_PT" |bc -l) )); then
if (( $(echo "$AVERAGE_PZ >= $MIN_AVG_PZ" |bc -l) )); then
if [[ $NUMBER_OF_PARTICLES -le $MAX_PARTICLES && $NUMBER_OF_PARTICLES \
-ge $MIN_PARTICLES && $EVENT_COUNTER -lt $N_OF_EVENTS ]]; then
# Increment event counter
EVENT_COUNTER=$EVENT_COUNTER+1
if ! grep -q "Scene from ${EVENT_UNIQUE_ID} READY." $PROGRESS_LOG; then
echo "Processing ${EVENT_UNIQUE_ID} ($NUMBER_OF_PARTICLES tracks) in Blender"
pushd ${BLENDER_SCRIPT_DIR}
echo "Processing ${EVENT_UNIQUE_ID} in Blender"
blender -noaudio --background -P animate_particles.py -- -radius=0.05 \
......@@ -470,41 +546,59 @@ elif [ "$SAMPLE" = "false" ]; then
-n_event=${EVENT_ID} -simulated_t=0.03 -fps=${FPS} -resolution=${RESOLUTION}\
-transparency=${TRANSPARENCY} -stamp_note="${EVENT_UNIQUE_ID}" -its=${ITS}\
-tpc=${TPC} -trd=${TRD} -emcal=${EMCAL} -detailed_tpc=${DETAILED_TPC} \
-blendersave=${BLENDERSAVE} -picpct=${PICPCT} -tpc_blender_path=${BLENDER_SCRIPT_DIR}
echo "Event ${EVENT_UNIQUE_ID} done."
-blendersave=${BLENDERSAVE} -picpct=${PICPCT} -tpc_blender_path=${BLENDER_SCRIPT_DIR}\
-output_path="${BLENDER_OUTPUT}"
echo "Scene from ${EVENT_UNIQUE_ID} READY." >> $PROGRESS_LOG
fi
if [ "$MOSAIC" = "true" ]; then
popd
pushd /tmp/alice_blender
for type in $CAMERAS; do
# Move animation images to final location
mv /tmp/alice_blender/*.png ${BLENDER_OUTPUT}
if ! grep -q "${type} for $EVENT_UNIQUE_ID FINISHED" $PROGRESS_LOG; then
blender -noaudio --background -P render.py -- -cam ${type} -datafile\
"${LOCAL_FILE_WITH_DATA}" -n_event ${EVENT_ID} -pic_pct ${PICPCT} -output_path "${BLENDER_OUTPUT}"
echo "${type} for $EVENT_UNIQUE_ID FINISHED" >> $PROGRESS_LOG
fi
done
if [ "$MOSAIC" = "true" ]; then
if ! grep -q "MOSAIC for $EVENT_UNIQUE_ID FINISHED" $PROGRESS_LOG; then
pushd ${BLENDER_OUTPUT}
# Setting input names for clips in order to make mosaic clip
INPUT_ONE=$(ls | awk 'NR==1')
INPUT_TWO=$(ls | awk 'NR==2')
INPUT_THREE=$(ls | awk 'NR==3')
INPUT_FOUR=$(ls | awk 'NR==4')
INPUT_ONE=$(ls *$EVENT_UNIQUE_ID*${FPS_DUR}.mp4 | awk 'NR==1')
INPUT_TWO=$(ls *$EVENT_UNIQUE_ID*${FPS_DUR}.mp4 | awk 'NR==2')
INPUT_THREE=$(ls *$EVENT_UNIQUE_ID*${FPS_DUR}.mp4 | awk 'NR==3')
INPUT_FOUR=$(ls *$EVENT_UNIQUE_ID*${FPS_DUR}.mp4 | awk 'NR==4')
ffmpeg -i ${INPUT_FOUR} -i ${INPUT_TWO} -i ${INPUT_THREE} -i ${INPUT_ONE} -filter_complex\
"[0:v][1:v]hstack=inputs=2[top];[2:v][3:v]hstack=inputs=2[bottom];[top][bottom]vstack=inputs=2[v]"\
-map "[v]" ${EVENT_UNIQUE_ID}_Mosaic.mp4
popd
pushd ${BLENDER_SCRIPT_DIR}
echo "MOSAIC for $EVENT_UNIQUE_ID FINISHED" >> $PROGRESS_LOG
popd
fi
# Move generated clips to final location
mv /tmp/alice_blender/* ${BLENDER_OUTPUT}
fi
if ! grep -q "TEXT DATA of $EVENT_UNIQUE_ID MOVED to final location" $PROGRESS_LOG; then
# Move processed file to final location
mv $LOCAL_FILE_WITH_DATA ${BLENDER_OUTPUT}/$LOCAL_FILE_WITH_DATA
echo "TEXT DATA of $EVENT_UNIQUE_ID MOVED to final location" >> $PROGRESS_LOG
fi
popd
echo "EVENT ${EVENT_UNIQUE_ID} DONE with FILE $LOCAL_FILE_WITH_DATA."
# Increment event counter
EVENT_COUNTER=$EVENT_COUNTER+1
rm event_counter.txt
echo "$EVENT_COUNTER" > event_counter.txt
else
if [[ $NUMBER_OF_PARTICLES -lt $MIN_PARTICLES ]]; then
......@@ -516,29 +610,25 @@ elif [ "$SAMPLE" = "false" ]; then
fi
# Remove non-processed files
pushd ${BLENDER_SCRIPT_DIR}
rm $LOCAL_FILE_WITH_DATA
popd
continue
fi
else
echo "Average Z Momentum too low (minimum accepted is $MIN_AVG_PZ). Continue."
# Remove non-processed files
pushd ${BLENDER_SCRIPT_DIR}
rm $LOCAL_FILE_WITH_DATA
popd
fi
else
echo "Average Transversal Momentum too low (minimum accepted is $MIN_AVG_PT). Continue."
# Remove non-processed files
pushd ${BLENDER_SCRIPT_DIR}
rm $LOCAL_FILE_WITH_DATA
popd
fi
done
popd
done
fi
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment