ATLAS Offline Software
Loading...
Searching...
No Matches
MuonConditions/MuonCondGeneral/MuonCondAlg/src/MdtCalibDbAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "MdtCalibDbAlg.h"
6
12#include "CLHEP/Random/RandGaussZiggurat.h"
13#include "CoralBase/Attribute.h"
14#include "CoralBase/AttributeListSpecification.h"
15#include "GaudiKernel/PhysicalConstants.h"
33
35
36
37#include <fstream>
38
39#include "TFile.h"
40#include "TSpline.h"
41#include "GeoModelKernel/throwExcept.h"
42
43using namespace MuonCalib;
44using namespace CxxUtils;
48
49namespace Muon {
50
52 ATH_MSG_DEBUG("initialize " << name());
53
54 // if timeslew correction vector m_MeanCorrectionVsR has non-zero size then set
55 // m_TsCorrectionT0=m_MeanCorrectionVsR[0] and subtract this each value in the vector.
56 if (m_MeanCorrectionVsR.size()) {
58 for (float & it : m_MeanCorrectionVsR) {
59 it -= m_TsCorrectionT0;
60 }
61 }
62
63 ATH_CHECK(m_idHelperSvc.retrieve());
64 ATH_CHECK(m_idToFixedIdTool.retrieve());
65 // initiallize random number generator if doing t0 smearing (for robustness studies)
66 if (m_t0Spread != 0.) {
67 ATH_CHECK(m_AthRNGSvc.retrieve());
68 ATH_MSG_DEBUG(" initialize Random Number Service: running with t0 shift "
69 << m_t0Shift << " spread " << m_t0Spread << " rt shift " << m_rtShift);
70 // getting our random numbers stream
71 m_RNGWrapper = m_AthRNGSvc->getEngine(this, m_randomStream);
72 if (!m_RNGWrapper) {
73 ATH_MSG_ERROR("Could not get random number engine from AthRNGSvc. Abort.");
74 return StatusCode::FAILURE;
75 }
76 }
77
78 if (m_rtShift != 0. || m_rtScale != 1. || m_t0Shift != 0. || m_t0Spread != 0.) {
79 ATH_MSG_INFO("************************************" << std::endl
80 << " Running with Calibration Deformations! " << std::endl
81 << " For performance studies only!" << std::endl
82 << " **************************************");
83 ATH_MSG_DEBUG(" rt scale " << m_rtScale << " t0 shift " << m_t0Shift << " spread " << m_t0Spread << " rt shift " << m_rtShift);
84 }
85
86 ATH_CHECK(m_readKeyRt.initialize());
87 ATH_CHECK(m_readKeyTube.initialize());
88 ATH_CHECK(m_writeKey.initialize());
90
91 if (m_useNewGeo) {
92 ATH_CHECK(detStore()->retrieve(m_r4detMgr));
93 } else {
94 ATH_CHECK(detStore()->retrieve(m_detMgr));
95 }
96 return StatusCode::SUCCESS;
97}
98 StatusCode MdtCalibDbAlg::declareDependency(const EventContext& ctx,
100
101 writeHandle.addDependency(EventIDRange(IOVInfiniteRange::infiniteTime()));
103 if (key.empty()) continue;
104 SG::ReadCondHandle readHandle{key, ctx};
105 if (!readHandle.isValid()) {
106 ATH_MSG_FATAL("Failed to retrieve conditions object "<<readHandle.fullKey());
107 return StatusCode::FAILURE;
108 }
109 writeHandle.addDependency(readHandle);
110 ATH_MSG_INFO("Size of CondAttrListCollection " << readHandle.fullKey() << " readCdoRt->size()= " << readHandle->size());
111 ATH_MSG_INFO("Range of input is " << readHandle.getRange());
112 }
113 if (m_readKeyDCS.empty()) {
114 return StatusCode::SUCCESS;
115 }
116 SG::ReadCondHandle readHandle{m_readKeyDCS, ctx};
117 if (!readHandle.isValid()) {
118 ATH_MSG_FATAL("Failed to retrieve conditions object "<<m_readKeyDCS.fullKey());
119 return StatusCode::FAILURE;
120 }
121 if (readHandle->hasDCS()) {
122 writeHandle.addDependency(readHandle);
123 }
124 return StatusCode::SUCCESS;
125}
126StatusCode MdtCalibDbAlg::execute(const EventContext& ctx) const {
127 ATH_MSG_DEBUG("execute " << name());
128 SG::WriteCondHandle writeHandle{m_writeKey, ctx};
129 if (writeHandle.isValid()) {
130 ATH_MSG_DEBUG("CondHandle " << writeHandle.fullKey() << " is already valid."
131 << ". In theory this should not be called, but may happen"
132 << " if multiple concurrent events are being processed out of order.");
133 return StatusCode::SUCCESS;
134 }
135 ATH_CHECK(declareDependency(ctx, writeHandle));
136
137 RegionGranularity gran{RegionGranularity::OnePerChamber};
138 if (m_readKeyRt.key() == "/MDT/RTUNIQUE") {
139 ATH_MSG_DEBUG("Save one set of Rt constants per chamber");
140 gran = RegionGranularity::OneRt;
141 } else if (m_UseMLRt) {
142 ATH_MSG_DEBUG("Save one set of calibration constants per multi layer");
143 gran = RegionGranularity::OnePerMultiLayer;
144 } else ATH_MSG_DEBUG("Save one set of calibration constants per chamber");
145 std::unique_ptr<MuonCalib::MdtCalibDataContainer> writeCdo = std::make_unique<MuonCalib::MdtCalibDataContainer>(m_idHelperSvc.get(), gran);
146
147 ATH_CHECK(loadRt(ctx, *writeCdo));
148 ATH_CHECK(loadTube(ctx, *writeCdo));
149 ATH_CHECK(writeHandle.record(std::move(writeCdo)));
150 return StatusCode::SUCCESS;
151}
152
154 ATH_MSG_DEBUG("defaultRt " << name());
155 std::string fileName = PathResolver::find_calib_file(m_RTfileName);
156 std::ifstream inputFile(fileName);
157 if (!inputFile) {
158 ATH_MSG_ERROR("Unable to open RT Ascii file: " << fileName);
159 return StatusCode::FAILURE;
160 }
161 ATH_MSG_DEBUG("Opened RT Ascii file: " << fileName);
162
163
164 // Read the RTs from the text file
166 rts.read(inputFile);
167 ATH_MSG_VERBOSE("File contains " << rts.nRts() << " RT relations ");
168
169 // Loop over all RTs in the file (but the default file only has 1 RT)
170 // Use the first valid RT found in the file as the default for all chambers.
171 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
172 //coverity[UNREACHABLE]
173 for (unsigned int n = 0; n < rts.nRts(); ++n) {
174 std::unique_ptr<MuonCalib::RtDataFromFile::RtRelation> rt(rts.getRt(n));
175
176 const MuonCalib::RtDataFromFile::RtRelation::DataVec &times = rt->times();
177 const MuonCalib::RtDataFromFile::RtRelation::DataVec &radii = rt->radii();
178 const MuonCalib::RtDataFromFile::RtRelation::DataVec &reso = rt->resolution();
179
180 // check if rt contains data, at least two points on the rt are required
181 if (times.size() < 2) {
182 ATH_MSG_ERROR(" defaultRt rt table has too few entries");
183 return StatusCode::FAILURE;
184 }
185 // check if all tables have same size
186 if (times.size() != radii.size() || times.size() != reso.size()) {
187 ATH_MSG_ERROR("defaultRt rt table size mismatch ");
188 return StatusCode::FAILURE;
189 }
190 // check for negative time bins, i.e. decreasing time value with radius
191 double t_min = times[0];
192 double bin_size = times[1] - t_min;
193 if (bin_size <= 0) {
194 ATH_MSG_ERROR("defaultRt rt table negative binsize ");
195 return StatusCode::FAILURE;
196 }
197
198 // create a vector to hold the r values,
199 // we need two extra fields to store t_min and bin_size
201 rtPars.push_back(t_min);
202 rtPars.push_back(bin_size);
203
204 // copy r values into vector
205 rtPars.insert(rtPars.end(), radii.begin(), radii.end());
206
207 ATH_MSG_DEBUG("defaultRt new MuonCalib::IRtRelation");
208
209 MuonCalib::CalibFunc::ParVec resoPars{t_min, bin_size};
210 // copy r values into vector
211 resoPars.insert(resoPars.end(), reso.begin(), reso.end());
212
213 ATH_MSG_DEBUG("defaultRt new MuonCalib::IRtResolution");
214
215 // create RT and resolution "I" objects
216 std::shared_ptr<MuonCalib::IRtRelation> rtRel{std::make_shared<RtRelationLookUp>(rtPars)};
217 std::shared_ptr<MuonCalib::IRtResolution> resoRel{std::make_shared<RtResolutionLookUp>(resoPars)};
218
219
220 // Since the same RT is loaded for all chambers you might be tempted to create it once
221 // and simply store the same pointer in writeCdoRt for all regions.
222 // However it seems that when StoreGate clears writeCdoRt (which will happen in LoadRt
223 // by detStore()->removeDataAndProxy) it will crash unless there are unique pointers/objects
224 // for rtRel, resoRel, and MdtRtRelation
225
226 // Loop over RT regions and store the default RT in each
227 RtRelationPtr MdtRt = std::make_shared<MuonCalib::MdtRtRelation>(rtRel, resoRel);
228
229 for(auto itr = idHelper.detectorElement_begin();
230 itr!= idHelper.detectorElement_end();++itr){
231 const Identifier detElId{*itr};
232 if (writeCdo.hasDataForChannel(detElId, msgStream())) {
233 const MdtFullCalibData* dataObj = writeCdo.getCalibData(detElId, msgStream());
234 if (dataObj->rtRelation) {
235 ATH_MSG_DEBUG("Rt relation constants for "<<m_idHelperSvc->toString(detElId)<<" already exists");
236 continue;
237 }
238 }
240 RtRelationPtr storeMe = MdtRt;
241 if (idHelper.multilayer(detElId) == 2) {
242 if (writeCdo.granularity() != RegionGranularity::OnePerMultiLayer) continue;
243 const Identifier firstML = idHelper.multilayerID(detElId, 1);
244 if (writeCdo.hasDataForChannel(firstML, msgStream())) {
245 const MdtFullCalibData* dataObj = writeCdo.getCalibData(firstML, msgStream());
246 if (dataObj->rtRelation) {
247 ATH_MSG_DEBUG("Copy Rt constanst from the first multi layer for "<<m_idHelperSvc->toString(detElId));
248 storeMe = dataObj->rtRelation;
249 }
250 }
251 }
252 ATH_MSG_DEBUG("Add default rt constants for "<<m_idHelperSvc->toString(detElId));
253 if (!writeCdo.storeData(detElId, std::move(storeMe), msgStream())) {
254 ATH_MSG_FATAL(__FILE__<<":"<<__LINE__<<" Failed to save default rts for "<<m_idHelperSvc->toString(detElId));
255 return StatusCode::FAILURE;
256 }
257
259 loadedRts[detElId] = MdtRt;
260
261 }
262
263 // if VERBOSE enabled print out RT function
264 if (msgLvl(MSG::VERBOSE)) {
265 int npoints = rtRel->nPar() - 2;
266 ATH_MSG_VERBOSE("defaultRt npoints from rtRel=" << npoints);
267 for (int ipt = 0; ipt < npoints; ++ipt) {
268 double t = t_min + ipt * bin_size;
269 ATH_MSG_VERBOSE(" " << ipt << " " << t << " " << rtRel->radius(t) << " " << resoRel->resolution(t));
270 }
271 }
272 break; // only need the first good RT from the text file
273
274 } // end loop over RTs in file
275
276 return StatusCode::SUCCESS;
277}
278std::optional<double> MdtCalibDbAlg::getInnerTubeRadius(const Identifier& id) const {
279 static std::atomic<bool> rtWarningPrinted = false;
280
281 if (m_detMgr) {
282 const MuonGM::MdtReadoutElement *detEl = m_detMgr->getMdtReadoutElement(id);
283 if (detEl) { return std::make_optional<double>(detEl->innerTubeRadius()); }
284
285 } else if (m_r4detMgr) {
286 const MuonGMR4::MdtReadoutElement* detEl = m_r4detMgr->getMdtReadoutElement(id);
287 if (detEl) { return std::make_optional<double>(detEl->innerTubeRadius()); }
288 }
289 if (!rtWarningPrinted) {
290 ATH_MSG_WARNING("getInnerTubeRadius() - no Muon station known under the name "
291 << m_idHelperSvc->toString(id));
292 rtWarningPrinted = true;
293 }
294 return std::nullopt;
295}
296
297StatusCode MdtCalibDbAlg::legacyRtPayloadToJSON(const coral::AttributeList& attr, nlohmann::json & json) const {
298 std::string data{}, delim{};
299 if (attr["data"].specification().type() == typeid(coral::Blob)) {
300 ATH_MSG_VERBOSE("Loading data as a BLOB, uncompressing...");
301 if (!CoralUtilities::readBlobAsString(attr["data"].data<coral::Blob>(), data)) {
302 ATH_MSG_FATAL("Cannot uncompress BLOB! Aborting...");
303 return StatusCode::FAILURE;
304 }
305 delim = "\n";
306 } else {
307 data = *(static_cast<const std::string *>((attr["data"]).addressOfData()));
308 delim = " ";
309 }
310 const std::vector<std::string> tokens = tokenize(data, delim);
311 if (tokens.size() < 2) {
312 ATH_MSG_FATAL("The line "<<data<<" cannot be resolved into header & payload");
313 return StatusCode::FAILURE;
314 }
315 const std::string& header = tokens[0];
316 const std::string& payload = tokens[1];
317 ATH_MSG_DEBUG("Header: '"<<header<<"' payload: '"<<payload<<"'");
319 unsigned int numPoints{0};
320 nlohmann::json channel{};
321 const bool rt_ts_applied = (attr["tech"].data<int>() & MuonCalib::TIME_SLEWING_CORRECTION_APPLIED);
322 channel["appliedRT"] = rt_ts_applied;
323 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
324 {
325
326 std::vector<int> tokensHeader = tokenizeInt(header, ",");
327 if(tokensHeader.size()< 2){
328 ATH_MSG_FATAL("Failed to deduce extract number of points & calib Identifier from "<<header);
329 return StatusCode::FAILURE;
330 }
331 unsigned int calibId = tokensHeader[0];
332 numPoints = tokensHeader[1];
333 MuonCalib::MuonFixedId id(calibId);
334 if (!id.is_mdt()) {
335 ATH_MSG_FATAL("Found non-MDT MuonFixedId, continuing...");
336 return StatusCode::FAILURE;
337 }
338 const Identifier athenaId = m_idToFixedIdTool->fixedIdToId(id);
339 if (!m_idHelperSvc->isMuon(athenaId)) {
340 ATH_MSG_WARNING("The translation from the calibration ID with station: "
341 <<id.stationNameString()<<"("<<id.stationName()<<") "
342 <<" eta:"<<id.eta()<<" phi: "<<id.phi());
343 }
344 channel["station"] = m_idHelperSvc->stationNameString(athenaId);
345 channel["eta"] = m_idHelperSvc->stationEta(athenaId);
346 channel["phi"] = m_idHelperSvc->stationPhi(athenaId);
347 channel["ml"] = idHelper.multilayer(athenaId);
348 channel["layer"] = idHelper.tubeLayer(athenaId);
349 channel["tube"] = idHelper.tube(athenaId);
350 }
352 const std::vector<double> dataPoints = tokenizeDouble(payload, ",");
353 std::vector<double> radii{}, times{}, resos{};
354 radii.reserve(numPoints);
355 times.reserve(numPoints);
356 resos.reserve(numPoints);
360 for (unsigned int k = 0 ; k < dataPoints.size(); ++k) {
361 const double value = dataPoints[k];
362 switch (k%3) {
363 case 0:
364 radii.push_back(value);
365 break;
366 case 1:
367 times.push_back(value);
368 break;
369 case 2:
370 resos.push_back(value);
371 break;
372
373 }
374 }
376 if (radii.size() != numPoints ||
377 times.size() != numPoints ||
378 resos.size() != numPoints) {
379 ATH_MSG_FATAL("Payload "<<payload<<" does not lead to the expected number of points "<<numPoints<<" vs. "<<dataPoints.size());
380 return StatusCode::FAILURE;
381 }
382 channel["radii"] = std::move(radii);
383 channel["times"] = std::move(times);
384 channel["resolutions"] = std::move(resos);
385 json.push_back(channel);
386 return StatusCode::SUCCESS;
387}
388
389StatusCode MdtCalibDbAlg::loadRt(const EventContext& ctx, MuonCalib::MdtCalibDataContainer& writeCdo) const {
390 ATH_MSG_DEBUG("loadRt " << name());
391
392 // Read Cond Handle
393 SG::ReadCondHandle readHandleRt{m_readKeyRt, ctx};
394 if (!readHandleRt.isValid()) {
395 ATH_MSG_ERROR("readCdoRt==nullptr");
396 return StatusCode::FAILURE;
397 }
398 // read new-style format 2020
399
400 nlohmann::json rtCalibJson = nlohmann::json::array();
401 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
402 if (m_newFormat2020) {
403 for (CondAttrListCollection::const_iterator itr = readHandleRt->begin();
404 itr != readHandleRt->end(); ++itr) {
405 const coral::AttributeList &atr = itr->second;
406 std::string data{};
407 if (atr["data"].specification().type() == typeid(coral::Blob)) {
408 ATH_MSG_VERBOSE("Loading data as a BLOB, uncompressing...");
409 if (!CoralUtilities::readBlobAsString(atr["data"].data<coral::Blob>(), data)) {
410 ATH_MSG_FATAL("Cannot uncompress BLOB! Aborting...");
411 return StatusCode::FAILURE;
412 }
413 } else {
414 ATH_MSG_VERBOSE("Loading data as a STRING");
415 data = *(static_cast<const std::string *>((atr["data"]).addressOfData()));
416 }
417 // unwrap the json and build the data vector
418 nlohmann::json yy = nlohmann::json::parse(data);
419 for (auto &it : yy.items()) {
420 nlohmann::json yx = it.value();
421 rtCalibJson.push_back(yx);
422 }
423 }
424 }
425 // read old-style format
426 else {
427 for (CondAttrListCollection::const_iterator itr = readHandleRt->begin();
428 itr != readHandleRt->end(); ++itr) {
429 ATH_CHECK(legacyRtPayloadToJSON(itr->second, rtCalibJson));
430 }
431 }
433 LoadedRtMap loadedRtRel{};
434 // unpack the strings in the collection and update the writeCdoRt
435 for (const auto& payload : rtCalibJson) {
436 const bool rt_ts_applied = payload["appliedRT"];
438 const std::string stName = payload["station"];
439 const Identifier athenaId = idHelper.channelID(stName, payload["eta"], payload["phi"],
440 payload["ml"], payload["layer"], payload["tube"]);
441
442 std::optional<double> innerTubeRadius = getInnerTubeRadius(idHelper.multilayerID(athenaId, 1));
443 if (!innerTubeRadius) continue;
444
445
446 const std::vector<double> radii = payload["radii"];
447 const std::vector<double> times = payload["times"];
448 const std::vector<double> resolutions = payload["resolutions"];
449
450 if (writeCdo.hasDataForChannel(athenaId, msgStream())) {
451 const MdtFullCalibData* dataObj = writeCdo.getCalibData(athenaId, msgStream());
452 if (dataObj->rtRelation) {
453 ATH_MSG_DEBUG("Rt relation constants for "<<m_idHelperSvc->toString(athenaId)<<" already exists");
454 continue;
455 }
456 }
457
458 MuonCalib::CalibFunc::ParVec rtPars{}, resoPars{};
459
460 MuonCalib::SamplePoint tr_point, ts_point; // pairs of numbers; tr = (time,radius); ts = (time,sigma) [sigma=resolution]
461 std::vector<MuonCalib::SamplePoint> tr_points{}, ts_points{};
463 float multilayer_tmax_diff{-std::numeric_limits<float>::max()};
464
465 // loop over RT function payload (triplets of radius,time,sigma(=resolution) )
466 for (unsigned int k = 0; k < radii.size(); ++k) {
467 float radius = radii[k];
468 if (m_rtShift != 0.) {
469 float oldradius = radius;
470 // TODO: What is this magic number
471 float rshift = m_rtShift * 1.87652e-2 * radius * (radius - *innerTubeRadius);
472 radius = oldradius + rshift;
473 ATH_MSG_DEBUG("DEFORM RT: old radius " << oldradius << " new radius " << radius << " shift " << rshift
474 << " max shift " << m_rtShift);
475 }
476
477 if (m_rtScale != 1.) {
478 float oldradius = radius;
479 radius = radius * m_rtScale;
480 ATH_MSG_DEBUG("DEFORM RT: old radius " << oldradius << " new radius " << radius << " scale factor " << m_rtScale);
481 }
482 tr_point.set_x2(radius);
483
484 float time = times[k];
485 tr_point.set_x1(time);
486 ts_point.set_x1(time);
487
488 float sigma = resolutions[k];
489 ts_point.set_x2(sigma);
490 ts_point.set_error(1.0);
491 tr_point.set_error(1.0);
492 if (tr_point.x2() < -99) { // if radius is < -99 then treat time as ML Tmax difference
493 multilayer_tmax_diff = tr_point.x1();
494 } else if (tr_points.empty() || (tr_points.back().x1() < tr_point.x1() && tr_points.back().x2() < tr_point.x2())) {
495 tr_points.push_back(tr_point);
496 ts_points.push_back(ts_point);
497 }
498 } // end loop over RT function payload (triplets of radius,time,resolution)
499
501 if (ts_points.size() < 3) {
502 ATH_MSG_FATAL("Rt relation broken!");
503 return StatusCode::FAILURE;
504 }
505
506 if (rt_ts_applied != m_TimeSlewingCorrection) {
507 float sign(rt_ts_applied ? -1.0 : 1.0);
508 float slice_width = (*innerTubeRadius) / static_cast<float>(m_MeanCorrectionVsR.size());
509 for (auto & tr_point : tr_points) {
510 int slice_number = static_cast<int>(std::floor(tr_point.x2() / slice_width));
511 if (slice_number < 0) slice_number = 0;
512 if (slice_number >= static_cast<int>(m_MeanCorrectionVsR.size()))
513 slice_number = static_cast<int>(m_MeanCorrectionVsR.size()) - 1;
514 tr_point.set_x1(tr_point.x1() + sign * m_MeanCorrectionVsR[slice_number]);
515 }
516 }
517
518 // Create resolution function from ts_points
519 std::unique_ptr<MuonCalib::IRtResolution> reso = getRtResolutionInterpolation(ts_points);
520 if (msgLvl(MSG::VERBOSE)) {
521 ATH_MSG_VERBOSE("Resolution points :");
522 for (const MuonCalib::SamplePoint& point : tr_points) {
523 ATH_MSG_VERBOSE(point.x1() << "|" << point.x2() << "|" << point.error());
524 }
525 ATH_MSG_DEBUG("Resolution parameters :");
526 for (unsigned int i = 0; i < reso->nPar(); i++) { ATH_MSG_VERBOSE(i << " " << reso->par(i)); }
527 }
528
529 // Create RT function from tr_points and load RT and resolution functions
530 std::unique_ptr<MuonCalib::IRtRelation> rt = MuonCalib::RtFromPoints::getRtRelationLookUp(tr_points);
531 if (!reso || !rt) { continue; }
532
533 if (rt->par(1) == 0.) {
534 ATH_MSG_FATAL("Bin size is 0");
535 for (const MuonCalib::SamplePoint& it: tr_points)
536 ATH_MSG_WARNING(it.x1() << " " << it.x2() << " " << it.error());
537 return StatusCode::FAILURE;
538 }
539 // Save ML difference if it is available
540 if (multilayer_tmax_diff > -8e8) { rt->SetTmaxDiff(multilayer_tmax_diff); }
541 // Store RT and resolution functions for this region
542 RtRelationPtr rt_rel = std::make_shared<MuonCalib::MdtRtRelation>(std::move(rt), std::move(reso));
543
544 if (!writeCdo.storeData(athenaId ,rt_rel, msgStream())) return StatusCode::FAILURE;
546 loadedRtRel[athenaId] = std::move(rt_rel);
547
548 } // end loop over itr (strings read from COOL)
549 ATH_CHECK(defaultRt(writeCdo, loadedRtRel));
550
551 if (loadedRtRel.empty()) {
552 return StatusCode::SUCCESS;
553 }
554
555 ATH_MSG_DEBUG("Initializing " << loadedRtRel.size()<< " b-field functions");
556 const MdtCondDbData* condDbData{nullptr};
557 if (!m_readKeyDCS.empty()) {
558 SG::ReadCondHandle readCondHandleDb{m_readKeyDCS, ctx};
560 if (readCondHandleDb->hasDCS()) {
561 condDbData = readCondHandleDb.cptr();
562 } else {
563 ATH_MSG_INFO("Do not retrieve the HV from DCS. Fall back to 2730 & 3080");
564 }
565 }
566
567
568 for (const auto& [athenaId, rtRelation] : loadedRtRel) {
569 CorrectionPtr corrFuncSet = std::make_unique<MuonCalib::MdtCorFuncSet>();
570
572 std::vector<double> corr_params(2);
573 bool loadDefault{false};
574 if (condDbData){
575 const MuonCond::DcsConstants& dcs{condDbData->getHvState(athenaId)};
576 corr_params[0] = dcs.readyVolt;
578 if (corr_params[0] < std::numeric_limits<float>::epsilon()) {
579 ATH_MSG_DEBUG("Chamber "<<m_idHelperSvc->toString(athenaId)<<" is switched off "<<dcs);
580 loadDefault = true;
581 }
582 } else loadDefault = true;
583 if (loadDefault) {
584 if (m_idHelperSvc->issMdt(athenaId)) {
585 corr_params[0] = 2730.0;
586 } else {
587 corr_params[0] = 3080.0;
588 }
589 }
590 corr_params[1] = 0.11; // epsilon parameter
591 corrFuncSet->setBField(std::make_unique<MuonCalib::BFieldCorFunc>("medium", corr_params, rtRelation->rt()));
592 }
594 corrFuncSet->setSlewing(std::make_unique<MuonCalib::MdtSlewCorFuncHardcoded>(MuonCalib::CalibFunc::ParVec()));
595 }
596 if (!writeCdo.storeData(athenaId, std::move(corrFuncSet), msgStream())) return StatusCode::FAILURE;
597 }
598
599 return StatusCode::SUCCESS;
600}
601
602// build the transient structure and load some defaults for T0s
604 const MdtIdHelper& id_helper{m_idHelperSvc->mdtIdHelper()};
605
606 // loop over modules (MDT chambers) and create an MdtTubeContainer for each
608 MdtIdHelper::const_id_iterator it_end = id_helper.module_end();
609 for (; it != it_end; ++it) {
610
611 if (writeCdo.hasDataForChannel(*it, msgStream())) {
612 const MdtFullCalibData* dataObj = writeCdo.getCalibData(*it, msgStream());
613 if (dataObj->tubeCalib) {
614 ATH_MSG_DEBUG("Rt relation constants for "<<m_idHelperSvc->toString(*it)<<" already exists");
615 continue;
616 }
617 }
618 // create an MdtTubeContainer
619 TubeContainerPtr tubes = std::make_unique<MuonCalib::MdtTubeCalibContainer>(m_idHelperSvc.get(), *it);
620 if (!writeCdo.storeData(*it, tubes, msgStream())) return StatusCode::FAILURE;
621
622 // is tubes ever 0? how could that happen?
623 double t0 = m_defaultT0;
624
625 unsigned int nml = tubes->numMultilayers();
626 unsigned int nlayers = tubes->numLayers();
627 unsigned int ntubes = tubes->numTubes();
628 int size = nml * nlayers * ntubes;
629
630 ATH_MSG_VERBOSE("Adding chamber " << m_idHelperSvc->toString(*it)
631 <<" size " << size << " ml " << nml << " l " << nlayers << " t " << ntubes);
632 for (unsigned int ml = 1; ml <= nml; ++ml) {
633 for (unsigned int l = 1; l <= nlayers; ++l) {
634 for (unsigned int t = 1; t <= ntubes; ++t) {
636 const Identifier tubeId = id_helper.channelID(*it, ml, l, t);
637 data.t0 = t0;
638 data.adcCal = 1.;
639 tubes->setCalib(std::move(data), tubeId, msgStream());
640 }
641 }
642 }
643 }
644 return StatusCode::SUCCESS;
645}
646
647
648StatusCode MdtCalibDbAlg::legacyTubePayloadToJSON(const coral::AttributeList& attr,nlohmann::json & json) const {
649 std::string data{};
650 if (attr["data"].specification().type() == typeid(coral::Blob)) {
651 ATH_MSG_VERBOSE("Loading data as a BLOB, uncompressing...");
652 if (!CoralUtilities::readBlobAsString(attr["data"].data<coral::Blob>(), data)) {
653 ATH_MSG_FATAL("Cannot uncompress BLOB! Aborting...");
654 return StatusCode::FAILURE;
655 }
656
657 } else {
658 data = *(static_cast<const std::string *>((attr["data"]).addressOfData()));
659 }
660 std::vector<std::string> tokens = tokenize(data, "\n");
661 if (tokens.size() < 2) {
662 ATH_MSG_FATAL("The line "<<data<<" cannot be resolved into header & payload");
663 return StatusCode::FAILURE;
664 }
665 std::string& header = tokens[0];
666 const std::string& payload = tokens[1];
667
672 const std::string stName = header.substr(2,3);
673 int eta{0}, phi{0}, nTubes{0};
674 {
675 std::replace(header.begin(), header.end(),'_', ',');
676 const std::vector<std::string> headerTokens = tokenize(header, ",");
677 phi = atoi(headerTokens[1]);
678 eta = atoi(headerTokens[2]);
679 nTubes = atoi(headerTokens[5]);
680 }
681 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
682 bool isValid{false};
683 const Identifier chamID = idHelper.elementID(stName, eta, phi, isValid);
684 if (!isValid) {
685 static std::atomic<bool> idWarningPrinted = false;
686 if (!idWarningPrinted) {
687 ATH_MSG_WARNING(__FILE__<<":"<<__LINE__<<" Identifier: "<<stName<<","<<eta<<","<<phi
688 <<" is invalid. Skipping");
689 idWarningPrinted.store(true, std::memory_order_relaxed);
690 }
691 return StatusCode::SUCCESS;
692 }
693 nlohmann::json channel{};
694 const bool t0_ts_applied = (attr["tech"].data<int>() & MuonCalib::TIME_SLEWING_CORRECTION_APPLIED);
695 channel["appliedT0"] = t0_ts_applied;
696 channel["station"] = stName;
697 channel["eta"] = eta;
698 channel["phi"] = phi;
699
700 const std::vector<double> payLoadData = tokenizeDouble(payload, ",");
701 std::vector<double> tzeros{}, meanAdcs{};
702 std::vector<int> statusCodes{};
705 for (unsigned int k = 0; k < payLoadData.size(); ++k){
706 const double value = payLoadData[k];
707 switch (k%3) {
708 case 0:
709 tzeros.push_back(value);
710 break;
711 case 1:
712 statusCodes.push_back(value);
713 break;
714 case 2:
715 meanAdcs.push_back(value);
716 break;
717
718 }
719 }
720 if (statusCodes.size() != tzeros.size() ||
721 statusCodes.size() != meanAdcs.size() ||
722 statusCodes.empty()) {
723 ATH_MSG_FATAL("Failed to properly readt t0 calibrations for chamber "<<m_idHelperSvc->toStringChamber(chamID));
724 return StatusCode::FAILURE;
725 }
727 int ml{1}, layer{1}, tube{1};
728
729 const int numMl = idHelper.numberOfMultilayers(chamID);
730 const Identifier secondMlID = idHelper.multilayerID(chamID, numMl);
731 const int tubesPerLay = std::max(idHelper.tubeMax(chamID), idHelper.tubeMax(secondMlID));
732 const int numLayers = std::max(idHelper.tubeLayerMax(chamID), idHelper.tubeLayerMax(secondMlID));
733 if (m_checkTubes && (numMl * numLayers * tubesPerLay) != nTubes) {
734 ATH_MSG_FATAL("Calibration database differs in terms of number of tubes for chamber "
735 <<m_idHelperSvc->toStringChamber(chamID)<<". Expected "<<(numMl * numLayers * tubesPerLay)
736 <<" vs. observed "<<nTubes);
737 return StatusCode::FAILURE;
738 }
739 nlohmann::json calibData = nlohmann::json::array();
740 for (unsigned int k = 0; k < tzeros.size(); ++k) {
741 nlohmann::json channelData{};
742 channelData["ml"] = ml;
743 channelData["layer"] =layer;
744 channelData["tube"] = tube;
745 channelData["t0"] = tzeros[k];
746 channelData["meanAdc"] = meanAdcs[k];
747 channelData["status"] = statusCodes[k];
748 ++tube;
749 if (tube > tubesPerLay){
750 tube = 1;
751 ++layer;
752 }
753 if (layer > numLayers){
754 layer = 1;
755 ++ml;
756 }
757 calibData.push_back(std::move(channelData));
758 }
759 channel["calibConstants"] = std::move(calibData);
760 json.push_back(std::move(channel));
761 return StatusCode::SUCCESS;
762}
763StatusCode MdtCalibDbAlg::loadTube(const EventContext& ctx, MuonCalib::MdtCalibDataContainer& writeCdo) const {
764 ATH_MSG_DEBUG("loadTube " << name());
765 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
766
767 // Read Cond Handle
768 SG::ReadCondHandle readHandleTube{m_readKeyTube, ctx};
769 // read new-style format 2020
770 nlohmann::json t0CalibJson = nlohmann::json::array();
771 if (m_newFormat2020) {
772 for (CondAttrListCollection::const_iterator itr = readHandleTube->begin();
773 itr != readHandleTube->end(); ++itr) {
774 const coral::AttributeList &atr = itr->second;
775 std::string data{};
776 if (atr["data"].specification().type() == typeid(coral::Blob)) {
777 ATH_MSG_VERBOSE("Loading data as a BLOB, uncompressing...");
778 if (!CoralUtilities::readBlobAsString(atr["data"].data<coral::Blob>(), data)) {
779 ATH_MSG_FATAL("Cannot uncompress BLOB! Aborting...");
780 return StatusCode::FAILURE;
781 }
782 } else {
783 ATH_MSG_VERBOSE("Loading data as a STRING");
784 data = *(static_cast<const std::string *>((atr["data"]).addressOfData()));
785 }
786 // unwrap the json and build the data vector
787 nlohmann::json yy = nlohmann::json::parse(data);
788 for (auto &it : yy.items()) {
789 nlohmann::json yx = it.value();
790 t0CalibJson.push_back(yx);
791 }
792 }
793 }
794 // read old-style format
795 else {
796 for (CondAttrListCollection::const_iterator itr = readHandleTube->begin();
797 itr != readHandleTube->end(); ++itr) {
798 ATH_CHECK(legacyTubePayloadToJSON(itr->second, t0CalibJson));
799 }
800 }
801
802 // Inverse of wire propagation speed
803 const float inversePropSpeed = 1. / (Gaudi::Units::c_light * m_prop_beta);
804 writeCdo.setInversePropSpeed(inversePropSpeed);
805
806
807 // unpack the strings in the collection and update the
808 // MdtTubeCalibContainers in TDS
809 for (const auto& chambChannel : t0CalibJson) {
810 const std::string stName = chambChannel["station"];
811 const int ieta = chambChannel["eta"];
812 const int iphi = chambChannel["phi"];
813 const bool t0_ts_applied = chambChannel["appliedT0"];
814 // need to check validity of Identifier since database contains all Run 2 MDT chambers, e.g. also EI chambers which are
815 // potentially replaced by NSW
816 bool isValid{false}; // the elementID takes a bool pointer to check the validity of the Identifier
817 const Identifier chId = idHelper.elementID(stName, ieta, iphi, isValid);
818 if (!isValid) {
819 static std::atomic<bool> idWarningPrinted = false;
820 if (!idWarningPrinted) {
821 ATH_MSG_WARNING("Element Identifier " << chId.get_compact() << " retrieved for station name " << stName
822 << " is not valid, skipping");
823 idWarningPrinted.store(true, std::memory_order_relaxed);
824 }
825 continue;
826 }
827
828 if (writeCdo.hasDataForChannel(chId, msgStream())) {
829 const MdtFullCalibData* dataObj = writeCdo.getCalibData(chId, msgStream());
830 if (dataObj->tubeCalib) {
831 ATH_MSG_DEBUG("Rt relation constants for "<<m_idHelperSvc->toString(chId)<<" already exists");
832 continue;
833 }
834 }
835
836 TubeContainerPtr tubes = std::make_unique<MdtTubeCalibContainer>(m_idHelperSvc.get(), chId);
837 if (!writeCdo.storeData(chId, tubes, msgStream())) {
838 ATH_MSG_FATAL(__FILE__<<":"<<__LINE__<<" Failed to add chamber "<<m_idHelperSvc->toString(chId)
839 <<" ID fields: "<<stName<<","<<ieta<<","<<iphi);
840 return StatusCode::FAILURE;
841 }
842 //const ref. No copy
843 const nlohmann::json& tubeConstants = chambChannel["calibConstants"];
844 for (const auto& tubeChannel : tubeConstants) {
845 const int ml = tubeChannel["ml"];
846 const int l = tubeChannel["layer"];
847 const int t = tubeChannel["tube"];
848 double tzero = tubeChannel["t0"];
849 if (m_t0Shift != 0.) {
850 tzero += m_t0Shift;
851 ATH_MSG_VERBOSE("T0 shift " << m_t0Shift << " t0 " << tzero << " id " << ml << " " << l << " " << t);
852 }
853 if (m_t0Spread != 0.) {
854 CLHEP::HepRandomEngine *engine = m_RNGWrapper->getEngine(ctx);
855 double sh = CLHEP::RandGaussZiggurat::shoot(engine, 0., m_t0Spread);
856 tzero += sh;
857 ATH_MSG_VERBOSE("T0 spread " << sh << " t0 " << tzero << " id " << ml << " " << l << " " << t);
858 }
859 if (!t0_ts_applied && m_TimeSlewingCorrection) { tzero += m_TsCorrectionT0; }
860 if (t0_ts_applied && !m_TimeSlewingCorrection) { tzero -= m_TsCorrectionT0; }
861
862 const int statusCode = tubeChannel["status"];
863 const double meanAdc = tubeChannel["meanAdc"];
865 datatube.statusCode = statusCode;
866 datatube.t0 = tzero;
867 datatube.adcCal = meanAdc;
868 const Identifier tubeId = idHelper.channelID(chId, ml, l, t);
869 tubes->setCalib(std::move(datatube), tubeId, msgStream());
870 }
871 } // end loop over readCdoTube
872
873 ATH_CHECK(defaultT0s(writeCdo));
874 // finally record writeCdo
875 return StatusCode::SUCCESS;
876}
877
878std::unique_ptr<MuonCalib::RtResolutionLookUp> MdtCalibDbAlg::getRtResolutionInterpolation(const std::vector<MuonCalib::SamplePoint> &sample_points) {
880 // VARIABLES //
882 std::vector<Double_t> x(sample_points.size(),0);
883 std::vector<Double_t> y(sample_points.size(),0);
884
885 for (unsigned int i = 0; i < sample_points.size(); i++) {
886 x[i] = sample_points[i].x1();
887 y[i] = sample_points[i].x2();
888 }
889 TSpline3 sp("Rt Res Tmp", x.data(), y.data(), sample_points.size());
891 // CREATE AN RtRelationLookUp OBJECT WITH THE CORRECT PARAMETERS //
893 unsigned int nb_points(100);
894 std::vector<double> res_param(nb_points + 2); // r-t parameters
895 Double_t bin_width = (x[sample_points.size() - 1] - x[0]) / static_cast<Double_t>(nb_points);
896
897 res_param[0] = x[0];
898 res_param[1] = bin_width;
899 for (unsigned int k = 0; k < nb_points; k++) {
900 Double_t xx = x[0] + k * bin_width;
901 res_param[k + 2] = sp.Eval(xx);
902 if (std::isnan(res_param[k + 2])) {
903 TFile outf("kacke.root", "RECREATE");
904 sp.Write("kacke");
905 THROW_EXCEPTION("MdtCalibDbAlg::getRtResolutionInterpolation "
906 "encountered nan element");
907 }
908 }
909 return std::make_unique<MuonCalib::RtResolutionLookUp>(std::move(res_param));
910}
911}
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_WARNING(x,...)
#define ATH_MSG_VERBOSE(x,...)
#define ATH_MSG_INFO(x,...)
#define ATH_MSG_FATAL(x,...)
This file defines the class for a collection of AttributeLists where each one is associated with a ch...
static boost::dynamic_bitset rshift(boost::dynamic_bitset<> const &b, int n)
nlohmann::json json
Abstract interface to IOVDbSvc to access IOVRange and tag information.
static Double_t sp
static Double_t t0
MdtFullCalibData::CorrectionPtr CorrectionPtr
MdtFullCalibData::TubeContainerPtr TubeContainerPtr
MdtCalibDataContainer::RegionGranularity RegionGranularity
int sign(int a)
#define y
#define x
const ServiceHandle< StoreGateSvc > & detStore() const
bool msgLvl(const MSG::Level lvl) const
ChanAttrListMap::const_iterator const_iterator
static EventIDRange infiniteTime()
Produces an EventIDRange that is inifinite in Time and invalid in RunLumi.
value_type get_compact() const
Get the compact id.
const DcsConstants & getHvState(const Identifier &multiLayerID) const
Identifier multilayerID(const Identifier &channeldID) const
int multilayer(const Identifier &id) const
Access to components of the ID.
Identifier elementID(int stationName, int stationEta, int stationPhi) const
int tube(const Identifier &id) const
static int tubeLayerMax()
int tubeLayer(const Identifier &id) const
int numberOfMultilayers(const Identifier &id) const
int tubeMax() const
Identifier channelID(int stationName, int stationEta, int stationPhi, int multilayer, int tubeLayer, int tube) const
std::vector< double > ParVec
Definition CalibFunc.h:35
bool storeData(const Identifier &mlID, CorrectionPtr corrFuncSet, MsgStream &msg)
bool hasDataForChannel(const Identifier &measId, MsgStream &msg) const
Checks whether a calibration data object is already present.
void setInversePropSpeed(const float speed)
const MdtFullCalibData * getCalibData(const Identifier &measId, MsgStream &msg) const
Returns the calibration data associated with this station.
void setBField(std::unique_ptr< IMdtBFieldCorFunc > &&bField)
void setSlewing(std::unique_ptr< IMdtSlewCorFunc > &&slew)
bool setCalib(SingleTubeCalib val, const Identifier &tubeId, MsgStream &msg)
set the calibration constants of a single tube
Implements fixed identifiers not dependent upon Athena Identifier for internal use in the calibration...
Definition MuonFixedId.h:50
Manages the I/O of the Rt realtions from/to file.
std::vector< double > DataVec
static std::unique_ptr< IRtRelation > getRtRelationLookUp(const std::vector< SamplePoint > &sample_points)
This class provides a sample point for the BaseFunctionFitter.
Definition SamplePoint.h:15
double x2() const
get the error on the x2 coordinate of the sample point
Definition SamplePoint.h:44
void set_error(const double merror)
Definition SamplePoint.h:62
void set_x2(const double mx2)
set the error of the x2 coordinate sample point to merror
Definition SamplePoint.h:60
void set_x1(const double mx1)
set the x2 coordinate of the sample point to mx2
Definition SamplePoint.h:58
double x1() const
< get the x1 coordinate of the sample point
Definition SamplePoint.h:42
Readout element to describe the Monitored Drift Tube (Mdt) chambers Mdt chambers usually comrpise out...
double innerTubeRadius() const
Returns the inner tube radius.
double innerTubeRadius() const
Returns the inner tube radius excluding the aluminium walls.
std::vector< Identifier >::const_iterator const_id_iterator
const_id_iterator module_end() const
const_id_iterator module_begin() const
Iterators over full set of ids.
const_id_iterator detectorElement_begin() const
Iterators over full set of ids.
const_id_iterator detectorElement_end() const
MuonCalib::MdtFullCalibData::RtRelationPtr RtRelationPtr
StatusCode legacyTubePayloadToJSON(const coral::AttributeList &attr, nlohmann::json &json) const
virtual StatusCode execute(const EventContext &ctx) const override
std::optional< double > getInnerTubeRadius(const Identifier &id) const
StatusCode declareDependency(const EventContext &ctx, SG::WriteCondHandle< MuonCalib::MdtCalibDataContainer > &writeHandle) const
SG::ReadCondHandleKey< CondAttrListCollection > m_readKeyTube
SG::WriteCondHandleKey< MuonCalib::MdtCalibDataContainer > m_writeKey
StatusCode defaultRt(MuonCalib::MdtCalibDataContainer &writeCdoRt, LoadedRtMap &loadedRts) const
Gaudi::Property< bool > m_checkTubes
only needed to retrieve information on number of tubes etc. (no alignment needed)
StatusCode loadTube(const EventContext &ctx, MuonCalib::MdtCalibDataContainer &writeCdo) const
ToolHandle< MuonCalib::IIdToFixedIdTool > m_idToFixedIdTool
StatusCode legacyRtPayloadToJSON(const coral::AttributeList &attr, nlohmann::json &json) const
Parses the legacy payload for the RT functions to a json format.
StatusCode defaultT0s(MuonCalib::MdtCalibDataContainer &writeCdoTube) const
static std::unique_ptr< MuonCalib::RtResolutionLookUp > getRtResolutionInterpolation(const std::vector< MuonCalib::SamplePoint > &sample_points)
StatusCode loadRt(const EventContext &ctx, MuonCalib::MdtCalibDataContainer &writeCdo) const
SG::ReadCondHandleKey< CondAttrListCollection > m_readKeyRt
static std::string find_calib_file(const std::string &logical_file_name)
const DataObjID & fullKey() const
const EventIDRange & getRange()
const_pointer_type cptr()
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 &)
std::vector< double > tokenizeDouble(std::string_view the_str, std::string_view delimiter)
const std::string & name() const
Return the root name of the expression.
bit_t size() const
Count the number of 1 bits in the set.
std::vector< int > tokenizeInt(std::string_view str, std::string_view delimiter)
std::vector< std::string > tokenize(std::string_view the_str, std::string_view delimiters)
Splits the string into smaller substrings.
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...
namespace to identify classes related only to muon calibration
const std::string & stName(StIndex index)
convert StIndex into a string
NRpcCablingAlg reads raw condition data and writes derived condition data to the condition store.
class which holds the full set of calibration constants for a given tube
GeoModel::TransientConstSharedPtr< MdtTubeCalibContainer > TubeContainerPtr
GeoModel::TransientConstSharedPtr< MdtCorFuncSet > CorrectionPtr
float adcCal
quality flag for the SingleTubeCalib constants: 0 all ok, 1 no hits found, 2 too few hits,...
Helper struct to cache all dcs constants in a common place of the memory.
#define THROW_EXCEPTION(MESSAGE)
Definition throwExcept.h:10