ATLAS Offline Software
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 
15 NswUncertDbAlg::NswUncertDbAlg(const std::string& name, ISvcLocator* 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  }
33  ATH_CHECK(m_idHelperSvc.retrieve());
34  return StatusCode::SUCCESS;
35 }
36 
37 StatusCode 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  }
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...");
72  if (!CoralUtilities::readBlobAsString(atr["data"].data<coral::Blob>(), data)) {
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  }
80  ATH_CHECK(parseDataFromJSON(lines, *writeCdo));
81  }
82  }
83  }
84  ATH_CHECK(writeHandle.record(std::move(writeCdo)));
85  return StatusCode::SUCCESS;
86 }
87 
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,
113  sTgcIdHelper::sTgcChannelTypes::Strip, 1
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 
NswErrorCalibData
Definition: NswErrorCalibData.h:19
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
Muon::nsw::STGTPSegments::moduleIDBits::stationPhi
constexpr uint8_t stationPhi
station Phi 1 to 8
Definition: NSWSTGTPDecodeBitmaps.h:129
dumpTgcDigiDeadChambers.gasGap
list gasGap
Definition: dumpTgcDigiDeadChambers.py:33
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
checkFileSG.line
line
Definition: checkFileSG.py:75
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
xAOD::uint8_t
uint8_t
Definition: Muon_v1.cxx:575
json
nlohmann::json json
Definition: HistogramDef.cxx:9
NswUncertDbAlg::execute
virtual StatusCode execute(const EventContext &) const override
Definition: NswUncertDbAlg.cxx:37
NswErrorCalibData::ErrorConstants
Helper struct to store different error calibrations for a certain channel range & also for seperate C...
Definition: NswErrorCalibData.h:50
NswUncertDbAlg::m_writeKey
SG::WriteCondHandleKey< NswErrorCalibData > m_writeKey
Definition: NswUncertDbAlg.h:45
parse
std::map< std::string, std::string > parse(const std::string &list)
Definition: egammaLayerRecalibTool.cxx:983
python.subdetectors.tile.Blob
Blob
Definition: tile.py:17
python.PyKernel.AttributeList
AttributeList
Definition: PyKernel.py:36
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
isValid
bool isValid(const T &p)
Definition: AtlasPID.h:214
ReadCondHandle.h
NswUncertDbAlg.h
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
NswUncertDbAlg::parseDataFromJSON
StatusCode parseDataFromJSON(const nlohmann::json &lines, NswErrorCalibData &errorCalibData) const
Load the Jitter constants from the JSON format.
Definition: NswUncertDbAlg.cxx:88
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
CaloCondBlobAlgs_fillNoiseFromASCII.lines
lines
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:104
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:88
CoralUtilities::readBlobAsString
bool readBlobAsString(const coral::Blob &, std::string &)
Definition: blobaccess.cxx:85
Identifier
Definition: DetectorDescription/Identifier/Identifier/Identifier.h:32
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
WriteCondHandle.h
NswUncertDbAlg::m_readKeysDb
SG::ReadCondHandleKeyArray< CondAttrListCollection > m_readKeysDb
Definition: NswUncertDbAlg.h:48
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Handler::svc
AthROOTErrorHandlerSvc * svc
Definition: AthROOTErrorHandlerSvc.cxx:10
IOVInfiniteRange.h
PathResolver.h
NswErrorCalibData::storeConstants
StatusCode storeConstants(const Identifier &gasGapId, ErrorConstants &&newConstants)
Definition: NswErrorCalibData.cxx:127
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
dumpNswErrorDb.maxStrip
tuple maxStrip
Definition: dumpNswErrorDb.py:27
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:431
SG::ReadCondHandleKey< CondAttrListCollection >
blobaccess.h
constants
Definition: Calorimeter/CaloClusterCorrection/python/constants.py:1
CondAttrListCollection::const_iterator
ChanAttrListMap::const_iterator const_iterator
Definition: CondAttrListCollection.h:63
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
CaloCondBlobAlgs_fillNoiseFromASCII.author
string author
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:26
NswUncertDbAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: NswUncertDbAlg.h:42
dumpNswErrorDb.minStrip
int minStrip
Definition: dumpNswErrorDb.py:26
NswUncertDbAlg::NswUncertDbAlg
NswUncertDbAlg(const std::string &name, ISvcLocator *svc)
Definition: NswUncertDbAlg.cxx:15
NswUncertDbAlg::m_readFromJSON
Gaudi::Property< std::string > m_readFromJSON
Use an external JSON file to load the Jitter constants from.
Definition: NswUncertDbAlg.h:40
IOVInfiniteRange::infiniteTime
static EventIDRange infiniteTime()
Produces an EventIDRange that is inifinite in Time and invalid in RunLumi.
Definition: IOVInfiniteRange.h:47
Muon::nsw::STGTPSegments::moduleIDBits::stationEta
constexpr uint8_t stationEta
1 to 3
Definition: NSWSTGTPDecodeBitmaps.h:127
NswUncertDbAlg::initialize
virtual StatusCode initialize() override
Definition: NswUncertDbAlg.cxx:18
SG::WriteCondHandle
Definition: WriteCondHandle.h:26
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37