Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/Geant4/HAPI.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002 # <<BEGIN-copyright>>
0003 # Copyright 2019, Lawrence Livermore National Security, LLC.
0004 # This file is part of the gidiplus package (https://github.com/LLNL/gidiplus).
0005 # gidiplus is licensed under the MIT license (see https://opensource.org/licenses/MIT).
0006 # SPDX-License-Identifier: MIT
0007 # <<END-copyright>>
0008 */
0009 
0010 #ifndef HAPI_hpp_included
0011 #define HAPI_hpp_included 1
0012 
0013 #include <string>
0014 #include <stdlib.h>
0015 #include <vector>
0016 #include <stdexcept>
0017 
0018 #include <nf_buffer.h>
0019 #include <nf_utilities.h>
0020 
0021 #define HAPI_USE_PUGIXML 1
0022 
0023 #ifdef HAPI_USE_PUGIXML
0024 #include <pugixml.hpp>
0025 #endif
0026 
0027 
0028 #ifdef HAPI_USE_HDF5
0029 #include <hdf5.h>
0030 #endif
0031 
0032 #include <LUPI.hpp>
0033 
0034 namespace HAPI {
0035 
0036 enum class NodeInteralType { pugiXML, HDF5 };
0037 
0038 // container classes for reading in from various data sources:
0039 
0040 /*
0041 ============================================================
0042 ========================= Attribute ========================
0043 ============================================================
0044  */
0045 
0046 class Node_internal;
0047 class Attribute {
0048 
0049     private:
0050         Node_internal *m_node;
0051         std::string m_name;
0052         //std::string m_value;
0053 
0054     public:
0055         inline Attribute() : m_node(nullptr), m_name() {}
0056 
0057         inline Attribute(Node_internal *a_node, std::string const &a_name) :
0058           m_node(a_node),
0059           m_name(a_name)
0060         {
0061         }
0062         ~Attribute() = default;
0063         //std::string const &name() const { return( m_name ); }
0064         inline std::string const value() const;
0065         inline int as_int() const;
0066         inline long as_long() const;
0067         inline double as_double() const;
0068 };
0069 
0070 /*
0071 ============================================================
0072 =========================== Text ===========================
0073 ============================================================
0074  */
0075 class Text {
0076 
0077     private:
0078         std::string m_text;
0079 
0080     public:
0081         Text();
0082         Text(std::string const &a_text);
0083         ~Text();
0084         std::string const &get() const { return( m_text ); }
0085 };
0086 
0087 /*
0088 ============================================================
0089 ================== Data_internal (base class) ==============
0090 ============================================================
0091  */
0092 class Data_internal {
0093 
0094     public:
0095         Data_internal() { };
0096         virtual ~Data_internal() = 0;
0097         //std::string const getDataType();
0098         //virtual template <typename T> T read() = 0;
0099         virtual void getDoubles(nf_Buffer<double> &buffer) = 0;
0100         virtual void getInts(nf_Buffer<int> &buffer) = 0;
0101         virtual size_t length() const = 0;
0102 };
0103 
0104 /*
0105 ============================================================
0106 =================== Node_internal (base class) =============
0107 ============================================================
0108  */
0109 class Node_internal {
0110 
0111     private:
0112         NodeInteralType m_type;
0113 
0114     public:
0115         Node_internal( NodeInteralType a_type );
0116         Node_internal( Node_internal const &a_node );
0117         virtual ~Node_internal() = 0;
0118 
0119         NodeInteralType type( ) const { return( m_type ); }
0120 
0121         virtual std::string attribute(const char* name) = 0;
0122         virtual int attribute_as_int(const char* name) = 0;
0123         virtual long attribute_as_long(const char* name) = 0;
0124         virtual double attribute_as_double(const char* name) = 0;
0125         virtual Node_internal *child(const char* name) = 0;
0126         virtual Node_internal *first_child() = 0;
0127         virtual Node_internal *next_sibling() = 0;
0128         virtual void to_next_sibling() = 0;
0129         virtual Node_internal *copy() = 0;
0130         virtual std::string name() const = 0;
0131         virtual bool empty() const = 0;
0132         virtual Text text() const = 0;
0133         virtual Data_internal *data() const = 0;
0134 };
0135 
0136 
0137 inline std::string const Attribute::value() const { return( m_node->attribute( m_name.c_str()) ); }
0138 inline int Attribute::as_int() const { return( m_node->attribute_as_int(m_name.c_str()) ); }
0139 inline long Attribute::as_long() const { return( m_node->attribute_as_long(m_name.c_str()) ); }
0140 inline double Attribute::as_double() const { return( m_node->attribute_as_double(m_name.c_str()) ); }
0141 
0142 /*
0143 ============================================================
0144 =========================== Data ===========================
0145 ============================================================
0146  */
0147 class Data {
0148 
0149     private:
0150         Data_internal *m_data;
0151 
0152     public:
0153         Data();
0154         Data( Data_internal *a_data );
0155         ~Data();
0156         void getDoubles(nf_Buffer<double> &buffer);
0157         void getInts(nf_Buffer<int> &buffer);
0158         size_t length() const;
0159 };
0160 
0161 /*
0162 ============================================================
0163 ============================ Node ==========================
0164 ============================================================
0165  */
0166 class Node {
0167 
0168     private:
0169         Node_internal *m_node;
0170 
0171     public:
0172         Node();
0173         Node( Node_internal *a_node );
0174         Node( Node const &a_node );
0175         ~Node();
0176         inline Attribute attribute(const char* a_name) const{
0177             return Attribute(m_node, a_name);
0178         }
0179         inline std::string attribute_as_string(const char* a_name) const{
0180             if(m_node == nullptr){
0181               return "";
0182             }
0183             return m_node->attribute(a_name);
0184         }
0185         inline int attribute_as_int(const char* a_name) const{
0186           if(m_node == nullptr){
0187             return 0;
0188           }
0189           return m_node->attribute_as_int(a_name);
0190         }
0191         inline long attribute_as_long(const char* a_name) const{
0192           if(m_node == nullptr){
0193             return 0;
0194           }
0195           return m_node->attribute_as_long(a_name);
0196         }
0197         inline double attribute_as_double(const char* a_name) const{
0198           if(m_node == nullptr){
0199             return 0.0;
0200           }
0201           return m_node->attribute_as_double(a_name);
0202         }
0203         Node child(const char* name) const;
0204         Node first_child() const;
0205         Node next_sibling() const;
0206         void to_next_sibling() const;
0207         Node &operator=(const Node &other);
0208         std::string name() const;
0209         bool empty() const;
0210         Text text() const;
0211         Data data() const;
0212 };
0213 
0214 
0215 /*
0216 ============================================================
0217 ======================= File (base class) ==================
0218 ============================================================
0219  */
0220 class File {
0221 
0222     public:
0223         File() { };
0224         virtual ~File() = 0;
0225         virtual Node child(const char* name) = 0;
0226         virtual Node first_child() = 0;
0227         virtual std::string name() const = 0;
0228 };
0229 
0230 
0231 /*
0232 ============================================================
0233 =============== Data Manager (for hybrid files) ============
0234 ============================================================
0235  */
0236 class DataManager {
0237 
0238     public:
0239         DataManager() {};
0240         virtual ~DataManager() {};
0241         static DataManager* m_instance;
0242     
0243     public:
0244         virtual void getDoubles(nf_Buffer<double> &result, size_t startIndex, size_t endIndex) = 0;
0245         virtual void getInts(nf_Buffer<int> &result, size_t startIndex, size_t endIndex) = 0;
0246 };
0247 
0248 
0249 #ifdef HAPI_USE_PUGIXML
0250 /*
0251 ============================================================
0252 ===================== XML using Pugi =======================
0253 ============================================================
0254  */
0255 class PugiXMLNode : public Node_internal {
0256 
0257     private:
0258         pugi::xml_node m_node;
0259 
0260     public:
0261         PugiXMLNode();
0262         PugiXMLNode(pugi::xml_node a_node);
0263         PugiXMLNode(const PugiXMLNode &other);
0264         virtual ~PugiXMLNode();
0265 
0266         std::string attribute(const char* name);
0267         int attribute_as_int(const char* name);
0268         long attribute_as_long(const char* name);
0269         double attribute_as_double(const char* name);
0270         Node_internal *child(char const *name);
0271         Node_internal *first_child();
0272         Node_internal *next_sibling();
0273         void to_next_sibling();
0274         Node_internal *copy();
0275         Node_internal &operator=(const PugiXMLNode &other);
0276         std::string name() const;
0277         bool empty() const;
0278         Text text() const;
0279         Data_internal *data() const;
0280 };
0281 
0282 class PugiXMLData : public Data_internal {
0283 
0284     private:
0285         pugi::xml_node m_node;
0286         size_t m_length;
0287         bool m_dataRead;
0288 
0289     public:
0290         PugiXMLData();
0291         PugiXMLData(pugi::xml_node a_node);
0292         virtual ~PugiXMLData();
0293         void getDoubles(nf_Buffer<double> &buffer);
0294         void getInts(nf_Buffer<int> &buffer);
0295         size_t length() const;
0296 };
0297 
0298 class PugiXMLFile : public File {
0299 
0300     private:
0301         std::string m_name;
0302         pugi::xml_document m_doc;
0303 
0304     public:
0305         PugiXMLFile();
0306         PugiXMLFile(char const *filename, std::string const &a_callingFunctionName);
0307         virtual ~PugiXMLFile();
0308         Node child(char const *name);
0309         Node first_child();
0310         std::string name() const;
0311 };
0312 #endif
0313 
0314 
0315 #ifdef HAPI_USE_HDF5
0316 /*
0317 ============================================================
0318 =========================== HDF ============================
0319 ============================================================
0320  */
0321 typedef struct {
0322     std::string name;
0323     std::string xmlName;  // HDF sometimes mangles names, need original name here
0324     size_t index;
0325     hid_t node_id;
0326 } childInfo;
0327 
0328 class HDFNode : public Node_internal {
0329 
0330     private:
0331         hid_t m_node_id;
0332         hid_t m_parent_id;
0333         size_t m_index;
0334         std::vector<childInfo> m_siblings;
0335         std::vector<childInfo> m_children;
0336 
0337     public:
0338         HDFNode();
0339         HDFNode(hid_t a_node_id, hid_t a_parent_id, size_t a_index, std::vector<childInfo> a_siblings);
0340         explicit HDFNode(hid_t a_file_id);
0341         HDFNode(const HDFNode &other);
0342         virtual ~HDFNode();
0343 
0344         //Attribute attribute(char const *name);
0345         std::string attribute(const char* name);
0346         int attribute_as_int(const char* name);
0347         long attribute_as_long(const char* name);
0348         double attribute_as_double(const char* name);
0349         Node_internal *child(char const *name);
0350         Node_internal *first_child();
0351         Node_internal *next_sibling();
0352         void to_next_sibling();
0353         Node_internal *copy();
0354         Node_internal &operator=(const HDFNode &other);
0355         std::string name() const;
0356         bool empty() const;
0357         Text text() const;
0358         Data_internal *data() const;
0359 
0360 };
0361 
0362 class HDFData : public Data_internal {
0363 
0364     private:
0365         hid_t m_node_id;
0366         hid_t m_dataspace_id;
0367         size_t m_length;
0368 
0369     public:
0370         HDFData();
0371         explicit HDFData(hid_t node_id);
0372         virtual ~HDFData();
0373         void getDoubles(nf_Buffer<double> &buffer);
0374         void getInts(nf_Buffer<int> &buffer);
0375         size_t length() const;
0376 };
0377 
0378 class HDFFile : public File {
0379 
0380     private:
0381         std::string m_name;
0382         hid_t m_doc;
0383         HDFNode *m_doc_as_node;
0384 
0385     public:
0386         HDFFile();
0387         explicit HDFFile(char const *filename);
0388         virtual ~HDFFile();
0389         Node child(char const *name);
0390         Node first_child();
0391         std::string name() const;
0392 };
0393 
0394 class HDFDataManager : public DataManager{
0395 
0396     private:
0397         std::string m_filename;
0398         bool m_iDataPresent;
0399         bool m_dDataPresent;
0400         hid_t m_file_id;
0401         hid_t m_dataset_ints, m_dataset_doubles;
0402         hid_t m_dataspace_ints, m_dataspace_doubles;
0403 
0404         hsize_t m_stride[1], m_block[1];
0405 
0406         size_t m_num_double_reads;
0407         size_t m_num_double_elem;
0408         size_t m_num_int_reads;
0409         size_t m_num_int_elem;
0410 
0411     public:
0412         HDFDataManager(std::string const &filename);
0413         virtual ~HDFDataManager();
0414         virtual void getDoubles(nf_Buffer<double> &result, size_t startIndex, size_t endIndex);
0415         virtual void getInts(nf_Buffer<int> &result, size_t startIndex, size_t endIndex);
0416 };
0417 #endif
0418 
0419 }               // end of namespace 'HAPI'
0420 
0421 #endif      // End of HAPI_hpp_included