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