177 bool maps_recreate =
true;
178 bool maps_set =
false;
179 bool tr_conf_set =
false;
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());
187 LOGF(info,
"Starting to decode file %s", name.c_str());
188 digiRunHeader.reset(
static_cast<DigiRunHeader*
>(dig_in->Get(fDigiRunHdrName)));
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",
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",
213 if (digiRunHeader->GetTrigConfig().TrigChannelConfig().size() > 0) {
214 tc = digiRunHeader->GetTrigConfig();
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());
223 auto& src = meta_f->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));
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();
245 digiRunHeader->T0Map() = fRawT0Map;
246 digiRunHeader->GetTrcBitMap() = fTrcBitMap;
247 digiRunHeader->GetScalerMap() = fScalersMap;
249 digiRunHeader->SetRunStartTP(fRunStartTime);
250 digiRunHeader->SetRunEndTP(fRunEndTime);
251 digiRunHeader->SetTrigConfig(tc);
261 for (
auto& mapel : fSpillMap) {
266 "LOST SPILL END! Making fake one!\n"
267 "Spill:\n\tstart ts: %s\n\tstop ts: %s",
273 metadata.
TrcMap() = fTrcBitMap;
297 uint16_t scalers_ar_size = scalers_index2name_map.size();
298 digiRunHeader->ScalersVec().resize(scalers_ar_size, 0);
299 LOGF(info,
"scalers_ar_size %d", scalers_ar_size);
302 RDataFrame df{
"bmndata", fDigiFileNames};
303 RDataFrame df_spills{
"spill", fDigiFileNames};
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)));
319 df_spills_withT0Raw_in_run
321 [](TClonesArray& array) {
322 return (array.GetEntries())
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));
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);
335 rtime = timer.RealTime();
336 ctime = timer.CpuTime();
337 LOGF(info,
"Real time %f s, CPU time %f s sum T0Raw timeshifts", rtime, ctime);
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());
347 fInTreeSpills->GetListOfBranches();
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");
354 fDigiTreeSpills->Branch(
"BmnSpillHeader.", &spillHeader);
358 fDigiTreeSpills->Branch(
"MSC", &msc_copy);
359 t0_copy =
new TClonesArray(t0raw->GetClass());
360 fDigiTreeSpills->Branch(
"T0Raw", &t0_copy);
362 size_t msc_entries_cnt_total(0);
363 size_t msc_entries_cnt(0);
365 typedef pair<Long64_t, Int_t> Index_Pair;
366 map<SysPoint, vector<Index_Pair>> mts;
368 vector<pair<SysPoint, size_t>> vts_t0;
372 LOGF(info,
"Start filling timestamp map");
375 for (Long64_t iEntry = 0; iEntry < fInTreeSpills->GetEntries(); ++iEntry) {
376 fInTreeSpills->GetEntry(iEntry);
409 Int_t ne_t0 = t0raw->GetEntries();
412 vts_t0.push_back(make_pair(dig->
GetTime() - ((Long64_t)t0raw_shift_av) * 1ns - 1s,
418 for (Long64_t iEntry = 0; iEntry < fInTreeSpills->GetEntries(); ++iEntry) {
419 fInTreeSpills->GetEntry(iEntry);
420 for (Int_t iDig = 0; iDig < msc->GetEntries(); ++iDig) {
422 mts[dig->
GetTime()].push_back(make_pair(iEntry, iDig));
425 Int_t ne_t0 = t0raw->GetEntries();
428 vts_t0.push_back(make_pair(dig->
GetTime() - ((Long64_t)t0raw_shift_av) * 1ns - 1s,
436 msc_entries_cnt_total = mts.size();
439 rtime = timer.RealTime();
440 ctime = timer.CpuTime();
441 LOGF(info,
"Real time %f s, CPU time %f s timestamp map filling", rtime, ctime);
446 ULong64_t n_events_run(0);
447 for (
auto& mapel : fSpillMap) {
453 LOGF(warning,
"Spill is outside the run! Skipping!");
461 auto df_events_in_spill = df.Filter(
465 {
"BmnEventHeader."});
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(
495 for (
auto& el : digiRunHeader->GetTrcBitMap()) {
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);
512 for (
auto& el : digiRunHeader->GetTrcBitMap()) {
513 const string& tr_name = el.first;
515 digiRunHeader->EventTriggerCnt()[tr_name] += spillHeader->
EventTriggerCnt()[tr_name];
516 LOGF(info,
"Evt_cnt %-11s %7llu", tr_name.c_str(), cnt[el.second]);
519 rtime = timer.RealTime();
520 ctime = timer.CpuTime();
521 LOGF(info,
"Real time %f s, CPU time %f s ev cnt calc", rtime, ctime);
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;
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); });
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);
536 LOGF(info,
"Start scalers calc");
540 auto df_spills_in_spill = df_spills.Filter(
541 [&](TClonesArray& tca) {
542 for (Int_t iDig = 0; iDig < tca.GetEntries(); ++iDig) {
550 map<SysPoint, vector<uint8_t>> mts9;
551 df_spills_in_spill.Foreach(
552 [&](TClonesArray& tca) {
553 for (Int_t iDig = 0; iDig < tca.GetEntries(); ++iDig) {
556 if (index_it == scalers2index_map.end())
558 uint8_t index = index_it->second;
559 vector<SysPoint>& times = dig->
GetTimes();
560 vector<uint8_t>& values = dig->
GetValues();
562 for (
size_t iTime = 0; iTime < dig->
GetNVals(); iTime++) {
564 auto it = mts9.find(time);
565 if (it == mts9.end()) {
566 vector<uint8_t> tarr(scalers_ar_size + 1, 0);
567 tarr[index] = values[iTime];
568 tarr[scalers_ar_size] = excs[iTime];
569 mts9.insert(make_pair(time, move(tarr)));
571 vector<uint8_t>& tarr = it->second;
572 tarr[index] += values[iTime];
573 tarr[scalers_ar_size] = excs[iTime];
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);
584 msc_entries_cnt += spillTimesCount;
585 msc_entries_cnt_total += mts9.size();
586 LOGF(info,
"Spill times count %lld", spillTimesCount);
590 spillHeader->
ScalersVec().resize(scalers_ar_size, 0);
594 for (
auto itTimes = itLower; itTimes != itUpper; itTimes++) {
595 auto& time = itTimes->first;
596 vector<uint8_t>& vals = itTimes->second;
598 msc_matrix->
Time(i_record) = time;
599 uint8_t ext_cond = vals[scalers_ar_size];
602 for (uint16_t iScaler = 0; iScaler < scalers_ar_size; iScaler++) {
603 if (vals[iScaler] == 0)
605 msc_matrix->
SetCount(i_record, iScaler, vals[iScaler]);
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)),
616 if (i_record > spillTimesCount)
617 LOGF(error,
"\n\t ERROR!!! irec %lu spillTimesCount %lu\n", i_record, spillTimesCount);
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);
625 for (
auto itTimes = itLower; itTimes != itUpper; itTimes++) {
626 vector<Index_Pair>& index_vec = itTimes->second;
627 spillEntriesCumulativeSize += index_vec.size();
630 msc_entries_cnt += spillTimesCount;
631 LOGF(info,
"entriesCumulativeSize %lld", spillEntriesCumulativeSize);
632 LOGF(info,
"Spill times count %lld", spillTimesCount);
639 spillHeader->
ScalersVec().resize(scalers_ar_size, 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;
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());
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());
675 auto it = scalers2index_map.find(make_pair(dig->
GetSerial(), iNum));
676 if (it == scalers2index_map.end())
678 uint16_t iScaler = it->second;
681 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,
695 if (i_record > spillTimesCount)
696 LOGF(error,
"\n\t ERROR!!! irec %lu spillTimesCount %lu\n", i_record, spillTimesCount);
732 for (
auto& p : spillIds_t0) {
733 Long64_t iEntry = p.second;
734 fInTreeSpills->GetEntry(iEntry);
735 t0_copy->AbsorbObjects(t0raw);
737 for (
int i = 0;
i < scalers_ar_size;
i++)
738 digiRunHeader->ScalersVec()[
i] += spillHeader->
ScalersVec()[
i];
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);
754 if (t0_copy->GetEntries()) {
771 LOGF(info,
"T0 time %s",
774 for (
auto& t0_map_el : digiRunHeader->T0Map()) {
775 const T0MapKey& key = t0_map_el.first;
776 if (key.first.find(
"BD") == 0)
778 LOGF(info,
"T0 %-14s.%02u = %9llu", key.first.c_str(), key.second,
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());
785 LOGF(error,
"No T0 in the spill %lu !", spillHeader->
GetSpillId());
786 fDigiTreeSpills->Fill();
788 digiRunHeader->SetNEvents(n_events_run);
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]);
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)
809 LOGF(info,
"T0 %-14s.%02u = %10llu", key.first.c_str(), key.second,
810 digiRunHeader->GetRawT0Counter(key.first.c_str(), key.second));
812 LOGF(info,
"MSC Total:");
813 digiRunHeader->PrintScalers();
814 delete fInTreeSpills;
824 return ((a.start_ts < b.start_ts) || ((a.start_ts == b.start_ts) && (a.cur_spill_time_ms < b.cur_spill_time_ms))
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()) {
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);
846 if (info.
stop_ts < ts_spill_stop)
850 for (
auto& it : fSpillMap) {
855 if (fSpillMap.size() == 0) {
856 LOGF(error,
"Spill map is empty!");
861 total_cntrs.
ResizeTo(fSpillMap.begin()->second.counters);
862 for (
auto& it : fSpillMap) {
865 if (blocks_count == 0)
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];
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];
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];
895 vector<string> trc_names{
"pBT",
"BT",
"MBT",
"NiT",
"BT&nBUSY",
"CCT1",
"CCT2"};
917 LOGF(info,
"\n\tTRC TOTAL:");
918 for (
string& name : trc_names) {
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]);