19NamespaceImp(Hal::Std);
23 TString RemoveUnits(TString tString) {
25 for (
int i = 0; i < tString.First(
'['); i++) {
26 res = res + tString[i];
31 TString GetUnits(TString
string) {
33 Int_t first =
string.First(
'[');
34 Int_t last =
string.Last(
']');
35 if (first < 0)
return "";
36 for (
int i = first + 1; i < last; i++) {
37 res = res +
string[i];
42 std::vector<TString> ExplodeString(TString
string, Char_t delimiter, Bool_t keepEmpty) {
43 std::vector<TString> array;
44 if (
string.Length() == 0)
return array;
45 if (
string[0] == delimiter)
string = string(1,
string.Length() - 1);
46 if (
string.Length() == 0)
return array;
47 if (
string[
string.Length() - 1] != delimiter)
string =
string + delimiter;
48 Int_t no =
string.CountChar(delimiter);
49 for (
int count = 0; count < no; count++) {
50 Size_t pos =
string.First(delimiter);
51 TString element = string(0, pos);
53 if (element.Length() != 0) array.push_back(element);
55 array.push_back(element);
57 string = string(pos + 1,
string.Length() - pos - 1);
62 std::vector<std::pair<TString, TString>> ConvertMainArgs(
int argc,
char* argv[]) {
63 std::vector<std::pair<TString, TString>> res;
64 for (
int i = 1; i < argc; i++) {
65 TString fullArg = argv[i];
66 if (fullArg.BeginsWith(
"--")) {
67 std::vector<TString> vec = ExplodeString(fullArg,
'=');
69 Hal::Cout::PrintInfo(Form(
"Problem with extraction of argument %s", fullArg.Data()), Hal::EInfo::kWarning);
70 }
else if (vec.size() != 2) {
71 vec[0] = vec[0](2, vec[0].Length() - 2);
72 res.push_back(std::pair<TString, TString>(vec[0],
""));
74 vec[0] = vec[0](2, vec[0].Length() - 2);
75 res.push_back(std::pair<TString, TString>(vec[0], vec[1]));
78 res.push_back(std::pair<TString, TString>(
"", fullArg));
84 TString RoundToString(Double_t value, Int_t precission, Option_t* opt) {
86 if (option ==
"prefix") {
87 Double_t lognum = TMath::Log10(TMath::Abs(value));
88 Int_t full_lognum = lognum / 3;
89 if (lognum < 0) { full_lognum--; }
91 Double_t number = value / TMath::Power(10, full_lognum * 3);
92 if (precission >= 0) {
93 answer = RoundToString(number, precission,
"");
95 precission = TMath::Abs(precission);
97 Double_t abs_value = TMath::Abs(number);
99 digits = precission - 1;
100 }
else if (abs_value < 100) {
101 digits = precission - 2;
103 digits = precission - 3;
105 if (digits < 0) digits = 0;
106 answer = RoundToString(number, digits,
"");
108 TString prefix =
"x";
109 switch (full_lognum * 3) {
110 case -24: prefix =
"y";
break;
111 case -21: prefix =
"z";
break;
112 case -18: prefix =
"a";
break;
113 case -15: prefix =
"f";
break;
114 case -12: prefix =
"p";
break;
115 case -9: prefix =
"n";
break;
116 case -6: prefix =
"#mu";
break;
117 case -3: prefix =
"m";
break;
118 case 0: prefix =
"";
break;
119 case 3: prefix =
"k";
break;
120 case 6: prefix =
"M";
break;
121 case 9: prefix =
"G";
break;
122 case 12: prefix =
"T";
break;
123 case 15: prefix =
"P";
break;
124 case 18: prefix =
"E";
break;
125 case 21: prefix =
"Z";
break;
126 case 24: prefix =
"Y";
break;
127 default: prefix = Form(
"x10^%i", full_lognum * 3);
break;
129 if (value == 0) prefix =
"";
130 return answer + prefix;
131 }
else if (option ==
"separators") {
132 ULong64_t integer = TMath::Abs(value);
133 Double_t fractional = TMath::Abs(value) - ((Double_t) integer);
134 fractional = TMath::Abs(fractional);
135 TString first_cout = RoundToString(integer, 0,
"separators");
136 TString sec_cout = RoundToString(fractional, precission,
"");
138 Int_t pos = sec_cout.First(
'.');
139 for (
int i = 1; i < sec_cout.Length() - 1; i++) {
141 other = other + sec_cout[pos + i];
143 other = other + sec_cout[pos + i] +
" ";
146 return Form(
"%s.%s", first_cout.Data(), other.Data());
148 return Form(
"-%s.%s", first_cout.Data(), other.Data());
150 TString flag = Form(
"%%4.%if", precission);
151 return Form(flag, value);
155 TString RoundToString(Int_t value, Int_t precission, Option_t* opt) {
156 TString option = opt;
157 if (option ==
"separators") {
158 TString val = Form(
"%i", TMath::Abs(value));
159 if (val.Length() < 4) {
167 Int_t mod = val.Length() % 3;
168 for (
int i = 0; i < val.Length(); i++) {
170 if (i != 0) newstr = newstr +
" ";
171 newstr = newstr + val[i];
175 newstr = newstr + val[i];
179 return (
"-" + newstr);
183 }
else if (option ==
"prefix") {
184 return RoundToString((Double_t) value, precission, opt);
186 return Form(
"%i", value);
190 TString RoundToString(ULong_t value, Int_t precission, Option_t* opt) {
192 TString option = opt;
193 if (option ==
"separators") {
194 TString val = Form(
"%lu", TMath::Abs((
long int) value));
195 if (val.Length() < 4) {
return val; }
197 Int_t mod = val.Length() % 3;
198 for (
int i = 0; i < val.Length(); i++) {
200 if (i != 0) newstr = newstr +
" ";
201 newstr = newstr + val[i];
205 newstr = newstr + val[i];
209 }
else if (option ==
"prefix") {
210 Double_t newval = (Double_t) value;
211 return RoundToString(newval, precission,
"prefix");
213 return Form(
"%lu", value);
217 TString RoundToString(ULong64_t value, Int_t precission, Option_t* opt) {
219 TString option = opt;
220 if (option ==
"separators") {
221 TString val = Form(
"%llu", TMath::Abs((
long long int) value));
222 if (val.Length() < 4) {
return val; }
224 Int_t mod = val.Length() % 3;
225 for (
int i = 0; i < val.Length(); i++) {
227 if (i != 0) newstr = newstr +
" ";
228 newstr = newstr + val[i];
232 newstr = newstr + val[i];
236 }
else if (option ==
"prefix") {
237 Double_t newval = (Double_t) value;
238 return RoundToString(newval, precission,
"prefix");
240 return Form(
"%llu", value);
244 TString RoundToString(UInt_t value, Int_t precission, Option_t* opt) {
246 TString option = opt;
247 if (option ==
"separators") {
248 TString val = Form(
"%u", value);
249 if (val.Length() < 4) {
return val; }
251 Int_t mod = val.Length() % 3;
252 for (
int i = 0; i < val.Length(); i++) {
254 if (i != 0) newstr = newstr +
" ";
255 newstr = newstr + val[i];
259 newstr = newstr + val[i];
263 }
else if (option ==
"prefix") {
264 Double_t newval = (Double_t) value;
265 return RoundToString(newval, precission,
"prefix");
267 return Form(
"%u", value);
271 TString RemoveString(TString text, TString pattern) {
272 TRegexp match(pattern);
273 TString newword = text;
274 TString regstring = text(match);
275 if (regstring > 0) newword.Remove(text.Index(match), regstring.Length());
279 TString RemoveNChars(TString str, Int_t n, Char_t opt) {
280 Int_t lenght = str.Length();
282 return TString(str(n, lenght - n));
284 return TString(str(0, lenght - n));
287 std::vector<TString> GetLinesFromFile(TString file, Bool_t skip) {
288 std::ifstream txtfile(file);
290 std::vector<TString> res;
291 while (std::getline(txtfile, instr)) {
292 TString line = instr;
293 if (line.Length() == 0 && skip)
continue;
294 res.push_back(instr);
300 std::vector<TString> FindBrackets(TString& option, Bool_t remove, Bool_t skipEmpty) {
302 TString copy = option;
303 TRegexp regexp(
"\\{[^}]*\\}");
304 std::vector<TString> res;
305 std::vector<TString> resRaw;
309 resRaw.push_back(expr);
310 expr = expr(1, expr.Length() - 2);
312 if (expr.Length() > 1) res.push_back(expr);
315 copy.Remove(copy.First(
'{'), 1);
318 }
while (expr.Length());
320 for (
auto i : resRaw)
321 option.ReplaceAll(i,
"");
326 void ReplaceInFile(TString path, TString newPath, TString oldPattern, TString newPattern) {
327 if (!FileExists(path))
return;
328 if (path.EqualTo(
".temp.txt")) {
329 Cout::PrintInfo(
"File .temp.txt cannot be modified by HAL", EInfo::kLowWarning);
332 if (path == newPath) {
333 ReplaceInFile(path,
".temp.txt", oldPattern, newPattern);
334 gSystem->Exec(Form(
"mv .temp.txt %s", path.Data()));
336 std::ifstream inFile;
338 std::ofstream outFile;
339 outFile.open(newPath);
340 for (std::string line; std::getline(inFile, line);) {
342 temp.ReplaceAll(oldPattern, newPattern);
343 outFile << temp << std::endl;
350 void ReplaceInFile(TString path,
352 std::initializer_list<TString> oldPattern,
353 std::initializer_list<TString> newPattern) {
354 if (!FileExists(path))
return;
355 std::vector<TString> list1 = Std::GetVector(oldPattern);
356 std::vector<TString> list2 = Std::GetVector(newPattern);
357 if (list1.size() != list2.size()) {
358 Cout::PrintInfo(Form(
"Not compatible lists %s %i", __FILE__, __LINE__), EInfo::kLowWarning);
361 if (list1.size() == 0)
return;
362 ReplaceInFile(path, newPath, list1[0], list2[0]);
363 for (
unsigned int i = 1; i < list1.size(); i++) {
364 ReplaceInFile(newPath, newPath, list1[i], list2[i]);
368 Bool_t FindParam(TString& option, TString pattern, Bool_t remove) {
370 option.ReplaceAll(
" ",
"");
371 option.ReplaceAll(
"++",
"");
373 TString lower_opt = option;
374 TString lower_pattern = pattern;
375 const Int_t lenght = lower_pattern.Length();
377 lower_pattern.ToLower();
381 if (lower_opt == lower_pattern) {
382 if (remove) option =
"";
384 }
else if (lower_opt.Length() == lower_pattern.Length()) {
387 lower_pattern.ReplaceAll(
"+",
"\\+");
391 Int_t index = lower_opt.Index(TRegexp(Form(
"^%s\\+", lower_pattern.Data())));
393 if (remove) option.Remove(index, lenght + 1);
399 index = lower_opt.Index(TRegexp(Form(
"\\+%s", lower_pattern.Data())));
400 if (index >= 0 && index + lenght + 1 == lower_opt.Length()) {
401 if (remove) option.Remove(index, lenght + 1);
407 index = lower_opt.Index(TRegexp(Form(
"\\+%s\\+", lower_pattern.Data())));
409 if (remove) option.Remove(index, lenght + 1);
415 Int_t FindParam2(TString& option, TString pattern, Bool_t remove) {
416 TString negPar = Form(
"!%s", pattern.Data());
417 if (FindParam(option, negPar, remove)) {
419 }
else if (FindParam(option, pattern, remove)) {
425 Bool_t FindExpressionSingleValue(TString& expression, Int_t& val, Bool_t remove) {
426 TString option = expression;
427 TRegexp regexp(
"{[0-9]+}");
428 TString expr = option(regexp);
429 if (expr.Length() <= 0) {
return kFALSE; }
431 option.Remove(option.Index(regexp),
433 TRegexp number_expr(
"{[0-9]+}");
434 TString first = expr(number_expr);
435 TString number_str = first(TRegexp(
"[0-9]+"));
436 val = number_str.Atoi();
437 if (remove) { expression = option; }
441 Bool_t FindExpressionTwoValues(TString& expression, Int_t& val1, Int_t& val2, Bool_t remove) {
442 TString option = expression;
443 TRegexp regexp(
"{[0-9]+x[0-9]+}");
444 TString expr = option(regexp);
445 if (expr.Length() <= 0) {
return kFALSE; }
447 option.Remove(option.Index(regexp),
449 TRegexp number_expr(
"{[0-9]+x");
450 TRegexp jump_expr(
"x[0-9]+}");
451 TString first = expr(number_expr);
452 TString sec = expr(jump_expr);
453 TString number_str = first(TRegexp(
"[0-9]+"));
454 TString jump_str = sec(TRegexp(
"[0-9]+"));
455 val1 = number_str.Atoi();
456 val2 = jump_str.Atoi();
457 if (remove) { expression = option; }
463 Bool_t FindExpressionTwoFloats(TString& expression, Double_t& val1, Double_t& val2, Bool_t remove) {
464 TRegexp regexp(
"{[-+]?[0-9]*\\.?[0-9]*,[-+]?[0-9]*\\.?[0-9]*}");
465 TString expr = expression(regexp);
466 if (expr.Length() > 1) {
467 TRegexp low_expr(
"{[-+]?[0-9]*\\.?[0-9]*,");
468 TRegexp high_expr(
",[-+]?[0-9]*\\.?[0-9]*}");
469 TString first = expr(low_expr);
470 first.ReplaceAll(
",",
"");
471 first.ReplaceAll(
"{",
"");
472 TString last = expr(high_expr);
473 last.ReplaceAll(
",",
"");
474 last.ReplaceAll(
"}",
"");
477 if (remove) expression.ReplaceAll(expr,
"");
static void PrintInfo(TString text, Hal::EInfo status)