ATLAS Offline Software
Loading...
Searching...
No Matches
CalibrationDbIOTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5// this
7
11// gaudi
12#include "GaudiKernel/MsgStream.h"
13
14// MdtCalibUtils
17
18// MdtCalibData
27
28// MdtCalibRt
30
31// MuonCalibStandAloneBase
34
35// MuonCalibMath
37
38// c - c++
39#include <dirent.h>
40#include <sys/types.h>
41
43#include "fstream"
44#include "iostream"
45#include "string"
46
47namespace MuonCalib {
49 CalibrationDbIOTool::CalibrationDbIOTool(const std::string &t, const std::string &n, const IInterface *p) :
51 declareInterface<ICalibrationIOTool>(this);
52 declareProperty("ConnectionString", m_db_ConnectionString);
53 declareProperty("WriterConnectionString", m_writer_connection_string);
54 declareProperty("WorkingSchema", m_db_WorkingSchema);
55 m_headid = -1;
56 declareProperty("HeadId", m_headid);
57 declareProperty("SiteName", m_sitename);
58 m_max_rt_points = 100;
59 declareProperty("MaxRtPoints", m_max_rt_points);
60 m_use_validated_t0 = true;
61 declareProperty("UseValidaedT0", m_use_validated_t0);
62 m_use_validated_t0 = false;
63 declareProperty("UseValidaedRt", m_use_validated_rt);
64 declareProperty("ReaderAccount", m_reader_account);
65 declareProperty("ReaderPassword", m_reader_password);
66 declareProperty("WriterAccount", m_writer_account);
67 declareProperty("WriterPassword", m_writer_password);
68 }
69
71 ServiceHandle<RegionSelectionSvc> reg_sel_svc{"RegionSelectionSvc", name()};
72 ATH_CHECK(reg_sel_svc.retrieve());
73 m_region_ids = reg_sel_svc->GetStationsInRegions();
74 ATH_MSG_INFO(" CalibrationDbIOTool::initialize() - number of selected regions: " << m_region_ids.size());
75
76 ATH_MSG_INFO("open connection");
77 m_connection = std::make_unique<CalibDbConnection>(m_db_ConnectionString, m_db_WorkingSchema);
78 ATH_MSG_INFO(".");
80 if (!m_connection->OpenConnection()) {
81 ATH_MSG_FATAL("Cannot open connection to database!");
82 return StatusCode::FAILURE;
83 }
84 ATH_MSG_INFO("..");
85 m_head_ops = std::make_unique<CalibHeadOperations>(*m_connection);
86 if (m_headid < 0) {
87 m_headid = m_head_ops->GetLatestHeadId();
88 if (m_headid < 0) {
89 ATH_MSG_FATAL("Cannot get latest head_id for site " << m_sitename);
90 return StatusCode::FAILURE;
91 }
92 }
93 return StatusCode::SUCCESS;
94 }
95
96 StatusCode CalibrationDbIOTool::WriteT0(const MdtTubeFitContainer *t0, const NtupleStationId &it, int /*iov_start*/, int /*iov_end*/) {
97 ATH_MSG_INFO("Writing to database");
98 if (!m_connection->OpenConnection()) {
99 ATH_MSG_FATAL("Open Connection failed!");
100 return StatusCode::FAILURE;
101 } else {
102 if (!get_connection(1)) { return StatusCode::FAILURE; }
103 m_data_connections[1]->OpenConnection();
105 std::vector<int> val(0);
106 if (!t0_op.WriteT0Chamber(it, t0, val, m_headid, m_sitename)) {
107 ATH_MSG_FATAL("Writing t0 failed!");
108 return StatusCode::FAILURE;
109 }
110 }
111 return StatusCode::SUCCESS;
112 }
113
114 StatusCode CalibrationDbIOTool::LoadT0(std::map<NtupleStationId, MdtStationT0Container *> &t0s, int /*iov_id*/) {
115 if (!m_connection->OpenConnection()) {
116 ATH_MSG_FATAL("Open Connection failed!");
117 return StatusCode::FAILURE;
118 } else {
119 if (!get_connection(0)) { return StatusCode::FAILURE; }
120 m_data_connections[0]->OpenConnection();
122 t0s.clear();
123
124 // loop on all the ids in the selected calibration region
125 for (std::vector<MuonCalib::NtupleStationId>::iterator it = m_region_ids.begin(); it != m_region_ids.end(); ++it) {
126 ATH_MSG_INFO("Reading t0s for region " << it->regionId());
129 if (t0 != nullptr) t0s[*it] = t0;
130 }
131 }
132 return StatusCode::SUCCESS;
133 }
134
135 StatusCode CalibrationDbIOTool::WriteRt(const RtCalibrationOutput *rt_relation, std::shared_ptr<const IRtResolution> resolution,
136 const NtupleStationId &station_id, int /*iov_start*/, int /*iov_end*/, bool /*real_rt*/,
137 bool /*real resolution*/) {
138 std::vector<SamplePoint> points;
139 // fill rt poits
140 fillRtPoints(rt_relation->rt(), points);
141 // fill resolution points
142 fillResPoints(resolution, points);
143 // fill multilayer rt difference as pseudo-point
144 if (rt_relation->rt()->hasTmaxDiff()) {
145 SamplePoint point(rt_relation->rt()->GetTmaxDiff(), -99.9, 0.0);
146 points.push_back(point);
147 }
148 if (!m_connection->OpenConnection()) {
149 ATH_MSG_FATAL("Open Connection failed!");
150 return StatusCode::FAILURE;
151 } else {
152 if (!get_connection(1)) { return StatusCode::FAILURE; }
153 m_data_connections[1]->OpenConnection();
155 if (!rt_op.WriteUpdateRt(station_id, m_headid, m_sitename, points, 0, rt_relation->fullInfo().get())) {
156 ATH_MSG_FATAL("Writing rt failed!");
157 return StatusCode::FAILURE;
158 }
159 }
160 return StatusCode::SUCCESS;
161 }
162
163 StatusCode CalibrationDbIOTool::LoadRt(std::map<NtupleStationId, IRtRelation *> &rts, std::map<NtupleStationId, IRtResolution *> &res,
164 int /*iov_id*/) {
165 if (!m_connection->OpenConnection()) {
166 ATH_MSG_FATAL("Open Connection failed!");
167 return StatusCode::FAILURE;
168 } else {
169 if (!get_connection(0)) { return StatusCode::FAILURE; }
170 m_data_connections[0]->OpenConnection();
172 // loop on all the ids in the selected calibration region
173 RtFromPoints rt_from_points;
174 RtResolutionFromPoints res_from_points;
175 for (std::vector<MuonCalib::NtupleStationId>::iterator it = m_region_ids.begin(); it != m_region_ids.end(); ++it) {
176 std::vector<SamplePoint> in_points;
177 RtFullInfo full_info;
178 if (!rt_op.LoadRt(*it, m_headid, m_use_validated_rt, m_sitename, in_points, &full_info)) continue;
179 ATH_MSG_INFO("Reading rts for region " << it->regionId());
180 std::vector<SamplePoint> outpoints;
181 float tmax_diff(-9e9);
182 for (auto & in_point : in_points) {
183 if (in_point.x2() < -99) {
184 tmax_diff = in_point.x1();
185 continue;
186 }
187 SamplePoint outpoint(in_point.x1(), in_point.x2(), 1.0);
188 outpoints.push_back(outpoint);
189 }
190 rts[*it] = rt_from_points.getRtRelationLookUp(outpoints).release();
191 if (tmax_diff > -8e8) { rts[*it]->SetTmaxDiff(tmax_diff); }
192 outpoints.clear();
193 for (auto & in_point : in_points) {
194 if (in_point.x2() < -99) { continue; }
195 SamplePoint outpoint(in_point.x1(), in_point.error(), 1.0);
196 outpoints.push_back(outpoint);
197 }
198 res[*it] = new RtResolutionLookUp(res_from_points.getRtResolutionLookUp(outpoints));
199 }
200 }
201 return StatusCode::SUCCESS;
202 }
203
204 void CalibrationDbIOTool::fillRtPoints(const std::shared_ptr<const IRtRelation>& rt, std::vector<SamplePoint> &points) {
205 points.clear();
206 // for rt relations based on support points, write points
207 std::shared_ptr<const RtRelationLookUp> rt_lookup = std::dynamic_pointer_cast<const RtRelationLookUp>(rt);
208 const CalibFunc::ParVec &rt_param = rt->parameters();
209 if (rt_lookup && static_cast<int>(rt_lookup->nPar() - 2) <= m_max_rt_points) {
210 double t_min(rt_param[0]);
211 double bin_size = rt_param[1];
212 unsigned int nb_points(rt_lookup->nPar() - 2);
213 for (unsigned int k = 0; k < nb_points; k++) {
214 double radius(rt_param[k + 2]);
215 if (std::isnan(radius)) {
216 ATH_MSG_WARNING("Skipping NAN");
217 continue;
218 }
219 SamplePoint point(t_min + bin_size * k, rt_param[k + 2], -1);
220 points.push_back(point);
221 }
222 return;
223 }
224 std::shared_ptr<const RtSpline> rt_spline = std::dynamic_pointer_cast<const RtSpline>(rt);
225 if (rt_spline && static_cast<int>(rt_param.size() / 2) <= m_max_rt_points) {
226 for (unsigned int i = 0; i < rt_param.size(); i += 2) {
227 SamplePoint point(rt_param[i], rt_param[i + 1], -1);
228 points.push_back(point);
229 }
230 return;
231 }
232 // for all others, or if the number of support points is too high, calculate new support points
233 for (int i = 0; i < m_max_rt_points; i++) {
234 double t = rt->tLower() + i * ((rt->tUpper() - rt->tLower()) / (m_max_rt_points - 1));
235 SamplePoint point(t, rt->radius(t), -1.0);
236 }
237 }
238
239 void CalibrationDbIOTool::fillResPoints(const std::shared_ptr<const IRtResolution>& rt_resolution, std::vector<SamplePoint> &points) {
240 for (auto & point : points) { point.set_error(rt_resolution->resolution(point.x1())); }
241 }
242
244 if (m_data_connections[write]) { return m_data_connections[write].get(); }
245 m_data_connections[write] = std::unique_ptr<CalibDbConnection>{m_head_ops->GetDataConnection(
247 return m_data_connections[write].get();
248 }
249
250} // namespace MuonCalib
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
std::pair< std::vector< unsigned int >, bool > res
static Double_t t0
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
std::vector< double > ParVec
Definition CalibFunc.h:35
bool WriteUpdateRt(const NtupleStationId &id, int head_id, const std::string &, const std::vector< SamplePoint > &points, int validflag, const RtFullInfo *full_info)
bool LoadRt(const NtupleStationId &id, int head_id, bool validated, const std::string &, std::vector< SamplePoint > &points, RtFullInfo *full_info)
MdtStationT0Container * LoadT0Calibration(const NtupleStationId &id, int head_id, std::string &site_name)
bool WriteT0Chamber(const NtupleStationId &id, const MdtTubeFitContainer *t0, std::vector< int > &validation_flag, int head_id, const std::string &site_name)
StatusCode WriteRt(const RtCalibrationOutput *rt_relation, std::shared_ptr< const IRtResolution > resolution, const NtupleStationId &station_id, int iov_start, int iov_end, bool, bool) override
write rt
std::array< std::unique_ptr< CalibDbConnection >, 2 > m_data_connections
bool m_use_validated_t0
if true use validated - jo
void fillResPoints(const std::shared_ptr< const IRtResolution > &rt_resolution, std::vector< SamplePoint > &points)
StatusCode initialize() override
initialisation
StatusCode WriteT0(const MdtTubeFitContainer *t0_output, const NtupleStationId &station_id, int iov_start, int iov_end) override
write out t0
CalibrationDbIOTool(const std::string &t, const std::string &n, const IInterface *p)
constructor
std::unique_ptr< CalibHeadOperations > m_head_ops
std::vector< MuonCalib::NtupleStationId > m_region_ids
std::string m_reader_account
account data
StatusCode LoadRt(std::map< NtupleStationId, IRtRelation * > &rts, std::map< NtupleStationId, IRtResolution * > &res, int) override
load rts
void fillRtPoints(const std::shared_ptr< const IRtRelation > &rt_relation, std::vector< SamplePoint > &points)
convert rt and resolution to points
StatusCode LoadT0(std::map< NtupleStationId, MdtStationT0Container * > &t0s, int) override
load t0s
CalibDbConnection * get_connection(int write)
std::unique_ptr< CalibDbConnection > m_connection
pointer to db connection
Holds single-tube full calibration information of one chamber.
Station Identifier for sorting calibration data.
Class for communication between event loop and rt calibration algorithm contains only a rt relation f...
std::shared_ptr< const RtFullInfo > fullInfo() const
std::shared_ptr< const IRtRelation > rt() const
access to private attributes
This class allows the user to retrieve an RtChebyshev or RtRelationLookUp object corresponding to a s...
static std::unique_ptr< IRtRelation > getRtRelationLookUp(const std::vector< SamplePoint > &sample_points)
addiitonal information used in validation of a rt-relation
Definition RtFullInfo.h:15
static RtResolutionLookUp getRtResolutionLookUp(const std::vector< SamplePoint > &sample_points)
Equidistant look up table for resolution tables with the time as key.
This class provides a sample point for the BaseFunctionFitter.
Definition SamplePoint.h:15
CscCalcPed - algorithm that finds the Cathode Strip Chamber pedestals from an RDO.