TrigConfigSvcCfg.py 11.4 KB
Newer Older
1
# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
Joerg Stelzer's avatar
Joerg Stelzer committed
2
3
4

from PyUtils.Decorators import memoize
from AthenaCommon.Logging import logging
5
6
7
from collections import OrderedDict as odict
import json

Joerg Stelzer's avatar
Joerg Stelzer committed
8
9
def getHLTPrescaleFolderName():
    return "/TRIGGER/HLT/PrescaleKey <tag>HEAD</tag>"
10

Joerg Stelzer's avatar
Joerg Stelzer committed
11
12
13
14
15
16
17
18
19
20
21
22

# L1 Json file name 
def getL1MenuFileName( flags=None ):
    if flags is None:
        from TriggerJobOpts.TriggerFlags import TriggerFlags as tf
        l1MenuFileName = 'L1Menu_'+tf.triggerMenuSetup()+'_'+tf.menuVersion()+'.json'
    else:
        l1MenuFileName = 'L1Menu_'+flags.Trigger.triggerMenuSetup+'_'+flags.Trigger.menuVersion+'.json'
    l1MenuFileName = l1MenuFileName.replace(".xml",".json").replace("LVL1config", "L1Menu")
    l1MenuFileName = l1MenuFileName.replace("_newJO","")
    return l1MenuFileName

23

Joerg Stelzer's avatar
Joerg Stelzer committed
24
25
26
27
28
29
30
31
32
33
34
# HLT Json file name 
def getHLTMenuFileName( flags=None ):
    if flags is None:
        from TriggerJobOpts.TriggerFlags import TriggerFlags as tf
        hltMenuFileName = 'HLTMenu_'+tf.triggerMenuSetup()+'_'+tf.menuVersion()+'.json'
    else:
        hltMenuFileName = 'HLTMenu_'+flags.Trigger.triggerMenuSetup+'_'+flags.Trigger.menuVersion+'.json'
    hltMenuFileName = hltMenuFileName.replace(".xml",".json").replace("HLTconfig", "HLTMenu").replace("HLTmenu", "HLTMenu")
    hltMenuFileName = hltMenuFileName.replace("_newJO","")
    return hltMenuFileName

Joerg Stelzer's avatar
Joerg Stelzer committed
35
36
37
38
39
40
41
42
43
# L1 Prescales set json file name
def getL1PrescalesSetFileName( flags=None ):
    if flags is None:
        from TriggerJobOpts.TriggerFlags import TriggerFlags as tf
        l1PrescalesSetFileName = 'L1PrescalesSet_'+tf.triggerMenuSetup()+'_'+tf.menuVersion()+'.json'
    else:
        l1PrescalesSetFileName = 'L1PrescalesSet_'+flags.Trigger.triggerMenuSetup+'_'+flags.Trigger.menuVersion+'.json'
    return l1PrescalesSetFileName

Joerg Stelzer's avatar
Joerg Stelzer committed
44

45
46
47
48
49
50
51
52
53
# HLT Prescales set json file name
def getHLTPrescalesSetFileName( flags=None ):
    if flags is None:
        from TriggerJobOpts.TriggerFlags import TriggerFlags as tf
        hltPrescalesSetFileName = 'HLTPrescalesSet_'+tf.triggerMenuSetup()+'_'+tf.menuVersion()+'.json'
    else:
        hltPrescalesSetFileName = 'HLTPrescalesSet_'+flags.Trigger.triggerMenuSetup+'_'+flags.Trigger.menuVersion+'.json'
    return hltPrescalesSetFileName

Joerg Stelzer's avatar
Joerg Stelzer committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Creates an L1 Prescale file from the menu
# this is a temporary solution, in the final version the L1PrescalesSet file should come from the menu
def createL1PrescalesFileFromMenu( flags=None ):
    log = logging.getLogger('TrigConfigSvcCfg')
    menuFN = getL1MenuFileName( flags )
    with open(menuFN,'r') as fh:
        data = json.load(fh)
        pso = odict()
        pso['filetype'] = 'l1prescale'
        pso['name'] = data['name']
        pso['cutValues'] = odict()
        ps = pso['cutValues']
        for name, item in sorted(data['items'].items()):
            ps[name] = odict([
                ("cut", 1),
                ("enabled", True),
                ("info", "prescale: 1")
            ])
    psFN = getL1PrescalesSetFileName( flags )
    with open(psFN, 'w') as outfile:
        json.dump(pso, outfile, indent = 4)
        log.info("Generated default L1 prescale set %s", outfile.name)

77
78
79
80

# Creates an HLT Prescale file from the menu
# this is a temporary solution, in the final version the HLTPrescalesSet file should come from the menu
def createHLTPrescalesFileFromMenu( flags=None ):
Joerg Stelzer's avatar
Joerg Stelzer committed
81
    log = logging.getLogger('TrigConfigSvcCfg')
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    menuFN = getHLTMenuFileName( flags )
    with open(menuFN,'r') as fh:
        data = json.load(fh)
        pso = odict()
        pso['filetype'] = 'hltprescale'
        pso['name'] = data['name']
        pso['prescales'] = odict()
        ps = pso['prescales']
        for ch in data['chains']:
            chName = ch['name']
            ps[chName] = odict([
                ("name", chName),
                ("counter", ch['counter']),
                ("hash", ch['nameHash']),
Joerg Stelzer's avatar
Joerg Stelzer committed
96
97
                ("prescale", 1),
                ("enabled", 1)
98
99
100
101
            ])
    psFN = getHLTPrescalesSetFileName( flags )
    with open(psFN, 'w') as outfile:
        json.dump(pso, outfile, indent = 4)
Joerg Stelzer's avatar
Joerg Stelzer committed
102
        log.info("Generated default HLT prescale set %s", outfile.name)
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123


def getTrigConfigFromFlag( flags=None ):
    log = logging.getLogger('TrigConfigSvcCfg')
    if flags is None:
        from TriggerJobOpts.TriggerFlags import TriggerFlags as tf
        tcflag = tf.triggerConfig()
        log.info("Parsing TriggerFlags.triggerConfig %s", tcflag)
    else:
        tcflag = flags.Trigger.triggerConfig
        log.info("Parsing flags.Trigger.triggerConfig %s", tcflag)
    if tcflag is None:
        tcflag = "FILE"
    source, dbconn, keys = (tcflag+":::").split(":")[:3]
    smk,l1psk,hltpsk,bgk = (keys+",,,").split(",")[:4]
    smk = int(smk) if smk != "" else None
    l1psk = int(l1psk) if l1psk!="" else None
    hltpsk = int(hltpsk) if hltpsk!="" else None
    bgk = int(bgk) if bgk!="" else None
    tcdict = {
        "source" : source.upper(),  # DB, FILE, COOL
124
        "dbconn" : dbconn, # db connection (if origin==DB or COOL) or "JOSVC" if connection is to be taken from TrigConf::IJobOptionsSvc 
125
126
127
128
129
130
131
132
133
        "smk"    : smk,
        "l1psk"  : l1psk,
        "hltpsk" : hltpsk,
        "bgk"    : bgk
    }
    return tcdict



Joerg Stelzer's avatar
Joerg Stelzer committed
134
135
136
137
138
139
140
141
142
143
144
# L1 menu generation
@memoize
def generateL1Menu( flags=None ):
    log = logging.getLogger('TrigConfigSvcCfg')
    from TriggerJobOpts.TriggerFlags import TriggerFlags
    fileName = getL1MenuFileName( flags )
    if flags:
        TriggerFlags.triggerMenuSetup = flags.Trigger.triggerMenuSetup
    menuName = TriggerFlags.triggerMenuSetup() if flags is None else flags.Trigger.triggerMenuSetup
    log.info("Generating L1 menu %s", menuName)
    from TriggerMenuMT.L1.L1MenuConfig import L1MenuConfig
Joerg Stelzer's avatar
Joerg Stelzer committed
145
146
147
148
149
    l1cfg = L1MenuConfig( menuName = menuName )
    outfile = l1cfg.writeJSON(outputFile = fileName)
    if outfile is not None:
        log.info("Wrote L1 menu file %s", outfile)
    return outfile
Joerg Stelzer's avatar
Joerg Stelzer committed
150
151
152


# configuration of L1ConfigSvc
Joerg Stelzer's avatar
Joerg Stelzer committed
153
@memoize
Joerg Stelzer's avatar
Joerg Stelzer committed
154
155
156
157
158
159
160
def getL1ConfigSvc( flags = None ):
    log = logging.getLogger('TrigConfigSvcCfg')
    from AthenaCommon.Logging import log
    from TriggerJobOpts.TriggerFlags import TriggerFlags
    from TrigConfigSvc.TrigConfigSvcConf import TrigConf__LVL1ConfigSvc
    from AthenaCommon.AppMgr import theApp
    # generate menu file
Joerg Stelzer's avatar
Joerg Stelzer committed
161
    generatedFile = generateL1Menu( flags=flags )
Joerg Stelzer's avatar
Joerg Stelzer committed
162
163
164

    # configure config svc
    l1ConfigSvc = TrigConf__LVL1ConfigSvc( "LVL1ConfigSvc" )
Joerg Stelzer's avatar
Joerg Stelzer committed
165

Joerg Stelzer's avatar
Joerg Stelzer committed
166
    l1ConfigSvc.ConfigSource = "XML"
167
168
169
170
171
172
    l1XMLFile = TriggerFlags.inputLVL1configFile() if flags is None else flags.Trigger.LVL1ConfigFile
    # check if file exists in this directory otherwise add the package to aid path resolution
    # also a '/' in the file name indicates that no package needs to be added
    import os.path
    if not ( "/" in l1XMLFile or os.path.isfile(l1XMLFile) ):
        l1XMLFile = "TriggerMenuMT/" + l1XMLFile
Joerg Stelzer's avatar
Joerg Stelzer committed
173
    l1ConfigSvc.XMLMenuFile = l1XMLFile
Joerg Stelzer's avatar
Joerg Stelzer committed
174
    log.info( "For run 2 style menu access configured LVL1ConfigSvc with input file : %s", l1XMLFile )
Joerg Stelzer's avatar
Joerg Stelzer committed
175
176
177
178

    if generatedFile is None:
        l1ConfigSvc.InputType = "none"
        l1ConfigSvc.JsonFileName = ""
Joerg Stelzer's avatar
Joerg Stelzer committed
179
        log.info( "For run 3 style menu access configured LVL1ConfigSvc with InputType='none'" )
Joerg Stelzer's avatar
Joerg Stelzer committed
180
181
182
183
    else:
        l1ConfigSvc.InputType = "file"
        l1JsonFileName = generatedFile
        l1ConfigSvc.JsonFileName = l1JsonFileName
Joerg Stelzer's avatar
Joerg Stelzer committed
184
        log.info( "For run 3 style menu access configured LVL1ConfigSvc with InputType='file' and JsonFileName=%s", l1JsonFileName )
Joerg Stelzer's avatar
Joerg Stelzer committed
185
186

    theApp.CreateSvc += [ "TrigConf::LVL1ConfigSvc/LVL1ConfigSvc" ]
Joerg Stelzer's avatar
Joerg Stelzer committed
187
188
189
190
    return l1ConfigSvc


# configuration of HLTConfigSvc
Joerg Stelzer's avatar
Joerg Stelzer committed
191
@memoize
Joerg Stelzer's avatar
Joerg Stelzer committed
192
193
194
195
196
197
198
def getHLTConfigSvc( flags = None ):
    log = logging.getLogger('TrigConfigSvcCfg')
    from AthenaCommon.Logging import log
    from TrigConfigSvc.TrigConfigSvcConf import TrigConf__HLTConfigSvc
    from AthenaCommon.AppMgr import theApp
    hltConfigSvc = TrigConf__HLTConfigSvc( "HLTConfigSvc" )
    hltXMLFile = "None"
Tim Martin's avatar
Tim Martin committed
199
    hltConfigSvc.ConfigSource = "None"
Joerg Stelzer's avatar
Joerg Stelzer committed
200
201
202
203
204
205
206
207
208
    hltConfigSvc.XMLMenuFile = hltXMLFile
    hltConfigSvc.InputType = "file"
    hltJsonFileName = getHLTMenuFileName( flags )
    hltConfigSvc.JsonFileName = hltJsonFileName
    theApp.CreateSvc += [ "TrigConf::HLTConfigSvc/HLTConfigSvc" ]
    log.info( "Configured HLTConfigSvc with run 2 style input file : %s", hltXMLFile  )
    log.info( "Configured HLTConfigSvc with InputType='file' and JsonFileName=%s", hltJsonFileName )
    return hltConfigSvc

209

Joerg Stelzer's avatar
Joerg Stelzer committed
210
# configuration of CondAlg providing the HLTPrescalesSet
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
@memoize
def setupHLTPrescaleCondAlg( flags = None ):
    log = logging.getLogger('TrigConfigSvcCfg')
    from TrigConfigSvc.TrigConfigSvcConf import TrigConf__HLTPrescaleCondAlg
    hltPrescaleCondAlg = TrigConf__HLTPrescaleCondAlg( "HLTPrescaleCondAlg" )

    tc = getTrigConfigFromFlag( flags )
    hltPrescaleCondAlg.Source = tc["source"]
    if tc["source"] == "COOL":
        hltPrescaleCondAlg.TriggerDB = tc["dbconn"]
    elif tc["source"] == "DB":
        hltPrescaleCondAlg.TriggerDB = tc["dbconn"]
        hltPrescaleCondAlg.HLTPsk    = tc["hltpsk"]
    elif tc["source"] == "FILE":
        hltPrescaleCondAlg.Filename = getHLTPrescalesSetFileName( flags )
    else:
        raise RuntimeError("trigger configuration flag 'trigConfig' starts with %s, which is not understood" % tc["source"])

229
230
231
232
233
    if tc["source"] == "COOL":
        if flags is None: # old style config
            from IOVDbSvc.CondDB import conddb
            conddb.addFolder( "TRIGGER", getHLTPrescaleFolderName(), className="AthenaAttributeList" )
            log.info("Adding folder %s to conddb", getHLTPrescaleFolderName() )
Joerg Stelzer's avatar
Joerg Stelzer committed
234
235
236
237
238
239
    # add the hltPrescaleCondAlg to condseq
    if flags is None: # old style config
        from AthenaCommon.AlgSequence import AthSequencer
        condSequence = AthSequencer("AthCondSeq")
        condSequence += hltPrescaleCondAlg
        log.info("Adding HLTPrescaleCondAlg to AthCondSeq")
240
241
242
    return hltPrescaleCondAlg


Joerg Stelzer's avatar
Joerg Stelzer committed
243
# provide L1 config service in new JO
Joerg Stelzer's avatar
Joerg Stelzer committed
244
245
246
247
248
249
def L1ConfigSvcCfg( flags ):
    from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
    acc = ComponentAccumulator()
    acc.addService( getL1ConfigSvc( flags ) )
    return acc

Joerg Stelzer's avatar
Joerg Stelzer committed
250
# provide HLT config service in new JO
Joerg Stelzer's avatar
Joerg Stelzer committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
def HLTConfigSvcCfg( flags ):
    from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
    acc = ComponentAccumulator()
    acc.addService( getHLTConfigSvc( flags ) )
    return acc

# provide both services in new JO
def TrigConfigSvcCfg( flags ):
    from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
    acc = ComponentAccumulator()
    acc.addService( getL1ConfigSvc( flags ) )
    acc.addService( getHLTConfigSvc( flags ) )
    return acc


266
267
268
269
def HLTPrescaleCondAlgCfg( flags ):
    log = logging.getLogger('TrigConfigSvcCfg')
    from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
    acc = ComponentAccumulator()
270
    acc.addCondAlgo( setupHLTPrescaleCondAlg( flags ) )
271
272
273
274
275
    tc = getTrigConfigFromFlag( flags )
    if tc["source"] == "COOL":
        from IOVDbSvc.IOVDbSvcConfig import addFolders
        acc.merge(addFolders(flags, getHLTPrescaleFolderName(), "TRIGGER_ONL", className="AthenaAttributeList"))
        log.info("Adding folder %s to CompAcc", getHLTPrescaleFolderName() )
276
277
278
    return acc


Joerg Stelzer's avatar
Joerg Stelzer committed
279
280
281
282
283
284
285
if __name__ == "__main__":
    from AthenaCommon.Configurable import Configurable
    Configurable.configurableRun3Behavior=True    

    from AthenaConfiguration.AllConfigFlags import ConfigFlags
    ConfigFlags.lock()
    acc = TrigConfigSvcCfg( ConfigFlags )
286
    acc.store( open( "test.pkl", "wb" ) )
Joerg Stelzer's avatar
Joerg Stelzer committed
287
    print("All OK")