CMS 3D CMS Logo

cmsIgProf_Analysis.py

Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 # Import statements. An '''Analogue''' of the include c++ statement,
00004 # but really really not the same!
00005 # In python everything is an object, even if we don't know. 
00006 # os and time will become 2 objects for us.
00007 
00008 import os
00009 import time
00010 
00011 def execute(command):
00012     print '[IgAnalysis] %s ...' %command
00013     exitstate=os.system(command)
00014     return exitstate
00015     
00016 def analyse_prof_sim(profile_name,outdir):
00017     
00018     # A way of formatting strings similar to the c++ one                 
00019     outfile1='%s/mem_live_peak.res' %outdir
00020     outfile2='%s/doEvent_output.txt' %outdir
00021     outfile3='%s/doBeginJob_output.txt' %outdir    
00022     outfile4='%s/mem_live.res' %outdir
00023     outfile5='%s/mem_total.res' %outdir    
00024     
00025     #A first manipulation of the profile
00026     command='igprof-analyse -d -v -g --value peak -r MEM_LIVE %s > %s'\
00027                                     %(profile_name,outfile1)
00028     execute(command) #we use the method system of the object os to run a command
00029     
00030     # now let's execute the segment and analyse commands                                                                
00031     commands_list=(\
00032     'igprof-segment edm::EDProducer::doEvent < %s |tee  -a %s' %(outfile1,outfile2),
00033     
00034     'igprof-segment edm::EDProducer::doBeginJob < %s |tee -a %s' %(outfile1,outfile3),
00035     
00036     'igprof-analyse -d -v -g -r MEM_LIVE %s |tee -a \%s'\
00037                                     %(profile_name,outfile4),
00038     
00039     'igprof-analyse -d -v -g -r MEM_TOTAL %s |tee -a \%s'\
00040                                     %(profile_name,outfile5)
00041                   )
00042     
00043     for command in commands_list: #no iterator can be clearer than this one
00044         #print command
00045         execute(command)
00046         
00047     # Now for every plain ascii file we make an html:
00048     titlestring='<b>Report executed with release %s on %s.</b>\n<br>\n<hr>\n'\
00049                                    %(os.environ['CMSSW_VERSION'],time.asctime())
00050     
00051     for command,filename in map(None,commands_list,[outfile2,outfile3,outfile4,outfile5]):
00052         command_info='Command executed: <em>%s</em><br><br>\n' %command 
00053         
00054         # we open and read the txt ascii file
00055         txt_file=open(filename,'r')
00056         txt_file_content=txt_file.readlines()#again:everything is an object
00057         txt_file.close()
00058         
00059         html_file_name='%s.html' %filename[:-4]# a way to say the string until its last but 4th char
00060         html_file=open(html_file_name,'w')
00061         html_file.write('<html>\n<body>\n'+\
00062                         titlestring+\
00063                         command_info)
00064         for line in txt_file_content:
00065             html_file.write(line+'<br>\n')
00066         html_file.write('\n</body>\n</html>')
00067         html_file.close()
00068 
00069     # compress all the plain txt files!
00070     execute('pushd %s;gzip *txt;popd' %outdir)
00071                 
00072 #-------------------------------------------------------------------------------
00073 
00074 # A procedure used for importing the function above with the import statement
00075 # or to run it if the script is called: power python..
00076 if __name__ == '__main__':
00077     
00078     import optparse
00079     
00080     # Here we define an option parser to handle commandline options..
00081     usage='IgProf_Analysis.py <options>'
00082     parser = optparse.OptionParser(usage)
00083     parser.add_option('-i', '--in_  profile',
00084                       help='The profile to manipulate' ,
00085                       default='',
00086                       dest='profile')
00087                       
00088     parser.add_option('-o', '--outdir',
00089                       help='The directory of the output' ,
00090                       default='',
00091                       dest='outdir')
00092                       
00093     (options,args) = parser.parse_args()
00094     
00095     # Now some fault control..If an error is found we raise an exception
00096     if options.profile=='' or\
00097        options.outdir=='':
00098         raise('Please select a profile and an output dir!')
00099     
00100     if not os.path.exists(options.profile) or\
00101        not os.path.exists(options.outdir):
00102         raise ('Outdir or input profile not present!')
00103     
00104     #launch the function!
00105     analyse_prof_sim(options.profile,options.outdir)        
00106  

Generated on Tue Jun 9 17:49:25 2009 for CMSSW by  doxygen 1.5.4