workflow_sketch.sh 10.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash

##############################
# Configurations             #
##############################
# Put aliBuild in the PATH env var
export PATH="/mnt/SSD/schnorr/python/bin:$PATH"
# Directory where runAnalysis.C is placed
export ALIROOT_SCRIPT_DIR=$(pwd)/aliRoot/
# Directory where blender scripts are
export BLENDER_SCRIPT_DIR=$(pwd)/animate/
# alienv working directory
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
13
14
export ALIENV_WORK_DIR=/home/breno/alice/sw
export ALIENV_OS_SPEC=ubuntu1804_x86-64
15
16
17
18
19
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"

##############################
20
# Command-line options       #
21
##############################
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# See the following link to understand the code below
# https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash

# saner programming env: these switches turn some bugs into errors
set -o errexit -o pipefail -o noclobber -o nounset

# -allow a command to fail with !’s side effect on errexit
# -use return value from ${PIPESTATUS[0]}, because ! hosed $?
! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
    echo 'I’m sorry, `getopt --test` failed in this environment.'
    exit 1
fi

36
37
OPTIONS=c:hdau:m:t:r:b:ipqe
LONGOPTS=camera:,resolution:,transperency:,duration:,maxparticles:,help,download,default,url:,its,tpc,trd,emcal
38
39
40
41
42
43
44
45
46
47
48
49
50
51

# -regarding ! and PIPESTATUS see above
# -temporarily store output to be able to check for errors
# -activate quoting/enhanced mode (e.g. by writing out “--options”)
# -pass arguments only via   -- "$@"   to separate them correctly
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
    # e.g. return value is 1
    #  then getopt has complained about wrong arguments to stdout
    exit 2
fi
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"

52
53
54
##############################
# Parse Parameters           #
##############################
55
CAMERA=Overview
56
57
DURATION=10
RESOLUTION=100
58
TRANSPERENCY=1
59
MAX_PARTICLES=0
60
61
62
HELP=false
DOWNLOAD=false
DEFAULT=false
63
URL=
64
65
66
67
ITS=1 # 1 means "build this detector", while 0 means "don't"
TPC=1
TRD=1
EMCAL=1
68
69
70
# now enjoy the options in order and nicely split until we see --
while true; do
    case "$1" in
71
72
73
74
75
      -h|--help)
          HELP=true
          shift
          break
          ;;
76
      -d|--download)
77
78
79
            DOWNLOAD=true
            shift
            ;;
80
      -a|--default)
81
82
83
            DEFAULT=true
            shift
            ;;
84
85
      -u|--url)
          URL="$2"
86
87
            shift 2
            ;;
88
89
90
91
      -m|--maxparticles)
          MAX_PARTICLES="$2"
          shift 2
          ;;
92
93
94
95
96
97
98
99
      -t|--duration)
          DURATION="$2"
          shift 2
          ;;
      -r|--resolution)
          RESOLUTION="$2"
          shift 2
          ;;
100
101
102
103
      -b|--transperency)
          TRANSPERENCY="$2"
          shift 2
          ;;
Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
104
      -c|--camera)
105
106
107
      	  CAMERA="$2"
      	  shift 2
      	  ;;
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
      -i|--its)
          ITS=0
          shift
          ;;
      -p|--tpc)
          TPC=0
          shift
          ;;
      -q|--trd)
          TRD=0
          shift
          ;;
      -e|--emcal)
          EMCAL=0
          shift
          ;;
124
125
126
127
128
        --)
            shift
            break
            ;;
        *)
129
            echo "Programming error $*"
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
            exit 3
            ;;
    esac
done

##############################
# Usage                      #
##############################
function usage()
{
    # Using a here doc with standard out.
    cat <<-END
Usage:
------
   -h | --help
     Display this help
   -d | --download
     Download the AliESDs.root file using the provided URL
   -u | --url URL
     Provide the URL to uniquely identify a AliESDs.root dataset.
     This should be in the format provided by http://opendata.cern.ch.
     See example below.
152
153
   -m | --maxparticles VALUE
     Get only events for which its number of particles is smaller than VALUE.
154
155
156
157
   -t | --duration VALUE
     Set the animation duration in seconds.
   -r | --resolution VALUE
     Set the animation resolution percentage.
158
159
160
   -b | --transperency VALUE
     Set detector transperency as a number greater than zero,
     where zero is full transperency and 1 is standard transperency
Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
161
162
163
164
   -c | --camera VALUE
     Which camera to use for the animation, where VALUE
     is a comma-separated list (without spaces)
     Options: Barrel,Forward,Overview (defaults to Barrel)
165
166
   -a | --default
     Creates a default animation with blender.
167
168
169
170
171
172
173
174
   -i | --its
     Removes ITS detector from animation
   -p | --tpc
     Removes TPC detector from animation
   -q | --trd
     Removes TRD detector from animation
   -e | --emcal
     Removes EMCal detector from animation
175
176
177
178
179
180
181

Example:
--------
$0 --url http://opendata.cern.ch/record/1103/files/assets/alice/2010/LHC10h/000139173/ESD/0004/AliESDs.root --download

END
}
182

Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
183
184
185
186
187
# Fix CAMERA to be accepted by the for loop
if [[ $CAMERA != "" ]]; then
    CAMERA=$(echo $CAMERA | sed -e 's#,#Camera #g' -e 's#$#Camera#')
fi

188
189
190
191
192
193
194
195
if [[ $HELP = "true" ]]; then
    usage
    exit
else
    echo "-------- Parsed parameters --------"
    echo "URL: $URL"
    echo "Download: $DOWNLOAD"
    echo "Default: $DEFAULT"
196
197
198
    echo "Transperency Parameter: $TRANSPERENCY"
    echo "Duration: $DURATION"
    echo "Resolution: $RESOLUTION"
199
    echo "Max particles: ${MAX_PARTICLES}"
Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
200
    echo "Camera: $CAMERA"
201
    echo "-----------------------------------"
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
    echo "------------ Detectors ------------"
    if [[ $ITS = 1 ]]; then
      echo "Building ITS"
    fi
    if [[ $TPC = 1 ]]; then
      echo "Building TPC"
    fi
    if [[ $TRD = 1 ]]; then
      echo "Building TRD"
    fi
    if [[ $EMCAL = 1 ]]; then
      echo "Building EMCAL"
    fi
    if [[ $TPC = 0 && $TPC = 0 && $TRD = 0 && $EMCAL = 0 ]]; then
      echo "Not building any detectors"
    fi
    echo "-----------------------------------"

220
221
fi

222
223
224
225
226
if [[ $URL = "" ]]; then
    echo "URL parameter is obligatory."
    exit
    usage
fi
227
228
229
230
231
232
233
234

# handle non-option arguments
if [[ $# -ne 0 ]]; then
    echo "$0: non-option arguments ($#, $*) are ignored."
    echo "Remove them manually as indicated between parenthesis."
    exit
fi

235
236
237
238
##############################
# Download Dataset           #
##############################
if [ "$DOWNLOAD" = "true" ]; then
239
240
241
242
243
    if [ -z $URL ]; then
        echo "Error. Must pass the dataset URL."
        usage
        exit
    fi
244
245
246
247
    echo "Downloading data."
    wget $URL
fi

248
249
250
251
######################################
# Established Unique ID based on URL #
######################################
UNIQUEID=$(echo $URL | sed \
252
253
254
255
                     -e "s#http://opendata.cern.ch/##" \
                     -e "s#/AliESDs.root##" \
                     -e "s#files/assets/##" \
                     -e "s#/#_#g")
256
257
echo "The unique ID is $UNIQUEID."

258
##############################
Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
259
# Default synthetic animation#
260
##############################
261
if [ "$DEFAULT" = "true" ]; then
262
263
264
265
266
    echo "Preparing default animation."
    ##############################
    # Phase 1: blender animate   #
    ##############################
    pushd ${BLENDER_SCRIPT_DIR}
267
    blender -noaudio --background -P animate_particles.py -- -radius=0.05 -duration=${DURATION} -camera=${CAMERA} -datafile="d-esd-detail.dat" -simulated_t=0.03 -fps=24 -resolution=${RESOLUTION} -transperency=${TRANSPERENCY} -stamp_note="opendata.cern.ch_record_1102_alice_2010_LHC10h_000139038_ESD_0001_2" -its=${ITS} -tpc=${TPC} -trd=${TRD} -emcal=${EMCAL}
268
    popd
269
    BLENDER_OUTPUT=.
270
271
272
273
    mkdir --verbose -p ${BLENDER_OUTPUT}
    mv --verbose /tmp/blender ${BLENDER_OUTPUT}
    echo "Done."

274
275
276
##############################
# Animation from file        #
##############################
277
elif [ "$DEFAULT" = "false" ]; then
278

279
  # Verify if AliESDs.root is here
280
281
282
  ALIESD_ROOT_FILE=$(pwd)/AliESDs.root
  if ! [[ -f "$ALIESD_ROOT_FILE" ]]
  then
Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
283
284
      echo "AliESDs.root not found."
      exit
285
286
  fi

287
288
289
290
  # Create directory where animations will be saved
  BLENDER_OUTPUT=$(pwd)/$UNIQUEID
  mkdir --verbose -p ${BLENDER_OUTPUT}

291
292
293
  ############################
  # Phase 1: aliroot extract #
  ############################
294
  eval $(alienv -w ${ALIENV_WORK_DIR} -a ${ALIENV_OS_SPEC} load ${ALIENV_ID})
295
  pushd ${ALIROOT_SCRIPT_DIR}
296
  # Remove existing symbolic link
297
  rm -f --verbose AliESDs.root
298
299
300
  # Create a symbolic link to the actual AliESDs.root
  ln --verbose -s ${ALIESD_ROOT_FILE} AliESDs.root
  # Run the extraction tool
301
  aliroot runAnalysis.C
302

303
304
305
  #################################################
  # Phase 1: iteration for every event identifier #
  #################################################
306

Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
307
  # Create directory where animations will be saved
308
309
  #BLENDER_OUTPUT=$(pwd)/$UNIQUEID
  #mkdir --verbose -p ${BLENDER_OUTPUT}
Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
310

311
312
313
314
  # 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 | \
315
316
                 sed -e "s#esd_detail-event_##" \
                   -e "s#\.dat##")
317
318
      EVENT_UNIQUE_ID=${UNIQUEID}_${EVENT_ID}

Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
319
      if ! [[ -s $FILE_WITH_DATA ]]; then
320
          echo "File $FILE_WITH_DATA has zero size. Ignore and continue."
321
	        rm $FILE_WITH_DATA
322
          continue
Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
323
      fi
324

325
326
327
      ##############################
      # Phase 2: blender animate   #
      ##############################
328
329
330

      LOCAL_FILE_WITH_DATA=${EVENT_UNIQUE_ID}.dat
      cp ${ALIROOT_SCRIPT_DIR}/$FILE_WITH_DATA \
331
332
       ${BLENDER_SCRIPT_DIR}/${LOCAL_FILE_WITH_DATA}

333
334
      rm $FILE_WITH_DATA

335
336
337
338
339
      NUMBER_OF_PARTICLES=$(wc -l ${BLENDER_SCRIPT_DIR}/$LOCAL_FILE_WITH_DATA | \
                        awk '{ print $1 }')
      echo "File $LOCAL_FILE_WITH_DATA has $NUMBER_OF_PARTICLES particles"
      if [[ $NUMBER_OF_PARTICLES -lt $MAX_PARTICLES ]]; then
        echo "Processing ${EVENT_UNIQUE_ID} ($NUMBER_OF_PARTICLES) in blender"
340

341
        pushd ${BLENDER_SCRIPT_DIR}
342

Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
343
        for type in $CAMERA; do
344
              echo "Processing ${EVENT_UNIQUE_ID} with $type Camera in blender"
345

346
              blender -noaudio --background -P animate_particles.py -- -radius=0.05 -duration=${DURATION} -camera=${type} -datafile="${LOCAL_FILE_WITH_DATA}" -n_event=${EVENT_ID} -simulated_t=0.03 -fps=24 -resolution=${RESOLUTION} -transperency=${TRANSPERENCY} -stamp_note="${EVENT_UNIQUE_ID}" -its=${ITS} -tpc=${TPC} -trd=${TRD} -emcal=${EMCAL}
347
348
              # Move generated file to final location
              mv /tmp/blender/* ${BLENDER_OUTPUT}
Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
349
              echo "${type} for event ${EVENT_UNIQUE_ID} done."
350
        done
351

352
        # Move processed file to final location
353
        mv $LOCAL_FILE_WITH_DATA ${BLENDER_OUTPUT}/$LOCAL_FILE_WITH_DATA
354

355
356
357
        popd
        echo "EVENT ${EVENT_UNIQUE_ID} DONE with FILE $LOCAL_FILE_WITH_DATA."
      else
358
359
360
361
362
363
364
        echo "Too many particles (maximum accepted is $MAX_PARTICLES). Continue."

        # Remove non-processed files
        pushd ${BLENDER_SCRIPT_DIR}
        rm $LOCAL_FILE_WITH_DATA
        popd

365
366
        continue
      fi
Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
367
  done
368
  popd
369

370
fi