diff options
Diffstat (limited to 'src/fit.c')
-rw-r--r-- | src/fit.c | 39 |
1 files changed, 33 insertions, 6 deletions
@@ -19,6 +19,10 @@ #include "optics.h" #include "vector.h" #include "pmt_response.h" +#include <signal.h> /* for signal() */ + +static int stop = 0; +static nlopt_opt opt; #define EV_RECORD 0x45562020 // 'EV ' (as written to ZDAB file) @@ -4965,6 +4969,8 @@ double nll(unsigned int n, const double *x, double *grad, void *params) double z1[1], z2[1]; struct timeval tv_start, tv_stop; + if (stop) nlopt_force_stop(opt); + T = x[0]; pos[0] = x[1]; @@ -5091,10 +5097,9 @@ int fit_event(event *ev, double *xopt, double *fmin) size_t i; fitParams fpars; double x[9], ss[9], lb[9], ub[9], fval, n, qhs_sum, x0[9], T0, Tmin; - int rv; struct timeval tv_start, tv_stop; - nlopt_opt opt = nlopt_create(NLOPT_LN_BOBYQA, 9); + opt = nlopt_create(NLOPT_LN_BOBYQA, 9); nlopt_set_min_objective(opt,nll,&fpars); /* Make a guess as to the energy. Right now we just use a simple @@ -5214,9 +5219,11 @@ int fit_event(event *ev, double *xopt, double *fmin) x[6] = guess_t0(ev,x+1); gettimeofday(&tv_start, NULL); - rv = nlopt_optimize(opt,x,&fval); + nlopt_optimize(opt,x,&fval); gettimeofday(&tv_stop, NULL); + if (stop) goto end; + long long elapsed = (tv_stop.tv_sec - tv_start.tv_sec)*1000 + (tv_stop.tv_usec - tv_start.tv_usec)/1000; printf("%4zu/%4zu %7.2f %7.2f %7.2f %7.2f %5.2f %5.2f %6.2f %5.2f %5.2f f() = %7.3e took %lld ms\n", @@ -5276,7 +5283,9 @@ int fit_event(event *ev, double *xopt, double *fmin) do { *fmin = fval; memcpy(xopt,x,sizeof(x)); - rv = nlopt_optimize(opt,x,&fval); + nlopt_optimize(opt,x,&fval); + + if (stop) goto end; } while (fval < *fmin && fabs(fval-*fmin) > 1e-5); if (fval < *fmin) { @@ -5286,7 +5295,12 @@ int fit_event(event *ev, double *xopt, double *fmin) nlopt_destroy(opt); - return rv; + return 0; + +end: + nlopt_destroy(opt); + + return NLOPT_FORCED_STOP; } void usage(void) @@ -5297,6 +5311,11 @@ void usage(void) exit(1); } +void sigint_handler(int dummy) +{ + stop = 1; +} + int main(int argc, char **argv) { int i; @@ -5334,6 +5353,9 @@ int main(int argc, char **argv) if (!filename) usage(); + signal(SIGPIPE, SIG_IGN); + signal(SIGINT, sigint_handler); + f = zebra_open(filename); if (!f) { @@ -5411,7 +5433,12 @@ int main(int argc, char **argv) if (ev.run != -1) { if (ev.run != bev.run || ev.gtid != bev.gtr_id) { /* New event, so we need to fit the old event. */ - fit_event(&ev,xopt,&fmin); + rv = fit_event(&ev,xopt,&fmin); + + if (rv == NLOPT_FORCED_STOP) { + printf("ctrl-c caught. quitting...\n"); + break; + } if (fout) { fprintf(fout, "%" PRIu32 " %10.2f %7.2f %7.2f %7.2f %5.2f %5.2f %6.2f %5.2f %5.2f f() = %7.3e\n", |