28#include <lal/StringInput.h>
29#include <lal/AVFactories.h>
30#include <lal/LALConstants.h>
31#include <lal/LALStdio.h>
32#include <lal/LogPrintf.h>
33#include <lal/HeapToplist.h>
34#include <lal/LALPulsarVCSInfo.h>
48#define snprintf _snprintf
56#define finite isfinite
70static FILE *debugfp = NULL;
75#define min(a,b) ((a)<(b)?(a):(b))
79#define SYNC_FAIL_LIMIT 5
97 fprintf( debugfp,
"%20lf %20lf\n%20lf %20lf\n%20lf %20lf\n%20lf %20lf\n\n",
129 fprintf( debugfp,
"%20lf %20lf\n%20u %20u\n\n",
152 fprintf( debugfp,
"%20lf %20lf\n%20u %20u\n\n",
175 fprintf( debugfp,
"%20lf %20lf\n%20lf %20lf\n\n",
196 fprintf( debugfp,
"%20lf %20lf\n%20lf %20lf\n\n",
217 fprintf( debugfp,
"%20lf %20lf\n%20lf %20lf\n\n",
238 void const *
const *pa = (
void const *
const * )
a;
239 void const *
const *pb = (
void const *
const * )b;
250 debugfp = fopen(
"debug_sort",
"w" );
302 char BSGLstr[256] =
"";
304 snprintf( BSGLstr,
sizeof( BSGLstr ),
" %.6f", fline.
log10BSGL );
309 char BSGLtLstr[256] =
"";
311 snprintf( BSGLtLstr,
sizeof( BSGLtLstr ),
" %.6f", fline.
log10BSGLtL );
313 char BtSGLtLstr[256] =
"";
315 snprintf( BtSGLtLstr,
sizeof( BtSGLtLstr ),
" %.6f", fline.
log10BtSGLtL );
319 char maxTwoFstr[256] =
"";
321 snprintf( maxTwoFstr,
sizeof( maxTwoFstr ),
" %.6f %d", fline.
maxTwoFl, fline.
maxTwoFlSeg );
327 char recalcStr[256] =
"";
330 snprintf( recalcStr,
sizeof( recalcStr ),
" %.6f", fline.
avTwoFrecalc );
333 strcat( recalcStr, buf0 );
339 strcat( recalcStr, buf0 );
345 strcat( recalcStr, buf0 );
352 len = snprintf( buf, buflen,
353 "%.16g %.13g %.13g %.13g %.13g %.13g %d %.6f%s%s%s%s%s\n",
369 len = snprintf( buf, buflen,
370 "%.16g %.13g %.13g %.13g %.13g %d %.6f%s%s%s%s%s\n",
393 UINT4 len = strlen( outstr );
395 char buf0[outstrlen];
397 snprintf( buf0,
sizeof( buf0 ),
" %.6f", quantities[X] );
398 len = strlen( outstr ) + strlen( buf0 ) + 1;
399 if ( len > outstrlen ) {
400 XLALPrintError(
"%s: assembled output string too long! (%d > %zu)\n", fn, len, outstrlen );
403 strcat( outstr, buf0 );
415 UINT4 len = strlen( outstr );
417 char buf0[outstrlen];
419 snprintf( buf0,
sizeof( buf0 ),
" %d", quantities[X] );
420 len = strlen( outstr ) + strlen( buf0 ) + 1;
421 if ( len > outstrlen ) {
422 XLALPrintError(
"%s: assembled output string too long! (%d > %zu)\n", fn, len, outstrlen );
425 strcat( outstr, buf0 );
446 if ( length >
sizeof( linebuf ) - 1 ) {
451 for (
i = 0;
i < length;
i++ ) {
452 *checksum += linebuf[
i];
499 errno, strerror( errno ),
i );
522#define TEMP_EXT ".tmp"
524 tempname = (
char * )malloc(
s );
532 fpnew = fopen( tempname,
"wb" );
535 tempname, errno, strerror( errno ) );
553 ret =
fprintf( fpnew,
"%s", VCSInfoString );
578 const CHAR *sortstat = NULL;
588 sortstat =
"BtSGLtL";
590 LogPrintf(
LOG_CRITICAL,
"Failed to write toplist sorting line, toplist is sorted by unknown statistic.\n" );
594 ret =
fprintf( fpnew,
"%%%% candidates selected by %s as toplist statistic\n", sortstat );
626 if ( ( write_done ) && ( length >= 0 ) ) {
627 ret =
fprintf( fpnew,
"%%DONE\n" );
639 tempname, errno, strerror( errno ) );
647 if ( ( ret = rename( tempname,
filename ) ) ) {
649 filename, ret, strerror( errno ), errno );
671#define __func__ __FUNCTION__
678#define LOGIOERROR(mess,filename) \
679 LogPrintf(LOG_CRITICAL, "ERROR: %s %s: %s (%s:%d): doserr:%d, errno:%d: %s\n",\
680 mess,filename,__func__,__FILE__,__LINE__,_doserrno,errno,strerror(errno))
682#define LOGIOERROR(mess,filename) \
683 LogPrintf(LOG_CRITICAL, "ERROR: %s %s: %s (%s:%d): errno:%d: %s\n",\
684 mess,filename,__func__,__FILE__,__LINE__,errno,strerror(errno))
690#define TMP_EXT ".tmp"
695 static UINT4 sync_fail_counter = 0;
705 tmpfilename =
LALCalloc( len,
sizeof(
char ) );
706 if ( !tmpfilename ) {
711 strcat( tmpfilename,
TMP_EXT );
714 fp = fopen( tmpfilename,
"wb" );
722 len = fwrite( &( tl->
elems ),
sizeof( tl->
elems ), 1,
fp );
724 LOGIOERROR(
"Couldn't write elems to", tmpfilename );
726 if ( fclose(
fp ) ) {
727 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
735 if ( len != tl->
elems ) {
736 LOGIOERROR(
"Couldn't write data to", tmpfilename );
738 if ( fclose(
fp ) ) {
739 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
748 len = fwrite( &idx,
sizeof( idx ), 1,
fp );
750 LOGIOERROR(
"Couldn't write idx to", tmpfilename );
752 if ( fclose(
fp ) ) {
753 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
758 for ( len = 0; len <
sizeof( idx ); len++ ) {
759 checksum += *( ( (
char * )&idx ) + len );
765 len = fwrite( &(
t2->elems ),
sizeof(
t2->elems ), 1,
fp );
767 LOGIOERROR(
"Couldn't write elems to", tmpfilename );
769 if ( fclose(
fp ) ) {
770 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
777 len = fwrite(
t2->data,
t2->size,
t2->elems,
fp );
778 if ( len !=
t2->elems ) {
779 LOGIOERROR(
"Couldn't write data to", tmpfilename );
781 if ( fclose(
fp ) ) {
782 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
791 len = fwrite( &idx,
sizeof( idx ), 1,
fp );
793 LOGIOERROR(
"Couldn't write idx to", tmpfilename );
795 if ( fclose(
fp ) ) {
796 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
801 for ( len = 0; len <
sizeof( idx ); len++ ) {
802 checksum += *( ( (
char * )&idx ) + len );
812 len = fwrite( &( t3->
elems ),
sizeof( t3->
elems ), 1,
fp );
814 LOGIOERROR(
"Couldn't write elems to", tmpfilename );
816 if ( fclose(
fp ) ) {
817 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
825 if ( len != t3->
elems ) {
826 LOGIOERROR(
"Couldn't write data to", tmpfilename );
828 if ( fclose(
fp ) ) {
829 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
838 len = fwrite( &idx,
sizeof( idx ), 1,
fp );
840 LOGIOERROR(
"Couldn't write idx to", tmpfilename );
842 if ( fclose(
fp ) ) {
843 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
848 for ( len = 0; len <
sizeof( idx ); len++ ) {
849 checksum += *( ( (
char * )&idx ) + len );
856 len = fwrite( &counter,
sizeof( counter ), 1,
fp );
858 LOGIOERROR(
"Couldn't write counter to", tmpfilename );
860 if ( fclose(
fp ) ) {
861 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
868 for ( len = 0; len <
sizeof( tl->
elems ); len++ ) {
869 checksum += *( ( (
char * ) & ( tl->
elems ) ) + len );
871 for ( len = 0; len < ( tl->
elems * tl->
size ); len++ ) {
872 checksum += *( ( (
char * )tl->
data ) + len );
875 for ( len = 0; len <
sizeof(
t2->elems ); len++ ) {
876 checksum += *( ( (
char * ) & (
t2->elems ) ) + len );
878 for ( len = 0; len < (
t2->elems *
t2->size ); len++ ) {
879 checksum += *( ( (
char * )
t2->data ) + len );
884 for ( len = 0; len <
sizeof( t3->
elems ); len++ ) {
885 checksum += *( ( (
char * ) & ( t3->
elems ) ) + len );
887 for ( len = 0; len < ( t3->
elems * t3->
size ); len++ ) {
888 checksum += *( ( (
char * )t3->
data ) + len );
892 for ( len = 0; len <
sizeof( counter ); len++ ) {
893 checksum += *( ( (
char * )&counter ) + len );
897 len = fwrite( &checksum,
sizeof( checksum ), 1,
fp );
899 LOGIOERROR(
"Couldn't write checksum to", tmpfilename );
901 if ( fclose(
fp ) ) {
902 LOGIOERROR(
"In addition: couldn't close", tmpfilename );
910 if ( fsync( fileno(
fp ) ) ) {
917 sync_fail_counter = 0;
922 if ( fclose(
fp ) ) {
929 if ( rename( tmpfilename,
filename ) ) {
930 LOGIOERROR(
"Couldn't rename\n", tmpfilename );
945 UINT4 checksum, indexsum = 0;
959 if ( errno == ENOENT ) {
977 len = fread( &( tl->
elems ),
sizeof( tl->
elems ), 1,
fp );
981 if ( fclose(
fp ) ) {
989 "Number of elements read larger than length of toplist: %zu > %zu\n",
991 if ( fclose(
fp ) ) {
999 if ( len != tl->
elems ) {
1002 if ( fclose(
fp ) ) {
1015 len = fread( &idx,
sizeof( idx ), 1,
fp );
1019 if ( fclose(
fp ) ) {
1025 for ( len = 0; len <
sizeof( idx ); len++ ) {
1026 indexsum += *( ( (
char * )&idx ) + len );
1032 len = fread( &(
t2->elems ),
sizeof(
t2->elems ), 1,
fp );
1036 if ( fclose(
fp ) ) {
1042 if (
t2->elems >
t2->length ) {
1044 "Number of elements read larger than length of toplist: %zu > %zu\n",
1045 t2->elems,
t2->length );
1046 if ( fclose(
fp ) ) {
1053 len = fread(
t2->data,
t2->size,
t2->elems,
fp );
1054 if ( len !=
t2->elems ) {
1057 if ( fclose(
fp ) ) {
1068 len = fread( &idx,
sizeof( idx ), 1,
fp );
1072 if ( fclose(
fp ) ) {
1077 t2->heap[
i] = (
char * )(
t2->data + idx *
t2->size );
1078 for ( len = 0; len <
sizeof( idx ); len++ ) {
1079 indexsum += *( ( (
char * )&idx ) + len );
1086 len = fread( &( t3->
elems ),
sizeof( t3->
elems ), 1,
fp );
1090 if ( fclose(
fp ) ) {
1098 "Number of elements read larger than length of toplist: %zu > %zu\n",
1100 if ( fclose(
fp ) ) {
1108 if ( len != t3->
elems ) {
1111 if ( fclose(
fp ) ) {
1123 len = fread( &idx,
sizeof( idx ), 1,
fp );
1127 if ( fclose(
fp ) ) {
1133 for ( len = 0; len <
sizeof( idx ); len++ ) {
1134 indexsum += *( ( (
char * )&idx ) + len );
1141 len = fread( counter,
sizeof( *counter ), 1,
fp );
1145 if ( fclose(
fp ) ) {
1159 len = fread( &checksum,
sizeof( checksum ), 1,
fp );
1163 if ( fclose(
fp ) ) {
1178 if ( fclose(
fp ) ) {
1192 checksum -= indexsum;
1193 for ( len = 0; len <
sizeof( tl->
elems ); len++ ) {
1194 checksum -= *( ( (
char * ) & ( tl->
elems ) ) + len );
1196 for ( len = 0; len < ( tl->
elems * tl->
size ); len++ ) {
1197 checksum -= *( ( (
char * )tl->
data ) + len );
1200 for ( len = 0; len <
sizeof(
t2->elems ); len++ ) {
1201 checksum -= *( ( (
char * ) & (
t2->elems ) ) + len );
1203 for ( len = 0; len < (
t2->elems *
t2->size ); len++ ) {
1204 checksum -= *( ( (
char * )
t2->data ) + len );
1208 for ( len = 0; len <
sizeof( t3->
elems ); len++ ) {
1209 checksum -= *( ( (
char * ) & ( t3->
elems ) ) + len );
1211 for ( len = 0; len < ( t3->
elems * t3->
size ); len++ ) {
1212 checksum -= *( ( (
char * )t3->
data ) + len );
1215 for ( len = 0; len <
sizeof( *counter ); len++ ) {
1216 checksum -= *( ( (
char * )counter ) + len );
1259static void dump_heap_order(
const toplist_t *tl,
const char *
name )
1263 if ( (
fp = fopen(
name,
"w" ) ) ) {
1264 for (
i = 0;
i < tl->
elems;
i++ ) {
1271static void sort_gctFstat_toplist_debug(
toplist_t *l )
1274 debugfp = fopen(
"debug_sort",
"w" );
1291 dump_heap_order( tl,
"heap_before.dump" );
1292 sort_gctFstat_toplist_debug( tl );
1293 dump_heap_order( tl,
"heap_after.dump" );
static void reduce_gctFstatline_precision(void *line)
void sort_gctFstat_toplist(toplist_t *l)
sorts the toplist with an internal sorting function, used before finally writing it
int create_gctFstat_toplist(toplist_t **tl, UINT8 length, SortBy_t whatToSortBy)
creates a toplist with length elements, returns -1 on error (usually out of memory),...
void free_gctFstat_toplist(toplist_t **l)
frees the space occupied by the toplist
int clear_gct_checkpoint(const char *filename)
removes a checkpoint returns 0 on success, errno on failure
static int print_single_detector_quantities_to_str(char *outstr, size_t outstrlen, const REAL4 *quantities, const UINT4 numDetectors)
static int write_gctFstat_toplist_item_to_fp(GCTtopOutputEntry fline, FILE *fp, UINT4 *checksum)
static int print_gctFstatline_to_str(GCTtopOutputEntry fline, char *buf, int buflen)
static int gctNC_smaller(const void *a, const void *b)
#define __func__
log an I/O error, i.e.
static void reduce_gctFstat_toplist_precision(toplist_t *l)
static int gctBSGLtL_smaller(const void *a, const void *b)
static int gctFstat_result_order(const void *a, const void *b)
static int gctBtSGLtL_smaller(const void *a, const void *b)
char * global_column_headings_stringp
static int gctFstat_smaller(const void *a, const void *b)
static int print_single_detector_intval_to_str(char *outstr, size_t outstrlen, const INT4 *quantities, const UINT4 numDetectors)
static int gctFstat_final_qsort(const void *a, const void *b)
int insert_into_gctFstat_toplist(toplist_t *tl, GCTtopOutputEntry *elem)
Inserts an element in to the toplist either if there is space left or the element is larger than the ...
int write_gctFstat_toplist_to_fp(toplist_t *tl, FILE *fp, UINT4 *checksum)
Writes the toplist to an (already open) filepointer Returns the number of written charactes sets the ...
static int gctBSGL_smaller(const void *a, const void *b)
int read_gct_checkpoint(const char *filename, toplist_t *tl, toplist_t *t2, toplist_t *t3, UINT4 *counter)
tries to read a checkpoint
int write_gct_checkpoint(const char *filename, toplist_t *tl, toplist_t *t2, toplist_t *t3, UINT4 counter, BOOLEAN do_sync)
Checkpointing.
#define LOGIOERROR(mess, filename)
int write_hfs_oputput(const char *filename, toplist_t *tl)
write the final output file:
static int _atomic_write_gctFstat_toplist_to_file(toplist_t *l, const char *filename, UINT4 *checksum, int write_done)
SortBy_t
enumerate all toplist-sorting options: by F (0), number-count (1), BSGL (2), "dual" toplists F + BSGL...
int create_toplist(toplist_t **list, size_t length, size_t size, int(*smaller)(const void *, const void *))
void free_toplist(toplist_t **list)
int insert_into_toplist(toplist_t *list, void *element)
void go_through_toplist(toplist_t *list, void(*handle)(void *))
void clear_toplist(toplist_t *list)
const LALVCSInfoList lalPulsarVCSInfoList
NULL-terminated list of VCS and build information for LALPulsar and its dependencies
#define XLAL_LAST_ELEM(x)
char * XLALVCSInfoString(const LALVCSInfoList vcs_list, const int verbose, const char *prefix)
void LogPrintf(LogLevel_t, const char *format,...) _LAL_GCC_PRINTF_FORMAT_(2
int XLALPrintError(const char *fmt,...) _LAL_GCC_PRINTF_FORMAT_(1
REAL4 log10BSGLtL
Line-robust statistic .
REAL4 maxTwoFXl[PULSAR_MAX_DETECTORS]
fixed-size array of single-detector maximum 2F over segments
INT4 maxTwoFlSeg
index of segment with maximum multi-detector 2F
UINT4 numDetectors
number of detectors for optional avTwoFX arrays
REAL4 avTwoFXrecalc[PULSAR_MAX_DETECTORS]
fixed-size array of single-detector -values as recomputed by recalcToplistStats
REAL4 log10BSGLrecalc
Line-robust statistic recomputed by recalcToplistStats.
REAL4 log10BSGLtLrecalc
Line-robust statistic recomputed by recalcToplistStats.
INT4 loudestSeg
index of the loudest segment in multi-F
REAL4 maxTwoFl
multi-detector maximum 2F over segments
REAL4 log10BtSGLtL
Line-robust transient-CW statistic .
REAL4 twoFXloudestSeg[PULSAR_MAX_DETECTORS]
single-IFO F-stat values from the loudest segment in multi-F
REAL4 log10BSGL
Line-robust statistic .
REAL8 F2dot
spindown value f2dot = d2f/dt2
REAL8 Alpha
skyposition: longitude in equatorial coords, radians
REAL8 Delta
skyposition: latitude
REAL8 F1dot
spindown value f1dot = df/dt
REAL4 twoFloudestSeg
loudest single-segment multi-IFO F-stat value
REAL4 avTwoFX[PULSAR_MAX_DETECTORS]
fixed-size array of single-detector average -values
INT4 maxTwoFXlSeg[PULSAR_MAX_DETECTORS]
fixed-size array of index of segment with maximum single-detector 2F
BOOLEAN have_f3dot
output F3dot value
REAL8 F3dot
spindown value f2dot = d2f/dt2
REAL4 avTwoFrecalc
average of -values as recomputed by recalcToplistStats
REAL4 avTwoF
average of F-statistic values over segments