RawData311InputDriver_service.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////
2 // file: RawData311InputDriver.cc
3 //
4 // brief Source to convert raw binary file from 3x1x1 to root file useful to LArSoft
5 // Adapted from LarRawInputDriverShortBo.cc
6 //
7 // Created: April 20th 2017, Last Modified:
8 // Author: Kevin Fusshoeller, kevin.fusshoeller@cern.ch
9 ////////////////////////////////////////////////////////////////////////////
10 
11 #include "RawData311InputDriver.h"
13 
15 
20 
21 #include "EventDecoder.h"
22 #include "dlardaq.h"
23 
24 #include <iostream>
25 #include <ios>
26 
27 // ---------------------------------------------------------------------------------------
28 // 311 DAQ interface
29 
30 namespace lris
31 {
32  void SplitAdc(const std::vector<dlardaq::adc16_t> *adc, size_t channel, uint32_t num_samples,
33  std::vector<short> &adclist)
34  {
35  for(uint32_t i = 0; i < num_samples; i++)
36  {
37  adclist.emplace_back(adc->at(channel*num_samples + i));
38  }
39  }// SplitAdc
40 
41 
42  //-------------------------------
43  size_t Get311Chan(size_t LAr_chan){
44  size_t crate = LAr_chan / 320;
45  size_t Chan311;
46 
47  LAr_chan = 8*(LAr_chan/8+1)-LAr_chan%8 -1;
48 
49  if(crate == 0)
50  {
51  LAr_chan = 32*(LAr_chan/32+1)-LAr_chan%32 -1;
52  size_t card = 4 - ((LAr_chan / 32) % 5);
53  if(LAr_chan > 159)
54  {
55  size_t shift = 31 - (LAr_chan % 32);
56  Chan311 = (2*card)*32 + shift;
57  }
58  else
59  {
60  size_t shift = 31 - (LAr_chan % 32);
61  Chan311 = (2*card + 1)*32 + shift;
62  }
63  }
64  else
65  {
66  size_t new_LAr_chan = LAr_chan - crate*320;
67  size_t card = ((new_LAr_chan / 32) % 5);
68  if(new_LAr_chan > 159)
69  {
70  size_t shift = new_LAr_chan % 32;
71  Chan311 = (2*card)*32 + shift;
72  }
73  else
74  {
75  size_t shift = new_LAr_chan % 32;
76  Chan311 = (2*card + 1)*32 + shift;
77  }
78  Chan311 = Chan311 + crate*320;
79  } // end of if/else statementi
80 
81  return Chan311;
82  } // Get311Chan
83 
84 
85  // ----------------------------------------------------------------------
86  //
87  // ----------------------------------------------------------------------
88 
89 
90  void ReadPedestalFile(std::string PedestalFileName, std::vector< std::pair<double, double> > &PedMap){
91  //initialize the channel-ped value map
92  std::ifstream file;
93  file.open(PedestalFileName);
94  if( !file.is_open() )
95  {
97  << "failed to open input file " << PedestalFileName << "\n";
98  }
99 
100  while(!file.eof())
101  {
102  size_t ch, cryo, crate, rawch;
103  double mean, rms;
104  file >> rawch >> cryo >> crate >> ch >> mean >> rms;
105  PedMap.emplace_back(mean, rms);
106  }
107 
108  file.close();
109  return;
110  }//Read Pedestal File()
111 
112 
113  // ---------------------------------------------------------------------
114  //
115  // ---------------------------------------------------------------------
116 
117 
118  void RawData311InputDriver::process_Event311(std::vector<raw::RawDigit>& digitList,
119  dlardaq::evheader_t &event_head,
120  uint16_t evt_num)
121  {
122  // one digit for every wire on each plane
123  digitList.clear();
124  digitList.resize(nchannels);
125  // Get the data.
126  std::vector<dlardaq::adc16_t> ADCvec311;
127  DataDecode.GetEvent(evt_num, event_head, ADCvec311);
128  std::vector<dlardaq::adc16_t> *ADCvec311Pointer = &ADCvec311;
129  // fill the wires
130  std::vector<short> adclist;
131 
132  for(size_t LAr_chan = 0; LAr_chan < (size_t)nchannels; LAr_chan++)
133  {
134  adclist.clear();
135  size_t Chan311 = Get311Chan(LAr_chan);
136  SplitAdc(ADCvec311Pointer, Chan311, nsamples, adclist);
137  short unsigned int nTickReadout = nsamples;
138  raw::ChannelID_t channel = LAr_chan;
140  raw::RawDigit rd(channel, nTickReadout, adclist, comp);
141 
142  double pedval = RawData311InputDriver::GetPedMean(Chan311, &fPedMap);
143  //std::cout << "Pedval: " << pedval << "\n";
144  double pedrms = RawData311InputDriver::GetPedRMS(Chan311, &fPedMap);
145  rd.SetPedestal(pedval, pedrms);
146 
147  digitList[LAr_chan] = rd;
148  }
149  }// process_Event311
150 
151 
152  //------------------------------------------------------------------
153  // class c'tor/d'tor
156  art::SourceHelper const &pm)
157  :
158  fSourceHelper(pm),
160  fEventCounter(0),
162  {
163  fPedestalFile = p.get<std::string>("PedestalFile");
164  helper.reconstitutes<std::vector<raw::RawDigit>, art::InEvent>("daq");
165  }
166 
167 
168  // Close File.
170  {
171  mf::LogInfo(__FUNCTION__)<<"File boundary: processed " <<fEventCounter <<" events out of " <<fNEvents <<"\n";
172  DataDecode.m_file.close();
173  }
174 
175 
176  // Read File.
178  art::FileBlock* &fb)
179  {
180  // Read in the pedestal file
182 
183  filename = name;
184  // Fill and return a new Fileblock
185  fb = new art::FileBlock(art::FileFormatVersion(1, "311 RawInput 2017"), name);
186 
187  // Prepare the EventDecoder
188  //short nchannels_Vplane = 320;
189  //short nchannels_Zplane = 960;
190  //short nchannels = nchannels_Vplane + nchannels_Zplane;
191  //uint32_t nsamples = 1667;
192  //DataDecode(nchannels, nsamples);
193 
194  DataDecode.m_file.open(name.c_str(), std::ios_base::in | std::ios_base::binary);
195  if( !DataDecode.m_file.is_open() )
196  {
198  << "failed to open input file " << name << "\n";
199  }
200 
201  // Read in the file header.
202  std::vector<dlardaq::BYTE> head_buf;
203  head_buf.resize(dlardaq::RunHeadSz);
204 
205  DataDecode.m_file.read(&head_buf[0], head_buf.size());
206  dlardaq::decode_runhead(&head_buf[0], file_head);
207 
208  // Define start of event data.
209  std::streampos data_start = DataDecode.m_file.tellg();
210 
211  // Read in the file footer.
212  std::vector<dlardaq::BYTE> foot_buf;
213  foot_buf.resize(dlardaq::FileFootSz);
214 
216  DataDecode.m_file.read(&foot_buf[0], foot_buf.size());
217  dlardaq::decode_filefoot(&foot_buf[0], file_foot);
218  DataDecode.m_file.seekg(data_start);
219 
221  if(fNEvents > 0 && fNEvents < 1000) //There should be 335 events at most in one file.
222  {
223  mf::LogInfo("")<<"Opened file " <<name <<" with " << fNEvents <<" events." <<"\n";
224  } else
225  {
227  <<"File " <<name <<" seems to have too many events: " <<fNEvents <<"\n";
228  }
229  }
230 
231 
232  // Read event.
234  art::SubRunPrincipal* const &/*inSR*/,
235  art::RunPrincipal* &outR,
236  art::SubRunPrincipal* &outSR,
237  art::EventPrincipal* &outE)
238  {
239  if(fEventCounter == fNEvents)
240  {
241  mf::LogInfo(__FUNCTION__)<<"All the files have been read in. Checking end of file..." <<"\n";
242  std::streampos current_position = DataDecode.m_file.tellg();
243  DataDecode.m_file.seekg(0, std::ios::end);
244  std::streampos file_length = DataDecode.m_file.tellg();
245  if( ((uint8_t)file_length - (uint8_t)current_position) > (uint8_t)100 )
246  {
248  <<"Processed " <<fEventCounter <<" events out of " <<fNEvents <<" but there are still "
249  <<(file_length - current_position) <<" bits left." <<"\n";
250  }
251  mf::LogInfo(__FUNCTION__)<<"Completed reading file and closing output file." <<"\n";
252  return false; //Tells readNext that all events have been read.
253  }
254 
255  mf::LogInfo(__FUNCTION__)<<"Reading event " << fEventCounter << " from " << fNEvents <<"\n";
256 
257  // Create empty result, then fill it from current file
258  dlardaq::evheader_t event_head;
259  std::unique_ptr< std::vector<raw::RawDigit> > tpc_raw_digits( new std::vector<raw::RawDigit> );
260  process_Event311(*tpc_raw_digits, event_head, fEventCounter++);
261 
262 
263 
264  // Prepare some stuff
265  art::RunNumber_t rn;
266  rn = file_head.run_num;
267 
268  std::uint64_t tthi = event_head.trig_info.ts.tv_sec;
269  std::uint64_t thilo = (tthi << 32) + event_head.trig_info.ts.tv_nsec;
270  art::Timestamp tstamp(thilo);
271 
272  size_t pos = filename.find("-");
273  size_t end = filename.find(".");
274  std::string str_subrn = filename.substr(pos+1, end-1);
275  int int_subrn = std::stoi(str_subrn);
276  art::SubRunNumber_t subrn = int_subrn;
277  art::SubRunID newID(rn, subrn);
278  if( fCurrentSubRunID.runID() != newID.runID() )
279  {
280  outR = fSourceHelper.makeRunPrincipal(rn, tstamp);
281  }
282  if( fCurrentSubRunID != newID )
283  {
284  outSR = fSourceHelper.makeSubRunPrincipal(rn, subrn, tstamp);
285  fCurrentSubRunID = newID;
286  }
287 
289  fEventCounter - 1, tstamp);
290 
291  // Put products in the event.
292  art::put_product_in_principal(std::move(tpc_raw_digits), *outE, "daq");
293 
294  return true;
295  }
296 
297 
298 } // namespace lris
static QCString name
Definition: declinfo.cpp:673
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
RawData311InputDriver(fhicl::ParameterSet const &pset, art::ProductRegistryHelper &helper, art::SourceHelper const &pm)
enum raw::_compress Compress_t
Collection of charge vs time digitized from a single readout channel.
Definition: RawDigit.h:69
static const size_t FileFootSz
Definition: dlardaq.h:60
trigger_t trig_info
Definition: dlardaq.h:83
void readFile(std::string const &name, art::FileBlock *&fb)
double rms(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:40
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
double GetPedRMS(size_t LAr_chan, std::vector< std::pair< double, double > > *fPedMap)
struct vector vector
SubRunPrincipal * makeSubRunPrincipal(SubRunAuxiliary const &subRunAux) const
int16_t adc
Definition: CRTFragment.hh:202
uint8_t channel
Definition: CRTFragment.hh:201
dlardaq::EventDecoder DataDecode
ssize_t decode_runhead(const char *buf, runheader_t &rh)
void ReadPedestalFile(std::string PedestalFileName, std::vector< std::pair< double, double > > &PedMap)
static const size_t RunHeadSz
Definition: dlardaq.h:56
no compression
Definition: RawTypes.h:9
RunPrincipal * makeRunPrincipal(RunAuxiliary const &runAux) const
Definition: SourceHelper.cc:92
art framework interface to geometry description
std::vector< std::pair< double, double > > fPedMap
art::SourceHelper const & fSourceHelper
double GetPedMean(size_t LAr_chan, std::vector< std::pair< double, double > > *fPedMap)
TypeLabel const & reconstitutes(std::string const &modLabel, std::string const &instanceName={})
RunID const & runID() const
Definition: SubRunID.h:79
std::ifstream m_file
Definition: EventDecoder.h:64
struct dune::tde::crate crate
def move(depos, offset)
Definition: depos.py:107
RunNumber_t run() const
Definition: SubRunID.h:85
T get(std::string const &key) const
Definition: ParameterSet.h:271
IDNumber_t< Level::SubRun > SubRunNumber_t
Definition: IDNumber.h:119
p
Definition: test.py:223
size_t Get311Chan(size_t LAr_chan)
ssize_t decode_filefoot(const char *buf, footer_t &rf)
ssize_t GetEvent(size_t evnum, dlardaq::evheader_t &eh, std::vector< adc16_t > &adc)
void process_Event311(std::vector< raw::RawDigit > &digitList, dlardaq::evheader_t &event_head, uint16_t evt_num)
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::enable_if_t<!detail::range_sets_supported(P::branch_type)> put_product_in_principal(std::unique_ptr< T > &&product, P &principal, std::string const &module_label, std::string const &instance_name={})
void SetPedestal(float ped, float sigma=1.)
Set pedestal and its RMS (the latter is 0 by default)
Definition: RawDigit.cxx:68
Conversion of binary data to root files.
EventPrincipal * makeEventPrincipal(EventAuxiliary const &eventAux, std::unique_ptr< History > &&history) const
bool readNext(art::RunPrincipal *const &inR, art::SubRunPrincipal *const &inSR, art::RunPrincipal *&outR, art::SubRunPrincipal *&outSR, art::EventPrincipal *&outE)
SubRunNumber_t subRun() const
Definition: SubRunID.h:91
struct timespec ts
Definition: dlardaq.h:70
uint16_t num_events
Definition: dlardaq.h:92
void SplitAdc(const std::vector< dlardaq::adc16_t > *adc, size_t channel, uint32_t num_samples, std::vector< short > &adclist)
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
double mean(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:16
uint32_t run_num
Definition: dlardaq.h:76
IDNumber_t< Level::Run > RunNumber_t
Definition: IDNumber.h:120