ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
AtHDFUnpacker.cxx
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  *********************************************************************/
4 
5 #include "AtHDFUnpacker.h"
6 
7 #include "AtAuxPad.h"
8 #include "AtMap.h"
9 #include "AtPad.h"
10 #include "AtPadReference.h"
11 #include "AtRawEvent.h"
12 
13 #include <FairLogger.h>
14 
15 #include <Rtypes.h>
16 #include <TString.h>
17 
18 #include <H5Gpublic.h>
19 #include <H5Ppublic.h>
20 
21 #include <algorithm> // for max_element, min_element
22 #include <cstdint>
23 #include <exception> // for exception
24 #include <iostream>
25 #include <memory>
26 #include <regex>
27 #include <sstream> // for basic_stringbuf<>::int_type, basic_strin...
28 #include <stdexcept> // for out_of_range
29 #include <string>
30 #include <type_traits>
31 #include <utility>
32 
34 
36 
38 {
39  auto numEvents = open(fInputFileName.c_str());
40  auto uniqueEvents = GetNumEvents();
41  if (fEventID > uniqueEvents)
42  LOG(fatal) << "Exceded valid range of event numbers. Looking for " << fEventID << " max event number is "
43  << uniqueEvents;
44  if (uniqueEvents != numEvents / 2)
45  LOG(error) << "Number of events from metaData does not match the number of entries in HDF5 file!";
46 
47  // Correct event ID for offset in file
49 }
50 
52 {
53  LOG(debug) << " Unpacking event ID: " << fEventID << " with internal ID " << fDataEventID;
54  fRawEvent = &event;
55  setEventIDAndTimestamps();
56  processData();
57 
58  fRawEvent->SetIsGood(kTRUE);
59  LOG(debug) << " Unpacked " << fRawEvent->GetNumPads() << " pads";
60  fEventID++;
61  fDataEventID++;
62 }
64 {
65  return fLastEvent - fFirstEvent + 1;
66 }
68 {
69  return fDataEventID >= fLastEvent;
70 }
71 
72 void AtHDFUnpacker::setEventIDAndTimestamps()
73 {
75 
76  try {
77  TString header_name = TString::Format("evt%lld_header", fDataEventID);
78  auto header = get_header(header_name.Data());
79 
81  for (int i = 0; i < fNumberTimestamps; ++i) {
82  fRawEvent->SetTimestamp(header.at(i + 1), i);
83  }
84  } catch (const std::out_of_range &e) {
85  LOG(error) << "Expected " << fNumberTimestamps << " but the header is not long enough!";
86  } catch (const std::exception &e) {
87  LOG(debug) << "Failed to load the header, not setting timestamps.";
89  }
90 }
92 {
93  TString event_name = TString::Format("evt%lld_data", fDataEventID);
94  // fRawEvent->SetEventName(event_name.Data());
95  LOG(debug) << fRawEvent->GetEventName() << "\n";
96  std::size_t npads = n_pads(event_name.Data());
97 
98  for (auto ipad = 0; ipad < npads; ++ipad)
99  processPad(ipad);
100 
101  end_raw_event(); // Close dataset
102 }
103 
104 void AtHDFUnpacker::processPad(std::size_t ipad)
105 {
106  std::vector<int16_t> rawadc = pad_raw_data(ipad);
107  AtPadReference PadRef = {rawadc[0], rawadc[1], rawadc[2], rawadc[3]};
108 
109  auto pad = createPadAndSetIsAux(PadRef);
110  setDimensions(pad);
111  setAdc(pad, rawadc);
112 }
113 AtPad *AtHDFUnpacker::createPadAndSetIsAux(const AtPadReference &padRef)
114 {
115  if (fSaveFPN && fMap->IsFPNchannel(padRef))
116  return fRawEvent->AddFPN(padRef);
117 
118  if (fMap->IsAuxPad(padRef)) {
119  auto padName = fMap->GetAuxName(padRef);
120  return fRawEvent->AddAuxPad(padName).first;
121  }
122 
123  auto padNumber = fMap->GetPadNum(padRef);
124  return fRawEvent->AddPad(padNumber);
125 }
126 void AtHDFUnpacker::setAdc(AtPad *pad, const std::vector<int16_t> &data)
127 {
128  auto baseline = getBaseline(data);
129  for (Int_t iTb = 0; iTb < 512; iTb++) {
130  pad->SetRawADC(iTb, data.at(iTb + 5)); // First 5 words are electronic id
131  pad->SetADC(iTb, data.at(iTb + 5) - baseline);
132  }
134 }
135 
136 Float_t AtHDFUnpacker::getBaseline(const std::vector<int16_t> &data)
137 {
138  Float_t baseline = 0;
139 
141  for (Int_t iTb = 5; iTb < 25; iTb++) // First 5 words are electronic id
142  baseline += data[iTb];
143  baseline /= 20.0;
144  }
145  return baseline;
146 }
147 void AtHDFUnpacker::setDimensions(AtPad *pad)
148 {
149  auto PadCenterCoord = fMap->CalcPadCenter(pad->GetPadNum());
150  Int_t pSizeID = fMap->GetPadSize(pad->GetPadNum());
151  pad->SetPadCoord(PadCenterCoord);
152  pad->SetSizeID(pSizeID);
153 }
154 
155 hid_t AtHDFUnpacker::open_file(char const *file, IO_MODE mode)
156 {
157  hid_t fileId;
158  (mode == IO_MODE::READ) ? fileId = H5Fopen(file, H5F_ACC_RDONLY, H5P_DEFAULT)
159  : fileId = H5Fopen(file, H5F_ACC_RDWR, H5P_DEFAULT);
160 
161  if (fileId >= 0) {
162  std::cout << "> hdf5_wrapper::open_file:MESSAGE, opening file: " << file << ", ID: " << fileId << '\n';
163 
164  return fileId;
165  } else {
166  std::cerr << "> AtHDFUnpacker::open_file:ERROR, invalid ID for file: " << file << '\n';
167  return 0;
168  }
169 }
170 
171 std::tuple<hid_t, hsize_t> AtHDFUnpacker::open_group(hid_t fileId, char const *group)
172 {
173  hid_t groupId = H5Gopen2(fileId, group, H5P_DEFAULT);
174  if (groupId >= 0) {
175  // std::cout << "> hdf5_wrapper::open_group:MESSAGE, opening group: " << group << ", ID: " << groupId << '\n';
176  hsize_t size;
177  H5Gget_num_objs(groupId, &size);
178  return std::make_tuple(groupId, size);
179 
180  } else {
181  std::cerr << "> AtHDFUnpacker::open_group:ERROR, invalid ID for group: " << group << '\n';
182  return std::make_tuple(-1, -1);
183  }
184 }
185 
186 std::tuple<hid_t, std::vector<hsize_t>> AtHDFUnpacker::open_dataset(hid_t locId, char const *dataset)
187 {
188 
189  hid_t datasetId = H5Dopen2(locId, dataset, H5P_DEFAULT);
190  if (datasetId >= 0) {
191  // std::cout << "> hdf5_wrapper::open_dataset:MESSAGE, opening dataset: " << dataset << ", ID: " << datasetId <<
192  // '\n';
193  hid_t dspaceId = H5Dget_space(datasetId);
194  int n_dims = H5Sget_simple_extent_ndims(dspaceId);
195  hsize_t dims[n_dims];
196  H5Sget_simple_extent_dims(dspaceId, dims, nullptr);
197  std::vector<hsize_t> v_dims(dims, dims + n_dims);
198  return std::make_tuple(datasetId, v_dims);
199  } else {
200  std::cerr << "> AtHDFUnpacker::open_dataset:ERROR, invalid ID for dataset: " << dataset << '\n';
201  std::vector<hsize_t> v{0};
202  return std::make_tuple(0, v);
203  }
204 }
205 
206 void AtHDFUnpacker::close_file(hid_t fileId)
207 {
208  herr_t retId = H5Fclose(fileId);
209  if (retId < 0)
210  std::cerr << "> AtHDFUnpacker::close_file:ERROR, cannot close file with ID: " << fileId << '\n';
211 }
212 
213 void AtHDFUnpacker::close_group(hid_t groupId)
214 {
215  herr_t retId = H5Gclose(groupId);
216  if (retId < 0)
217  std::cerr << "> AtHDFUnpacker::close_group:ERROR, cannot close group with ID: " << groupId << '\n';
218 }
219 
220 void AtHDFUnpacker::close_dataset(hid_t datasetId)
221 {
222  herr_t retId = H5Dclose(datasetId);
223  if (retId < 0)
224  std::cerr << "> AtHDFUnpacker::close_dataset:ERROR, cannot close dataset with ID: " << datasetId << '\n';
225 }
226 
228 {
229  // Look for the meta group and from it pull the minimum and maximum event numbers
230  auto meta_size = open_group(_file, "meta");
231  auto metaID = std::get<0>(meta_size);
232  if (metaID > 0) {
233  std::string datasetName = "meta";
234  auto dataset_dims = open_dataset(metaID, datasetName.c_str());
235  auto datasetId = std::get<0>(dataset_dims);
236  auto len = std::get<1>(dataset_dims).at(0);
237 
238  auto *data = new int64_t[len]; // NOLINT
239  H5Dread(datasetId, H5T_NATIVE_ULONG, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
240 
241  fFirstEvent = data[0];
242  fLastEvent = data[2];
243 
244  delete[] data; // NOLINT
245  } else {
246 
247  // If there is not the meta data then we need to look through every event in the
248  // file and pull out the first and last event numbers
249  auto addToVector = [](hid_t group, const char *name, void *op_data) -> herr_t {
250  auto num = std::stoi(std::regex_replace(std::string(name), std::regex("[^0-9]"), ""));
251  auto data = (std::vector<long> *)op_data; // NOLINT
252  data->push_back(num);
253  LOG(debug) << name << " to " << num << " " << data->size();
254  return 0;
255  };
256  int idx = 0;
257  std::vector<long> eventIDs;
258  H5Giterate(_file, "get", &idx, addToVector, &eventIDs);
259  fLastEvent = *std::max_element(eventIDs.begin(), eventIDs.end());
260  fFirstEvent = *std::min_element(eventIDs.begin(), eventIDs.end());
261  }
262  LOG(info) << "Events: " << fFirstEvent << " to " << fLastEvent;
263 }
264 std::size_t AtHDFUnpacker::open(char const *file)
265 {
267  if (f == 0)
268  return 0;
269  _file = f;
270 
271  auto group_n_entries = open_group(f, "get");
272  if (std::get<0>(group_n_entries) == -1)
273  return 0;
274  _group = std::get<0>(group_n_entries);
276  return std::get<1>(group_n_entries);
277 }
278 
279 std::string AtHDFUnpacker::get_event_name(std::size_t idx)
280 {
281  if (_group >= 0) {
282  char name[100];
283  H5Lget_name_by_idx(_group, ".", H5_INDEX_NAME, H5_ITER_INC, idx, name, 100, H5P_DEFAULT);
284  std::string name_string(name);
285  return name_string;
286  } else {
287  std::cerr << "> AtHDFUnpacker::get_event_name:ERROR, invalid ID " << idx << '\n';
288  return "invalid";
289  }
290 }
291 
292 std::vector<uint64_t> AtHDFUnpacker::get_header(std::string headerName)
293 {
294  std::vector<uint64_t> retVec;
295 
296  auto dataset_dims = open_dataset(_group, headerName.c_str());
297  if (std::get<0>(dataset_dims) == 0)
298  return retVec;
299 
300  _dataset = std::get<0>(dataset_dims);
301 
302  // Get the length of the header
303  auto len = std::get<1>(dataset_dims).at(0);
304 
305  retVec.resize(len);
306  // auto *data = new uint64_t[len];
307  H5Dread(_dataset, H5T_NATIVE_ULONG, H5S_ALL, H5S_ALL, H5P_DEFAULT, retVec.data());
308 
310  // Add read data to the vector and return it
311  // for (int i = 0; i < len; ++i)
312  // retVec.push_back(data[i]);
313 
314  // delete[] data;
315 
316  return retVec;
317 }
318 
319 std::size_t AtHDFUnpacker::n_pads(std::string i_raw_event)
320 {
321  std::string dataset_name = i_raw_event;
322  auto dataset_dims = open_dataset(_group, dataset_name.c_str());
323  if (std::get<0>(dataset_dims) == 0)
324  return 0;
325  _dataset = std::get<0>(dataset_dims);
326  return std::get<1>(dataset_dims)[0];
327 }
328 
329 std::vector<int16_t> AtHDFUnpacker::pad_raw_data(std::size_t i_pad)
330 {
331  int16_t data[517];
332  hsize_t counts[2] = {1, 517};
333  hsize_t offsets[2] = {i_pad, 0};
334  hsize_t dims_out[2] = {1, 517};
335  read_slab<int16_t>(_dataset, counts, offsets, dims_out, data);
336  std::vector<int16_t> datav(data, data + 517);
337  return datav;
338 }
339 
340 /*std::size_t AtHDFUnpacker::inievent()
341 {
342  return _inievent;
343 }
344 */
345 
346 std::size_t AtHDFUnpacker::datasets()
347 {
348  H5Literate(_group, H5_INDEX_NAME, H5_ITER_INC, nullptr, file_info, nullptr);
349  return 0;
350 }
351 
352 herr_t AtHDFUnpacker::file_info(hid_t loc_id, const char *name, const H5L_info_t *linfo, void *opdata)
353 {
354  hid_t group;
355  /*
356  * Open the group using its name.
357  */
358  group = H5Gopen2(loc_id, name, H5P_DEFAULT);
359  /*
360  * Display group name.
361  */
362  std::cout << "Name : " << name << "\n";
363  H5Gclose(group);
364  return 0;
365 }
366 
368 {
370 }
371 
372 void AtHDFUnpacker::close()
373 {
375  close_file(_file);
376 }
AtUnpacker::fMap
mapPtr fMap
Definition: AtUnpacker.h:21
AtPad.h
AtHDFUnpacker::GetNumEvents
Long64_t GetNumEvents() override
Definition: AtHDFUnpacker.cxx:63
AtHDFUnpacker::_file
hid_t _file
Definition: AtHDFUnpacker.h:83
AtHDFUnpacker::IO_MODE::READ
@ READ
AtHDFUnpacker::n_pads
virtual std::size_t n_pads(std::string i_raw_event)
Definition: AtHDFUnpacker.cxx:319
AtHDFUnpacker::setFirstAndLastEventNum
virtual void setFirstAndLastEventNum()
Definition: AtHDFUnpacker.cxx:227
AtRawEvent.h
AtAuxPad.h
AtPadReference.h
AtPad::SetPadCoord
void SetPadCoord(const XYPoint &point)
Definition: AtPad.h:87
AtUnpacker::fRawEvent
AtRawEvent * fRawEvent
Definition: AtUnpacker.h:25
AtHDFUnpacker::_dataset
hid_t _dataset
Definition: AtHDFUnpacker.h:85
f
double(* f)(double t, const double *par)
Definition: lmcurve.cxx:21
AtHDFUnpacker::fNumberTimestamps
Int_t fNumberTimestamps
Definition: AtHDFUnpacker.h:35
AtPad::SetRawADC
void SetRawADC(const rawTrace &val)
Definition: AtPad.h:89
AtHDFUnpacker.h
AtHDFUnpacker::fIsBaseLineSubtraction
Bool_t fIsBaseLineSubtraction
Definition: AtHDFUnpacker.h:36
AtHDFUnpacker::end_raw_event
void end_raw_event()
Definition: AtHDFUnpacker.cxx:367
AtPad::SetPedestalSubtracted
void SetPedestalSubtracted(Bool_t val=kTRUE)
Definition: AtPad.h:86
AtHDFUnpacker::open_file
hid_t open_file(char const *file, IO_MODE mode)
Definition: AtHDFUnpacker.cxx:155
AtHDFUnpacker::processData
virtual void processData()
Definition: AtHDFUnpacker.cxx:91
AtPad::SetADC
void SetADC(const trace &val)
Definition: AtPad.h:91
AtBaseEvent::SetEventID
void SetEventID(ULong_t evtid)
Definition: AtBaseEvent.h:60
AtHDFUnpacker::close_group
void close_group(hid_t group)
Definition: AtHDFUnpacker.cxx:213
AtUnpacker::fDataEventID
Long64_t fDataEventID
Definition: AtUnpacker.h:24
AtBaseEvent::SetIsGood
void SetIsGood(Bool_t value)
Definition: AtBaseEvent.h:61
AtRawEvent
Definition: AtRawEvent.h:34
AtHDFUnpacker::IsLastEvent
bool IsLastEvent() override
Definition: AtHDFUnpacker.cxx:67
AtHDFUnpacker::open
virtual std::size_t open(char const *file)
Definition: AtHDFUnpacker.cxx:264
AtHDFUnpacker::processPad
virtual void processPad(std::size_t padIndex)
Definition: AtHDFUnpacker.cxx:104
AtHDFUnpacker::close_file
void close_file(hid_t file)
Definition: AtHDFUnpacker.cxx:206
AtHDFUnpacker::IO_MODE
IO_MODE
Definition: AtHDFUnpacker.h:54
AtBaseEvent::AddAuxPad
std::pair< AtAuxPad *, bool > AddAuxPad(std::string auxName)
Add new auxilary pad (AtAuxPad) to event.
Definition: AtBaseEvent.cxx:44
AtUnpacker::fInputFileName
std::string fInputFileName
Definition: AtUnpacker.h:22
AtHDFUnpacker::pad_raw_data
virtual std::vector< int16_t > pad_raw_data(std::size_t i_pad)
Definition: AtHDFUnpacker.cxx:329
AtHDFUnpacker::getBaseline
Float_t getBaseline(const std::vector< int16_t > &data)
Definition: AtHDFUnpacker.cxx:136
AtHDFUnpacker::close_dataset
void close_dataset(hid_t dataset)
Definition: AtHDFUnpacker.cxx:220
AtHDFUnpacker::FillRawEvent
void FillRawEvent(AtRawEvent &event) override
Definition: AtHDFUnpacker.cxx:51
AtHDFUnpacker::AtHDFUnpacker
AtHDFUnpacker(mapPtr map)
Definition: AtHDFUnpacker.cxx:35
AtPad::GetPadNum
Int_t GetPadNum() const
Definition: AtPad.h:96
AtHDFUnpacker::open_group
std::tuple< hid_t, hsize_t > open_group(hid_t fileId, char const *group)
Definition: AtHDFUnpacker.cxx:171
AtRawEvent::AddPad
AtPad * AddPad(Ts &&...params)
Create a new pad in this event.
Definition: AtRawEvent.h:82
AtUnpacker::fSaveFPN
Bool_t fSaveFPN
Definition: AtUnpacker.h:27
mapPtr
std::shared_ptr< AtMap > mapPtr
Definition: AtUnpacker.h:17
AtMap.h
AtRawEvent::GetNumPads
Int_t GetNumPads() const
Definition: AtRawEvent.h:122
AtRawEvent::AddFPN
AtPad * AddFPN(const AtPadReference &ref)
Create a new FPN channel.
Definition: AtRawEvent.cxx:77
AtUnpacker::fEventID
Long64_t fEventID
Definition: AtUnpacker.h:23
AtBaseEvent::GetEventName
std::string GetEventName() const
Definition: AtBaseEvent.h:72
AtBaseEvent::SetTimestamp
void SetTimestamp(ULong64_t timestamp, int index=0)
Definition: AtBaseEvent.cxx:35
AtPad
Container class for AtPadBase objects.
Definition: AtPad.h:38
AtHDFUnpacker
Definition: AtHDFUnpacker.h:32
AtPad::SetSizeID
void SetSizeID(Int_t sizeID)
Definition: AtPad.h:84
AtHDFUnpacker::_group
hid_t _group
Definition: AtHDFUnpacker.h:84
AtPadReference
Definition: AtPadReference.h:20
AtUnpacker
Definition: AtUnpacker.h:19
AtBaseEvent::SetNumberOfTimestamps
void SetNumberOfTimestamps(int numTS)
Definition: AtBaseEvent.h:63
ClassImp
ClassImp(AtHDFUnpacker)
AtHDFUnpacker::open_dataset
std::tuple< hid_t, std::vector< hsize_t > > open_dataset(hid_t locId, char const *dataset)
Definition: AtHDFUnpacker.cxx:186
AtHDFUnpacker::fFirstEvent
std::size_t fFirstEvent
Definition: AtHDFUnpacker.h:38
AtHDFUnpacker::Init
void Init() override
Definition: AtHDFUnpacker.cxx:37
AtHDFUnpacker::fLastEvent
std::size_t fLastEvent
Definition: AtHDFUnpacker.h:39