LALPulsar 7.1.1.1-eeff03c
RecalcToplistStats.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2013 Karl Wette
3 * Copyright (C) 2011-2014 David Keitel
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with with program; see the file COPYING. If not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1301 USA
19 */
20
21/*---------- INCLUDES ----------*/
22#define __USE_ISOC99 1
23#include "RecalcToplistStats.h"
24
25/*---------- local DEFINES ----------*/
26#define TRUE (1==1)
27#define FALSE (1==0)
28
29/*----- Macros ----- */
30
31/*----- SWITCHES -----*/
32
33/*---------- internal types ----------*/
34
35/*---------- Global variables ----------*/
36
37/*---------- internal prototypes ----------*/
38
39/*==================== FUNCTION DEFINITIONS ====================*/
40
41
42/** XLAL function to go through a (Hough or GCT) toplist and compute line-robust statistics for each candidate */
43int XLALComputeExtraStatsForToplist( toplist_t *list, /**< list of cancidates with f, sky position etc. - no output so far */
44 const RecalcStatsParams *recalcParams /**< additional input values and parameters */
45 )
46{
47
48 /* check input parameters and report errors */
49 XLAL_CHECK( list && recalcParams->listEntryTypeName && recalcParams->Fstat_in_vec && recalcParams->detectorIDs, XLAL_EFAULT, "Empty pointer as input parameter." );
50 XLAL_CHECK( list->data && list->heap, XLAL_EFAULT, "Input toplist has no elements." );
51 XLAL_CHECK( list->elems > 0, XLAL_EBADLEN, "Input toplist has zero length." );
52
53 /* check listEntryTypeName only once by strcmp, afterwards by int, to be faster */
54 UINT4 listEntryType = 0;
55 if ( strcmp( recalcParams->listEntryTypeName, "GCTtop" ) == 0 ) {
56 listEntryType = 1;
57 }
58 if ( strcmp( recalcParams->listEntryTypeName, "HoughFstat" ) == 0 ) {
59 listEntryType = 2;
60 }
61 XLAL_CHECK( listEntryType != 0, XLAL_EBADLEN, "Unsupported entry type for input toplist! Supported types currently are: GCTtop, HoughFstat." );
62
63 /* set up temporary variables and structs */
64 PulsarDopplerParams XLAL_INIT_DECL( candidateDopplerParams ); /* struct containing sky position, frequency and fdot for the current candidate */
65 UINT4 X;
66
67 /* initialize doppler parameters */
68 candidateDopplerParams.refTime = recalcParams->refTimeGPS; /* spin parameters in toplist refer to this refTime */
69
70 UINT4 numDetectors = recalcParams->detectorIDs->length;
71
72 UINT4 j;
73 UINT4 numElements = list->elems;
74 /* loop over toplist: re-compute TwoF and TwoFX for all candidates (average over segments) */
75 for ( j = 0; j < numElements; j++ ) {
76
77 void *elemV = NULL;
78 if ( listEntryType == 1 ) {
80 elemV = elem;
81
82 /* get frequency, sky position, doppler parameters from toplist candidate and save to dopplerParams */
83 candidateDopplerParams.Alpha = elem->Alpha;
84 candidateDopplerParams.Delta = elem->Delta;
85 candidateDopplerParams.fkdot[0] = elem->Freq;
86 candidateDopplerParams.fkdot[1] = elem->F1dot;
87 candidateDopplerParams.fkdot[2] = elem->F2dot;
88 } else if ( listEntryType == 2 ) {
90 elemV = elem;
91
92 XLAL_CHECK( ( elem->sumTwoFX = XLALCreateREAL4Vector( numDetectors ) ) != NULL, XLAL_EFUNC, "Failed call to XLALCreateREAL4Vector( %d ).", numDetectors );
93
94 /* get frequency, sky position, doppler parameters from toplist candidate and save to dopplerParams */
95 candidateDopplerParams.Alpha = elem->AlphaBest;
96 candidateDopplerParams.Delta = elem->DeltaBest;
97 candidateDopplerParams.fkdot[0] = elem->Freq;
98 candidateDopplerParams.fkdot[1] = elem->f1dot;
99 /* no 2nd spindown in HoughFstatOutputEntry */
100 } /* if listEntryType 2 */
101
102 /* recalculate multi- and single-IFO Fstats for all segments for this candidate */
103 RecalcStatsComponents XLAL_INIT_DECL( recalcStats ); /* struct containing multi-detector F-stat, single-detector F-stats, BSGL */
104 recalcStats.log10BSGL = -LAL_REAL4_MAX; /* proper initialization here is not 0 */
105 recalcStats.log10BSGLtL = -LAL_REAL4_MAX;
106 XLAL_CHECK( XLALComputeExtraStatsSemiCoherent( &recalcStats, &candidateDopplerParams, recalcParams ) == XLAL_SUCCESS, XLAL_EFUNC, "Failed call to XLALComputeExtraStatsSemiCoherent()." );
107
108 /* save values in toplist */
109 if ( listEntryType == 1 ) {
110 GCTtopOutputEntry *elem = elemV;
111 elem->numDetectors = numDetectors;
112 elem->avTwoFrecalc = recalcStats.avTwoF; /* average over segments */
113 for ( X = 0; X < numDetectors; X ++ ) {
114 elem->avTwoFXrecalc[X] = recalcStats.avTwoFX[X];
115 }
116 elem->log10BSGLrecalc = recalcStats.log10BSGL;
117 elem->log10BSGLtLrecalc = recalcStats.log10BSGLtL;
118 if ( recalcParams->loudestSegOutput ) {
119 elem->loudestSeg = recalcStats.loudestSeg;
120 elem->twoFloudestSeg = recalcStats.twoFloudestSeg;
121 for ( X = 0; X < numDetectors; X ++ ) {
122 elem->twoFXloudestSeg[X] = recalcStats.twoFXloudestSeg[X];
123 }
124 }
125 } /* if ( listEntryType == 1 ) */
126 else if ( listEntryType == 2 ) {
128
129 elem->sumTwoF = recalcStats.avTwoF; /* this is also the average over segments, the field is only called "sumTwoF" due to Hough legacy */
130 for ( X = 0; X < numDetectors; X ++ ) {
131 elem->sumTwoFX->data[X] = recalcStats.avTwoFX[X];
132 }
133 } /* if ( listEntryType == 2 ) */
134
135 } /* for j < numElements */
136
137 return ( XLAL_SUCCESS );
138
139} /* XLALComputeExtraStatsForToplist() */
140
141
142/**
143 * XLAL Function to recalculate multi-IFO F-stat 2F and single-IFO 2FX for all semicoherent search segments
144 * This returns AVERAGE F-stats over segments, not sums.
145 */
146int XLALComputeExtraStatsSemiCoherent( RecalcStatsComponents *recalcStats, /**< [out] structure containing multi TwoF, single TwoF, BSGL */
147 const PulsarDopplerParams *dopplerParams, /**< sky position, frequency and fdot for a given candidate */
148 const RecalcStatsParams *recalcParams /**< additional input values and parameters */
149 )
150{
151
152 /* check input parameters and report errors */
153 XLAL_CHECK( recalcStats && dopplerParams && recalcParams->Fstat_in_vec && recalcParams->detectorIDs && recalcParams->startTstack, XLAL_EFAULT, "Empty pointer as input parameter." );
154
155 UINT4 numSegments = recalcParams->Fstat_in_vec->length;
156 UINT4 numDetectors = recalcParams->detectorIDs->length;
157
158 recalcStats->numDetectors = numDetectors;
159
160 /* variables necessary to catch segments where not all detectors have data */
161 INT4 detid = -1; /* count through detector IDs for matching with name strings */
162 UINT4 numSegmentsX[numDetectors]; /* number of segments with data might be different for each detector */
163 for ( UINT4 X = 0; X < numDetectors; X++ ) {
164 numSegmentsX[X] = 0;
165 }
166
167 /* internal dopplerParams structure, for extrapolating to correct reftimes for each segment */
168 PulsarDopplerParams XLAL_INIT_DECL( dopplerParams_temp ); /* struct containing sky position, frequency and fdot for the current candidate */
169 dopplerParams_temp.Alpha = dopplerParams->Alpha;
170 dopplerParams_temp.Delta = dopplerParams->Delta;
171 XLAL_INIT_MEM( dopplerParams_temp.fkdot );
172
173 /* just in case the caller hasn't properly initialized recalcStats, make sure everything is 0 before the loop */
174 REAL4 sumTwoF = 0.0;
175 REAL4 sumTwoFX[numDetectors];
176 for ( UINT4 X = 0; X < numDetectors; X++ ) {
177 sumTwoFX[X] = 0.0;
178 recalcStats->twoFXloudestSeg[X] = 0.0;
179 }
180 recalcStats->twoFloudestSeg = 0.0;
181 REAL4 maxTwoFXl[PULSAR_MAX_DETECTORS]; // max single-IFO Fstat-value, maximized over all segments
182 XLAL_INIT_MEM( maxTwoFXl );
183
184 /* compute single- and multi-detector Fstats for each data segment and sum up */
185 FstatResults *Fstat_res = NULL;
186 for ( UINT4 k = 0; k < numSegments; k++ ) {
187
188 /* starttime of segment */
189 dopplerParams_temp.refTime = recalcParams->startTstack->data[k];
190 /* convert LIGOTimeGPS into real number difference for XLALExtrapolatePulsarSpins */
191 REAL8 deltaTau = XLALGPSDiff( &dopplerParams_temp.refTime, &dopplerParams->refTime );
192 /* extrapolate pulsar spins to correct time for this segment */
193 XLAL_CHECK( XLALExtrapolatePulsarSpins( dopplerParams_temp.fkdot, dopplerParams->fkdot, deltaTau ) == XLAL_SUCCESS, XLAL_EFUNC, "XLALExtrapolatePulsarSpins() failed." );
194
195 /* recompute multi-detector Fstat and atoms */
196 XLAL_CHECK( XLALComputeFstat( &Fstat_res, recalcParams->Fstat_in_vec->data[k], &dopplerParams_temp, 1, FSTATQ_2F | FSTATQ_2F_PER_DET ) == XLAL_SUCCESS, XLAL_EFUNC, "XLALComputeFstat() failed with errno=%d", xlalErrno );
197
198 sumTwoF += Fstat_res->twoF[0]; /* sum up multi-detector Fstat for this segment*/
199
200 BOOLEAN update_loudest = FALSE;
201 BOOLEAN updated_twoFXloudestSeg[numDetectors];
202 if ( recalcParams->loudestSegOutput && ( Fstat_res->twoF[0] > recalcStats->twoFloudestSeg ) ) {
203 update_loudest = TRUE;
204 recalcStats->loudestSeg = k;
205 recalcStats->twoFloudestSeg = Fstat_res->twoF[0];
206 for ( UINT4 Y = 0; Y < numDetectors; Y++ ) {
207 updated_twoFXloudestSeg[Y] = FALSE;
208 }
209 }
210
211 /* for each segment, number of detectors with data might be smaller than overall number */
212 const UINT4 numDetectorsSeg = Fstat_res->numDetectors;
213
214 /* get single-detector Fstats, with correct detector matching in case of single-IFO segments */
215 for ( UINT4 X = 0; X < numDetectorsSeg; X++ ) {
216
217 /* match detector ID in this segment to one from detectorIDs list, sum up the corresponding twoFX */
218 detid = -1;
219 for ( UINT4 Y = 0; Y < numDetectors; Y++ ) {
220 if ( strcmp( Fstat_res->detectorNames[X], recalcParams->detectorIDs->data[Y] ) == 0 ) {
221 detid = Y;
222 }
223 }
224
225 XLAL_CHECK( detid != -1, XLAL_EFAILED, "For segment k=%d, detector X=%d, could not match detector ID %s.", k, X, Fstat_res->detectorNames[X] );
226
227 numSegmentsX[detid] += 1; /* have to keep this for correct averaging */
228
229 sumTwoFX[detid] += Fstat_res->twoFPerDet[X][0]; /* sum up single-detector Fstat for this segment*/
230
231 if ( update_loudest ) {
232 recalcStats->twoFXloudestSeg[detid] = Fstat_res->twoFPerDet[X][0];
233 updated_twoFXloudestSeg[detid] = TRUE;
234 }
235
236 maxTwoFXl[detid] = fmaxf( maxTwoFXl[detid], Fstat_res->twoFPerDet[X][0] );
237
238 } /* for X < numDetectorsSeg */
239
240 /* need to overwrite the twoFXloudestSeg when not all detectors have data in the loudest segment */
241 if ( update_loudest ) {
242 for ( UINT4 Y = 0; Y < numDetectors; Y++ ) {
243 if ( !updated_twoFXloudestSeg[Y] ) {
244 recalcStats->twoFXloudestSeg[Y] = 0.0;
245 }
246 }
247 } /* if ( update_loudest ) */
248
249 } /* for k < numSegments */
250
251 if ( recalcParams->BSGLsetup ) {
252 recalcStats->log10BSGL = XLALComputeBSGL( sumTwoF, sumTwoFX, recalcParams->BSGLsetup );
253 XLAL_CHECK( xlalErrno == 0, XLAL_EFUNC, "XLALComputeBSGL() failed with xlalErrno = %d\n", xlalErrno );
254
255 if ( recalcParams->computeBSGLtL ) {
256 recalcStats->log10BSGLtL = XLALComputeBSGLtL( sumTwoF, sumTwoFX, maxTwoFXl, recalcParams->BSGLsetup );
257 XLAL_CHECK( xlalErrno == 0, XLAL_EFUNC, "XLALComputeBSGLtL() failed with xlalErrno = %d\n", xlalErrno );
258 }
259 } // if BSGLsetup != NULL
260
261 /* get average stats over all segments */
262 recalcStats->avTwoF = sumTwoF / numSegments;
263 for ( UINT4 X = 0; X < numDetectors; X++ ) {
264 recalcStats->avTwoFX[X] = sumTwoFX[X] / numSegmentsX[X];
265 }
266
267 XLALDestroyFstatResults( Fstat_res );
268
269 return ( XLAL_SUCCESS );
270
271} /* XLALComputeExtraStatsSemiCoherent() */
void * toplist_elem(toplist_t *list, size_t ind)
Definition: HeapToplist.c:185
int j
int k
int XLALComputeExtraStatsSemiCoherent(RecalcStatsComponents *recalcStats, const PulsarDopplerParams *dopplerParams, const RecalcStatsParams *recalcParams)
XLAL Function to recalculate multi-IFO F-stat 2F and single-IFO 2FX for all semicoherent search segme...
#define TRUE
#define FALSE
int XLALComputeExtraStatsForToplist(toplist_t *list, const RecalcStatsParams *recalcParams)
XLAL function to go through a (Hough or GCT) toplist and compute line-robust statistics for each cand...
Functions to recompute statistics for GCT/hough toplist entries.
int XLALComputeFstat(FstatResults **Fstats, FstatInput *input, const PulsarDopplerParams *doppler, const UINT4 numFreqBins, const FstatQuantities whatToCompute)
Compute the -statistic over a band of frequencies.
Definition: ComputeFstat.c:760
void XLALDestroyFstatResults(FstatResults *Fstats)
Free all memory associated with a FstatResults structure.
Definition: ComputeFstat.c:934
@ FSTATQ_2F
Compute multi-detector .
Definition: ComputeFstat.h:96
@ FSTATQ_2F_PER_DET
Compute for each detector.
Definition: ComputeFstat.h:98
int XLALExtrapolatePulsarSpins(PulsarSpins fkdot1, const PulsarSpins fkdot0, REAL8 dtau)
Extrapolate the Pulsar spin-parameters (fkdot0) from the initial reference-epoch to the new referen...
#define LAL_REAL4_MAX
unsigned char BOOLEAN
#define XLAL_INIT_MEM(x)
double REAL8
#define XLAL_INIT_DECL(var,...)
uint32_t UINT4
int32_t INT4
float REAL4
REAL4 XLALComputeBSGLtL(const REAL4 twoF, const REAL4 twoFX[PULSAR_MAX_DETECTORS], const REAL4 maxtwoFlX[PULSAR_MAX_DETECTORS], const BSGLSetup *setup)
Single-bin wrapper of XLALVectorComputeBSGLtL(), provided for backwards compatibility.
REAL4 XLALComputeBSGL(const REAL4 twoF, const REAL4 twoFX[PULSAR_MAX_DETECTORS], const BSGLSetup *setup)
Single-bin wrapper of XLALVectorComputeBSGL(), provided for backwards compatibility.
#define PULSAR_MAX_DETECTORS
maximal number of detectors we can handle (for static arrays of detector quantities)
REAL4Vector * XLALCreateREAL4Vector(UINT4 length)
#define xlalErrno
#define XLAL_CHECK(assertion,...)
XLAL_EBADLEN
XLAL_SUCCESS
XLAL_EFAULT
XLAL_EFUNC
XLAL_EFAILED
REAL8 XLALGPSDiff(const LIGOTimeGPS *t1, const LIGOTimeGPS *t0)
size_t numDetectors
FstatInput ** data
Pointer to the data array.
Definition: ComputeFstat.h:87
UINT4 length
Number of elements in array.
Definition: ComputeFstat.h:86
XLALComputeFstat() computed results structure.
Definition: ComputeFstat.h:202
UINT4 numDetectors
Number of detectors over which the were computed.
Definition: ComputeFstat.h:221
REAL4 * twoFPerDet[PULSAR_MAX_DETECTORS]
If whatWasComputed & FSTATQ_2F_PER_DET is true, the values computed at numFreqBins frequencies space...
Definition: ComputeFstat.h:277
REAL4 * twoF
If whatWasComputed & FSTATQ_2F is true, the multi-detector values computed at numFreqBins frequencie...
Definition: ComputeFstat.h:242
CHAR detectorNames[PULSAR_MAX_DETECTORS][3]
Names of detectors over which the were computed.
Definition: ComputeFstat.h:225
Definition: GCTtoplist.h:42
Type to hold the fields that will be kept in a "toplist"
LIGOTimeGPS * data
array of timestamps
Definition: SFTfileIO.h:193
Type containing the 'Doppler-parameters' affecting the time-evolution of the phase.
PulsarSpins fkdot
Intrinsic spins: [Freq, f1dot, f2dot, ... ] where fkdot = d^kFreq/dt^k.
REAL8 Delta
Sky position: DEC (latitude) in equatorial coords and radians.
LIGOTimeGPS refTime
Reference time of pulsar parameters (in SSB!)
REAL8 Alpha
Sky position: RA (longitude) in equatorial coords and radians.
Type containing multi- and single-detector -statistics and line-robust statistic.
UINT4 numDetectors
number of detectors, numDetectors=0 should make all code ignore the TwoFX field.
REAL4 avTwoF
multi-detector -statistic, averaged over segments
REAL4 twoFXloudestSeg[PULSAR_MAX_DETECTORS]
single-IFO -stat values from the loudest segment in multi-
INT4 loudestSeg
index of the segment with the highest multi-detector -statistic
REAL4 avTwoFX[PULSAR_MAX_DETECTORS]
fixed-size array of single-detector -statistics, averaged over segments
REAL4 twoFloudestSeg
loudest single-segment, multi-detector -stat
REAL4 log10BSGL
line-robust statistic
Type containing input arguments for XLALComputeExtraStatsForToplist()
LALStringVector * detectorIDs
detector name vector with all detectors present in any data sements
BOOLEAN computeBSGLtL
re-compute BSGLtL as well, or not
const char * listEntryTypeName
type of toplist entries, give as name string
BSGLSetup * BSGLsetup
pre-computed setup for line-robust statistic BSGL
LIGOTimeGPSVector * startTstack
starting GPS time of each stack
FstatInputVector * Fstat_in_vec
vector of input data for XLALComputeFstat()
BOOLEAN loudestSegOutput
return extra info about loudest segment
LIGOTimeGPS refTimeGPS
reference time for fkdot values in toplist
char ** heap
Definition: HeapToplist.h:40
size_t elems
Definition: HeapToplist.h:37
char * data
Definition: HeapToplist.h:39