Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALApps 10.1.0.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
randombank.c
Go to the documentation of this file.
1/*
2* Copyright (C) 2007 Duncan Brown
3*
4* This program is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License as published by
6* the Free Software Foundation; either version 2 of the License, or
7* (at your option) any later version.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU General Public License for more details.
13*
14* You should have received a copy of the GNU General Public License
15* along with with program; see the file COPYING. If not, write to the
16* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17* MA 02110-1301 USA
18*/
19
20/*-----------------------------------------------------------------------
21 *
22 * File Name: randombank.c
23 *
24 * Author: Brown, D. A.
25 *
26 *
27 *-----------------------------------------------------------------------
28 */
29
30#include <config.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <sys/types.h>
35#include <sys/stat.h>
36#include <fcntl.h>
37#include <regex.h>
38#include <time.h>
39
40#include <series.h>
41
42#include <lal/LALConfig.h>
43#include <lal/LALgetopt.h>
44#include <lal/LALStdio.h>
45#include <lal/LALStdlib.h>
46#include <lal/LALError.h>
47#include <lal/LALDatatypes.h>
48#include <lal/AVFactories.h>
49#include <lal/LIGOLwXML.h>
50#include <lal/LIGOLwXMLRead.h>
51#include <lal/LIGOMetadataTables.h>
52#include <lal/LIGOMetadataUtils.h>
53#include <lal/Units.h>
54#include <lal/Date.h>
55#include <lal/LALInspiral.h>
56#include <lal/LALInspiralBank.h>
57#include "inspiral.h"
58#include <LALAppsVCSInfo.h>
59
60#define CVS_ID_STRING "$Id$"
61#define CVS_NAME_STRING "$Name$"
62#define CVS_REVISION "$Revision$"
63#define CVS_SOURCE "$Source$"
64#define CVS_DATE "$Date$"
65#define PROGRAM_NAME "tmpltbank"
66
67int arg_parse_check( int argc, char *argv[], ProcessParamsTable *procparams );
68
69/*
70 *
71 * variables that control program behaviour
72 *
73 */
74
75/* debugging */
76extern int vrbflg; /* verbocity of lal function */
77
78/* template bank generation parameters */
79LIGOTimeGPS gpsStartTime = { 0, 0 }; /* input data GPS start time */
80LIGOTimeGPS gpsEndTime = { 0, 0 }; /* input data GPS end time */
81REAL4 minMass = -1; /* minimum component mass */
82REAL4 maxMass = -1; /* maximum component mass */
83REAL4 fLow = -1; /* low frequency cutoff */
84INT4 numTmplts = -1; /* number of template to create */
85enum { unset, urandom, user } randSeedType = unset; /* sim seed type */
86INT4 randomSeed = 0; /* value of sim rand seed */
87
88/* output parameters */
89CHAR *userTag = NULL;
91
92int main ( int argc, char *argv[] )
93{
94 /* lal function variables */
96
97 /* templates */
99 InspiralTemplate newTmplt;
100 SnglInspiralTable *thisTmplt = NULL;
101
102 /* output data */
103 SnglInspiralTable *templateBank = NULL;
104 ProcessTable *proctable;
105 ProcessParamsTable *procparams;
106 LIGOLwXMLStream *results;
107
108 /* counters and other variables */
109 INT4 i;
110 CHAR fname[256];
111
112
113 /*
114 *
115 * initialization
116 *
117 */
118
119
120 /* set up inital debugging values */
122
123 /* create the process and process params tables */
124 proctable = (ProcessTable *)
125 calloc( 1, sizeof(ProcessTable) );
126 XLALGPSTimeNow(&(proctable->start_time));
129 procparams = (ProcessParamsTable *) calloc( 1, sizeof(ProcessParamsTable) );
130
131 /* call the argument parse and check function */
132 arg_parse_check( argc, argv, procparams );
133
134 /* can use LALMalloc() / LALCalloc() from here */
135
136
137 /*
138 *
139 * create the radom number seed
140 *
141 */
142
143
144 if ( randSeedType == urandom )
145 {
146 FILE *fpRand = NULL;
147 INT4 randByte;
148
149 if ( vrbflg )
150 fprintf( stdout, "obtaining random seed from /dev/urandom: " );
151
152 randomSeed = 0;
153 fpRand = fopen( "/dev/urandom", "r" );
154 if ( fpRand )
155 {
156 for ( randByte = 0; randByte < 4 ; ++randByte )
157 {
158 INT4 tmpSeed = (INT4) fgetc( fpRand );
159 randomSeed += tmpSeed << ( randByte * 8 );
160 }
161 fclose( fpRand );
162 }
163 else
164 {
165 perror( "error obtaining random seed from /dev/urandom" );
166 exit( 1 );
167 }
168 }
169 else if ( randSeedType == user )
170 {
171 if ( vrbflg )
172 fprintf( stdout, "using user specified random seed: " );
173 }
174
175 if ( vrbflg ) fprintf( stdout, "%d\n", randomSeed );
176
177 /* create the tmplt bank random parameter structure */
179 &status );
180
181
182 /*
183 *
184 * create a random template bank
185 *
186 */
187
188
189 for ( i = 0; i < numTmplts; ++i )
190 {
191 memset( &newTmplt, 0, sizeof(InspiralTemplate) );
192 newTmplt.massChoice = m1Andm2;
193 newTmplt.order = LAL_PNORDER_TWO;
194 newTmplt.fLower = fLow;
195
196 /* set up the injection masses */
197 if ( maxMass == minMass )
198 {
199 newTmplt.mass1 = (REAL8) maxMass;
200 newTmplt.mass2 = (REAL8) maxMass;
201 }
202 else
203 {
204 REAL4 mass;
205 /* generate random parameters for the injection */
207 newTmplt.mass1 = (maxMass - minMass) * mass;
208 newTmplt.mass1 += minMass;
209
211 newTmplt.mass2 = (maxMass - minMass) * mass;
212 newTmplt.mass2 += minMass;
213 }
214
216
217 if ( ! templateBank )
218 {
219 thisTmplt = templateBank =
221 }
222 else
223 {
224 thisTmplt = thisTmplt->next =
226 }
227
228 thisTmplt->mass1 = newTmplt.mass1;
229 thisTmplt->mass2 = newTmplt.mass2;
230 thisTmplt->mchirp = newTmplt.chirpMass;
231 thisTmplt->eta = newTmplt.eta;
232 thisTmplt->tau0 = newTmplt.t0;
233 thisTmplt->tau2 = newTmplt.t2;
234 thisTmplt->tau3 = newTmplt.t3;
235 thisTmplt->tau4 = newTmplt.t4;
236 thisTmplt->tau5 = newTmplt.t5;
237 thisTmplt->ttotal = newTmplt.tC;
238 thisTmplt->psi0 = newTmplt.psi0;
239 thisTmplt->psi3 = newTmplt.psi3;
240 thisTmplt->f_final = newTmplt.fFinal;
241 thisTmplt->eta = newTmplt.eta;
242 thisTmplt->beta = newTmplt.beta;
243 snprintf( thisTmplt->ifo, LIGOMETA_IFO_MAX, "P1" );
244 snprintf( thisTmplt->search, LIGOMETA_SEARCH_MAX, "randombank" );
245 snprintf( thisTmplt->channel, LIGOMETA_CHANNEL_MAX, "SIM-BANK" );
246 }
247
248
249 /*
250 *
251 * write the output data
252 *
253 */
254
255 /* open the output xml file */
256 if ( userTag && !outCompress )
257 {
258 snprintf( fname, sizeof(fname), "P1-TMPLTBANK_%s-%d-%d.xml",
261 }
262 else if ( userTag && outCompress )
263 {
264 snprintf( fname, sizeof(fname), "P1-TMPLTBANK_%s-%d-%d.xml.gz",
267 }
268 else if ( !userTag && outCompress )
269 {
270 snprintf( fname, sizeof(fname), "P1-TMPLTBANK-%d-%d.xml.gz",
273 }
274 else
275 {
276 snprintf( fname, sizeof(fname), "P1-TMPLTBANK-%d-%d.xml",
279 }
280 results = XLALOpenLIGOLwXMLFile( fname );
281
282 /* write the process table */
283 snprintf( proctable->ifos, LIGOMETA_IFO_MAX, "P1" );
284 XLALGPSTimeNow(&(proctable->end_time));
285 XLALWriteLIGOLwXMLProcessTable( results, proctable );
286 free( proctable );
287
288 /* erase the first empty process params entry */
289 {
290 ProcessParamsTable *emptyPPtable = procparams;
291 procparams = procparams->next;
292 free( emptyPPtable );
293 }
294
295 /* write the process params table */
296 XLALWriteLIGOLwXMLProcessParamsTable( results, procparams );
297 XLALDestroyProcessParamsTable( procparams );
298
299 /* write the template bank to the file */
300 if ( templateBank )
301 XLALWriteLIGOLwXMLSnglInspiralTable( results, templateBank );
302 while ( templateBank )
303 {
304 thisTmplt = templateBank;
305 templateBank = templateBank->next;
306 LALFree( thisTmplt );
307 }
308
309 /* close the output xml file */
310 XLALCloseLIGOLwXMLFile( results );
311
312 /* free the rest of the memory, check for memory leaks and exit */
315 exit( 0 );
316}
317
318
319/* ------------------------------------------------------------------------- */
320
321#define ADD_PROCESS_PARAM( pptype, format, ppvalue ) \
322this_proc_param = this_proc_param->next = (ProcessParamsTable *) \
323 calloc( 1, sizeof(ProcessParamsTable) ); \
324 snprintf( this_proc_param->program, LIGOMETA_PROGRAM_MAX, "%s", \
325 PROGRAM_NAME ); \
326 snprintf( this_proc_param->param, LIGOMETA_PARAM_MAX, "--%s", \
327 long_options[option_index].name ); \
328 snprintf( this_proc_param->type, LIGOMETA_TYPE_MAX, "%s", pptype ); \
329 snprintf( this_proc_param->value, LIGOMETA_VALUE_MAX, format, ppvalue );
330
331#define USAGE \
332" --help display this message\n"\
333" --version print version information and exit\n"\
334" --user-tag STRING set the process_params usertag to STRING\n"\
335"\n"\
336" --gps-start-time SEC GPS second of data start time\n"\
337" --gps-end-time SEC GPS second of data end time\n"\
338" --low-frequency-cutoff F Compute tau parameters from F Hz\n"\
339" --minimum-mass MASS set minimum component mass of bank to MASS\n"\
340" --maximum-mass MASS set maximum component mass of bank to MASS\n"\
341" --number-of-template N create N random templatess of bank to MASS\n"\
342" --random-seed SEED set random number seed for injections to SEED\n"\
343 " (urandom|integer)\n"\
344" --write-compress write a compressed xml file\n"\
345 "\n"
346
347int arg_parse_check( int argc, char *argv[], ProcessParamsTable *procparams )
348{
349 /* LALgetopt arguments */
350 struct LALoption long_options[] =
351 {
352 /* these options set a flag */
353 {"verbose", no_argument, &vrbflg, 1 },
354 {"write-compress", no_argument, &outCompress, 1 },
355 {"help", no_argument, 0, 'h'},
356 {"gps-start-time", required_argument, 0, 'a'},
357 {"gps-end-time", required_argument, 0, 'b'},
358 {"low-frequency-cutoff", required_argument, 0, 'i'},
359 {"minimum-mass", required_argument, 0, 'A'},
360 {"maximum-mass", required_argument, 0, 'B'},
361 {"number-of-templates", required_argument, 0, 'K'},
362 {"random-seed", required_argument, 0, 'J'},
363 {"user-tag", required_argument, 0, 'Z'},
364 {"userTag", required_argument, 0, 'Z'},
365 {0, 0, 0, 0}
366 };
367 int c;
368 ProcessParamsTable *this_proc_param = procparams;
369
370
371 /*
372 *
373 * parse command line arguments
374 *
375 */
376
377
378 while ( 1 )
379 {
380 /* LALgetopt_long stores long option here */
381 int option_index = 0;
382 size_t LALoptarg_len;
383
384 c = LALgetopt_long_only( argc, argv, "hs:a:b:i:A:B:K:J:Z:",
385 long_options, &option_index );
386
387 /* detect the end of the options */
388 if ( c == - 1 )
389 {
390 break;
391 }
392
393 switch ( c )
394 {
395 case 0:
396 /* if this option set a flag, do nothing else now */
397 if ( long_options[option_index].flag != 0 )
398 {
399 break;
400 }
401 else
402 {
403 fprintf( stderr, "error parsing option %s with argument %s\n",
404 long_options[option_index].name, LALoptarg );
405 exit( 1 );
406 }
407 break;
408
409 case 'h':
410 fprintf( stdout, USAGE );
411 exit( 0 );
412 break;
413
414 case 'Z':
415 /* create storage for the usertag */
416 LALoptarg_len = strlen( LALoptarg ) + 1;
417 userTag = (CHAR *) calloc( LALoptarg_len, sizeof(CHAR) );
418 memcpy( userTag, LALoptarg, LALoptarg_len );
419
420 this_proc_param = this_proc_param->next = (ProcessParamsTable *)
421 calloc( 1, sizeof(ProcessParamsTable) );
422 snprintf( this_proc_param->program, LIGOMETA_PROGRAM_MAX, "%s",
423 PROGRAM_NAME );
424 snprintf( this_proc_param->param, LIGOMETA_PARAM_MAX, "-userTag" );
425 snprintf( this_proc_param->type, LIGOMETA_TYPE_MAX, "string" );
426 snprintf( this_proc_param->value, LIGOMETA_VALUE_MAX, "%s",
427 LALoptarg );
428 break;
429
430 case 'A':
431 minMass = (REAL4) atof( LALoptarg );
432 if ( minMass <= 0 )
433 {
434 fprintf( stdout, "invalid argument to --%s:\n"
435 "miniumum component mass must be > 0: "
436 "(%f solar masses specified)\n",
437 long_options[option_index].name, minMass );
438 exit( 1 );
439 }
440 ADD_PROCESS_PARAM( "float", "%e", minMass );
441 break;
442
443 case 'B':
444 maxMass = (REAL4) atof( LALoptarg );
445 if ( maxMass <= 0 )
446 {
447 fprintf( stdout, "invalid argument to --%s:\n"
448 "maxiumum component mass must be > 0: "
449 "(%f solar masses specified)\n",
450 long_options[option_index].name, maxMass );
451 exit( 1 );
452 }
453 ADD_PROCESS_PARAM( "float", "%e", maxMass );
454 break;
455
456 case 'J':
457 if ( ! strcmp( "urandom", LALoptarg ) )
458 {
460 ADD_PROCESS_PARAM( "string", "%s", LALoptarg );
461 }
462 else
463 {
465 randomSeed = (INT4) atoi( LALoptarg );
466 ADD_PROCESS_PARAM( "int", "%d", randomSeed );
467 }
468 break;
469
470 case 'K':
471 numTmplts = (INT4) atoi( LALoptarg );
472 if ( numTmplts < 1 )
473 {
474 fprintf( stderr, "invalid argument to --%s:\n"
475 "number of template bank simulations"
476 "must be greater than 1: (%d specified)\n",
477 long_options[option_index].name, numTmplts );
478 exit( 1 );
479 }
480 ADD_PROCESS_PARAM( "int", "%d", numTmplts );
481 break;
482
483 case 'a':
484 {
485 long int gstartt = atol( LALoptarg );
486 if ( gstartt < 441417609 )
487 {
488 fprintf( stderr, "invalid argument to --%s:\n"
489 "GPS start time is prior to "
490 "Jan 01, 1994 00:00:00 UTC:\n"
491 "(%ld specified)\n",
492 long_options[option_index].name, gstartt );
493 exit( 1 );
494 }
495 gpsStartTime.gpsSeconds = (INT4) gstartt;
497 ADD_PROCESS_PARAM( "int", "%ld", gstartt );
498 }
499 break;
500
501 case 'b':
502 {
503 long int gendt = atol( LALoptarg );
504 if ( gendt < 441417609 )
505 {
506 fprintf( stderr, "invalid argument to --%s:\n"
507 "GPS end time is prior to "
508 "Jan 01, 1994 00:00:00 UTC:\n"
509 "(%ld specified)\n",
510 long_options[option_index].name, gendt );
511 exit( 1 );
512 }
513 gpsEndTime.gpsSeconds = (INT4) gendt;
515 ADD_PROCESS_PARAM( "int", "%ld", gendt );
516 }
517 break;
518
519 case 'i':
520 fLow = (REAL4) atof( LALoptarg );
521 if ( fLow < 0 )
522 {
523 fprintf( stdout, "invalid argument to --%s:\n"
524 "low frequency cutoff is less than 0 Hz: "
525 "(%f Hz specified)\n",
526 long_options[option_index].name, fLow );
527 exit( 1 );
528 }
529 ADD_PROCESS_PARAM( "float", "%e", fLow );
530 break;
531
532 case '?':
533 fprintf( stderr, USAGE );
534 exit( 1 );
535 break;
536
537 default:
538 fprintf( stderr, "unknown error while parsing options\n" );
539 fprintf( stderr, USAGE );
540 exit( 1 );
541 }
542 }
543
544 if ( LALoptind < argc )
545 {
546 fprintf( stderr, "extraneous command line arguments:\n" );
547 while ( LALoptind < argc )
548 {
549 fprintf ( stderr, "%s\n", argv[LALoptind++] );
550 }
551 exit( 1 );
552 }
553
554 /*
555 *
556 * check validity of arguments
557 *
558 */
559
560
561 if ( minMass < 0 )
562 {
563 fprintf( stderr, "--minimum-mass must be specified\n" );
564 exit( 1 );
565 }
566 if ( maxMass < 0 )
567 {
568 fprintf( stderr, "--maximum-mass must be specified\n" );
569 exit( 1 );
570 }
571
572 /* check that the bank parameters have been specified */
573 if ( numTmplts < 0 )
574 {
575 fprintf( stderr, "--number-of-templates must be specified\n" );
576 exit( 1 );
577 }
578
579 return 0;
580}
581
582#undef ADD_PROCESS_PARAM
const LALVCSInfo lalAppsVCSIdentInfo
Identable VCS and build information for LALApps.
lal_errhandler_t lal_errhandler
int LAL_ERR_EXIT(LALStatus *stat, const char *func, const char *file, const int line, volatile const char *id)
#define LAL_CALL(function, statusptr)
void LALInspiralParameterCalc(LALStatus *status, InspiralTemplate *params)
void LALCheckMemoryLeaks(void)
#define LALCalloc(m, n)
#define LALFree(p)
int LALgetopt_long_only(int argc, char *const *argv, const char *options, const struct LALoption *long_options, int *opt_index)
int LALoptind
char * LALoptarg
#define no_argument
#define required_argument
int XLALCloseLIGOLwXMLFile(LIGOLwXMLStream *xml)
LIGOLwXMLStream * XLALOpenLIGOLwXMLFile(const char *path)
int XLALWriteLIGOLwXMLProcessTable(LIGOLwXMLStream *, const ProcessTable *)
int XLALWriteLIGOLwXMLProcessParamsTable(LIGOLwXMLStream *, const ProcessParamsTable *)
int XLALWriteLIGOLwXMLSnglInspiralTable(LIGOLwXMLStream *xml, const SnglInspiralTable *sngl_inspiral)
#define LIGOMETA_SEARCH_MAX
#define LIGOMETA_TYPE_MAX
#define LIGOMETA_CHANNEL_MAX
#define LIGOMETA_PROGRAM_MAX
#define LIGOMETA_VALUE_MAX
#define LIGOMETA_PARAM_MAX
#define LIGOMETA_IFO_MAX
void XLALDestroyProcessParamsTable(ProcessParamsTable *)
int XLALPopulateProcessTable(ProcessTable *ptable, const char *program_name, const char *cvs_revision, const char *cvs_source, const char *cvs_date, long process_id)
#define fprintf
LIGOTimeGPS * XLALGPSTimeNow(LIGOTimeGPS *gpstime)
double REAL8
#define XLAL_INIT_DECL(var,...)
char CHAR
int32_t INT4
float REAL4
m1Andm2
LAL_PNORDER_TWO
void LALCreateRandomParams(LALStatus *status, RandomParams **params, INT4 seed)
void LALUniformDeviate(LALStatus *status, REAL4 *deviate, RandomParams *params)
void LALDestroyRandomParams(LALStatus *status, RandomParams **params)
char name[LIGOMETA_SOURCE_MAX]
Definition: inspinj.c:561
static LALStatus status
Definition: inspinj.c:552
int i
Definition: inspinj.c:596
c
float atol
int main(int argc, char *argv[])
Definition: randombank.c:92
REAL4 fLow
Definition: randombank.c:83
#define PROGRAM_NAME
Definition: randombank.c:65
enum @5 randSeedType
#define USAGE
Definition: randombank.c:331
@ user
Definition: randombank.c:85
@ urandom
Definition: randombank.c:85
@ unset
Definition: randombank.c:85
INT4 outCompress
Definition: randombank.c:90
#define ADD_PROCESS_PARAM(pptype, format, ppvalue)
Definition: randombank.c:321
REAL4 minMass
Definition: randombank.c:81
LIGOTimeGPS gpsStartTime
Definition: randombank.c:79
int vrbflg
defined in lal/lib/std/LALError.c
REAL4 maxMass
Definition: randombank.c:82
INT4 randomSeed
Definition: randombank.c:86
INT4 numTmplts
Definition: randombank.c:84
LIGOTimeGPS gpsEndTime
Definition: randombank.c:80
CHAR * userTag
Definition: randombank.c:89
int arg_parse_check(int argc, char *argv[], ProcessParamsTable *procparams)
Definition: randombank.c:347
RandomParams * randParams
Definition: spininj.c:212
CHAR fname[256]
Definition: spininj.c:211
LALPNOrder order
InputMasses massChoice
const char *const vcsDate
const char *const vcsStatus
const char *const vcsId
int * flag
INT4 gpsNanoSeconds
CHAR type[LIGOMETA_TYPE_MAX]
CHAR param[LIGOMETA_PARAM_MAX]
CHAR value[LIGOMETA_VALUE_MAX]
struct tagProcessParamsTable * next
CHAR program[LIGOMETA_PROGRAM_MAX]
LIGOTimeGPS start_time
CHAR ifos[LIGOMETA_IFOS_MAX]
LIGOTimeGPS end_time
CHAR ifo[LIGOMETA_IFO_MAX]
struct tagSnglInspiralTable * next
CHAR channel[LIGOMETA_CHANNEL_MAX]
CHAR search[LIGOMETA_SEARCH_MAX]