-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Expand file tree
/
Copy pathPrepareSherpaLibs.sh
More file actions
executable file
·479 lines (426 loc) · 17.3 KB
/
PrepareSherpaLibs.sh
File metadata and controls
executable file
·479 lines (426 loc) · 17.3 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/bin/bash
#
# file: PrepareSherpaLibs.sh
# description: prepare SHERPA libraries and cross sections for local use
# generate CMSSW python script
# uses: SHERPA datacards, libs and cross sections
#
# author: Markus Merschmeyer, Sebastian Thüer
# III. Physics Institute A, RWTH Aachen University
# date: 15th August 2012
# version: 4.2
# +-----------------------------------------------------------------------------------------------+
# function definitions
# +-----------------------------------------------------------------------------------------------+
function print_help() {
echo "" && \
echo "PrepareSherpaLibs version 4.2" && echo && \
echo "options: -i path path to SHERPA datacard, library & cross section files" && \
echo " can also be in WWW (http://...) or SE (srm://...)" && \
echo " -> ( "${datadir}" )" && \
echo " -p process SHERPA dataset/process name ( "${dataset}" )" && \
## echo " -m mode CMSSW running mode ( "${imode}" )" && \
## echo " [ 'PROD' : for production validation ]" && \
## echo " [ 'LOCAL' : local running of CMSSW ]" && \
## echo " -a path user analysis path inside CMSSW ( "${MYANADIR}" )" && \
echo " -D filename (optional) name of data card file ( "${cfdc}" )" && \
echo " -L filename (optional) name of library file ( "${cflb}" )" && \
echo " -C filename (optional) name of cross section file ( "${cfcr}" )" && \
echo " -G filename (optional) name of MI grid file ( "${cfgr}" )" && \
echo " -e filename (optional) name of extended weight list file ( "${weightfile}" ) " && \
echo " (example file in GeneratorInterface/SherpaInterface/python/ExtendedSherpaWeights_cfi.py)" && \
## echo " -P SRM path (CRAB) SE path for final results" && \
## echo " -> ( "${MYSRMPATH}" )" && \
echo " -H hepmcversion (optional) HepMC version, default is HepMC2 ( "${vhepmc}" )" && \
echo " -h display this help and exit" && echo
}
# function to build a python script for cmsDriver
function build_python_cff() {
## imode=$1 # mode (PRODuction, LOCAL, CRAB, ...)
cfffilename=$1 # config file name
process=$2 # process name
checksum=$3 # MD5 checksum
hepmc_version=$4 # HepMC version (HepMC2 or HepMC3)
if [ -e ${cfffilename} ]; then rm ${cfffilename}; fi
touch ${cfffilename}
echo "import FWCore.ParameterSet.Config as cms" >> ${cfffilename}
echo "import os" >> ${cfffilename}
if [ ! $weightlist == "" ]; then
echo "from $weightlist import *" >> ${cfffilename}
fi
echo "" >> ${cfffilename}
echo "source = cms.Source(\"EmptySource\")" >> ${cfffilename}
echo "" >> ${cfffilename}
if [ $hepmc_version == "HepMC3" ]; then
echo "generator = cms.EDFilter(\"SherpaHepMC3GeneratorFilter\"," >> ${cfffilename}
elif [ $hepmc_version == "HepMC2" ]; then
echo "generator = cms.EDFilter(\"SherpaGeneratorFilter\"," >> ${cfffilename}
else
echo "[ EXIT ]: Unsupported HepMC version: $hepmc_version. Please use HepMC3 or HepMC2."
exit 1
fi
echo " maxEventsToPrint = cms.int32(0)," >> ${cfffilename}
echo " filterEfficiency = cms.untracked.double(1.0)," >> ${cfffilename}
echo " crossSection = cms.untracked.double(-1)," >> ${cfffilename}
echo " SherpaProcess = cms.string('"${process}"')," >> ${cfffilename}
echo " SherpackLocation = cms.string('./')," >> ${cfffilename}
echo " SherpackChecksum = cms.string('"${checksum}"')," >> ${cfffilename}
echo " FetchSherpack = cms.bool(False)," >> ${cfffilename}
## if [ "${imode}" = "PROD" ]; then
echo " SherpaPath = cms.string('./')," >> ${cfffilename}
echo " SherpaPathPiece = cms.string('./')," >> ${cfffilename}
## elif [ "${imode}" = "LOCAL" ]; then
## echo " SherpaPath = cms.string(os.getcwd())," >> ${cfffilename}
## echo " SherpaPathPiece = cms.string(os.getcwd())," >> ${cfffilename}
## fi
echo " SherpaResultDir = cms.string('Result')," >> ${cfffilename}
echo " SherpaDefaultWeight = cms.double(1.0)," >> ${cfffilename}
if [ ! $weightlist == "" ]; then
echo " SherpaWeightsBlock = SherpaWeightsBlock," >> ${cfffilename}
fi
echo " SherpaParameters = cms.PSet(parameterSets = cms.vstring(" >> ${cfffilename}
fcnt=0
for file in `ls *.dat`; do
let fcnt=${fcnt}+1
done
for file in `ls *.dat`; do
let fcnt=${fcnt}-1
pstnam=`echo ${file} | cut -f1 -d"."`
if [ ${fcnt} -gt 0 ]; then
echo " \""${pstnam}"\"," >> ${cfffilename}
else
echo " \""${pstnam}"\")," >> ${cfffilename}
fi
done
for file in `ls *.dat`; do
pstnam=`echo ${file} | cut -f1 -d"."`
echo " "${pstnam}" = cms.vstring(" >> ${cfffilename}
cp ${file} ${file}.tmp1
sed -e 's/[%\!].*//g' < ${file}.tmp1 > ${file}.tmp2 # remove comment lines (beginning with % or !)
mv ${file}.tmp2 ${file}.tmp1
sed -e 's/^[ \t]*//;s/[ \t]*$//' < ${file}.tmp1 > ${file}.tmp2 # remove beginnig & trailing whitespaces
mv ${file}.tmp2 ${file}.tmp1
sed '/^$/d' < ${file}.tmp1 > ${file}.tmp2 # remove empty lines
mv ${file}.tmp2 ${file}.tmp1
#sed -e 's/^/ /g;s/ (/(/;s/ }/}/' < ${file}.tmp1 > ${file}.tmp2 # add single space in front of parameters
sed -e 's/^/ /g;s/ }/}/' < ${file}.tmp1 > ${file}.tmp2 # add single space in front of parameters
mv ${file}.tmp2 ${file}.tmp1
###
sed -e 's/\"/\\"/g' < ${file}.tmp1 > ${file}.tmp2 # protect existing '"' by '\"'
mv ${file}.tmp2 ${file}.tmp1
###
sed -e 's/^/\t\t\t\t"/;s/$/\",/' < ${file}.tmp1 > ${file}.tmp2 # add ([]") and ("') around parameters
mv ${file}.tmp2 ${file}.tmp1
sed -e '$s/\",/\"/' < ${file}.tmp1 > ${file}.tmp2 # fix last line
mv ${file}.tmp2 ${file}.tmp1
cat ${file}.tmp1 >> ${cfffilename}
echo " )," >> ${cfffilename}
rm ${file}.tmp*
done
echo " )" >> ${cfffilename}
echo ")" >> ${cfffilename}
echo "" >> ${cfffilename}
# echo "ProducerSourceSequence = cms.Sequence(generator)" >> ${cfffilename}
echo "ProductionFilterSequence = cms.Sequence(generator)" >> ${cfffilename}
echo "" >> ${cfffilename}
# cat > sherpa_custom_cff.py << EOF
#import FWCore.ParameterSet.Config as cms
#
#def customise(process):
#
# process.genParticles.abortOnUnknownPDGCode = False
#
# return(process)
#EOF
}
# function to copy files from different locations (WWW, SE, local)
function file_copy() {
# $1 : target path
# $2 : file name
# $3 : destination path
#
# srmopt=" -debug -streams_num=1 "
srmopt=" -streams_num=1 "
#
tpath="aaa/"
fname="xxx.yyy"
dpath="bbb/"
if [ $# -ge 1 ]; then
tpath=$1
if [ $# -ge 2 ]; then
fname=$2
if [ $# -ge 3 ]; then
dpath=$3
fi
fi
fi
#
if [ ! "${tpath}" = "${dpath}" ]; then
if [ "${tpath}" = "./" ]; then
tpath=${PWD}
fi
if [ "${dpath}" = "./" ]; then
dpath=${PWD}
fi
cd /tmp
#
TLOC0=`echo ${tpath} | cut -f1 -d "/" | grep -c -i http`
TLOC1=`echo ${tpath} | cut -f1 -d "/" | grep -c -i srm`
if [ ${TLOC0} -gt 0 ]; then # file is in WWW
echo " <I> retrieving WWW file: "${fname}
echo " <I> from path: "${tpath}
wget ${tpath}/${fname}
elif [ ${TLOC1} -gt 0 ]; then # file is in SE
echo " <I> retrieving SE file: "${fname}
echo " <I> from path: "${tpath}
srmcp ${srmopt} ${tpath}/${fname} file:///${PWD}/${fname}
else # local file
echo " <I> copying local file: "${fname}
echo " <I> from path: "${tpath}
if [ -e ${tpath}/${fname} ]; then
cp ${tpath}/${fname} ./
else
echo " <W> file does not exist, make sure everything is OK"
fi
fi
#
DLOC0=`echo ${dpath} | cut -f1 -d "/" | grep -c -i http`
DLOC1=`echo ${dpath} | cut -f1 -d "/" | grep -c -i srm`
if [ ${DLOC1} -gt 0 ]; then # file has to go to SE
echo " <I> storing SE file: "${fname}
echo " <I> to path: "${dpath}
srmcp ${srmopt} file:///${PWD}/${fname} ${dpath}/${fname}
else # local file
echo " <I> copying local file: "${fname}
echo " <I> to path: "${dpath}
if [ -e ${fname} ]; then
cp ${fname} ${dpath}/
else
echo " <W> file does not exist, make sure everything is OK"
fi
fi
#
rm -f ${fname}
cd -
fi
}
function parse_extended_weight() {
if [ -e $CMSSW_BASE/src/$weightlist ]
then
weightlist=$(echo "$weightlist" | sed 's:/python/:.:g' | sed 's:/:.:g' | sed 's:.py::g' )
else
echo '$CMSSW_BASE/src/$weightlist does not exist.'
exit 1
fi
}
# +-----------------------------------------------------------------------------------------------+
# start of the script
# +-----------------------------------------------------------------------------------------------+
# save current path name
HDIR=`pwd`
# 'guess' SHERPA dataset name
ip=1
cproc="XXX"
for file in `ls sherpa_*_libs.tgz`; do
if [ $ip -eq 1 ]; then
cproc=`echo $file | awk -F"sherpa_" '{print $2}' | awk -F"_libs" '{print $1}'`
fi
let ip=$ip+1
done
# dummy setup (if all options are missing)
datadir=${HDIR} # path to SHERPA datacards (libraries)
dataset=${cproc} # SHERPA dataset/process name
if [ -e ${CMSSW_BASE} ]; then
CMSSWDIR=${CMSSW_BASE} # CMSSW directory
##else
## if [ ! "${imode}" = "PROD" ];then
## echo " <E> \$CMSSW_BASE "${CMSSW_BASE}" does not exist"
## echo " <E> stopping..."
## exit 1
## fi
fi
##imode="PROD" # CMSSW running mode
##MYANADIR="A/B" # user analysis directory inside CMSSW
# # -> CMSSW_X_Y_Z/src/${MYANADIR}/
cfdc="" # custom data card file name
cflb="" # custom library file name
cfcr="" # custom cross section file name
cfgr="" # custom MI grid file name
weightfile="" # sherpa weights ordering file
vhepmc="HepMC2" # hepmc format version, default is HepMC2, you can change it to HepMC3
##MYSRMPATH="./" # SRM path for storage of results
TDIR=TMP
# get & evaluate options
##while getopts :i:p:d:m:a:D:L:C:G:P:h OPT
while getopts :i:p:d:D:L:C:G:e:H:h OPT
do
case $OPT in
i) datadir=$OPTARG ;;
p) dataset=$OPTARG ;;
## m) imode=$OPTARG ;;
## a) MYANADIR=$OPTARG ;;
D) cfdc=$OPTARG ;;
L) cflb=$OPTARG ;;
C) cfcr=$OPTARG ;;
G) cfgr=$OPTARG ;;
e) weightlist=$OPTARG && parse_extended_weight;;
H) vhepmc=$OPTARG ;;
## P) MYSRMPATH=$OPTARG ;;
h) print_help && exit 0 ;;
\?)
shift `expr $OPTIND - 1`
if [ "$1" = "--help" ]; then print_help && exit 0;
else
echo -n "PrepareSherpaLibs: error: unrecognized option "
if [ $OPTARG != "-" ]; then echo "'-$OPTARG'. try '-h'"
else echo "'$1'. try '-h'"
fi
print_help && exit 1
fi
shift 1
OPTIND=1
esac
done
# make sure to use absolute path names...
xpth=`echo ${datadir} | cut -f1 -d"/"`
if [ "$xpth" = "" ] || [ "$xpth" = "." ] || [ "$xpth" = ".." ] || [ "$xpth" = "~" ]; then
echo " <I> fixing data directory: "${datadir}
cd ${datadir} && datadir=`pwd`; cd ${HDIR}
echo " <I> to: "${datadir}
fi
# print current options/parameters
echo " -> data card directory '"${datadir}"'"
echo " -> dataset name '"${dataset}"'"
echo " -> HepMC version '"${vhepmc}"'"
##echo " -> operation mode: '"${imode}"'"
##echo " -> CMSSW user analysis path: '"${MYANADIR}"'"
# set up
##if [ "${imode}" = "PROD" ] || [ "${imode}" = "GRID" ]; then
MYCMSSWTEST=${HDIR}/${TDIR}
MYCMSSWPYTH=${HDIR}/${TDIR}
MYCMSSWSHPA=${HDIR}/${TDIR}
##else
## MYCMSSWTEST=${CMSSWDIR}/src/${MYANADIR}/test
## MYCMSSWPYTH=${CMSSWDIR}/src/${MYANADIR}/python
## MYCMSSWSHPA=${CMSSWDIR}/src/${MYANADIR}/test
## if [ ! -e ${MYCMSSWTEST} ]; then # create user analysis path
## mkdir -p ${MYCMSSWTEST}
## else
## rm -f ${CMSSWDIR}/python/${MYANADIR}/*.py* # ...clean up
## fi
## if [ ! -e ${MYCMSSWPYTH} ]; then # create 'python' subdirectory
## mkdir -p ${MYCMSSWPYTH}
## else
## rm -f ${MYCMSSWPYTH}/*.py* # ...clean up
## fi
##fi
# set SHERPA data file names
cardfile=sherpa_${dataset}_crdE.tgz
libsfile=sherpa_${dataset}_libs.tgz
crssfile=sherpa_${dataset}_crss.tgz
gridfile=sherpa_${dataset}_migr.tgz
if [ ! "${cfdc}" = "" ]; then cardfile=${cfdc}; fi
if [ ! "${cflb}" = "" ]; then libsfile=${cflb}; fi
if [ ! "${cfcr}" = "" ]; then crssfile=${cfcr}; fi
if [ ! "${cfgr}" = "" ]; then gridfile=${cfgr}; fi
##if [ ! "${imode}" = "CRAB" ]; then
# create dataset directory tree
if [ -e ${MYCMSSWSHPA} ]; then
echo " <W> dataset directory "${MYCMSSWSHPA}" exists"
echo " <W> ...removing..."
rm -rf ${MYCMSSWSHPA}
fi
mkdir -p ${MYCMSSWSHPA}
# get & unpack dataset files, generate .cff and .cfi files
cd ${MYCMSSWSHPA}
file_copy ${datadir} ${cardfile} ${PWD}
file_copy ${datadir} ${libsfile} ${PWD}
file_copy ${datadir} ${crssfile} ${PWD}
file_copy ${datadir} ${gridfile} ${PWD}
if [ -e ${cardfile} ]; then
tar -xzf ${cardfile} && rm ${cardfile}
else
echo " <E> file not found: "${cardfile}
exit 1
fi
if [ -e ${libsfile} ]; then
tar -xzf ${libsfile} && rm ${libsfile}
else
echo " <E> file not found: "${libsfile}
exit 1
fi
if [ -e ${crssfile} ]; then
tar -xzf ${crssfile} && rm ${crssfile}
else
echo " <E> file not found: "${crssfile}
exit 1
fi
if [ -e ${gridfile} ]; then
tar -xzf ${gridfile} && rm ${gridfile}
else
echo " <W> no MI grid file: "${gridfile}
fi
cd -
##fi
# initialize variables for python file generation
spsummd5=""
##if [ "${imode}" = "PROD" ] || [ "${imode}" = "LOCAL" ]; then
shpamstfile="sherpa_"${dataset}"_MASTER.tgz"
shpamstmd5s="sherpa_"${dataset}"_MASTER.md5"
shpacfffile="sherpa_"${dataset}"_MASTER_cff.py"
cd ${MYCMSSWSHPA}
tar -czf ${shpamstfile} *
####
md5sum ${shpamstfile} > ${shpamstmd5s}
spsummd5=`md5sum ${shpamstfile} | cut -f1 -d" "`
####
## build_python_cff ${imode} ${shpacfffile} ${dataset} ${spsummd5}
build_python_cff ${shpacfffile} ${dataset} ${spsummd5} ${vhepmc}
mv ${shpamstfile} $HDIR
mv ${shpamstmd5s} $HDIR
mv ${shpacfffile} $HDIR
cd $HDIR
rm -rf $TDIR
##fi
echo " <I> "
echo " <I> "
echo " <I> "
echo " <I> generated sherpack:"
echo " <I> "${shpamstfile}
echo " <I> generated python fragment:"
echo " <I> "${shpacfffile}
echo " <I> MD5 checksum file:"
echo " <I> "${shpamstmd5s}
echo " <I> "
echo " <I> "
echo " <I> "
echo " <I> ATTENTION,"
echo " <I> please edit the generated python fragment according to your needs:"
echo " <I> "
echo " <I> for a local production edit the line"
echo " <I> SherpackLocation = cms.string('[local path to your sherpack]')"
echo " <I> and make sure that the SherpaInterface does not try to fetch the sherpack"
echo " <I> FetchSherpack = cms.bool(False)"
echo " <I> "
echo " <I> "
echo " <I> for a production using the frontier database use"
echo " <I> SherpackLocation = cms.string('[frontier database path to your sherpack]')"
echo " <I> and this time make sure that the SherpaInterface fetches the sherpack"
echo " <I> FetchSherpack = cms.bool(true)"
echo " <I> "
echo " <I> "
echo " <I> for a production with CRAB add the sherpack to the list of additional files"
echo " <I> additional_input_files = [name of the ..._MASTER.tgz sherpack]"
echo " <I> make sure that the sherpack location is"
echo " <I> SherpackLocation = cms.string('./')"
echo " <I> and make sure that the SherpaInterface does not try to fetch the sherpack"
echo " <I> FetchSherpack = cms.bool(False)"
echo " <I> "
echo " <I> "
echo " <I> "
echo " <I> a good way to test the generated python file is to cross-check it with cmsDriver.py:"
echo " cmsDriver.py A/B/python/"${shpacfffile}" \\"
echo " -s GEN -n 100 --no_exec --conditions auto:mc --eventcontent RAWSIM"
echo " <I> "
echo " <I> "
echo " <I> "