Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-07 22:33:06

0001 #! /usr/bin/env python3
0002 
0003 import os
0004 import sys
0005 import errno
0006 
0007 #print error/help message and exit
0008 def help_message():
0009     print "Usage:\n\
0010 compare [folder_name] [options] -v versions_to_compare -f files_to_compare\n\
0011 Versions and files must be whitespace separated.\n\
0012 If no folder is specified the pwd will be used.\n\
0013 folder_name, if specified, must be the first argument.\n\
0014 Options:\n\
0015 -h   prints this message.\n\
0016 --no_exec   does not show graphs (batch mode).\n\
0017 --outfile filename.root    writes the root output in the specified file.\n\
0018 --html   produce html output\n\
0019 --canvas   with --html option and without --no_exec option produces output directly in png, if not specified output is produced in eps and then converted in png format with an external program (but can be used in batch mode).\n\
0020 Example:\n\
0021 ./compare.py myDir -v CMSSW_X_Y_Z CMSSW_J_K_W -f file1.root file2.root --no_exec --outfile out.root"
0022     sys.exit()
0023 
0024 #run command in the command line with specified environment
0025 def runcmd(envir,program,*args):
0026     pid=os.fork()
0027     if not pid:
0028         os.execvpe(program,(program,)+args,envir)
0029     return os.wait()[0]
0030 
0031 def runcmd2(envir,program,args):
0032     pid=os.fork()
0033     if not pid:
0034         os.execvpe(program,(program,)+args,envir)
0035     return os.wait()[0]
0036 
0037 #recursive function to search the graphs in the root file
0038 #unfortunately different versions/files produce different paths
0039 wdir=0
0040 ptf=""
0041 def srcpath(fldr):
0042     tmp=[]
0043     wdir.cd(fldr)
0044     for key in wdir.GetListOfKeys():
0045         if key.IsFolder():
0046             tmp=srcpath(key.GetName())
0047             if tmp[0]:
0048                 return [True,key.GetName()+"/"+tmp[1]]
0049         else:
0050             if key.GetName()=="eventEnergyEB":
0051                 pth=wdir.pwd()
0052                 return [True,""]
0053     wdir.cd("../")
0054     return [False,""]
0055 
0056 #print the help message
0057 if "-h" in sys.argv or "-help" in sys.argv or "--help" in sys.argv:
0058     help_message()
0059 
0060 #move to the working diretory (if specified)
0061 cwd=""
0062 if len(sys.argv)>1:
0063     if not sys.argv[1][0]=="-":
0064         name=sys.argv[1]        
0065         try:
0066             cwd=os.getcwd()
0067             os.chdir(name)
0068         except OSError, inst:
0069             if inst.errno==errno.ENOENT:
0070                 print "Error: the specified working folder does not exist"
0071                 help_message()
0072 else: help_message()
0073 
0074 #read and parse the input
0075 state="n"
0076 root_out="compare_out.root"
0077 execute=True
0078 html=False
0079 cnv=False
0080 
0081 ver=[]
0082 fil=[]
0083 for arg in sys.argv:
0084     if arg=="-v" or arg=="-V":
0085         state="v"
0086     elif arg=="-f" or arg=="-F":
0087         state="f"
0088     elif arg=="--no_exec":
0089         execute=False
0090     elif arg=="--outfile":
0091         state="r"
0092     elif arg=="--html":
0093         html=True
0094     elif arg=="--canvas":
0095         cnv=True
0096     elif state=="v":
0097         ver.append(arg)
0098     elif state=="f":
0099         fil.append(arg)
0100     elif state=="r":
0101         root_out=arg
0102 
0103 #disable window output if --no_exec argument is passed
0104 if not execute:
0105     sys.argv.append("-b")
0106 
0107 #try to use pyROOT directly from here, if environment variables were not set
0108 #this will run again the program with environment variables of the highest version of CMSSW specified in -v
0109 try:
0110     from ROOT import *
0111 except ImportError:
0112     print "Warning: environment variables not set, proceeding anyway running this script with the environment variables of the higher version of CMSSW specified in -v"
0113     #storing cmsenv environment variables
0114     os.chdir(max(ver))
0115     env=os.popen("scramv1 runtime -sh","r")
0116     environment=os.environ
0117     for l in env.readlines():
0118         variable,value=l[7:len(l)-3].strip().split("=",1)
0119         environment[variable]=value[1:]
0120     env.close()
0121     if cwd=="":
0122         os.chdir("../")
0123     else:
0124         os.chdir("../../")
0125     #running again the program
0126     if execute:
0127         runcmd2(environment,"./compare.py",tuple(sys.argv[1:]))#works only if compare.py is located in the pwd
0128     else:
0129         runcmd2(environment,"./compare.py",tuple(sys.argv[1:-1]))#works only if compare.py is located in the pwd
0130 else:    
0131     gSystem.Load("libFWCoreFWLite.so")
0132     FWLiteEnabler::enable()
0133     outfile=TFile(root_out,"recreate")
0134     histo=[]
0135     canvas=[]
0136     legend=TLegend(.89,0.8,1,.2)
0137     histonames=["eventEnergyEB","eventEnergyEE","iEtaDistributionEB","iphiDistributionEB","meanEnergyEB","meanEnergyEE","nRechitsEB","nRechitsEE","rhEnergyEB","rhEnergyEE","iEtaDistributionEE"]
0138     colornames=[kRed,kBlue,kGreen,kCyan,kMagenta,kYellow,kOrange,kPink,kViolet,kAzure,kTeal,kSpring]
0139 #kBlack,
0140     for nv,v in enumerate(ver):
0141         histo.append([])#a list for every version: "version list"
0142         for nf,f in enumerate(fil):
0143             if not nv:
0144                 canvas.append(TCanvas(f[:len(f)-5],f[:len(f)-5]))
0145                 canvas[nf].Divide(2,6)
0146             histo[nv].append([])#a list for every file in each version: "file list"
0147             histo[nv][nf].append(TFile(v+"/"+f))#the first element of the "file list" is the file itself
0148             histo[nv][nf].append([])#the second element of the "file list" is the "histo list"
0149             histo[nv][nf][0].cd()
0150             wdir=gDirectory
0151             pth=srcpath("")[1]
0152             for nh,h in enumerate(histonames):
0153                 histo[nv][nf][1].append(histo[nv][nf][0].Get(pth+h))#the histo list contains histos
0154                 canvas[nf].cd(nh+1)
0155                 histo[nv][nf][1][nh].SetLineColor(colornames[nv%len(colornames)])
0156                 if nv:
0157                     histo[nv][nf][1][nh].Draw("same")
0158                 else:
0159                     histo[nv][nf][1][nh].Draw()
0160                 if nf==0 and nh==0:
0161                     legend.AddEntry(histo[nv][nf][1][nh],v[6:],"l")
0162                 if nv==(len(ver)-1):
0163                     legend.Draw()
0164             if nv==(len(ver)-1):
0165                 outfile.cd()
0166                 canvas[nf].Write()
0167     if execute:
0168         print "Press enter to end the program"
0169         os.system("read")
0170     if html:
0171         if cnv:
0172             if execute:
0173                 for nf,f in enumerate(fil):
0174                     try:
0175                         os.mkdir(f[:len(f)-5])
0176                     except OSError,inst:
0177                         if inst.errno==errno.EEXIST:
0178                             print "Possibly overwriting images"
0179                     os.system("cp "+cwd+"/temp.html "+f[:len(f)-5]+"/index.html")
0180                     os.chdir(f[:len(f)-5])
0181                     canvas[nf].cd()
0182                     canvas[nf].SetWindowSize(1050,780)
0183                     #os.system("sleep 2")
0184                     for nh,h in enumerate(histonames):
0185                         canvas[nf].cd(nh+1).SetPad(0,0,1,1)
0186                         canvas[nf].cd(nh+1).Print(h+".png","png")
0187                     #os.system("sleep 2")
0188                     canvas[nf].SetWindowSize(500,375)
0189                     #os.system("sleep 2")
0190                     for nh,h in enumerate(histonames):
0191                         canvas[nf].cd(nh+1).SetPad(0,0,1,1)
0192                         canvas[nf].cd(nh+1).Print(h+"_s.png","png")
0193                     #os.system("sleep 2")
0194                     os.chdir("../")
0195             else:
0196                 print "Warning:to use --canvas option do not use --no_exec option. Rerun without --canvas option."
0197         else:
0198             for nf,f in enumerate(fil):
0199                 try:
0200                     os.mkdir(f[:len(f)-5])
0201                 except OSError,inst:
0202                     if inst.errno==errno.EEXIST:
0203                         print "Possibly overwriting images"
0204                 os.system("cp "+cwd+"temp.html "+f[:len(f)-5]+"/index.html")
0205                 os.chdir(f[:len(f)-5])
0206                 canvas[nf].cd()
0207                 canvas[nf].SetWindowSize(1050,780)
0208                 for nh,h in enumerate(histonames):
0209                     canvas[nf].cd(nh+1).SetPad(0,0,1,1)
0210                     canvas[nf].cd(nh+1).Print(h+".eps")
0211                     gSystem.Exec("pstopnm -ppm -xborder 0 -yborder 0 -xsize 1050 -portrait "+h+".eps");
0212                     gSystem.Exec("pnmtopng "+h+".eps001.ppm > "+h+".png")
0213                     try:
0214                         os.remove(h+".eps")
0215                     except OSError:
0216                         pass
0217                     try:
0218                         os.remove(h+".eps001.ppm")
0219                     except OSError:
0220                         pass
0221                 canvas[nf].SetWindowSize(500,375)
0222                 for nh,h in enumerate(histonames):
0223                     canvas[nf].cd(nh+1).SetPad(0,0,1,1)
0224                     canvas[nf].cd(nh+1).Print(h+"_s.eps")
0225                     gSystem.Exec("pstopnm -ppm -xborder 0 -yborder 0 -xsize 500 -portrait "+h+"_s.eps");
0226                     gSystem.Exec("pnmtopng "+h+"_s.eps001.ppm > "+h+"_s.png")
0227                     try:
0228                         os.remove(h+"_s.eps")
0229                     except OSError:
0230                         pass
0231                     try:
0232                         os.remove(h+"_s.eps001.ppm")
0233                     except OSError:
0234                         pass
0235                 os.chdir("../")