test
CMS 3D CMS Logo

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