OnlineUnpack.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file OnlineUnpack.cxx
3 /// \brief Unpack the online data to DDT format
4 ///
5 /// \author janzirn@fnal.gov
6 ////////////////////////////////////////////////////////////////////////
7 #include "DDTCore/OnlineUnpack.h"
9 
10 #include "DAQDataFormats/RawTrigger.h"
11 #include "DAQDataFormats/RawTriggerHeader.h"
12 #include "DAQDataFormats/RawTriggerTime.h"
13 #include "DAQDataFormats/RawTriggerMask.h"
14 #include "DAQDataFormats/RawTriggerTimingMarker.h"
15 #include "DAQDataFormats/RawDataBlock.h"
16 #include "DAQDataFormats/RawMicroBlock.h"
17 #include "RawFileParser/RawFileParser.h"
18 #include "NovaDAQConventions/DAQConventions.h"
19 #include "DAQChannelMap/DAQChannelMap.h"
20 
24 
25 #include <iostream>
26 #include <boost/algorithm/string.hpp>
27 
28 namespace novaddt{
29 
30 //-----------------------------------------------------------------------------
31 //Constructor
33  fFilterCorruptedNanoslices (false)
34  ,fDetId (novadaq::cnv::kNEARDET)
35  ,fNumberOfNonemptyMicroblocks(0)
36  ,fHitNumber (0)
37  ,fCurrentFileName (file_name)
38 {
39 }
40 
41 //-----------------------------------------------------------------------------
42 //Destructor
44 {
45 }
46 
47 //-----------------------------------------------------------------------------
49  std::vector<novaddt::DAQHit>* hits,
50  novaddt::EventHeader* evt_header){
51 
52  // First set the pointer to the raw Buffer.
53  fRawEvent.setBufferSource(buffer);
54 
55  return ProcessRawEvent_(hits, evt_header);
56 }
57 
58 //-----------------------------------------------------------------------------
60  std::vector<novaddt::DAQHit>* hits,
61  novaddt::EventHeader* evt_header){
62  errno = 0; // Currently our only way of distinguishing EOF from read failure.
63  const bool result = parser.getEvent(fRawEvent);
64  if (errno) {
66  << "Read error attempting to obtain next raw event:"
67  << strerror(errno) << ".\n";
68  }
69  return result?ProcessRawEvent_(hits,evt_header):false;
70 }
71 
72 //-----------------------------------------------------------------------------
73 bool OnlineUnpack::ProcessRawEvent_(std::vector<novaddt::DAQHit>* hits,
74  novaddt::EventHeader* evt_header){
75 
76  // This will give us access to accurate TDC and ADC info
78 
79  /// Variable that is saying whether there was at least one Microblock
80  /// We are getting detector ID from microblocks. If there is none, we'll take it from Configuration Block
82 
83  /// Checking for the markers
84  if ( fRawEvent.getHeaderMarkerLeft() != daqdataformats::raweventhdr::Marker_value[0]
85  || fRawEvent.getHeaderMarkerRight() != daqdataformats::raweventhdr::Marker_value[1]){
86  std::cout<<"Event Header markers are wrong\n";
87 
88  return false;
89  }
90 
91  /// Trigger pointer
92  daqdataformats::RawTrigger* trigger = fRawEvent.getTrigger();
93 
94 
95  /// Checking Trigger markers
96 #ifdef DAQDATAFORMATS_VERSIONING_IS_IMPLEMENTED
97  if ( trigger->getTriggerHeader()->getDelimiter() != daqdataformats::triggerheader::TriggerHeaderDelimiter ){
98 #else
99  if ( trigger->getTriggerHeader()->getDelimiter() != daqdataformats::TriggerHeaderDelimiter ){
100 #endif
101 
102  std::cout<<"Trigger Header markers are wrong\n";
103  return false;
104  }
105  // Now the unpacking...
106 
107  /// Filling Trigger stuff
108  fUnpackedEventParameters.fTriggerHeader_Source = trigger->getTriggerHeader()->getSource();
109  fUnpackedEventParameters.fTriggerHeader_SourceID = trigger->getTriggerHeader()->getSourceID();
110  fUnpackedEventParameters.fTriggerHeader_SourceSubID = trigger->getTriggerHeader()->getSourceSubID();
111  fUnpackedEventParameters.fTriggerHeader_TriggerNumber = trigger->getTriggerHeader()->getTriggerNumber();
112  fUnpackedEventParameters.fTriggerHeader_MasterTriggerNumber = trigger->getTriggerHeader()->getMasterTriggerNumber();
113  fUnpackedEventParameters.fTriggerTime_GenTime = trigger->getTriggerTime()->getTriggerTimeStamp();
114  fUnpackedEventParameters.fTriggerTime_ClockSource = trigger->getTriggerTime()->getClockSource();
115  fUnpackedEventParameters.fTriggerTimingMarker_TimeStart = trigger->getTriggerTimingMarker()->getTriggerStart_baseClock();
116  fUnpackedEventParameters.fTriggerTimingMarker_ExtractionStart = trigger->getTriggerTimingMarker()->getTriggerStart_833kHz();
117  fUnpackedEventParameters.fTriggerRange_TriggerLength = trigger->getTriggerRange()->getTriggerLength();
118  fUnpackedEventParameters.fTriggerMask_MCBit = trigger->getTriggerMask()->getMonteCarloFlag();
119  fUnpackedEventParameters.fTriggerMask_TMod = trigger->getTriggerMask()->getTrigMod();
120  fUnpackedEventParameters.fTriggerMask_TriggerTypesLow = trigger->getTriggerMask()->getTrigMask_Low();
121  fUnpackedEventParameters.fTriggerMask_TriggerTypesHigh = trigger->getTriggerMask()->getTrigMask_High();
122  fUnpackedEventParameters.fTriggerMask_TriggerTypesHigh2 = trigger->getTriggerMask()->getTrigMask_High2();
123  fUnpackedEventParameters.fTriggerMask_Prescale = trigger->getTriggerMask()->getPrescale();
124 
126 
127  /// Loop over DataBlocks
128  const uint32_t n_datablocks = fRawEvent.getDataBlockNumber();
129 
130  /// Counter for total number of microslices in the event
131  uint32_t total_microslices=0;
132 
133  uint64_t eventStart = UINT64_MAX;
134 
135  for(uint32_t idatablock = 0; idatablock < n_datablocks; ++idatablock){
136 
137  // Set the datablock
138  fRawEvent.setFloatingDataBlock(idatablock);
139  daqdataformats::RawDataBlock* datablock = fRawEvent.getFloatingDataBlock();
140 
141  /// Checking DataBlock markers
142 #ifdef DAQDATAFORMATS_VERSIONING_IS_IMPLEMENTED
143  if ( datablock->getHeader()->getMarker() != daqdataformats::datablockheader::DataBlock_Marker ){
144 #else
145  if ( datablock->getHeader()->getMarker() != daqdataformats::datablock::DataBlock_Marker ){
146 #endif
147  std::cout<<"DataBlock Header markers are wrong\n";
148  return false;
149  }// end of checking DataBlock markers
150 
151  /// Loop over the MicroBlocks
152  const uint32_t n_microblocks = datablock->getNumMicroBlocks();
153 
154  for(uint32_t imicro = 0; imicro < n_microblocks; ++imicro){
155 
156  /// Set the microblock
157  datablock->setFloatingMicroBlock(imicro);
158 
159  daqdataformats::RawMicroBlock* microblock = (daqdataformats::RawMicroBlock*)datablock->getFloatingMicroBlock();
160  daqdataformats::RawMicroSlice* microslice = (daqdataformats::RawMicroSlice*)microblock->getMicroSlice();
161 
162  /// Doing various checks
163  /// 1. Cannot be less than 8
164  if(microslice->getHeader()->getByteCount() < 8) return false;
165 
166 #ifndef DAQDATAFORMATS_VERSIONING_IS_IMPLEMENTED
167  /// 2. Has to be dividable by 4 if nanoslice is version 0
168  if(microslice->getHeader()->getByteCount() % 4) return false;
169 #endif
170  /// 3. Cannot be empty
171  if(!microslice->getHeader()->getDataPresent() && microslice->getHeader()->getByteCount()!= 8) return false;
172 
173  /// Increment the total number of valid microslices in the event
174  total_microslices++;
175 
176  /// Get the Detector and a DAQChannelMap
177  fDetId = (int)microblock->getHeader()->getDetId();
178  //daqchannelmap::DAQChannelMap* dMap = daqchannelmap::DAQChannelMap::getInstance(fDetId);
180 
181  /// Increment fNumberOfNonemptyMicroblocks
183 
184  /// Diblock number
185  const uint32_t diblock = microblock->getHeader()->getDiblockId();
186  /// DCM number in the diblock
187  const uint32_t dcmid = microblock->getHeader()->getDCMId();
188 
189  /// Get the time and compare to event start time
190  uint64_t tmpTime = microslice->getTime();
191  if(tmpTime < eventStart){
192  eventStart = tmpTime;
193  }
194 
196 
197 
198  /// Loop over NanoSlices
199  const uint32_t n_nanoslices = microslice->getNumNanoSlices();
200  for(uint32_t inano = 0; inano < n_nanoslices; ++inano){
201 
202  /// Set the nanoslice
203  microslice->setFloatingNanoSlice(inano);
204 
207 
208 
209  /// See if we need to filter the nanoslice
211  /// NanoSlice has to have DataPresent Flag
212  if(!nanoslice_header->getDataPresent()) continue;
213  }// end of filtering
214 
215 
216  /// DCM link number for the FEB
217  const uint32_t dcm_link = nanoslice_header->getFEBId();
218  /// Pixel number
219  const uint32_t pixel = nanoslice_header->getPixelAddr();
220  /// Get the daq channel for the hit
221  const daqchannelmap::dchan daq_channel = dMap->encodeDChan(fDetId, diblock, dcmid, dcm_link, pixel);
222 
223  try{
224  // DAQChannelMap is fixed to correctly map DAQ to the Offline, at least
225  // for the first 4 blocks of any detector!! A further fix is being done
226  // for the AA DiBlock interfaces that are present beyond this.
227  dMap->getDetector(daq_channel);
228  dMap->getDiBlock(daq_channel);
229  dMap->getDCM(daq_channel);
230  dMap->getFEB(daq_channel);
231  dMap->getPixel(daq_channel);
232  dMap->getView(daq_channel);
233  }
234  catch(...){
235  mf::LogError("BadDAQChannel") << "we have a bad DAQChannel " << daq_channel
236  << " don't make a digit from it";
237  continue;
238  }
239 
240  /// Hit time and charge
241  wpr->setup(microslice, nanoslice);
242  novaddt::ADC ADC = wpr->getADC();
243  novaddt::TDC TDC = wpr->getTDC();
244 
245  /// Push DAQHits
246  fHit = novaddt::DAQHit(fDetId, daq_channel, ADC, TDC);
247  hits->push_back(fHit);
248 
249  /// Advance the hit number
250  ++fHitNumber;
251  }// end of loop over nanoslices
252 
253  //std::cout<<"end micro "<<imicro<<"\n";
254  }// end of loop over microblocks
255  }// end of loop over datablocks
256 
257  /// Bug in Datalogger that has detector ID in Configuration block different than in Microblocks
258  /// Try to figure out the detector from the file name
261  std::cout<<"From "<<__PRETTY_FUNCTION__<<":"<<__LINE__;
262  std::cout<<" Warning!!! Could not get the detector ID.";
263  std::cout<<" The empty event "<<fRawEvent.getEventNumber();
264  std::cout<<" will be marked to have DetectorID="<<fDetId;
265  std::cout<<"("<<novadaq::cnv::DetInfo::GetName(fDetId)<<")\n";
266  }
267  }// end of trying to figure out the detector
268 
269  /// Now filling novaddt::EventHeader (maybe...)
270 
271  evt_header->totalBytes = fRawEvent.getEventSize();
272  evt_header->eventNumber = fRawEvent.getEventNumber();
273  evt_header->datablockCount = fRawEvent.getDataBlockNumber();
274  evt_header->missingData = fRawEvent.isEventIncomplete();
275  evt_header->timeStart = eventStart;
276 
277  return true;
278 }// end of ProcessRawEvent_
279 
280 
281 //------------------------------------------------------------------------------
282 // Try to get Detector ID from the file name
284 
285  if(!file_name) return false;
286 
287  if (boost::find_first(*file_name, "ndos") || boost::find_first(*file_name, "NDOS")){
289  }
290  else if(boost::find_first(*file_name, "fardet") || boost::find_first(*file_name, "FarDet")){
292  }
293  else if(boost::find_first(*file_name, "neardet") || boost::find_first(*file_name, "NearDet")){
295  }
296 
297  return true;
298 }
299 
300 
301 }//end namespace
302 
static std::string GetName(int id)
uint64_t fTriggerTime_GenTime
Definition: OnlineUnpack.h:32
uint32_t fTriggerHeader_Source
Definition: OnlineUnpack.h:27
diblock
print "ROW IS " print row
Definition: geo2elec.py:31
IMPLEMENT_MAIN_STANDARD IMPLEMENT_MAIN_setBufferSource daqdataformats::RawNanoSliceHeader * getHeader() const
Get the NanoSlice header pointer.
Definition: RawNanoSlice.h:57
uint32_t fTriggerMask_TriggerTypesHigh
Definition: OnlineUnpack.h:41
novaddt::DAQHit fHit
Instance of DAQHit class.
Definition: OnlineUnpack.h:81
const std::string * fCurrentFileName
Current file name.
Definition: OnlineUnpack.h:84
uint32_t getNumNanoSlices() const
Get current Hit Probability (for a microslice generation)
int fNumberOfHits
! Detector type
Definition: OnlineUnpack.h:46
UnpackedEvent fUnpackedEventParameters
Definition: OnlineUnpack.h:82
uint32_t fTriggerRange_TriggerLength
Definition: OnlineUnpack.h:37
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
uint32_t fTriggerMask_TriggerTypesHigh2
Definition: OnlineUnpack.h:42
uint32_t fTriggerMask_Prescale
Definition: OnlineUnpack.h:43
uint32_t datablockCount
Definition: EventHeader.h:26
::xsd::cxx::tree::buffer< char > buffer
Definition: Database.h:179
Class to hold the data from the global trigger in the correct format.
Definition: RawTrigger.h:40
uint64_t getTime() const
Get method for upper half of timing word.
uint32_t fTriggerMask_TriggerTypesLow
Definition: OnlineUnpack.h:40
Far Detector at Ash River, MN.
int fDetId
Detector ID reading from Microblocks.
Definition: OnlineUnpack.h:78
void hits()
Definition: readHits.C:15
bool setFloatingNanoSlice(const uint32_t inano) const
Set the SIM flag.
static DAQChannelMap * getInstance(int detID)
bool ProcessRawEvent_(std::vector< novaddt::DAQHit > *, novaddt::EventHeader *)
daqdataformats::RawMicroSliceHeader * getHeader() const
Prototype Near Detector on the surface at FNAL.
uint64_t fTriggerTimingMarker_ExtractionStart
Definition: OnlineUnpack.h:35
Near Detector in the NuMI cavern.
uint64_t fTriggerHeader_MasterTriggerNumber
! Universal trigger number
Definition: OnlineUnpack.h:31
~OnlineUnpack()
Destructor.
uint32_t fTriggerTime_ClockSource
Definition: OnlineUnpack.h:33
void setup(daqdataformats::RawMicroSlice *theMicro, daqdataformats::RawNanoSlice *theNano)
OStream cout
Definition: OStream.cxx:6
Service for extracting timing and pulse height information from traces with multipoint readout...
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
bool ProcessRawEvent(void *rawevent_buffer, std::vector< novaddt::DAQHit > *, novaddt::EventHeader *)
Unpack the RawEvent.
Class to hold the MicroSlice data.
Definition: RawMicroSlice.h:48
uint64_t fTriggerHeader_TriggerNumber
! Identifier of the subsystem or channel on source device
Definition: OnlineUnpack.h:30
bool fFilterCorruptedNanoslices
Do we need to filter out Corrupted NanoSlices?
Definition: OnlineUnpack.h:69
uint32_t fTriggerHeader_SourceID
! Trigger source (see DAQDataFormats/TriggerDefines.h)
Definition: OnlineUnpack.h:28
uint64_t fTriggerTimingMarker_TimeStart
Definition: OnlineUnpack.h:34
int getEvent(daqdataformats::RawEvent &theEvent)
Retrieve the next event from the file.
uint32_t fTriggerHeader_SourceSubID
! Identifier of the source system
Definition: OnlineUnpack.h:29
uint32_t dchan
< DAQ Channel Map Package
bool getDetIDFromFileName(const std::string *)
Try to get Detector ID from the file name.
daqdataformats::RawEvent fRawEvent
Flat DAQ RawEvent.
Definition: OnlineUnpack.h:80
int errno
Definition: errno.cpp:12
OnlineUnpack(const std::string *file_name=nullptr)
Constructor.
uint32_t fNumberOfNonemptyMicroblocks
Definition: OnlineUnpack.h:79
daqdataformats::RawNanoSlice * getFloatingNanoSlice() const
Get the MicroSlice Timing Marker.
enum BeamMode string