3 from collections
import OrderedDict
as odict
4 from itertools
import groupby
6 from AthenaCommon.Logging
import logging
7 from AthenaConfiguration.Enums
import BeamType
9 from .Limits
import Limits
10 from .L1MenuFlags
import L1MenuFlags
13 log = logging.getLogger(__name__)
17 create BunchGroupSet for simulation
19 This BGS is independent from the menu, and contains only BG0 (BCRVeto) and BG1 (Paired)
29 sets default bunchgroups for all menus, needed for simulation.
31 if hasattr(L1MenuFlags,
"BunchGroupNames"):
33 name = L1MenuFlags.MenuSetup().
partition(
'_')[0]
37 if flags.Beam.Type
is BeamType.Cosmics:
40 bunchgroupnames = L1MenuFlags.BunchGroupNames()[:Limits.NumBunchgroups]
41 bunchgroupnames += [
'NotUsed'] * (Limits.NumBunchgroups - len(bunchgroupnames))
42 for i,bgname
in enumerate(bunchgroupnames):
43 bgs.bunchGroups[i].name = bgname
53 nameUndefBG =
"NotUsed"
54 def __init__(self, internalNumber, name = nameUndefBG, partition=0, bunches=None):
56 if internalNumber<0
or internalNumber >= Limits.NumBunchgroups:
57 raise RuntimeError(
'Cannot create bunchgroup with internal number %i (must be between 0 and %i)' % (internalNumber, Limits.NumBunchgroups-1))
64 return "bunchgroup %s(%i) with %i bunches" % (self.
name, self.
internalNumber, len(self))
77 """ turn list of bunches into trains """
80 if any(map(
lambda bcid : bcid<0
or bcid >= 3564, self.
bunches)):
81 raise RuntimeError(
"Found bcid outside range 0..3563 in bunchgroup %s" % self.
name)
82 for k,g
in groupby(enumerate(self.
bunches),
lambda x : x[1]-x[0]):
84 self.
normalized += [ (train[0][1], len(train)) ]
88 confObj[
"name"] = self.
name
90 confObj[
"info"] =
"%i bunches, %i groups" % (len(self), len(self.
normalized))
93 confObj[
"bcids"] += [ odict( [ (
"first", first), (
"length", length) ] ) ]
108 first = L1MenuFlags.BunchGroupPartitioning()
109 last = first[1:] + [ Limits.NumBunchgroups ]
110 partitioning = dict( zip([1,2,3],zip(first,last)) )
123 doesExist = self.
bunchGroups[bunchGroup.internalNumber].name != BunchGroupSet.BunchGroup.nameUndefBG
125 raise RuntimeError(
"Adding bunchgroup with internal number %i, which already exists" % bunchGroup.internalNumber)
128 if hasattr(L1MenuFlags,
"BunchGroupPartitioning"):
129 for lowestBG
in L1MenuFlags.BunchGroupPartitioning():
130 if bunchGroup.internalNumber >= lowestBG:
132 bunchGroup.partition = partition
133 self.
bunchGroups[bunchGroup.internalNumber] = bunchGroup
140 confObj[
"BGRP%i" % bg.internalNumber] = bg.json()
143 def writeJSON(self, outputFile, destdir="./", pretty=True):
144 outputFile = destdir.rstrip(
'/') +
'/' + outputFile
146 confObj[
"name"] = self.
name
147 confObj[
"filetype"] =
"bunchgroupset"
148 confObj[
"bunchGroups"] = self.
json()
149 with open( outputFile, mode=
"wt" )
as fh:
151 json.dump(confObj, fh, indent = 4
if pretty
else None, separators=(
',',
': '))
152 log.info(
"Wrote %s", outputFile)