TPCCLIB
Loading...
Searching...
No Matches
tacdc.c
Go to the documentation of this file.
1
4/*****************************************************************************/
5#include "tpcclibConfig.h"
6/*****************************************************************************/
7#include "tpcift.h"
8/*****************************************************************************/
9#include <stdio.h>
10#include <stdlib.h>
11#include <math.h>
12#include <time.h>
13#include <string.h>
14/*****************************************************************************/
15#include "tpcisotope.h"
16#include "tpctac.h"
17/*****************************************************************************/
18
19/*****************************************************************************/
27 TAC *tac
28) {
29 if(tac==NULL) return(ISOTOPE_UNKNOWN);
30 char buf[MAX_ISOTOPE_LEN+1];
31 if(tacGetHeaderIsotope(&tac->h, buf, NULL)!=TPCERROR_OK) return(ISOTOPE_UNKNOWN);
32 return(isotopeIdentify(buf));
33}
34/*****************************************************************************/
35
36/*****************************************************************************/
43 TAC *tac,
45 int isotope
46) {
47 if(tac!=NULL) tacSetHeaderIsotope(&tac->h, isotopeName(isotope));
48}
49/*****************************************************************************/
50
51/*****************************************************************************/
62 TAC *tac,
64 int isotope,
66 int mode,
68 TPCSTATUS *status
69) {
70 int verbose=0; if(status!=NULL) verbose=status->verbose;
71 if(verbose>0) printf("%s()\n", __func__);
72 /* check that data exists */
73 if(tac==NULL || tac->sampleNr<1 || tac->tacNr<1) {
74 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
75 return TPCERROR_NO_DATA;
76 }
77 /* check time unit */
78 if(!unitIsTime(tac->tunit)) {
79 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_UNKNOWN_UNIT);
81 }
82 /* check isotope and get halflife */
83 double f, lambda, halflife=isotopeHalflife(isotope); // halflife in min
84 if(isotope==ISOTOPE_UNKNOWN || isnan(halflife)) {
85 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_UNKNOWN_ISOTOPE);
87 }
88 /* convert halflife to the same unit that the TAC data has */
90 if(!isnan(f)) halflife*=f;
91 /* calculate the lambda */
92 lambda=lambdaFromHalflife(halflife);
93 if(mode==0) lambda=-lambda;
94 if(verbose>2) {
95 printf("halflife := %g\n", halflife);
96 printf("lambda := %g\n", lambda);
97 }
98
99
100 /*
101 * Decay correction / removal
102 */
103 if(verbose>2) {
104 if(mode!=0) printf("decay correction\n"); else printf("removing decay correction\n");
105 fflush(stdout);
106 }
107 for(int i=0; i<tac->sampleNr; i++) {
108 /* Calculate decay correction factor */
109 if(tac->isframe) {
110 if(isnan(tac->x1[i]) || isnan(tac->x2[i])) continue;
111 f=decayCorrectionFactorFromLambda(lambda, tac->x1[i], tac->x2[i]-tac->x1[i]);
112 } else {
113 if(isnan(tac->x[i])) continue;
114 f=decayCorrectionFactorFromLambda(lambda, tac->x[i], 0.0);
115 }
116 if(verbose>6) printf(" %10.4f -> %e\n", tac->x[i], f);
117 /* Correct all TACs */
118 for(int j=0; j<tac->tacNr; j++) tac->c[j].y[i]*=f;
119 /* Opposite correction for weights, if available */
122 tac->w[i]/=f;
123 }
124 if(tacIsWeighted(tac)) tacWeightNorm(tac, NULL);
127
128 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
129 return(TPCERROR_OK);
130}
131/*****************************************************************************/
132
133/*****************************************************************************/
double lambdaFromHalflife(double halflife)
Definition decay.c:47
double decayCorrectionFactorFromLambda(double lambda, double starttime, double duration)
Definition decay.c:79
char * isotopeName(int isotope_code)
Definition isotope.c:101
double isotopeHalflife(int isotope_code)
Definition isotope.c:62
int isotopeIdentify(const char *isotope)
Definition isotope.c:145
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
double * y
Definition tpctac.h:75
Definition tpctac.h:87
double * x
Definition tpctac.h:97
int sampleNr
Definition tpctac.h:89
IFT h
Optional (but often useful) header information.
Definition tpctac.h:141
double * w
Definition tpctac.h:111
int isframe
Definition tpctac.h:95
TACC * c
Definition tpctac.h:117
weights weighting
Definition tpctac.h:115
int tunit
Definition tpctac.h:109
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.
int tacGetIsotope(TAC *tac)
Definition tacdc.c:25
int tacDecayCorrection(TAC *tac, int isotope, int mode, TPCSTATUS *status)
Definition tacdc.c:59
void tacSetIsotope(TAC *tac, int isotope)
Definition tacdc.c:41
int tacSetHeaderDecayCorrection(IFT *h, decaycorrection dc)
Definition tacift.c:578
int tacSetHeaderIsotope(IFT *h, const char *s)
Definition tacift.c:341
int tacGetHeaderIsotope(IFT *h, char *s, TPCSTATUS *status)
Definition tacift.c:290
int tacIsWeighted(TAC *tac)
Definition tacw.c:24
int tacWeightNorm(TAC *tac, TPCSTATUS *status)
Definition tacw.c:237
@ WEIGHTING_ON_FD
Weights based on decay and sample frequency or frame length (Thiele et al, 2008).
@ WEIGHTING_ON_GENERAL
Weighted or weights are available, but not specified.
@ WEIGHTING_ON_COUNTS
Weights based on counts (Mazoyer et al, 1986).
@ UNIT_MIN
minutes
@ TPCERROR_UNKNOWN_UNIT
Unknown data unit.
@ TPCERROR_OK
No error.
@ TPCERROR_UNKNOWN_ISOTOPE
Unknown isotope.
@ TPCERROR_NO_DATA
File contains no data.
double unitConversionFactor(const int u1, const int u2)
Definition units.c:487
int unitIsTime(int u)
Definition units.c:359
Header file for library libtpcift.
Header file for library libtpcisotope.
#define MAX_ISOTOPE_LEN
Max string length for PET isotope.
Definition tpcisotope.h:41
@ DECAY_NOTCORRECTED
Data is not corrected for physical decay.
Definition tpcisotope.h:80
@ DECAY_CORRECTED
Data is corrected for physical decay.
Definition tpcisotope.h:81
isotope
Definition tpcisotope.h:50
@ ISOTOPE_UNKNOWN
Unknown.
Definition tpcisotope.h:51
Header file for library libtpctac.