Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JetScapeXML.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file JetScapeXML.cc
1 /*******************************************************************************
2  * Copyright (c) The JETSCAPE Collaboration, 2018
3  *
4  * Modular, task-based framework for simulating all aspects of heavy-ion collisions
5  *
6  * For the list of contributors see AUTHORS.
7  *
8  * Report issues at https://github.com/JETSCAPE/JETSCAPE/issues
9  *
10  * or via email to bugs.jetscape@gmail.com
11  *
12  * Distributed under the GNU General Public License 3.0 (GPLv3 or later).
13  * See COPYING for details.
14  ******************************************************************************/
15 
16 #include "JetScapeXML.h"
17 #include "JetScapeLogger.h"
18 #include <stdlib.h>
19 
20 using namespace std;
21 
22 namespace Jetscape {
23 
24 JetScapeXML *JetScapeXML::m_pInstance = NULL;
25 
26 JetScapeXML *JetScapeXML::Instance() {
27  if (!m_pInstance) {
28  JSINFO << "Created JetScapeXML Instance";
29  m_pInstance = new JetScapeXML();
30  }
31 
32  return m_pInstance;
33 }
34 
35 //________________________________________________________________
36 void JetScapeXML::OpenXMLMasterFile() {
37  JSWARN << "Deprecated function OpenXMLMasterFile(): Call OpenXMLMainFile() instead!";
38  OpenXMLMainFile();
39 }
40 
41 void JetScapeXML::OpenXMLMainFile() {
42 
43  if (!IsMainFileOpen()) {
44 
45  xml_doc_main.LoadFile((char *)GetXMLMainFileName().c_str());
46  VERBOSE(2) << "Trying XML Main file : " << GetXMLMainFileName();
47 
48  if (xml_doc_main.ErrorID() < 1) {
49  JSINFO << "Open XML Main file : " << GetXMLMainFileName();
50  xml_root_main =
51  (tinyxml2::XMLElement *)xml_doc_main.FirstChildElement("jetscape");
52 
53  if (!xml_root_main) {
54  JSWARN << "Not a valid JetScape XML Main file!";
55  exit(-1);
56  }
57  } else { //Check for an old default Master file
58  auto errCode = xml_doc_main.ErrorID(); //Save the original error code
59  SetXMLMainFileName("../config/jetscape_master.xml"); //Try old default Master XML file
60  xml_doc_main.LoadFile((char *)GetXMLMainFileName().c_str());
61  VERBOSE(2) << "Looking for Old XML Master file : " << GetXMLMainFileName();
62 
63  if (xml_doc_main.ErrorID() < 1) {
64  JSWARN << "Using Deprecated XML Master file : " << GetXMLMainFileName();
65  xml_root_main =
66  (tinyxml2::XMLElement *)xml_doc_main.FirstChildElement("jetscape");
67 
68  if (!xml_root_main) {
69  JSWARN << "Not a valid JetScape XML Main file!";
70  exit(-1);
71  }
72  } else {
73  JSWARN << "XML Main file not found/not properly opened! Error code : "
74  << errCode;
75  exit(-1);
76  }
77  }
78 
79  xml_main_file_open = true;
80  }
81 }
82 
83 //________________________________________________________________
84 void JetScapeXML::OpenXMLMasterFile(string m_name) {
85  JSWARN << "Deprecated function OpenXMLMasterFile(): Call OpenXMLMainFile() instead!";
86  OpenXMLMainFile(m_name);
87 }
88 
89 //________________________________________________________________
90 void JetScapeXML::OpenXMLMainFile(string m_name) {
91  SetXMLMainFileName(m_name);
92  OpenXMLMainFile();
93 }
94 
95 //________________________________________________________________
96 void JetScapeXML::OpenXMLUserFile() {
97 
98  if (!xml_user_file_open) {
99 
100  xml_doc_user.LoadFile((char *)GetXMLUserFileName().c_str());
101  VERBOSE(2) << "Trying XML User file : " << GetXMLUserFileName();
102 
103  if (xml_doc_user.ErrorID() < 1) {
104  JSINFO << "Open XML User file : " << GetXMLUserFileName();
105  xml_root_user =
106  (tinyxml2::XMLElement *)xml_doc_user.FirstChildElement("jetscape");
107 
108  if (!xml_root_user) {
109  JSWARN << "Not a valid JetScape XML User file!";
110  exit(-1);
111  }
112  } else {
113  JSWARN << "XML User file not found/not properly opened! Error code : "
114  << xml_doc_user.ErrorID();
115  exit(-1);
116  }
117 
118  xml_user_file_open = true;
119  }
120 }
121 
122 //________________________________________________________________
123 void JetScapeXML::OpenXMLUserFile(string m_name) {
124  SetXMLUserFileName(m_name);
125  OpenXMLUserFile();
126 }
127 
128 //________________________________________________________________
130 JetScapeXML::GetXMLElementMaster(std::initializer_list<const char *> &path) {
131 
132  JSWARN << "Deprecated function GetXMLElementMaster(). Call GetXMLElementMain() instead!";
133  return GetXMLElementMain(path);
134 }
135 
136 //________________________________________________________________
138 JetScapeXML::GetXMLElementMain(std::initializer_list<const char *> &path) {
139 
140  VERBOSE(2) << "Looking for element in Main file: " << path;
141 
142  OpenXMLMainFile();
143 
144  tinyxml2::XMLElement *currentElement = nullptr;
145  for (auto &elementName : path) {
146  if (!currentElement) {
147  currentElement = xml_root_main->FirstChildElement(elementName);
148 
149  if (currentElement) {
150  VERBOSE(3) << "Loaded " << elementName << " from xml_root_main";
151  } else {
152  VERBOSE(3) << elementName << " not found in xml_root_main";
153  }
154  } else {
155  currentElement = currentElement->FirstChildElement(elementName);
156 
157  if (currentElement) {
158  VERBOSE(3) << "Loaded " << elementName << " from "
159  << currentElement->Name();
160  } else {
161  VERBOSE(3) << elementName << " not found in " << elementName;
162  }
163  }
164  }
165 
166  if (currentElement) {
167  VERBOSE(2) << "Found element.";
168  } else {
169  VERBOSE(2) << "Did not find element.";
170  }
171 
172  return currentElement;
173 }
174 
175 //________________________________________________________________
177 JetScapeXML::GetXMLElementUser(std::initializer_list<const char *> &path) {
178 
179  VERBOSE(2) << "Looking for element in User file: " << path;
180 
181  OpenXMLUserFile();
182 
183  tinyxml2::XMLElement *currentElement = nullptr;
184  for (auto &elementName : path) {
185  if (!currentElement) {
186  currentElement = xml_root_user->FirstChildElement(elementName);
187 
188  if (currentElement) {
189  VERBOSE(3) << "Loaded " << elementName << " from xml_root_user";
190  } else {
191  VERBOSE(3) << elementName << " not found in xml_root_user";
192  }
193  } else {
194  currentElement = currentElement->FirstChildElement(elementName);
195 
196  if (currentElement) {
197  VERBOSE(3) << "Loaded " << elementName << " from "
198  << currentElement->Name();
199  } else {
200  VERBOSE(3) << elementName << " not found in " << elementName;
201  }
202  }
203  }
204 
205  if (currentElement) {
206  VERBOSE(2) << "Found element.";
207  } else {
208  VERBOSE(2) << "Did not find element.";
209  }
210 
211  return currentElement;
212 }
213 
214 //________________________________________________________________
216 JetScapeXML::GetElement(std::initializer_list<const char *> path,
217  bool isRequired /* = true */) {
218 
219  // Try to get value from User XML file
220  tinyxml2::XMLElement *elementUser = GetXMLElementUser(path);
221  if (elementUser) {
222  return elementUser;
223  }
224  // Else, try to get value from Main XML file
225  else {
226  tinyxml2::XMLElement *elementMain = GetXMLElementMain(path);
227  if (elementMain) {
228  return elementMain;
229  } else {
230  if (isRequired) {
231  JSWARN << "XML element " << path << " not found, but is required.";
232  exit(-1);
233  }
234  return nullptr;
235  }
236  }
237 }
238 
239 //________________________________________________________________
241 JetScapeXML::GetElementText(std::initializer_list<const char *> path,
242  bool isRequired /* = true */) {
243 
244  tinyxml2::XMLElement *element = GetElement(path, isRequired);
245 
246  if (element) {
247  return element->GetText();
248  } else {
249  return "";
250  }
251 }
252 
253 //________________________________________________________________
254 int JetScapeXML::GetElementInt(std::initializer_list<const char *> path,
255  bool isRequired /* = true */) {
256 
257  tinyxml2::XMLElement *element = GetElement(path, isRequired);
258 
259  if (element) {
260  int value = 0;
261  element->QueryIntText(&value);
262  return value;
263  } else {
264  return 0;
265  }
266 }
267 
268 //________________________________________________________________
269 double JetScapeXML::GetElementDouble(std::initializer_list<const char *> path,
270  bool isRequired /* = true */) {
271 
272  tinyxml2::XMLElement *element = GetElement(path, isRequired);
273 
274  if (element) {
275  double value = 0;
276  element->QueryDoubleText(&value);
277  return value;
278  } else {
279  return 0.;
280  }
281 }
282 
283 //________________________________________________________________
284 std::ostream &operator<<(std::ostream &os,
285  std::initializer_list<const char *> path) {
286 
287  int i = 0;
288  int size = path.size();
289 
290  os << "\"";
291  for (auto name : path) {
292  os << name;
293  if (i < size - 1) {
294  os << ":";
295  }
296  i++;
297  }
298  os << "\"";
299 
300  return os;
301 }
302 
303 } // end namespace Jetscape