CMS 3D CMS Logo

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