ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/RateMonShiftTool_dev/DatabaseRatePredictor.py
Revision: 1.57
Committed: Tue Sep 11 16:53:43 2012 UTC (12 years, 7 months ago) by awoodard
Content type: text/x-python
Branch: MAIN
Changes since 1.56: +8 -7 lines
Log Message:
Fixing sigma calc in secondary mode to account for possibility num_ls != 1

File Contents

# User Rev Content
1 grchrist 1.3 #!/usr/bin/env python
2    
3 abrinke1 1.1 from DatabaseParser import *
4 abrinke1 1.5 from GetListOfRuns import *
5 abrinke1 1.1 import sys
6     import os
7     from numpy import *
8     import pickle
9 amott 1.18 import getopt
10 awoodard 1.47 from StreamMonitor import StreamMonitor
11 grchrist 1.29
12 abrinke1 1.1 from ROOT import gROOT, TCanvas, TF1, TGraph, TGraphErrors, TPaveStats, gPad, gStyle
13 amott 1.18 from ROOT import TFile, TPaveText, TBrowser
14 abrinke1 1.1 from ROOT import gBenchmark
15     import array
16     import math
17 grchrist 1.8 from ReadConfig import RateMonConfig
18 muell149 1.46 from TablePrint import *
19 abrinke1 1.5 from selectionParser import selectionParser
20    
21 amott 1.18 def usage():
22     print sys.argv[0]+" [options] <list of runs>"
23     print "This script is used to generate fits and do secondary shifter validation"
24     print "<list of runs> this is a list of the form: a b c-d e f-g, specifying individual runs and/or run ranges"
25     print " be careful with using ranges (c-d), it is highly recommended to use a JSON in this case"
26     print "options: "
27     print "--makeFits run in fit making mode"
28     print "--secondary run in secondary shifter mode"
29 grchrist 1.28 print "--TMD put in TMD predictions"
30 amott 1.18 print "--fitFile=<path> path to the fit file"
31     print "--json=<path> path to the JSON file"
32     print "--TriggerList=<path> path to the trigger list (without versions!)"
33 grchrist 1.26 print "--maxdt=<max deadtime> Mask LS above max deadtime threshold"
34     print "--All Mask LS with any red LS on WBM LS page (not inc castor zdc etc)"
35     print "--Mu Mask LS with Mu off"
36     print "--HCal Mask LS with HCal barrel off"
37     print "--Tracker Mask LS with Tracker barrel off"
38     print "--ECal Mask LS with ECal barrel off"
39     print "--EndCap Mask LS with EndCap sys off, used in combination with other subsys"
40     print "--Beam Mask LS with Beam off"
41 grchrist 1.34 print "--NoVersion Ignore version numbers"
42 grchrist 1.35 print "--linear Force Linear fits"
43 grchrist 1.38 print "--inst Fits using inst not delivered"
44     print "--TMDerr Use errors from TMD predictions"
45 muell149 1.46 print "--write Writes fit info into csv, for ranking nonlinear triggers"
46 awoodard 1.50 print "--AllTriggers Run for all triggers instead of specifying a trigger list"
47 muell149 1.46
48 amott 1.18 class Modes:
49 grchrist 1.29 none,fits,secondary = range(3)
50    
51     def pickYear():
52     global thisyear
53 grchrist 1.32 thisyear="2012"
54 grchrist 1.29 print "Year set to ",thisyear
55    
56 amott 1.18
57 abrinke1 1.1 def main():
58 amott 1.18 try:
59 grchrist 1.29
60 grchrist 1.32 ##set year to 2012
61 grchrist 1.29 pickYear()
62 awoodard 1.47
63 grchrist 1.22 try:
64 awoodard 1.50 opt, args = getopt.getopt(sys.argv[1:],"",["makeFits","secondary","fitFile=","json=","TriggerList=","maxdt=","All","Mu","HCal","Tracker","ECal","EndCap","Beam","NoVersion","linear","inst","TMDerr","write","AllTriggers"])
65 grchrist 1.22
66     except getopt.GetoptError, err:
67     print str(err)
68     usage()
69     sys.exit(2)
70    
71 grchrist 1.21 ## if len(args)<1:
72     ## print "\nPlease specify at least 1 run to look at\n"
73     ## usage()
74     ## sys.exit(0)
75 amott 1.18
76 grchrist 1.25
77     ##### RUN LIST ########
78 grchrist 1.22 run_list=[]
79 grchrist 1.23
80 grchrist 1.22 if len(args)<1:
81     inputrunlist=[]
82     print "No runs specified"
83     runinput=raw_input("Enter run range in form <run1> <run2> <run3> or <run1>-<run2>:")
84     inputrunlist.append(runinput)
85    
86    
87     if runinput.find(' ')!=-1:
88     args=runinput.split(' ')
89     else:
90     args.append(runinput)
91    
92 grchrist 1.23
93    
94 grchrist 1.22 for r in args:
95     if r.find('-')!=-1: # r is a run range
96     rrange = r.split('-')
97     if len(rrange)!=2:
98     print "Invalid run range %s" % (r,)
99     sys.exit(0)
100     try:
101     for rr in range(int(rrange[0]),int(rrange[1])+1):
102     run_list.append(rr)
103     except:
104     print "Invalid run range %s" % (r,)
105     sys.exit(0)
106     else: # r is not a run range
107     try:
108     run_list.append(int(r))
109     except:
110     print "Invalid run %s" % (r,)
111 grchrist 1.26
112 grchrist 1.21
113 grchrist 1.25 ##### READ CMD LINE ARGS #########
114 grchrist 1.22 mode = Modes.none
115     fitFile = ""
116     jsonfile = ""
117     trig_list = []
118 grchrist 1.25 max_dt=-1.0
119     subsys=-1.0
120 grchrist 1.34 NoVersion=False
121 grchrist 1.35 linear=False
122 grchrist 1.38 do_inst=False
123     TMDerr=False
124 muell149 1.46 wp_bool=False
125 awoodard 1.50 all_triggers=False
126 grchrist 1.25 SubSystemOff={'All':False,'Mu':False,'HCal':False,'ECal':False,'Tracker':False,'EndCap':False,'Beam':False}
127 grchrist 1.22 for o,a in opt:
128     if o == "--makeFits":
129     mode = Modes.fits
130     elif o == "--secondary":
131     mode = Modes.secondary
132     elif o == "--fitFile":
133     fitFile = str(a)
134     elif o == "--json":
135     jsonfile = a
136 grchrist 1.26 elif o=="--maxdt":
137     max_dt = float(a)
138 grchrist 1.25 elif o=="--All":
139     subsys=1
140     SubSystemOff["All"]=True
141     elif o=="--Mu":
142     subsys=1
143     SubSystemOff["Mu"]=True
144     elif o=="--HCal":
145     SubSystemOff["HCal"]=True
146     subsys=1
147     elif o=="--Tracker":
148     SubSystemOff["Tracker"]=True
149     subsys=1
150 grchrist 1.26 elif o=="--ECal":
151     SubSystemOff["ECal"]=True
152     subsys=1
153 grchrist 1.25 elif o=="--EndCap":
154     SubSystemOff["EndCap"]=True
155     subsys=1
156     elif o=="--Beam":
157     SubSystemOff["Beam"]=True
158     subsys=1
159 grchrist 1.34 elif o=="--NoVersion":
160     NoVersion=True
161 grchrist 1.35 elif o=="--linear":
162     linear=True
163 grchrist 1.38 elif o=="--inst":
164     do_inst=True
165     elif o=="--TMDerr":
166     TMDerr=True
167 muell149 1.46 elif o=="--write":
168     wp_bool=True
169 awoodard 1.50 elif o=="--AllTriggers":
170     all_triggers=True
171 grchrist 1.22 elif o == "--TriggerList":
172     try:
173     f = open(a)
174     for entry in f:
175     if entry.startswith('#'):
176     continue
177     if entry.find(':')!=-1:
178     entry = entry[:entry.find(':')] ## We can point this to the existing monitor list, just remove everything after ':'!
179     if entry.find('#')!=-1:
180     entry = entry[:entry.find('#')] ## We can point this to the existing monitor list, just remove everything after ':'!
181     trig_list.append( entry.rstrip('\n'))
182     except:
183     print "\nInvalid Trigger List\n"
184     sys.exit(0)
185     else:
186     print "\nInvalid Option %s\n" % (str(o),)
187     usage()
188     sys.exit(2)
189    
190     print "\n\n"
191 grchrist 1.25 ###### MODES #########
192    
193 grchrist 1.22 if mode == Modes.none: ## no mode specified
194     print "\nNo operation mode specified!\n"
195     modeinput=raw_input("Enter mode, --makeFits or --secondary:")
196     print "modeinput=",modeinput
197     if not (modeinput=="--makeFits" or modeinput=="--secondary"):
198     print "not either"
199     usage()
200 amott 1.18 sys.exit(0)
201 grchrist 1.22 elif modeinput == "--makeFits":
202     mode=Modes.fits
203     elif modeinput =="--secondary":
204     mode=Modes.secondary
205     else:
206     print "FATAL ERROR: No Mode specified"
207 amott 1.18 sys.exit(0)
208 grchrist 1.22
209     if mode == Modes.fits:
210     print "Running in Fit Making mode\n\n"
211     elif mode == Modes.secondary:
212     print "Running in Secondary Shifter mode\n\n"
213     else: ## should never get here, but exit if we do
214 grchrist 1.21 print "FATAL ERROR: No Mode specified"
215     sys.exit(0)
216 grchrist 1.22
217     if fitFile=="" and not mode==Modes.fits:
218     print "\nPlease specify fit file. These are available:\n"
219 grchrist 1.29 path="Fits/%s/" % (thisyear) # insert the path to the directory of interest
220 grchrist 1.22 dirList=os.listdir(path)
221     for fname in dirList:
222     print fname
223 grchrist 1.24 fitFile = path+raw_input("Enter fit file in format Fit_HLT_10LS_Run176023to180252.pkl: ")
224    
225 grchrist 1.21 ##usage()
226     ##sys.exit(0)
227 grchrist 1.22 elif fitFile=="":
228 grchrist 1.39 NoVstr=""
229     if NoVersion:
230     NoVstr="NoV_"
231 grchrist 1.38 if not do_inst:
232 grchrist 1.39 fitFile="Fits/%s/Fit_HLT_%s10LS_Run%sto%s.pkl" % (thisyear,NoVstr,min(run_list),max(run_list))
233 grchrist 1.38 else:
234 grchrist 1.39 fitFile="Fits/%s/Fit_inst_HLT_%s10LS_Run%sto%s.pkl" % (thisyear,NoVstr,min(run_list),max(run_list))
235 grchrist 1.26
236 grchrist 1.39 if "NoV" in fitFile:
237     NoVersion=True
238    
239 grchrist 1.25
240     ###### TRIGGER LIST #######
241 grchrist 1.24
242 awoodard 1.50 if trig_list == [] and not all_triggers:
243 grchrist 1.22
244     print "\nPlease specify list of triggers\n"
245     print "Available lists are:"
246     dirList=os.listdir(".")
247     for fname in dirList:
248     entry=fname
249     if entry.find('.')!=-1:
250     extension = entry[entry.find('.'):] ## We can point this to the existing monitor list, just remove everything after ':'!
251     if extension==".list":
252     print fname
253 grchrist 1.26 trig_input=raw_input("\nEnter triggers in format HLT_IsoMu30_eta2p1 or a .list file: ")
254 grchrist 1.21
255 grchrist 1.22 if trig_input.find('.')!=-1:
256     extension = trig_input[trig_input.find('.'):]
257 grchrist 1.21 if extension==".list":
258 grchrist 1.22 try:
259     fl=open(trig_input)
260     except:
261     print "Cannot open file"
262     usage()
263     sys.exit(0)
264 grchrist 1.21
265 grchrist 1.22 for line in fl:
266     if line.startswith('#'):
267     continue
268     if len(line)<1:
269     continue
270 grchrist 1.21
271 grchrist 1.22 if len(line)>=2:
272     arg=line.rstrip('\n').rstrip(' ').lstrip(' ')
273     trig_list.append(arg)
274     else:
275     arg=''
276     else:
277     trig_list.append(trig_input)
278 grchrist 1.21
279 abrinke1 1.5 ## Can use any combination of LowestRunNumber, HighestRunNumber, and NumberOfRuns -
280     ## just modify "ExistingRuns.sort" and for "run in ExistingRuns" accordingly
281    
282 grchrist 1.22 if jsonfile=="":
283     JSON=[]
284     else:
285     print "Using JSON: %s" % (jsonfile,)
286     JSON = GetJSON(jsonfile) ##Returns array JSON[runs][ls_list]
287 abrinke1 1.5
288 grchrist 1.21
289 abrinke1 1.5
290 grchrist 1.22 ###### TO CREATE FITS #########
291     if mode == Modes.fits:
292     trig_name = "HLT"
293 grchrist 1.34 num_ls = 10
294 grchrist 1.22 physics_active_psi = True ##Requires that physics and active be on, and that the prescale column is not 0
295     #JSON = [] ##To not use a JSON file, just leave the array empty
296     debug_print = False
297     no_versions=False
298 grchrist 1.34 min_rate = 0.0
299 grchrist 1.22 print_table = False
300     data_clean = True ##Gets rid of anomalous rate points, reqires physics_active_psi (PAP) and deadtime < 20%
301     ##plot_properties = [varX, varY, do_fit, save_root, save_png, fit_file]
302 grchrist 1.38 if not do_inst:
303     plot_properties = [["delivered", "rate", True, True, False, fitFile]]
304 awoodard 1.54 # plot_properties = [["delivered", "rawrate", True, True, False, fitFile]]
305 grchrist 1.38 else:
306     plot_properties = [["inst", "rate", True, True, False, fitFile]]
307 amott 1.18
308 grchrist 1.34 masked_triggers = ["AlCa_", "DST_", "HLT_L1", "HLT_Zero"]
309 grchrist 1.22 save_fits = True
310 grchrist 1.25 if max_dt==-1.0:
311     max_dt=0.08 ## no deadtime cutuse 2.0
312 grchrist 1.22 force_new=True
313     print_info=True
314 grchrist 1.25 if subsys==-1.0:
315     SubSystemOff={'All':False,'Mu':False,'HCal':False,'ECal':False,'Tracker':False,'EndCap':False,'Beam':True}
316 grchrist 1.22
317    
318     ###### TO SEE RATE VS PREDICTION ########
319     if mode == Modes.secondary:
320     trig_name = "HLT"
321     num_ls = 1
322     physics_active_psi = True
323     debug_print = False
324     no_versions=False
325 grchrist 1.34 min_rate = 0.0
326 grchrist 1.22 print_table = False
327     data_clean = True
328     ##plot_properties = [varX, varY, do_fit, save_root, save_png, fit_file]
329 awoodard 1.52 plot_properties = [["ls", "rawrate", False, True, False,fitFile]]
330     ## rate is calculated as: (measured rate, deadtime corrected) * prescale [prediction not dt corrected]
331     ## rawrate is calculated as: measured rate [prediction is dt corrected]
332 grchrist 1.22
333 grchrist 1.34 masked_triggers = ["AlCa_", "DST_", "HLT_L1", "HLT_Zero"]
334 grchrist 1.22 save_fits = False
335 grchrist 1.25 if max_dt==-1.0:
336     max_dt=2.0 ## no deadtime cut=2.0
337 grchrist 1.22 force_new=True
338     print_info=True
339 grchrist 1.25 if subsys==-1.0:
340     SubSystemOff={'All':True,'Mu':False,'HCal':False,'ECal':False,'Tracker':False,'EndCap':False,'Beam':True}
341 grchrist 1.13
342 grchrist 1.26 for k in SubSystemOff.iterkeys():
343     print k,"=",SubSystemOff[k]," ",
344     print " "
345 grchrist 1.33
346 grchrist 1.22 ######## END PARAMETERS - CALL FUNCTIONS ##########
347 awoodard 1.50 [Rates,LumiPageInfo]= GetDBRates(run_list, trig_name, trig_list, num_ls, max_dt, physics_active_psi, JSON, debug_print, force_new, SubSystemOff,NoVersion,all_triggers)
348     rootFileName = MakePlots(Rates, LumiPageInfo, run_list, trig_name, trig_list, num_ls, min_rate, max_dt, print_table, data_clean, plot_properties, masked_triggers, save_fits, debug_print,SubSystemOff, print_info,NoVersion, linear, do_inst, TMDerr,wp_bool,all_triggers)
349 awoodard 1.47
350 grchrist 1.22 except KeyboardInterrupt:
351     print "Wait... come back..."
352 abrinke1 1.1
353 awoodard 1.47
354    
355    
356 awoodard 1.50 def GetDBRates(run_list,trig_name,trig_list, num_ls, max_dt, physics_active_psi,JSON,debug_print, force_new, SubSystemOff,NoVersion,all_triggers):
357 abrinke1 1.1
358     Rates = {}
359 grchrist 1.10 LumiPageInfo={}
360 abrinke1 1.5 ## Save in RefRuns with name dependent on trig_name, num_ls, JSON, and physics_active_psi
361     if JSON:
362 amott 1.18 #print "Using JSON file"
363 abrinke1 1.5 if physics_active_psi:
364 grchrist 1.29 RefRunNameTemplate = "RefRuns/%s/Rates_%s_%sLS_JPAP.pkl"
365 abrinke1 1.5 else:
366 grchrist 1.29 RefRunNameTemplate = "RefRuns/%s/Rates_%s_%sLS_JSON.pkl"
367 abrinke1 1.5 else:
368 grchrist 1.14 print "Using Physics and Active ==1"
369 abrinke1 1.5 if physics_active_psi:
370 grchrist 1.29 RefRunNameTemplate = "RefRuns/%s/Rates_%s_%sLS_PAP.pkl"
371 abrinke1 1.5 else:
372 grchrist 1.29 RefRunNameTemplate = "RefRuns/%s/Rates_%s_%sLS.pkl"
373 grchrist 1.10
374    
375 grchrist 1.29 RefRunFile = RefRunNameTemplate % (thisyear,trig_name,num_ls)
376     RefRunFileHLT = RefRunNameTemplate % (thisyear,"HLT",num_ls)
377 grchrist 1.10
378     print "RefRun=",RefRunFile
379     print "RefRunFileHLT",RefRunFileHLT
380 grchrist 1.11 if not force_new:
381     try: ##Open an existing RefRun file with the same parameters and trigger name
382     pkl_file = open(RefRunFile, 'rb')
383     Rates = pickle.load(pkl_file)
384     pkl_file.close()
385     os.remove(RefRunFile)
386     print "using",RefRunFile
387    
388    
389 abrinke1 1.5 except:
390 grchrist 1.11 try: ##Open an existing RefRun file with the same parameters and HLT for trigger name
391     pkl_file = open(RefRunFileHLT)
392     HLTRates = pickle.load(pkl_file)
393     for key in HLTRates:
394     if trig_name in str(key):
395     Rates[key] = HLTRates[key]
396     #print str(RefRunFile)+" does not exist. Creating ..."
397     except:
398     print str(RefRunFile)+" does not exist. Creating ..."
399 grchrist 1.10
400     ## try the lumis file
401 grchrist 1.29 RefLumiNameTemplate = "RefRuns/%s/Lumis_%s_%sLS.pkl"
402     RefLumiFile= RefLumiNameTemplate % (thisyear,"HLT",num_ls)
403 grchrist 1.11 if not force_new:
404     try:
405     pkl_lumi_file = open(RefLumiFile, 'rb')
406     LumiPageInfo = pickle.load(pkl_lumi_file)
407     pkl_lumi_file.close()
408     os.remove(RefLumiFile)
409     print "using",RefLumiFile
410     except:
411     print str(RefLumiFile)+" doesn't exist. Make it..."
412 grchrist 1.34
413    
414     trig_list_noV=[]
415     for trigs in trig_list:
416     trig_list_noV.append(StripVersion(trigs))
417 grchrist 1.40
418 grchrist 1.34 if NoVersion:
419     trig_list=trig_list_noV
420    
421 abrinke1 1.5 for RefRunNum in run_list:
422     if JSON:
423     if not RefRunNum in JSON:
424     continue
425 awoodard 1.47 try:
426 abrinke1 1.1 ExistsAlready = False
427     for key in Rates:
428     if RefRunNum in Rates[key]["run"]:
429     ExistsAlready = True
430     break
431 grchrist 1.10
432    
433     LumiExistsLAready=False
434     for v in LumiPageInfo.itervalues():
435     if RefRunNum == v["Run"]:
436     LumiExistsAlready=True
437     break
438     if ExistsAlready and LumiExistsAlready:
439 abrinke1 1.1 continue
440 grchrist 1.10
441    
442 abrinke1 1.1 except:
443     print "Getting info for run "+str(RefRunNum)
444    
445     if RefRunNum < 1:
446     continue
447 grchrist 1.30 ColRunNum,isCol,isGood = GetLatestRunNumber(RefRunNum)
448     if not isGood:
449     print "Run ",RefRunNum, " is not Collisions"
450    
451     continue
452 awoodard 1.50
453 grchrist 1.26 if not isCol:
454     print "Run ",RefRunNum, " is not Collisions"
455    
456     continue
457    
458 grchrist 1.17 print "calculating rates and green lumis for run ",RefRunNum
459 grchrist 1.10
460 abrinke1 1.5 if True: ##Placeholder
461 abrinke1 1.1 if True: #May replace with "try" - for now it's good to know when problems happen
462     RefParser = DatabaseParser()
463     RefParser.RunNumber = RefRunNum
464     RefParser.ParseRunSetup()
465 abrinke1 1.5 RefLumiRangePhysicsActive = RefParser.GetLSRange(1,9999) ##Gets array of all LS with physics and active on
466     RefLumiArray = RefParser.GetLumiInfo() ##Gets array of all existing LS and their lumi info
467 abrinke1 1.2 RefLumiRange = []
468 grchrist 1.17 RefMoreLumiArray = RefParser.GetMoreLumiInfo()#dict with keys as bits from lumisections WBM page and values are dicts with key=LS:value=bit
469 amott 1.18
470 abrinke1 1.5 for iterator in RefLumiArray[0]: ##Makes array of LS with proper PAP and JSON properties
471 grchrist 1.11 ##cheap way of getting PSCol None-->0
472 amott 1.18 if RefLumiArray[0][iterator] not in range(1,9):
473 grchrist 1.11 RefLumiArray[0][iterator]=0
474 grchrist 1.15
475 grchrist 1.11
476 grchrist 1.17 if not physics_active_psi or (RefLumiArray[5][iterator] == 1 and RefLumiArray[6][iterator] == 1 and RefMoreLumiArray["b1pres"][iterator]==1 and RefMoreLumiArray["b2pres"][iterator]==1 and RefMoreLumiArray["b1stab"][iterator] and RefMoreLumiArray["b2stab"][iterator]==1):
477 abrinke1 1.5 if not JSON or RefRunNum in JSON:
478     if not JSON or iterator in JSON[RefRunNum]:
479     RefLumiRange.append(iterator)
480 grchrist 1.15 #print iterator, RefLumiArray[0][iterator], "active=",RefLumiArray[5][iterator],"physics=",RefLumiArray[6][iterator]
481 grchrist 1.9 #print "hbhea=",RefMoreLumiArray['hbhea'][iterator]
482    
483 abrinke1 1.5 try:
484     nls = RefLumiRange[0]
485     LSRange = {}
486     except:
487     print "Run "+str(RefRunNum)+" has no good LS"
488     continue
489     if num_ls > len(RefLumiRange):
490 abrinke1 1.1 print "Run "+str(RefRunNum)+" is too short: from "+str(nls)+" to "+str(RefLumiRange[-1])+", while num_ls = "+str(num_ls)
491     continue
492     while nls < RefLumiRange[-1]-num_ls:
493 abrinke1 1.5 LSRange[nls] = []
494     counter = 0
495     for iterator in RefLumiRange:
496     if iterator >= nls and counter < num_ls:
497     LSRange[nls].append(iterator)
498     counter += 1
499 abrinke1 1.1 nls = LSRange[nls][-1]+1
500 abrinke1 1.5
501 grchrist 1.17 #print "Run "+str(RefRunNum)+" contains LS from "+str(min(LSRange))+" to "+str(max(LSRange))
502 grchrist 1.8 for nls in sorted(LSRange.iterkeys()):
503 abrinke1 1.1 TriggerRates = RefParser.GetHLTRates(LSRange[nls])
504 awoodard 1.47
505     ## Clumsy way to append Stream A. Should choose correct method for calculating stream a based on ps column used in data taking.
506 awoodard 1.57 if ('HLT_Stream_A' in trig_list) or all_triggers:
507     config = RateMonConfig(os.path.abspath(os.path.dirname(sys.argv[0])))
508     config.ReadCFG()
509     stream_mon = StreamMonitor()
510     core_a_rates = stream_mon.getStreamACoreRatesByLS(RefParser,LSRange[nls],config).values()
511     avg_core_a_rate = sum(core_a_rates)/len(LSRange[nls])
512     TriggerRates['HLT_Stream_A'] = [1,1,avg_core_a_rate,avg_core_a_rate]
513 abrinke1 1.5
514     [inst, live, delivered, dead, pscols] = RefParser.GetAvLumiInfo(LSRange[nls])
515 grchrist 1.17 deadtimebeamactive=RefParser.GetDeadTimeBeamActive(LSRange[nls])
516 awoodard 1.47
517 abrinke1 1.2 physics = 1
518     active = 1
519 abrinke1 1.5 psi = 99
520     for iterator in LSRange[nls]: ##Gets lowest value of physics, active, and psi in the set of lumisections
521 abrinke1 1.2 if RefLumiArray[5][iterator] == 0:
522     physics = 0
523     if RefLumiArray[6][iterator] == 0:
524     active = 0
525 abrinke1 1.5 if RefLumiArray[0][iterator] < psi:
526     psi = RefLumiArray[0][iterator]
527 abrinke1 1.2
528 grchrist 1.17 MoreLumiMulti=LumiRangeGreens(RefMoreLumiArray,LSRange,nls,RefRunNum,deadtimebeamactive)
529 grchrist 1.10
530 abrinke1 1.5 if inst < 0 or live < 0 or delivered < 0:
531     print "Run "+str(RefRunNum)+" LS "+str(nls)+" inst lumi = "+str(inst)+" live lumi = "+str(live)+", delivered = "+str(delivered)+", physics = "+str(physics)+", active = "+str(active)
532 abrinke1 1.2
533 grchrist 1.10
534    
535     LumiPageInfo[nls]=MoreLumiMulti
536    
537 abrinke1 1.1 for key in TriggerRates:
538 grchrist 1.34 if NoVersion:
539     name = StripVersion(key)
540     else:
541     name=key
542 grchrist 1.26 if not name in trig_list:
543 awoodard 1.50 if all_triggers:
544     trig_list.append(name)
545     else:
546     continue
547 awoodard 1.47
548 abrinke1 1.1 if not Rates.has_key(name):
549     Rates[name] = {}
550     Rates[name]["run"] = []
551     Rates[name]["ls"] = []
552     Rates[name]["ps"] = []
553     Rates[name]["inst_lumi"] = []
554     Rates[name]["live_lumi"] = []
555     Rates[name]["delivered_lumi"] = []
556     Rates[name]["deadtime"] = []
557     Rates[name]["rawrate"] = []
558     Rates[name]["rate"] = []
559     Rates[name]["rawxsec"] = []
560     Rates[name]["xsec"] = []
561 abrinke1 1.2 Rates[name]["physics"] = []
562     Rates[name]["active"] = []
563 abrinke1 1.5 Rates[name]["psi"] = []
564 grchrist 1.9
565    
566 abrinke1 1.1 [avps, ps, rate, psrate] = TriggerRates[key]
567     Rates[name]["run"].append(RefRunNum)
568     Rates[name]["ls"].append(nls)
569     Rates[name]["ps"].append(ps)
570     Rates[name]["inst_lumi"].append(inst)
571     Rates[name]["live_lumi"].append(live)
572     Rates[name]["delivered_lumi"].append(delivered)
573 grchrist 1.17 Rates[name]["deadtime"].append(deadtimebeamactive)
574 abrinke1 1.1 Rates[name]["rawrate"].append(rate)
575 abrinke1 1.2 if live == 0:
576 awoodard 1.56 Rates[name]["rate"].append(0.0)
577 abrinke1 1.2 Rates[name]["rawxsec"].append(0.0)
578     Rates[name]["xsec"].append(0.0)
579     else:
580 awoodard 1.56 try:
581     Rates[name]["rate"].append(psrate/(1.0-deadtimebeamactive))
582     except:
583     Rates[name]["rate"].append(0.0)
584 abrinke1 1.2 Rates[name]["rawxsec"].append(rate/live)
585     Rates[name]["xsec"].append(psrate/live)
586     Rates[name]["physics"].append(physics)
587     Rates[name]["active"].append(active)
588 abrinke1 1.5 Rates[name]["psi"].append(psi)
589 awoodard 1.47
590 grchrist 1.9
591 grchrist 1.10 #for keys, values in MoreLumiMulti.iteritems():
592     # Rates[name][keys].append(values)
593 grchrist 1.9 #print nls, name, keys, values
594 awoodard 1.47
595 grchrist 1.10 RateOutput = open(RefRunFile, 'wb') ##Save new Rates[] to RefRuns
596     pickle.dump(Rates, RateOutput, 2)
597     RateOutput.close()
598     LumiOutput = open(RefLumiFile,'wb')
599     pickle.dump(LumiPageInfo,LumiOutput, 2)
600     LumiOutput.close()
601    
602    
603     return [Rates,LumiPageInfo]
604 abrinke1 1.1
605 awoodard 1.50 def MakePlots(Rates, LumiPageInfo, run_list, trig_name, trig_list, num_ls, min_rate, max_dt, print_table, data_clean, plot_properties, masked_triggers, save_fits, debug_print, SubSystemOff, print_info,NoVersion, linear,do_inst, TMDerr,wp_bool,all_triggers):
606 abrinke1 1.1 min_run = min(run_list)
607     max_run = max(run_list)
608 muell149 1.46
609     priot.has_been_called=False
610 grchrist 1.11
611 abrinke1 1.5 InputFit = {}
612     OutputFit = {}
613 grchrist 1.14 first_trigger=True
614 abrinke1 1.1
615 grchrist 1.37
616     [[varX, varY, do_fit, save_root, save_png, fit_file]] = plot_properties
617    
618     RootNameTemplate = "%s_%sLS_%s_vs_%s_Run%s-%s.root"
619     RootFile = RootNameTemplate % (trig_name, num_ls, varX, varY, min_run, max_run)
620 abrinke1 1.1
621 amott 1.20 if not do_fit:
622     try:
623 abrinke1 1.1 pkl_file = open(fit_file, 'rb')
624 abrinke1 1.5 InputFit = pickle.load(pkl_file)
625 grchrist 1.40
626 amott 1.20 except:
627     print "ERROR: could not open fit file: %s" % (fit_file,)
628     if save_root:
629     try:
630     os.remove(RootFile)
631     except:
632     pass
633    
634 grchrist 1.38 trig_list_noV=[]
635     for trigs in trig_list:
636     trig_list_noV.append(StripVersion(trigs))
637     if NoVersion:
638     trig_list=trig_list_noV
639 grchrist 1.40
640 amott 1.20 ## check that all the triggers we ask to plot are in the input fit
641     if not save_fits:
642     goodtrig_list = []
643 grchrist 1.40 FitInputNoV={}
644 amott 1.20 for trig in trig_list:
645 grchrist 1.40
646     if NoVersion:
647     for trigger in InputFit.iterkeys():
648     FitInputNoV[StripVersion(trigger)]=InputFit[trigger]
649     InputFit=FitInputNoV
650 awoodard 1.56
651 amott 1.20 else:
652 grchrist 1.40 if not InputFit.has_key(trig):
653     print "WARNING: No Fit Prediction for Trigger %s, SKIPPING" % (trig,)
654     else:
655     goodtrig_list.append(trig)
656     trig_list = goodtrig_list
657 amott 1.18
658 grchrist 1.38
659 grchrist 1.34
660 grchrist 1.26 for print_trigger in sorted(Rates):
661 abrinke1 1.7 ##Limits Rates[] to runs in run_list
662     NewTrigger = {}
663 grchrist 1.34
664 grchrist 1.8 if not print_trigger in trig_list:
665 awoodard 1.50 if all_triggers:
666     trig_list.append(print_trigger)
667     else:
668     print "not in trig_list:",print_trigger, trig_list
669     continue
670 grchrist 1.34
671 abrinke1 1.7 for key in Rates[print_trigger]:
672     NewTrigger[key] = []
673     for iterator in range (len(Rates[print_trigger]["run"])):
674     if Rates[print_trigger]["run"][iterator] in run_list:
675     for key in Rates[print_trigger]:
676     NewTrigger[key].append(Rates[print_trigger][key][iterator])
677     Rates[print_trigger] = NewTrigger
678    
679 grchrist 1.34
680 abrinke1 1.1 meanrawrate = sum(Rates[print_trigger]["rawrate"])/len(Rates[print_trigger]["rawrate"])
681 grchrist 1.40
682 abrinke1 1.1 if not trig_name in print_trigger:
683 grchrist 1.40 print "failed",trig_name, print_trigger
684 abrinke1 1.1 continue
685 grchrist 1.40
686 abrinke1 1.1 if meanrawrate < min_rate:
687     continue
688     masked_trig = False
689     for mask in masked_triggers:
690     if str(mask) in print_trigger:
691     masked_trig = True
692     if masked_trig:
693     continue
694 grchrist 1.34
695 abrinke1 1.5 OutputFit[print_trigger] = {}
696 abrinke1 1.1
697     lowlumi = 0
698 abrinke1 1.7 meanlumi_init = median(Rates[print_trigger]["live_lumi"])
699 abrinke1 1.1 meanlumi = 0
700     highlumi = 0
701     lowxsec = 0
702     meanxsec = 0
703     highxsec = 0
704     nlow = 0
705     nhigh = 0
706 grchrist 1.15
707 abrinke1 1.1 for iterator in range(len(Rates[print_trigger]["rate"])):
708     if Rates[print_trigger]["live_lumi"][iterator] <= meanlumi_init:
709 grchrist 1.11 if ( Rates[print_trigger]["rawrate"][iterator] > 0.04 and Rates[print_trigger]["physics"][iterator] == 1 and Rates[print_trigger]["active"][iterator] == 1 and Rates[print_trigger]["deadtime"][iterator] < max_dt and Rates[print_trigger]["psi"][iterator] > 0 and Rates[print_trigger]["live_lumi"] > 500):
710 abrinke1 1.1 meanxsec+=Rates[print_trigger]["xsec"][iterator]
711     lowxsec+=Rates[print_trigger]["xsec"][iterator]
712     meanlumi+=Rates[print_trigger]["live_lumi"][iterator]
713     lowlumi+=Rates[print_trigger]["live_lumi"][iterator]
714     nlow+=1
715     if Rates[print_trigger]["live_lumi"][iterator] > meanlumi_init:
716 grchrist 1.11 if ( Rates[print_trigger]["rawrate"][iterator] > 0.04 and Rates[print_trigger]["physics"][iterator] == 1 and Rates[print_trigger]["active"][iterator] == 1 and Rates[print_trigger]["deadtime"][iterator] < max_dt and Rates[print_trigger]["psi"][iterator] > 0 and Rates[print_trigger]["live_lumi"] > 500):
717 abrinke1 1.1 meanxsec+=Rates[print_trigger]["xsec"][iterator]
718     highxsec+=Rates[print_trigger]["xsec"][iterator]
719     meanlumi+=Rates[print_trigger]["live_lumi"][iterator]
720     highlumi+=Rates[print_trigger]["live_lumi"][iterator]
721     nhigh+=1
722 abrinke1 1.7 try:
723     meanxsec = meanxsec/(nlow+nhigh)
724     meanlumi = meanlumi/(nlow+nhigh)
725     slopexsec = ( (highxsec/nhigh) - (lowxsec/nlow) ) / ( (highlumi/nhigh) - (lowlumi/nlow) )
726     except:
727 grchrist 1.40 #print str(print_trigger)+" has no good datapoints - setting initial xsec slope estimate to 0"
728 abrinke1 1.7 meanxsec = median(Rates[print_trigger]["xsec"])
729     meanlumi = median(Rates[print_trigger]["live_lumi"])
730     slopexsec = 0
731 abrinke1 1.1
732 abrinke1 1.5 [run_t,ls_t,ps_t,inst_t,live_t,delivered_t,deadtime_t,rawrate_t,rate_t,rawxsec_t,xsec_t,psi_t,e_run_t,e_ls_t,e_ps_t,e_inst_t,e_live_t,e_delivered_t,e_deadtime_t,e_rawrate_t,e_rate_t,e_rawxsec_t,e_xsec_t,e_psi_t,rawrate_fit_t,rate_fit_t,rawxsec_fit_t,xsec_fit_t,e_rawrate_fit_t,e_rate_fit_t,e_rawxsec_fit_t,e_xsec_fit_t] = MakePlotArrays()
733    
734 amott 1.20 if not do_fit:
735 grchrist 1.40
736 abrinke1 1.5 FitType = InputFit[print_trigger][0]
737     X0 = InputFit[print_trigger][1]
738     X1 = InputFit[print_trigger][2]
739     X2 = InputFit[print_trigger][3]
740     X3 = InputFit[print_trigger][4]
741 awoodard 1.57 sigma = InputFit[print_trigger][5]/math.sqrt(num_ls)*3#Display 3 sigma band to show outliers more clearly
742 grchrist 1.38 X0err= InputFit[print_trigger][7]
743 grchrist 1.40 ##print print_trigger," X0err=",X0err
744 grchrist 1.14 #print str(print_trigger)+" "+str(FitType)+" "+str(X0)+" "+str(X1)+" "+str(X2)+" "+str(X3)
745 amott 1.20 #if (first_trigger):
746     # print '%20s % 10s % 6s % 5s % 5s % 3s % 4s' % ('trigger', 'fit type ', 'cubic', 'quad', ' linear', ' c ', 'Chi2')
747     # first_trigger=False
748     #print '%20s % 10s % 2.2g % 2.2g % 2.2g % 2.2g % 2.2g' % (print_trigger, FitType, X3, X2, X1, X0, Chi2)
749 grchrist 1.14 #print '{}, {}, {:02.2g}, {:02.2g}, {:02.2g}, {:02.2g} '.format(print_trigger, FitType, X0, X1, X2, X3)
750 grchrist 1.8 ## we are 2 lumis off when we start! -gets worse when we skip lumis
751 grchrist 1.17 it_offset=0
752 abrinke1 1.1 for iterator in range(len(Rates[print_trigger]["rate"])):
753     if not Rates[print_trigger]["run"][iterator] in run_list:
754     continue
755     prediction = meanxsec + slopexsec * (Rates[print_trigger]["live_lumi"][iterator] - meanlumi)
756     realvalue = Rates[print_trigger]["xsec"][iterator]
757 grchrist 1.8
758 grchrist 1.11
759     if pass_cuts(data_clean, realvalue, prediction, meanxsec, Rates, print_trigger, iterator, num_ls,LumiPageInfo,SubSystemOff,max_dt,print_info, trig_list):
760 grchrist 1.40
761 abrinke1 1.1 run_t.append(Rates[print_trigger]["run"][iterator])
762     ls_t.append(Rates[print_trigger]["ls"][iterator])
763     ps_t.append(Rates[print_trigger]["ps"][iterator])
764     inst_t.append(Rates[print_trigger]["inst_lumi"][iterator])
765 grchrist 1.17 live_t.append(Rates[print_trigger]["live_lumi"][iterator])
766     delivered_t.append(Rates[print_trigger]["delivered_lumi"][iterator])
767     deadtime_t.append(Rates[print_trigger]["deadtime"][iterator])
768 abrinke1 1.1 rawrate_t.append(Rates[print_trigger]["rawrate"][iterator])
769     rate_t.append(Rates[print_trigger]["rate"][iterator])
770     rawxsec_t.append(Rates[print_trigger]["rawxsec"][iterator])
771     xsec_t.append(Rates[print_trigger]["xsec"][iterator])
772 abrinke1 1.5 psi_t.append(Rates[print_trigger]["psi"][iterator])
773 abrinke1 1.1
774     e_run_t.append(0.0)
775     e_ls_t.append(0.0)
776     e_ps_t.append(0.0)
777     e_inst_t.append(14.14)
778     e_live_t.append(14.14)
779     e_delivered_t.append(14.14)
780 abrinke1 1.2 e_deadtime_t.append(0.01)
781 abrinke1 1.1 e_rawrate_t.append(math.sqrt(Rates[print_trigger]["rawrate"][iterator]/(num_ls*23.3)))
782     e_rate_t.append(Rates[print_trigger]["ps"][iterator]*math.sqrt(Rates[print_trigger]["rawrate"][iterator]/(num_ls*23.3)))
783 abrinke1 1.5 e_psi_t.append(0.0)
784 abrinke1 1.2 if live_t[-1] == 0:
785     e_rawxsec_t.append(0)
786     e_xsec_t.append(0)
787     else:
788 grchrist 1.16 try:
789     e_rawxsec_t.append(math.sqrt(Rates[print_trigger]["rawrate"][iterator]/(num_ls*23.3))/Rates[print_trigger]["live_lumi"][iterator])
790     e_xsec_t.append(Rates[print_trigger]["ps"][iterator]*math.sqrt(Rates[print_trigger]["rawrate"][iterator]/(num_ls*23.3))/Rates[print_trigger]["live_lumi"][iterator])
791     except:
792     e_rawxsec_t.append(0.)
793     e_xsec_t.append(0.)
794 amott 1.20 if not do_fit:
795 grchrist 1.38 if not do_inst:
796     if FitType == "expo":
797 grchrist 1.43 rate_prediction = X0 + X1*math.exp(X2+X3*delivered_t[-1])
798 grchrist 1.38 else:
799     rate_prediction = X0 + X1*delivered_t[-1] + X2*delivered_t[-1]*delivered_t[-1] + X3*delivered_t[-1]*delivered_t[-1]*delivered_t[-1]
800 abrinke1 1.5 ## if rate_t[-1] < 0.7 * rate_prediction or rate_t[-1] > 1.4 * rate_prediction:
801     ## print str(run_t[-1])+" "+str(ls_t[-1])+" "+str(print_trigger)+" "+str(ps_t[-1])+" "+str(deadtime_t[-1])+" "+str(rate_prediction)+" "+str(rate_t[-1])+" "+str(rawrate_t[-1])
802 grchrist 1.38 else:
803     if FitType == "expo":
804 grchrist 1.43 rate_prediction = X0 + X1*math.exp(X2+X3*inst_t[-1])
805 grchrist 1.38 else:
806     rate_prediction = X0 + X1*inst_t[-1] + X2*inst_t[-1]*inst_t[-1] + X3*inst_t[-1]*inst_t[-1]*inst_t[-1]
807 abrinke1 1.5
808 abrinke1 1.2 if live_t[-1] == 0:
809 abrinke1 1.5 rawrate_fit_t.append(0)
810     rate_fit_t.append(0)
811 abrinke1 1.2 rawxsec_fit_t.append(0)
812     xsec_fit_t.append(0)
813 abrinke1 1.7 e_rawrate_fit_t.append(0)
814 awoodard 1.45 e_rate_fit_t.append(sigma)
815 abrinke1 1.7 e_rawxsec_fit_t.append(0)
816     e_xsec_fit_t.append(0)
817 grchrist 1.11 #print "live_t=0", ls_t[-1], rawrate_fit_t[-1]
818 abrinke1 1.2 else:
819 grchrist 1.11 if ps_t[-1]>0.0:
820     rawrate_fit_t.append(rate_prediction*(1.0-deadtime_t[-1])/(ps_t[-1]))
821     else:
822     rawrate_fit_t.append(0.0)
823    
824 awoodard 1.52 rate_fit_t.append(rate_prediction)
825 awoodard 1.45 e_rate_fit_t.append(sigma)
826 abrinke1 1.5 rawxsec_fit_t.append(rawrate_fit_t[-1]/live_t[-1])
827     xsec_fit_t.append(rate_prediction*(1.0-deadtime_t[-1])/live_t[-1])
828 grchrist 1.38 try:
829    
830     if not TMDerr:
831 awoodard 1.45 e_rawrate_fit_t.append(sigma*rawrate_fit_t[-1]/rate_fit_t[-1])
832     e_rawxsec_fit_t.append(sigma*rawxsec_fit_t[-1]/rate_fit_t[-1])
833     e_xsec_fit_t.append(sigma*xsec_fit_t[-1]/rate_fit_t[-1])
834 grchrist 1.38 ###error from TMD predictions, calculated at 5e33
835     else:
836     e_rawrate_fit_t.append(X0err*inst_t[-1]/5000.)
837     e_rawxsec_fit_t.append(X0err/live_t[-1]*inst_t[-1]/5000.)
838     e_xsec_fit_t.append(X0err/live_t[-1]*inst_t[-1]/5000.)
839    
840     except:
841     print print_trigger, "has no fitted rate for LS", Rates[print_trigger]["ls"][iterator]
842 awoodard 1.45 e_rawrate_fit_t.append(sigma)
843     e_rawxsec_fit_t.append(sigma)
844     e_xsec_fit_t.append(sigma)
845 grchrist 1.11 #print "live_t>0", ls_t[-1], rawrate_fit_t[-1]
846 abrinke1 1.5
847 grchrist 1.17 ##print iterator, iterator, "ls=",ls_t[-1],"rate=",round(rawrate_t[-1],2), "deadtime=",round(deadtime_t[-1],2),"rawrate_fit=",round(rawrate_fit_t[-1],2),"max it=",len(Rates[print_trigger]["rate"])
848 grchrist 1.16
849 grchrist 1.27 if (print_info and num_ls==1 and (fabs(rawrate_fit_t[-1]-rawrate_t[-1])>2.5*sqrt(sum(Rates[print_trigger]["rawrate"])/len(Rates[print_trigger]["rawrate"])))):
850 grchrist 1.38 pass
851     ###print '%-60s has a bad prediction, run=%-10s LS=%-4s' % (print_trigger, Rates[print_trigger]["run"][iterator], Rates[print_trigger]["ls"][iterator])
852 grchrist 1.8
853 abrinke1 1.5 else: ##If the data point does not pass the data_clean filter
854 grchrist 1.11 #print "not passed", iterator, ls_t[-1], rawrate_fit_t[-1]
855 abrinke1 1.1 if debug_print:
856     print str(print_trigger)+" has xsec "+str(round(Rates[print_trigger]["xsec"][iterator],6))+" at lumi "+str(round(Rates[print_trigger]["live_lumi"][iterator],2))+" where the expected value is "+str(prediction)
857    
858 abrinke1 1.5 ## End "for iterator in range(len(Rates[print_trigger]["rate"])):" loop
859 abrinke1 1.1
860 abrinke1 1.5 AllPlotArrays = [run_t,ls_t,ps_t,inst_t,live_t,delivered_t,deadtime_t,rawrate_t,rate_t,rawxsec_t,xsec_t,psi_t,e_run_t,e_ls_t,e_ps_t,e_inst_t,e_live_t,e_delivered_t,e_deadtime_t,e_rawrate_t,e_rate_t,e_rawxsec_t,e_xsec_t,e_psi_t,rawrate_fit_t,rate_fit_t,rawxsec_fit_t,xsec_fit_t,e_rawrate_fit_t,e_rate_fit_t,e_rawxsec_fit_t,e_xsec_fit_t]
861 grchrist 1.40
862 abrinke1 1.5 [VX, VXE, VY, VYE, VF, VFE] = GetVXVY(plot_properties, fit_file, AllPlotArrays)
863 grchrist 1.40
864 abrinke1 1.5 if save_root or save_png:
865     c1 = TCanvas(str(varX),str(varY))
866     c1.SetName(str(print_trigger)+"_"+str(varY)+"_vs_"+str(varX))
867 grchrist 1.40
868     try:
869     gr1 = TGraphErrors(len(VX), VX, VY, VXE, VYE)
870     except:
871     print "No lumisections with events for", print_trigger, "probably due to v high deadtime"
872     continue
873 abrinke1 1.5 gr1.SetName("Graph_"+str(print_trigger)+"_"+str(varY)+"_vs_"+str(varX))
874     gr1.GetXaxis().SetTitle(varX)
875     gr1.GetYaxis().SetTitle(varY)
876     gr1.SetTitle(str(print_trigger))
877     gr1.SetMinimum(0)
878     gr1.SetMaximum(1.2*max(VY))
879     #gr1.GetXaxis().SetLimits(min(VX)-0.2*max(VX),1.2*max(VX))
880     gr1.GetXaxis().SetLimits(0,1.2*max(VX))
881     gr1.SetMarkerStyle(8)
882 grchrist 1.40
883    
884 abrinke1 1.5 if fit_file:
885     gr1.SetMarkerSize(0.8)
886     else:
887     gr1.SetMarkerSize(0.5)
888     gr1.SetMarkerColor(2)
889    
890 amott 1.20 if not do_fit:
891 abrinke1 1.5 gr3 = TGraphErrors(len(VX), VX, VF, VXE, VFE)
892     gr3.SetMarkerStyle(8)
893     gr3.SetMarkerSize(0.4)
894     gr3.SetMarkerColor(4)
895     gr3.SetFillColor(4)
896     gr3.SetFillStyle(3003)
897 abrinke1 1.1
898 abrinke1 1.5 if do_fit:
899 grchrist 1.35 if "rate" in varY and not linear:
900    
901 grchrist 1.41 f1a=0
902 abrinke1 1.5 f1a = TF1("f1a","pol2",0,8000)
903     f1a.SetLineColor(4)
904     f1a.SetLineWidth(2)
905 grchrist 1.41 #f1a.SetParLimits(0,0,0.2*(sum(VY)/len(VY))+0.8*min(VY))
906     #f1a.SetParLimits(1,0,2.0*max(VY)/(max(VX)*max(VX)))
907 awoodard 1.48 gr1.Fit("f1a","QN","rob=0.90")
908 muell149 1.46
909    
910     f1d=0
911     f1d = TF1("f1d","pol1",0,8000)#linear
912     f1d.SetLineColor(4)
913     f1d.SetLineWidth(2)
914     #f1a.SetParLimits(0,0,0.2*(sum(VY)/len(VY))+0.8*min(VY))
915     #f1a.SetParLimits(1,0,2.0*max(VY)/(max(VX)*max(VX)))
916 awoodard 1.48 gr1.Fit("f1d","QN","rob=0.90")
917 muell149 1.46
918 abrinke1 1.5 f1b = 0
919     f1c = 0
920 muell149 1.46 meanps = median(Rates[print_trigger]["ps"])
921     av_rte = mean(VY)
922    
923 grchrist 1.43 if True:
924     f1b = TF1("f1b","pol3",0,8000)
925     f1b.SetLineColor(2)
926     f1b.SetLineWidth(2)
927 grchrist 1.41 ## f1b.SetParLimits(0,0,0.2*(sum(VY)/len(VY))+0.8*min(VY))
928     ## f1b.SetParLimits(1,0,f1a.GetParameter(1)+0.0000001)
929     ## f1b.SetParLimits(2,0,f1a.GetParameter(2)+0.0000000001)
930     ## f1b.SetParLimits(3,0,2.0*max(VY)/(max(VX)*max(VX)*max(VX)))
931 awoodard 1.48 gr1.Fit("f1b","QN","rob=0.90")
932 grchrist 1.41 ## #if f1b.GetChisquare()/f1b.GetNDF() < f1a.GetChisquare()/f1a.GetNDF():
933     ## #print "X0 = "+str(f1a.GetParameter(0))+" X1 = "+str(f1a.GetParameter(1))+" X2 = "+str(f1a.GetParameter(2))
934     ## #print str(print_trigger)+" f1a Chi2 = "+str(10*f1a.GetChisquare()*math.sqrt(len(VY))/(math.sqrt(sum(VY))*num_ls*f1a.GetNDF()))+", f1b Chi2 = "+str(10*f1b.GetChisquare()*math.sqrt(len(VY))/(math.sqrt(sum(VY))*num_ls*f1b.GetNDF()))
935     ## #print "X0 = "+str(f1b.GetParameter(0))+" X1 = "+str(f1b.GetParameter(1))+" X2 = "+str(f1b.GetParameter(2))+" X3 = "+str(f1b.GetParameter(3))
936     ## if (first_trigger):
937     ## print '%-60s %4s x0 x1 x2 x3 chi2 ndf chi2/ndf' % ('trigger', 'type')
938 grchrist 1.15
939 grchrist 1.41 ## first_trigger=False
940 grchrist 1.15
941    
942 abrinke1 1.1
943 grchrist 1.43 f1c = TF1("f1c","[0]+[1]*expo(2)",0,8000)
944     f1c.SetLineColor(3)
945     f1c.SetLineWidth(2)
946     f1c.SetParLimits(0,0,0.2*(sum(VY)/len(VY))+0.8*min(VY))
947     f1c.SetParLimits(1,max(VY)/math.exp(10.0),max(VY)/math.exp(2.0))
948     f1c.SetParLimits(2,0.0,0.0000000001)
949     f1c.SetParLimits(3,2.0/max(VX),10.0/max(VX))
950 awoodard 1.48 gr1.Fit("f1c","QN","rob=0.90")
951 grchrist 1.41 ## #if f1c.GetChisquare()/f1c.GetNDF() < f1a.GetChisquare()/f1a.GetNDF():
952     ## #print str(print_trigger)+" f1a Chi2 = "+str(10*f1a.GetChisquare()*math.sqrt(len(VY))/(math.sqrt(sum(VY))*num_ls*f1a.GetNDF()))+", f1c Chi2 = "+str(10*f1c.GetChisquare()*math.sqrt(len(VY))/(math.sqrt(sum(VY))*num_ls*f1c.GetNDF()))
953     ## #print "X0 = "+str(f1c.GetParameter(0))+" X1 = "+str(f1c.GetParameter(1))+" X2 = "+str(f1c.GetParameter(2))+" X3 = "+str(f1c.GetParameter(3))
954 grchrist 1.15
955    
956 grchrist 1.34 ## if (f1c.GetChisquare()/f1c.GetNDF() < f1b.GetChisquare()/f1b.GetNDF() and f1c.GetChisquare()/f1c.GetNDF() < f1a.GetChisquare()/f1a.GetNDF()):
957     ## print '%-60s expo % .2f+/-%.2f % .2e+/-%.1e % .2e+/-%.1e % .2e+/-%.1e %7.2f %4.0f %5.3f ' % (print_trigger, f1c.GetParameter(0), f1c.GetParError(0), f1c.GetParameter(1), f1c.GetParError(1), 0 , 0 , 0 , 0 , f1c.GetChisquare(), f1c.GetNDF(), f1c.GetChisquare()/f1c.GetNDF())
958     ## elif (f1b.GetChisquare()/f1b.GetNDF() < f1a.GetChisquare()/f1a.GetNDF()):
959     ## print '%-60s cube % .2f+/-%.2f % .2e+/-%.1e % .2e+/-%.1e % .2e+/-%.1e %7.2f %4.0f %5.3f ' % (print_trigger, f1b.GetParameter(0), f1b.GetParError(0), f1b.GetParameter(1), f1b.GetParError(1), f1b.GetParameter(2), f1b.GetParError(2), f1b.GetParameter(3), f1b.GetParError(3), f1b.GetChisquare(), f1b.GetNDF(), f1b.GetChisquare()/f1b.GetNDF())
960     ## else:
961 grchrist 1.43
962 awoodard 1.48 # print f1a.GetChisquare()/f1a.GetNDF(), f1b.GetChisquare()/f1b.GetNDF(), f1c.GetChisquare()/f1c.GetNDF()
963 grchrist 1.15
964 abrinke1 1.5 else: ##If this is not a rate plot
965     f1a = TF1("f1a","pol1",0,8000)
966     f1a.SetLineColor(4)
967     f1a.SetLineWidth(2)
968     if "xsec" in varY:
969     f1a.SetParLimits(0,0,meanxsec*1.5)
970     if slopexsec > 0:
971     f1a.SetParLimits(1,0,max(VY)/max(VX))
972     else:
973     f1a.SetParLimits(1,2*slopexsec,-2*slopexsec)
974 abrinke1 1.1 else:
975 abrinke1 1.5 f1a.SetParLimits(0,-1000,1000)
976     gr1.Fit("f1a","Q","rob=0.80")
977 abrinke1 1.1
978 grchrist 1.35 if (first_trigger):
979     print '%-60s %4s x0 x1 x2 x3 chi2 ndf chi2/ndf' % ('trigger', 'type')
980     first_trigger=False
981 grchrist 1.40 try:
982     print '%-60s | line | % .2f | +/-%.2f | % .2e | +/-%.1e | % .2e | +/-%.1e | % .2e | +/-%.1e | %7.2f | %4.0f | %5.3f | ' % (print_trigger, f1a.GetParameter(0), f1a.GetParError(0), f1a.GetParameter(1), f1a.GetParError(1), 0 , 0 , 0 , 0 , f1a.GetChisquare(), f1a.GetNDF(), f1a.GetChisquare()/f1a.GetNDF())
983     except:
984     pass
985 awoodard 1.48
986     if print_table or save_fits:
987     if not do_fit:
988     print "Can't have save_fits = True and do_fit = False"
989     continue
990     try:
991     if (f1c.GetChisquare()/f1c.GetNDF() < (f1a.GetChisquare()/f1a.GetNDF()-1) and (f1b.GetChisquare()/f1b.GetNDF() < f1a.GetChisquare()/f1a.GetNDF()-1)):
992     print '%-60s | expo | % .2f | +/-%.2f | % .2e | +/-%.1e | % .2e | +/-%.1e | % .2e | +/-%.1e | %7.2f | %4.0f | %5.3f | ' % (print_trigger, f1c.GetParameter(0) , f1c.GetParError(0) , f1c.GetParameter(1) , f1c.GetParError(1) , f1c.GetParameter(2), f1c.GetParError(2) ,f1c.GetParameter(3), f1c.GetParError(3) ,f1c.GetChisquare() , f1c.GetNDF() , f1c.GetChisquare()/f1c.GetNDF())
993     f1c.SetLineColor(1)
994     priot(wp_bool,print_trigger,meanps,f1d,f1c,"expo",av_rte)
995     sigma = CalcSigma(VX, VY, f1c)*math.sqrt(num_ls)
996     OutputFit[print_trigger] = ["expo", f1c.GetParameter(0) , f1c.GetParameter(1) , f1c.GetParameter(2) , f1c.GetParameter(3) , sigma , meanrawrate, f1c.GetParError(0) , f1c.GetParError(1) , f1c.GetParError(2) , f1c.GetParError(3)]
997    
998     elif (f1b.GetChisquare()/f1b.GetNDF() < (f1a.GetChisquare()/f1a.GetNDF()-1)):
999     print '%-60s | cube | % .2f | +/-%.2f | % .2e | +/-%.1e | % .2e | +/-%.1e | % .2e | +/-%.1e | %7.2f | %4.0f | %5.3f | ' % (print_trigger, f1b.GetParameter(0) , f1b.GetParError(0) , f1b.GetParameter(1) , f1b.GetParError(1) , f1b.GetParameter(2), f1b.GetParError(2) ,f1b.GetParameter(3), f1b.GetParError(3), f1b.GetChisquare() , f1b.GetNDF() , f1b.GetChisquare()/f1b.GetNDF())
1000     f1b.SetLineColor(1)
1001     priot(wp_bool,print_trigger,meanps,f1d,f1b,"cubic",av_rte)
1002     sigma = CalcSigma(VX, VY, f1b)*math.sqrt(num_ls)
1003     OutputFit[print_trigger] = ["poly", f1b.GetParameter(0) , f1b.GetParameter(1) , f1b.GetParameter(2) , f1b.GetParameter(3) , sigma , meanrawrate, f1b.GetParError(0) , f1b.GetParError(1) , f1b.GetParError(2) , f1b.GetParError(3)]
1004    
1005     else:
1006 awoodard 1.54
1007     print '%-60s | quad | % .2f | +/-%.2f | % .2e | +/-%.1e | % .2e | +/-%.1e | % .2e | +/-%.1e | %7.2f | %4.0f | %5.3f | ' % (print_trigger, f1a.GetParameter(0) , f1a.GetParError(0) , f1a.GetParameter(1) , f1a.GetParError(1) , f1a.GetParameter(2), f1a.GetParError(2), 0 , 0 , f1a.GetChisquare() , f1a.GetNDF() , f1a.GetChisquare()/f1a.GetNDF())
1008 awoodard 1.48 f1a.SetLineColor(1)
1009     priot(wp_bool,print_trigger,meanps,f1d,f1a,"quad",av_rte)
1010 awoodard 1.53 sigma = CalcSigma(VX, VY, f1a)*math.sqrt(num_ls)
1011 awoodard 1.48 OutputFit[print_trigger] = ["poly", f1a.GetParameter(0) , f1a.GetParameter(1) , f1a.GetParameter(2) , 0.0 , sigma , meanrawrate, f1a.GetParError(0) , f1a.GetParError(1) , f1a.GetParError(2) , 0.0]
1012 awoodard 1.55
1013 awoodard 1.48 except ZeroDivisionError:
1014     print "No NDF for",print_trigger,"skipping"
1015    
1016 abrinke1 1.5 if save_root or save_png:
1017     gr1.Draw("APZ")
1018 amott 1.20 if not do_fit:
1019 abrinke1 1.5 gr3.Draw("P3")
1020     if do_fit:
1021     f1a.Draw("same")
1022     try:
1023     f1b.Draw("same")
1024     f1c.Draw("same")
1025 muell149 1.46 f1d.Draw("same")
1026 abrinke1 1.5 except:
1027     True
1028     c1.Update()
1029     if save_root:
1030     myfile = TFile( RootFile, 'UPDATE' )
1031     c1.Write()
1032     myfile.Close()
1033     if save_png:
1034     c1.SaveAs(str(print_trigger)+"_"+str(varY)+"_vs_"+str(varX)+".png")
1035 abrinke1 1.1
1036 grchrist 1.35
1037 grchrist 1.28 ##print print_trigger, OutputFit[print_trigger]
1038 abrinke1 1.1 if save_root:
1039     print "Output root file is "+str(RootFile)
1040    
1041     if save_fits:
1042 grchrist 1.29 #FitNameTemplate = "Fits/%/Fit_%s_%sLS_Run%sto%s.pkl" % (thisyear)
1043 amott 1.20 #FitFile = FitNameTemplate % (trig_name, num_ls, min_run, max_run)
1044     if os.path.exists(fit_file):
1045     os.remove(fit_file)
1046     FitOutputFile = open(fit_file, 'wb')
1047 abrinke1 1.5 pickle.dump(OutputFit, FitOutputFile, 2)
1048     FitOutputFile.close()
1049 amott 1.20 print "Output fit file is "+str(fit_file)
1050 abrinke1 1.1
1051     if print_table:
1052 abrinke1 1.5 print "The expo fit is of the form p0+p1*e^(p2*x), poly is p0+(p1/10^3)*x+(p2/10^6)*x^2+(p3/10^9)*x^3, where x is Deliv. Lumi."
1053     print '%60s%10s%10s%10s%10s%10s%10s%10s' % ("Trig", "fit", "p0", "p1", "p2", "p3", "Chi2", "Av raw")
1054     for print_trigger in OutputFit:
1055     _trigger = (print_trigger[:56] + '...') if len(print_trigger) > 59 else print_trigger
1056 abrinke1 1.1 try:
1057 abrinke1 1.5 if OutputFit[print_trigger][0] == "poly":
1058     print '%60s%10s%10s%10s%10s%10s%10s' % (_trigger, OutputFit[print_trigger][0], round(OutputFit[print_trigger][1],3), round(OutputFit[print_trigger][2],6)*1000, round(OutputFit[print_trigger][3],9)*1000000, round(OutputFit[print_trigger][4],12)*1000000000, round(OutputFit[print_trigger][5],2), round(OutputFit[print_trigger][6],3))
1059     else:
1060     print '%60s%10s%10s%10s%10s%10s%10s' % (_trigger, OutputFit[print_trigger][0], OutputFit[print_trigger][1], OutputFit[print_trigger][2], OutputFit[print_trigger][3], OutputFit[print_trigger][4], round(OutputFit[print_trigger][5],2), round(OutputFit[print_trigger][6],3))
1061 abrinke1 1.1 except:
1062     print str(print_trigger)+" is somehow broken"
1063 amott 1.18 return RootFile
1064 abrinke1 1.5
1065     ############# SUPPORTING FUNCTIONS ################
1066    
1067    
1068 awoodard 1.47 def CalcSigma(var_x, var_y, func):
1069     residuals = []
1070     for x, y in zip(var_x,var_y):
1071 awoodard 1.53 residuals.append(y - func.Eval(x,0,0))
1072 awoodard 1.47
1073     res_squared = [i*i for i in residuals]
1074 awoodard 1.53 if len(res_squared) > 2:
1075     sigma = math.sqrt(sum(res_squared)/(len(res_squared)-2))
1076     else:
1077     sigma = 0
1078 awoodard 1.47 return sigma
1079    
1080 abrinke1 1.5 def GetJSON(json_file):
1081    
1082     input_file = open(json_file)
1083     file_content = input_file.read()
1084     inputRange = selectionParser(file_content)
1085     JSON = inputRange.runsandls()
1086     return JSON
1087     ##JSON is an array: JSON[run_number] = [1st ls, 2nd ls, 3rd ls ... nth ls]
1088    
1089     def MakePlotArrays():
1090     run_t = array.array('f')
1091     ls_t = array.array('f')
1092     ps_t = array.array('f')
1093     inst_t = array.array('f')
1094     live_t = array.array('f')
1095     delivered_t = array.array('f')
1096     deadtime_t = array.array('f')
1097     rawrate_t = array.array('f')
1098     rate_t = array.array('f')
1099     rawxsec_t = array.array('f')
1100     xsec_t = array.array('f')
1101     psi_t = array.array('f')
1102    
1103     e_run_t = array.array('f')
1104     e_ls_t = array.array('f')
1105     e_ps_t = array.array('f')
1106     e_inst_t = array.array('f')
1107     e_live_t = array.array('f')
1108     e_delivered_t = array.array('f')
1109     e_deadtime_t = array.array('f')
1110     e_rawrate_t = array.array('f')
1111     e_rate_t = array.array('f')
1112     e_rawxsec_t = array.array('f')
1113     e_xsec_t = array.array('f')
1114     e_psi_t = array.array('f')
1115    
1116     rawrate_fit_t = array.array('f')
1117     rate_fit_t = array.array('f')
1118     rawxsec_fit_t = array.array('f')
1119     xsec_fit_t = array.array('f')
1120     e_rawrate_fit_t = array.array('f')
1121     e_rate_fit_t = array.array('f')
1122     e_rawxsec_fit_t = array.array('f')
1123     e_xsec_fit_t = array.array('f')
1124    
1125     return [run_t,ls_t,ps_t,inst_t,live_t,delivered_t,deadtime_t,rawrate_t,rate_t,rawxsec_t,xsec_t,psi_t,e_run_t,e_ls_t,e_ps_t,e_inst_t,e_live_t,e_delivered_t,e_deadtime_t,e_rawrate_t,e_rate_t,e_rawxsec_t,e_xsec_t,e_psi_t,rawrate_fit_t,rate_fit_t,rawxsec_fit_t,xsec_fit_t,e_rawrate_fit_t,e_rate_fit_t,e_rawxsec_fit_t,e_xsec_fit_t]
1126    
1127    
1128     def GetVXVY(plot_properties, fit_file, AllPlotArrays):
1129    
1130     VF = "0"
1131     VFE = "0"
1132    
1133     [run_t,ls_t,ps_t,inst_t,live_t,delivered_t,deadtime_t,rawrate_t,rate_t,rawxsec_t,xsec_t,psi_t,e_run_t,e_ls_t,e_ps_t,e_inst_t,e_live_t,e_delivered_t,e_deadtime_t,e_rawrate_t,e_rate_t,e_rawxsec_t,e_xsec_t,e_psi_t,rawrate_fit_t,rate_fit_t,rawxsec_fit_t,xsec_fit_t,e_rawrate_fit_t,e_rate_fit_t,e_rawxsec_fit_t,e_xsec_fit_t] = AllPlotArrays
1134     for varX, varY, do_fit, save_root, save_png, fit_file in plot_properties:
1135     if varX == "run":
1136     VX = run_t
1137     VXE = run_t_e
1138     elif varX == "ls":
1139     VX = ls_t
1140     VXE = e_ls_t
1141     elif varX == "ps":
1142     VX = ps_t
1143     VXE = e_ps_t
1144     elif varX == "inst":
1145     VX = inst_t
1146     VXE = e_inst_t
1147     elif varX == "live":
1148     VX = live_t
1149     VXE = e_live_t
1150     elif varX == "delivered":
1151     VX = delivered_t
1152     VXE = e_delivered_t
1153     elif varX == "deadtime":
1154     VX = deadtime_t
1155     VXE = e_deadtime_t
1156     elif varX == "rawrate":
1157     VX = rawrate_t
1158     VXE = e_rawrate_t
1159     elif varX == "rate":
1160     VX = rate_t
1161     VXE = e_rate_t
1162     elif varX == "rawxsec":
1163     VX = rawxsec_t
1164     VXE = e_rawxsec_t
1165     elif varX == "xsec":
1166     VX = xsec_t
1167     VXE = e_xsec_t
1168     elif varX == "psi":
1169     VX = psi_t
1170     VXE = e_psi_t
1171     else:
1172     print "No valid variable entered for X"
1173     continue
1174     if varY == "run":
1175     VY = run_t
1176     VYE = run_t_e
1177     elif varY == "ls":
1178     VY = ls_t
1179     VYE = e_ls_t
1180     elif varY == "ps":
1181     VY = ps_t
1182     VYE = e_ps_t
1183     elif varY == "inst":
1184     VY = inst_t
1185     VYE = e_inst_t
1186     elif varY == "live":
1187     VY = live_t
1188     VYE = e_live_t
1189     elif varY == "delivered":
1190     VY = delivered_t
1191     VYE = e_delivered_t
1192     elif varY == "deadtime":
1193     VY = deadtime_t
1194     VYE = e_deadtime_t
1195     elif varY == "rawrate":
1196     VY = rawrate_t
1197     VYE = e_rawrate_t
1198     if fit_file:
1199     VF = rawrate_fit_t
1200     VFE = e_rawrate_fit_t
1201     elif varY == "rate":
1202     VY = rate_t
1203     VYE = e_rate_t
1204     if fit_file:
1205     VF = rate_fit_t
1206     VFE = e_rate_fit_t
1207     elif varY == "rawxsec":
1208     VY = rawxsec_t
1209     VYE = e_rawxsec_t
1210     if fit_file:
1211     VF = rawxsec_fit_t
1212     VFE = e_rawxsec_fit_t
1213     elif varY == "xsec":
1214     VY = xsec_t
1215     VYE = e_xsec_t
1216     if fit_file:
1217     VF = xsec_fit_t
1218     VFE = e_xsec_fit_t
1219     elif varY == "psi":
1220     VY = psi_t
1221     VYE = e_psi_t
1222     else:
1223     print "No valid variable entered for Y"
1224     continue
1225    
1226     return [VX, VXE, VY, VYE, VF, VFE]
1227    
1228 grchrist 1.11 def pass_cuts(data_clean, realvalue, prediction, meanxsec, Rates, print_trigger, iterator, num_ls,LumiPageInfo,SubSystemOff, max_dt, print_info, trig_list):
1229 grchrist 1.17 it_offset=0
1230 grchrist 1.15 Passed=True
1231     subsystemfailed=[]
1232 grchrist 1.11
1233 grchrist 1.8 if num_ls==1:
1234     ##fit is 2 ls ahead of real rate
1235 grchrist 1.17
1236 grchrist 1.8
1237 grchrist 1.10 LS=Rates[print_trigger]["ls"][iterator]
1238     #print "ls=",LS,
1239     LSRange=LumiPageInfo[LS]["LSRange"]
1240     #print LSRange,
1241     LS2=LSRange[-1]
1242     #LS2=LSRange.pop()
1243     #print "LS2=",LS2
1244    
1245     #print LumiPageInfo[LS]
1246     lumidict={}
1247     lumidict=LumiPageInfo[LS]
1248 grchrist 1.15
1249 grchrist 1.11
1250    
1251    
1252    
1253     if print_info:
1254     if (iterator==0 and print_trigger==trig_list[0]):
1255     print '%10s%10s%10s%10s%10s%10s%10s%15s%20s' % ("Status", "Run", "LS", "Physics", "Active", "Deadtime", " MaxDeadTime", " Passed all subsystems?", " List of Subsystems failed")
1256    
1257     ## if SubSystemOff["All"]:
1258     ## for keys in LumiPageInfo[LS]:
1259     ## #print LS, keys, LumiPageInfo[LS][keys]
1260     ## if not LumiPageInfo[LS][keys]:
1261     ## Passed=False
1262     ## subsystemfailed.append(keys)
1263     ## break
1264     ## else:
1265     if SubSystemOff["Mu"] or SubSystemOff["All"]:
1266 grchrist 1.38 if not (LumiPageInfo[LS]["rpc"] and LumiPageInfo[LS]["dt0"] and LumiPageInfo[LS]["dtp"] and LumiPageInfo[LS]["dtm"] and LumiPageInfo[LS]["cscp"] and LumiPageInfo[LS]["cscm"]):
1267 grchrist 1.11 Passed=False
1268     subsystemfailed.append("Mu")
1269     if SubSystemOff["HCal"] or SubSystemOff["All"]:
1270     if not (LumiPageInfo[LS]["hbhea"] and LumiPageInfo[LS]["hbheb"] and LumiPageInfo[LS]["hbhec"]):
1271     Passed=False
1272     subsystemfailed.append("HCal")
1273     if (SubSystemOff["EndCap"] or SubSystemOff["All"]) and not (LumiPageInfo[LS]["hf"]):
1274     Passed=False
1275     subsystemfailed.append("HCal-EndCap")
1276     if SubSystemOff["ECal"] or SubSystemOff["All"]:
1277     if not (LumiPageInfo[LS]["ebp"] and LumiPageInfo[LS]["ebm"]):
1278     Passed=False
1279     subsystemfailed.append("ECal")
1280     if (SubSystemOff["EndCap"] or SubSystemOff["All"]) and not (LumiPageInfo[LS]["eep"] and LumiPageInfo[LS]["eem"] and LumiPageInfo[LS]["esp"] or LumiPageInfo[LS]["esm"]):
1281     Passed=False
1282     subsystemfailed.append("ECal-EndCap")
1283     if SubSystemOff["Tracker"] or SubSystemOff["All"]:
1284     if not (LumiPageInfo[LS]["tob"] and LumiPageInfo[LS]["tibtid"] and LumiPageInfo[LS]["bpix"] and LumiPageInfo[LS]["fpix"]):
1285     Passed=False
1286     subsystemfailed.append("Tracker")
1287     if (SubSystemOff["EndCap"] or SubSystemOff["All"]) and not (LumiPageInfo[LS]["tecp"] and LumiPageInfo[LS]["tecm"]):
1288     Passed=False
1289     subsystemfailed.append("Tracker-EndCap")
1290     if SubSystemOff["Beam"] or SubSystemOff["All"]:
1291     if not(LumiPageInfo[LS]["b1pres"] and LumiPageInfo[LS]["b2pres"] and LumiPageInfo[LS]["b1stab"] and LumiPageInfo[LS]["b2stab"]):
1292     Passed=False
1293     subsystemfailed.append("Beam")
1294 grchrist 1.12 else:
1295 grchrist 1.17
1296 grchrist 1.12 Passed=True
1297 grchrist 1.10
1298 grchrist 1.38
1299 grchrist 1.10
1300 grchrist 1.8 if not data_clean or (
1301    
1302 grchrist 1.11 Rates[print_trigger]["physics"][iterator] == 1
1303 grchrist 1.8 and Rates[print_trigger]["active"][iterator] == 1
1304 grchrist 1.17 and Rates[print_trigger]["deadtime"][iterator] < max_dt
1305 grchrist 1.11 #and Rates[print_trigger]["psi"][iterator] > 0
1306 grchrist 1.10 and Passed
1307 grchrist 1.8 ):
1308 grchrist 1.11 #print LS, "True"
1309 grchrist 1.26 if (print_info and num_ls==1 and (realvalue <0.4*prediction or realvalue>2.5*prediction)):
1310     pass
1311     ##print '%-60s%10s%10s%10s%10s%10s%10s%10s%15s%20s' % (print_trigger,"Passed", Rates[print_trigger]["run"][iterator], LS, Rates[print_trigger]["physics"][iterator], Rates[print_trigger]["active"][iterator], round(Rates[print_trigger]["deadtime"][iterator],2), max_dt, Passed, subsystemfailed)
1312    
1313 grchrist 1.8 return True
1314     else:
1315 grchrist 1.11
1316 grchrist 1.13 if (print_info and print_trigger==trig_list[0] and num_ls==1):
1317 grchrist 1.17 print '%10s%10s%10s%10s%10s%10s%10s%15s%20s' % ("Failed", Rates[print_trigger]["run"][iterator], LS, Rates[print_trigger]["physics"][iterator], Rates[print_trigger]["active"][iterator], round(Rates[print_trigger]["deadtime"][iterator],2), max_dt, Passed, subsystemfailed)
1318 grchrist 1.26
1319 grchrist 1.8 return False
1320 abrinke1 1.5
1321 grchrist 1.10
1322     #### LumiRangeGreens ####
1323     ####inputs: RefMoreLumiArray --dict with lumi page info in LS by LS blocks,
1324     #### LRange --list range over lumis,
1325     #### nls --number of lumisections
1326     #### RefRunNum --run number
1327     ####
1328     ####outputs RangeMoreLumi --lumi page info in dict LSRange blocks with lumi, added items Run and LSRange
1329 grchrist 1.17 def LumiRangeGreens(RefMoreLumiArray,LSRange,nls,RefRunNum,deadtimebeamactive):
1330 grchrist 1.9
1331     RangeMoreLumi={}
1332     for keys,values in RefMoreLumiArray.iteritems():
1333     RangeMoreLumi[keys]=1
1334 grchrist 1.10
1335 grchrist 1.9 for iterator in LSRange[nls]:
1336     for keys, values in RefMoreLumiArray.iteritems():
1337     if RefMoreLumiArray[keys][iterator]==0:
1338     RangeMoreLumi[keys]=0
1339 grchrist 1.10 RangeMoreLumi['LSRange']=LSRange[nls]
1340     RangeMoreLumi['Run']=RefRunNum
1341 grchrist 1.17 RangeMoreLumi['DeadTimeBeamActive']=deadtimebeamactive
1342 grchrist 1.9 return RangeMoreLumi
1343    
1344 grchrist 1.10 #### CheckLumis ####
1345     ####inputs:
1346     #### PageLumiInfo --dict of LS with dict of some lumipage info
1347     #### Rates --dict of triggernames with dict of info
1348     def checkLS(Rates, PageLumiInfo,trig_list):
1349     rateslumis=Rates[trig_list[-1]]["ls"]
1350     keys=PageLumiInfo.keys()
1351     print "lumi run=",PageLumiInfo[keys[-1]]["Run"]
1352     ll=0
1353     for ls in keys:
1354     print ls,rateslumis[ll]
1355     ll=ll+1
1356     return False
1357    
1358    
1359 grchrist 1.9
1360 grchrist 1.29
1361 abrinke1 1.1 if __name__=='__main__':
1362 grchrist 1.29 global thisyear
1363 abrinke1 1.1 main()