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

# User Rev Content
1 yiiyama 1.4 #!/usr/bin/env python
2    
3 yiiyama 1.1 import sys
4 yiiyama 1.2 from optparse import OptionParser
5 yiiyama 1.6 from xml.dom.minidom import parseString
6 yiiyama 1.1
7 yiiyama 1.2 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 yiiyama 1.6 # hack to make multiple file processing possible
24     lines = '<Files>'
25    
26 yiiyama 1.2 for f in filenames:
27     try:
28 yiiyama 1.6 lines += file(f, "r").read()
29 yiiyama 1.2 except IOError:
30     sys.stderr.write("Cannot open " + f + "\n")
31 yiiyama 1.1
32 yiiyama 1.6 lines += '</Files>'
33    
34     dom = parseString(lines)
35 yiiyama 1.1
36 yiiyama 1.6 runBlocks = dom.getElementsByTagName('Run')
37 yiiyama 1.1
38     lumilist = dict()
39    
40 yiiyama 1.6 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 yiiyama 1.1
62     jsonTxt = "{"
63    
64 yiiyama 1.3 runs = lumilist.keys()
65     runs.sort()
66    
67     for run in runs:
68 yiiyama 1.1 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