forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan-hvlv-ccdb.cxx
More file actions
1125 lines (917 loc) · 36.2 KB
/
Copy pathscan-hvlv-ccdb.cxx
File metadata and controls
1125 lines (917 loc) · 36.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include <algorithm>
#include <ctime>
#include <fstream>
#include <iterator>
#include <limits>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#include <boost/program_options.hpp>
#include <fmt/format.h>
#include "TFile.h"
#include "TCanvas.h"
#include "TGraph.h"
#include "TH1F.h"
#include "TLine.h"
#include "TMultiGraph.h"
#include "TStyle.h"
#include "CCDB/BasicCCDBManager.h"
#include "CCDB/CcdbApi.h"
#include "CommonUtils/ConfigurableParam.h"
#include "DetectorsDCS/DataPointIdentifier.h"
#include "DetectorsDCS/DataPointValue.h"
#include "MCHConditions/DCSAliases.h"
#include "MCHStatus/HVStatusCreator.h"
#include "MCHStatus/StatusMapCreatorParam.h"
namespace po = boost::program_options;
using namespace o2;
using DPID = dcs::DataPointIdentifier;
using DPVAL = dcs::DataPointValue;
using DPMAP = std::unordered_map<DPID, std::vector<DPVAL>>;
using DPMAP2 = std::map<std::string, std::map<uint64_t, double>>;
using RBMAP = std::map<int, std::pair<uint64_t, uint64_t>>;
using DPBMAP = std::map<uint64_t, uint64_t>;
using ISSUE = std::tuple<uint64_t, uint64_t, double, double, std::string>;
using ISSUELIST = std::vector<ISSUE>;
using ISSUEMAP = std::map<std::string, ISSUELIST>;
//----------------------------------------------------------------------------
bool containsAKey(std::string data, const std::set<std::string>& Keys)
{
/// check if the data contains one of the keys
auto itKey = std::find_if(Keys.begin(), Keys.end(), [&data](const auto& key) {
return data.find(key) != data.npos;
});
return itKey != Keys.end();
}
//----------------------------------------------------------------------------
bool isValid(std::string alias)
{
/// check if the alias is a valid (part of a) DCS alias
static const std::vector<std::string> aliases =
mch::dcs::aliases({mch::dcs::MeasurementType::HV_V,
mch::dcs::MeasurementType::LV_V_FEE_ANALOG,
mch::dcs::MeasurementType::LV_V_FEE_DIGITAL,
mch::dcs::MeasurementType::LV_V_SOLAR});
auto itAlias = std::find_if(aliases.begin(), aliases.end(), [&alias](const auto& a) {
return a.find(alias) != a.npos;
});
return itAlias != aliases.end();
}
//----------------------------------------------------------------------------
void scanWhat(std::string what, std::string& path, bool& scanHV, bool& scanAll, std::set<std::string>& aliases)
{
/// get what to scan and where
static const std::set<std::string> hvKeys{"HV", "Quad", "Slat"};
static const std::set<std::string> lvKeys{"LV", "Group", "an", "di", "Sol"};
// HV or LV ?
path = "";
scanHV = false;
if (containsAKey(what, hvKeys)) {
path = "MCH/Calib/HV";
scanHV = true;
}
if (containsAKey(what, lvKeys)) {
if (scanHV) {
printf("error: cannot scan HV and LV channels at the same time\n");
exit(1);
}
path = "MCH/Calib/LV";
}
if (path.empty()) {
printf("error: no valid HV or LV channel to scan\n");
exit(1);
}
// everything or specific aliases ?
if (what.find(scanHV ? "HV" : "LV") != what.npos) {
scanAll = true;
aliases.clear();
} else {
scanAll = false;
std::istringstream input(what);
for (std::string alias; std::getline(input, alias, ',');) {
if (isValid(alias)) {
aliases.insert(alias);
} else {
printf("error: \"%s\" invalid (part of) HV or LV alias\n", alias.c_str());
exit(1);
}
}
}
}
//----------------------------------------------------------------------------
uint64_t ms2s(uint64_t ts)
{
/// convert the time stamp from ms to s
return (ts + 500) / 1000;
}
//----------------------------------------------------------------------------
std::string getTime(uint64_t ts)
{
/// convert the time stamp (ms) to local time
time_t t = ms2s(ts);
std::string time = std::ctime(&t);
time.pop_back(); // remove trailing \n
return time;
}
//----------------------------------------------------------------------------
std::set<int> getRuns(std::string runList)
{
/// read the runList from an ASCII file, or a comma separated run list, or a single run
std::set<int> runs{};
auto isNumber = [](std::string val) { return !val.empty() && val.find_first_not_of("0123456789") == val.npos; };
if (isNumber(runList)) {
runs.insert(std::stoi(runList));
} else if (runList.find(",") != runList.npos) {
std::istringstream input(runList);
for (std::string run; std::getline(input, run, ',');) {
if (isNumber(run)) {
runs.insert(std::stoi(run));
}
}
} else {
std::ifstream input(runList);
if (input.is_open()) {
for (std::string run; std::getline(input, run);) {
if (isNumber(run)) {
runs.insert(std::stoi(run));
}
}
}
}
return runs;
}
//----------------------------------------------------------------------------
RBMAP getRunBoundaries(ccdb::CcdbApi const& api, std::string runList)
{
/// return the SOR / EOR time stamps for every runs in the list
RBMAP runBoundaries{};
auto runs = getRuns(runList);
for (auto run : runs) {
auto boundaries = ccdb::CCDBManagerInstance::getRunDuration(api, run);
runBoundaries.emplace(run, boundaries);
}
return runBoundaries;
}
//----------------------------------------------------------------------------
void checkRunBoundaries(const RBMAP& runBoundaries)
{
/// check the consistency of the run time boundaries
if (runBoundaries.empty()) {
printf("error: no run found from the list\n");
exit(1);
}
bool error = false;
int previousRun = 0;
uint64_t endOfPreviousRun = 0;
for (const auto& [run, boundaries] : runBoundaries) {
if (boundaries.second <= boundaries.first) {
printf("error: run %d EOR <= SOR: %llu - %llu (%s - %s)\n",
run, boundaries.first, boundaries.second,
getTime(boundaries.first).c_str(), getTime(boundaries.second).c_str());
error = true;
}
if (boundaries.first <= endOfPreviousRun) {
printf("error: SOR run %d <= EOR run %d: %llu (%s) <= %llu (%s)\n",
run, previousRun, boundaries.first, getTime(boundaries.first).c_str(),
endOfPreviousRun, getTime(endOfPreviousRun).c_str());
error = true;
}
previousRun = run;
endOfPreviousRun = boundaries.second;
}
if (error) {
exit(1);
}
}
//----------------------------------------------------------------------------
void printRunBoundaries(const RBMAP& runBoundaries)
{
/// print the list of runs with their time boundaries
printf("\nlist of runs with their boundaries:\n");
printf("------------------------------------\n");
for (const auto& [run, boundaries] : runBoundaries) {
printf("%d: %llu - %llu (%s - %s)\n", run, boundaries.first, boundaries.second,
getTime(boundaries.first).c_str(), getTime(boundaries.second).c_str());
}
printf("------------------------------------\n");
}
//----------------------------------------------------------------------------
void drawRunBoudaries(const RBMAP& runBoundaries, TCanvas* c)
{
/// draw the run time boundaries
c->cd();
for (const auto& [run, boundaries] : runBoundaries) {
TLine* startRunLine = new TLine(ms2s(boundaries.first), c->GetUymin(), ms2s(boundaries.first), c->GetUymax());
startRunLine->SetUniqueID(run);
startRunLine->SetLineColor(4);
startRunLine->SetLineWidth(1);
startRunLine->Draw();
TLine* endRunLine = new TLine(ms2s(boundaries.second), c->GetUymin(), ms2s(boundaries.second), c->GetUymax());
endRunLine->SetUniqueID(run);
endRunLine->SetLineColor(2);
endRunLine->SetLineWidth(1);
endRunLine->Draw();
}
}
//----------------------------------------------------------------------------
DPBMAP getDPBoundaries(ccdb::CcdbApi const& api, std::string what,
uint64_t tStart, uint64_t tStop, uint64_t timeInterval)
{
/// get the time boundaries of every HV/LV files found in the time range
// add an extra margin (ms) of ± 1 min to the creation time,
// which corresponds to the end of the time interval covered by the file
static const uint64_t timeMarging = 60000;
std::istringstream fileInfo(api.list(what.c_str(), false, "text/plain",
tStop + timeInterval + timeMarging, tStart - timeMarging));
DPBMAP dpBoundaries{};
std::string dummy{};
uint64_t begin = 0;
uint64_t end = 0;
for (std::string line; std::getline(fileInfo, line);) {
if (line.find("Validity:") == 0) {
std::istringstream in(line);
in >> dummy >> begin >> dummy >> end;
dpBoundaries.emplace(begin, end);
}
}
if (dpBoundaries.empty()) {
printf("\e[0;31merror: no file found in %s in time range %llu - %llu (%s - %s) --> use the default one\e[0m\n",
what.c_str(), tStart, tStop, getTime(tStart).c_str(), getTime(tStop).c_str());
dpBoundaries.emplace(1, 9999999999999);
}
return dpBoundaries;
}
//----------------------------------------------------------------------------
void checkDPBoundaries(const DPBMAP& dpBoundaries, bool scanHV, uint64_t tStart, uint64_t tStop)
{
/// check the consistency of HV/LV file time boundaries
bool error = false;
if (dpBoundaries.begin()->first > tStart) {
printf("error: the beginning of the time range is not covered: %llu > %llu (%s > %s)\n",
dpBoundaries.begin()->first, tStart,
getTime(dpBoundaries.begin()->first).c_str(), getTime(tStart).c_str());
error = true;
}
if (dpBoundaries.rbegin()->second < tStop) {
printf("error: the end of the time range is not covered: %llu < %llu (%s < %s)\n",
dpBoundaries.rbegin()->second, tStop,
getTime(dpBoundaries.rbegin()->second).c_str(), getTime(tStop).c_str());
error = true;
}
uint64_t previousTStop = dpBoundaries.begin()->first;
for (auto [tStart, tStop] : dpBoundaries) {
if (tStop <= tStart) {
printf("error: EOF <= SOF: %llu - %llu (%s - %s)\n",
tStart, tStop, getTime(tStart).c_str(), getTime(tStop).c_str());
error = true;
}
if (tStart != previousTStop) {
printf("error: end of %s file != start of next %s file: %llu (%s) != %llu (%s))\n",
scanHV ? "HV" : "LV", scanHV ? "HV" : "LV",
previousTStop, getTime(previousTStop).c_str(), tStart, getTime(tStart).c_str());
error = true;
}
previousTStop = tStop;
}
if (error) {
exit(1);
}
}
//----------------------------------------------------------------------------
void printDPBoundaries(const DPBMAP& dpBoundaries, bool scanHV)
{
/// print the time boundaries of every HV/LV files found in the full time range
printf("\nlist of %s file time boundaries:\n", scanHV ? "HV" : "LV");
printf("------------------------------------\n");
for (auto [tStart, tStop] : dpBoundaries) {
printf("%llu - %llu (%s - %s)\n", tStart, tStop, getTime(tStart).c_str(), getTime(tStop).c_str());
}
printf("------------------------------------\n");
}
//----------------------------------------------------------------------------
double getLVLimit(std::string alias)
{
/// return the LV limit for that channel
static const double lvLimits[3] = {1.5, 1.5, 6.}; // FeeAnalog, FeeDigital, Solar
if (alias.find("an") != alias.npos) {
return lvLimits[0];
} else if (alias.find("di") != alias.npos) {
return lvLimits[1];
}
return lvLimits[2];
}
//----------------------------------------------------------------------------
void drawLimit(double limit, TCanvas* c)
{
/// draw the HV/LV limit for the displayed chamber
c->cd();
TLine* l = new TLine(c->GetUxmin(), limit, c->GetUxmax(), limit);
l->SetLineColor(1);
l->SetLineWidth(1);
l->SetLineStyle(2);
l->Draw();
}
//----------------------------------------------------------------------------
std::string getDuration(uint64_t tStart, uint64_t tStop)
{
/// get the duration (dd hh:mm:ss) between the two time stamps (ms)
auto dt = ms2s(tStop - tStart);
auto s = dt % 60;
auto m = (dt / 60) % 60;
auto h = (dt / 3600) % 24;
auto d = dt / 86400;
return fmt::format("{:02}d {:02}:{:02}:{:02}", d, h, m, s);
}
//----------------------------------------------------------------------------
double getValue(DPVAL dp)
{
/// return the value of this data point
union Converter {
uint64_t raw_data;
double value;
} converter;
converter.raw_data = dp.payload_pt1;
return converter.value;
}
//----------------------------------------------------------------------------
std::string getDE(std::string alias)
{
/// for DCS HV alias: return the corresponding DE (and sector)
/// for DCS LV alias: return an empty string
auto de = mch::dcs::aliasToDetElemId(alias);
if (de) {
return (mch::dcs::isQuadrant(mch::dcs::aliasToChamber(alias)))
? fmt::format("DE{}-{}", *de, mch::dcs::aliasToNumber(alias) % 10)
: fmt::format("DE{}", *de);
}
return "";
}
//----------------------------------------------------------------------------
void fillDataPoints(const std::vector<DPVAL>& dps, std::map<uint64_t, double>& dps2,
uint64_t tMin, uint64_t tMax, int warningLevel)
{
/// fill the map of data points
static const uint64_t tolerance = 5000;
if (dps.empty()) {
printf("error: the file does not contain any data point\n");
exit(1);
}
auto itDP = dps.begin();
auto ts = itDP->get_epoch_time();
std::string header = "warning:";
std::string color = (ts + tolerance < tMin || ts > tMin + tolerance) ? "\e[0;31m" : "\e[0;34m";
bool printWarning = warningLevel > 1 || (warningLevel == 1 && color == "\e[0;31m");
// check if the first data point is a copy of the last one from previous file
if (!dps2.empty()) {
auto previousTS = dps2.rbegin()->first;
if (ts != previousTS || getValue(*itDP) != dps2.rbegin()->second) {
if (ts <= previousTS) {
printf("error: wrong data point order (%llu <= %llu)\n", ts, previousTS);
exit(1);
}
if (printWarning) {
printf("%s%s missing the previous data point (dt = %s%llu ms)", color.c_str(), header.c_str(),
(previousTS < tMin) ? "-" : "+", (previousTS < tMin) ? tMin - previousTS : previousTS - tMin);
if (ts <= tMin) {
printf(" but get one at dt = -%llu ms\e[0m\n", tMin - ts);
} else {
printf("\e[0m\n");
}
header = " ";
}
}
}
// add the first data point (should be before the start of validity of the file)
if (ts >= tMax) {
printf("error: first data point exceeding file validity range (dt = +%llu ms)\n", ts - tMax);
exit(1);
} else if (ts > tMin && printWarning) {
printf("%s%s missing data point prior file start of validity (dt = +%llu ms)\e[0m\n",
color.c_str(), header.c_str(), ts - tMin);
header = " ";
}
dps2.emplace(ts, getValue(*itDP));
// add other data points (should be within the validity range of the file)
auto previousTS = ts;
for (++itDP; itDP < dps.end(); ++itDP) {
ts = itDP->get_epoch_time();
if (ts <= previousTS) {
printf("error: wrong data point order (%llu <= %llu)\n", ts, previousTS);
exit(1);
}
if (ts < tMin && (warningLevel > 1 || (warningLevel == 1 && ts + tolerance < tMin))) {
printf("%s%s data point outside of file validity range (dt = -%llu ms)\e[0m\n",
(ts + tolerance < tMin) ? "\e[0;31m" : "\e[0;34m", header.c_str(), tMin - ts);
} else if (ts >= tMax && warningLevel >= 1) {
printf("\e[0;31m%s data point outside of file validity range (dt = +%llu ms)\e[0m\n",
header.c_str(), ts - tMax);
}
dps2.emplace(ts, getValue(*itDP));
previousTS = ts;
}
}
//----------------------------------------------------------------------------
void selectDataPoints(DPMAP2 dpsMapsPerCh[10], uint64_t tStart, uint64_t tStop)
{
/// remove the data points outside of the given time range and, if needed,
/// add a data point at the boundaries with value equal to the preceding one
for (int ch = 0; ch < 10; ++ch) {
for (auto& [alias, dps] : dpsMapsPerCh[ch]) {
// get the first data point in the time range, remove the previous ones
// and add a data point with value equal to the preceding one if it exits
// or to this one otherwise
auto itFirst = dps.lower_bound(tStart);
if (itFirst != dps.begin()) {
double previousVal = std::prev(itFirst)->second;
for (auto it = dps.begin(); it != itFirst;) {
it = dps.erase(it);
}
dps.emplace(tStart, previousVal);
} else if (itFirst->first != tStart) {
if (itFirst->first > tStop) {
printf("error (%s): all data points are posterior to the end of the time range\n", alias.c_str());
} else {
printf("error (%s): first data point is posterior to the beginning of the time range\n", alias.c_str());
}
dps.emplace(tStart, itFirst->second);
}
// get the first data point exceeding the time range, remove it and the next ones
// and add a data point with value equal to the preceding one if needed
auto itLast = dps.upper_bound(tStop);
double previousVal = std::prev(itLast)->second;
for (auto it = itLast; it != dps.end();) {
it = dps.erase(it);
}
dps.emplace(tStop, previousVal);
}
}
}
//----------------------------------------------------------------------------
void printDataPoints(const DPMAP2 dpsMapsPerCh[10], std::string hvlvFormat, bool all)
{
/// print all the registered data points
const auto format1 = fmt::format(" %llu (%s): {} V\n", hvlvFormat.c_str());
const auto format2 = fmt::format(": %llu (%s): {} V -- %llu (%s): {} V\n",
hvlvFormat.c_str(), hvlvFormat.c_str());
for (int ch = 0; ch < 10; ++ch) {
printf("\n------------ chamber %d ------------\n", ch + 1);
for (const auto& [alias, dps] : dpsMapsPerCh[ch]) {
printf("- %s: %lu values", alias.c_str(), dps.size());
if (all) {
printf("\n");
for (const auto& [ts, val] : dps) {
printf(format1.c_str(), ts, getTime(ts).c_str(), val);
}
} else if (!dps.empty()) {
const auto firstdt = dps.begin();
const auto lastdt = dps.rbegin();
printf(format2.c_str(),
firstdt->first, getTime(firstdt->first).c_str(), firstdt->second,
lastdt->first, getTime(lastdt->first).c_str(), lastdt->second);
} else {
printf("\n");
}
}
}
}
//----------------------------------------------------------------------------
TGraph* mapToGraph(std::string alias, const std::map<uint64_t, double>& dps)
{
/// create a graph for the DCS channel and add the data points
TGraph* g = new TGraph(dps.size());
auto pos = alias.find(".");
auto shortAlias = alias.substr(0, pos);
auto de = getDE(alias);
auto title = de.empty() ? fmt::format("{}", shortAlias.c_str())
: fmt::format("{} ({})", de.c_str(), shortAlias.c_str());
g->SetNameTitle(alias.c_str(), title.c_str());
int i(0);
for (auto [ts, val] : dps) {
g->SetPoint(i, ms2s(ts), val);
++i;
}
g->SetMarkerSize(1.5);
g->SetMarkerStyle(2);
g->SetLineStyle(2);
return g;
}
//----------------------------------------------------------------------------
TCanvas* drawDataPoints(TMultiGraph* mg, double min, double max)
{
/// display the data points of the given chamber
TCanvas* c = new TCanvas(mg->GetName(), mg->GetHistogram()->GetTitle(), 1500, 900);
mg->Draw("A plc pmc");
mg->SetMinimum(min);
mg->SetMaximum(max);
mg->GetXaxis()->SetTimeDisplay(1);
mg->GetXaxis()->SetTimeFormat("%d/%m %H:%M");
mg->GetXaxis()->SetTimeOffset(0, "local");
mg->GetXaxis()->SetNdivisions(21010);
c->BuildLegend();
c->Update();
return c;
}
//----------------------------------------------------------------------------
void findIssues(const std::map<uint64_t, double>& dps, double limit, ISSUELIST& issues)
{
/// return the list of HV/LV issues (time range, min value, mean value) for each DCS channel
uint64_t tStart(0);
double min(0.);
double mean(0.);
uint64_t prevTS(0);
double prevVal(-1.);
for (auto [ts, val] : dps) {
if (val < limit) {
if (tStart == 0) {
// start a new issue...
tStart = ts;
min = val;
mean = 0.;
prevTS = ts;
prevVal = val;
} else {
// ... or complement the current one
min = std::min(min, val);
mean += prevVal * (ts - prevTS);
prevTS = ts;
prevVal = val;
}
} else if (tStart > 0) {
// complete the current issue, if any, and register it
mean += prevVal * (ts - prevTS);
mean /= (ts - tStart);
issues.emplace_back(tStart, ts, min, mean, "");
tStart = 0;
}
}
// complete the last issue, if any and its duration is != 0, and register it
if (tStart > 0 && prevTS != tStart) {
mean /= (prevTS - tStart);
issues.emplace_back(tStart, prevTS, min, mean, "");
}
}
//----------------------------------------------------------------------------
void fillO2Issues(const std::vector<mch::HVStatusCreator::TimeRange>& o2issues, ISSUELIST& issues,
uint64_t tMin, uint64_t tMax)
{
/// fill the list of issues from O2 (extend the previous one and/or create new ones)
// the list must not be empty
if (o2issues.empty()) {
printf("error: O2 returns an empty list of issues\n");
exit(1);
}
for (auto itIssue = o2issues.begin(); itIssue != o2issues.end(); ++itIssue) {
// exclude issues fully outside of the DP file boudaries
if (itIssue->end <= tMin || itIssue->begin >= tMax) {
printf("\e[0;35mwarning: skipping O2 issue outside of file boundaries (%llu - %llu)\e[0m\n",
itIssue->begin, itIssue->end);
continue;
}
// only the first issue could in principle extend before the start of the DP file, to O
if (itIssue->begin < tMin - mch::StatusMapCreatorParam::Instance().timeMargin &&
(itIssue != o2issues.begin() || itIssue->begin != 0)) {
printf("\e[0;35mwarning: O2 returns an issue with uncommon start time (%llu < %llu)\e[0m\n",
itIssue->begin, tMin - mch::StatusMapCreatorParam::Instance().timeMargin);
}
// only the last issue could in principle extend beyond the end of the DP file, to infinity
if (itIssue->end >= tMax + mch::StatusMapCreatorParam::Instance().timeMargin &&
(itIssue != std::prev(o2issues.end()) || itIssue->end != std::numeric_limits<uint64_t>::max())) {
printf("\e[0;35mwarning: O2 returns an issue with uncommon end time (%llu >= %llu)\e[0m\n",
itIssue->end, tMax + mch::StatusMapCreatorParam::Instance().timeMargin);
}
// extend the last issue in case of continuity accross the DP files or add a new one,
// restricting their time range within the DP file boundaries
if (itIssue->begin <= tMin && !issues.empty() && std::get<1>(issues.back()) == tMin) {
std::get<1>(issues.back()) = std::min(itIssue->end, tMax);
} else {
issues.emplace_back(std::max(itIssue->begin, tMin), std::min(itIssue->end, tMax), 0., 0., "");
}
}
}
//----------------------------------------------------------------------------
std::string findAffectedRuns(const RBMAP& runBoundaries, uint64_t tStart, uint64_t tStop)
{
/// return the list of affected runs in this time range
std::string runs;
for (const auto& [run, boundaries] : runBoundaries) {
if (boundaries.second <= tStart) {
continue;
} else if (boundaries.first >= tStop) {
break;
}
runs += fmt::format("{},", run);
}
if (!runs.empty()) {
runs.pop_back();
}
return runs;
}
//----------------------------------------------------------------------------
void selectIssues(ISSUEMAP issuesPerCh[10], const RBMAP& runBoundaries, uint64_t minDuration)
{
/// select HV/LV issues of a minimum duration (ms) occurring during runs
for (int ch = 0; ch < 10; ++ch) {
for (auto& issues : issuesPerCh[ch]) {
for (auto itIssue = issues.second.begin(); itIssue != issues.second.end();) {
auto tStart = std::get<0>(*itIssue);
auto tStop = std::get<1>(*itIssue);
if (tStop - tStart < minDuration) {
itIssue = issues.second.erase(itIssue);
} else {
auto runs = findAffectedRuns(runBoundaries, tStart, tStop);
if (runs.empty()) {
itIssue = issues.second.erase(itIssue);
} else {
std::get<4>(*itIssue) = runs;
++itIssue;
}
}
}
}
}
}
//----------------------------------------------------------------------------
void selectO2Issues(ISSUEMAP issuesPerCh[10], const RBMAP& runBoundaries)
{
/// select HV issues from O2 algorithm occurring during runs
/// and restrict the range of issues to the run range
for (int ch = 0; ch < 10; ++ch) {
for (auto& issues : issuesPerCh[ch]) {
for (auto itIssue = issues.second.begin(); itIssue != issues.second.end();) {
auto& tStart = std::get<0>(*itIssue);
auto& tStop = std::get<1>(*itIssue);
auto runs = findAffectedRuns(runBoundaries, tStart, tStop);
if (runs.empty()) {
itIssue = issues.second.erase(itIssue);
} else {
tStart = std::max(tStart, runBoundaries.begin()->second.first);
tStop = std::min(tStop, runBoundaries.rbegin()->second.second);
std::get<4>(*itIssue) = runs;
++itIssue;
}
}
}
}
}
//----------------------------------------------------------------------------
bool eraseIssue(const ISSUE& issue, ISSUELIST& issues)
{
/// find an issue with the same time range and associated run list and erase it
/// return true in case of success
auto itIssue = std::find_if(issues.begin(), issues.end(), [&issue](const auto& i) {
return (std::get<0>(i) == std::get<0>(issue) &&
std::get<1>(i) == std::get<1>(issue) &&
std::get<4>(i) == std::get<4>(issue));
});
if (itIssue != issues.end()) {
issues.erase(itIssue);
return true;
}
return false;
}
//----------------------------------------------------------------------------
void printIssues(const ISSUEMAP issuesPerCh[10], const ISSUEMAP o2IssuesPerCh[10],
bool scanHV, std::string hvlvFormat)
{
/// print all HV/LV issues
// copy the issues so that we can modify them (i.e. add empty lists or delete issues after printing)
ISSUEMAP issuesPerChCopy[10];
ISSUEMAP o2IssuesPerChCopy[10];
for (int ch = 0; ch < 10; ++ch) {
issuesPerChCopy[ch] = issuesPerCh[ch];
o2IssuesPerChCopy[ch] = o2IssuesPerCh[ch];
}
// make sure that all alias keys in the map o2IssuesPerChCopy are also in issuesPerChCopy in order to
// simplify the loop over all issues from both algorithms and fix the order in which they are printed
for (int ch = 0; ch < 10; ++ch) {
for (const auto& [alias, o2Issues] : o2IssuesPerChCopy[ch]) {
if (!o2Issues.empty()) {
issuesPerChCopy[ch].try_emplace(alias, ISSUELIST{});
}
}
}
auto printHeader = [](std::string alias) {
auto de = getDE(alias);
if (de.empty()) {
printf("Problem found for %s:\n", alias.c_str());
} else {
printf("Problem found for %s (%s):\n", alias.c_str(), de.c_str());
}
};
const auto format = fmt::format("%llu - %llu: %s (duration = %s, min = {} V, mean = {} V) --> run(s) %s\n",
hvlvFormat.c_str(), hvlvFormat.c_str());
auto printIssue = [&format](ISSUE issue, std::string color) {
const auto& [tStart, tStop, min, mean, runs] = issue;
printf("%s", color.c_str());
printf(format.c_str(), tStart, tStop,
getTime(tStart).c_str(), getDuration(tStart, tStop).c_str(), min, mean, runs.c_str());
printf("\e[0m");
};
if (scanHV) {
printf("\n------ list of issues from \e[0;31mthis macro only\e[0m, \e[0;35mO2 only\e[0m, or \e[0;32mboth\e[0m ------\n");
} else {
printf("\n------ list of issues ------\n");
}
bool foundIssues = false;
for (int ch = 0; ch < 10; ++ch) {
for (const auto& [alias, issues] : issuesPerChCopy[ch]) {
auto& o2Issues = o2IssuesPerChCopy[ch][alias];
if (!issues.empty() || !o2Issues.empty()) {
foundIssues = true;
printHeader(alias);
// print all issues found by this macro
for (const auto& issue : issues) {
// change color if the issue is not found by the O2 algorithm (only for HV)
std::string color = (scanHV && !eraseIssue(issue, o2Issues)) ? "\e[0;31m" : "\e[0;32m";
printIssue(issue, color);
}
// print other issues found by the O2 algorithm
for (const auto& issue : o2Issues) {
printIssue(issue, "\e[0;35m");
}
printf("----------------------------\n");
}
}
}
if (!foundIssues) {
printf("----------------------------\n");
}
}
//----------------------------------------------------------------------------
int main(int argc, char** argv)
{
/// scan HV or LV CCDB objects looking for issues
std::string runList = "";
std::string what = "";
std::string config = "";
uint64_t minDuration = 0;
uint64_t timeInterval = 30;
int warningLevel = 1;
int printLevel = 1;
std::string outFileName = "";
po::options_description usage("Usage");
// clang-format off
usage.add_options()
("help,h", "produce help message")
("runs,r",po::value<std::string>(&runList)->default_value(""),"run(s) to scan (comma separated list of runs or ASCII file with one run per line)")
("channels,c",po::value<std::string>(&what)->default_value(""),R"(channel(s) to scan ("HV" or "LV" or comma separated list of (part of) DCS aliases))")
("configKeyValues",po::value<std::string>(&config)->default_value(""),"Semicolon separated key=value strings to change HV thresholds")
("duration,d",po::value<uint64_t>(&minDuration)->default_value(0),"minimum duration (ms) of HV/LV issues to consider")
("interval,i",po::value<uint64_t>(&timeInterval)->default_value(30),"creation time interval (minutes) between CCDB files")
("warning,w",po::value<int>(&warningLevel)->default_value(1),"warning level (0, 1 or 2)")
("print,p",po::value<int>(&printLevel)->default_value(1),"print level (0, 1, 2 or 3)")
("output,o",po::value<std::string>(&outFileName)->default_value("scan.root"),"output root file name")
;
// clang-format on
po::options_description cmdline;
cmdline.add(usage);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(cmdline).run(), vm);
if (vm.count("help")) {
std::cout << "This program scans HV or LV channels looking for issues\n";
std::cout << usage << "\n";
return 2;
}
try {
po::notify(vm);
} catch (const po::error& e) {
std::cout << "error: " << e.what() << "\n";
exit(1);
}
if (runList.empty()) {
printf("error: you must provide run(s) to scan\n");
exit(1);
}
if (what.empty()) {
printf("error: you must provide channel(s) to scan\n");
exit(1);
}
// setup printout and display
const double hvRange[2] = {-10., 1700.};
const double lvRange[3] = {-1., 4., 8.}; // min, max FeeAnalog/FeeDigital, max Solar
const std::string hvFormat = "%7.2f";