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"
46 m_query->addToTableList(
"MDT_TUBE");
47 coral::AttributeList conditionData;
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";
54 m_query->setCondition(condition, conditionData);
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");
58 coral::ICursor &cursor =
m_query->execute();
61 while (cursor.next()) {
62 const coral::AttributeList &
al = cursor.currentRow();
68 ATH_MSG_WARNING(
"No tubes found for " <<
id.regionId() <<
" Header " << site_name << head_id );
74 catch (coral::SchemaException &e) {
86 m_query->addToTableList(
"MDT_TUBE");
87 m_query->addToTableList(
"MDT_TUBE_V");
88 coral::AttributeList conditionData;
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();
96 std::string condition =
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";
99 m_query->setCondition(condition, conditionData);
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");
129 coral::ICursor &cursor =
m_query->execute();
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;
135 while (cursor.next()) {
136 const coral::AttributeList &
al = cursor.currentRow();
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>();
162 alg_flg =
al[
"ALGO_FLAG"].data<std::string>();
163 tb_grp =
al[
"TUBE_GROUPING"].data<std::string>();
171 for (
auto& [tubeId, tubeFit] : fits) {
173 tubeId.mdtMultilayer(),
174 tubeId.mdtTubeLayer(),
176 ret->
setFit(std::move(tubeFit), 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!" );
199 coral::ITableDataEditor *editor[] = {&
m_db_conn.GetTableEditor(std::string(
"MDT_TUBE")),
200 &
m_db_conn.GetTableEditor(std::string(
"MDT_TUBE_V")),
201 &
m_db_conn.GetTableEditor(std::string(
"MDT_TUBE_C"))};
203 if (validation_flag.size() == 0) {
204 if (!
insertTubes(site_name, head_id,
id,
t0, validation_flag, editor)) {
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;
220 if (!
insertTubes(site_name, head_id,
id,
t0, validation_flag, editor)) {
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) {
263 ATH_MSG_WARNING(
id.regionId() <<
" tb=" << tb <<
" ly=" << ly <<
" ml=" << ml <<
" head_id=" << head_id
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);
329 coral::AttributeList conditionData;
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)
334 condition = condition +
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)
338 condition = condition +
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";
341 m_query->setCondition(condition, conditionData);
342 m_query->addToOrderList(
"MDT_TUBE.CHAMBER");
344 int old_chamber_id(-1);
345 std::map<IConditionsStorage::TubeId, coral::AttributeList> rows_of_a_chamber;
346 coral::ICursor &cursor =
m_query->execute();
347 while (cursor.next()) {
348 const coral::AttributeList &
al = cursor.currentRow();
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";
465 coral::AttributeList updateData;
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++) {
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";
545 coral::AttributeList updateData;
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");
566 coral::AttributeList conditionData;
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";
575 m_query->setCondition(condition, conditionData);
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");
579 coral::ICursor &cursor =
m_query->execute();
580 bool has_unvalidated(
false), has_validated(
false);
581 while (cursor.next()) {
582 const coral::AttributeList &
al = cursor.currentRow();
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;
#define ATH_MSG_WARNING(x)
char data[hepevt_bytes_allocation_ATLAS]
MsgStream & msg() const
The standard message stream.
AthMessaging(IMessageSvc *msgSvc, const std::string &name)
Constructor.
Identifier elementID(int stationName, int stationEta, int stationPhi) const
Identifier channelID(int stationName, int stationEta, int stationPhi, int multilayer, int tubeLayer, int tube) const
MdtStationT0Container * LoadT0Calibration(const NtupleStationId &id, int head_id, std::string &site_name)
void initRowBuffer(std::vector< coral::AttributeList > &rowBuffer, const NtupleStationId &id, const int head_id, const std::string &site_name, const MdtTubeFitContainer *t0)
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
bool setValidationFlag(const std::string &site_name, int head_id, const NtupleStationId &id, int from, int to, coral::ITableDataEditor &editor)
CalibT0DbOperations(CalibDbConnection &db_conn)
bool insertTubes(const std::string &site_name, int head_id, const NtupleStationId &id, const MdtTubeFitContainer *t0, const std::vector< int > &validation_flag, coral::ITableDataEditor *editor[])
bool checkTubesPresent(const int head_id, const std::string &site_name, const NtupleStationId &id, const std::vector< int > &validflag)
bool setValidFlag(const std::string &site_name, const int head_id, const int tube_id, const int new_validflag, coral::ITableDataEditor &editor)
bool WriteT0Chamber(const NtupleStationId &id, const MdtTubeFitContainer *t0, std::vector< int > &validation_flag, int head_id, const std::string &site_name)
bool ReadForConditions(const std::string &site_name, int head_id, IConditionsStorage &storage)
void fillRowBuffer(std::vector< coral::AttributeList > &rowBuffer, const MdtTubeFitContainer *t0, const int ml1, const int ly, const int ml2, const MuonFixedId &fixId)
MdtTubeFitContainer * LoadT0Validation(const NtupleStationId &id, int head_id, std::string &site_name)
CalibDbConnection & m_db_conn
virtual bool StoreT0Chamber(const int &chamber, const std::map< TubeId, coral::AttributeList > &rows)=0
virtual const std::vector< std::string > & RowsFromMdtTubeTable() const
virtual const std::vector< std::string > & RowsFromMdtTubeVTable() const
virtual const std::vector< std::string > & RowsFromMdtTubeCTable() const
bool Sett0(const unsigned int &ml, const unsigned int &ly, const unsigned int &tb, double t0, double adc=0)
set t0 value of the tube tb in layer ly of multilayer ml; tb, ly, ml>0
bool setCalib(SingleTubeCalib val, const Identifier &tubeId, MsgStream &msg)
set the calibration constants of a single tube
Holds single-tube full calibration information of one chamber.
void setGroupBy(const std::string &group_by)
bool setFit(SingleTubeFit &&val, const Identifier &tubeId, MsgStream &log)
set the calibration constants of a single tube
void setImplementation(const std::string &impl)
set the name of the implementation used to fill this class
Implements fixed identifiers not dependent upon Athena Identifier for internal use in the calibration...
bool setTechnology(int num)
set technology to num in the fixed identifier
bool setStationEta(int num)
set station eta to num in the fixed identifier
bool setMdtTubeLayer(int num)
Mdt specific: set layer to num.
bool setStationPhi(int num)
set station phi to num in the fixed identifier
bool setMdtTube(int num)
Mdt specific: set tube to num.
int mdtTubeLayer() const
Mdt specific:
int mdtTube() const
Mdt specific:
unsigned int getIdInt() const
bool setMdtMultilayer(int num)
Mdt specific: set multilayer to num.
bool setStationName(int num)
set station name to num in the fixed identifier
int mdtMultilayer() const
Mdt specific:
Station Identifier for sorting calibration data.
int count(std::string s, const std::string ®x)
count how many occurances of a regx are in a string
CscCalcPed - algorithm that finds the Cathode Strip Chamber pedestals from an RDO.