gstlal-inspiral  1.9.2
 All Classes Namespaces Files Functions
gstlal_ll_inspiral_calculate_range
1 #!/bin/bash
2 #
3 # Copyright (C) 2015 Chad Hanna, Cody Messick
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 
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.
22 ###
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.
25 ###
26 ### This program takes nine arguments;
27 ###
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
37 
38 
39 #
40 # Close stdout file descripter and redirect stdout to stderr
41 #
42 
43 exec 1<&-
44 exec 1>&2
45 
46 #
47 # Collect input arguments
48 #
49 
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}\"")
52  shift
53 done
54 
55 #
56 # Set necessary variables
57 #
58 
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
72 
73 #
74 # Do not start until cumulative segments file is created
75 #
76 
77 while ! [ -s ${CUMULATIVE_SEG_FILE} ]; do
78  echo sleeping...
79  sleep 300
80 done
81 
82 #
83 # Find start time of analysis
84 #
85 
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}
91 else
92  START_FILE=${CUMULATIVE_SEG_FILE}
93 fi
94 
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}
98 
99 while true; do
100  #
101  # Set end time of simdb query
102  #
103 
104  QUERYSTOP=$(lalapps_tconvert)
105 
106  #
107  # Write TOTAL_SEG_XML_GZ, which will keep track of segments through entire analysis
108  #
109 
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}
113  fi
114 
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
130  sleep 15
131  cp ${CUMULATIVE_SEG_FILE} ${TOTAL_SEG_XML}
132  gzip ${TOTAL_SEG_XML}
133  done
134  cp ${TOTAL_SEG_XML_GZ} ${TMP_TOTAL_SEG_XML_GZ}
135  else
136  cp ${TOTAL_SEG_XML_GZ} ${TMP_TOTAL_SEG_XML_GZ}
137 
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
144  # segment names
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
148  sleep 15
149  cp ${CUMULATIVE_SEG_FILE} ${TMP_CUMULATIVE_SEG_XML}
150  gzip ${TMP_CUMULATIVE_SEG_XML}
151  done
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
155  SEG_FILES=""
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}"
159  fi
160  done
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
163  fi
164 
165  #
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
168  #
169 
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}
173  fi
174 
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
179  QUERYSTART=${START}
180  if [[ ${INJ_FILE} = *".gz" ]]; then
181  cp ${INJ_FILE} ${LIGOLW_XML_GZ}
182  else
183  cp ${INJ_FILE} ${LIGOLW_XML}
184  gzip ${LIGOLW_XML}
185  fi
186  cp ${LIGOLW_XML_GZ} ${TMP_LIGOLW_XML_GZ}
187  else
188  cp ${LIGOLW_XML_GZ} ${TMP_LIGOLW_XML_GZ}
189 
190  if [ $(ligolw_print --table coinc_inspiral --column end_time ${TMP_LIGOLW_XML_GZ} | wc -l) -eq 0 ]; then
191  QUERYSTART=${START}
192  else
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)
199  fi
200  fi
201 
202  #
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
205  # goes wrong
206  #
207 
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}
210 
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}
213  fi
214 
215  rm ${TMP_QUERY_XML}
216 
217  #
218  # If events have been downloaded since the analysis has begun, clean up
219  # the events+injections file and then generate the range plot
220  #
221 
222  if [ $(ligolw_print --table coinc_inspiral --column end_time ${TMP_LIGOLW_XML_GZ} | wc -l) -gt 0 ]; then
223 
224  #
225  # Delete old segments from events+injections file, then add new
226  # segments, again using atomic operations
227  #
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}
230  fi
231  ligolw_add --verbose ${TMP_LIGOLW_XML_GZ} ${TMP_TOTAL_SEG_XML_GZ} --output ${TMP_LIGOLW_XML_GZ}
232 
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}
235 
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
240  FROM
241  coinc_inspiral AS old JOIN
242  coinc_inspiral AS new ON
243  (new.end_time == old.end_time AND old.end_time >= '"${QUERYSTART}"')
244  GROUP BY
245  old.coinc_event_id;
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);
251  DROP INDEX tmpindex;
252  DROP TABLE _idmap_;' ${TMP_LIGOLW_XML_GZ}
253 
254  lalapps_inspinjfind --verbose --revert ${TMP_LIGOLW_XML_GZ}
255  lalapps_inspinjfind --verbose ${TMP_LIGOLW_XML_GZ}
256 
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}; }
261 
262  gstlal_inspiral_plot_sensitivity --data-segments-name=statevectorsegments --bin-by-chirp-mass --verbose ${LIGOLW_SQLITE}
263  echo plotting...
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}
265  fi
266 
267  echo sleeping...
268  sleep 600
269 done