workflow_sketch.sh 23.5 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
export PATH="/home/schnorr/install/blender-2.79-linux-glibc219-x86_64/:$PATH"

19
20
21
# Progress log file
export PROGRESS_LOG=$(pwd)/progress.log

22
23
24
25
26
27
28
29
if [[ -f $PROGRESS_LOG ]]; then
  if grep -q "JOB FINISHED" $PROGRESS_LOG; then
    rm $PROGRESS_LOG
  fi
fi

# Define a timestamp function
timestamp() {
30
  date +"%y-%m-%d, %T, $1"
31
32
}

33
##############################
34
# Command-line options       #
35
##############################
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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

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

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

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

Example:
--------
285
286
$0 --url http://opendata.cern.ch/record/1103/files/assets/alice/2010/LHC10h/000\
139173/ESD/0004/AliESDs.root --download
287
288
289

END
}
290

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

296
# Set cameras properly if MOSAIC is called
297
if [[ $MOSAIC = "true" ]]; then
298

299
300
301
302
303
304
305
306
307
    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"
308

309
310
fi

311
312
313
314
315
316
317
if [[ $HELP = "true" ]]; then
    usage
    exit
else
    echo "-------- Parsed parameters --------"
    echo "URL: $URL"
    echo "Download: $DOWNLOAD"
318
    echo "Sample: $SAMPLE"
319
    echo "Transparency Parameter: $TRANSPARENCY"
320
    echo "Duration: $DURATION"
321
    echo "Particle Radius Scale: $RADIUS"
322
    echo "Resolution: $RESOLUTION"
323
    echo "FPS: $FPS"
324
    echo "Max particles: ${MAX_PARTICLES}"
325
326
    echo "Min particles: ${MIN_PARTICLES}"
    echo "Number of events: ${N_OF_EVENTS}"
327
    echo "Min Average Z Momentum: ${MIN_AVG_PZ}"
328
    echo "Min Average Transversal Momentum: ${MIN_AVG_PT}"
329
    echo "Cameras: $CAMERAS"
330
    echo "Mosaic: $MOSAIC"
331
    echo "Picture Percentage: ${PICPCT}%"
332
    echo "Background Shade: ${BGSHADE}"
333
    echo "-----------------------------------"
334
335
336
337
    echo "------------ Detectors ------------"
    if [[ $ITS = 1 ]]; then
      echo "Building ITS"
    fi
338
339
340
341
342
343
    if [[ $DETAILED_TPC = 1 ]]; then
      echo "Building detailed TPC"
    else
      if [[ $TPC = 1 ]]; then
        echo "Building TPC"
      fi
344
345
346
347
348
349
350
351
352
353
354
    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 "-----------------------------------"
355
356
fi

357
358
359
360
361
# Get number of frames
FPS_DUR="$FPS $DURATION"
FPS_DUR=$(echo $FPS_DUR | awk '{print $1*$2}')


362
363
364
365
366
367
368
# 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

369

370
371
372
373
##############################
# Download Dataset           #
##############################
if [ "$DOWNLOAD" = "true" ]; then
374
    if [ -z $URL ]; then
375
        echo "Error. Must pass the dataset URL in order to download ESD file."
376
377
378
        usage
        exit
    fi
379

380
    if ! grep -q "ESD DOWNLOAD DONE" $PROGRESS_LOG; then
381
382
      echo "Downloading data."
      wget $URL
383
      timestamp "ESD DOWNLOAD DONE" >> $PROGRESS_LOG
384
    fi
385

386
387
388
389
390
391
392
393
394
395
396
397
    ######################################
    # 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
398

399
##############################
400
# Sample synthetic animation#
401
##############################
402
if [ "$SAMPLE" = "true" ]; then
403
    ##############################
404
    # Phase 1: Blender animate   #
405
    ##############################
406
407
408
    BLENDER_OUTPUT=$(pwd)/sample
    mkdir --verbose -p ${BLENDER_OUTPUT}

409
    pushd ${BLENDER_SCRIPT_DIR}
410
411
    echo "Preparing sample animation in Blender"

412
    blender -noaudio --background -P animate_particles.py -- -radius=${RADIUS} \
413
    -duration=${DURATION} -cameras="${CAMERAS}" -datafile="d-esd-detail.dat" -simulated_t=0.03\
414
415
    -fps=${FPS} -resolution=${RESOLUTION} -transparency=${TRANSPARENCY} \
    -stamp_note="opendata.cern.ch_record_1102_alice_2010_LHC10h_000139038_ESD_0001_2" -its=${ITS}\
416
    -tpc=${TPC} -trd=${TRD} -emcal=${EMCAL} -detailed_tpc=${DETAILED_TPC} \
417
    -blendersave=1 -bgshade=${BGSHADE} -tpc_blender_path=${BLENDER_SCRIPT_DIR} \
418
    -output_path="${BLENDER_OUTPUT}"
419

420
421
422
    popd
    echo "Done."

423
424
425
##############################
# Animation from file        #
##############################
426
elif [ "$SAMPLE" = "false" ]; then
427

428
  if ! grep -q "DATA_ANALYSIS, FINISHED" $PROGRESS_LOG; then
429
430
431
432
433
434
435
436
437
438
439
440

    # 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 #
    ############################
441
    timestamp "STARTING DATA ANALYSIS" >> $PROGRESS_LOG
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
    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
459
    timestamp "${UNIQUEID}, DATA_ANALYSIS, FINISHED" >> $PROGRESS_LOG
460
461
462
463
464
465
466
467
468
469

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

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

    fi
470
471
  fi

472
  if ! grep -q "${UNIQUEID}, ANIMATION DIRECTORY CREATED" $PROGRESS_LOG ; then
473
474
475
476

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

478
    timestamp "${UNIQUEID}, ANIMATION DIRECTORY CREATED" >> $PROGRESS_LOG
479
480
  else
    BLENDER_OUTPUT=$(pwd)/$UNIQUEID
481
482
483
484
  fi

  pushd ${ALIROOT_SCRIPT_DIR} # push back to aliroot directory

485
  if ! grep -q "${UNIQUEID}, DATA ANALYSIS FILES MOVED to animation directory" $PROGRESS_LOG; then
486
487
488
    #################################################
    # Phase 1: iteration for every event identifier #
    #################################################
489

490
491
    # Get all extracted files
    EXTRACTED_FILES=$(ls -1 esd_detail-event_*.dat | sort --version-sort)
492

493
    for FILE_WITH_DATA in $EXTRACTED_FILES; do
494

495
496
        if ! [[ -s $FILE_WITH_DATA ]]; then
            echo "File $FILE_WITH_DATA has zero size. Ignore and continue."
497
  	        rm -f $FILE_WITH_DATA
498
499
            continue
        fi
500

501
502
        mv ${ALIROOT_SCRIPT_DIR}/$FILE_WITH_DATA \
         ${BLENDER_SCRIPT_DIR}
503

504
    done
505

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

508
  fi
509

510
511
  popd
  pushd ${BLENDER_SCRIPT_DIR}
512

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

515
    # Event counter for animating no more events than the informed amount
516
    rm -f event_counter.txt
517
518
    EVENT_COUNTER=0
    echo "$EVENT_COUNTER" > event_counter.txt
519
    timestamp "${UNIQUEID}, CREATED EVENT COUNTER FILE" >> $PROGRESS_LOG
520
521
522

  fi

523
  if ! grep -q "${UNIQUEID}, DATA FILES RENAMED according to UNIQUEID" $PROGRESS_LOG; then
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538

    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
539

540
    timestamp "${UNIQUEID}, DATA FILES RENAMED according to UNIQUEID" >> $PROGRESS_LOG
541

542
543
544
545
  fi

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

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

548
    for LOCAL_FILE_WITH_DATA in $EXTRACTED_FILES; do
549

550
551
552
      ##############################
      # Phase 2: Event selection   #
      ##############################
553

554
555
      NUMBER_OF_PARTICLES=$(wc -l $LOCAL_FILE_WITH_DATA | \
                        awk '{ print $1 }')
556

557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
      AVERAGE_PZ=$(awk 'BEGIN {pzsum=0;n=0} {pzsum+=$8;n++} END {print sqrt(pzsum*pzsum/n/n)}'\
      ${LOCAL_FILE_WITH_DATA})

      AVERAGE_PT=$(awk 'BEGIN {ptsum=0;n=0} {ptsum+=$9;n++} END {print ptsum/n}' \
      ${LOCAL_FILE_WITH_DATA})

      echo "File $LOCAL_FILE_WITH_DATA:"
      echo "Number of particles: $NUMBER_OF_PARTICLES"
      echo "Average Z momentum: $AVERAGE_PZ"
      echo "Average transversal momentum $AVERAGE_PT"

      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
581

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

584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
        # 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
608

609
610
611
612
        # Increment event counter
        EVENT_COUNTER=$EVENT_COUNTER+1
        rm -f event_counter.txt
        echo "$EVENT_COUNTER" > event_counter.txt
613

614
        echo "This event will be animated. Continue."
615

616
      fi
617

618
      echo " " # Skip line to make it neat
619

620
    done
621

622
    timestamp "${UNIQUEID}, REMOVED UNUSED FILES" >> $PROGRESS_LOG
623

624
  fi
625

626
627
  # Remove event counter file
  rm -f event_counter.txt
628

629
630
631
632
633
634
635
  if [[ $N_OF_EVENTS = 0 ]]; then
    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
636

637
638
639
640
  ################################################
  #   Create script so we can use GNU parallel   #
  #     to create multiple Blender scenes        #
  ################################################
641
  if ! grep -q "${UNIQUEID}, PARALLEL, SCENES, STARTING" $PROGRESS_LOG; then
642
643

    rm -f scene-making
644
645
    rm -f make-event-*
    rm -f blender-scenes
646
647
648
649
650
651
652

    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}
653
654
      NUMBER_OF_PARTICLES=$(wc -l $LOCAL_FILE_WITH_DATA | \
                        awk '{ print $1 }')
655

656
657
658
659
660
661
      # 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}
      echo timestamp \"${UNIQUEID}, ${EVENT_ID}, BLENDER SCENE, STARTING, ${NUMBER_OF_PARTICLES}\" \>\> $PROGRESS_LOG >> make-event-${EVENT_ID}
662
      echo blender -noaudio --background -P animate_particles.py -- -radius=${RADIUS} \
663
664
665
666
  -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} \
667
  -blendersave=1 -bgshade=${BGSHADE} -tpc_blender_path=${BLENDER_SCRIPT_DIR} \
668
669
670
671
672
673
674
675
  -output_path=\'${BLENDER_OUTPUT}\' >> make-event-${EVENT_ID}
      echo timestamp \"${UNIQUEID}, ${EVENT_ID}, BLENDER SCENE, FINISHED, ${NUMBER_OF_PARTICLES}\" \>\> $PROGRESS_LOG >> make-event-${EVENT_ID}

      # Write text inside "make-event-N" on common file for all events for registering sake
      more make-event-${EVENT_ID} >> blender-scenes

      # Write command to run code inside "make-event-N" on "scene-making" file
      echo ./make-event-${EVENT_ID} >> scene-making
676
    done
677

678
    timestamp "${UNIQUEID}, PARALLEL, SCENES, STARTING" >> $PROGRESS_LOG
679

680
  fi
681

682
683
684
685
  ###################################
  # Make Blender scenes in parallel #
  ###################################
  if ! grep -q "${UNIQUEID}, PARALLEL, SCENES, FINISHED" $PROGRESS_LOG; then
686

687
    chmod +x make-event-*
688
    parallel < scene-making
689

690
    timestamp "${UNIQUEID}, PARALLEL, SCENES, FINISHED" >> $PROGRESS_LOG
691

692
  fi
693

694
  rm -f scene-making
695
696
  rm -f make-event-*
  mv blender-scenes ${BLENDER_OUTPUT}/blender-scenes
697

698
699
700
701
  #####################################
  # Render scenes in selected cameras #
  #####################################
  for LOCAL_FILE_WITH_DATA in $EXTRACTED_FILES; do
702

703
704
705
706
707
708
    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 }')
709

710
    for type in $CAMERAS; do
711

712
      if ! grep -q "${UNIQUEID}, ${EVENT_ID}, ${type}, FINISHED" $PROGRESS_LOG; then
713

714
715
716
        timestamp "${UNIQUEID}, ${EVENT_ID}, ${type}, STARTING, $NUMBER_OF_PARTICLES" >> $PROGRESS_LOG
        blender -noaudio --background -P render.py -- -cam ${type} -datafile\
         "${LOCAL_FILE_WITH_DATA}" -n_event ${EVENT_ID} -pic_pct ${PICPCT} -output_path "${BLENDER_OUTPUT}"
717

718
        timestamp "${UNIQUEID}, ${EVENT_ID}, ${type}, FINISHED, $NUMBER_OF_PARTICLES" >> $PROGRESS_LOG
719

720
      fi
721

722
    done
723

724
725
726
727
    ########################
    # Make mosaic 2x2 clip #
    ########################
    if [ "$MOSAIC" = "true" ]; then
728

729
730
      if ! grep -q "${UNIQUEID}, ${EVENT_ID}, MOSAIC, FINISHED" $PROGRESS_LOG; then
        pushd ${BLENDER_OUTPUT}
731

732
        timestamp "${UNIQUEID}, ${EVENT_ID}, MOSAIC, STARTING, $NUMBER_OF_PARTICLES" >> $PROGRESS_LOG
733

734
735
736
        # Delete existing incomplete .mp4 file
        if [[ -f ${EVENT_UNIQUE_ID}_Mosaic.mp4 ]]; then
          rm ${EVENT_UNIQUE_ID}_Mosaic.mp4
737
        fi
738

739
        # Setting input names for clips in order to make mosaic clip
740
741
742
743
        INPUT_ONE=*${EVENT_UNIQUE_ID}*BarrelCamera*${FPS_DUR}.mp4
        INPUT_TWO=*${EVENT_UNIQUE_ID}*ForwardCamera*${FPS_DUR}.mp4
        INPUT_THREE=*${EVENT_UNIQUE_ID}*Moving1Camera*${FPS_DUR}.mp4
        INPUT_FOUR=*${EVENT_UNIQUE_ID}*OverviewCamera*${FPS_DUR}.mp4
744
745
746
747
748
749
750
751

        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

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

        popd
752
      fi
753
754
755

    fi

756
757
758
759
    #######################################################
    # Move text data files to where animations are stored #
    #######################################################
    if ! grep -q "${UNIQUEID}, ${EVENT_ID}, TEXT DATA MOVED to final location" $PROGRESS_LOG; then
760

761
762
763
      # Move processed file to final location
      mv $LOCAL_FILE_WITH_DATA ${BLENDER_OUTPUT}/$LOCAL_FILE_WITH_DATA
      timestamp "${UNIQUEID}, ${EVENT_ID}, TEXT DATA MOVED to final location" >> $PROGRESS_LOG
764

765
766
767
768
769
    fi

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

  done
770
  popd
771

772
773
774
775
776
777
778
779
780
  ########################
  # Remove blender files #
  ########################
  if [[ $BLENDERSAVE = 0 ]]; then
    pushd ${BLENDER_OUTPUT}
    rm -f *.blend
    popd
  fi

781
fi
782
timestamp "${UNIQUEID}, JOB FINISHED" >> $PROGRESS_LOG