ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/yiiyama/Toolset/scripts/fjr2json.py
Revision: 1.3
Committed: Wed Oct 17 14:52:45 2012 UTC (12 years, 6 months ago) by yiiyama
Content type: text/x-python
Branch: MAIN
Changes since 1.2: +4 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 import re
2 import sys
3 from optparse import OptionParser
4
5 parser = OptionParser()
6 parser.add_option("-f", "--file", dest="filename", help="list of jfr files", metavar="FILE")
7
8 (options, args) = parser.parse_args()
9
10 filenames = []
11 lines = []
12
13 if options.filename:
14 filelist = file(options.filename, "r")
15 for f in filelist:
16 filenames.append(f.strip())
17
18 filelist.close()
19 else:
20 filenames = args
21
22 for f in filenames:
23 try:
24 lines += file(f, "r").readlines()
25 except IOError:
26 sys.stderr.write("Cannot open " + f + "\n")
27
28 # TODO could a file contain more than one run?
29 runPat = re.compile(r'\s*\{([0-9]+):[ ]\[([0-9, ]+)\]\}')
30
31 inRunBlock = False
32
33 lumilist = dict()
34
35 for line in lines:
36 if '<Runs>' in line:
37 inRunBlock = True
38 continue
39
40 if '</Runs>' in line:
41 inRunBlock = False
42 continue
43
44 if inRunBlock:
45 matches = runPat.match(line)
46 if not matches:
47 continue
48
49 run = int(matches.group(1))
50 if run not in lumilist:
51 lumilist[run] = list()
52
53 lumis = re.findall('[0-9]+', matches.group(2))
54 for lumi in lumis:
55 lumilist[run].append(int(lumi))
56
57 jsonTxt = "{"
58
59 runs = lumilist.keys()
60 runs.sort()
61
62 for run in runs:
63 lumis = lumilist[run]
64 if len(lumis) == 0:
65 continue
66
67 lumis.sort()
68
69 jsonTxt += '"' + str(run) + '": [[' + str(lumis[0]) + ', '
70
71 l = lumis[0] - 1
72 for lumi in lumis:
73 if lumi != l + 1:
74 jsonTxt += str(l) + '], [' + str(lumi) + ', '
75
76 l = lumi
77
78 jsonTxt += str(lumis[len(lumis) - 1]) + ']], '
79
80 jsonTxt = jsonTxt.rstrip(', ')
81 jsonTxt += "}"
82
83 print jsonTxt