ATLAS Offline Software
MmCTPCondDbAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
10 #include <CoralBase/Blob.h>
12 #include <fstream>
13 
14 
15 
16 // Initialize
18 
19  ATH_CHECK(m_idHelperSvc.retrieve());
20  if( !m_idHelperSvc->hasMM() ){
21  ATH_MSG_ERROR("MuonIdHelperSvc does not have MM, the CTP calibration should not be run!");
22  return StatusCode::FAILURE;
23  }
24 
26  if (m_readFromJSON.value().size()) {
27  ATH_MSG_INFO("Read the uncertainty data from a JSON file "<<m_readFromJSON);
28  } else if (!m_readKeyDb.empty()) {
29  ATH_MSG_INFO("Read the uncertainty data from a COOL databse: " << m_readKeyDb.fullKey() );
30  } else {
31  ATH_MSG_FATAL("Neither an extrenal JSON nor a COOL folder were defined. Please check");
32  return StatusCode::FAILURE;
33  }
35 
36 
37 
38  return StatusCode::SUCCESS;
39 }
40 
41 StatusCode MmCTPCondDbAlg::execute(const EventContext& ctx) const {
42  ATH_MSG_DEBUG("execute " << name());
43  // launching Write Cond Handle
44  SG::WriteCondHandle writeHandle{m_writeKey, ctx};
45  if (writeHandle.isValid()) {
46  ATH_MSG_DEBUG("CondHandle " << writeHandle.fullKey() << " is already valid."
47  << " In theory this should not be called, but may happen"
48  << " if multiple concurrent events are being processed out of order.");
49  return StatusCode::SUCCESS;
50  }
51 
52  std::unique_ptr<Muon::mmCTPClusterCalibData> writeCdo{std::make_unique<Muon::mmCTPClusterCalibData>(m_idHelperSvc.get())};
53  writeHandle.addDependency(EventIDRange(IOVInfiniteRange::infiniteTime()));
54 
55  if (!m_readFromJSON.value().empty()) {
56  std::ifstream inStream{PathResolverFindCalibFile(m_readFromJSON)};
57  if (!inStream.good()) {
58  ATH_MSG_FATAL("No such file or directory");
59  return StatusCode::FAILURE;
60  }
62  inStream >> lines;
63  ATH_CHECK(parseDataFromJSON(lines, *writeCdo));
64  } else{
65 
66  SG::ReadCondHandle readHandle{m_readKeyDb, ctx};
67  if (!readHandle.isValid()) {
68  ATH_MSG_FATAL("Failed to load NSW error calibration folder from "<<m_readKeyDb.fullKey());
69  return StatusCode::FAILURE;
70  }
71 
72  for (CondAttrListCollection::const_iterator itr = readHandle->begin();
73  itr != readHandle->end(); ++itr) {
74  const coral::AttributeList& atr = itr->second;
75  std::string data = *(static_cast<const std::string*>((atr["data"]).addressOfData()));
77  ATH_CHECK(parseDataFromJSON(lines, *writeCdo));
78  }
79  }
80 
81  ATH_CHECK(writeHandle.record(std::move(writeCdo)));
82  return StatusCode::SUCCESS;
83 }
84 
86  Muon::mmCTPClusterCalibData& ctpClusterCondData) const {
87  for (auto& corr : lines.items()) {
88  nlohmann::json line = corr.value();
89 
90  //Check entry keys, shall we make it to just check the first entry and then assume the rest is okay? maybe it doesn't take that much time to do it on the whole thing...
91  if (!line.contains("station") || !line.contains("phi") || !line.contains("eta") || !line.contains("multilayer") || !line.contains("gasGap")) {
92  ATH_MSG_ERROR("Missing expected keys in the JSON file");
93  return StatusCode::FAILURE; // Handle error appropriately
94  }
95 
96  // Ensure the JSON array exists and has exactly 2 elements
97  if (!line.contains("P1Parameters") || !line["P1Parameters"].is_array() || line["P1Parameters"].size() != 2) {
98  ATH_MSG_ERROR("Get unexpected size of the P1Parameters array");
99  return StatusCode::FAILURE; // Handle error appropriately
100  }
101 
103  const std::string stationType = line["station"];
104  const int stationPhi = line["phi"];
105  const int stationEta = line["eta"];
106  const int multiLayer = line["multilayer"];
107  const int gasGap = line["gasGap"];
108 
109  // Convert JSON array to std::array<double, 2>
110  std::array<double, 2> modelPars;
111  std::copy_n(line["P1Parameters"].begin(), 2, modelPars.begin());
112 
113  Identifier errorCalibId{};
114 
115  //Using PCB 1 as default
116  errorCalibId = m_idHelperSvc->mmIdHelper().channelID(stationType, stationEta, stationPhi, multiLayer, gasGap, 1 );
117 
119 
120  ATH_CHECK(ctpClusterCondData.storeConstants(errorCalibId, std::move(constants)));
121 
122 
123  }
124  return StatusCode::SUCCESS;
125 }
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:161
MmCTPCondDbAlg::parseDataFromJSON
StatusCode parseDataFromJSON(const nlohmann::json &lines, Muon::mmCTPClusterCalibData &ctpClusterCondData) const
Definition: MmCTPCondDbAlg.cxx:85
dumpTgcDigiDeadChambers.gasGap
list gasGap
Definition: dumpTgcDigiDeadChambers.py:33
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
Muon::mmCTPClusterCalibData::storeConstants
StatusCode storeConstants(const Identifier &gasGapId, CTPParameters &&newConstants)
Definition: mmCTPClusterCalibData.cxx:15
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
json
nlohmann::json json
Definition: HistogramDef.cxx:9
parse
std::map< std::string, std::string > parse(const std::string &list)
Definition: egammaLayerRecalibTool.cxx:1113
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
python.PyKernel.AttributeList
AttributeList
Definition: PyKernel.py:36
SG::VarHandleKey::empty
bool empty() const
Test if the key is blank.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:150
dq_defect_bulk_create_defects.line
line
Definition: dq_defect_bulk_create_defects.py:27
ReadCondHandle.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
CaloCondBlobAlgs_fillNoiseFromASCII.lines
lines
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:103
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
MmCTPCondDbAlg::m_readFromJSON
Gaudi::Property< std::string > m_readFromJSON
Parse data from COOL.
Definition: MmCTPCondDbAlg.h:28
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
MmCTPCondDbAlg.h
WriteCondHandle.h
MmCTPCondDbAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override final
Definition: MmCTPCondDbAlg.cxx:41
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Muon::mmCTPClusterCalibData::CTPParameters
Definition: mmCTPClusterCalibData.h:21
IOVInfiniteRange.h
PathResolver.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
MmCTPCondDbAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: MmCTPCondDbAlg.h:32
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:283
blobaccess.h
constants
Definition: Calorimeter/CaloClusterCorrection/python/constants.py:1
CondAttrListCollection::const_iterator
ChanAttrListMap::const_iterator const_iterator
Definition: CondAttrListCollection.h:63
MmCTPCondDbAlg::initialize
virtual StatusCode initialize() override final
Definition: MmCTPCondDbAlg.cxx:17
MmCTPCondDbAlg::m_readKeyDb
SG::ReadCondHandleKey< CondAttrListCollection > m_readKeyDb
Definition: MmCTPCondDbAlg.h:37
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:159
Muon::mmCTPClusterCalibData
Definition: mmCTPClusterCalibData.h:18
sTgcDigitEffiDump.multiLayer
int multiLayer
Definition: sTgcDigitEffiDump.py:36
SG::WriteCondHandle
Definition: WriteCondHandle.h:26
MmCTPCondDbAlg::m_writeKey
SG::WriteCondHandleKey< Muon::mmCTPClusterCalibData > m_writeKey
Definition: MmCTPCondDbAlg.h:36
Identifier
Definition: IdentifierFieldParser.cxx:14