20#include <fairlogger/Logger.h>
29 for (
auto const& cls : clslist) {
43 for (
int i = 0;
i < 6;
i++) {
44 cnts0[cls.first][
i] = cls.second[
i];
48 std::array<uint64_t, 6> cntsbk{0};
50 for (
int i = 0;
i < 6;
i++) {
55 cntsbk[
i] = (uint64_t)cls.second[
i] + 0xffffffffull *
overflows[cls.first][
i] - (uint64_t)
cnts0[cls.first][
i];
60 BKClient->triggerCounters()->createOrUpdateForRun(runNumber, clsname, ts, cntsbk[0], cntsbk[1], cntsbk[2], cntsbk[3], cntsbk[4], cntsbk[5]);
61 }
catch (std::runtime_error& error) {
62 std::cerr <<
"An error occurred: " << error.what() << std::endl;
65 LOG(
debug) <<
"Run BK:" << runNumber <<
" class:" << clsname <<
" cls.first" << cls.first <<
" ts:" << ts <<
" cnts:" << cntsbk[0] <<
" " << cntsbk[1] <<
" " << cntsbk[2] <<
" " << cntsbk[3] <<
" " << cntsbk[4] <<
" " << cntsbk[5];
74 for (uint32_t
i = 0;
i < NRUNS;
i++) {
75 mActiveRuns[
i] =
nullptr;
78 if (mBKHost !=
"none") {
79 mBKClient = BkpClientFactory::create(mBKHost);
80 LOG(info) <<
"BK Client created with:" << mBKHost;
82 LOG(info) <<
"BK not sent";
86 LOG(info) <<
"QCDB writing every:" << mQCWritePeriod <<
" 10 secs";
88 LOG(info) <<
"CTP vNew cfg:" << mNew;
89 LOG(info) <<
"CTPRunManager initialised.";
93 LOG(info) <<
"Loading run: " << cfg;
94 auto now = std::chrono::system_clock::now();
95 long timeStamp = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
96 LOG(info) <<
"Timestamp real time:" << timeStamp;
97 size_t pos = cfg.find(
" ");
98 std::string cfgmod = cfg;
99 if (
pos == std::string::npos) {
100 LOG(warning) <<
"Space not found in CTP config";
102 std::string
f = cfg.substr(0,
pos);
103 if (
f.find(
"run") == std::string::npos) {
104 double_t
tt = std::stold(
f);
105 timeStamp = (
tt * 1000.);
106 LOG(info) <<
"Timestamp file:" << timeStamp;
107 cfgmod = cfg.substr(
pos, cfg.size());
108 LOG(info) <<
"ctpcfg: using ctp time";
122 mRunsLoaded[runnumber] = activerun;
133 LOG(info) <<
"Stopping run index: " << irun;
134 if (mActiveRuns[irun] ==
nullptr) {
135 LOG(error) <<
"No config for run index:" << irun;
140 mActiveRuns[irun]->timeStop = timeStamp * 1000.;
141 saveRunScalersToCCDB(mActiveRuns[irun]->scalers, mActiveRuns[irun]->timeStart, mActiveRuns[irun]->timeStop);
142 saveRunScalersToQCDB(mActiveRuns[irun]->scalers, mActiveRuns[irun]->timeStart, mActiveRuns[irun]->timeStop);
143 delete mActiveRuns[irun];
144 mActiveRuns[irun] =
nullptr;
149 if (mActiveRuns[irun] ==
nullptr) {
150 LOG(error) <<
"No config for run index:" << irun;
153 std::string orb =
"extorb";
154 std::string orbitid =
"orbitid";
157 std::vector<int> clslist = mActiveRuns[irun]->cfg.getTriggerClassList();
158 for (
auto const& cls : clslist) {
168 scalraw.
lmBefore = mCounters[mScalerName2Position[cmb]];
169 scalraw.
lmAfter = mCounters[mScalerName2Position[cma]];
170 scalraw.
l0Before = mCounters[mScalerName2Position[c0b]];
171 scalraw.
l0After = mCounters[mScalerName2Position[c0a]];
172 scalraw.
l1Before = mCounters[mScalerName2Position[c1b]];
173 scalraw.
l1After = mCounters[mScalerName2Position[c1a]];
176 scalrec.
scalers.push_back(scalraw);
188 mActiveRuns[irun]->send2BK(mBKClient,
time,
start);
192 for (uint32_t
i = 0;
i < NINPS;
i++) {
193 uint32_t inpcount = mCounters[
offset +
i];
201 scalrec.
intRecord.
orbit = mCounters[mScalerName2Position[orbitid]];
204 mActiveRuns[irun]->scalers.addScalerRacordRaw(scalrec);
212 LOG(info) <<
"Processing message with topic:" << topic;
213 std::string firstcounters;
214 if (topic.find(
"clear") != std::string::npos) {
216 LOG(info) <<
"Loaded runs cleared.";
219 if (topic.find(
"ctpconfig") != std::string::npos) {
220 LOG(info) <<
"ctpconfig received";
224 if (topic.find(
"sox") != std::string::npos) {
226 size_t irun =
message.find(
"run");
227 if (irun == std::string::npos) {
228 LOG(warning) <<
"run keyword not found in SOX";
231 LOG(info) <<
"SOX received, Run keyword position:" << irun;
234 firstcounters =
message.substr(0, irun);
236 if (topic.find(
"eox") != std::string::npos) {
237 LOG(info) <<
"EOX received";
240 static int nerror = 0;
241 if (topic ==
"rocnts") {
243 LOG(warning) <<
"Skipping topic rocnts";
249 std::vector<std::string> tokens;
250 if (firstcounters.size() > 0) {
258 LOG(warning) <<
"v2 scaler size";
264 double timeStamp = std::stold(tokens.at(0));
265 std::time_t
tt = timeStamp;
266 LOG(info) <<
"Processing scalers, all good, time:" << tokens.at(0) <<
" " << std::asctime(std::localtime(&
tt));
267 for (uint32_t
i = 1;
i < tokens.size();
i++) {
268 mCounters[
i - 1] = std::stoull(tokens.at(
i));
269 if (
i < (NRUNS + 1)) {
270 std::cout << mCounters[
i - 1] <<
" ";
273 std::cout << std::endl;
274 LOG(info) <<
"Counter size:" << tokens.size();
276 for (uint32_t
i = 0;
i < NRUNS;
i++) {
277 if ((mCounters[
i] == 0) && (mActiveRunNumbers[
i] == 0)) {
279 }
else if ((mCounters[
i] != 0) && (mActiveRunNumbers[
i] == mCounters[
i])) {
281 LOG(info) <<
"Run continue:" << mCounters[
i];
284 if (mActiveRuns[
i]->qcwpcount > mQCWritePeriod) {
286 mActiveRuns[
i]->qcwpcount = 0;
288 mActiveRuns[
i]->qcwpcount++;
289 }
else if ((mCounters[
i] != 0) && (mActiveRunNumbers[
i] == 0)) {
290 LOG(info) <<
"Run started:" << mCounters[
i];
291 auto run = mRunsLoaded.find(mCounters[
i]);
292 if (run != mRunsLoaded.end()) {
293 mActiveRunNumbers[
i] = mCounters[
i];
294 mActiveRuns[
i] = run->second;
295 mRunsLoaded.erase(run);
299 LOG(error) <<
"Trying to start run which is not loaded:" << mCounters[
i];
301 }
else if ((mCounters[
i] == 0) && (mActiveRunNumbers[
i] != 0)) {
303 LOG(error) <<
"Internal error in processMessage: mEOX != 1 expected 0: mEOX:" << mEOX;
305 LOG(info) <<
"Run stopped:" << mActiveRunNumbers[
i];
307 mActiveRunNumbers[
i] = 0;
318 std::cout <<
"Active runs:";
319 for (
auto const& arun : mActiveRunNumbers) {
320 std::cout << arun <<
" ";
322 std::cout <<
" #loaded runs:" << mRunsLoaded.size();
323 for (
auto const& lrun : mRunsLoaded) {
324 std::cout <<
" " << lrun.second->cfg.getRunNumber();
326 std::cout << std::endl;
343 for (
int i = 0;
i < NRUNS;
i++) {
344 if (mActiveRuns[
i] !=
nullptr) {
355 int NLTG_start = NRUNS;
356 int NCLKFP_start = NLTG_start + NDET * 32;
357 int NINPS_start = NCLKFP_start + 7;
358 int NCLS_start = NINPS_start + NINPS;
359 std::cout <<
"====> CTP counters:" << std::endl;
360 std::cout <<
"RUNS:" << std::endl;
362 for (uint32_t
i = 0;
i < NRUNS;
i++) {
363 std::cout << ipos <<
":" << mCounters[
i] <<
" ";
366 std::cout << std::endl;
367 for (
int i = 0;
i < NDET;
i++) {
368 std::cout <<
"LTG" <<
i + 1 << std::endl;
369 for (
int j = NLTG_start +
i * 32;
j < NLTG_start + (
i + 1) * 32;
j++) {
370 std::cout << ipos <<
":" << mCounters[
j] <<
" ";
373 std::cout << std::endl;
375 std::cout <<
"BC40,BC240,Orbit,pulser, fastlm, busy,spare" << std::endl;
376 for (
int i = NCLKFP_start;
i < NCLKFP_start + 7;
i++) {
377 std::cout << ipos <<
":" << mCounters[
i] <<
" ";
380 std::cout << std::endl;
381 std::cout <<
"INPUTS:" << std::endl;
382 for (
int i = NINPS_start;
i < NINPS_start + NINPS;
i++) {
383 std::cout << ipos <<
":" << mCounters[
i] <<
" ";
386 std::cout << std::endl;
387 std::cout <<
"CLASS M Before" << std::endl;
388 for (
int i = NCLS_start;
i < NCLS_start + 64;
i++) {
389 std::cout << ipos <<
":" << mCounters[
i] <<
" ";
392 std::cout << std::endl;
393 std::cout <<
"CLASS M After" << std::endl;
394 for (
int i = NCLS_start + 64;
i < NCLS_start + 2 * 64;
i++) {
395 std::cout << ipos <<
":" << mCounters[
i] <<
" ";
398 std::cout << std::endl;
399 std::cout <<
"CLASS 0 Before" << std::endl;
400 for (
int i = NCLS_start + 2 * 64;
i < NCLS_start + 3 * 64;
i++) {
401 std::cout << ipos <<
":" << mCounters[
i] <<
" ";
404 std::cout << std::endl;
405 std::cout <<
"CLASS 0 After" << std::endl;
406 for (
int i = NCLS_start + 3 * 64;
i < NCLS_start + 4 * 64;
i++) {
407 std::cout << ipos <<
":" << mCounters[
i] <<
" ";
410 std::cout << std::endl;
411 std::cout <<
"CLASS 1 Before" << std::endl;
412 for (
int i = NCLS_start + 4 * 64;
i < NCLS_start + 5 * 64;
i++) {
413 std::cout << ipos <<
":" << mCounters[
i] <<
" ";
416 std::cout << std::endl;
417 std::cout <<
"CLASS 1 After" << std::endl;
418 for (
int i = NCLS_start + 5 * 64;
i < NCLS_start + 6 * 64;
i++) {
419 std::cout << ipos <<
":" << mCounters[
i] <<
" ";
422 std::cout << std::endl;
423 std::cout <<
" REST:" << std::endl;
424 for (uint32_t
i = NCLS_start + 6 * 64;
i < mCounters.size();
i++) {
425 if ((ipos % 10) == 0) {
426 std::cout << std::endl;
427 std::cout << ipos <<
":";
429 std::cout << mCounters[
i] <<
" ";
Managing runs for config and scalers.
std::vector< int > getTriggerClassList() const
uint64_t getTriggerClassMask() const
std::string getClassNameFromHWIndex(int index)
o2::detectors::DetID::mask_t getDetectorMask() const
void printStream(std::ostream &stream) const
int loadConfigurationRun3(const std::string &ctpconfiguartion)
int stopRun(uint32_t irun, long timeStamp)
int startRun(const std::string &cfg)
void printActiveRuns() const
int processMessage(std::string &topic, const std::string &message)
int loadRun(const std::string &cfg)
int addScalers(uint32_t irun, std::time_t time, bool start=0)
static constexpr uint32_t NCOUNTERS
static constexpr uint32_t NCOUNTERSv2
void setDetectorMask(o2::detectors::DetID::mask_t mask)
static std::vector< std::string > scalerNames
void setRunNumber(uint32_t rnumber)
void setClassMask(std::bitset< CTP_NCLASSES > classMask)
static std::string mCCDBHost
Database constants.
int saveRunScalersToQCDB(CTPRunScalers &scalers, long timeStart, long timeStop)
static std::string mQCDBHost
static void setQCDBHost(std::string host)
int saveRunScalersToCCDB(CTPRunScalers &scalers, long timeStart, long timeStop)
int saveRunConfigToCCDB(CTPConfiguration *cfg, long timeStart)
GLuint GLsizei const GLchar * message
std::string to_string(gsl::span< T, Size > span)
uint16_t bc
bunch crossing ID of interaction
int send2BK(std::unique_ptr< BkpClient > &BKClient, size_t ts, bool start)
std::vector< CTPScalerRaw > scalers
o2::InteractionRecord intRecord
std::vector< uint32_t > scalersInps
static std::vector< std::string > tokenize(const std::string &src, char delim, bool trimToken=true, bool skipEmpty=true)
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"