3 # Copyright (C) 2013 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
20 # A program to extract the loudest coincs from an offline gstlal_inspiral DAG database into single files that can be uploaded to gracedb
22 # ### Command line interface
23 # + `--fap-thresh` [probability] (float): Set the false alarm probability: default 0.01.
24 # + `--gps-times` [GPS (comma separated)]: Restrict times to this list. give as GPS1,GPS2,...GPSN. Assumes +- 1s.
26 from glue.ligolw
import ligolw, lsctables, utils, dbtables
29 from optparse
import OptionParser
31 def snglrow(connection):
32 return lsctables.table.get_table(dbtables.get_xml(connection), lsctables.SnglInspiralTable.tableName).row_from_cols
33 def coincrow(connection):
34 return lsctables.table.get_table(dbtables.get_xml(connection), lsctables.CoincInspiralTable.tableName).row_from_cols
35 def coinceventrow(connection):
36 return lsctables.table.get_table(dbtables.get_xml(connection), lsctables.CoincTable.tableName).row_from_cols
37 def coinceventmaprow(connection):
38 return lsctables.table.get_table(dbtables.get_xml(connection), lsctables.CoincMapTable.tableName).row_from_cols
40 parser = OptionParser()
41 parser.add_option(
"--fap-thresh", default=0.01, type=
"float", metavar=
"probability", help=
"Set the false alarm probability: default 0.01")
42 parser.add_option(
"--gps-times", metavar=
"GPS (comma separated)", help=
"Restrict times to this list. give as GPS1,GPS2,...GPSN. Assumes +- 1s")
43 options, filenames = parser.parse_args()
45 if len(filenames) != 1:
46 print >> sys.stderr,
"Only supports excactly 1 database currently"
49 db = sqlite3.connect(filenames[0])
51 if options.gps_times is None:
52 cids = list(db.cursor().execute(
'SELECT coinc_inspiral.coinc_event_id, coinc_inspiral.end_time, coinc_inspiral.ifos FROM coinc_inspiral JOIN coinc_event ON coinc_event.coinc_event_id == coinc_inspiral.coinc_event_id WHERE NOT EXISTS(SELECT * FROM time_slide WHERE time_slide.time_slide_id == coinc_event.time_slide_id AND time_slide.offset != 0) AND coinc_inspiral.false_alarm_rate <= ?', (options.fap_thresh,)).fetchall())
54 gps_list = [int(t) for t in options.gps_times.split(
',')]
56 gps_list.extend([t+1 for t in gps_list])
58 gps_list.extend([t-1 for t in gps_list])
59 print >> sys.stderr, gps_list
60 query =
'SELECT coinc_inspiral.coinc_event_id, coinc_inspiral.end_time, coinc_inspiral.ifos FROM coinc_inspiral JOIN coinc_event ON coinc_event.coinc_event_id == coinc_inspiral.coinc_event_id WHERE NOT EXISTS(SELECT * FROM time_slide WHERE time_slide.time_slide_id == coinc_event.time_slide_id AND time_slide.offset != 0) AND coinc_inspiral.false_alarm_rate <= ? AND coinc_inspiral.end_time IN (%s)' % (
",".join([
"%d" % t for t in sorted(gps_list)]),)
61 print >> sys.stderr, query
62 cids = list(db.cursor().execute(query, (options.fap_thresh,)).fetchall())
64 for (cid, time, ifos) in cids:
65 xmldocmain = ligolw.Document()
66 xmldoc = xmldocmain.appendChild(ligolw.LIGO_LW())
68 # coinc inspiral table
69 coincinspiral = lsctables.New(lsctables.CoincInspiralTable)
70 xmldoc.appendChild(coincinspiral)
71 rowfunc = coincrow(db)
72 for val in db.cursor().execute(
'SELECT * FROM coinc_inspiral WHERE coinc_event_id == ?', (cid,)):
73 coincinspiral.append(rowfunc(val))
76 coincevent = lsctables.New(lsctables.CoincTable)
77 xmldoc.appendChild(coincevent)
78 rowfunc = coinceventrow(db)
79 for val in db.cursor().execute(
'SELECT * FROM coinc_event WHERE coinc_event_id == ?', (cid,)):
80 coincevent.append(rowfunc(val))
82 # coinc event map table
83 coinceventmap = lsctables.New(lsctables.CoincMapTable)
84 xmldoc.appendChild(coinceventmap)
85 rowfunc = coinceventmaprow(db)
87 for val in db.cursor().execute(
'SELECT * FROM coinc_event_map WHERE coinc_event_id == ?', (cid,)):
89 snglids.append(row.event_id)
90 coinceventmap.append(row)
93 sngl = lsctables.New(lsctables.SnglInspiralTable)
94 xmldoc.appendChild(sngl)
96 #FIXME Terrible hack, figure out how to do this correctly
97 query =
'SELECT * FROM sngl_inspiral WHERE event_id IN (%s)' %
",".join([
'"%s"' % str(i) for i in snglids])
98 for val in db.cursor().execute(query):
99 sngl.append(rowfunc(val))
101 utils.write_filename(xmldocmain,
'%s-LLOID-%d-0.xml.gz' % (ifos.replace(
",",
""), time), gz=True,
verbose=True)