BmnRoot 26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnFunctionSet.cxx
Go to the documentation of this file.
1#include "BmnFunctionSet.h"
2
3#include "BmnFieldConst.h"
4#include "BmnFieldMap.h"
5#include "BmnMultiField.h"
6#include "FairFileHeader.h"
7#include "FairIon.h"
8#include "FairLogger.h"
9#include "FairRootManager.h"
10#include "TChain.h"
11#include "TChainElement.h"
12#include "TClass.h"
13#include "TGeoElement.h"
14#include "TKey.h"
15#include "TObjString.h"
16#include "TPRegexp.h"
17#include "TSystem.h"
18#include "TTree.h"
19
20#include <TMath.h>
21#include <fstream>
22#include <iomanip>
23#include <iostream>
24#include <sstream>
25using namespace std;
26using namespace std::chrono;
27using namespace std::chrono_literals;
28
29// check whether file exists: 1 - exists, 0 - not exists, -1 - exists but cannot access with the mode (default:
30// kFileExists)
31int BmnFunctionSet::CheckFileExist(TString& fileName, int iVerbose, EAccessMode mode)
32{
33 gSystem->ExpandPathName(fileName);
34 if (gSystem->AccessPathName(fileName.Data(), kFileExists) == true) {
35 if (iVerbose)
36 cout << "ERROR: no specified file: " << fileName << endl;
37 return 0;
38 }
39 if ((mode != kFileExists) && (gSystem->AccessPathName(fileName.Data(), mode) == true)) {
40 if (iVerbose)
41 cout << "ERROR: no required permissions to access the file: " << fileName << endl;
42 return -1;
43 }
44
45 return 1;
46}
47
48// check whether directory exists: 1 - exists, 0 - not exists, -1 - exists but cannot access with the mode (default:
49// kFileExists)
51{
52 gSystem->ExpandPathName(fileName);
53 TString dirName(gSystem->DirName(fileName.Data()));
54 if (gSystem->AccessPathName(dirName.Data(), kFileExists) == true) {
55 if (iVerbose)
56 cout << "ERROR: no specified directory: " << dirName << endl;
57 return 0;
58 }
59 if ((mode != kFileExists) && (gSystem->AccessPathName(dirName.Data(), mode) == true)) {
60 if (iVerbose)
61 cout << "ERROR: no required permissions to access the directory: " << dirName << endl;
62 return -1;
63 }
64
65 return 1;
66}
67
68// create directory tree for the file name if not exists: 1 - exists, 0 - created
69// error codes: -1 - exists but cannot access with the mode (default: kWritePermission), -2 - cannot be created, -3 -
70// empty file name
72{
73 if (fileName == "") {
74 if (iVerbose)
75 cout << "ERROR: the specified file name is empty" << endl;
76 return -3;
77 }
78
79 int status = CheckDirectoryExist(fileName, 0, mode);
80 if (status < 0) {
81 if (iVerbose)
82 cout << "ERROR: the directory of the specified file (\"" << fileName
83 << "\") does not provide required permissions" << endl;
84 return status;
85 }
86 if (status == 0) {
87 TString dirName(gSystem->DirName(fileName.Data()));
88 int status_parent = CreateDirectoryTree(dirName, 0, mode);
89 if (status_parent < 0)
90 return status_parent;
91 if (gSystem->MakeDirectory(dirName) != 0) {
92 if (iVerbose)
93 cout << "ERROR: the directory of the specified file (\"" << fileName << "\") could not be created"
94 << endl;
95 return -2;
96 }
97 return 0;
98 }
99
100 return 1;
101}
102
103// get the list of input ROOT files for the given input file, which can be a ROOT file with data, a ROOT file with
104// a TChain object or a text file (extension is not '.root') with a list of ROOT files
105// return value: number of files in the list, < 0 - error codes
106int BmnFunctionSet::GetInputFileList(TString inputFileName, vector<TString>& fileList)
107{
108 fileList.clear();
109 if (CheckFileExist(inputFileName) != 1) {
110 LOG(error) << "BmnFunctionSet::GetInputFileList: no specified file: " << inputFileName;
111 return -1;
112 }
113
114 if (inputFileName.EndsWith(".root")) {
115 TFile* rootFile = TFile::Open(inputFileName.Data());
116 if (!rootFile || rootFile->IsZombie()) {
117 LOG(error) << "BmnFunctionSet::GetInputFileList: opening the ROOT file failed: " << inputFileName;
118 delete rootFile;
119 return -2;
120 }
121
122 // search for a TChain object, otherwise the ROOT file itself contains the data
123 TChain* chain = nullptr;
124 TIter next(rootFile->GetListOfKeys());
125 TKey* key;
126 while ((key = (TKey*)next()) != nullptr) {
127 if (strcmp(key->GetClassName(), "TChain") == 0) {
128 chain = rootFile->Get<TChain>(key->GetName());
129 break;
130 }
131 }
132 if (chain == nullptr)
133 fileList.push_back(inputFileName);
134 else {
135 TIter nextElement(chain->GetListOfFiles());
136 TChainElement* element;
137 while ((element = (TChainElement*)nextElement()) != nullptr)
138 fileList.push_back(element->GetTitle());
139 }
140 rootFile->Close(); // the chain object is deleted together with the file
141 delete rootFile;
142 } else {
143 ifstream listFile(inputFileName.Data());
144 if (!listFile.is_open()) {
145 LOG(error) << "BmnFunctionSet::GetInputFileList: the file list could not be opened: " << inputFileName;
146 return -2;
147 }
148
149 string line;
150 while (getline(listFile, line)) {
151 istringstream lineStream(line);
152 string word;
153 if ((lineStream >> word) && (word[0] != '#'))
154 fileList.push_back(word);
155 }
156 }
157
158 // the check also expands the paths, so the files can be given as, e.g., "$VMCWORKDIR/bmndst.root"
159 for (auto& fileName : fileList) {
160 if (CheckFileExist(fileName) != 1) {
161 LOG(error) << "BmnFunctionSet::GetInputFileList: no specified file: " << fileName << " (listed in "
162 << inputFileName << ")";
163 fileList.clear();
164 return -3;
165 }
166 }
167 if (fileList.empty()) {
168 LOG(error) << "BmnFunctionSet::GetInputFileList: no input files found in: " << inputFileName;
169 return -4;
170 }
171
172 return fileList.size();
173}
174
176{
177 TFile* fRootFile = nullptr;
178 if (fileName == "") {
179 FairRootManager* ioman = FairRootManager::Instance();
180 if (ioman == nullptr) {
181 LOG(error) << "isSimulationFile(): FairRootManager is not instantiated";
182 return -1;
183 }
184 TTree* cur_tree = ioman->GetInTree();
185 if (cur_tree == nullptr) {
186 LOG(error) << "isSimulationFile(): There is no current tree opened";
187 return -2;
188 }
189
190 fRootFile = cur_tree->GetCurrentFile();
191 } else {
192 gSystem->ExpandPathName(fileName);
193 if (gSystem->AccessPathName(fileName.Data()) == true) {
194 LOG(error) << "isSimulationFile(): no specified file: " << fileName;
195 return -3;
196 }
197
198 fRootFile = TFile::Open(fileName.Data());
199 if (!fRootFile || fRootFile->IsZombie()) {
200 LOG(error) << "isSimulationFile(): opening the input file failed: " << fileName;
201 if (fRootFile) {
202 fRootFile->Close();
203 delete fRootFile;
204 }
205 return -4;
206 }
207 }
208
209 TObject* branch_list = fRootFile->Get("BranchList");
210 if (branch_list != nullptr) {
211 TObject* mctrack_find = branch_list->FindObject("MCTrack");
212 if (mctrack_find != nullptr) {
213 if (fileName != "") {
214 fRootFile->Close();
215 delete fRootFile;
216 }
217
218 return 1;
219 }
220 }
221
222 if (fileName != "") {
223 fRootFile->Close();
224 delete fRootFile;
225 }
226 return 0;
227}
228
229int BmnFunctionSet::GetPeriodNumber(TFile* root_file, int method_number)
230{
231 if (!root_file || root_file->IsZombie()) {
232 LOG(error) << "BmnFunctionSet::GetPeriodNumber: ROOT-file is not opened " << root_file->GetName();
233 return -2;
234 }
235
236 int period_number = -1;
237 switch (method_number) {
238 // method number: 0
239 case 0: {
240 TIter next(root_file->GetListOfKeys());
241 TKey* key;
242 while ((key = (TKey*)next())) {
243 if (std::string(key->GetClassName()) == "FairRtdbRun") {
244 TString str_period_number = key->GetName();
245 if (str_period_number.IsDigit()) {
246 period_number = str_period_number.Atoi();
247 break;
248 } // if (str_period_number.IsDigit())
249 } // if "FairRtdbRun" class
250 } // while TKey*)next()
251
252 if (period_number == -1) {
253 LOG(error) << "BmnFunctionSet::GetPeriodNumber: FairRtdbRun object not found in ROOT-file "
254 << root_file->GetName();
255 return -3;
256 }
257 return period_number;
258 } // method number == 0
259 // method number: 1
260 case 1: {
261 FairFileHeader* header = root_file->Get<FairFileHeader>("FileHeader");
262 if (!header) {
263 LOG(error) << "BmnFunctionSet::GetPeriodNumber: 'FileHeader' branch not found in ROOT-file "
264 << root_file->GetName();
265 return -4;
266 }
267
268 period_number = (int)header->GetRunId();
269 return period_number;
270 }
271 default:
272 return -5;
273 }
274
275 return -6;
276}
277
278TString BmnFunctionSet::JoinKeys(const std::vector<std::pair<TString, TString>>& dict)
279{
280 TString result;
281 for (size_t i = 0; i < dict.size(); ++i) {
282 if (i > 0)
283 result += "|";
284 result += dict[i].first;
285 }
286 return result;
287}
288
289// get magnetic field value (mV) for the given beam and its energy (e.g. parsed by ParseFileName)
290// beam: beam particle name; energy: beam energy (GeV)
291// return value: magnetic field value (mV), < 0 - in case of errors
292double BmnFunctionSet::GetMagneticFieldByEnergy(const TString& beam, double energy)
293{
294 double field_value = -1;
295 if (beam == "Xe") {
296 // comparison tolerance between doubles
297 const Double_t kEnergyTolerance = 1e-4;
298 // known energy -> field value in mV
299 const vector<pair<Double_t, Double_t>> energy_to_field = {{1.6, 51.5}, {2.2, 64.3}, {3.0, 85.0}, {3.8, 103.8}};
300
301 if ((energy < 1) || (energy > 6)) {
302 LOG(error) << "BmnFunctionSet::GetMagneticFieldByEnergy: defined energy is out of range: " << energy;
303 return -3;
304 }
305
306 // compare with known energies (in tolerance) or use approximate formula if energy not found in the table
307 size_t i = 0;
308 for (; i < energy_to_field.size(); ++i) {
309 const auto& entry = energy_to_field[i];
310
311 // check the exact energy value from the known table
312 if (TMath::Abs(energy - entry.first) < kEnergyTolerance) {
313 field_value = entry.second;
314 break;
315 }
316
317 if (entry.first > energy) {
318 LOG(warning) << "BmnFunctionSet::GetMagneticFieldByEnergy: " << energy
319 << " GeV is not table value, using approximate formula for the magnetic field";
320 // get nearest energy value from the table
321 const auto& ref =
322 (i > 0 && TMath::Abs(energy_to_field[i - 1].first - energy) < TMath::Abs(entry.first - energy))
323 ? energy_to_field[i - 1]
324 : entry;
325
326 // formula to calculate magnetic field using base (known) "energy - magnetic field" (ignoring B0)
327 field_value =
328 ref.second * TMath::Sqrt(energy * (energy + 1.862)) / TMath::Sqrt(ref.first * (ref.first + 1.862));
329 break;
330 }
331 }
332
333 // if given energy greater than maximum value in the table
334 if (i == energy_to_field.size()) {
335 LOG(warning) << "BmnFunctionSet::GetMagneticFieldByEnergy: " << energy
336 << " GeV is not table value, using approximate formula for the magnetic field";
337 const auto& entry = energy_to_field[i - 1];
338 // formula to calculate magnetic field using base (known) "energy - magnetic field" (ignoring B0)
339 field_value = entry.second * TMath::Sqrt(energy * (energy + 1.862))
340 / TMath::Sqrt(entry.first * (entry.first + 1.862));
341 }
342 } else
343 LOG(warning) << "BmnFunctionSet::GetMagneticFieldByEnergy: field value is not defined for the beam: " << beam;
344
345 return field_value;
346}
347
348// get the correction factor for the magnetic field map scale calculated as 'field voltage / map voltage'
349// energy: beam energy (GeV)
350// return value: correction factor to multiply the field map scale by, 1 - for the energies without correction
352{
353 // comparison tolerance between doubles
354 const Double_t kEnergyTolerance = 1e-4;
355 // known energy (GeV) -> correction factor of the field map scale (AZ-070826)
356 const vector<pair<Double_t, Double_t>> energy_to_correction = {{1.6, 1.01}, {2.2, 1. / 1.005}, {3.0, 1. / 1.02}};
357
358 for (const auto& entry : energy_to_correction)
359 if (TMath::Abs(energy - entry.first) < kEnergyTolerance) {
360 LOG(debug) << "BmnFunctionSet::GetFieldScaleCorrection: the field map scale is corrected by "
361 << entry.second << " for the beam energy: " << energy << " GeV";
362 return entry.second;
363 }
364
365 // the correction is known for several energies only, the field map scale is not changed for the others
366 return 1.;
367}
368
369// set the scale of the magnetic field of any type
370bool BmnFunctionSet::SetFieldScale(FairField* field, double scale)
371{
372 if (field == nullptr) {
373 LOG(error) << "BmnFunctionSet::SetFieldScale: no magnetic field to set the scale";
374 return false;
375 }
376
377 // the field map applies the scale, when it reads the map during the initialization
378 if (BmnFieldMap* fieldMap = dynamic_cast<BmnFieldMap*>(field)) {
379 fieldMap->SetScale(scale);
380 return true;
381 }
382 if (BmnFieldConst* fieldConst = dynamic_cast<BmnFieldConst*>(field)) {
383 fieldConst->SetField(fieldConst->GetBx() * scale, fieldConst->GetBy() * scale, fieldConst->GetBz() * scale);
384 fieldConst->SetFieldOff(scale == 0);
385 return true;
386 }
387 // the combined field is scaled as a whole, i.e. all its fields with the same scale
388 if (BmnMultiField* multiField = dynamic_cast<BmnMultiField*>(field)) {
389 bool isScaled = true;
390 TIter nextField(multiField->GetFieldList());
391 while (FairField* subField = (FairField*)nextField())
392 if (!SetFieldScale(subField, scale))
393 isScaled = false;
394 return isScaled;
395 }
396
397 LOG(error) << "BmnFunctionSet::SetFieldScale: the scale of the magnetic field of the " << field->ClassName()
398 << " class is not supported";
399 return false;
400}
401
402// parsing file name to define beam, target and energy of the run
403// the file name is parsed with the '_' separator, if it does not fit then with the '.' separator
404// file_name: name of the file to parse
405// result (beam, target, energy (GeV)) returns via links, energy is < 0 in case of errors
406// return value: 0 - success, < 0 - error codes
407Int_t BmnFunctionSet::ParseFileName(const TString& file_name, TString& beam, TString& target, Double_t& energy)
408{
409 Int_t rc = ParseFileNameBySeparator(file_name, "_", beam, target, energy);
410 if (rc == 0)
411 return 0;
412
413 // the dot can be used as a separator in the file name, then it also breaks a float energy value into 2 parts
414 LOG(debug) << "BmnFunctionSet::ParseFileName: trying to use '.' separator instead of '_' to parse the file name: "
415 << file_name;
416 rc = ParseFileNameBySeparator(file_name, ".", beam, target, energy);
417 if (rc != 0) {
418 LOG(error) << "BmnFunctionSet::ParseFileName: beam, target and energy were not defined by the file name: "
419 << file_name;
420 // clean the values, which could be defined by the partial parsing
421 beam = "";
422 target = "";
423 }
424
425 return rc;
426}
427
428// define beam and energy of the run by FairIonGenerator added to the primary generator (e.g. ION generator in
429// run_sim_bmn.C): the generated ion is considered as the beam
430// result (beam, energy - kinetic energy per nucleon (GeV)) returns via links, energy is < 0 in case of errors
431// return value: 0 - success, < 0 - error codes
432Int_t BmnFunctionSet::GetBeamByIonGenerator(FairPrimaryGenerator* primary_generator, TString& beam, Double_t& energy)
433{
434 beam = "";
435 energy = -1;
436 if ((primary_generator == nullptr) || (primary_generator->GetListOfGenerators() == nullptr)) {
437 LOG(error) << "BmnFunctionSet::GetBeamByIonGenerator: no primary generator or its list of generators";
438 return -1;
439 }
440
441 // FairIonGenerator is found by the class name, because BmnBase is not linked with the FairRoot generators
442 TClass* ion_generator_class = TClass::GetClass("FairIonGenerator");
443 char* ion_generator = nullptr;
444 if (ion_generator_class) {
445 TIter next(primary_generator->GetListOfGenerators());
446 while (TObject* generator = next()) {
447 if (generator->IsA() == ion_generator_class) {
448 ion_generator =
449 static_cast<char*>(ion_generator_class->DynamicCast(TObject::Class(), generator, kFALSE));
450 break;
451 }
452 }
453 }
454 if (ion_generator == nullptr) {
455 LOG(error) << "BmnFunctionSet::GetBeamByIonGenerator: FairIonGenerator was not found in the primary generator";
456 return -2;
457 }
458
459 // FairIonGenerator has no getters, so the ion and its momentum are read via the ROOT dictionary.
460 // The momentum is stored for the whole ion, while it is given per nucleon in the FairIonGenerator constructor
461 const Long_t offset_ion = ion_generator_class->GetDataMemberOffset("fIon"),
462 offset_px = ion_generator_class->GetDataMemberOffset("fPx"),
463 offset_py = ion_generator_class->GetDataMemberOffset("fPy"),
464 offset_pz = ion_generator_class->GetDataMemberOffset("fPz");
465 if ((offset_ion == 0) || (offset_px == 0) || (offset_py == 0) || (offset_pz == 0)) {
466 LOG(error) << "BmnFunctionSet::GetBeamByIonGenerator: the ion or momentum was not found in FairIonGenerator";
467 return -3;
468 }
469 const FairIon* ion = *reinterpret_cast<FairIon**>(ion_generator + offset_ion);
470 if ((ion == nullptr) || (ion->GetZ() < 1) || (ion->GetA() < 1)) {
471 LOG(error) << "BmnFunctionSet::GetBeamByIonGenerator: FairIonGenerator does not generate an ion";
472 return -4;
473 }
474 const Int_t z = ion->GetZ(), a = ion->GetA();
475 const Double_t px = *reinterpret_cast<Double_t*>(ion_generator + offset_px),
476 py = *reinterpret_cast<Double_t*>(ion_generator + offset_py),
477 pz = *reinterpret_cast<Double_t*>(ion_generator + offset_pz);
478
479 // beam name by the ion charge (p and d - as in the generator file names parsed by ParseFileName)
480 if ((z == 1) && (a <= 2))
481 beam = (a == 1) ? "p" : "d";
482 else {
483 TGeoElementTable element_table(200);
484 element_table.BuildDefaultElements();
485 TGeoElement* element = element_table.GetElement(z);
486 if (element == nullptr) {
487 LOG(error) << "BmnFunctionSet::GetBeamByIonGenerator: no chemical element for the ion charge: " << z;
488 return -5;
489 }
490 beam = element->GetName(); // upper case, e.g. "XE"
491 beam.ToLower();
492 beam[0] = toupper(beam[0]);
493 }
494
495 // kinetic energy per nucleon by the momentum per nucleon, the nucleon mass is the same as used in the momentum
496 // formula of GetMagneticFieldByEnergy (1.862 = 2 * 0.931)
497 const Double_t kNucleonMass = 0.931; // GeV
498 const Double_t momentum = TMath::Sqrt(px * px + py * py + pz * pz) / a;
499 energy = TMath::Sqrt(momentum * momentum + kNucleonMass * kNucleonMass) - kNucleonMass;
500
501 return 0;
502}
503
504// parsing file name with the given separator (used by ParseFileName)
505// separator: symbol to split the file name into tokens
506// return value: 0 - success, < 0 - error codes
508 const TString& separator,
509 TString& beam,
510 TString& target,
511 Double_t& energy)
512{
513 energy = -1;
514
515 static const vector<pair<TString, TString>> beam_to_particle = {
516 {"bi", "Bi"}, {"pb", "Pb"}, {"au", "Au"}, {"xe", "Xe"}, {"ag", "Ag"},
517 {"kr", "Kr"}, {"ar", "Ar"}, {"c", "C"}, {"d", "d"}, {"p", "p"}};
518 static const vector<pair<TString, TString>> target_to_particle = {
519 {"csi", "CsI"}, {"pb", "Pb"}, {"au", "Au"}, {"cs", "Cs"}, {"xe", "Xe"}, {"sn", "Sn"},
520 {"ag", "Ag"}, {"cu", "Cu"}, {"al", "Al"}, {"c", "C"}, {"p", "p"}};
521
522 // split file name by separation symbol
523 TObjArray* file_tokens = file_name.Tokenize(separator);
524 if (!file_tokens) {
525 LOG(error) << "BmnFunctionSet::ParseFileNameBySeparator: failed to tokenize file name: " << file_name;
526 return -1;
527 }
528 const Int_t n_tokens = file_tokens->GetEntriesFast();
529 Int_t token_num = 0;
530
531 // lambda: get token as TString
532 auto GetToken = [&](Int_t i) -> TString { return static_cast<TObjString*>(file_tokens->At(i))->GetString(); };
533
534 // parse beam and target: group #1 — beam, group #3 — target
535 TPRegexp beam_target_re(TString::Format("^(?P<beam>(%s))(?P<target>(%s))$", JoinKeys(beam_to_particle).Data(),
536 JoinKeys(target_to_particle).Data()));
537
538 TString beam_key, target_key;
539 Bool_t found = kFALSE;
540 while (!found) {
541 if (token_num >= n_tokens)
542 break;
543 TString token_lower = GetToken(token_num);
544 token_lower.ToLower();
545
546 TObjArray* match = beam_target_re.MatchS(token_lower);
547 if (match && match->GetEntriesFast() >= 3) {
548 beam_key = static_cast<TObjString*>(match->At(1))->GetString();
549 target_key = static_cast<TObjString*>(match->At(3))->GetString();
550 found = kTRUE;
551 }
552
553 delete match;
554 ++token_num;
555 }
556
557 if (!found) {
558 LOG(debug) << "BmnFunctionSet::ParseFileNameBySeparator: Beam and Target were not found in the file name: "
559 << file_name << " (separator: " << separator << ")";
560 delete file_tokens;
561 return -2;
562 }
563
564 for (const auto& kv : beam_to_particle) {
565 if (kv.first == beam_key)
566 beam = kv.second;
567 }
568 for (const auto& kv : target_to_particle) {
569 if (kv.first == target_key)
570 target = kv.second;
571 }
572
573 // parse energy: \d+\.?\d*
574 TPRegexp energy_re("\\d+\\.?\\d*");
575 found = kFALSE;
576 bool isUnit = false;
577 TString energy_str;
578 while (!found) {
579 if (token_num >= n_tokens)
580 break;
581 TString token = GetToken(token_num);
582
583 TObjArray* energy_gr = energy_re.MatchS(token);
584 if (energy_gr) {
585 energy_str = static_cast<TObjString*>(energy_gr->At(0))->GetString();
586 found = kTRUE;
587 // check if there is GeV or AGeV
588 token.ToLower();
589 if (token.Contains("gev"))
590 isUnit = true;
591 }
592
593 delete energy_gr;
594 ++token_num;
595 }
596
597 if (!found) {
598 LOG(debug) << "BmnFunctionSet::ParseFileNameBySeparator: Energy was not found in the file name: " << file_name
599 << " (separator: " << separator << ")";
600 delete file_tokens;
601 return -3;
602 }
603
604 // if dot is separator then it breaks float value of the energy into 2 parts
605 if (separator == "." && token_num < n_tokens) {
606 TString token = GetToken(token_num);
607 TPRegexp digits_re("^\\d+");
608 TObjArray* digits_gr = digits_re.MatchS(token);
609 if (digits_gr && digits_gr->GetEntriesFast() > 0) {
610 energy_str += ".";
611 energy_str += static_cast<TObjString*>(digits_gr->At(0))->GetString();
612 // check if there is GeV or AGeV
613 token.ToLower();
614 if (token.Contains("gev"))
615 isUnit = true;
616
617 delete digits_gr;
618 ++token_num;
619 }
620 }
621
622 // if there is no GeV or AGeV label (before or in the next token), then raise error
623 if (!isUnit) {
624 TString token = (token_num < n_tokens) ? GetToken(token_num) : TString("");
625 // check if there is GeV or AGeV
626 token.ToLower();
627 if (!token.Contains("gev")) {
628 LOG(debug) << "BmnFunctionSet::ParseFileNameBySeparator: energy units (gev) were not found in the file "
629 "name: "
630 << file_name << " (separator: " << separator << ")";
631 delete file_tokens;
632 return -4;
633 }
634 }
635
636 delete file_tokens;
637
638 // convert the parsed energy to the number
639 const Double_t energy_value = energy_str.Atof();
640 if (energy_value <= 0) {
641 LOG(debug) << "BmnFunctionSet::ParseFileNameBySeparator: energy value is not correct in the file name: "
642 << file_name << " (separator: " << separator << ")";
643 return -5;
644 }
645 energy = energy_value;
646
647 return 0;
648}
649
650// check whether path is a directory
652{
653 FileStat_t file_stat;
654 gSystem->GetPathInfo(path.Data(), file_stat);
655 if (R_ISDIR(file_stat.fMode))
656 return true;
657 else
658 return false;
659}
660
662{
663 Int_t nans = duration_cast<nanoseconds>(p.time_since_epoch()).count() % 1000000000;
664 Long64_t sec = duration_cast<nanoseconds>(p.time_since_epoch()).count() / 1000000000;
665 return TTimeStamp{time_t(sec), nans};
666}
667
669{
670 Int_t nans = t.GetNanoSec();
671 Int_t sec = t.GetSec();
672 return SysPoint{sec * 1s + nans * 1ns};
673}
674
676{
677 LOG(info) << TimePoint2String(p);
678}
679
681{
682 time_t tt = SysClock::to_time_t(p);
683 auto nans = duration_cast<nanoseconds>(p.time_since_epoch()).count() % 1000000000;
684 std::stringstream sst;
685 sst << std::put_time(std::gmtime(&tt), "%a, %d %b %Y %T %z (%Z)"); // as TTimeStamp
686 // sst << std::put_time(std::localtime(&tt), "%a, %d %b %Y %T %z (%Z)"); // as TTimeStamp
687 sst << " +" << std::setw(9) << nans;
688 return sst.str();
689}
690
691SysPoint BmnFunctionSet::TDatime2GMT(TDatime& tdatime, Int_t timezoneOffsetHours)
692{
693 std::tm tm = {};
694 tm.tm_year = tdatime.GetYear() - 1900;
695 tm.tm_mon = tdatime.GetMonth() - 1;
696 tm.tm_mday = tdatime.GetDay();
697 tm.tm_hour = tdatime.GetHour();
698 tm.tm_min = tdatime.GetMinute();
699 tm.tm_sec = tdatime.GetSecond();
700 time_t t = std::time(nullptr);
701 std::tm* local = std::localtime(&t);
702 int offset = local->tm_gmtoff;
703 return (SysClock::from_time_t(std::mktime(&tm) + offset) - 1h * timezoneOffsetHours);
704}
std::chrono::time_point< SysClock > SysPoint
int i
Definition P4_F32vec4.h:22
bool Bool_t
int Int_t
long Long_t
constexpr Bool_t kFALSE
double Double_t
long long Long64_t
constexpr Bool_t kTRUE
EAccessMode
kFileExists
Bool_t R_ISDIR(Int_t mode)
R__EXTERN TSystem * gSystem
static double GetMagneticFieldByEnergy(const TString &beam, double energy)
static void PrintTimePoint(SysPoint p)
static int CreateDirectoryTree(TString &fileName, int iVerbose=0, EAccessMode mode=kWritePermission)
static SysPoint TDatime2GMT(TDatime &tdatime, Int_t timezoneOffsetHours=0)
static int CheckDirectoryExist(TString &fileName, int iVerbose=0, EAccessMode mode=kFileExists)
static double GetFieldScaleCorrection(double energy)
static TString JoinKeys(const std::vector< std::pair< TString, TString > > &dict)
static Int_t ParseFileName(const TString &file_name, TString &beam, TString &target, Double_t &energy)
static std::string TimePoint2String(SysPoint p)
static bool SetFieldScale(FairField *field, double scale)
static int isSimulationFile(TString fileName="")
static Int_t GetBeamByIonGenerator(FairPrimaryGenerator *primary_generator, TString &beam, Double_t &energy)
static SysPoint TimeStamp2TP(TTimeStamp p)
static bool isDirectory(TString path)
static int GetInputFileList(TString inputFileName, std::vector< TString > &fileList)
static TTimeStamp TimePoint2TS(SysPoint p)
static int GetPeriodNumber(TFile *root_file, int method_number=0)
static int CheckFileExist(TString &fileName, int iVerbose=0, EAccessMode mode=kFileExists)
static Int_t ParseFileNameBySeparator(const TString &file_name, const TString &separator, TString &beam, TString &target, Double_t &energy)
TObjArray * GetListOfFiles() const
static TClass * GetClass(Bool_t load=kTRUE, Bool_t silent=kFALSE)
Longptr_t GetDataMemberOffset(const char *membername) const
const void * DynamicCast(const TClass *base, const void *obj, Bool_t up=kTRUE)
Int_t GetMonth() const
Int_t GetDay() const
Int_t GetHour() const
Int_t GetSecond() const
Int_t GetYear() const
Int_t GetMinute() const
T * Get(const char *namecycle)
TList * GetListOfKeys() const override
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
void Close(Option_t *option="") override
TGeoElement * GetElement(Int_t z)
void BuildDefaultElements()
virtual const char * GetClassName() const
const char * GetName() const override
const char * GetTitle() const override
Int_t GetEntriesFast() const
TObject * At(Int_t idx) const override
const TString & GetString() const
virtual TObject * FindObject(const char *name) const
static TClass * Class()
R__ALWAYS_INLINE Bool_t IsZombie() const
TObjArray * MatchS(const TString &s, const TString &mods="", Int_t start=0, Int_t nMaxMatch=10)
void ToLower()
Int_t Atoi() const
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Double_t Atof() const
const char * Data() const
Bool_t IsDigit() const
TObjArray * Tokenize(const TString &delim) const
static TString Format(const char *fmt,...)
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
time_t GetSec() const
Int_t GetNanoSec() const
TFile * GetCurrentFile() const
Double_t Sqrt(Double_t x)
Double_t Abs(Double_t d)