BmnRoot
Loading...
Searching...
No Matches
BmnMetadataDecoder.cxx
Go to the documentation of this file.
2// STL
3#include <bitset>
4#include <chrono>
5#include <filesystem>
6#include <iostream>
7#include <regex>
8// ROOT
9#include "TStopwatch.h"
10#include "TSystem.h"
11
12#include <ROOT/RDF/RInterface.hxx>
13#include <ROOT/RDataFrame.hxx>
14// FairRoot
15#include "FairLogger.h"
16// BmnRoot
17#include "BmnADCDigit.h"
18#include "BmnConverterTools.h"
19#include "BmnGemStripDigit.h"
20#include "BmnHRBDigit.h"
21#include "BmnMSCDigit.h"
22#include "BmnMSCMatrix.h"
23#include "BmnMSCZSDigit.h"
24#include "BmnRawDataDecoder.h"
25#include "BmnSyncDigit.h"
26#include "BmnTDCDigit.h"
27#include "BmnTQDCADCDigit.h"
28#include "DigiRunHeader.h"
29#include "TangoData.h"
30#include "UniDetectorParameter.h"
31#include "UniRawFile.h"
32#include "UniRun.h"
33
34namespace fs = std::filesystem;
35using namespace std::chrono;
36
37BmnMetadataDecoder::BmnMetadataDecoder(vector<string> files, string outfile, ULong_t period, BmnSetup setup)
38 : fPeriodId(period)
39 , fTime_s(0)
40 , fTime_ns(0)
41 , fRunStartTime(SysPoint::min())
42 , fRunEndTime(SysPoint::min())
43 , fRawTree(nullptr)
44 , fDigiTree(nullptr)
45 , fDigiFileNames(files)
46 , fOutFileName(outfile)
47 , fDigiRunHdrName("DigiRunHeader")
48 , fRawRunHdrName("RawRunHeader")
49 , fMetadataName("RawRunMetadata")
50 , fDigiFileOut(nullptr)
51 , fCalibFile(nullptr)
52 , msc(nullptr)
53 , t0raw(nullptr)
54 , msc_copy(nullptr)
55 , t0_copy(nullptr)
56 , inEventHeader(nullptr)
57 , spillHeader(nullptr)
58 //, metadata(make_unique<BmnMetadataRaw>())
59 , fBmnSetup(setup)
60 , fPeriodSetupExt(Form("%d%s.txt", fPeriodId, ((fBmnSetup == kBMNSETUP) ? "" : "_SRC")))
61//, fTrigMapper(make_unique(TString("Trig_map_Run") + fPeriodSetupExt))
62// , fMSCMapper(nullptr)
63{
64 TTree::SetMaxTreeSize(1'000'000'000'000); // 932 GB
65 if (fOutFileName.length() == 0)
66 fOutFileName = string(Form("bmn_run%d_meta.root", fRunId));
67}
68
73
75{
76 fDigiDir = DirName;
77 // string re_str(Form(".*mpd_run_Top_%u(_ev\\d+_p\\d+)\\.root", fRunId));
78 string re_digi_str(Form(".*%u(_ev\\d+_p\\d+)[^\\/]*\\.root", fRunId));
79 string re_data_str("([^\\/]+).data");
80 // TPRegexp re_dig(re_str);
81 regex re_dig(re_digi_str);
82 regex re_data(re_data_str);
83 // Check if the path exists and is a directory
84 if (fs::exists(fDigiDir) && fs::is_directory(fDigiDir)) {
85 std::cout << "Target files in a directory: " << fDigiDir << std::endl;
86 // Get file names from the DB
87 if (get_filenames_from_db) {
88 UniRun* run = UniRun::GetRun(fPeriodId, fRunId);
89 if (run) {
90 TObjArray* arrNames = run->GetRawFiles();
91 for (const auto& j : *arrNames) {
92 UniRawFile* pRawFile = (UniRawFile*)j;
93 TString strFilePath = pRawFile->GetFilePath();
94 int32_t lastSlash = strFilePath.Last('/');
95 TString strFileName = (lastSlash == TString::kNPOS)
96 ? strFilePath
97 : TString(strFilePath(lastSlash + 1, strFilePath.Length()));
98 if (strFileName.Length() == 0)
99 continue;
100 if (regex_match(strFileName.Data(), re_data)) {
101 string name_digi = regex_replace(strFileName.Data(), re_data, "$1");
102 fDigiFileNames.push_back(fDigiDir + "/" + name_digi + ".root");
103 } else
104 continue;
105 }
106 delete run;
107 }
108 } else {
109 // Iterate through the directory
110 for (const auto& entry : fs::directory_iterator(fDigiDir)) {
111 string fileName = entry.path();
112 if (regex_match(fileName, re_dig)) {
113 // if (re_dig.MatchB(fileName)) {
114 std::cout << entry.path().filename() << std::endl;
115 fDigiFileNames.push_back(fileName.data());
116 }
117 }
118 }
119 if (fDigiFileNames.size() == 0) {
120 LOGF(error, "No valid files in te directory!");
121 return kBMNERROR;
122 }
123 return kBMNSUCCESS;
124 } else {
125 LOGF(error, "Wrong path.");
126 return kBMNERROR;
127 }
128}
129
131{
132 fTarDir = DirName;
133 // string re_str(Form(".*mpd_run_Top_%u(_ev\\d+_p\\d+)\\.root", fRunId));
134 string re_str(Form(".*mpd_run_Top_%u(_ev\\d+_p\\d+)\\.root.tar", fRunId));
135 TPRegexp re_tar(re_str);
136 // Check if the path exists and is a directory
137 if (fs::exists(fTarDir) && fs::is_directory(fTarDir)) {
138 std::cout << "Target files in the directory: " << fTarDir << std::endl;
139 // Iterate through the directory
140 for (const auto& entry : fs::directory_iterator(fTarDir)) {
141 string fileName = entry.path();
142 if (re_tar.MatchB(fileName)) {
143 std::cout << entry.path().filename() << std::endl;
144 fTarFileNames.push_back(fileName.data());
145 }
146 }
147 return kBMNSUCCESS;
148 } else {
149 LOGF(error, "Wrong path.");
150 return kBMNERROR;
151 }
152}
153
155{
156 // // try get run interval from DB
157 // if (!get_run_interval_from_file) {
158 // UniRun* pRun = UniRun::GetRun(fPeriodId, fRunId);
159 // if (!pRun) {
160 // get_run_interval_from_file = true;
161 // } else {
162 // TDatime fRunStartDate = pRun->GetStartDatetime();
163 // TDatime* fRunEndDate = pRun->GetEndDatetime();
164 // fRunStartTime = BmnFunctionSet::TDatime2GMT(fRunStartDate) - 3h; // crutch for zone shift
165 // if (fRunEndDate)
166 // fRunEndTime = BmnFunctionSet::TDatime2GMT(*fRunEndDate) - 3h;
167 // else
168 // get_run_interval_from_file = true;
169 // // LOGF(info, "Set Run from DB:\n\tstart ts: %s\n\tstop ts: %s",
170 // // fRunStartDate.AsString(), fRunEndDate->AsString());
171 // LOGF(info, "Set Run from DB:\n\tstart ts: %s\n\tstop ts: %s",
172 // BmnFunctionSet::TimePoint2String(fRunStartTime), BmnFunctionSet::TimePoint2String(fRunEndTime));
173 // delete pRun;
174 // }
175 // }
176 // get status blocks, maps and run interval from digi files
177 bool maps_recreate = true;
178 bool maps_set = false;
179 bool tr_conf_set = false;
180 BmnTrigConfig tc;
181 for (string& name : fDigiFileNames) {
182 unique_ptr<TFile> dig_in{TFile::Open(name.c_str(), "READ")};
183 if (dig_in->IsOpen() == false) {
184 LOGF(error, "\n!!!!\ncannot open file %s !\n", name.c_str());
185 // return kBMNERROR;
186 } else {
187 LOGF(info, "Starting to decode file %s", name.c_str());
188 digiRunHeader.reset(static_cast<DigiRunHeader*>(dig_in->Get(fDigiRunHdrName)));
189 if (!maps_set) {
190 fRawT0Map = digiRunHeader->T0Map();
191 fTrcBitMap = digiRunHeader->GetTrcBitMap();
192 fScalersMap = digiRunHeader->GetScalerMap();
193 if (get_run_interval_from_file) {
194 fRunStartTime = digiRunHeader->GetRunStartTP();
195 fRunEndTime = digiRunHeader->GetRunEndTP();
196 LOGF(debug, "Set Run:\n\tstart ts: %s\n\tstop ts: %s",
199 }
200 maps_set = true;
201 } else {
202 if (get_run_interval_from_file) {
203 if (fRunStartTime > digiRunHeader->GetRunStartTP())
204 fRunStartTime = digiRunHeader->GetRunStartTP();
205 if (fRunEndTime < digiRunHeader->GetRunEndTP())
206 fRunEndTime = digiRunHeader->GetRunEndTP();
207 LOGF(debug, "Upd Run:\n\tstart ts: %s\n\tstop ts: %s",
210 }
211 }
212 if (!tr_conf_set) {
213 if (digiRunHeader->GetTrigConfig().TrigChannelConfig().size() > 0) {
214 tc = digiRunHeader->GetTrigConfig();
215 tr_conf_set = true;
216 }
217 }
218 unique_ptr<BmnMetadataRaw> meta_f{static_cast<BmnMetadataRaw*>(dig_in->Get(fMetadataName))};
219 if (meta_f.get() == nullptr) {
220 LOGF(warning, "No metadata found in the %s!", name.c_str());
221 continue;
222 }
223 auto& src = meta_f->SpillStatusVec();
224 auto& dst = metadata.SpillStatusVec();
225 LOGF(debug, "vec size %u", src.size());
226 dst.reserve(dst.size() + src.size());
227 std::move(std::begin(src), std::end(src), std::back_inserter(dst));
228 }
229 }
230 LOGF(info, "Set Run:\n\tstart ts: %s\n\tstop ts: %s", BmnFunctionSet::TimePoint2String(fRunStartTime),
232 if (maps_recreate) {
233 TString PeriodSetupExt = Form("%d%s.txt", fPeriodId, ((fBmnSetup == kBMNSETUP) ? "" : "_SRC"));
234 fTrigMapper = make_unique<BmnTrigRaw2Digit>(TString("Trig_PlaceMap_Run") + PeriodSetupExt,
235 TString("Trig_map_Run") + PeriodSetupExt, nullptr);
236 digiRunHeader->T0Map() = fTrigMapper->T0Map();
237 digiRunHeader->GetTrcBitMap() = fTrigMapper->GetTrcMap();
238 digiRunHeader->GetScalerMap() = fTrigMapper->GetScalerMap();
239 scalers2name_map = fTrigMapper->GetScaler2NameMap();
240 scalers2index_map = fTrigMapper->GetScaler2IdxMap();
241 trcIdx2scalerIdx = fTrigMapper->GetTrcIdx2ScalerIdxMap();
242 scalers_name2index_map = fTrigMapper->GetScalerName2IdxMap();
243 scalers_index2name_map = fTrigMapper->GetScalerIdx2NameMap();
244 } else {
245 digiRunHeader->T0Map() = fRawT0Map;
246 digiRunHeader->GetTrcBitMap() = fTrcBitMap;
247 digiRunHeader->GetScalerMap() = fScalersMap;
248 }
249 digiRunHeader->SetRunStartTP(fRunStartTime);
250 digiRunHeader->SetRunEndTP(fRunEndTime);
251 digiRunHeader->SetTrigConfig(tc);
252}
253
255
257{
258 TStopwatch timer;
259 Double_t rtime;
260 Double_t ctime;
261 for (auto& mapel : fSpillMap) {
262 BmnSpillInfo& si = mapel.second;
263 if (si.stop_ts - si.start_ts < 1ms) {
264 si.stop_ts = si.start_ts + ((fPeriodId > 8) ? 8s : 5s);
265 LOGF(warning,
266 "LOST SPILL END! Making fake one!\n"
267 "Spill:\n\tstart ts: %s\n\tstop ts: %s",
269 }
270 }
271 metadata.SpillMap() = fSpillMap;
272 metadata.RawT0Map() = fRawT0Map;
273 metadata.TrcMap() = fTrcBitMap;
274 metadata.ScalersMap() = fScalersMap;
275 // /** Create scaler maps for name <--> (serial,channel) correspondence and navigation in the result matrix */
276 // // map: <serial, channel> -> name
277 // SerCh2Name scalers2name_map;
278 // // map: <serial, channel> -> index in the list (inside spillHeader->ScalersVec(), and MSC matrix)
279 // map<std::pair<uint32_t, uint16_t>, uint16_t> scalers2index_map;
280 // map<uint16_t, uint16_t> trcIdx2scalerIdx;
281 // // map: name -> index in the list
282 // NameChMap scalers_name2index_map;
283 // ChNameMap scalers_index2name_map;
284 // uint16_t scalers_ar_size(0); // MSC list size
285 // for (auto& map_el : fScalersMap) {
286 // const string& name = map_el.first;
287 // scalers2name_map.insert(make_pair(map_el.second, name));
288 // scalers_name2index_map.insert(make_pair(name, scalers_ar_size));
289 // scalers_index2name_map.insert(make_pair(scalers_ar_size, name));
290 // scalers2index_map.insert(make_pair(map_el.second, scalers_ar_size));
291 // auto it = fTrcBitMap.find(name);
292 // if (it != fTrcBitMap.end()) {
293 // trcIdx2scalerIdx.insert(make_pair(it->second, scalers_ar_size));
294 // }
295 // scalers_ar_size++;
296 // }
297 uint16_t scalers_ar_size = scalers_index2name_map.size(); // MSC list size
298 digiRunHeader->ScalersVec().resize(scalers_ar_size, 0);
299 LOGF(info, "scalers_ar_size %d", scalers_ar_size);
301 // ROOT::EnableImplicitMT();
302 RDataFrame df{"bmndata", fDigiFileNames};
303 RDataFrame df_spills{"spill", fDigiFileNames};
304 // calc the average time shift of raw T0 digits
305 timer.Start();
306 auto df_spills_withT0Raw_in_run = df_spills.Filter(
307 [&](TClonesArray& array) {
308 if (array.GetEntries()) {
309 BmnT0Raw<kT0_BIN_BLOCK_WORDS>* t0raw_dig = static_cast<BmnT0Raw<kT0_BIN_BLOCK_WORDS>*>(array.At(0));
310 LOGF(info, "T0 raw time %s tsle %10.2f ", BmnFunctionSet::TimePoint2String(t0raw_dig->GetTime()),
311 t0raw_dig->GetTimeSinceLastEvNs());
312 return ((t0raw_dig->GetTimeSinceLastEv() < duration_cast<nanoseconds>(24h))
313 && (t0raw_dig->GetTimeSinceLastEv() > duration_cast<nanoseconds>(-24h)));
314 } else
315 return false;
316 },
317 {"T0Raw"});
318 auto sum_ptr_dbl =
319 df_spills_withT0Raw_in_run
320 .Define("sls",
321 [](TClonesArray& array) {
322 return (array.GetEntries())
323 ? static_cast<BmnT0Raw<kT0_BIN_BLOCK_WORDS>*>(array.At(0))->GetTimeSinceLastEvNs()
324 : (Double_t)0.0;
325 },
326 {"T0Raw"})
327 .Sum("sls");
328 Double_t sum = sum_ptr_dbl ? *sum_ptr_dbl : 0.0;
329 auto df_spills_in_run_cnt_ptr = df_spills_withT0Raw_in_run.Count();
330 LOGF(info, "Raw T0 count : %lld", (*df_spills_in_run_cnt_ptr));
331 // printf("sum : %f\n", sum);
332 auto t0raw_shift_av = df_spills_in_run_cnt_ptr ? sum / (*df_spills_in_run_cnt_ptr) : 0.0;
333 LOGF(info, "Average raw T0 TimeSinceLastEv %10.3f ns", t0raw_shift_av);
334 timer.Stop();
335 rtime = timer.RealTime();
336 ctime = timer.CpuTime();
337 LOGF(info, "Real time %f s, CPU time %f s sum T0Raw timeshifts", rtime, ctime);
338 // TRaversing spill chain by hands and rearranging digits by spill time intervals
339 TChain* fInTreeSpills = new TChain("spill");
340 TChain* fInTreeEvents = new TChain("bmndata");
341 for (string& name : fDigiFileNames) {
342 LOGF(debug, "Filling tchain with a file %s", name.c_str());
343 fInTreeSpills->Add(name.c_str());
344 fInTreeEvents->Add(name.c_str());
345 }
346
347 fInTreeSpills->GetListOfBranches(); // ROOT magic for SetBranchAddress to work
348 fInTreeSpills->SetBranchAddress("MSC", &msc);
349 fInTreeSpills->SetBranchAddress("T0Raw", &t0raw);
350 fInTreeEvents->SetBranchAddress("BmnEventHeader.", &inEventHeader);
351 fDigiFileOut = new TFile(fOutFileName.data(), "recreate", "");
352 fDigiTreeSpills = new TTree("spill", "spill");
353 spillHeader = new BmnSpillHeader();
354 fDigiTreeSpills->Branch("BmnSpillHeader.", &spillHeader);
355 // fMSCMapper = new BmnMscRaw2Digit(fPeriodId, fRunId, fMSCMapFileName, fInTreeSpills, fDigiTreeSpills);
356 // msc_copy = new TClonesArray(msc->GetClass());
357 msc_copy = new TClonesArray(BmnMSCMatrix<UChar_t>::Class());
358 fDigiTreeSpills->Branch("MSC", &msc_copy);
359 t0_copy = new TClonesArray(t0raw->GetClass());
360 fDigiTreeSpills->Branch("T0Raw", &t0_copy);
361
362 size_t msc_entries_cnt_total(0); // all MSC times (to check presence of MSC outside spills)
363 size_t msc_entries_cnt(0); // MSC entries inside spills
364 // make map (time_point -> (i-tree entry,i-digit)) to sort and rearrange MSC by time
365 typedef pair<Long64_t, Int_t> Index_Pair; // (i-tree entry,i-digit)
366 map<SysPoint, vector<Index_Pair>> mts; // for period 8
367 // map<SysPoint, size_t> time2times_map_entry;
368 vector<pair<SysPoint, size_t>> vts_t0; // for period 8
369
370 // map<SysPoint, vector<uint8_t>> mts9; // for period 9
371
372 LOGF(info, "Start filling timestamp map");
373 timer.Start();
374 if (fPeriodId > 8) {
375 for (Long64_t iEntry = 0; iEntry < fInTreeSpills->GetEntries(); ++iEntry) {
376 fInTreeSpills->GetEntry(iEntry);
382 // for (Int_t iDig = 0; iDig < msc->GetEntries(); ++iDig) {
383 // BmnMSCZSDigit<uint8_t>* dig = static_cast<BmnMSCZSDigit<uint8_t>*>(msc->At(iDig));
384 // auto index_it = scalers2index_map.find(make_pair(dig->GetSerial(), dig->GetChannel()));
385 // if (index_it == scalers2index_map.end())
386 // continue;
387 // uint8_t index = index_it->second;
388 // vector<SysPoint>& times = dig->GetTimes();
389 // vector<uint8_t>& values = dig->GetValues();
390 // vector<uint8_t>& excs = dig->GetExtCond();
391 // for (size_t iTime= 0; iTime< dig->GetNVals(); iTime++){
392 // SysPoint& time = times[iTime];
393 // auto it = mts9.find(time);
394 // if (it == mts9.end()){
395 // vector<uint8_t> tarr(scalers_ar_size+1, 0); // scalers + ext conditions
396 // tarr[index] = values[iTime];
397 // tarr[scalers_ar_size] = excs[iTime];
398 // mts9.insert(make_pair(time, move(tarr)));
399 // } else{
400 // vector<uint8_t>& tarr = it->second;
401 // tarr[index] += values[iTime];
402 // tarr[scalers_ar_size] = excs[iTime];
403 // }
404 // }
405 // }
408
409 Int_t ne_t0 = t0raw->GetEntries();
410 if (ne_t0 > 0) {
411 BmnT0Raw<kT0_BIN_BLOCK_WORDS>* dig = static_cast<BmnT0Raw<kT0_BIN_BLOCK_WORDS>*>(t0raw->At(0));
412 vts_t0.push_back(make_pair(dig->GetTime() - ((Long64_t)t0raw_shift_av) * 1ns - 1s,
413 iEntry)); // crutch to fit rough time inside the spill
414 LOG(info) << "T0 time " << BmnFunctionSet::TimePoint2String(dig->GetTime()) << " iEntry " << iEntry;
415 }
416 }
417 } else {
418 for (Long64_t iEntry = 0; iEntry < fInTreeSpills->GetEntries(); ++iEntry) {
419 fInTreeSpills->GetEntry(iEntry);
420 for (Int_t iDig = 0; iDig < msc->GetEntries(); ++iDig) {
421 BmnMSCDigit<UChar_t>* dig = static_cast<BmnMSCDigit<UChar_t>*>(msc->At(iDig));
422 mts[dig->GetTime()].push_back(make_pair(iEntry, iDig));
423 // msc_entries_cnt_total++;
424 }
425 Int_t ne_t0 = t0raw->GetEntries();
426 if (ne_t0 > 0) {
427 BmnT0Raw<kT0_BIN_BLOCK_WORDS>* dig = static_cast<BmnT0Raw<kT0_BIN_BLOCK_WORDS>*>(t0raw->At(0));
428 vts_t0.push_back(make_pair(dig->GetTime() - ((Long64_t)t0raw_shift_av) * 1ns - 1s,
429 iEntry)); // crutch to fit rough time inside the spill
430 LOG(info) << "T0 time " << BmnFunctionSet::TimePoint2String(dig->GetTime()) << " iEntry " << iEntry;
431 }
432 }
433 // size_t i_rec{0};
434 // for (auto &rec : mts)
435 // time2times_map_entry[rec.first] = i_rec++;
436 msc_entries_cnt_total = mts.size();
437 }
438 timer.Stop();
439 rtime = timer.RealTime();
440 ctime = timer.CpuTime();
441 LOGF(info, "Real time %f s, CPU time %f s timestamp map filling", rtime, ctime);
442
443 LOGF(info, "Run:\n\tstart ts: %s\n\tstop ts: %s", BmnFunctionSet::TimePoint2String(fRunStartTime),
445 UInt_t spillId = 0;
446 ULong64_t n_events_run(0);
447 for (auto& mapel : fSpillMap) {
449 BmnSpillInfo& si = mapel.second;
450 LOGF(info, "Spill:\n\tstart ts: %s\n\tstop ts: %s", BmnFunctionSet::TimePoint2String(si.start_ts),
452 if ((fRunStartTime > si.stop_ts) || (fRunEndTime < si.start_ts)) {
453 LOGF(warning, "Spill is outside the run! Skipping!");
454 continue;
455 }
456 spillHeader->SetStartTime(si.start_ts);
457 spillHeader->SetStopTime(si.stop_ts);
458 spillHeader->SetSpillId(spillId++);
459 // count activated triggers in events
460 timer.Start();
461 auto df_events_in_spill = df.Filter(
462 [&](BmnEventHeader& eh) {
463 return ((eh.GetEventTimeTP() >= si.start_ts) && (eh.GetEventTimeTP() <= si.stop_ts));
464 },
465 {"BmnEventHeader."});
466 // ULong64_t ev_sp_cnt = *(df_spill.Count());
467 // LOGF(info, "NEvents %llu", ev_sp_cnt);
468 // spillHeader->SetNEvents(ev_sp_cnt);
469
470 // for (auto& el : metadata.TrcMap()) {
471 // auto df_sp_trig = df_spill.Filter(
472 // [el](BmnEventHeader& eh) { return (eh.GetInputSignalsAR() & BIT(el.second)); },
473 // {"BmnEventHeader."});
474 // ULong64_t df_sp_trig_cnt = *(df_sp_trig.Count());
475 // const string& tr_name = el.first;
476 // LOGF(info, "Evt_cnt %-12s %7llu bit %u", tr_name.c_str(), df_sp_trig_cnt, el.second);
477 // spillHeader->EventTriggerCnt()[tr_name] = df_sp_trig_cnt;
478 // metadata.EventTriggerCnt()[tr_name] += spillHeader->EventTriggerCnt()[tr_name];
488 // }
489 // crosscheck
490 ULong64_t cnt[32] = {};
491 UInt_t first_ev_id(kMaxUInt);
492 UInt_t last_ev_id(0);
493 auto df_events_in_spill_trig_cntr = df_events_in_spill.Filter(
494 [&](BmnEventHeader& eh) {
495 for (auto& el : digiRunHeader->GetTrcBitMap()) {
496 if (eh.GetInputSignalsAR() & BIT(el.second))
497 cnt[el.second]++;
498 }
499 if (first_ev_id > eh.GetEventId())
500 first_ev_id = eh.GetEventId();
501 if (last_ev_id < eh.GetEventId())
502 last_ev_id = eh.GetEventId();
503 return true;
504 },
505 {"BmnEventHeader."});
506 ULong64_t n_events = *(df_events_in_spill_trig_cntr.Count());
507 n_events_run += n_events;
508 LOGF(info, "Events in the spill %7llu", n_events);
509 spillHeader->SetNEvents(n_events);
510 spillHeader->SetFirstEventId(first_ev_id);
511 spillHeader->SetLastEventId(last_ev_id);
512 for (auto& el : digiRunHeader->GetTrcBitMap()) {
513 const string& tr_name = el.first;
514 spillHeader->EventTriggerCnt()[tr_name] = cnt[el.second];
515 digiRunHeader->EventTriggerCnt()[tr_name] += spillHeader->EventTriggerCnt()[tr_name];
516 LOGF(info, "Evt_cnt %-11s %7llu", tr_name.c_str(), cnt[el.second]);
517 }
518 timer.Stop();
519 rtime = timer.RealTime();
520 ctime = timer.CpuTime();
521 LOGF(info, "Real time %f s, CPU time %f s ev cnt calc", rtime, ctime);
522
523 // calc T0 count inside spill
524 std::sort(vts_t0.begin(), vts_t0.end(),
525 [](pair<SysPoint, size_t>& a, pair<SysPoint, size_t>& b) { return (a.first < b.first); });
526 vector<pair<SysPoint, size_t>> spillIds_t0;
527 timer.Start();
528 copy_if(begin(vts_t0), end(vts_t0), std::back_inserter(spillIds_t0),
529 [&si](pair<SysPoint, size_t> p) { return (p.first >= si.start_ts) && (p.first <= si.stop_ts); });
530 timer.Stop();
531 rtime = timer.RealTime();
532 ctime = timer.CpuTime();
533 LOGF(info, "IDs entries T0:%d", spillIds_t0.size());
534 LOGF(debug, "Real time %f s, CPU time %f s ids copy T0", rtime, ctime);
535
536 LOGF(info, "Start scalers calc");
537 timer.Start();
538 if (fPeriodId > 8) {
539 // calc MSC count inside spill
540 auto df_spills_in_spill = df_spills.Filter(
541 [&](TClonesArray& tca) {
542 for (Int_t iDig = 0; iDig < tca.GetEntries(); ++iDig) {
543 BmnMSCZSDigit<uint8_t>* dig = static_cast<BmnMSCZSDigit<uint8_t>*>(tca.At(iDig));
544 return ((dig->GetTime() >= si.start_ts) && (dig->GetTime() <= si.stop_ts));
545 }
546 return false;
547 },
548 {"MSC"});
550 map<SysPoint, vector<uint8_t>> mts9; // for period 9
551 df_spills_in_spill.Foreach(
552 [&](TClonesArray& tca) {
553 for (Int_t iDig = 0; iDig < tca.GetEntries(); ++iDig) {
554 BmnMSCZSDigit<uint8_t>* dig = static_cast<BmnMSCZSDigit<uint8_t>*>(tca.At(iDig));
555 auto index_it = scalers2index_map.find(make_pair(dig->GetSerial(), dig->GetChannel()));
556 if (index_it == scalers2index_map.end())
557 continue;
558 uint8_t index = index_it->second;
559 vector<SysPoint>& times = dig->GetTimes();
560 vector<uint8_t>& values = dig->GetValues();
561 vector<uint8_t>& excs = dig->GetExtCond();
562 for (size_t iTime = 0; iTime < dig->GetNVals(); iTime++) {
563 SysPoint& time = times[iTime];
564 auto it = mts9.find(time);
565 if (it == mts9.end()) {
566 vector<uint8_t> tarr(scalers_ar_size + 1, 0); // scalers + ext conditions
567 tarr[index] = values[iTime];
568 tarr[scalers_ar_size] = excs[iTime];
569 mts9.insert(make_pair(time, move(tarr)));
570 } else {
571 vector<uint8_t>& tarr = it->second;
572 tarr[index] += values[iTime];
573 tarr[scalers_ar_size] = excs[iTime];
574 }
575 }
576 }
577 },
578 {"MSC"});
579
580 auto itLower = mts9.lower_bound(si.start_ts);
581 auto itUpper = mts9.upper_bound(si.stop_ts);
582 size_t spillTimesCount = std::distance(itLower, itUpper);
583 // size_t spillTimesCount = mts9.size();
584 msc_entries_cnt += spillTimesCount;
585 msc_entries_cnt_total += mts9.size();
586 LOGF(info, "Spill times count %lld", spillTimesCount);
587 // create single MSC matrix object for spill
588 BmnMSCMatrix<uint8_t>* msc_matrix = new ((*msc_copy)[msc_copy->GetEntries()])
589 BmnMSCMatrix<uint8_t>(spillTimesCount, scalers_name2index_map);
590 spillHeader->ScalersVec().resize(scalers_ar_size, 0);
591 // Fill MSC matrix elements
592 size_t i_record{0};
593 // for (auto & [time, vals] : mts9) {
594 for (auto itTimes = itLower; itTimes != itUpper; itTimes++) {
595 auto& time = itTimes->first;
596 vector<uint8_t>& vals = itTimes->second;
597
598 msc_matrix->Time(i_record) = time;
599 uint8_t ext_cond = vals[scalers_ar_size];
600 msc_matrix->IsSpillBitVec()[i_record] = ext_cond & MSC_EC_SPILL;
601 msc_matrix->IsBusyBitVec()[i_record] = ext_cond & MSC_EC_BUSY;
602 for (uint16_t iScaler = 0; iScaler < scalers_ar_size; iScaler++) {
603 if (vals[iScaler] == 0)
604 continue;
605 msc_matrix->SetCount(i_record, iScaler, vals[iScaler]);
606 // msc_matrix->Counts()(i_record, iScaler) = vals[iScaler];
607 // spillHeader->Scalers()[name] += vals[iScaler];
608
609 spillHeader->ScalersVec()[iScaler] += vals[iScaler];
610 LOGF(debug, "el %8llu %2lu count %3u sp %u busy %u GC", i_record, iScaler,
611 (UChar_t)(msc_matrix->GetCount(i_record, iScaler)),
612 (UInt_t)msc_matrix->IsSpillBitVec()[i_record], (UInt_t)msc_matrix->IsBusyBitVec()[i_record]);
613 }
614 i_record++;
615 }
616 if (i_record > spillTimesCount)
617 LOGF(error, "\n\t ERROR!!! irec %lu spillTimesCount %lu\n", i_record, spillTimesCount);
618 } else {
619 // calc MSC count inside spill
620 auto itLower = mts.lower_bound(si.start_ts);
621 auto itUpper = mts.upper_bound(si.stop_ts);
622 Long64_t spillEntriesCumulativeSize{0};
623 size_t spillTimesCount = std::distance(itLower, itUpper);
624 // size_t spillTimesShift = std::distance(mts.begin(), itLower);
625 for (auto itTimes = itLower; itTimes != itUpper; itTimes++) {
626 vector<Index_Pair>& index_vec = itTimes->second;
627 spillEntriesCumulativeSize += index_vec.size();
628 }
629 // msc_entries_cnt += spillEntriesCumulativeSize;
630 msc_entries_cnt += spillTimesCount;
631 LOGF(info, "entriesCumulativeSize %lld", spillEntriesCumulativeSize);
632 LOGF(info, "Spill times count %lld", spillTimesCount);
633 // LOGF(info, "Spill times shift %lld", spillTimesShift);
634
635 // create single MSC matrix object for spill
636 BmnMSCMatrix<uint8_t>* msc_matrix = new ((*msc_copy)[msc_copy->GetEntries()])
637 BmnMSCMatrix<uint8_t>(spillTimesCount, scalers_name2index_map);
638 // BmnMSCMatrix<UChar_t>(spillTimesCount, scalers_ar_size);
639 spillHeader->ScalersVec().resize(scalers_ar_size, 0);
640
641 // Fill MSC matrix elements
642 size_t i_record{0};
643 for (auto itTimes = itLower; itTimes != itUpper; itTimes++) {
644 auto& time = itTimes->first;
645 msc_matrix->Time(i_record) = time;
646 vector<Index_Pair>& index_vec = itTimes->second;
647 // msc_entries_cnt += index_vec.size();
648 LOGF(debug2, "index_vec.size() %8llu", index_vec.size());
649 if (index_vec.size() > 1)
650 LOGF(debug, "new time");
651 for (Index_Pair& index_pair : index_vec) {
652 Long64_t& iEntry = index_pair.first;
653 Int_t& iDig = index_pair.second;
654 fInTreeSpills->GetEntry(iEntry);
655 if (iDig >= msc->GetEntries()) {
656 LOGF(error, "\n\t ERROR!!! iDig %d msc->GetEntries() %d\n", iDig, msc->GetEntries());
657 break;
658 }
659 BmnMSCDigit<UChar_t>* dig = static_cast<BmnMSCDigit<UChar_t>*>(msc->At(iDig));
660 LOGF(debug2, "iEntry %7d iDig %3d el %8llu ", iEntry, iDig, i_record);
661 if (index_vec.size() > 1) {
662 LOGF(debug, "dig->IsSpill() %d", dig->IsSpill());
663 LOGF(debug, "dig->IsBusy() %d", dig->IsBusy());
664 }
665 msc_matrix->IsSpillBitVec()[i_record] = dig->IsSpill();
666 msc_matrix->IsBusyBitVec()[i_record] = dig->IsBusy();
667 for (UInt_t iNum = 0; iNum < MSC_N_COUNTERS; iNum++) {
668 UInt_t val = dig->GetValue()[iNum];
669 // if (val == 0)
670 // continue;
671 // auto it = scalers2name_map.find(make_pair(dig->GetSerial(), iNum));
672 // if (it == scalers2name_map.end())
673 // continue;
674 // string& name = it->second;
675 auto it = scalers2index_map.find(make_pair(dig->GetSerial(), iNum));
676 if (it == scalers2index_map.end())
677 continue;
678 uint16_t iScaler = it->second;
679 // msc_matrix->Counts(i_record, iScaler) += val;
680 // msc_matrix->Counts()(i_record, iScaler) += val;
681 msc_matrix->SetCount(i_record, iScaler, val);
682 // msc_matrix->SetCount(i_record, iScaler) += val;
683 if (index_vec.size() > 1)
684 LOGF(debug, "el %8llu %2lu ser 0x%8X = %u sp %u busy %u", i_record, iScaler,
685 dig->GetSerial(), (UChar_t)(msc_matrix->GetCount(i_record, iScaler)),
686 // (UChar_t)(msc_matrix->Counts()(i_record, iScaler)),
687 (UInt_t)msc_matrix->IsSpillBitVec()[i_record],
688 (UInt_t)msc_matrix->IsBusyBitVec()[i_record]);
689 // spillHeader->Scalers()[name] += val;
690 spillHeader->ScalersVec()[iScaler] += val;
691 }
692 }
693 i_record++;
694 }
695 if (i_record > spillTimesCount)
696 LOGF(error, "\n\t ERROR!!! irec %lu spillTimesCount %lu\n", i_record, spillTimesCount);
697 }
698
699 // for (Long64_t iEntry = 0; iEntry < fInTreeSpills->GetEntries(); ++iEntry) {
700 // fInTreeSpills->GetEntry(iEntry);
701 // for (Int_t iDig = 0; iDig < msc->GetEntries(); ++iDig) {
702 // BmnMSCDigit<UChar_t>* dig = static_cast<BmnMSCDigit<UChar_t>*>(msc->At(iDig));
703 // size_t i_record = time2times_map_entry[dig->GetTime()] - spillTimesShift;
704 // LOGF(info, "iEntry %7d iDig %3d el %8llu ", iEntry, iDig, i_record);
705 // msc_matrix->Time(i_record) = dig->GetTime();
706 // msc_matrix->IsSpillBitVec()[i_record] = dig->IsSpill();
707 // msc_matrix->IsBusyBitVec()[i_record] = dig->IsBusy();
708 // for (UInt_t iNum = 0; iNum < MSC_N_COUNTERS; iNum++) {
709 // UInt_t val = dig->GetValue()[iNum];
710 // if (val == 0)
711 // continue;
712 // // auto it = scalers2name_map.find(make_pair(dig->GetSerial(), iNum));
713 // // if (it == scalers2name_map.end())
714 // // continue;
715 // // string& name = it->second;
716 // auto it = scalers2index_map.find(make_pair(dig->GetSerial(), iNum));
717 // if (it == scalers2index_map.end())
718 // continue;
719 // uint16_t index = it->second;
720 // msc_matrix->Counts(i_record, index) += val;
721 // LOGF(debug, "el %8llu %2lu = %u sp %u busy %u",
722 // i_record,
723 // index,
724 // (UChar_t)(msc_matrix->Counts(i_record, index)),
725 // (UInt_t)msc_matrix->IsSpillBitVec()[i_record],
726 // (UInt_t)msc_matrix->IsBusyBitVec()[i_record]);
727 // // spillHeader->Scalers()[name] += val;
728 // spillHeader->ScalersVec()[index] += val;
729 // }
730 // }
731 // }
732 for (auto& p : spillIds_t0) {
733 Long64_t iEntry = p.second;
734 fInTreeSpills->GetEntry(iEntry);
735 t0_copy->AbsorbObjects(t0raw);
736 }
737 for (int i = 0; i < scalers_ar_size; i++)
738 digiRunHeader->ScalersVec()[i] += spillHeader->ScalersVec()[i];
739 // for (auto& scaler_map_el : spillHeader->Scalers()) {
740 // const string& sc_name = scaler_map_el.first;
741 // metadata.Scalers()[sc_name] += spillHeader->Scalers()[sc_name];
742 // }
743 timer.Stop();
744 rtime = timer.RealTime();
745 ctime = timer.CpuTime();
746 LOGF(info, "Real time %f s, CPU time %f s Scalers calc", rtime, ctime);
747 for (size_t iSc = 0; iSc < scalers_index2name_map.size(); iSc++) {
748 const string& name = scalers_index2name_map[iSc];
749 uint64_t& val = spillHeader->ScalersVec()[iSc];
750 spillHeader->Scalers()[name] = val;
751 LOGF(info, "MSC %-14s %10llu", name.c_str(), val);
752 }
753
754 if (t0_copy->GetEntries()) {
755 BmnT0Raw<kT0_BIN_BLOCK_WORDS>* t0r = static_cast<BmnT0Raw<kT0_BIN_BLOCK_WORDS>*>(t0_copy->At(0));
756 for (UInt_t i = 0; i < kT0_BIN_BLOCK_WORDS; i++) {
757 spillHeader->RawT0Counters()[i] = t0r->Counters()[i];
758 digiRunHeader->RawT0Counters()[i] += spillHeader->RawT0Counters()[i];
759 }
760 // LOGF(info, "comment: %s", t0r->GetComment().data());
761 // for (auto& p : t0r->DelayLines())
762 // LOGF(info, "Delay %2d : %3d", p.first, p.second);
763 // for (auto& p : t0r->Discriminators()) {
764 // LOGF(info, "Discriminator %2d : ", p.first);
765 // p.second.print();
766 // }
767 // for (auto& p : t0r->LVChannelConfig()) {
768 // LOGF(info, "LV %2d : ", p.first);
769 // p.second.print();
770 // }
771 LOGF(info, "T0 time %s",
773 (static_cast<BmnT0Raw<kT0_BIN_BLOCK_WORDS>*>(t0_copy->At(0)))->GetTime()));
774 for (auto& t0_map_el : digiRunHeader->T0Map()) {
775 const T0MapKey& key = t0_map_el.first;
776 if (key.first.find("BD") == 0)
777 continue;
778 LOGF(info, "T0 %-14s.%02u = %9llu", key.first.c_str(), key.second,
779 spillHeader->RawT0Counters()[t0_map_el.second]);
780 }
781 if (t0_copy->GetEntries() > 1)
782 LOGF(error, "More than 1 T0 in the spill %lu ! %d records found", spillHeader->GetSpillId(),
783 t0_copy->GetEntries());
784 } else
785 LOGF(error, "No T0 in the spill %lu !", spillHeader->GetSpillId());
786 fDigiTreeSpills->Fill();
787 }
788 digiRunHeader->SetNEvents(n_events_run);
789 // if (msc_entries_cnt_total > msc_entries_cnt)
790 LOGF(warning,
791 "MSC branch entries %9llu total\n"
792 "MSC branch entries %9llu in spills\n"
793 "MSC branch entries %9llu outside spills!",
794 msc_entries_cnt_total, msc_entries_cnt, msc_entries_cnt_total - msc_entries_cnt);
795 digiRunHeader->ScalersName2IndexMap() = move(scalers_name2index_map);
796 digiRunHeader->ScalersIndex2NameMap() = move(scalers_index2name_map);
797 digiRunHeader->TrcIndex2ScalerIndexMap() = move(trcIdx2scalerIdx);
798 LOGF(info, "Evt trigger count Total:");
799 LOGF(info, "All events %8llu", digiRunHeader->GetNEvents());
800 for (auto& el : digiRunHeader->GetTrcBitMap()) {
801 const string& tr_name = el.first;
802 LOGF(info, "%-12s %8llu", tr_name.c_str(), digiRunHeader->EventTriggerCnt()[tr_name]);
803 }
804 LOGF(info, "Raw T0 Total:");
805 for (auto& t0_map_el : fRawT0Map) {
806 const T0MapKey& key = t0_map_el.first;
807 if (key.first.find("BD") == 0)
808 continue;
809 LOGF(info, "T0 %-14s.%02u = %10llu", key.first.c_str(), key.second,
810 digiRunHeader->GetRawT0Counter(key.first.c_str(), key.second));
811 }
812 LOGF(info, "MSC Total:");
813 digiRunHeader->PrintScalers();
814 delete fInTreeSpills;
815 // ROOT::DisableImplicitMT();
816 return kBMNSUCCESS;
817}
818
820{
822 // sort status blocks
823 std::sort(metadata.SpillStatusVec().begin(), metadata.SpillStatusVec().end(), [](SpillStatus& a, SpillStatus& b) {
824 return ((a.start_ts < b.start_ts) || ((a.start_ts == b.start_ts) && (a.cur_spill_time_ms < b.cur_spill_time_ms))
825 // ((a.start_ts == b.start_ts) && (a.stop_ts < b.stop_ts))
826 );
827 });
828 for (SpillStatus& ss : metadata.SpillStatusVec()) {
829 LOG(debug) << BmnFunctionSet::TimePoint2String(ss.start_ts) << " start";
830 LOG(debug) << BmnFunctionSet::TimePoint2String(ss.stop_ts) << " stop";
831 }
832 // make spill map
833 for (SpillStatus& ss : metadata.SpillStatusVec()) {
834 SysPoint ts_spill_start = ss.start_ts;
835 SysPoint ts_spill_stop = ss.stop_ts;
836 auto spill_it = fSpillMap.find(ts_spill_start);
837 if (spill_it == fSpillMap.end()) {
838 bool inserted;
839 std::tie(spill_it, inserted) = fSpillMap.insert(
840 make_pair(ts_spill_start, BmnSpillInfo{.start_ts = ts_spill_start, .stop_ts = ts_spill_start}));
841 LOGF(debug, "spill record inserted %d", inserted);
842 BmnSpillInfo& si = spill_it->second;
843 si.counters.ResizeTo(ss.counters);
844 }
845 BmnSpillInfo& info = spill_it->second;
846 if (info.stop_ts < ts_spill_stop)
847 info.stop_ts = ts_spill_stop;
848 info.vec_spill_status.push_back(ss);
849 }
850 for (auto& it : fSpillMap) {
851 BmnSpillInfo& info = it.second;
852 LOGF(info, "\tstart %s\n\tstop %s", BmnFunctionSet::TimePoint2String(info.start_ts),
854 }
855 if (fSpillMap.size() == 0) {
856 LOGF(error, "Spill map is empty!");
857 return kBMNERROR;
858 }
859 // calculate counters for each spill & sum total ones
860 auto& total_cntrs = metadata.SpillCounters();
861 total_cntrs.ResizeTo(fSpillMap.begin()->second.counters);
862 for (auto& it : fSpillMap) {
863 BmnSpillInfo& info = it.second;
864 size_t blocks_count = info.vec_spill_status.size();
865 if (blocks_count == 0)
866 continue;
867 auto& first_ss = info.vec_spill_status.front();
868 auto& last_ss = info.vec_spill_status.back();
869 info.counters = last_ss.counters;
870 auto& spill_cntrs = info.counters;
871 for (size_t i = 0; i < SpillStatus::counters_cnt; i++) {
872 spill_cntrs.cntr_after_err[i] -= first_ss.counters.cntr_after_err[i];
873 spill_cntrs.cntr_befor_err[i] -= first_ss.counters.cntr_befor_err[i];
874 total_cntrs.cntr_after_err[i] += spill_cntrs.cntr_after_err[i];
875 total_cntrs.cntr_befor_err[i] += spill_cntrs.cntr_befor_err[i];
876 }
877 for (size_t i_pair = 0; i_pair < spill_cntrs.cntr_input.size(); i_pair++) {
878 auto& spill_p_input = spill_cntrs.cntr_input[i_pair];
879 auto& first_p_input = first_ss.counters.cntr_input[i_pair];
880 auto& spill_p_input_ar = spill_cntrs.cntr_input_after_red[i_pair];
881 auto& first_p_input_ar = first_ss.counters.cntr_input_after_red[i_pair];
882 auto& total_p_input = total_cntrs.cntr_input[i_pair];
883 auto& total_p_input_ar = total_cntrs.cntr_input_after_red[i_pair];
884 for (size_t i = 0; i < SpillStatus::counters_cnt; i++) {
885 spill_p_input.second[i] -= first_p_input.second[i];
886 spill_p_input_ar.second[i] -= first_p_input_ar.second[i];
887 total_p_input.second[i] += spill_p_input.second[i];
888 total_p_input_ar.second[i] += spill_p_input_ar.second[i];
889 // LOGF(info, "cntr %2u input %s total %lu", i, total_p_input.first.c_str(),
890 // total_p_input.second[i]);
891 }
892 }
893 }
894 metadata.SpillMap() = fSpillMap;
895 vector<string> trc_names{"pBT", "BT", "MBT", "NiT", "BT&nBUSY", "CCT1", "CCT2"};
896 // for (auto& it : metadata.SpillMap()) {
897 // BmnSpillInfo& info = it.second;
898 // auto& spill_cntrs = info.counters;
899 // LOGF(info, "\n\tstart %s\n\tstop %s", BmnFunctionSet::TimePoint2String(info.start_ts),
900 // BmnFunctionSet::TimePoint2String(info.stop_ts));
901 // if (spill_cntrs.cntr_input.size() <= 1) {
902 // LOGF(warning, "not enough data");
903 // continue;
904 // }
905 // for (string& name : trc_names) {
906 // uint16_t icntr = fTrigBitsMap[name];
907 // LOGF(info, "\t icntr %6u %8s", icntr, name.c_str());
908 // LOGF(info, "\t%8s before %8lu after %8lu ", name.c_str(),
909 // spill_cntrs.cntr_befor_err[icntr],
910 // spill_cntrs.cntr_after_err[icntr]);
911 // LOGF(info, "\t%8s BUSY input %8lu input_red %8lu ", name.c_str(),
912 // spill_cntrs.cntr_input[6].second[icntr], spill_cntrs.cntr_input_after_red[6].second[icntr]);
913 // LOGF(info, "\t%8s NOT_BUSY input %8lu input_red %8lu ", name.c_str(),
914 // spill_cntrs.cntr_input[7].second[icntr], spill_cntrs.cntr_input_after_red[7].second[icntr]);
915 // }
916 // }
917 LOGF(info, "\n\tTRC TOTAL:");
918 for (string& name : trc_names) {
919
920 uint16_t icntr = fTrcBitMap[name];
921 LOGF(info, "\t icntr %6u %8s", icntr, name.c_str());
922 LOGF(info, "\t%8s before %8lu after %8lu ", name.c_str(), total_cntrs.cntr_befor_err[icntr],
923 total_cntrs.cntr_after_err[icntr]);
924 LOGF(info, "\t%8s BUSY input %8lu input_red %8lu ", name.c_str(), total_cntrs.cntr_input[6].second[icntr],
925 total_cntrs.cntr_input_after_red[6].second[icntr]);
926 LOGF(info, "\t%8s NOT_BUSY input %8lu input_red %8lu ", name.c_str(), total_cntrs.cntr_input[7].second[icntr],
927 total_cntrs.cntr_input_after_red[7].second[icntr]);
928 }
929 // SaveData();
930 // DisposeDecoder();
931
932 return kBMNSUCCESS;
933}
934
936{
937 LoadConfig();
938
939 return kBMNSUCCESS;
940}
941
943{
944 if (fTrigMapper)
945 fTrigMapper->ClearArrays();
946 inEventHeader->Clear();
947}
948
950{
951 if (spillHeader)
952 spillHeader->Clear();
953 if (msc_copy)
954 msc_copy->Delete();
955 if (t0_copy)
956 t0_copy->Delete();
957}
958
960{
961
962 fRunStartTime = SysPoint::min();
963 fRunEndTime = SysPoint::min();
964 fRootFileName = file;
965 LOGF(debug, "RunId %d got from the file (ResetDecoder)", fRunId);
966 fRawTree->Reset();
967 fDigiTree->Reset();
968}
969
971{
972 metadata.SpillStatusVec()
973 .clear(); // !!! temporary remove raw trc statuses from file @TODO resolve the writing issue
974 for (auto& it : metadata.SpillMap()) {
975 BmnSpillInfo& info = it.second;
976 info.vec_spill_status.clear();
977 }
978 // gDebug = 2;
979 LOGF(debug, "Writing run header to file");
980 Int_t wret = fDigiFileOut->WriteObject(digiRunHeader.get(), fDigiRunHdrName.Data());
981 if (wret > 0)
982 LOGF(debug, "Written bytes %d", wret);
983 else
984 DBGERR("WriteObject")
985
986 LOGF(debug, "Writing metadata to file");
987 TStopwatch timer;
988 Double_t rtime;
989 Double_t ctime;
990 timer.Start();
991 wret = fDigiFileOut->WriteObject(&metadata, fMetadataName.Data());
992 timer.Stop();
993 rtime = timer.RealTime();
994 ctime = timer.CpuTime();
995 LOGF(debug, "Real time %f s, CPU time %f s WriteObject", rtime, ctime);
996 if (wret > 0)
997 LOGF(debug, "Written bytes %d", wret);
998 else
999 DBGERR("WriteObject")
1000
1001 LOGF(debug, "Flushing other data to file");
1002 fDigiFileOut->Write();
1003 LOGF(debug, "Closing the file");
1004 fDigiFileOut->Close();
1005
1006 return kBMNSUCCESS;
1007}
1008
1010{
1011 // if (fTrigMapper)
1012 // delete fTrigMapper;
1013 // if (fMSCMapper)
1014 // delete fMSCMapper;
1015
1016 if (msc_copy)
1017 delete msc_copy;
1018 if (t0_copy)
1019 delete t0_copy;
1020
1021 if (inEventHeader)
1022 delete inEventHeader;
1023 if (spillHeader)
1024 delete spillHeader;
1025
1026 return kBMNSUCCESS;
1027}
1028
1030{
1031 TPRegexp re_data(".*\\w+_\\w+_\\w+_(\\d+)([^/]*)\\.data");
1032 TPRegexp re_raw(".*\\w+_run(\\d+)([^/]*)_raw\\.root");
1033 TString str(name);
1034 if (re_data.MatchB(str))
1035 re_data.Substitute(str, "$2");
1036 else {
1037 if (re_raw.MatchB(str))
1038 re_raw.Substitute(str, "$2");
1039
1040 else
1041 return "";
1042 }
1043 return str;
1044}
1045
1046BmnStatus BmnMetadataDecoder::LoadConfig()
1047{
1048 LOGF(debug, "%s", __func__);
1049 if (fDecoderConfigFileName.length() == 0)
1050 fDecoderConfigFileName = string(getenv("VMCWORKDIR")) + "/config/bmnconf.json";
1051 pt::read_json(fDecoderConfigFileName, conf);
1052 return kBMNSUCCESS;
1053}
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
BmnSetup
Definition BmnEnums.h:89
@ kBMNSETUP
Definition BmnEnums.h:90
std::chrono::time_point< SysClock > SysPoint
std::pair< std::string, uint16_t > T0MapKey
Definition BmnAliases.h:5
#define DBGERR(a)
Definition BmnMath.h:25
const uint8_t MSC_EC_BUSY
Definition RawTypes.h:90
const uint32_t MSC_N_COUNTERS
Definition RawTypes.h:88
const uint8_t MSC_EC_SPILL
Definition RawTypes.h:89
const uint32_t kT0_BIN_BLOCK_WORDS
Definition RawTypes.h:93
SysPoint GetEventTimeTP()
UInt_t GetEventId()
UInt_t GetInputSignalsAR()
static std::string TimePoint2String(SysPoint p)
UInt_t GetSerial() const
Definition BmnMSCDigit.h:60
SysPoint GetTime() const
Definition BmnMSCDigit.h:66
bool IsSpill()
Definition BmnMSCDigit.h:72
bool IsBusy()
Definition BmnMSCDigit.h:74
std::array< IntType, MSC_N_COUNTERS > & GetValue()
Definition BmnMSCDigit.h:64
SysPoint & Time(size_t record_id)
auto & IsBusyBitVec()
void SetCount(size_t record_id, uint16_t scaler_id, IntType val)
auto & IsSpillBitVec()
IntType GetCount(size_t record_id, string scaler_name)
SysPoint GetTime() const
std::vector< IntType > & GetValues()
uint8_t GetChannel() const
uint32_t GetSerial() const
std::vector< uint8_t > & GetExtCond()
std::vector< SysPoint > & GetTimes()
size_t GetNVals()
BmnMetadataDecoder(vector< string > file={}, string outfile="", ULong_t period=8, BmnSetup setup=kBMNSETUP)
BmnStatus SetTarDirectory(string DirName)
BmnStatus SetDigiDirectory(string fDigiDir)
void ResetDecoder(TString file)
TString GetSubNameAfterRunId(TString name)
Name2SerCh & ScalersMap()
NameChMap & TrcMap()
map< SysPoint, BmnSpillInfo > & SpillMap()
SpillCntrs< SpillStatus::counters_cnt > & SpillCounters()
T0ChMap & RawT0Map()
vector< SpillStatus > & SpillStatusVec()
void SetSpillId(UInt_t spill)
void SetLastEventId(UInt_t event_id)
void SetStartTime(SysPoint time)
UInt_t GetSpillId()
vector< uint64_t > & ScalersVec()
MapString2Cnt & EventTriggerCnt()
MapString2Cnt & Scalers()
void SetFirstEventId(UInt_t event_id)
array< UInt_t, kT0_BIN_BLOCK_WORDS > & RawT0Counters()
void SetStopTime(SysPoint time)
void SetNEvents(UInt_t events)
std::array< UInt_t, counters_cnt > & Counters()
Definition BmnT0Raw.h:116
SysPoint GetTime()
Definition BmnT0Raw.h:92
TString GetFilePath()
get file path of the current raw file
Definition UniRawFile.h:110
static UniRun * GetRun(int period_number, int run_number)
get run from the database
Definition UniRun.cxx:177
-clang-format
Definition setup.py:1
SysPoint stop_ts
std::vector< SpillStatus > vec_spill_status
SysPoint start_ts
SpillCntrs< SpillStatus::counters_cnt > counters
std::array< ULong64_t, counters_cnt > cntr_after_err
Definition SpillCntrs.h:11
void ResizeTo(const SpillCntrs &example)
Definition SpillCntrs.h:16
static const size_t counters_cnt
Definition SpillStatus.h:38