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