22 #include "GaudiKernel/PhysicalConstants.h"
24 #include "CoralBase/Attribute.h"
25 #include "CoralBase/AttributeListSpecification.h"
26 #include "CoralBase/Blob.h"
29 #include <unordered_map>
41 #define SET_BRANCHADDRESS(tree, branchName) \
43 if (!tree.GetBranch(#branchName)) { \
44 ATH_MSG_FATAL("The branch "<<#branchName<<" does not exist."); \
45 return StatusCode::FAILURE; \
47 if (tree.SetBranchAddress(#branchName,&branchName) != 0){ \
48 ATH_MSG_FATAL("Failed to connect branch "<<#branchName<<"."); \
49 return StatusCode::FAILURE; \
61 if (m_t0TreeName.value().empty()) {
62 m_t0RootFile.value().clear();
64 if (m_rtTreeName.value().empty()) {
65 m_rtRootFile.value().clear();
67 const bool initRt = (m_rtJSON.value().empty() && m_rtRootFile.value().empty());
68 const bool initT0 = (m_t0JSON.value().empty() && m_t0RootFile.value().empty());
69 ATH_CHECK(m_readKeyRt.initialize(initRt));
70 ATH_CHECK(m_readKeyTube.initialize(initT0));
72 if (m_rtRootFile.value().size()) {
73 ATH_MSG_INFO(
"Load RT - calibration constants from ROOT File "<<m_rtRootFile);
75 }
else if (m_rtJSON.value().size()) {
76 ATH_MSG_INFO(
"Load the RT- calibration constants from JSON file "<<m_rtJSON);
79 ATH_MSG_INFO(
"Load RT - calibration constants from COOL: "<<m_readKeyRt.fullKey());
81 if (m_t0RootFile.value().size()) {
82 ATH_MSG_INFO(
"Load T0 - calibration constants from ROOT File "<<m_t0RootFile);
84 }
else if (m_t0JSON.value().size()) {
85 ATH_MSG_INFO(
"Load the T0 - calibration constants from JSON file "<<m_t0JSON);
88 return StatusCode::SUCCESS;
94 if(writeHandle.isValid()) {
95 ATH_MSG_DEBUG(
"CondHandle " << writeHandle.fullKey() <<
" is already valid.");
96 return StatusCode::SUCCESS;
99 auto writeCdo = std::make_unique<MdtCalibDataContainer>(m_idHelperSvc.get(), RegionGranularity::OnePerMultiLayer);
104 if (!m_readKeyRt.empty()) {
107 writeHandle.addDependency(readHandle);
109 itr != readHandle->end(); ++itr) {
113 return StatusCode::FAILURE;
118 if(m_dbPayloadType ==
"TTree"){
119 std::unique_ptr<TTree>
tree;
122 return StatusCode::FAILURE;
125 }
else if (m_dbPayloadType==
"JSON") {
130 return StatusCode::FAILURE;
133 ATH_CHECK(parseRtPayload(std::move(rtBlob),*writeCdo));
135 ATH_MSG_FATAL(
"Payload type " << m_dbPayloadType <<
" not understood. Options are: TTree or JSON");
136 return StatusCode::FAILURE;
140 }
else if (!m_rtRootFile.value().empty()) {
141 std::unique_ptr<TFile>
inFile{TFile::Open(m_rtRootFile.value().c_str(),
"READ")};
144 return StatusCode::FAILURE;
146 TTree*
tree{
nullptr};
147 inFile->GetObject(m_rtTreeName.value().c_str(),
tree);
149 ATH_MSG_FATAL(
"The object "<<m_rtRootFile<<
" does not contain "<<m_rtTreeName);
150 return StatusCode::FAILURE;
153 }
else if (!m_rtJSON.value().empty()) {
154 std::ifstream
inFile{m_rtJSON};
157 return StatusCode::FAILURE;
161 ATH_CHECK(parseRtPayload(std::move(rtBlob),*writeCdo));
163 if (!m_readKeyTube.empty()) {
166 writeHandle.addDependency(readHandle);
168 itr != readHandle->end(); ++itr) {
172 return StatusCode::FAILURE;
177 if(m_dbPayloadType ==
"TTree"){
178 std::unique_ptr<TTree>
tree;
181 return StatusCode::FAILURE;
184 }
else if (m_dbPayloadType==
"JSON") {
189 return StatusCode::FAILURE;
192 ATH_CHECK(parseT0Payload(std::move(rtBlob),*writeCdo));
194 ATH_MSG_FATAL(
"Payload type " << m_dbPayloadType <<
" not understood. Options are: TTree or JSON");
195 return StatusCode::FAILURE;
199 }
else if (!m_t0RootFile.value().empty()) {
200 std::unique_ptr<TFile>
inFile{TFile::Open(m_t0RootFile.value().c_str(),
"READ")};
203 return StatusCode::FAILURE;
205 TTree*
tree{
nullptr};
206 inFile->GetObject(m_t0TreeName.value().c_str(),
tree);
208 ATH_MSG_FATAL(
"The object "<<m_rtRootFile<<
" does not contain "<<m_t0TreeName);
209 return StatusCode::FAILURE;
213 std::ifstream
inFile{m_t0JSON};
216 return StatusCode::FAILURE;
220 ATH_CHECK(parseT0Payload(std::move(t0Blob), *writeCdo));
223 ATH_CHECK(writeHandle.record(std::move(writeCdo)));
224 ATH_MSG_INFO(
"Recorded succesfully "<<m_writeKey.fullKey()<<
" "<<writeHandle.getRange());
225 return StatusCode::SUCCESS;
231 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
233 std::string* rtType{
nullptr}, *trType{
nullptr}, *resoType{
nullptr};
235 std::vector<double>* rtParams{
nullptr}, *trParams{
nullptr}, *resoParams{
nullptr};
238 std::vector<short>* stationEta{
nullptr};
239 std::vector<unsigned short>* stationPhi{
nullptr}, *
multiLayer{
nullptr};
252 for (Long64_t
e = 0;
e< rtTree.GetEntries(); ++
e) {
255 std::vector<Identifier> detIds{};
259 detIds.emplace_back(detElId);
263 return StatusCode::FAILURE;
265 ITrRelationPtr trRel = (*trType).size() ? makeTr(*trType, *trParams) :
nullptr;
266 if ((*trType).size() && !trRel) {
267 return StatusCode::FAILURE;
271 return StatusCode::FAILURE;
274 std::make_unique<MdtRtRelation>(std::move(rtRel), std::move(rtReso), std::move(trRel));
276 if (!outContainer.
storeData(detId, mdtRel, msgStream())){
277 return StatusCode::FAILURE;
281 return StatusCode::SUCCESS;
283 IRtRelationPtr MdtCalibDbAlg::makeRt(
const std::string& rtType,
const std::vector<double>& rtParams)
const {
285 if (rtType ==
"RtRelationLookUp") {
286 rtRel = std::make_unique<RtRelationLookUp>(rtParams);
287 }
else if (rtType ==
"RtLegendre"){
288 rtRel = std::make_unique<RtLegendre>(rtParams);
289 }
else if (rtType ==
"RtChebyshev") {
290 rtRel = std::make_unique<RtChebyshev>(rtParams);
293 ATH_MSG_VERBOSE(
"Fetched new rt-relation <"<<rtType<<
"> valid for drift times ranging from "
294 <<rtRel->tLower()<<
"-"<<rtRel->tUpper()<<
".");
296 ATH_MSG_FATAL(
"The rt-relation function type <"<<rtType<<
"> is not yet supported.");
301 ITrRelationPtr MdtCalibDbAlg::makeTr(
const std::string& trType,
const std::vector<double>& trParams)
const {
303 if (trType ==
"TrChebyshev") {
304 trRel = std::make_unique<TrChebyshev>(trParams);
305 }
else if(trType ==
"TrLegendre") {
306 trRel = std::make_unique<TrLegendre>(trParams);
309 ATH_MSG_VERBOSE(
"Fetched new tr-relation <"<<trType<<
"> valid for drift radii ranging from "
310 <<trRel->minRadius()<<
"-"<<trRel->maxRadius()<<
".");
312 ATH_MSG_FATAL(
"The rt-relation function type <"<<trType<<
"> is not yet supported.");
317 IRtResolutionPtr MdtCalibDbAlg::makeReso(
const std::string& resoType,
const std::vector<double>& resoParams,
320 if (resoType ==
"RtResolutionLookUp") {
321 rtReso = std::make_unique<RtResolutionLookUp>(resoParams);
322 }
else if (resoType ==
"RtResolutionChebyshev") {
323 rtReso = std::make_unique<RtResolutionChebyshev>(resoParams);
324 }
else if (resoType ==
"RadiusResolutionChebyshev") {
325 rtReso = std::make_unique<RadiusResolutionChebyshev>(resoParams, rt);
327 ATH_MSG_FATAL(
"The rt resolution type <"<<resoType<<
"> is not yet supported.");
333 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
336 for (
auto&
payload : rtBlob.items()) {
337 std::vector<Identifier> detIds{};
341 for (
auto& chambList : calibConstants[
"chambers"].
items()) {
343 const std::string station = chambPayload[
"station"];
344 const int eta = chambPayload[
"eta"];
345 const int phi = chambPayload[
"phi"];
346 const int ml = chambPayload[
"ml"];
347 const Identifier mlId{idHelper.channelID(station, eta, phi, ml, 1, 1)};
348 detIds.emplace_back(mlId);
354 IRtRelationPtr rtRel = makeRt(rtPayload[
"type"], rtPayload[
"params"]);
356 return StatusCode::FAILURE;
358 if (calibConstants.find(
"trRelation") != calibConstants.end()) {
360 trRel = makeTr(trPayload[
"type"], trPayload[
"params"]);
362 return StatusCode::FAILURE;
367 IRtResolutionPtr rtReso =makeReso(resoPayload[
"type"], resoPayload[
"params"], rtRel);
369 return StatusCode::FAILURE;
372 std::make_unique<MdtRtRelation>(std::move(rtRel), std::move(rtReso), std::move(trRel));
378 outContainer.
getCalibData(mlId, msgStream())->rtRelation) {
381 if (!outContainer.
storeData(mlId, mdtRel, msgStream())) {
382 return StatusCode::FAILURE;
386 return StatusCode::SUCCESS;
394 unsigned short stationPhi{0},
code{0};
395 float t0{0.f},
adc{0.f};
396 std::vector<unsigned short>*
multiLayer{
nullptr}, *tubeLayer{
nullptr}, *
tube{
nullptr};
409 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
411 for (Long64_t
e = 0 ;
e <t0Tree.GetEntries(); ++
e) {
418 return StatusCode::FAILURE;
420 TubeContainerPtr calibChannels = std::make_unique<MdtTubeCalibContainer>(m_idHelperSvc.get(), detId);
424 t0Calib.statusCode =
code;
430 ATH_MSG_FATAL(
"Failed to get a valid tube Identifier for "<<m_idHelperSvc->toStringChamber(detId)
432 return StatusCode::FAILURE;
434 if (!calibChannels->
setCalib(t0Calib, tubeId, msgStream())) {
435 return StatusCode::FAILURE;
438 if (!outContainer.
storeData(detId, calibChannels, msgStream())){
439 return StatusCode::FAILURE;
442 return StatusCode::SUCCESS;
446 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
450 for (
auto& dbEntry : t0Blob.items()) {
452 const std::string station =
payload[
"station"];
453 const int eta =
payload[
"eta"];
454 const int phi =
payload[
"phi"];
458 ATH_MSG_FATAL(
"Failed to create a valid identifier from "<<station<<
", "<<eta<<
", "<<phi);
459 return StatusCode::FAILURE;
461 TubeContainerPtr calibChannels = std::make_unique<MdtTubeCalibContainer>(m_idHelperSvc.get(), detId);
466 calibConstant.
t0 = calibPayload[
"t0"];
467 calibConstant.adcCal = calibPayload[
"adc"];
468 calibConstant.statusCode = calibPayload[
"code"];
469 for (
auto& tubeEntry : calibPayload[
"tubes"].
items()) {
471 const int ml = tubePayload[
"ml"];
472 const int tubeLayer = tubePayload[
"tl"];
473 const std::string tubeStr = tubePayload[
"no"];
475 std::vector<int> tubes{};
476 for (
const std::string& token: tubeTokens) {
477 if (token.find(
"-") == std::string::npos) {
481 if (rangeToken.size() != 2) {
483 return StatusCode::FAILURE;
486 if (tubeLow >= tubeHigh){
488 <<
". The lower end must be strictly smaller than the upper one");
489 return StatusCode::FAILURE;
492 tubes.push_back(
tube);
496 for (
const int tube : tubes) {
499 ATH_MSG_FATAL(
"No valid tube identifier from "<<m_idHelperSvc->toStringDetEl(detId)
500 <<
", layer: "<<tubeLayer<<
", tube: "<<
tube);
501 return StatusCode::FAILURE;
503 if (!calibChannels->
setCalib(calibConstant, tubeId, msgStream())){
504 return StatusCode::FAILURE;
509 if (!outContainer.
storeData(detId, std::move(calibChannels), msgStream())) {
510 return StatusCode::FAILURE;
514 return StatusCode::SUCCESS;