18#include "GaudiKernel/IIncidentSvc.h"
19#include "GaudiKernel/FileIncident.h"
20#include "GaudiKernel/EventIDBase.h"
21#include "GaudiKernel/EventIDRange.h"
32#include "CoralBase/AttributeListSpecification.h"
33#include "nlohmann/json.hpp"
37 const std::string& name,
38 const IInterface* parent)
39 : base_class(
type, name, parent)
67 incSvc->addListener(
this,
"FirstInputFile", 60);
80 std::map<std::string, std::map<std::string, std::string>> folderPayloads;
82 for (
const auto& [key, value] :
m_payloads.value()) {
84 size_t colonPos = key.find(
':');
85 if (colonPos == std::string::npos) {
86 ATH_MSG_ERROR(
"Invalid payload key format: " << key <<
" (expected 'folder:key')");
87 return StatusCode::FAILURE;
90 std::string folderName = key.substr(0, colonPos);
91 std::string paramName = key.substr(colonPos + 1);
92 folderPayloads[folderName][paramName] = value;
95 static const std::string beginRunKey{
"beginRun"};
96 static const std::string endRunKey{
"endRun"};
97 static const std::string specType{
"string"};
100 const auto isIOVKey = [](
const std::string& key) {
101 return key == beginRunKey || key == endRunKey;
103 ATH_MSG_DEBUG(
"Processing " << folderPayloads.size() <<
" folder(s) for direct payload registration");
104 for (
const auto& [folderName, parameters] : folderPayloads) {
106 auto beginRunItr = parameters.find(beginRunKey);
107 auto endRunItr = parameters.find(endRunKey);
108 if ( (beginRunItr == parameters.end()) || (endRunItr == parameters.end()) ) {
109 ATH_MSG_ERROR(
"Payload for folder " << folderName <<
" missing beginRun or endRun");
110 return StatusCode::FAILURE;
113 unsigned int beginRun = std::stoul(beginRunItr->second);
114 unsigned int endRun = std::stoul(endRunItr->second);
116 ATH_MSG_DEBUG(
"Registering folder " << folderName <<
" with " << parameters.size() - 2
117 <<
" parameters, IOV [" << beginRun <<
", " << endRun <<
"]");
124 coral::AttributeListSpecification* spec =
new coral::AttributeListSpecification();
125 for (
const auto& [key, value] : parameters) {
126 if (!isIOVKey(key)) {
127 spec->extend(key, specType);
131 coral::AttributeList attrList(*spec,
true);
132 for (
const auto& [key, value] : parameters) {
133 if (!isIOVKey(key)) {
134 attrList[key].setValue(value);
138 auto payload = std::make_unique<CondAttrListCollection>(
true);
139 payload->addNewStart(
IOVTime(beginRun, 0));
141 payload->add(0, attrList);
143 ATH_MSG_DEBUG(
"Created payload with IOV [" << beginRun <<
", " << endRun <<
"]");
155 return(StatusCode::SUCCESS);
163 return StatusCode::SUCCESS;
170 const FileIncident* fileInc =
dynamic_cast<const FileIncident*
>(&inc);
171 if(!fileInc)
throw std::runtime_error(
"Unable to get FileName from FirstInputFile incident");
173 const std::string fileName = fileInc->fileName();
174 ATH_MSG_DEBUG(
"handle() " << inc.type() <<
" for " << fileName);
180 if(!
sc.isSuccess())
throw std::runtime_error(
"Could not process input file meta data");
190 return StatusCode::SUCCESS;
195 return StatusCode::SUCCESS;
203 return StatusCode::SUCCESS;
207 std::vector<std::string> iovMetaStrings;
211 ATH_MSG_WARNING(
"Could not find IOVMetaDataContainer for folder " << folderName <<
", skipping");
216 if (!jsonStr.empty()) {
217 iovMetaStrings.push_back(
"IOVMeta." + folderName +
"=" + jsonStr);
218 ATH_MSG_DEBUG(
"Serialized folder " << folderName <<
" (" << jsonStr.size() <<
" bytes JSON)");
222 if (iovMetaStrings.empty()) {
224 return StatusCode::SUCCESS;
229 if (
m_metaDataStore->contains<std::vector<std::string>>(
"IOVMetaDataStrings")) {
231 std::vector<std::string>* existingStrings =
nullptr;
233 existingStrings->insert(existingStrings->end(), iovMetaStrings.begin(), iovMetaStrings.end());
234 ATH_MSG_DEBUG(
"Appended " << iovMetaStrings.size() <<
" IOV metadata strings to existing collection");
237 auto iovMetaData = std::make_unique<std::vector<std::string>>(std::move(iovMetaStrings));
239 ATH_MSG_DEBUG(
"Stored " << iovMetaStrings.size() <<
" IOV metadata strings in MetaDataStore");
242 return StatusCode::SUCCESS;
247 return StatusCode::SUCCESS;
273 ATH_MSG_DEBUG(
"checkOverrideRunNumber: check if tag is set in jobOpts");
277 SmartIF<IProperty> appMgr{serviceLocator()->service(
"ApplicationMgr")};
279 ATH_MSG_ERROR(
"checkOverrideRunNumber: Cannot get ApplicationMgr ");
282 StringProperty property(
"EvtSel",
"");
283 StatusCode
sc = appMgr->getProperty(&property);
284 if (!
sc.isSuccess()) {
285 ATH_MSG_ERROR(
"checkOverrideRunNumber: unable to get EvtSel: found " << property.value());
289 const std::string eventSelector =
property.value();
290 SmartIF<IProperty> evtSel{serviceLocator()->service(eventSelector)};
292 ATH_MSG_ERROR(
"checkOverrideRunNumber: Cannot get EventSelector " << eventSelector);
297 BooleanProperty overrideRunNumber(
"OverrideRunNumberFromInput",
false);
298 sc = evtSel->getProperty(&overrideRunNumber);
299 if (!
sc.isSuccess()) {
301 ATH_MSG_DEBUG(
"resetRunNumber: unable to get OverrideRunNumberFromInput property from EventSelector ");
307 IntegerProperty runNumber(
"RunNumber", 0);
308 sc = evtSel->getProperty(&runNumber);
309 if (!
sc.isSuccess()) {
310 ATH_MSG_ERROR(
"checkOverrideRunNumber: unable to get RunNumber from EventSelector: found "
311 << runNumber.value());
316 IntegerProperty oldRunNumber(
"OldRunNumber", 0);
317 sc = evtSel->getProperty(&oldRunNumber);
318 if (!
sc.isSuccess()) {
319 ATH_MSG_ERROR(
"checkOverrideRunNumber: unable to get OldRunNumber from EventSelector: found "
320 << oldRunNumber.value());
328 else ATH_MSG_DEBUG(
"checkOverrideRunNumber: OverrideRunNumberFromInput not set for " << eventSelector);
338 std::string folderDescr =
"<timeStamp>run-event</timeStamp><addrHeader><address_header service_type=\"256\" clid=\"1238547719\" /> </addrHeader><typeName>CondAttrListCollection</typeName>" ;
347 const std::string& folderDescription)
const
350 std::scoped_lock guard(
m_mutex );
356 return(StatusCode::FAILURE);
359 ATH_MSG_DEBUG(
"IOVMetaDataContainer for folder " << folderName <<
" has been registered ");
362 return StatusCode::SUCCESS;
371 std::scoped_lock guard(
m_mutex );
378 ATH_MSG_DEBUG(
"Retrieved IOVMetaDataContainer from MetaDataStore for folder "
382 ATH_MSG_ERROR(
"addPayload: Could not find IOVMetaDataContainer in MetaDataStore for folder "
384 <<
". One must have previously called registerFolder. ");
385 return StatusCode::FAILURE;
394 bool success = cont->
merge(payload);
396 ATH_MSG_DEBUG(
"Added new payload for folder " << folderName);
401 <<
" (may be duplicate payload).");
409 if(payload && msgLvl(MSG::DEBUG)) {
410 std::ostringstream stream;
411 payload->dump(stream);
415 return StatusCode::SUCCESS;
428 ATH_MSG_DEBUG(
"begin modifyPayload for folder " << folderName);
431 bool modifyAttr =
false;
432 std::string attributeName;
435 for (
unsigned int i = 0; i < folders.size(); ++i) {
436 if (folderName == folders[i]) {
437 if (attrs.size() > i) {
438 attributeName = attrs[i];
440 ATH_MSG_DEBUG(
"modifyPayload: remove attribute " << attributeName);
447 ATH_MSG_DEBUG(
"modifyPayload: folder " << folderName <<
" OK ");
448 return StatusCode::SUCCESS;
451 bool iovSizeIsZero = coll->
iov_size() == 0;
464 unsigned int nchans = coll->
size();
465 bool hasChanNames = (coll->
name_size() == nchans);
466 for (
unsigned int ichan = 0; ichan < nchans; ++ichan) {
471 for (
unsigned int iatt = 0; iatt < oldAttrList.size(); ++iatt) {
473 if (attributeName == oldAttrList[iatt].specification().name()) {
474 ATH_MSG_DEBUG(
"modifyPayload: skipping attribute name " << oldAttrList[iatt].specification().name());
479 newAttrList.extend(oldAttrList[iatt].specification().name(),
480 oldAttrList[iatt].specification().
type());
481 const coral::Attribute& oldAttr = oldAttrList[iatt];
482 coral::Attribute& newAttr = newAttrList[oldAttrList[iatt].specification().name()];
486 << oldAttrList[iatt].specification().name() <<
" "
489 coll1->
add(chan, newAttrList);
490 if (!iovSizeIsZero) coll1->
add(chan, coll->
iovRange(chan));
491 if(hasChanNames)coll1->
add(chan, coll->
chanName(chan));
492 ATH_MSG_DEBUG(
"modifyPayload: copied attribute list for channel " << chan);
496 if (msgLvl(MSG::DEBUG)) {
497 std::ostringstream stream;
502 return StatusCode::SUCCESS;
518 ,
const std::string& folderDescription)
const
528 ATH_MSG_DEBUG(
"No IOVMetaDataContainer in MetaDataStore for folder " << folderName
529 <<
". Created a new instance");
531 if (!
sc.isSuccess()) {
532 ATH_MSG_ERROR(
"Could not record IOVMetaDataContainer in MetaDataStore for folder " << folderName);
538 ATH_MSG_DEBUG(
"IOVMetaDataContainer already in MetaDataStore for folder " << folderName);
540 if (!
sc.isSuccess()) {
541 ATH_MSG_ERROR(
"Could not retrieve IOVMetaDataContainer in MetaDataStore for folder " << folderName);
553 std::scoped_lock guard(
m_mutex );
555 ATH_MSG_DEBUG(
"processInputFileMetaData: file name " << fileName);
562 if (!
sc.isSuccess()) {
563 ATH_MSG_DEBUG(
"processInputFileMetaData: Could not retrieve IOVMetaDataContainer objects from InputMetaDataStore - cannot process input file meta data");
564 return StatusCode::SUCCESS;
567 ATH_MSG_DEBUG(
"processInputFileMetaData: Retrieved from IOVMetaDataContainer(s) from InputMetaDataStore");
570 unsigned int ncolls = 0;
571 unsigned int ndupColls = 0;
572 for (; cont != contEnd; ++cont) {
574 , cont->folderDescription());
578 std::list<SG::ObjectWithVersion<IOVMetaDataContainer> > allVersions;
580 if (!
sc.isSuccess()) {
588 ATH_MSG_DEBUG(
"processInputFileMetaData: New container: payload size " << payload->size() <<
" version key " << obj.versionedKey);
591 if (msgLvl(MSG::VERBOSE)) {
593 ATH_MSG_VERBOSE(
"Before merge, payload minRange for folder " << cont->folderName());
594 if (payloadMaster && payloadMaster->
size()) {
598 unsigned int iPayload = 0;
599 for (; itColl != itCollEnd; ++itColl, ++iPayload) {
601 << (*itColl)->size());
611 if (msgLvl(MSG::DEBUG)) {
615 std::ostringstream stream;
616 for (; itColl1 != itCollEnd1; ++itColl1) (*itColl1)->dump(stream);
627 for (; itColl != itCollEnd; ++itColl) {
639 ATH_MSG_ERROR(
"processInputFileMetaData: Could not modify the payload for folder " << contMaster->
folderName());
640 return StatusCode::FAILURE;
644 if (!contMaster->
merge(coll)) {
656 ATH_MSG_DEBUG(
"processInputFileMetaData: Merged together containers for folder " << cont->folderName() <<
" ncoll/ndup "
657 << ncolls <<
" " << ndupColls);
661 if (payloadMaster && payloadMaster->
size()) {
666 if ((*itColl)->minRange().start().isTimestamp()) lastStop =
IOVTime(0);
668 bool hasError =
false;
669 for (; itColl != itCollEnd; ++itColl) {
670 if ((*itColl)->minRange().start() < lastStop) hasError =
true;
671 lastStop = (*itColl)->minRange().stop();
674 ATH_MSG_ERROR(
"processInputFileMetaData: error after merge of file meta data. " );
675 ATH_MSG_ERROR(
"processInputFileMetaData: Filename " << fileName);
677 ATH_MSG_ERROR(
"processInputFileMetaData: MinRange for meta data folders ");
678 unsigned int iPayload = 0;
679 itColl = payloadMaster->
begin();
680 for (; itColl != itCollEnd; ++itColl, ++iPayload) {
681 ATH_MSG_ERROR(iPayload <<
" " << (*itColl)->minRange() <<
" " << (*itColl)->size());
687 if (msgLvl(MSG::VERBOSE)) {
689 ATH_MSG_VERBOSE(
"processInputFileMetaData: After merge, payload minRange ");
694 unsigned int iPayload = 0;
695 for (; itColl != itCollEnd; ++itColl, ++iPayload) {
697 << (*itColl)->size());
704 if (msgLvl(MSG::DEBUG)) {
705 ATH_MSG_DEBUG(
"processInputFileMetaData: Input payload " << cont->folderName());
706 std::ostringstream streamInp;
707 itColl = payload->begin();
708 itCollEnd = payload->end();
709 for (; itColl != itCollEnd; ++itColl) (*itColl)->dump(streamInp);
712 std::ostringstream streamOut;
715 for (; itColl != itCollEnd; ++itColl) (*itColl)->dump(streamOut);
721 ATH_MSG_DEBUG(
"processInputFileMetaData: Total number of attribute collections merged together " << ncolls
722 <<
" Number of duplicate collections " << ndupColls);
731 return StatusCode::SUCCESS;
740 if (!payloads || payloads->
size() == 0) {
745 using json = nlohmann::json;
748 jsonData[
"folder"] = container->folderName();
749 jsonData[
"description"] = container->folderDescription();
750 jsonData[
"iovs"] = json::array();
762 if (start.isRunEvent()) {
763 iov[
"range"][
"start"] = {{
"run", start.run()}, {
"event", start.event()}};
764 iov[
"range"][
"stop"] = {{
"run", stop.run()}, {
"event", stop.event()}};
766 iov[
"range"][
"start"] = {{
"timestamp", start.timestamp()}};
767 iov[
"range"][
"stop"] = {{
"timestamp", stop.timestamp()}};
771 iov[
"attrs"] = json::object();
772 for (
const auto& chanAttrPair : *coll) {
774 const coral::AttributeList& attrList = chanAttrPair.second;
776 std::string chanKey =
"chan" + std::to_string(chan);
777 iov[
"attrs"][chanKey] = json::object();
779 for (
const auto& attr : attrList) {
780 auto & thisAttribute = iov[
"attrs"][chanKey][attr.specification().name()];
782 const std::type_info&
type = attr.specification().
type();
783 if (
type ==
typeid(std::string)) {
784 thisAttribute = attr.data<std::string>();
785 }
else if (
type ==
typeid(
int)) {
786 thisAttribute = attr.data<
int>();
787 }
else if (
type ==
typeid(
unsigned int)) {
788 thisAttribute = attr.data<
unsigned int>();
789 }
else if (
type ==
typeid(
long)) {
790 thisAttribute = attr.data<
long>();
791 }
else if (
type ==
typeid(
unsigned long)) {
792 thisAttribute = attr.data<
unsigned long>();
793 }
else if (
type ==
typeid(
long long)) {
794 thisAttribute = attr.data<
long long>();
795 }
else if (
type ==
typeid(
unsigned long long)) {
796 thisAttribute = attr.data<
unsigned long long>();
797 }
else if (
type ==
typeid(
float)) {
798 thisAttribute = attr.data<
float>();
799 }
else if (
type ==
typeid(
double)) {
800 thisAttribute = attr.data<
double>();
801 }
else if (
type ==
typeid(
bool)) {
802 thisAttribute = attr.data<
bool>();
805 std::ostringstream oss;
806 attr.toOutputStream(oss);
807 thisAttribute = oss.str();
808 ATH_MSG_DEBUG(
"Attribute " << attr.specification().name() <<
" has unsupported type, converted to string: " << oss.str());
813 jsonData[
"iovs"].push_back(iov);
816 return jsonData.dump();
834 bool iovSizeIsZero = coll->
iov_size() == 0;
839 if (start.isRunEvent() && stop.isRunEvent()) {
848 return StatusCode::SUCCESS;
853 <<
" iovSizeIsZero: " << (
int)iovSizeIsZero
854 <<
" newRange " << newRange);
867 unsigned int nchans = coll->
size();
869 for (
unsigned int ichan = 0; ichan < nchans; ++ichan) {
872 coll->
add(chan, newRange);
873 ATH_MSG_DEBUG(
"overrideIOV: overriding the IOV of collection chan " << chan);
879 if (msgLvl(MSG::DEBUG)) {
880 ATH_MSG_DEBUG(
"overrideIOV: after overriding the IOV of collection");
881 std::ostringstream stream;
888 return StatusCode::SUCCESS;
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
This file defines the class for a collection of AttributeLists where each one is associated with a ch...
Hold mappings of ranges to condition objects.
a traits class that associates a CLID to a type T It also detects whether T inherits from Gaudi DataO...
Basic time unit for IOVSvc.
read-copy-update (RCU) style synchronization for Athena.
size_t size() const
Number of registered mappings.
This class is a collection of AttributeLists where each one is associated with a channel number.
const AttributeList & attributeList(ChanNum chanNum) const
attribute list for a given channel number
ChanNum chanNum(unsigned int index) const
channel number for index: (index = 0 to size-1)
void addNewStart(const IOVTime &start)
Add new start time to minRange - make sure that start is >= to new start.
name_size_type name_size() const
number of Chan/Name pairs
bool add(ChanNum chanNum, const AttributeList &attributeList)
Adding in chan/attrList pairs.
void addNewStop(const IOVTime &stop)
Add new stop time to minRange - make sure that stop is <= to new stop.
void dump() const
Dump our contents to std::cout.
const std::string & chanName(ChanNum chanNum) const
find name for particular channel
void resetMinRange()
Reset minRange according to the IOVs of the contained channels.
const IOVRange & minRange() const
Current minimal IOVRange.
size_type size() const
number of Chan/AttributeList pairs
iov_size_type iov_size() const
number of Chan/IOV pairs
coral::AttributeList AttributeList
const IOVRange & iovRange(ChanNum chanNum) const
IOVRange list for a given channel number.
This class is a container for the payload of conditions data.
size_type size() const
size of payload vector
payloadVec::const_iterator const_iterator
const_iterator begin() const
Begin of payload vector.
const_iterator end() const
End of payload vector.
const IOVTime & stop() const
bool isInRange(const IOVTime &t) const
const IOVTime & start() const
Basic time unit for IOVSvc.
static constexpr uint32_t MAXRUN
static constexpr uint32_t MAXEVENT
a const_iterator facade to DataHandle.
associate a data object with its VersionedKey The object is held by a ReadHandle to delay its retriev...
const std::string & key() const
Get the key string with which the current object was stored.
ElementLink_p1< typename GenerateELinkIndexType_p1< typename LINK::index_type >::type > type