Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-12-17 09:15:26

0001 #!/usr/bin/env bash
0002 set -euo pipefail
0003 
0004 # 1) Read runs from runlist.txt (one run per line, integers only)
0005 echo " "
0006 echo "Starting do_cosmics_calibration.sh"
0007 
0008 if [[ ! -s runlist.txt ]]; then
0009   echo "ERROR: runlist.txt missing or empty" >&2
0010   exit 1
0011 fi
0012 
0013 mapfile -t run_array < <(awk 'NF && $1 ~ /^[0-9]+$/ {print $1}' runlist.txt)
0014 
0015 if (( ${#run_array[@]} == 0 )); then
0016   echo "ERROR: no valid run numbers in runlist.txt" >&2
0017   exit 1
0018 fi
0019 
0020 min_run=${run_array[0]}
0021 max_run=${run_array[0]}
0022 for r in "${run_array[@]}"; do
0023   (( r < min_run )) && min_run=$r
0024   (( r > max_run )) && max_run=$r
0025 done
0026 
0027 echo "Run range: ${min_run} - ${max_run}"
0028 
0029 mkdir -p hist
0030 mkdir -p output
0031 
0032 ohcal_partial=()
0033 ihcal_partial=()
0034 
0035 # 2) Per-run hadd for OHCal and IHCal
0036 echo " "
0037 echo "1) Processing hadd for production results:"
0038 echo " "
0039 for run in "${run_array[@]}"; do
0040 
0041   ########## OHCal: HIST_COSMIC_HCALOUT ##########
0042   ohcal_out="hist/ohcal_hist_${run}.root"
0043 
0044   if [[ -f "${ohcal_out}" ]]; then
0045     echo "    Skipping OHCal hadd run ${run} - output already exists"
0046     ohcal_partial+=("${ohcal_out}")
0047   else
0048     ohcal_files=$(
0049       psql -d FileCatalog -X -A -t -P pager=off -v ON_ERROR_STOP=1 -c "
0050         SELECT coalesce(string_agg(f.full_file_path, ' '), '')
0051         FROM public.datasets d
0052         JOIN public.files   f ON f.lfn = d.filename
0053         WHERE d.dsttype  = 'HIST_COSMIC_HCALOUT'
0054           AND d.runnumber = ${run};
0055       "
0056     )
0057 
0058     if [[ -n "${ohcal_files// }" ]]; then
0059       echo "    Processing OHCal hadd run ${run}"
0060       hadd -j -k "${ohcal_out}" ${ohcal_files} >/dev/null
0061       ohcal_partial+=("${ohcal_out}")
0062     else
0063       echo "    No OHCal files for run ${run}"
0064     fi
0065   fi
0066 
0067   ########## IHCal: HIST_COSMIC_HCALIN ##########
0068   ihcal_out="hist/ihcal_hist_${run}.root"
0069 
0070   if [[ -f "${ihcal_out}" ]]; then
0071     echo "    Skipping IHCal hadd run ${run} - output already exists"
0072     ihcal_partial+=("${ihcal_out}")
0073   else
0074     ihcal_files=$(
0075       psql -d FileCatalog -X -A -t -P pager=off -v ON_ERROR_STOP=1 -c "
0076         SELECT coalesce(string_agg(f.full_file_path, ' '), '')
0077         FROM public.datasets d
0078         JOIN public.files   f ON f.lfn = d.filename
0079         WHERE d.dsttype  = 'HIST_COSMIC_HCALIN'
0080           AND d.runnumber = ${run};
0081       "
0082     )
0083 
0084     if [[ -n "${ihcal_files// }" ]]; then
0085       echo "    Processing IHCal hadd run ${run}"
0086       hadd -j -k "${ihcal_out}" ${ihcal_files} >/dev/null
0087       ihcal_partial+=("${ihcal_out}")
0088     else
0089       echo "No IHCal files for run ${run}"
0090     fi
0091   fi
0092 
0093 done
0094 
0095 # 3) Final hadd across per-run ROOTs
0096 # Name final outputs with the min and max run numbers
0097 echo " "
0098 if (( ${#ohcal_partial[@]} > 0 )); then
0099   echo "Processing final OHCal hadd"
0100   ohcal_final="hist/ohcal_hist_${min_run}_${max_run}.root"
0101   hadd -j -k -f "${ohcal_final}" "${ohcal_partial[@]}" >/dev/null
0102   echo "Final OHCal hadd output: ${ohcal_final}"
0103 else
0104   echo "No OHCal files processed. Done."
0105 fi
0106 
0107 if (( ${#ihcal_partial[@]} > 0 )); then
0108   echo "Processing final IHCal hadd"
0109   ihcal_final="hist/ihcal_hist_${min_run}_${max_run}.root"
0110   hadd -j -k -f "${ihcal_final}" "${ihcal_partial[@]}" >/dev/null
0111   echo "Final IHCal hadd output: ${ihcal_final}"
0112 else
0113   echo "No IHCal files processed. Done."
0114 fi
0115 
0116 # 4) Run function_fitting.C for ihcal and ohcal
0117 echo " "
0118 echo "2) Running function_fitting.C for IHCal and OHCal to get MPV values"
0119 root -l -q -b "script/function_fitting.C(${min_run},${max_run},\"ihcal\")"
0120 root -l -q -b "script/function_fitting.C(${min_run},${max_run},\"ohcal\")"
0121 
0122 # 3) Run readhcalcalib.C (no arguments)
0123 echo " "
0124 echo "3) Running readhcalcalib.C to read pre-calibration constants (Need already prepared ohcal_precalib.root and ihcal_precalib.root from CDB)"
0125 root -l -q -b 'script/readhcalcalib.C'
0126 
0127 # 4) Run get_calibration_factor.C(min_run, max_run)
0128 echo " "
0129 echo "4) Running get_calibration_factor.C to compute new calibration factors"
0130 root -l -q -b "script/get_calibration_factor.C(${min_run},${max_run})"
0131 echo "Done. New calibration factor files have been saved to output/"
0132 
0133 # 5) Run get_CDBTree.C(min_run, max_run)
0134 echo " "
0135 echo "5) Running get_CDBTree.C to form CDB Trees with new calibration factors"
0136 root -l -q -b "script/get_CDBTree.C(${min_run},${max_run})"
0137 echo "Done. Calibration factor files and CDB TTrees have been saved to output/"