ATLAS Offline Software
Loading...
Searching...
No Matches
CoolInserter.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
5// this
7
12// coral
14#include "CoolKernel/ConstRecordAdapter.h"
15#include "CoolKernel/FolderSpecification.h"
16#include "CoralBase/Attribute.h"
17#include "CoralBase/AttributeList.h"
18
19// MuonCalibIdentifier
21
22// MuonCalibMath
24
25// MuonCalibStandAloneBase
28
29// c - c++
30#include <typeinfo>
31
32#include "exception"
33#include "iostream"
34#include "sstream"
35#include "zlib.h"
36
37namespace MuonCalib {
38
39 CoolInserter::CoolInserter(const std::string &name, ISvcLocator *pSvcLocator) :
40 AthAlgorithm(name, pSvcLocator),
41 m_version("v0.0"),
42 m_reg_sel_svc("RegionSelectionSvc", name),
43 m_cool_connect(false),
44 m_t0_created(false),
45 m_rt_created(false) {
46 declareProperty("CoolConnectionString", m_cool_connection_string);
47 declareProperty("RtVersion", m_version);
48 declareProperty("T0Tag", m_tagt0);
49 declareProperty("RtTag", m_tagrt);
50 declareProperty("IOVStart", m_iov_start);
51 declareProperty("IOVEnd", m_iov_end);
52 declareProperty("T0Folder", m_t0_folder);
53 declareProperty("RtFolder", m_rt_folder);
54 declareProperty("T0FolderCompress", m_compressed_t0);
55 declareProperty("RtFolderCompress", m_compressed_rt);
58 m_iovt_start = 0;
59 m_iovt_end = 0;
60 m_aver_t0 = 0;
61 m_aver_adc = 0;
62 }
63
65 // check jo
66 if (m_tagt0.size() != m_t0_folder.size() || m_tagrt.size() != m_rt_folder.size()) {
67 ATH_MSG_FATAL("Configuration error: Number of folders and tags do not match!");
68 return StatusCode::FAILURE;
69 }
70
71 ATH_CHECK(m_idHelperSvc.retrieve());
72
73 ATH_CHECK(m_DetectorManagerKey.initialize());
74
75 // get region selection service
76 ATH_CHECK(m_reg_sel_svc.retrieve());
77
78 // connect to cool database
79 try {
80 m_db = m_app.databaseService().openDatabase(m_cool_connection_string, false);
81 } catch (cool::DatabaseDoesNotExist &e) {
82 ATH_MSG_INFO("Creating new database.");
83 try {
84 m_db = m_app.databaseService().createDatabase(m_cool_connection_string);
85 } catch (cool::Exception &e) {
86 ATH_MSG_FATAL("Cannot create database and datbasae does not exist!");
87 return StatusCode::FAILURE;
88 }
89 }
90 m_cool_connect = true;
91 if (m_t0_folder.size()) {
92 ATH_MSG_INFO("Replication into t0 folders: ");
93 for (unsigned int i = 0; i < m_t0_folder.size(); i++) { ATH_MSG_INFO(m_t0_folder[i] << "(" << m_tagt0[i] << ") "); }
94 }
95 if (m_rt_folder.size()) {
96 ATH_MSG_INFO("Replication into rt folders: ");
97 for (unsigned int i = 0; i < m_rt_folder.size(); i++) { ATH_MSG_INFO(m_rt_folder[i] << "(" << m_tagrt[i] << ") "); }
98 }
99 // retrieve calibration sources
101
102 // get iov
103 for (unsigned int i = 0; i < m_calibration_sources.size(); i++) {
104 int iov_start(-1), iov_end(-1);
105 m_calibration_sources[i]->SetIOV(iov_start, iov_end);
107 if (m_iov_end == -1 && iov_end != -1) m_iov_end = iov_end;
108 }
109 if (m_iov_start < 0 || m_iov_end == -1) {
110 ATH_MSG_FATAL("IOV has to be set, if no calibration source provides it!");
111 return StatusCode::FAILURE;
112 }
113 IOVTime start(m_iov_start, 0);
114 m_iovt_start = start.re_time();
115 if (m_iov_end < -1) {
117 m_iovt_end = end.timestamp();
118 } else {
119 IOVTime end(m_iov_end, 0);
120 m_iovt_end = end.re_time();
121 }
122 ATH_MSG_INFO("IOV is " << m_iov_start << " to " << m_iov_end);
123 // fill compressed flags
124 if (m_compressed_t0.size() < m_t0_folder.size())
125 for (unsigned int i = m_compressed_t0.size(); i < m_t0_folder.size(); i++) m_compressed_t0.push_back(false);
126 if (m_compressed_rt.size() < m_rt_folder.size())
127 for (unsigned int i = m_compressed_rt.size(); i < m_rt_folder.size(); i++) m_compressed_rt.push_back(false);
128 return StatusCode::SUCCESS;
129 }
130
132 ATH_MSG_INFO("finalizing ");
133 try {
134 for (auto & i : m_mdtt0_fld) i->flushStorageBuffer();
135 for (auto & i : m_mdtrt_fld) i->flushStorageBuffer();
136 } catch (std::exception &e) {
137 ATH_MSG_FATAL("Exception in finalize: " << e.what());
138 return StatusCode::FAILURE;
139 }
140 return StatusCode::SUCCESS;
141 }
142
144 for (unsigned int i = 0; i < m_calibration_sources.size(); i++) {
145 ATH_MSG_INFO("Now running " << m_calibration_sources[i]);
146 if (!m_calibration_sources[i]->InstertCalibration(this, static_cast<bool>(m_t0_folder.size()),
147 static_cast<bool>(m_rt_folder.size()))) {
148 ATH_MSG_FATAL("Calibration Source " << m_calibration_sources[i].name() << " failed!");
149 return StatusCode::FAILURE;
150 }
151 }
152 return StatusCode::SUCCESS;
153 }
154
156 m_n_tubes_added = 0;
157 m_sid = sid;
158 m_sid.SetMultilayer(0);
159
161 const MuonGM::MuonDetectorManager *MuonDetMgr = DetectorManagerHandle.cptr();
162 if (MuonDetMgr == nullptr) {
163 ATH_MSG_ERROR("Null pointer to the read MuonDetectorManager conditions object");
164 return false;
165 }
166
167 if (!m_sid.InitializeGeometry(m_idHelperSvc->mdtIdHelper(), MuonDetMgr)) return false;
168 // get number of tubes
169 int max_nly(-1);
170 int max_ntb(-1);
171 for (int ml = 0; ml < m_sid.NMultilayers(); ml++) {
172 if (max_nly < m_sid.NLayers(ml)) max_nly = m_sid.NLayers(ml);
173 if (max_ntb < m_sid.NTubes(ml)) max_ntb = m_sid.NTubes(ml);
174 }
175 m_n_tubes_chamber = max_nly * max_ntb * m_sid.NMultilayers();
176 m_data_string.str("");
177 m_data_string.clear();
178 m_data_string << "T0" << m_sid.regionId() << "," << m_version << ",1," << m_n_tubes_chamber << std::endl;
179 m_aver_t0 = 0;
180 m_aver_adc = 0;
181 return true;
182 }
183
184 bool CoolInserter::AppendT0(float t0, int validflag, float adc0) {
185 if (m_n_tubes_added >= m_n_tubes_chamber) { return false; }
186 m_data_string << t0 << "," << validflag << "," << adc0 << ",";
188 m_aver_t0 += t0;
189 m_aver_adc += adc0;
190 return true;
191 }
192
193 bool CoolInserter::StoreT0Chamber(const NtupleStationId &id, const std::string &file, unsigned int creation_flags) {
194 MuonFixedId fid(id.FixedId());
195 // generate fixed if for chamber
196 if (!m_reg_sel_svc->isInRegion(fid)) { return true; }
197 // do not fill double
198 if (m_t0_filled.find(id) != m_t0_filled.end()) { return true; }
199 m_t0_filled.insert(id);
200 // check number of added tubes
202 ATH_MSG_WARNING("Filling missing tubes with average for chamber" << m_sid.regionId());
205 for (int i = m_n_tubes_added; i < m_n_tubes_chamber; i++) { AppendT0(m_aver_t0, 5, m_aver_adc); }
206 }
207 // cool
209 for (unsigned int i = 0; i < m_t0_folder.size(); i++) {
210 if (!m_db->existsFolder(m_t0_folder[i])) {
211 if (!create_folder(i, false)) {
212 ATH_MSG_FATAL("Cannot create folder '" << m_t0_folder[i] << "'!");
213 return false;
214 }
215 }
216 m_mdtt0_fld.push_back(m_db->getFolder(m_t0_folder[i]));
217 m_mdtt0_fld[i]->setupStorageBuffer();
218
219 cool::RecordSpecification mdtt0_rec = (m_mdtt0_fld[i]->payloadSpecification());
220 bool compressed(false);
221 if (m_compressed_t0.size() > i) compressed = m_compressed_t0[i];
222 m_mdtt0_cool_row[compressed] = cool::Record(mdtt0_rec).attributeList();
223 }
224 m_t0_created = true;
225 }
226 if (m_t0_created) {
227 for (auto & it : m_mdtt0_cool_row) {
228 it.second["file"].data<cool::String4k>() = file;
229 it.second["tech"].data<int>() = creation_flags;
230 if (it.first) {
231 uLongf dest_len = compressBound(m_data_string.str().size());
232 coral::Blob &blob(it.second["data"].data<coral::Blob>());
233 blob.resize(dest_len);
234 Bytef *p = static_cast<Bytef *>(blob.startingAddress());
235
236 compress(p, &dest_len, reinterpret_cast<const Bytef *>(m_data_string.str().c_str()), m_data_string.str().size());
237 blob.resize(dest_len);
238 } else {
239 it.second["data"].data<cool::String16M>() = m_data_string.str();
240 }
241 for (unsigned int i = 0; i < m_mdtt0_fld.size(); i++) {
242 cool::ConstRecordAdapter record(m_mdtt0_fld[i]->payloadSpecification(), m_mdtt0_cool_row[m_compressed_t0[i]]);
243 m_mdtt0_fld[i]->storeObject(m_iovt_start, m_iovt_end, record, id.FixedId(), m_tagt0[i]);
244 }
245 }
246 }
247 return true;
248 }
249
250 bool CoolInserter::StoreRtChamber(const NtupleStationId &id, const std::map<int, SamplePoint> &points, const std::string &file,
251 unsigned int creation_flags) {
252 // generate fixed if for chamber
253 MuonFixedId fid(id.FixedId());
254 if (!m_reg_sel_svc->isInRegion(fid)) { return true; }
255 // check for duplicates
256 NtupleStationId id_cp(id);
257 bool found[3];
258 for (int i = 0; i < 3; i++) {
259 id_cp.SetMultilayer(i);
260 found[i] = (m_rt_filled.find(id_cp) != m_rt_filled.end());
261 }
262 if (id.GetMl() == 0) {
263 if (found[0] || (found[1] && found[2])) return true;
264 if (found[1]) { id_cp.SetMultilayer(2); }
265 if (found[2]) { id_cp.SetMultilayer(1); }
266 if (!found[1] && !found[2]) { id_cp.SetMultilayer(0); }
267 }
268 if (id.GetMl() != 0) {
269 if (found[0] || found[id.GetMl()]) return true;
270 }
271 m_rt_filled.insert(id_cp);
273 for (unsigned int i = 0; i < m_rt_folder.size(); i++) {
274 if (!m_db->existsFolder(m_rt_folder[i])) {
275 if (!create_folder(i, true)) {
276 ATH_MSG_FATAL("Cannot create folder '" << m_rt_folder[i] << "'!");
277 return false;
278 }
279 }
280 m_mdtrt_fld.push_back(m_db->getFolder(m_rt_folder[i]));
281 m_mdtrt_fld[i]->setupStorageBuffer();
282
283 cool::RecordSpecification mdtrt_rec = (m_mdtrt_fld[i]->payloadSpecification());
284 bool compressed(false);
285 if (m_compressed_rt.size() > i) compressed = m_compressed_rt[i];
286 m_mdtrt_cool_row[compressed] = cool::Record(mdtrt_rec).attributeList();
287 }
288 m_rt_created = true;
289 }
290 std::ostringstream data_column;
291 data_column << id_cp.FixedId() << "," << points.size() << std::endl;
292 for (const auto & point : points) {
293 data_column << point.second.x2() << "," << point.second.x1() << "," << point.second.error() << ",";
294 }
295 if (m_rt_created) {
296 for (auto & it : m_mdtrt_cool_row) {
297 it.second["tech"].data<int>() = creation_flags;
298 it.second["file"].data<cool::String4k>() = file;
299 if (it.first) {
300 uLongf dest_len = compressBound(data_column.str().size());
301 coral::Blob &blob(it.second["data"].data<coral::Blob>());
302 blob.resize(dest_len);
303 Bytef *p = static_cast<Bytef *>(blob.startingAddress());
304
305 compress(p, &dest_len, reinterpret_cast<const Bytef *>(data_column.str().c_str()), data_column.str().size());
306 blob.resize(dest_len);
307 } else {
308 it.second["data"].data<cool::String16M>() = data_column.str();
309 }
310 } // for(std::map<bool, coral::AttributeList> ...
311 for (unsigned int i = 0; i < m_mdtrt_fld.size(); i++) {
312 cool::ConstRecordAdapter record(m_mdtrt_fld[i]->payloadSpecification(), m_mdtrt_cool_row[m_compressed_rt[i]]);
313 m_mdtrt_fld[i]->storeObject(m_iovt_start, m_iovt_end, record, id_cp.FixedId(), m_tagrt[i]);
314 }
315 }
316 return true;
317 }
318
319 inline bool CoolInserter::create_folder(const unsigned int &fldr_nr, const bool &is_rt) {
320 std::string folder;
321 std::string description;
322 if (is_rt) {
323 folder = m_rt_folder[fldr_nr];
324 description = std::string(
325 "<timeStamp>run-lumi</timeStamp><addrHeader><address_header service_type=\"71\" clid=\"1238547719\" "
326 "/></addrHeader><typeName>CondAttrListCollection</typeName><key>") +
327 folder + "</key>";
328 } else {
329 folder = m_t0_folder[fldr_nr];
330 description = std::string(
331 "<timeStamp>run-lumi</timeStamp><addrHeader><address_header service_type=\"71\" clid=\"1238547719\" "
332 "/></addrHeader><typeName>CondAttrListCollection</typeName><key>") +
333 folder + "</key>";
334 }
335
336 bool compressed(false);
337 if (is_rt) {
338 if (m_compressed_rt.size() > fldr_nr)
339 if (m_compressed_rt[fldr_nr]) compressed = true;
340 } else {
341 if (m_compressed_t0.size() > fldr_nr)
342 if (m_compressed_t0[fldr_nr]) compressed = true;
343 }
344 cool::RecordSpecification spec;
345 spec.extend("tech", cool::StorageType::Int32);
346 if (compressed) {
347 spec.extend("file", cool::StorageType::String255);
348 spec.extend("data", cool::StorageType::Blob64k);
349 } else {
350 spec.extend("file", cool::StorageType::String4k);
351 if (is_rt) {
352 spec.extend("data", cool::StorageType::String4k);
353 } else {
354 spec.extend("data", cool::StorageType::String16M);
355 }
356 }
357 cool::FolderSpecification fspec(cool::FolderVersioning::MULTI_VERSION, spec);
358 m_db->createFolder(folder, fspec, description, true);
359 return true;
360 }
361
362} // namespace MuonCalib
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
Basic time unit for IOVSvc.
static Double_t t0
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
Basic time unit for IOVSvc.
Definition IOVTime.h:33
static constexpr uint64_t MAXTIMESTAMP
Definition IOVTime.h:58
std::vector< std::string > m_t0_folder
std::vector< std::string > m_tagrt
CoolInserter(const std::string &name, ISvcLocator *pSvcLocator)
ToolHandleArray< IMuonCalibConditionsSource > m_calibration_sources
std::map< bool, coral::AttributeList > m_mdtrt_cool_row
StatusCode finalize()
finalize functions
StatusCode execute()
execute function NOTE: This will read all events at once
bool AppendT0(float t0, int validflag, float adc0)
cool::IDatabasePtr m_db
bool StartT0Chamber(const NtupleStationId &sid)
std::vector< bool > m_compressed_rt
std::vector< cool::IFolderPtr > m_mdtt0_fld
std::map< bool, coral::AttributeList > m_mdtt0_cool_row
std::set< NtupleStationId > m_rt_filled
bool create_folder(const unsigned int &fldr_nr, const bool &is_rt)
StatusCode initialize()
Is called at the beginning of the analysis.
std::set< NtupleStationId > m_t0_filled
bool StoreT0Chamber(const NtupleStationId &id, const std::string &file, unsigned int creation_flags=0)
std::string m_cool_connection_string
NtupleStationId m_sid
std::vector< bool > m_compressed_t0
bool StoreRtChamber(const NtupleStationId &id, const std::map< int, SamplePoint > &points, const std::string &file, unsigned int creation_flags=0)
std::ostringstream m_data_string
cool::Application m_app
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
ServiceHandle< RegionSelectionSvc > m_reg_sel_svc
SG::ReadCondHandleKey< MuonGM::MuonDetectorManager > m_DetectorManagerKey
std::vector< cool::IFolderPtr > m_mdtrt_fld
std::vector< std::string > m_tagt0
std::vector< std::string > m_rt_folder
Implements fixed identifiers not dependent upon Athena Identifier for internal use in the calibration...
Definition MuonFixedId.h:50
Station Identifier for sorting calibration data.
void SetMultilayer(const int &ml)
The MuonDetectorManager stores the transient representation of the Muon Spectrometer geometry and pro...
const_pointer_type cptr()
std::string description
glabal timer - how long have I taken so far?
Definition hcg.cxx:91
CscCalcPed - algorithm that finds the Cathode Strip Chamber pedestals from an RDO.
TFile * file