Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:18:47

0001 #! /usr/bin/env python3
0002 from __future__ import print_function
0003 import sys, imp
0004 import os
0005 import subprocess
0006 
0007 from optparse import OptionParser
0008 
0009 
0010 # --
0011 # -- Usage :
0012 # -- Rate within a given PD :
0013 # --       ./PDRates.py  --runNumber 135525 --PD Mu
0014 # -- Plot the rate of a PD versus the LS number:
0015 # --       ./PDRates.py  --runNumber 135525 --PD Mu --perLS
0016 # -- Rates in all PDs:
0017 # --       ./PDRates.py  --runNumber 135525
0018 # --
0019 
0020 
0021 parser = OptionParser(usage="Example: ./PDRates.py --runNumber 146644 --PD Mu --perLS")
0022 parser.add_option("--runNumber",  dest="RunNumber",  help="run number", type="int", metavar="RunNumber")
0023 parser.add_option("--PD",dest="PrimaryDataset",help="PD name",metavar="PrimaryDataset")
0024 parser.add_option("--perLS",action="store_true",default=False,dest="perLS",help="plot the rate vs the LS number",metavar="perLS")
0025 parser.add_option("--logy",action="store_true",default=False,dest="logy",help="log scale for the y axis",metavar="logy")
0026 parser.add_option("--saveplot",action="store_true",default=False,dest="saveplot",help="save the plot as a tmp.eps",metavar="saveplot")
0027 (options, args) = parser.parse_args()
0028 
0029 
0030 def PrimaryDatasets(Run):
0031     # -- List of Primary Datasets:
0032 
0033     dbs_cmd = """ dbs search --query='find primds.name 
0034             where run=%d and dataset like */RAW' """ % (Run)
0035     rows = subprocess.getoutput(dbs_cmd)
0036     lines = rows.split("\n")
0037     j=0
0038     print("\nThe primary datasets for this run are: \n")
0039     for Line in lines:
0040         j=j+1
0041         if j <=4:
0042             continue
0043         print(Line)
0044         line=Line.split()
0045         Datasets.append(line[0])
0046     print(" ")
0047 
0048 
0049 
0050 def RateInPD(Run,PrimaryDataset,lsMin,lsMax,printLS=False):
0051     dbs_cmd = """ dbs search --query='find file,lumi,file.numevents, file.size
0052         where run=%d and dataset like /%s/*/RAW 
0053         and lumi >= %d and lumi <= %d
0054         and file.status=VALID '""" % (Run, PrimaryDataset,lsMin, lsMax)
0055     rows = subprocess.getoutput(dbs_cmd)
0056     lines = rows.split("\n")
0057     j=0
0058     LumiSections = []
0059     Files = []
0060     Evts = 0
0061     Size = 0
0062     LSinFile = {}
0063     NumberOfLSInFile = {}
0064     for Line in lines:
0065         j=j+1
0066         if j <=4:
0067             continue
0068         line=Line.split()
0069         LS = line[1]
0070         file = line[0]
0071         Nevts = int(line[2])
0072         size = int(line[3])
0073         LSinFile[LS] = file
0074         if LumiSections.count(LS) == 0:
0075             LumiSections.append(LS)
0076         if Files.count(file) == 0:
0077             Files.append(file)
0078             Evts += Nevts
0079             Size += size
0080             NumberOfLSInFile[file] =1
0081         else:
0082             NumberOfLSInFile[file] = NumberOfLSInFile[file]+1
0083         RatePerLS[LS] = Nevts
0084 
0085     Number_of_LS = len(LumiSections)
0086     LS_Length = 23.3
0087     if Run < 125100:
0088         LS_Length = 93.3
0089     rate = Evts / (Number_of_LS * LS_Length)
0090     if Evts > 0:
0091         size_per_event = (Size / Evts) / 1000.
0092     else:
0093         size_per_event=-1
0094     print("Rate in \t",PrimaryDataset,"\t is : \t",rate," Hz", " \t size is : \t",size_per_event, "kB / event ")
0095 
0096     lsmin=9999999
0097     lsmax=-1
0098     for (LS,file) in LSinFile.items():
0099         nls = NumberOfLSInFile[file]
0100         RatePerLS[LS] = RatePerLS[LS] / nls
0101         RatePerLS[LS] = RatePerLS[LS] / LS_Length
0102         if int(LS) > lsmax:
0103             lsmax=int(LS)
0104         if int(LS) < lsmin:
0105             lsmin=int(LS)
0106     if printLS:
0107         print("lsmin lsmax",lsmin,lsmax)
0108         for ls in range(lsmin,lsmax):
0109             if not repr(ls) in RatePerLS.keys():
0110                 RatePerLS[LS] = 0
0111                 print("Missing LS ",ls)
0112 
0113 
0114 if __name__ == "__main__":
0115 
0116     if not options.RunNumber:
0117         print("wrong usage")
0118         exit(2)
0119 
0120     lsMin = -1
0121     lsMax = 999999
0122 
0123 # -- does not work yet.
0124 #  if options.lsMin:
0125 #   lsMin = options.lsMin
0126 #  if options.lsMax:
0127 #   lsMax = options.lsMax
0128 
0129 
0130     print("\nRun Number: ",options.RunNumber)
0131     if options.PrimaryDataset:
0132         Run = options.RunNumber
0133         PrimaryDataset = options.PrimaryDataset
0134         RatePerLS = {}
0135         if not options.perLS:
0136             RateInPD(Run,PrimaryDataset,lsMin, lsMax, False)
0137         if options.perLS:
0138             average = 0
0139             nLS_within_range = 0
0140             RateInPD(Run,PrimaryDataset,lsMin, lsMax, True)
0141             RatesTmp = open("rates_tmp.txt","w")
0142             #RatesTmpSort = open("rates_tmp_sort.txt","w")
0143             for (LS, rate) in RatePerLS.items():
0144                 RatesTmp.write(LS+"\t"+repr(rate)+"\n")
0145                 #if int(LS) >=  lsMin and int(LS) <= lsMax:
0146                 #nLS_within_range =nLS_within_range +1
0147                 #average = average + rate
0148             #print "Average rate within ",options.lsMin," and ",options.lsMax," is: ",average/nLS_within_range
0149             #if os.path.exists("./rates_tmp_sort.txt"):
0150                 #os.system("rm rates_tmp_sort.txt")
0151             #os.system("sort -n rates_tmp.txt > rates_tmp_sort.txt")
0152             RatesTmp.close()
0153 
0154             TempFile = open("tmp.gnuplot.txt","w")
0155             if options.logy:
0156                 TempFile.write("set logscale y \n")
0157             if options.saveplot:
0158                 TempFile.write("set term postscript eps enhanced \n")
0159                 TempFile.write("set output \"tmp.eps\" \n")
0160             st_title = " \"Rates in PrimaryDataset " + PrimaryDataset+ " in Run " + repr(Run)+ "\" "
0161             TempFile.write("set title " + st_title + "\n")
0162             TempFile.write(" set pointsize 2. \n")
0163             TempFile.write(" set nokey \n")
0164             TempFile.write(" set xlabel \"LS number\" \n")
0165             TempFile.write(" set ylabel \"Rate (Hz)\" \n")
0166             TempFile.write(" plot \"rates_tmp.txt\" using 1:2 title 'Rate per LS' \n")
0167             if not options.saveplot:
0168                 TempFile.write(" pause -1")
0169             else:
0170                 print("The plot is saved under tmp.eps")
0171             TempFile.close()
0172 
0173             os.system("gnuplot tmp.gnuplot.txt")
0174 
0175 
0176     else:
0177         Run = options.RunNumber
0178         Datasets = []
0179         PrimaryDatasets(Run)
0180         for PrimaryDataset in Datasets:
0181             RatePerLS = {}
0182             RateInPD(Run,PrimaryDataset,lsMin,lsMax)
0183 
0184 
0185