workflow_sketch.sh 25.9 KB
Newer Older
1
2
3
4
5
6
7
#!/bin/bash

##############################
# Configurations             #
##############################
# Directory where runAnalysis.C is placed
export ALIROOT_SCRIPT_DIR=$(pwd)/aliRoot/
8
# Directory where Blender scripts are
9
10
export BLENDER_SCRIPT_DIR=$(pwd)/animate/
# alienv working directory
11
export ALIENV_WORK_DIR=$(pwd)/../sw
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
12
export ALIENV_OS_SPEC=ubuntu1804_x86-64
13
export ALIENV_ID=AliPhysics/latest-aliroot5-user
14
# Put Blender 2.79b in the PATH env var
15
export PATH="$(pwd)/../blender-2.79-linux-glibc219-x86_64/:$PATH"
16

17
18
19
# Progress log file
export PROGRESS_LOG=$(pwd)/progress.log

Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
20
# Execution log file
21
22
23
DATE_TIME=$(date +"%y-%m-%d-%T")
export EXE_LOG=$(pwd)/execution-${DATE_TIME}.log
echo "Execution ${DATE_TIME}" >> $EXE_LOG
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
24
25
echo " " >> $EXE_LOG

26
27
28
29
30
31
32
33
if [[ -f $PROGRESS_LOG ]]; then
  if grep -q "JOB FINISHED" $PROGRESS_LOG; then
    rm $PROGRESS_LOG
  fi
fi

# Define a timestamp function
timestamp() {
34
  date +"%y-%m-%d, %T, $1"
35
36
}

37
##############################
38
# Command-line options       #
39
##############################
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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

54
OPTIONS=c:hdau:m:n:t:r:
55
LONGOPTS=cameras:,mosaic,resolution:,fps:,transparency:,duration:,radius:,maxparticles:,\
56
minparticles:,numberofevents:,minavgpz:,minavgpt:,help,download,sample,url:,its,\
57
tpc,detailedtpc,trd,emcal,blendersave,picpct:,bgshade:
58
59
60
61
62
63
64
65
66
67
68
69
70
71

# -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"

72
73
74
##############################
# Parse Parameters           #
##############################
75
CAMERAS=Overview
76
MOSAIC=false
77
DURATION=5
78
RADIUS=1
79
RESOLUTION=100
80
FPS=24
81
TRANSPARENCY=1
82
83
84
MAX_PARTICLES=1000
MIN_PARTICLES=0
N_OF_EVENTS=10
85
MIN_AVG_PZ=0
86
MIN_AVG_PT=0
87
88
HELP=false
DOWNLOAD=false
89
SAMPLE=false
90
URL=
91
92
ITS=1 # 1 means "build this detector", while 0 means "don't"
TPC=1
93
DETAILED_TPC=0
94
95
TRD=1
EMCAL=1
96
BLENDERSAVE=0
97
PICPCT=80
98
BGSHADE=0
99
100
101
# now enjoy the options in order and nicely split until we see --
while true; do
    case "$1" in
102
103
104
105
106
      -h|--help)
          HELP=true
          shift
          break
          ;;
107
      -d|--download)
108
109
110
          DOWNLOAD=true
          shift
          ;;
111
      -a|--sample)
112
113
114
          SAMPLE=true
          shift
          ;;
115
116
      -u|--url)
          URL="$2"
117
118
            shift 2
            ;;
119
120
121
122
      -m|--maxparticles)
          MAX_PARTICLES="$2"
          shift 2
          ;;
123
124
125
126
127
128
129
130
      --minparticles)
          MIN_PARTICLES="$2"
          shift 2
          ;;
      -n|--numberofevents)
          N_OF_EVENTS="$2"
          shift 2
          ;;
131
132
133
134
      --minavgpz)
          MIN_AVG_PZ="$2"
          shift 2
          ;;
135
136
137
138
      --minavgpt)
          MIN_AVG_PT="$2"
          shift 2
          ;;
139
140
141
142
      -t|--duration)
          DURATION="$2"
          shift 2
          ;;
143
144
145
146
147
      -r|--radius)
          RADIUS="$2"
          shift 2
          ;;
      --resolution)
148
149
150
          RESOLUTION="$2"
          shift 2
          ;;
151
152
153
154
      --fps)
          FPS="$2"
          shift 2
          ;;
155
156
      --transparency)
          TRANSPARENCY="$2"
157
158
          shift 2
          ;;
159
160
      -c|--cameras)
      	  CAMERAS="$2"
161
162
      	  shift 2
      	  ;;
163
164
165
166
      --mosaic)
          MOSAIC=true
          shift
          ;;
167
168
169
170
      --picpct)
      	  PICPCT="$2"
      	  shift 2
      	  ;;
171
172
173
174
      --bgshade)
      	  BGSHADE="$2"
      	  shift 2
      	  ;;
175
      --its)
176
177
178
          ITS=0
          shift
          ;;
179
      --tpc)
180
181
182
          TPC=0
          shift
          ;;
183
184
185
186
      --detailedtpc)
          DETAILED_TPC=1
          shift
          ;;
187
      --trd)
188
189
190
          TRD=0
          shift
          ;;
191
      --emcal)
192
193
194
          EMCAL=0
          shift
          ;;
195
196
197
198
      --blendersave)
          BLENDERSAVE=1
          shift
          ;;
199
200
201
202
203
        --)
            shift
            break
            ;;
        *)
204
            echo "Programming error $*"
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
            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.
227
   -m | --maxparticles VALUE
228
229
     Get only events for which its number of particles does not
     exceed VALUE.
230
   --minparticles VALUE
231
232
     Get only events for which its number of particles is greater than
     or equal to VALUE.
233
   -n | --numberofevents VALUE
234
     Set number of events to be animated inside chosen ESD file (defaults to 10)
235
236
   --minavgpz VALUE
     Get only events for which its absolute value of average momentum in
237
238
     the z direction is greater than or equal to VALUE, in GeV/c. Useful
     for animating events with 'boosts' of particles to the same side.
239
240
   --minavgpt VALUE
     Get only events for which its average transversal momentum is
241
242
     greater than or equal to VALUE, in GeV/c. Useful for animating
     events with 'boosts' of particles on the xy plane.
243
244
   -t | --duration VALUE
     Set the animation duration in seconds.
245
246
247
248
   -r | --radius VALUE
     Scale default particle radius to a given VALUE, greater than zero.
     For example, VALUE=2 scales particle to twice its default size.
   --resolution VALUE
249
250
     Set the animation resolution percentage, where
     VALUE must be an integer from 1 to 100.
251
252
   --fps VALUE
     Set number of frames per second in animation.
253
254
255
   --transparency VALUE
     Set detector transparency as a number greater than zero,
     where zero is full transparency and 1 is standard transparency
256
257
   -c | --cameras VALUE
     Which cameras to use for the animation, where VALUE
Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
258
     is a comma-separated list (without spaces)
259
     Options: Barrel,Side,Forward,Overview,Moving1,Moving2,Moving3,Moving4 (defaults to Overview)
260
   --mosaic
261
262
263
     Make animations in all four available steady cameras and combine them into
     a single 2x2 clip containing all perspectives, totalizing at least five
     generated .mp4 videos.
264
   --picpct VALUE
265
266
     Percentage of animation to take HD picture, saved along with the clip,
     where VALUE must be an integer
267
268
269
   --bgshade VALUE
     Set background shade of black to VALUE, where 0 is totally black
     and 1 is totally white
270
   -a | --sample
271
     Creates a sample Blender animation of Event 2 from URL
272
273
     http://opendata.cern.ch/record/1102/files/assets/alice/2010/LHC10h/000139\
038/ESD/0001/AliESDs.root
274
   --its
275
     Removes ITS detector from animation
276
277
   --detailedtpc
     Includes more detailed version of TPC in animation
278
   --tpc
279
     Removes TPC detector from animation
280
   --trd
281
     Removes TRD detector from animation
282
   --emcal
283
     Removes EMCal detector from animation
284
285
   --blendersave
     Saves Blender file along with animation clip
286
287
288

Example:
--------
289
290
$0 --url http://opendata.cern.ch/record/1103/files/assets/alice/2010/LHC10h/000\
139173/ESD/0004/AliESDs.root --download
291
292
293

END
}
294

Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
295
# Fix CAMERA to be accepted by the for loop
296
297
if [[ $CAMERAS != "" ]]; then
    CAMERAS=$(echo $CAMERAS | sed -e 's#,#Camera #g' -e 's#$#Camera#')
Lucas Mello Schnorr's avatar
Lucas Mello Schnorr committed
298
299
fi

300
# Set cameras properly if MOSAIC is called
301
if [[ $MOSAIC = "true" ]]; then
302

303
304
305
306
307
308
309
310
311
    CAMS=
    for type in $CAMERAS; do # Add 'non-mosaic' selected cameras to variable CAMS
      if [[ "${type}" != "OverviewCamera" && "${type}" != "BarrelCamera" \
      && "${type}" != "Moving1Camera" && "${type}" != "ForwardCamera" ]]; then
        CAMS+="${type} "
      fi
    done
    # Set variable CAMERAS to contain all selected cameras + mosaic cameras
    CAMERAS="${CAMS}OverviewCamera BarrelCamera Moving1Camera ForwardCamera"
312

313
314
fi

315
316
317
318
319
320
321
if [[ $HELP = "true" ]]; then
    usage
    exit
else
    echo "-------- Parsed parameters --------"
    echo "URL: $URL"
    echo "Download: $DOWNLOAD"
322
    echo "Sample: $SAMPLE"
323
    echo "Transparency Parameter: $TRANSPARENCY"
324
    echo "Duration: $DURATION"
325
    echo "Particle Radius Scale: $RADIUS"
326
    echo "Resolution: $RESOLUTION"
327
    echo "FPS: $FPS"
328
    echo "Max particles: ${MAX_PARTICLES}"
329
330
    echo "Min particles: ${MIN_PARTICLES}"
    echo "Number of events: ${N_OF_EVENTS}"
331
    echo "Min Average Z Momentum: ${MIN_AVG_PZ}"
332
    echo "Min Average Transversal Momentum: ${MIN_AVG_PT}"
333
    echo "Cameras: $CAMERAS"
334
    echo "Mosaic: $MOSAIC"
335
    echo "Picture Percentage: ${PICPCT}%"
336
    echo "Background Shade: ${BGSHADE}"
337
    echo "-----------------------------------"
338
    echo "------------ Detectors ------------"
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
    echo "-------- Parsed parameters --------" >> $EXE_LOG
    echo "URL: $URL" >> $EXE_LOG
    echo "Download: $DOWNLOAD" >> $EXE_LOG
    echo "Sample: $SAMPLE" >> $EXE_LOG
    echo "Transparency Parameter: $TRANSPARENCY" >> $EXE_LOG
    echo "Duration: $DURATION" >> $EXE_LOG
    echo "Particle Radius Scale: $RADIUS" >> $EXE_LOG
    echo "Resolution: $RESOLUTION" >> $EXE_LOG
    echo "FPS: $FPS" >> $EXE_LOG
    echo "Max particles: ${MAX_PARTICLES}" >> $EXE_LOG
    echo "Min particles: ${MIN_PARTICLES}" >> $EXE_LOG
    echo "Number of events: ${N_OF_EVENTS}" >> $EXE_LOG
    echo "Min Average Z Momentum: ${MIN_AVG_PZ}" >> $EXE_LOG
    echo "Min Average Transversal Momentum: ${MIN_AVG_PT}" >> $EXE_LOG
    echo "Cameras: $CAMERAS" >> $EXE_LOG
    echo "Mosaic: $MOSAIC" >> $EXE_LOG
    echo "Picture Percentage: ${PICPCT}%" >> $EXE_LOG
    echo "Background Shade: ${BGSHADE}" >> $EXE_LOG
    echo "-----------------------------------" >> $EXE_LOG
    echo "------------ Detectors ------------" >> $EXE_LOG
359
360
    if [[ $ITS = 1 ]]; then
      echo "Building ITS"
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
361
      echo "Building ITS" >> $EXE_LOG
362
    fi
363
364
    if [[ $DETAILED_TPC = 1 ]]; then
      echo "Building detailed TPC"
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
365
      echo "Building detailed TPC" >> $EXE_LOG
366
367
368
    else
      if [[ $TPC = 1 ]]; then
        echo "Building TPC"
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
369
        echo "Building TPC" >> $EXE_LOG
370
      fi
371
372
373
    fi
    if [[ $TRD = 1 ]]; then
      echo "Building TRD"
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
374
      echo "Building TRD" >> $EXE_LOG
375
376
377
    fi
    if [[ $EMCAL = 1 ]]; then
      echo "Building EMCAL"
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
378
      echo "Building EMCAL" >> $EXE_LOG
379
380
381
    fi
    if [[ $TPC = 0 && $TPC = 0 && $TRD = 0 && $EMCAL = 0 ]]; then
      echo "Not building any detectors"
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
382
      echo "Not building any detectors" >> $EXE_LOG
383
384
    fi
    echo "-----------------------------------"
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
385
386
387
    echo "-----------------------------------" >> $EXE_LOG
    echo " " >> $EXE_LOG

388
389
fi

390
391
392
393
394
# Get number of frames
FPS_DUR="$FPS $DURATION"
FPS_DUR=$(echo $FPS_DUR | awk '{print $1*$2}')


395
396
397
398
399
400
401
# 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

402

403
404
405
406
##############################
# Download Dataset           #
##############################
if [ "$DOWNLOAD" = "true" ]; then
407
    if [ -z $URL ]; then
408
        echo "Error. Must pass the dataset URL in order to download ESD file."
409
410
411
        usage
        exit
    fi
412

413
    if ! grep -q "ESD DOWNLOAD DONE" $PROGRESS_LOG; then
414
415
      echo "Downloading data."
      wget $URL
416
      timestamp "ESD DOWNLOAD DONE" >> $PROGRESS_LOG
417
    fi
418

419
420
421
422
423
424
425
426
427
428
429
430
    ######################################
    # 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
431

432
##############################
433
# Sample synthetic animation#
434
##############################
435
if [ "$SAMPLE" = "true" ]; then
436
    ##############################
437
    # Phase 1: Blender animate   #
438
    ##############################
439
440
441
    BLENDER_OUTPUT=$(pwd)/sample
    mkdir --verbose -p ${BLENDER_OUTPUT}

442
    pushd ${BLENDER_SCRIPT_DIR}
443
444
    echo "Preparing sample animation in Blender"

445
    blender -noaudio --background -P animate_particles.py -- -radius=${RADIUS} \
446
    -duration=${DURATION} -cameras="${CAMERAS}" -datafile="d-esd-detail.dat" -simulated_t=0.03\
447
448
    -fps=${FPS} -resolution=${RESOLUTION} -transparency=${TRANSPARENCY} \
    -stamp_note="opendata.cern.ch_record_1102_alice_2010_LHC10h_000139038_ESD_0001_2" -its=${ITS}\
449
    -tpc=${TPC} -trd=${TRD} -emcal=${EMCAL} -detailed_tpc=${DETAILED_TPC} \
450
    -blendersave=1 -bgshade=${BGSHADE} -tpc_blender_path=${BLENDER_SCRIPT_DIR} \
451
    -output_path="${BLENDER_OUTPUT}"
452

453
454
455
    popd
    echo "Done."

456
457
458
##############################
# Animation from file        #
##############################
459
elif [ "$SAMPLE" = "false" ]; then
460

461
  if ! grep -q "DATA_ANALYSIS, FINISHED" $PROGRESS_LOG; then
462
463
464
465
466
467
468
469
470
471
472
473

    # Verify if AliESDs.root is here
    ALIESD_ROOT_FILE=$(pwd)/AliESDs.root
    if ! [[ -f "$ALIESD_ROOT_FILE" ]]
    then
        echo "AliESDs.root not found."
        exit
    fi

    ############################
    # Phase 1: aliroot extract #
    ############################
474
    timestamp "STARTING DATA ANALYSIS" >> $PROGRESS_LOG
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
    eval $(alienv -w ${ALIENV_WORK_DIR} -a ${ALIENV_OS_SPEC} load ${ALIENV_ID})
    pushd ${ALIROOT_SCRIPT_DIR}
    # Remove existing symbolic link
    rm -f --verbose AliESDs.root
    # Create a symbolic link to the actual AliESDs.root
    ln --verbose -s ${ALIESD_ROOT_FILE} AliESDs.root
    # Run the extraction tool
    aliroot runAnalysis.C

    if [ "$DOWNLOAD" = "false" ]; then

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

    fi

    popd
492
    timestamp "${UNIQUEID}, DATA_ANALYSIS, FINISHED" >> $PROGRESS_LOG
493
494
495
496
497
498
499
500
501
502

  else
    if [ "$DOWNLOAD" = "false" ]; then

      pushd ${ALIROOT_SCRIPT_DIR}
      UNIQUEID=$(more uniqueid.txt)
      echo "The unique ID is $UNIQUEID."
      popd

    fi
503
504
  fi

505
  if ! grep -q "${UNIQUEID}, ANIMATION DIRECTORY CREATED" $PROGRESS_LOG ; then
506
507
508
509

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

511
    timestamp "${UNIQUEID}, ANIMATION DIRECTORY CREATED" >> $PROGRESS_LOG
512
513
  else
    BLENDER_OUTPUT=$(pwd)/$UNIQUEID
514
515
516
517
  fi

  pushd ${ALIROOT_SCRIPT_DIR} # push back to aliroot directory

518
  if ! grep -q "${UNIQUEID}, DATA ANALYSIS FILES MOVED to animation directory" $PROGRESS_LOG; then
519
520
521
    #################################################
    # Phase 1: iteration for every event identifier #
    #################################################
522

523
524
    # Get all extracted files
    EXTRACTED_FILES=$(ls -1 esd_detail-event_*.dat | sort --version-sort)
525

526
    for FILE_WITH_DATA in $EXTRACTED_FILES; do
527

528
529
        if ! [[ -s $FILE_WITH_DATA ]]; then
            echo "File $FILE_WITH_DATA has zero size. Ignore and continue."
530
  	        rm -f $FILE_WITH_DATA
531
532
            continue
        fi
533

534
535
        mv ${ALIROOT_SCRIPT_DIR}/$FILE_WITH_DATA \
         ${BLENDER_SCRIPT_DIR}
536

537
    done
538

539
    timestamp "${UNIQUEID}, DATA ANALYSIS FILES MOVED to animation directory" >> $PROGRESS_LOG
540

541
  fi
542

543
544
  popd
  pushd ${BLENDER_SCRIPT_DIR}
545

546
  if ! grep -q "${UNIQUEID}, CREATED EVENT COUNTER FILE" $PROGRESS_LOG; then
547

548
    # Event counter for animating no more events than the informed amount
549
    rm -f event_counter.txt
550
551
    EVENT_COUNTER=0
    echo "$EVENT_COUNTER" > event_counter.txt
552
    timestamp "${UNIQUEID}, CREATED EVENT COUNTER FILE" >> $PROGRESS_LOG
553
554
555

  fi

556
  if ! grep -q "${UNIQUEID}, DATA FILES RENAMED according to UNIQUEID" $PROGRESS_LOG; then
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571

    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}

        LOCAL_FILE_WITH_DATA=${EVENT_UNIQUE_ID}.dat

        mv $FILE_WITH_DATA $LOCAL_FILE_WITH_DATA

    done
572

573
    timestamp "${UNIQUEID}, DATA FILES RENAMED according to UNIQUEID" >> $PROGRESS_LOG
574

575
576
577
578
  fi

  EXTRACTED_FILES=$(ls -1 ${UNIQUEID}_*.dat | sort --version-sort)

579
  if ! grep -q "${UNIQUEID}, REMOVED UNUSED FILES" $PROGRESS_LOG; then
580

581
    for LOCAL_FILE_WITH_DATA in $EXTRACTED_FILES; do
582

583
584
585
      ##############################
      # Phase 2: Event selection   #
      ##############################
586

587
588
      NUMBER_OF_PARTICLES=$(wc -l $LOCAL_FILE_WITH_DATA | \
                        awk '{ print $1 }')
589

590
      AVERAGE_PZ=$(awk 'BEGIN {pzsum=0;n=0} {pzsum+=$8;n++} END {printf "%f", sqrt(pzsum*pzsum/n/n)}'\
591
592
      ${LOCAL_FILE_WITH_DATA})

593
      AVERAGE_PT=$(awk 'BEGIN {ptsum=0;n=0} {ptsum+=$9;n++} END {printf "%f", ptsum/n}' \
594
595
596
597
598
      ${LOCAL_FILE_WITH_DATA})

      echo "File $LOCAL_FILE_WITH_DATA:"
      echo "Number of particles: $NUMBER_OF_PARTICLES"
      echo "Average Z momentum: $AVERAGE_PZ"
599
      echo "Average transversal momentum: $AVERAGE_PT"
600
601
602
603
604
605
606
607
608
609
610
611
612
613

      EVENT_COUNTER=$(more event_counter.txt)

      ######################################################################
      # Remove text data of files for events that will not be animated:    #
      ######################################################################
      if (( $(echo "$AVERAGE_PT < $MIN_AVG_PT" |bc -l) )); then

        echo "Average Transversal Momentum too low (minimum accepted is $MIN_AVG_PT). Deleting data text file."

        # Remove non-processed files
        rm -f $LOCAL_FILE_WITH_DATA

      elif (( $(echo "$AVERAGE_PZ < $MIN_AVG_PZ" |bc -l) )); then
614

615
        echo "Average Z Momentum too low (minimum accepted is $MIN_AVG_PZ). Deleting data text file."
616

617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
        # Remove non-processed files
        rm -f $LOCAL_FILE_WITH_DATA

      elif [[ $NUMBER_OF_PARTICLES -lt $MIN_PARTICLES ]]; then

        echo "Too little particles (minimum accepted is $MIN_PARTICLES). Deleting data text file."

        # Remove non-processed files
        rm -f $LOCAL_FILE_WITH_DATA

      elif [[ $NUMBER_OF_PARTICLES -gt $MAX_PARTICLES ]]; then

        echo "Too many particles (maximum accepted is $MAX_PARTICLES). Deleting data text file."

        # Remove non-processed files
        rm -f $LOCAL_FILE_WITH_DATA

      elif [[ $EVENT_COUNTER -ge $N_OF_EVENTS ]]; then

        echo "Numbers of events set to be animated has already been reached. Deleting data text file."

        # Remove non-processed files
        rm -f $LOCAL_FILE_WITH_DATA
      else
641

642
643
644
645
        # Increment event counter
        EVENT_COUNTER=$EVENT_COUNTER+1
        rm -f event_counter.txt
        echo "$EVENT_COUNTER" > event_counter.txt
646

647
        echo "This event will be animated. Continue."
648

Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
649
650
651
652
653
        # Print information about selected events in execution file
        echo "File $LOCAL_FILE_WITH_DATA:" >> $EXE_LOG
        echo "Number of particles: $NUMBER_OF_PARTICLES" >> $EXE_LOG
        echo "Average Z momentum: $AVERAGE_PZ" >> $EXE_LOG
        echo "Average transversal momentum: $AVERAGE_PT" >> $EXE_LOG
654
        echo " " >> $EXE_LOG
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
655

656
      fi
657

658
      echo " " # Skip line to make it neat
659

660
    done
661

662
    timestamp "${UNIQUEID}, REMOVED UNUSED FILES" >> $PROGRESS_LOG
663

664
  fi
665

666
667
  # Remove event counter file
  rm -f event_counter.txt
668

Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
669
  # No text data files inside animation directory means job is done
670
  if ! $(ls | grep -q "${UNIQUEID}"); then
671
672
673
674
675
676
    timestamp "${UNIQUEID}, JOB FINISHED" >> $PROGRESS_LOG
    exit
  else
    # Now the list of extracted files shall only include events we want to animate
    EXTRACTED_FILES=$(ls -1 ${UNIQUEID}_*.dat | sort --version-sort)
  fi
677

678
679
680
681
  ################################################
  #   Create script so we can use GNU parallel   #
  #     to create multiple Blender scenes        #
  ################################################
682
  if ! grep -q "${UNIQUEID}, PARALLEL, SCENES, STARTING" $PROGRESS_LOG; then
683

684
    rm -f make-event-*
685

686
687
    CAM_ROT=0 # Binary variable that sets to which side Moving Camera 1 rotates

688
689
690
691
692
693
    for LOCAL_FILE_WITH_DATA in $EXTRACTED_FILES; do

      EVENT_ID=$(echo $LOCAL_FILE_WITH_DATA | \
                 sed -e "s#${UNIQUEID}_##" \
                   -e "s#\.dat##")
      EVENT_UNIQUE_ID=${UNIQUEID}_${EVENT_ID}
694
695
      NUMBER_OF_PARTICLES=$(wc -l $LOCAL_FILE_WITH_DATA | \
                        awk '{ print $1 }')
696

697
698
699
700
701
      # Write commands for making scene and tracking their progress to separate files "make-event-N"
      echo "# Define a timestamp function" >> make-event-${EVENT_ID}
      echo "timestamp() {" >> make-event-${EVENT_ID}
      echo "  date +\"%y-%m-%d, %T, \$1\"" >> make-event-${EVENT_ID}
      echo "}" >> make-event-${EVENT_ID}
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
702
      echo timestamp \"${UNIQUEID}, EVENT ${EVENT_ID}, BLENDER SCENE, STARTING, ${NUMBER_OF_PARTICLES} PARTICLES\" \>\> $PROGRESS_LOG >> make-event-${EVENT_ID}
703
      echo blender -noaudio --background -P animate_particles.py -- -radius=${RADIUS} \
704
705
706
707
  -duration=${DURATION} -datafile=\'${LOCAL_FILE_WITH_DATA}\' \
  -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} \
708
  -blendersave=1 -bgshade=${BGSHADE} -tpc_blender_path=${BLENDER_SCRIPT_DIR} \
709
  -output_path=\'${BLENDER_OUTPUT}\' -direction=${CAM_ROT} >> make-event-${EVENT_ID}
Breno Rilho Lemos's avatar
Breno Rilho Lemos committed
710
      echo timestamp \"${UNIQUEID}, EVENT ${EVENT_ID}, BLENDER SCENE, FINISHED, ${NUMBER_OF_PARTICLES} PARTICLES\" \>\> $PROGRESS_LOG >> make-event-${EVENT_ID}
711
      echo rm -f make-event-${EVENT_ID} >> make-event-${EVENT_ID}
712

713
714
715
      # Switch value of CAM_ROT
      if [[ ${CAM_ROT} == 0 ]]; then CAM_ROT=1; else CAM_ROT=0; fi

716
    done
717

718
    timestamp "${UNIQUEID}, PARALLEL, SCENES, STARTING" >> $PROGRESS_LOG
719

720
  fi
721

722
723
724
725
  ###################################
  # Make Blender scenes in parallel #
  ###################################
  if ! grep -q "${UNIQUEID}, PARALLEL, SCENES, FINISHED" $PROGRESS_LOG; then
726
    if [[ -f make-event-* ]]; then
727

728
729
      MAKE_EVENT_FILES=$(ls -1 make-event-*) # List of all files 'make-event-n'
      rm -f scene-making
730

731
732
733
734
      # Write command to run code inside "make-event-N" on "scene-making" file
      for FILE in $MAKE_EVENT_FILES; do
        echo ./${FILE} >> scene-making
      done
735

736
737
738
739
740
741
      # O. Tange (2011): GNU Parallel - The Command-Line Power Tool,
      # ;login: The USENIX Magazine, February 2011:42-47.
      chmod +x make-event-*
      parallel < scene-making
      
    fi
742
    timestamp "${UNIQUEID}, PARALLEL, SCENES, FINISHED" >> $PROGRESS_LOG
743

744
  fi
745

746
  rm -f scene-making
747
  rm -f make-event-*
748

749
750
751
752
  #####################################
  # Render scenes in selected cameras #
  #####################################
  for LOCAL_FILE_WITH_DATA in $EXTRACTED_FILES; do
753

754
755
756
757
758
759
    EVENT_ID=$(echo $LOCAL_FILE_WITH_DATA | \
               sed -e "s#${UNIQUEID}_##" \
                 -e "s#\.dat##")
    EVENT_UNIQUE_ID=${UNIQUEID}_${EVENT_ID}
    NUMBER_OF_PARTICLES=$(wc -l $LOCAL_FILE_WITH_DATA | \
                      awk '{ print $1 }')
760

761
    for type in $CAMERAS; do
762

763
      if ! grep -q "${UNIQUEID}, EVENT ${EVENT_ID}, ${type}, FINISHED" $PROGRESS_LOG; then
764

765
        timestamp "${UNIQUEID}, EVENT ${EVENT_ID}, ${type}, STARTING, $NUMBER_OF_PARTICLES PARTICLES" >> $PROGRESS_LOG
766
767
        blender -noaudio --background -P render.py -- -cam ${type} -datafile\
         "${LOCAL_FILE_WITH_DATA}" -n_event ${EVENT_ID} -pic_pct ${PICPCT} -output_path "${BLENDER_OUTPUT}"
768

769
        timestamp "${UNIQUEID}, EVENT ${EVENT_ID}, ${type}, FINISHED, $NUMBER_OF_PARTICLES PARTICLES" >> $PROGRESS_LOG
770

771
      fi
772

773
    done
774

775
776
777
778
    ########################
    # Make mosaic 2x2 clip #
    ########################
    if [ "$MOSAIC" = "true" ]; then
779

780
781
      if ! grep -q "${UNIQUEID}, ${EVENT_ID}, MOSAIC, FINISHED" $PROGRESS_LOG; then
        pushd ${BLENDER_OUTPUT}
782

783
        timestamp "${UNIQUEID}, ${EVENT_ID}, MOSAIC, STARTING, $NUMBER_OF_PARTICLES" >> $PROGRESS_LOG
784

785
786
787
        # Delete existing incomplete .mp4 file
        if [[ -f ${EVENT_UNIQUE_ID}_Mosaic.mp4 ]]; then
          rm ${EVENT_UNIQUE_ID}_Mosaic.mp4
788
        fi
789

790
        # Setting input names for clips in order to make mosaic clip
791
792
793
794
        INPUT_ONE=*${EVENT_UNIQUE_ID}.dat*BarrelCamera*${FPS_DUR}.mp4
        INPUT_TWO=*${EVENT_UNIQUE_ID}.dat*ForwardCamera*${FPS_DUR}.mp4
        INPUT_THREE=*${EVENT_UNIQUE_ID}.dat*Moving1Camera*${FPS_DUR}.mp4
        INPUT_FOUR=*${EVENT_UNIQUE_ID}.dat*OverviewCamera*${FPS_DUR}.mp4
795
796
797

        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]"\
798
799
800
801
         -map "[v]" ${EVENT_UNIQUE_ID}_${FPS_DUR}FPS_Mosaic.mp4

        # Add Mosaic to list to make video strip containing all mosaics
        echo file \'${EVENT_UNIQUE_ID}_${FPS_DUR}FPS_Mosaic.mp4\' >> videostrip.txt
802
803
804
805

        timestamp "${UNIQUEID}, ${EVENT_ID}, MOSAIC, FINISHED, $NUMBER_OF_PARTICLES" >> $PROGRESS_LOG

        popd
806
      fi
807
808
809

    fi

810
    echo "EVENT ${EVENT_UNIQUE_ID} DONE with FILE $LOCAL_FILE_WITH_DATA."
811

812
813
    # Remove text data files
    rm -f $LOCAL_FILE_WITH_DATA
814

815
  done
816

817
  if [ "$MOSAIC" = "true" ]; then
818

819
820
821
822
823
824
825
826
827
828
829
830
    if ! grep -q "${UNIQUEID}, VIDEO STRIP, FINISHED" $PROGRESS_LOG; then

      pushd ${BLENDER_OUTPUT}
      timestamp "${UNIQUEID}, VIDEO STRIP, STARTING" >> $PROGRESS_LOG

      ffmpeg -f concat -safe 0 -i videostrip.txt -c copy Mosaic_VideoStrip_${DATE_TIME}.mp4
      rm -f videostrip.txt

      timestamp "${UNIQUEID}, VIDEO STRIP, FINISHED" >> $PROGRESS_LOG
      popd
    fi
  fi
831
  popd
832

833
834
835
836
837
838
839
840
841
  ########################
  # Remove blender files #
  ########################
  if [[ $BLENDERSAVE = 0 ]]; then
    pushd ${BLENDER_OUTPUT}
    rm -f *.blend
    popd
  fi

842
fi
843
timestamp "${UNIQUEID}, JOB FINISHED" >> $PROGRESS_LOG