ATLAS Offline Software
Loading...
Searching...
No Matches
NswCondTestAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "NswCondTestAlg.h"
6
7// STL
8#include <stdio.h>
9
10#include <chrono>
11#include <ctime>
12#include <iostream>
13#include <sstream>
14#include <fstream>
15
16
17
18// Gaudi and Athena
20#include "CoralBase/Blob.h"
22#include "GaudiKernel/StatusCode.h"
23#include "Identifier/Identifier.h"
24
25// constructor
26NswCondTestAlg::NswCondTestAlg(const std::string& name, ISvcLocator* pSvcLocator) : AthReentrantAlgorithm(name, pSvcLocator) {}
27
28// destructor
30
31// initialize
33 ATH_MSG_INFO("Calling initialize");
34 ATH_CHECK(m_readKey_tdopdo.initialize());
35 ATH_CHECK(m_readKey_vmm.initialize(m_isMC));
36 ATH_CHECK(m_readKey_t0.initialize(!m_isMC));
37 ATH_CHECK(m_idHelperSvc.retrieve());
38 return StatusCode::SUCCESS;
39}
40
41// execute
42StatusCode NswCondTestAlg::execute(const EventContext& ctx) const {
43 ATH_MSG_INFO("Calling execute");
44
45 // setup parameters
46 std::chrono::duration<double> retrieving_MM_TDO_A{};
47 std::chrono::duration<double> retrieving_MM_TDO_C{};
48 std::chrono::duration<double> retrieving_MM_PDO_A{};
49 std::chrono::duration<double> retrieving_MM_PDO_C{};
50 std::chrono::duration<double> retrieving_MM_VMM_A{};
51 std::chrono::duration<double> retrieving_MM_VMM_C{};
52 std::chrono::duration<double> retrieving_STGC_TDO_A{};
53 std::chrono::duration<double> retrieving_STGC_TDO_C{};
54 std::chrono::duration<double> retrieving_STGC_PDO_A{};
55 std::chrono::duration<double> retrieving_STGC_PDO_C{};
56 std::chrono::duration<double> retrieving_STGC_VMM_A{};
57 std::chrono::duration<double> retrieving_STGC_VMM_C{};
58 std::chrono::duration<double> retrieving_T0{};
59
60 // retrieve all folders
61 ATH_CHECK(retrieveTdoPdo(ctx, TimeChargeType::PDO, "MM", "A", retrieving_MM_PDO_A));
62 ATH_CHECK(retrieveTdoPdo(ctx, TimeChargeType::PDO, "MM", "C", retrieving_MM_PDO_C));
63 ATH_CHECK(retrieveTdoPdo(ctx, TimeChargeType::TDO, "MM", "A", retrieving_MM_TDO_A));
64 ATH_CHECK(retrieveTdoPdo(ctx, TimeChargeType::TDO, "MM", "C", retrieving_MM_TDO_C));
65 if(m_isMC) ATH_CHECK(retrieveVmm(ctx, "MM", "A", retrieving_MM_VMM_A));
66 if(m_isMC) ATH_CHECK(retrieveVmm(ctx, "MM", "C", retrieving_MM_VMM_C));
67 ATH_CHECK(retrieveTdoPdo(ctx, TimeChargeType::TDO, "STGC", "A", retrieving_STGC_TDO_A));
68 ATH_CHECK(retrieveTdoPdo(ctx, TimeChargeType::TDO, "STGC", "C", retrieving_STGC_TDO_C));
69 ATH_CHECK(retrieveTdoPdo(ctx, TimeChargeType::PDO, "STGC", "A", retrieving_STGC_PDO_A));
70 ATH_CHECK(retrieveTdoPdo(ctx, TimeChargeType::PDO, "STGC", "C", retrieving_STGC_PDO_C));
71 if(m_isMC) ATH_CHECK(retrieveVmm(ctx, "STGC", "A", retrieving_STGC_VMM_A));
72 if(m_isMC) ATH_CHECK(retrieveVmm(ctx, "STGC", "C", retrieving_STGC_VMM_C));
73 if(!m_isMC) ATH_CHECK(retrieveT0(ctx,retrieving_T0));
74
75 // postprocess
76 ATH_MSG_INFO("Retrieving time for (MM , TDO, Side A) = "
77 << (std::chrono::duration_cast<std::chrono::microseconds>(retrieving_MM_TDO_A).count() * 1.0) << "s ");
78 ATH_MSG_INFO("Retrieving time for (MM , TDO, Side C) = "
79 << (std::chrono::duration_cast<std::chrono::microseconds>(retrieving_MM_TDO_C).count() * 1.0) << "s ");
80 ATH_MSG_INFO("Retrieving time for (MM , PDO, Side A) = "
81 << (std::chrono::duration_cast<std::chrono::microseconds>(retrieving_MM_PDO_A).count() * 1.0) << "s ");
82 ATH_MSG_INFO("Retrieving time for (MM , PDO, Side C) = "
83 << (std::chrono::duration_cast<std::chrono::microseconds>(retrieving_MM_PDO_C).count() * 1.0) << "s ");
84 if(m_isMC){
85 ATH_MSG_INFO("Retrieving time for (MM , VMM, Side A) = "
86 << (std::chrono::duration_cast<std::chrono::microseconds>(retrieving_MM_VMM_A).count() * 1.0) << "s ");
87 ATH_MSG_INFO("Retrieving time for (MM , VMM, Side C) = "
88 << (std::chrono::duration_cast<std::chrono::microseconds>(retrieving_MM_VMM_C).count() * 1.0) << "s ");
89 }
90
91 ATH_MSG_INFO("Retrieving time for (STGC, TDO, Side A) = "
92 << (std::chrono::duration_cast<std::chrono::microseconds>(retrieving_STGC_TDO_A).count() * 1.0) << "s ");
93 ATH_MSG_INFO("Retrieving time for (STGC, TDO, Side C) = "
94 << (std::chrono::duration_cast<std::chrono::microseconds>(retrieving_STGC_TDO_C).count() * 1.0) << "s ");
95 ATH_MSG_INFO("Retrieving time for (STGC, PDO, Side A) = "
96 << (std::chrono::duration_cast<std::chrono::microseconds>(retrieving_STGC_PDO_A).count() * 1.0) << "s ");
97 ATH_MSG_INFO("Retrieving time for (STGC, PDO, Side C) = "
98 << (std::chrono::duration_cast<std::chrono::microseconds>(retrieving_STGC_PDO_C).count() * 1.0) << "s ");
99 if(m_isMC) {
100 ATH_MSG_INFO("Retrieving time for (STGC, VMM, Side A) = "
101 << (std::chrono::duration_cast<std::chrono::microseconds>(retrieving_STGC_VMM_A).count() * 1.0) << "s ");
102 ATH_MSG_INFO("Retrieving time for (STGC, VMM, Side C) = "
103 << (std::chrono::duration_cast<std::chrono::microseconds>(retrieving_STGC_VMM_C).count() * 1.0) << "s ");
104 }
105
106 if(!m_isMC){
107 ATH_MSG_INFO("retrieving time for T0 for NSW = "
108 << (std::chrono::duration_cast<std::chrono::microseconds>(retrieving_T0).count() * 1.0) << "s ");
109 }
110
111 ATH_MSG_INFO("MADE IT TO THE END!!");
112 return StatusCode::SUCCESS;
113}
114
115// retrieveTdoPdo
116StatusCode NswCondTestAlg::retrieveTdoPdo(const EventContext& ctx, TimeChargeType data, const std::string& tech,
117 const std::string& side, std::chrono::duration<double>& timer) const {
118 ATH_MSG_INFO("Starting with " << " data for " << tech << " and " << side << " at " << timestamp());
119 auto start1 = std::chrono::high_resolution_clock::now();
120
121 // Start with an infinte range and narrow it down as needed
122 EventIDRange rangeW = IOVInfiniteRange::infiniteMixed();
123
124 // Retrieve Data Object
126 const NswCalibDbTimeChargeData* readCdo{*readHandle};
127 if (!readCdo) {
128 ATH_MSG_ERROR("Null pointer to the read conditions object");
129 return StatusCode::FAILURE;
130 }
131
132 EventIDRange range;
133 if (!readHandle.range(range)) {
134 ATH_MSG_ERROR("Failed to retrieve validity range for " << readHandle.key());
135 return StatusCode::FAILURE;
136 }
137
138 // Intersect validity range of this obj with the validity of already-loaded objs
139 rangeW = EventIDRange::intersect(range, rangeW);
140
141 // retrieve all channels
142 std::vector<Identifier> channelIds = readCdo->getChannelIds(data, tech, side);
143 ATH_MSG_INFO("Found data for " << channelIds.size() << " channels!");
144
145 // retrieve data for the first channel
146 std::stringstream sstr{};
147 if (!channelIds.empty()) {
148 const Identifier& channel = channelIds[0];
149
150 const NswCalibDbTimeChargeData::CalibConstants& calib_data = *readCdo->getCalibForChannel(data, channel);
151 ATH_MSG_INFO("Checking channel 0 (Id = " << channel.get_compact() << ") "<<calib_data);
152 if (!m_logName.empty()){
153 for (const Identifier& chan_id : channelIds) {
154 const NswCalibDbTimeChargeData::CalibConstants& calib_data = *readCdo->getCalibForChannel(data, chan_id);
155 sstr<<m_idHelperSvc->toString(chan_id)<<" "<<calib_data<<std::endl;
156 }
157 }
158 } else if (!m_logName.empty()) {
159 const NswCalibDbTimeChargeData::CalibConstants& calib_data = *readCdo->getZeroCalibChannel(data, tech == "MM" ? TimeTech::MM : TimeTech::STGC);
160 sstr<<"Dummy calib channel "<<calib_data<<std::endl;
161 }
162
163 if (!m_logName.empty()){
164 std::ofstream ostr{m_logName+"_"+ (data == TimeChargeType::TDO ? "TDO" : "PDO") + "_"+tech+side+".txt"};
165 ostr<<sstr.str()<<std::endl;
166 }
167
168 ATH_MSG_ALWAYS(sstr.str());
169
170 auto end1 = std::chrono::high_resolution_clock::now();
171 timer += end1 - start1;
172 ATH_MSG_INFO("Ending at " << timestamp());
173 return StatusCode::SUCCESS;
174}
175
176// retrieveVmm
177StatusCode NswCondTestAlg::retrieveVmm(const EventContext& ctx, const std::string& tech, const std::string& side,
178 std::chrono::duration<double>& timer) const {
179 ATH_MSG_INFO("Starting with VMM data for " << tech << " and " << side << " at " << timestamp());
180 auto start1 = std::chrono::high_resolution_clock::now();
181
182 // Start with an infinte range and narrow it down as needed
183 EventIDRange rangeW = IOVInfiniteRange::infiniteMixed();
184
185 // Retrieve Data Object
187 const NswCalibDbThresholdData* readCdo{*readHandle};
188 if (!readCdo) {
189 ATH_MSG_ERROR("Null pointer to the read conditions object");
190 return StatusCode::FAILURE;
191 }
192
193 EventIDRange range;
194 if (!readHandle.range(range)) {
195 ATH_MSG_ERROR("Failed to retrieve validity range for " << readHandle.key());
196 return StatusCode::FAILURE;
197 }
198
199 // Intersect validity range of this obj with the validity of already-loaded objs
200 rangeW = EventIDRange::intersect(range, rangeW);
201
202 // retrieve all channels
203 std::vector<Identifier> channelIds = readCdo->getChannelIds(tech, side);
204 ATH_MSG_INFO("Found data for " << channelIds.size() << " channels!");
205
206 // retrieve data for the first channel
207 std::stringstream sstr {};
208 if (!channelIds.empty()) {
209 const Identifier& channel = channelIds[0];
210 float threshold{0.};
211 readCdo->getThreshold(channel, threshold);
212 ATH_MSG_INFO("Checking channel 0 (Id = " << m_idHelperSvc->toString(channel)<< ") threshold "<< threshold);
213 if (!m_logName.empty()){
214 for (const Identifier& id : channelIds){
215 sstr<<m_idHelperSvc->toString(id)<<" threshold "<<threshold<<std::endl;
216 }
217 }
218 }
219 if (!m_logName.empty()){
220 std::ofstream ostr{m_logName+"_THRESH_"+tech+side+".txt"};
221 ostr<<sstr.str()<<std::endl;
222 }
223
224 auto end1 = std::chrono::high_resolution_clock::now();
225 timer += end1 - start1;
226 ATH_MSG_INFO("Ending at " << timestamp());
227 return StatusCode::SUCCESS;
228}
229
230// retrieve T0
231StatusCode NswCondTestAlg::retrieveT0(const EventContext& ctx,
232 std::chrono::duration<double>& timer) const {
233 ATH_MSG_INFO("Starting to retrieve T0 values at " << timestamp());
234 auto start1 = std::chrono::high_resolution_clock::now();
235
236 // Start with an infinte range and narrow it down as needed
237 EventIDRange rangeW = IOVInfiniteRange::infiniteMixed();
238
239 // Retrieve Data Object
241 const NswT0Data* readCdo{*readHandle};
242 if (!readCdo) {
243 ATH_MSG_ERROR("Null pointer to the read conditions object");
244 return StatusCode::FAILURE;
245 }
246
247 EventIDRange range;
248 if (!readHandle.range(range)) {
249 ATH_MSG_ERROR("Failed to retrieve validity range for " << readHandle.key());
250 return StatusCode::FAILURE;
251 }
252
253 // Intersect validity range of this obj with the validity of already-loaded objs
254 rangeW = EventIDRange::intersect(range, rangeW);
255
256 std::stringstream sstr {};
257 const MmIdHelper& id_helper = m_idHelperSvc->mmIdHelper();
258 auto it = id_helper.detectorElement_begin();
259 for(; it!= id_helper.detectorElement_end(); it++){
260 for(int i_layer=id_helper.gasGapMin(*it); i_layer <= id_helper.gasGapMax(*it); i_layer++){
261 Identifier layerId = id_helper.channelID(*it,id_helper.multilayer(*it), i_layer,1);
262 for(int i_channel=id_helper.channelMin(layerId); i_channel < id_helper.channelMax(layerId); i_channel++){
263 Identifier channelId = id_helper.channelID(layerId,id_helper.multilayer(*it), i_layer, i_channel);
264 float t0{0};
265 if(!readCdo->getT0(channelId, t0)){
266 ATH_MSG_ERROR("Failed to retrieve t0 calibration for channel"<< m_idHelperSvc->toString(channelId));
267 return StatusCode::FAILURE;
268 }
269 if (!m_logName.empty()){
270 sstr<<m_idHelperSvc->toString(channelId)<<" T0 "<< t0 <<std::endl;
271 }
272 }
273 }
274 }
275
276 if (!m_logName.empty()){
277 std::ofstream ostr{m_logName+"_T0.txt"};
278 ostr<<sstr.str()<<std::endl;
279 }
280
281 auto end1 = std::chrono::high_resolution_clock::now();
282 timer += end1 - start1;
283 ATH_MSG_INFO("Ending at " << timestamp());
284 return StatusCode::SUCCESS;
285}
286
287std::string NswCondTestAlg::timestamp() const {
288 auto now = std::chrono::system_clock::now();
289 std::time_t time_now = std::chrono::system_clock::to_time_t(now);
290 std::tm local_tm;
291 localtime_r(&time_now, &local_tm);
292 auto duration_since_epoch = now.time_since_epoch();
293 auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(duration_since_epoch) % 1000;
294 std::ostringstream oss;
295 oss << std::put_time(&local_tm, "%H:%M:%S") << '.' << std::setw(3) << std::setfill('0') << milliseconds.count();
296 return oss.str();
297}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_ALWAYS(x)
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
An algorithm that can be simultaneously executed in multiple threads.
static EventIDRange infiniteMixed()
Produces an mixed EventIDRange that is infinite in Time and RunLumi.
Identifier channelID(int stationName, int stationEta, int stationPhi, int multilayer, int gasGap, int channel) const
static int gasGapMax()
static int channelMin()
static int channelMax()
static int gasGapMin()
int multilayer(const Identifier &id) const
const_id_iterator detectorElement_begin() const
Iterators over full set of ids.
const_id_iterator detectorElement_end() const
std::vector< Identifier > getChannelIds(const std::string="", const std::string="") const
bool getThreshold(const Identifier &, float &) const
const CalibConstants * getCalibForChannel(const CalibDataType type, const Identifier &channelId) const
Retrieves the calibration constant for a particular readout channel.
const CalibConstants * getZeroCalibChannel(const CalibDataType type, const MuonCond::CalibTechType tech) const
Returns the dummy calibration constant for the given technology type.
std::vector< Identifier > getChannelIds(const CalibDataType type, const std::string &tech, const std::string &side) const
StatusCode retrieveT0(const EventContext &, std::chrono::duration< double > &) const
virtual StatusCode initialize() override
SG::ReadCondHandleKey< NswCalibDbThresholdData > m_readKey_vmm
virtual ~NswCondTestAlg() override
Gaudi::Property< bool > m_isMC
StatusCode retrieveVmm(const EventContext &, const std::string &, const std::string &, std::chrono::duration< double > &) const
SG::ReadCondHandleKey< NswT0Data > m_readKey_t0
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
StatusCode retrieveTdoPdo(const EventContext &ctx, TimeChargeType data, const std::string &tech, const std::string &side, std::chrono::duration< double > &timer) const
std::string timestamp() const
virtual StatusCode execute(const EventContext &) const override
NswCalibDbTimeChargeData::CalibDataType TimeChargeType
NswCondTestAlg(const std::string &name, ISvcLocator *pSvcLocator)
Gaudi::Property< std::string > m_logName
SG::ReadCondHandleKey< NswCalibDbTimeChargeData > m_readKey_tdopdo
bool getT0(const Identifier &channelId, float &channelT0) const
Definition NswT0Data.cxx:58
bool range(EventIDRange &r)
const std::string & key() const
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
Helper struct to cache all calibration constants in a common place of the memory.