Skip to content

Instantly share code, notes, and snippets.

@Nepomuk
Last active August 29, 2015 14:16
Show Gist options
  • Save Nepomuk/588832a90824b634ad01 to your computer and use it in GitHub Desktop.
Save Nepomuk/588832a90824b634ad01 to your computer and use it in GitHub Desktop.
Recursively add multiple root data files into one.
#!/bin/bash
prefix="cascade"
dataDir="data/prometheus/"
subDir=""
postfixes="pid_complete
reco_complete
cascade_ana-0"
tempFile="temp.root"
printHelp() {
echo "./combineData.sh <subDir> <prefix> [filesAtOnce]"
echo
echo "Merging files in directory ${dataDir}<subDir>. Files should start"
echo "with <prefix>_[0..9]. Optionally, you can choose how many files"
echo "should be merged at once (default: 1)."
exit
}
if test "$1" != ""; then
subDir=$1
else
printHelp
fi
if test "$2" != ""; then
prefix=$2
else
printHelp
fi
filesAtOnce="1"
if test "$3" != ""; then
if [[ $3 =~ ^[0-9]+$ ]]; then
filesAtOnce=$3
fi
fi
cd $dataDir/$subDir
for postfix in $postfixes; do
files=`ls ${prefix}_*_${postfix}.root`
filesArray=( $files )
arraylength=${#filesArray[@]}
echo
echo "Merging ${arraylength} files for ${postfix}."
master="${prefix}__master__${postfix}.root"
currentEntryList=()
for (( i = 0; i < ${arraylength}; i+=1));
do
currentEntry=${filesArray[0]}
currentEntryList+=($currentEntry)
if [[ ${#currentEntryList[@]} -ge $filesAtOnce || $((i+1)) -eq $arraylength ]]; then
if [[ "$i" -lt $filesAtOnce ]]; then
echo "hadd -f $master ${currentEntryList[@]}"
hadd -f $master ${currentEntryList[@]}
else
# echo "mv $master 'temp.root'"
mv $master "temp.root"
echo "hadd -f $master ${currentEntryList[@]} $tempFile"
hadd -f $master ${currentEntryList[@]} "temp.root"
fi
currentEntryList=()
fi
filesArray=( ${filesArray[@]/$currentEntry} )
done
if [[ -e $tempFile ]]; then
rm $tempFile
fi
echo
echo "Merging done. Final file written: ${master}"
echo
echo "------------------------------------------"
done
@Nepomuk
Copy link
Author

Nepomuk commented Mar 14, 2015

An input-file based version is over here: https://gist.github.com/AndiH/121e3d89fc647fb65338

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment