12 """Plot timeline from TimelineSvc"""
13 from __future__
import print_function
15 __author__ =
"Frank Winklmeier"
21 from collections
import defaultdict
33 def read(f, regex='.*', skipevents=0):
35 regex = re.compile(regex)
36 for l
in open(f,
'r'):
38 names = l.lstrip(
'#').split()
41 for i, f
in enumerate(l.split()):
42 setattr(d, names[i], int(f)
if f.isdigit()
else f)
43 if d.event >= skipevents:
49 """Find event start/stop times"""
51 t = defaultdict(
lambda: [sys.maxint, 0, -1])
54 if d.start < t[d.event][0]:
55 t[d.event][0] = d.start
56 t[d.event][2] = d.slot
57 if d.end > t[d.event][1]:
66 global algcolors, evtcolors
68 from ROOT
import TColor
69 algcolors =
range(2, 10) + [20, 28, 29, 30, 33, 38, 40] +
range(41, 50)
71 TColor.GetColor(0, 255 - g, g)
72 for g
in range(20, 255, (255 - 20) / nevtcolors)
84 tmin =
min(f.start
for f
in data)
85 tmax =
max(f.end
for f
in data)
86 slots = 1 +
max(f.slot
for f
in data)
87 threads = sorted(list(set(f.thread
for f
in data)))
88 threadid = dict((k, v)
for v, k
in enumerate(threads))
89 ymax = len(threads)
if showThreads
else slots
91 c = ROOT.TCanvas(
'timeline',
'Timeline', width, height)
95 c.SetBottomMargin(0.1)
96 c.coord = ROOT.TH2I(
'coord',
';Time (ns)', 100, 0, tmax - tmin, ymax, 0,
98 c.coord.GetYaxis().SetTitle((
'Thread' if showThreads
else 'Slot'))
99 c.coord.GetYaxis().SetTitleOffset(0.5)
100 c.coord.GetYaxis().CenterTitle()
101 c.coord.SetStats(
False)
102 c.coord.GetYaxis().SetNdivisions(ymax)
103 c.coord.GetXaxis().CenterTitle()
113 y = (threadid[d.thread]
if showThreads
else d.slot)
115 if alg
not in colors
and len(mycolors) > 0:
116 colors[alg] = mycolors.pop(0)
117 if len(mycolors) == 0:
118 print(
"Too many algorithm to show")
125 l = ROOT.TBox(t0, y + .1, t1, y + .8)
126 l.SetFillColor(colors[alg])
129 l2 = ROOT.TBox(t0, y + .8, t1, y + .9)
130 l2.SetFillColor(evtcolors[d.event % nevtcolors])
139 for k, v
in tevt.iteritems():
140 y = ymax + bheight * v[2]
141 l = ROOT.TBox(v[0] - tmin, y + 0.2 * bheight, v[1] - tmin, y + bheight)
142 l.SetFillColor(evtcolors[k % nevtcolors])
147 c.leg = ROOT.TLegend(0.8, 0.4, 0.98, 0.9)
148 for alg, cl
in sorted(colors.iteritems(), key=operator.itemgetter(1)):
149 e = c.leg.AddEntry(
'', alg,
'F')
155 bwidth = 0.18 / nevtcolors
156 for cl
in range(nevtcolors):
160 l.SetLineColor(evtcolors[cl])
161 l.DrawLineNDC(0.807 + bwidth * cl, 0.37, 0.807 + bwidth * (cl + 1),
164 c.t1 = ROOT.TText(0.807, 0.314,
'Events')
167 c.t1.SetTextSize(0.04)
170 c.t2 = ROOT.TText(0.02, 0.92,
'Event')
173 c.t2.SetTextSize(0.03)
174 c.t2.SetTextAngle(90)
176 c.t3 = ROOT.TText(0.03, 0.922,
'Slots')
179 c.t3.SetTextSize(0.03)
180 c.t3.SetTextAngle(90)
191 parser = argparse.ArgumentParser(description=__doc__)
193 parser.add_argument(
'timeline', nargs=1, help=
'timeline file')
199 help=
'Regular expression to filter algorithms')
206 help=
'Do not wait for user input')
212 help=
'Show slots instead of threads (leads to overlaps!)')
219 const=
'timeline.png',
220 help=
'Save to FILE [%(const)s]')
227 help=
'Number of colors used for events (10 is default)')
234 help=
'Number of events to skip at the start')
241 help=
'width of the output picture')
248 help=
'height of the output picture')
250 args = parser.parse_args()
252 data =
read(args.timeline[0], args.select, args.skipevents)
253 c =
plot(data,
not args.slots, args.batch, args.nevtcolors, args.width,
256 c.SaveAs(args.outfile)
261 if __name__ ==
'__main__':