TPCCLIB
Loading...
Searching...
No Matches
abssbkg.c
Go to the documentation of this file.
1
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <math.h>
14/*****************************************************************************/
15#include "tpcextensions.h"
16#include "tpctac.h"
17#include "tpcabss.h"
18/*****************************************************************************/
19
20/*****************************************************************************/
21static char *info[] = {
22 "Subtract constant background radioactivity in on-line blood sampler data.",
23 "The average of coincidence cps from 0 to specified time is calculated",
24 "and subtracted from the data; in case of Scanditronics/GEMS this is",
25 "a separate process for the two detector pairs, and in case of Allogg",
26 "separate for the two channels.",
27 "The background radioactivity is assumed to be stable during the assay",
28 "(not decaying); do not use this program in case of momentary background",
29 "radiation.",
30 "Notice that the original data file is overwritten by default.",
31 " ",
32 "Usage: @P [options] abssfile time",
33 " ",
34 "Options:",
35 " -o=<filename>",
36 " Name for corrected file; by default the original file is overwritten.",
37 " --dry",
38 " Dry run: background is shown but data is not changed.",
39 " -stdoptions", // List standard options like --help, -v, etc
40 " ",
41 "Example 1: calculate background from 0-60 s and subtract it.",
42 " @P -o=us1328_corr.bld us1328.bld 60",
43 "Example 2: calculate mean radioactivity [cps] from the measurement.",
44 " @P --dry us1328.bld 999999",
45 " ",
46 "See also: absszero, absscal, absstime, abssfch, abssexam, fitdelay",
47 " ",
48 "Keywords: input, blood, background, ABSS",
49 0};
50/*****************************************************************************/
51
52/*****************************************************************************/
53/* Turn on the globbing of the command line, since it is disabled by default in
54 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
55 In Unix&Linux wildcard command line processing is enabled by default. */
56/*
57#undef _CRT_glob
58#define _CRT_glob -1
59*/
60int _dowildcard = -1;
61/*****************************************************************************/
62
63/*****************************************************************************/
67int main(int argc, char **argv)
68{
69 int ai, help=0, version=0, verbose=1;
70 char abssfile[FILENAME_MAX], outfile[FILENAME_MAX];
71 char *cptr;
72 double bkgTime=nan("");
73 int dryRun=0;
74 int ret;
75
76
77 /*
78 * Get arguments
79 */
80 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
81 abssfile[0]=outfile[0]=(char)0;
82 /* Options */
83 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
84 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
85 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
86 if(strncasecmp(cptr, "O=", 2)==0 && strlen(cptr)>2) {
87 strlcpy(outfile, cptr+2, FILENAME_MAX); continue;
88 } else if(strcasecmp(cptr, "DRY")==0) {
89 dryRun=1; continue;
90 }
91 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
92 return(1);
93 } else break;
94
95 TPCSTATUS status; statusInit(&status);
96 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
97 status.verbose=verbose-3;
98
99 /* Print help or version? */
100 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
101 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
102 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
103
104 /* Process other arguments, starting from the first non-option */
105 if(ai<argc) strlcpy(abssfile, argv[ai++], FILENAME_MAX);
106 if(ai<argc) {
107 if(atofCheck(argv[ai], &bkgTime) || bkgTime<1.0) {
108 fprintf(stderr, "Error: invalid bkg time '%s'.\n", argv[ai]);
109 return(1);
110 }
111 ai++;
112 }
113 if(ai<argc) {
114 fprintf(stderr, "Error: invalid argument '%s'.\n", argv[ai]);
115 return(1);
116 }
117 /* Did we get all the information that we need? */
118 if(isnan(bkgTime)) {
119 fprintf(stderr, "Error: missing command-line argument; use option --help\n");
120 return(1);
121 }
122 if(!outfile[0]) strcpy(outfile, abssfile);
123
124
125 /* In verbose mode print arguments and options */
126 if(verbose>1) {
127 printf("abssfile := %s\n", abssfile);
128 printf("bkgTime := %g\n", bkgTime);
129 printf("dryRun := %d\n", dryRun);
130 printf("outfile := %s\n", outfile);
131 fflush(stdout);
132 }
133
134 /*
135 * Read ABSS file
136 */
137 if(verbose>1) printf("reading %s\n", abssfile);
138 TAC abss; tacInit(&abss);
139 ret=tacRead(&abss, abssfile, &status);
140 if(ret!=TPCERROR_OK) {
141 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
142 tacFree(&abss); return(2);
143 }
144 if(verbose>1) {
145 printf("sampleNr := %d\n", abss.sampleNr);
146 }
147 if(verbose>2) {
148 printf("fileformat := %d\n", abss.format);
149 printf("tacNr := %d\n", abss.tacNr);
150 }
151 if(abss.format!=TAC_FORMAT_ABSS_ALLOGG &&
155 {
156 fprintf(stderr, "Error: not valid ABSS format.\n");
157 tacFree(&abss); return(2);
158 }
159 if(verbose>100) abssWrite(&abss, stdout, NULL);
160 if(abss.tacNr<1 || abss.sampleNr<1) {
161 fprintf(stderr, "Error: not valid ABSS format.\n");
162 tacFree(&abss); return(2);
163 }
164
165
166 /*
167 * Calculate the background
168 */
169 if(verbose>1) {printf("calculating the background\n"); fflush(stdout);}
170 int i, n1=0, n2=0;
171 double fdur, a1, a2, bkg1, bkg2;
172 bkg1=bkg2=0.0;
175 {
176 for(i=0, n1=n2=0; i<abss.sampleNr; i++) {
177 a1=abss.c[0].y[i]; a2=abss.c[3].y[i];
178 if(verbose>5) {
179 printf("%g %g %g %g\n", abss.x1[i], abss.x2[i], a1, a2);
180 fflush(stdout);
181 }
182 fdur=abss.x2[i]-abss.x1[i];
183 if(isnan(fdur) || fdur<1.0E-06 || isnan(abss.x[i])) continue;
184 if(abss.x[i]>bkgTime) break;
185 a1/=fdur; a2/=fdur;
186 if(!isnan(a1)) {bkg1+=a1; n1++;}
187 if(!isnan(a2)) {bkg2+=a2; n2++;}
188 }
189 bkg1/=(double)n1;
190 bkg2/=(double)n2;
191 if(verbose>0 || dryRun!=0) {
192 printf("background_ch1[cps] := %g\n", bkg1);
193 printf("background_ch2[cps] := %g\n", bkg2);
194 }
195 } else if(abss.format==TAC_FORMAT_ABSS_ALLOGG_OLD ||
197 {
198 for(i=0, n1=n2=0; i<abss.sampleNr; i++) {
199 a1=abss.c[0].y[i]; a2=abss.c[1].y[i];
200 if(verbose>5) {
201 printf("%g %g %g %g\n", abss.x1[i], abss.x2[i], a1, a2);
202 fflush(stdout);
203 }
204 fdur=abss.x2[i]-abss.x1[i];
205 if(isnan(fdur) || fdur<1.0E-06 || isnan(abss.x[i])) continue;
206 if(abss.x[i]>bkgTime) break;
207 a1/=fdur; a2/=fdur;
208 if(!isnan(a1)) {bkg1+=a1; n1++;}
209 if(!isnan(a2)) {bkg2+=a2; n2++;}
210 }
211 bkg1/=(double)n1;
212 bkg2/=(double)n2;
213 if(verbose>0 || dryRun!=0) {
214 printf("background_ch1[cps] := %g\n", bkg1);
215 printf("background_ch2[cps] := %g\n", bkg2);
216 }
217 }
218 if(verbose>0 || dryRun!=0) {fflush(stdout);}
219
220
221 /* If dry run, then that's it */
222 if(dryRun!=0) {
223 tacFree(&abss); return(0);
224 }
225
226
227 /*
228 * Otherways, subtract the background
229 */
230 if(verbose>1) {
231 printf("subtracting background from samples\n"); fflush(stdout);}
234 {
235 for(i=0; i<abss.sampleNr; i++) {
236 fdur=abss.x2[i]-abss.x1[i];
237 if(isnan(fdur) || fdur<1.0E-06) continue;
238 abss.c[0].y[i]-=bkg1*fdur;
239 abss.c[3].y[i]-=bkg2*fdur;
240 }
241 } else if(abss.format==TAC_FORMAT_ABSS_ALLOGG_OLD ||
243 {
244 for(i=0; i<abss.sampleNr; i++) {
245 fdur=abss.x2[i]-abss.x1[i];
246 if(isnan(fdur) || fdur<1.0E-06 || isnan(abss.x[i])) continue;
247 abss.c[0].y[i]-=bkg1*fdur;
248 abss.c[1].y[i]-=bkg2*fdur;
249 }
250 }
251 if(verbose>1) {
252 printf("writing corrected data file in %s\n", outfile); fflush(stdout);}
253 FILE *fp;
254 fp=fopen(outfile, "w");
255 if(fp==NULL) {
256 fprintf(stderr, "Error: cannot open file for writing.\n");
257 tacFree(&abss); return(11);
258 }
259 ret=abssWrite(&abss, fp, &status);
260 fclose(fp);
261 if(ret!=TPCERROR_OK) {
262 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
263 tacFree(&abss); return(12);
264 }
265 if(verbose>0) {
266 printf("corrected data written in %s\n", outfile);
267 fflush(stdout);
268 }
269
270 tacFree(&abss);
271 return(0);
272}
273/*****************************************************************************/
274
275/*****************************************************************************/
int abssWrite(TAC *d, FILE *fp, TPCSTATUS *status)
Definition abssio.c:53
int atofCheck(const char *s, double *v)
Definition decpoint.c:94
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:47
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:169
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:339
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:114
void statusInit(TPCSTATUS *s)
Definition statusmsg.c:104
char * errorMsg(tpcerror e)
Definition statusmsg.c:68
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
double * y
Definition tpctac.h:75
Definition tpctac.h:87
double * x
Definition tpctac.h:97
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
TACC * c
Definition tpctac.h:117
double * x2
Definition tpctac.h:101
double * x1
Definition tpctac.h:99
int tacNr
Definition tpctac.h:91
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
void tacFree(TAC *tac)
Definition tac.c:106
void tacInit(TAC *tac)
Definition tac.c:24
int tacRead(TAC *d, const char *fname, TPCSTATUS *status)
Definition tacio.c:413
Header file for libtpcabss.
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
Header file for library libtpctac.
@ TAC_FORMAT_ABSS_ALLOGG
ALLOGG ABSS data; reading supported.
Definition tpctac.h:56
@ TAC_FORMAT_ABSS_GEMS
GEMS ABSS data; reading supported.
Definition tpctac.h:54
@ TAC_FORMAT_ABSS_ALLOGG_OLD
ALLOGG ABSS data (old format); reading supported.
Definition tpctac.h:55
@ TAC_FORMAT_ABSS_SCANDITRONICS
Scanditronics ABSS data; reading supported.
Definition tpctac.h:53