REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestMetadata.h
1/*************************************************************************
2 * This file is part of the REST software framework. *
3 * *
4 * Copyright (C) 2016 GIFNA/TREX (University of Zaragoza) *
5 * For more information see http://gifna.unizar.es/trex *
6 * *
7 * REST is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * REST is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have a copy of the GNU General Public License along with *
18 * REST in $REST_PATH/LICENSE. *
19 * If not, see http://www.gnu.org/licenses/. *
20 * For the list of contributors see $REST_PATH/CREDITS. *
21 *************************************************************************/
22
23#ifndef RestCore_TRestMetadata
24#define RestCore_TRestMetadata
25
26#define TIXML_USE_STL
27
28#include <TApplication.h>
29#include <TClass.h>
30#include <TStreamerElement.h>
31#include <TVirtualStreamerInfo.h>
32
33#include <mutex>
34#include <thread>
35
36#include "TRestDataBase.h"
37#include "TRestPhysics.h"
38#include "TRestReflector.h"
39#include "TRestStringHelper.h"
40#include "TRestStringOutput.h"
41#include "TRestSystemOfUnits.h"
42#include "TRestTools.h"
43#include "TRestVersion.h"
44#include "tinyxml.h"
45
46/* We keep using REST_RELEASE, REST_VERSION(2,X,Y) and REST_VERSION_CODE
47 to determine the installed REST version and avoid too much prototyping
48
49REST version is connected independently to each metadata structure. TRestRun
50 should be our reference to determine the version of REST used to write the
51ROOT file.
52
53By including #include "TRestVersion.h" we get access to the definition of
54 REST_RELEASE, REST_VERSION_CODE, REST_VERSION(2,X,Y) which is the REST
55version we are using.
56
57// storing the STATIC version of this installation
58namespace REST_VersionGlob {
59 TString GetRESTVersion();
60 int GetRESTVersionCode();
61};
62inline TString GetRESTVersion() const { return REST_VersionGlob::GetRESTVersion(); }
63inline int GetRESTVersionCode() const { return REST_VersionGlob::GetRESTVersionCode(); }
64
65*/
66
67class TRestManager;
68
70class TRestMetadata : public TNamed {
71 private:
72 void ReadEnvInElement(TiXmlElement* e, bool overwrite = true);
73 void ReadElement(TiXmlElement* e, bool recursive = false);
74 void ReplaceForLoopVars(TiXmlElement* e, std::map<std::string, std::string> forLoopVar);
75 void ExpandForLoopOnce(TiXmlElement* e, const std::map<std::string, std::string>& forLoopVar);
76 void ExpandForLoops(TiXmlElement* e, std::map<std::string, std::string> forLoopVar);
77 void ExpandIfSections(TiXmlElement* e);
78 void ExpandIncludeFile(TiXmlElement* e);
79 std::string GetUnits(TiXmlElement* e);
80 std::string FieldNamesToUpper(std::string inputString);
81 void ReadOneParameter(std::string name, std::string value);
82 TiXmlElement* ReplaceElementAttributes(TiXmlElement* e);
83
85 TString fVersion = REST_RELEASE; //<
86 TString fCommit = REST_COMMIT; //<
87 TString fLibraryVersion = "0"; //<
88
89 Bool_t fOfficialRelease = false; //<
90 Bool_t fCleanState = false; //<
91
92 protected:
93 // new xml utilities
94 std::string GetFieldValue(std::string parName, TiXmlElement* e);
95 std::string GetParameter(std::string parName, TiXmlElement* e,
96 TString defaultValue = PARAMETER_NOT_FOUND_STR);
97 Double_t GetDblParameterWithUnits(std::string parName, TiXmlElement* e,
98 Double_t defaultVal = PARAMETER_NOT_FOUND_DBL);
99 TVector2 Get2DVectorParameterWithUnits(std::string parName, TiXmlElement* e,
100 TVector2 defaultValue = TVector2(-1, -1));
101 TVector3 Get3DVectorParameterWithUnits(std::string parName, TiXmlElement* e,
102 TVector3 defaultValue = TVector3(-1, -1, -1));
103 TiXmlElement* GetElementFromFile(std::string configFilename, std::string NameOrDecalre = "");
104 TiXmlElement* GetElement(std::string eleDeclare, TiXmlElement* e = nullptr);
105 TiXmlElement* GetNextElement(TiXmlElement* e);
106 TiXmlElement* GetElementWithName(std::string eleDeclare, std::string eleName, TiXmlElement* e);
107 TiXmlElement* GetElementWithName(std::string eleDeclare, std::string eleName);
108 std::pair<std::string, std::string> GetParameterAndUnits(std::string parname, TiXmlElement* e = nullptr);
109 TiXmlElement* StringToElement(std::string definition);
110 std::string ElementToString(TiXmlElement* ele);
111
112 // old xml parser interface.
113 std::string GetKEYStructure(std::string keyName);
114 std::string GetKEYStructure(std::string keyName, size_t& Position);
115 std::string GetKEYStructure(std::string keyName, std::string buffer);
116 std::string GetKEYStructure(std::string keyName, size_t& Position, std::string buffer);
117 std::string GetKEYStructure(std::string keyName, size_t& Position, TiXmlElement* ele);
118 std::string GetKEYDefinition(std::string keyName);
119 std::string GetKEYDefinition(std::string keyName, size_t& Position);
120 std::string GetKEYDefinition(std::string keyName, std::string buffer);
121 std::string GetKEYDefinition(std::string keyName, size_t& Position, std::string buffer);
122 std::string GetParameter(std::string parName, size_t& pos, std::string inputString);
123 std::string GetFieldValue(std::string fieldName, std::string definition, size_t fromPosition = 0);
124
125 // some utils
126 std::string ReplaceVariables(const std::string buffer);
127 std::string ReplaceConstants(const std::string buffer);
128 std::string SearchFile(std::string filename);
129 TString GetSearchPath();
130 void ReSetVersion();
131 void UnSetVersion();
132 void SetLibraryVersion(TString version);
133
134 // Load global setting for the rml section, e.g., name, title.
135 virtual Int_t LoadSectionMetadata();
137 virtual void InitFromConfigFile() {
138 std::map<std::string, std::string> parameters = GetParametersList();
139
140 for (auto& p : parameters) p.second = ReplaceMathematicalExpressions(p.second);
141
142 ReadParametersList(parameters);
143 }
144
146 virtual void InitFromRootFile() {}
147
148 void ReadParametersList(std::map<std::string, std::string>& list);
149
156
158 std::string fConfigFileName;
160 std::string fSectionName;
162 std::string configBuffer;
164 std::string messageBuffer;
165
169 endl_t RESTendl;
170
174 Bool_t fStore;
176 TiXmlElement* fElement;
178 TiXmlElement* fElementGlobal;
180 std::map<std::string, std::string> fVariables;
182 std::map<std::string, std::string> fConstants;
183
185 Bool_t fError = false;
186
188 Int_t fNErrors = 0;
189
191 Bool_t fWarning = false;
192
194 Int_t fNWarnings = 0;
195
197 TString fErrorMessage = "";
198
200 TString fWarningMessage = "";
201
202 std::map<std::string, std::string> GetParametersList();
203 void ReadAllParameters();
204
206 TRestMetadata(const char* configFilename);
207
208 public:
210 inline Bool_t GetError() const { return fError; }
211
213 inline Bool_t GetWarning() const { return fWarning; }
214
216 void AddLog(std::string log = "", bool print = true);
217
219 void SetError(std::string message = "", bool print = true, int maxPrint = 5);
220
222 void SetWarning(std::string message = "", bool print = true, int maxPrint = 5);
223
225 TString GetErrorMessage();
226
228 TString GetWarningMessage();
229
230 inline Int_t GetNumberOfErrors() const { return fNErrors; }
231
232 inline Int_t GetNumberOfWarnings() const { return fNWarnings; }
233
234 Int_t LoadConfigFromElement(TiXmlElement* eSectional, TiXmlElement* eGlobal,
235 std::map<std::string, std::string> envs = {});
236 Int_t LoadConfigFromFile(const std::string& configFilename, const std::string& sectionName = "");
237 Int_t LoadConfigFromBuffer();
238
239 TRestMetadata* InstantiateChildMetadata(int index, std::string pattern = "");
240 TRestMetadata* InstantiateChildMetadata(std::string pattern = "", std::string name = "");
241
244 virtual void Merge(const TRestMetadata&);
245
247 virtual void Initialize() {}
248
250 void Print() { PrintMetadata(); } // *MENU*
251
253 virtual void PrintMetadata();
254
256 virtual void UpdateMetadataMembers() {}
257
259 void PrintTimeStamp(Double_t timeStamp);
260
262 void PrintConfigBuffer(); // *MENU*
263
265 void WriteConfigBuffer(std::string fName);
266
268 void PrintMessageBuffer(); // *MENU*
269
270 // getters and setters
271 std::string GetSectionName();
272
273 std::string GetConfigBuffer();
274
275 std::string GetDataMemberValue(std::string memberName);
276
277 std::vector<std::string> GetDataMemberValues(std::string memberName, Int_t precision = 0);
278
279 TString GetVersion(); // *MENU*
280
281 TString GetCommit();
282
283 TString GetLibraryVersion();
284
285 inline Bool_t isOfficialRelease() const { return fOfficialRelease; }
286
287 inline Bool_t isCleanState() const { return fCleanState; }
288
289 UInt_t GetVersionMajor() const;
290 UInt_t GetVersionMinor() const;
291 UInt_t GetVersionPatch() const;
292
293 Int_t GetVersionCode();
295 inline TString GetDataPath() {
296 std::string dataPath = GetParameter("mainDataPath", "");
297 if (dataPath == "") {
298 dataPath = "./";
299 }
300 return dataPath;
301 }
302
305
307 TString GetVerboseLevelString();
308
310 inline TString GetMainDataPath() { return GetDataPath(); }
311
312 std::string GetParameter(std::string parName, TString defaultValue = PARAMETER_NOT_FOUND_STR); // *MENU*
313
314 Double_t GetDblParameterWithUnits(std::string parName, Double_t defaultValue = PARAMETER_NOT_FOUND_DBL);
315
316 TVector2 Get2DVectorParameterWithUnits(std::string parName, TVector2 defaultValue = TVector2(-1, -1));
317
318 TVector3 Get3DVectorParameterWithUnits(std::string parName, TVector3 defaultValue = TVector3(-1, -1, -1));
319
321 void DoNotStore() { fStore = false; }
323 void Store() { fStore = true; }
325 void SetSectionName(std::string sName) { fSectionName = sName; }
327 void SetConfigFile(std::string configFilename) { fConfigFileName = configFilename; }
330
334 virtual Int_t Write(const char* name = nullptr, Int_t option = 0, Int_t bufsize = 0);
335
337
338 // Making class constructors protected to keep this class abstract
339 TRestMetadata& operator=(const TRestMetadata&);
341
343 ClassDef(TRestMetadata, 9);
344};
345
346#endif
Managing applications and executing tasks.
Definition: TRestManager.h:16
A base class for any REST metadata class.
Definition: TRestMetadata.h:70
void UnSetVersion()
Resets the version of TRestRun to -1, in case the file is old REST file. Only TRestRun is allowed to ...
std::map< std::string, std::string > fVariables
Saving a list of rml variables. name-value std::pair.
virtual void PrintMetadata()
Implemented it in the derived metadata class to print out specific metadata information.
endl_t RESTendl
Termination flag object for TRestStringOutput.
std::string ElementToString(TiXmlElement *ele)
Convert an TiXmlElement object to string.
void DoNotStore()
If this method is called the metadata information will not be stored in disk.
TiXmlElement * GetElement(std::string eleDeclare, TiXmlElement *e=nullptr)
Get an xml element from a given parent element, according to its declaration.
virtual void Initialize()
Making default settings.
TString GetSearchPath()
Int_t LoadConfigFromFile(const std::string &configFilename, const std::string &sectionName="")
Give the file name, find out the corresponding section. Then call the main starter.
TString GetMainDataPath()
Gets a std::string with the path used for data storage.
TiXmlElement * fElementGlobal
Saving the global element, to be passed to the resident class, if necessary.
void PrintMessageBuffer()
Print the buffered message.
std::map< std::string, std::string > GetParametersList()
It retrieves a map of all parameter:value found in the metadata class.
void PrintConfigBuffer()
Print the config xml section stored in the class.
std::string GetUnits(TiXmlElement *e)
Returns a string with the unit name given in the given xml element.
TiXmlElement * GetElementWithName(std::string eleDeclare, std::string eleName, TiXmlElement *e)
Get an xml element from a given parent element, according to its declaration and its field "name".
TRestStringOutput::REST_Verbose_Level fVerboseLevel
Verbose level used to print debug info.
TiXmlElement * StringToElement(std::string definition)
Parsing a string into TiXmlElement object.
TiXmlElement * GetElementFromFile(std::string configFilename, std::string NameOrDecalre="")
Open an xml encoded file and find its element.
TRestMetadata * InstantiateChildMetadata(int index, std::string pattern="")
This method will retrieve a new TRestMetadata instance of a child element of the present TRestMetadat...
Int_t fNErrors
It counts the number of errors notified.
TString GetCommit()
Returns the REST commit value stored in fCommit.
virtual void InitFromConfigFile()
To make settings from rml file. This method must be implemented in the derived class.
TString fErrorMessage
A std::string to store an optional error message through method SetError.
void SetLibraryVersion(TString version)
Set the library version of this metadata class.
TString GetVerboseLevelString()
returns the verbose level in type of TString
void SetHostmgr(TRestManager *m)
Set the host manager for this class.
TRestStringOutput::REST_Verbose_Level GetVerboseLevel()
returns the verboselevel in type of REST_Verbose_Level enumerator
std::string ReplaceVariables(const std::string buffer)
Identifies environmental variable replacing marks in the input buffer, and replace them with correspo...
Int_t LoadConfigFromBuffer()
Initialize data from a string element buffer.
Int_t fNWarnings
It counts the number of warnings notified.
void ReplaceForLoopVars(TiXmlElement *e, std::map< std::string, std::string > forLoopVar)
Helper method for TRestMetadata::ExpandForLoops().
std::string GetFieldValue(std::string parName, TiXmlElement *e)
Returns the field value of an xml element which has the specified name.
std::string messageBuffer
The buffer to store the output message through TRestStringOutput in this class.
void SetWarning(std::string message="", bool print=true, int maxPrint=5)
A metadata class may use this method to signal that something went wrong.
void Print()
Implementing TObject::Print() method.
virtual void InitFromRootFile()
Method called after the object is retrieved from root file.
TString GetDataPath()
Returns a std::string with the path used for data storage.
Bool_t GetError() const
It returns true if an error was identified by a derived metadata class.
Int_t LoadConfigFromElement(TiXmlElement *eSectional, TiXmlElement *eGlobal, std::map< std::string, std::string > envs={})
Main starter method.
void ReSetVersion()
Resets the version of TRestRun to REST_RELEASE. Only TRestRun is allowed to update version.
TString fWarningMessage
It can be used as a way to identify that something went wrong using SetWarning method.
TiXmlElement * ReplaceElementAttributes(TiXmlElement *e)
replace the field value(attribute) of the given xml element
void SetConfigFile(std::string configFilename)
set config file path from external
std::string GetKEYStructure(std::string keyName)
Gets the first key structure for keyName found inside buffer after fromPosition.
TString GetErrorMessage()
Returns a std::string containing the error message.
void ReadAllParameters()
Reflection methods, Set value of a datamember in class according to TRestMetadata::fElement.
void SetSectionName(std::string sName)
set the section name, clear the section content
void ExpandIncludeFile(TiXmlElement *e)
Open the given rml file and find the corresponding section.
TString GetWarningMessage()
Returns a std::string containing the warning message.
TString GetVersion()
Returns the REST version stored in fVersion.
std::string SearchFile(std::string filename)
Search files in current directory and directories specified in "searchPath" section.
TString fVersion
REST version std::string, only used for archive and retrieve.
Definition: TRestMetadata.h:85
void ExpandForLoopOnce(TiXmlElement *e, const std::map< std::string, std::string > &forLoopVar)
Helper method for TRestMetadata::ExpandForLoops().
void ReadParametersList(std::map< std::string, std::string > &list)
It reads a parameter list and associates it to its corresponding metadata member. par0 --> fPar0.
void AddLog(std::string log="", bool print=true)
Add logs to messageBuffer.
std::string GetSectionName()
Returns the section name of this class, defined at the beginning of fSectionName.
Bool_t fError
It can be used as a way to identify that something went wrong using SetError method.
std::vector< std::string > GetDataMemberValues(std::string memberName, Int_t precision=0)
Get the value of datamember as a vector of strings.
std::string GetConfigBuffer()
Returns the config section of this class.
virtual void Merge(const TRestMetadata &)
Bool_t fStore
This variable is used to determine if the metadata structure should be stored in the ROOT file.
void ExpandForLoops(TiXmlElement *e, std::map< std::string, std::string > forLoopVar)
Expands the loop structures found in the given xml section.
TRestManager * fHostmgr
All metadata classes can be initialized and managed by TRestManager.
void WriteConfigBuffer(std::string fName)
Writes the config buffer to a file in append mode.
std::string GetDataMemberValue(std::string memberName)
Get the value of data member as string.
void ReadEnvInElement(TiXmlElement *e, bool overwrite=true)
Identify an environmental variable section and add it into env section list.
virtual Int_t LoadSectionMetadata()
This method does some preparation of xml section.
std::string fConfigFileName
Full name of the rml file.
std::string FieldNamesToUpper(std::string inputString)
This method updates all the field names inside the definition string provided by argument to make the...
TString GetLibraryVersion()
Returns the REST libraty version stored in fLibraryVersion.
void Store()
If this method is called the metadata information will be stored in disk.
std::string ReplaceConstants(const std::string buffer)
Identifies "constants" in the input buffer, and replace them with corresponding value.
std::map< std::string, std::string > fConstants
Saving a list of rml constants. name-value std::pair. Constants are temporary for this class only.
std::pair< std::string, std::string > GetParameterAndUnits(std::string parname, TiXmlElement *e=nullptr)
Returns the unit string of the given parameter of the given xml section.
Bool_t fWarning
It can be used as a way to identify that something went wrong using SetWarning method.
virtual void UpdateMetadataMembers()
Method to allow implementation of specific metadata members updates at inherited classes.
TRestMetadata()
TRestMetadata default constructor.
void PrintTimeStamp(Double_t timeStamp)
Print the current time on local machine.
virtual Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
overwriting the write() method with fStore considered
void ExpandIfSections(TiXmlElement *e)
Judge the if condition and expands the elements inside it.
void ReadElement(TiXmlElement *e, bool recursive=false)
Read the given xml section, applying rml syntax(if, for, include, etc.)
void SetError(std::string message="", bool print=true, int maxPrint=5)
A metadata class may use this method to signal that something went wrong.
Bool_t GetWarning() const
It returns true if an error was identified by a derived metadata class.
TiXmlElement * GetNextElement(TiXmlElement *e)
Get the next sibling xml element of this element, with same eleDeclare.
std::string GetParameter(std::string parName, TiXmlElement *e, TString defaultValue=PARAMETER_NOT_FOUND_STR)
Returns the value for the parameter named parName in the given section.
std::string GetKEYDefinition(std::string keyName)
Gets the first key definition for keyName found inside buffer starting at fromPosition.
std::string fSectionName
Section name given in the constructor of the derived metadata class.
void SetVerboseLevel(TRestStringOutput::REST_Verbose_Level v)
sets the verbose level
std::string configBuffer
The buffer where the corresponding metadata section is stored. Filled only during Write()
TiXmlElement * fElement
Saving the sectional element together with global element.
~TRestMetadata()
TRestMetadata default destructor.
REST_Verbose_Level
Enumerate of verbose level, containing five levels.
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.