ATLAS Offline Software
Loading...
Searching...
No Matches
CalibT0DbOperations.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
6
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"
29#include "cmath"
30#include "sstream"
31
32namespace MuonCalib {
33
35 AthMessaging{"CalibT0DbOperations"}, m_db_conn(db_conn) {
36 m_idHelperSvc.retrieve().ignore();
37 }
38
39 MdtStationT0Container *CalibT0DbOperations::LoadT0Calibration(const NtupleStationId &id, int head_id, std::string &site_name) {
40 try {
41 // select MDT_TUBE.TUBE_ID, MDT_TUBE.P4 from MDT_TUBE where MDT_TUBE.HEAD_ID = :hid and MDT_TUBE.CHAMBER = :chamb and
42 // MDT_TUBE.VALIDFLAG>1
43 m_db_conn.OpenTransaction();
44 m_query = m_db_conn.GetQuery();
45 m_query->setRowCacheSize(432);
46 m_query->addToTableList("MDT_TUBE");
47 coral::AttributeList conditionData;
48 conditionData.extend<int>("hid");
49 conditionData[0].data<int>() = head_id;
50 // as long as the chamber id is stored as a string, it is more efficient to pass the chamber id as a string in the query
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();
60 int count(0);
61 while (cursor.next()) {
62 const coral::AttributeList &al = cursor.currentRow();
63 MuonFixedId fid(al[0].data<int>());
64 ret->Sett0(fid.mdtMultilayer(), fid.mdtTubeLayer(), fid.mdtTube(), al[1].data<float>(), al[2].data<float>());
65 count++;
66 }
67 if (count == 0) {
68 ATH_MSG_WARNING( "No tubes found for " << id.regionId() << " Header " << site_name << head_id );
69 delete ret;
70 return nullptr;
71 }
72 return ret;
73 } // try
74 catch (coral::SchemaException &e) {
75 ATH_MSG_WARNING( "Schema exception : " << e.what() );
76 return nullptr;
77 }
78 }
79
80 MdtTubeFitContainer *CalibT0DbOperations::LoadT0Validation(const NtupleStationId &id, int head_id, std::string & /*site_name*/) {
81 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
82 try {
83 m_db_conn.OpenTransaction();
84 m_query = m_db_conn.GetQuery();
85 m_query->setRowCacheSize(432);
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;
91 // as long as the chamber id is stored as a string, it is more efficient to pass the chamber id as a string in the query
92 std::ostringstream ostr;
93 ostr << id.FixedId();
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;
107 if (i == 4) {
108 ostr << "MDT_TUBE.P4";
109 } else {
110 ostr << "MDT_TUBE_V.P" << i;
111 }
112 std::ostringstream ostr2;
113 ostr2 << "P" << i;
114 m_query->addToOutputList(ostr.str(), ostr2.str());
115 if (i > 3 && i < 7) {
116 if (i == 4) {
117 m_query->addToOutputList("MDT_TUBE_V.P4_ERR", "P4_ERR");
118 } else {
119 ostr << "_ERR";
120 ostr2 << "_ERR";
121 m_query->addToOutputList(ostr.str(), ostr2.str());
122 }
123 }
124 }
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;
134 bool str_set(false);
135 while (cursor.next()) {
136 const coral::AttributeList &al = cursor.currentRow();
137 MuonFixedId fid(al["TUBE_ID"].data<int>());
138 MdtTubeFitContainer::SingleTubeCalib &calib(calibs[fid]);
139 calib.t0 = al["P4"].data<float>();
140 calib.adcCal = 0;
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>();
145 fit.chi2Tdc = 0.0;
146 fit.chi2TdcEnd = 0.0;
147 for (int i = 0; i < 7; i++) {
148 std::ostringstream ostr2;
149 ostr2 << "P" << i;
150 fit.par[i] = al[ostr2.str()].data<float>();
151 if (i > 3 && i < 7) {
152 ostr2 << "_ERR";
153 fit.cov[i] = al[ostr2.str()].data<float>();
154 } else {
155 fit.cov[i] = 0.0;
156 }
157 }
158 if (n_tubes < fid.mdtTube()) n_tubes = fid.mdtTube();
159 if (n_layers < fid.mdtTubeLayer()) n_layers = fid.mdtTubeLayer();
160 if (n_ml < fid.mdtMultilayer()) n_ml = fid.mdtMultilayer();
161 if (!str_set) {
162 alg_flg = al["ALGO_FLAG"].data<std::string>();
163 tb_grp = al["TUBE_GROUPING"].data<std::string>();
164 str_set = true;
165 }
166 }
167
168 // create output class
169 const Identifier moduleID = idHelper.elementID(id.GetStation(), id.GetEta(), id.GetPhi());
170 MdtTubeFitContainer *ret = new MdtTubeFitContainer(m_idHelperSvc.get(), moduleID);
171 for (auto& [tubeId, tubeFit] : fits) {
172 const Identifier thisId = idHelper.channelID(moduleID,
173 tubeId.mdtMultilayer(),
174 tubeId.mdtTubeLayer(),
175 tubeId.mdtTube());
176 ret->setFit(std::move(tubeFit), thisId, msg());
177 ret->setCalib(calibs[tubeId], thisId, msg());
178 }
179 ret->setGroupBy(tb_grp);
180 ret->setImplementation(alg_flg);
181 return ret;
182 } // try
183 catch (coral::SchemaException &e) {
184 ATH_MSG_WARNING( "Schema exception : " << e.what() );
185 return nullptr;
186 }
187 }
188
189 bool CalibT0DbOperations::WriteT0Chamber(const NtupleStationId &id, const MdtTubeFitContainer *t0, std::vector<int> &validation_flag,
190 int head_id, const std::string &site_name) {
191 try {
192 m_db_conn.OpenTransaction();
193 if (!checkTubesPresent(head_id, site_name, id, validation_flag)) {
194 ATH_MSG_WARNING( "Trying to insert data for chamber " << id.regionId() << " header " << head_id << site_name
195 << " which already exists!" );
196 m_db_conn.Rollback();
197 return false;
198 }
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"))};
202 // insert new tubes
203 if (validation_flag.size() == 0) {
204 if (!insertTubes(site_name, head_id, id, t0, validation_flag, editor)) {
205 m_db_conn.Rollback();
206 return false;
207 }
208 m_db_conn.Commit();
209 return true;
210 }
211 // check if all tubes ar enew or if all tubes a validated good
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;
217 }
218 // insert new tubes
219 if (all_new) {
220 if (!insertTubes(site_name, head_id, id, t0, validation_flag, editor)) {
221 m_db_conn.Rollback();
222 return false;
223 }
224 m_db_conn.Commit();
225 return true;
226 }
227 // all tubes are good. Update the whole chamber in one go
228 if (all_good) {
229 if (!setValidationFlag(site_name, head_id, id, 0, 3, *editor[0])) {
230 m_db_conn.Rollback();
231 return false;
232 }
233 m_db_conn.Commit();
234 return true;
235 }
236 // update flag for failed and dummy tubes - insert for failed
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();
242 MuonFixedId fixId(0);
243 fixId.setTechnology(0);
244 fixId.setStationName(id.GetStation());
245 fixId.setStationEta(id.GetEta());
246 fixId.setStationPhi(id.GetPhi());
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;
252 fixId.setMdtTube(tb);
253 fixId.setMdtTubeLayer(ly);
254 fixId.setMdtMultilayer(ml);
255 // new tubes or tubes that are validated bad are inserted
256 int rowsUpdated;
257 if (validation_flag[tb_index] == 5)
258 rowsUpdated = setValidFlag(site_name, head_id, fixId.getIdInt(), 5, *editor[0]);
259 else
260 rowsUpdated = setValidFlag(site_name, head_id, fixId.getIdInt(), 1, *editor[0]);
261
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!" );
265 m_db_conn.Rollback();
266 return false;
267 }
268 if (validation_flag[tb_index] != 5) {
269 // initialize row buffers
270 if (!row_buffer_initialized) {
271 initRowBuffer(rowBuffer, id, head_id, site_name, t0);
272 row_buffer_initialized = true;
273 }
274 // fill row buffers
275 fillRowBuffer(rowBuffer, t0, ml, ly, tb, fixId);
276 rowBuffer[0]["VALIDFLAG"].data<int>() = validation_flag[tb_index];
277 // loop over 3 t0 tables - get seqid from 1st and insert it into 2nd and 3rd.
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]);
281 }
282 }
283 }
284 }
285 }
286 // now all tubes whit validflag == 0 are good update in one go
287 if (!setValidationFlag(site_name, head_id, id, 0, 3, *editor[0])) {
288 m_db_conn.Rollback();
289 return false;
290 }
291 m_db_conn.Commit();
292 return true;
293 } // try
294 catch (coral::SchemaException &e) {
295 ATH_MSG_WARNING( "Schema exception : " << e.what() );
296 m_db_conn.Rollback();
297 return false;
298 }
299 }
300
301 bool CalibT0DbOperations::ReadForConditions(const std::string& /*site_name*/, int head_id, IConditionsStorage &storage) {
302 try {
303 // prepare query
304 m_db_conn.OpenTransaction();
305 m_query = m_db_conn.GetQuery();
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);
310 const std::vector<std::string> &rows_tube_table(storage.RowsFromMdtTubeTable());
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);
314 }
315 const std::vector<std::string> &rows_tube_v_table(storage.RowsFromMdtTubeVTable());
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);
320 }
321 }
322 const std::vector<std::string> &rows_tube_c_table(storage.RowsFromMdtTubeCTable());
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);
327 }
328 }
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");
343 // execute query
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();
349 // if we have a new chamber - call callback
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();
353 }
354 // store row
355 IConditionsStorage::TubeId id(al["TUBE_ID"].data<int>());
356 rows_of_a_chamber[id] = al;
357 old_chamber_id = al["CHAMBER"].data<int>();
358 }
359 if (!storage.StoreT0Chamber(old_chamber_id, rows_of_a_chamber)) { return false; }
360 return true;
361 } catch (coral::SchemaException &e) {
362 ATH_MSG_WARNING( "Schema exception : " << e.what() );
363 return false;
364 }
365 }
366
367 inline void CalibT0DbOperations::initRowBuffer(std::vector<coral::AttributeList> &rowBuffer, const NtupleStationId &id,
368 const int head_id, const std::string & /*site_name*/, const MdtTubeFitContainer *t0) {
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");
374 }
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;
383 ostr2 << "P" << i;
384 if (i == 4)
385 rowBuffer[0].extend<float>(ostr2.str());
386 else
387 rowBuffer[1].extend<float>(ostr2.str());
388 ostr2 << "_ERR";
389 if (i > 3 && i < 7)
390 rowBuffer[1].extend<float>(ostr2.str());
391 else
392 rowBuffer[2].extend<float>(ostr2.str());
393 }
394 for (int i = 0; i < 4; i++) {
395 std::ostringstream ostr2;
396 ostr2 << "ADC_" << i;
398 rowBuffer[0].extend<float>(ostr2.str());
399 ostr2 << "_ERR";
400 rowBuffer[2].extend<float>(ostr2.str());
401 }
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";
410 } else {
411 rowBuffer[1]["ALGO_FLAG"].data<std::string>() = "Unknown";
412 }
413 rowBuffer[1].extend<std::string>("TUBE_GROUPING");
414 rowBuffer[1]["TUBE_GROUPING"].data<std::string>() = "UNKNOWN";
415 }
416
417 inline void CalibT0DbOperations::fillRowBuffer(std::vector<coral::AttributeList> &rowBuffer, const MdtTubeFitContainer *t0,
418 const int ml, const int ly, const int tb, const MuonFixedId &fixId) {
419 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
420 const Identifier channelID = idHelper.channelID(fixId.stationName(),
421 fixId.eta(),
422 fixId.phi(),
423 ml, ly, tb);
424
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;
434 ostr2 << "P" << i;
435 if (i == 4)
436 rowBuffer[0][ostr2.str()].data<float>() = val;
437 else
438 rowBuffer[1][ostr2.str()].data<float>() = val;
439 ostr2 << "_ERR";
440 val = t0->getFit(channelID)->cov[i];
441 if (std::isnan(val)) val = -99999.;
442 if (i > 3 && i < 7)
443 rowBuffer[1][ostr2.str()].data<float>() = val;
444 else
445 rowBuffer[2][ostr2.str()].data<float>() = val;
446 }
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];
451 ostr2 << "_ERR";
452 rowBuffer[2][ostr2.str()].data<float>() = t0->getFit(channelID)->adc_err[i];
453 }
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;
459 }
460
461 bool CalibT0DbOperations::setValidFlag(const std::string & /*site_name*/, const int head_id, const int tube_id,
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;
470 // updateData.extend<std::string>("sn");
471 // updateData[2].data<std::string>()=site_name;
472 updateData.extend<int>("newval");
473 updateData[2].data<int>() = new_validflag;
474 long rowsUpdated = editor.updateRows(updateAction, updateCondition, updateData);
475 return rowsUpdated;
476 }
477
478 bool CalibT0DbOperations::insertTubes(const std::string &site_name, int head_id, const NtupleStationId &id,
479 const MdtTubeFitContainer *t0, const std::vector<int> &validation_flag,
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();
487 // do not insert completely dead chambers
488 bool all_dead(true);
489 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
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) {
496 all_dead = false;
497 break;
498 }
499 }
500 }
501 }
502 if (all_dead) {
503 ATH_MSG_WARNING( "Refusing to insert completely dead chamber" );
504 return false;
505 }
506 MuonFixedId fixId(0);
507 fixId.setTechnology(0);
508 fixId.setStationName(id.GetStation());
509 fixId.setStationEta(id.GetEta());
510 fixId.setStationPhi(id.GetPhi());
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);
515 fixId.setMdtTube(tb);
516 fixId.setMdtTubeLayer(ly);
517 fixId.setMdtMultilayer(ml);
518 int v_flag = 0;
519 if (static_cast<int>(validation_flag.size()) > tb_index) { v_flag = validation_flag[tb_index]; }
520 if (!row_buffer_initialized) {
521 initRowBuffer(rowBuffer, id, head_id, site_name, t0);
522 for (int i = 0; i < 3; i++) { bulk_inserter[i] = editor[i]->bulkInsert(rowBuffer[i], 432); }
523 row_buffer_initialized = true;
524 }
525 fillRowBuffer(rowBuffer, t0, ml, ly, tb, fixId);
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();
530 }
531 }
532 }
533 }
534 for (unsigned int i = 0; i < 3; i++) {
535 bulk_inserter[i]->flush();
536 delete bulk_inserter[i];
537 }
538 return true;
539 }
540
541 bool CalibT0DbOperations::setValidationFlag(const std::string & /*site_name*/, int head_id, const NtupleStationId &id, int from, int to,
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);
558 }
559
560 inline bool CalibT0DbOperations::checkTubesPresent(const int head_id, const std::string & /*site_name*/, const NtupleStationId &id,
561 const std::vector<int> &validflag) {
562 m_query = m_db_conn.GetQuery();
563 // select count(MDT_TUBE.TUBE_ID) as N_TUBES, MDT_TUBE.VALIDFLAG as VALIDFLAG from MDT_TUBE where MDT_TUBE.HEAD_ID = :hid and
564 // MDT_TUBE.CHAMBER = :chamb and SITE_NAME=:sn group by MDT_TUBE.VALIDFLAG;
565 m_query->addToTableList("MDT_TUBE");
566 coral::AttributeList conditionData;
567 conditionData.extend<int>("hid");
568 conditionData[0].data<int>() = head_id;
569 // as long as the chamber id is stored as a string, it is more efficient to pass the chamber id as a string in the query
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();
583 if (al["VALIDFLAG"].data<short>() > 0) {
584 if (al["N_TUBES"].data<double>() > 0) { has_validated = true; }
585 } else {
586 if (al["N_TUBES"].data<double>() > 0) { has_unvalidated = true; }
587 }
588 }
589 // check what has to be done
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;
594 }
595 return true;
596 }
597
598} // namespace MuonCalib
#define ATH_MSG_WARNING(x)
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
static Double_t al
static Double_t t0
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)
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...
Definition MuonFixedId.h:50
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 &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
CscCalcPed - algorithm that finds the Cathode Strip Chamber pedestals from an RDO.