Last active
August 29, 2015 14:16
-
-
Save Nepomuk/588832a90824b634ad01 to your computer and use it in GitHub Desktop.
Recursively add multiple root data files into one.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An input-file based version is over here: https://gist.github.com/AndiH/121e3d89fc647fb65338