1#include "TRestGDMLParser.h"
7string TRestGDMLParser::GetEntityVersion(
const string& name)
const {
8 for (
auto& [entityName, entityVersion] : fEntityVersionMap) {
9 if (entityName == name) {
16void TRestGDMLParser::Load(
const string& filename) {
19 RESTError <<
"TRestGDMLParser: Input GDML file: \"" << filenameAbsolute
20 <<
"\" does not exist. Please double check your current path and filename" <<
RESTendl;
27 std::ifstream t(filenameAbsolute);
28 std::string str((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
32 size_t pp = fFileString.find(
"##VERSION", 0);
33 if (pp != string::npos) {
34 size_t pp2 = fFileString.find(
"##", pp + 4);
35 if (pp2 != string::npos) fGdmlVersion = fFileString.substr(pp + 9, pp2 - pp - 9);
41 cout <<
"TRestGDMLParser: Initializing variables" << endl;
42 int pos = fFileString.find(
"<gdml", 0);
44 string elementString = fFileString.substr(pos, -1);
52 cout <<
"TRestGDMLParser: Replacing expressions in GDML" << endl;
54 fFileString =
Replace(fFileString,
"= \"",
"=\"");
55 fFileString =
Replace(fFileString,
" =\"",
"=\"");
56 fFileString =
Replace(fFileString,
" = \"",
"=\"");
58 ReplaceAttributeWithKeyWord(
"cos(");
59 ReplaceAttributeWithKeyWord(
"sin(");
60 ReplaceAttributeWithKeyWord(
"tan(");
61 ReplaceAttributeWithKeyWord(
"sqrt(");
62 ReplaceAttributeWithKeyWord(
"log(");
63 ReplaceAttributeWithKeyWord(
"exp(");
67 fOutputGdmlFilename = fOutputGdmlDirectory +
"PID" + std::to_string(getpid()) +
"_" + filenameNoPath;
68 cout <<
"TRestGDMLParser: Creating temporary file at: \"" << fOutputGdmlFilename <<
"\"" << endl;
70 filesystem::create_directories(fOutputGdmlDirectory);
73 outputFile.open(fOutputGdmlFilename, ios::trunc);
74 outputFile << fFileString << endl;
77 std::ifstream fileToCheckExistence(fOutputGdmlFilename);
78 if (!fileToCheckExistence) {
79 std::cout <<
"TRestGDMLParser: Problem writing temporary file." << std::endl;
84TGeoManager* TRestGDMLParser::CreateGeoManager() {
86 if (!fOutputGdmlFilename.empty()) {
87 const auto currentPath = filesystem::current_path();
88 filesystem::current_path(fOutputGdmlDirectory);
89 auto geoManager =
new TGeoManager();
90 geoManager->Import(fOutputGdmlFilename.c_str());
91 filesystem::current_path(currentPath);
97void TRestGDMLParser::PrintContent() { cout << fFileString << endl; }
99void TRestGDMLParser::ReplaceEntity() {
101 while ((pos = fFileString.find(
"<!ENTITY", pos)) != -1) {
102 int pos1 = fFileString.find_first_not_of(
" ", pos + 8);
103 int pos2 = fFileString.find(
"SYSTEM", pos1);
106 int pos3 = fFileString.find(
"\"", pos2) + 1;
107 int pos4 = fFileString.find(
"\"", pos3);
110 cout <<
"TRestGDMLParser: Replacing entity: " << entityName <<
", file: " << entityFile << endl;
112 if ((
int)entityFile.find(
"http") != -1) {
114 fOutputGdmlDirectory +
"PID" + std::to_string(getpid()) +
"_" + entityName +
".xml";
117 cout <<
"TRestGDMLParser: Download failed!" << endl;
120 entityFile = entityField;
122 entityFile = fPath +
"/" + entityFile;
126 if ((pos5 = fFileString.find(
"&" + entityName +
";")) != -1) {
128 std::ifstream entityFileRead(entityFile);
129 std::string str((std::istreambuf_iterator<char>(entityFileRead)),
130 std::istreambuf_iterator<char>());
131 entityFileRead.close();
135 fEntityVersionMap[entityName] =
"";
136 size_t pp = str.find(
"##VERSION", 0);
137 if (pp != string::npos) {
138 size_t pp2 = str.find(
"##", pp + 4);
139 if (pp2 != string::npos) {
140 fEntityVersionMap[entityName] = str.substr(pp + 9, pp2 - pp - 9);
144 fFileString.replace(pos5, entityName.length() + 2, str);
146 cout <<
"GDML ERROR! No matching reference file for entity: \"" << entityName <<
"\"" << endl;
147 cout <<
"file name: \"" << entityFile <<
"\"" << endl;
151 cout <<
"TRestGDMLParser: Warning! redundant entity: \"" << entityName <<
"\"" << endl;
157void TRestGDMLParser::ReplaceAttributeWithKeyWord(
const string& keyword) {
159 while ((n = fFileString.find(keyword, 0)) != -1) {
160 int pos1 = 0, pos2 = 0;
161 for (
int i = n; i >= 0; i--) {
162 if (fFileString[i] ==
'"') {
168 for (
unsigned int i = n; i < fFileString.size(); i++) {
169 if (fFileString[i] ==
'"') {
174 string target = fFileString.substr(pos1, pos2 - pos1);
177 if (replace == target) {
178 cout <<
"Error! failed to replace mathematical expressions! check the file!" << endl;
179 cout << replace << endl;
182 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.