22 #include "GaudiKernel/PhysicalConstants.h"
24 #include "CoralBase/Attribute.h"
25 #include "CoralBase/AttributeListSpecification.h"
26 #include "CoralBase/Blob.h"
31 #include <unordered_map>
43 #define SET_BRANCHADDRESS(tree, branchName) \
45 if (!tree.GetBranch(#branchName)) { \
46 ATH_MSG_FATAL("The branch "<<#branchName<<" does not exist."); \
47 return StatusCode::FAILURE; \
49 if (tree.SetBranchAddress(#branchName,&branchName) != 0){ \
50 ATH_MSG_FATAL("Failed to connect branch "<<#branchName<<"."); \
51 return StatusCode::FAILURE; \
63 if (m_t0TreeName.value().empty()) {
64 m_t0RootFile.value().clear();
66 if (m_rtTreeName.value().empty()) {
67 m_rtRootFile.value().clear();
69 const bool initRt = (m_rtJSON.value().empty() && m_rtRootFile.value().empty());
70 const bool initT0 = (m_t0JSON.value().empty() && m_t0RootFile.value().empty());
71 ATH_CHECK(m_readKeyRt.initialize(initRt));
72 ATH_CHECK(m_readKeyTube.initialize(initT0));
74 if (m_rtRootFile.value().size()) {
75 ATH_MSG_INFO(
"Load RT - calibration constants from ROOT File "<<m_rtRootFile);
77 }
else if (m_rtJSON.value().size()) {
78 ATH_MSG_INFO(
"Load the RT- calibration constants from JSON file "<<m_rtJSON);
81 ATH_MSG_INFO(
"Load RT - calibration constants from COOL: "<<m_readKeyRt.fullKey());
83 if (m_t0RootFile.value().size()) {
84 ATH_MSG_INFO(
"Load T0 - calibration constants from ROOT File "<<m_t0RootFile);
86 }
else if (m_t0JSON.value().size()) {
87 ATH_MSG_INFO(
"Load the T0 - calibration constants from JSON file "<<m_t0JSON);
90 return StatusCode::SUCCESS;
96 if(writeHandle.isValid()) {
97 ATH_MSG_DEBUG(
"CondHandle " << writeHandle.fullKey() <<
" is already valid.");
98 return StatusCode::SUCCESS;
101 auto writeCdo = std::make_unique<MdtCalibDataContainer>(m_idHelperSvc.get(), RegionGranularity::OnePerMultiLayer);
106 if (!m_readKeyRt.empty()) {
109 writeHandle.addDependency(readHandle);
111 itr != readHandle->end(); ++itr) {
115 return StatusCode::FAILURE;
120 if(m_dbPayloadType ==
"TTree"){
121 std::unique_ptr<TTree>
tree;
124 return StatusCode::FAILURE;
127 }
else if (m_dbPayloadType==
"JSON") {
132 return StatusCode::FAILURE;
135 ATH_CHECK(parseRtPayload(std::move(rtBlob),*writeCdo));
137 ATH_MSG_FATAL(
"Payload type " << m_dbPayloadType <<
" not understood. Options are: TTree or JSON");
138 return StatusCode::FAILURE;
142 }
else if (!m_rtRootFile.value().empty()) {
143 std::unique_ptr<TFile>
inFile{TFile::Open(m_rtRootFile.value().c_str(),
"READ")};
146 return StatusCode::FAILURE;
148 TTree*
tree{
nullptr};
149 inFile->GetObject(m_rtTreeName.value().c_str(),
tree);
151 ATH_MSG_FATAL(
"The object "<<m_rtRootFile<<
" does not contain "<<m_rtTreeName);
152 return StatusCode::FAILURE;
155 }
else if (!m_rtJSON.value().empty()) {
156 std::ifstream
inFile{m_rtJSON};
159 return StatusCode::FAILURE;
163 ATH_CHECK(parseRtPayload(std::move(rtBlob),*writeCdo));
165 if (!m_readKeyTube.empty()) {
168 writeHandle.addDependency(readHandle);
170 itr != readHandle->end(); ++itr) {
174 return StatusCode::FAILURE;
179 if(m_dbPayloadType ==
"TTree"){
180 std::unique_ptr<TTree>
tree;
183 return StatusCode::FAILURE;
186 }
else if (m_dbPayloadType==
"JSON") {
191 return StatusCode::FAILURE;
194 ATH_CHECK(parseT0Payload(std::move(rtBlob),*writeCdo));
196 ATH_MSG_FATAL(
"Payload type " << m_dbPayloadType <<
" not understood. Options are: TTree or JSON");
197 return StatusCode::FAILURE;
201 }
else if (!m_t0RootFile.value().empty()) {
202 std::unique_ptr<TFile>
inFile{TFile::Open(m_t0RootFile.value().c_str(),
"READ")};
205 return StatusCode::FAILURE;
207 TTree*
tree{
nullptr};
208 inFile->GetObject(m_t0TreeName.value().c_str(),
tree);
210 ATH_MSG_FATAL(
"The object "<<m_rtRootFile<<
" does not contain "<<m_t0TreeName);
211 return StatusCode::FAILURE;
215 std::ifstream
inFile{m_t0JSON};
218 return StatusCode::FAILURE;
222 ATH_CHECK(parseT0Payload(std::move(t0Blob), *writeCdo));
225 ATH_CHECK(writeHandle.record(std::move(writeCdo)));
226 ATH_MSG_INFO(
"Recorded succesfully "<<m_writeKey.fullKey()<<
" "<<writeHandle.getRange());
227 return StatusCode::SUCCESS;
233 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
235 std::string* rtType{
nullptr}, *trType{
nullptr}, *resoType{
nullptr};
237 std::vector<double>* rtParams{
nullptr}, *trParams{
nullptr}, *resoParams{
nullptr};
240 std::vector<short>* stationEta{
nullptr};
241 std::vector<unsigned short>* stationPhi{
nullptr}, *
multiLayer{
nullptr};
254 for (Long64_t
e = 0;
e< rtTree.GetEntries(); ++
e) {
257 std::vector<Identifier> detIds{};
261 detIds.emplace_back(detElId);
265 return StatusCode::FAILURE;
267 ITrRelationPtr trRel = (*trType).size() ? makeTr(*trType, *trParams) :
nullptr;
268 if ((*trType).size() && !trRel) {
269 return StatusCode::FAILURE;
273 return StatusCode::FAILURE;
276 std::make_unique<MdtRtRelation>(std::move(rtRel), std::move(rtReso), std::move(trRel));
278 if (!outContainer.
storeData(detId, mdtRel, msgStream())){
279 return StatusCode::FAILURE;
283 return StatusCode::SUCCESS;
285 IRtRelationPtr MdtCalibDbAlg::makeRt(
const std::string& rtType,
const std::vector<double>& rtParams)
const {
287 if (rtType ==
"RtRelationLookUp") {
288 rtRel = std::make_unique<RtRelationLookUp>(rtParams);
289 }
else if (rtType ==
"RtLegendre"){
290 rtRel = std::make_unique<RtLegendre>(rtParams);
291 }
else if (rtType ==
"RtChebyshev") {
292 rtRel = std::make_unique<RtChebyshev>(rtParams);
295 ATH_MSG_VERBOSE(
"Fetched new rt-relation <"<<rtType<<
"> valid for drift times ranging from "
296 <<rtRel->tLower()<<
"-"<<rtRel->tUpper()<<
".");
298 ATH_MSG_FATAL(
"The rt-relation function type <"<<rtType<<
"> is not yet supported.");
303 ITrRelationPtr MdtCalibDbAlg::makeTr(
const std::string& trType,
const std::vector<double>& trParams)
const {
305 if (trType ==
"TrChebyshev") {
306 trRel = std::make_unique<TrChebyshev>(trParams);
307 }
else if(trType ==
"TrLegendre") {
308 trRel = std::make_unique<TrLegendre>(trParams);
311 ATH_MSG_VERBOSE(
"Fetched new tr-relation <"<<trType<<
"> valid for drift radii ranging from "
312 <<trRel->minRadius()<<
"-"<<trRel->maxRadius()<<
".");
314 ATH_MSG_FATAL(
"The rt-relation function type <"<<trType<<
"> is not yet supported.");
319 IRtResolutionPtr MdtCalibDbAlg::makeReso(
const std::string& resoType,
const std::vector<double>& resoParams,
322 if (resoType ==
"RtResolutionLookUp") {
323 rtReso = std::make_unique<RtResolutionLookUp>(resoParams);
324 }
else if (resoType ==
"RtResolutionChebyshev") {
325 rtReso = std::make_unique<RtResolutionChebyshev>(resoParams);
326 }
else if (resoType ==
"RadiusResolutionChebyshev") {
327 rtReso = std::make_unique<RadiusResolutionChebyshev>(resoParams, rt);
329 ATH_MSG_FATAL(
"The rt resolution type <"<<resoType<<
"> is not yet supported.");
335 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
338 for (
auto&
payload : rtBlob.items()) {
339 std::vector<Identifier> detIds{};
343 for (
auto& chambList : calibConstants[
"chambers"].
items()) {
345 const std::string station = chambPayload[
"station"];
346 const int eta = chambPayload[
"eta"];
347 const int phi = chambPayload[
"phi"];
348 const int ml = chambPayload[
"ml"];
349 const Identifier mlId{idHelper.channelID(station, eta, phi, ml, 1, 1)};
350 detIds.emplace_back(mlId);
356 IRtRelationPtr rtRel = makeRt(rtPayload[
"type"], rtPayload[
"params"]);
358 return StatusCode::FAILURE;
360 if (calibConstants.find(
"trRelation") != calibConstants.end()) {
362 trRel = makeTr(trPayload[
"type"], trPayload[
"params"]);
364 return StatusCode::FAILURE;
369 IRtResolutionPtr rtReso =makeReso(resoPayload[
"type"], resoPayload[
"params"], rtRel);
371 return StatusCode::FAILURE;
374 std::make_unique<MdtRtRelation>(std::move(rtRel), std::move(rtReso), std::move(trRel));
380 outContainer.
getCalibData(mlId, msgStream())->rtRelation) {
383 if (!outContainer.
storeData(mlId, mdtRel, msgStream())) {
384 return StatusCode::FAILURE;
388 return StatusCode::SUCCESS;
396 unsigned short stationPhi{0},
code{0};
397 float t0{0.f},
adc{0.f};
398 std::vector<unsigned short>*
multiLayer{
nullptr}, *tubeLayer{
nullptr}, *
tube{
nullptr};
412 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
415 for (Long64_t
e = 0 ;
e <t0Tree.GetEntries(); ++
e) {
422 return StatusCode::FAILURE;
424 TubeContainerPtr calibChannels = std::make_unique<MdtTubeCalibContainer>(m_idHelperSvc.get(), detId);
425 SingleTubeCalibPtr t0Calib = std::make_unique<SingleTubeCalib>();
426 t0Calib->adcCal =
adc;
428 t0Calib->statusCode =
code;
429 t0Calib = (*t0Constants.insert(t0Calib).first);
434 ATH_MSG_FATAL(
"Failed to get a valid tube Identifier for "<<m_idHelperSvc->toStringChamber(detId)
436 return StatusCode::FAILURE;
438 if (!calibChannels->
setCalib(t0Calib, tubeId, msgStream())) {
439 return StatusCode::FAILURE;
442 if (!outContainer.
storeData(detId, calibChannels, msgStream())){
443 return StatusCode::FAILURE;
446 return StatusCode::SUCCESS;
450 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
456 for (
auto& dbEntry : t0Blob.items()) {
458 const std::string station =
payload[
"station"];
459 const int eta =
payload[
"eta"];
460 const int phi =
payload[
"phi"];
464 ATH_MSG_FATAL(
"Failed to create a valid identifier from "<<station<<
", "<<eta<<
", "<<phi);
465 return StatusCode::FAILURE;
467 TubeContainerPtr calibChannels = std::make_unique<MdtTubeCalibContainer>(m_idHelperSvc.get(), detId);
471 SingleTubeCalibPtr calibConstant{std::make_unique<SingleTubeCalib>()};
472 calibConstant->t0 = calibPayload[
"t0"];
473 calibConstant->adcCal = calibPayload[
"adc"];
474 calibConstant->statusCode = calibPayload[
"code"];
475 calibConstant = *t0Constants.insert(calibConstant).first;
476 for (
auto& tubeEntry : calibPayload[
"tubes"].
items()) {
478 const int ml = tubePayload[
"ml"];
479 const int tubeLayer = tubePayload[
"tl"];
480 const std::string tubeStr = tubePayload[
"no"];
482 std::vector<int> tubes{};
483 for (
const std::string& token: tubeTokens) {
484 if (token.find(
"-") == std::string::npos) {
488 if (rangeToken.size() != 2) {
490 return StatusCode::FAILURE;
493 if (tubeLow >= tubeHigh){
495 <<
". The lower end must be strictly smaller than the upper one");
496 return StatusCode::FAILURE;
499 tubes.push_back(
tube);
503 for (
const int tube : tubes) {
506 ATH_MSG_FATAL(
"No valid tube identifier from "<<m_idHelperSvc->toStringDetEl(detId)
507 <<
", layer: "<<tubeLayer<<
", tube: "<<
tube);
508 return StatusCode::FAILURE;
510 if (!calibChannels->
setCalib(calibConstant, tubeId, msgStream())){
511 return StatusCode::FAILURE;
516 if (!outContainer.
storeData(detId, std::move(calibChannels), msgStream())) {
517 return StatusCode::FAILURE;
521 return StatusCode::SUCCESS;