Millepede-II V04-16-03
compareResults.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
3
37import math, sys
38
39
40
48def combineFiles(name1, name2, name3):
49
50 # Add results from file
51 def addFile(name):
52 tf = open(name)
53 for line in iter(tf):
54 if line[0] == '*' or line[0] == '!':
55 continue
56 fields = line.split()
57 if len(fields) < 3:
58 continue
59 if fields[0].isalpha():
60 continue
61 label = int(fields[0])
62 value = float(fields[1])
63 if label not in results:
64 results[label] = []
65 results[label].append(value)
66 tf.close()
67
68 results = {}
69 print(' comparing MP2 results files')
70 # read first file
71 print(' input file1 ', name1)
72 addFile(name1)
73 # read second file
74 print(' input file2 ', name2)
75 addFile(name2)
76
77 # write output file, correlate
78 sn = sx = sy = sxx = sxy = syy = 0.
79 fout = open(name3, 'w')
80 fout.write("label/I:value_1/F:value_2/F\n") # ROOT tree header
81 for l in sorted(results.keys()):
82 r = results[l]
83 if len(r) == 2:
84 # matched label
85 if r[0] != 0. or r[1] != 0.:
86 fout.write("%10d %12.5g %12.5g \n" % (l, r[0], r[1]))
87 sn += 1.; sx += r[0]; sy += r[1]; sxx += r[0] * r[0]; sxy += r[0] * r[1]; syy += r[1] * r[1]
88 elif len(r) == 1:
89 # unmatched label
90 if r[0] != 0.:
91 fout.write("%10d %12.5g %12.5g \n" % (-l, r[0], r[0]))
92 print(' unmatched ', l)
93 fout.close()
94
95 print(' output file ', name3)
96 print(' total parameters ', len(results))
97 matched = int(sn)
98 if (matched <= 0):
99 return
100
101 #comparision
102 print(' matched parameters ', matched)
103 sx /= sn; sy /= sn
104 print(' mean1, mean2 ', sx, sy)
105 sxx /= sn; syy /= sn
106 print(' rms1, rms2 ', math.sqrt(sxx - sx * sx), math.sqrt(syy - sy * sy))
107 sxy /= sn
108 print(' correlation ', (sxy - sx * sy) / (math.sqrt(sxx - sx * sx) * math.sqrt(syy - sy * sy)))
109
110
111if __name__ == '__main__':
112 # input files
113 fileNameIn1 = "millepede.res1"
114 fileNameIn2 = "millepede.res2"
115 # output file
116 fileNameOut = "mp2compare.txt"
117 # ## use command line arguments ?
118 narg = len(sys.argv)
119 if narg > 1:
120 if narg < 3:
121 print(" usage: compareResults.py <input file name1> <input file name2> [<output file name>]")
122 sys.exit(2)
123 else:
124 fileNameIn1 = sys.argv[1]
125 fileNameIn2 = sys.argv[2]
126 if narg > 3:
127 fileNameOut = sys.argv[3]
128
129 # combine input files 1 and 2 into output file
130 combineFiles(fileNameIn1, fileNameIn2, fileNameOut)
def combineFiles(name1, name2, name3)
Combine two MP-II result files.