RawTriggerV0.cpp
Go to the documentation of this file.
1 #include "DAQDataFormats/RawTriggerV0.h"
2 #include "DAQDataFormats/RawTriggerHeader.h"
3 #include "DAQDataFormats/RawTriggerTimingMarker.h"
4 #include "DAQDataFormats/RawTriggerRange.h"
5 #include "DAQDataFormats/RawTriggerTime.h"
6 #include "DAQDataFormats/RawTriggerMask.h"
7 
8 #include <boost/format.hpp>
9 
10  /// It is requred to
11 /// 1. include DAQDataFormats/DataFormatFirst.inc
12 /// 2. define CURRENT_CLASS
13 /// 3. define CURRENT_CLASS_VERSION for version of the class
14 /// OR define LAST_CLASS_VERSION for the main structure of the class
15 /// 4. (optional) define CURRENT_FIELDS_SUB_NAMESPACE
16 /// 5. include DAQDataFormats/DataFormatLast.inc
17 /// before the declaration of the DataFormat version class in the header.
18 #include "DAQDataFormats/DataFormatFirst.inc"
19 #define CURRENT_CLASS RawTrigger
20 #define CURRENT_CLASS_VERSION 0
21 #define CURRENT_FIELDS_SUB_NAMESPACE rawtrigger
22 #include "DAQDataFormats/DataFormatLast.inc"
23 
24 namespace daqdataformats{
25 namespace VERSION_NAMESPACE{
26 
27 //------------------------Constructors------------------------------------------
29  RawDAQData(version, true)
30  ,ZEROOUT_SET(TriggerHeader)
35  ,ZEROOUT_GET_AND_SET(TriggerStart)
36  ,_function_readDataFromFileDescriptor(&RawTrigger::readDataUnknownVersion)
37  ,_Header (NULL)
38  ,_Marker (NULL)
39  ,_Range (NULL)
40  ,_Time (NULL)
41  ,_Mask (NULL)
42  ,_size (1) /// Need at least size 1 to be able to set the Header pointer
43  ,_version_TriggerTimingMarker(daqdataformats::rawdaqdataformat::DAQDATAFORMAT_UNKNOWN_VERSION)
44  ,_version_TriggerRange (daqdataformats::rawdaqdataformat::DAQDATAFORMAT_UNKNOWN_VERSION)
45  ,_version_TriggerTime (daqdataformats::rawdaqdataformat::DAQDATAFORMAT_UNKNOWN_VERSION)
46  ,_version_TriggerMask (daqdataformats::rawdaqdataformat::DAQDATAFORMAT_UNKNOWN_VERSION)
47 {}
48 //------------------------------------------------------------------------------
50  RawDAQData(copyin)
51  ,DEEPCOPY_SET(TriggerHeader)
56  ,DEEPCOPY_GET_AND_SET(TriggerStart)
57  ,_function_readDataFromFileDescriptor(copyin._function_readDataFromFileDescriptor)
58  ,_Header (NULL)
59  ,_Marker (NULL)
60  ,_Range (NULL)
61  ,_Time (NULL)
62  ,_Mask (NULL)
63  ,_size (copyin._size) /// Need at least size 1 to be able to set the Header pointer
68 { }
69 
70 
71 
72 
73 //------------------------Destructor--------------------------------------------
75  if(_Header){
76  delete _Header;
77  delete _Marker;
78  delete _Range;
79  delete _Time;
80  delete _Mask;
81  }
82 }
83 
84 //------------------------------------------------------------------------------
85 // Default Initialization of Data members
87 
88  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" Initializing Header");
89  _Header->init();
90  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" Initializing _Time");
91  _Time->init();
92  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" Initializing _Marker");
93  _Marker->init();
94  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" Initializing _Range");
95  _Range->init();
96  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" Initializing _Mask");
97  _Mask->init();
98  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" End of method");
99 }
100 
101 //------------------------------------------------------------------------------
102 uint64_t RawTrigger::getTriggerStart() const { return _Marker->getTriggerStart_baseClock();}
103 bool RawTrigger::setTriggerStart(const uint64_t start_time) { return _Marker->setTriggerStart_baseClock(start_time);}
104 
105 
106 //------------------------------------------------------------------------------
107 void RawTrigger::print(std::ostream& os) const{
108 
109  os<<boost::format("---RawTrigger version %d... Size=%d\n")% _version% EXECUTE_FUNCTION(sizeofdata)();
110 
111  _Header->print(os);
112 
113  _Time->print(os);
114 
115  _Marker->print(os);
116 
117  _Range->print(os);
118 
119  _Mask->print(os);
120 
121  PRINT_ON_DEBUG("Buffer is set to "<<_Buffer);
122 
123  os<<boost::format("---END OF RawTrigger version %d... Size=%d\n")% _version% EXECUTE_FUNCTION(sizeofdata)();
124 
125 }
126 
127 //------------------------------------------------------------------------------
128 bool RawTrigger::setTriggerHeader (const daqdataformats::RawTriggerHeader& header) { _Header->readData(header.getBuffer()); return true;}
129 bool RawTrigger::setTriggerTimingMarker(const daqdataformats::RawTriggerTimingMarker& marker) { _Marker->readData(marker.getBuffer()); return true;}
130 bool RawTrigger::setTriggerRange (const daqdataformats::RawTriggerRange& range ) { _Range->readData(range.getBuffer()) ; return true;}
131 bool RawTrigger::setTriggerTime (const daqdataformats::RawTriggerTime& time ) { _Time->readData(time.getBuffer()) ; return true;}
132 bool RawTrigger::setTriggerMask (const daqdataformats::RawTriggerMask& mask ) { _Mask->readData(mask.getBuffer()) ; return true;}
133 
134 //------------------------------------------------------------------------------
135 bool RawTrigger::setBufferSource(const void* pointer){
136 
137 
138  _Buffer = (void*)pointer;
139 
140  /// If Header is already defined, then set its buffer source
141  /// Otherwise, need to construct it
142  if(_Header){
143  void* pointer_new = setBufferSourceGeneral(*_Header, _Buffer);
144  pointer_new = setBufferSourceGeneral(*_Time , pointer_new);
145  pointer_new = setBufferSourceGeneral(*_Marker, pointer_new);
146  pointer_new = setBufferSourceGeneral(*_Range , pointer_new);
147  return _Mask->setBufferSource(pointer_new);
148  }
149 
150  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" initializing");
151  /// So, Header, etc. are not constructed
152 
153 
154  /// Construct header
155  _Header = new daqdataformats::RawTriggerHeader(_Buffer, _version); ///< version of the TriggerHeader equals the version of the TriggerHeader
156  _size = _Header->sizeofdata(); ///< Trigger size so far
157  PRINT_ON_DEBUG("size = "<<_size);
158  /// Adjust internal buffer. One more word is needed to consttruct the next object
159  _shouldBufferBeInternal = isInternalBuffer();
160  if(_shouldBufferBeInternal) resizeInternalBuffer(_size + 1);
161 
162  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" After initializing Header size = "<<_size);
163 
164  PRINT_ON_DEBUG("Version_trigger = "<<_version<<" "<<_version_TriggerTime);;
165 
166  /// Construct Time
168  _size += _Time->sizeofdata(); ///< Trigger size so far
169  PRINT_ON_DEBUG("size = "<<_size<<" Added "<<_Time->sizeofdata());
170  /// Adjust internal buffer. One more word is needed to consttruct the next object
171  if(_shouldBufferBeInternal) resizeInternalBuffer(_size + 1);
172 
173 
174  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" After initializing _Time size = "<<_size);
175 
176  /// Construct TriggerTimingMarker
178  _size += _Marker->sizeofdata(); ///< Trigger size so far
179  PRINT_ON_DEBUG("size = "<<_size);
180 
181  /// Adjust internal buffer. One more word is needed to consttruct the next object
182  if(_shouldBufferBeInternal) resizeInternalBuffer(_size + 1);
183  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" After initializing _Marker size = "<<_size);
184 
185  /// Construct Range
187  _size += _Range->sizeofdata(); ///< Trigger size so far
188  PRINT_ON_DEBUG("size = "<<_size);
189  /// Adjust internal buffer. One more word is needed to consttruct the next object
190  if(_shouldBufferBeInternal) resizeInternalBuffer(_size + 1);
191  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" After initializing _Range size = "<<_size);
192 
193  /// Construct Mask
195  _size += _Mask->sizeofdata(); ///< Final Trigger size
196  PRINT_ON_DEBUG("size = "<<_size);
197  /// Final adjustment of internal buffer
198  if(_shouldBufferBeInternal) resizeInternalBuffer(_size);
199  PRINT_ON_DEBUG(__PRETTY_FUNCTION__<<" After initializing _Mask size = "<<_size);
200 
201  return true;
202 }
203 
204 
205 //------------------------------------------------------------------------------
211  return true;
212 }
213 
214 
215 //------------------------------------------------------------------------------
216 // Read an entire DataBlock from datablock_buffer into _Buffer
217 void* RawTrigger::readData(const void* datablock_buffer){
218 
219  const uint32_t header_size = _Header->sizeofdata();
220 
221  //Read the header and unpack it
222  void* datablock_buffer_new = readDataGeneral(datablock_buffer, header_size, 0);
223 
224  /// Check pointers after each read or resize buffer
225  checkBufferInternalness();
226 
227  // read the rest of
228  datablock_buffer_new = readDataGeneral(datablock_buffer_new, EXECUTE_FUNCTION(sizeofdata)() - header_size, header_size);
229 
230  /// Check pointers after each read or resize buffer
231  checkBufferInternalness();
232 
233  return datablock_buffer_new;
234 }
235 
236 //------------------------------------------------------------------------------
237 // Method to read data from a PIPE (not socket).
238 // Only one read can be made. After that the pipe is closed.
239 // According to Ron Rechenmacher, the maximum size of a PIPE is 512 bytes.
240 readDataStatus RawTrigger::readDataFromFileDescriptor(const int fd)
241 {
242  if (fd < 0) return rds_error;
243 
244  /// since the version is known, the internal buffer is set accordingly
245  /// The maximum size of a pipe
246  const size_t bytes_to_read = 512;
247 
248  const size_t bytes_read = read(fd, (char*)_Buffer, bytes_to_read );
249 
250  if ((int)bytes_read==-1 || bytes_read == 0)
251  return rds_error;
252 
253  /// Cant be greater
254  if (bytes_read > bytes_to_read){
255  return rds_error;
256  }
257 
258  /// check for reasonableness
260  return rds_error;
261 
262  const uint32_t size_of_data = EXECUTE_FUNCTION(sizeofdata)();
263 
264  // at this point, the header should have been read in and the
265  // size from within can be examined.
266  if (size_of_data > 512/sizeof (uint32_t) )
267  return rds_error;
268 
269  /// check if something is wrong
270  if (((size_t)size_of_data*sizeof (uint32_t)) != bytes_read)
271  return rds_error;
272 
273  return rds_complete;
274 }
275 
276 //------------------------------------------------------------------------------
277 // Method to read data from a PIPE (not socket).
278 // Only one read can be made. After that the pipe is closed.
279 // According to Ron Rechenmacher, the maximum size of a PIPE is 512 bytes.
281 {
282 
283  if (fd < 0) return rds_error;
284 
285  /// since the version is known, the internal buffer is set accordingly
286  /// The maximum size of a pipe
287  const size_t bytes_to_read = 512;
288 
289  /// If the buffer is internal, need to resize it
290  if (isInternalBuffer()){
291  _InternalBuffer.resize(bytes_to_read / sizeof(uint32_t));
292  _Buffer = (void*)(&_InternalBuffer[0]);
293  }// end of checking if the buffer is internal
294 
295 
296  const size_t bytes_read = read(fd, (char*)_Buffer, bytes_to_read );
297 
298 
299  if ((int)bytes_read==-1 || bytes_read==0)
300  return rds_error;
301 
302  /// Cant be greater
303  if (bytes_read > bytes_to_read){
304  return rds_error;
305  }
306 
307  /// Now we can figure out the version of the Trigger
308  _version = figureOutTheVersion(_Buffer);
309 
310 
311  /// Now that we know the version, initialize function pointers
312  setFunctionPointers();
313 
314  /// Need to construct all Headers and stuff
315  setBufferSource(_Buffer);
316 
317  /// check for reasonableness
319  return rds_error;
320 
321  const uint32_t size_of_data = EXECUTE_FUNCTION(sizeofdata)();
322 
323  // at this point, the header should have been read in and the
324  // size from within can be examined.
325  if (size_of_data > 512/sizeof (uint32_t) )
326  return rds_error;
327 
328  /// check if something is wrong
329  if (((size_t)size_of_data*sizeof (uint32_t)) != bytes_read)
330  return rds_error;
331 
332  return rds_complete;
333 }
334 
335 
336 }}// end of namespaces
#define EXECUTE_FUNCTION(FULLFUNCTIONNAME)
Execute and return the delegate.
Definition: Macros.h:54
#define DEEPCOPY_GET_AND_SET(FUNC)
Definition: FunctionBind.h:44
Class to hold the data from the global trigger in the correct format.
Definition: RawTriggerV0.h:65
#define VERSION_NAMESPACE
daqdataformats::RawTriggerTime * _Time
Definition: RawTriggerV0.h:113
#define ZEROOUT_GET_AND_SET(FUNC)
Definition: FunctionBind.h:56
#define DEEPCOPY_SET(FUNC)
Definition: FunctionBind.h:43
Class to hold the data corresponding to the trigger mask.
#define ZEROOUT_SET(FUNC)
Definition: FunctionBind.h:55
Class to hold the data from the global trigger range in the correct format.
Class that contains the timing information relating to a given trigger.
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
const XML_Char * version
Definition: expat.h:187
daqdataformats::RawTriggerTimingMarker * _Marker
Definition: RawTriggerV0.h:111
daqdataformats::RawTriggerRange * _Range
Definition: RawTriggerV0.h:112
#define RAWBUFF32
Definition: RawDAQData.h:16
int32_t version_t
Definition: RawDAQData.h:72
daqdataformats::RawTriggerMask * _Mask
Definition: RawTriggerV0.h:114
#define PRINT_ON_DEBUG(x)
Definition: RawDAQData.h:35
static const version_t DAQDATAFORMAT_UNKNOWN_VERSION
Definition: RawDAQData.h:81
Class to hold the data from the FEBs in correct formats.
Class to hold the data corresponding to the trigger mask.
readDataStatus readDataUnknownVersion(const int)
void print(std::ostream &os=std::cout) const
daqdataformats::RawTriggerHeader * _Header
Trigger Consists of the Following Items.
Definition: RawTriggerV0.h:110