FelixRawDecoder_module.cc
Go to the documentation of this file.
1 // art includes
11 #include "art_root_io/TFileService.h"
12 
13 // artdaq and dune-raw-data includes
15 #include "artdaq-core/Data/Fragment.hh"
16 #include "artdaq-core/Data/ContainerFragment.hh"
19 
20 // larsoft includes
22 
23 // ROOT includes
24 #include "TH1.h"
25 
26 // C++ Includes
27 #include <memory>
28 #include <iostream>
29 
30 namespace dune {
31  class FelixRawDecoder;
32 }
33 
35 public:
36  explicit FelixRawDecoder(fhicl::ParameterSet const & p);
37  // The destructor generated by the compiler is fine for classes
38  // without bare pointers or other resoufelix use.
39 
40  // Plugins should not be copied or assigned.
41  FelixRawDecoder(FelixRawDecoder const &) = delete;
42  FelixRawDecoder(FelixRawDecoder &&) = delete;
43  FelixRawDecoder & operator = (FelixRawDecoder const &) = delete;
45 
46  // Required functions.
47  void produce(art::Event & e) override;
48  void reconfigure(const fhicl::ParameterSet &pset);
49  void beginJob() override;
50 
51  void setRootObjects();
52 
53 private:
54  typedef std::vector<raw::RawDigit> RawDigits;
55 
56  bool _process(
57  const artdaq::Fragment& frag,
58  RawDigits& raw_digits);
59 
60  // Declare member data here.
64  TH1D* _h_nframes;
65 
66  std::vector<uint16_t> _buffer;
67 };
68 
69 
71  : EDProducer{pset}
72 // Initialize member data here.
73 {
75  //fs->registerFileSwitchCallback(this, &FelixRawDecoder::setRootObjects);
77 
78  reconfigure(pset);
79  // Call appropriate produces<>() functions here.
80  //produces< std::vector<raw::RawDigit> > ( _output_label );
81  produces<RawDigits>( _output_label );
82 }
83 
85 
86  _input_label = pset.get<std::string>("RawDataLabel");
87  _output_label = pset.get<std::string>("OutputDataLabel");
88  _expect_container_fragments = pset.get<bool>("ExpectContainerFragments", true);
89 }
90 
93  _h_nframes = file_srv->make<TH1D>("felix_NFrames","FELIX: Number of frames", 1000, 0, 1000);
94 }
96 }
97 
99  // TODO Use MF_LOG_DEBUG
100  MF_LOG_INFO("FelixRawDecoder")
101  << "-------------------- FELIX RawDecoder -------------------";
102 
103  RawDigits raw_digits;
104  unsigned int n_felix_frags = 0;
105 
107  art::InputTag itag1(_input_label, "ContainerFELIX");
108  auto cont_frags = evt.getHandle<artdaq::Fragments>(itag1);
109  art::EventNumber_t eventNumber = evt.event();
110  // Check if there is Timing data in this event
111  // Don't crash code if not present, just don't save anything
112  try { cont_frags->size(); }
113  catch(std::exception const&) {
114  std::cout << "WARNING: Container FELIX data not found in event " << eventNumber << std::endl;
115  std::vector<raw::RawDigit> digits;
116  evt.put(std::make_unique<std::vector<raw::RawDigit>>(std::move(digits)), _output_label);
117  return;
118  }
119  //Check that the data is valid
120  if(!cont_frags){
121  MF_LOG_ERROR("FelixRawDecoder")
122  << "Run: " << evt.run()
123  << ", SubRun: " << evt.subRun()
124  << ", Event: " << evt.event()
125  << " Container Fragments is NOT VALID";
126  }
127 
128  for (auto const& cont : *cont_frags)
129  {
130  artdaq::ContainerFragment cont_frag(cont);
131  for (size_t ii = 0; ii < cont_frag.block_count(); ++ii)
132  {
133  //artdaq::Fragment frag;
134  //size_t frag_size = cont_frag.fragSize(ii);
135  //frag.resizeBytes(frag_size);
136  //memcpy(frag.headerAddress(), cont_frag.at(ii), frag_size);
137  //if (_process(frag, raw_digits)) ++n_felix_frags;
138  if (_process(*cont_frag[ii], raw_digits)) ++n_felix_frags;
139  }
140  }
141  }
142  else
143  {
144  art::InputTag itag2(_input_label, "FELIX");
145  auto frags = evt.getHandle<artdaq::Fragments>(itag2);
146  // Check if there is Timing data in this event
147  // Don't crash code if not present, just don't save anything
148  art::EventNumber_t eventNumber = evt.event();
149  try { frags->size(); }
150  catch(std::exception const&) {
151  std::cout << "WARNING: Raw FELIX data not found in event " << eventNumber << std::endl;
152  std::vector<raw::RawDigit> digits;
153  evt.put(std::make_unique<std::vector<raw::RawDigit>>(std::move(digits)), _output_label);
154  return;
155  }
156 
157  //Check that the data is valid
158  if(!frags.isValid()){
159  MF_LOG_ERROR("FelixRawDecoder")
160  << "Run: " << evt.run()
161  << ", SubRun: " << evt.subRun()
162  << ", Event: " << evt.event()
163  << " Fragments is NOT VALID";
164  }
165 
166  for(auto const& frag: *frags)
167  {
168  if (_process(frag, raw_digits)) ++n_felix_frags;
169  }
170  }
171 
172  MF_LOG_INFO("FelixRawDecoder")
173  << " Processed " << n_felix_frags
174  << " FELIX Fragments, "
175  << raw_digits.size()
176  << " RawDigits.";
177 
178  evt.put(std::make_unique<decltype(raw_digits)>(std::move(raw_digits)),
179  _output_label);
180 }
181 
182 //FIXME: need to re-write this function using Felix overlay class
184  const artdaq::Fragment& frag,
185  RawDigits& raw_digits
186  )
187 {
188  // FIXME: Remove hard-coded fragment type
189  //if((unsigned)frag.type() != 2) return false;
190 
191  MF_LOG_INFO("FelixRawDecoder")
192  << " SequenceID = " << frag.sequenceID()
193  << " fragmentID = " << frag.fragmentID()
194  << " fragmentType = " << (unsigned)frag.type()
195  << " Timestamp = " << frag.timestamp();
197  //Load overlay class.
198  dune::FelixFragment felix(frag);
199  //Get detector elemen number
200  uint8_t crate = felix.crate_no(0);
201  uint8_t slot = felix.slot_no(0);
202  uint8_t fiber = felix.fiber_no(0); // two numbers?
203  const unsigned n_frames = felix.total_frames(); // One frame contains 20 ticks.
204  std::cout<<" Nframes = "<<n_frames<<std::endl;
205  _h_nframes->Fill(n_frames);
206  const unsigned n_channels = dune::FelixFrame::num_ch_per_frame;// 256
208  //v_adc.reserve(n_frames*n_channels);
209  // Fill the adc vector.
210  //typedef std::tuple<uint8_t, uint8_t, uint8_t, unsigned> WireInfo_tuple; // unused
211  for(unsigned ch = 0; ch < n_channels; ++ch) {
212  v_adc.clear();
213  std::cout<<"crate:slot:fiber = "<<crate<<", "<<slot<<", "<<fiber<<std::endl;
214  std::vector<dune::adc_t> waveform( felix.get_ADCs_by_channel(ch) );
215  for(unsigned int nframe=0;nframe<waveform.size();nframe++){
216  if(ch==0 && nframe<100) {
217  if(nframe==0) std::cout<<"Print the first 100 ADCs of Channel#1"<<std::endl;
218  std::cout<<waveform.at(nframe)<<" ";
219  if(nframe==99) std::cout<<std::endl;
220  }
221  v_adc.push_back(waveform.at(nframe));
222  }
223  int offlineChannel = -1;
224  offlineChannel = channelMap->GetOfflineNumberFromDetectorElements(crate, slot, fiber, ch,dune::PdspChannelMapService::kFELIX); // FIXME
225  // Push to raw_digits.
226  raw::RawDigit raw_digit(offlineChannel, n_frames, v_adc);
227  raw_digits.push_back(raw_digit);
228  }
229  return true;
230 }
231 
static constexpr size_t num_ch_per_frame
Definition: FelixFormat.hh:298
size_t total_frames() const
std::vector< uint16_t > _buffer
EventNumber_t event() const
Definition: DataViewImpl.cc:85
Collection of charge vs time digitized from a single readout channel.
Definition: RawDigit.h:69
uint8_t slot_no(const unsigned &frame_ID=0) const
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
uint8_t crate_no(const unsigned &frame_ID=0) const
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
static constexpr double fs
Definition: Units.h:100
#define MF_LOG_ERROR(category)
std::vector< short > ADCvector_t
Type representing a (compressed) vector of ADC counts.
Definition: RawDigit.h:73
FelixRawDecoder(fhicl::ParameterSet const &p)
unsigned int GetOfflineNumberFromDetectorElements(unsigned int crate, unsigned int slot, unsigned int fiber, unsigned int fembchannel, FelixOrRCE frswitch)
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
struct dune::tde::crate crate
def move(depos, offset)
Definition: depos.py:107
T get(std::string const &key) const
Definition: ParameterSet.h:271
p
Definition: test.py:223
SubRunNumber_t subRun() const
Definition: DataViewImpl.cc:78
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
RunNumber_t run() const
Definition: DataViewImpl.cc:71
#define MF_LOG_INFO(category)
void produce(art::Event &e) override
uint8_t fiber_no(const unsigned &frame_ID=0) const
adc_v get_ADCs_by_channel(const uint8_t channel_ID) const
bool _process(const artdaq::Fragment &frag, RawDigits &raw_digits)
IDNumber_t< Level::Event > EventNumber_t
Definition: IDNumber.h:118
void reconfigure(const fhicl::ParameterSet &pset)
TCEvent evt
Definition: DataStructs.cxx:7
FelixRawDecoder & operator=(FelixRawDecoder const &)=delete
std::vector< Fragment > Fragments
Definition: HDF5Utils.h:57
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)
std::vector< raw::RawDigit > RawDigits