-
Notifications
You must be signed in to change notification settings - Fork 13
/
copyPrivateLHEs.sh
executable file
·164 lines (140 loc) · 3.82 KB
/
copyPrivateLHEs.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/bash
################################
#
# copyPrivateLHEs.py
#
# Script to copy privately produced LHE files to EOS and store their article
# in a CSV file to be used by manageRequests.py.
#
# author: David G. Sheffield (Rutgers)
#
################################
IFS=,
inputFile=""
outputFile=""
force=0
add=0
rename=0
while [ "$1" != "" ]; do
case $1 in
-o) shift
outputFile=$1
;;
-f) force=1
;;
-a) add=1
;;
-r) rename=1
;;
-h | --help) echo "Usage: sh copyPrivateLHEs.sh inputFile [-o outputFile] [-f]"
echo " inputFile CSV file with location of LHE files."
echo " -o outputFile CSV file to store EOS article numbers and values from inputFile."
echo " Default: inputFile=file.lhe produces outputFile=file_EOS.lhe."
echo " -f Force outputFile to be overwritten."
echo " -a Add new rows to outputFile if it already exists."
echo " -r Rename LHE files to dataset name."
exit
;;
*) inputFile=$1
;;
esac
shift
done
if [ "${inputFile}" == "" ]; then
echo "Error: No input file provided."
exit 1
fi
if [ ! -f $inputFile ]; then
echo "Error: The file ${inputFile} does not exist."
exit 1
fi
if [ "${outputFile}" == "" ]; then
outputFile=$(echo $inputFile | sed 's/\.csv$/_EOS.csv/')
elif [ "${inputFile}" == "${outputFile}" ]; then
echo "Error: Cannot have both output and input files be ${inputFile}"
exit 1
fi
if [ -f $outputFile ]; then
if [ $force == 1 ]; then
rm $outputFile
elif [ $add != 1 ]; then
echo "Error: The output file ${outputFile} already exists."
echo " Use the flag -f to force overwrite it or -a to add to it."
exit 1
fi
elif [ $add == 1 ]; then
echo "Error: The output file ${outputFile} does not exist. Cannot add to it."
exit 1
fi
echo "Copying $(($(wc -l < ${inputFile}) - 1)) LHE files to EOS"
home_dir=$PWD
inputFile="${home_dir}/${inputFile}"
outputFile="${home_dir}/${outputFile}"
work_dir="tmp_pLHE_work"
mkdir ${work_dir}
cd ${work_dir}
ind=-1
ind_dataset=-1
failures=0
isFirstLine=1
while read -a line; do
if [ $isFirstLine == 1 ]; then
for i in "${!line[@]}"; do
if [ "${line[i]}" == "LHE" ]; then
ind=$i
elif [ "${line[i]}" == "Dataset name" ]; then
ind_dataset=$i
fi
if [ $add != 1 ]; then
echo -n "${line[i]}," >> $outputFile
fi
done
if [ $add != 1 ]; then
echo "EOS" >> $outputFile
fi
if [ $ind == -1 ]; then
echo 'Error: Could not find "LHE" in ${inputFile}.'
exit 2
fi
if [ $rename == 1 ] && [ $ind_dataset == -1 ]; then
echo 'Error: Could not find "Dataset name" in ${inputFile}.'
fi
isFirstLine=0
continue
fi
for i in "${!line[@]}"; do
echo -n "${line[i]}," >> $outputFile
done
file=${line[ind]}
name=$(basename $file)
if [ $rename == 1 ]; then
name="${line[ind_dataset]}.lhe"
fi
cp $file $name
copy_output="tmpoutput"
cmsLHEtoEOSManager.py -n -c -f $name > $copy_output
article_id=$(grep -Po "(?<=Creating new article with identifier )\d*" $copy_output)
if [ "${article_id}" == "" ]; then
echo -e "\033[1;31mFailed to copy ${name}.\033[1;m"
echo "" >> $outputFile
failures=$((failures + 1))
else
if [ $(grep "Checksum OK" $copy_output) ]; then
echo -e "\033[1;32mCopied ${name} to ${article_id}.\033[1;m"
echo "${article_id}" >> $outputFile
else
echo -e "\033[1;31mFailed to correctly copy ${name} to ${article_id}. Checksums do not match.\033[1;m"
echo "BAD${article_id}CHECKSUM" >> $outputFile
failures=$((failures + 1))
fi
fi
name_xz="${name}.xz"
rm $name_xz
done < $inputFile
cd $home_dir
rm -r ${work_dir}
if [ $failures == 0 ]; then
echo "Successfully copied all LHE files."
else
echo "Failed to copy ${failures} LHE files."
fi