8 #include "CoralBase/Attribute.h"
9 #include "CoralBase/AttributeList.h"
10 #include "CoralBase/AttributeSpecification.h"
11 #include "CoralKernel/Context.h"
12 #include "GaudiKernel/MsgStream.h"
19 #include "RelationalAccess/IAuthenticationCredentials.h"
20 #include "RelationalAccess/IAuthenticationService.h"
21 #include "RelationalAccess/IBulkOperation.h"
22 #include "RelationalAccess/ICursor.h"
23 #include "RelationalAccess/IQuery.h"
24 #include "RelationalAccess/IRelationalDomain.h"
25 #include "RelationalAccess/IRelationalService.h"
26 #include "RelationalAccess/ISchema.h"
27 #include "RelationalAccess/ITable.h"
28 #include "RelationalAccess/ITableDataEditor.h"
29 #include "RelationalAccess/ITransaction.h"
30 #include "RelationalAccess/SchemaException.h"
38 m_db_conn(db_conn), m_rt_id(-1), m_rt_map_t_id(-1), m_head_id(-1), m_region_id(-1), m_n_points(0) {
50 if (!
getRtId(validated, full_info)) {
52 log << MSG::WARNING <<
"Header query failed!" <<
endmsg;
65 }
catch (coral::SchemaException&
e) {
67 log << MSG::WARNING <<
"Schema exception : " <<
e.what() <<
endmsg;
73 const std::vector<SamplePoint>&
points,
int validflag,
const RtFullInfo* full_info) {
79 if (!
getRtId((validflag > 1),
nullptr)) {
82 log << MSG::WARNING <<
"Questing RT header failed!" <<
endmsg;
88 log << MSG::WARNING <<
"Failed update header" <<
endmsg;
100 log << MSG::WARNING <<
"Failed update header" <<
endmsg;
111 }
catch (coral::SchemaException&
e) {
113 log << MSG::WARNING <<
"Schema exception : " <<
e.what() <<
endmsg;
122 log << MSG::WARNING <<
"No current rt!" <<
endmsg;
128 std::string updateAction =
"VALIDFLAG=:val";
129 std::string updateCondition =
"HEAD_ID=:hid and REGION_ID=:rid and CALIBFLAG=:rtid";
131 updateData.extend<
int>(
"val");
132 updateData[0].data<
int>() = validflag;
133 updateData.extend<
int>(
"rtid");
134 updateData[
"rtid"].data<
int>() =
m_rt_id;
135 updateData.extend<
int>(
"hid");
136 updateData[
"hid"].data<
int>() =
m_head_id;
137 updateData.extend<
int>(
"rid");
140 int n_rows = editor.updateRows(updateAction, updateCondition, updateData);
143 log << MSG::WARNING <<
"Invalid number of rows updated in MDT_RT! " << n_rows <<
" instead of 1" <<
endmsg;
149 }
catch (coral::SchemaException&
e) {
151 log << MSG::WARNING <<
"Schema exception : " <<
e.what() <<
endmsg;
161 m_query->addToTableList(
"MDT_RT");
162 m_query->addToTableList(
"MDT_RT_MAP");
164 conditionData.extend<
int>(
"hid");
165 conditionData[0].data<
int>() =
head_id;
167 "MDT_RT.HEAD_ID=:hid and MDT_RT_MAP.HEAD_ID=:hid and MDT_RT.REGION_ID = MDT_RT_MAP.REGION_ID and MDT_RT.CALIBFLAG = "
168 "MDT_RT_MAP.CALIBFLAG and MDT_RT.VALIDFLAG>1";
170 m_query->addToOrderList(
"MDT_RT.REGION_ID");
171 m_query->addToOutputList(
"MDT_RT.REGION_ID",
"REGION_ID");
172 m_query->addToOutputList(
"MDT_RT_MAP.POINT_NR",
"POINT_NR");
173 m_query->addToOutputList(
"MDT_RT_MAP.T",
"T");
174 m_query->addToOutputList(
"MDT_RT_MAP.R",
"R");
175 m_query->addToOutputList(
"MDT_RT_MAP.S",
"S");
176 m_query->setRowCacheSize(100000);
178 int last_region_id(-1);
179 std::map<int, SamplePoint>
points;
183 int rt_id =
static_cast<int>(al[
"REGION_ID"].data<
float>());
184 if (last_region_id > 0 && rt_id != last_region_id) {
187 log << MSG::WARNING <<
"Storage_failed" <<
endmsg;
192 last_region_id = rt_id;
193 SamplePoint pt(al[
"T"].data<float>(), al[
"R"].data<float>(), al[
"S"].data<float>());
194 points[
static_cast<int>(al[
"POINT_NR"].data<
short>())] =
pt;
196 if (last_region_id > 0) {
199 log << MSG::WARNING <<
"Storage_failed" <<
endmsg;
204 }
catch (coral::SchemaException&
e) {
206 log << MSG::WARNING <<
"Schema exception : " <<
e.what() <<
endmsg;
215 m_query->addToTableList(
"MDT_RT");
217 conditionData.extend<
int>(
"hid");
218 conditionData[0].data<
int>() =
m_head_id;
219 conditionData.extend<
int>(
"rid");
222 std::string
condition =
"HEAD_ID=:hid and REGION_ID=:rid";
229 m_query->addToOutputList(
"CALIBFLAG");
230 m_query->addToOutputList(
"BINS");
231 m_query->addToOutputList(
"ALGO_FLAG");
232 if (full_info !=
nullptr) {
233 m_query->addToOutputList(
"N_SEGS");
234 m_query->addToOutputList(
"AVER_ANGLE");
235 m_query->addToOutputList(
"DELTA_AVER_ANGLE");
236 m_query->addToOutputList(
"AVER_SPREAD");
237 m_query->addToOutputList(
"DELTA_AVER_SPREAD");
238 m_query->addToOutputList(
"CONVERGENCE");
239 m_query->addToOutputList(
"B_AVER");
240 m_query->addToOutputList(
"T_AVER");
248 m_rt_id = al[
"CALIBFLAG"].data<
short>();
249 m_n_points =
static_cast<int>(al[
"BINS"].data<
short>());
251 if (full_info !=
nullptr) {
252 full_info->
setNumSeg(
static_cast<int>(al[
"N_SEGS"].data<int>()));
253 full_info->
setMeanAng(al[
"AVER_ANGLE"].data<float>());
254 full_info->
setRmsAng(al[
"DELTA_AVER_ANGLE"].data<float>());
255 full_info->
setMeanPos(al[
"AVER_SPREAD"].data<float>());
256 full_info->
setRmsPos(al[
"DELTA_AVER_SPREAD"].data<float>());
257 full_info->
setConvEst(al[
"CONVERGENCE"].data<float>());
263 log << MSG::WARNING <<
"Found " << n_rows <<
" in MDT_RT instead of 0 or 1" <<
endmsg;
272 m_query->addToTableList(
"MDT_RT_MAP");
274 conditionData.extend<
int>(
"rtid");
275 conditionData[0].data<
int>() =
m_rt_id;
276 conditionData.extend<
int>(
"hid");
277 conditionData[1].data<
int>() =
m_head_id;
278 conditionData.extend<
int>(
"rid");
280 std::string
condition =
"HEAD_ID=:hid and REGION_ID=:rid and CALIBFLAG=:rtid";
282 m_query->addToOutputList(
"MDT_RT_MAP.POINT_NR",
"POINT_NR");
283 m_query->addToOutputList(
"MDT_RT_MAP.R",
"R");
284 m_query->addToOutputList(
"MDT_RT_MAP.T",
"T");
285 m_query->addToOutputList(
"MDT_RT_MAP.S",
"S");
286 m_query->addToOrderList(
"MDT_RT_MAP.POINT_NR");
294 int pt_nr(
static_cast<int>(al[
"POINT_NR"].data<short>()));
297 log << MSG::WARNING <<
"Illegal point number " << pt_nr <<
endmsg;
300 SamplePoint point(al[
"T"].data<float>(), al[
"R"].data<float>(), al[
"S"].data<float>());
315 std::string updateAction =
316 "N_SEGS = :N_SEGS, AVER_ANGLE = :AVER_ANGLE, DELTA_AVER_ANGLE = :DELTA_AVER_ANGLE, AVER_SPREAD = :AVER_SPREAD, "
317 "DELTA_AVER_SPREAD = :DELTA_AVER_SPREAD, CONVERGENCE = :CONVERGENCE, VALIDFLAG=:VALIDFLAG, BINS=:BINS, ALGO_FLAG=:ALGO_FLAG";
318 std::string updateCondition =
"HEAD_ID=:hid and REGION_ID=:rid and CALIBFLAG=:rtid";
321 updateData.extend<
int>(
"rtid");
322 updateData[
"rtid"].data<
int>() =
m_rt_id;
323 updateData.extend<
int>(
"hid");
324 updateData[
"hid"].data<
int>() =
m_head_id;
325 updateData.extend<
int>(
"rid");
327 if (editor.updateRows(updateAction, updateCondition, updateData) != 1) {
329 log << MSG::WARNING <<
"Invalid number of rows updated in MDT_RT!" <<
endmsg;
339 std::string deleteCondition =
"HEAD_ID = :hid and REGION_ID=:rid and CALIBFLAG=:rtid and POINT_NR>=:max_point";
341 deleteData.extend<
int>(
"hid");
343 deleteData.extend<
int>(
"rid");
345 deleteData.extend<
int>(
"rtid");
346 deleteData[2].data<
int>() =
m_rt_id;
347 deleteData.extend<
int>(
"max_point");
348 deleteData[3].data<
int>() =
static_cast<int>(
points.size());
349 long rowsDeleted = editor.deleteRows(deleteCondition, deleteData);
352 log << MSG::WARNING << rowsDeleted <<
" Points removed instead of " <<
m_n_points -
static_cast<int>(
points.size())
353 <<
"! Rolling back!" <<
endmsg;
359 table_data[
"CALIBFLAG"].data<
int>() =
m_rt_id;
360 table_data[
"HEAD_ID"].data<
int>() =
m_head_id;
362 for (
int i = 0; i < static_cast<int>(
points.size());
i++) {
363 table_data[
"POINT_NR"].data<
int>() =
i;
364 table_data[
"T"].data<
float>() =
points[
i].
x1();
365 table_data[
"R"].data<
float>() =
points[
i].
x2();
368 std::string updateAction =
"R=:R, T=:T, S=:S";
369 std::string updateCondition =
"CALIBFLAG=:CALIBFLAG and POINT_NR=:POINT_NR and HEAD_ID=:HEAD_ID and REGION_ID=:REGION_ID";
370 if (editor.updateRows(updateAction, updateCondition, table_data) != 1) {
372 log << MSG::WARNING <<
"Wrong number of points updated!" <<
endmsg;
376 editor.insertRow(table_data);
386 editor.insertRow(rowBuffer);
387 return getRtId((validflag > 1),
nullptr);
394 table_data[
"CALIBFLAG"].data<
int>() =
m_rt_id;
395 table_data[
"HEAD_ID"].data<
int>() =
m_head_id;
397 coral::IBulkOperation* bulk_inserter = editor.bulkInsert(table_data, 100);
399 for (
int i = 0; i < static_cast<int>(
points.size());
i++) {
400 table_data[
"POINT_NR"].data<
int>() =
i;
401 table_data[
"T"].data<
float>() =
points[
i].
x1();
402 table_data[
"R"].data<
float>() =
points[
i].
x2();
404 bulk_inserter->processNextIteration();
406 bulk_inserter->flush();
407 delete bulk_inserter;
412 const int& n_bins,
const bool& for_insert) {
414 updateData.extend<
int>(
"REGION_ID");
416 updateData.extend<
int>(
"HEAD_ID");
417 updateData[
"HEAD_ID"].data<
int>() =
m_head_id;
418 updateData.extend<
int>(
"LOWRUN");
419 updateData[
"LOWRUN"].data<
int>() = 0;
420 updateData.extend<
int>(
"UPRUN");
421 updateData[
"UPRUN"].data<
int>() = 1;
422 updateData.extend<std::string>(
"HISTOGRAM");
423 updateData[
"HISTOGRAM"].data<std::string>() =
"DUMMY";
428 }
else if (full_info->
implementation() ==
"RtCalibrationCruved") {
430 }
else if (full_info->
implementation() ==
"RtCalibrationAnalytic") {
432 }
else if (full_info->
implementation() ==
"RtCalibrationAnalyticExt") {
446 updateData.extend<std::string>(
"ALGO_FLAG");
447 updateData[
"ALGO_FLAG"].data<std::string>() =
m_prev_algs;
448 updateData.extend<
int>(
"N_SEGS");
449 updateData[
"N_SEGS"].data<
int>() = full_info->
numSeg();
450 updateData.extend<
float>(
"AVER_ANGLE");
451 updateData[
"AVER_ANGLE"].data<
float>() = full_info->
meanAng();
452 if (std::abs(full_info->
meanAng()) > 9.9999) {
453 updateData[
"AVER_ANGLE"].data<
float>() = ((full_info->
meanAng() < 0) ? -1 : 1) * 9.9999;
455 updateData.extend<
float>(
"DELTA_AVER_ANGLE");
456 updateData[
"DELTA_AVER_ANGLE"].data<
float>() = full_info->
rmsAng();
457 if (std::abs(full_info->
rmsAng()) > 9.9999) {
458 updateData[
"DELTA_AVER_ANGLE"].data<
float>() = ((full_info->
rmsAng() < 0) ? -1 : 1) * 9.9999;
460 updateData.extend<
float>(
"AVER_SPREAD");
461 updateData[
"AVER_SPREAD"].data<
float>() = full_info->
meanPos();
462 if (std::abs(full_info->
meanPos()) > 9.9999) {
463 updateData[
"AVER_SPREAD"].data<
float>() = ((full_info->
meanPos() < 0) ? -1 : 1) * 9.9999;
465 updateData.extend<
float>(
"DELTA_AVER_SPREAD");
466 updateData[
"DELTA_AVER_SPREAD"].data<
float>() = full_info->
rmsPos();
467 if (std::abs(full_info->
rmsPos()) > 9.9999) {
468 updateData[
"DELTA_AVER_SPREAD"].data<
float>() = ((full_info->
rmsPos() < 0) ? -1 : 1) * 9.9999;
470 updateData.extend<
float>(
"CONVERGENCE");
471 updateData[
"CONVERGENCE"].data<
float>() = full_info->
convEst();
472 if (std::abs(full_info->
convEst()) > 999.999) {
473 updateData[
"CONVERGENCE"].data<
float>() = ((full_info->
convEst() < 0) ? -1 : 1) * 999.999;
476 updateData.extend<
float>(
"B_AVER");
477 updateData[
"B_AVER"].data<
float>() = 0.;
478 updateData.extend<
float>(
"T_AVER");
479 updateData[
"T_AVER"].data<
float>() = 0.;
480 updateData.extend<
float>(
"HV");
481 updateData[
"HV"].data<
float>() = 0.;
482 updateData.extend<
float>(
"CURR");
483 updateData[
"CURR"].data<
float>() = 0.;
484 updateData.extend<
float>(
"GAS");
485 updateData[
"GAS"].data<
float>() = 0.;
486 updateData.extend<
int>(
"CALIBFLAG");
487 updateData[
"CALIBFLAG"].data<
int>() =
static_cast<int>(validflag > 3);
489 updateData.extend<
int>(
"VALIDFLAG");
490 updateData[
"VALIDFLAG"].data<
int>() = validflag;
491 updateData.extend<
short>(
"BINS");
492 updateData[
"BINS"].data<
short>() = n_bins;
496 rowBuffer.extend<
int>(
"HEAD_ID");
497 rowBuffer.extend<
int>(
"REGION_ID");
498 rowBuffer.extend<
int>(
"CALIBFLAG");
499 rowBuffer.extend<
int>(
"POINT_NR");
500 rowBuffer.extend<
float>(
"T");
501 rowBuffer.extend<
float>(
"R");
502 rowBuffer.extend<
float>(
"S");
506 const int& validflag) {
510 m_query->addToTableList(
"MDT_RT");
512 conditionData.extend<
int>(
"hid");
513 conditionData[0].data<
int>() =
head_id;
515 std::ostringstream ostr;
516 ostr <<
id.FixedId();
517 conditionData.extend<std::string>(
"chamb");
518 conditionData[1].data<std::string>() = ostr.str();
519 std::string
condition =
"MDT_RT.HEAD_ID = :hid and MDT_RT.REGION_ID = :chamb";
521 m_query->addToOutputList(
"count(MDT_RT.CALIBFLAG)",
"N_RTS");
522 m_query->addToOutputList(
"MDT_RT.VALIDFLAG",
"VALIDFLAG");
523 m_query->groupBy(
"MDT_RT.VALIDFLAG");
525 bool has_unvalidated(
false), has_validated(
false);
528 if (al[
"VALIDFLAG"].data<short>() > 0) {
529 if (al[
"N_RTS"].data<double>() > 0) { has_validated =
true; }
531 if (al[
"N_RTS"].data<double>() > 0) { has_unvalidated =
true; }
534 if (validflag == 0 && has_unvalidated)
return false;
535 if (validflag > 0 && has_validated)
return false;