Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-27 09:13:45

0001 /**
0002  *  @file   PandoraSDK/include/Helpers/XmlHelper.h
0003  * 
0004  *  @brief  Header file for the xml helper class.
0005  * 
0006  *  $Log: $
0007  */
0008 #ifndef PANDORA_XML_HELPER_H
0009 #define PANDORA_XML_HELPER_H 1
0010 
0011 #include "Objects/CartesianVector.h"
0012 #include "Objects/TrackState.h"
0013 
0014 #include "Pandora/PandoraInternal.h"
0015 #include "Pandora/StatusCodes.h"
0016 
0017 #include "Xml/tinyxml.h"
0018 
0019 namespace pandora
0020 {
0021 
0022 /**
0023  *  @brief  XmlHelper class
0024  */
0025 class XmlHelper
0026 {
0027 public:
0028     /**
0029      *  @brief  Read a value from an xml element
0030      * 
0031      *  @param  xmlHandle the relevant xml handle
0032      *  @param  xmlElementName the name of the xml element to examine
0033      *  @param  t to receive the value
0034      */
0035     template <typename T>
0036     static StatusCode ReadValue(const TiXmlHandle &xmlHandle, const std::string &xmlElementName, T &t);
0037 
0038     /**
0039      *  @brief  Read a vector of values from a (space separated) list in an xml element
0040      * 
0041      *  @param  xmlHandle the relevant xml handle
0042      *  @param  xmlElementName the name of the xml element to examine
0043      *  @param  vector to receive the vector of values
0044      */
0045     template <typename T>
0046     static StatusCode ReadVectorOfValues(const TiXmlHandle &xmlHandle, const std::string &xmlElementName, std::vector<T> &vector);
0047 
0048     /**
0049      *  @brief  Read a two-dimensional array of values into a vector of vectors. Each row of values must be contained
0050      *          within <rowname></rowname> xml tags, whilst the values in the row must be space separated
0051      * 
0052      *  @param  xmlHandle the relevant xml handle
0053      *  @param  xmlElementName the name of the xml element to examine
0054      *  @param  rowName the row name
0055      *  @param  vector to receive the 2d vector of values
0056      */
0057     template <typename T>
0058     static StatusCode Read2DVectorOfValues(const TiXmlHandle &xmlHandle, const std::string &xmlElementName, const std::string &rowName,
0059         std::vector< std::vector<T> > &vector);
0060 
0061     /**
0062      *  @brief  Process an algorithm described in an xml element with a matching "description = ..." attribute
0063      * 
0064      *  @param  algorithm the parent algorithm calling this function
0065      *  @param  xmlHandle the relevant xml handle
0066      *  @param  description the description attribute of the algorithm xml element
0067      *  @param  algorithmName to receive the name of the algorithm instance
0068      */
0069     static StatusCode ProcessAlgorithm(const Algorithm &algorithm, const TiXmlHandle &xmlHandle, const std::string &description,
0070         std::string &algorithmName);
0071 
0072     /**
0073      *  @brief  Process a single algorithm described in an xml file (the first found by the xml handle)
0074      * 
0075      *  @param  algorithm the parent algorithm calling this function
0076      *  @param  xmlHandle the relevant xml handle
0077      *  @param  algorithmName to receive the name of the algorithm instance
0078      */
0079     static StatusCode ProcessFirstAlgorithm(const Algorithm &algorithm, const TiXmlHandle &xmlHandle, std::string &algorithmName);
0080 
0081     /**
0082      *  @brief  Process a list of daughter algorithms in an xml file
0083      * 
0084      *  @param  algorithm the parent algorithm calling this function
0085      *  @param  xmlHandle the relevant xml handle
0086      *  @param  listName the name of the algorithm list
0087      *  @param  algorithmNames to receive the names of the algorithm instances
0088      */
0089     static StatusCode ProcessAlgorithmList(const Algorithm &algorithm, const TiXmlHandle &xmlHandle, const std::string &listName,
0090         StringVector &algorithmNames);
0091 
0092     /**
0093      *  @brief  Process an algorithm tool described in an xml element with a matching "description = ..." attribute
0094      * 
0095      *  @param  algorithm the parent algorithm calling this function
0096      *  @param  xmlHandle the relevant xml handle
0097      *  @param  description the description attribute of the algorithm tool xml element
0098      *  @param  pAlgorithmTool to receive the address of the algorithm tool instance
0099      */
0100     static StatusCode ProcessAlgorithmTool(const Algorithm &algorithm, const TiXmlHandle &xmlHandle, const std::string &description,
0101         AlgorithmTool *&pAlgorithmTool);
0102 
0103     /**
0104      *  @brief  Process a single algorithm tool described in an xml file (the first found by the xml handle)
0105      * 
0106      *  @param  algorithm the parent algorithm calling this function
0107      *  @param  xmlHandle the relevant xml handle
0108      *  @param  pAlgorithmTool to receive the address of the algorithm tool instance
0109      */
0110     static StatusCode ProcessFirstAlgorithmTool(const Algorithm &algorithm, const TiXmlHandle &xmlHandle, AlgorithmTool *&pAlgorithmTool);
0111 
0112     /**
0113      *  @brief  Process a list of algorithms tools in an xml file
0114      * 
0115      *  @param  algorithm the parent algorithm calling this function
0116      *  @param  xmlHandle the relevant xml handle
0117      *  @param  listName the name of the algorithm tool list
0118      *  @param  algorithmToolVector to receive the vector of addresses of the algorithm tool instances
0119      */
0120     static StatusCode ProcessAlgorithmToolList(const Algorithm &algorithm, const TiXmlHandle &xmlHandle, const std::string &listName,
0121         AlgorithmToolVector &algorithmToolVector);
0122 
0123     /**
0124      *  @brief  Tokenize a string
0125      * 
0126      *  @param  inputString the input string
0127      *  @param  tokens to receive the resulting tokens
0128      *  @param  delimiter the specified delimeter
0129      */
0130     static void TokenizeString(const std::string &inputString, StringVector &tokens, const std::string &delimiter = " ");
0131 };
0132 
0133 //------------------------------------------------------------------------------------------------------------------------------------------
0134 
0135 template <typename T>
0136 inline StatusCode XmlHelper::ReadValue(const TiXmlHandle &xmlHandle, const std::string &xmlElementName, T &t)
0137 {
0138     const TiXmlElement *const pXmlElement = xmlHandle.FirstChild(xmlElementName).Element();
0139 
0140     if (!pXmlElement)
0141         return STATUS_CODE_NOT_FOUND;
0142 
0143     if (!StringToType(pXmlElement->GetText(), t))
0144         return STATUS_CODE_FAILURE;
0145 
0146     return STATUS_CODE_SUCCESS;
0147 }
0148 
0149 template <>
0150 inline StatusCode XmlHelper::ReadValue<CartesianVector>(const TiXmlHandle &xmlHandle, const std::string &xmlElementName, CartesianVector &t)
0151 {
0152     const TiXmlElement *const pXmlElement = xmlHandle.FirstChild(xmlElementName).Element();
0153 
0154     if (!pXmlElement)
0155         return STATUS_CODE_NOT_FOUND;
0156 
0157     StringVector tokens;
0158     TokenizeString(pXmlElement->GetText(), tokens);
0159 
0160     if (tokens.size() != 3)
0161         return STATUS_CODE_FAILURE;
0162 
0163     float x(0.f), y(0.f), z(0.f);
0164 
0165     if (!StringToType(tokens[0], x) || !StringToType(tokens[1], y) || !StringToType(tokens[2], z))
0166         return STATUS_CODE_FAILURE;
0167 
0168     t = CartesianVector(x, y, z);
0169 
0170     return STATUS_CODE_SUCCESS;
0171 }
0172 
0173 template <>
0174 inline StatusCode XmlHelper::ReadValue<TrackState>(const TiXmlHandle &xmlHandle, const std::string &xmlElementName, TrackState &t)
0175 {
0176     const TiXmlElement *const pXmlElement = xmlHandle.FirstChild(xmlElementName).Element();
0177 
0178     if (!pXmlElement)
0179         return STATUS_CODE_NOT_FOUND;
0180 
0181     StringVector tokens;
0182     TokenizeString(pXmlElement->GetText(), tokens);
0183 
0184     if (tokens.size() != 6)
0185         return STATUS_CODE_FAILURE;
0186 
0187     float x(0.f), y(0.f), z(0.f), px(0.f), py(0.f), pz(0.f);
0188 
0189     if (!StringToType(tokens[0], x) || !StringToType(tokens[1], y) || !StringToType(tokens[2], z) ||
0190         !StringToType(tokens[3], px) || !StringToType(tokens[4], py) || !StringToType(tokens[5], pz))
0191     {
0192         return STATUS_CODE_FAILURE;
0193     }
0194 
0195     t = TrackState(x, y, z, px, py, pz);
0196 
0197     return STATUS_CODE_SUCCESS;
0198 }
0199 
0200 template <>
0201 inline StatusCode XmlHelper::ReadValue<bool>(const TiXmlHandle &xmlHandle, const std::string &xmlElementName, bool &t)
0202 {
0203     const TiXmlElement *const pXmlElement = xmlHandle.FirstChild(xmlElementName).Element();
0204 
0205     if (!pXmlElement)
0206         return STATUS_CODE_NOT_FOUND;
0207 
0208     const std::string xmlElementString = pXmlElement->GetText();
0209 
0210     if ((xmlElementString == "1") || (xmlElementString == "true"))
0211     {
0212         t = true;
0213     }
0214     else if ((xmlElementString == "0") || (xmlElementString == "false"))
0215     {
0216         t = false;
0217     }
0218     else
0219     {
0220         return STATUS_CODE_FAILURE;
0221     }
0222 
0223     return STATUS_CODE_SUCCESS;
0224 }
0225 
0226 //------------------------------------------------------------------------------------------------------------------------------------------
0227 
0228 template <typename T>
0229 inline StatusCode XmlHelper::ReadVectorOfValues(const TiXmlHandle &xmlHandle, const std::string &xmlElementName, std::vector<T> &vector)
0230 {
0231     const TiXmlElement *const pXmlElement = xmlHandle.FirstChild(xmlElementName).Element();
0232 
0233     if (!pXmlElement)
0234         return STATUS_CODE_NOT_FOUND;
0235 
0236     StringVector tokens;
0237     TokenizeString(pXmlElement->GetText(), tokens);
0238 
0239     for (const std::string &token : tokens)
0240     {
0241         T t;
0242 
0243         if (!StringToType(token, t))
0244             return STATUS_CODE_FAILURE;
0245 
0246         vector.push_back(t);
0247     }
0248 
0249     return STATUS_CODE_SUCCESS;
0250 }
0251 
0252 //------------------------------------------------------------------------------------------------------------------------------------------
0253 
0254 template <typename T>
0255 inline StatusCode XmlHelper::Read2DVectorOfValues(const TiXmlHandle &xmlHandle, const std::string &xmlElementName, const std::string &rowName,
0256     std::vector< std::vector<T> > &vector)
0257 {
0258     TiXmlElement *const pXmlElement = xmlHandle.FirstChild(xmlElementName).Element();
0259 
0260     if (!pXmlElement)
0261         return STATUS_CODE_NOT_FOUND;
0262 
0263     TiXmlElement *pXmlRowElement = TiXmlHandle(pXmlElement).FirstChild(rowName).Element();
0264 
0265     if (!pXmlRowElement)
0266         return STATUS_CODE_NOT_FOUND;
0267 
0268     for ( ; nullptr != pXmlRowElement; pXmlRowElement = pXmlRowElement->NextSiblingElement(rowName))
0269     {
0270         std::vector<T> rowVector;
0271 
0272         StringVector tokens;
0273         TokenizeString(pXmlRowElement->GetText(), tokens);
0274 
0275         for (const std::string &token : tokens)
0276         {
0277             T t;
0278 
0279             if (!StringToType(token, t))
0280                 return STATUS_CODE_FAILURE;
0281 
0282             rowVector.push_back(t);
0283         }
0284 
0285         vector.push_back(rowVector);
0286     }
0287 
0288     return STATUS_CODE_SUCCESS;
0289 }
0290 
0291 //------------------------------------------------------------------------------------------------------------------------------------------
0292 
0293 inline StatusCode XmlHelper::ProcessFirstAlgorithm(const Algorithm &algorithm, const TiXmlHandle &xmlHandle, std::string &algorithmName)
0294 {
0295     const std::string emptyDescription;
0296     return XmlHelper::ProcessAlgorithm(algorithm, xmlHandle, emptyDescription, algorithmName);
0297 }
0298 
0299 //------------------------------------------------------------------------------------------------------------------------------------------
0300 
0301 inline StatusCode XmlHelper::ProcessFirstAlgorithmTool(const Algorithm &algorithm, const TiXmlHandle &xmlHandle, AlgorithmTool *&pAlgorithmTool)
0302 {
0303     const std::string emptyDescription;
0304     return XmlHelper::ProcessAlgorithmTool(algorithm, xmlHandle, emptyDescription, pAlgorithmTool);
0305 }
0306 
0307 } // namespace pandora
0308 
0309 #endif // #ifndef PANDORA_XML_HELPER_H