workflow_sketch.sh 12.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
#!/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/
10
# Directory where Blender scripts are
11
12
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
export ALIENV_ID=AliPhysics/latest-aliroot5-user
16
# Put Blender 2.79b in the PATH env var
17
18
19
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:n:t:r:
LONGOPTS=camera:,resolution:,fps:,transperency:,duration:,maxparticles:,minparticles:,numberofevents:,help,download,sample,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
FPS=24
59
TRANSPERENCY=1
60
61
62
MAX_PARTICLES=1000
MIN_PARTICLES=0
N_OF_EVENTS=10
63
64
HELP=false
DOWNLOAD=false
65
SAMPLE=false
66
URL=
67
68
69
70
ITS=1 # 1 means "build this detector", while 0 means "don't"
TPC=1
TRD=1
EMCAL=1
71
72
73
# now enjoy the options in order and nicely split until we see --
while true; do
    case "$1" in
74
75
76
77
78
      -h|--help)
          HELP=true
          shift
          break
          ;;
79
      -d|--download)
80
81
82
            DOWNLOAD=true
            shift
            ;;
83
84
      -a|--sample)
            SAMPLE=true
85
86
            shift
            ;;
87
88
      -u|--url)
          URL="$2"
89
90
            shift 2
            ;;
91
92
93
94
      -m|--maxparticles)
          MAX_PARTICLES="$2"
          shift 2
          ;;
95
96
97
98
99
100
101
102
      --minparticles)
          MIN_PARTICLES="$2"
          shift 2
          ;;
      -n|--numberofevents)
          N_OF_EVENTS="$2"
          shift 2
          ;;
103
104
105
106
107
108
109
110
      -t|--duration)
          DURATION="$2"
          shift 2
          ;;
      -r|--resolution)
          RESOLUTION="$2"
          shift 2
          ;;
111
112
113
114
115
      --fps)
          FPS="$2"
          shift 2
          ;;
      --transperency)
116
117
118
          TRANSPERENCY="$2"
          shift 2
          ;;
Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
119
      -c|--camera)
120
121
122
      	  CAMERA="$2"
      	  shift 2
      	  ;;
123
      --its)
124
125
126
          ITS=0
          shift
          ;;
127
      --tpc)
128
129
130
          TPC=0
          shift
          ;;
131
      --trd)
132
133
134
          TRD=0
          shift
          ;;
135
      --emcal)
136
137
138
          EMCAL=0
          shift
          ;;
139
140
141
142
143
        --)
            shift
            break
            ;;
        *)
144
            echo "Programming error $*"
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
            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.
167
   -m | --maxparticles VALUE
168
169
170
171
172
     Get only events for which its number of particles does not exceed VALUE.
   --minparticles VALUE
     Get only events for which its number of particles is greater than or equal to VALUE.
   -n | --numberofevents VALUE
     Set number of events to be animated inside chosen ESD file.
173
174
175
176
   -t | --duration VALUE
     Set the animation duration in seconds.
   -r | --resolution VALUE
     Set the animation resolution percentage.
177
178
179
   --fps VALUE
     Set number of frames per second in animation.
   --transperency VALUE
180
181
     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
182
183
184
185
   -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)
186
187
188
   -a | --sample
     Creates a sample animation with Blender of Event x in ESD file {tal}.
   --its
189
     Removes ITS detector from animation
190
   --tpc
191
     Removes TPC detector from animation
192
   ---trd
193
     Removes TRD detector from animation
194
   --emcal
195
     Removes EMCal detector from animation
196
197
198
199
200
201
202

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

END
}
203

Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
204
205
206
207
208
# Fix CAMERA to be accepted by the for loop
if [[ $CAMERA != "" ]]; then
    CAMERA=$(echo $CAMERA | sed -e 's#,#Camera #g' -e 's#$#Camera#')
fi

209
210
211
212
213
214
215
if [[ $HELP = "true" ]]; then
    usage
    exit
else
    echo "-------- Parsed parameters --------"
    echo "URL: $URL"
    echo "Download: $DOWNLOAD"
216
    echo "Sample: $SAMPLE"
217
218
219
    echo "Transperency Parameter: $TRANSPERENCY"
    echo "Duration: $DURATION"
    echo "Resolution: $RESOLUTION"
220
    echo "FPS: $FPS"
221
    echo "Max particles: ${MAX_PARTICLES}"
222
223
    echo "Min particles: ${MIN_PARTICLES}"
    echo "Number of events: ${N_OF_EVENTS}"
Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
224
    echo "Camera: $CAMERA"
225
    echo "-----------------------------------"
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
    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 "-----------------------------------"

244
245
fi

246
247
248
249
250
251
252
# 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

253
254
255
256
##############################
# Download Dataset           #
##############################
if [ "$DOWNLOAD" = "true" ]; then
257
    if [ -z $URL ]; then
258
        echo "Error. Must pass the dataset URL in order to download ESD file."
259
260
261
        usage
        exit
    fi
262
263
264
    echo "Downloading data."
    wget $URL

265
266
267
268
269
270
271
272
273
274
275
276
    ######################################
    # Established Unique ID based on URL #
    ######################################
    UNIQUEID=$(echo $URL | sed \
                         -e "s#http://opendata.cern.ch/##" \
                         -e "s#/AliESDs.root##" \
                         -e "s#files/assets/##" \
                         -e "s#/#_#g")

    echo "The unique ID is $UNIQUEID."

fi
277

278
##############################
279
# Sample synthetic animation#
280
##############################
281
if [ "$SAMPLE" = "true" ]; then
282
    ##############################
283
    # Phase 1: Blender animate   #
284
285
    ##############################
    pushd ${BLENDER_SCRIPT_DIR}
286
287
288
289
    for type in $CAMERA; do
      echo "Preparing sample animation with $type Camera in Blender"
      blender -noaudio --background -P animate_particles.py -- -radius=0.05 -duration=${DURATION} -camera=${type} -datafile="d-esd-detail.dat" -simulated_t=0.03 -fps=${FPS} -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}
    done
290
    popd
291
    BLENDER_OUTPUT=.
292
293
294
295
    mkdir --verbose -p ${BLENDER_OUTPUT}
    mv --verbose /tmp/blender ${BLENDER_OUTPUT}
    echo "Done."

296
297
298
##############################
# Animation from file        #
##############################
299
elif [ "$SAMPLE" = "false" ]; then
300

301
  # Verify if AliESDs.root is here
302
303
304
  ALIESD_ROOT_FILE=$(pwd)/AliESDs.root
  if ! [[ -f "$ALIESD_ROOT_FILE" ]]
  then
Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
305
306
      echo "AliESDs.root not found."
      exit
307
308
  fi

309
310
311
  ############################
  # Phase 1: aliroot extract #
  ############################
312
  eval $(alienv -w ${ALIENV_WORK_DIR} -a ${ALIENV_OS_SPEC} load ${ALIENV_ID})
313
  pushd ${ALIROOT_SCRIPT_DIR}
314
  # Remove existing symbolic link
315
  rm -f --verbose AliESDs.root
316
317
318
  # Create a symbolic link to the actual AliESDs.root
  ln --verbose -s ${ALIESD_ROOT_FILE} AliESDs.root
  # Run the extraction tool
319
  aliroot runAnalysis.C
320

321
322
323
324
325
326
327
328
329
330
331
332
333
334
  if [ "$DOWNLOAD" = "false" ]; then

    UNIQUEID=$(more uniqueid.txt)
    echo "The unique ID is $UNIQUEID."
    rm uniqueid.txt

  fi

  # Create directory where animations will be saved
  popd
  BLENDER_OUTPUT=$(pwd)/$UNIQUEID
  mkdir --verbose -p ${BLENDER_OUTPUT}
  pushd ${ALIROOT_SCRIPT_DIR} # push back to aliroot directory

335
336
337
  #################################################
  # Phase 1: iteration for every event identifier #
  #################################################
338

339
340
341
  # Event counter for animating no more events than the informed amount
  EVENT_COUNTER=0

342
343
344
345
  # 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 | \
346
347
                 sed -e "s#esd_detail-event_##" \
                   -e "s#\.dat##")
348
349
      EVENT_UNIQUE_ID=${UNIQUEID}_${EVENT_ID}

Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
350
      if ! [[ -s $FILE_WITH_DATA ]]; then
351
          echo "File $FILE_WITH_DATA has zero size. Ignore and continue."
352
	        rm $FILE_WITH_DATA
353
          continue
Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
354
      fi
355

356
357
358
      ##############################
      # Phase 2: blender animate   #
      ##############################
359
360
361

      LOCAL_FILE_WITH_DATA=${EVENT_UNIQUE_ID}.dat
      cp ${ALIROOT_SCRIPT_DIR}/$FILE_WITH_DATA \
362
363
       ${BLENDER_SCRIPT_DIR}/${LOCAL_FILE_WITH_DATA}

364
365
      rm $FILE_WITH_DATA

366
367
368
      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"
369
370
371
372
373
374
375

      if [[ $NUMBER_OF_PARTICLES -lt $MAX_PARTICLES+1 && $NUMBER_OF_PARTICLES -gt $MIN_PARTICLES-1 && $EVENT_COUNTER -lt $N_OF_EVENTS ]]; then

        # Increment event counter
        EVENT_COUNTER=$EVENT_COUNTER+1

        echo "Processing ${EVENT_UNIQUE_ID} ($NUMBER_OF_PARTICLES) in Blender"
376

377
        pushd ${BLENDER_SCRIPT_DIR}
378

Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
379
        for type in $CAMERA; do
380
              echo "Processing ${EVENT_UNIQUE_ID} with $type Camera in Blender"
381

382
              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=${FPS} -resolution=${RESOLUTION} -transperency=${TRANSPERENCY} -stamp_note="${EVENT_UNIQUE_ID}" -its=${ITS} -tpc=${TPC} -trd=${TRD} -emcal=${EMCAL}
383
384
              # Move generated file to final location
              mv /tmp/blender/* ${BLENDER_OUTPUT}
Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
385
              echo "${type} for event ${EVENT_UNIQUE_ID} done."
386
        done
387

388
        # Move processed file to final location
389
        mv $LOCAL_FILE_WITH_DATA ${BLENDER_OUTPUT}/$LOCAL_FILE_WITH_DATA
390

391
392
393
        popd
        echo "EVENT ${EVENT_UNIQUE_ID} DONE with FILE $LOCAL_FILE_WITH_DATA."
      else
394
395
396
397
398
399
400
401

        if [[ $NUMBER_OF_PARTICLES -lt $MIN_PARTICLES ]]; then
          echo "Too little particles (minimum accepted is $MIN_PARTICLES). Continue."
        elif [[ $NUMBER_OF_PARTICLES -gt $MAX_PARTICLES ]]; then
          echo "Too many particles (maximum accepted is $MAX_PARTICLES). Continue."
        else
          echo "Numbers of events set to be animated has already been reached."
        fi
402
403
404
405
406
407

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

408
409
        continue
      fi
Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
410
  done
411
  popd
412

413
fi