HDF5Utils.cc
Go to the documentation of this file.
1 #include "HDF5Utils.h"
2 #include <cstring>
3 #include <iostream>
4 #include "detdataformats/wib/WIBFrame.hpp"
6 #include <algorithm>
7 #include "TMath.h"
8 
9 namespace dune {
10 namespace HDF5Utils {
11 
13  HDFFileInfoPtr hdfFileInfoPtr(new HDFFileInfo());
14  hdfFileInfoPtr->filePtr = H5Fopen(fileName.data(), H5F_ACC_RDONLY, H5P_DEFAULT);
15  hdfFileInfoPtr->bytesWritten = 0;
16  hdfFileInfoPtr->fileName = fileName;
17  hdfFileInfoPtr->runNumber = 0; // don't know it yet. Fill it when we get fragments.
18  hdfFileInfoPtr->dataFormatVersion = 0;
19 
20  hid_t grp = H5Gopen(hdfFileInfoPtr->filePtr,"/", H5P_DEFAULT);
21  if (attrExists(grp, "data_format_version"))
22  {
23  hid_t ga = H5Aopen_name(grp, "data_format_version");
24  H5Aread(ga, H5Aget_type(ga), &hdfFileInfoPtr->dataFormatVersion);
25  H5Aclose(ga);
26  }
27  H5Gclose(grp);
28  return hdfFileInfoPtr;
29 }
30 
31 void closeFile(HDFFileInfoPtr hdfFileInfoPtr) {
32  H5Fclose(hdfFileInfoPtr->filePtr);
33  hdfFileInfoPtr->filePtr = 0;
34 }
35 
36 std::deque<std::string> getTopLevelGroupNames(HDFFileInfoPtr& hdfFileInfoPtr) {
37  hid_t grp = H5Gopen(hdfFileInfoPtr->filePtr, "/", H5P_DEFAULT);
38  std::deque<std::string> theList = getMidLevelGroupNames(grp);
39  H5Gclose(grp);
40  return theList;
41 }
42 
43 std::deque<std::string> getMidLevelGroupNames(hid_t grp) {
44  std::deque<std::string> theList;
45  hsize_t nobj = 0;
46  H5Gget_num_objs(grp, &nobj);
47  for (hsize_t idx = 0; idx < nobj; ++idx) {
48  hsize_t len = H5Gget_objname_by_idx(grp, idx, NULL, 0 );
49  char *memb_name = new char(len+1);
50  H5Gget_objname_by_idx(grp, idx, memb_name, len+1 );
51  theList.emplace_back(memb_name);
52  delete memb_name;
53  }
54  return theList;
55 }
56 
57 
58  uint64_t formatTrigTimeStamp (uint64_t trigTimeStamp)
59  {
60  int clock_period = 20; //20 ns with 50MHz DAQClock //FIXME don't hardcode
61  uint64_t trigtime = trigTimeStamp * clock_period; //time in ns
62  uint64_t million = 1e9;
63  uint64_t trigtime_sec = trigtime/million;
64  //std::cout << "trigtime_sec: " << trigtime_sec << std::endl;
65  uint64_t trigtime_nanosec = trigtime % million;
66  uint64_t toReturn = (trigtime_sec << 32) | trigtime_nanosec;
67  return toReturn;
68  }
69 
70 
71  bool attrExists(hid_t object, const std::string &attrname) {
72  // Save old error handler
73  H5E_auto_t old_func;
74  void *old_client_data;
75  H5Eget_auto(H5E_DEFAULT,&old_func, &old_client_data);
76 
77  // Turn off error handling */
78  H5Eset_auto(H5E_DEFAULT,NULL, NULL);
79 
80  // Probe. On failure, retval is supposed to be negative
81 
82  hid_t retval = H5Aopen_name(object, attrname.data());
83 
84  // Restore previous error handler
85  H5Eset_auto(H5E_DEFAULT,old_func, old_client_data);
86 
87  bool result = (retval >= 0);
88  return result;
89 }
90 
91 hid_t getGroupFromPath(hid_t fd, const std::string &path) {
92  hid_t grp = H5Gopen(fd, path.data(), H5P_DEFAULT);
93  return grp;
94 }
95 
96 void getHeaderInfo(hid_t the_group, const std::string & det_type,
97  HeaderInfo & info) {
98  hid_t datasetid = H5Dopen(the_group, det_type.data(), H5P_DEFAULT);
99  hsize_t ds_size = H5Dget_storage_size(datasetid);
100  //std::cout << " Data Set Size (bytes): " << ds_size << std::endl;
101  // todo -- check for zero size
102  if (ds_size < 64) {
103  //std::cout << "TriggerRecordHeader datset too small" << std::endl;
104  }
105 
106  size_t narray = ds_size / sizeof(char);
107  size_t rdr = ds_size % sizeof(char);
108  if (rdr > 0 || narray == 0) narray++;
109  char *ds_data = new char[narray];
110  H5Dread(datasetid, H5T_STD_I8LE, H5S_ALL, H5S_ALL,
111  H5P_DEFAULT, ds_data);
112  //std::cout << std::hex << " Retrieved data: ecode: " << ecode <<
113  //" first byte: " << firstbyte << " last byte: " <<
114  //lastbyte << std::dec << std::endl;
115  H5Dclose(datasetid);
116 
117  //int magic_word = 0;
118  memcpy(&info.magicWord, &ds_data[0],4);
119  //std::cout << " Magic word: 0x" << std::hex << info.magicWord << std::dec <<
120  //std::endl;
121 
122  //int version = 0;
123  memcpy(&info.version, &ds_data[4],4);
124  //std::cout << " Version: " << std::dec << info.version << std::dec <<
125  //std::endl;
126 
127  //uint64_t trignum=0;
128  memcpy(&info.trigNum, &ds_data[8],8);
129  //std::cout << " Trig Num: " << std::dec << info.trigNum << std::dec <<
130  //std::endl;
131 
132  //uint64_t trig_timestamp=0;
133  memcpy(&info.trigTimestamp, &ds_data[16],8);
134  //std::cout << " Trig Timestamp: " << std::dec << info.trigTimestamp <<
135  //std::dec << std::endl;
136 
137  //uint64_t nreq=0;
138  memcpy(&info.nReq, &ds_data[24],8);
139  //std::cout << " No. of requested components: " << std::dec << info.nReq <<
140  //std::dec << std::endl;
141 
142  //int runno=0;
143  memcpy(&info.runNum, &ds_data[32], 4);
144  //std::cout << " Run Number: " << std::dec << info.runNum << std::endl;
145  //run_id = info.runNum;
146 
147  //int errbits=0;
148  memcpy(&info.errBits, &ds_data[36], 4);
149  //std::cout << " Error bits: " << std::dec << info.errBits << std::endl;
150 
151  //short triggertype=0;
152  memcpy(&info.triggerType, &ds_data[40], 2);
153  //std::cout << " Trigger type: " << std::dec << info.triggerType << std::endl;
154 
155  //delete[] ds_data; // free up memory
156 }
157 
158 
159 
160 }
161 }
static QCString result
std::unique_ptr< HDFFileInfo > HDFFileInfoPtr
Definition: HDF5Utils.h:46
std::string string
Definition: nybbler.cc:12
std::deque< std::string > getMidLevelGroupNames(hid_t grp)
Definition: HDF5Utils.cc:43
std::deque< std::string > getTopLevelGroupNames(HDFFileInfoPtr &hdfFileInfoPtr)
Definition: HDF5Utils.cc:36
fileName
Definition: dumpTree.py:9
bool attrExists(hid_t object, const std::string &attrname)
Definition: HDF5Utils.cc:71
hid_t getGroupFromPath(hid_t fd, const std::string &path)
Definition: HDF5Utils.cc:91
void getHeaderInfo(hid_t the_group, const std::string &det_type, HeaderInfo &info)
Definition: HDF5Utils.cc:96
uint64_t formatTrigTimeStamp(uint64_t trigTimeStamp)
Definition: HDF5Utils.cc:58
HDFFileInfoPtr openFile(const std::string &fileName)
Definition: HDF5Utils.cc:12
void closeFile(HDFFileInfoPtr hdfFileInfoPtr)
Definition: HDF5Utils.cc:31