5 #include "artdaq-core/Utilities/TimeUtils.hh" 6 #include <boost/algorithm/string.hpp> 12 #include <sys/resource.h> 15 namespace HDFFileReader {
24 hdfFileInfoPtr->filePtr = H5Fopen(fileName.data(), H5F_ACC_RDONLY, H5P_DEFAULT);
25 hdfFileInfoPtr->bytesWritten = 0;
27 hdfFileInfoPtr->runNumber = 0;
28 hdfFileInfoPtr->dataFormatVersion = 0;
30 hid_t grp = H5Gopen(hdfFileInfoPtr->filePtr,
"/", H5P_DEFAULT);
33 hid_t ga = H5Aopen_name(grp,
"data_format_version");
34 H5Aread(ga, H5Aget_type(ga), &hdfFileInfoPtr->dataFormatVersion);
38 return hdfFileInfoPtr;
43 H5Fclose(oldHdfFileInfoPtr->filePtr);
44 oldHdfFileInfoPtr->filePtr = 0;
47 newHdfFileInfoPtr->filePtr = H5Fopen(oldHdfFileInfoPtr->fileName.data(), H5F_ACC_RDONLY, H5P_DEFAULT);
48 newHdfFileInfoPtr->bytesWritten = 0;
49 newHdfFileInfoPtr->fileName = oldHdfFileInfoPtr->fileName;
50 newHdfFileInfoPtr->runNumber = oldHdfFileInfoPtr->runNumber;
51 newHdfFileInfoPtr->dataFormatVersion = oldHdfFileInfoPtr->dataFormatVersion;
52 return newHdfFileInfoPtr;
57 H5Fclose(hdfFileInfoPtr->filePtr);
58 hdfFileInfoPtr->filePtr = 0;
63 hid_t grp = H5Gopen(hdfFileInfoPtr->filePtr,
"/", H5P_DEFAULT);
71 std::list<std::string> theList;
73 H5Gget_num_objs(grp,&nobj);
74 for (hsize_t idx = 0; idx < nobj; ++idx)
76 hsize_t len = H5Gget_objname_by_idx(grp, idx, NULL, 0 );
77 char *memb_name =
new char(len+1);
78 H5Gget_objname_by_idx(grp, idx, memb_name, len+1 );
79 theList.emplace_back(memb_name);
87 hid_t grp = H5Gopen(hdfFileInfoPtr->filePtr,path.data(),H5P_DEFAULT);
98 void *old_client_data;
99 H5Eget_auto(H5E_DEFAULT,&old_func, &old_client_data);
102 H5Eset_auto(H5E_DEFAULT,NULL, NULL);
106 hid_t retval = H5Aopen_name(
object, attrname.data());
109 H5Eset_auto(H5E_DEFAULT,old_func, old_client_data);
111 bool result = (retval >= 0);
125 artdaq::Fragment::fragment_id_t sampleFragmentID = 1001;
130 for (
auto& detectorTypeName : detectorTypeNames)
132 if (detectorTypeName !=
"TPC")
continue;
134 if (dtnameloc ==
"TPC")
138 fragmentMap[dtnameloc] = std::make_unique<artdaq::Fragments>();
140 std::string subdetGroupPath = topLevelGroupName +
"/" + detectorTypeName;
146 for (
auto& subdetGeoName : subdetGeoNames)
148 std::string geoGroupPath = subdetGroupPath +
"/" + subdetGeoName;
152 for (
auto& dataSetName : dataSetNames)
154 std::string dataSetPath = geoGroupPath +
"/" + dataSetName;
162 hid_t datasetid = H5Dopen(geoGroup,dataSetName.data(),H5P_DEFAULT);
163 hsize_t ds_size = H5Dget_storage_size(datasetid);
164 std::vector<char> ds_data(ds_size);
165 H5Dread(datasetid, H5T_STD_I8LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, ds_data.data());
168 artdaq::FragmentPtr frag;
169 artdaq::Fragment::type_t
type = artdaq::Fragment::InvalidFragmentType;
170 uint32_t payloadOffset = 0;
173 if (ds_size < 81)
continue;
174 frag = artdaq::Fragment::FragmentBytes(ds_size - 80);
176 frag->setUserType(type);
192 memcpy(&trignum, &ds_data[16],8);
194 frag->setSequenceID(trignum);
196 uint64_t triggerTime = 0;
197 memcpy(&triggerTime, &ds_data[24],8);
200 uint64_t windowStart=0;
201 memcpy(&windowStart, &ds_data[32],8);
204 uint64_t windowEnd=0;
205 memcpy(&windowEnd, &ds_data[40],8);
209 memcpy(&runno, &ds_data[48], 4);
211 hdfFileInfoPtr->runNumber = runno;
250 felixMetadata.
offset_frames = (uint32_t) ((triggerTime - windowStart) / 25);
251 felixMetadata.
window_frames = (uint32_t) ((windowEnd - windowStart) / 25);
254 frag->setMetadata(felixMetadata);
255 frag->setTimestamp(windowStart);
257 memcpy(frag->dataBeginBytes() + payloadOffset, &ds_data[80], ds_size-80);
260 uint64_t pdsPayloadHeaderWord = 0;
263 payloadOffset =
sizeof(uint64_t);
264 frag = artdaq::Fragment::FragmentBytes(ds_size +
sizeof(uint64_t));
272 if (
attrExists(requestedGroup,
"trigger_timestamp"))
275 hid_t
a1 = H5Aopen_name(requestedGroup,
"trigger_timestamp");
276 H5Aread(a1, H5Aget_type(a1), ×tamp);
281 if (
attrExists(requestedGroup,
"timeslice_start"))
284 hid_t
a1 = H5Aopen_name(requestedGroup,
"timeslice_start");
285 H5Aread(a1, H5Aget_type(a1), ×tamp);
292 if (
attrExists(datasetid,
"timestamp_of_first_packet"))
295 hid_t
a1 = H5Aopen_name(datasetid,
"timestamp_of_first_packet");
296 H5Aread(a1, H5Aget_type(a1), ×tamp);
303 if (
attrExists(requestedGroup,
"timeslice_end"))
306 hid_t
a1 = H5Aopen_name(requestedGroup,
"timeslice_end");
307 H5Aread(a1, H5Aget_type(a1), ×tamp);
309 pdsMetadata.
endTime = timestamp * 3;
313 if (
attrExists(datasetid,
"timestamp_of_last_packet"))
316 hid_t
a1 = H5Aopen_name(datasetid,
"timestamp_of_last_packet");
317 H5Aread(a1, H5Aget_type(a1), ×tamp);
319 pdsMetadata.
endTime = timestamp * 3;
324 if (
attrExists(datasetid,
"number_of_packets"))
326 uint32_t packetCount;
327 hid_t
a1 = H5Aopen_name(datasetid,
"number_of_packets");
328 H5Aread(a1, H5Aget_type(a1), &packetCount);
336 hid_t
a1 = H5Aopen_name(datasetid,
"size_in_bytes");
337 H5Aread(a1, H5Aget_type(a1), &sizeBytes);
339 pdsMetadata.
length = 9 + (sizeBytes /
sizeof(uint32_t));
340 pdsPayloadHeaderWord |= ((2 + (sizeBytes /
sizeof(uint32_t))) & 0xffffffff);
344 pdsMetadata.
length = 9 + (ds_size /
sizeof(uint32_t));
345 pdsPayloadHeaderWord |= ((2 + (ds_size /
sizeof(uint32_t))) & 0xffffffff);
348 frag->setMetadata(pdsMetadata);
349 pdsPayloadHeaderWord |= 0x000003e700000000;
352 artdaq::Fragment::sequence_id_t seqID;
353 artdaq::Fragment::fragment_id_t fragID;
355 std::stringstream seqStr(topLevelGroupName);
358 seqID &= 0xffffffffffff;
360 fragID = sampleFragmentID++;
362 frag->setFragmentID(fragID);
369 memcpy(frag->dataBeginBytes(), &pdsPayloadHeaderWord,
sizeof(uint64_t));
372 fragmentMap[dtnameloc]->emplace_back(
std::move(*(frag.release())));
376 H5Gclose(subdetGroup);
380 H5Gclose(requestedGroup);
HDFFileInfoPtr reopenFile(HDFFileInfoPtr oldHdfFileInfoPtr)
HDFFileInfoPtr openFile(const std::string &fileName)
std::list< std::string > getTopLevelGroupNames(HDFFileInfoPtr &hdfFileInfoPtr)
std::list< std::string > getMidLevelGroupNames(hid_t grp)
const std::string PDS_GROUP_NAME
FragmentListsByType getFragmentsForEvent(HDFFileInfoPtr &hdfFileInfoPtr, const std::string &topLevelGroupName)
const std::string TPC_GROUP_NAME
std::unique_ptr< HDFFileInfo > HDFFileInfoPtr
std::map< std::string, std::unique_ptr< artdaq::Fragments > > FragmentListsByType
void closeFile(HDFFileInfoPtr hdfFileInfoPtr)
hid_t getGroupFromPath(HDFFileInfoPtr &hdfFileInfoPtr, const std::string &path)
bool attrExists(hid_t object, const std::string &attrname)