GRSISort "v4.1.1.0"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
GValue.cxx
Go to the documentation of this file.
1#include "GValue.h"
2#include "TBuffer.h"
3
4#include <iostream>
5#include <vector>
6#include <algorithm>
7#include <utility>
8#include <fstream>
9#include <sstream>
10#include <cmath>
11
12#include "TGRSIUtilities.h"
13
14// std::string GValue::fValueData
15// std::map<unsigned int, GValue*> GValue::fValueMap;
16GValue* GValue::fDefaultValue = new GValue("GValue", std::sqrt(-1.));
17std::map<std::string, GValue*> GValue::fValueVector;
18
19GValue::GValue(const char* name, double value, EPriority priority)
20 : TNamed(name, name), fValue(value), fPriority(priority)
21{
22}
23
24GValue::GValue(const char* name) : TNamed(name, name)
25{
26}
27
28GValue::GValue(const GValue& val) : TNamed(val)
29{
30 val.Copy(*this);
31}
32
33void GValue::Copy(TObject& obj) const
34{
35 TNamed::Copy(obj);
36 static_cast<GValue&>(obj).fValue = fValue;
37 static_cast<GValue&>(obj).fPriority = fPriority;
38}
39
40double GValue::Value(const std::string& name)
41{
42 return GValue::Value(name, std::sqrt(-1.));
43}
44
45double GValue::Value(const std::string& name, const double& defaultValue)
46{
47 if(fValueVector.count(name) == 0u) {
48 return defaultValue;
49 }
50 return fValueVector.at(name)->GetValue();
51}
52
53void GValue::SetReplaceValue(const std::string& name, double value, EPriority priority)
54{
55 GValue* gvalue = FindValue(name);
56 if(gvalue == nullptr) {
57 gvalue = new GValue(name.c_str(), value, priority);
58 AddValue(gvalue);
59 } else if(priority <= gvalue->fPriority) {
60 gvalue->SetValue(value);
61 gvalue->fPriority = priority;
62 }
63}
64
65GValue* GValue::FindValue(const std::string& name)
66{
67 GValue* value = nullptr;
68 if(name.length() == 0u) {
69 return GetDefaultValue();
70 }
71 if(fValueVector.count(name) != 0u) {
72 value = fValueVector[name];
73 }
74 return value;
75}
76
78{
80 if(strlen(GetName()) != 0u) {
81 oldvalue->SetName(GetName());
82 }
83
84 if(GetValue() != -1) {
85 oldvalue->SetValue(GetValue());
86 oldvalue->fPriority = fPriority;
87 }
88
89 if(strlen(GetInfo()) != 0u) {
90 oldvalue->SetInfo(GetInfo());
91 }
92 return true;
93 }
94
95 return false;
96}
97
99{
101 Copy(*oldvalue);
102 return true;
103 }
104 return false;
105}
106
107bool GValue::AddValue(GValue* value, Option_t*)
108{
109 if(value == nullptr) {
110 return false;
111 }
112 std::string temp_string = value->GetName();
113 if(temp_string.empty()) {
114 // default value, get rid of it and ignore;
115 delete value;
116 return false;
117 }
118
119 GValue* oldvalue = GValue::FindValue(temp_string);
120 if(oldvalue != nullptr) {
121 value->ReplaceValue(oldvalue);
122 delete value;
123 return true;
124 }
125 fValueVector[temp_string] = value;
126 return true;
127}
128
129std::string GValue::PrintToString() const
130{
131
132 std::string buffer;
133 buffer.append(GetName());
134 buffer.append("\t{\n");
135 buffer.append(Form("value:\t%f\n", fValue));
136 if(!info.empty()) {
137 buffer.append("info:\t");
138 buffer.append(info);
139 buffer.append("\n");
140 }
141 buffer.append("}\n");
142 return buffer;
143}
144
145void GValue::Print(Option_t*) const
146{
147 std::cout << PrintToString() << std::endl;
148}
149
150int GValue::WriteValFile(const std::string& filename, Option_t*)
151{
152 // std::string filebuffer;
153 if(filename.length() != 0u) {
154 std::ofstream outfile;
155 outfile.open(filename.c_str());
156 if(!outfile.is_open()) {
157 return -1;
158 }
159 for(auto& iter : fValueVector) {
160 outfile << iter.second->PrintToString() << std::endl
161 << std::endl;
162 }
163 } else {
164 for(auto& iter : fValueVector) {
165 std::cout << iter.second->PrintToString() << std::endl
166 << std::endl;
167 }
168 }
169 return fValueVector.size();
170}
171
172std::string GValue::WriteToBuffer(Option_t*)
173{
174 std::string buffer;
175 if(GValue::Size() == 0) {
176 return buffer;
177 }
178 for(auto& iter : fValueVector) {
179 buffer.append(iter.second->PrintToString());
180 buffer.append("\n");
181 }
182 return buffer;
183}
184
186{
187 // loop over all values and delete them
188 for(const auto& value : fValueVector) {
189 delete value.second;
190 }
191 // delete map
192 fValueVector.clear();
193}
194
195int GValue::ReadValFile(const char* filename, Option_t* opt)
196{
197 std::string infilename = filename;
198 if(infilename.length() == 0) {
199 return -1;
200 }
201
202 std::ifstream infile;
203 infile.open(infilename.c_str());
204 if(!infile) {
205 std::cerr << __PRETTY_FUNCTION__ << ": could not open infile " << infilename << std::endl; // NOLINT(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-bounds-array-to-pointer-decay)
206 return -2;
207 }
208 infile.seekg(0, std::ios::end);
209 size_t length = infile.tellg();
210 if(length == 0) {
211 std::cerr << __PRETTY_FUNCTION__ << ": infile " << infilename << " appears to be empty." << std::endl; // NOLINT(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-bounds-array-to-pointer-decay)
212 return -2;
213 }
214
215 std::string sbuffer;
216 std::vector<char> buffer(length);
217 infile.seekg(0, std::ios::beg);
218 infile.read(buffer.data(), static_cast<int>(length));
219 sbuffer.assign(buffer.data());
220
221 int values_found = ParseInputData(sbuffer, EPriority::kValFile, opt);
222 return values_found;
223}
224
225// Parses input file. Should be in the form:
226// NAME {
227// Name :
228// Value :
229// Info :
230//}
231int GValue::ParseInputData(const std::string& input, EPriority priority, Option_t* opt)
232{
233 std::istringstream infile(input);
234 GValue* value = nullptr;
235 std::string line;
236 int linenumber = 0;
237 int newvalues = 0;
238
239 bool brace_open = false;
240 std::string name;
241
242 while(!std::getline(infile, line).fail()) {
243 linenumber++;
244 trim(line);
245 size_t comment = line.find("//");
246 if(comment != std::string::npos) {
247 line = line.substr(0, comment);
248 }
249 if(line.length() == 0) {
250 continue;
251 }
252 size_t openbrace = line.find('{');
253 size_t closebrace = line.find('}');
254 size_t colon = line.find(':');
255
256 //=============================================//
257 if(openbrace == std::string::npos && closebrace == std::string::npos && colon == std::string::npos) {
258 continue;
259 }
260 //=============================================//
261 if(openbrace != std::string::npos) {
262 brace_open = true;
263 name = line.substr(0, openbrace);
264 trim(name);
265 value = new GValue(name.c_str());
266 }
267 //=============================================//
268 if(brace_open) {
269 if(colon != std::string::npos) {
270 std::string type;
271 if(openbrace == std::string::npos || openbrace > colon) {
272 type = line.substr(0, colon);
273 } else {
274 type = line.substr(openbrace + 1, colon - (openbrace + 1));
275 }
276 line = line.substr(colon + 1, line.length());
277 trim(line); // strip beginning whitespace (not needed for value itself, but for the readability of info)
278 trim(type);
279 std::transform(type.begin(), type.end(), type.begin(), ::toupper);
280 if(type == "NAME") {
281 value->SetName(line.c_str());
282 } else if(type == "VALUE") {
283 value->SetValue(std::atof(line.c_str()));
284 value->fPriority = priority;
285 } else if(type == "INFO") {
286 value->SetInfo(line.c_str());
287 }
288 }
289 }
290 //=============================================//
291 if(closebrace != std::string::npos) {
292 brace_open = false;
293 if(value != nullptr) {
294 // Check whether value is in vector. If it isn't add it.
295 GValue* cur_value = FindValue(value->GetName());
296 if(cur_value == nullptr) {
297 AddValue(value);
298 newvalues++;
299 } else {
300 value->AppendValue(cur_value);
301 delete value;
302 newvalues++;
303 }
304 }
305 value = nullptr;
306 name.clear();
307 }
308 }
309 if(strcmp(opt, "debug") == 0) {
310 std::cout << "parsed " << linenumber << " lines" << std::endl;
311 }
312 return newvalues;
313}
314
315void GValue::Streamer(TBuffer& R__b)
316{
317 SetBit(kCanDelete);
318 UInt_t R__s = 0;
319 UInt_t R__c = 0;
320 if(R__b.IsReading()) {
321 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
322 TNamed::Streamer(R__b);
323 if(R__v > 1) {
324 }
325 {
326 TString R__str;
327 R__str.Streamer(R__b);
328 ParseInputData(R__str.Data(), EPriority::kRootFile);
329 }
330 R__b.CheckByteCount(R__s, R__c, GValue::IsA());
331 } else {
332 R__c = R__b.WriteVersion(GValue::IsA(), true);
333 TNamed::Streamer(R__b);
334 {
335 TString R__str = GValue::WriteToBuffer();
336 R__str.Streamer(R__b);
337 }
338 R__b.SetByteCount(R__c, true);
339 }
340}
void trim(std::string &line, const std::string &trimChars=" \f\n\r\t\v")
static GValue * fDefaultValue
Definition GValue.h:72
static bool AddValue(GValue *, Option_t *opt="")
Definition GValue.cxx:107
static void SetReplaceValue(const std::string &name, double value, EPriority priority=EPriority::kUser)
Definition GValue.cxx:53
GValue()=default
void SetValue(double value)
Definition GValue.h:29
static std::map< std::string, GValue * > fValueVector
Definition GValue.h:73
static int ReadValFile(const char *filename="", Option_t *opt="replace")
Definition GValue.cxx:195
EPriority
Definition GValue.h:12
void Copy(TObject &obj) const override
Definition GValue.cxx:33
const char * GetInfo() const
Definition GValue.h:27
bool ReplaceValue(GValue *)
Definition GValue.cxx:98
static int Size()
Definition GValue.h:63
static int WriteValFile(const std::string &filename="", Option_t *opt="")
Definition GValue.cxx:150
static GValue * FindValue(const std::string &="")
Definition GValue.cxx:65
EPriority fPriority
Definition GValue.h:70
bool AppendValue(GValue *)
Definition GValue.cxx:77
std::string info
Definition GValue.h:71
static int ParseInputData(const std::string &input, EPriority priority, Option_t *opt="")
Definition GValue.cxx:231
static GValue * GetDefaultValue()
Definition GValue.h:35
static void Clear()
Definition GValue.cxx:185
double fValue
Definition GValue.h:69
static std::string WriteToBuffer(Option_t *opt="")
Definition GValue.cxx:172
static double Value(const std::string &)
Definition GValue.cxx:40
void SetInfo(const char *temp)
Definition GValue.h:30
std::string PrintToString() const
Definition GValue.cxx:129
double GetValue() const
Definition GValue.h:26
void Print(Option_t *opt="") const override
Definition GValue.cxx:145