3#include "FairFileHeader.h"
5#include "FairRootManager.h"
20using namespace std::chrono;
21using namespace std::chrono_literals;
27 gSystem->ExpandPathName(fileName);
30 cout <<
"ERROR: no specified file: " << fileName << endl;
35 cout <<
"ERROR: no required permissions to access the file: " << fileName << endl;
46 gSystem->ExpandPathName(fileName);
50 cout <<
"ERROR: no specified directory: " << dirName << endl;
55 cout <<
"ERROR: no required permissions to access the directory: " << dirName << endl;
69 cout <<
"ERROR: the specified file name is empty" << endl;
76 cout <<
"ERROR: the directory of the specified file (\"" << fileName
77 <<
"\") does not provide required permissions" << endl;
83 if (status_parent < 0)
85 if (
gSystem->MakeDirectory(dirName) != 0) {
87 cout <<
"ERROR: the directory of the specified file (\"" << fileName <<
"\") could not be created"
104 LOG(error) <<
"BmnFunctionSet::GetInputFileList: no specified file: " << inputFileName;
108 if (inputFileName.
EndsWith(
".root")) {
110 if (!rootFile || rootFile->
IsZombie()) {
111 LOG(error) <<
"BmnFunctionSet::GetInputFileList: opening the ROOT file failed: " << inputFileName;
117 TChain* chain =
nullptr;
120 while ((key = (TKey*)next()) !=
nullptr) {
121 if (strcmp(
key->GetClassName(),
"TChain") == 0) {
122 chain = rootFile->
Get<TChain>(
key->GetName());
126 if (chain ==
nullptr)
127 fileList.push_back(inputFileName);
130 TChainElement* element;
131 while ((element = (TChainElement*)nextElement()) !=
nullptr)
132 fileList.push_back(element->
GetTitle());
137 ifstream listFile(inputFileName.
Data());
138 if (!listFile.is_open()) {
139 LOG(error) <<
"BmnFunctionSet::GetInputFileList: the file list could not be opened: " << inputFileName;
144 while (getline(listFile, line)) {
145 istringstream lineStream(line);
147 if ((lineStream >> word) && (word[0] !=
'#'))
148 fileList.push_back(word);
153 for (
auto& fileName : fileList) {
155 LOG(error) <<
"BmnFunctionSet::GetInputFileList: no specified file: " << fileName <<
" (listed in "
156 << inputFileName <<
")";
161 if (fileList.empty()) {
162 LOG(error) <<
"BmnFunctionSet::GetInputFileList: no input files found in: " << inputFileName;
166 return fileList.size();
171 TFile* fRootFile =
nullptr;
172 if (fileName ==
"") {
173 FairRootManager* ioman = FairRootManager::Instance();
174 if (ioman ==
nullptr) {
175 LOG(error) <<
"isSimulationFile(): FairRootManager is not instantiated";
178 TTree* cur_tree = ioman->GetInTree();
179 if (cur_tree ==
nullptr) {
180 LOG(error) <<
"isSimulationFile(): There is no current tree opened";
186 gSystem->ExpandPathName(fileName);
187 if (
gSystem->AccessPathName(fileName.
Data()) ==
true) {
188 LOG(error) <<
"isSimulationFile(): no specified file: " << fileName;
193 if (!fRootFile || fRootFile->
IsZombie()) {
194 LOG(error) <<
"isSimulationFile(): opening the input file failed: " << fileName;
203 TObject* branch_list = fRootFile->
Get(
"BranchList");
204 if (branch_list !=
nullptr) {
206 if (mctrack_find !=
nullptr) {
207 if (fileName !=
"") {
216 if (fileName !=
"") {
225 if (!root_file || root_file->
IsZombie()) {
226 LOG(error) <<
"BmnFunctionSet::GetPeriodNumber: ROOT-file is not opened " << root_file->
GetName();
230 int period_number = -1;
231 switch (method_number) {
236 while ((key = (
TKey*)next())) {
237 if (std::string(key->GetClassName()) ==
"FairRtdbRun") {
238 TString str_period_number = key->GetName();
239 if (str_period_number.
IsDigit()) {
240 period_number = str_period_number.
Atoi();
246 if (period_number == -1) {
247 LOG(error) <<
"BmnFunctionSet::GetPeriodNumber: FairRtdbRun object not found in ROOT-file "
251 return period_number;
255 FairFileHeader* header = root_file->
Get<FairFileHeader>(
"FileHeader");
257 LOG(error) <<
"BmnFunctionSet::GetPeriodNumber: 'FileHeader' branch not found in ROOT-file "
262 period_number = (int)header->GetRunId();
263 return period_number;
275 for (
size_t i = 0;
i < dict.size(); ++
i) {
278 result += dict[
i].first;
288 double field_value = -1;
291 const Double_t kEnergyTolerance = 1e-4;
293 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}};
295 if ((energy < 1) || (energy > 6)) {
296 LOG(error) <<
"BmnFunctionSet::GetMagneticFieldByEnergy: defined energy is out of range: " << energy;
302 for (;
i < energy_to_field.size(); ++
i) {
303 const auto& entry = energy_to_field[
i];
306 if (
TMath::Abs(energy - entry.first) < kEnergyTolerance) {
307 field_value = entry.second;
311 if (entry.first > energy) {
312 LOG(warning) <<
"BmnFunctionSet::GetMagneticFieldByEnergy: " << energy
313 <<
" GeV is not table value, using approximate formula for the magnetic field";
317 ? energy_to_field[
i - 1]
328 if (
i == energy_to_field.size()) {
329 LOG(warning) <<
"BmnFunctionSet::GetMagneticFieldByEnergy: " << energy
330 <<
" GeV is not table value, using approximate formula for the magnetic field";
331 const auto& entry = energy_to_field[
i - 1];
333 field_value = entry.second *
TMath::Sqrt(energy * (energy + 1.862))
334 /
TMath::Sqrt(entry.first * (entry.first + 1.862));
337 LOG(warning) <<
"BmnFunctionSet::GetMagneticFieldByEnergy: field value is not defined for the beam: " << beam;
348 const Double_t kEnergyTolerance = 1e-4;
350 const vector<pair<Double_t, Double_t>> energy_to_correction = {{1.6, 1.01}, {2.2, 1. / 1.005}, {3.0, 1. / 1.02}};
352 for (
const auto& entry : energy_to_correction)
353 if (
TMath::Abs(energy - entry.first) < kEnergyTolerance) {
354 LOG(debug) <<
"BmnFunctionSet::GetFieldScaleCorrection: the field map scale is corrected by "
355 << entry.second <<
" for the beam energy: " << energy <<
" GeV";
375 LOG(debug) <<
"BmnFunctionSet::ParseFileName: trying to use '.' separator instead of '_' to parse the file name: "
379 LOG(error) <<
"BmnFunctionSet::ParseFileName: beam, target and energy were not defined by the file name: "
397 TString setup_tag =
"", unexpected_part =
"";
405 if (is_xml_file && local_settings.
IsNull())
406 local_settings = token;
407 else if (!is_xml_file && setup_tag.
IsNull())
409 else if (unexpected_part.IsNull())
410 unexpected_part = token;
415 if (local_settings.
IsNull()) {
416 LOG(warning) <<
"BmnFunctionSet::ParseGeometryTag: no XML file with a geometry customization was found after "
417 "';', the whole value is used as the setup tag: "
421 if (!unexpected_part.IsNull()) {
422 LOG(error) <<
"BmnFunctionSet::ParseGeometryTag: unexpected part '" << unexpected_part
423 <<
"' in the geometry tag: " << geometry_tag;
428 geometry_tag = setup_tag;
431 TString local_settings_path = local_settings;
433 LOG(error) <<
"BmnFunctionSet::ParseGeometryTag: no XML file with a geometry customization: "
434 << local_settings_path;
453 static const vector<pair<TString, TString>> beam_to_particle = {
454 {
"bi",
"Bi"}, {
"pb",
"Pb"}, {
"au",
"Au"}, {
"xe",
"Xe"}, {
"ag",
"Ag"},
455 {
"kr",
"Kr"}, {
"ar",
"Ar"}, {
"c",
"C"}, {
"d",
"d"}, {
"p",
"p"}};
456 static const vector<pair<TString, TString>> target_to_particle = {
457 {
"csi",
"CsI"}, {
"pb",
"Pb"}, {
"au",
"Au"}, {
"cs",
"Cs"}, {
"xe",
"Xe"}, {
"sn",
"Sn"},
458 {
"ag",
"Ag"}, {
"cu",
"Cu"}, {
"al",
"Al"}, {
"c",
"C"}, {
"p",
"p"}};
463 LOG(error) <<
"BmnFunctionSet::ParseFileNameBySeparator: failed to tokenize file name: " << file_name;
474 JoinKeys(target_to_particle).Data()));
479 if (token_num >= n_tokens)
481 TString token_lower = GetToken(token_num);
496 LOG(debug) <<
"BmnFunctionSet::ParseFileNameBySeparator: Beam and Target were not found in the file name: "
497 << file_name <<
" (separator: " << separator <<
")";
502 for (
const auto& kv : beam_to_particle) {
503 if (kv.first == beam_key)
506 for (
const auto& kv : target_to_particle) {
507 if (kv.first == target_key)
517 if (token_num >= n_tokens)
519 TString token = GetToken(token_num);
527 if (token.Contains(
"gev"))
536 LOG(debug) <<
"BmnFunctionSet::ParseFileNameBySeparator: Energy was not found in the file name: " << file_name
537 <<
" (separator: " << separator <<
")";
543 if (separator ==
"." && token_num < n_tokens) {
544 TString token = GetToken(token_num);
552 if (token.Contains(
"gev"))
562 TString token = (token_num < n_tokens) ? GetToken(token_num) :
TString(
"");
565 if (!token.Contains(
"gev")) {
566 LOG(debug) <<
"BmnFunctionSet::ParseFileNameBySeparator: energy units (gev) were not found in the file "
568 << file_name <<
" (separator: " << separator <<
")";
578 if (energy_value <= 0) {
579 LOG(debug) <<
"BmnFunctionSet::ParseFileNameBySeparator: energy value is not correct in the file name: "
580 << file_name <<
" (separator: " << separator <<
")";
583 energy = energy_value;
592 gSystem->GetPathInfo(path.Data(), file_stat);
601 Int_t nans = duration_cast<nanoseconds>(p.time_since_epoch()).count() % 1000000000;
602 Long64_t sec = duration_cast<nanoseconds>(p.time_since_epoch()).count() / 1000000000;
610 return SysPoint{sec * 1s + nans * 1ns};
620 time_t tt = SysClock::to_time_t(p);
621 auto nans = duration_cast<nanoseconds>(p.time_since_epoch()).count() % 1000000000;
622 std::stringstream sst;
623 sst << std::put_time(std::gmtime(&tt),
"%a, %d %b %Y %T %z (%Z)");
625 sst <<
" +" << std::setw(9) << nans;
632 tm.tm_year = tdatime.
GetYear() - 1900;
634 tm.tm_mday = tdatime.
GetDay();
635 tm.tm_hour = tdatime.
GetHour();
638 time_t t = std::time(
nullptr);
639 std::tm* local = std::localtime(&t);
640 int offset = local->tm_gmtoff;
641 return (SysClock::from_time_t(std::mktime(&tm) + offset) - 1h * timezoneOffsetHours);
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 Int_t ParseGeometryTag(TString &geometry_tag, TString &local_settings)
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 int isSimulationFile(TString fileName="")
static SysPoint TimeStamp2TP(TTimeStamp p)
static bool isDirectory(TString path)
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 GetInputFileList(TString inputFileName, std::vector< TString > &fileList)
static Int_t ParseFileNameBySeparator(const TString &file_name, const TString &separator, TString &beam, TString &target, Double_t &energy)
TObjArray * GetListOfFiles() 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
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
R__ALWAYS_INLINE Bool_t IsZombie() const
TObjArray * MatchS(const TString &s, const TString &mods="", Int_t start=0, Int_t nMaxMatch=10)
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
const char * Data() const
TObjArray * Tokenize(const TString &delim) const
static TString Format(const char *fmt,...)
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
TFile * GetCurrentFile() const
std::chrono::time_point< SysClock > SysPoint
Double_t Sqrt(Double_t x)