|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Checks that multithreaded Geant4 in o2-sim gives the same output for 1 and N transport threads. |
| 3 | +# |
| 4 | +# Usage: o2-sim-mt-equality.sh [-n events] [-j threads] [-g generator] [--field kG] [--seed seed] [--workdir dir] [module ...] |
| 5 | +# |
| 6 | +# Runs the same seeded workload with --isMT -j 1 and --isMT -j N and compares the kinematics and hit files |
| 7 | +# event by event and branch by branch. Exits with 1 at the first difference. Without modules, the passive |
| 8 | +# geometry with ITS is used. |
| 9 | + |
| 10 | +nev=2 |
| 11 | +nthreads=2 |
| 12 | +gen=pythia8pp |
| 13 | +field=-5 |
| 14 | +seed=4711 |
| 15 | +workdir= |
| 16 | +modules=() |
| 17 | +while [ $# -gt 0 ]; do |
| 18 | + case $1 in |
| 19 | + -n) nev=$2; shift 2 ;; |
| 20 | + -j) nthreads=$2; shift 2 ;; |
| 21 | + -g) gen=$2; shift 2 ;; |
| 22 | + --field) field=$2; shift 2 ;; |
| 23 | + --seed) seed=$2; shift 2 ;; |
| 24 | + --workdir) workdir=$2; shift 2 ;; |
| 25 | + -h|--help) sed -n '2,9p' $0; exit 0 ;; |
| 26 | + *) modules+=("$1"); shift ;; |
| 27 | + esac |
| 28 | +done |
| 29 | +[ ${#modules[@]} -eq 0 ] && modules=(HALL MAG DIPO COMP PIPE ABSO SHIL ITS) |
| 30 | +o2sim=o2-sim |
| 31 | +[ -x "$O2_ROOT/bin/o2-sim" ] && o2sim=$O2_ROOT/bin/o2-sim |
| 32 | +[ -z "$workdir" ] && workdir=$(mktemp -d ${TMPDIR:-/tmp}/o2sim-mt-equality-XXXXXX) |
| 33 | +mkdir -p $workdir && cd $workdir || exit 1 |
| 34 | + |
| 35 | +# content hash per event and branch of the o2sim tree, independent of the basket layout |
| 36 | +cat > hashtree.C <<'EOF' |
| 37 | +void hashtree(const char* fname) |
| 38 | +{ |
| 39 | + TFile f(fname); |
| 40 | + auto t = (TTree*)f.Get("o2sim"); |
| 41 | + if (!t) { |
| 42 | + return; |
| 43 | + } |
| 44 | + std::vector<std::tuple<TClass*, void*, std::string>> objs; |
| 45 | + objs.reserve(t->GetListOfBranches()->GetEntries()); // the branch addresses point into objs |
| 46 | + for (auto b : *t->GetListOfBranches()) { |
| 47 | + auto br = (TBranch*)b; |
| 48 | + TClass* cl = nullptr; |
| 49 | + EDataType dt; |
| 50 | + br->GetExpectedType(cl, dt); |
| 51 | + if (!cl) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + objs.emplace_back(cl, cl->New(), br->GetName()); |
| 55 | + br->SetAddress(&std::get<1>(objs.back())); |
| 56 | + } |
| 57 | + for (Long64_t i = 0; i < t->GetEntries(); ++i) { |
| 58 | + t->GetEntry(i); |
| 59 | + for (auto& [cl, obj, name] : objs) { |
| 60 | + TBufferFile buf(TBuffer::kWrite); |
| 61 | + buf.WriteObjectAny(obj, cl); |
| 62 | + TMD5 md5; |
| 63 | + md5.Update((UChar_t*)buf.Buffer(), buf.Length()); |
| 64 | + md5.Final(); |
| 65 | + printf("HASH event %lld branch %s %s\n", i, name.c_str(), md5.AsString()); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | +EOF |
| 70 | + |
| 71 | +run() { # <threads> |
| 72 | + local dir=mt$1 |
| 73 | + mkdir -p $dir && cd $dir |
| 74 | + $o2sim --isMT 1 -j $1 -n $nev -g $gen -e TGeant4 --seed $seed --field $field -m ${modules[@]} \ |
| 75 | + --configKeyValues "SimCutParams.trackSeed=true" > sim.log 2>&1 |
| 76 | + local rc=$? |
| 77 | + if [ $rc -ne 0 ] || ! grep -q "SIMULATION RETURNED SUCCESFULLY" sim.log; then |
| 78 | + echo "o2-sim with $1 threads failed (exit code $rc), see $workdir/$dir" |
| 79 | + exit 1 |
| 80 | + fi |
| 81 | + for f in o2sim_Kine.root o2sim_Hits*.root; do |
| 82 | + [ -f $f ] && root -l -b -q "../hashtree.C(\"$f\")" 2>/dev/null | grep "^HASH" | sed "s/^HASH/$f/" > $f.hashes |
| 83 | + done |
| 84 | + cd .. |
| 85 | +} |
| 86 | + |
| 87 | +echo "o2-sim MT equality: ${modules[*]}, $nev events of $gen, field $field kG, 1 against $nthreads threads, in $workdir" |
| 88 | +run 1 |
| 89 | +run $nthreads |
| 90 | +status=0 |
| 91 | +for h in mt1/*.hashes; do |
| 92 | + f=$(basename $h) |
| 93 | + if [ ! -s $h ]; then |
| 94 | + echo "${f%.hashes}: no entries" |
| 95 | + status=1 |
| 96 | + elif cmp -s $h mt$nthreads/$f; then |
| 97 | + echo "${f%.hashes}: identical ($(wc -l < $h) event-branch hashes)" |
| 98 | + else |
| 99 | + echo "${f%.hashes}: DIFFERS, first at $(diff $h mt$nthreads/$f | grep -m1 '^<' | cut -d' ' -f3-6)" |
| 100 | + status=1 |
| 101 | + fi |
| 102 | +done |
| 103 | +[ $(ls mt$nthreads/*.hashes | wc -l) -eq $(ls mt1/*.hashes | wc -l) ] || { echo "the runs wrote different output files"; status=1; } |
| 104 | +[ $status -eq 0 ] && echo "EQUALITY TEST PASSED" || echo "EQUALITY TEST FAILED" |
| 105 | +exit $status |
0 commit comments