6 if [[ ! $ZSH_EVAL_CONTEXT =~ :file$ ]];
then
7 echo "Auxiliary script for running the conjugate gradient fit in zsh."
8 echo "This script should be sourced by the steering script."
12 # This script contains auxiliary functions for running the conjugate gradient fit.
13 # It should be sourced by the steering script.
14 # The steering script should provide for the method getChi2 and set the fit parameters before calling method gradient.
15 # The method getChi2 should set the current value of chi2 to its first positional parameter.
16 # The fit parameters are defined by the associative array PARAMETERS.
17 # In this, each key corresponds to a valid zsh command that can be used for changing a specific fit parameter.
18 # The command should contain the wild card '%' which is repeatedly replaced by a value, referred to as step size.
19 # The associated value is used to evaluate the derivative of chi2 per unit step size of each fit parameter.
20 # These step sizes should be tuned beforehand to yield similar absolute values of the derivatives.
21 # To this end, the methods evaluate and gprint (in this order) can be used.
23 typeset -
a CHI2 #
internal chi2 values; index [1] corresponds to best value
24 typeset -
a GRADIENT # derivative of
chi2 per unit step size of each fit parameter
25 let
"EPSILON = 5.0E-4" #
default maximal
distance to minimum
26 let
"NUMBER_OF_ITERATIONS = 1000" #
default maximum number of iterations
29 if [[ -z
"$DEBUG" ]];
then
35 # Auxiliary function to evaluate chi2 and gradient thereof at current position.
37 # The external method getChi2 is used to evaluate the chi2.
38 # The value corresponding to each key in the associative array PARAMETERS
39 # is used as unit step size for the evaluation of the derivative of the chi2.
40 # The results are stored in the internal variables CHI2[1] and GRADIENT, respectively.
50 for KEY
in ${(
k)PARAMETERS};
do
51 if (( ${#KEY} > $WIDTH ));
then
59 for KEY
in ${(ko)PARAMETERS};
do
63 VALUE=$PARAMETERS[$KEY]
85 GRADIENT[
$i]=$(($CHI2[2] - $CHI2[3]))
87 if (( $DEBUG >= 3 ));
then
88 printf
"%-${WIDTH}s %12.5f %12.5f\n" $KEY $PARAMETERS[$KEY] $GRADIENT[
$i]
91 let
"V += $GRADIENT[$i]*$GRADIENT[$i]"
95 if (( $DEBUG >= 3 ));
then
96 printf
"%-${WIDTH}s %12s %12.5f\n" "|gradient|" "" $((sqrt($V)))
102 # Move current position along gradient by given scaling factor of the unit step sizes.
104 # \param 1 scaling factor
108 if (( $1 > 0.0 ));
then
112 for KEY
in ${(ko)PARAMETERS};
do
125 let
"i = ${#GRADIENT}"
127 for KEY
in ${(kO)PARAMETERS};
do
144 # The algorithm
is based on the conjugate gradient method.
145 # The position
is moved to the optimal value and the
final chi2 is returned.
147 # If the option
is one, the step sizes are fine-tuned according to the gradient of the
chi2.
148 # The value corresponding to each key
in the associative array PARAMETERS
is then
149 # multiplied by the corresponding element
in GRADIENT and only the
sign thereof
is maintained.
151 # \param 1 maximum number of extra steps
152 # \param 2 variable containing
final chi2 value on
return
159 if (( ${#PARAMETERS} == 0 ));
then
167 for (( i = 1; i <= ${#GRADIENT}; ++i));
do
168 G[
$i]=$((-1.0 * $GRADIENT[
$i]))
173 for ((
N = 0; $N != $NUMBER_OF_ITERATIONS;
N += 1 ));
do
175 if (( $DEBUG >= 3 ));
then
176 printf
"chi2[1] %4d %12.5f\n" $N $CHI2[1]
179 # minimise chi2 in direction of gradient
184 for (( DS = 1.0, M = 0 ; $DS > 1.0e-3; ));
do
190 if (( $DEBUG >= 3 ));
then
191 printf
"chi2[4] %4d %12.5f %12.5e\n" $M $CHI2[4] $DS
194 if (( $CHI2[4] < $CHI2[3] ));
then
206 # if chi2 increases, try additional steps first to overcome possible hurdle, then try single step with reduced size
208 if (( $DS == 1.0 ));
then
210 if (( $M == 0 ));
then
214 if (( $M != $1 ));
then
222 for (( ; $M != 0; --M ));
do
232 if (( $CHI2[3] < $CHI2[2] ));
then
234 # final step based on parabolic interpolation through following points
236 # X1 = -1 * DS -> CHI[2]
237 # X2 = 0 * DS -> CHI[3]
238 # X3 = +1 * DS -> CHI[4]
240 let
"F21 = $CHI2[3] - $CHI2[2]" #
F(X2) -
F(X1)
241 let "F23 = $CHI2[3] - $CHI2[4]"
# F(X2) - F(X3)
243 let
"XS = 0.5 * ($F21 - $F23) / ($F23 + $F21)"
245 move $((+1.0 * $XS * $DS))
249 if (( $CHI2[4] < $CHI2[3] ));
then
255 move $((-1.0 * $XS * $DS))
260 if (( $DEBUG >= 3 ));
then
261 printf "
chi2[3] %4
d %12.5
f %12.5e\
n" $N $CHI2[3] $DS
274 if (( fabs($CHI2[3] - $CHI2[1]) < $EPSILON * 0.5 * (fabs($CHI2[1]) + fabs($CHI2[3])) ));
then
286 for (( i = 1;
$i <= ${#GRADIENT}; ++i ));
do
287 let
"GG += $G[$i]*$G[$i]"
288 let
"DGG += ($GRADIENT[$i] + $G[$i]) * $GRADIENT[$i]"
291 if (( $GG == 0.0 ));
then
295 let
"GAM = $DGG / $GG"
297 for (( i = 1;
$i <= ${#GRADIENT}; ++i ));
do
298 G[
$i]=$((-1.0 * $GRADIENT[
$i]))
299 H[
$i]=$(($G[
$i] + $GAM * $H[
$i]))
304 if (( $DEBUG >= 3 ));
then
305 printf
"chi2[1] %4d %12.5f\n" $N $CHI2[1]
313 # Additional fit function to be called after gradient.
315 # The algorithm is based on sorting the derivatives of the chi2 and
316 # moving the position one-by-one to the optimal value.
317 # The final chi2 is returned.
319 # \param 1 variable containing final chi2 value on return
328 for KEY
in ${(
k)PARAMETERS};
do
329 if (( ${#KEY} > $WIDTH ));
then
330 let
"WIDTH = ${#KEY}"
335 for ((
N = 0; $N != $NUMBER_OF_ITERATIONS;
N += 1 ));
do
345 for KEY
in ${(ko)PARAMETERS};
do
347 VALUE=$PARAMETERS[$KEY]
351 for K1
in ${(
o)BUFFER};
do
357 for K1
in ${(O)BUFFER};
do
361 for K1
in ${(
o)BUFFER};
do
367 for K1
in ${(O)BUFFER};
do
371 if (( $DEBUG >= 3 ));
then
372 printf
"%-${WIDTH}s %12.5f %12.5f -> %+9.5f <- %+9.5f\n" $KEY $VALUE $CHI2[1] $(($CHI2[2] - $CHI2[1])) $(($CHI2[3] - $CHI2[1]))
375 # map index to derivative of chi2 and maintain sign of step
377 if (($CHI2[2] < $CHI2[1] && $CHI2[2] < $CHI2[3]));
then
379 G[
$i]=$(($CHI2[2] - $CHI2[1]))
383 elif (($CHI2[3] < $CHI2[1] && $CHI2[3] < $CHI2[2]));
then
385 G[
$i]=$(($CHI2[3] - $CHI2[1]))
389 # elif (($CHI2[3] - $CHI2[2] > 0.5*($CHI2[2] + $CHI2[3]) - $CHI2[1])); then
391 # G[$i]=$(($CHI2[2] - $CHI2[1]))
395 # elif (($CHI2[2] - $CHI2[3] > 0.5*($CHI2[2] + $CHI2[3]) - $CHI2[1])); then
397 # G[$i]=$(($CHI2[3] - $CHI2[1]))
406 if (( ${#G} == 0 ));
then
411 # sort derivatives of chi2 and optimise position one-by-one
415 for i VALUE
in `printf
"%d %12.3e\n" ${(kv)G} | sort -g -k2`;
do
417 KEY=${${(ko)PARAMETERS}[
$i]}
419 BUFFER=(${(s/;/)${(
Q)KEY}})
423 let
"DS = $GRADIENT[$i] * $PARAMETERS[$KEY]"
425 if (($DEBUG >= 3));
then
426 printf
"%-${WIDTH}s %12.5f %12.5f\n" $KEY $DS $G[
$i]
431 for ((
n = 1; ; ++
n ));
do
433 for K1
in ${(
o)BUFFER};
do
439 if (( $DEBUG >= 3 ));
then
440 printf
"chi2[4] %4d %12.5f\n" $n $CHI2[4]
443 if (( $CHI2[4] < $CHI2[3] ));
then
450 for K1
in ${(O)BUFFER};
do
454 if (( $CHI2[3] < $CHI2[2] ));
then
456 # final step based on parabolic interpolation through following points
458 # X1 = -1 * DS -> CHI[2]
459 # X2 = 0 * DS -> CHI[3]
460 # X3 = +1 * DS -> CHI[4]
462 let
"F21 = $CHI2[3] - $CHI2[2]" #
F(X2) -
F(X1)
463 let "F23 = $CHI2[3] - $CHI2[4]"
# F(X2) - F(X3)
465 let
"XS = 0.5 * ($F21 - $F23) / ($F23 + $F21)"
467 for K1
in ${(
o)BUFFER};
do
473 if (( $CHI2[4] < $CHI2[3] ));
then
479 for K1
in ${(O)BUFFER};
do
485 if (( $DEBUG >= 3 ));
then
486 printf
"chi2[3] %4d %12.5f\n" $n $CHI2[3]
495 if (( fabs($CHI2[2] - $CHI2[1]) < $EPSILON * 0.5 * (fabs($CHI2[1]) + fabs($CHI2[2])) ));
then
503 if (( $DEBUG >= 3 ));
then
504 printf
"chi2[1] %4d %12.5f\n" $N $CHI2[1]
set Main(RunControl, Responder) is
then fatal No hydrophone data file $HYDROPHONE_TXT fi sort gr k
Q(UTCMax_s-UTCMin_s)-livetime_s
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
o $QUALITY_ROOT d $DEBUG!JPlot1D f
then JShowerPostfit f $INPUT_FILE o $OUTPUT_FILE N
static const double H
Planck constant [eV s].
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
then fatal Wrong number of arguments fi JConvertDetectorFormat a o
skip elif((BINFRAC< 1.0))
then JMuonMCEvt f $INPUT_FILE o $INTERMEDIATE_FILE d
then if[[!-f $DETECTOR]] then JDetector sh $DETECTOR fi cat $WORKDIR trigger_parameters txt<< EOFtrigger3DMuon.enabled=1;trigger3DMuon.numberOfHits=5;trigger3DMuon.gridAngle_deg=1;ctMin=0.0;TMaxLocal_ns=15.0;EOF set_variable TRIGGEREFFICIENCY_TRIGGERED_EVENTS_ONLY INPUT_FILES=() for((i=1;$i<=$NUMBER_OF_RUNS;++i));do JSirene.sh $DETECTOR $JPP_DATA/genhen.km3net_wpd_V2_0.evt.gz $WORKDIR/sirene_ ${i}.root JTriggerEfficiency.sh $DETECTOR $DETECTOR $WORKDIR/sirene_ ${i}.root $WORKDIR/trigger_efficiency_ ${i}.root $WORKDIR/trigger_parameters.txt $JPP_DATA/PMT_parameters.txt INPUT_FILES+=($WORKDIR/trigger_efficiency_ ${i}.root) done for ANGLE_DEG in $ANGLES_DEG[*];do set_variable SIGMA_NS 3.0 set_variable OUTLIERS 3 set_variable OUTPUT_FILE $WORKDIR/matrix\[${ANGLE_DEG}\deg\].root $JPP_DIR/examples/JReconstruction-f"$INPUT_FILES[*]"-o $OUTPUT_FILE-S ${SIGMA_NS}-A ${ANGLE_DEG}-O ${OUTLIERS}-d ${DEBUG}--!fiif[[$OPTION=="plot"]];then if((0));then for H1 in h0 h1;do JPlot1D-f"$WORKDIR/matrix["${^ANGLES_DEG}" deg].root:${H1}"-y"1 2e3"-Y-L TR-T""-\^"number of events [a.u.]"-> o chi2
esac typeset A BUFFER $JPP_DIR examples JAcoustics JCreep f $INPUT_FILE BUFFER
int sign(const T &value)
Get sign of value.
double getChi2(const double P)
Get chi2 corresponding to given probability.
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
source $JPP_DIR setenv csh $JPP_DIR &dev null eval JShellParser o a A
#define DEBUG(A)
Message macros.