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 fElementGlobal = define ==
nullptr ? nullptr :
new TiXmlElement(*define);
57 cout <<
"TRestGDMLParser: Replacing expressions in GDML" << endl;
59 fFileString =
Replace(fFileString,
"= \"",
"=\"");
60 fFileString =
Replace(fFileString,
" =\"",
"=\"");
61 fFileString =
Replace(fFileString,
" = \"",
"=\"");
63 ReplaceAttributeWithKeyWord(
"cos(");
64 ReplaceAttributeWithKeyWord(
"sin(");
65 ReplaceAttributeWithKeyWord(
"tan(");
66 ReplaceAttributeWithKeyWord(
"sqrt(");
67 ReplaceAttributeWithKeyWord(
"log(");
68 ReplaceAttributeWithKeyWord(
"exp(");
72 fOutputGdmlFilename = fOutputGdmlDirectory +
"PID" + std::to_string(getpid()) +
"_" + filenameNoPath;
73 cout <<
"TRestGDMLParser: Creating temporary file at: \"" << fOutputGdmlFilename <<
"\"" << endl;
75 filesystem::create_directories(fOutputGdmlDirectory);
78 outputFile.open(fOutputGdmlFilename, ios::trunc);
79 outputFile << fFileString << endl;
82 std::ifstream fileToCheckExistence(fOutputGdmlFilename);
83 if (!fileToCheckExistence) {
84 std::cout <<
"TRestGDMLParser: Problem writing temporary file." << std::endl;
89TGeoManager* TRestGDMLParser::CreateGeoManager() {
91 if (!fOutputGdmlFilename.empty()) {
92 const auto currentPath = filesystem::current_path();
93 filesystem::current_path(fOutputGdmlDirectory);
94 auto geoManager =
new TGeoManager();
95 geoManager->Import(fOutputGdmlFilename.c_str());
96 filesystem::current_path(currentPath);
102void TRestGDMLParser::PrintContent() { cout << fFileString << endl; }
104void TRestGDMLParser::ReplaceEntity() {
106 while ((pos = fFileString.find(
"<!ENTITY", pos)) != -1) {
107 int pos1 = fFileString.find_first_not_of(
" ", pos + 8);
108 int pos2 = fFileString.find(
"SYSTEM", pos1);
111 int pos3 = fFileString.find(
"\"", pos2) + 1;
112 int pos4 = fFileString.find(
"\"", pos3);
115 cout <<
"TRestGDMLParser: Replacing entity: " << entityName <<
", file: " << entityFile << endl;
117 if ((
int)entityFile.find(
"http") != -1) {
119 fOutputGdmlDirectory +
"PID" + std::to_string(getpid()) +
"_" + entityName +
".xml";
122 cout <<
"TRestGDMLParser: Download failed!" << endl;
125 entityFile = entityField;
127 entityFile = fPath +
"/" + entityFile;
131 if ((pos5 = fFileString.find(
"&" + entityName +
";")) != -1) {
133 std::ifstream entityFileRead(entityFile);
134 std::string str((std::istreambuf_iterator<char>(entityFileRead)),
135 std::istreambuf_iterator<char>());
136 entityFileRead.close();
140 fEntityVersionMap[entityName] =
"";
141 size_t pp = str.find(
"##VERSION", 0);
142 if (pp != string::npos) {
143 size_t pp2 = str.find(
"##", pp + 4);
144 if (pp2 != string::npos) {
145 fEntityVersionMap[entityName] = str.substr(pp + 9, pp2 - pp - 9);
149 fFileString.replace(pos5, entityName.length() + 2, str);
151 cout <<
"GDML ERROR! No matching reference file for entity: \"" << entityName <<
"\"" << endl;
152 cout <<
"file name: \"" << entityFile <<
"\"" << endl;
156 cout <<
"TRestGDMLParser: Warning! redundant entity: \"" << entityName <<
"\"" << endl;
162void TRestGDMLParser::ReplaceAttributeWithKeyWord(
const string& keyword) {
164 while ((n = fFileString.find(keyword, 0)) != -1) {
165 int pos1 = 0, pos2 = 0;
166 for (
int i = n; i >= 0; i--) {
167 if (fFileString[i] ==
'"') {
173 for (
unsigned int i = n; i < fFileString.size(); i++) {
174 if (fFileString[i] ==
'"') {
179 string target = fFileString.substr(pos1, pos2 - pos1);
182 if (replace == target) {
183 cout <<
"Error! failed to replace mathematical expressions! check the file!" << endl;
184 cout << replace << endl;
187 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.