1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18   
 19   
 20   
 21   
 22   
 23   
 24   
 25   
 26  """ 
 27  A collection of functions needed for ligolw_cbc_compute_durations 
 28  """ 
 29   
 30  import sys 
 31  import sqlite3 
 32  from operator import itemgetter 
 33   
 34  from glue import iterutils 
 35  from glue import segments 
 36  from glue.ligolw import table 
 37  from glue.ligolw import lsctables 
 38  from glue.ligolw import dbtables 
 39  from glue.ligolw.utils import segments as ligolw_segments 
 40   
 41   
 42   
 43   
 44   
 45   
 46   
 47   
 48   
 50          """ 
 51          Return a glue.segments.segmentlistdict object containing coalesced single-ifo 
 52          segments obtained from the search_summary table. 
 53   
 54          @param connection: sqlite connection for the input database 
 55          @param usertag: the usertag for the desired filter jobs e.g. ("FULL_DATA","PLAYGROUND") 
 56          """ 
 57   
 58          xmldoc = dbtables.get_xml(connection) 
 59          seglist_dict = segments.segmentlistdict() 
 60           
 61          for row in map(  
 62                  lsctables.SearchSummaryTable.get_table(xmldoc).row_from_cols,  
 63                  connection.cursor().execute(""" 
 64                          SELECT search_summary.* 
 65                          FROM search_summary 
 66                                  JOIN process_params ON ( 
 67                                          process_params.process_id == search_summary.process_id) 
 68                          WHERE 
 69                                  process_params.value == :1 
 70                                  AND process_params.program == :2 
 71                  """, (usertag, program_name) )): 
 72   
 73                  instrument = row.get_ifos().pop() 
 74                  if usertag == "FULL_DATA" or usertag == "PLAYGROUND": 
 75                          try: 
 76                                  seglist_dict[instrument].append(row.get_out()) 
 77                          except KeyError: 
 78                                  seglist_dict[instrument] = [row.get_out()] 
 79                  else: 
 80                          buffer_time = 72 
 81                          filtered_segment = segments.segment(row.get_in()[0]+buffer_time, row.get_in()[1]-buffer_time) 
 82                          try: 
 83                                  seglist_dict[instrument].append(filtered_segment) 
 84                          except KeyError: 
 85                                  seglist_dict[instrument] = [filtered_segment] 
 86                           
 87          xmldoc.unlink() 
 88   
 89          seglist_dict = segments.segmentlistdict((key, segments.segmentlist(sorted(set(value)))) for key, value in seglist_dict.items()) 
 90          return seglist_dict 
  91   
 93          """ 
 94          Returns two dictionaries, one of the on instruments and the other of the excluded  
 95          instruments in a given coincident time type e.g. (H1,L1; H1,L1,V1). The dictionary 
 96          keys are in the same format as the instruments column of the experiment table. 
 97   
 98          @param ifo_keys: an sorted list of single ifos as strings 
 99          @param min_num_ifos: the minimum number of ifos in a combination 
100          """ 
101          on_ifos = {} 
102          excluded_ifos = {} 
103          ifo_set = set(ifo_keys) 
104          for num_ifos in range(min_num_ifos, len(ifo_set)+1): 
105                  for sub_combo in iterutils.choices( list(ifo_set), num_ifos): 
106                          sorted_on_list = sorted(sub_combo) 
107                          on_ifos[','.join(sorted_on_list)] = sorted_on_list 
108                          excluded_ifos[','.join(sorted_on_list)] = list(ifo_set - set(sorted_on_list)) 
109   
110          return on_ifos, excluded_ifos 
 111   
112   
114          """ 
115          Return a dictionary of glue.segments.segmentlistdict objects containing  
116          veto segments dictionaries for each ifo and the top-level keys being the 
117          associated veto-definer names. 
118          """ 
119          veto_segments = {} 
120   
121           
122          veto_def_names = set( lsctables.SegmentDefTable.get_table(xmldoc).getColumnByName('name') ) 
123   
124          for name in veto_def_names: 
125                  if verbose: 
126                          print >>sys.stderr, "Retrieving veto segments for %s..." % name 
127                  try: 
128                          veto_segments[name] = ligolw_segments.segmenttable_get_by_name(xmldoc, name).coalesce() 
129                  except AttributeError: 
130                           
131                           
132                           
133                          from glue.lal import LIGOTimeGPS 
134   
135                          del lsctables.SegmentTable.validcolumns['start_time_ns'] 
136                          del lsctables.SegmentTable.validcolumns['end_time_ns'] 
137   
138                          def get_segment(self): 
139                                  """ 
140                                  Return the segment described by this row. 
141                                  """ 
142                                  return segments.segment(LIGOTimeGPS(self.start_time, 0), LIGOTimeGPS(self.end_time, 0)) 
 143   
144                          def set_segment(self, segment): 
145                                  """ 
146                                  Set the segment described by this row. 
147                                  """ 
148                                  self.start_time = segment[0].seconds 
149                                  self.end_time = segment[1].seconds 
150   
151                          lsctables.Segment.get = get_segment 
152                          lsctables.Segment.set = set_segment 
153   
154                          veto_segments[name] = ligolw_segments.segmenttable_get_by_name(xmldoc, name).coalesce() 
155   
156          return veto_segments 
157   
159          """ 
160          Return the exclusive coinc segments for each (time-slide,on instruments) pair for 
161          time-slides done along a line (as opposed to rings). 
162   
163          @param segments_dict: the glue.segments.segmentlistdict object which contains the  
164                  single-ifo segments used to compute experiment durations 
165          @param offset_vect: the glue.offsetvector object that contains the time shifts 
166                  for a given time-slide. The keys are the set of ifos being shifted.  
167          """ 
168          segments_dict.coalesce() 
169          on_ifos_dict, excluded_ifos_dict = get_allifo_combos(segments_dict, 2) 
170           
171          coinc_segs = segments.segmentlistdict() 
172   
173           
174          for ifo, shift in offset_vect.items(): 
175                  segments_dict.offsets[ifo] = shift 
176   
177          for on_ifos_key, combo in on_ifos_dict.items(): 
178                   
179                  coinc_segs[on_ifos_key] = segments_dict.intersection( combo ) 
180                   
181                   
182                  excluded_ifos = excluded_ifos_dict[on_ifos_key] 
183                  if excluded_ifos: 
184                           
185                          coinc_segs[on_ifos_key] -= segments_dict.union( excluded_ifos ) 
186   
187                  coinc_segs[on_ifos_key].coalesce() 
188   
189          return coinc_segs 
 190   
191 -def get_livetimes(segments_dict, time_slide_dict, verbose = False): 
 192          """ 
193          Obtain the live-times for each set of coincident segments grouped by 
194          time_slide_id and on-ifos. 
195          """ 
196          livetimes = {} 
197   
198          for time_slide_id, offset_vect in time_slide_dict.items(): 
199                   
200                  coinc_segs = get_coinc_segments(segments_dict, offset_vect) 
201   
202                   
203                  for exclusive_ifos, segments_list in coinc_segs.items(): 
204                          livetimes[time_slide_id, exclusive_ifos] = float( abs(segments_list) ) 
205   
206          return livetimes 
 207