gstlal-inspiral  0.4.2
 All Classes Namespaces Files Functions Variables Pages
gstlal_inspiral_coinc_extractor
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2013 Chad Hanna
4 #
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.
9 #
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.
14 #
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
18 
19 ## @file
20 # A program to extract the loudest coincs from an offline gstlal_inspiral DAG database into single files that can be uploaded to gracedb
21 #
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.
25 
26 from glue.ligolw import ligolw, lsctables, utils, dbtables
27 import sqlite3
28 import sys
29 from optparse import OptionParser
30 
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
39 
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()
44 
45 if len(filenames) != 1:
46  print >> sys.stderr, "Only supports excactly 1 database currently"
47  sys.exit(1)
48 
49 db = sqlite3.connect(filenames[0])
50 
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())
53 else:
54  gps_list = [int(t) for t in options.gps_times.split(',')]
55  # +1
56  gps_list.extend([t+1 for t in gps_list])
57  # -1
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())
63 
64 for (cid, time, ifos) in cids:
65  xmldocmain = ligolw.Document()
66  xmldoc = xmldocmain.appendChild(ligolw.LIGO_LW())
67 
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))
74 
75  # coinc event table
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))
81 
82  # coinc event map table
83  coinceventmap = lsctables.New(lsctables.CoincMapTable)
84  xmldoc.appendChild(coinceventmap)
85  rowfunc = coinceventmaprow(db)
86  snglids = []
87  for val in db.cursor().execute('SELECT * FROM coinc_event_map WHERE coinc_event_id == ?', (cid,)):
88  row = rowfunc(val)
89  snglids.append(row.event_id)
90  coinceventmap.append(row)
91 
92  # sngl inspiral table
93  sngl = lsctables.New(lsctables.SnglInspiralTable)
94  xmldoc.appendChild(sngl)
95  rowfunc = snglrow(db)
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))
100 
101  utils.write_filename(xmldocmain, '%s-LLOID-%d-0.xml.gz' % (ifos.replace(",",""), time), gz=True, verbose=True)