| The Gaudi Framework v28r2-patches (bc4ad17a)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

hivetimeline.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 """Plot timeline from TimelineSvc"""
3
4 __author__ = "Frank Winklmeier"
5
6 import sys
7 import re
8 import argparse
9 import operator
10 from collections import defaultdict
11
12 algcolors = []
13 evtcolors = []
14
15 # Attributes will be added in read()
16 class Data:
17 pass
18
19 def read(f,regex='.*',skipevents=0):
20 data = []
21 regex = re.compile(regex)
22 for l in open(f,'r'):
23 if l.startswith('#'): # e.g. #start end algorithm thread slot event
24 names = l.lstrip('#').split()
25 continue
26 d = Data()
27 for i,f in enumerate(l.split()):
28 setattr(d, names[i], int(f) if f.isdigit() else f)
29 if d.event >= skipevents:
30 data.append(d)
31 return data
32
33 def findEvents(data):
34 """Find event start/stop times"""
35
36 t = defaultdict(lambda : [sys.maxint, 0, -1]) # start,stop,slot
37 nbslots = 0
38 for d in data:
39 if d.start<t[d.event][0]:
40 t[d.event][0] = d.start
41 t[d.event][2] = d.slot
42 if d.end>t[d.event][1]:
43 t[d.event][1] = d.end
44 if d.slot > nbslots:
45 nbslots = d.slot
46
47 return t, nbslots
48
49 def setPalette(nevts, nevtcolors):
50 global algcolors, evtcolors
51
52 from ROOT import TColor
53 algcolors = range(2,10)+[20,28,29,30,33,38,40]+range(41,50)
54 evtcolors = [TColor.GetColor(0,255-g,g) for g in range(20,255,(255-20)/nevtcolors)]
55
56 def plot(data, showThreads=True, batch=False, nevtcolors=10, width=1200, height=500):
57 import ROOT
58
59 tmin = min(f.start for f in data)
60 tmax = max(f.end for f in data)
61 slots = 1+max(f.slot for f in data)
62 threads = sorted(list(set(f.thread for f in data)))
63 threadid = dict((k,v) for v,k in enumerate(threads)) # map thread : id
64 ymax = len(threads) if showThreads else slots
65
66 c = ROOT.TCanvas('timeline','Timeline',width,height)
67 c.SetLeftMargin(0.05)
68 c.SetRightMargin(0.2)
69 c.SetTopMargin(0.1)
70 c.SetBottomMargin(0.1)
71 c.coord = ROOT.TH2I('coord',';Time (ns)',100,0,tmax-tmin,ymax,0,ymax)
72 c.coord.GetYaxis().SetTitle(('Thread' if showThreads else 'Slot'))
73 c.coord.GetYaxis().SetTitleOffset(0.5)
74 c.coord.GetYaxis().CenterTitle()
75 c.coord.SetStats(False)
76 c.coord.GetYaxis().SetNdivisions(ymax)
77 c.coord.GetXaxis().CenterTitle()
78
79 c.Draw()
80 c.coord.Draw()
81
82 c.lines = []
83 colors = {}
84 setPalette(ymax, nevtcolors)
85 mycolors = algcolors
86 for d in data:
87 y = (threadid[d.thread] if showThreads else d.slot)
88 alg = d.algorithm
89 if alg not in colors and len(mycolors)>0:
90 colors[alg] = mycolors.pop(0)
91 if len(mycolors)==0:
92 print "Too many algorithm to show"
93
94 if alg in colors:
95 t0 = d.start - tmin
96 t1 = d.end - tmin
97
98 # Alg
99 l = ROOT.TBox(t0, y+.1, t1, y+.8)
100 l.SetFillColor(colors[alg])
101
102 # Event
103 l2 = ROOT.TBox(t0, y+.8, t1, y+.9)
104 l2.SetFillColor(evtcolors[d.event % nevtcolors])
105 c.lines += [l,l2]
106
107 l2.Draw()
108 l.Draw()
109
110 # Global event timeline
111 tevt, nbslots = findEvents(data)
112 bheight = 0.1
113 for k,v in tevt.iteritems():
114 y = ymax+bheight*v[2]
115 l = ROOT.TBox(v[0]-tmin,y+0.2*bheight,v[1]-tmin,y+bheight)
116 l.SetFillColor(evtcolors[k % nevtcolors])
117 c.lines += [l]
118 l.Draw()
119
120 # Alg legend
121 c.leg = ROOT.TLegend(0.8,0.4,0.98,0.9)
122 for alg,cl in sorted(colors.iteritems(),key=operator.itemgetter(1)):
123 e = c.leg.AddEntry('',alg,'F')
124 e.SetLineColor(cl)
125 e.SetFillColor(cl)
126 e.SetFillStyle(1001)
127
128 # Event legend
129 bwidth = 0.18 / nevtcolors
130 for cl in range(nevtcolors):
131 l = ROOT.TLine()
132 c.lines.append(l)
133 l.SetLineWidth(10)
134 l.SetLineColor(evtcolors[cl])
135 l.DrawLineNDC(0.807+bwidth*cl,0.37,0.807+bwidth*(cl+1),0.37)
136
137 c.t1 = ROOT.TText(0.807,0.314,'Events')
138 c.t1.SetNDC()
139 c.t1.SetTextFont(42)
140 c.t1.SetTextSize(0.04)
141 c.t1.Draw()
142
143 c.t2 = ROOT.TText(0.02,0.92,'Event')
144 c.t2.SetNDC()
145 c.t2.SetTextFont(42)
146 c.t2.SetTextSize(0.03)
147 c.t2.SetTextAngle(90)
148 c.t2.Draw()
149 c.t3 = ROOT.TText(0.03,0.922,'Slots')
150 c.t3.SetNDC()
151 c.t3.SetTextFont(42)
152 c.t3.SetTextSize(0.03)
153 c.t3.SetTextAngle(90)
154 c.t3.Draw()
155
156 c.leg.Draw()
157 c.Update()
158 if not batch: raw_input()
159 return c
160
161
162 def main():
163 parser = argparse.ArgumentParser(description=__doc__)
164
165 parser.add_argument('timeline', nargs=1,
166 help='timeline file')
167
168 parser.add_argument('-s', '--select', default='.*',
169 help='Regular expression to filter algorithms')
170
171 parser.add_argument('-b', '--batch', action='store_true', default=False,
172 help='Do not wait for user input')
173
174 parser.add_argument('--slots', action='store_true', default=False,
175 help='Show slots instead of threads (leads to overlaps!)')
176
177 parser.add_argument('-p', '--print', dest='outfile' , nargs='?',
178 const='timeline.png',
179 help='Save to FILE [%(const)s]')
180
181 parser.add_argument('-n', '--nevtcolors', default=10, type=int,
182 help='Number of colors used for events (10 is default)')
183
184 parser.add_argument('-e', '--skipevents', default=0, type=int,
185 help='Number of events to skip at the start')
186
187 parser.add_argument('-x', '--width', default=1200, type=int,
188 help='width of the output picture')
189
190 parser.add_argument('-y', '--height', default=500, type=int,
191 help='height of the output picture')
192
193 args = parser.parse_args()
194
195 data = read(args.timeline[0], args.select, args.skipevents)
196 c = plot(data, not args.slots, args.batch, args.nevtcolors, args.width, args.height)
197 if args.outfile:
198 c.SaveAs(args.outfile)
199
200 return 0
201
202 if __name__ == '__main__':
203 sys.exit(main())
hivetimeline.Data
Definition: hivetimeline.py:16
Gaudi::Functional::details::zip::range
decltype(auto) range(Args &&...args)
Zips multiple containers together to form a single range.
Definition: FunctionalDetails.h:57
hivetimeline.read
def read(f, regex='.*', skipevents=0)
Definition: hivetimeline.py:19
hivetimeline.plot
def plot(data, showThreads=True, batch=False, nevtcolors=10, width=1200, height=500)
Definition: hivetimeline.py:56
hivetimeline.main
def main()
Definition: hivetimeline.py:162
hivetimeline.setPalette
def setPalette(nevts, nevtcolors)
Definition: hivetimeline.py:49
hivetimeline.findEvents
def findEvents(data)
Definition: hivetimeline.py:33