Project
Loading...
Searching...
No Matches
QualityControlFlagCollection.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12// O2 include
14#include "DataFormatsQualityControl/FlagTypeFactory.h"
15#include "Framework/Logger.h"
16
17#include <iostream>
18#include <boost/algorithm/string/replace.hpp>
19#include <boost/tokenizer.hpp>
20#include <utility>
21
22namespace o2::quality_control
23{
24
25constexpr const char* csvHeader = "start,end,flag_id,flag_name,flag_bad,comment,source";
26constexpr size_t csvColumns = 7;
27
28QualityControlFlagCollection::QualityControlFlagCollection(std::string name, std::string detector, RangeInterval validityRange,
29 int runNumber, std::string periodName, std::string passName,
30 std::string provenance)
31 : mName(std::move(name)), mDetID(std::move(detector)), mValidityRange(validityRange), mRunNumber(runNumber), mPeriodName(std::move(periodName)), mPassName(passName), mProvenance(std::move(provenance))
32{
33}
34
36{
37 mQualityControlFlags.insert(std::move(trf));
38}
39
41{
42 mQualityControlFlags.insert(trf);
43}
44
46{
47 return mQualityControlFlags.size();
48}
49
51{
52 if (mDetID != other.mDetID) {
53 // We assume that one QualityControlFlagCollection should correspond to one detector at most.
54 // However, if this becomes annoying, we can reconsider it.
55 throw std::runtime_error(
56 "The detector ID of the target collection '" + mDetID + "' is different than the other '" + mDetID);
57 }
58 mQualityControlFlags.merge(other.mQualityControlFlags);
59}
60
66
67QualityControlFlagCollection::collection_t::const_iterator QualityControlFlagCollection::begin() const
68{
69 return mQualityControlFlags.begin();
70}
71
72QualityControlFlagCollection::collection_t::const_iterator QualityControlFlagCollection::end() const
73{
74 return mQualityControlFlags.end();
75}
76
78{
79 auto escapeComma = [](const std::string& str) {
80 return boost::algorithm::replace_all_copy(str, ",", "\\,");
81 };
82 output << csvHeader << '\n';
83 for (const auto& trf : *this) {
84 output << fmt::format("{},{},{},\"{}\",{:d},\"{}\",\"{}\"\n",
85 trf.getStart(), trf.getEnd(),
86 trf.getFlag().getID(), escapeComma(trf.getFlag().getName()), trf.getFlag().getBad(),
87 escapeComma(trf.getComment()), escapeComma(trf.getSource()));
88 }
89}
90
92{
93 std::string line;
94 std::getline(input, line);
95 if (line != csvHeader) {
96 throw std::runtime_error(
97 "Unsupported TRFCollection format, the first line is \"" + line + "\" instead of \"" + csvHeader + "\"");
98 }
99
100 while (std::getline(input, line)) {
101 boost::tokenizer<boost::escaped_list_separator<char>> tok(line);
102
105 FlagType flag = FlagTypeFactory::Invalid();
106 std::string comment;
107 std::string source;
108 auto it = tok.begin();
109 bool valid = true;
110 size_t pos = 0;
111 for (; it != tok.end() && valid; pos++, it++) {
112 switch (pos) {
113 case 0: {
114 if (it->empty()) {
115 LOG(error) << "Invalid line, empty start time of a flag, skipping...";
116 valid = false;
117 } else {
118 start = static_cast<QualityControlFlag::time_type>(std::stoull(*it));
119 }
120 break;
121 }
122 case 1: {
123 if (it->empty()) {
124 LOG(error) << "Invalid line, empty end time of a flag, skipping...";
125 valid = false;
126 } else {
127 end = static_cast<QualityControlFlag::time_type>(std::stoull(*it));
128 }
129 break;
130 }
131 case 2: {
132 if (it->empty()) {
133 LOG(error) << "Invalid line, empty flag id, skipping...";
134 valid = false;
135 } else {
136 flag.mId = std::stoul(*it);
137 }
138 break;
139 }
140 case 3: {
141 if (it->empty()) {
142 LOG(error) << "Invalid line, empty flag name, skipping...";
143 valid = false;
144 } else {
145 flag.mName = *it;
146 }
147 break;
148 }
149 case 4: {
150 if (it->empty()) {
151 LOG(error) << "Invalid line, empty flag 'bad' field, skipping...";
152 valid = false;
153 } else {
154 flag.mBad = static_cast<bool>(std::stoul(*it));
155 }
156 break;
157 }
158 case 5: {
159 comment = *it;
160 break;
161 }
162 case 6: {
163 source = *it;
164 break;
165 }
166 default: {
167 LOG(error) << "More columns (" << pos + 1 << ") than expected (" << csvColumns
168 << ") in this line, skipping...";
169 valid = false;
170 break;
171 }
172 }
173 }
174 if (valid && pos < csvColumns) {
175 LOG(error) << "Less columns (" << pos << ") than expected (" << csvColumns << ") in this line, skipping...";
176 } else if (valid) {
177 insert({start, end, flag, comment, source});
178 }
179 }
180}
181
182std::ostream& operator<<(std::ostream& output, const QualityControlFlagCollection& data)
183{
184 data.streamTo(output);
185 return output;
186}
187
189{
190 return mName;
191}
192
194{
195 return mDetID;
196}
197
199{
200 return mRunNumber;
201}
202
204{
205 return mPeriodName;
206}
207
209{
210 return mPassName;
211}
213{
214 return mProvenance;
215}
216
217} // namespace o2::quality_control
void output(const std::map< std::string, ChannelStat > &channels)
Definition rawdump.cxx:197
classes for defining time ranges with a certain mask to be able to cut on
uint16_t pos
Definition RawData.h:3
void streamTo(std::ostream &output) const
write data to ostream
QualityControlFlagCollection(std::string name, std::string detector="TST", RangeInterval validityRange={}, int runNumber=0, std::string periodName="Invalid", std::string passName="Invalid", std::string provenance="qc")
void streamFrom(std::istream &input)
Read data from instream.
GLuint GLuint end
Definition glcorearb.h:469
GLuint const GLchar * name
Definition glcorearb.h:781
GLsizei GLsizei GLchar * source
Definition glcorearb.h:798
GLboolean * data
Definition glcorearb.h:298
GLuint start
Definition glcorearb.h:469
std::ostream & operator<<(std::ostream &os, FlagType const &my)
Definition FlagType.cxx:26
Defining DataPointCompositeObject explicitly as copiable.
VectorOfTObjectPtrs other
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
const std::string str