Commit bfe7d4da authored by Joerg Stelzer's avatar Joerg Stelzer
Browse files

Implement HLTPrescaleCondAlg to read from different sources

parent d5b204ea
......@@ -59,6 +59,8 @@ namespace TrigConf {
// maps HLT chain hashes to prescales
std::unordered_map<uint32_t, HLTPrescale> m_prescalesByHash {1024};
std::string m_name;
};
}
......
......@@ -19,6 +19,7 @@ TrigConf::HLTPrescalesSet::~HLTPrescalesSet()
void
TrigConf::HLTPrescalesSet::update()
{
m_name = m_data.get_child("name").get_value<std::string>();
const auto & prescales = m_data.get_child("prescales");
for( auto & p : prescales ) {
HLTPrescale ps;
......@@ -30,6 +31,12 @@ TrigConf::HLTPrescalesSet::update()
}
}
std::string
TrigConf::HLTPrescalesSet::name() const
{
return m_name;
}
std::size_t
TrigConf::HLTPrescalesSet::size() const
{
......
......@@ -2,6 +2,11 @@
from PyUtils.Decorators import memoize
from AthenaCommon.Logging import logging
from collections import OrderedDict as odict
import json
_hltPrescaleKeyFolderName = "/TRIGGER/HLT/PrescaleKey <tag>HEAD</tag>"
# L1 Json file name
def getL1MenuFileName( flags=None ):
......@@ -14,6 +19,7 @@ def getL1MenuFileName( flags=None ):
l1MenuFileName = l1MenuFileName.replace("_newJO","")
return l1MenuFileName
# HLT Json file name
def getHLTMenuFileName( flags=None ):
if flags is None:
......@@ -26,6 +32,69 @@ def getHLTMenuFileName( flags=None ):
return hltMenuFileName
# 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
# 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 ):
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']),
("prescale", 1)
])
psFN = getHLTPrescalesSetFileName( flags )
with open(psFN, 'w') as outfile:
json.dump(pso, outfile, indent = 4)
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
"dbconn" : dbconn, # db connection (if origin==DB or COOL) or "JOS" if connection is to be taken from TrigConf::IJobOptionsSvc
"smk" : smk,
"l1psk" : l1psk,
"hltpsk" : hltpsk,
"bgk" : bgk
}
return tcdict
# L1 menu generation
@memoize
def generateL1Menu( flags=None ):
......@@ -97,6 +166,43 @@ def getHLTConfigSvc( flags = None ):
log.info( "Configured HLTConfigSvc with InputType='file' and JsonFileName=%s", hltJsonFileName )
return hltConfigSvc
# configuration of HLTConfigSvc
@memoize
def setupHLTPrescaleCondAlg( flags = None ):
global _hltPrescaleKeyFolderName
log = logging.getLogger('TrigConfigSvcCfg')
from TrigConfigSvc.TrigConfigSvcConf import TrigConf__HLTPrescaleCondAlg
from AthenaCommon.Constants import DEBUG
hltPrescaleCondAlg = TrigConf__HLTPrescaleCondAlg( "HLTPrescaleCondAlg" )
hltPrescaleCondAlg.OutputLevel = DEBUG
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 )
createHLTPrescalesFileFromMenu( flags )
else:
raise RuntimeError("trigger configuration flag 'trigConfig' starts with %s, which is not understood" % tc["source"])
if flags is None: # old style config
from AthenaCommon.AlgSequence import AthSequencer
condSequence = AthSequencer("AthCondSeq")
condSequence += hltPrescaleCondAlg
from IOVDbSvc.CondDB import conddb
conddb.addFolder( "TRIGGER", _hltPrescaleKeyFolderName, className="AthenaAttributeList",
extensible = True)
log.info("Adding folder %s to conddb", _hltPrescaleKeyFolderName )
return hltPrescaleCondAlg
# provide L1 config service in new JO
def L1ConfigSvcCfg( flags ):
from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
......@@ -120,6 +226,18 @@ def TrigConfigSvcCfg( flags ):
return acc
def HLTPrescaleCondAlgCfg( flags ):
global _hltPrescaleKeyFolderName
log = logging.getLogger('TrigConfigSvcCfg')
from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
from IOVDbSvc.IOVDbSvcConfig import addFolders
acc = ComponentAccumulator()
acc.addCondAlgo( setupHLTPrescaleCondAlgCfg( flags ) )
acc.merge(addFolders(flags, _hltPrescaleKeyFolderName, "TRIGGER_ONL", className="AthenaAttributeList"))
log.info("Adding folder %s to CompAcc", _hltPrescaleKeyFolderName )
return acc
if __name__ == "__main__":
from AthenaCommon.Configurable import Configurable
Configurable.configurableRun3Behavior=True
......
......@@ -4,6 +4,8 @@
#include "./HLTPrescaleCondAlg.h"
#include "TrigConfIO/TrigDBHLTPrescalesSetLoader.h"
#include "TrigConfIO/JsonFileLoader.h"
#include "TrigConfInterfaces/IJobOptionsSvc.h"
#include "CoolKernel/types.h"
......@@ -14,47 +16,107 @@ TrigConf::HLTPrescaleCondAlg::HLTPrescaleCondAlg(const std::string& name, ISvcLo
StatusCode
TrigConf::HLTPrescaleCondAlg::initialize() {
ATH_MSG_INFO("initialize");
ATH_MSG_DEBUG("HLTPrescaleCondAlg::initialize()");
ATH_CHECK(m_pskFolderInputKey.initialize());
ATH_CHECK(m_hltPrescaleSetOutputKey.initialize());
if( m_configSource == "COOL" && m_dbConnection == "JOSVC" ) {
if( auto joSvc = serviceLocator()->service<TrigConf::IJobOptionsSvc>( "JobOptionsSvc" ) ) {
if( joSvc->hltPrescaleKey()>0 ) {
m_psk = joSvc->hltPrescaleKey();
m_dbConnection = joSvc->server();
ATH_MSG_DEBUG("Set psk to " << m_psk << " and db connection to " << m_dbConnection );
}
} else {
ATH_MSG_DEBUG("Did not locate TrigConf::IJobOptionsSvc");
}
}
ATH_MSG_DEBUG("Source " << m_configSource);
ATH_MSG_DEBUG("TriggerDB " << m_dbConnection);
ATH_MSG_DEBUG("HLTPsk " << m_psk);
ATH_MSG_DEBUG("Filename " << m_filename.value());
return StatusCode::SUCCESS;
}
StatusCode
TrigConf::HLTPrescaleCondAlg::execute(const EventContext& ctx) const {
SG::ReadCondHandle<AthenaAttributeList> readH(m_pskFolderInputKey, ctx);
const AthenaAttributeList * pskAL{ *readH };
if ( pskAL == nullptr ) {
ATH_MSG_FATAL("Null pointer to the read conditions object of " << m_pskFolderInputKey.key());
return StatusCode::FAILURE;
}
ATH_MSG_DEBUG("HLTPrescaleCondAlg::execute");
unsigned int hltPsk = m_psk;
EventIDRange range;
if (not readH.range(range)) {
ATH_MSG_FATAL("Failed to retrieve validity range for " << readH.key());
return StatusCode::FAILURE;
}
if(m_configSource == "COOL") {
SG::ReadCondHandle<AthenaAttributeList> readH(m_pskFolderInputKey, ctx);
const AthenaAttributeList * pskAL{ *readH };
if ( pskAL == nullptr ) {
ATH_MSG_FATAL("Null pointer to the read conditions object of " << m_pskFolderInputKey.key());
return StatusCode::FAILURE;
} else {
ATH_MSG_DEBUG("Retrieved the AthenaAttributeList");
}
if (not readH.range(range)) {
ATH_MSG_FATAL("Failed to retrieve validity range for " << readH.key());
return StatusCode::FAILURE;
} else {
ATH_MSG_DEBUG("Retrieved the current IOV of the readHandle");
}
// get the prescale key from the cool folder
hltPsk = (*pskAL)["HltPrescaleKey"].data<cool::UInt32>();
ATH_MSG_DEBUG( "Extracted the HLT PSK " << hltPsk << " for run " << ctx.eventID().run_number() << " and lb " << ctx.eventID().lumi_block() );
} else {
EventIDBase::number_type run = ctx.eventID().run_number();
EventIDBase start, stop;
start.set_run_number(run);
start.set_lumi_block(0);
stop.set_run_number(run+1);
stop.set_lumi_block(0);
range = EventIDRange(start,stop);
}
// get the prescale key from the cool folder
unsigned int hltPsk = (*pskAL)["HltPrescaleKey"].data<cool::UInt32>();
TrigConf::HLTPrescalesSet * pss = new TrigConf::HLTPrescalesSet;
if( m_configSource == "FILE" ) {
// load the file into the HLT prescales set
ATH_MSG_DEBUG( "Setting up JsonFileLoader with file " << m_filename.value() );
TrigConf::JsonFileLoader psLoader;
psLoader.setLevel(TrigConf::MSGTC::WARNING);
ATH_MSG_DEBUG( "Going to load prescales" );
if( psLoader.loadFile( m_filename, *pss) ) {
ATH_MSG_INFO( "HLT prescales set successfully loaded from file " << m_filename.value() );
} else {
ATH_MSG_ERROR( "Failed loading HLT prescales set from file " << m_filename.value() );
return StatusCode::FAILURE;
}
} else if( hltPsk != 0 ) {
// load the HLT psk into HLT prescales set
ATH_MSG_DEBUG( "Setting up TrigDBHLTPrescalesSetLoader with DB connection " << m_dbConnection.value() );
TrigConf::TrigDBHLTPrescalesSetLoader psLoader(m_dbConnection);
psLoader.setLevel(TrigConf::MSGTC::WARNING);
ATH_MSG_DEBUG( "Going to load prescales" );
if( psLoader.loadHLTPrescales( hltPsk, *pss ) ) {
ATH_MSG_INFO( "HLT prescales set successfully loaded from db with key " << m_psk.value() );
} else {
ATH_MSG_ERROR( "Failed loading HLT prescales set from db with key " << m_psk.value() );
return StatusCode::FAILURE;
}
// load the HLT psk into HLT prescales set
TrigConf::TrigDBHLTPrescalesSetLoader psLoader(m_dbConnection);
psLoader.setLevel(TrigConf::MSGTC::WARNING);
TrigConf::HLTPrescalesSet * pss = new TrigConf::HLTPrescalesSet;
if( psLoader.loadHLTPrescales( hltPsk, *pss ) ) {
ATH_MSG_INFO( "HLT prescales set successfully loaded from db with key " << m_psk.value() );
} else {
ATH_MSG_ERROR( "Failed loading HLT prescales set from db with key " << m_psk.value() );
ATH_MSG_ERROR( "Failed loading HLT prescales set (not reading from FILE and no psk known)" );
return StatusCode::FAILURE;
}
}
// recording HLT prescales set
SG::WriteCondHandle<TrigConf::HLTPrescalesSet> writeCondHandle(m_hltPrescaleSetOutputKey, ctx);
......
......@@ -7,6 +7,7 @@
#include "AthenaBaseComps/AthReentrantAlgorithm.h"
#include "StoreGate/ReadCondHandleKey.h"
#include "StoreGate/WriteCondHandleKey.h"
#include "TrigConfData/HLTPrescalesSet.h"
......@@ -35,14 +36,16 @@ namespace TrigConf {
private:
// input key to the HLT Prescale Key folder
SG::ReadCondHandleKey<AthenaAttributeList> m_pskFolderInputKey{ this, "PSKFolder", "/TRIGGER/HLT/HLTPrescaleKey", "SG Key of AthenaAttributeList containing hlt psk"};
SG::ReadCondHandleKey<AthenaAttributeList> m_pskFolderInputKey{ this, "PSKFolder", "/TRIGGER/HLT/PrescaleKey", "SG Key of AthenaAttributeList containing hlt psk"};
// output key to store the HLTPrescalesSet
SG::WriteCondHandleKey<TrigConf::HLTPrescalesSet> m_hltPrescaleSetOutputKey{ this, "HLTPrescales", "HLTPrescales", "HLT prescales"};
// other properties
// properties
Gaudi::Property< std::string > m_configSource { this, "Source", "FILE", "Configuration source, can be 'FILE', 'DB', or 'COOL'" };
Gaudi::Property< std::string > m_dbConnection { this, "TriggerDB", "TRIGGERDB", "DB connection alias" };
Gaudi::Property< unsigned int > m_psk { this, "PSK", 0, "HLT prescale key" };
Gaudi::Property< unsigned int > m_psk { this, "HLTPsk", 0, "HLT prescale key" };
Gaudi::Property< std::string > m_filename { this, "Filename", "", "HLT prescale json file" };
};
......
......@@ -447,10 +447,12 @@ if not opt.createHLTMenuExternally:
import sys
sys.exit(0)
from TrigConfigSvc.TrigConfigSvcCfg import getHLTConfigSvc
svcMgr += getHLTConfigSvc()
from TrigConfigSvc.TrigConfigSvcCfg import getHLTConfigSvc, setupHLTPrescaleCondAlg
svcMgr += getHLTConfigSvc()
setupHLTPrescaleCondAlg()
# ---------------------------------------------------------------
# ID conditions
# ---------------------------------------------------------------
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment