ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/yiiyama/Toolset/scripts/fjr2json.py
Revision: 1.1
Committed: Wed Oct 17 12:22:59 2012 UTC (12 years, 6 months ago) by yiiyama
Content type: text/x-python
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 yiiyama 1.1 import re
2     import sys
3    
4     lines = []
5     for i in range(1, len(sys.argv)):
6     lines += file(sys.argv[i], "r").readlines()
7    
8     # TODO could a file contain more than one run?
9     runPat = re.compile(r'\s*\{([0-9]+):[ ]\[([0-9, ]+)\]\}')
10    
11     inRunBlock = False
12    
13     lumilist = dict()
14    
15     for line in lines:
16     if '<Runs>' in line:
17     inRunBlock = True
18     continue
19    
20     if '</Runs>' in line:
21     inRunBlock = False
22     continue
23    
24     if inRunBlock:
25     matches = runPat.match(line)
26     if not matches:
27     continue
28    
29     run = int(matches.group(1))
30     if run not in lumilist:
31     lumilist[run] = list()
32    
33     lumis = re.findall('[0-9]+', matches.group(2))
34     for lumi in lumis:
35     lumilist[run].append(int(lumi))
36    
37     jsonTxt = "{"
38    
39     for run in lumilist:
40     lumis = lumilist[run]
41     if len(lumis) == 0:
42     continue
43    
44     lumis.sort()
45    
46     jsonTxt += '"' + str(run) + '": [[' + str(lumis[0]) + ', '
47    
48     l = lumis[0] - 1
49     for lumi in lumis:
50     if lumi != l + 1:
51     jsonTxt += str(l) + '], [' + str(lumi) + ', '
52    
53     l = lumi
54    
55     jsonTxt += str(lumis[len(lumis) - 1]) + ']], '
56    
57     jsonTxt = jsonTxt.rstrip(', ')
58     jsonTxt += "}"
59    
60     print jsonTxt