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
19 ### A program to extract the loudest coincs from an offline gstlal_inspiral DAG database into single files that can be uploaded to gracedb
21 from ligo.lw
import ligolw, lsctables, dbtables
22 from ligo.lw
import utils as ligolw_utils
25 from optparse
import OptionParser
27 def snglrow(connection):
28 return lsctables.table.get_table(dbtables.get_xml(connection), lsctables.SnglInspiralTable.tableName).row_from_cols
29 def procrow(connection):
30 return lsctables.table.get_table(dbtables.get_xml(connection), lsctables.ProcessTable.tableName).row_from_cols
31 def procparamrow(connection):
32 return lsctables.table.get_table(dbtables.get_xml(connection), lsctables.ProcessParamsTable.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 coincdefrow(connection):
38 return lsctables.table.get_table(dbtables.get_xml(connection), lsctables.CoincDefTable.tableName).row_from_cols
39 def coinceventmaprow(connection):
40 return lsctables.table.get_table(dbtables.get_xml(connection), lsctables.CoincMapTable.tableName).row_from_cols
41 def timesliderow(connection):
42 return lsctables.table.get_table(dbtables.get_xml(connection), lsctables.TimeSlideTable.tableName).row_from_cols
44 parser = OptionParser()
45 parser.add_option(
"--far-thresh", default=0.01, type=
"float", metavar=
"probability", help=
"Set the false alarm probability: default 0.01")
46 parser.add_option(
"--gps-times", metavar=
"GPS (comma separated)", help=
"Restrict times to this list. give as GPS1,GPS2,...GPSN. Assumes +- 1s")
47 options, filenames = parser.parse_args()
49 if len(filenames) != 1:
50 print >> sys.stderr,
"Only supports excactly 1 database currently"
53 db = sqlite3.connect(filenames[0])
55 if options.gps_times is None:
56 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.combined_far <= ?', (options.far_thresh,)).fetchall())
58 gps_list = [int(t) for t in options.gps_times.split(
',')]
60 gps_list.extend([t+1 for t in gps_list])
62 gps_list.extend([t-1 for t in gps_list])
63 print >> sys.stderr, gps_list
64 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.combined_far <= ? AND coinc_inspiral.end_time IN (%s)' % (
",".join([
"%d" % t for t in sorted(gps_list)]),)
65 print >> sys.stderr, query
66 cids = list(db.cursor().execute(query, (options.far_thresh,)).fetchall())
68 for (cid, time, ifos) in cids:
69 xmldocmain = ligolw.Document()
70 xmldoc = xmldocmain.appendChild(ligolw.LIGO_LW())
73 process = lsctables.New(lsctables.ProcessTable)
74 xmldoc.appendChild(process)
76 # FIXME Can probably take less process information
77 # FIXME Add process information about this program
78 for val in db.cursor().execute(
'SELECT * FROM process'):
79 process.append(rowfunc(val))
81 # process params table
82 process = lsctables.New(lsctables.ProcessParamsTable)
83 xmldoc.appendChild(process)
84 rowfunc = procparamrow(db)
85 # FIXME Can probably take less param information
86 # FIXME Add process param information about this program
87 for val in db.cursor().execute(
'SELECT * FROM process_params'):
88 process.append(rowfunc(val))
90 # coinc inspiral table
91 coincinspiral = lsctables.New(lsctables.CoincInspiralTable)
92 xmldoc.appendChild(coincinspiral)
93 rowfunc = coincrow(db)
94 for val in db.cursor().execute(
'SELECT * FROM coinc_inspiral WHERE coinc_event_id == ?', (cid,)):
95 coincinspiral.append(rowfunc(val))
98 coincevent = lsctables.New(lsctables.CoincTable)
99 xmldoc.appendChild(coincevent)
100 rowfunc = coinceventrow(db)
101 for val in db.cursor().execute(
'SELECT * FROM coinc_event WHERE coinc_event_id == ?', (cid,)):
102 coincevent.append(rowfunc(val))
105 coincdef = lsctables.New(lsctables.CoincDefTable)
106 xmldoc.appendChild(coincdef)
107 rowfunc = coincdefrow(db)
108 for val in db.cursor().execute(
'SELECT * FROM coinc_definer'):
109 coincdef.append(rowfunc(val))
112 timeslide = lsctables.New(lsctables.TimeSlideTable)
113 xmldoc.appendChild(timeslide)
114 rowfunc = timesliderow(db)
115 for val in db.cursor().execute(
'SELECT * FROM time_slide'):
116 timeslide.append(rowfunc(val))
118 # coinc event map table
119 coinceventmap = lsctables.New(lsctables.CoincMapTable)
120 xmldoc.appendChild(coinceventmap)
121 rowfunc = coinceventmaprow(db)
123 for val in db.cursor().execute(
'SELECT * FROM coinc_event_map WHERE coinc_event_id == ?', (cid,)):
125 snglids.append(row.event_id)
126 coinceventmap.append(row)
128 # sngl inspiral table
129 sngl = lsctables.New(lsctables.SnglInspiralTable)
130 xmldoc.appendChild(sngl)
131 rowfunc = snglrow(db)
132 #FIXME Terrible hack, figure out how to do this correctly
133 query =
'SELECT * FROM sngl_inspiral WHERE event_id IN (%s)' %
",".join([
'"%s"' % str(i) for i in snglids])
134 for val in db.cursor().execute(query):
135 thisrow = rowfunc(val)
136 fullrow = lsctables.New(lsctables.SnglInspiralTable)
137 for col in fullrow.validcolumns:
138 setattr(fullrow, col, None)
139 for attr in (
"process_id",
"ifo",
"end_time",
"end_time_ns",
"eff_distance",
"coa_phase",
"mass1",
"mass2",
"snr",
"chisq",
"chisq_dof",
"bank_chisq",
"bank_chisq_dof",
"sigmasq",
"spin1x",
"spin1y",
"spin1z",
"spin2x",
"spin2y",
"spin2z",
"event_id"):
140 setattr(fullrow, attr, getattr(thisrow, attr))
143 ligolw_utils.write_filename(xmldocmain,
'%s-GSTLAL_AllSky-%d-0.xml.gz' % (ifos.replace(
",",
""), time), verbose=True)