gsystem
Loading...
Searching...
No Matches
systemTextFactory.cc
Go to the documentation of this file.
1
8// string for gexit
9#include "gutilities.h"
10
11// gsystem
12#include "systemTextFactory.h"
13#include "gsystemConventions.h"
14
15// cpp
16#include <fstream>
17#include <memory>
18
19// returns the file stream, checking all possible directories.
20// SYSTEMTYPE can be:
21// - GTEXTGEOMTYPE (mandatory, exit if not found)
22// - GTEXTMATSTYPE
23std::unique_ptr<std::ifstream> GSystemTextFactory::gSystemTextFileStream(GSystem* system, const std::string& SYSTEMTYPE) {
24 std::string fileName = system->getFilePath();
25 std::string variation = system->getVariation();
26 std::string fname = fileName + SYSTEMTYPE + variation + ".txt";
27
28 log->info(0, "gSystemTextFileStream filename is: ", fname);
29
30 // default dir is "."
31 auto IN = std::make_unique<std::ifstream>(fname.c_str());
32
33 if (IN->good()) {
34 log->info(1, "Trying file ", fname);
35 return IN;
36 }
37 else {
38 // file not good, now trying other locations
39 for (const auto& trialLocation : possibleLocationOfFiles) {
40 std::string newName = trialLocation + "/" + fname;
41
42 log->info(1, "Trying file ", newName);
43
44 IN->open(newName.c_str());
45 if (IN->good()) {
46 // file found, return stream
47 log->info(1, "Trying file ", newName);
48 return IN;
49 }
50 }
51 }
52
53 // at this point the file was not found
54 // this could be ok if we are looking for the material file - if the system annotation is 'mats_only'
55 if (SYSTEMTYPE == GTEXTGEOMTYPE) {
56 if (system->getAnnotations() == "mats_only") {
57 log->info(1, "File ", fname, " not found, but this is ok because the system annotation is 'mats_only'");
58 return nullptr;
59 }
60 log->error(ERR_GSETUPFILENOTOFOUND, "File ", fname, " not found.");
61 }
62
63 // the file was not found
64 return nullptr;
65}
std::shared_ptr< GLogger > log
void info(int level, Args &&... args) const
void error(int exit_code, Args &&... args) const
std::vector< std::string > possibleLocationOfFiles
List of candidate directories used by file-based factories.
Represents a single detector system (e.g., calorimeter, tracker).
Definition gsystem.h:32
std::string getFilePath() const
Gets the full file path of the system.
Definition gsystem.cc:202
std::string getAnnotations() const
Definition gsystem.h:124
std::string getVariation() const
Definition gsystem.h:122
Conventions and shared constants for the detector-system module.
#define ERR_GSETUPFILENOTOFOUND
#define GTEXTGEOMTYPE