CMS 3D CMS Logo

PDRates.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 from __future__ import print_function
3 import sys, imp
4 import os
5 import commands
6 
7 from optparse import OptionParser
8 
9 
10 import six
11 # --
12 # -- Usage :
13 # -- Rate within a given PD :
14 # -- ./PDRates.py --runNumber 135525 --PD Mu
15 # -- Plot the rate of a PD versus the LS number:
16 # -- ./PDRates.py --runNumber 135525 --PD Mu --perLS
17 # -- Rates in all PDs:
18 # -- ./PDRates.py --runNumber 135525
19 # --
20 
21 
22 parser = OptionParser(usage="Example: ./PDRates.py --runNumber 146644 --PD Mu --perLS")
23 parser.add_option("--runNumber", dest="RunNumber", help="run number", type="int", metavar="RunNumber")
24 parser.add_option("--PD",dest="PrimaryDataset",help="PD name",metavar="PrimaryDataset")
25 parser.add_option("--perLS",action="store_true",default=False,dest="perLS",help="plot the rate vs the LS number",metavar="perLS")
26 parser.add_option("--logy",action="store_true",default=False,dest="logy",help="log scale for the y axis",metavar="logy")
27 parser.add_option("--saveplot",action="store_true",default=False,dest="saveplot",help="save the plot as a tmp.eps",metavar="saveplot")
28 (options, args) = parser.parse_args()
29 
30 
31 def PrimaryDatasets(Run):
32  # -- List of Primary Datasets:
33 
34  dbs_cmd = """ dbs search --query='find primds.name
35  where run=%d and dataset like */RAW' """ % (Run)
36  rows = commands.getoutput(dbs_cmd)
37  lines = rows.split("\n")
38  j=0
39  print("\nThe primary datasets for this run are: \n")
40  for Line in lines:
41  j=j+1
42  if j <=4:
43  continue
44  print(Line)
45  line=Line.split()
46  Datasets.append(line[0])
47  print(" ")
48 
49 
50 
51 def RateInPD(Run,PrimaryDataset,lsMin,lsMax,printLS=False):
52  dbs_cmd = """ dbs search --query='find file,lumi,file.numevents, file.size
53  where run=%d and dataset like /%s/*/RAW
54  and lumi >= %d and lumi <= %d
55  and file.status=VALID '""" % (Run, PrimaryDataset,lsMin, lsMax)
56  rows = commands.getoutput(dbs_cmd)
57  lines = rows.split("\n")
58  j=0
59  LumiSections = []
60  Files = []
61  Evts = 0
62  Size = 0
63  LSinFile = {}
64  NumberOfLSInFile = {}
65  for Line in lines:
66  j=j+1
67  if j <=4:
68  continue
69  line=Line.split()
70  LS = line[1]
71  file = line[0]
72  Nevts = int(line[2])
73  size = int(line[3])
74  LSinFile[LS] = file
75  if LumiSections.count(LS) == 0:
76  LumiSections.append(LS)
77  if Files.count(file) == 0:
78  Files.append(file)
79  Evts += Nevts
80  Size += size
81  NumberOfLSInFile[file] =1
82  else:
83  NumberOfLSInFile[file] = NumberOfLSInFile[file]+1
84  RatePerLS[LS] = Nevts
85 
86  Number_of_LS = len(LumiSections)
87  LS_Length = 23.3
88  if Run < 125100:
89  LS_Length = 93.3
90  rate = Evts / (Number_of_LS * LS_Length)
91  if Evts > 0:
92  size_per_event = (Size / Evts) / 1000.
93  else:
94  size_per_event=-1
95  print("Rate in \t",PrimaryDataset,"\t is : \t",rate," Hz", " \t size is : \t",size_per_event, "kB / event ")
96 
97  lsmin=9999999
98  lsmax=-1
99  for (LS,file) in six.iteritems(LSinFile):
100  nls = NumberOfLSInFile[file]
101  RatePerLS[LS] = RatePerLS[LS] / nls
102  RatePerLS[LS] = RatePerLS[LS] / LS_Length
103  if int(LS) > lsmax:
104  lsmax=int(LS)
105  if int(LS) < lsmin:
106  lsmin=int(LS)
107  if printLS:
108  print("lsmin lsmax",lsmin,lsmax)
109  for ls in range(lsmin,lsmax):
110  if not repr(ls) in RatePerLS.keys():
111  RatePerLS[LS] = 0
112  print("Missing LS ",ls)
113 
114 
115 if __name__ == "__main__":
116 
117  if not options.RunNumber:
118  print("wrong usage")
119  exit(2)
120 
121  lsMin = -1
122  lsMax = 999999
123 
124 # -- does not work yet.
125 # if options.lsMin:
126 # lsMin = options.lsMin
127 # if options.lsMax:
128 # lsMax = options.lsMax
129 
130 
131  print("\nRun Number: ",options.RunNumber)
132  if options.PrimaryDataset:
133  Run = options.RunNumber
134  PrimaryDataset = options.PrimaryDataset
135  RatePerLS = {}
136  if not options.perLS:
137  RateInPD(Run,PrimaryDataset,lsMin, lsMax, False)
138  if options.perLS:
139  average = 0
140  nLS_within_range = 0
141  RateInPD(Run,PrimaryDataset,lsMin, lsMax, True)
142  RatesTmp = open("rates_tmp.txt","w")
143  #RatesTmpSort = open("rates_tmp_sort.txt","w")
144  for (LS, rate) in six.iteritems(RatePerLS):
145  RatesTmp.write(LS+"\t"+repr(rate)+"\n")
146  #if int(LS) >= lsMin and int(LS) <= lsMax:
147  #nLS_within_range =nLS_within_range +1
148  #average = average + rate
149  #print "Average rate within ",options.lsMin," and ",options.lsMax," is: ",average/nLS_within_range
150  #if os.path.exists("./rates_tmp_sort.txt"):
151  #os.system("rm rates_tmp_sort.txt")
152  #os.system("sort -n rates_tmp.txt > rates_tmp_sort.txt")
153  RatesTmp.close()
154 
155  TempFile = open("tmp.gnuplot.txt","w")
156  if options.logy:
157  TempFile.write("set logscale y \n")
158  if options.saveplot:
159  TempFile.write("set term postscript eps enhanced \n")
160  TempFile.write("set output \"tmp.eps\" \n")
161  st_title = " \"Rates in PrimaryDataset " + PrimaryDataset+ " in Run " + repr(Run)+ "\" "
162  TempFile.write("set title " + st_title + "\n")
163  TempFile.write(" set pointsize 2. \n")
164  TempFile.write(" set nokey \n")
165  TempFile.write(" set xlabel \"LS number\" \n")
166  TempFile.write(" set ylabel \"Rate (Hz)\" \n")
167  TempFile.write(" plot \"rates_tmp.txt\" using 1:2 title 'Rate per LS' \n")
168  if not options.saveplot:
169  TempFile.write(" pause -1")
170  else:
171  print("The plot is saved under tmp.eps")
172  TempFile.close()
173 
174  os.system("gnuplot tmp.gnuplot.txt")
175 
176 
177  else:
178  Run = options.RunNumber
179  Datasets = []
180  PrimaryDatasets(Run)
181  for PrimaryDataset in Datasets:
182  RatePerLS = {}
183  RateInPD(Run,PrimaryDataset,lsMin,lsMax)
184 
185 
186 
def RateInPD(Run, PrimaryDataset, lsMin, lsMax, printLS=False)
Definition: PDRates.py:51
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def PrimaryDatasets(Run)
Definition: PDRates.py:31