Project
Loading...
Searching...
No Matches
LHCIFfileReader.h
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#ifndef GRP_LHCIF_FILE_READER_H_
13#define GRP_LHCIF_FILE_READER_H_
14
15#include "Rtypes.h"
16#include <gsl/span>
17#include "Framework/Logger.h"
20
22
23namespace o2
24{
25namespace grp
26{
28{
29 public:
30 LHCIFfileReader() = default; // default constructor
31 ~LHCIFfileReader() = default; // default destructor
32
33 void loadLHCIFfile(const std::string& fileName); // load LHCIF file
34 void loadLHCIFfile(gsl::span<const char> configBuf); // load LHCIF file from buffer
35 template <typename T>
36 void readValue(const std::string& alias, std::string& type, int& nel, int& nmeas, std::vector<std::pair<long, std::vector<T>>>& meas);
37
38 private:
39 std::string mFileBuffStr; // buffer containing content of LHC IF file
40
41 ClassDefNV(LHCIFfileReader, 1);
42};
43
44template <typename T>
45void LHCIFfileReader::readValue(const std::string& alias, std::string& type, int& nele, int& nmeas, std::vector<std::pair<long, std::vector<T>>>& meas)
46{
47 // look for value 'value' in the string from the LHC
48
49 auto posStart = mFileBuffStr.find(alias);
50 if (posStart == std::string::npos) {
51 LOG(info) << alias << " not found in LHC IF file";
52 return;
53 }
54 auto posEnd = mFileBuffStr.find("\n", posStart);
55 LOG(debug) << "posStart = " << posStart << ", posEnd = " << posEnd;
56 if (posEnd == std::string::npos) {
57 posEnd = mFileBuffStr.size();
58 }
59 std::string subStr = mFileBuffStr.substr(posStart, posEnd - posStart);
60 LOG(debug) << "subStr = " << subStr;
61 auto tokensStr = o2::utils::Str::tokenize(subStr, '\t', true, false);
62 LOG(debug) << "size of tokensStr = " << tokensStr.size();
63 if (tokensStr.size() < 5) {
64 LOG(fatal) << "Number of tokens too small: " << tokensStr.size() << ", should be at 5 (alias, type, nelements, value(s), timestamp(s)";
65 }
66 auto tokensStr_type = o2::utils::Str::tokenize(tokensStr[1], ':', true, false);
67 LOG(debug) << "size of tokensStr_type = " << tokensStr_type.size();
68
69 type = tokensStr_type[0];
70 LOG(debug) << "type = " << type;
71
72 nele = std::stoi(tokensStr_type[1]); // number of elements per measurement
73 nmeas = std::stoi(tokensStr[2]); // number of measurements
74 LOG(debug) << "nele = " << nele << ", nmeas = " << nmeas;
75 int shift = 3; // number of tokens that are not measurments (alias, type, number of measurements)
76
77 // RS: this check is wrong: the provided pair might be simply empty, but they are requested by the check above (Number of tokens too small...)
78 // if ((tokensStr.size() - shift) != (nele + 1) * nmeas) { // +1 to account for the timestamp
79 // LOG(fatal) << "Wrong number of pairs (value(s), timestamp): " << tokensStr.size() - 3 << ", should be " << (nele + 1) * nmeas;
80 // }
81 meas.reserve(nmeas);
82
83 for (int idx = 0; idx < nmeas; ++idx) {
84 std::vector<T> vect;
85 vect.reserve(nele);
86 if constexpr (std::is_same<T, int32_t>::value) {
87 if (type == "i" || type == "b") {
88 for (int iele = 0; iele < nele; ++iele) {
89 LOG(debug) << alias << ": value int/bool = " << tokensStr[shift + iele];
90 vect.emplace_back(std::stoi(tokensStr[shift + iele]));
91 }
92 } else {
93 LOG(fatal) << "templated function called with wrong type, should be int32_t or bool, but it is " << type;
94 }
95 } else if constexpr (std::is_same<T, float>::value) {
96 if (type == "f") {
97 for (int iele = 0; iele < nele; ++iele) {
98 LOG(debug) << alias << ": value float = " << tokensStr[shift + iele];
99 vect.emplace_back(std::stof(tokensStr[shift + iele]));
100 }
101 } else {
102 LOG(fatal) << "templated function called with wrong type, should be float";
103 }
104 }
105
106 else if constexpr (std::is_same<T, std::string>::value) {
107 if (type == "s") {
108 for (int iele = 0; iele < nele; ++iele) {
109 LOG(debug) << alias << ": value string = " << tokensStr[shift + iele];
110 vect.emplace_back(tokensStr[shift + iele]);
111 }
112 } else {
113 LOG(fatal) << "templated function called with wrong type, should be string";
114 }
115 }
116
117 LOG(debug) << "timestamp = " << std::stof(tokensStr[shift + nele]);
118 meas.emplace_back(std::stol(tokensStr[shift + nele]) * 1000, vect); // measurement comes in seconds, we want it in ms
119 }
120}
121
122} // namespace grp
123} // namespace o2
124#endif
Header to collect LHC related constants.
std::ostringstream debug
void readValue(const std::string &alias, std::string &type, int &nel, int &nmeas, std::vector< std::pair< long, std::vector< T > > > &meas)
void loadLHCIFfile(const std::string &fileName)
GLint GLint GLsizei GLint GLenum GLenum type
Definition glcorearb.h:275
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
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"