CMS 3D CMS Logo

timingPdfMaker.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 #################################################################
4 # timingPdfMaker.py - python script
5 # Created by Alejandro Gomez
6 # For more info, please check:
7 # https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideHLTTimingSummary
8 # or by email: alejandro.gomez@cern.ch
9 
10 # This python script creates managebles pdf for HLT Timing Studies.
11 # It saves some.png files from a former root file, creates a tex file,
12 # compiles it twice for better results and remove all the temp files.
13 # Please check the usage function to see how it works.
14 ###############################################################
15 
16 #############################################
17 ## Imports
18 ############################################
19 
20 import os, glob
21 import os.path
22 import operator
23 import subprocess
24 import six
25 import sys, getopt
26 sys.argv.append('-b')
27 from ROOT import *
28 import cStringIO
29 
30 
31 gROOT.SetStyle("Plain")
32 gStyle.SetOptStat(111111)
33 gStyle.SetHistFillColor(kBlue)
34 
35 ###############################################################
36 def usage():
37 ###############################################################
38  print "\nThis is the usage function\n"
39  print 'Usage: '+sys.argv[0]+' -i <file1> [option]'
40  print 'e.g.: '+sys.argv[0]+' -i outputTiming.root -t'
41  print 'e.g.: '+sys.argv[0]+' -i outputTiming.root -s HLT_Jet300_v5\n'
42 
43  print '\n-----Options-----'
44  print ' -i Input File'
45  print ' -o Output File (optional)'
46  print ' -t For only main time info per event. It will take less than 1 min.'
47  print ' -p For path time info. It will take approx 25 min.'
48  print ' -m For module time info. It will take approx 25 min.'
49  print ' -s Path_Name (For an specific path). Ti will take less than 1 min.'
50  print '\n For -p or -m option, the process needs like 200 Mb in disk space,'
51  print ' but please dont be afraid. Before the process ends, all the temporal files'
52  print ' will be erased.'
53 
54 
55 ###############################################################
56 def maininfo(infile, outfile):
57  ''' Creates main info tex file'''
58 ##############################################################
59  texpreamble = ['\documentclass[10pt,a5paper,landscape]{report}\n',
60  '\usepackage{graphicx}\n',
61  '\usepackage[a5paper,vmargin={5mm,2mm},hmargin={5mm,5mm}]{geometry}\n',
62  '\usepackage[linktocpage]{hyperref}\n',
63  '\hypersetup{backref, colorlinks=true}\n',
64  '\\title{ \\textbf{\Huge{HLT Timing Summary}} \\footnote{\large{Documentation at \url{https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideHLTTimingSummary}}} \\\\ Main Info }\n',
65  '\\author{\Large{CMS Experiment}}\n',
66  '\date{\\today}\n',
67  '\\begin{document}\n',
68  '\maketitle\n',
69  '\\newpage\n',
70  '\clearpage\n'
71  '\\tableofcontents\n',
72  '\\newpage\n',
73  '\\newpage \chapter{Total time for all modules per event} \\newpage \centering \includegraphics[scale=0.6]{totalTimetemp.png}\n']
74 
75  names1 = {}
76  names4 = {}
77  specific = {}
78  file1 = TFile(infile,'read')
79  for k in file1.GetListOfKeys():
80  allnames = k.ReadObj().GetName()
81  if 'pathTime_' in allnames:
82  pathname = '_'.join(allnames.split('_')[1:])
83  if not pathname in names1:
84  names1[pathname] = k.ReadObj().GetMean()
85  elif 'incPathTime_' in allnames:
86  pathname = '_'.join(allnames.split('_')[1:])
87  if not pathname in names4:
88  names4[pathname] = k.ReadObj().GetMean()
89 
90  names2 = dict(sorted(six.iteritems(names1), key=operator.itemgetter(1),reverse=True)[:10])
91  names3 = sorted(names2, key=names2.get, reverse=True)
92  names5 = dict(sorted(six.iteritems(names4), key=operator.itemgetter(1),reverse=True)[:10])
93  names6 = sorted(names5, key=names5.get, reverse=True)
94 
95  texfile = open(outfile+'-main.tex', 'w')
96  texfile.writelines(texpreamble)
97  if os.path.exists(infile.replace('.root','')+'-summary.txt'):
98  excludefile = open(infile.replace('.root','')+'-summary.txt', 'r')
99  texfile.write('\\newpage \section{Summary} \n \\begin{verbatim} \n')
100  for line in excludefile.readlines():
101  texfile.write(line+'\n')
102  excludefile.close()
103  texfile.write('\end{verbatim}')
104  texfile.write('\\newpage \\chapter{10 Slowest Paths}\n')
105  texfile.write('\section{Average module (in path) time}\n')
106  for path in names3:
107  texfile.write('\\newpage \subsection{'+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.35]{moduleInPathTimeSummary'+ path.replace('_','') +'temp.png}\n')
108  get_plot2(infile,'moduleInPathTimeSummary_'+path)
109  texfile.write('\section{Average module (in path) running time}\n')
110  for path in names3:
111  texfile.write('\\newpage \subsection{'+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.35]{moduleInPathScaledTimeSummary'+ path.replace('_','') +'temp.png}\n')
112  get_plot2(infile,'moduleInPathScaledTimeSummary_'+path)
113  texfile.write('\section{Per event time for path}\n')
114  for path in names3:
115  texfile.write('\\newpage \subsection{'+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.6]{pathTime'+ path.replace('_','') +'temp.png}\n')
116  get_plot1(infile,'pathTime_'+path)
117  texfile.write('\section{Per event incremental time for path}\n')
118  for path in names6:
119  texfile.write('\\newpage \subsection{'+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.6]{incPathTime'+ path.replace('_','') +'temp.png}\n')
120  get_plot1(infile,'incPathTime_'+path)
121  texfile.write('\end{document}')
122  texfile.close()
123 
124  texfile.close()
125 
126  get_plot1(infile,'totalTime')
127 
128 
129 
130 #################################################
131 def pathsinfo(infile,outfile):
132  '''Create the paths info tex file'''
133 ################################################
134 
135  texpreamble = ['\documentclass[10pt,a5paper,landscape]{book}\n',
136  '\usepackage{graphicx}\n',
137  '\usepackage[a5paper,vmargin={5mm,2mm},hmargin={5mm,5mm}]{geometry}\n',
138  '\usepackage[linktocpage]{hyperref}\n',
139  '\usepackage[titles]{tocloft}\n'
140  '\hypersetup{backref, colorlinks=true}\n',
141  '\setlength{\cftsecnumwidth}{4em}\n'
142  '\\title{ \\textbf{\Huge{HLT Timing Summary}} \\footnote{\large{Documentation at \url{https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideHLTTimingSummary}}} \\\\ Paths Info }\n',
143  '\\author{\Large{CMS Experiment}}\n',
144  '\date{\\today}\n',
145  '\\begin{document}\n',
146  '\maketitle\n',
147  '\\newpage\n',
148  '\\tableofcontents\n',
149  '\\newpage\n']
150 
151  names1 = {}
152  file = TFile(infile,'read')
153  for k in file.GetListOfKeys():
154  allnames= k.ReadObj().GetName()
155  mean = 1
156  if 'moduleInPathScaledTime_' in allnames:
157  pathname = '_'.join(allnames.split('_')[1:-1])
158  if not pathname in names1:
159  names1[pathname] = mean
160  names = sorted(names1.keys())
161 
162  texfile = open(outfile+'-paths.tex', 'w')
163  texfile.writelines(texpreamble)
164 
165  texfile.write('\\chapter{Average module (in path) time}\n')
166  for path in names:
167  texfile.write('\\newpage \section{'+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.35]{moduleInPathTimeSummary'+ path.replace('_','') +'temp.png}\n')
168  get_plot2(infile,'moduleInPathTimeSummary_'+path)
169  texfile.write('\\chapter{Average module (in path) running time}\n')
170  for path in names:
171  texfile.write('\\newpage \section{'+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.35]{moduleInPathScaledTimeSummary'+ path.replace('_','') +'temp.png}\n')
172  get_plot2(infile,'moduleInPathScaledTimeSummary_'+path)
173  texfile.write('\\chapter{Failing module (by path)}')
174  for path in names:
175  texfile.write('\\newpage \section{'+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.35]{failedModule'+ path.replace('_','') +'temp.png}\n')
176  get_plot2(infile,'failedModule_'+path)
177  texfile.write('\\chapter{Per event time for path}\n')
178  for path in names:
179  texfile.write('\\newpage \section{'+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.6]{pathTime'+ path.replace('_','') +'temp.png}\n')
180  get_plot1(infile,'pathTime_'+path)
181  texfile.write('\\chapter{Per event incremental time for path}\n')
182  for path in names:
183  texfile.write('\\newpage \section{'+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.6]{incPathTime'+ path.replace('_','') +'temp.png}\n')
184  get_plot1(infile,'incPathTime_'+path)
185 
186  texfile.write('\end{document}')
187  texfile.close()
188 
189 
190 #################################################
191 def moduleinfo(infile,outfile):
192  '''Create the paths info tex file'''
193 ################################################
194 
195  texpreamble = ['\documentclass[10pt,a5paper,landscape]{report}\n',
196  '\usepackage{graphicx}\n',
197  '\usepackage[a5paper,vmargin={5mm,2mm},hmargin={5mm,5mm}]{geometry}\n',
198  '\usepackage[linktocpage]{hyperref}\n',
199  '\hypersetup{backref, colorlinks=true}\n',
200  '\usepackage[titles]{tocloft}\n'
201  '\setlength{\cftsecnumwidth}{4em}\n'
202  '\\title{ \\textbf{\Huge{HLT Timing Summary}} \\footnote{\large{Documentation at \url{https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideHLTTimingSummary}}} \\\\ Modules Info }\n',
203  '\\author{\Large{CMS Experiment}}\n',
204  '\date{\\today}\n',
205  '\\begin{document}\n',
206  '\maketitle\n',
207  '\\newpage\n',
208  '\\tableofcontents\n',
209  '\\newpage\n']
210 
211  names1 = {}
212  file = TFile(infile,'read')
213  for k in file.GetListOfKeys():
214  allnames = k.ReadObj().GetName()
215  mean = 1
216  if 'moduleTime_' in allnames:
217  modname = ''.join(allnames.split('_')[1:])
218  if not (('!' in modname) or ('-' in modname)):
219  if not modname in names1:
220  names1[modname] = mean
221  names = sorted(names1.keys())
222 
223  texfile1 = open(outfile+'-modules.tex', 'w')
224  texfile1.writelines(texpreamble)
225 
226  texfile1.write('\\chapter{Time per event for module} \n \\newpage')
227  for modules in names:
228  texfile1.write('\section{'+modules+'}')
229  texfile1.write('\centering \includegraphics[scale=0.4]{moduleTime'+ modules +'temp.png}\n')
230  get_plot1(infile,'moduleTime_'+modules)
231  texfile1.write('\end{document}')
232  texfile1.close()
233 
234  texfile2 = open(outfile+'-runningModules.tex', 'w')
235  texfile2.writelines(texpreamble)
236  texfile2.write('\\chapter{Running Time per event for module} \n \\newpage')
237  for modules in names:
238  texfile2.write('\section{'+modules+'}')
239  texfile2.write('\centering \includegraphics[scale=0.45]{moduleScaledTime'+modules+'temp.png}\n')
240  get_plot1(infile,'moduleScaledTime_'+ modules)
241 
242  texfile2.write('\end{document}')
243  texfile2.close()
244 
245 ###############################################################
246 def specificpathinfo(infile, outfile, path):
247  ''' Creates an specific path info tex file'''
248 ##############################################################
249  texpreamble = ['\documentclass[10pt,a5paper,landscape]{report}\n',
250  '\usepackage{graphicx}\n',
251  '\usepackage[a5paper,vmargin={5mm,2mm},hmargin={5mm,5mm}]{geometry}\n',
252  '\usepackage[linktocpage]{hyperref}\n',
253  '\usepackage[titles]{tocloft}\n'
254  '\hypersetup{backref, colorlinks=true}\n',
255  '\setlength{\cftsubsecnumwidth}{4em}\n'
256  '\\title{ \\textbf{\Huge{HLT Timing Summary}} \\footnote{\large{Documentation at \url{https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideHLTTimingSummary}}} \\\\ Main Info + ' + path.replace('_','\_') +' info }\n',
257  '\\author{\Large{CMS Experiment}}\n',
258  '\date{\\today}\n',
259  '\\begin{document}\n',
260  '\maketitle\n',
261  '\\tableofcontents \n'
262  '\\newpage\n \\chapter{Main Info} \n',
263  '\\newpage \centering \section{Total time for all modules per event} \includegraphics[scale=0.6]{totalTimetemp.png}\n']
264 
265  texfile = open(outfile+'-'+path+'.tex', 'w')
266  texfile.writelines(texpreamble)
267  get_plot1(infile,'totalTime')
268 
269  names = {}
270  file1 = TFile(infile,'read')
271  for k in file1.GetListOfKeys():
272  allnames = k.ReadObj().GetName()
273  if 'moduleInPathScaledTime_' in allnames:
274  pathname = '_'.join(allnames.split('_')[1:-1])
275  if not pathname in names:
276  names[pathname] = {}
277  for pathnames in names.keys():
278  histo = file1.Get('moduleInPathTimeSummary_'+pathnames)
279  for bin in range(histo.GetNbinsX()):
280  label = histo.GetXaxis().GetBinLabel(bin+1)
281  names[pathnames][bin+1] = label
282 
283  for pathname in names:
284  if path in pathname:
285  texfile.write('\chapter{' + path.replace('_','\_')+ ' Info} \n')
286  texfile.write('\\newpage \section{Average module in '+ path.replace('_','\_') +' time} \centering \includegraphics[scale=0.35]{moduleInPathTimeSummary'+ path.replace('_','') +'temp.png}\n')
287  get_plot2(infile,'moduleInPathTimeSummary_'+path)
288  texfile.write('\\newpage \section{Average module in '+ path.replace('_','\_') +' running time} \centering \includegraphics[scale=0.35]{moduleInPathScaledTimeSummary'+ path.replace('_','') +'temp.png}\n')
289  get_plot2(infile,'moduleInPathScaledTimeSummary_'+path)
290  texfile.write('\\newpage \section{Per event time for '+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.6]{pathTime'+ path.replace('_','') +'temp.png}\n')
291  get_plot1(infile,'pathTime_'+path)
292  texfile.write('\\newpage \section{Per event incremental time for '+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.6]{incPathTime'+ path.replace('_','') +'temp.png}\n')
293  get_plot1(infile,'incPathTime_'+path)
294  texfile.write('\section{Running time per event for '+path.replace('_','\_')+'}')
295  for modules in names[path].values():
296  texfile.write('\subsection{'+ modules +'} \centering \includegraphics[scale=0.6]{moduleInPathScaledTime'+ path.replace('_','') + modules +'temp.png}\n')
297  get_plot1(infile,'moduleInPathScaledTime_'+ path +'_'+ modules)
298  texfile.write('\\end{document}')
299  texfile.close()
300 
301 
302 
303 
304 ###################################################################
305 def get_plot1(file,allnames):
306  ''' Function to create the plot and save it as.png file '''
307 ###################################################################
308  file = TFile(file,'read')
309  histo = file.Get(allnames)
310  can = TCanvas('can', '', 800,600)
311  can.cd()
312  histo.UseCurrentStyle()
313  histo.Draw()
314  can.SetBorderMode(0)
315  can.SetFillColor(kWhite)
316  can.SaveAs(allnames.replace('_','')+'temp.png')
317  del can
318 
319 
320 
321 ###################################################################
322 def get_plot2(infile,allnames):
323  ''' Function to create the plot and save it as.png file '''
324 ###################################################################
325  file1 = TFile(infile,'read')
326  histo = file1.Get(allnames)
327  can = TCanvas('can', '', 1600,1000)
328  can.cd()
329  histo.UseCurrentStyle()
330  histo.Draw()
331  if histo.GetNbinsX() > 50:
332  histo.GetXaxis().SetLabelSize(0.02)
333  else:
334  histo.GetXaxis().SetLabelSize(0.03)
335  can.SetBorderMode(0)
336  can.SetBorderSize(0)
337  can.SetFillColor(kWhite)
338  can.SetBottomMargin(0.4)
339  can.SaveAs(allnames.replace('_','')+'temp.png')
340  del can
341 
342 ###############################################################
343 def main(argv):
344 ###############################################################
345  print "\nPython script that creates Timing Summary pdf files"
346  print "For more info, please contact Alejandro Gomez"
347  print "email: alejandro.gomez@cern.ch\n"
348 
349  infile = None
350  outfile = None
351  path = None
352  call_maininfo = False
353  call_pathsinfo = False
354  call_modulesinfo = False
355  call_specificinfo = False
356  try:
357  opts, args = getopt.getopt(argv, 'hi:o:tbpms:', ['help', 'input=', 'output='])
358  if not opts:
359  print 'No options supplied'
360  usage()
361  except getopt.GetoptError as e:
362  print e
363  usage()
364  sys.exit(2)
365  for opt, arg in opts:
366  if opt in ('h', '--help'):
367  usage()
368  sys.exit(2)
369  elif opt == '-b':
370  print 'Running in batch mode'
371  elif opt in ('-i', '--input'):
372  infile = arg
373  outfile = infile.replace('.root','')
374  elif opt in ('-o', '--output'):
375  outfile = arg
376  elif opt == '-t':
377  call_maininfo = True
378  elif opt == '-p':
379  call_pathsinfo = True
380  elif opt == '-m':
381  call_modulesinfo = True
382  elif opt == '-s':
383  path = arg
384  call_specificinfo = True
385  else:
386  usage()
387  sys.exit(2)
388 
389 
390  if call_maininfo:
391  print 'Creating the Main Info Timing Summary pdf'
392  print 'Creating plots...'
393  maininfo(infile,outfile)
394  print 'Compiling tex file......'
395  subprocess.call(['pdflatex', '-interaction=batchmode', outfile+'-main.tex'])
396  print 'Verifing......'
397  subprocess.call(['pdflatex', '-interaction=batchmode', outfile+'-main.tex']) #twice for better compilation
398  print 'Removing temp files.........'
399  os.remove(outfile+'-main.aux')
400  os.remove(outfile+'-main.log')
401  os.remove(outfile+'-main.out')
402  os.remove(outfile+'-main.tex')
403  os.remove(outfile+'-main.toc')
404  for filename in glob.glob('*temp.png'):
405  os.remove(filename)
406  print '{0}-main.pdf is done'.format(outfile)
407 
408  if call_pathsinfo:
409  print 'Creating the Paths Info Timing Summary pdf'
410  print 'This process takes awhile... please be patient'
411  print 'Creating plots...'
412  pathsinfo(infile,outfile)
413  print 'Compiling tex file......'
414  subprocess.call(['pdflatex', '-interaction=batchmode', outfile+'-paths.tex'])
415  print 'Verifing......'
416  subprocess.call(['pdflatex', '-interaction=batchmode', outfile+'-paths.tex']) #twice for better compilation
417  print 'Removing temp files.........'
418  os.remove(outfile+'-paths.aux')
419  os.remove(outfile+'-paths.log')
420  os.remove(outfile+'-paths.out')
421  os.remove(outfile+'-paths.tex')
422  os.remove(outfile+'-paths.toc')
423  for filename in glob.glob('*temp.png'):
424  os.remove(filename)
425  print '{0}-paths.pdf is done'.format(outfile)
426 
427  if call_modulesinfo:
428  print 'Creating the Modules Info Timing Summary pdf'
429  print 'This process takes awhile... please be patient'
430  print 'Creating plots...'
431  moduleinfo(infile,outfile)
432  print 'Compiling tex file......'
433  subprocess.call(['pdflatex', '-interaction=batchmode', outfile+'-modules.tex'])
434  subprocess.call(['pdflatex', '-interaction=batchmode', outfile+'-runningModules.tex'])
435  print 'Verifing......'
436  subprocess.call(['pdflatex', '-interaction=batchmode', outfile+'-modules.tex']) #twice for better compilation
437  subprocess.call(['pdflatex', '-interaction=batchmode', outfile+'-runningModules.tex']) #twice for better compilation
438  print 'Removing temp files.........'
439  os.remove(outfile+'-modules.aux')
440  os.remove(outfile+'-runningModules.aux')
441  os.remove(outfile+'-modules.log')
442  os.remove(outfile+'-runningModules.log')
443  os.remove(outfile+'-modules.out')
444  os.remove(outfile+'-runningModules.out')
445  os.remove(outfile+'-modules.tex')
446  os.remove(outfile+'-runningModules.tex')
447  os.remove(outfile+'-modules.toc')
448  os.remove(outfile+'-runningModules.toc')
449  for filename in glob.glob('*temp.png'):
450  os.remove(filename)
451  print '{0}-modules.pdf is done'.format(outfile)
452  print '{0}-runningModules.pdf is done'.format(outfile)
453 
454  if call_specificinfo:
455  print 'Creating the Main Info + '+ path +' Timing Summary pdf'
456  print 'This process takes awhile... please be patient'
457  print 'Creating plots...'
458  specificpathinfo(infile,outfile,path)
459  print 'Compiling tex file......'
460  subprocess.call(['pdflatex', '-interaction=batchmode', outfile+'-'+path+'.tex'])
461  print 'Verifing......'
462  subprocess.call(['pdflatex', '-interaction=batchmode', outfile+'-'+path+'.tex']) #twice for better compilation
463  print 'Removing temp files.........'
464  os.remove(outfile+'-'+path+'.aux')
465  os.remove(outfile+'-'+path+'.log')
466  os.remove(outfile+'-'+path+'.out')
467  os.remove(outfile+'-'+path+'.tex')
468  os.remove(outfile+'-'+path+'.toc')
469  for filename in glob.glob('*temp.png'):
470  os.remove(filename)
471  print '{0}-'.format(outfile)+path+'.pdf is done'
472 
473 
474 #######################################################
475 if __name__ =='__main__':
476 #######################################################
477  main(sys.argv[1:])
def pathsinfo(infile, outfile)
def specificpathinfo(infile, outfile, path)
def moduleinfo(infile, outfile)
def get_plot2(infile, allnames)
def get_plot1(file, allnames)
def maininfo(infile, outfile)
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
Definition: main.py:1