ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/RateMonShiftTool_dev/DatabaseRatePredictor.py
Revision: 1.60
Committed: Wed Oct 3 11:36:09 2012 UTC (12 years, 7 months ago) by grchrist
Content type: text/x-python
Branch: MAIN
CVS Tags: V00-02-03
Changes since 1.59: +1 -1 lines
Log Message:
changes for P5 deployment

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