3 # Copyright (C) 2010--2015 Kipp Cannon, Chad Hanna
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 2 of the License, or (at your
8 # option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 ### A program to compute the noise probability distributions of likehood ratios for inspiral triggers
22 # =============================================================================
26 # =============================================================================
30 from optparse
import OptionParser
34 from ligo.lw
import ligolw
35 from ligo.lw
import utils as ligolw_utils
36 from ligo.lw.utils
import process as ligolw_process
37 from gstlal
import far
38 from lal.utils
import CacheEntry
41 __author__ =
"Kipp Cannon <kipp.cannon@ligo.org>"
42 __version__ =
"git id %s" %
"" # FIXME
47 # =============================================================================
51 # =============================================================================
55 def parse_command_line():
56 parser = OptionParser(
57 version =
"Name: %%prog\n%s" %
"" # FIXME
59 parser.add_option(
"--likelihood-cache", metavar =
"filename", help =
"Also load the likelihood ratio data files listsed in this LAL cache. See lalapps_path2cache for information on how to produce a LAL cache file.")
60 parser.add_option(
"-v",
"--verbose", action =
"store_true", help =
"Be verbose.")
61 parser.add_option(
"--ranking-stat-samples", metavar =
"N", default = 2**24, type =
"int", help =
"Construct ranking statistic histograms by drawing this many samples from the ranking statistic generator (default = 2^24).")
62 parser.add_option(
"--add-zerolag-to-background", action =
"store_true", help =
"Add zerolag events to background before populating coincident parameter PDF histograms")
63 parser.add_option(
"--output", metavar =
"filename", help =
"Write merged raw likelihood data and likelihood ratio histograms to this LIGO Light-Weight XML file.")
64 options, urls = parser.parse_args()
66 paramdict = options.__dict__.copy()
68 if options.likelihood_cache is not None:
69 urls += [CacheEntry(line).url for line in open(options.likelihood_cache)]
71 raise ValueError(
"must provide some likelihood files")
73 if options.output is None:
74 raise ValueError(
"must set --output")
76 return options, urls, paramdict
80 # =============================================================================
84 # =============================================================================
93 options, urls, paramdict = parse_command_line()
97 # load parameter distribution data
101 rankingstat = far.marginalize_pdf_urls(urls,
"RankingStat", verbose = options.verbose)
105 # invoke .finish() to apply density estimation kernels and correct the
106 # normalization. preserve the original, raw data, so we can write it do
107 # disk later with the ranking stat PDF obtained from it. if we wish to
108 # draw samples from a population that includes the zero-lag, then we also
109 # construct a separate, sampler, PDF object.
113 lr_rankingstat = rankingstat.copy()
114 lr_rankingstat.finish()
115 if options.add_zerolag_to_background:
116 signal_noise_pdfs = rankingstat.copy()
117 signal_noise_pdfs.denominator.lnzerolagdensity = signal_noise_pdfs.zerolag
118 signal_noise_pdfs.finish()
120 signal_noise_pdfs = None
124 # generate likelihood ratio histograms
128 rankingstatpdf = far.RankingStatPDF(lr_rankingstat, signal_noise_pdfs = signal_noise_pdfs, nsamples = options.ranking_stat_samples, verbose = options.verbose)
132 # Write the parameter and ranking statistic distribution data to a file
136 xmldoc = ligolw.Document()
137 xmldoc.appendChild(ligolw.LIGO_LW())
138 process = ligolw_process.register_to_xmldoc(xmldoc, u
"gstlal_inspiral_calc_rank_pdfs", paramdict = paramdict, instruments = rankingstat.instruments)
139 far.gen_likelihood_control_doc(xmldoc, rankingstat, rankingstatpdf)
140 ligolw_process.set_process_end_time(process)
141 ligolw_utils.write_filename(xmldoc, options.output, verbose = options.verbose)