|
| 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" |
| 18 | +#include <boost/tokenizer.hpp> |
| 19 | +#include <boost/lexical_cast.hpp> |
| 20 | + |
| 21 | +/// @brief Class to read the LHC InterFace file coming from the DCS filepush service |
| 22 | + |
| 23 | +namespace o2 |
| 24 | +{ |
| 25 | +namespace grp |
| 26 | +{ |
| 27 | +class LHCIFfileReader |
| 28 | +{ |
| 29 | + public: |
| 30 | + LHCIFfileReader() = default; // default constructor |
| 31 | + ~LHCIFfileReader() = default; // default destructor |
| 32 | + |
| 33 | + void loadLHCIFfile(const char* 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 char* alias, std::string& type, int& nel, int& nmeas, std::vector<std::pair<float, 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 | + |
| 44 | +template <typename T> |
| 45 | +void LHCIFfileReader::readValue(const char* alias, std::string& type, int& nele, int& nmeas, std::vector<std::pair<float, 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 | + boost::char_separator<char> sep("\t"); |
| 62 | + boost::tokenizer<boost::char_separator<char>> tokens(subStr, sep); |
| 63 | + std::vector<std::string> tokensStr{begin(tokens), end(tokens)}; |
| 64 | + LOG(DEBUG) << "size of tokensStr = " << tokensStr.size(); |
| 65 | + if (tokensStr.size() < 5) { |
| 66 | + LOG(FATAL) << "Number of tokens too small: " << tokensStr.size() << ", should be at 5 (alias, type, nelements, value(s), timestamp(s)"; |
| 67 | + } |
| 68 | + boost::char_separator<char> sep_type(":"); |
| 69 | + boost::tokenizer<boost::char_separator<char>> tokens_type(tokensStr[1], sep_type); |
| 70 | + std::vector<std::string> tokensStr_type{begin(tokens_type), end(tokens_type)}; |
| 71 | + LOG(DEBUG) << "size of tokensStr_type = " << tokensStr_type.size(); |
| 72 | + |
| 73 | + type = tokensStr_type[0]; |
| 74 | + LOG(DEBUG) << "type = " << type; |
| 75 | + |
| 76 | + nele = std::stoi(tokensStr_type[1]); // number of elements per measurement |
| 77 | + nmeas = std::stoi(tokensStr[2]); // number of measurements |
| 78 | + LOG(DEBUG) << "nele = " << nele << ", nmeas = " << nmeas; |
| 79 | + int shift = 3; // number of tokens that are not measurments (alias, type, number of measurements) |
| 80 | + if ((tokensStr.size() - shift) != (nele + 1) * nmeas) { // +1 to account for the timestamp |
| 81 | + LOG(FATAL) << "Wrong number of pairs (value(s), timestamp): " << tokensStr.size() - 3 << ", should be " << (nele + 1) * nmeas; |
| 82 | + } |
| 83 | + meas.reserve(nmeas); |
| 84 | + |
| 85 | + for (int idx = 0; idx < nmeas; ++idx) { |
| 86 | + std::vector<T> vect; |
| 87 | + vect.reserve(nele); |
| 88 | + if constexpr (std::is_same<T, int32_t>::value) { |
| 89 | + if (type == "i" || type == "b") { |
| 90 | + for (int iele = 0; iele < nele; ++iele) { |
| 91 | + LOG(INFO) << alias << ": value int/bool = " << tokensStr[shift + iele]; |
| 92 | + vect.emplace_back(std::stoi(tokensStr[shift + iele])); |
| 93 | + } |
| 94 | + } else { |
| 95 | + LOG(FATAL) << "templated function called with wrong type, should be int32_t or bool, but it is " << type; |
| 96 | + } |
| 97 | + } else if constexpr (std::is_same<T, float>::value) { |
| 98 | + if (type == "f") { |
| 99 | + for (int iele = 0; iele < nele; ++iele) { |
| 100 | + LOG(INFO) << alias << ": value float = " << tokensStr[shift + iele]; |
| 101 | + vect.emplace_back(std::stof(tokensStr[shift + iele])); |
| 102 | + } |
| 103 | + } else { |
| 104 | + LOG(FATAL) << "templated function called with wrong type, should be float"; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + else if constexpr (std::is_same<T, std::string>::value) { |
| 109 | + if (type == "s") { |
| 110 | + for (int iele = 0; iele < nele; ++iele) { |
| 111 | + LOG(INFO) << alias << ": value string = " << tokensStr[shift + iele]; |
| 112 | + vect.emplace_back(tokensStr[shift + iele]); |
| 113 | + } |
| 114 | + } else { |
| 115 | + LOG(FATAL) << "templated function called with wrong type, should be string"; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + LOG(DEBUG) << "timestamp = " << std::stof(tokensStr[shift + nele]); |
| 120 | + meas.emplace_back(std::make_pair(std::stof(tokensStr[shift + nele]), vect)); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +} // namespace grp |
| 125 | +} // namespace o2 |
| 126 | +#endif |
0 commit comments