ROOTPWA
calcAmplitudesForMassBin.sh
Go to the documentation of this file.
1 #!/bin/bash
2 ##########################################################################
3 #
4 # Copyright 2010
5 #
6 # This file is part of rootpwa
7 #
8 # rootpwa is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # rootpwa is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with rootpwa. If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##########################################################################
22 #-------------------------------------------------------------------------
23 # File and Version Information:
24 # $Rev:: $: revision of last commit
25 # $Author:: $: author of last commit
26 # $Date:: $: date of last commit
27 #
28 # Description:
29 # calculates amplitudes for a single mass bin specified by
30 # (1-based) index MASS_BIN_INDEX
31 #
32 # runs locally as well as on a batch system
33 #
34 # job arguments may be passed via command line or environment
35 # variable
36 #
37 # a typical command line to run this script locally looks like this:
38 # for i in $(seq 1 50);\
39 # do calcAmplitudesForMassBin.sh ${i} &> ${ROOTPWA_LOGS_DIR}/calcAmplitudes.${i}.log;\
40 # done
41 #
42 # uses ROOTPWA environment variables
43 # ROOTPWA_ENV_SET
44 # ROOTPWA_BIN
45 # ROOTPWA_KEYS_DIR
46 # ROOTPWA_DATA_DIR
47 #
48 #
49 # Author List:
50 # Boris Grube TUM (original author)
51 #
52 #
53 #-------------------------------------------------------------------------
54 
55 
56 # file layout parameters
57 # subdirectory names for amplitudes
58 DT_AMP_DIR_NAME=AMPS
59 PS_AMP_DIR_NAME=PSPAMPS
60 PSACC_AMP_DIR_NAME=ACCAMPS
61 # extensions of event data files; naming scheme is <mass bin><extension>
62 DT_FILE_EXT=.evt
63 PS_FILE_EXT=.genbod.evt
64 #PS_FILE_EXT=.ps.evt
65 PSACC_FILE_EXT=.acc.evt
66 #DT_FILE_EXT=.root
67 #PS_FILE_EXT=.ps.root
68 #PSACC_FILE_EXT=.acc.root
69 # amplitude file format
70 AMP_FILE_EXT=.amp
71 #AMP_FILE_EXT=.root
72 # integral file name
73 INT_FILE_NAME=norm.int
74 #INT_FILE_NAME=norm.root
75 
76 
77 # SGE setup
78 # if SGE is used as batch system, job arrays can be conveniently used
79 # to run this script; MASS_BIN_INDEX is set to SGE_TASK_ID
80 # define queue
81 ##$ -l medium=TRUE,h_vmem=200M
82 # path for stdout files
83 ##$ -o /afs/e18.ph.tum.de/user/bgrube/compass/pwa/4PionMuoProdPwa/logs
84 # merge sterr into stdout
85 #$ -j yes
86 # send mail when job is aborted, rescheduled, or suspended
87 ##$ -M bgrube
88 ##$ -m as
89 # enable path aliasing
90 #$ -cwd
91 # export all environment variables to job
92 #$ -V
93 
94 
95 echo ">>> info: called ${0} ${*}"
96 
97 
98 function usage {
99  cat << EOF
100 usage: $(basename ${0}) [OPTIONS]... [MASS BIN INDEX]
101 
102 creates amplitude and integral files for real data, phase-space MC,
103 and accepted phase-space MC in mass bin directory given by 1-based
104 index [MASS BIN INDEX] by running 'calcAmplitudes' and 'calcIntegrals'
105 
106 options:
107  -${KEY_PATTERN_OPT} '<PATTERN>' glob pattern that defines set of key files to be processed
108  -${MASS_BINS_DIR_OPT} <DIR> path to directory with mass bins
109  -${PDG_TABLE_OPT} <FILE> path to PDG table file
110  -${HELP_OPT} display this help and exit
111 EOF
112 echo
113 printPar
114 if [[ "${#}" -eq 1 ]]
115 then
116  exit ${1}
117 else
118  exit 1
119 fi
120 }
121 
122 
123 function printPar {
124  cat << EOF
125 parameter values:
126  glob pattern that defines set of key files ... -${KEY_PATTERN_OPT} '${KEY_PATTERN}'
127  path to directory with mass bins ............. -${MASS_BINS_DIR_OPT} '${MASS_BINS_DIR}'
128  path to PDG table file ....................... -${PDG_TABLE_OPT} '${PDG_TABLE}'
129 
130  mass bin index ............................... ${MASS_BIN_INDEX}
131 EOF
132 }
133 
134 
135 function runCalcAmplitudes {
136  local _DT_FILE="${1}"
137  local _AMP_DIR="${2}"
138  local _NMB_OF_KEYS=$(eval ls -1 ${KEY_PATTERN} | wc -l)
139  # process all key files
140  declare -i _COUNT_KEY=0
141  for _KEY_FILE in ${KEY_PATTERN}
142  do
143  local _KEY_NAME=$(basename ${_KEY_FILE})
144  local _AMP_FILE=${_AMP_DIR}/${_KEY_NAME/.key/${AMP_FILE_EXT}}
145  (( ++_COUNT_KEY ))
146  echo
147  echo ">>> info: generating amplitudes for '${_KEY_NAME}' [${_COUNT_KEY}/${_NMB_OF_KEYS}]"
148  # don't overwrite existing files
149  if [[ -s "${_AMP_FILE}" ]]
150  then
151  echo "??? warning: file '${_AMP_FILE}' already exists. skipping."
152  else
153  local _CMD="${ROOTPWA_BIN}/calcAmplitudes -k ${_KEY_FILE} -p ${PDG_TABLE} -o ${_AMP_FILE} ${_DT_FILE}"
154  echo "${_CMD}"
155  time eval ${_CMD}
156  fi
157  done
158 }
159 
160 
161 function runInt {
162  local _AMP_DIR="${1}"
163  echo
164  echo ">>> info: generating integrals for '${_AMP_DIR}'"
165  local _CURRENT_DIR=$(pwd)
166  # avoid problems with full path names in pwa2000
167  cd "${_AMP_DIR}"
168  local _CMD="${ROOTPWA_BIN}/int *.amp > ${INT_FILE_NAME}" # works only with .amp and .int files
169  echo "${_CMD}"
170  time eval ${_CMD}
171  cd ${_CURRENT_DIR}
172 }
173 
174 
175 function runCalcIntegrals {
176  local _AMP_DIR="${1}"
177  echo
178  echo ">>> info: generating integrals for '${_AMP_DIR}'"
179  local _CMD="${ROOTPWA_BIN}/calcIntegrals -o ${_AMP_DIR}/${INT_FILE_NAME} ${_AMP_DIR}/*${AMP_FILE_EXT}"
180  echo "${_CMD}"
181  time eval ${_CMD}
182 }
183 
184 
185 function runPhaseSpaceGen {
186  local _NMB_EVENTS=50000
187  local _SEED=1234567890
188  local _PS_FILE="${1}"
189  local _MASS_BIN_NAME=$(basename "${_PS_FILE}" "${PS_FILE_EXT}")
190  local _BIN_M_MIN=${_MASS_BIN_NAME%.*}
191  local _BIN_M_MAX=${_MASS_BIN_NAME#*.}
192  echo
193  echo ">>> info: generating ${_NMB_EVENTS} phase space events for mass bin [${_BIN_M_MIN}, ${_BIN_M_MAX}] MeV/c^2"
194  local _CMD="root -b -q -l \"runPhaseSpaceGen.C(${_NMB_EVENTS}, \\\"${_PS_FILE}\\\", ${_BIN_M_MIN}, ${_BIN_M_MAX}, ${_SEED})\""
195  echo "${_CMD}"
196  time eval ${_CMD}
197 }
198 
199 
200 # take arguments either from command line, environment, or use default
201 # (in this order of priority)
202 # option variables
203 KEY_PATTERN_OPT="k"
204 MASS_BINS_DIR_OPT="m"
205 PDG_TABLE_OPT="p"
206 HELP_OPT="h"
207 # use default values, if variables are not defined in environment
208 if [[ -z "${KEY_PATTERN}" && ! -z "${ROOTPWA_KEYS_DIR}" ]]
209 then
210  KEY_PATTERN="${ROOTPWA_KEYS_DIR}/*.key"
211 fi
212 if [[ -z "${MASS_BIN_INDEX}" ]]
213 then
214  MASS_BIN_INDEX="1"
215 fi
216 # override MASS_BIN_INDEX with SGE_TASK_ID
217 if [[ -n "${SGE_TASK_ID}" ]]
218 then
219  MASS_BIN_INDEX="${SGE_TASK_ID}"
220 fi
221 if [[ -z "${MASS_BINS_DIR}" ]]
222 then
223  MASS_BINS_DIR="${ROOTPWA_DATA_DIR}"
224 fi
225 if [[ -z "${PDG_TABLE}" ]]
226 then
227  PDG_TABLE="${ROOTPWA}/amplitude/particleDataTable.txt"
228 fi
229 # parse command line options
230 while getopts "${KEY_PATTERN_OPT}:${MASS_BINS_DIR_OPT}:${PDG_TABLE_OPT}:${HELP_OPT}" OPTION
231 do
232  case ${OPTION} in
233  ${KEY_PATTERN_OPT}) KEY_PATTERN=${OPTARG};;
234  ${MASS_BINS_DIR_OPT}) MASS_BINS_DIR=${OPTARG};;
235  ${PDG_TABLE_OPT}) PDG_TABLE=${OPTARG};;
236  ${HELP_OPT}) usage 0;;
237  esac
238 done
239 shift $((${OPTIND} - 1)) # just leave remaining arguments in $*
240 if [[ -n "${1}" ]]
241 then
242  MASS_BIN_INDEX="${1}"
243 fi
244 if [[ -z "${KEY_PATTERN}" || -z "${MASS_BINS_DIR}" || -z "${PDG_TABLE}" || -z "${MASS_BIN_INDEX}" ]]
245 then
246  usage 1
247 fi
248 if [[ "${ROOTPWA_ENV_SET}" != "true" ]]
249 then
250  echo "!!! error: ROOTPWA environment is not setup. please source the ROOTPWA setup script first."
251  exit 1
252 fi
253 
254 
255 # convert all input paths to absolute paths
256 KEY_PATTERN=$(readlink --canonicalize-missing "${KEY_PATTERN}")
257 MASS_BINS_DIR=$(readlink --canonicalize-missing "${MASS_BINS_DIR}")
258 PDG_TABLE=$(readlink --canonicalize-missing "${PDG_TABLE}")
259 
260 
261 echo ">>> info: ${0} started on $(date)"
262 printPar && echo
263 
264 
265 # construct array of mass bins in ascending order
266 MASS_BINS=( $(find ${MASS_BINS_DIR} -type d -regex '.*/[0-9]+.[0-9]+' -printf '%f\n' | sort -n) )
267 if [[ -z "${MASS_BINS}" ]]
268 then
269  echo "!!! error: cannot find any mass bins in '${MASS_BINS_DIR}'"
270  exit 1
271 fi
272 # find right mass bin
273 MASS_BIN_DIR=${MASS_BINS[$(expr ${MASS_BIN_INDEX} - 1)]}
274 if [[ -z "${MASS_BIN_DIR}" ]]
275 then
276  echo "!!! error: cannot find mass bin with index ${MASS_BIN_INDEX}"
277  exit 1
278 fi
279 MASS_BIN_DIR=${MASS_BINS_DIR}/${MASS_BIN_DIR}
280 if [[ ! -d "${MASS_BIN_DIR}" ]]
281 then
282  echo "!!! error: mass bin directory '${MASS_BIN_DIR}' does not exist"
283  exit 1
284 fi
285 
286 
287 # define mass bin file structure
288 # name of mass bin
289 MASS_BIN_NAME=$(basename ${MASS_BIN_DIR})
290 # directory for amplitude files from real data
291 DT_AMP_DIR=${MASS_BIN_DIR}/${DT_AMP_DIR_NAME}
292 # path of real data file
293 DT_FILE=${MASS_BIN_DIR}/${MASS_BIN_NAME}${DT_FILE_EXT}
294 if [[ ! -e "${DT_FILE}" ]]
295 then
296  echo "!!! error: data file '${DT_FILE}' does not exist. exiting."
297  exit 1
298 fi
299 # directory for amplitude files from Monte-Carlo
300 PS_AMP_DIR=${MASS_BIN_DIR}/${PS_AMP_DIR_NAME}
301 # path of Monte-Carlo data file
302 PS_FILE=${MASS_BIN_DIR}/${MASS_BIN_NAME}${PS_FILE_EXT}
303 # directory for amplitude files from Monte-Carlo
304 PSACC_AMP_DIR=${MASS_BIN_DIR}/${PSACC_AMP_DIR_NAME}
305 # path of Monte-Carlo data file
306 PSACC_FILE=${MASS_BIN_DIR}/${MASS_BIN_NAME}${PSACC_FILE_EXT}
307 
308 
309 # check whether necessary file and directories are there
310 if [[ "$(eval ls -1 ${KEY_PATTERN} | wc -l)" == "0" ]]
311 then
312  echo "!!! error: no key files match glob pattern '${KEY_PATTERN}'. exiting."
313  exit 1
314 fi
315 if [[ ! -s "${PDG_TABLE}" ]]
316 then
317  echo "!!! error: PDG table file '${PDG_TABLE}' does not exist. exiting."
318  exit 1
319 fi
320 
321 
322 # calculate amplitudes
323 echo ">>> info: starting amplitude calculation for mass bin ${MASS_BIN_INDEX} in '${MASS_BIN_DIR}' using waveset(s) '${KEY_PATTERN}'"
324 echo ">>> info: $(eval ls -1 ${KEY_PATTERN} | wc -l) key files:"
325 eval ls -l ${KEY_PATTERN}
326 echo
327 echo ">>> info: ${0} started on $(date)"
328 
329 
330 echo "------------------------------------------------------------"
331 echo ">>> info: processing real data for mass bin '${MASS_BIN_DIR}'"
332 # create directory if necessary
333 if [[ ! -d "${DT_AMP_DIR}" ]]
334 then
335  mkdir --parents --verbose "${DT_AMP_DIR}"
336 fi
337 # generate amplitude files for real data
338 runCalcAmplitudes "${DT_FILE}" "${DT_AMP_DIR}"
339 echo
340 
341 
342 echo "------------------------------------------------------------"
343 echo ">>> info: processing phase-space Monte Carlo data for mass bin '${MASS_BIN_DIR}'"
344 # generate amplitude files for phase-space Monte Carlo data
345 if [[ ! -s "${PS_FILE}" ]]
346 then
347  echo "??? warning: phase-space MC data file '${PS_FILE}' does not exist"
348  # generate phase space for normalization integral
349  #runPhaseSpaceGen "${PS_FILE}"
350 fi
351 # create directory if necessary
352 if [[ ! -d "${PS_AMP_DIR}" ]]
353 then
354  mkdir --parents --verbose "${PS_AMP_DIR}"
355 fi
356 # generate amplitude files for phase-space MC data
357 runCalcAmplitudes "${PS_FILE}" "${PS_AMP_DIR}"
358 # perform integration for phase-space MC data
359 #runInt "${PS_AMP_DIR}"
360 runCalcIntegrals "${PS_AMP_DIR}"
361 echo
362 
363 
364 echo "------------------------------------------------------------"
365 echo ">>> info: processing accepted phase-space Monte-Carlo data for mass bin '${MASS_BIN_DIR}'"
366 # generate amplitude files for accepted phase-space Monte-Carlo data
367 if [[ ! -s "${PSACC_FILE}" ]]
368 then
369  echo "??? warning: accepted phase-space MC data file '${PSACC_FILE}' does not exist"
370 else
371  # create directory if necessary
372  if [[ ! -d "${PSACC_AMP_DIR}" ]]
373  then
374  mkdir --parents --verbose "${PSACC_AMP_DIR}"
375  fi
376  # generate amplitude files for accepted phase-space MC data
377  runCalcAmplitudes "${PSACC_FILE}" "${PSACC_AMP_DIR}"
378  # perform integration for accepted phase-space MC data
379  #runInt "${PSACC_AMP_DIR}"
380  runCalcIntegrals "${PSACC_AMP_DIR}"
381 fi
382 echo
383 
384 
385 echo ">>> info: ${0} successfully finished on $(date)"
386 
387 exit 0