gstlal-inspiral  0.4.2
 All Classes Namespaces Files Functions Variables Pages
inj_simplify_and_cluster.sql
1 -- Copyright (C) 2011--2012,2014 Kipp Cannon, Chad Hanna, Melissa Frei
2 --
3 -- This program is free software; you can redistribute it and/or modify it
4 -- under the terms of the GNU General Public License as published by the
5 -- Free Software Foundation; either version 2 of the License, or (at your
6 -- option) any later version.
7 --
8 -- This program is distributed in the hope that it will be useful, but
9 -- WITHOUT ANY WARRANTY; without even the implied warranty of
10 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11 -- Public License for more details.
12 --
13 -- You should have received a copy of the GNU General Public License along
14 -- with this program; if not, write to the Free Software Foundation, Inc.,
15 -- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 
17 
18 DROP table if EXISTS sim_inspiral;
19 
20 --
21 -- coinc_definer clean up
22 --
23 
24 CREATE TEMPORARY TABLE _idmap_ AS
25  SELECT
26  old.coinc_def_id AS old,
27  MIN(new.coinc_def_id) AS new
28  FROM
29  coinc_definer AS old
30  JOIN coinc_definer AS new ON (
31  new.search == old.search
32  AND new.search_coinc_type == old.search_coinc_type
33  )
34  GROUP BY
35  old.coinc_def_id;
36 CREATE INDEX tmpindex ON _idmap_ (old);
37 
38 UPDATE coinc_event SET coinc_def_id = (SELECT new FROM _idmap_ WHERE old == coinc_def_id);
39 DELETE FROM coinc_definer WHERE coinc_def_id IN (SELECT old FROM _idmap_ WHERE old != new);
40 
41 DROP INDEX tmpindex;
42 DROP TABLE _idmap_;
43 
44 --
45 -- segment_definer clean up. NOTE; this assumes no meaningful information
46 -- is stored in the version and comment columns, and will scramble it if
47 -- there is. that is, two segment_definer rows are considered to be
48 -- equivalent even if their version and comment values differ, and the one
49 -- with the higher ID will be discarded.
50 --
51 
52 CREATE TEMPORARY TABLE _idmap_ AS
53  SELECT
54  old.segment_def_id AS old,
55  MIN(new.segment_def_id) AS new
56  FROM
57  segment_definer AS old
58  JOIN segment_definer AS new ON (
59  new.ifos == old.ifos
60  AND new.name == old.name
61  )
62  GROUP BY
63  old.segment_def_id;
64 CREATE INDEX tmpindex ON _idmap_ (old);
65 
66 UPDATE segment_summary SET segment_def_id = (SELECT new FROM _idmap_ WHERE old == segment_def_id);
67 UPDATE segment SET segment_def_id = (SELECT new FROM _idmap_ WHERE old == segment_def_id);
68 DELETE FROM segment_definer WHERE segment_def_id IN (SELECT old FROM _idmap_ WHERE old != new);
69 
70 DROP INDEX tmpindex;
71 DROP TABLE _idmap_;
72 
73 --
74 -- time_slide clean up
75 --
76 
77 CREATE TEMPORARY TABLE _idmap_ AS
78  SELECT
79  time_slide_id AS old,
80  (SELECT group_concat(instrument || "=" || offset) FROM time_slide AS time_slide_a WHERE time_slide_a.time_slide_id == time_slide.time_slide_id ORDER BY instrument) AS repr,
81  NULL AS new
82  FROM
83  time_slide
84  GROUP BY
85  time_slide_id;
86 CREATE INDEX tmpindex ON _idmap_ (repr, old);
87 
88 UPDATE _idmap_ SET new = (SELECT MIN(old) FROM _idmap_ AS a WHERE a.repr == _idmap_.repr);
89 DROP INDEX tmpindex;
90 CREATE INDEX tmpindex ON _idmap_ (old);
91 
92 UPDATE coinc_event SET time_slide_id = (SELECT _idmap_.new FROM _idmap_ WHERE _idmap_.old == time_slide_id);
93 DELETE FROM time_slide WHERE time_slide_id IN (SELECT old FROM _idmap_ WHERE old != new);
94 
95 DROP INDEX tmpindex;
96 DROP TABLE _idmap_;
97 
98 
99 --
100 -- begin clustering
101 --
102 
103 
104 SELECT
105  "Number of coincs before clustering: " || count(*)
106 FROM
107  coinc_event;
108 
109 --
110 -- create a look-up table of info required for clustering
111 --
112 
113 CREATE TEMPORARY TABLE _cluster_info_ AS
114  SELECT
115  coinc_event.coinc_event_id AS coinc_event_id,
116  coinc_event.time_slide_id AS category,
117  (coinc_inspiral.end_time - (SELECT MIN(end_time) FROM coinc_inspiral)) + 1e-9 * coinc_inspiral.end_time_ns AS end_time,
118  coinc_event.likelihood AS ranking_stat
119  FROM
120  coinc_event
121  JOIN coinc_inspiral ON (
122  coinc_inspiral.coinc_event_id == coinc_event.coinc_event_id
123  );
124 CREATE INDEX tmpindex1 ON _cluster_info_ (coinc_event_id);
125 CREATE INDEX tmpindex2 ON _cluster_info_ (category, end_time, ranking_stat);
126 
127 --
128 -- delete coincs that are within 4 s of coincs with higher SNR in the same
129 -- category
130 --
131 
132 DELETE FROM
133  coinc_event
134 WHERE
135  EXISTS (
136  SELECT
137  *
138  FROM
139  _cluster_info_ AS _cluster_info_a_
140  JOIN _cluster_info_ AS _cluster_info_b_ ON (
141  _cluster_info_b_.category == _cluster_info_a_.category
142  AND (_cluster_info_b_.end_time BETWEEN _cluster_info_a_.end_time - 4.0 AND _cluster_info_a_.end_time + 4.0)
143  AND _cluster_info_b_.ranking_stat > _cluster_info_a_.ranking_stat
144  )
145  WHERE
146  _cluster_info_a_.coinc_event_id == coinc_event.coinc_event_id
147  );
148 DROP INDEX tmpindex1;
149 DROP INDEX tmpindex2;
150 DROP TABLE _cluster_info_;
151 
152 SELECT
153  "Number of coincs after clustering: " || count(*)
154 FROM
155  coinc_event;
156 
157 --
158 -- delete unused coinc_inspiral rows
159 --
160 
161 DELETE FROM
162  coinc_inspiral
163 WHERE
164  coinc_event_id NOT IN (
165  SELECT
166  coinc_event_id
167  FROM
168  coinc_event
169  );
170 
171 --
172 -- delete unused coinc_event_map rows
173 --
174 
175 DELETE FROM
176  coinc_event_map
177 WHERE
178  coinc_event_id NOT IN (
179  SELECT
180  coinc_event_id
181  FROM
182  coinc_event
183  );
184 
185 --
186 -- delete unused sngl_inspiral rows
187 --
188 
189 DELETE FROM
190  sngl_inspiral
191 WHERE
192  event_id NOT IN (
193  SELECT
194  event_id
195  FROM
196  coinc_event_map
197  WHERE
198  table_name == 'sngl_inspiral'
199  );
200 
201 --
202 -- shrink the file
203 --
204 
205 VACUUM;