-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheventDictClassMethods.py
1216 lines (1138 loc) · 61 KB
/
eventDictClassMethods.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
description = "methods and attributes of the EventDict class"
author = "Min-A Cho ([email protected]), Deep Chatterjee ([email protected])"
#-----------------------------------------------------------------------
# Import packages
#-----------------------------------------------------------------------
from ligo.gracedb.rest import GraceDb, HTTPError
import os
import json
import pickle
import urllib
import logging
import ConfigParser
import time
import datetime
import subprocess as sp
import re
import operator
import functools
import random
#-----------------------------------------------------------------------
# Creating the global event dictionaries variable for local bookkeeping
#-----------------------------------------------------------------------
global eventDicts # global variable for local bookkeeping of event candidate checks, files, labels, etc.
### important thing is it saves the event_dict as an INSTANCE of the EventDict class
eventDicts = {} # it's a dictionary storing data in the form 'graceid': event_dict where each event_dict is created below in class EventDict
global eventDictionaries # global variable for local bookkeeping
### important thing is it saves the event_dict as a DICTIONARY
eventDictionaries = {}
#-----------------------------------------------------------------------
# Define initGracedb() function
#-----------------------------------------------------------------------
def initGraceDb(client):
if 'http' in client:
g = GraceDb(client)
else:
# assume local path to FAKE_DB is passed
from ligoTest.gracedb.rest import FakeDb
# FakeDb directory is created if non-existent
if os.path.exists(client):
g = FakeDb(client)
else:
g = None
return g
#-----------------------------------------------------------------------
#-----------------------------------------------------------------------
# EventDict class
#-----------------------------------------------------------------------
class EventDict():
'''
creates an event_dict for each event candidate to keep track of checks, files, comments, labels coming in
'''
def __init__(self):
self.data = {} # create a blank dictionary that gets populated later
def __getitem__(self, key):
self.key = key
return self.data[self.key]
def setup(self, dictionary, graceid, configdict, client, config, logger):
self.dictionary = dictionary # a dictionary either extracted from an lvalert or from a call to graceDb
self.graceid = graceid
self.configdict = configdict # stores settings used
self.client = client
self.config = config
self.logger = logger
self.data.update({
'advocate_signoffCheckresult': None,
'advocatelogkey' : 'no',
'advocatesignoffs' : [],
'configuration' : self.configdict,
'currentstate' : 'new_to_preliminary',
'far' : self.dictionary['far'],
'farCheckresult' : None,
'farlogkey' : 'no',
'em_coinc_json' : None,
'expirationtime' : None,
'external_trigger' : None,
'gpstime' : float(self.dictionary['gpstime']),
'graceid' : self.graceid,
'group' : self.dictionary['group'],
'groupergroup' : {},
'have_lvem_skymapCheckresult': None,
'idq_joint_fapCheckresult' : None,
'idqlogkey' : 'no',
'idqvalues' : {},
'ifoslogkey' : 'no',
'ifosCheckresult' : None,
'injectionCheckresult' : None,
'injectionsfound' : None,
'injectionlogkey' : 'no',
'instruments' : str(self.dictionary['instruments']).split(','),
'jointfapvalues' : {},
'labelCheckresult' : None,
'labels' : self.dictionary['labels'].keys(),
'lastsentskymap' : None,
'lastsentpreliminaryskymap' : None,
'loggermessages' : [],
'lvemskymaps' : {},
'operator_signoffCheckresult': None,
'operatorlogkey' : 'no',
'operatorsignoffs' : {},
'pipeline' : self.dictionary['pipeline'],
'search' : self.dictionary['search'] if self.dictionary.has_key('search') else '',
'virgo_dqCheckresult' : None,
'virgo_dqIsVetoed' : None,
'virgo_dqlogkey' : 'no',
'virgoInjections' : 0, ### hardcoding to be 0 so that we do not wait for virgo injection statement to come in
'voeventerrors' : [],
'voevents' : []
})
def update(self):
'''
creates an event_dict with signoff and iDQ information for self.graceid
this event_dict starts off with currentstate new_to_preliminary
'''
# get the most recent voevent information
voevent_dicts = self.client.voevents(self.graceid).json()['voevents']
for voevent in voevent_dicts: # this traverses voevents in the order they were sent
voevent_text = voevent['text'] # this is the actual xml text
internal = re.findall(r'internal\" dataType=\"int\" value=\"(\S+)\"', voevent_text)[0]
vetted = re.findall(r'Vetted\" dataType=\"int\" value=\"(\S+)\"', voevent_text)[0]
open_alert = re.findall(r'OpenAlert\" dataType=\"int\" value=\"(\S+)\"', voevent_text)[0]
hardware_inj = re.findall(r'HardwareInj\" dataType=\"int\" value=\"(\S+)\"', voevent_text)[0]
voevent_type = voevent['voevent_type']
if voevent_type=='PR':
voevent_type = 'preliminary'
elif voevent_type=='IN':
voevent_type = 'initial'
elif voevent_type=='UP':
voevent_type = 'update'
elif voevent_type=='RE':
voevent_type = 'retraction'
# update sent skymaps if any from the voevent
skymap = re.findall(r'skymap_fits_basic\" dataType=\"string\" value=\"(\S+)\"', voevent_text)
# update event_dict in the case there were any skymaps
if len(skymap)==1:
skymap = re.findall(r'files/(\S+)', skymap[0])[0]
if voevent_type=='preliminary':
self.data['lastsentpreliminaryskymap'] = skymap
elif voevent_type=='initial' or voevent_type=='update':
self.data['lastsentskymap'] = skymap
elif len(skymap)==0:
skymap = None
thisvoevent = '(internal,vetted,open_alert,hardware_inj,skymap):({0},{1},{2},{3},{4})-'.format(internal, vetted, open_alert, hardware_inj, skymap) + voevent_type
thisvoevent = '{0}-'.format(len(self.data['voevents']) + 1) + thisvoevent
self.data['voevents'].append(thisvoevent)
# update signoff information if available
url = self.client.templates['signoff-list-template'].format(graceid=self.graceid) # construct url for the operator/advocate signoff list
signoff_list = self.client.get(url).json()['signoff'] # pull down signoff list
for signoff_object in signoff_list:
record_signoff(self.data, signoff_object)
# update iDQ information, skymaps, EM-Bright information, and past farCheck results if available
log_dicts = self.client.logs(self.graceid).json()['log']
for message in log_dicts: # going through the log from oldest to most recent for recording skymaps
if 'lvem' in message['tag_names'] and '.fits' in message['filename']:
record_skymap(self.data, message['filename'], message['issuer']['display_name'], logger) # this way, the ordering in which the skymaps came in is properly noted
else:
pass
for message in reversed(log_dicts): # going through the log from most recent to oldest message
if re.match('minimum glitch-FAP', message['comment']):
record_idqvalues(self.data, message['comment'], logger)
elif re.match('EM-Bright probabilities computed from detection pipeline', message['comment']):
record_em_bright(self.data, message['comment'], logger)
elif re.match('AP: Candidate event rejected due to large FAR', message['comment']):
default_farthresh = float(re.findall(r'>= (.*)', message['comment'])[0])
self.configdict['default_farthresh'] = default_farthresh
self.data['configuration'] = self.configdict
self.data['farlogkey'] = 'yes'
self.data['farCheckresult'] = False
elif re.match('AP: Candidate event has low enough FAR', message['comment']):
default_farthresh = float(re.findall(r'< (.*)', message['comment'])[0])
self.configdict['default_farthresh'] = default_farthresh
self.data['configuration'] = self.configdict
self.data['farlogkey'] = 'yes'
self.data['farCheckresult'] = True
elif 'AP: Automatically passed Virgo check' in message['comment']:
self.data['virgo_dqCheckresult'] = True
self.data['virgo_dqlogkey'] = 'yes'
elif 'AP: Rejecting because of Virgo veto' in message['comment']:
self.data['virgo_dqCheckresult'] = False
self.data['virgo_dqlogkey'] = 'yes'
elif 'AP: Passed Virgo DQ check' in message['comment']:
self.data['virgo_dqCheckresult'] = True
self.data['virgo_dqlogkey'] = 'yes'
#elif 'V1 veto channel' in message['comment'] and message['comment'].endswith('vetoed'):
# record_virgo_dqIsVetoed(self.data, message['comment'], logger)
#elif 'V1 hardware injection' in message['comment'] and message['comment'].endswith('injections'):
# record_virgoInjections(self.data, message['comment'], logger)
else:
pass
#-----------------------------------------------------------------------
# external GRB trigger local data bookkeeping
#-----------------------------------------------------------------------
def grb_trigger_setup(self, dictionary, graceid, client, config, logger):
self.dictionary = dictionary # a dictionary either extracted from an lvalert or from a call to gracedb
self.graceid = graceid
self.client = client
self.config = config
self.logger = logger
self.data.update({
'em_coinc_json' : None,
'expirationtime' : None,
'graceid' : self.graceid,
'grb_offline_json' : None,
'grb_online_json' : None,
'labels' : self.dictionary['labels'].keys(),
'loggermessages' : [],
'pipeline' : self.dictionary['pipeline']
})
#-----------------------------------------------------------------------
# ifosCheck
#-----------------------------------------------------------------------
def ifosCheck(self):
ifos = self.data['instruments'] # instruments as a list
ifolist = ",".join(ifos) # instrument list as a string
num_ifo = len(ifos) # number of ifos
res = False if num_ifo == 1 else True if num_ifo >= 3 \
else 'V1' not in ifos
self.data['ifosCheckresult'] = res
if not(res):
self.client.writeLog(self.graceid, \
'AP: Candidate event rejected due to single ifo OR V1 in two-ifo coinc')
self.data['ifoslogkey'] = 'yes'
message = '{0} -- {1} -- Rejecting due to single ifo OR presence of V1 in two-ifo coinc: {2}'.format(convertTime(), self.graceid, ifolist)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
return res
#-----------------------------------------------------------------------
# farCheck
#-----------------------------------------------------------------------
def __get_farthresh__(self, pipeline, search, config):
try:
return config.getfloat('farCheck', 'farthresh[{0}.{1}]'.format(pipeline, search))
except:
return config.getfloat('farCheck', 'default_farthresh')
def farCheck(self):
'''
checks to see if the far of the event candidate is less than the threshold
'''
farCheckresult = self.data['farCheckresult']
if farCheckresult!=None:
return farCheckresult
else:
far = self.data['far']
pipeline = self.data['pipeline']
search = self.data['search']
farthresh = self.__get_farthresh__(pipeline, search, self.config)
if far >= farthresh:
self.client.writeLog(self.graceid, 'AP: Candidate event rejected due to large FAR. {0} >= {1}'.format(far, farthresh), tagname='em_follow')
self.data['farlogkey'] = 'yes'
message = '{0} -- {1} -- Rejected due to large FAR. {2} >= {3}'.format(convertTime(), self.graceid, far, farthresh)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
self.data['farCheckresult'] = False
else:
pass
return False
elif far==None:
self.client.writeLog(self.graceid, 'AP: Candidate event is missing FAR.', tagname='em_follow')
self.data['farlogkey'] = 'yes'
message = '{0} -- {1} -- Candidate event is missing FAR.'.format(convertTime(), self.graceid)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
self.data['farCheckresult'] = False
else:
pass
return False
elif far < farthresh:
self.client.writeLog(self.graceid, 'AP: Candidate event has low enough FAR. {0} < {1}'.format(far, farthresh), tagname='em_follow')
self.data['farlogkey'] = 'yes'
message = '{0} -- {1} -- Low enough FAR. {2} < {3}'.format(convertTime(), self.graceid, far, farthresh)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
self.data['farCheckresult'] = True
else:
pass
return True
#-----------------------------------------------------------------------
# labelCheck
#-----------------------------------------------------------------------
def labelCheck(self):
'''
checks whether event has INJ, DQV, EM_Throttled, EM_Superseded, H1NO, L1NO, V1NO, or ADVNO label. it will treat INJ as a real event or not depending on config setting
'''
labels = self.data['labels']
if checkLabels(labels, self.config) > 0:
message = '{0} -- {1} -- Ignoring event due to bad label.'.format(convertTime(), self.graceid)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
self.data['labelCheckresult'] = False
else:
pass
return False
else:
self.data['labelCheckresult'] = True
return True
#-----------------------------------------------------------------------
# injectionCheck
#-----------------------------------------------------------------------
def injectionCheck(self):
injectionCheckresult = self.data['injectionCheckresult']
if injectionCheckresult!=None:
return injectionCheckresult
elif 'V1' in self.data['instruments']:
virgoInjections = self.data['virgoInjections']
if virgoInjections==None: # we need virgo Injection information which has not come in yet
message = '{0} -- {1} -- Have not received Virgo injection statement yet.'.format(convertTime(), self.graceid)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
else:
pass
return None
else: # update the number of injectionsfound with the virgoInjections information
self.data['injectionsfound'] = virgoInjections
pass
eventtime = float(self.data['gpstime'])
time_duration = self.config.getfloat('injectionCheck', 'time_duration')
from raven.search import query
th = time_duration
tl = -th
Injections = query('HardwareInjection', eventtime, tl, th) # XXX this only reflects the raven search results! Nicolas says Virgo might not be able to record their injections in GraceDb right now, 6/30/17
if 'V1' not in self.data['instruments']:
self.data['injectionsfound'] = 0 # start with 0 and then add what we just found with raven query
self.data['injectionsfound'] += len(Injections) # now we have the total injections found, including those from Virgo if V1 was part of the instruments list
hardware_inj = self.config.get('labelCheck', 'hardware_inj')
injectionsfound = self.data['injectionsfound']
if injectionsfound > 0:
# label as INJ if INJ label is not there already
if 'INJ' not in self.data['labels']:
self.client.writeLabel(self.graceid, 'INJ')
if hardware_inj=='no':
self.client.writeLog(self.graceid, 'AP: Ignoring new event because we found a hardware injection +/- {0} seconds of event gpstime or from Virgo injections statement if V1 is involved.'.format(th), tagname = "em_follow")
self.data['injectionlogkey'] = 'yes'
message = '{0} -- {1} -- Ignoring new event because we found a hardware injection +/- {2} seconds of event gpstime or from Virgo injections statement if V1 is involved.'.format(convertTime(), self.graceid, th)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
self.data['injectionCheckresult'] = False
else:
pass
return False
else:
self.client.writeLog(self.graceid, 'AP: Found hardware injection +/- {0} seconds of event gpstime or from Virgo injections statement if V1 is involved but treating as real event in config.'.format(th), tagname = "em_follow")
self.data['injectionlogkey'] = 'yes'
message = '{0} -- {1} -- Found hardware injection +/- {2} seconds of event gpstime or from Virgo injections statement if V1 is involved but treating as real event in config.'.format(convertTime(), self.graceid, th)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
self.data['injectionCheckresult'] = True
else:
pass
return True
elif injectionsfound==0:
self.client.writeLog(self.graceid, 'AP: No hardware injection found near event gpstime +/- {0} seconds or from Virgo injections statement if V1 is involved.'.format(th), tagname="em_follow")
self.data['injectionlogkey'] = 'yes'
message = '{0} -- {1} -- No hardware injection found near event gpstime +/- {2} seconds or from Virgo injections statement if V1 is involved.'.format(convertTime(), self.graceid, th)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
self.data['injectionCheckresult'] = True
else:
pass
return True
#-----------------------------------------------------------------------
# have_lvem_skymapCheck
#-----------------------------------------------------------------------
def have_lvem_skymapCheck(self):
'''
checks whether there is an lvem tagged skymap that has not been sent in an alert
'''
# this function should only return True or None, never False
# if return True, we have a new lvem skymap
# otherwise, add this Check to queueByGraceID
currentstate = self.data['currentstate']
lvemskymaps = self.data['lvemskymaps'].keys()
if currentstate=='preliminary_to_initial':
if len(lvemskymaps)>=1:
self.data['have_lvem_skymapCheckresult'] = True
skymap = sorted(lvemskymaps)[-1]
skymap = re.findall(r'-(\S+)', skymap)[0]
message = '{0} -- {1} -- Initial skymap tagged lvem {2} available.'.format(convertTime(), self.graceid, skymap)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
else:
pass
return True
else:
self.data['have_lvem_skymapCheckresult'] = None
return None
elif (currentstate=='initial_to_update' or currentstate=='complete'):
if len(lvemskymaps)>=2:
if lvemskymaps[-1]!=self.data['lastsentskymap']:
self.data['have_lvem_skymapCheckresult'] = True
skymap = sorted(lvemskymaps)[-1]
skymap = re.findall(r'-(\S+)', skymap)[0]
message = '{0} -- {1} -- Update skymap tagged lvem {2} available.'.format(convertTime(), self.graceid, skymap)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
else:
pass
return True
else:
self.data['have_lvem_skymapCheckresult'] = None
return None
else:
self.data['have_lvem_skymapCheckresult'] = None
return None
#-----------------------------------------------------------------------
# idq_joint_fapCheck
#-----------------------------------------------------------------------
def __get_idqthresh__(self, pipeline, search, config):
try:
return config.getfloat('idq_joint_fapCheck', 'idqthresh[{0}.{1}]'.format(pipeline, search))
except:
return config.getfloat('idq_joint_fapCheck', 'default_idqthresh')
def __compute_joint_fap_values__(self, config):
idqvalues = self.data['idqvalues']
jointfapvalues = self.data['jointfapvalues']
idq_pipelines = config.get('idq_joint_fapCheck', 'idq_pipelines')
idq_pipelines = idq_pipelines.replace(' ', '')
idq_pipelines = idq_pipelines.split(',')
for idqpipeline in idq_pipelines:
pipeline_values = []
for key in idqvalues.keys():
if idqpipeline in key:
pipeline_values.append(idqvalues[key])
jointfapvalues[idqpipeline] = functools.reduce(operator.mul, pipeline_values, 1)
def idq_joint_fapCheck(self):
group = self.data['group']
ignore_idq = self.config.get('idq_joint_fapCheck', 'ignore_idq')
idq_joint_fapCheckresult = self.data['idq_joint_fapCheckresult']
if idq_joint_fapCheckresult!=None:
return idq_joint_fapCheckresult
elif group in ignore_idq:
# self.logger.info('{0} -- {1} -- Not using idq checks for events with group(s) {2}.'.format(convertTime(), self.graceid, ignore_idq))
self.data['idq_joint_fapCheckresult'] = True
return True
else:
pipeline = self.data['pipeline']
search = self.data['search']
idqthresh = self.__get_idqthresh__(pipeline, search, self.config)
self.__compute_joint_fap_values__(self.config)
idqvalues = self.data['idqvalues']
idqlogkey = self.data['idqlogkey']
idqinstruments = ['H1', 'L1'] #only H1 and L1 have idq running, not V1
instruments = list(set(idqinstruments).intersection(self.data['instruments'])) # the intersection of idqinstruments and our trigger's list of instruments
jointfapvalues = self.data['jointfapvalues']
idq_pipelines = self.config.get('idq_joint_fapCheck', 'idq_pipelines')
idq_pipelines = idq_pipelines.replace(' ', '')
idq_pipelines = idq_pipelines.split(',')
if len(idqvalues)==0:
message = '{0} -- {1} -- Have not gotten all the minfap values yet.'.format(convertTime(), self.graceid)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
else:
pass
return None
elif (0 < len(idqvalues) < (len(idq_pipelines)*len(instruments))):
message = '{0} -- {1} -- Have not gotten all the minfap values yet.'.format(convertTime(), self.graceid)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
else:
pass
if (min(idqvalues.values() and jointfapvalues.values()) < idqthresh):
if idqlogkey=='no':
self.client.writeLog(self.graceid, 'AP: Finished running iDQ checks. Candidate event rejected because incomplete joint min-FAP value already less than iDQ threshold. {0} < {1}'.format(min(idqvalues.values() and jointfapvalues.values()), idqthresh), tagname='em_follow')
self.data['idqlogkey']='yes'
message = '{0} -- {1} -- iDQ check result: {2} < {3}'.format(convertTime(), self.graceid, min(idqvalues.values() and jointfapvalues.values()), idqthresh)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
self.data['idq_joint_fapCheckresult'] = False
else:
pass
#self.client.writeLabel(self.graceid, 'DQV') [apply DQV in parseAlert when return False]
return False
elif (len(idqvalues) > (len(idq_pipelines)*len(instruments))):
message = '{0} -- {1} -- Too many minfap values in idqvalues dictionary.'.format(convertTime(), self.graceid)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
else:
pass
else:
message = '{0} -- {1} -- Ready to run iDQ checks.'.format(convertTime(), self.graceid)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
else:
pass
# 'glitch-FAP' is the probabilty that the classifier thinks there was a glitch and there was not a glitch
# 'glitch-FAP' -> 0 means high confidence that there is a glitch
# 'glitch-FAP' -> 1 means low confidence that there is a glitch
# What we want is the minimum of the products of FAPs from different sites computed for each classifier
for idqpipeline in idq_pipelines:
jointfap = 1
for idqdetector in instruments:
detectorstring = '{0}.{1}'.format(idqpipeline, idqdetector)
jointfap = jointfap*idqvalues[detectorstring]
jointfapvalues[idqpipeline] = jointfap
message = '{0} -- {1} -- Got joint_fap = {2} for iDQ pipeline {3}.'.format(convertTime(), self.graceid, jointfap, idqpipeline)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
else:
pass
if min(jointfapvalues.values()) > idqthresh:
if idqlogkey=='no':
self.client.writeLog(self.graceid, 'AP: Finished running iDQ checks. Candidate event passed iDQ checks. {0} > {1}'.format(min(jointfapvalues.values()), idqthresh), tagname = 'em_follow')
self.data['idqlogkey']='yes'
message = '{0} -- {1} -- Passed iDQ check: {2} > {3}.'.format(convertTime(), self.graceid, min(jointfapvalues.values()), idqthresh)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
self.data['idq_joint_fapCheckresult'] = True
else:
pass
return True
else:
if idqlogkey=='no':
self.client.writeLog(self.graceid, 'AP: Finished running iDQ checks. Candidate event rejected due to low iDQ FAP value. {0} < {1}'.format(min(jointfapvalues.values()), idqthresh), tagname = 'em_follow')
self.data['idqlogkey'] = 'yes'
message = '{0} -- {1} -- iDQ check result: {2} < {3}'.format(convertTime(), self.graceid, min(jointfapvalues.values()), idqthresh)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
self.data['idq_joint_fapCheckresult'] = False
else:
pass
#self.client.writeLabel(self.graceid, 'DQV') [apply DQV in parseAlert when return False]
return False
#-----------------------------------------------------------------------
# operator_signoffCheck
#-----------------------------------------------------------------------
def operator_signoffCheck(self):
operator_signoffCheckresult = self.data['operator_signoffCheckresult']
if operator_signoffCheckresult!=None:
return operator_signoffCheckresult
else:
instruments = self.data['instruments']
operatorlogkey = self.data['operatorlogkey']
operatorsignoffs = self.data['operatorsignoffs']
if len(operatorsignoffs) < len(instruments):
if 'NO' in operatorsignoffs.values():
if operatorlogkey=='no':
self.client.writeLog(self.graceid, 'AP: Candidate event failed operator signoff check.', tagname = 'em_follow')
self.data['operatorlogkey'] = 'yes'
# self.client.writeLabel(self.graceid, 'DQV') [apply DQV in parseAlert when return False]
self.data['operator_signoffCheckresult'] = False
return False
else:
message = '{0} -- {1} -- Not all operators have signed off yet.'.format(convertTime(), self.graceid)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
else:
pass
else:
if 'NO' in operatorsignoffs.values():
if operatorlogkey=='no':
self.client.writeLog(self.graceid, 'AP: Candidate event failed operator signoff check.', tagname = 'em_follow')
self.data['operatorlogkey'] = 'yes'
#self.client.writeLabel(self.graceid, 'DQV') [apply DQV in parseAlert when return False]
self.data['operator_signoffCheckresult'] = False
return False
else:
if operatorlogkey=='no':
message = '{0} -- {1} -- Candidate event passed operator signoff check.'.format(convertTime(), self.graceid)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
else:
pass
self.client.writeLog(self.graceid, 'AP: Candidate event passed operator signoff check.', tagname = 'em_follow')
self.data['operatorlogkey'] = 'yes'
self.data['operator_signoffCheckresult'] = True
return True
#-----------------------------------------------------------------------
# advocate_signoffCheck
#-----------------------------------------------------------------------
def advocate_signoffCheck(self):
advocate_signoffCheckresult = self.data['advocate_signoffCheckresult']
if advocate_signoffCheckresult!=None:
return advocate_signoffCheckresult
else:
advocatelogkey = self.data['advocatelogkey']
advocatesignoffs = self.data['advocatesignoffs']
if len(advocatesignoffs)==0:
message = '{0} -- {1} -- Advocates have not signed off yet.'.format(convertTime(), self.graceid)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
else:
pass
elif len(advocatesignoffs) > 0:
if 'NO' in advocatesignoffs:
if advocatelogkey=='no':
self.client.writeLog(self.graceid, 'AP: Candidate event failed advocate signoff check.', tagname = 'em_follow')
self.data['advocatelogkey'] = 'yes'
#self.client.writeLabel(self.graceid, 'DQV') [apply DQV in parseAlert when return False]
self.data['advocate_signoffCheckresult'] = False
return False
else:
if advocatelogkey=='no':
message = '{0} -- {1} -- Candidate event passed advocate signoff check.'.format(convertTime(), self.graceid)
if loggerCheck(self.data, message)==False:
logger.info(message)
else:
pass
self.client.writeLog(self.graceid, 'AP: Candidate event passed advocate signoff check.', tagname = 'em_follow')
self.data['advocatelogkey'] = 'yes'
self.data['advocate_signoffCheckresult'] = True
return True
#-----------------------------------------------------------------------
# virgo_dqCheck
#-----------------------------------------------------------------------
def virgo_dqCheck(self):
virgo_dqCheckresult = self.data['virgo_dqCheckresult']
if virgo_dqCheckresult!=None:
return virgo_dqCheckresult
else:
if 'V1' not in self.data['instruments']: # automatically passes this check since Virgo is not involved
self.client.writeLog(self.graceid, 'AP: Automatically passed Virgo DQ check -- V1 not involved.', tagname = "em_follow")
self.data['virgo_dqlogkey'] = 'yes'
message = '{0} -- {1} -- Automatically passed Virgo DQ check -- V1 not involved.'.format(convertTime(), self.graceid)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
self.data['virgo_dqCheckresult']=True
else:
pass
return True
else: # Virgo is involved so see if we have a value for virgo_dqIsVetoed
virgo_dqIsVetoed = self.data['virgo_dqIsVetoed']
if virgo_dqIsVetoed==None: # this information has not come in yet
return None
elif virgo_dqIsVetoed==True: # Virgo data quality vetoed this trigger
self.client.writeLog(self.graceid, 'AP: Rejecting because of Virgo veto.', tagname = "em_follow")
self.data['virgo_logkey'] = 'yes'
message = '{0} -- {1} -- Rejecting because of Virgo veto.'.format(convertTime(), self.graceid)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
self.data['virgo_dqCheckresult']=False
else:
pass
return False
elif virgo_dqIsVetoed==False: # Virgo data quality okayed this trigger
self.client.writeLog(self.graceid, 'AP: Passed Virgo DQ check.', tagname = "em_follow")
self.data['virgo_logkey'] = 'yes'
message = '{0} -- {1} -- Passed Virgo DQ check.'.format(convertTime(), self.graceid)
if loggerCheck(self.data, message)==False:
self.logger.info(message)
self.data['virgo_dqCheckresult']=True
else:
pass
return True
#-----------------------------------------------------------------------
# Saving event dictionaries
#-----------------------------------------------------------------------
def saveEventDicts(approval_processorMPfiles):
'''
saves eventDicts (the dictonary of event dictionaries) to a pickle file and txt file
'''
### figure out filenames, etc.
### FIXME: THIS SHOULD NOT BE HARD CODED! Instead, use input arguments
homedir = os.path.expanduser('~')
pklfilename = '{0}{1}/EventDicts.p'.format(homedir, approval_processorMPfiles)
txtfilename = '{0}{1}/EventDicts.txt'.format(homedir, approval_processorMPfiles)
### write pickle file
file_obj = open(pklfilename, 'wb')
pickle.dump(eventDictionaries, file_obj) # note: we save eventDictionaries rather than eventDicts because we run into pickling errors with the instances saved in eventDicts
file_obj
### write txt file
file_obj = open(txtfilename, 'w')
for graceid in sorted(eventDictionaries.keys()): ### iterate through graceids
file_obj.write('{0}\n'.format(graceid))
event_dict = eventDictionaries[graceid]
for key in sorted(event_dict.keys()): ### iterate through keys for this graceid
if key!='loggermessages':
file_obj.write(' {0}: {1}\n'.format(key, event_dict[key]))
file_obj.write('\n')
file_obj.close()
#-----------------------------------------------------------------------
# Loading event dictionaries
#-----------------------------------------------------------------------
def loadEventDicts(approval_processorMPfiles):
'''
loads eventDictionaries (the dictionary of event dictionaries) to do things like resend VOEvents for an event candidate
'''
homedir = os.path.expanduser('~')
pklname = '{0}{1}/EventDicts.p'.format(homedir, approval_processorMPfiles)
if os.path.exists(pklname): ### check to see if the file actually exists
file_obj = open(pklname, 'rb')
global eventDictionaries
eventDictionaries = pickle.load(file_obj) ### if something fails here, we want to know about it!
file_obj.close()
#-----------------------------------------------------------------------
# Load logger
#-----------------------------------------------------------------------
def loadLogger(config):
'''
sets up logger.
assumes the config has already been set up! if it hasn't been, please run loadConfig before running loadLogger
'''
global logger
logger = logging.getLogger('approval_processorMP')
approval_processorMPfiles = config.get('general', 'approval_processorMPfiles')
logfile = config.get('general', 'approval_processorMP_logfile')
homedir = os.path.expanduser('~')
logging_filehandler = logging.FileHandler('{0}{1}{2}'.format(homedir, approval_processorMPfiles, logfile))
logging_filehandler.setLevel(logging.INFO)
logger.setLevel(logging.INFO)
logger.addHandler(logging_filehandler)
return logger
#-----------------------------------------------------------------------
# Load config
#-----------------------------------------------------------------------
def loadConfig():
'''
loads the childConfig-approval_processorMP.ini
it will prompt the user if they want to use the one on the gracedb.processor machine, or if they want to specify a specific one
'''
config = ConfigParser.SafeConfigParser()
default = raw_input('do you want to use the default childConfig-approval_processorMP.ini in the grinch installation? options are yes or no\n')
if default=='yes':
#config.read('/home/gracedb.processor/public_html/monitor/approval_processorMP/files/childConfig-approval_processorMP.ini')
config.read('/home/gracedb.processor/opt/etc/childConfig-approval_processorMP.ini')
elif default=='no':
config.read('{0}/childConfig-approval_processorMP.ini'.format(raw_input('childConfig-approval_processorMP.ini file directory? *do not include forward slash at end*\n')))
else:
print 'sorry. options were yes or no. try again'
return config
#-----------------------------------------------------------------------
# Make config_dict
#-----------------------------------------------------------------------
def makeConfigDict(config):
client = config.get('general', 'client')
force_all_internal = config.get('general', 'force_all_internal')
preliminary_internal = config.get('general', 'preliminary_internal')
hardware_inj = config.get('labelCheck', 'hardware_inj')
default_farthresh = config.getfloat('farCheck', 'default_farthresh')
humanscimons = config.get('operator_signoffCheck', 'humanscimons')
### extract options about advocates
advocates = config.get('advocate_signoffCheck', 'advocates')
### extract options about idq
ignore_idq = config.get('idq_joint_fapCheck', 'ignore_idq')
default_idqthresh = config.getfloat('idq_joint_fapCheck', 'default_idqthresh')
### set up configdict (passed to local data structure: eventDicts)
configdict = {
'force_all_internal' : force_all_internal,
'preliminary_internal': preliminary_internal,
'hardware_inj' : hardware_inj,
'default_farthresh' : default_farthresh,
'humanscimons' : humanscimons,
'advocates' : advocates,
'ignore_idq' : ignore_idq,
'default_idqthresh' : default_idqthresh,
'client' : client
}
return configdict
#-----------------------------------------------------------------------
# Utilities
#-----------------------------------------------------------------------
def convertTime(ts=None):
if ts is None:
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
return st
def loggerCheck(event_dict, message):
loggermessages = event_dict['loggermessages']
graceid = event_dict['graceid']
message = re.findall(r'-- {0} -- (.*)'.format(graceid), message)[0]
if message in loggermessages:
return True
else:
event_dict['loggermessages'].append(message)
return False
def is_external_trigger(alert):
'''a function that looks to see if lvalert regards an external GRB trigger or not'''
graceid = alert['uid']
if re.match('E', graceid):
return True
if alert.has_key('object'): # noticed that label alert_types do not have 'object' key
group = alert['object']['group'] if alert['object'].has_key('group') else '' # lvalerts produced for uploaded comments dont have group,
# pipeline, or search info
pipeline = alert['object']['pipeline'] if alert['object'].has_key('pipeline') else ''
search = alert['object']['search'] if alert['object'].has_key('search') else ''
if group=='External':
return True
elif pipeline=='Swift' or pipeline=='Fermi' or pipeline=='SNEWS':
return True
elif search=='GRB':
return True
else:
return False
def checkLabels(labels, config):
hardware_inj = config.get('labelCheck', 'hardware_inj')
if hardware_inj == 'yes':
badlabels = ['DQV', 'EM_Throttled', 'EM_Superseded', 'H1NO', 'L1NO', 'V1NO', 'ADVNO']
else:
badlabels = ['DQV', 'EM_Throttled', 'EM_Superseded', 'INJ', 'H1NO', 'L1NO', 'V1NO', 'ADVNO']
intersectionlist = list(set(badlabels).intersection(labels))
return len(intersectionlist)
def record_coinc_info(event_dict, comment, alert, logger):
graceid = event_dict['graceid']
# is this a log comment from PyGRB or X-pipeline for an external trigger?
if is_external_trigger(alert)==True:
coinc_info = re.findall('(.*): Significant event in on-source \(FAP = (.*) for the most significant event\)', comment)
coinc_pipeline = coinc_info[0][0]
coinc_fap = float(coinc_info[0][1])
message = '{0} -- {1} -- {2} coincidence found with FAP {3}.'.format(convertTime(), graceid, coinc_pipeline, coinc_fap)
if loggerCheck(event_dict, message)==False:
logger.info(message)
else:
pass
return coinc_pipeline, coinc_fap
# if this a log comment from RAVEN
else:
coinc_info = re.findall('Temporal coincidence with external trigger (.*)>(.*)<(.*) gives a coincident FAR = (.*) Hz', comment) # this parsing looks messy but only because the raw string we need to parse contains html code
exttrig = coinc_info[0][1]
event_dict['external_trigger'] = exttrig
coinc_far = coinc_info[0][3]
message = '{0} -- {1} -- RAVEN coincidence found with FAR {2}. External trigger {3}.'.format(convertTime(), graceid, coinc_far, exttrig)
if loggerCheck(event_dict, message)==False:
logger.info(message)
else:
pass
return exttrig, coinc_far
def record_em_bright(event_dict, comment, logger):
graceid = event_dict['graceid']
em_bright_info = {}
ProbHasNS, RemnantThresh, ProbHasRemnant = re.findall('The probability of second object being a neutron star = (.*)% \n The probability of remnant mass outside the black hole in excess of (.*) M_sun = (.*)% \n', comment)[0]
em_bright_info['ProbHasNS'] = float(ProbHasNS)/100
em_bright_info['ProbHasRemnant'] = float(ProbHasRemnant)/100
em_bright_info['RemnantMassThreshInM_Sun'] = float(RemnantThresh)
event_dict['em_bright_info'] = em_bright_info
message = '{0} -- {1} -- EM Bright probabilities recorded.'.format(convertTime(), graceid)
if loggerCheck(event_dict, message)==False:
logger.info(message)
else:
pass
def record_label(event_dict, label):
labels = event_dict['labels']
graceid = event_dict['graceid']
labels.append(label)
message = '{0} -- {1} -- Got {2} label.'.format(convertTime(), graceid, label)
if loggerCheck(event_dict, message)==False:
logger.info(message)
else:
pass
def current_lvem_skymap(event_dict):
lvemskymaps = sorted(event_dict['lvemskymaps'].keys())
if len(lvemskymaps)==0:
return None
else:
skymap = sorted(lvemskymaps)[-1]
skymap = re.findall(r'-(\S+)', skymap)[0]
return skymap
def record_skymap(event_dict, skymap, submitter, logger):
# this only records skymaps with the lvem tag
graceid = event_dict['graceid']
lvemskymaps = sorted(event_dict['lvemskymaps'].keys())
currentnumber = len(lvemskymaps) + 1
skymapkey = '{0}'.format(currentnumber) + '-'+ skymap
# check if we already have the skymap
count = 0
for map in lvemskymaps:
if skymap in map:
count += 1
else:
count +=0
if count==0:
event_dict['lvemskymaps'][skymapkey] = submitter
message = '{0} -- {1} -- Got the lvem skymap {2}.'.format(convertTime(), graceid, skymap)
if loggerCheck(event_dict, message)==False:
logger.info(message)
else:
pass
def record_idqvalues(event_dict, comment, logger):
graceid = event_dict['graceid']
idqinfo = re.findall('minimum glitch-FAP for (.*) at (.*) with', comment)
idqpipeline = idqinfo[0][0]
idqdetector = idqinfo[0][1]
minfap = re.findall('is (.*)', comment)
minfap = float(minfap[0])
detectorstring = '{0}.{1}'.format(idqpipeline, idqdetector)
event_dict['idqvalues'][detectorstring] = minfap
message = '{0} -- {1} -- Got the minfap for {2} using {3} is {4}.'.format(convertTime(), graceid, idqdetector, idqpipeline, minfap)
if loggerCheck(event_dict, message)==False:
logger.info(message)
else:
pass
def record_signoff(event_dict, signoff_object):
instrument = signoff_object['instrument']
signofftype = signoff_object['signoff_type']
status = signoff_object['status']
if signofftype=='OP':
operatorsignoffs = event_dict['operatorsignoffs']
operatorsignoffs[instrument] = status
if signofftype=='ADV':
advocatesignoffs = event_dict['advocatesignoffs']
advocatesignoffs.append(status)
def record_virgo_dqIsVetoed(event_dict, comment, logger):
graceid = event_dict['graceid']
response = re.findall('this event (.*) vetoed', comment)[0]
if response=='IS':
event_dict['virgo_dqIsVetoed']=True
elif response=='IS NOT':
event_dict['virgo_dqIsVetoed']=False
message = '{0} -- {1} -- Virgo {2} vetoing this trigger.'.format(convertTime(), graceid, response)
if loggerCheck(event_dict, message)==False:
logger.info(message)
else:
pass
def record_virgoInjections(event_dict, comment, logger):
graceid = event_dict['graceid']
response = re.findall('V1 hardware injection: (.*) injections', comment)[0]
if response=="DID NOT FIND":
event_dict['virgoInjections']=0
elif response=="DID FIND" or response=="FOUND": # XXX: need response from Sarah A. on what the log comment actually looks like
event_dict['virgoInjections']=1
message = '{0} -- {1} -- Virgo {2} injections.'.format(convertTime(), graceid, response)
if loggerCheck(event_dict, message)==False:
logger.info(message)
else:
pass
#-----------------------------------------------------------------------
# process_alert
#-----------------------------------------------------------------------
def process_alert(event_dict, voevent_type, client, config, logger, set_internal='do nothing'):
graceid = event_dict['graceid']
pipeline = event_dict['pipeline']
voeventerrors = event_dict['voeventerrors']
voevents = event_dict['voevents']
# setting default internal value settings for alerts
force_all_internal = config.get('general', 'force_all_internal')
if force_all_internal=='yes':
internal = 1
else:
internal = 0
open_default_farthresh = config.getfloat('farCheck', 'open_default_farthresh')
far = event_dict['far']
if far < open_default_farthresh: # the far is below the open alert default far threshold so we send an open alert
open_alert = 1
else:
open_alert = 0
if voevent_type=='preliminary':
if force_all_internal=='yes':
internal = 1
else:
if pipeline in preliminary_internal:
internal = 1
else:
internal = 0
vetted = 0 # default value for preliminary alerts