7 #include "CoralBase/Attribute.h"
8 #include "CoralBase/AttributeList.h"
9 #include "CoralBase/AttributeSpecification.h"
10 #include "CoralKernel/Context.h"
17 #include "RelationalAccess/IAuthenticationCredentials.h"
18 #include "RelationalAccess/IAuthenticationService.h"
19 #include "RelationalAccess/IBulkOperation.h"
20 #include "RelationalAccess/ICursor.h"
21 #include "RelationalAccess/IQuery.h"
22 #include "RelationalAccess/IRelationalDomain.h"
23 #include "RelationalAccess/IRelationalService.h"
24 #include "RelationalAccess/ISchema.h"
25 #include "RelationalAccess/ITable.h"
26 #include "RelationalAccess/ITableDataEditor.h"
27 #include "RelationalAccess/ITransaction.h"
28 #include "RelationalAccess/SchemaException.h"
35 AthMessaging{
"CalibT0DbOperations"}, m_db_conn(db_conn) {
36 m_idHelperSvc.retrieve().ignore();
46 m_query->addToTableList(
"MDT_TUBE");
48 conditionData.extend<
int>(
"hid");
49 conditionData[0].data<
int>() =
head_id;
51 conditionData.extend<
int>(
"chamb");
52 conditionData[1].data<
int>() =
id.FixedId();
53 std::string
condition =
"MDT_TUBE.HEAD_ID = :hid and MDT_TUBE.CHAMBER = :chamb and MDT_TUBE.VALIDFLAG>1";
55 m_query->addToOutputList(
"MDT_TUBE.TUBE_ID",
"TUBE_ID");
56 m_query->addToOutputList(
"MDT_TUBE.P4",
"T0");
57 m_query->addToOutputList(
"MDT_TUBE.ADC_1",
"ADC_1");
74 catch (coral::SchemaException &
e) {
86 m_query->addToTableList(
"MDT_TUBE");
87 m_query->addToTableList(
"MDT_TUBE_V");
89 conditionData.extend<
int>(
"hid");
90 conditionData[0].data<
int>() =
head_id;
92 std::ostringstream ostr;
94 conditionData.extend<std::string>(
"chamb");
95 conditionData[1].data<std::string>() = ostr.str();
97 "MDT_TUBE.HEAD_ID = :hid and MDT_TUBE.CHAMBER = :chamb and MDT_TUBE.VALIDFLAG=0 and MDT_TUBE_V.HEAD_ID=:hid and "
98 "MDT_TUBE_V.TUBE_ID = MDT_TUBE.TUBE_ID and MDT_TUBE_V.CALIBFLAG=MDT_TUBE.CALIBFLAG";
100 m_query->addToOutputList(
"MDT_TUBE.TUBE_ID",
"TUBE_ID");
101 m_query->addToOutputList(
"MDT_TUBE.P4",
"T0");
102 m_query->addToOutputList(
"MDT_TUBE_V.ENTRIES",
"ENTRIES");
103 m_query->addToOutputList(
"MDT_TUBE_V.CHISQUARE_1",
"CHISQUARE_1");
104 m_query->addToOutputList(
"MDT_TUBE_V.CHISQUARE_2",
"CHISQUARE_2");
105 for (
int i = 0;
i < 7;
i++) {
106 std::ostringstream ostr;
108 ostr <<
"MDT_TUBE.P4";
110 ostr <<
"MDT_TUBE_V.P" <<
i;
112 std::ostringstream ostr2;
114 m_query->addToOutputList(ostr.str(), ostr2.str());
115 if (
i > 3 &&
i < 7) {
117 m_query->addToOutputList(
"MDT_TUBE_V.P4_ERR",
"P4_ERR");
121 m_query->addToOutputList(ostr.str(), ostr2.str());
125 m_query->addToOutputList(
"MDT_TUBE.NHITS",
"NHITS");
126 m_query->addToOutputList(
"MDT_TUBE.NHITS_ABOVE_ADC_CUT",
"NHITS_ABOVE_ADC_CUT");
127 m_query->addToOutputList(
"MDT_TUBE_V.ALGO_FLAG",
"ALGO_FLAG");
128 m_query->addToOutputList(
"MDT_TUBE_V.TUBE_GROUPING",
"TUBE_GROUPING");
130 int n_tubes(0), n_layers(0), n_ml(0);
131 std::map<MuonFixedId, MdtTubeFitContainer::SingleTubeFit> fits;
132 std::map<MuonFixedId, MdtTubeFitContainer::SingleTubeCalib> calibs;
133 std::string alg_flg, tb_grp;
139 calib.t0 = al[
"P4"].data<
float>();
142 fit.statistics = al[
"ENTRIES"].data<
int>();
143 fit.n_hits = al[
"NHITS"].data<
int>();
144 fit.n_hits_above_adc_cut = al[
"NHITS_ABOVE_ADC_CUT"].data<
int>();
146 fit.chi2TdcEnd = 0.0;
147 for (
int i = 0;
i < 7;
i++) {
148 std::ostringstream ostr2;
150 fit.par[
i] = al[ostr2.str()].data<
float>();
151 if (
i > 3 &&
i < 7) {
153 fit.cov[
i] = al[ostr2.str()].data<
float>();
159 if (n_layers < fid.mdtTubeLayer()) n_layers = fid.mdtTubeLayer();
160 if (n_ml < fid.mdtMultilayer()) n_ml = fid.mdtMultilayer();
162 alg_flg = al[
"ALGO_FLAG"].data<std::string>();
163 tb_grp = al[
"TUBE_GROUPING"].data<std::string>();
169 const Identifier moduleID = idHelper.elementID(
id.GetStation(),
id.GetEta(),
id.GetPhi());
171 for (
auto& [tubeId, tubeFit] : fits) {
172 const Identifier thisId = idHelper.channelID(moduleID,
173 tubeId.mdtMultilayer(),
174 tubeId.mdtTubeLayer(),
176 ret->
setFit(std::move(tubeFit), thisId,
msg());
177 ret->
setCalib(std::move(calibs[tubeId]), thisId,
msg());
183 catch (coral::SchemaException &
e) {
190 int head_id,
const std::string &site_name) {
194 ATH_MSG_WARNING(
"Trying to insert data for chamber " <<
id.regionId() <<
" header " <<
head_id << site_name
195 <<
" which already exists!" );
203 if (validation_flag.size() == 0) {
212 bool all_new(
true), all_good(
true);
213 for (
int it : validation_flag) {
214 if (
it != 0) all_new =
false;
215 if (
it != 3) all_good =
false;
216 if (!all_new && !all_good)
break;
237 std::vector<coral::AttributeList> rowBuffer(3);
238 bool row_buffer_initialized(
false);
239 unsigned int nml = t0->numMultilayers();
240 unsigned int nly = t0->numLayers();
241 unsigned int ntb = t0->numTubes();
247 for (
unsigned int ml = 1; ml <= nml; ml++) {
248 for (
unsigned int ly = 1; ly <= nly; ly++) {
249 for (
unsigned int tb = 1;
tb <= ntb;
tb++) {
250 int tb_index = (
tb -1) + ntb * (ly -1) + ntb * nly * (ml -1);
251 if (validation_flag[tb_index] == 3)
continue;
257 if (validation_flag[tb_index] == 5)
262 if (rowsUpdated != 1) {
264 <<
": " << rowsUpdated <<
" Rows to be updated! This is wrong! Check database!" );
268 if (validation_flag[tb_index] != 5) {
270 if (!row_buffer_initialized) {
272 row_buffer_initialized =
true;
276 rowBuffer[0][
"VALIDFLAG"].data<
int>() = validation_flag[tb_index];
278 for (
unsigned int i = 0;
i < 3;
i++) {
279 rowBuffer[
i][
"CALIBFLAG"].data<
int>() =
static_cast<int>(validation_flag[tb_index] > 3);
280 editor[
i]->insertRow(rowBuffer[
i]);
294 catch (coral::SchemaException &
e) {
306 m_query->addToTableList(
"MDT_TUBE");
307 m_query->addToOutputList(
"MDT_TUBE.CHAMBER",
"CHAMBER");
308 m_query->addToOutputList(
"MDT_TUBE.TUBE_ID",
"TUBE_ID");
309 m_query->setRowCacheSize(10000);
311 for (
const auto &
it : rows_tube_table) {
312 if (
it ==
"CHAMBER" ||
it ==
"TUBE_ID")
continue;
313 m_query->addToOutputList(std::string(
"MDT_TUBE.") +
it,
it);
316 if (rows_tube_v_table.size() != 0) {
317 m_query->addToTableList(
"MDT_TUBE_V");
318 for (
const auto &
it : rows_tube_v_table) {
319 m_query->addToOutputList(std::string(
"MDT_TUBE_V.") +
it,
it);
323 if (rows_tube_c_table.size() != 0) {
324 m_query->addToTableList(
"MDT_TUBE_C");
325 for (
const auto &
it : rows_tube_c_table) {
326 m_query->addToOutputList(std::string(
"MDT_TUBE_C.") +
it,
it);
330 conditionData.extend<
int>(
"HEAD_ID");
331 conditionData[
"HEAD_ID"].data<
int>() =
head_id;
332 std::string
condition =
"MDT_TUBE.HEAD_ID = :HEAD_ID and MDT_TUBE.VALIDFLAG>1 ";
333 if (rows_tube_v_table.size() != 0)
335 " and MDT_TUBE.HEAD_ID = MDT_TUBE_V.HEAD_ID and MDT_TUBE.TUBE_ID=MDT_TUBE_V.TUBE_ID and "
336 "MDT_TUBE.CALIBFLAG=MDT_TUBE_V.CALIBFLAG";
337 if (rows_tube_c_table.size() != 0)
339 " and MDT_TUBE.HEAD_ID = MDT_TUBE_C.HEAD_ID and MDT_TUBE.TUBE_ID=MDT_TUBE_C.TUBE_ID and "
340 "MDT_TUBE.CALIBFLAG=MDT_TUBE_C.CALIBFLAG";
342 m_query->addToOrderList(
"MDT_TUBE.CHAMBER");
344 int old_chamber_id(-1);
345 std::map<IConditionsStorage::TubeId, coral::AttributeList> rows_of_a_chamber;
350 if (old_chamber_id != al[
"CHAMBER"].data<int>() && rows_of_a_chamber.size() != 0) {
351 if (!storage.
StoreT0Chamber(old_chamber_id, rows_of_a_chamber)) {
return false; }
352 rows_of_a_chamber.clear();
356 rows_of_a_chamber[
id] = al;
357 old_chamber_id = al[
"CHAMBER"].data<
int>();
359 if (!storage.
StoreT0Chamber(old_chamber_id, rows_of_a_chamber)) {
return false; }
361 }
catch (coral::SchemaException &
e) {
369 for (
unsigned int i = 0;
i < 3;
i++) {
370 rowBuffer[
i].extend<
int>(
"TUBE_ID");
371 rowBuffer[
i].extend<
int>(
"HEAD_ID");
372 rowBuffer[
i][
"HEAD_ID"].data<
int>() =
head_id;
373 rowBuffer[
i].extend<
int>(
"CALIBFLAG");
375 rowBuffer[0].extend<
int>(
"CHAMBER");
376 rowBuffer[0][
"CHAMBER"].data<
int>() =
id.FixedId();
377 rowBuffer[0].extend<
int>(
"VALIDFLAG");
378 rowBuffer[1].extend<
int>(
"ENTRIES");
379 rowBuffer[1].extend<
float>(
"CHISQUARE_1");
380 rowBuffer[1].extend<
float>(
"CHISQUARE_2");
381 for (
int i = 0;
i < 8;
i++) {
382 std::ostringstream ostr2;
385 rowBuffer[0].extend<
float>(ostr2.str());
387 rowBuffer[1].extend<
float>(ostr2.str());
390 rowBuffer[1].extend<
float>(ostr2.str());
392 rowBuffer[2].extend<
float>(ostr2.str());
394 for (
int i = 0;
i < 4;
i++) {
395 std::ostringstream ostr2;
396 ostr2 <<
"ADC_" <<
i;
398 rowBuffer[0].extend<
float>(ostr2.str());
400 rowBuffer[2].extend<
float>(ostr2.str());
402 rowBuffer[2].extend<
float>(
"ADC_CHISQUARE");
403 rowBuffer[0].extend<
int>(
"NHITS");
404 rowBuffer[0].extend<
int>(
"NHITS_ABOVE_ADC_CUT");
405 rowBuffer[1].extend<std::string>(
"ALGO_FLAG");
406 if (t0->implementation() ==
"T0CalibrationClassic") {
407 rowBuffer[1][
"ALGO_FLAG"].data<std::string>() =
"T0Classic";
408 }
else if (t0->implementation() ==
"T0CalibrationMT") {
409 rowBuffer[1][
"ALGO_FLAG"].data<std::string>() =
"T0MT";
411 rowBuffer[1][
"ALGO_FLAG"].data<std::string>() =
"Unknown";
413 rowBuffer[1].extend<std::string>(
"TUBE_GROUPING");
414 rowBuffer[1][
"TUBE_GROUPING"].data<std::string>() =
"UNKNOWN";
418 const int ml,
const int ly,
const int tb,
const MuonFixedId &fixId) {
425 for (
unsigned int i = 0;
i < 3;
i++) { rowBuffer[
i][
"TUBE_ID"].data<
int>() = fixId.
getIdInt(); }
426 rowBuffer[1][
"ENTRIES"].data<
int>() = t0->getFit(channelID)->statistics;
427 rowBuffer[1][
"CHISQUARE_1"].data<
float>() = t0->getFit(channelID)->chi2Tdc;
428 rowBuffer[1][
"CHISQUARE_2"].data<
float>() = t0->getFit(channelID)->chi2TdcEnd;
429 rowBuffer[1][
"TUBE_GROUPING"].data<std::string>() = t0->getFit(channelID)->group_by;
430 for (
int i = 0;
i < 8;
i++) {
431 double val = t0->getFit(channelID)->par[
i];
432 if (std::isnan(
val))
val = -99999.;
433 std::ostringstream ostr2;
436 rowBuffer[0][ostr2.str()].data<
float>() =
val;
438 rowBuffer[1][ostr2.str()].data<
float>() =
val;
440 val = t0->getFit(channelID)->cov[
i];
441 if (std::isnan(
val))
val = -99999.;
443 rowBuffer[1][ostr2.str()].data<
float>() =
val;
445 rowBuffer[2][ostr2.str()].data<
float>() =
val;
447 for (
int i = 0;
i < 4;
i++) {
448 std::ostringstream ostr2;
449 ostr2 <<
"ADC_" <<
i;
450 rowBuffer[0][ostr2.str()].data<
float>() = t0->getFit(channelID)->adc_par[
i];
452 rowBuffer[2][ostr2.str()].data<
float>() = t0->getFit(channelID)->adc_err[
i];
454 rowBuffer[2][
"ADC_CHISQUARE"].data<
float>() = t0->getFit(channelID)->adc_chi2;
455 rowBuffer[0][
"NHITS"].data<
int>() = t0->getFit(channelID)->n_hits;
456 if (t0->getFit(channelID)->n_hits > 999999) rowBuffer[0][
"NHITS"].data<int>() = 999999;
457 rowBuffer[0][
"NHITS_ABOVE_ADC_CUT"].data<
int>() = t0->getFit(channelID)->n_hits_above_adc_cut;
458 if (t0->getFit(channelID)->n_hits_above_adc_cut > 999999) rowBuffer[0][
"NHITS_ABOVE_ADC_CUT"].data<int>() = 999999;
462 const int new_validflag, coral::ITableDataEditor &editor) {
463 std::string updateAction =
"validflag = :newval";
464 std::string updateCondition =
"head_id=:hid and tube_id=:tid and validflag=0";
466 updateData.extend<
int>(
"hid");
467 updateData[0].data<
int>() =
head_id;
468 updateData.extend<
int>(
"tid");
469 updateData[1].data<
int>() =
tube_id;
472 updateData.extend<
int>(
"newval");
473 updateData[2].data<
int>() = new_validflag;
474 long rowsUpdated = editor.updateRows(updateAction, updateCondition, updateData);
480 coral::ITableDataEditor *editor[]) {
481 std::vector<coral::AttributeList> rowBuffer(3);
482 std::vector<coral::IBulkOperation *> bulk_inserter(3);
483 bool row_buffer_initialized(
false);
484 unsigned int nml = t0->numMultilayers();
485 unsigned int nly = t0->numLayers();
486 unsigned int ntb = t0->numTubes();
490 const Identifier elementID = idHelper.elementID(
id.GetStation(),
id.GetEta(),
id.GetPhi());
491 for (
unsigned int ml = 1; ml <= nml; ml++) {
492 for (
unsigned int ly = 1; ly <= nly; ly++) {
493 for (
unsigned int tb = 1;
tb <= ntb;
tb++) {
494 const Identifier channelID = idHelper.channelID(elementID, ml, ly,
tb);
495 if (t0->getFit(channelID)->statistics > 0) {
511 for (
unsigned int ml = 1; ml <= nml; ml++) {
512 for (
unsigned int ly = 1; ly <= nly; ly++) {
513 for (
unsigned int tb = 1;
tb <= ntb;
tb++) {
514 int tb_index = (
tb -1) + ntb * (ly -1) + ntb * nly * (ml -1);
519 if (
static_cast<int>(validation_flag.size()) > tb_index) { v_flag = validation_flag[tb_index]; }
520 if (!row_buffer_initialized) {
522 for (
int i = 0;
i < 3;
i++) { bulk_inserter[
i] = editor[
i]->bulkInsert(rowBuffer[
i], 432); }
523 row_buffer_initialized =
true;
526 rowBuffer[0][
"VALIDFLAG"].data<
int>() = v_flag;
527 for (
unsigned int i = 0;
i < 3;
i++) {
528 rowBuffer[
i][
"CALIBFLAG"].data<
int>() =
static_cast<int>(v_flag > 3);
529 bulk_inserter[
i]->processNextIteration();
534 for (
unsigned int i = 0;
i < 3;
i++) {
535 bulk_inserter[
i]->flush();
536 delete bulk_inserter[
i];
542 coral::ITableDataEditor &editor) {
543 std::string updateAction =
"validflag = :newval";
544 std::string updateCondition =
"head_id=:hid and chamber=:cham and validflag=:old_val";
546 updateData.extend<
int>(
"hid");
547 updateData[0].data<
int>() =
head_id;
548 updateData.extend<std::string>(
"cham");
549 std::ostringstream ostr;
550 ostr <<
id.FixedId();
551 updateData[1].data<std::string>() = ostr.str();
552 updateData.extend<
int>(
"newval");
553 updateData[2].data<
int>() =
to;
554 updateData.extend<
int>(
"old_val");
555 updateData[3].data<
int>() = from;
556 long rowsUpdated = editor.updateRows(updateAction, updateCondition, updateData);
557 return (rowsUpdated <= 432);
561 const std::vector<int> &validflag) {
565 m_query->addToTableList(
"MDT_TUBE");
567 conditionData.extend<
int>(
"hid");
568 conditionData[0].data<
int>() =
head_id;
570 std::ostringstream ostr;
571 ostr <<
id.FixedId();
572 conditionData.extend<std::string>(
"chamb");
573 conditionData[1].data<std::string>() = ostr.str();
574 std::string
condition =
"MDT_TUBE.HEAD_ID = :hid and MDT_TUBE.CHAMBER = :chamb";
576 m_query->addToOutputList(
"count(MDT_TUBE.TUBE_ID)",
"N_TUBES");
577 m_query->addToOutputList(
"MDT_TUBE.VALIDFLAG",
"VALIDFLAG");
578 m_query->groupBy(
"MDT_TUBE.VALIDFLAG");
580 bool has_unvalidated(
false), has_validated(
false);
583 if (al[
"VALIDFLAG"].data<short>() > 0) {
584 if (al[
"N_TUBES"].data<double>() > 0) { has_validated =
true; }
586 if (al[
"N_TUBES"].data<double>() > 0) { has_unvalidated =
true; }
590 if (validflag.size() == 0)
return !has_unvalidated;
591 for (
int it : validflag) {
592 if (
it > 0 && has_validated)
return false;
593 if (
it == 0 && has_unvalidated)
return false;