3 # Copyright (C) 2015 Chad Hanna, Cody Messick
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 ### This program queries simdb and uses the results to plot information about the
21 ### effective range of the analysis in a while True loop.
23 ### This program is not meant to be executed standalone by a user. It should be
24 ### part of a DAG managing a running gstlal_inspiral online analysis.
26 ### This program takes nine arguments;
28 ### - The path of the injection xml file
29 ### - The path of the cumulative segments xml file
30 ### - The path of the marginalized likelihood xml file
31 ### - The simdb service URL
32 ### - The gracedb pipeline
33 ### - The gracedb group
34 ### - The gracedb search
35 ### - The minimum injection distance in Mpc
36 ### - The maximum injection distance in Mpc
40 # Close stdout file descripter and redirect stdout to stderr
47 # Collect input arguments
50 for VAR in INJ_FILE CUMULATIVE_SEG_FILE MARGINALIZED_LIKELIHOOD_FILE SIMDB_SERVICE_URL PIPELINE GROUP SEARCH MIN_INJ_DIST MAX_INJ_DIST;
do
51 eval $(echo
"${VAR}=\"${1}\"")
56 # Set necessary variables
59 WORKINGDIR=$(dirname ${MARGINALIZED_LIKELIHOOD_FILE})
60 TMP_LIGOLW_XML_GZ=${WORKINGDIR}/tmp_sensitivity_plot_data.xml.gz
61 LIGOLW_XML_GZ=${WORKINGDIR}/sensitivity_plot_data.xml.gz
62 LIGOLW_XML=${WORKINGDIR}/sensitivity_plot_data.xml
63 LIGOLW_SQLITE=${WORKINGDIR}/sensitivity_plot_data.sqlite
64 TMP_TOTAL_SEG_XML_GZ=${WORKINGDIR}/tmp_total_cumulative_segments.xml.gz
65 TOTAL_SEG_XML_GZ=${WORKINGDIR}/total_cumulative_segments.xml.gz
66 TOTAL_SEG_XML=${WORKINGDIR}/total_cumulative_segments.xml
67 STATEVECTOR_SEG_XML=${WORKINGDIR}/tmp_statevector_segments.xml.gz
68 WHITEHT_SEG_XML=${WORKINGDIR}/tmp_whiteht_segments.xml.gz
69 TRIGGER_SEG_XML=${WORKINGDIR}/tmp_trigger_segments.xml.gz
70 TMP_QUERY_XML=${WORKINGDIR}/tmp.xml
71 TMP_CUMULATIVE_SEG_XML=${WORKINGDIR}/tmp_cumulative_segments.xml
74 # Do not start until cumulative segments file is created
77 while ! [ -s ${CUMULATIVE_SEG_FILE} ];
do
83 # Find start time of analysis
86 # FIXME find a less hacky way to get the start time. This method assumes that
87 # the trigger segments for both H1 and L1 start at essentially the same time.
88 # If this ever changes this method may stop working
89 if [ -s ${TOTAL_SEG_XML_GZ} ] && [ $(gzip --test ${TOTAL_SEG_XML_GZ}; echo $?) -eq 0 ]; then
90 START_FILE=${TOTAL_SEG_XML_GZ}
92 START_FILE=${CUMULATIVE_SEG_FILE}
95 SEGMENT_DEF_ID=$(ligolw_print --table segment_definer --column name --column segment_def_id ${START_FILE} | grep triggersegments | sed -e
's/,/\n/g' | grep segment_definer | head -n 1)
96 START=$(ligolw_print --table segment --column start_time --column segment_def_id ${START_FILE} | grep ${SEGMENT_DEF_ID} | sed -e
's/,/\n/g' | sed -e
'/segment_definer/d' | sort -g | head -n 1)
97 echo analysis start time read as ${START}
101 # Set end time of simdb query
104 QUERYSTOP=$(lalapps_tconvert)
107 # Write TOTAL_SEG_XML_GZ, which will keep track of segments through entire analysis
110 # First check
if TMP_TOTAL_SEG_XML_GZ exists and is uncorrupted
111 if [ -s ${TMP_TOTAL_SEG_XML_GZ} ] && [ $(gzip --test ${TMP_TOTAL_SEG_XML_GZ}; echo $?) -eq 0 ]; then
112 mv ${TMP_TOTAL_SEG_XML_GZ} ${TOTAL_SEG_XML_GZ}
115 if ! [ -s ${TOTAL_SEG_XML_GZ} ] || ! [ $(gzip --test ${TOTAL_SEG_XML_GZ}; echo $?) -eq 0 ]; then
116 # If TOTAL_SEG_XML_GZ exists but is corrupted, then something went
117 # wrong and we will need to copy the cumulative segments file
118 # again. If TOTAL_SEG_XML_GZ doesn't exist, then obviously we need
119 # to copy the cumulative segments file
120 echo creating ${TOTAL_SEG_XML_GZ}, either because it does not already exist or it is a corrupt file
121 cp ${CUMULATIVE_SEG_FILE} ${TOTAL_SEG_XML}
122 gzip ${TOTAL_SEG_XML}
123 while ! [ $(gzip --test ${TOTAL_SEG_XML_GZ}; echo $?) -eq 0 ];
do
124 # The reason for this while loop is that the cumulative
125 # segments file is updated periodically by the
126 # analysis, so this prevents issues where this program
127 # tries to copy the cumulative segments file when it is
128 # in the middle of being updated, resulting in a
129 # corrupted TOTAL_SEG_XML_GZ
131 cp ${CUMULATIVE_SEG_FILE} ${TOTAL_SEG_XML}
132 gzip ${TOTAL_SEG_XML}
134 cp ${TOTAL_SEG_XML_GZ} ${TMP_TOTAL_SEG_XML_GZ}
136 cp ${TOTAL_SEG_XML_GZ} ${TMP_TOTAL_SEG_XML_GZ}
138 # Make a copy of the cumulative segments file to ensure that it
139 # doesn't get updated in the middle of being coalesced with
140 # TMP_TOTAL_SEG_XML_GZ
141 # FIXME The copy process can be skipped and the coalescing
142 # process can be improved by modifying
143 # gstlal_segments_operations gstlal-ugly to take multiple
145 cp ${CUMULATIVE_SEG_FILE} ${TMP_CUMULATIVE_SEG_XML}
146 gzip ${TMP_CUMULATIVE_SEG_XML}
147 while ! [ $(gzip --test ${TMP_CUMULATIVE_SEG_XML}.gz; echo $?) -eq 0 ];
do
149 cp ${CUMULATIVE_SEG_FILE} ${TMP_CUMULATIVE_SEG_XML}
150 gzip ${TMP_CUMULATIVE_SEG_XML}
152 gstlal_segments_operations --
union --segment-file1 ${TMP_CUMULATIVE_SEG_XML}.gz --segment-file2 ${TMP_TOTAL_SEG_XML_GZ} --output-file ${STATEVECTOR_SEG_XML} --segment-name1 statevectorsegments --segment-name2 statevectorsegments --output-segment-name statevectorsegments --verbose
153 gstlal_segments_operations --
union --segment-file1 ${TMP_CUMULATIVE_SEG_XML}.gz --segment-file2 ${TMP_TOTAL_SEG_XML_GZ} --output-file ${WHITEHT_SEG_XML} --segment-name1 whitehtsegments --segment-name2 whitehtsegments --output-segment-name whitehtsegments --verbose
154 gstlal_segments_operations --
union --segment-file1 ${TMP_CUMULATIVE_SEG_XML}.gz --segment-file2 ${TMP_TOTAL_SEG_XML_GZ} --output-file ${TRIGGER_SEG_XML} --segment-name1 triggersegments --segment-name2 triggersegments --output-segment-name triggersegments --verbose
156 for SEG_FILE in ${STATEVECTOR_SEG_XML} ${WHITEHT_SEG_XML} ${TRIGGER_SEG_XML};
do
157 if [ $(ligolw_print --table segment ${SEG_FILE} | wc -l) -gt 0 ]; then
158 SEG_FILES=
"${SEG_FILE} ${SEG_FILES}"
161 ligolw_add ${SEG_FILES} --verbose --output ${TMP_TOTAL_SEG_XML_GZ}
162 rm ${STATEVECTOR_SEG_XML} ${WHITEHT_SEG_XML} ${TRIGGER_SEG_XML} ${TMP_CUMULATIVE_SEG_XML}.gz
166 # Set the simdb query start time and create the events+injections file
167 # if first time through loop or if LIGOLW_XML_GZ file has been corrupted
170 # First check if TMP_LIGOLW_XML_GZ exists and is uncorrupted
171 if [ -s ${TMP_LIGOLW_XML_GZ} ] && [ $(gzip --test ${TMP_LIGOLW_XML_GZ}; echo $?) -eq 0 ]; then
172 mv ${TMP_LIGOLW_XML_GZ} ${LIGOLW_XML_GZ}
175 if ! [ -s ${LIGOLW_XML_GZ} ] || ! [ $(gzip --test ${LIGOLW_XML_GZ}; echo $?) -eq 0 ]; then
176 # If LIGOLW_XML_GZ exists but is corrupted, then something went
177 # wrong and it will need to be remade
178 echo creating ${LIGOLW_XML_GZ}, either because it does not already exist or it is a corrupt file
180 if [[ ${INJ_FILE} = *
".gz" ]]; then
181 cp ${INJ_FILE} ${LIGOLW_XML_GZ}
183 cp ${INJ_FILE} ${LIGOLW_XML}
186 cp ${LIGOLW_XML_GZ} ${TMP_LIGOLW_XML_GZ}
188 cp ${LIGOLW_XML_GZ} ${TMP_LIGOLW_XML_GZ}
190 if [ $(ligolw_print --table coinc_inspiral --column end_time ${TMP_LIGOLW_XML_GZ} | wc -l) -eq 0 ]; then
193 # QUERYSTART is chosen so that the simdb query will
194 # always begin at the time of the last event which was
195 # downloaded. This way we avoid any potential issues
196 # with the asynchronous nature of the simdb queries and
197 # the segment updates
198 QUERYSTART=$(ligolw_print --table coinc_inspiral --column end_time ${TMP_LIGOLW_XML_GZ} | sort -g | tail -n 1)
203 # Download simdb file to a temporary file and then add it to the the
204 # events+injections file, but
using atomic operations in
case something
208 echo querying simdb from ${QUERYSTART} to ${QUERYSTOP}...
209 gracedb --ligolw --service-url=${SIMDB_SERVICE_URL} search ${QUERYSTART}..${QUERYSTOP} ${PIPELINE} ${GROUP} ${SEARCH} > ${TMP_QUERY_XML}
211 if [ $(ligolw_print --table coinc_inspiral --column end_time ${TMP_QUERY_XML} | wc -l) -gt 0 ]; then
212 ligolw_add ${TMP_LIGOLW_XML_GZ} ${TMP_QUERY_XML} --verbose --output ${TMP_LIGOLW_XML_GZ}
218 # If events have been downloaded since the analysis has begun, clean up
219 # the events+injections file and then generate the range plot
222 if [ $(ligolw_print --table coinc_inspiral --column end_time ${TMP_LIGOLW_XML_GZ} | wc -l) -gt 0 ]; then
225 # Delete old segments from events+injections file, then add new
226 # segments, again using atomic operations
228 if [ $(ligolw_print --table segment ${TMP_LIGOLW_XML_GZ} | wc -l) -gt 0 ]; then
229 lalapps_run_sqlite --verbose --sql=
'DELETE FROM segment; DELETE FROM segment_definer' ${TMP_LIGOLW_XML_GZ}
231 ligolw_add --verbose ${TMP_LIGOLW_XML_GZ} ${TMP_TOTAL_SEG_XML_GZ} --output ${TMP_LIGOLW_XML_GZ}
233 # FIXME Find a better way to get the location of simplify_and_cluster.sql
234 lalapps_run_sqlite --verbose --sql-file=$(dirname $(dirname $(which gstlal_inspiral)))/share/gstlal/simplify_and_cluster.sql ${TMP_LIGOLW_XML_GZ}
236 # Get rid of duplicate events, of which there should only be one or two
237 lalapps_run_sqlite --verbose --sql=
'CREATE TEMPORARY TABLE _idmap_ AS
238 SELECT old.coinc_event_id AS old_id,
239 MIN(new.coinc_event_id) AS new_id
241 coinc_inspiral AS old JOIN
242 coinc_inspiral AS new ON
243 (new.end_time == old.end_time AND old.end_time >= '"${QUERYSTART}"')
246 CREATE INDEX tmpindex ON _idmap_(old_id);
247 DELETE FROM sngl_inspiral WHERE event_id IN (SELECT event_id FROM coinc_event_map WHERE coinc_event_id IN (SELECT old_id FROM _idmap_ WHERE old_id != new_id));
248 DELETE FROM coinc_event_map WHERE coinc_event_id IN (SELECT old_id FROM _idmap_ WHERE old_id != new_id);
249 DELETE FROM coinc_inspiral WHERE coinc_event_id IN (SELECT old_id FROM _idmap_ WHERE old_id != new_id);
250 DELETE FROM coinc_event WHERE coinc_event_id IN (SELECT old_id FROM _idmap_ WHERE old_id != new_id);
252 DROP TABLE _idmap_;' ${TMP_LIGOLW_XML_GZ}
254 lalapps_inspinjfind --verbose --revert ${TMP_LIGOLW_XML_GZ}
255 lalapps_inspinjfind --verbose ${TMP_LIGOLW_XML_GZ}
257 # Try to generate the sqlite database, but if something goes
258 # wrong then delete the database and try again. This should fix
259 # issues with the database locking
260 ligolw_sqlite --verbose --database ${LIGOLW_SQLITE} --replace ${TMP_LIGOLW_XML_GZ} || { rm ${LIGOLW_SQLITE} && ligolw_sqlite --verbose --database ${LIGOLW_SQLITE} ${TMP_LIGOLW_XML_GZ}; }
262 gstlal_inspiral_plot_sensitivity --data-segments-name=statevectorsegments --bin-by-chirp-mass --verbose ${LIGOLW_SQLITE}
264 gstlal_ll_inspiral_plot_sensitivity --base64 --cumulative-segment-file ${TMP_TOTAL_SEG_XML_GZ} --marginalized-likelihood-file ${MARGINALIZED_LIKELIHOOD_FILE} --min-inj-distance ${MIN_INJ_DIST} --max-inj-distance ${MAX_INJ_DIST} --simdb-query-end-time ${QUERYSTOP} --verbose ${LIGOLW_SQLITE}