BmnRoot
Loading...
Searching...
No Matches
BmnConverterTools.cxx
Go to the documentation of this file.
1#include "BmnConverterTools.h"
2
3// STL
4#include <chrono>
5#include <regex>
6// ROOT
7#include <TPRegexp.h>
8// BmnRoot
9#include "BmnFunctionSet.h"
10#include "BmnMath.h"
12
13// using std::string;
14// using std::make_pair;
15using namespace std::chrono_literals;
16using namespace std::chrono;
17
18BmnConverterTools::T0Config BmnConverterTools::GetT0Cfg(string str)
19{
20 if (str.rfind("Comment", 0) == 0)
21 return T0Config::Comment;
22 if (str.rfind("Discriminators", 0) == 0)
23 return T0Config::Discriminators;
24 if (str.rfind("Delay lines", 0) == 0)
25 return T0Config::DelayLines;
26 if (str.rfind("Switches", 0) == 0)
27 return T0Config::Switches;
28 if (str.rfind("LV", 0) == 0)
29 return T0Config::LV;
30 if (str.rfind("BD_multiplicity", 0) == 0)
31 return T0Config::BDMult;
32 if (str.rfind("Extra delays mask", 0) == 0)
33 return T0Config::ExtraDelaysMask;
34 return T0Config::Undefined;
35}
36
38{
39 UInt_t nans = static_cast<UInt_t>(jt["ns"]);
40 UInt_t sec = static_cast<UInt_t>(jt["sec"]) - tai_utc_dif;
41 bool valid = static_cast<UInt_t>(jt["valid"]);
42 return valid ? SysPoint{sec * 1s + nans * 1ns} : SysPoint::min();
43}
44
46{
48 try {
49 regex rempty("\\s+\n*");
50 istringstream iss(str);
51 string line;
52 bool main_found = false;
53 bool cfg_found = false;
54 while (std::getline(iss, line)) {
55 if (regex_match(line, rempty)) {
56 if (main_found) {
57 cfg_found = true;
58 break;
59 }
60 } else {
61 smatch sm;
62 regex rSpillTime("(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})\\.BMNCfg\n*");
63 if (regex_search(line, sm, rSpillTime)) {
64 if (sm.size() > 6) {
65 int32_t year = std::stoi(sm[1]);
66 int32_t month = std::stoi(sm[2]);
67 int32_t day = std::stoi(sm[3]);
68 int32_t hour = std::stoi(sm[4]);
69 int32_t min = std::stoi(sm[5]);
70 int32_t sec = std::stoi(sm[6]);
71 TTimeStamp ts(year, month, day, hour, min, sec);
72 LOGF(debug, "ts cfg: %s", ts.AsString());
73 // rt0dig->SetTS(ts);
74 }
75 }
76 main_found = true;
77 }
78 }
79 if (!cfg_found)
80 return kBMNERROR;
82 for (const auto& n : node.children) {
83 auto& ch = n.children;
84 switch (GetT0Cfg(n.value)) {
85 case T0Config::Comment: {
86 if (ch.size() > 0)
87 rt0dig->SetComment(ch[0].value);
88 break;
89 }
90 case T0Config::Discriminators: {
91 regex re("Discriminator(\\d+)\\r*");
92 for (const auto& node_par : ch) {
93 string snum = regex_replace(node_par.value, re, "$1");
94 uint32_t idis = std::stoul(snum.data());
95 auto& dvec = rt0dig->Discriminators();
96 /* if (dvec.size() < (idis + 1))
97 dvec.resize(idis + 1); */
98 auto& disc = dvec[idis];
99 for (const auto& node_chan : node_par.children) {
100 if (node_chan.value.rfind("Polarity", 0) == 0) {
101 if (node_chan.children.size() > 0) {
102 const auto& str_field = node_chan.children[0].value;
103 if (str_field.rfind("Positive", 0) == 0)
104 disc.polarity = 1;
105 else
106 disc.polarity = -1;
107 }
108 continue;
109 }
110 regex rthr("Threshold,\\s+(\\w+)\\r*");
111 if (regex_match(node_chan.value, rthr)) {
112 string unit = regex_replace(node_chan.value, rthr, "$1");
113 if (node_chan.children.size() > 0) {
114 const auto& str_field = node_chan.children[0].value;
115 disc.threshold = std::atoi(str_field.data());
116 disc.threshold_unit = move(unit);
117 }
118 continue;
119 }
120 }
121 // disc.print();
122 }
123 } break;
124 case T0Config::DelayLines: {
125 regex re("Delay(\\d+)\\r*");
126 for (const auto& node_par : ch) {
127 string snum = regex_replace(node_par.value, re, "$1");
128 uint32_t idis = std::stoul(snum.data());
129 auto& delvec = rt0dig->DelayLines();
130 /* if (delvec.size() < (idis + 1))
131 delvec.resize(idis + 1); */
132 if (node_par.children.size() > 0) {
133 const auto& str_field = node_par.children[0].value;
134 delvec[idis] = std::atoi(str_field.data());
135 }
136 }
137 } break;
138 case T0Config::Switches: {
139 regex re("Switch(\\d+)\\r*");
140 for (const auto& node_par : ch) {
141 string snum = regex_replace(node_par.value, re, "$1");
142 uint32_t idis = std::stoul(snum.data());
143 auto& swvec = rt0dig->Switches();
144 /* if (swvec.size() < (idis + 1))
145 swvec.resize(idis + 1); */
146 if (node_par.children.size() > 0) {
147 const auto& str_field = node_par.children[0].value;
148 swvec[idis] = (str_field.rfind("On", 0) == 0) ? true : false;
149 }
150 }
151 } break;
152 case T0Config::LV: {
153 regex re("Channel(\\d+)\\r*");
154 for (const auto& node_par : ch) {
155 string snum = regex_replace(node_par.value, re, "$1");
156 uint32_t idis = std::stoul(snum.data());
157 auto& dvec = rt0dig->LVChannelConfig();
158 /* if (dvec.size() < (idis + 1))
159 dvec.resize(idis + 1); */
160 auto& cfg = dvec[idis];
161 for (const auto& node_chan : node_par.children) {
162 if (node_chan.value.rfind("Active", 0) == 0) {
163 if (node_chan.children.size() > 0) {
164 const auto& str_field = node_chan.children[0].value;
165 cfg.active = (str_field.rfind("1", 0) == 0) ? true : false;
166 }
167 continue;
168 }
169 if (node_chan.value.rfind("Channel name", 0) == 0) {
170 if (node_chan.children.size() > 0)
171 cfg.name = node_chan.children[0].value;
172 continue;
173 }
174 if (node_chan.value.rfind("V_P1", 0) == 0) {
175 if (node_chan.children.size() > 0) {
176 const auto& str_field = node_chan.children[0].value;
177 cfg.V_P1 = std::atoi(str_field.data());
178 }
179 continue;
180 }
181 if (node_chan.value.rfind("V_P2", 0) == 0) {
182 if (node_chan.children.size() > 0) {
183 const auto& str_field = node_chan.children[0].value;
184 cfg.V_P1 = std::atoi(str_field.data());
185 }
186 continue;
187 }
188 if (node_chan.value.rfind("State", 0) == 0) {
189 if (node_chan.children.size() > 0) {
190 const auto& str_field = node_chan.children[0].value;
191 cfg.state = (str_field.rfind("On", 0) == 0) ? true : false;
192 }
193 continue;
194 }
195 }
196 // cfg.print();
197 }
198 } break;
199 case T0Config::BDMult: {
200 regex re("BDM_(\\w+)\\r*");
201 for (const auto& node_par : ch) {
202 string spar = regex_replace(node_par.value, re, "$1");
203 if (node_par.children.size() > 0) {
204 const auto& str_field = node_par.children[0].value;
205 uint32_t thr = std::atoi(str_field.data());
206 if (spar == "H")
207 rt0dig->SetBDMultH(thr);
208 if (spar == "L")
209 rt0dig->SetBDMultL(thr);
210 // LOGF(info, "BDMH %u BDML %u ", rt0dig->GetBDMultH(),
211 // rt0dig->GetBDMultL());
212 }
213 }
214 } break;
215 case T0Config::ExtraDelaysMask: {
216 regex re("\\$([0-9a-fA-F]+)\\r*");
217 for (const auto& node_par : ch) {
218 string snum = regex_replace(node_par.value, re, "$1");
219 uint32_t num = std::stoul(snum.data(), nullptr, 16);
220 // LOGF(info, " num %X", num);
221 rt0dig->SetExtraDelaysMask(num);
222 }
223 } break;
224 default:
225 break;
226 }
227 }
228 } catch (const std::exception& e) {
229 LOG(error) << "Error: " << e.what();
230 return kBMNERROR;
231 }
232 return kBMNSUCCESS;
233}
234
236{
237 SpillStatus ss{};
238 auto& j_runtime = jv["runTime"];
239 auto& j_spill = j_runtime["spill"];
240 ss.phase = static_cast<UInt_t>(j_spill["phase"]);
241 ss.cur_spill_time_ms = static_cast<UInt_t>(j_spill["curSpillTimeMs"]);
242 ss.spill_cnt = static_cast<UInt_t>(j_spill["count"]);
243 auto j_device_time = j_runtime["time"];
244 ss.app_ts = SysClock::from_time_t(static_cast<UInt_t>(j_device_time["app"]));
245 auto j_device_time_WR = j_device_time["WR"];
246 ss.device_ts = ParseTAI(j_device_time_WR, tai_utc_dif);
247 auto j_spill_start = j_spill["spillStartTaiWrStat"];
248 ss.start_ts = ParseTAI(j_spill_start, tai_utc_dif);
249 auto j_spill_stop = j_spill["spillStopTaiWrStat"];
250 ss.stop_ts = ParseTAI(j_spill_stop, tai_utc_dif);
251 ss.CheckTimes();
252 auto& j_counters = j_runtime["counters"];
253 if (j_counters.contains("input_afterReduc")) {
254 {
255 auto& j_cnt_input = j_counters["input"];
256 UInt_t size_input = j_cnt_input.size();
257 ss.counters.cntr_input.resize(size_input);
258 size_t iInput = 0;
259 for (auto& [key, val] : j_cnt_input.items()) {
260 string name = static_cast<string>(key);
261 ss.counters.cntr_input[iInput].first = move(name);
262 size_t size = min(val.size(), SpillStatus::counters_cnt);
263 for (size_t i = 0; i < size; i++)
264 ss.counters.cntr_input[iInput].second[i] = static_cast<UInt_t>(val[i]);
265 iInput++;
266 }
267 }
268 {
269 auto& j_cnt_input = j_counters["input_afterReduc"];
270 UInt_t size_input = j_cnt_input.size();
271 ss.counters.cntr_input_after_red.resize(size_input);
272 size_t iInput = 0;
273 for (auto& [key, val] : j_cnt_input.items()) {
274 string name = static_cast<string>(key);
275 ss.counters.cntr_input_after_red[iInput].first = move(name);
276 size_t size = min(val.size(), SpillStatus::counters_cnt);
277 for (size_t i = 0; i < size; i++)
278 ss.counters.cntr_input_after_red[iInput].second[i] = static_cast<UInt_t>(val[i]);
279 iInput++;
280 }
281 }
282 } else { // crutch for runs before #7081
283 {
284 auto& j_cnt_input = j_counters["input"];
285 size_t size = min(j_cnt_input.size(), SpillStatus::counters_cnt);
286 ss.counters.cntr_input.resize(1);
287 ss.counters.cntr_input[0].first = "7: ~xOff & Run & Spill";
288 for (size_t i = 0; i < size; i++)
289 ss.counters.cntr_input[0].second[i] = static_cast<UInt_t>(j_cnt_input[i]);
290 }
291 {
292 auto& j_cnt_input = j_counters["after_reduc"];
293 size_t size = min(j_cnt_input.size(), SpillStatus::counters_cnt);
294 ss.counters.cntr_input_after_red.resize(1);
295 ss.counters.cntr_input_after_red[0].first = "7: ~xOff & Run & Spill";
296 for (size_t i = 0; i < size; i++)
297 ss.counters.cntr_input_after_red[0].second[i] = static_cast<UInt_t>(j_cnt_input[i]);
298 }
299 }
300 {
301 auto& j_cnt_ae = j_counters["after_err"];
302 size_t size = min(j_cnt_ae.size(), SpillStatus::counters_cnt);
303 for (size_t i = 0; i < size; i++)
304 ss.counters.cntr_after_err[i] = static_cast<UInt_t>(j_cnt_ae[i]);
305 }
306 {
307 auto& j_cnt_ae = j_counters["befor_err"];
308 size_t size = min(j_cnt_ae.size(), SpillStatus::counters_cnt);
309 for (size_t i = 0; i < size; i++)
310 ss.counters.cntr_befor_err[i] = static_cast<UInt_t>(j_cnt_ae[i]);
311 }
312 LOG(debug) << BmnFunctionSet::TimePoint2String(ss.start_ts) << " start";
313 LOG(debug) << BmnFunctionSet::TimePoint2String(ss.stop_ts) << " stop";
314 LOG(debug) << BmnFunctionSet::TimePoint2String(ss.device_ts) << " dev ts WR";
315 LOG(debug) << BmnFunctionSet::TimePoint2String(ss.app_ts) << " app ts";
316 return ss;
317}
318
320{
321 // Phys trig config
322 auto ph_trig_setups = j["programs"]["Trc#T0"]["knownSetups"];
323 if (ph_trig_setups.size()) {
324 auto ph_trig = ph_trig_setups.begin().value()["trc"]["phyTrig"];
325 UInt_t match_win = ph_trig["matchWin"];
326 UInt_t trig_delay = ph_trig["trigDelay"];
327 UInt_t read_chan_mask = ph_trig["readChMask"];
328 UInt_t start_chan_mask = ph_trig["startChMask"];
329 UInt_t calib_cnt = ph_trig["calibTrigger"]["count"];
330 UInt_t calib_en = (bool)ph_trig["calibTrigger"]["en"];
331 trig_conf.SetMatchWindow(match_win * TRC_CLOCK_NS);
332 trig_conf.SetTrigDelay(trig_delay * TRC_CLOCK_NS);
333 trig_conf.SetCalibCount(calib_cnt);
334 trig_conf.SetCalibEn(calib_en);
335 trig_conf.SetStartChannelMask(start_chan_mask);
336 trig_conf.SetReadChannelMask(read_chan_mask);
337 LOGF(debug, "start_chan_mask %u", start_chan_mask);
338 LOGF(debug, "read_chan_mask %u", read_chan_mask);
339 auto ch_conf = ph_trig["chCfg"];
340 unordered_map<string, BmnTrigChannelConfig>& tcc = trig_conf.TrigChannelConfig();
341 for (auto& trig_chan_conf : ch_conf) {
342 LOG(debug2) << trig_chan_conf.dump();
343 string name = trig_chan_conf["name"];
344 if (name.length() == 0)
345 continue;
347 tcc_el.after_protect_en = (bool)trig_chan_conf["reg"]["after_protect_en"].get<int>();
348 uint32_t ap_clocks = trig_chan_conf["reg"]["after_protect_time"];
349 tcc_el.after_protect_time = ap_clocks * TRC_CLOCK_NS;
350 tcc_el.before_protect_en = (bool)trig_chan_conf["reg"]["before_protect_en"].get<int>();
351 uint32_t bp_clocks = trig_chan_conf["reg"]["before_protect_time"];
352 tcc_el.before_protect_time = bp_clocks * TRC_CLOCK_NS;
353 tcc_el.delay = trig_chan_conf["reg"]["delay"];
354 tcc_el.reduction_en = (bool)trig_chan_conf["reg"]["reduction_en"].get<int>();
355 tcc_el.reduction_factor = trig_chan_conf["reg"]["reduction_factor"];
356 tcc.insert(make_pair(name, tcc_el));
357 }
358 return kBMNSUCCESS;
359 } else
360 return kBMNERROR;
361}
362
363BmnStatus BmnConverterTools::ParseComplexTLV(UInt_t* d, UInt_t& len, UInt_t& runId)
364{
365 uint16_t iWord = 0;
366 while (iWord < len / kNBYTESINWORD) {
367 UInt_t word = d[iWord++];
368 UInt_t idLen = 0;
369 switch (word) {
371 idLen = d[iWord++];
372 if (idLen != kNBYTESINWORD) {
373 LOGF(error, "Wrong RunId length %u", idLen);
374 return kBMNERROR;
375 }
376 runId = d[iWord];
377 LOGF(info, "\tRun Id: %u", runId);
378 break;
379 case RECORD_EVENT_ORDER: {
380 idLen = d[iWord++];
381 if (idLen != kNBYTESINWORD) {
382 LOGF(error, "Wrong event order length %u", idLen);
383 return kBMNERROR;
384 }
385 UInt_t event_order = d[iWord];
386 LOGF(info, "\tEvent Order: %u", event_order);
387 break;
388 }
389 case RECORD_FILE_ID: {
390 idLen = d[iWord++];
391 if (idLen != kNBYTESINWORD) {
392 LOGF(error, "Wrong FileId length %u", idLen);
393 return kBMNERROR;
394 }
395 UInt_t file_id = d[iWord];
396 LOGF(info, "\tFile Id: %u", file_id);
397 break;
398 }
399 case RECORD_RUN_INDEX: {
400 idLen = d[iWord++];
401 if (idLen + iWord * kNBYTESINWORD > len) {
402 LOGF(error, "Wrong RunIndex length %u", idLen);
403 return kBMNERROR;
404 }
405 // printf("idLen %u\n", idLen);
406 TString runIndex(reinterpret_cast<const char*>(d + iWord), idLen);
407 LOGF(info, "\tRun Index: %s", runIndex.Data());
408 break;
409 }
410 default:
411 LOGF(warning, "Unknown record %08X", word);
412 break;
413 }
414 iWord += idLen * kNBYTESINWORD;
415 }
416 return kBMNSUCCESS;
417}
418
420{
421 TString str(name);
422 TPRegexp re_data(".+_(\\d+)([^/]*)\\.data");
423 TPRegexp re_raw(".+_(\\d+)([^/]*)_raw\\.root");
424 // TPRegexp re_data(".*\\w+_\\w+_\\w+_(\\d+)([^/]*)\\.data");
425 // TPRegexp re_raw(".*\\w+_run(\\d+)([^/]*)_raw\\.root");
426 if (re_data.MatchB(str))
427 re_data.Substitute(str, "$2");
428 else {
429 if (re_raw.MatchB(str))
430 re_raw.Substitute(str, "$2");
431 else
432 return "";
433 }
434 return str;
435}
436
438{
439 string ss(s.Data());
440 return ParseRawFileName(ss, prop);
441}
443{
444 int32_t lastSlash = path.rfind('/');
445 string name(path.substr(lastSlash + 1, path.length() - lastSlash));
446 LOGF(debug, "name: %s", name.data());
447 regex re6("(\\w+)_(\\w+)_(\\w+)_(\\d+)_ev(\\d+)_p(\\d+)\\.(\\w+)\\r*");
448 regex re5("(\\w+)_(\\w+)_(\\w+)_(\\d+)_ev(\\d+)(_p\\d+){0,1}\\.(\\w+)\\r*");
449 if (!regex_match(name, re5)) {
450 LOGF(error, "regex doesn't match: %s", name.data());
451 return kBMNERROR;
452 }
453 string str_exp = regex_replace(name, re5, "$1");
454 string str_run = regex_replace(name, re5, "$2");
455 string str_run_in = regex_replace(name, re5, "$3");
456 string str_run_id = regex_replace(name, re5, "$4");
457 string str_ev_ord = regex_replace(name, re5, "$5");
458 uint32_t run_id = std::stoul(str_run_id.data());
459 uint32_t ev_ord = std::stoul(str_ev_ord.data());
460
461 prop.SetRunId(run_id);
462 prop.SetRunIndex(str_run_in);
463 prop.SetEventOrder(ev_ord);
464
465 uint32_t part_id = 0;
466 if (regex_match(name, re6)) {
467 string str_part = regex_replace(name, re6, "$6");
468 part_id = std::stoul(str_part.data());
469 }
470 prop.SetFileId(part_id);
471
472 LOGF(debug, "exp %s", str_exp.data());
473 LOGF(debug, "run %s", str_run.data());
474 LOGF(debug, "run_in %s", prop.GetRunIndex().data());
475 LOGF(debug, "run_id %d", prop.GetRunId());
476 LOGF(debug, "ev_ord %d", prop.GetEventOrder());
477 LOGF(debug, "part_id %d", prop.GetFileId());
478 return kBMNSUCCESS;
479}
const float thr
const Float_t d
Z-ccordinate of the first GEM-station.
Definition BmnMwpcHit.cxx:7
int i
Definition P4_F32vec4.h:22
friend F32vec4 min(const F32vec4 &a, const F32vec4 &b)
Definition P4_F32vec4.h:30
BmnStatus
Definition BmnEnums.h:24
@ kBMNERROR
Definition BmnEnums.h:26
@ kBMNSUCCESS
Definition BmnEnums.h:25
std::chrono::time_point< SysClock > SysPoint
const uint32_t RECORD_FILE_ID
Definition RawTypes.h:17
const uint32_t RECORD_RUN_NUMBER
Definition RawTypes.h:14
const uint32_t RECORD_EVENT_ORDER
Definition RawTypes.h:16
const uint32_t kNBYTESINWORD
Definition RawTypes.h:19
const double TRC_CLOCK_NS
Definition RawTypes.h:97
const uint32_t RECORD_RUN_INDEX
Definition RawTypes.h:15
static SysPoint ParseTAI(json &j, Int_t tai_utc_dif=0)
static TString GetSubNameAfterRunId(TString name)
static SpillStatus ParseJsonStatus(json &j, Int_t tai_utc_dif=0)
static BmnStatus ParseComplexTLV(UInt_t *buf, UInt_t &len, UInt_t &runId)
static BmnStatus ParseJsonConfig(json &j, BmnTrigConfig &trig_conf)
static BmnStatus ParseRawT0TextConfig(string &s, BmnT0Raw< kT0_BIN_BLOCK_WORDS > *rt0dig)
static BmnStatus ParseRawFileName(TString s, BmnFileProp &prop)
uint32_t GetFileId() const
Definition BmnFileProp.h:24
string GetRunIndex() const
Definition BmnFileProp.h:22
void SetFileId(uint32_t fid)
Definition BmnFileProp.h:19
void SetRunIndex(string index)
Definition BmnFileProp.h:17
uint32_t GetRunId() const
Definition BmnFileProp.h:21
void SetEventOrder(uint32_t order)
Definition BmnFileProp.h:18
void SetRunId(uint32_t v)
Definition BmnFileProp.h:16
uint32_t GetEventOrder() const
Definition BmnFileProp.h:23
static std::string TimePoint2String(SysPoint p)
map< uint32_t, int32_t > & DelayLines()
Definition BmnT0Raw.h:125
map< uint32_t, bool > & Switches()
Definition BmnT0Raw.h:126
map< uint32_t, Discriminator_Config > & Discriminators()
Definition BmnT0Raw.h:122
void SetComment(string str)
Definition BmnT0Raw.h:128
void SetBDMultL(uint32_t val)
Definition BmnT0Raw.h:136
void SetBDMultH(uint32_t val)
Definition BmnT0Raw.h:132
map< uint32_t, LV_Channel_Config > & LVChannelConfig()
Definition BmnT0Raw.h:124
void SetExtraDelaysMask(uint32_t val)
Definition BmnT0Raw.h:140
void SetTrigDelay(uint32_t v)
void SetReadChannelMask(uint32_t v)
void SetStartChannelMask(uint32_t v)
void SetCalibCount(uint32_t v)
unordered_map< string, BmnTrigChannelConfig > & TrigChannelConfig()
void SetCalibEn(bool v)
void SetMatchWindow(uint32_t v)
a class to store JSON values
Definition json.hpp:17282
iterator begin() noexcept
returns an iterator to the first element
Definition json.hpp:19664
void ParseIStringStream(istringstream &iss, Node &root)
static const size_t counters_cnt
Definition SpillStatus.h:38