BankEfficiency.c

Go to the documentation of this file.
00001 /*
00002 *  Copyright (C) 2007 Thomas Cokelaer
00003 *
00004 *  This program is free software; you can redistribute it and/or modify
00005 *  it under the terms of the GNU General Public License as published by
00006 *  the Free Software Foundation; either version 2 of the License, or
00007 *  (at your option) any later version.
00008 *
00009 *  This program is distributed in the hope that it will be useful,
00010 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 *  GNU General Public License for more details.
00013 *
00014 *  You should have received a copy of the GNU General Public License
00015 *  along with with program; see the file COPYING. If not, write to the
00016 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00017 *  MA  02111-1307  USA
00018 */
00019 
00020 
00021 #include "BankEfficiency.h"
00022 /* --- version information ------------------------------------------------ */
00023 NRCSID( BANKEFFICIENCYC, "$Id: BankEfficiency.c,v 1.142 2008/07/07 17:36:46 devanka Exp $");
00024 RCSID(  "$Id: BankEfficiency.c,v 1.142 2008/07/07 17:36:46 devanka Exp $");
00025 
00026 #define CVS_ID_STRING_C      "$Id: BankEfficiency.c,v 1.142 2008/07/07 17:36:46 devanka Exp $"
00027 #define CVS_REVISION_C       "$Revision: 1.142 $"
00028 #define CVS_NAME_STRING_C    "$Name:  $"
00029 #define CVS_SOURCE_C         "$Source: /usr/local/cvs/lscsoft/lalapps/src/findchirp/BankEfficiency.c,v $"
00030 #define CVS_DATE_C           "$Date: 2008/07/07 17:36:46 $"
00031 #define PROGRAM_NAME         "BankEfficiency"
00032 
00033 /* --- global variables --------------------------------------------------- */
00034 RandomParams *randParams = NULL;
00035 INT4          randnStartPad = 0;  /* injections always at the same time if zero*/
00036 INT4          ascii2xml = 0;
00037   
00038 /* --- Main program ------------------------------------------------------- */ 
00039 int
00040 main (INT4 argc, CHAR **argv )
00041 {  
00042   /* --- Local variables -------------------------------------------------- */  
00043   INT4                   i;
00044   INT4                   j;
00045   INT4                   thisTemplateIndex;
00046   Order                  tempOrder;  /* temporary phase order */
00047 
00048   /* --- the general input structure --- */
00049   UserParametersIn       userParam;
00050 
00051   /* --- signal related --- */
00052   REAL4Vector            signal;
00053   RandomInspiralSignalIn randIn;
00054   
00055   /* --- template bank related --- */
00056   Mybank                 mybank;  
00057   InspiralTemplate       insptmplt;
00058   InspiralCoarseBankIn   coarseBankIn;
00059   INT4                   sizeBank = 0;
00060   MetadataTable          templateBank;
00061   SnglInspiralTable      *tmpltHead = NULL;
00062 
00063   /* --- filtering related --- */
00064   REAL4Vector               correlation;
00065   BankEfficiencyBCV         bankefficiencyBCV;
00066   InspiralWaveOverlapIn     overlapin;      
00067 
00068   /* --- results and data mining --- */
00069   OverlapOutputIn        overlapOutputThisTemplate;
00070   OverlapOutputIn        overlapOutputBestTemplate;
00071 
00072   /* --- fft related --- */
00073   RealFFTPlan           *fwdp = NULL;
00074   RealFFTPlan           *revp = NULL;
00075 
00076   /* --- others ---*/
00077   LALStatus              status = blank_status;
00078   FILE                  *Foutput;
00079 
00080   /* --- fast option related --- */
00081   UINT4                   fast_index;
00082       
00083   /* --- eccentricity related to the bank */
00084   REAL4                  tempEccentricity = 0;
00085 
00086   /* --- ambiguity function and statistics --- */
00087   gsl_histogram         *histogramNoise = gsl_histogram_alloc (200);  
00088   gsl_matrix            *amb1;
00089 
00090   /* --- parameter for the simulation --- */
00091   BankEfficiencySimulation   simulation;  
00092   
00093   /* --- Some initialization --- */ 
00094   gsl_histogram_set_ranges_uniform (histogramNoise, 0.,20.);
00095   lal_errhandler = LAL_ERR_EXIT;
00096   lalDebugLevel = 0;
00097   templateBank.snglInspiralTable = NULL;
00098   
00099   /* --- Initialization of structures related to all the user parameters --- */
00100   BankEfficiencyParametersInitialization(&coarseBankIn, &randIn, &userParam);
00101   
00102   /* --- Read user parameters --- */
00103   BankEfficiencyParseParameters(&argc, argv,&coarseBankIn, &randIn, &userParam);
00104 
00105   /* --- Check input parameters --- */
00106   BankEfficiencyUpdateParams(&coarseBankIn, &randIn, &userParam);
00107   
00108   /* eccentric bank initialisation */
00109   BankEfficiencyEccentricBankInit(&userParam);
00110   
00111   /* --- Init a random structure using the user seed --- */  
00112   randParams = XLALCreateRandomParams(randIn.useed );
00113   
00114   /* --- Ascii2XML option --- */
00115   /* This is a call to the function that converts the ASCII output of
00116    * BankEfficiency into a standard XML output. Useful when several ASCII
00117    * output are available and needs to be converted all together into a single
00118    * XML file.
00119    * */
00120   if (ascii2xml == 1){
00121     BankEfficiencyAscii2Xml();    
00122   }
00123   
00124   /* --- Condor related --- */
00125   /* If one want to use condor script to keep track of the input parameters, 
00126    * we first need to create a prototype containing all parameters, which can 
00127    * be read later using --ascii2xml option. Use with bep.dag DAGMan script.  
00128   */
00129   if (userParam.printPrototype){
00130     BankEfficiencyPrintProtoXml(coarseBankIn, randIn, userParam);   
00131   }
00132   
00133   /* --- Allocate memory -------------------------------------------------- */
00134   /* --- First, estimate the size of the signal --- */
00135   LAL_CALL(BankEfficiencyGetMaximumSize(&status, 
00136       randIn, coarseBankIn, userParam, &(signal.length)), 
00137       &status);  
00138        
00139   /* --- Set size of the other vectors --- */
00140   randIn.psd.length     = signal.length/2 + 1; 
00141   correlation.length    = signal.length;
00142   /* --- Allocate memory for some vectors --- */
00143   signal.data      = (REAL4*)LALCalloc(1, sizeof(REAL4) * signal.length);
00144   correlation.data = (REAL4*)LALCalloc(1, sizeof(REAL4) * correlation.length);
00145   randIn.psd.data  = (REAL8*)LALCalloc(1, sizeof(REAL8) * randIn.psd.length);
00146   /* --- Allocate memory for BCV filtering only --- */ 
00147   if (userParam.template == BCV)
00148   {
00149         INT4 n;
00150         n = signal.length;
00151     bankefficiencyBCV.FilterBCV1.length = n;
00152     bankefficiencyBCV.FilterBCV2.length = n;
00153     bankefficiencyBCV.FilterBCV1.data = (REAL4*)LALCalloc(1, sizeof(REAL4) * n);
00154     bankefficiencyBCV.FilterBCV2.data = (REAL4*)LALCalloc(1, sizeof(REAL4) * n);
00155   }
00156   
00157   /* --- Create the PSD noise ----------------------------------------------- */
00158   LAL_CALL(BankEfficiencyCreatePsd(&status, &coarseBankIn, &randIn, userParam), 
00159        &status);
00160  
00161   /* --- Create the template bank ------------------------------------------- */
00162   LAL_CALL(BankEfficiencyCreateTemplateBank(&status, &coarseBankIn,
00163       &templateBank, &tmpltHead, userParam, &randIn, &sizeBank), &status);
00164 
00165   /* --- populate MyBank strucuture --- */
00166   BankEfficiencyInitMyBank(&mybank, &sizeBank, tmpltHead, userParam);    
00167        
00168   /* --- Allocate memory for the ambiguity function ------------------------- */
00169   /* For each bank, we have t0,t3,and the maximum */
00170   amb1 = gsl_matrix_alloc(4,sizeBank); /*allocated t0,t3,max*/
00171   for (i=0;i<4; i++)
00172   {
00173     for (j=0;j<sizeBank; j++)
00174     {
00175       gsl_matrix_set(amb1, i, j, 0.); /* set it to zero */
00176     }
00177   }      
00178       
00179   /* --- Estimate the fft's plans ----------------------------------------- */
00180   LALCreateForwardRealFFTPlan(&status, &fwdp, signal.length, 0);
00181   LALCreateReverseRealFFTPlan(&status, &revp, signal.length, 0);
00182   
00183   /* --- The overlap structure -------------------------------------------- */
00184   overlapin.nBegin      = 0;
00185   overlapin.nEnd        = 0;
00186   overlapin.psd         = randIn.psd;
00187   overlapin.fwdp        = randIn.fwdp = fwdp;
00188   overlapin.revp        = revp;
00189   overlapin.ifExtOutput = 0;  /* new option from Anand's WaveOverlap */
00190   overlapin.ifCorrelationOutput = 1; /* output of WaveOverlap is sqrt(x^2+y^2)*/
00191 
00192   /* --- Create Matrix for BCV Maximization once for all ------------------ */
00193   if ((userParam.template == BCV) )
00194   {
00195     LAL_CALL( BankEfficiencyCreatePowerVector(&status, 
00196        &(bankefficiencyBCV.powerVector), randIn, signal.length),  &status);
00197 
00198     BankEfficiencyCreateBCVMomentVector(
00199         &(bankefficiencyBCV.moments), &coarseBankIn.shf,randIn.param.tSampling, 
00200         randIn.param.fLower, signal.length);
00201   }
00202   
00203   /* ------------------------------------------------------------------------ */
00204   /* --- Main loop ---------------------------------------------------------- */
00205   /* ------------------------------------------------------------------------ */  
00206   /* --- ntrials is used to keep track of the simulation trials           --- */
00207   simulation.ntrials = 0;
00208   simulation.N = userParam.ntrials;
00209   /* --- loop over the number of simulations (ntrials) --- */  
00210   while (++simulation.ntrials <= userParam.ntrials) 
00211   {     
00212     if (vrbflg){
00213       fprintf(stdout,"Simulation number %d/%d\n", simulation.ntrials, userParam.ntrials);
00214     }
00215       
00216     /* --- initialisation for each simulation --- */
00217     BankEfficiencyInitOverlapOutputIn(&overlapOutputBestTemplate);
00218       
00219     /* --- set the fcutoff (signal,overlap) --- */
00220     randIn.param.fCutoff = userParam.signalfFinal; 
00221       
00222     /* --- generate the signal waveform --- */
00223     LAL_CALL(BankEfficiencyGenerateInputData(&status, 
00224         &signal, &randIn, userParam), &status);
00225     
00226     /* --- populate the main structure of the overlap ---*/
00227     overlapin.signal = signal;
00228        
00229     /*  --- populate the insptmplt with the signal parameter --- */ 
00230     insptmplt = randIn.param; /* set the sampling and other common parameters */     
00231     insptmplt.approximant  = userParam.template;        /* set the template   */
00232     insptmplt.order        = coarseBankIn.order;        /* set the phaseorder */ 
00233     insptmplt.ampOrder     = coarseBankIn.ampOrder;     /* set the amp order  */
00234 
00235     /* --- reset the bank (none of the template have been used) --- */
00236     for (i=0; i<sizeBank; i++){
00237       mybank.used[i] = 0;
00238     }
00239       
00240     /* --- set the general parameter for each simulation --- */
00241     simulation.filter_processed = 0; /* count number of templates being used  */ 
00242     simulation.lastEMatch       = 1; /* ematch of the last template being used*/
00243     simulation.filteringIndex   = 0; /* current filter number                 */
00244     simulation.stop             = 0; /* flag to stop this simulation          */
00245     simulation.bestSNR          = 0; /* value of the best SNR                 */
00246     simulation.bestSNRIndex     = 0; /* index of the template that has the best SNR*/
00247     simulation.SNRMax           = 0; /* For how long have we the best SNR     */
00248     simulation.fastParam1 = userParam.fastParam1;
00249     simulation.fastParam1 *= 1.+log10(userParam.eccentricBank.bins);
00250     simulation.eMatch           = userParam.eMatch; /* reset the ematch       */
00251     simulation.bestEMatch       = -1e16; /* reset ematch to small value*/
00252     simulation.randIn           = randIn;        
00253 
00254     /* --- loop over the bank until counter reaches size bank --- */ 
00255     while ( simulation.filteringIndex<sizeBank && simulation.stop == 0 )    
00256     {
00257       /* --- iterate the counter --- */
00258       simulation.filteringIndex++;
00259       
00260       /* -- reset the output structure --- */
00261       BankEfficiencyInitOverlapOutputIn(&overlapOutputThisTemplate);
00262     
00263       GetClosestValidTemplate(mybank, simulation.randIn, &fast_index);
00264           
00265       LAL_CALL(BankEfficiencyCreateListFromTmplt(&status, 
00266              &insptmplt, mybank, fast_index), &status); 
00267       thisTemplateIndex = fast_index;
00268                 
00269       switch(userParam.template)
00270       {                 
00271         case BCV:
00272             
00273           insptmplt.massChoice = psi0Andpsi3;  
00274           LAL_CALL(LALInspiralParameterCalc(&status, &(insptmplt)),&status);
00275           overlapin.param = insptmplt;
00276 
00277           LAL_CALL(LALInspiralParameterCalc( &status,  &(overlapin.param) ), 
00278               &status);
00279 
00280           /* if faithfulness is required, the template parameters are 
00281            * identical to the signal except for the name of the approximant.
00282            *  */
00283           if (userParam.faithfulness)
00284           {
00285             insptmplt                    = randIn.param;
00286             overlapin.param              = randIn.param;
00287             overlapin.param.approximant  = userParam.template; 
00288             insptmplt.fFinal             = randIn.param.fFinal;
00289             /* --- the faithfulness requires only one filtering, so we stop - */
00290             simulation.stop              = 1;                
00291            }
00292      
00293           LAL_CALL(BankEfficiencyInspiralOverlapBCV(&status, 
00294                     &insptmplt, userParam, &randIn, 
00295                     &overlapin, &overlapOutputThisTemplate,
00296                     &correlation, &bankefficiencyBCV),
00297                     &status);
00298 
00299           /* keep track of what has been done in the overlap function */
00300             
00301           overlapOutputThisTemplate.freq =  overlapin.param.fFinal;
00302           overlapOutputThisTemplate.templateNumber = thisTemplateIndex;
00303           simulation.filter_processed++;
00304           break;
00305               
00306         case AmpCorPPN:
00307         case TaylorT1: 
00308         case Eccentricity:
00309         case TaylorT2:
00310         case TaylorT3:
00311         case TaylorF1:
00312         case TaylorF2:
00313         case EOB:
00314         case EOBNR:
00315         case PadeT1:
00316         case PadeF1:
00317         case SpinTaylor:
00318           if (vrbflg){
00319             fprintf(stderr,"closeest template is tau0= %f tau3= %f  index=%d filterintIndex= %d,",   
00320               mybank.tau0[fast_index], mybank.tau3[fast_index],fast_index,simulation.filteringIndex);
00321           }
00322                   
00323                     
00324           /* --- first we create the template --- */
00325           insptmplt.massChoice = t03;
00326           LAL_CALL(LALInspiralParameterCalc( &status,  &(insptmplt) ), 
00327               &status);
00328           /* --- set up the overlapin strucutre --- */
00329           overlapin.param = insptmplt;
00330           LAL_CALL(LALInspiralParameterCalc( &status,  &(overlapin.param) ), 
00331               &status);
00332           overlapin.param.fCutoff = coarseBankIn.fUpper;
00333           overlapin.param.fLower  = coarseBankIn.fLower;
00334           overlapin.param.fFinal  = randIn.param.tSampling/2. - 1;
00335 
00336           /* --- the fast simulation option. We compute the ematch --- */
00337           simulation.lastEMatch = BankEfficiencyComputeEMatch(&randIn, mybank,
00338               thisTemplateIndex);
00339           if (simulation.lastEMatch > simulation.bestEMatch)
00340             simulation.bestEMatch = simulation.lastEMatch;
00341           
00342           /* --- if check option is on --- */    
00343           if (userParam.faithfulness)
00344           {
00345             simulation.lastEMatch = 1; /* if faithfulness, masses are equal so match is 1*/                
00346             tempOrder = insptmplt.order;
00347             tempEccentricity = insptmplt.eccentricity;
00348             /* --- now, we overwrite insptmplt with the signal params---*/
00349             insptmplt = randIn.param;
00350             overlapin.param = randIn.param;
00351             /* --- but get back some parameters --- */
00352             LAL_CALL(LALInspiralParameterCalc( &status,  &(overlapin.param) ), &status);
00353             overlapin.param.fCutoff = coarseBankIn.fUpper;
00354             overlapin.param.fFinal = randIn.param.tSampling/2. - 1;
00355             overlapin.param.approximant        = userParam.template;
00356             overlapin.param.fLower  = coarseBankIn.fLower;
00357             /* we want to keep the original order except for eccentricity 
00358              * where order must be zero anyway*/
00359             overlapin.param.order              = tempOrder;
00360             overlapin.param.eccentricity       = tempEccentricity;
00361             insptmplt.eccentricity             = tempEccentricity;
00362             /* --- the faithfulness requires only one filtering, so we stop - */
00363             simulation.stop = 1;                
00364           }          
00365           /* if we want to cut integration before the fFinal */
00366           /*              overlapin.param.fCutoff = 1023;    */          
00367       
00368       
00369           if (userParam.fastSimulation == Heuristic1)
00370           {                        
00371             /* --- decrease the ematch parameter if needed --- */            
00372             if (simulation.lastEMatch < simulation.eMatch){
00373               simulation.eMatch = simulation.lastEMatch-1;
00374             }
00375             /* --- if SNRMax has reached the max iteration, we stop --- */
00376             if (simulation.SNRMax >= simulation.fastParam1 
00377                && userParam.fastSimulation){
00378               simulation.stop = 1;
00379             }
00380           }                        
00381             
00382           /* --- filteing!=1 ensure that at least 1 filter will be used    ---*/
00383           /* --- while lastEMatch is greater than the ematch --- */          
00384           if ((simulation.filteringIndex !=1) &&
00385               (userParam.fastSimulation != None) 
00386               && (simulation.lastEMatch < simulation.eMatch ))
00387           {                     
00388             gsl_matrix_set(amb1,2,thisTemplateIndex, 0); 
00389             gsl_matrix_set(amb1,3,thisTemplateIndex, 0); 
00390             gsl_matrix_set(amb1,0,thisTemplateIndex, insptmplt.t0); 
00391             gsl_matrix_set(amb1,1,thisTemplateIndex, insptmplt.t3);
00392             /* --- if we enter here, then we should stop the simulation --- */
00393             simulation.stop = 1;
00394           }       
00395           else
00396           {                          
00397             LAL_CALL(BankEfficiencyWaveOverlap(&status, 
00398                   &correlation,
00399                   &overlapin,
00400                   &overlapOutputThisTemplate,
00401                    randIn.param.nStartPad), &status);
00402                                 
00403                 
00404             overlapOutputThisTemplate.templateNumber = thisTemplateIndex;
00405             /* we compute the averaged ambiguity function a t=ta and 
00406              * the averaged maximizaed ambiguity function over time*/
00407             BankEfficiencyPopulateAmbiguityFunction(
00408                 amb1,correlation,thisTemplateIndex, 
00409                 overlapOutputThisTemplate, randIn.param.nStartPad,
00410                 insptmplt);
00411                 
00412             /* is it correct to do so ? */    
00413             insptmplt.fFinal = overlapin.param.fFinal;
00414             simulation.filter_processed++;
00415             
00416             if (vrbflg){
00417             fprintf(stderr, "snr = %f ematch=%f (threshold is %f) snrMax for %d\n",
00418                 overlapOutputThisTemplate.rhoMax, simulation.lastEMatch,
00419                 simulation.eMatch,simulation.SNRMax);
00420                 
00421             fflush(stderr);
00422             }
00423             
00424             if (overlapOutputThisTemplate.rhoMax > simulation.bestSNR)
00425             {              
00426               simulation.bestSNR  = overlapOutputThisTemplate.rhoMax;
00427               simulation.bestSNRIndex = simulation.filteringIndex; 
00428               simulation.SNRMax = 0;
00429               if (userParam.fastSimulation == Heuristic1)
00430               {
00431                 simulation.randIn.param.t0 = insptmplt.t0;
00432                 simulation.randIn.param.t3 = insptmplt.t3;
00433               }
00434               simulation.bestEMatch      = simulation.lastEMatch;                            
00435             }
00436             else
00437             {
00438               simulation.SNRMax++;
00439             }
00440                                         
00441           }
00442         break;
00443       } /*end of the switch over template*/
00444       
00445       
00446       
00447       /* accumulates histogram of the correlations over all templates and all 
00448        * simulations. */
00449       if (userParam.printSNRHisto){      
00450         BankEfficiencyUpdateSNRHistogram(&correlation, histogramNoise);
00451       }
00452         
00453       /* for the final results, we keep only the loudest SNR over the entire
00454        * template bank, for each simulations.*/ 
00455       BankEfficiencyKeepHighestValues( overlapOutputThisTemplate, 
00456           &overlapOutputBestTemplate, insptmplt);
00457                       
00458     }  /* --- end of  bank process --- */
00459       
00460     /* --- print the results on stdout and xml file if requested --- */
00461     LAL_CALL(BankEfficiencyFinalise(&status,mybank,
00462         overlapOutputBestTemplate,randIn,userParam,simulation, 
00463         coarseBankIn), &status);
00464                   
00465     if (userParam.template == BCV) 
00466     {
00467       if (userParam.printBestOverlap || userParam.printBestTemplate) 
00468       {
00469         userParam.extraFinalPrinting = 1;
00470         LAL_CALL(BankEfficiencyInspiralOverlapBCV(&status, 
00471                     &insptmplt, userParam, &randIn,
00472                      &overlapin, &overlapOutputThisTemplate, &correlation, 
00473                      &bankefficiencyBCV),
00474                      &status);    
00475       }
00476     }
00477 
00478     /* --- print correlation of the last computed correlation --- */
00479     if (userParam.printBestOverlap ){
00480         BankEfficiencySaveVector("BankEfficiency-correlation.dat", correlation,
00481             randIn.param.tSampling);
00482     }
00483         
00484   }  /* --- end while(trial) --- */
00485   
00486 
00487   /* --- save histrogram of all the SNR output correlations --- */
00488   if (userParam.printSNRHisto)
00489   {
00490     Foutput = fopen(BANKEFFICIENCY_SNRHISTOGRAM,"w");
00491     gsl_histogram_fprintf(Foutput, histogramNoise, "%f", "%g");
00492     fclose(Foutput);
00493   }
00494 
00495 
00496   /* --- we save the ambiguity in a file --- */
00497   BankEfficiencyPrintAmbiguity(userParam,sizeBank,amb1);
00498   
00499   /* free memory */
00500   while ( templateBank.snglInspiralTable )
00501   {
00502     tmpltHead = templateBank.snglInspiralTable;
00503     templateBank.snglInspiralTable = templateBank.snglInspiralTable->next;
00504     LALFree( tmpltHead );
00505   }
00506   
00507   /* --- destroy the plans, correlation and signal --- */
00508   
00509   /*XLALDestroyRandomParams(randParams );*/
00510   
00511   if (userParam.template == BCV)
00512   {
00513     LALFree(bankefficiencyBCV.powerVector.fm5_3.data);
00514     LALFree(bankefficiencyBCV.powerVector.fm2_3.data);
00515     LALFree(bankefficiencyBCV.powerVector.fm1_2.data);
00516     LALFree(bankefficiencyBCV.powerVector.fm7_6.data);
00517     LALFree(bankefficiencyBCV.moments.a11.data);
00518     LALFree(bankefficiencyBCV.moments.a21.data);
00519     LALFree(bankefficiencyBCV.moments.a22.data);   
00520   
00521     if (userParam.template == BCV)
00522     {  
00523       LALFree(bankefficiencyBCV.FilterBCV2.data);
00524       LALFree(bankefficiencyBCV.FilterBCV1.data);
00525     }
00526   }
00527   
00528   LALFree(randIn.psd.data);
00529   LALDDestroyVector( &status, &(coarseBankIn.shf.data) );
00530  
00531   LALFree(signal.data);
00532   LALFree(correlation.data);
00533 
00534   LALDestroyRealFFTPlan(&status,&fwdp);
00535   LALDestroyRealFFTPlan(&status,&revp);
00536 
00537   gsl_matrix_free(amb1);  
00538   gsl_histogram_free(histogramNoise);
00539 
00540   free(mybank.mass1);
00541   free(mybank.mass2);
00542   /*todo  free other varaible in mybank*/
00543   
00544   LALCheckMemoryLeaks();    
00545     
00546   return 0;
00547 }
00548 
00549 
00550 
00551               
00552 /* *************************************************************************
00553  * Some output Results
00554  * ********************************************************************** */
00555 void BankEfficiencyKeepHighestValues(
00556   OverlapOutputIn  resultThisTemplate,
00557   OverlapOutputIn *resultBestTemplate,
00558   InspiralTemplate insptmplt)
00559 {
00560   
00561   if (resultThisTemplate.rhoMax > resultBestTemplate->rhoMax)
00562   {        
00563     resultBestTemplate->rhoMax         = resultThisTemplate.rhoMax;
00564     resultBestTemplate->phase          = resultThisTemplate.phase;
00565     resultBestTemplate->alpha          = resultThisTemplate.alpha;
00566     resultBestTemplate->rhoBin         = resultThisTemplate.rhoBin;
00567     resultBestTemplate->freq           = resultThisTemplate.freq;
00568     resultBestTemplate->templateNumber = resultThisTemplate.templateNumber;    
00569     resultBestTemplate->snrAtCoaTime   = resultThisTemplate.snrAtCoaTime;
00570     resultBestTemplate->eccentricity   = insptmplt.eccentricity;
00571   }
00572 }
00573 
00574 
00575 /* ************************************************************************
00576  * Some output Results
00577  * ********************************************************************** */
00578 void BankEfficiencyGetResult(
00579   LALStatus        *status,
00580   InspiralTemplate *list,
00581   InspiralTemplate  injected,
00582   OverlapOutputIn   bestOverlap, 
00583   ResultIn         *result,
00584   UserParametersIn  userParam
00585 )
00586 {
00587   INT4 templateNumber;
00588   InspiralTemplate trigger;
00589 
00590   INITSTATUS (status, "GetResult", BANKEFFICIENCYC);
00591   ATTATCHSTATUSPTR(status);
00592      
00593   templateNumber = bestOverlap.templateNumber;
00594 
00595   trigger = *list;
00596 
00597   if (userParam.template == BCV ){
00598 
00599     /*    LALInspiralParameterCalc( status->statusPtr,  &trigger );
00600     CHECKSTATUSPTR(status);                         
00601     */
00602     result->psi0_inject  = injected.psi0;
00603     result->psi3_inject  = injected.psi3;     
00604     result->psi0_trigger = trigger.psi0;
00605     result->psi3_trigger = trigger.psi3;     
00606     result->tau0_trigger = 0.;
00607     result->tau3_trigger = 0.;
00608     result->tau0_inject  = 0.;
00609     result->tau3_inject  = 0.; 
00610   }
00611   else
00612   {
00613     LALInspiralParameterCalc( status->statusPtr,  &trigger );
00614     CHECKSTATUSPTR(status);                         
00615     result->psi0_inject  = 0.;
00616     result->psi3_inject  = 0.;     
00617     result->psi0_trigger = 0.;
00618     result->psi3_trigger = 0.;     
00619     result->tau0_trigger = trigger.t0;
00620     result->tau3_trigger = trigger.t3;
00621     result->tau0_inject  = injected.t0;
00622     result->tau3_inject  = injected.t3; 
00623     result->polarisationAngle  = injected.polarisationAngle;
00624     result->inclination  = injected.inclination;     
00625   }
00626 
00627   result->mass1_inject = injected.mass1;
00628   result->mass2_inject = injected.mass2;
00629   result->fend_inject  = injected.fFinal;
00630   result->fend_trigger = bestOverlap.freq;
00631   result->rho_final    = bestOverlap.rhoMax;
00632   result->alphaF       = bestOverlap.alpha*pow(bestOverlap.freq,2./3.);
00633   result->bin          = bestOverlap.rhoBin;
00634   result->phase        = bestOverlap.phase;
00635   result->layer        = bestOverlap.layer;
00636   result->phase        = bestOverlap.phase;
00637   result->snrAtCoaTime = bestOverlap.snrAtCoaTime;
00638   result->eccentricity = bestOverlap.eccentricity;
00639   
00640   DETATCHSTATUSPTR(status);
00641   RETURN (status);
00642 }
00643 
00644 
00645 void BankEfficiencyPrintResults(
00646   ResultIn                 result, 
00647   RandomInspiralSignalIn   randIn,
00648   BankEfficiencySimulation simulation)
00649 {
00650   fprintf(stdout, 
00651   "%e %e %e %e %e %e %e %e %e %e %e %e %e %e %e %e %e %e  %e %e %e %e %d %d %d %d %f %d/%d\n",  
00652       result.psi0_trigger, result.psi3_trigger,
00653       randIn.param.psi0, randIn.param.psi3, 
00654       result.tau0_trigger, result.tau3_trigger,
00655       randIn.param.t0, randIn.param.t3, 
00656       result.eccentricity, randIn.param.eccentricity,randIn.param.alpha1,
00657       result.fend_trigger, randIn.param.fFinal,
00658       randIn.param.mass1,randIn.param.mass2,
00659       randIn.param.inclination,randIn.param.polarisationAngle,
00660       randIn.param.startPhase, result.rho_final, 
00661       result.snrAtCoaTime, result.phase, 
00662       result.alphaF,result.bin, randIn.param.nStartPad, result.nfast, 
00663       simulation.bestSNRIndex,simulation.bestEMatch,simulation.ntrials, simulation.N);
00664 
00665   fflush(stdout);
00666 }
00667 
00668 
00669 /* ****************************************************************************
00670  * Function to generate and stored the moments in  REAL4vectors. 
00671  * ************************************************************************* */
00672 void BankEfficiencyCreateBCVMomentVector(
00673   BankEfficiencyMoments *moments,
00674   REAL8FrequencySeries  *psd,
00675   REAL8                 sampling,
00676   REAL8                 fLower,
00677   INT4                  length)
00678 {
00679   REAL8 m7 = 0.;                            /* the moments */
00680   REAL8 m5 = 0.;    
00681   REAL8 m3 = 0.;
00682   REAL8 f;
00683   
00684   UINT4 kMin;
00685   UINT4 kMax;
00686   UINT4 k;
00687   
00688   InspiralMomentsIn     in;
00689 
00690   moments->a11.length =  length / 2 ;
00691   moments->a22.length =  length / 2 ;
00692   moments->a21.length =  length / 2 ;
00693   moments->a11.data   = (REAL4*) LALCalloc(1, sizeof(REAL4) * moments->a11.length);
00694   moments->a22.data   = (REAL4*) LALCalloc(1, sizeof(REAL4) * moments->a22.length);
00695   moments->a21.data   = (REAL4*) LALCalloc(1, sizeof(REAL4) * moments->a21.length);
00696 
00697   /* inspiral structure */
00698   in.shf    = psd;          /* The spectrum             */
00699   in.xmin   = fLower;       /* Lower frequency of integral? is it correct or 
00700                                should we put zero? */
00701   in.xmax   = sampling/2.;  /* We dont' need to cut here */
00702   in.norm   = 1./4.;        /* Some normalization */
00703 
00704   in.norm *= sampling * sampling;
00705 
00706   /* The minimum and maximum frequency */
00707   kMin = (UINT8) floor( (in.xmin - psd->f0) / psd->deltaF );
00708   kMax = (UINT8) floor( (in.xmax - psd->f0) / psd->deltaF );
00709      
00710   /* Skip frequency less than fLower.*/
00711   for (k = 0; k < kMin ; k++)
00712   {
00713     moments->a11.data[k] = 0.;
00714     moments->a21.data[k] = 0.;
00715     moments->a22.data[k] = 0.;
00716   }
00717   for ( k = kMin; k < kMax; ++k )
00718   {
00719     f = psd->f0 + (REAL8) k * psd->deltaF;
00720     if (psd->data->data[k])
00721     {
00722       m7 +=  pow( f, -(7./3.) ) / psd->data->data[k] * psd->deltaF / in.norm;
00723       m5 +=  pow( f, -(5./3) )  / psd->data->data[k] * psd->deltaF / in.norm;
00724       m3 +=  pow( f, -(1.) )    / psd->data->data[k] * psd->deltaF / in.norm;
00725       moments->a11.data[k] = 1./sqrt(m7);
00726       moments->a22.data[k] = 1. / sqrt(m3 - m5*m5/m7);
00727       moments->a21.data[k] = -m5 / m7 * moments->a22.data[k];
00728     }
00729   }
00730   BankEfficiencySaveVector("moment1.dat",moments->a11,sampling);
00731 }
00732 
00733 
00734 /* ****************************************************************************
00735  * Create an orthogonal filter. by taking its complex conjuguate
00736  * ************************************************************************** */
00737 void BankEfficiencyGetOrthogonalFilter(
00738   REAL4Vector *filter)
00739 {
00740   UINT4   i;
00741   UINT4   n      = filter->length;
00742   UINT4   nby2   = filter->length / 2;
00743   REAL4   temp;
00744   
00745   for (i = 1; i < nby2; i++)
00746   {
00747     temp              =  filter->data[i];
00748     filter->data[i]   = -filter->data[n-i];
00749     filter->data[n-i] = temp;
00750   }
00751 }
00752 
00753 /* *****************************************************************************
00754  * Create a vector f^{a/b} 
00755  * ************************************************************************** */
00756 void BankEfficiencyCreateVectorFreqPower(
00757   REAL4Vector      *vector,
00758   InspiralTemplate  params,
00759   INT4              a,
00760   INT4              b)
00761 {
00762   INT4  i;
00763   INT4  n = vector->length; /* Length of the vector to create */
00764       
00765   REAL8 power = (REAL8)a / (REAL8)b;       /* the frequency power */
00766   REAL8 f;                                 /* frequency */
00767   REAL8 df = params.tSampling/(REAL8)n/2.; /* sampling frequency */
00768 
00769   /* First value equal to zero */
00770   vector->data[0] = 0.;
00771   for( i = 1; i < n; i++)
00772   {
00773     f = i * df; 
00774     /* Probably not efficient but this function is 
00775      * just called once at the beginning */
00776     vector->data[i] = pow(f, power);
00777   }
00778 }
00779 
00780 
00781 /* *****************************************************************************
00782  * Create the filters for BCV correlation                   
00783  * ************************************************************************** */
00784 void BankEfficiencyCreateBCVFilters(
00785   BankEfficiencyBCV *bankefficiencyBCV,
00786   UINT4              kMin,           /* position of the integration   */
00787   UINT4              kMax,
00788   REAL4              psi0,
00789