ARTEMIS-CRIB
 
Loading...
Searching...
No Matches
TMUXPositionConverter.cc
Go to the documentation of this file.
1/**
2 * @file TMUXPositionConverter.cc
3 * @brief
4 * @author Kodai Okawa<okawa@cns.s.u-tokyo.ac.jp>
5 * @date 2022-01-30 11:50:14
6 * @note last modified: 2025-01-04 15:56:46
7 * @details
8 */
9
11
12#include <TObjArray.h>
13#include <TObjString.h>
14#include <constant.h>
15
16#include <algorithm>
17#include <iterator>
18#include <memory>
19
20/// ROOT macro for class implementation
22
23namespace art::crib {
25
27
28/**
29 * @details
30 * - Checks if `fParams` is empty. If so, returns `kInvalidI` and logs a warning.
31 * - Uses `std::lower_bound` to find the first element in `fParams` not less than `val`.
32 * - If `val` is out of range (less than the first or greater than the last element), returns `kInvalidI`.
33 * - Otherwise, returns the index of the boundary just below `val`.
34 */
35Double_t TMUXPositionConverter::Convert(const Double_t val) const {
36 if (fParams.empty()) {
37 Warning("Convert", "fParams is empty. Returning invalid value.");
38 return kInvalidI;
39 }
40
41 auto it = std::lower_bound(fParams.begin(), fParams.end(), val);
42 if (it == fParams.begin() || it == fParams.end()) {
43 return kInvalidI;
44 }
45
46 return std::distance(fParams.begin(), it - 1);
47}
48
49/**
50 * @details
51 * - Strips leading and trailing spaces and removes comments starting with `#`.
52 * - Replaces commas and tabs with spaces, and normalizes multiple spaces into a single space.
53 * - Splits the string into tokens, attempts to convert each token to a `Double_t`, and stores valid values in `fParams`.
54 * - Invalid tokens are skipped, and a warning is logged.
55 * - After parsing, `fParams` is sorted to prepare for binary search operations.
56 */
57Bool_t TMUXPositionConverter::LoadString(const TString &str) {
58 TString lineContent = str;
59 lineContent = lineContent.Strip(TString::kBoth);
60 if (lineContent.BeginsWith("#") || lineContent.IsNull()) {
61 return kFALSE;
62 }
63
64 Ssiz_t hashIndex = lineContent.Index("#");
65 if (hashIndex != kNPOS) {
66 lineContent.Remove(hashIndex);
67 }
68 lineContent = lineContent.Strip(TString::kBoth);
69
70 lineContent.ReplaceAll(",", " ");
71 lineContent.ReplaceAll("\t", " ");
72 lineContent.ReplaceAll(" +", " ");
73
74 std::unique_ptr<TObjArray> tokens(lineContent.Tokenize(" "));
75 if (!tokens) {
76 return kFALSE;
77 }
78
79 for (int i = 0; i < tokens->GetEntries(); ++i) {
80 auto *token = dynamic_cast<TObjString *>(tokens->At(i));
81 if (!token) {
82 Warning("LoadString", "Invalid token at index %d", i);
83 continue;
84 }
85
86 TString valueStr = token->GetString();
87 if (!valueStr.IsFloat()) {
88 Warning("LoadString", "Invalid value: %s", valueStr.Data());
89 continue;
90 }
91 fParams.emplace_back(valueStr.Atof());
92 }
93 std::sort(fParams.begin(), fParams.end());
94 Info("LoadString", "Loaded %zu parameters", fParams.size());
95
96 return !fParams.empty();
97}
98
99/**
100 * @details
101 * Iterates over all elements in `fParams` and logs their values using ROOT's `Info` function.
102 */
103void TMUXPositionConverter::Print(Option_t *) const {
104 for (const auto &e : fParams) {
105 Info("Print", "fParams element: %lf", e);
106 }
107}
108} // namespace art::crib
ClassImp(art::crib::TMUXPositionConverter)
ROOT macro for class implementation.
A class for loading and converting MUX position parameters.
~TMUXPositionConverter() override
Default destructor.
std::vector< Double_t > fParams
A vector to store the loaded numeric parameters.
Bool_t LoadString(const TString &str) override
Loads numeric parameters from a string.
TMUXPositionConverter()
Default constructor.
Double_t Convert(Double_t val) const override
Converts a value based on the loaded parameters.
void Print(Option_t *opt="") const override
Prints the loaded parameters.
return to the guide