ATLAS Offline Software
trigbs_prescaleL1.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
4 """
5 Module for applying L1 prescales to the input data
6 
7 Used as a "-Z" plugin to athenaHLT:
8 athenaHLT -Z TrigByteStreamTools.trigbs_prescaleL1 ...
9 """
10 
11 import random
12 import zlib
13 import sys
14 from AthenaCommon.Logging import logging
15 
16 import eformat
17 
18 import cppyy
19 cppyy.load_library('libTrigByteStreamToolsDict')
20 from ROOT import CTPdataformat
21 from TrigByteStreamTools import CTPfragment
22 
23 prescales = None
24 log = logging.getLogger('trigbs_prescaleL1.py')
25 log.setLevel(logging.INFO)
26 
27 def loadPrescales(config):
28  """Load L1 prescales from DB"""
29 
30  database = config['db-server']
31  l1psk = int(config['db-extra']['lvl1key'])
32 
33  log.info('Applying L1 prescale set %s from %s', l1psk, database)
34  from TrigConfigSvc.TrigConfigSvcUtils import getL1Prescales
35  name, prescales = getL1Prescales(database,l1psk)
36  log.debug('Prescales: %s', str(prescales))
37  return prescales
38 
39 
40 def modify_general(**kwargs):
41  """Prescale L1 items in CTP fragment used by HLT"""
42  global prescales
43 
44  if prescales is None:
45  prescales = loadPrescales(kwargs['configuration'])
46 
47  event = kwargs["event"]
48 
49  # Set seed based on event time stamp to make it reproducible
50  seed = '%s%s' % (event.bc_time_seconds(),event.bc_time_nanoseconds())
51  random.seed(zlib.crc32(seed))
52 
53  new_event=eformat.write.FullEventFragment()
54  new_event.copy_header(event)
55  for rob in event:
56  if rob.source_id()!=eformat.helper.SourceIdentifier(eformat.helper.SubDetector.TDAQ_CTP,1):
57  new_event.append_unchecked(rob)
58  else:
59  data = [d for d in rob.rod_data()]
60  v = CTPfragment.ctpFormatVersion(rob)
61  TBPpos = CTPfragment._versioned(CTPdataformat,'TBPpos',v)
62  TBPwords = CTPfragment._versioned(CTPdataformat,'TBPwords',v)
63  TAVpos = CTPfragment._versioned(CTPdataformat,'TAVpos',v)
64 
65  L1TBP = CTPfragment.decodeTriggerBits(data[TBPpos:TBPpos+TBPwords])
66  log.debug('L1TBP: %s', L1TBP)
67  newL1TAV = []
68  for ctp in L1TBP:
69  if prescales[ctp]<=0:
70  continue
71  if random.uniform(0,prescales[ctp])>1:
72  continue
73  newL1TAV.append(ctp)
74 
75  log.debug('New L1TAV: %s', newL1TAV)
76  newL1TAVBits = CTPfragment.encodeTriggerBits(newL1TAV,TBPwords)
77  for i,value in enumerate(newL1TAVBits):
78  data[TAVpos+i] = value
79 
80  # Write new CTP ROB
81  newrob = eformat.write.ROBFragment(rob)
82  newrob.rod_data(data)
83  new_event.append(newrob)
84  # Update event header
85  l1bits = [b for b in event.lvl1_trigger_info()]
86  new_event.lvl1_trigger_info(l1bits[0:TBPwords]+data[TAVpos:TAVpos+TBPwords]*2)
87 
88  return new_event.readonly()
89 
90 
91 
92 if __name__ == "__main__":
93  if len(sys.argv)<=1:
94  print("Syntax: trigbs_prescaleL1.py FILE")
95  sys.exit(1)
96 
97  log.setLevel(logging.DEBUG)
98  kwargs = {'configuration' : {'db-server':'TRIGGERDB_RUN1',
99  'db-extra' : {'lvl1key' : 300}}}
100 
101  kwargs = {'configuration' : {'db-server':'TRIGGERDBREPR',
102  'db-extra' : {'lvl1key' : 30}}}
103 
104  ost = eformat.ostream()
105  for e in eformat.istream(sys.argv[1]):
106  kwargs['event'] = e
107  new_event = modify_general(**kwargs)
108  ost.write(new_event)
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
python.trigbs_prescaleL1.loadPrescales
def loadPrescales(config)
Definition: trigbs_prescaleL1.py:27
python.trigbs_prescaleL1.modify_general
def modify_general(**kwargs)
Definition: trigbs_prescaleL1.py:40
python.utils.AtlRunQueryTriggerUtils.getL1Prescales
def getL1Prescales(l1prescalekey, run_number)
Definition: AtlRunQueryTriggerUtils.py:562
str
Definition: BTagTrackIpAccessor.cxx:11