ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
AtFRIBHDFUnpacker.cxx
Go to the documentation of this file.
1 #include "AtFRIBHDFUnpacker.h"
2 
3 #include "AtGenericTrace.h" // for AtGenericTrace
4 #include "AtRawEvent.h"
5 
6 #include <FairLogger.h>
7 
8 #include <Rtypes.h>
9 #include <TString.h>
10 
11 #include <H5Cpp.h> // for herr_t, hsize_t
12 #include <H5Gpublic.h> // for H5Giterate
13 #include <H5Ipublic.h> // for hid_t
14 
15 #include <algorithm> // for max_element, min_element
16 #include <cstdint> // for int16_t
17 #include <regex> // for match_results<>::_Base_type, regex_replace
18 #include <sstream> // for basic_stringbuf<>::int_type, basic_strin...
19 #include <tuple> // for get
20 #include <utility> // for __tuple_element_t
21 
23 
25 
27 {
28  auto numEvents = open(fInputFileName.c_str());
29  auto uniqueEvents = GetNumEvents();
30  LOG(info) << numEvents << " " << uniqueEvents << "\n";
31  if (fEventID > uniqueEvents)
32  LOG(fatal) << "Exceded valid range of event numbers. Looking for " << fEventID << " max event number is "
33  << uniqueEvents;
34  if (uniqueEvents != numEvents / 3)
35  LOG(error) << "Number of events from metaData does not match the number of entries in HDF5 file!";
36 
37  // Correct event ID for offset in file
39 }
40 
41 std::size_t AtFRIBHDFUnpacker::open(char const *file)
42 {
44  if (f == 0)
45  return 0;
46  _file = f;
47 
48  auto group_n_entries = open_group(f, "frib/evt");
49  if (std::get<0>(group_n_entries) == -1)
50  return 0;
51  _group = std::get<0>(group_n_entries);
53  return std::get<1>(group_n_entries);
54 }
55 
57 {
58  TString event_name = TString::Format("evt%lld_1903", fDataEventID);
59  fRawEvent->SetEventName(event_name.Data());
60  std::size_t npads = n_pads(event_name.Data());
61 
62  for (auto ipad = 0; ipad < npads; ++ipad)
63  processPad(ipad);
64 
65  end_raw_event(); // Close dataset
66 }
67 
68 void AtFRIBHDFUnpacker::processPad(std::size_t ipad)
69 {
70  std::vector<int16_t> rawadc = pad_raw_data(ipad);
71 
72  auto trace = fRawEvent->AddGenericTrace(ipad);
73  auto baseline = getBaseline(rawadc);
74  for (Int_t iTb = 0; iTb < 2048; iTb++) {
75  trace->SetRawADC(iTb, rawadc.at(iTb));
76  trace->SetADC(iTb, rawadc.at(iTb) - baseline);
77  }
78 }
79 
80 std::vector<int16_t> AtFRIBHDFUnpacker::pad_raw_data(std::size_t i_pad)
81 {
82  int16_t data[2048];
83  hsize_t counts[2] = {2048, 1};
84  hsize_t offsets[2] = {0, i_pad};
85  hsize_t dims_out[2] = {2048, 1};
86  read_slab<int16_t>(_dataset, counts, offsets, dims_out, data);
87  std::vector<int16_t> datav(data, data + 2048);
88  return datav;
89 }
90 
91 std::size_t AtFRIBHDFUnpacker::n_pads(std::string i_raw_event)
92 {
93  std::string dataset_name = i_raw_event;
94  auto dataset_dims = open_dataset(_group, dataset_name.c_str());
95  if (std::get<0>(dataset_dims) == 0)
96  return 0;
97  _dataset = std::get<0>(dataset_dims);
98  return std::get<1>(dataset_dims)[1];
99 }
100 
102 {
103  // Look for the meta group and from it pull the minimum and maximum event numbers
104  open_group(_file, "meta");
105 
106  // N.B. This function is adapted to the format of the FRIB DAQ data stored in the HDF5.
107  auto addToVector = [](hid_t group, const char *name, void *op_data) -> herr_t {
108  std::string text = std::string(name);
109  if (text.find("header") != std::string::npos)
110  return 0;
111  std::regex regex("evt(\\d+)_\\d+");
112  std::string result = std::regex_replace(text, regex, "$1\n");
113 
114  int number;
115  std::istringstream iss(result);
116  while (iss >> number) {
117  LOG(debug) << number << std::endl;
118  }
119 
120  // auto num = std::stoi(std::regex_replace(std::string(name), std::regex("[^0-9]"), ""));
121  auto data = (std::vector<long> *)op_data; // NOLINT
122  data->push_back(number);
123  if (number == 0)
124  LOG(info) << name << " to " << number << " " << data->size();
125  return 0;
126  };
127  int idx = 0;
128  std::vector<long> eventIDs;
129  H5Giterate(_file, "frib/evt", &idx, addToVector, &eventIDs);
130  fLastEvent = *std::max_element(eventIDs.begin(), eventIDs.end());
131  fFirstEvent = *std::min_element(eventIDs.begin(), eventIDs.end());
132  //}
133  LOG(info) << "Events: " << fFirstEvent << " to " << fLastEvent;
134 }
AtHDFUnpacker::GetNumEvents
Long64_t GetNumEvents() override
Definition: AtHDFUnpacker.cxx:63
AtHDFUnpacker::_file
hid_t _file
Definition: AtHDFUnpacker.h:83
AtHDFUnpacker::IO_MODE::READ
@ READ
AtRawEvent.h
AtFRIBHDFUnpacker::pad_raw_data
std::vector< int16_t > pad_raw_data(std::size_t i_pad) override
Definition: AtFRIBHDFUnpacker.cxx:80
AtFRIBHDFUnpacker::processPad
void processPad(std::size_t padIndex) override
Definition: AtFRIBHDFUnpacker.cxx:68
AtFRIBHDFUnpacker::processData
void processData() override
Definition: AtFRIBHDFUnpacker.cxx:56
AtBaseEvent::SetEventName
void SetEventName(std::string name)
Definition: AtBaseEvent.h:65
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
AtFRIBHDFUnpacker::open
std::size_t open(char const *file) override
Definition: AtFRIBHDFUnpacker.cxx:41
AtHDFUnpacker::end_raw_event
void end_raw_event()
Definition: AtHDFUnpacker.cxx:367
AtFRIBHDFUnpacker
Definition: AtFRIBHDFUnpacker.h:22
AtHDFUnpacker::open_file
hid_t open_file(char const *file, IO_MODE mode)
Definition: AtHDFUnpacker.cxx:155
AtFRIBHDFUnpacker::n_pads
std::size_t n_pads(std::string i_raw_event) override
Definition: AtFRIBHDFUnpacker.cxx:91
AtUnpacker::fDataEventID
Long64_t fDataEventID
Definition: AtUnpacker.h:24
AtUnpacker::fInputFileName
std::string fInputFileName
Definition: AtUnpacker.h:22
AtHDFUnpacker::getBaseline
Float_t getBaseline(const std::vector< int16_t > &data)
Definition: AtHDFUnpacker.cxx:136
AtHDFUnpacker::open_group
std::tuple< hid_t, hsize_t > open_group(hid_t fileId, char const *group)
Definition: AtHDFUnpacker.cxx:171
ClassImp
ClassImp(AtFRIBHDFUnpacker)
AtFRIBHDFUnpacker::Init
void Init() override
Definition: AtFRIBHDFUnpacker.cxx:26
AtRawEvent::AddGenericTrace
AtGenericTrace * AddGenericTrace(Ts &&...params)
Definition: AtRawEvent.h:109
mapPtr
std::shared_ptr< AtMap > mapPtr
Definition: AtUnpacker.h:17
AtFRIBHDFUnpacker::AtFRIBHDFUnpacker
AtFRIBHDFUnpacker(mapPtr map)
Definition: AtFRIBHDFUnpacker.cxx:24
AtUnpacker::fEventID
Long64_t fEventID
Definition: AtUnpacker.h:23
trace
std::array< Double_t, 512 > trace
Definition: AtCalibration.h:9
AtGenericTrace.h
AtFRIBHDFUnpacker::setFirstAndLastEventNum
void setFirstAndLastEventNum() override
Definition: AtFRIBHDFUnpacker.cxx:101
AtHDFUnpacker
Definition: AtHDFUnpacker.h:32
AtHDFUnpacker::_group
hid_t _group
Definition: AtHDFUnpacker.h:84
AtHDFUnpacker::open_dataset
std::tuple< hid_t, std::vector< hsize_t > > open_dataset(hid_t locId, char const *dataset)
Definition: AtHDFUnpacker.cxx:186
AtFRIBHDFUnpacker.h
AtHDFUnpacker::fFirstEvent
std::size_t fFirstEvent
Definition: AtHDFUnpacker.h:38
AtHDFUnpacker::fLastEvent
std::size_t fLastEvent
Definition: AtHDFUnpacker.h:39