VDColdboxPDSDecoder_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: VDColdboxPDSDecoder
3 // Plugin Type: producer (Unknown Unknown)
4 // File: VDColdboxPDSDecoder_module.cc
5 //
6 // Generated at Tue Nov 9 16:28:27 2021 by Jacob Calcutt using cetskelgen
7 // from version .
8 ////////////////////////////////////////////////////////////////////////
9 
17 #include "fhiclcpp/ParameterSet.h"
19 
22 #include <hdf5.h>
25 #include "daqdataformats/Fragment.hpp"
26 #include "detdataformats/ssp/SSPTypes.hpp"
27 
28 #include <memory>
29 
30 namespace dune {
31  class VDColdboxPDSDecoder;
32 }
33 
34 
36 public:
37  explicit VDColdboxPDSDecoder(fhicl::ParameterSet const& p);
38  // The compiler-generated destructor is fine for non-base
39  // classes without bare pointers or other resource use.
40 
41  // Plugins should not be copied or assigned.
46 
47  // Required functions.
48  void produce(art::Event& e) override;
49 
50 private:
51 
52  std::pair<raw::OpDetWaveform, recob::OpHit> MakeWaveformAndHit(
53  const dunedaq::detdataformats::ssp::EventHeader * event_header,
54  size_t data_pos, uint8_t * data_ptr);
55 
56  hid_t fPrevStoredHandle = -1;
57  hid_t fHDFFile = -1;
60  bool fForceOpen;
61  bool fDebug;
62 
63  // Declare member data here.
64 
65 };
66 
67 
69  : EDProducer{p},
70  fOutputDataLabel{p.get<std::string>("OutputDataLabel")},
71  fFileInfoLabel{p.get<std::string>("FileInfoLabel", "daq")},
72  fForceOpen(p.get<bool>("ForceOpen", false)),
73  fDebug(p.get<bool>("Debug", false)) {
74  produces<std::vector<raw::OpDetWaveform>>(fOutputDataLabel);
75  produces<std::vector<recob::OpHit>>(fOutputDataLabel);
76 }
77 
79 
80  using namespace dune::HDF5Utils;
81  using namespace dunedaq::detdataformats::ssp;
82 
83  //To-do: put in sizes here?
84  std::unique_ptr<std::vector<raw::OpDetWaveform>> output_wfs
85  = std::make_unique<std::vector<raw::OpDetWaveform>>();
86  std::unique_ptr<std::vector<recob::OpHit>> output_hits
87  = std::make_unique<std::vector<recob::OpHit>>();
88 
89 
90  auto infoHandle = e.getHandle<raw::DUNEHDF5FileInfo>(fFileInfoLabel);
91  const std::string & group_name = infoHandle->GetEventGroupName();
92  const std::string & file_name = infoHandle->GetFileName();
93  hid_t file_id = infoHandle->GetHDF5FileHandle();
94 
95  //If the fcl file said to force open the file
96  //(i.e. because one is just running DataPrep), then open
97  //but only if we are on a new file -- identified by if the handle
98  //stored in the event is different
99  if (fForceOpen && (file_id != fPrevStoredHandle)) {
100  std::cout << "Opening" << std::endl;
101  fHDFFile = H5Fopen(file_name.data(), H5F_ACC_RDONLY, H5P_DEFAULT);
102  }//If the handle is the same, fHDFFile won't change
103  else if (!fForceOpen) {
104  fHDFFile = file_id;
105  }
106  fPrevStoredHandle = file_id;
107 
108  hid_t PDS_group = getGroupFromPath(fHDFFile, group_name + "/PDS");
109  std::deque<std::string> region_names = getMidLevelGroupNames(PDS_group);
110  if (fDebug)
111  std::cout << "Got " << region_names.size() << " regions" << std::endl;
112  for (const auto & n : region_names) {
113  hid_t region_group = getGroupFromPath(PDS_group, n);
114  std::deque<std::string> element_names = getMidLevelGroupNames(region_group);
115  if (fDebug)
116  std::cout << "Got " << element_names.size() << " elements" << std::endl;
117  for (const auto & element_name : element_names) {
118  if (fDebug) std::cout << element_name << std::endl;
119 
120  hid_t dataset = H5Dopen(region_group, element_name.data(), H5P_DEFAULT);
121  hsize_t ds_size = H5Dget_storage_size(dataset);
122  if (fDebug) std::cout << "\tDataset size: " << ds_size << std::endl;
123 
124  std::vector<char> ds_data(ds_size);
125  H5Dread(dataset, H5T_STD_I8LE, H5S_ALL, H5S_ALL, H5P_DEFAULT,
126  ds_data.data());
127  H5Dclose(dataset);
128 
129  Fragment frag(&ds_data[0], Fragment::BufferAdoptionMode::kReadOnlyMode);
130  if (fDebug) {
131  std::cout << "\tMade fragment" << std::endl;
132  std::cout << "\t" << frag.get_header() << std::endl;
133  }
134 
135 
136  uint8_t * data_ptr = reinterpret_cast<uint8_t*>(frag.get_data());
137 
138  size_t iP = 0;
139  size_t data_pos = 0;
140  while (data_pos < (frag.get_header().size - sizeof(FragmentHeader))) {
141  EventHeader * event_header
142  = reinterpret_cast<EventHeader*>(data_ptr + data_pos);
143  if (fDebug) {
144  std::cout << "\tEvent header " << event_header->length << std::endl;
145  std::cout << "\t" << iP << std::endl;
146  }
147 
148  auto wf_hit = MakeWaveformAndHit(event_header, data_pos, data_ptr);
149  output_wfs->emplace_back(wf_hit.first);
150  output_hits->emplace_back(wf_hit.second);
151 
152  //Iterate position in the data
153  data_pos += event_header->length;
154  ++iP;
155  }
156  if (fDebug)
157  std::cout << "Iterated through " << iP << " packets " << std::endl;
158  }
159  H5Gclose(region_group);
160  }
161  H5Gclose(PDS_group);
162 
163  e.put(std::move(output_wfs), fOutputDataLabel);
164  e.put(std::move(output_hits), fOutputDataLabel);
165 }
166 
167 std::pair<raw::OpDetWaveform, recob::OpHit>
169  const dunedaq::detdataformats::ssp::EventHeader * event_header,
170  size_t data_pos, uint8_t * data_ptr) {
171 
172  using namespace dunedaq::detdataformats::ssp;
173  size_t nADC = (event_header->length - sizeof(EventHeader))/2;
174  if (fDebug) std::cout << "\tnADC: " << nADC << std::endl;
175  unsigned long ts = 0;
176  for (unsigned int iword = 0; iword <= 3; ++iword) {
177  ts += ((unsigned long)(event_header->timestamp[iword])) << 16 * iword;
178  }
179 
180  //Need time, channel
181  raw::OpDetWaveform wf(ts, event_header->group2, nADC);
182 
183  //Iterate to the start of the adc data
184  unsigned short * adc_ptr = reinterpret_cast<unsigned short *>(
185  data_ptr + data_pos + sizeof(EventHeader));
186 
187  //size_t peak_tick = 0;
188  //unsigned short max_adc = 0;
189 
190  for (size_t i = 0; i < nADC; ++i) {
191  wf.push_back(*(adc_ptr + i));
192  //max_adc = std::max(max_adc, *(adc_ptr + i));
193  //if(max_adc == *(adc_ptr + i)) peak_tick = i;
194  }
195  //std::cout << "Max adc, peak tick: " << max_adc << ", " << peak_tick <<
196  // std::endl;
197 
198  return {wf, recob::OpHit()};
199 }
200 
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
VDColdboxPDSDecoder & operator=(VDColdboxPDSDecoder const &)=delete
QCString file_name
std::list< std::string > getMidLevelGroupNames(hid_t grp)
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
std::void_t< T > n
def move(depos, offset)
Definition: depos.py:107
p
Definition: test.py:223
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
std::pair< raw::OpDetWaveform, recob::OpHit > MakeWaveformAndHit(const dunedaq::detdataformats::ssp::EventHeader *event_header, size_t data_pos, uint8_t *data_ptr)
void produce(art::Event &e) override
hid_t getGroupFromPath(HDFFileInfoPtr &hdfFileInfoPtr, const std::string &path)
QTextStream & endl(QTextStream &s)
VDColdboxPDSDecoder(fhicl::ParameterSet const &p)