CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/HLTrigger/Tools/python/PDRates.py

Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 import sys, imp
00003 import os
00004 import commands
00005 
00006 from optparse import OptionParser
00007 
00008 
00009 # --
00010 # -- Usage :
00011 # -- Rate within a given PD :
00012 # --       ./PDRates.py  --runNumber 135525 --PD Mu
00013 # -- Plot the rate of a PD versus the LS number:
00014 # --       ./PDRates.py  --runNumber 135525 --PD Mu --perLS
00015 # -- Rates in all PDs:
00016 # --       ./PDRates.py  --runNumber 135525
00017 # --
00018 
00019 
00020 parser = OptionParser(usage="Example: ./PDRates.py --runNumber 146644 --PD Mu --perLS")
00021 parser.add_option("--runNumber",  dest="RunNumber",  help="run number", type="int", metavar="RunNumber")
00022 parser.add_option("--PD",dest="PrimaryDataset",help="PD name",metavar="PrimaryDataset")
00023 parser.add_option("--perLS",action="store_true",default=False,dest="perLS",help="plot the rate vs the LS number",metavar="perLS")
00024 parser.add_option("--logy",action="store_true",default=False,dest="logy",help="log scale for the y axis",metavar="logy")
00025 parser.add_option("--saveplot",action="store_true",default=False,dest="saveplot",help="save the plot as a tmp.eps",metavar="saveplot")
00026 (options, args) = parser.parse_args()
00027 
00028 
00029 def PrimaryDatasets(Run):
00030         # -- List of Primary Datasets:
00031 
00032         dbs_cmd = """ dbs search --query='find primds.name 
00033                 where run=%d and dataset like */RAW' """ % (Run)
00034         rows = commands.getoutput(dbs_cmd)
00035         lines = rows.split("\n")
00036         j=0
00037         print "\nThe primary datasets for this run are: \n"
00038         for Line in lines:
00039                 j=j+1
00040                 if j <=4:
00041                         continue
00042                 print Line
00043                 line=Line.split()
00044                 Datasets.append(line[0])
00045         print " "
00046 
00047 
00048 
00049 def RateInPD(Run,PrimaryDataset,lsMin,lsMax,printLS=False):
00050         dbs_cmd = """ dbs search --query='find file,lumi,file.numevents, file.size
00051                 where run=%d and dataset like /%s/*/RAW 
00052                 and lumi >= %d and lumi <= %d
00053                 and file.status=VALID '""" % (Run, PrimaryDataset,lsMin, lsMax)
00054         rows = commands.getoutput(dbs_cmd)
00055         lines = rows.split("\n")
00056         j=0
00057         LumiSections = []
00058         Files = []
00059         Evts = 0
00060         Size = 0
00061         LSinFile = {}
00062         NumberOfLSInFile = {}
00063         for Line in lines:
00064                 j=j+1
00065                 if j <=4:
00066                         continue
00067                 line=Line.split()
00068                 LS = line[1]
00069                 file = line[0]
00070                 Nevts = int(line[2])
00071                 size = int(line[3])
00072                 LSinFile[LS] = file
00073                 if LumiSections.count(LS) == 0:
00074                         LumiSections.append(LS)
00075                 if Files.count(file) == 0:
00076                         Files.append(file)
00077                         Evts += Nevts
00078                         Size += size
00079                         NumberOfLSInFile[file] =1
00080                 else:
00081                         NumberOfLSInFile[file] = NumberOfLSInFile[file]+1
00082                 RatePerLS[LS] = Nevts
00083                         
00084         Number_of_LS = len(LumiSections)
00085         LS_Length = 23.3
00086         if Run < 125100:
00087                 LS_Length = 93.3
00088         rate = Evts / (Number_of_LS * LS_Length)
00089         if Evts > 0:
00090                 size_per_event = (Size / Evts) / 1000.
00091         else:
00092                 size_per_event=-1
00093         print "Rate in \t",PrimaryDataset,"\t is : \t",rate," Hz", " \t size is : \t",size_per_event, "kB / event "
00094 
00095         lsmin=9999999
00096         lsmax=-1
00097         for (LS,file) in LSinFile.iteritems():
00098                 nls = NumberOfLSInFile[file]
00099                 RatePerLS[LS] = RatePerLS[LS] / nls
00100                 RatePerLS[LS] = RatePerLS[LS] / LS_Length
00101                 if int(LS) > lsmax:
00102                         lsmax=int(LS)
00103                 if int(LS) < lsmin:
00104                         lsmin=int(LS)
00105         if printLS:
00106                 print "lsmin lsmax",lsmin,lsmax
00107                 for ls in range(lsmin,lsmax):
00108                         if not `ls` in RatePerLS.keys():
00109                                 RatePerLS[LS] = 0
00110                                 print "Missing LS ",ls
00111 
00112 
00113 if __name__ == "__main__":
00114 
00115   if not options.RunNumber:
00116         print "wrong usage"
00117         exit(2)
00118 
00119   lsMin = -1
00120   lsMax = 999999
00121 
00122 # -- does not work yet.
00123 #  if options.lsMin:
00124 #       lsMin = options.lsMin
00125 #  if options.lsMax:
00126 #       lsMax = options.lsMax
00127 
00128 
00129   print "\nRun Number: ",options.RunNumber
00130   if options.PrimaryDataset:
00131         Run = options.RunNumber
00132         PrimaryDataset = options.PrimaryDataset
00133         RatePerLS = {}
00134         if not options.perLS:
00135                 RateInPD(Run,PrimaryDataset,lsMin, lsMax, False)
00136         if options.perLS:
00137                 average = 0
00138                 nLS_within_range = 0
00139                 RateInPD(Run,PrimaryDataset,lsMin, lsMax, True)
00140                 RatesTmp = open("rates_tmp.txt","w")
00141                 #RatesTmpSort = open("rates_tmp_sort.txt","w")
00142                 for (LS, rate) in RatePerLS.iteritems():
00143                         RatesTmp.write(LS+"\t"+`rate`+"\n")
00144                         #if int(LS) >=  lsMin and int(LS) <= lsMax:
00145                                 #nLS_within_range =nLS_within_range +1
00146                                 #average = average + rate
00147                 #print "Average rate within ",options.lsMin," and ",options.lsMax," is: ",average/nLS_within_range
00148                 #if os.path.exists("./rates_tmp_sort.txt"):
00149                         #os.system("rm rates_tmp_sort.txt")
00150                 #os.system("sort -n rates_tmp.txt > rates_tmp_sort.txt")
00151                 RatesTmp.close()
00152                 
00153                 TempFile = open("tmp.gnuplot.txt","w")
00154                 if options.logy:
00155                         TempFile.write("set logscale y \n")
00156                 if options.saveplot:
00157                         TempFile.write("set term postscript eps enhanced \n")
00158                         TempFile.write("set output \"tmp.eps\" \n")
00159                 st_title = " \"Rates in PrimaryDataset " + PrimaryDataset+ " in Run " + `Run`+ "\" "
00160                 TempFile.write("set title " + st_title + "\n")
00161                 TempFile.write(" set pointsize 2. \n")
00162                 TempFile.write(" set nokey \n")
00163                 TempFile.write(" set xlabel \"LS number\" \n")
00164                 TempFile.write(" set ylabel \"Rate (Hz)\" \n")
00165                 TempFile.write(" plot \"rates_tmp.txt\" using 1:2 title 'Rate per LS' \n")
00166                 if not options.saveplot:
00167                         TempFile.write(" pause -1")
00168                 else:
00169                         print "The plot is saved under tmp.eps"
00170                 TempFile.close()
00171 
00172                 os.system("gnuplot tmp.gnuplot.txt")
00173 
00174 
00175   else:
00176         Run = options.RunNumber
00177         Datasets = []
00178         PrimaryDatasets(Run)
00179         for PrimaryDataset in Datasets:
00180                 RatePerLS = {}
00181                 RateInPD(Run,PrimaryDataset,lsMin,lsMax)
00182                 
00183 
00184