CMS 3D CMS Logo

timingPdfMaker.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 
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 
19 
20 from __future__ import print_function
21 import os, glob
22 import os.path
23 import operator
24 import subprocess
25 import six
26 import sys, getopt
27 sys.argv.append('-b')
28 from ROOT import *
29 import cStringIO
30 
31 
32 gROOT.SetStyle("Plain")
33 gStyle.SetOptStat(111111)
34 gStyle.SetHistFillColor(kBlue)
35 
36 
37 def usage():
38 
39  print("\nThis is the usage function\n")
40  print('Usage: '+sys.argv[0]+' -i <file1> [option]')
41  print('e.g.: '+sys.argv[0]+' -i outputTiming.root -t')
42  print('e.g.: '+sys.argv[0]+' -i outputTiming.root -s HLT_Jet300_v5\n')
43 
44  print('\n-----Options-----')
45  print(' -i Input File')
46  print(' -o Output File (optional)')
47  print(' -t For only main time info per event. It will take less than 1 min.')
48  print(' -p For path time info. It will take approx 25 min.')
49  print(' -m For module time info. It will take approx 25 min.')
50  print(' -s Path_Name (For an specific path). Ti will take less than 1 min.')
51  print('\n For -p or -m option, the process needs like 200 Mb in disk space,')
52  print(' but please dont be afraid. Before the process ends, all the temporal files')
53  print(' will be erased.')
54 
55 
56 
57 def maininfo(infile, outfile):
58  ''' Creates main info tex file'''
59 
60  texpreamble = ['\documentclass[10pt,a5paper,landscape]{report}\n',
61  '\\usepackage{graphicx}\n',
62  '\\usepackage[a5paper,vmargin={5mm,2mm},hmargin={5mm,5mm}]{geometry}\n',
63  '\\usepackage[linktocpage]{hyperref}\n',
64  '\hypersetup{backref, colorlinks=true}\n',
65  '\\title{ \\textbf{\Huge{HLT Timing Summary}} \\footnote{\large{Documentation at \\url{https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideHLTTimingSummary}}} \\\\ Main Info }\n',
66  '\\author{\Large{CMS Experiment}}\n',
67  '\date{\\today}\n',
68  '\\begin{document}\n',
69  '\maketitle\n',
70  '\\newpage\n',
71  '\clearpage\n'
72  '\\tableofcontents\n',
73  '\\newpage\n',
74  '\\newpage \chapter{Total time for all modules per event} \\newpage \centering \includegraphics[scale=0.6]{totalTimetemp.png}\n']
75 
76  names1 = {}
77  names4 = {}
78  specific = {}
79  file1 = TFile(infile,'read')
80  for k in file1.GetListOfKeys():
81  allnames = k.ReadObj().GetName()
82  if 'pathTime_' in allnames:
83  pathname = '_'.join(allnames.split('_')[1:])
84  if not pathname in names1:
85  names1[pathname] = k.ReadObj().GetMean()
86  elif 'incPathTime_' in allnames:
87  pathname = '_'.join(allnames.split('_')[1:])
88  if not pathname in names4:
89  names4[pathname] = k.ReadObj().GetMean()
90 
91  names2 = dict(sorted(six.iteritems(names1), key=operator.itemgetter(1),reverse=True)[:10])
92  names3 = sorted(names2, key=names2.get, reverse=True)
93  names5 = dict(sorted(six.iteritems(names4), key=operator.itemgetter(1),reverse=True)[:10])
94  names6 = sorted(names5, key=names5.get, reverse=True)
95 
96  texfile = open(outfile+'-main.tex', 'w')
97  texfile.writelines(texpreamble)
98  if os.path.exists(infile.replace('.root','')+'-summary.txt'):
99  excludefile = open(infile.replace('.root','')+'-summary.txt', 'r')
100  texfile.write('\\newpage \section{Summary} \n \\begin{verbatim} \n')
101  for line in excludefile.readlines():
102  texfile.write(line+'\n')
103  excludefile.close()
104  texfile.write('\end{verbatim}')
105  texfile.write('\\newpage \\chapter{10 Slowest Paths}\n')
106  texfile.write('\section{Average module (in path) time}\n')
107  for path in names3:
108  texfile.write('\\newpage \subsection{'+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.35]{moduleInPathTimeSummary'+ path.replace('_','') +'temp.png}\n')
109  get_plot2(infile,'moduleInPathTimeSummary_'+path)
110  texfile.write('\section{Average module (in path) running time}\n')
111  for path in names3:
112  texfile.write('\\newpage \subsection{'+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.35]{moduleInPathScaledTimeSummary'+ path.replace('_','') +'temp.png}\n')
113  get_plot2(infile,'moduleInPathScaledTimeSummary_'+path)
114  texfile.write('\section{Per event time for path}\n')
115  for path in names3:
116  texfile.write('\\newpage \subsection{'+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.6]{pathTime'+ path.replace('_','') +'temp.png}\n')
117  get_plot1(infile,'pathTime_'+path)
118  texfile.write('\section{Per event incremental time for path}\n')
119  for path in names6:
120  texfile.write('\\newpage \subsection{'+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.6]{incPathTime'+ path.replace('_','') +'temp.png}\n')
121  get_plot1(infile,'incPathTime_'+path)
122  texfile.write('\end{document}')
123  texfile.close()
124 
125  texfile.close()
126 
127  get_plot1(infile,'totalTime')
128 
129 
130 
131 
132 def pathsinfo(infile,outfile):
133  '''Create the paths info tex file'''
134 
135 
136  texpreamble = ['\documentclass[10pt,a5paper,landscape]{book}\n',
137  '\\usepackage{graphicx}\n',
138  '\\usepackage[a5paper,vmargin={5mm,2mm},hmargin={5mm,5mm}]{geometry}\n',
139  '\\usepackage[linktocpage]{hyperref}\n',
140  '\\usepackage[titles]{tocloft}\n'
141  '\hypersetup{backref, colorlinks=true}\n',
142  '\setlength{\cftsecnumwidth}{4em}\n'
143  '\\title{ \\textbf{\Huge{HLT Timing Summary}} \\footnote{\large{Documentation at \\url{https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideHLTTimingSummary}}} \\\\ Paths Info }\n',
144  '\\author{\Large{CMS Experiment}}\n',
145  '\date{\\today}\n',
146  '\\begin{document}\n',
147  '\maketitle\n',
148  '\\newpage\n',
149  '\\tableofcontents\n',
150  '\\newpage\n']
151 
152  names1 = {}
153  file = TFile(infile,'read')
154  for k in file.GetListOfKeys():
155  allnames= k.ReadObj().GetName()
156  mean = 1
157  if 'moduleInPathScaledTime_' in allnames:
158  pathname = '_'.join(allnames.split('_')[1:-1])
159  if not pathname in names1:
160  names1[pathname] = mean
161  names = sorted(names1.keys())
162 
163  texfile = open(outfile+'-paths.tex', 'w')
164  texfile.writelines(texpreamble)
165 
166  texfile.write('\\chapter{Average module (in path) time}\n')
167  for path in names:
168  texfile.write('\\newpage \section{'+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.35]{moduleInPathTimeSummary'+ path.replace('_','') +'temp.png}\n')
169  get_plot2(infile,'moduleInPathTimeSummary_'+path)
170  texfile.write('\\chapter{Average module (in path) running time}\n')
171  for path in names:
172  texfile.write('\\newpage \section{'+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.35]{moduleInPathScaledTimeSummary'+ path.replace('_','') +'temp.png}\n')
173  get_plot2(infile,'moduleInPathScaledTimeSummary_'+path)
174  texfile.write('\\chapter{Failing module (by path)}')
175  for path in names:
176  texfile.write('\\newpage \section{'+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.35]{failedModule'+ path.replace('_','') +'temp.png}\n')
177  get_plot2(infile,'failedModule_'+path)
178  texfile.write('\\chapter{Per event time for path}\n')
179  for path in names:
180  texfile.write('\\newpage \section{'+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.6]{pathTime'+ path.replace('_','') +'temp.png}\n')
181  get_plot1(infile,'pathTime_'+path)
182  texfile.write('\\chapter{Per event incremental time for path}\n')
183  for path in names:
184  texfile.write('\\newpage \section{'+ path.replace('_','\_') +'} \centering \includegraphics[scale=0.6]{incPathTime'+ path.replace('_','') +'temp.png}\n')
185  get_plot1(infile,'incPathTime_'+path)
186 
187  texfile.write('\end{document}')
188  texfile.close()
189 
190 
191 
192 def moduleinfo(infile,outfile):
193  '''Create the paths info tex file'''
194 
195 
196  texpreamble = ['\documentclass[10pt,a5paper,landscape]{report}\n',
197  '\\usepackage{graphicx}\n',
198  '\\usepackage[a5paper,vmargin={5mm,2mm},hmargin={5mm,5mm}]{geometry}\n',
199  '\\usepackage[linktocpage]{hyperref}\n',
200  '\hypersetup{backref, colorlinks=true}\n',
201  '\\usepackage[titles]{tocloft}\n'
202  '\setlength{\cftsecnumwidth}{4em}\n'
203  '\\title{ \\textbf{\Huge{HLT Timing Summary}} \\footnote{\large{Documentation at \\url{https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideHLTTimingSummary}}} \\\\ Modules Info }\n',
204  '\\author{\Large{CMS Experiment}}\n',
205  '\date{\\today}\n',
206  '\\begin{document}\n',
207  '\maketitle\n',
208  '\\newpage\n',
209  '\\tableofcontents\n',
210  '\\newpage\n']
211 
212  names1 = {}
213  file = TFile(infile,'read')
214  for k in file.GetListOfKeys():
215  allnames = k.ReadObj().GetName()
216  mean = 1
217  if 'moduleTime_' in allnames:
218  modname = ''.join(allnames.split('_')[1:])
219  if not (('!' in modname) or ('-' in modname)):
220  if not modname in names1:
221  names1[modname] = mean
222  names = sorted(names1.keys())
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:])
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
timingPdfMaker.usage
def usage()
Definition: timingPdfMaker.py:37
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
timingPdfMaker.get_plot1
def get_plot1(file, allnames)
Definition: timingPdfMaker.py:306
contentValuesCheck.values
values
Definition: contentValuesCheck.py:38
timingPdfMaker.specificpathinfo
def specificpathinfo(infile, outfile, path)
Definition: timingPdfMaker.py:247
timingPdfMaker.maininfo
def maininfo(infile, outfile)
Definition: timingPdfMaker.py:57
timingPdfMaker.moduleinfo
def moduleinfo(infile, outfile)
Definition: timingPdfMaker.py:192
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
timingPdfMaker.pathsinfo
def pathsinfo(infile, outfile)
Definition: timingPdfMaker.py:132
main
Definition: main.py:1
format
timingPdfMaker.get_plot2
def get_plot2(infile, allnames)
Definition: timingPdfMaker.py:323
timingPdfMaker.main
def main(argv)
Definition: timingPdfMaker.py:344