Ticket #7490: CNCS_67147_event_compare.py

File CNCS_67147_event_compare.py, 2.8 KB (added by Peter Peterson, 7 years ago)
Line 
1def compareEvents(detId, left, right):
2    lftTofs = left.getTofs()
3    lftPuls = left.getPulseTimes()
4    if len(lftTofs) != len(lftPuls):
5        raise RuntimeError("Something really wrong with %d left: %d != %d" % (detId, len(lftTofs), len(lftPuls)))
6
7    rgtTofs = right.getTofs()
8    rgtPuls = right.getPulseTimes()
9    if len(rgtTofs) != len(rgtPuls):
10        raise RuntimeError("Something really wrong with %d right: %d != %d" % (detId, len(rgtTofs), len(rgtPuls)))
11
12    if len(lftTofs) != len(rgtTofs):
13        print "detId %d has different length events %d != %d" % (len(lftTofs), len(rgtTofs))
14        return
15
16    results = []
17
18    total_num_diff = 0
19    for i in range(len(lftTofs)):
20        is_diff = False
21        if abs(lftTofs[i]-rgtTofs[i]) >= .1: # tolerance of 100ns
22            is_diff = True
23            results.append("tof[%3d] %f != %f (diff=%f)" % (i, lftTofs[i], rgtTofs[i], \
24                                                               rgtTofs[i]-lftTofs[i]))
25        if lftPuls[i] != rgtPuls[i]:
26            diff = rgtPuls[i]-lftPuls[i]
27            if abs(diff.total_nanoseconds()) > 1:
28                is_diff = True
29                diff = diff.total_nanoseconds()/1000000000. # convert to seconds
30                minutes = int(diff/60.)
31                seconds = abs(diff - minutes*60.)
32                diff = [minutes, seconds]
33                results.append("pulse[%3d] %s != %s (diff=%dm%ds)" % (i, str(lftPuls[i]), str(rgtPuls[i]), \
34                                                                       diff[0], diff[1]))                 
35        if is_diff:
36            total_num_diff += 1
37
38    if total_num_diff>0:
39        print "****", detId, "numEvents:", len(lftTofs), "totalDiff:", total_num_diff
40        for result in results:
41            print result
42
43    return total_num_diff
44
45
46files = ("CNCS_7860_event.nxs","CNCS_7860_neutron_event.dat")
47files = ("CNCS_67147_event.nxs","/SNS/CNCS/IPTS-5383/0/67147/preNeXus/CNCS_67147_neutron_event.dat")
48
49nexus = LoadEventNexus(files[0], BankName="bank3")
50prenexus = LoadEventPreNexus(files[1], SpectrumList="2048-3071")
51#SortEvents(nexus, "Pulse Time + TOF")
52#SortEvents(prenexus, "Pulse Time + TOF")
53numNxsSpec = nexus.getNumberHistograms()
54numPreSpec = prenexus.getNumberHistograms()
55total_diff = 0
56for i in range(numPreSpec):
57    preEvents = prenexus.getEventList(i)
58    detId = prenexus.getSpectrum(i).getDetectorIDs()[0]
59    nxsEvents = None
60    for j in range(i, numNxsSpec):
61        if nexus.getSpectrum(j).hasDetectorID(detId):
62            nxsEvents = nexus.getEventList(j)
63            break
64
65    total_diff += compareEvents(detId, preEvents, nxsEvents)
66if total_diff == nexus.getNumberEvents():
67    print "all events different out of %d" % nexus.getNumberEvents()
68else:
69    print "%d events different out of %d" % (total_diff, nexus.getNumberEvents())