ATLAS Offline Software
Loading...
Searching...
No Matches
NswUncertDbAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4#include "NswUncertDbAlg.h"
5
10#include <CoralBase/Blob.h>
12#include <fstream>
13
14
15namespace Muon {
17 ATH_CHECK(m_readKeysDb.initialize(m_readFromJSON.value().empty()));
18 if (m_readFromJSON.value().size()) {
19 ATH_MSG_INFO("Read the uncertainty data from a JSON file "<<m_readFromJSON);
20 } else if (m_readKeysDb.size()) {
21 std::stringstream folderStr{};
23 folderStr<<" **** "<<key.fullKey()<<std::endl;
24 }
25 ATH_MSG_INFO("Read the parametrized NSW uncertainties from COOL: "<<std::endl<<folderStr.str());
26 } else {
27 ATH_MSG_FATAL("Neither an extrenal JSON nor a COOL folder were defined. Please check");
28 return StatusCode::FAILURE;
29 }
30 ATH_CHECK(m_writeKey.initialize());
31 ATH_CHECK(m_idHelperSvc.retrieve());
32 return StatusCode::SUCCESS;
33}
34
35StatusCode NswUncertDbAlg::execute(const EventContext& ctx) const {
36 ATH_MSG_DEBUG("execute " << name());
37 SG::WriteCondHandle writeHandle{m_writeKey, ctx};
38 if (writeHandle.isValid()) {
39 ATH_MSG_DEBUG("CondHandle " << writeHandle.fullKey() << " is already valid."
40 << ". In theory this should not be called, but may happen"
41 << " if multiple concurrent events are being processed out of order.");
42 return StatusCode::SUCCESS;
43 }
44
45 std::unique_ptr<NswErrorCalibData> writeCdo = std::make_unique<NswErrorCalibData>(m_idHelperSvc.get());
46 writeHandle.addDependency(EventIDRange(IOVInfiniteRange::infiniteTime()));
47
48 if (!m_readFromJSON.value().empty()) {
49 std::ifstream inStream{PathResolverFindCalibFile(m_readFromJSON)};
50 if (!inStream.good()) {
51 ATH_MSG_FATAL("No such file or directory");
52 return StatusCode::FAILURE;
53 }
54 nlohmann::json lines;
55 inStream >> lines;
56 ATH_CHECK(parseDataFromJSON(lines, *writeCdo));
57 } else {
59 SG::ReadCondHandle readHandle{key, ctx};
60 if (!readHandle.isValid()) {
61 ATH_MSG_FATAL("Failed to load NSW error calibration folder from "<<key.fullKey());
62 return StatusCode::FAILURE;
63 }
64 for (CondAttrListCollection::const_iterator itr = readHandle->begin();
65 itr != readHandle->end(); ++itr) {
66 const coral::AttributeList& atr = itr->second;
67 std::string data{};
68 if (atr["data"].specification().type() == typeid(coral::Blob)) {
69 ATH_MSG_VERBOSE("Loading data as a BLOB, uncompressing...");
71 ATH_MSG_FATAL("Cannot uncompress BLOB! Aborting...");
72 return StatusCode::FAILURE;
73 }
74 } else {
75 data = *(static_cast<const std::string*>((atr["data"]).addressOfData()));
76 }
77 nlohmann::json lines = nlohmann::json::parse(data);
78 ATH_CHECK(parseDataFromJSON(lines, *writeCdo));
79 }
80 }
81 }
82 ATH_CHECK(writeHandle.record(std::move(writeCdo)));
83 return StatusCode::SUCCESS;
84}
85
86StatusCode NswUncertDbAlg::parseDataFromJSON(const nlohmann::json& lines,
87 NswErrorCalibData& nswErrorCalib) const {
88
89 for (auto& corr : lines.items()) {
90 nlohmann::json line = corr.value();
92 const std::string stationType = line["station"];
93 const int stationPhi = line["phi"];
94 const int stationEta = line["eta"];
95 const int multilayer = line["multilayer"];
96 const int gasGap = line["gasGap"];
97 Identifier errorCalibId{};
98 bool isValid{true};
99 if (stationType[0] == 'M') {
100 if (!m_idHelperSvc->hasMM()) continue;
101 errorCalibId = m_idHelperSvc->mmIdHelper().channelID(stationType, stationEta, stationPhi,
102 multilayer, gasGap, 1
103#ifndef NDEBUG
104 ,isValid
105#endif
106 );
107 } else if (stationType[0] == 'S') {
108 if (!m_idHelperSvc->hasSTGC()) continue;
109 errorCalibId = m_idHelperSvc->stgcIdHelper().channelID (stationType, stationEta, stationPhi,
110 multilayer, gasGap,
112#ifndef NDEBUG
113 ,isValid
114#endif
115 );
116
117 } else {
118 isValid = false;
119 }
120 if (!isValid) {
121 ATH_MSG_ERROR("Failed to construct a valid Identifier from "
122 <<stationType<<", "<<stationEta<<", "<<stationPhi<<", "
123 <<multilayer<<", "<<gasGap);
124 return StatusCode::FAILURE;
125 }
126
127 const uint16_t minStrip = line["minStrip"];
128 const uint16_t maxStrip = line["maxStrip"];
129 const uint8_t author = line["clusterAuthor"];
130 const std::string modelName = line["modelName"];
131 std::vector<double> modelPars = line["modelPars"];
132
133 ATH_MSG_VERBOSE("Load uncertainties for channel " <<m_idHelperSvc->toString(errorCalibId)<<" "<<modelPars
134 <<"model name: "<<modelName<<" author: "<<static_cast<int>(author));
135
136
138 minStrip, maxStrip, std::move(modelPars)};
139
140 ATH_CHECK(nswErrorCalib.storeConstants(errorCalibId, std::move(constants)));
141 }
142
143 return StatusCode::SUCCESS;
144}
145}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
bool isValid(const T &p)
Av: we implement here an ATLAS-sepcific convention: all particles which are 99xxxxx are fine.
Definition AtlasPID.h:878
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
ChanAttrListMap::const_iterator const_iterator
static EventIDRange infiniteTime()
Produces an EventIDRange that is inifinite in Time and invalid in RunLumi.
StatusCode parseDataFromJSON(const nlohmann::json &lines, NswErrorCalibData &errorCalibData) const
Load the Jitter constants from the JSON format.
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Gaudi::Property< std::string > m_readFromJSON
Use an external JSON file to load the Jitter constants from.
SG::WriteCondHandleKey< NswErrorCalibData > m_writeKey
virtual StatusCode execute(const EventContext &) const override
virtual StatusCode initialize() override
SG::ReadCondHandleKeyArray< CondAttrListCollection > m_readKeysDb
Helper struct to store different error calibrations for a certain channel range & also for seperate C...
StatusCode storeConstants(const Identifier &gasGapId, ErrorConstants &&newConstants)
void addDependency(const EventIDRange &range)
StatusCode record(const EventIDRange &range, T *t)
record handle, with explicit range DEPRECATED
const DataObjID & fullKey() const
bool readBlobAsString(const coral::Blob &, std::string &)
NRpcCablingAlg reads raw condition data and writes derived condition data to the condition store.