Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2026-05-23 08:12:16

0001 #!/bin/bash                                                                                                                                                                                               
0002 
0003 export USER="$(id -u -n)"
0004 export LOGNAME=${USER}
0005 export HOME=/sphenix/u/${LOGNAME}/macros/detectors/sPHENIX/
0006 export MYINSTALL=/sphenix/user/bkimelman/sPHENIX/install/
0007 
0008 source /opt/sphenix/core/bin/sphenix_setup.sh -n
0009 source /opt/sphenix/core/bin/setup_local.sh $MYINSTALL
0010 
0011 
0012 set -euo pipefail
0013 shopt -s nullglob
0014 
0015 baseDir="$2"
0016 fileBase="$3"
0017 closureMode="$4"
0018 
0019 closureModeStr=""
0020 if [[ "$closureMode" == "kFull" ]]; then
0021     closureModeStr="fullClosure"
0022 elif [[ "$closureMode" == "kHalf" ]]; then
0023     closureModeStr="halfClosure"
0024 elif [[ "$closureMode" == "kNoTest" ]]; then
0025     closureModeStr="noTest"
0026 fi
0027 
0028 jetSample=$1
0029 chunkSize=10
0030 
0031 rawDir="$baseDir/Jet${jetSample}"
0032 workDir="$baseDir/.merge_tmp_Jet${jetSample}-${closureModeStr}"
0033 finalOut="$baseDir/${fileBase}_Jet${jetSample}-${closureModeStr}.root"
0034 #finalOut="$baseDir/${fileBase}_Jet${jetSample}-all.root"
0035 
0036 mkdir -p "$workDir"
0037 rm -f -- "$workDir"/*.root
0038 
0039 extract_range() {
0040     local base="$1"
0041 
0042     # Raw files
0043     if [[ $base =~ ^${fileBase}_Jet${jetSample}_seg([0-9]{6})_to_([0-9]{6})-${closureModeStr}\.root$ ]];
0044     #if [[ $base =~ ^${fileBase}_Jet${jetSample}_seg([0-9]{6})_to_([0-9]{6})\.root$ ]];
0045     then
0046         printf '%s %s\n' "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"
0047         return 0
0048     fi
0049 
0050     # Intermediate files
0051     if [[ $base =~ ^merged_Jet${jetSample}_([0-9]{6})_to_([0-9]{6})-${closureModeStr}\.root$ ]];
0052     #if [[ $base =~ ^merged_Jet${jetSample}_([0-9]{6})_to_([0-9]{6})\.root$ ]];
0053     then
0054         printf '%s %s\n' "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"
0055         return 0
0056     fi
0057 
0058     echo "ERROR: cannot parse range from: $base" >&2
0059     return 1
0060 }
0061 
0062 current=( "$rawDir/${fileBase}_Jet${jetSample}_"*"-${closureModeStr}.root" )
0063 #current=( "$rawDir/${fileBase}_Jet${jetSample}_"*".root" )
0064 
0065 if (( ${#current[@]} == 0 ));
0066 then
0067     echo "ERROR: no input files found" >&2
0068     exit 1
0069 fi
0070 
0071 delete_current=false
0072 round=0
0073 
0074 while (( ${#current[@]} > 1)); do
0075     echo "Round $round: ${#current[@]} inputs"
0076 
0077     next=()
0078 
0079     for ((i=0; i<${#current[@]}; i+=chunkSize)); do
0080         chunk=( "${current[@]:i:chunkSize}" )
0081 
0082         firstBase=$(basename "${chunk[0]}")
0083         lastIndex=$((${#chunk[@]} - 1))
0084         lastBase=$(basename "${chunk[$lastIndex]}")
0085 
0086         read -r firstNum _ < <(extract_range "$firstBase")
0087         read -r _ secondNum < <(extract_range "$lastBase")
0088         
0089         outfile="$workDir/merged_Jet${jetSample}_${firstNum}_to_${secondNum}-${closureModeStr}.root"
0090         #outfile="$workDir/merged_Jet${jetSample}_${firstNum}_to_${secondNum}.root"
0091 
0092         echo "   Round $round: hadd -f $outfile"
0093         hadd -f "$outfile" "${chunk[@]}"
0094 
0095         next+=( "$outfile" )
0096     done
0097 
0098     echo "Done with round $round"
0099     
0100     printf 'Next round inputs:\n%s\n' "${next[@]}"
0101 
0102     if (( round > 0 )); then
0103         rm -f -- "${current[@]}"
0104     fi
0105 
0106     echo "Round $round produced ${#next[@]} merged files"
0107     current=( "${next[@]}" )
0108     echo "After round $round, current has ${#current[@]} files"
0109     ((++round))
0110 done
0111 
0112 mv -f -- "${current[0]}" "$finalOut"
0113 rm -rf -- "$workDir"
0114 
0115 echo "Final file: $finalOut"