diff options
author | tlatorre <tlatorre@uchicago.edu> | 2019-03-07 14:43:51 -0600 |
---|---|---|
committer | tlatorre <tlatorre@uchicago.edu> | 2019-03-07 14:43:51 -0600 |
commit | a96111c8026d47147708796091ec7bef216aefd8 (patch) | |
tree | 07a31d0bffeb24e85e3d3eee9105ed019a40f13c /src | |
parent | f68ab9ebbcab088f0f4ab60a2397eb471998ae74 (diff) | |
download | sddm-a96111c8026d47147708796091ec7bef216aefd8.tar.gz sddm-a96111c8026d47147708796091ec7bef216aefd8.tar.bz2 sddm-a96111c8026d47147708796091ec7bef216aefd8.zip |
update code to allow you to run the fit outside of the src directory
To enable the fitter to run outside of the src directory, I created a new
function open_file() which works exactly like fopen() except that it searches
for the file in both the current working directory and the path specified by an
environment variable.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile | 10 | ||||
-rw-r--r-- | src/db.c | 3 | ||||
-rw-r--r-- | src/electron.c | 3 | ||||
-rw-r--r-- | src/fit.c | 19 | ||||
-rw-r--r-- | src/muon.c | 3 | ||||
-rw-r--r-- | src/pmt.c | 3 | ||||
-rw-r--r-- | src/pmt_response.c | 8 | ||||
-rw-r--r-- | src/proton.c | 3 | ||||
-rw-r--r-- | src/quantum_efficiency.c | 3 | ||||
-rw-r--r-- | src/test-find-peaks.c | 1 | ||||
-rw-r--r-- | src/test.c | 1 | ||||
-rw-r--r-- | src/util.c | 41 | ||||
-rw-r--r-- | src/util.h | 9 |
13 files changed, 85 insertions, 22 deletions
diff --git a/src/Makefile b/src/Makefile index 214bf3f..7a78f14 100644 --- a/src/Makefile +++ b/src/Makefile @@ -14,23 +14,23 @@ calculate_limits: calculate_limits.c solid_angle.o: solid_angle.c -test: test.o solid_angle.o optics.o muon.o vector.o quantum_efficiency.o pdg.o scattering.o misc.o mt19937ar.o sno_charge.o path.o random.o pmt_response.o db.o dict.o siphash.o electron.o proton.o likelihood.o pmt.o quad.o find_peaks.o +test: test.o solid_angle.o optics.o muon.o vector.o quantum_efficiency.o pdg.o scattering.o misc.o mt19937ar.o sno_charge.o path.o random.o pmt_response.o db.o dict.o siphash.o electron.o proton.o likelihood.o pmt.o quad.o find_peaks.o util.o test-vector: test-vector.o vector.o mt19937ar.o -test-likelihood: test-likelihood.o muon.o random.o optics.o quantum_efficiency.o mt19937ar.o pdg.o vector.o solid_angle.o scattering.o pmt_response.o db.o dict.o siphash.o misc.o +test-likelihood: test-likelihood.o muon.o random.o optics.o quantum_efficiency.o mt19937ar.o pdg.o vector.o solid_angle.o scattering.o pmt_response.o db.o dict.o siphash.o misc.o util.o test-path: test-path.o mt19937ar.o vector.o path.o random.o misc.o test-charge: test-charge.o sno_charge.o misc.o vector.o -test-time-pdf: test-time-pdf.o zebra.o likelihood.o pmt.o vector.o misc.o muon.o optics.o quantum_efficiency.o solid_angle.o pdg.o scattering.o zdab_utils.o pack2b.o sno_charge.o db.o dqxx.o dict.o siphash.o path.o pmt_response.o release.o electron.o proton.o +test-time-pdf: test-time-pdf.o zebra.o likelihood.o pmt.o vector.o misc.o muon.o optics.o quantum_efficiency.o solid_angle.o pdg.o scattering.o zdab_utils.o pack2b.o sno_charge.o db.o dqxx.o dict.o siphash.o path.o pmt_response.o release.o electron.o proton.o util.o test-zebra: test-zebra.o zebra.o pack2b.o -fit: fit.o zebra.o likelihood.o pmt.o vector.o misc.o muon.o optics.o quantum_efficiency.o solid_angle.o pdg.o scattering.o zdab_utils.o pack2b.o sno_charge.o db.o dqxx.o dict.o siphash.o path.o pmt_response.o release.o electron.o proton.o find_peaks.o quad.o dc.o sort.o +fit: fit.o zebra.o likelihood.o pmt.o vector.o misc.o muon.o optics.o quantum_efficiency.o solid_angle.o pdg.o scattering.o zdab_utils.o pack2b.o sno_charge.o db.o dqxx.o dict.o siphash.o path.o pmt_response.o release.o electron.o proton.o find_peaks.o quad.o dc.o sort.o util.o -test-find-peaks: test-find-peaks.o zebra.o likelihood.o pmt.o vector.o misc.o muon.o optics.o quantum_efficiency.o solid_angle.o pdg.o scattering.o zdab_utils.o pack2b.o sno_charge.o db.o dqxx.o dict.o siphash.o path.o pmt_response.o release.o electron.o proton.o find_peaks.o +test-find-peaks: test-find-peaks.o zebra.o likelihood.o pmt.o vector.o misc.o muon.o optics.o quantum_efficiency.o solid_angle.o pdg.o scattering.o zdab_utils.o pack2b.o sno_charge.o db.o dqxx.o dict.o siphash.o path.o pmt_response.o release.o electron.o proton.o find_peaks.o util.o calculate-csda-range: calculate-csda-range.o @@ -5,6 +5,7 @@ #include <stdint.h> /* for uint32_t */ #include <stdlib.h> /* for atoi() */ #include "dict.h" +#include "util.h" char db_err[256]; @@ -147,7 +148,7 @@ int load_file(dict *db, const char *filename) return -1; } - FILE *f = fopen(filename, "r"); + FILE *f = open_file(filename, "r"); if (!f) { sprintf(db_err, "unable to open file '%s': %s", filename, strerror(errno)); diff --git a/src/electron.c b/src/electron.c index 60bcec7..2f6902e 100644 --- a/src/electron.c +++ b/src/electron.c @@ -18,6 +18,7 @@ #include <gsl/gsl_integration.h> #include <math.h> /* for fmax() */ #include <gsl/gsl_sf_gamma.h> +#include "util.h" static int initialized = 0; @@ -215,7 +216,7 @@ static int init() double value; int n; - FILE *f = fopen("e_water_liquid.txt", "r"); + FILE *f = open_file("e_water_liquid.txt", "r"); if (!f) { fprintf(stderr, "failed to open e_water_liquid.txt: %s\n", strerror(errno)); @@ -26,6 +26,7 @@ #include "quad.h" #include "sno.h" #include "find_peaks.h" +#include "util.h" /* Maximum number of fit parameters. Should be at least 4 + 3*MAX_VERTICES. */ #define MAX_PARS 100 @@ -5977,7 +5978,11 @@ int main(int argc, char **argv) fprintf(fout, "data:\n"); } - load_pmt_info(); + if (load_pmt_info()) { + zebra_close(f); + if (output) fclose(fout); + return 1; + } for (i = 0; i < MAX_PMTS; i++) { ev.pmt_hits[i].hit = 0; @@ -5992,32 +5997,32 @@ int main(int argc, char **argv) if (load_file(db, "DQXX_0000010000.dat")) { fprintf(stderr, "failed to load DQXX_0000010000.dat: %s\n", db_err); - exit(1); + goto err; } if (dqxx_init(db, &ev)) { fprintf(stderr, "failed to initialize DQXX bank: %s\n", dqxx_err); - exit(1); + goto err; } if (load_file(db, "pmt_response_qoca_d2o_20060216.dat")) { fprintf(stderr, "failed to load pmt_response_qoca_d2o_20060216.dat: %s\n", db_err); - exit(1); + goto err; } if (load_file(db, "rsp_rayleigh.dat")) { fprintf(stderr, "failed to load rsp_rayleigh.dat: %s\n", db_err); - exit(1); + goto err; } if (pmt_response_init(db)) { fprintf(stderr, "failed to initialize PMTR bank: %s\n", pmtr_err); - exit(1); + goto err; } if (optics_init(db)) { fprintf(stderr, "failed to initialize optics: %s\n", optics_err); - exit(1); + goto err; } while (1) { @@ -15,6 +15,7 @@ #include "scattering.h" #include "pmt_response.h" #include "misc.h" +#include "util.h" static int initialized = 0; @@ -158,7 +159,7 @@ static int init() double value; int n; - FILE *f = fopen("muE_deuterium_oxide_liquid.txt", "r"); + FILE *f = open_file("muE_deuterium_oxide_liquid.txt", "r"); if (!f) { fprintf(stderr, "failed to open muE_water_liquid.txt: %s\n", strerror(errno)); @@ -4,6 +4,7 @@ #include <string.h> #include <stdlib.h> #include "vector.h" +#include "util.h" static int initialized = 0; @@ -17,7 +18,7 @@ int load_pmt_info() double value; int n; - FILE *f = fopen("pmt.txt", "r"); + FILE *f = open_file("pmt.txt", "r"); if (!f) { fprintf(stderr, "failed to open pmt.txt: %s\n", strerror(errno)); diff --git a/src/pmt_response.c b/src/pmt_response.c index 7f22ddb..feda3a6 100644 --- a/src/pmt_response.c +++ b/src/pmt_response.c @@ -427,8 +427,8 @@ int pmt_response_init(dict *db) void pmt_response_free(void) { - gsl_spline2d_free(spline_resp); - gsl_spline2d_free(spline_reflec); - gsl_interp_accel_free(xacc); - gsl_interp_accel_free(yacc); + if (spline_resp) gsl_spline2d_free(spline_resp); + if (spline_reflec) gsl_spline2d_free(spline_reflec); + if (xacc) gsl_interp_accel_free(xacc); + if (yacc) gsl_interp_accel_free(yacc); } diff --git a/src/proton.c b/src/proton.c index f5a6b61..32b17a1 100644 --- a/src/proton.c +++ b/src/proton.c @@ -15,6 +15,7 @@ #include "scattering.h" #include "pmt_response.h" #include "misc.h" +#include "util.h" static int initialized = 0; @@ -38,7 +39,7 @@ static int init() double value; int n; - FILE *f = fopen("proton_water_liquid.txt", "r"); + FILE *f = open_file("proton_water_liquid.txt", "r"); if (!f) { fprintf(stderr, "failed to open proton_water_liquid.txt: %s\n", strerror(errno)); diff --git a/src/quantum_efficiency.c b/src/quantum_efficiency.c index a5571ab..200ad2c 100644 --- a/src/quantum_efficiency.c +++ b/src/quantum_efficiency.c @@ -3,6 +3,7 @@ #include <string.h> #include <stdlib.h> #include "misc.h" +#include "util.h" static int initialized = 0; @@ -17,7 +18,7 @@ static int init() double value; int n; - FILE *f = fopen("pmt_pcath_response.dat", "r"); + FILE *f = open_file("pmt_pcath_response.dat", "r"); if (!f) { fprintf(stderr, "failed to open pmt_pcath_response.dat: %s\n", strerror(errno)); diff --git a/src/test-find-peaks.c b/src/test-find-peaks.c index 0af3fa0..361b5fc 100644 --- a/src/test-find-peaks.c +++ b/src/test-find-peaks.c @@ -10,6 +10,7 @@ #include <errno.h> /* for errno */ #include <string.h> /* for strerror() */ #include "vector.h" +#include "util.h" #define EV_RECORD 0x45562020 #define MCTK_RECORD 0x4d43544b @@ -26,6 +26,7 @@ #include "quad.h" #include "find_peaks.h" #include <gsl/gsl_sort.h> +#include "util.h" typedef int testFunction(char *err); diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..cd39c3f --- /dev/null +++ b/src/util.c @@ -0,0 +1,41 @@ +#include "util.h" +#include <unistd.h> /* for access() */ +#include <stdlib.h> /* for getenv() */ +#include <stdio.h> /* for sprintf(), fopen() */ + +/* Equivalent to fopen() but uses find_file() to find the path to the filename. */ +FILE *open_file(const char *filename, const char *mode) +{ + char *path; + + path = find_file(filename); + + if (!path) return NULL; + + return fopen(path,mode); +} + +/* Returns the full path to `filename` by searching the current working + * directory and the path specified by the environment variable SDDM_DATA. + * + * Returns a pointer to the full path on success or NULL if the file isn't + * found. */ +char *find_file(const char *filename) +{ + static char path[256]; + char *dir; + + /* First check in the current working directory. */ + sprintf(path, "%s", filename); + if (access(path, F_OK) == 0) return path; + + /* Now check in the directory specified by the environment variable + * SDDM_DATA. */ + dir = getenv("SDDM_DATA"); + if (dir) { + sprintf(path, "%s/%s", dir,filename); + if (access(path, F_OK) == 0) return path; + } + + return NULL; +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..ef77df8 --- /dev/null +++ b/src/util.h @@ -0,0 +1,9 @@ +#ifndef UTIL_H +#define UTIL_H + +#include <stdio.h> + +FILE *open_file(const char *filename, const char *mode); +char *find_file(const char *filename); + +#endif |