7 #include "Acts/Utilities/Enumerate.hpp"
16 std::string whiteSpace(
int n,
const char space=
' ') {
18 spaces.assign(
n, space);
21 std::string rangeString(std::vector<ushort>& numbers){
22 std::ranges::sort(numbers);
24 std::stringstream sstr{};
25 ushort low{numbers.front()}, high{numbers.front()};
26 for (
const ushort n : numbers) {
27 if (high ==
n || high+1 ==
n) {
34 }
else if (low + 1!=high){
43 if (low == numbers.back()) {
45 }
else if (low + 1 != high) {
58 MdtCalibJsonDumpAlg::~MdtCalibJsonDumpAlg() =
default;
62 if (m_rtJSON.value().empty() || m_t0JSON.value().empty()) {
63 ATH_MSG_FATAL(
"No output file has been given " << m_rtJSON<<
" "<<m_t0JSON);
64 return StatusCode::FAILURE;
66 if (m_t0JSON.value() == m_rtJSON.value()){
67 ATH_MSG_FATAL(
"You can't dump the two streams into the same JSON "<<m_t0JSON<<
" & "<<m_rtJSON);
68 return StatusCode::FAILURE;
72 ATH_MSG_INFO(
"Dump calibration constants in " << m_rtJSON);
73 return StatusCode::SUCCESS;
78 return StatusCode::SUCCESS;
81 const EventContext &ctx{Gaudi::Hive::currentContext()};
86 ATH_MSG_INFO(
"IOV "<<calibHandle.getRange()<<
" has already been dumped");
87 return StatusCode::SUCCESS;
89 m_seenIDs.emplace_back(calibHandle.getRange());
90 const MdtIdHelper &idHelper{m_idHelperSvc->mdtIdHelper()};
95 unsigned int rtCounter{0}, t0Counter{0};
96 for (
auto itr = idHelper.detectorElement_begin(); itr != idHelper.detectorElement_end(); ++itr) {
98 const MdtFullCalibData *calibData = calibHandle->getCalibData(detId, msgStream());
103 ATH_MSG_VERBOSE(
"Group rt constants of "<<m_idHelperSvc->toStringDetEl(detId));
105 const int ml = idHelper.multilayer(detId);
107 T0PerChamb& chambT0{t0Groups.insert(std::make_pair(idHelper.elementID(detId),
T0PerChamb{sorter})).first->second};
110 for (
int tube = 1;
tube <= idHelper.tubeMax(detId); ++
tube) {
120 chambT0[singleCalib].insert(chId);
125 ATH_CHECK(dumpRtRelations(ctx, rtGroups, calibHandle.getRange()));
126 ATH_CHECK(dumpTubeT0s(ctx, t0Groups, calibHandle.getRange()));
128 unsigned t0GrpCounter{0};
129 for (
const auto&[chId, t0Channels] : t0Groups) {
130 t0GrpCounter+=t0Channels.size();
132 if ((rtCounter == 0) or (t0Counter == 0)){
133 ATH_MSG_ERROR(
"MdtCalibJsonDumpAlg::execute: Counter is zero in numerator");
134 return StatusCode::FAILURE;
136 ATH_MSG_INFO(
std::format(
"Grouped {:d} / {:d} ({:.2f}%) rt relations & {:d}/ {:d} ({:.2f}%) t0 calibration constants",
137 rtGroups.size(), rtCounter, (100.* rtGroups.size() / rtCounter),
138 t0GrpCounter, t0Counter,(100.*t0GrpCounter / t0Counter)));
139 return StatusCode::SUCCESS;
141 StatusCode MdtCalibJsonDumpAlg::dumpRtRelations(
const EventContext& ctx,
const RtGrouper& rtRelMap,
const EventIDRange& eventRange) {
142 std::ofstream outFileRT{
toString(eventRange) + m_rtJSON};
143 if (!outFileRT.good()) {
145 return StatusCode::FAILURE;
148 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
151 outFileRT<<whiteSpace(m_whiteSpace)<<
"{\n";
152 outFileRT<<whiteSpace(m_whiteSpace)<<
" \"chambers\": [\n";
154 const std::set<Identifier>& chambers =
payload.second;
156 for (
const auto [chCounter, chambId] : Acts::enumerate(chambers)) {
157 outFileRT<<whiteSpace(2*m_whiteSpace)<<
"{"
158 <<
std::format(
"\"station\": \"{:}\", \"eta\": {:2d}, \"phi\": {:1d}, \"ml\": {:1d}",
159 idHelper.stationNameString(idHelper.stationName(chambId)), idHelper.stationEta(chambId),
160 idHelper.stationPhi(chambId), idHelper.multilayer(chambId))<<
"}";
162 m_rt_stName.push_back(idHelper.stationNameString(idHelper.stationName(chambId)));
163 m_rt_stEta.push_back(idHelper.stationEta(chambId));
164 m_rt_stPhi.push_back(idHelper.stationPhi(chambId));
165 m_rt_stMl.push_back(idHelper.multilayer(chambId));
167 if (chCounter +1 != chambers.size()) {
174 outFileRT<<whiteSpace(m_whiteSpace+1)<<
"\"rtRelation\": {\n";
175 outFileRT<<whiteSpace(2*m_whiteSpace)<<
"\"type\": \""<<rtRel->
rt()->
name()<<
"\",\n";
176 m_rt_type = rtRel->
rt()->
name();
178 outFileRT<<whiteSpace(2*m_whiteSpace)<<
"\"params\": "<<
dump(rtRel->
rt()->
parameters())<<
"},\n";
180 if (rtRel->
tr()->
name() !=
"TrRelationLookUp") {
181 outFileRT<<whiteSpace(m_whiteSpace+1)<<
"\"trRelation\": {\n";
182 outFileRT<<whiteSpace(2*m_whiteSpace)<<
"\"type\": \""<<rtRel->
tr()->
name()<<
"\",\n";
183 outFileRT<<whiteSpace(2*m_whiteSpace)<<
"\"params\": "<<
dump(rtRel->
tr()->
parameters())<<
"},\n";
184 m_tr_type = rtRel->
tr()->
name();
188 outFileRT<<whiteSpace(m_whiteSpace+1)<<
"\"rtReso\": {\n";
189 outFileRT<<whiteSpace(2*m_whiteSpace)<<
"\"type\": \""<<rtRel->
rtRes()->
name()<<
"\",\n";
190 outFileRT<<whiteSpace(2*m_whiteSpace)<<
"\"params\": "<<
dump(rtRel->
rtRes()->
parameters())<<
"}\n";
193 if (
counter +1 != rtRelMap.size()) {
194 outFileRT<<whiteSpace(m_whiteSpace)<<
"},\n";
196 outFileRT<<whiteSpace(m_whiteSpace)<<
"}\n";
198 m_rt_iov_start = eventRange.start();
199 m_rt_iov_end = eventRange.stop();
203 return StatusCode::SUCCESS;
206 std::stringstream sstr{};
216 std::stringstream sstr{};
217 if (std::abs(
v)<
std::pow(0.1, m_precision + 1)) {
220 sstr<<std::setprecision(m_precision +
std::max(std::floor(std::log10(std::abs(
v))), 1.))<<
v;
224 StatusCode MdtCalibJsonDumpAlg::dumpTubeT0s(
const EventContext& ctx,
const T0Grouper& t0Map,
const EventIDRange& eventRange) {
225 std::ofstream outFileT0{
toString(eventRange) + m_t0JSON};
226 if (!outFileT0.good()) {
228 return StatusCode::FAILURE;
231 const MdtIdHelper& idHelper{m_idHelperSvc->mdtIdHelper()};
232 for (
const auto [
count,
payload] : Acts::enumerate(t0Map)) {
233 outFileT0<<whiteSpace(m_whiteSpace)<<
"{\n";
237 m_t0_stName = idHelper.stationNameString(idHelper.stationName(detId));
238 m_t0_stEta = idHelper.stationEta(detId);
239 m_t0_stPhi = idHelper.stationPhi(detId);
241 outFileT0<<whiteSpace(m_whiteSpace+1)
242 <<
std::format(
"\"station\": \"{:}\", \"eta\": {:2d}, \"phi\": {:1d}, \"calibConstants\":[\n",
243 idHelper.stationNameString(idHelper.stationName(detId)),
244 idHelper.stationEta(detId), idHelper.stationPhi(detId));
245 for (
const auto [calibCounter, calibPayload] : Acts::enumerate(calibConstants)) {
246 outFileT0<<whiteSpace(2*m_whiteSpace)<<
"{\"t0\": "<<
dump(calibPayload.first->t0)<<
", ";
247 outFileT0<<
"\"adc\": "<<
dump(calibPayload.first->adcCal)<<
", ";
248 outFileT0<<
"\"code\": "<<
dump(calibPayload.first->statusCode)<<
", ";
249 outFileT0<<
"\"tubes\": [";
250 Identifier refId = (*calibPayload.second.begin());
252 m_t0_t0= calibPayload.first->t0;
253 m_t0_adc = calibPayload.first->adcCal;
254 m_t0_code = calibPayload.first->statusCode;
255 std::vector<u_short> tubes{};
256 auto dumpTubeLayer = [&tubes, &outFileT0, &idHelper, &refId](
const Identifier& newLayId) {
257 outFileT0<<
std::format(
"{{\"ml\": {:1d}, \"tl\": {:1d}, \"no\": \"{:}\"}}",
258 idHelper.multilayer(refId), idHelper.tubeLayer(refId),
263 for (
const Identifier& tubeId : calibPayload.second){
264 const int ml = idHelper.multilayer(tubeId);
265 const int tl = idHelper.tubeLayer(tubeId);
266 m_t0_multiLayer.push_back(ml);
267 m_t0_tubeLayer.push_back(
tl);
268 m_t0_tube.push_back(idHelper.tube(tubeId));
269 if (
tl != idHelper.tubeLayer(refId) || ml != idHelper.multilayer(refId)) {
270 dumpTubeLayer(tubeId);
273 tubes.push_back(idHelper.tube(tubeId));
275 dumpTubeLayer(refId);
277 if (calibCounter +1 != calibConstants.size()){
280 outFileT0<<
"}]"<<std::endl;
284 if (
count +1 != t0Map.size()){
285 outFileT0<<whiteSpace(m_whiteSpace)<<
"},\n";
287 outFileT0<<whiteSpace(m_whiteSpace)<<
"}"<<std::endl;
289 m_t0_iov_start = eventRange.start();
290 m_t0_iov_end = eventRange.stop();
294 return StatusCode::SUCCESS;
298 std::stringstream sstr{};
300 std::string
outStr{sstr.str()};
302 [](
const unsigned char c){
303 return !std::isalnum(c) && c!=
'-' && c !=
'_';