CMS 3D CMS Logo

Classes | Functions | Variables
generateEDF Namespace Reference

Classes

class  LumiInfo
 

LumiInfo Class

More...
 
class  LumiInfoCont
 

LumiInfoCont Class

More...
 

Functions

def loadEvents (filename, cont, options)
 

General Functions

More...
 
def makeEDFplot (lumiCont, eventsDict, totalWeight, outputFile, options)
 

Variables

 action
 
 allowedEDF
 

## Main

More...
 
 ArgumentDefaultsHelpFormatter
 
 choices
 
 cont
 load Luminosity info ## More...
 
 default
 
 description
 
 dest
 
 done
 
 eventsDict
 make EDF plots ## More...
 
 False
 
 float
 
 formatter_class
 
 help
 
 inputGroup
 
 int
 
 maxIntLum
 
 maxRun
 
 metavar
 
 minIntLum
 
 minRun
 
 modeGroup
 
 nargs
 
 nonSpaceRE
 
 options
 
 parser
 
 pieces
 
 plotGroup
 
 prevRecLumi
 
 rangeGroup
 
 recLumIndex
 
 recLumis
 look for which runs correspond to what total ## recorded integrated luminosity ## More...
 
 recLumValue
 
 sepRE
 
 str
 
 totalWeight
 
 type
 
 usage
 

Function Documentation

◆ loadEvents()

def generateEDF.loadEvents (   filename,
  cont,
  options 
)

General Functions

Definition at line 238 of file generateEDF.py.

References mps_setup.append, int, and print().

238 def loadEvents (filename, cont, options):
239  eventsDict = {}
240  print("loading events from '%s'" % filename)
241  events = open (filename, 'r') runIndex, lumiIndex, eventIndex, weightIndex = 0, 1, 2, 3
242  if options.relOrder:
243  lumiIndex, eventIndex = 2,1
244  minPieces = 3
245  totalWeight = 0.
246  if options.weights:
247  minPieces = 4
248  for line in events:
249  pieces = sepRE.split (line.strip())
250  if len (pieces) < minPieces:
251  if nonSpaceRE.search (line):
252  print("skipping", line)
253  continue
254  try:
255  run, lumi, event = int( pieces[runIndex] ), \
256  int( pieces[lumiIndex] ), \
257  int( pieces[eventIndex] )
258  except:
259  continue
260  key = (run, lumi)
261  if key not in cont:
262  if options.ignore:
263  print("Warning, %s is not found in the lumi information" \
264  % key.__str__())
265  continue
266  else:
267  raise RuntimeError("%s is not found in lumi information. Use '--ignoreNoLumiEvents' option to ignore these events and continue." \
268  % key.__str__())
269  if options.edfMode != 'time' and not cont[key].xingInfo:
270  if options.ignore:
271  print("Warning, %s does not have Xing information" \
272  % key.__str__())
273  continue
274  else:
275  raise RuntimeError("%s does not have Xing information. Use '--ignoreNoLumiEvents' option to ignore these events and continue." \
276  % key.__str__())
277  if options.weights:
278  weight = float (pieces[weightIndex])
279  else:
280  weight = 1
281  eventsDict.setdefault( key, []).append( (event, weight) )
282  totalWeight += weight
283  events.close()
284  return eventsDict, totalWeight
285 
286 
287 
def loadEvents(filename, cont, options)
General Functions
Definition: generateEDF.py:238
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47

◆ makeEDFplot()

def generateEDF.makeEDFplot (   lumiCont,
  eventsDict,
  totalWeight,
  outputFile,
  options 
)

Definition at line 288 of file generateEDF.py.

References float, WZElectronSkims53X_cff.max, SiStripPI.min, and print().

288 def makeEDFplot (lumiCont, eventsDict, totalWeight, outputFile, options):
289  # make TGraph
290  xVals = [0]
291  yVals = [0]
292  expectedVals = [0]
293  predVals = [0]
294  weight = 0
295  expectedChunks = []
296 
299  if 'time' == options.edfMode:
300  # if we have a minimum run number, clear the lists
301  if lumiCont.minRun or lumiCont.minIntLum:
302  xVals = []
303  yVals = []
304  expectedVals = []
305  predVals = []
306  # loop over events
307  for key, eventList in sorted( eventsDict.items() ):
308  usePoints = True
309  # should we add this point?
310  if lumiCont.minRun and lumiCont.minRun > key[0] or \
311  lumiCont.maxRun and lumiCont.maxRun < key[0]:
312  usePoints = False
313  for event in eventList:
314  weight += event[1]
315  if not usePoints:
316  continue
317  factor = old_div(weight, totalWeight)
318  try:
319  intLum = lumiCont[key].totalRecorded
320  except:
321  raise RuntimeError("key %s not found in lumi information" \
322  % key.__str__())
323  if lumiCont.minIntLum and lumiCont.minIntLum > intLum or \
324  lumiCont.maxIntLum and lumiCont.maxIntLum < intLum:
325  continue
326  lumFrac = old_div(intLum, lumiCont.totalRecLum)
327  xVals.append( lumiCont[key].totalRecorded)
328  yVals.append (factor)
329  expectedVals.append (lumFrac)
330  predVals.append (lumFrac * options.pred)
331  # put on the last point if we aren't giving a maximum run
332  if not lumiCont.maxRun and not lumiCont.maxIntLum:
333  xVals.append (lumiCont.totalRecLum)
334  yVals.append (1)
335  expectedVals.append (1)
336  predVals.append (options.pred)
337 
340  if options.resetExpected:
341  slope = old_div((yVals[-1] - yVals[0]), (xVals[-1] - xVals[0]))
342  print("slope", slope)
343  for index, old in enumerate (expectedVals):
344  expectedVals[index] = yVals[0] + \
345  slope * (xVals[index] - xVals[0])
346 
349  if options.breakExpectedIntLum:
350  breakExpectedIntLum = []
351  for chunk in options.breakExpectedIntLum:
352  pieces = sepRE.split (chunk)
353  try:
354  for piece in pieces:
355  breakExpectedIntLum.append( float(piece) )
356  except:
357  raise RuntimeError("'%s' from '%s' is not a valid float" \
358  % (piece, chunk))
359  breakExpectedIntLum.sort()
360  boundaries = []
361  breakIndex = 0
362  done = False
363  for index, xPos in enumerate (xVals):
364  if xPos > breakExpectedIntLum[breakIndex]:
365  boundaries.append (index)
366  while breakIndex < len (breakExpectedIntLum):
367  breakIndex += 1
368  if breakIndex >= len (breakExpectedIntLum):
369  done = True
370  break
371  # If this next position is different, than
372  # we're golden. Otherwise, let it go through
373  # the loop again.
374  if xPos <= breakExpectedIntLum[breakIndex]:
375  break
376  if done:
377  break
378  # do we have any boundaries?
379  if not boundaries:
380  raise RuntimeError("No values of 'breakExpectedIntLum' are in current range.")
381  # is the first boundary at 0? If not, add 0
382  if boundaries[0]:
383  boundaries.insert (0, 0)
384  # is the last boundary at the end? If not, make the end a
385  # boundary
386  if boundaries[-1] != len (xVals) - 1:
387  boundaries.append( len (xVals) - 1 )
388  rangeList = list(zip (boundaries, boundaries[1:]))
389  for thisRange in rangeList:
390  upper = thisRange[1]
391  lower = thisRange[0]
392  slope = old_div((yVals[upper] - yVals[lower]), \
393  (xVals[upper] - xVals[lower]))
394  print("slope", slope)
395  # now go over the range inclusively
396  pairList = []
397  for index in range (lower, upper + 1):
398  newExpected = yVals[lower] + \
399  slope * (xVals[index] - xVals[lower])
400  pairList.append( (xVals[index], newExpected) )
401  expectedVals[index] = newExpected
402  expectedChunks.append (pairList)
403 
406  elif 'instLum' == options.edfMode or 'instIntLum' == options.edfMode:
407  eventTupList = []
408  if not lumiCont.xingInfo:
409  raise RuntimeError("Luminosity Xing information missing.")
410  for key, eventList in sorted( eventsDict.items() ):
411  try:
412  lumi = lumiCont[key]
413  instLum = lumi.aveInstLum
414  fracAXIL = lumi.fracAXILrecorded
415  totalAXIL = lumi.totalAXILrecorded
416  except:
417  raise RuntimeError("key %s not found in lumi information" \
418  % key.__str__())
419  for event in eventList:
420  eventTupList.append( (instLum, fracAXIL, totalAXIL, key,
421  event[0], event[1], ) )
422  eventTupList.sort()
423  for eventTup in eventTupList:
424  weight += eventTup[5]
425  factor = old_div(weight, totalWeight)
426  if 'instLum' == options.edfMode:
427  xVals.append (eventTup[0])
428  else:
429  xVals.append (eventTup[2])
430  yVals.append (factor)
431  expectedVals.append (eventTup[1])
432  predVals.append (eventTup[1] * options.pred)
433  else:
434  raise RuntimeError("It looks like Charles screwed up if you are seeing this.")
435 
436  size = len (xVals)
437  step = int (old_div(math.sqrt(size), 2) + 1)
438  if options.printValues:
439  for index in range (size):
440  print("%8f %8f %8f" % (xVals[index], yVals[index], expectedVals[index]), end=' ')
441  if index > step:
442  denom = xVals[index] - xVals[index - step]
443  numer = yVals[index] - yVals[index - step]
444  if denom:
445  print(" %8f" % (old_div(numer, denom)), end=' ')
446  if 0 == index % step:
447  print(" **", end=' ')
449  print()
450  print()
451 
452  xArray = array.array ('d', xVals)
453  yArray = array.array ('d', yVals)
454  expected = array.array ('d', expectedVals)
455  graph = ROOT.TGraph( size, xArray, yArray)
456  graph.SetTitle (options.title)
457  graph.SetMarkerStyle (20)
458  expectedGraph = ROOT.TGraph( size, xArray, expected)
459  expectedGraph.SetLineColor (ROOT.kRed)
460  expectedGraph.SetLineWidth (3)
461  if options.noDataPoints:
462  expectedGraph.SetLineStyle (2)
463 
464  # run statistical tests
465  if options.weights:
466  print("average weight per event:", old_div(weight, ( size - 1)))
467  maxDistance = ROOT.TMath.KolmogorovTest (size, yArray,
468  size, expected,
469  "M")
470  prob = ROOT.TMath.KolmogorovProb( maxDistance * math.sqrt( size ) )
471 
472  # display everything
473  ROOT.gROOT.SetStyle('Plain')
474  ROOT.gROOT.SetBatch()
475  c1 = ROOT.TCanvas()
476  graph.GetXaxis().SetRangeUser (min (xVals), max (xVals))
477  minValue = min (min(yVals), min(expected))
478  if options.pred:
479  minValue = min (minValue, min (predVals))
480  graph.GetYaxis().SetRangeUser (minValue,
481  max (max(yVals), max(expected), max(predVals)))
482  graph.SetLineWidth (3)
483  if options.noDataPoints:
484  graph.Draw ("AL")
485  else:
486  graph.Draw ("ALP")
487  if 'instLum' == options.edfMode:
488  graph.GetXaxis().SetTitle ("Average Xing Inst. Luminosity (1/ub/s)")
489  graph.GetXaxis().SetRangeUser (0., lumiCont.max('aveInstLum'))
490  else:
491  if 'instIntLum' == options.edfMode:
492  graph.GetXaxis().SetTitle ("Integrated Luminosity - Inst. Lum. Ordered (1/%s)" \
493  % lumiCont.invunits)
494  else:
495  graph.GetXaxis().SetTitle ("Integrated Luminosity (1/%s)" \
496  % lumiCont.invunits)
497  graph.GetYaxis().SetTitle ("Fraction of Events Seen")
498  expectedGraphs = []
499  if expectedChunks:
500  for index, chunk in enumerate (expectedChunks):
501  expectedXarray = array.array ('d', [item[0] for item in chunk])
502  expectedYarray = array.array ('d', [item[1] for item in chunk])
503  expectedGraph = ROOT.TGraph( len(chunk),
504  expectedXarray,
505  expectedYarray )
506  expectedGraph.SetLineWidth (3)
507  if options.noDataPoints:
508  expectedGraph.SetLineStyle (2)
509  if index % 2:
510  expectedGraph.SetLineColor (ROOT.kBlue)
511  else:
512  expectedGraph.SetLineColor (ROOT.kRed)
513  expectedGraph.Draw("L")
514  expectedGraphs.append (expectedGraph)
515  exptectedGraph = expectedGraphs[0]
516  else:
517  expectedGraph.Draw ("L")
518  green = 0
519  if options.pred:
520  predArray = array.array ('d', predVals)
521  green = ROOT.TGraph (size, xArray, predArray)
522  green.SetLineWidth (3)
523  green.SetLineColor (8)
524  green.Draw ('l')
525  legend = ROOT.TLegend(0.15, 0.65, 0.50, 0.85)
526  legend.SetFillStyle (0)
527  legend.SetLineColor(ROOT.kWhite)
528  observed = 'Observed'
529  if options.weights:
530  observed += ' (weighted)'
531  legend.AddEntry(graph, observed,"PL")
532  if options.resetExpected:
533  legend.AddEntry(expectedGraph, "Expected from partial yield","L")
534  else:
535  legend.AddEntry(expectedGraph, "Expected from total yield","L")
536  if options.pred:
537  legend.AddEntry(green, options.predLabel,"L")
538  legend.AddEntry("","D_{stat}=%.3f, N=%d" % (maxDistance, size),"")
539  legend.AddEntry("","P_{KS}=%.3f" % prob,"")
540  legend.Draw()
541 
542  # save file
543  c1.Print (outputFile)
544 
545 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def makeEDFplot(lumiCont, eventsDict, totalWeight, outputFile, options)
Definition: generateEDF.py:288

Variable Documentation

◆ action

generateEDF.action

Definition at line 574 of file generateEDF.py.

◆ allowedEDF

generateEDF.allowedEDF

## Main

command line options ##

Definition at line 558 of file generateEDF.py.

◆ ArgumentDefaultsHelpFormatter

generateEDF.ArgumentDefaultsHelpFormatter

Definition at line 559 of file generateEDF.py.

◆ choices

generateEDF.choices

Definition at line 608 of file generateEDF.py.

◆ cont

◆ default

generateEDF.default

Definition at line 565 of file generateEDF.py.

◆ description

generateEDF.description

Definition at line 559 of file generateEDF.py.

◆ dest

generateEDF.dest

Definition at line 564 of file generateEDF.py.

◆ done

generateEDF.done

Definition at line 649 of file generateEDF.py.

◆ eventsDict

generateEDF.eventsDict

make EDF plots ##

Definition at line 679 of file generateEDF.py.

◆ False

generateEDF.False

Definition at line 574 of file generateEDF.py.

◆ float

generateEDF.float

Definition at line 580 of file generateEDF.py.

Referenced by makeEDFplot().

◆ formatter_class

generateEDF.formatter_class

Definition at line 559 of file generateEDF.py.

◆ help

generateEDF.help

Definition at line 566 of file generateEDF.py.

◆ inputGroup

generateEDF.inputGroup

Definition at line 562 of file generateEDF.py.

◆ int

generateEDF.int

Definition at line 576 of file generateEDF.py.

Referenced by loadEvents().

◆ maxIntLum

generateEDF.maxIntLum

Definition at line 624 of file generateEDF.py.

◆ maxRun

generateEDF.maxRun

Definition at line 622 of file generateEDF.py.

◆ metavar

generateEDF.metavar

Definition at line 609 of file generateEDF.py.

◆ minIntLum

generateEDF.minIntLum

Definition at line 623 of file generateEDF.py.

◆ minRun

generateEDF.minRun

Definition at line 621 of file generateEDF.py.

◆ modeGroup

generateEDF.modeGroup

Definition at line 563 of file generateEDF.py.

◆ nargs

generateEDF.nargs

Definition at line 610 of file generateEDF.py.

◆ nonSpaceRE

generateEDF.nonSpaceRE

Definition at line 16 of file generateEDF.py.

◆ options

generateEDF.options

Definition at line 612 of file generateEDF.py.

◆ parser

generateEDF.parser

Definition at line 559 of file generateEDF.py.

◆ pieces

generateEDF.pieces

Definition at line 633 of file generateEDF.py.

◆ plotGroup

generateEDF.plotGroup

Definition at line 560 of file generateEDF.py.

◆ prevRecLumi

generateEDF.prevRecLumi

Definition at line 648 of file generateEDF.py.

◆ rangeGroup

generateEDF.rangeGroup

Definition at line 561 of file generateEDF.py.

◆ recLumIndex

generateEDF.recLumIndex

Definition at line 646 of file generateEDF.py.

◆ recLumis

generateEDF.recLumis

look for which runs correspond to what total ## recorded integrated luminosity ##

Definition at line 631 of file generateEDF.py.

◆ recLumValue

generateEDF.recLumValue

Definition at line 636 of file generateEDF.py.

◆ sepRE

generateEDF.sepRE

Definition at line 15 of file generateEDF.py.

◆ str

generateEDF.str

Definition at line 588 of file generateEDF.py.

◆ totalWeight

◆ type

generateEDF.type

Definition at line 564 of file generateEDF.py.

◆ usage

generateEDF.usage

Definition at line 559 of file generateEDF.py.