TPCCLIB
Loading...
Searching...
No Matches
tacdc.c File Reference

Decay correction for TAC data. More...

#include "tpcclibConfig.h"
#include "tpcift.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "tpcisotope.h"
#include "tpctac.h"

Go to the source code of this file.

Functions

int tacGetIsotope (TAC *tac)
 
void tacSetIsotope (TAC *tac, int isotope)
 
int tacDecayCorrection (TAC *tac, int isotope, int mode, TPCSTATUS *status)
 

Detailed Description

Decay correction for TAC data.

Definition in file tacdc.c.

Function Documentation

◆ tacDecayCorrection()

int tacDecayCorrection ( TAC * tac,
int isotope,
int mode,
TPCSTATUS * status )

Correct TAC data for physical decay, or remove the decay correction. Sample weights, if available, are modified, too.

See also
tacGetHeaderDecayCorrection, tacGetIsotope, tacGetHeaderIsotope, decayCorrectionFactorFromIsotope, tacDelay
Returns
enum tpcerror (TPCERROR_OK when successful).
Author
Vesa Oikonen
Parameters
tacPointer to TAC structure; status of decay correction in TAC is not verified, but set in this function. TAC must contain valid sample time unit.
isotopeIsotope code.
mode0=Remove decay correction; 1=Correct for decay.
statusPointer to status data; enter NULL if not needed.

Definition at line 59 of file tacdc.c.

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}
double lambdaFromHalflife(double halflife)
Definition decay.c:47
double decayCorrectionFactorFromLambda(double lambda, double starttime, double duration)
Definition decay.c:79
double isotopeHalflife(int isotope_code)
Definition isotope.c:62
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
double * y
Definition tpctac.h:75
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 tacSetHeaderDecayCorrection(IFT *h, decaycorrection dc)
Definition tacift.c:578
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
@ 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

Referenced by tacSetWeights().

◆ tacGetIsotope()

int tacGetIsotope ( TAC * tac)

Read isotope code from TAC header.

See also
tacGetHeaderIsotope, tacSetIsotope, isotopeIdentify, tacDecayCorrection
Author
Vesa Oikonen
Returns
Returns isotope code, or ISOTOPE_UNKNOWN, if not found.
Parameters
tacPointer to source TAC structure.

Definition at line 25 of file tacdc.c.

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}
int isotopeIdentify(const char *isotope)
Definition isotope.c:145
int tacGetHeaderIsotope(IFT *h, char *s, TPCSTATUS *status)
Definition tacift.c:290
#define MAX_ISOTOPE_LEN
Max string length for PET isotope.
Definition tpcisotope.h:41

Referenced by imgFromSIF(), tacReadModelingData(), tacReadReference(), and tacSetWeights().

◆ tacSetIsotope()

void tacSetIsotope ( TAC * tac,
int isotope )

Write isotope code into TAC header, overwriting any previous field.

See also
tacSetHeaderIsotope, tacGetIsotope
Author
Vesa Oikonen
Parameters
tacPointer to target TAC structure.
isotopeIsotope code.

Definition at line 41 of file tacdc.c.

46 {
47 if(tac!=NULL) tacSetHeaderIsotope(&tac->h, isotopeName(isotope));
48}
char * isotopeName(int isotope_code)
Definition isotope.c:101
int tacSetHeaderIsotope(IFT *h, const char *s)
Definition tacift.c:341

Referenced by imgToSIF(), tacRead4DM(), tacReadModelingData(), tacReadReference(), and tacSetWeights().