ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/yiiyama/Toolset/scripts/fjr2json.py
Revision: 1.6
Committed: Fri Feb 8 20:21:58 2013 UTC (12 years, 2 months ago) by yiiyama
Content type: text/x-python
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +30 -25 lines
Log Message:
simplified jsondiff

File Contents

# Content
1 #!/usr/bin/env python
2
3 import sys
4 from optparse import OptionParser
5 from xml.dom.minidom import parseString
6
7 parser = OptionParser()
8 parser.add_option("-f", "--file", dest="filename", help="list of jfr files", metavar="FILE")
9
10 (options, args) = parser.parse_args()
11
12 filenames = []
13
14 if options.filename:
15 filelist = file(options.filename, "r")
16 for f in filelist:
17 filenames.append(f.strip())
18
19 filelist.close()
20 else:
21 filenames = args
22
23 # hack to make multiple file processing possible
24 lines = '<Files>'
25
26 for f in filenames:
27 try:
28 lines += file(f, "r").read()
29 except IOError:
30 sys.stderr.write("Cannot open " + f + "\n")
31
32 lines += '</Files>'
33
34 dom = parseString(lines)
35
36 runBlocks = dom.getElementsByTagName('Run')
37
38 lumilist = dict()
39
40 for runTag in runBlocks:
41 run = 0
42 for i in range(runTag.attributes.length):
43 attr = runTag.attributes.item(i)
44 if attr.name == "ID":
45 run = attr.value
46
47 if run == 0:
48 raise RuntimeError("No runnumber")
49
50 if run not in lumilist:
51 lumilist[run] = list()
52
53 children = runTag.childNodes
54
55 for child in children:
56 if child.localName == "LumiSection":
57 for i in range(child.attributes.length):
58 attr = child.attributes.item(i)
59 if attr.name == "ID":
60 lumilist[run].append(int(attr.value))
61
62 jsonTxt = "{"
63
64 runs = lumilist.keys()
65 runs.sort()
66
67 for run in runs:
68 lumis = lumilist[run]
69 if len(lumis) == 0:
70 continue
71
72 lumis.sort()
73
74 jsonTxt += '"' + str(run) + '": [[' + str(lumis[0]) + ', '
75
76 l = lumis[0] - 1
77 for lumi in lumis:
78 if lumi != l + 1:
79 jsonTxt += str(l) + '], [' + str(lumi) + ', '
80
81 l = lumi
82
83 jsonTxt += str(lumis[len(lumis) - 1]) + ']], '
84
85 jsonTxt = jsonTxt.rstrip(', ')
86 jsonTxt += "}"
87
88 print jsonTxt