6#include "FairFileHeader.h"
9#include "FairRootManager.h"
26using namespace std::chrono;
27using namespace std::chrono_literals;
33 gSystem->ExpandPathName(fileName);
36 cout <<
"ERROR: no specified file: " << fileName << endl;
41 cout <<
"ERROR: no required permissions to access the file: " << fileName << endl;
52 gSystem->ExpandPathName(fileName);
56 cout <<
"ERROR: no specified directory: " << dirName << endl;
61 cout <<
"ERROR: no required permissions to access the directory: " << dirName << endl;
75 cout <<
"ERROR: the specified file name is empty" << endl;
82 cout <<
"ERROR: the directory of the specified file (\"" << fileName
83 <<
"\") does not provide required permissions" << endl;
89 if (status_parent < 0)
91 if (
gSystem->MakeDirectory(dirName) != 0) {
93 cout <<
"ERROR: the directory of the specified file (\"" << fileName <<
"\") could not be created"
110 LOG(error) <<
"BmnFunctionSet::GetInputFileList: no specified file: " << inputFileName;
114 if (inputFileName.
EndsWith(
".root")) {
116 if (!rootFile || rootFile->
IsZombie()) {
117 LOG(error) <<
"BmnFunctionSet::GetInputFileList: opening the ROOT file failed: " << inputFileName;
126 while ((key = (
TKey*)next()) !=
nullptr) {
132 if (chain ==
nullptr)
133 fileList.push_back(inputFileName);
138 fileList.push_back(element->
GetTitle());
143 ifstream listFile(inputFileName.
Data());
144 if (!listFile.is_open()) {
145 LOG(error) <<
"BmnFunctionSet::GetInputFileList: the file list could not be opened: " << inputFileName;
150 while (getline(listFile, line)) {
151 istringstream lineStream(line);
153 if ((lineStream >> word) && (word[0] !=
'#'))
154 fileList.push_back(word);
159 for (
auto& fileName : fileList) {
161 LOG(error) <<
"BmnFunctionSet::GetInputFileList: no specified file: " << fileName <<
" (listed in "
162 << inputFileName <<
")";
167 if (fileList.empty()) {
168 LOG(error) <<
"BmnFunctionSet::GetInputFileList: no input files found in: " << inputFileName;
172 return fileList.size();
177 TFile* fRootFile =
nullptr;
178 if (fileName ==
"") {
179 FairRootManager* ioman = FairRootManager::Instance();
180 if (ioman ==
nullptr) {
181 LOG(error) <<
"isSimulationFile(): FairRootManager is not instantiated";
184 TTree* cur_tree = ioman->GetInTree();
185 if (cur_tree ==
nullptr) {
186 LOG(error) <<
"isSimulationFile(): There is no current tree opened";
192 gSystem->ExpandPathName(fileName);
193 if (
gSystem->AccessPathName(fileName.
Data()) ==
true) {
194 LOG(error) <<
"isSimulationFile(): no specified file: " << fileName;
199 if (!fRootFile || fRootFile->
IsZombie()) {
200 LOG(error) <<
"isSimulationFile(): opening the input file failed: " << fileName;
209 TObject* branch_list = fRootFile->
Get(
"BranchList");
210 if (branch_list !=
nullptr) {
212 if (mctrack_find !=
nullptr) {
213 if (fileName !=
"") {
222 if (fileName !=
"") {
231 if (!root_file || root_file->
IsZombie()) {
232 LOG(error) <<
"BmnFunctionSet::GetPeriodNumber: ROOT-file is not opened " << root_file->
GetName();
236 int period_number = -1;
237 switch (method_number) {
242 while ((key = (
TKey*)next())) {
243 if (std::string(key->
GetClassName()) ==
"FairRtdbRun") {
245 if (str_period_number.
IsDigit()) {
246 period_number = str_period_number.
Atoi();
252 if (period_number == -1) {
253 LOG(error) <<
"BmnFunctionSet::GetPeriodNumber: FairRtdbRun object not found in ROOT-file "
257 return period_number;
261 FairFileHeader* header = root_file->
Get<FairFileHeader>(
"FileHeader");
263 LOG(error) <<
"BmnFunctionSet::GetPeriodNumber: 'FileHeader' branch not found in ROOT-file "
268 period_number = (int)header->GetRunId();
269 return period_number;
281 for (
size_t i = 0;
i < dict.size(); ++
i) {
284 result += dict[
i].first;
294 double field_value = -1;
297 const Double_t kEnergyTolerance = 1e-4;
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}};
301 if ((energy < 1) || (energy > 6)) {
302 LOG(error) <<
"BmnFunctionSet::GetMagneticFieldByEnergy: defined energy is out of range: " << energy;
308 for (;
i < energy_to_field.size(); ++
i) {
309 const auto& entry = energy_to_field[
i];
312 if (
TMath::Abs(energy - entry.first) < kEnergyTolerance) {
313 field_value = entry.second;
317 if (entry.first > energy) {
318 LOG(warning) <<
"BmnFunctionSet::GetMagneticFieldByEnergy: " << energy
319 <<
" GeV is not table value, using approximate formula for the magnetic field";
323 ? energy_to_field[
i - 1]
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];
339 field_value = entry.second *
TMath::Sqrt(energy * (energy + 1.862))
340 /
TMath::Sqrt(entry.first * (entry.first + 1.862));
343 LOG(warning) <<
"BmnFunctionSet::GetMagneticFieldByEnergy: field value is not defined for the beam: " << beam;
354 const Double_t kEnergyTolerance = 1e-4;
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}};
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";
372 if (field ==
nullptr) {
373 LOG(error) <<
"BmnFunctionSet::SetFieldScale: no magnetic field to set the scale";
379 fieldMap->SetScale(scale);
383 fieldConst->SetField(fieldConst->GetBx() * scale, fieldConst->GetBy() * scale, fieldConst->GetBz() * scale);
384 fieldConst->SetFieldOff(scale == 0);
389 bool isScaled =
true;
390 TIter nextField(multiField->GetFieldList());
397 LOG(error) <<
"BmnFunctionSet::SetFieldScale: the scale of the magnetic field of the " << field->ClassName()
398 <<
" class is not supported";
414 LOG(debug) <<
"BmnFunctionSet::ParseFileName: trying to use '.' separator instead of '_' to parse the file name: "
418 LOG(error) <<
"BmnFunctionSet::ParseFileName: beam, target and energy were not defined by the file name: "
436 if ((primary_generator ==
nullptr) || (primary_generator->GetListOfGenerators() ==
nullptr)) {
437 LOG(error) <<
"BmnFunctionSet::GetBeamByIonGenerator: no primary generator or its list of generators";
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) {
454 if (ion_generator ==
nullptr) {
455 LOG(error) <<
"BmnFunctionSet::GetBeamByIonGenerator: FairIonGenerator was not found in the primary generator";
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";
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";
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);
480 if ((z == 1) && (a <= 2))
481 beam = (a == 1) ?
"p" :
"d";
486 if (element ==
nullptr) {
487 LOG(error) <<
"BmnFunctionSet::GetBeamByIonGenerator: no chemical element for the ion charge: " << z;
492 beam[0] = toupper(beam[0]);
497 const Double_t kNucleonMass = 0.931;
499 energy =
TMath::Sqrt(momentum * momentum + kNucleonMass * kNucleonMass) - kNucleonMass;
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"}};
525 LOG(error) <<
"BmnFunctionSet::ParseFileNameBySeparator: failed to tokenize file name: " << file_name;
536 JoinKeys(target_to_particle).Data()));
541 if (token_num >= n_tokens)
543 TString token_lower = GetToken(token_num);
558 LOG(debug) <<
"BmnFunctionSet::ParseFileNameBySeparator: Beam and Target were not found in the file name: "
559 << file_name <<
" (separator: " << separator <<
")";
564 for (
const auto& kv : beam_to_particle) {
565 if (kv.first == beam_key)
568 for (
const auto& kv : target_to_particle) {
569 if (kv.first == target_key)
579 if (token_num >= n_tokens)
581 TString token = GetToken(token_num);
598 LOG(debug) <<
"BmnFunctionSet::ParseFileNameBySeparator: Energy was not found in the file name: " << file_name
599 <<
" (separator: " << separator <<
")";
605 if (separator ==
"." && token_num < n_tokens) {
606 TString token = GetToken(token_num);
624 TString token = (token_num < n_tokens) ? GetToken(token_num) :
TString(
"");
628 LOG(debug) <<
"BmnFunctionSet::ParseFileNameBySeparator: energy units (gev) were not found in the file "
630 << file_name <<
" (separator: " << separator <<
")";
640 if (energy_value <= 0) {
641 LOG(debug) <<
"BmnFunctionSet::ParseFileNameBySeparator: energy value is not correct in the file name: "
642 << file_name <<
" (separator: " << separator <<
")";
645 energy = energy_value;
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;
672 return SysPoint{sec * 1s + nans * 1ns};
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)");
687 sst <<
" +" << std::setw(9) << nans;
694 tm.tm_year = tdatime.
GetYear() - 1900;
696 tm.tm_mday = tdatime.
GetDay();
697 tm.tm_hour = tdatime.
GetHour();
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);
std::chrono::time_point< SysClock > SysPoint
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)
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
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
Double_t Sqrt(Double_t x)