13hid_t getNodeId(hid_t loc_id, std::string name);
14herr_t map_children(hid_t loc_id,
char const *name,
const H5L_info_t *info,
void *opdata);
36HDFNode::HDFNode( hid_t a_node_id, hid_t a_parent_id,
size_t a_index, std::vector<childInfo> a_siblings ) :
38 m_node_id( a_node_id ),
39 m_parent_id( a_parent_id ),
41 m_siblings( a_siblings ) {
43 H5I_type_t id_type = H5Iget_type( m_node_id );
44 if (H5I_GROUP == id_type) {
45 hsize_t start_idx = 0;
46 H5Literate(m_node_id, H5_INDEX_NAME, H5_ITER_NATIVE, &start_idx, map_children, &m_children);
48 else if (H5I_DATASET == id_type) {
52 throw "Unknown object type encountered in HDF file";
63HDFNode::HDFNode( hid_t a_file_id) :
65 m_node_id( a_file_id ),
69 hsize_t start_idx = 0;
70 H5Literate(m_node_id, H5_INDEX_NAME, H5_ITER_NATIVE, &start_idx, map_children, &m_children);
78HDFNode::HDFNode(
const HDFNode &other) :
79 Node_internal( other ),
80 m_node_id( other.m_node_id ),
81 m_parent_id( other.m_parent_id ),
82 m_index( other.m_index ),
83 m_siblings( other.m_siblings ),
84 m_children( other.m_children ) {
101std::string HDFNode::attribute(
char const *a_name) {
103 if ((m_node_id == 0) || (!H5Aexists(m_node_id, a_name)))
106 hid_t attr_id = H5Aopen(m_node_id, a_name, H5P_DEFAULT);
107 size_t attr_len = H5Aget_storage_size(attr_id);
108 hid_t atype = H5Aget_type(attr_id);
110 std::vector<char> buffer(attr_len+1, 0);
111 H5Aread(attr_id, atype, buffer.data());
113 std::string attrValue(buffer.data());
119int HDFNode::attribute_as_int(
const char* a_name){
120 hid_t attr_id = H5Aopen(m_node_id, a_name, H5P_DEFAULT);
121 size_t attr_len = H5Aget_storage_size(attr_id);
122 hid_t atype = H5Aget_type(attr_id);
124 std::vector<char> buffer(attr_len+1, 0);
125 H5Aread(attr_id, atype, buffer.data());
127 return atoi(buffer.data());
130long HDFNode::attribute_as_long(
const char* a_name){
131 hid_t attr_id = H5Aopen(m_node_id, a_name, H5P_DEFAULT);
132 size_t attr_len = H5Aget_storage_size(attr_id);
134 std::vector<char> buffer(attr_len+1, 0);
135 H5Aread(attr_id, H5T_NATIVE_CHAR, buffer.data());
137 return atol(buffer.data());
140double HDFNode::attribute_as_double(
const char* a_name){
141 hid_t attr_id = H5Aopen(m_node_id, a_name, H5P_DEFAULT);
142 size_t attr_len = H5Aget_storage_size(attr_id);
143 hid_t atype = H5Aget_type(attr_id);
145 std::vector<char> buffer(attr_len+1, 0);
146 H5Aread(attr_id, atype, buffer.data());
148 return atof(buffer.data());
158Node_internal *HDFNode::child(
char const *a_name) {
160 for (
size_t idx=0; idx<m_children.size(); idx++)
162 if ( m_children[idx].xmlName == a_name ) {
163 hid_t child_id = getNodeId(m_node_id, m_children[idx].
name.c_str());
164 HDFNode *child =
new HDFNode(child_id, m_node_id, idx, m_children);
168 return new HDFNode();
176Node_internal *HDFNode::first_child() {
178 if (m_children.size() == 0)
179 return new HDFNode();
180 hid_t child_id = getNodeId(m_node_id, m_children[0].name);
181 HDFNode *child =
new HDFNode(child_id, m_node_id, 0, m_children);
191Node_internal *HDFNode::next_sibling() {
193 size_t nextIndex = m_index + 1;
194 if (nextIndex >= this->m_siblings.size())
195 return new HDFNode();
196 hid_t sibling_id = this->m_siblings[nextIndex].node_id;
197 HDFNode *sibling =
new HDFNode(sibling_id, m_parent_id, nextIndex, m_siblings);
208void HDFNode::to_next_sibling() {
210 size_t nextIndex = m_index + 1;
211 size_t nsibs = this->m_siblings.size();
212 if (nextIndex >= nsibs)
218 m_node_id = this->m_siblings[nextIndex].node_id;
229Node_internal *HDFNode::copy() {
232 return new HDFNode();
234 HDFNode *
copy =
new HDFNode( m_node_id, m_parent_id, m_index, m_siblings );
235 copy->m_children = m_children;
244Node_internal &HDFNode::operator=(
const HDFNode &other) {
246 this->m_node_id = other.m_node_id;
247 this->m_parent_id = other.m_parent_id;
248 this->m_index = other.m_index;
249 this->m_siblings = other.m_siblings;
250 this->m_children = other.m_children;
261std::string HDFNode::name()
const {
266 hid_t attr_id = H5Aopen(m_node_id,
"_xmltag", H5P_DEFAULT);
267 size_t attr_len = H5Aget_storage_size(attr_id);
268 hid_t atype = H5Aget_type(attr_id);
270 std::vector<char> buffer(attr_len+1, 0);
271 H5Aread(attr_id, atype, buffer.data());
273 return std::string(buffer.data());
283bool HDFNode::empty()
const {
285 return (m_node_id == 0);
295Text HDFNode::text()
const {
297#if H5_VERSION_GE(1,12,0)
299 herr_t status = H5Oget_info3(m_node_id, &infobuf, H5O_INFO_NUM_ATTRS);
302 herr_t status = H5Oget_info(m_node_id, &infobuf);
304 if (status != 0)
throw "unable to extract text from HDF";
306 switch (infobuf.type) {
310 case H5O_TYPE_DATASET: {
311 size_t len = H5Dget_storage_size(m_node_id);
312 hid_t dtype = H5Dget_type(m_node_id);
313 std::vector<char> buffer(len+1,0);
314 H5Dread(m_node_id, dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, buffer.data());
316 return Text(std::string(buffer.data())); }
319 throw "encountered unexpected type in HDF::text()!";
329Data_internal *HDFNode::data()
const {
331 return new HDFData( m_node_id );
343hid_t getNodeId(hid_t loc_id, std::string name)
345#if H5_VERSION_GE(1,12,0)
347 herr_t status = H5Oget_info_by_name3( loc_id,
name.c_str( ), &infobuf, H5O_INFO_NUM_ATTRS, H5P_DEFAULT );
350 herr_t status = H5Oget_info_by_name(loc_id,
name.c_str(), &infobuf, H5P_DEFAULT);
353 throw "requested child node not found in getNodeId";
358 switch (infobuf.type) {
360 node_id = H5Gopen(loc_id,
name.c_str(), H5P_DEFAULT);
362 case H5O_TYPE_DATASET:
363 node_id = H5Dopen(loc_id,
name.c_str(), H5P_DEFAULT);
366 throw "encountered unexpected type in getNodeId!";
377herr_t map_children(hid_t loc_id,
char const *name,
LUPI_maybeUnused const H5L_info_t *info,
void *opdata)
379 std::vector<HAPI::childInfo> *children =
380 static_cast< std::vector<HAPI::childInfo>*
>(opdata);
382 hid_t node_id = H5Oopen(loc_id, name, H5P_DEFAULT);
386 hid_t attr_id = H5Aopen(node_id,
"_xmltag", H5P_DEFAULT);
387 hid_t atype = H5Aget_type(attr_id);
388 size_t attr_len = H5Aget_storage_size(attr_id);
390 std::vector<char> buffer(attr_len+1, 0);
391 H5Aread(attr_id, atype, buffer.data());
393 xmlTag = std::string(buffer.data());
399 hid_t attr_id = H5Aopen(node_id,
"_xmlindex", H5P_DEFAULT);
401 H5Aread(attr_id, H5T_NATIVE_UINT16_g, &index);
404 HAPI::childInfo infos = {
411 if (index >= children->size())
413 children->resize(index+1);
415 (*children)[index] = infos;
const char * name(G4int ptype)
void copy(G4double dst[], const G4double src[], std::size_t size=G4FieldTrack::ncompSVEC)