ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/yiiyama/Toolset/scripts/fjr2json.py
Revision: 1.4
Committed: Mon Oct 22 12:27:19 2012 UTC (12 years, 6 months ago) by yiiyama
Content type: text/x-python
Branch: MAIN
Changes since 1.3: +2 -0 lines
Log Message:
making executable

File Contents

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