ATLAS Offline Software
TriggerAPI.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
3 __author__ = 'Javier Montejo'
4 __version__="$Revision: 2.0 $"
5 __doc__="Interface to retrieve lists of unprescaled triggers according to types and periods"
6 
7 import sys
8 from TriggerMenuMT.TriggerAPI.TriggerInfo import TriggerInfo
9 from TriggerMenuMT.TriggerAPI.TriggerEnums import TriggerPeriod, TriggerType
10 from TriggerMenuMT.TriggerAPI import SerializeAPI
11 from AthenaCommon.Logging import logging
12 
13 class TriggerAPI:
14  log = logging.getLogger(__name__)
15  dbQueries = {}
16  customGRL = None
17  flags = None
18  release = None
19  cacheread = False
20 
21  @classmethod
22  def reset(cls):
23  cls.dbQueries = {}
24  cls.customGRL = None
25  cls.flags = None
26  cls.release = None
27  cls.cacheread = False
28 
29  @classmethod
30  def init(cls):
31  if cls.cacheread: return
32  cls.cacheread = True
33  cls.dbQueries = SerializeAPI.load()
34  todel = [(p,grl) for p,grl in cls.dbQueries if p & TriggerPeriod.future]
35  for key in todel:
36  del cls.dbQueries[key]
37 
38  @classmethod
39  def setConfigFlags(cls, flags):
40  """Set ConfigFlags (only required for "future" periods)"""
41  cls.flags = flags
42 
43  @classmethod
44  def setRelease(cls, release):
45  import re
46  if release and re.match(r'21\.1(\.[0-9]+)+$',release):
47  cls.release = release
48  elif release=="current": #Don't allow the release to be automatically overwritten
49  cls.release = release
50  else:
51  cls.log.warning("Release doesn't seem to be a well-formed 21.1 release, ignoring: "+release)
52 
53  @classmethod
54  def setCustomGRL(cls, grl):
55  if TriggerInfo.testCustomGRL(grl):
56  cls.customGRL = grl
57  else:
58  cls.log.warning("Couldn't set GRL: "+grl)
59  cls.log.warning("Will use default GRL")
60  cls.customGRL = None
61 
62  @classmethod
63  def getLowestUnprescaled(cls, period, triggerType=TriggerType.ALL, additionalTriggerType=TriggerType.UNDEFINED, matchPattern="", livefraction=1.0, reparse=False):
64  ''' Returns a list of the lowest-pt-threshold HLT chains that were always unprescaled in the given period.
65  period: see TriggerEnums.TriggerPeriod for all possibilities, recommeded TriggerPeriod.y2018
66  triggerType: see TriggerEnums.TriggerType for all possibilities, example TriggerType.el_single
67  additionalTriggerType: can request additional types to match, use TriggerType.ALL to show combined triggers of any kind
68  accepts also a list as input in that case all types have to match
69  matchPattern: provide additionally a regex-like expression to be applied
70  livefraction: accept items that are not unprescaled but have a live fraction above this threshold, example 0.95
71  The live fraction is only an approximation, weighting the number of lumiblocks by prescale.
72  '''
73  cls._loadTriggerPeriod(period,reparse)
74  return cls.dbQueries[(period,cls.customGRL)]._getLowestUnprescaled(triggerType, additionalTriggerType, matchPattern, livefraction)
75 
76  @classmethod
77  def getLowestUnprescaledAnyPeriod(cls, period, triggerType=TriggerType.ALL, additionalTriggerType=TriggerType.UNDEFINED, matchPattern="", livefraction=1.0, reparse=False):
78  ''' Returns a list of the lowest-pt-threshold HLT chains that were unprescaled in at least one of
79  the subperiods within the given period. The lowest granularity can be seen in TriggerEnums.TriggerPeriod
80  See getLowestUnprescaled for a detailed description of the options
81  '''
82  lowset = set()
83  for i, ibin in enumerate(reversed(bin(period)[2:])): #to binary
84  ibin = int(ibin)
85  if not ibin: continue
86  subperiod = 2**i
87  cls._loadTriggerPeriod(subperiod, reparse)
88  subperiodset = set( cls.dbQueries[(subperiod,cls.customGRL)]._getLowestUnprescaled(triggerType, additionalTriggerType, matchPattern, livefraction) )
89  lowset |= subperiodset
90  return list(lowset)
91 
92  @classmethod
93  def getUnprescaled(cls, period, triggerType=TriggerType.ALL, additionalTriggerType=TriggerType.UNDEFINED, matchPattern="", livefraction=1.0, reparse=False):
94  ''' Returns a list of always-unprescaled HLT chains, including backup items with higher thresholds.
95  See getLowestUnprescaled for a detailed description of the options
96  '''
97  return cls.dbQueries[(period,cls.customGRL)]._getUnprescaled(triggerType, additionalTriggerType, matchPattern, livefraction)
98 
99  @classmethod
100  def getUnprescaledAnyPeriod(cls, period, triggerType=TriggerType.ALL, additionalTriggerType=TriggerType.UNDEFINED, matchPattern="", livefraction=1.0, reparse=False):
101  ''' Returns a list of HLT chains that were unprescaled in at least one of
102  the subperiods within the given period. The lowest granularity can be seen in TriggerEnums.TriggerPeriod
103  See getLowestUnprescaled for a detailed description of the options
104  '''
105  lowset = set()
106  for i, ibin in enumerate(reversed(bin(period)[2:])): #to binary
107  ibin = int(ibin)
108  if not ibin: continue
109  subperiod = 2**i
110  cls._loadTriggerPeriod(subperiod, reparse)
111  subperiodset = set( cls.dbQueries[(subperiod,cls.customGRL)]._getUnprescaled(triggerType, additionalTriggerType, matchPattern, livefraction) )
112  lowset |= subperiodset
113  return list(lowset)
114 
115  @classmethod
116  def getInactive(cls, period, triggerType=TriggerType.ALL, additionalTriggerType=TriggerType.UNDEFINED, matchPattern="", livefraction=1e-99, reparse=False):
117  ''' Returns a list of HLT chains that were fully inactive, excluding disabled chains in rerun.
118  See getLowestUnprescaled for a detailed description of the options
119  '''
120  cls._loadTriggerPeriod(period,reparse)
121  return cls.dbQueries[(period,cls.customGRL)]._getInactive(triggerType, additionalTriggerType, matchPattern, livefraction)
122 
123  @classmethod
124  def getActive(cls, period, triggerType=TriggerType.ALL, additionalTriggerType=TriggerType.UNDEFINED, matchPattern="", livefraction=1e-99, reparse=False):
125  ''' Returns a list of HLT chains that were active at some point, including disabled chains in rerun.
126  See getLowestUnprescaled for a detailed description of the options
127  '''
128  cls._loadTriggerPeriod(period,reparse)
129  return cls.dbQueries[(period,cls.customGRL)]._getActive(triggerType, additionalTriggerType, matchPattern, livefraction)
130 
131  @classmethod
132  def getAllHLT(cls, period, triggerType=TriggerType.ALL, additionalTriggerType=TriggerType.UNDEFINED, matchPattern="", livefraction=0, reparse=False):
133  ''' Returns a map of {HLT chains: average live fraction} for a given period.
134  The average live fraction is an approximation weighting the number of lumiblocks by prescale.
135  *** Don't use this number in analysis!!! ***
136  See getLowestUnprescaled for a detailed description of the options
137  '''
138  cls._loadTriggerPeriod(period,reparse)
139  return cls.dbQueries[(period,cls.customGRL)]._getAllHLT(triggerType, additionalTriggerType, matchPattern, livefraction)
140 
141  @classmethod
142  def checkPeriodConsistency(cls, period=TriggerPeriod.future, triggerType=TriggerType.ALL, additionalTriggerType=TriggerType.UNDEFINED, matchPattern="", reparse=False):
143  ''' Returns a list of triggers that are tighter than the lowest unprescaled but are not flagged as primary
144  This only makes sense for future periods, the past is already consistent :)
145  See getLowestUnprescaled for a detailed description of the options
146  '''
147  period &= TriggerPeriod.future #Only meaningful for future periods
148  cls._loadTriggerPeriod(period,reparse)
149  return cls.dbQueries[(period,cls.customGRL)]._checkPeriodConsistency(triggerType, additionalTriggerType, matchPattern)
150 
151  @classmethod
152  def _loadTriggerPeriod(cls, period, reparse):
153  if period != TriggerPeriod.customGRL and not period & TriggerPeriod.future: cls.init()
154  if (period,cls.customGRL) not in cls.dbQueries:
155  if TriggerPeriod.isRunNumber(period) or (isinstance(period,TriggerPeriod) and period.isBasePeriod()):
156  cls.dbQueries[(period,cls.customGRL)] = TriggerInfo(period,cls.customGRL,cls.release,cls.flags)
157  else:
158  basePeriods = [tp for tp in TriggerPeriod.basePeriods() if tp & period]
159  for bp in basePeriods:
160  cls._loadTriggerPeriod(bp,reparse)
161  cls.dbQueries[(period,cls.customGRL)] = TriggerInfo.merge([cls.dbQueries[(bp,cls.customGRL)] for bp in basePeriods])
162  if reparse: cls.dbQueries[(period,cls.customGRL)].reparse()
163 
164  @classmethod
165  def dumpAPI(cls, full=False):
166  if not full: #store only what is not in the central pickle/json
167  for period,grl in list(cls.dbQueries.keys()):
168  if TriggerPeriod.isRunNumber(period) or (isinstance(period,TriggerPeriod) and period.isBasePeriod()): continue
169  del cls.dbQueries[(period,grl)]
170  SerializeAPI.dump(cls.dbQueries)
171  cls.log.info(sorted(cls.dbQueries.keys()))
172 
173 
174 def main(dumpFullAPI=False):
175  ''' Run some tests or dump the full pickle/json for CalibPath '''
176  log = logging.getLogger(__name__)
177 
178  if dumpFullAPI:
179  for triggerPeriod in TriggerPeriod:
180  unprescaled = TriggerAPI.getLowestUnprescaled(triggerPeriod,TriggerType.mu_single)
181  log.info(triggerPeriod)
182  log.info(sorted(unprescaled))
183  #Cache also one run for the example script
184  unprescaled = TriggerAPI.getLowestUnprescaled(337833,TriggerType.mu_single)
185  log.info(337833)
186  log.info(sorted(unprescaled))
187  TriggerAPI.dumpAPI(full=True)
188  else:
189  try: period = int(sys.argv[1])
190  except Exception: period = TriggerPeriod.y2018
191  for triggerType in TriggerType:
192  unprescaled = TriggerAPI.getLowestUnprescaled(period,triggerType)
193  log.info(triggerType)
194  log.info(sorted(unprescaled))
195 
196 if __name__ == "__main__":
197  dumpFullAPI = ("dumpFullAPI" in sys.argv)
198  sys.exit(main(dumpFullAPI))
199 
grepfile.info
info
Definition: grepfile.py:38
python.TriggerAPI.TriggerAPI.TriggerAPI.init
def init(cls)
Definition: TriggerAPI.py:30
python.TriggerAPI.TriggerAPI.main
def main(dumpFullAPI=False)
Definition: TriggerAPI.py:174
python.TriggerAPI.TriggerAPI.TriggerAPI.flags
flags
Definition: TriggerAPI.py:17
python.TriggerAPI.TriggerAPI.TriggerAPI.cacheread
cacheread
Definition: TriggerAPI.py:19
python.TriggerAPI.TriggerAPI.TriggerAPI.getActive
def getActive(cls, period, triggerType=TriggerType.ALL, additionalTriggerType=TriggerType.UNDEFINED, matchPattern="", livefraction=1e-99, reparse=False)
Definition: TriggerAPI.py:124
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
python.TriggerAPI.TriggerAPI.TriggerAPI.setRelease
def setRelease(cls, release)
Definition: TriggerAPI.py:44
python.TriggerAPI.TriggerAPI.TriggerAPI.getUnprescaledAnyPeriod
def getUnprescaledAnyPeriod(cls, period, triggerType=TriggerType.ALL, additionalTriggerType=TriggerType.UNDEFINED, matchPattern="", livefraction=1.0, reparse=False)
Definition: TriggerAPI.py:100
python.TriggerAPI.TriggerAPI.TriggerAPI.getInactive
def getInactive(cls, period, triggerType=TriggerType.ALL, additionalTriggerType=TriggerType.UNDEFINED, matchPattern="", livefraction=1e-99, reparse=False)
Definition: TriggerAPI.py:116
bin
Definition: BinsDiffFromStripMedian.h:43
python.TriggerAPI.TriggerAPI.TriggerAPI.dumpAPI
def dumpAPI(cls, full=False)
Definition: TriggerAPI.py:165
python.TriggerAPI.TriggerAPI.TriggerAPI.getLowestUnprescaledAnyPeriod
def getLowestUnprescaledAnyPeriod(cls, period, triggerType=TriggerType.ALL, additionalTriggerType=TriggerType.UNDEFINED, matchPattern="", livefraction=1.0, reparse=False)
Definition: TriggerAPI.py:77
python.TriggerAPI.TriggerAPI.TriggerAPI.reset
def reset(cls)
Definition: TriggerAPI.py:22
python.TriggerAPI.TriggerAPI.TriggerAPI.getUnprescaled
def getUnprescaled(cls, period, triggerType=TriggerType.ALL, additionalTriggerType=TriggerType.UNDEFINED, matchPattern="", livefraction=1.0, reparse=False)
Definition: TriggerAPI.py:93
python.TriggerAPI.TriggerAPI.TriggerAPI.release
release
Definition: TriggerAPI.py:18
python.TriggerAPI.TriggerAPI.TriggerAPI
Definition: TriggerAPI.py:13
python.TriggerAPI.TriggerAPI.TriggerAPI.dbQueries
dbQueries
Definition: TriggerAPI.py:15
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
python.TriggerAPI.TriggerAPI.TriggerAPI.getLowestUnprescaled
def getLowestUnprescaled(cls, period, triggerType=TriggerType.ALL, additionalTriggerType=TriggerType.UNDEFINED, matchPattern="", livefraction=1.0, reparse=False)
Definition: TriggerAPI.py:63
python.TriggerAPI.TriggerAPI.TriggerAPI.customGRL
customGRL
Definition: TriggerAPI.py:16
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.TriggerAPI.TriggerAPI.TriggerAPI._loadTriggerPeriod
def _loadTriggerPeriod(cls, period, reparse)
Definition: TriggerAPI.py:152
python.TriggerAPI.TriggerAPI.TriggerAPI.checkPeriodConsistency
def checkPeriodConsistency(cls, period=TriggerPeriod.future, triggerType=TriggerType.ALL, additionalTriggerType=TriggerType.UNDEFINED, matchPattern="", reparse=False)
Definition: TriggerAPI.py:142
python.TriggerAPI.TriggerAPI.TriggerAPI.log
log
Definition: TriggerAPI.py:14
python.TriggerAPI.TriggerAPI.TriggerAPI.setCustomGRL
def setCustomGRL(cls, grl)
Definition: TriggerAPI.py:54
python.TriggerAPI.TriggerAPI.TriggerAPI.setConfigFlags
def setConfigFlags(cls, flags)
Definition: TriggerAPI.py:39
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:798
python.TriggerAPI.TriggerAPI.TriggerAPI.getAllHLT
def getAllHLT(cls, period, triggerType=TriggerType.ALL, additionalTriggerType=TriggerType.UNDEFINED, matchPattern="", livefraction=0, reparse=False)
Definition: TriggerAPI.py:132