CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
cmsTiming_parser.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 
4 def get_max(data,index=1):
5  max_time=-1
6  for el in data:
7  sec=el[index]
8  if max_time<sec:
9  max_time=sec
10  return max_time
11 
12 def get_min(data,index=1):
13  min_time=1e20
14  for el in data:
15  sec=el[index]
16  if min_time>sec:
17  min_time=sec
18  return min_time
19 
20 def manipulate_log(outdir,logfile_name,secsperbin):
21 
22  import time
23  import sys
24  import ROOT
25 
26  # the fundamental structure: the key is the evt number the value is a list containing
27  # VSIZE deltaVSIZE RSS deltaRSS
28  data=[]
29 
30  # open file and read it and fill the structure!
31  logfile=open(logfile_name,'r')
32  logfile_lines=logfile.readlines()
33  logfile.close()
34 
35  # we get the info we need!
36  i=0
37  while i < len(logfile_lines):
38  line=logfile_lines[i]
39  if 'TimeEvent>' in line:
40  line=line[:-1] #no \n!
41  line_content_list=line.split(' ')
42  event_number=int(line_content_list[1])
43  seconds=float(line_content_list[3])
44  data.append((event_number,seconds))
45  i+=1
46 
47  # init Graph and histo
48 
49  # The Graphs
50  __argv=sys.argv # trick for a strange behaviour of the TApp..
51  sys.argv=sys.argv[:1]
52  ROOT.gROOT.SetStyle("Plain") # style paranoia
53  sys.argv=__argv
54  #Cannot use this option when the logfile includes
55  #a large number of events... PyRoot seg-faults.
56  #Set ROOT in batch mode to avoid canvases popping up!
57  ROOT.gROOT.SetBatch(1)
58 
59  # Save in file
60  rootfilename='%s/graphs.root' %outdir
61  myfile=ROOT.TFile(rootfilename,'RECREATE')
62 
63 
64  # Set fancy limits
65  min_val=get_min(data,1)
66  max_val=get_max(data,1)
67  interval=max_val-min_val
68 
69  min_val=min_val-interval*0.2
70  max_val=max_val+interval*0.2
71  interval=max_val-min_val
72 
73  nbins=int(interval/secsperbin)
74 
75  print 'Minval=',min_val,' maxval=',max_val, ' interval=',interval
76 
77  histo=ROOT.TH1F('Seconds per event','Seconds per event',nbins,min_val,max_val)
78  histo.GetXaxis().SetTitle("s")
79 
80  npoints=len(data)
81 
82  graph=ROOT.TGraph(npoints)
83  graph.SetMarkerStyle(8)
84  graph.SetMarkerSize(.7)
85  graph.SetMarkerColor(1)
86  graph.SetLineWidth(3)
87  graph.SetLineColor(2)
88  graph.SetTitle('Seconds per event')
89  graph.SetName('SecondsPerEvent')
90  graph.GetXaxis().SetTitle("Event")
91 
92  last_event=data[-1][0]
93  print 'last event =',last_event
94  graph.GetXaxis().SetLimits(0,last_event)
95 
96  graph.GetYaxis().SetTitleOffset(1.3)
97  graph.GetYaxis().SetTitle("s")
98  graph.GetYaxis().SetRangeUser(0,max_val)
99 
100 
101 
102  # Fill them
103 
104  evt_counter=0
105  total=0
106  for evt_num,secs in data:
107  graph.SetPoint(evt_counter,evt_num,secs)
108  histo.Fill(secs)
109  total+=secs
110  evt_counter+=1
111 
112  print 'Total Time=', total
113 
114  #A line which represents the average is drawn in the TGraph
115  avg=histo.GetMean()
116  avg_line=ROOT.TLine(1,avg,last_event,avg)
117  avg_line.SetLineColor(4)
118  avg_line.SetLineWidth(2)
119 
120  # draw and save!
121  graph_canvas=ROOT.TCanvas('graph_canvas')
122  graph_canvas.cd()
123  graph.Draw("ALP")
124  avg_line.Draw("Same")
125 
126  graph_canvas.Print("%s/graph.gif" %outdir,"gif")
127 
128  # write it on file
129  graph.Write()
130  graph_canvas.Write()
131 
132  histo_canvas=ROOT.TCanvas('histo_canvas')
133  histo_canvas.cd()
134  histo.Draw('')
135 
136  histo_canvas.Print("%s/histo.gif" %outdir,"gif")
137 
138  # write it on file
139  histo.Write()
140  histo_canvas.Write()
141 
142  myfile.Close()
143 
144  # The html page!------------------------------------------------------------------------------
145 
146  titlestring='<b>Report executed with release %s on %s.</b>\n<br>\n<hr>\n'\
147  %(os.environ['CMSSW_VERSION'],time.asctime())
148 
149  html_file_name='%s/%s.html' %(outdir,logfile_name[:-4])# a way to say the string until its last but 4th char
150  html_file=open(html_file_name,'w')
151  html_file.write('<html>\n<body>\n'+\
152  titlestring)
153  html_file.write('<table>\n'+\
154  '<tr><td><img src=graph.gif></img></td></tr>'+\
155  '<tr><td><img src=histo.gif></img></td></tr>'+\
156  '</table>\n')
157  html_file.write('\n</body>\n</html>')
158  html_file.close()
159 
160 
161 #################################################################################################
162 
163 if __name__ == '__main__':
164 
165  import optparse
166  import os
167 
168  # Here we define an option parser to handle commandline options..
169  usage='timing_parser.py <options>'
170  parser = optparse.OptionParser(usage)
171  parser.add_option('-i', '--in_ profile',
172  help='The profile to manipulate' ,
173  default='',
174  dest='profile')
175 
176  parser.add_option('-o', '--outdir',
177  help='The directory of the output' ,
178  default='',
179  dest='outdir')
180 
181  parser.add_option('-n',
182  help='Number of secs per bin. Default is 1.' ,
183  default='1',
184  dest='startevt')
185 
186  (options,args) = parser.parse_args()
187 
188  # Now some fault control..If an error is found we raise an exception
189  if options.profile=='' or\
190  options.outdir=='':
191  raise('Please select a profile and an output dir!')
192 
193  if not os.path.exists(options.profile) or\
194  not os.path.exists(options.outdir):
195  raise ('Outdir or input profile not present!')
196 
197  try:
198  startevt=float(options.startevt)
199  except ValueError:
200  print 'Problems in convertng starting event value!'
201 
202 
203  #launch the function!
204  manipulate_log(options.outdir,options.profile,startevt)
205 
206