16 #include "GaudiKernel/PhysicalConstants.h"
18 #include "CoralBase/Attribute.h"
19 #include "CoralBase/AttributeListSpecification.h"
20 #include "CoralBase/Blob.h"
23 #include <unordered_map>
39 return StatusCode::SUCCESS;
45 if(writeHandle.isValid()) {
46 ATH_MSG_DEBUG(
"CondHandle " << writeHandle.fullKey() <<
" is already valid.");
47 return StatusCode::SUCCESS;
51 auto writeCdo = std::make_unique<MuonCalib::MdtCalibDataContainer>(
m_idHelperSvc.get(), gran);
56 ATH_CHECK(writeHandle.record(std::move(writeCdo)));
59 return StatusCode::SUCCESS;
67 if(!readHandleRt.isValid()) {
69 return StatusCode::FAILURE;
72 std::unordered_map<Identifier, RtPayload> rtCalibration{};
74 it != readHandleRt->end(); ++
it) {
77 return StatusCode::FAILURE;
81 std::vector<const MuonGMR4::MdtReadoutElement*> detEls =
m_r4detMgr->getAllMdtReadoutElements();
83 const Identifier& detElId = mdtDetEl->identify();
93 auto it = rtCalibration.find(detElId);
95 if(
it == rtCalibration.end()) {
103 it = rtCalibration.find(mlID);
104 if(
it == rtCalibration.end()) {
119 std::vector<MuonCalib::SamplePoint> samplePointsRt{};
120 for(
size_t i = 0;
i <
payload.times.size(); ++
i) {
123 samplePointsRt.emplace_back(chebyShevT,
payload.radii[
i],
payload.resolutions[
i]);
129 for(
const double c :
fitter.coefficients()) {
135 std::vector<MuonCalib::SamplePoint> samplePointsTr{};
136 for(
size_t i = 0;
i <
payload.radii.size(); ++
i) {
139 samplePointsTr.emplace_back(chebyShevR,
payload.times[
i],
payload.resolutions[
i]);
143 for(
const double c :
fitter.coefficients()) {
148 auto rtRelRegion{std::make_shared<MuonCalib::RtChebyshev>(rtPars)};
149 auto rtResRegion{std::make_shared<MuonCalibR4::RtResolutionSqrt>(resolutionPars)};
150 auto trRelRegion{std::make_shared<MuonCalib::TrChebyshev>(rtRelRegion, trPars)};
151 RtRelationPtr MdtRt = std::make_unique<MuonCalib::MdtRtRelation>(std::move(rtRelRegion), std::move(rtResRegion), std::move(trRelRegion), 0.);
152 if(!writeHandle.
storeData(detElId, MdtRt, msgStream())) {
154 return StatusCode::FAILURE;
157 return StatusCode::SUCCESS;
163 if(!readHandleTube.isValid()) {
165 return StatusCode::FAILURE;
167 std::unordered_map<Identifier, RtTubePayload> tubeCalibration{};
169 it != readHandleTube->end(); ++
it) {
172 return StatusCode::FAILURE;
176 std::vector<const MuonGMR4::MdtReadoutElement*> detEls =
m_r4detMgr->getAllMdtReadoutElements();
178 const Identifier& chamberId = mdtDetEl->identify();
186 if(!writeCdo.
storeData(chamberId, tubes, msgStream())) {
188 return StatusCode::FAILURE;
191 for(std::size_t
tube = 1;
tube <= mdtDetEl->numTubesInLay(); ++
tube){
193 auto it = tubeCalibration.find(tubeId);
194 if(
it == tubeCalibration.end()) {
201 tubeCalibration.inversePropSpeed = inversePropSpeed;
202 tubeCalibration.t0 =
payload.tZero;
203 tubeCalibration.adcCal =
payload.meanAdc;
204 if(!tubes->setCalib(std::move(tubeCalibration), tubeId, msgStream())) {
206 return StatusCode::FAILURE;
211 return StatusCode::SUCCESS;
216 std::string
data{}, delim{};
217 if (attributeList[
"data"].specification().
type() ==
typeid(
coral::Blob)) {
220 return StatusCode::FAILURE;
224 data = *(
static_cast<const std::string *
>((attributeList[
"data"]).addressOfData()));
230 return StatusCode::FAILURE;
235 if(tokensHeader.size()< 2){
236 ATH_MSG_FATAL(
"Failed to deduce extract number of points & calib Identifier from "<<
header);
237 return StatusCode::FAILURE;
239 const std::size_t calibId = tokensHeader[0];
240 const std::size_t numPoints = tokensHeader[1];
244 return StatusCode::FAILURE;
248 ATH_MSG_WARNING(
"The translation from the calibration ID with station: "
249 <<
id.stationNameString()<<
"("<<
id.
stationName()<<
") "
250 <<
" eta:"<<
id.eta()<<
" phi: "<<
id.phi());
257 channelPayload.ml =
m_idHelperSvc->mdtIdHelper().multilayer(athenaId);
258 channelPayload.layer =
m_idHelperSvc->mdtIdHelper().tubeLayer(athenaId);
259 channelPayload.tube =
m_idHelperSvc->mdtIdHelper().tube(athenaId);
260 channelPayload.appliedRT = (attributeList[
"tech"].data<
int>());
263 if (dataPoints.size() % 3 != 0) {
264 ATH_MSG_FATAL(
"Payload does not contain a multiple of 3 elements. Size: " << dataPoints.size());
265 return StatusCode::FAILURE;
269 const std::size_t expectedPoints = dataPoints.size() / 3;
270 if (expectedPoints != numPoints) {
271 ATH_MSG_FATAL(
"Number of data points (" << expectedPoints <<
") does not match numPoints in header (" << numPoints <<
").");
272 return StatusCode::FAILURE;
275 std::vector<double> radii(expectedPoints);
276 std::vector<double>
times(expectedPoints);
277 std::vector<double> resos(expectedPoints);
279 for (
size_t i = 0;
i < expectedPoints; ++
i) {
280 radii[
i] = dataPoints[
i * 3];
281 times[
i] = dataPoints[
i * 3 + 1];
282 resos[
i] = dataPoints[
i * 3 + 2];
284 channelPayload.radii = std::move(radii);
285 channelPayload.times = std::move(
times);
286 channelPayload.resolutions = std::move(resos);
288 rtCalibration[athenaId] = std::move(channelPayload);
289 return StatusCode::SUCCESS;
296 if (attributeList[
"data"].specification().
type() ==
typeid(
coral::Blob)) {
300 return StatusCode::FAILURE;
303 data = *(
static_cast<const std::string*
>(attributeList[
"data"].addressOfData()));
308 ATH_MSG_FATAL(
"The line " <<
data <<
" cannot be resolved into header & payload");
309 return StatusCode::FAILURE;
315 std::string stName =
header.substr(2, 3);
319 if (headerTokens.size() < 6) {
321 return StatusCode::FAILURE;
324 int phi =
std::atoi(headerTokens[1].c_str());
325 int eta =
std::atoi(headerTokens[2].c_str());
329 channelPayload.
station = stName;
330 channelPayload.eta = eta;
331 channelPayload.phi = phi;
332 channelPayload.appliedT0 = (attributeList[
"tech"].data<
int>());
335 if (payLoadData.size() % 3 != 0) {
336 ATH_MSG_FATAL(
"Payload size is not a multiple of 3. Size: " << payLoadData.size());
337 return StatusCode::FAILURE;
340 std::size_t numPoints = payLoadData.size() / 3;
341 std::vector<double> tZeros, meanAdcs;
342 std::vector<int> statusCodes;
344 tZeros.reserve(numPoints);
345 meanAdcs.reserve(numPoints);
346 statusCodes.reserve(numPoints);
348 for (std::size_t
i = 0;
i < payLoadData.size();
i += 3) {
349 tZeros.push_back(payLoadData[
i]);
350 statusCodes.push_back(
static_cast<int>(payLoadData[
i + 1]));
351 meanAdcs.push_back(payLoadData[
i + 2]);
354 if (statusCodes.size() != tZeros.size() || statusCodes.size() != meanAdcs.size() || statusCodes.empty()) {
356 return StatusCode::FAILURE;
360 const int numMl =
m_idHelperSvc->mdtIdHelper().numberOfMultilayers(chamID);
367 for(std::size_t
i = 0;
i < tZeros.size(); ++
i){
373 tubePayload.
tZero = tZeros[
i];
376 tubeCalibration[tubeID] = std::move(tubePayload);
377 if (++
tube > tubesPerLay) {
385 return StatusCode::SUCCESS;