forked from cms-tsg-fog/RateMon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotTriggerRates.py
More file actions
281 lines (260 loc) · 13.9 KB
/
plotTriggerRates.py
File metadata and controls
281 lines (260 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#######################################################
# File: plotTriggerRates.py
# Author: Nathaniel Carl Rupprecht, Charlie Mueller
# Date Created: June 19, 2015
#
# Dependencies: RateMonitor.py DBParser.py
#
# Data Type Key:
# { a, b, c, ... } -- denotes a tuple
# [ key ] <object> -- denotes a dictionary of keys associated with objects
# ( object ) -- denotes a list of objects
#######################################################
# Imports
import getopt # For getting command line options
# Import the RateMonitor object
from DBParser import *
from RateMonitorNCR import *
#from RateMonitorNCR_multifit import *
## ----------- End Imports ------------ #
# Class MonitorController:
# Parsers command line input and uses it to set the parameters of an instance of RateMonitor which it contains. It then runs rateMonitor.
class MonitorController:
# Default constructor for Monitor Controller class
def __init__(self):
self.rateMonitor = RateMonitor()
# Use: Parses arguments from the command line and sets class variables
# Returns: True if parsing was successful, False if not
def parseArgs(self):
# Get the command line arguments
try:
opt, args = getopt.getopt(sys.argv[1:],"",["fitFile=", "triggerList=", "runList=", "jsonFile=",
"runFile=","saveDirectory=","sigma=", "preferLinear=",
"Secondary", "updateOnlineFits", "All", "Raw", "Help", "createFit",
"debugFitter", "nonLinear","vsInstLumi",
"L1Triggers", "AllTriggers","datasetRate", "L1ARate", "streamRate", "streamBandwidth",
"streamSize", "useFills"])
except:
print "Error geting options: command unrecognized. Exiting."
return False
if len(opt) == 0 and len(args) == 0:
self.printOptions()
return False
# Process Options
self.rateMonitor.ops = opt
for label, op in opt:
if label == "--Secondary":
self.rateMonitor.certifyMode = True # Run in secondary mode
self.rateMonitor.fit = False # We don't make fits in secondary mode
self.rateMonitor.useFit = True
self.rateMonitor.L1Triggers = True
elif label == "--fitFile":
self.rateMonitor.fitFile = str(op)
print "Using fit file:", self.rateMonitor.fitFile
elif label == "--runList" or label == "--runFile":
self.rateMonitor.runFile = str(op)
print "Using the runs in file", self.rateMonitor.runFile
self.loadRunsFromFile()
elif label == "--jsonFile":
self.rateMonitor.jsonFilter = True
self.rateMonitor.jsonFile = str(op)
elif label == "--Help":
self.printOptions()
return False
elif label == "--sigma":
self.rateMonitor.sigmas = float(op)
#elif label == "--preferLinear":
#self.rateMonitor.fitFinder.preferLinear = float(op)
elif label == "--All":
self.rateMonitor.processAll = True
elif label == "--Raw":
self.rateMonitor.varY = "rawRate"
elif label == "--saveDirectory":
self.rateMonitor.saveDirectory = str(op)
elif label == "--triggerList":
self.loadTriggersFromFile(str(op))
self.rateMonitor.useTrigList = True
self.rateMonitor.L1Triggers = True
elif label == "--L1Triggers":
self.rateMonitor.L1Triggers = True
self.rateMonitor.HLTTriggers = False
elif label == "--AllTriggers":
self.rateMonitor.L1Triggers = True
elif label == "--updateOnlineFits":
self.rateMonitor.updateOnlineFits = True
elif label == "--createFit":
if not self.rateMonitor.certifyMode: self.rateMonitor.fit = True
else: print "We do not create fits in secondary mode"
elif label == "--debugFitter":
self.rateMonitor.fitFinder.saveDebug = True
elif label == "--nonLinear":
self.rateMonitor.fitFinder.forceLinear = False
elif label == "--vsInstLumi":
self.rateMonitor.pileUp = False
elif label == "--streamRate":
self.rateMonitor.labelY = "rate [Hz]"
self.rateMonitor.plotStreams = True
self.rateMonitor.dataCol = 0
elif label == "--streamSize":
self.rateMonitor.labelY = "stream size [bytes]"
self.rateMonitor.plotStreams = True
self.rateMonitor.dataCol = 1
elif label == "--streamBandwidth":
self.rateMonitor.labelY = "stream bandwidth [bytes]"
self.rateMonitor.dataCol = 2
self.rateMonitor.plotStreams = True
elif label == "--datasetRate":
self.rateMonitor.labelY = "primary dataset rate [Hz]"
self.rateMonitor.plotDatasets = True
self.rateMonitor.dataCol = 0
elif label == "--L1ARate":
self.rateMonitor.labelY = "L1Physics rate [Hz]"
self.rateMonitor.plotL1ARate = True
self.rateMonitor.dataCol = 0
elif label == "--useFills":
self.rateMonitor.useFills = True
else:
print "Unimplemented option '%s'." % label
return False
# Process Arguments
if len(args) > 0: # There are arguments to look at
for item in args:
if item.find('-') != -1:
rng = item.split('-')
if len(rng) != 2:
print "Error: Invalid range format."
return False
else: # Add the runs in the range to the run list
try:
for r in range(int(rng[0]), int(rng[1])+1):
if not int(r) in self.rateMonitor.runList:
self.rateMonitor.runList.append(int(r))
except:
print "Error: Could not parse run range"
return False
elif self.rateMonitor.useFills:
try:
if not int(item) in self.rateMonitor.fillList:
self.rateMonitor.fillList.append(int(item))
except:
print "Error: Could not parse fill arguments."
return False
else: # Not a range, but a single run
try:
if not int(item) in self.rateMonitor.runList:
self.rateMonitor.runList.append(int(item))
except:
print "Error: Could not parse run arguments."
return False
# If no runs were specified, we cannot run rate monitoring
if len(self.rateMonitor.runList) == 0 and len(self.rateMonitor.fillList) == 0:
print "Error: No runs/fills were specified."
return False
# If no fit file was specified, don't try to make a fit
if self.rateMonitor.fitFile == "" and self.rateMonitor.certifyMode:
print "Fit file needed for certification. Exiting."
exit(0)
if self.rateMonitor.fitFile == "":
self.rateMonitor.useFit = False
# Check JSON file exists
if self.rateMonitor.jsonFilter:
if not os.path.exists(self.rateMonitor.jsonFile):
print "The specifed JSON file does not exist. Exiting."
exit(0)
return True
# Use: Prints out all the possible options that you can specify in the command line
# Returns: (void)
def printOptions(self):
print ""
print "Usage: python plotTriggerRates.py [Options] <list of runs (optional)>"
print "\n<list of runs> : Either single runs (like '10000 10003 10007'), or ranges (like '10001-10003')"
print ""
print "OPTIONS:"
print "\n--Help : Prints this display"
#print "\nFile Options:"
print "--fitFile=<name> : Loads fit information from the file named <name>."
print "--jsonFile=<name> : Filter runs and lumisections according the to provided JSON file."
print "--triggerList=<name> : Loads a list of triggers to process from the file <name>. We will only process the triggers listed in triggerfiles."
#print "\nRun Options:"
print "--Secondary : Run the program in 'secondary mode,' making plots of raw rate vs lumisection."
# print "\nFitting Options:"
print "--createFit : Make a fit for the data we plot. Only a primary mode feature."
print "--sigma=<num> : The acceptable tolerance for the fit. default is 3 sigma"
# print "--debugFitter : Creates a root file showing all the points labeled as good and bad when doing the fit"
print "--nonLinear : Forces fits to be nonLinear"
print "--vsInstLumi : Plot rates vs inst. lumi"
print "--L1Triggers : ONLY L1 triggers are plotted for the runs."
print "--AllTriggers : Both L1 and HLT triggers are plotted for the runs."
# print "--preferLinear=<num> : If the MSE for the linear fit is less then <num> worse then the best fit, we will use the linear fit."
print "--streamRate : Plots the stream rate vs inst lumi."
print "--streamSize : Plots the stream size vs inst lumi."
#print "--streamBandwidth : Plots the stream bandwidth vs inst lumi."
print "--datasetRate : Plots the PD rate vs inst lumi."
# print "\nCut/Normalization Options:"
#print " we skip that run. This overrides that functionality."
print ""
print "EXAMPLES:"
print ""
print "fit making mode:\n python plotTriggerRates.py --createFit --triggerList=monitorlist_COLLISIONS.list 251643 251638 251883 251244 251562\n"
print "certification mode:\n python plotTriggerRates.py --Secondary --triggerList=monitorlist_COLLISIONS.list --fitFile=Fits/2016/FOG.pkl 276437 276453 276454 276455 276456 276457 276458"
#print "You can specify runs by typing them in the form <run1> (single runs), or <run2>-<run3> (ranges), or both. Do this after all other arguments"
#print "Multiple runFiles can be specified, and you can add more runs to the run list by specifying them on the command line as described in the above line."
print ""
# Use: Opens a file containing a list of runs and adds them to the RateMonitor class's run list
# Note: We do not clear the run list, this way we could add runs from multiple files to the run list
# Arguments:
# -- fileName (Default=None): The name of the file that runs are contained in
# Returns: (void)
def loadRunsFromFile(self, fileName = None):
# Use self.fileName as the default fileName
if fileName == None:
fileName = self.rateMonitor.runFile
try:
file = open(fileName, 'r')
except:
print "File", fileName, "(a run list file) failed to open."
return
# Load all the runs. There should only be run numbers and whitespaces in the run list file
allRunNumbers = file.read().split() # Get all the numbers on the line, no argument -> split on any whitespace
for run in allRunNumbers:
# Check if the run is a range
if run.find('-') != -1:
try:
start, end = run.split('-')
for rn in range(start, end+1):
if not int(run) in self.rateMonitor.runList:
self.rateMonitor.runList.append(int(rn))
except:
print "Range specified in file", fileName, "could not be parsed."
else:
try:
if not int(run) in self.rateMonitor.runList:
self.rateMonitor.runList.append(int(run))
except:
print "Error in parsing run in file", fileName
# Use: Opens a file containing a list of trigger names and adds them to the RateMonitor class's trigger list
# Note: We do not clear the trigger list, this way we could add triggers from multiple files to the trigger list
# -- fileName: The name of the file that trigger names are contained in
# Returns: (void)
def loadTriggersFromFile(self, fileName):
try:
file = open(fileName, 'r')
except:
print "File", fileName, "(a trigger list file) failed to open."
return
allTriggerNames = file.read().split() # Get all the words, no argument -> split on any whitespace
for triggerName in allTriggerNames:
try:
if not str(triggerName) in self.rateMonitor.TriggerList:
self.rateMonitor.TriggerList.append(stripVersion(str(triggerName)))
except:
print "Error parsing trigger name in file", fileName
# Use: Runs the rateMonitor object using parameters supplied as command line arguments
# Returns: (void)
def run(self):
if self.parseArgs(): self.rateMonitor.runBatch()
## ----------- End of class MonitorController ------------ #
## ----------- Main -----------##
if __name__ == "__main__":
controller = MonitorController()
controller.run()