ATLAS Offline Software
BunchGroupSet.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
3 from collections import OrderedDict as odict
4 from itertools import groupby
5 
6 from AthenaCommon.Logging import logging
7 from AthenaConfiguration.Enums import BeamType
8 
9 from .Limits import Limits
10 from .L1MenuFlags import L1MenuFlags
11 
12 
13 log = logging.getLogger(__name__)
14 
16  """
17  create BunchGroupSet for simulation
18 
19  This BGS is independent from the menu, and contains only BG0 (BCRVeto) and BG1 (Paired)
20  """
21  bgs = BunchGroupSet("MC")
22  bgs.addBunchGroup( BunchGroupSet.BunchGroup(name = 'BCRVeto', internalNumber = 0).addRange(0,3539).addRange(3561,3563).normalize() )\
23  .addBunchGroup( BunchGroupSet.BunchGroup(name = 'Paired', internalNumber = 1).addTrain(0,3564).normalize() )
24  return bgs
25 
26 
28  """
29  sets default bunchgroups for all menus, needed for simulation.
30  """
31  if hasattr(L1MenuFlags, "BunchGroupNames"): # if flag has been set
32  # if menu defines bunchgroup names, then we generate a bunchgroup set from that
33  name = L1MenuFlags.MenuSetup().partition('_')[0]
34  bgs = BunchGroupSet(name)
35  bgs.addBunchGroup( BunchGroupSet.BunchGroup(name = 'BCRVeto', internalNumber = 0).addRange(0,3539).addRange(3561,3563).normalize() )\
36  .addBunchGroup( BunchGroupSet.BunchGroup(name = 'Paired', internalNumber = 1).addTrain(0,3445).addTrain(3536,4).addTrain(3561,3).normalize() )
37  if flags.Beam.Type is BeamType.Cosmics:
38  bgs.addBunchGroup( BunchGroupSet.BunchGroup(name = 'EMPTY', internalNumber = 3).addTrain(0,3564).normalize() )
39 
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
44  else:
46 
47  return bgs
48 
49 
51 
53  nameUndefBG = "NotUsed"
54  def __init__(self, internalNumber, name = nameUndefBG, partition=0, bunches=None):
55  # check if internalNumber is within limits
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))
58  self.name = name # name of the bunchgroup
59  self.internalNumber = internalNumber # number between 0 and 15
60  self.menuPartition = partition # CTP partition that the bunchgroup belongs to
61  self.bunches = bunches if bunches else [] # list of bcids
62  self.normalized = [] # list of trains (filled in normalized())
63  def __str__(self):
64  return "bunchgroup %s(%i) with %i bunches" % (self.name, self.internalNumber, len(self))
65  def __len__(self):
66  return len(self.bunches)
67  def addBunch(self, bcid):
68  self.bunches += [ bcid ]
69  return self
70  def addRange(self, firstbcid, lastbcid):
71  self.bunches += list(range(firstbcid, lastbcid+1))
72  return self
73  def addTrain(self, firstbcid, length):
74  self.bunches += list(range(firstbcid, firstbcid+length))
75  return self
76  def normalize(self):
77  """ turn list of bunches into trains """
78  self.normalized = []
79  self.bunches = sorted(list(set(self.bunches))) # uniq and sort
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]):
83  train = list(g)
84  self.normalized += [ (train[0][1], len(train)) ]
85  return self
86  def json(self):
87  confObj = odict()
88  confObj["name"] = self.name
89  confObj["id"] = self.internalNumber
90  confObj["info"] = "%i bunches, %i groups" % (len(self), len(self.normalized))
91  confObj["bcids"] = []
92  for first, length in self.normalized:
93  confObj["bcids"] += [ odict( [ ("first", first), ("length", length) ] ) ]
94  return confObj
95 
96 
97  def __init__(self, name='', menuPartition=0):
98  self.name = name
99  self.menuPartition = menuPartition
100  self.bunchGroups = [ BunchGroupSet.BunchGroup(i) for i in range(0,Limits.NumBunchgroups) ] # initialize all BunchGroups
101 
102  def __len__(self):
103  return len(self.bunchGroups)
104 
105 
106  @classmethod
107  def partitioning(cls):
108  first = L1MenuFlags.BunchGroupPartitioning()
109  last = first[1:] + [ Limits.NumBunchgroups ]
110  partitioning = dict( zip([1,2,3],zip(first,last)) )
111  return partitioning
112 
113 
114  def resize(self, newsize):
115  if type(self.bunchGroups) is not list:
116  self.bunchGroups = []
117  self.bunchGroups += newsize * [None]
118  self.bunchGroups = self.bunchGroups[:newsize]
119 
120 
121  def addBunchGroup(self, bunchGroup):
122  # check if one already exists with this number
123  doesExist = self.bunchGroups[bunchGroup.internalNumber].name != BunchGroupSet.BunchGroup.nameUndefBG
124  if doesExist:
125  raise RuntimeError("Adding bunchgroup with internal number %i, which already exists" % bunchGroup.internalNumber)
126 
127  partition=0
128  if hasattr(L1MenuFlags, "BunchGroupPartitioning"):
129  for lowestBG in L1MenuFlags.BunchGroupPartitioning():
130  if bunchGroup.internalNumber >= lowestBG:
131  partition += 1
132  bunchGroup.partition = partition
133  self.bunchGroups[bunchGroup.internalNumber] = bunchGroup
134  return self
135 
136 
137  def json(self):
138  confObj = odict()
139  for bg in self.bunchGroups:
140  confObj["BGRP%i" % bg.internalNumber] = bg.json()
141  return confObj
142 
143  def writeJSON(self, outputFile, destdir="./", pretty=True):
144  outputFile = destdir.rstrip('/') + '/' + outputFile
145  confObj = odict()
146  confObj["name"] = self.name
147  confObj["filetype"] = "bunchgroupset"
148  confObj["bunchGroups"] = self.json()
149  with open( outputFile, mode="wt" ) as fh:
150  import json
151  json.dump(confObj, fh, indent = 4 if pretty else None, separators=(',', ': '))
152  log.info("Wrote %s", outputFile)
153  return outputFile
python.L1.Base.BunchGroupSet.BunchGroupSet.bunchGroups
bunchGroups
Definition: BunchGroupSet.py:100
python.L1.Base.BunchGroupSet.BunchGroupSet.BunchGroup.__init__
def __init__(self, internalNumber, name=nameUndefBG, partition=0, bunches=None)
Definition: BunchGroupSet.py:54
python.L1.Base.BunchGroupSet.BunchGroupSet
Definition: BunchGroupSet.py:50
python.L1.Base.BunchGroupSet.BunchGroupSet.BunchGroup.addTrain
def addTrain(self, firstbcid, length)
Definition: BunchGroupSet.py:73
python.L1.Base.BunchGroupSet.BunchGroupSet.BunchGroup.bunches
bunches
Definition: BunchGroupSet.py:61
python.L1.Base.BunchGroupSet.BunchGroupSet.__len__
def __len__(self)
Definition: BunchGroupSet.py:102
python.L1.Base.BunchGroupSet.BunchGroupSet.BunchGroup.__str__
def __str__(self)
Definition: BunchGroupSet.py:63
python.L1.Base.BunchGroupSet.createDefaultBunchGroupSet
def createDefaultBunchGroupSet(flags)
Definition: BunchGroupSet.py:27
python.L1.Base.BunchGroupSet.BunchGroupSet.resize
def resize(self, newsize)
Definition: BunchGroupSet.py:114
python.L1.Base.BunchGroupSet.createDefaultBunchGroupSetMC
def createDefaultBunchGroupSetMC()
Definition: BunchGroupSet.py:15
normalize
Double_t normalize(TF1 *func, Double_t *rampl=NULL, Double_t from=0., Double_t to=0., Double_t step=1.)
Definition: LArPhysWaveHECTool.cxx:825
python.L1.Base.BunchGroupSet.BunchGroupSet.menuPartition
menuPartition
Definition: BunchGroupSet.py:99
python.L1.Base.BunchGroupSet.BunchGroupSet.BunchGroup.normalize
def normalize(self)
Definition: BunchGroupSet.py:76
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
python.L1.Base.BunchGroupSet.BunchGroupSet.BunchGroup.addRange
def addRange(self, firstbcid, lastbcid)
Definition: BunchGroupSet.py:70
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
python.L1.Base.BunchGroupSet.BunchGroupSet.BunchGroup.internalNumber
internalNumber
Definition: BunchGroupSet.py:59
python.L1.Base.BunchGroupSet.BunchGroupSet.BunchGroup.__len__
def __len__(self)
Definition: BunchGroupSet.py:65
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:232
python.L1.Base.BunchGroupSet.BunchGroupSet.addBunchGroup
def addBunchGroup(self, bunchGroup)
Definition: BunchGroupSet.py:121
python.L1.Base.BunchGroupSet.BunchGroupSet.BunchGroup
Definition: BunchGroupSet.py:52
python.L1.Base.BunchGroupSet.BunchGroupSet.BunchGroup.addBunch
def addBunch(self, bcid)
Definition: BunchGroupSet.py:67
python.L1.Base.BunchGroupSet.BunchGroupSet.BunchGroup.json
def json(self)
Definition: BunchGroupSet.py:86
python.L1.Base.BunchGroupSet.BunchGroupSet.BunchGroup.menuPartition
menuPartition
Definition: BunchGroupSet.py:60
python.L1.Base.BunchGroupSet.BunchGroupSet.json
def json(self)
Definition: BunchGroupSet.py:137
Trk::open
@ open
Definition: BinningType.h:40
python.L1.Base.BunchGroupSet.BunchGroupSet.name
name
Definition: BunchGroupSet.py:98
python.L1.Base.BunchGroupSet.BunchGroupSet.partitioning
def partitioning(cls)
Definition: BunchGroupSet.py:107
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
StateLessPT_NewConfig.partition
partition
Definition: StateLessPT_NewConfig.py:49
pickleTool.object
object
Definition: pickleTool.py:30
python.L1.Base.BunchGroupSet.BunchGroupSet.writeJSON
def writeJSON(self, outputFile, destdir="./", pretty=True)
Definition: BunchGroupSet.py:143
python.L1.Base.BunchGroupSet.BunchGroupSet.BunchGroup.name
name
Definition: BunchGroupSet.py:58
python.L1.Base.BunchGroupSet.BunchGroupSet.__init__
def __init__(self, name='', menuPartition=0)
Definition: BunchGroupSet.py:97
python.L1.Base.BunchGroupSet.BunchGroupSet.BunchGroup.normalized
normalized
Definition: BunchGroupSet.py:62