Project
Loading...
Searching...
No Matches
statusmap-to-rejectlist.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
13
14#include <map>
15#include <string>
16#include <tuple>
17#include <vector>
18
19#include <boost/program_options.hpp>
20
21#include <TFile.h>
22#include <TTreeReader.h>
23#include <TTreeReaderValue.h>
24
25#include "CCDB/CcdbApi.h"
27#include "Framework/Logger.h"
29#include "MCHStatus/StatusMap.h"
30
31namespace po = boost::program_options;
32
33using BadChannelsVector = std::vector<o2::mch::DsChannelId>;
34
35//____________________________________________________________________________________
36std::tuple<TFile*, TTreeReader*> loadData(const std::string inFile)
37{
39
40 TFile* f = TFile::Open(inFile.c_str(), "READ");
41 if (!f || f->IsZombie()) {
42 LOG(error) << "opening file " << inFile << " failed";
43 exit(2);
44 }
45
46 TTreeReader* r = new TTreeReader("o2sim", f);
47 if (r->IsZombie()) {
48 LOG(error) << "tree o2sim not found";
49 exit(2);
50 }
51
52 return std::make_tuple(f, r);
53}
54
55//____________________________________________________________________________________
56int size(const std::map<int, std::vector<int>>& badChannels)
57{
59
60 int n = 0;
61
62 for (const auto& channels : badChannels) {
63 n += channels.second.size();
64 }
65
66 return n;
67};
68
69//____________________________________________________________________________________
70void printContent(const std::string inFile, const uint32_t mask)
71{
73
74 auto [dataFile, dataReader] = loadData(inFile);
75 TTreeReaderValue<o2::mch::StatusMap> statusMap(*dataReader, "statusmaps");
76
77 int iTF(-1);
78 int firstTF(-1);
79 int lastTF(-1);
80 std::map<int, std::vector<int>> currentBadChannels{};
81
82 while (dataReader->Next()) {
83 ++iTF;
84
85 // record the first status map
86 if (firstTF < 0) {
87 firstTF = iTF;
88 lastTF = iTF;
89 currentBadChannels = o2::mch::applyMask(*statusMap, mask);
90 continue;
91 }
92
93 auto badChannels = o2::mch::applyMask(*statusMap, mask);
94
95 // extend the TF range of the current status map if it did not change
96 if (badChannels == currentBadChannels) {
97 lastTF = iTF;
98 continue;
99 }
100
101 // print the current status map
102 LOGP(info, "TF [{}, {}]: the status map contains {} bad channels in {} detection element{} (using statusMask=0x{:x})",
103 firstTF, lastTF, size(currentBadChannels), currentBadChannels.size(), currentBadChannels.size() > 1 ? "s" : "", mask);
104
105 // update the current status map
106 firstTF = iTF;
107 lastTF = iTF;
108 currentBadChannels = badChannels;
109 }
110
111 // print the last status map
112 LOGP(info, "TF [{}, {}]: the status map contains {} bad channels in {} detection element{} (using statusMask=0x{:x})",
113 firstTF, lastTF, size(currentBadChannels), currentBadChannels.size(), currentBadChannels.size() > 1 ? "s" : "", mask);
114
115 dataFile->Close();
116}
117
118//____________________________________________________________________________________
119void dumpContent(const std::string inFile, const size_t iTF, const uint32_t mask)
120{
122
123 auto [dataFile, dataReader] = loadData(inFile);
124 TTreeReaderValue<o2::mch::StatusMap> statusMap(*dataReader, "statusmaps");
125
126 if (dataReader->SetEntry(iTF) != TTreeReader::kEntryValid) {
127 LOGP(error, "invalid TF index {} (number of TFs = {})", iTF, dataReader->GetEntries());
128 exit(3);
129 }
130
131 LOGP(info, "status map content for TF {} with statusMask=0x{:x}:", iTF, mask);
132
133 for (const auto& status : *statusMap) {
134 if ((mask & status.second) != 0) {
135 auto channel = status.first;
136 if (!channel.isValid()) {
137 LOGP(error, "invalid channel with status {}", status.second);
138 } else {
139 LOGP(info, "{} status {}", asString(channel), status.second);
140 }
141 }
142 }
143
144 dataFile->Close();
145}
146
147//____________________________________________________________________________________
148BadChannelsVector statusMap2RejectList(const std::string inFile, const size_t iTF, const uint32_t mask)
149{
151
152 auto [dataFile, dataReader] = loadData(inFile);
153 TTreeReaderValue<o2::mch::StatusMap> statusMap(*dataReader, "statusmaps");
154
155 if (dataReader->SetEntry(iTF) != TTreeReader::kEntryValid) {
156 LOGP(error, "invalid TF index {} (number of TFs = {})", iTF, dataReader->GetEntries());
157 exit(3);
158 }
159
161
162 for (const auto& status : *statusMap) {
163 auto channel = status.first;
164 if (!channel.isValid()) {
165 LOG(error) << "invalid channel";
166 }
167 if ((mask & status.second) != 0) {
168 const auto c = o2::mch::DsChannelId(channel.getSolarId(), channel.getElinkId(), channel.getChannel());
169 bv.emplace_back(c);
170 }
171 }
172
173 dataFile->Close();
174
175 LOGP(info, "the reject list contains {} bad channels (using statusMask=0x{:x})", bv.size(), mask);
176
177 return bv;
178}
179
180//____________________________________________________________________________________
181void uploadRejectList(const std::string ccdbUrl, uint64_t startTS, uint64_t endTS, const BadChannelsVector& bv)
182{
184 api.init(ccdbUrl);
185 std::map<std::string, std::string> md;
186
187 LOGP(info, "storing MCH RejectList (valid from {} to {}) to MCH/Calib/RejectList",
188 startTS, endTS);
189
190 api.storeAsTFileAny(&bv, "MCH/Calib/RejectList", md, startTS, endTS);
191}
192
193//____________________________________________________________________________________
194int main(int argc, char** argv)
195{
196 po::variables_map vm;
197 po::options_description usage("Usage");
198
199 std::string ccdbUrl;
200 uint64_t startTS;
201 uint64_t endTS;
202 std::string inFile;
203 size_t iTF;
204 uint32_t mask;
205 bool print;
206 bool dump;
207
208 auto tnow = std::chrono::system_clock::now().time_since_epoch();
209 using namespace std::chrono_literals;
210 auto tend = tnow + 24h;
211 uint64_t now = std::chrono::duration_cast<std::chrono::milliseconds>(tnow).count();
212 uint64_t end = std::chrono::duration_cast<std::chrono::milliseconds>(tend).count();
213
215
216 // clang-format off
217 usage.add_options()
218 ("help,h", "produce help message")
219 ("ccdb,c",po::value<std::string>(&ccdbUrl)->default_value("http://localhost:6464"),"ccdb url")
220 ("starttimestamp,st",po::value<uint64_t>(&startTS)->default_value(now),"timestamp for query or put - (default=now)")
221 ("endtimestamp,et", po::value<uint64_t>(&endTS)->default_value(end), "end of validity (for put) - default=1 day from now")
222 ("infile,f",po::value<std::string>(&inFile)->default_value("mchstatusmaps.root"),"input file of StatusMap objects")
223 ("tf,i", po::value<size_t>(&iTF)->default_value(0), "index of the TF to process")
224 ("mask,m", po::value<uint32_t>(&mask)->default_value(defaultMask), "mask to apply to the statusMap to produce the RejectList")
225 ("print,p",po::bool_switch(&print),"print the content of the input file without processing it")
226 ("dump,d",po::bool_switch(&dump),"dump the raw content of the input file without processing it")
227 ;
228 // clang-format on
229
230 po::options_description cmdline;
231 cmdline.add(usage);
232
233 po::store(po::command_line_parser(argc, argv).options(cmdline).run(), vm);
234
235 if (vm.count("help")) {
236 LOG(info) << "This program converts a StatusMap to a RejectList CCDB object";
237 LOG(info) << usage;
238 return 2;
239 }
240
241 try {
242 po::notify(vm);
243 } catch (boost::program_options::error& e) {
244 LOG(error) << e.what();
245 exit(1);
246 }
247
248 if (dump) {
249 dumpContent(inFile, iTF, mask);
250 } else if (print) {
251 printContent(inFile, mask);
252 } else {
253 auto bv = statusMap2RejectList(inFile, iTF, mask);
254 uploadRejectList(ccdbUrl, startTS, endTS, bv);
255 }
256
257 return 0;
258}
std::string asString(TDataMember const &dm, char *pointer)
void print() const
std::vector< o2::mch::DsChannelId > BadChannelsVector
uint32_t c
Definition RawData.h:2
Class for time synchronization of RawReader instances.
int storeAsTFileAny(const T *obj, std::string const &path, std::map< std::string, std::string > const &metadata, long startValidityTimestamp=-1, long endValidityTimestamp=-1, std::vector< char >::size_type maxSize=0) const
Definition CcdbApi.h:157
void init(std::string const &hosts)
Definition CcdbApi.cxx:165
void dump(const std::string what, DPMAP m, int verbose)
Definition dcs-ccdb.cxx:79
GLdouble n
Definition glcorearb.h:1982
GLsizeiptr size
Definition glcorearb.h:659
GLuint GLuint end
Definition glcorearb.h:469
GLdouble f
Definition glcorearb.h:310
GLboolean r
Definition glcorearb.h:1233
GLint GLuint mask
Definition glcorearb.h:291
GLsizeiptr const void GLenum usage
Definition glcorearb.h:659
std::map< int, std::vector< int > > applyMask(const o2::mch::StatusMap &statusMap, uint32_t mask)
BadChannelsVector statusMap2RejectList(const std::string inFile, const size_t iTF, const uint32_t mask)
std::tuple< TFile *, TTreeReader * > loadData(const std::string inFile)
void printContent(const std::string inFile, const uint32_t mask)
void dumpContent(const std::string inFile, const size_t iTF, const uint32_t mask)
void uploadRejectList(const std::string ccdbUrl, uint64_t startTS, uint64_t endTS, const BadChannelsVector &bv)
std::string ccdbUrl
#define main
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
std::vector< ChannelData > channels