1#include "TRestGDMLParser.h"
10string TRestGDMLParser::GetEntityVersion(
const string& name)
const {
11 for (
auto& [entityName, entityVersion] : fEntityVersionMap) {
12 if (entityName == name) {
19void TRestGDMLParser::Load(
const string& filename) {
22 RESTError <<
"TRestGDMLParser: Input GDML file: \"" << filenameAbsolute
23 <<
"\" does not exist. Please double check your current path and filename" <<
RESTendl;
30 std::ifstream t(filenameAbsolute);
31 std::string str((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
35 size_t pp = fFileString.find(
"##VERSION", 0);
36 if (pp != string::npos) {
37 size_t pp2 = fFileString.find(
"##", pp + 4);
38 if (pp2 != string::npos) fGdmlVersion = fFileString.substr(pp + 9, pp2 - pp - 9);
44 cout <<
"TRestGDMLParser: Initializing variables" << endl;
45 int pos = fFileString.find(
"<gdml", 0);
47 string elementString = fFileString.substr(pos, -1);
55 cout <<
"TRestGDMLParser: Replacing expressions in GDML" << endl;
57 fFileString =
Replace(fFileString,
"= \"",
"=\"");
58 fFileString =
Replace(fFileString,
" =\"",
"=\"");
59 fFileString =
Replace(fFileString,
" = \"",
"=\"");
61 ReplaceAttributeWithKeyWord(
"cos(");
62 ReplaceAttributeWithKeyWord(
"sin(");
63 ReplaceAttributeWithKeyWord(
"tan(");
64 ReplaceAttributeWithKeyWord(
"sqrt(");
65 ReplaceAttributeWithKeyWord(
"log(");
66 ReplaceAttributeWithKeyWord(
"exp(");
70 fOutputGdmlFilename = fOutputGdmlDirectory +
"PID" + std::to_string(getpid()) +
"_" + filenameNoPath;
71 cout <<
"TRestGDMLParser: Creating temporary file at: \"" << fOutputGdmlFilename <<
"\"" << endl;
73 filesystem::create_directories(fOutputGdmlDirectory);
76 outputFile.open(fOutputGdmlFilename, ios::trunc);
77 outputFile << fFileString << endl;
80 std::ifstream fileToCheckExistence(fOutputGdmlFilename);
81 if (!fileToCheckExistence) {
82 std::cout <<
"TRestGDMLParser: Problem writing temporary file." << std::endl;
87TGeoManager* TRestGDMLParser::CreateGeoManager() {
89 if (!fOutputGdmlFilename.empty()) {
90 const auto currentPath = filesystem::current_path();
91 filesystem::current_path(fOutputGdmlDirectory);
92 auto geoManager =
new TGeoManager();
93 geoManager->Import(fOutputGdmlFilename.c_str());
94 filesystem::current_path(currentPath);
100void TRestGDMLParser::PrintContent() { cout << fFileString << endl; }
102void TRestGDMLParser::ReplaceEntity() {
104 while ((pos = fFileString.find(
"<!ENTITY", pos)) != -1) {
105 int pos1 = fFileString.find_first_not_of(
" ", pos + 8);
106 int pos2 = fFileString.find(
"SYSTEM", pos1);
109 int pos3 = fFileString.find(
"\"", pos2) + 1;
110 int pos4 = fFileString.find(
"\"", pos3);
113 cout <<
"TRestGDMLParser: Replacing entity: " << entityName <<
", file: " << entityFile << endl;
115 if ((
int)entityFile.find(
"http") != -1) {
117 fOutputGdmlDirectory +
"PID" + std::to_string(getpid()) +
"_" + entityName +
".xml";
120 cout <<
"TRestGDMLParser: Download failed!" << endl;
123 entityFile = entityField;
125 entityFile = fPath +
"/" + entityFile;
129 if ((pos5 = fFileString.find(
"&" + entityName +
";")) != -1) {
131 std::ifstream entityFileRead(entityFile);
132 std::string str((std::istreambuf_iterator<char>(entityFileRead)),
133 std::istreambuf_iterator<char>());
134 entityFileRead.close();
138 fEntityVersionMap[entityName] =
"";
139 size_t pp = str.find(
"##VERSION", 0);
140 if (pp != string::npos) {
141 size_t pp2 = str.find(
"##", pp + 4);
142 if (pp2 != string::npos) {
143 fEntityVersionMap[entityName] = str.substr(pp + 9, pp2 - pp - 9);
147 fFileString.replace(pos5, entityName.length() + 2, str);
149 cout <<
"GDML ERROR! No matching reference file for entity: \"" << entityName <<
"\"" << endl;
150 cout <<
"file name: \"" << entityFile <<
"\"" << endl;
154 cout <<
"TRestGDMLParser: Warning! redundant entity: \"" << entityName <<
"\"" << endl;
160void TRestGDMLParser::ReplaceAttributeWithKeyWord(
const string& keyword) {
162 while ((n = fFileString.find(keyword, 0)) != -1) {
163 int pos1 = 0, pos2 = 0;
164 for (
int i = n; i >= 0; i--) {
165 if (fFileString[i] ==
'"') {
171 for (
unsigned int i = n; i < fFileString.size(); i++) {
172 if (fFileString[i] ==
'"') {
177 string target = fFileString.substr(pos1, pos2 - pos1);
180 if (replace == target) {
181 cout <<
"Error! failed to replace mathematical expressions! check the file!" << endl;
182 cout << replace << endl;
185 fFileString.replace(pos1, pos2 - pos1, replace);
std::string RemoveWhiteSpaces(std::string in)
Returns the input string removing all white spaces.
std::string Replace(std::string in, std::string thisString, std::string byThisString, size_t fromPosition=0, Int_t N=0)
Replace any occurences of thisSring by byThisString inside string in.
std::string ReplaceMathematicalExpressions(std::string buffer, Int_t precision=0, std::string errorMessage="")
Evaluates and replaces valid mathematical expressions found in the input string buffer.