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 #ifndef WIN32
47 #include <unistd.h>
48 #endif
49 
50 /* We keep using REST_RELEASE, REST_VERSION(2,X,Y) and REST_VERSION_CODE
51  to determine the installed REST version and avoid too much prototyping
52 
53 REST version is connected independently to each metadata structure. TRestRun
54  should be our reference to determine the version of REST used to write the
55 ROOT file.
56 
57 By including #include "TRestVersion.h" we get access to the definition of
58  REST_RELEASE, REST_VERSION_CODE, REST_VERSION(2,X,Y) which is the REST
59 version we are using.
60 
61 // storing the STATIC version of this installation
62 namespace REST_VersionGlob {
63  TString GetRESTVersion();
64  int GetRESTVersionCode();
65 };
66 inline TString GetRESTVersion() const { return REST_VersionGlob::GetRESTVersion(); }
67 inline int GetRESTVersionCode() const { return REST_VersionGlob::GetRESTVersionCode(); }
68 
69 */
70 
71 class TRestManager;
72 
74 class TRestMetadata : public TNamed {
75  private:
76  void ReadEnvInElement(TiXmlElement* e, bool overwrite = true);
77  void ReadElement(TiXmlElement* e, bool recursive = false);
78  void ReplaceForLoopVars(TiXmlElement* e, std::map<std::string, std::string> forLoopVar);
79  void ExpandForLoopOnce(TiXmlElement* e, const std::map<std::string, std::string>& forLoopVar);
80  void ExpandForLoops(TiXmlElement* e, std::map<std::string, std::string> forLoopVar);
81  void ExpandIfSections(TiXmlElement* e);
82  void ExpandIncludeFile(TiXmlElement* e);
83  std::string GetUnits(TiXmlElement* e);
84  std::string FieldNamesToUpper(std::string inputString);
85  void ReadOneParameter(std::string name, std::string value);
86  TiXmlElement* ReplaceElementAttributes(TiXmlElement* e);
87 
89  TString fVersion = REST_RELEASE; //<
90  TString fCommit = REST_COMMIT; //<
91  TString fLibraryVersion = "0"; //<
92 
93  Bool_t fOfficialRelease = false; //<
94  Bool_t fCleanState = false; //<
95 
96  protected:
97  // new xml utilities
98  std::string GetFieldValue(std::string parName, TiXmlElement* e);
99  std::string GetParameter(std::string parName, TiXmlElement* e,
100  TString defaultValue = PARAMETER_NOT_FOUND_STR);
101  Double_t GetDblParameterWithUnits(std::string parName, TiXmlElement* e,
102  Double_t defaultVal = PARAMETER_NOT_FOUND_DBL);
103  TVector2 Get2DVectorParameterWithUnits(std::string parName, TiXmlElement* e,
104  TVector2 defaultValue = TVector2(-1, -1));
105  TVector3 Get3DVectorParameterWithUnits(std::string parName, TiXmlElement* e,
106  TVector3 defaultValue = TVector3(-1, -1, -1));
107  TiXmlElement* GetElementFromFile(std::string configFilename, std::string NameOrDecalre = "");
108  TiXmlElement* GetElement(std::string eleDeclare, TiXmlElement* e = nullptr);
109  TiXmlElement* GetNextElement(TiXmlElement* e);
110  TiXmlElement* GetElementWithName(std::string eleDeclare, std::string eleName, TiXmlElement* e);
111  TiXmlElement* GetElementWithName(std::string eleDeclare, std::string eleName);
112  std::pair<std::string, std::string> GetParameterAndUnits(std::string parname, TiXmlElement* e = nullptr);
113  TiXmlElement* StringToElement(std::string definition);
114  std::string ElementToString(TiXmlElement* ele);
115 
116  // old xml parser interface.
117  std::string GetKEYStructure(std::string keyName);
118  std::string GetKEYStructure(std::string keyName, size_t& Position);
119  std::string GetKEYStructure(std::string keyName, std::string buffer);
120  std::string GetKEYStructure(std::string keyName, size_t& Position, std::string buffer);
121  std::string GetKEYStructure(std::string keyName, size_t& Position, TiXmlElement* ele);
122  std::string GetKEYDefinition(std::string keyName);
123  std::string GetKEYDefinition(std::string keyName, size_t& Position);
124  std::string GetKEYDefinition(std::string keyName, std::string buffer);
125  std::string GetKEYDefinition(std::string keyName, size_t& Position, std::string buffer);
126  std::string GetParameter(std::string parName, size_t& pos, std::string inputString);
127  std::string GetFieldValue(std::string fieldName, std::string definition, size_t fromPosition = 0);
128 
129  // some utils
130  std::string ReplaceVariables(const std::string buffer);
131  std::string ReplaceConstants(const std::string buffer);
132  std::string SearchFile(std::string filename);
133  TString GetSearchPath();
134  void ReSetVersion();
135  void UnSetVersion();
136  void SetLibraryVersion(TString version);
137 
138  // Load global setting for the rml section, e.g., name, title.
139  virtual Int_t LoadSectionMetadata();
141  virtual void InitFromConfigFile() {
142  std::map<std::string, std::string> parameters = GetParametersList();
143 
144  for (auto& p : parameters) p.second = ReplaceMathematicalExpressions(p.second);
145 
146  ReadParametersList(parameters);
147  }
148 
150  virtual void InitFromRootFile() {}
151 
152  void ReadParametersList(std::map<std::string, std::string>& list);
153 
160 
162  std::string fConfigFileName;
164  std::string fSectionName;
166  std::string configBuffer;
168  std::string messageBuffer;
169 
173  endl_t RESTendl;
174 
178  Bool_t fStore;
180  TiXmlElement* fElement;
182  TiXmlElement* fElementGlobal;
184  std::map<std::string, std::string> fVariables;
186  std::map<std::string, std::string> fConstants;
187 
189  Bool_t fError = false;
190 
192  Int_t fNErrors = 0;
193 
195  Bool_t fWarning = false;
196 
198  Int_t fNWarnings = 0;
199 
201  TString fErrorMessage = "";
202 
204  TString fWarningMessage = "";
205 
206  std::map<std::string, std::string> GetParametersList();
207  void ReadAllParameters();
208 
209  TRestMetadata();
210  TRestMetadata(const char* configFilename);
211 
212  public:
214  inline Bool_t GetError() const { return fError; }
215 
217  inline Bool_t GetWarning() const { return fWarning; }
218 
220  void AddLog(std::string log = "", bool print = true);
221 
223  void SetError(std::string message = "", bool print = true, int maxPrint = 5);
224 
226  void SetWarning(std::string message = "", bool print = true, int maxPrint = 5);
227 
229  TString GetErrorMessage();
230 
232  TString GetWarningMessage();
233 
234  inline Int_t GetNumberOfErrors() const { return fNErrors; }
235 
236  inline Int_t GetNumberOfWarnings() const { return fNWarnings; }
237 
238  Int_t LoadConfigFromElement(TiXmlElement* eSectional, TiXmlElement* eGlobal,
239  std::map<std::string, std::string> envs = {});
240  Int_t LoadConfigFromFile(const std::string& configFilename, const std::string& sectionName = "");
241  Int_t LoadConfigFromBuffer();
242 
243  TRestMetadata* InstantiateChildMetadata(int index, std::string pattern = "");
244  TRestMetadata* InstantiateChildMetadata(std::string pattern = "", std::string name = "");
245 
248  virtual void Merge(const TRestMetadata&);
249 
251  virtual void Initialize() {}
252 
254  void Print() { PrintMetadata(); } // *MENU*
255 
257  virtual void PrintMetadata();
258 
260  virtual void UpdateMetadataMembers() {}
261 
263  void PrintTimeStamp(Double_t timeStamp);
264 
266  void PrintConfigBuffer(); // *MENU*
267 
269  void WriteConfigBuffer(std::string fName);
270 
272  void PrintMessageBuffer(); // *MENU*
273 
274  // getters and setters
275  std::string GetSectionName();
276 
277  std::string GetConfigBuffer();
278 
279  std::string GetDataMemberValue(std::string memberName);
280 
281  std::vector<std::string> GetDataMemberValues(std::string memberName, Int_t precision = 0);
282 
283  TString GetVersion(); // *MENU*
284 
285  TString GetCommit();
286 
287  TString GetLibraryVersion();
288 
289  inline Bool_t isOfficialRelease() const { return fOfficialRelease; }
290 
291  inline Bool_t isCleanState() const { return fCleanState; }
292 
293  UInt_t GetVersionMajor() const;
294  UInt_t GetVersionMinor() const;
295  UInt_t GetVersionPatch() const;
296 
297  Int_t GetVersionCode();
299  inline TString GetDataPath() {
300  std::string dataPath = GetParameter("mainDataPath", "");
301  if (dataPath == "") {
302  dataPath = "./";
303  }
304  return dataPath;
305  }
306 
309 
311  TString GetVerboseLevelString();
312 
314  inline TString GetMainDataPath() { return GetDataPath(); }
315 
316  std::string GetParameter(std::string parName, TString defaultValue = PARAMETER_NOT_FOUND_STR); // *MENU*
317 
318  Double_t GetDblParameterWithUnits(std::string parName, Double_t defaultValue = PARAMETER_NOT_FOUND_DBL);
319 
320  TVector2 Get2DVectorParameterWithUnits(std::string parName, TVector2 defaultValue = TVector2(-1, -1));
321 
322  TVector3 Get3DVectorParameterWithUnits(std::string parName, TVector3 defaultValue = TVector3(-1, -1, -1));
323 
325  void DoNotStore() { fStore = false; }
327  void Store() { fStore = true; }
329  void SetSectionName(std::string sName) { fSectionName = sName; }
331  void SetConfigFile(std::string configFilename) { fConfigFileName = configFilename; }
333  void SetHostmgr(TRestManager* m) { fHostmgr = m; }
334 
338  virtual Int_t Write(const char* name = nullptr, Int_t option = 0, Int_t bufsize = 0);
339 
340  ~TRestMetadata();
341 
342  // Making class constructors protected to keep this class abstract
343  TRestMetadata& operator=(const TRestMetadata&);
345 
347  ClassDef(TRestMetadata, 9);
348 };
349 
350 #endif
Managing applications and executing tasks.
Definition: TRestManager.h:16
A base class for any REST metadata class.
Definition: TRestMetadata.h:74
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:89
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.