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