23#include <lal/LALStdlib.h>
24#include <lal/FrequencySeries.h>
26#include <lal/StringInput.h>
27#include <lal/TimeSeries.h>
28#include <lal/LALInference.h>
29#include <lal/LALDatatypes.h>
31#include <lal/LALInferenceCalibrationErrors.h>
32#include <gsl/gsl_linalg.h>
33#include <gsl/gsl_blas.h>
34#include <gsl/gsl_rng.h>
35#include <gsl/gsl_randist.h>
36#include <lal/LALInferenceLikelihood.h>
64 if (!strcmp(ifoname,
"H1")){
71 else if(!strcmp(ifoname,
"L1")){
78 else if(!strcmp(ifoname,
"V1")){
86 fprintf(stderr,
"Unknown IFO in fill_IFO_vars_from_IFOname! Valid codes are H1, L1, V1. Aborting\n");
94 if (!strcmp(ifoname,
"H1")){
107 else if(!strcmp(ifoname,
"L1")){
120 else if(!strcmp(ifoname,
"V1")){
125 stddev[4]=360.0*7
e-06;
126 stddev[5]=360.0*7
e-06;
134 fprintf(stderr,
"Unknown IFO in fill_IFO_Pha_vars_from_IFOname! Valid codes are H1, L1, V1. Aborting\n");
153 if(!
this) {
fprintf(stderr,
"No command line arguments given!\n"); exit(1);}
155 for(
this=commandLine;
this;
this=this->next)
157 if(!strcmp(this->param,
"--ifo"))
187 const gsl_rng_type *
type;
190 gsl_rng_default_seed = calib_seed_ampli;
191 type = gsl_rng_default;
192 p = gsl_rng_alloc (
type);
197 REAL8 amp_stdev[3]={0.0};
201 REAL8 amp_freq_bin[2+2]={0.0};
216 for(
j=0;
j<nbins;
j++){
217 if (logF[
i]>=log10(amp_freq_bin[
j]) && logF[
i]<=log10(amp_freq_bin[
j+1])) {
218 ampErr[
i]=gsl_ran_gaussian(
p, amp_stdev[
j]);
230 const gsl_rng_type *
type;
233 gsl_rng_default_seed = calib_seed_pha;
234 type = gsl_rng_default;
235 p = gsl_rng_alloc (
type);
239 REAL8 pha_stdev[6]={0.0};
242 REAL8 pha_freq_bin[5+2]={0.0};
258 for(
j=0;
j<nbins;
j++){
259 if (logF[
i]>=log10(pha_freq_bin[
j]) && logF[
i]<=log10(pha_freq_bin[
j+1])) {
260 phaErr[
i]=gsl_ran_gaussian(
p, pha_stdev[
j]);
276 REAL8 ampli,phase=0.0;
283 ampli=sqrt(creal(datum)*creal(datum)+cimag(datum)*cimag(datum));
284 phase=atan2(cimag(datum),creal(datum));
290 doff->
data->
data[ui]=
crect(ampli*cos(phase),ampli*sin(phase));
315 doff->
data->
data[ui]=
crect(creal(datum)*ampli,cimag(datum)*ampli);
339 Spectrum->
data->
data[ui]*=(ampli*ampli);
351------------------------------------------------------------------------------------------------------------------\n\
352--- Calibration Errors Handling Arguments ------------------------------------------------------------------------\n\
353------------------------------------------------------------------------------------------------------------------\n\
354(--AddCalibrationErrors) Adds calibration errors into the f domain datastream (that includes both noise and signal)\n\
355(--RandomCE) Add a random realization of phase and amplitude CE, using the S6/VSR2-3 error budget as an indication of the 1-sigma errors\n\
356(--ConstantCE) Assumes calibration errors are constant over the bandwidth (requires --IFO-constant_calamp and --IFO-constant_calpha for each ifo)\n\
357(--IFO-constant_calamp Constant amplitude CE for instrument IFO. 0.0 means no error, 0.1 means 10 percent\n\
358(--IFO-constant_calpha Constant phase CE for instrument IFO. 0.0 means no error, 5 means a 5 degree shift \n\
359(--RandomLinearCE ) Assumes CE are given by a contant plateau plus a random jittering of a few percent.\n\t\t After a given frequency f CE increase linearly with a given slope (requires --IFO-calamp_plateau, --IFO-calamp_knee, --IFO-calamp_slope AND similar with calamp<->calpha. Phase Errors in degree, Amp errors are relative (0.05=5%) \n\
360(--IFO-calamp_plateau, --IFO-calamp_knee, --IFO-calamp_slope) Add on the i-th IFO's stream amplitude errors on the form (IFOi_c + jitter) for f<IFOi_f and (IFOi_c-f)*IFOi_slope for f>IFOi_f\n\
361(--IFO-calpha_plateau, --IFO-calpha_knee, --IFO-calpha_slope) Add on the i-th IFO's stream phase errors on the form (IFOi_c + jitter) for f<IFOi_f and (IFOi_c-f)*IFOi_slope for f>IFOi_f\n\
362 * Constant Calibration Model \n\
363 (--MarginalizeConstantCalAmp ) If given, will add a constant value of Amplitude CE per each IFO on the top of the CBC parameters.\n\
364 (--MarginalizeConstantCalPha ) If given, will add a constant value of Phase CE per each IFO on the top of the CBC parameters.\n\
365 (--constcal_ampsigma ) If given, will use gaussian prior on the constant amplitude error with this sigma (e.g. 0.05=5%) .\n\
366 (--constcal_phasigma ) If given, will use gaussian prior on the constant phase error with this sigma (e.g. 5=5degs).\n\
367 * Spline Calibration Model \n\
368 (--enable-spline-calibration) Enable cubic-spline calibration error model.\n\
369 (--spcal-nodes N) Set the number of spline nodes per detector (default 5)\n\
370 (--IFO-spcal-amp-uncertainty X) Set the prior on relative amplitude uncertainty for the instrument IFO (mandatory with --enable-spline-calibration)\n\
371 (--IFO-spcal-phase-uncertainty X) Set the prior on phase uncertanity in degrees for the instrument IFO (mandatory with --enable-spline-calibration)\n\
372 (--IFO-spcal-envelope F) Read amplitude and phase calibration uncertainty envelope from file F\n\n\n";
386 fprintf(stdout,
"No --AddCalibrationErrors option given. Not applying calibration errors in injection...\n");
392 fprintf(stdout,
"--dataseed is required when running with --AddCalibrationErrors\n");
398 int calib_seed_ampli=0.0;
399 int calib_seed_phase=0.0;
400 REAL4 tmpampli,tmpphase;
403 calib_seed_ampli=floor(1E6*tmpampli);
404 calib_seed_phase=floor(1E6*tmpphase);
405 fprintf(stdout,
"Using calibseedAmp %d and calibseedPha %d\n",calib_seed_ampli,calib_seed_phase);
412 while (tmpdata!=NULL) {
414 tmpdata=tmpdata->
next;
420 while (tmpdata!=NULL) {
421 memset(ampCoeffs[this_ifo],0.0,2*
Npoints*
sizeof(
REAL8));
422 memset(phaseCoeffs[this_ifo],0.0,2*
Npoints*
sizeof(
REAL8));
424 tmpdata=tmpdata->
next;
428 fprintf(stdout,
"Applying random phase and amplitude errors. \n");
432 while (tmpdata!=NULL){
437 calib_seed_ampli+=floor(1E6*tmpampli);
438 calib_seed_phase+=floor(1E6*tmpphase);
440 tmpdata=tmpdata->
next;
447 char plateau_option[] =
"constant_calamp";
448 char **pplateau=NULL;
449 char pplateau_option[] =
"constant_calpha";
450 char **IFOnames=NULL;
454 fprintf(stderr,
"Must provide a --IFO-constant_calamp option for each IFO if --ConstantCE is given\n");
457 fprintf(stdout,
"Applying constant amplitude calibration errors. \n");
460 fprintf(stderr,
"Must provide a --IFO-constant_calpha option for each IFO if --ConstantCE is given\n");
463 fprintf(stdout,
"Applying constant phase calibration errors. \n");
464 for(
i=0;
i<num_ifos;
i++){
465 (ampCoeffs[
i])[0]=atof(plateau[
i]);
466 (phaseCoeffs[
i])[0]=atof(pplateau[
i])*
LAL_PI/180.0;
468 for(
i=0;
i<num_ifos;
i++) {
469 if(plateau)
if (plateau[
i])
XLALFree(plateau[
i]);
470 if(pplateau)
if (pplateau[
i])
XLALFree(pplateau[
i]);
475 char plateau_option[] =
"calamp_plateau";
477 char knee_option[] =
"calamp_knee";
479 char slope_option[] =
"calamp_slope";
480 char **IFOnames=NULL;
484 fprintf(stderr,
"Must provide a --IFO-calamp_plateau option for each IFO if --RandomLinearCE is given\n");
489 fprintf(stderr,
"Must provide a --IFO-calamp_knee option for each IFO if --RandomLinearCE is given\n");
494 fprintf(stderr,
"Must provide a --IFO-calamp_slope option for each IFO if --RandomLinearCE is given\n");
498 fprintf(stdout,
"Applying quasi constant amplitude calibration errors. \n");
501 while (tmpdata!=NULL){
509 calib_seed_ampli+=floor(1E6*tmpampli);
511 (ampCoeffs[
i])[
Npoints]=atof(plateau[
i]);
513 (ampCoeffs[
i])[
Npoints+2]=atof(slope[
i]);
516 tmpdata=tmpdata->
next;
518 for(
i=0;
i<num_ifos;
i++) {
519 if(plateau)
if (plateau[
i])
XLALFree(plateau[
i]);
524 char **pplateau=NULL;
525 char pplateau_option[] =
"calpha_plateau";
527 char pknee_option[] =
"calpha_knee";
529 char pslope_option[] =
"calpha_slope";
533 fprintf(stderr,
"Must provide a --IFO-calpha_plateau option for each IFO if --RandomLinearCE is given\n");
538 fprintf(stderr,
"Must provide a --IFO-calpha_knee option for each IFO if --RandomLinearCE is given\n");
543 fprintf(stderr,
"Must provide a --IFO-calpha_slope option for each IFO if --RandomLinearCE is given\n");
547 fprintf(stdout,
"Applying quasi constant phase calibration errors. \n");
551 while (tmpdata!=NULL){
560 calib_seed_phase+=floor(1E6*tmpphase);
565 (phaseCoeffs[
i])[
Npoints+1]=atof(pknee[
i]);
567 (phaseCoeffs[
i])[
Npoints+2]=atof(pslope[
i]);
570 tmpdata=tmpdata->
next;
572 for(
i=0;
i<num_ifos;
i++) {
573 if(pplateau)
if (pplateau[
i])
XLALFree(pplateau[
i]);
575 if(pslope)
if (pslope[
i])
XLALFree(pslope[
i]);
579 fprintf(stderr,
"Must provide a calibration error flag together with --AddCalibrationErrors\n");
585 while (tmpdata!=NULL){
586 PrintCEtoFile(ampCoeffs[this_ifo],phaseCoeffs[this_ifo],tmpdata, commandLine);
591 tmpdata=tmpdata->
next;
603 if (
df*f_low_idx<freq_min || df*f_high_idx >
freq_max) {
604 fprintf(stderr,
"The min and max frequency in LALInspiralCalibrationErrors.c are inside the range [flow,fmin] of the integral overlap. Exiting...\n");
613 char caliboutname[2048];
614 sprintf(caliboutname,
"%s_CE_%s.dat",
outfile, IFOdata->
name);
615 calibout=fopen(caliboutname,
"w");
617 for(ui=f_low_idx;ui<f_high_idx;ui++){
675 gsl_matrix *
m = gsl_matrix_calloc (R+1, 2*
M+1);
676 gsl_matrix *U = gsl_matrix_calloc (R+1, R+1);
677 gsl_vector *
a = gsl_vector_calloc (R+1);
678 gsl_matrix *
c = gsl_matrix_calloc (R+1, 2*
M+1);
679 gsl_vector *ym = gsl_vector_calloc (2*
M+1);
680 gsl_matrix *tmr = gsl_matrix_calloc (R+1, 2*
M+1);
683 gsl_matrix *mT = gsl_matrix_calloc (2*
M+1, R+1);
684 gsl_matrix *mmT = gsl_matrix_calloc (R+1, R+1);
685 gsl_matrix *InvmmT = gsl_matrix_calloc (R+1, R+1);
686 gsl_matrix *InvmmTm = gsl_matrix_calloc (R+1, 2*
M+1);
687 gsl_matrix *InvU = gsl_matrix_calloc (R+1, R+1);
700 for(
j=0;
j<(2*
M+1);
j++)
702 gsl_matrix_set(
m,
i,
j, pow((
j-
M),
i));
719 gsl_matrix_set(U,
i,
i, pow(dlogf,
i));
733 for(
i=0;
i<(R+1);
i++)
735 for(
j=0;
j<(2*
M+1);
j++)
737 gsl_matrix_set(mT,
j,
i, gsl_matrix_get(
m,
i,
j));
752 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
776 gsl_matrix_set(InvU,
i,
i, 1.0/gsl_matrix_get(U,
i,
i));
781 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
796 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
812 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
827 gsl_vector_set(ym,
j,
y[
j]);
831 gsl_blas_dgemv( CblasNoTrans,
837 D[
k] = factorial*gsl_vector_get(
a,
k);
840 gsl_vector_set_zero (ym);
841 gsl_vector_set_zero (
a);
870 gsl_matrix_free(tmr);
872 gsl_matrix_free(mmT);
873 gsl_matrix_free(InvmmT);
874 gsl_matrix_free(InvmmTm);
875 gsl_matrix_free(InvU);
891 gsl_matrix *InvS = gsl_matrix_calloc (
N,
N);
892 gsl_matrix *
V = gsl_matrix_calloc (
N,
N);
893 gsl_matrix *U = gsl_matrix_calloc (
N,
N);
894 gsl_matrix *C = gsl_matrix_calloc (
N,
N);
895 gsl_vector *
s = gsl_vector_alloc (
N);
896 gsl_matrix *II= gsl_matrix_calloc (
N,
N);
916 gsl_matrix_memcpy(U, A);
919 gsl_linalg_SV_decomp_jacobi(U,
V,
s);
922 for (
i = 0;
i<
N;
i++)
924 gsl_vector_set(
s,
i, 1./gsl_vector_get(
s,
i) );
925 gsl_matrix_set( InvS,
i,
i, gsl_vector_get(
s,
i) );
938 gsl_matrix_transpose(U);
941 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
945 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
965 gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
987 gsl_matrix_free(InvS);
1017 if (
Npoints%2==0)
fprintf(stderr,
"The number of points used for the fit must be odd, in ConvertCoefficientsToFunction. Exiting\n");
1020 REAL8 logF = log10(f)-cen;
void LALInferenceApplyCalibrationErrors(LALInferenceIFOData *IFOdata, ProcessParamsTable *commandLine)
static void FitErrorRealisation(INT4 R, INT4 N, REAL8 *y, REAL8 dlogf, REAL8 *D)
static void ApplyPhaseCalibrationErrors(COMPLEX16FrequencySeries *doff, REAL8 *Pcoeffs)
static INT4 getNamedDataOptionsByDetectors(ProcessParamsTable *commandLine, char ***ifos, char ***out, const char *name, UINT4 *N)
Parse the calibration command line looking for options of the kind —IFO-option value Unlike the corre...
static void fill_IFO_Pha_vars_from_IFOname(REAL8 *stddev, REAL8 *fbin, char *ifoname)
static void fill_IFO_Amp_vars_from_IFOname(REAL8 *stddev, REAL8 *fbin, char *ifoname)
static void ApplyBothPhaseAmplitudeErrors(COMPLEX16FrequencySeries *doff, REAL8 *Acoeffs, REAL8 *Pcoeffs)
static void CreateRandomPhaseCalibrationErrors(REAL8 *phacoeffs, int calib_seed_pha, char *ifoname)
static void ApplySquaredAmplitudeErrors(REAL8FrequencySeries *Spectrum, REAL8 *Acoeffs)
static void InvertMatrixSVD(gsl_matrix *A, gsl_matrix *InvA, int N)
static void ApplyAmplitudeCalibrationErrors(COMPLEX16FrequencySeries *doff, REAL8 *Acoeffs)
static void PrintCEtoFile(REAL8 *Acoeffs, REAL8 *Pcoeffs, LALInferenceIFOData *IFOdata, ProcessParamsTable *commandLine)
static void CreateRandomAmplitudeCalibrationErrors(REAL8 *ampcoeffs, int calib_seed_ampli, char *ifoname)
const REAL8 random_linearCE_bit
static REAL8 ConvertCoefficientsToFunction(REAL8 *coeff, REAL8 f)
static REAL8 ConvertRandTransitionSlopeToFunction(REAL8 *coeff, REAL8 f)
ProcessParamsTable * LALInferenceGetProcParamVal(ProcessParamsTable *procparams, const char *name)
Returns the element of the process params table with "name".
void * XLALCalloc(size_t m, size_t n)
void * XLALRealloc(void *p, size_t n)
char char * XLALStringDuplicate(const char *s)
RandomParams * XLALCreateRandomParams(INT4 seed)
void XLALDestroyRandomParams(RandomParams *params)
void LALUniformDeviate(LALStatus *status, REAL4 *deviate, RandomParams *params)
Structure to contain IFO data.
REAL8FrequencySeries * oneSidedNoisePowerSpectrum
COMPLEX16FrequencySeries * freqData
What is this?
COMPLEX16FrequencySeries * whiteFreqData
Buffer for frequency domain data.
struct tagLALInferenceIFOData * next
ROQ data.
REAL8 fLow
FFT plan needed for time/time-and-phase marginalisation.
CHAR value[LIGOMETA_VALUE_MAX]