Belle II Software  release-08-01-10
DeSerializerFILE.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <daq/rawdata/modules/DeSerializerFILE.h>
10 #include <rawdata/dataobjects/PreRawCOPPERFormat_latest.h>
11 #include <rawdata/dataobjects/RawFTSW.h>
12 #define USE_PCIE40
13 using namespace std;
14 using namespace Belle2;
15 
16 //#define NO_DATA_CHECK
17 //#define WO_FIRST_EVENUM_CHECK
18 
19 //-----------------------------------------------------------------
20 // Register the Module
21 //-----------------------------------------------------------------
22 REG_MODULE(DeSerializerFILE)
23 
24 //-----------------------------------------------------------------
25 // Implementation
26 //-----------------------------------------------------------------
27 
29 {
30  //Set module properties
31  setDescription("Encode DataStore into RingBuffer");
32 
33  // setPropertyFlags(c_Input | c_ParallelProcessingCertified);
34  // addParam("FinesseBitFlag", finesse_bit_flag, "finnese (A,B,C,D) -> bit (0,1,2,3)", 15);
35  addParam("inputFileName", m_fname_in, "Input binary filename", string(""));
36  addParam("inputRepetitionTimes", m_repetition_max,
37  "Input repetition times to read the input file", 0);
38 
39  //Parameter definition
40  B2INFO("DeSerializerFILE: Constructor done.");
41 
42 }
43 
44 
45 DeSerializerFILEModule::~DeSerializerFILEModule()
46 {
47 }
48 
49 void DeSerializerFILEModule::fileOpen()
50 {
51  m_fp_in = fopen(m_fname_in.c_str(), "r");
52  if (!m_fp_in) {
53  char err_buf[500];
54  sprintf(err_buf, "[FATAL] Cannot open an input file(%s): %s : Exiting...\n",
55  strerror(errno), m_fname_in.c_str());
56  print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
57  exit(-1);
58  }
59  return;
60 }
61 
62 void DeSerializerFILEModule::initialize()
63 {
64  B2INFO("DeSerializerFILE: initialize() started.");
65  fileOpen();
66 
67  // Initialize EvtMetaData
68  m_eventMetaDataPtr.registerInDataStore();
69 
70  memset(time_array0, 0, sizeof(time_array0));
71  memset(time_array1, 0, sizeof(time_array1));
72  memset(time_array2, 0, sizeof(time_array2));
73  memset(time_array3, 0, sizeof(time_array3));
74  memset(time_array4, 0, sizeof(time_array4));
75  memset(time_array5, 0, sizeof(time_array5));
76 
77  // Initialize Array of RawCOPPER
78  m_rawDataBlock.registerInDataStore();
79  m_rawCOPPER.registerInDataStore();
80  m_rawSVD.registerInDataStore();
81  m_rawCDC.registerInDataStore();
82  m_rawTOP.registerInDataStore();
83  m_rawARICH.registerInDataStore();
84  m_rawECL.registerInDataStore();
85  m_rawKLM.registerInDataStore();
86 
87  m_start_flag = 0;
88 
89  m_repetition_cnt = 0;
90  B2INFO("DeSerializerFILE: initialize() done.");
91 
92 }
93 
94 
95 int* DeSerializerFILEModule::readOneDataBlock(int* /*delete_flag*/, int* /*size_word*/, int* /*data_type*/)
96 {
97 #ifdef USE_PCIE40
98  char err_buf[500];
99  sprintf(err_buf, "[FATAL] This function is not supported. Exiting...: \n%s %s %d\n",
100  __FILE__, __PRETTY_FUNCTION__, __LINE__);
101  print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
102  exit(1);
103 #else
104  *delete_flag = 1;
105  //
106  // Read 1st word( data size ) of RawDataBlock
107  //
108 
109  // int recvd_byte = RawHeader::RAWHEADER_NWORDS * sizeof(int);
110  int temp_size_word = 0;
111  int recvd_byte = 0;
112  int bytes_to_read = sizeof(int);
113 
114  int read_size = 0;
115  // if( read_size = fread( (char*)(&temp_size_word), bytes_to_read, 1, m_fp_in ) < 0 ) {
116  while (true) {
117  if ((read_size = fread((char*)(&temp_size_word), 1, bytes_to_read, m_fp_in)) != bytes_to_read) {
118  if (feof(m_fp_in)) {
119  if (m_repetition_max > m_repetition_cnt) {
120  m_repetition_cnt++;
121  fclose(m_fp_in);
122  fileOpen();
123  continue;
124  } else {
125  return 0x0;
126  }
127  }
128  char err_buf[500];
129  sprintf(err_buf, "[FATAL] Failed to read header(%s).", strerror(errno));
130  print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
131  exit(-1);
132  }
133  break;
134  }
135  recvd_byte += read_size;
136 
137  int start_word = 0, stop_word = 0;
138  int* temp_buf = 0x0;
139 
140 
141  // Not checked if this part works for PCIe40 data-format
142  *data_type = RAW_DATABLOCK;
143  *size_word = temp_size_word;
144  start_word = 1;
145  stop_word = *size_word;
146  temp_buf = readfromFILE(m_fp_in, *size_word, start_word, stop_word);
147  if (temp_size_word == 0x7fff0008) {
148  // 0x7fff0008 -> 2.1Gword. So this is too large for the length of one RawDataBlock.
149  *data_type = COPPER_DATABLOCK;
150 
151  int pos_data_length;
152 
153  pos_data_length = PreRawCOPPERFormat_latest::POS_DATA_LENGTH;
154 
155  start_word = 0;
156  stop_word = pos_data_length;
157  int* length_buf = readfromFILE(m_fp_in, pos_data_length, start_word, stop_word);
158 
159 
160  *size_word = length_buf[ pos_data_length - 1 ] +
161  PreRawCOPPERFormat_latest::SIZE_COPPER_DRIVER_HEADER + PreRawCOPPERFormat_latest::SIZE_COPPER_DRIVER_TRAILER
162  + m_pre_rawcpr.tmp_header.RAWHEADER_NWORDS + m_pre_rawcpr.tmp_trailer.RAWTRAILER_NWORDS;
163  start_word = 1 + pos_data_length + m_pre_rawcpr.tmp_header.RAWHEADER_NWORDS;
164  stop_word = *size_word - m_pre_rawcpr.tmp_trailer.RAWTRAILER_NWORDS;
165 
166  temp_buf = readfromFILE(m_fp_in, *size_word, start_word, stop_word);
167 
168 
169  temp_buf[ m_pre_rawcpr.tmp_header.RAWHEADER_NWORDS ] = 0x7fff0008;
170  memcpy((temp_buf + m_pre_rawcpr.tmp_header.RAWHEADER_NWORDS + 1),
171  length_buf, sizeof(int) * pos_data_length);
172 
173  delete length_buf;
174  } else {
175  *data_type = RAW_DATABLOCK;
176  *size_word = temp_size_word;
177  start_word = 1;
178  stop_word = *size_word;
179  temp_buf = readfromFILE(m_fp_in, *size_word, start_word, stop_word);
180  }
181  return temp_buf;
182 #endif
183 }
184 
185 
186 
187 int* DeSerializerFILEModule::readfromFILE(FILE* fp_in, const int size_word, const int start_word, const int stop_word)
188 {
189  //
190  // Allocate buffer if needed
191  //
192  int* temp_buf;
193  temp_buf = new int[ size_word ];
194 
195  memset(temp_buf, 0, size_word * sizeof(int));
196  temp_buf[ 0 ] = size_word;
197  int recvd_byte = start_word * sizeof(int);
198  int bytes_to_read = stop_word * sizeof(int);
199  int read_size;
200 
201  if ((read_size = fread((char*)temp_buf + recvd_byte, 1, bytes_to_read - recvd_byte, fp_in)) < 0) {
202  if (feof(fp_in)) {
203  delete[] temp_buf;
204  return 0x0;
205  }
206  char err_buf[500];
207  sprintf(err_buf, "[FATAL] Failed to read header. Exiting...");
208  print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
209  exit(-1);
210  }
211  recvd_byte += read_size;
212 
213  if (bytes_to_read != recvd_byte) {
214  char err_buf[500];
215  sprintf(err_buf, "[FATAL] Read less bytes(%d) than expected(%d). Exiting...\n",
216  recvd_byte, bytes_to_read);
217  print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
218  exit(-1);
219  }
220  return temp_buf;
221 }
222 
223 
224 
225 void DeSerializerFILEModule::event()
226 {
227 
228  if (m_start_flag == 0) {
229  m_start_flag = 1;
230  m_dummy_evenum = 0;
231  }
232 
233  if (m_start_flag == 0) {
234  B2INFO("DeSerializerFILE: event() started.");
235  m_start_time = getTimeSec();
236  n_basf2evt = 0;
237  m_start_flag = 1;
238  }
239 
240  // rawcprarray.create();
241  // raw_dblkarray.create();
242 
243  // RawCOPPER* temp_rawcopper;
244 
245  int first_flag = 0;
246  unsigned int prev_eve_num = 0;
247  int eof_flag = 0;
248 
249  //
250  // Update EventMetaData
251  //
252 
253  while (true) {
254  int size_word = 0;
255  int delete_flag = 0;
256  // int* temp_buf = readOneEventFromCOPPERFIFO(j, &delete_flag, &size_word);
257  int* temp_buf;
258 
259  if (m_prev_buf_flag == 1) {
260  temp_buf = m_prev_buf;
261  size_word = temp_buf[ 0 ];
262  m_prev_buf_flag = 0;
263  } else {
264  int data_type;
265  temp_buf = readOneDataBlock(&delete_flag, &size_word, &data_type);
266 
267 
268  if (temp_buf == 0x0) { // End of File
269  printf("[DEBUG] End of file\n");
270  eof_flag = 1;
271  break;
272  }
273 
274  if (data_type == COPPER_DATABLOCK) {
275 
276  RawCOPPER temp_rawcopper;
277 
278  int num_nodes = 1;
279  int num_events = 1;
280  temp_rawcopper.SetBuffer(temp_buf, size_word, 0, num_events, num_nodes);
281 
282  {
283  printf("[FATAL] Sorry. This version does not support fillNewCOPPERheader()");
284  exit(1);
285  // fillNewRawCOPPERHeader(&temp_rawcopper);
286  }
287 
288 
289  unsigned int temp_cur_evenum = 0, temp_cur_copper_ctr = 0;
290 
291  try {
292  temp_rawcopper.CheckData(0,
293  m_dummy_evenum - 2, &temp_cur_evenum,
294  m_dummy_evenum - 2, &temp_cur_copper_ctr,
295  m_prev_exprunsubrun_no, &m_exprunsubrun_no);
296 
297  } catch (string err_str) {
298  print_err.PrintError(m_shmflag, &g_status, err_str);
299  exit(1);
300  }
301  m_prev_run_no = m_run_no; // The place of this update depends on # of nodes
302  }
303 
304 
305  }
306 
307  //
308  // Check Event #
309  //
310  RawDataBlock temp_rawdblk;
311  int num_nodes = 1;
312  int num_events = 1;
313 
314  temp_rawdblk.SetBuffer(temp_buf, size_word, 0, num_nodes, num_events);
315  int block_num = 0;
316 
317  unsigned int eve_num;
318  //int subsysid = 0;
319 
320  if (temp_rawdblk.CheckFTSWID(block_num)) {
321  RawFTSW temp_raw_ftsw;
322  temp_raw_ftsw.SetBuffer(temp_buf, size_word, 0, num_nodes, num_events);
323  eve_num = temp_raw_ftsw.GetEveNo(block_num);
324  } else {
325  RawCOPPER temp_raw_copper;
326  temp_raw_copper.SetBuffer(temp_buf, size_word, 0, num_nodes, num_events);
327  eve_num = temp_raw_copper.GetEveNo(block_num);
328  //subsysid = temp_raw_copper.GetNodeID(block_num);
329  }
330 
331  if (eve_num != prev_eve_num && first_flag != 0) {
332  m_prev_buf_flag = 1;
333  m_prev_buf = temp_buf;
334  break;
335  }
336  prev_eve_num = eve_num;
337 
338  if (temp_rawdblk.CheckFTSWID(block_num)) {
340  (ary.appendNew())->SetBuffer(temp_buf, size_word, 1, num_nodes, num_events);
341  } else {
342 
343  //
344  // Switch to each detector and register RawXXX
345  //
346  // if (subsysid == CDC_ID) {
347  // StoreArray<RawCDC> ary;
348  // (ary.appendNew())->SetBuffer(temp_buf, size_word, 1, 1, 1);
349  // } else if (subsysid == SVD_ID) {
350  StoreArray<RawSVD> ary;
351  (ary.appendNew())->SetBuffer(temp_buf, size_word, 1, 1, 1);
352  // } else if (subsysid == ECL_ID) {
353  // StoreArray<RawECL> ary;
354  // (ary.appendNew())->SetBuffer(temp_buf, size_word, 1, 1, 1);
355  // } else if (subsysid == TOP_ID) {
356  // StoreArray<RawTOP> ary;
357  // (ary.appendNew())->SetBuffer(temp_buf, size_word, 1, 1, 1);
358  // } else if (subsysid == ARICH_ID) {
359  // StoreArray<RawARICH> ary;
360  // (ary.appendNew())->SetBuffer(temp_buf, size_word, 1, 1, 1);
361  // } else if (subsysid == KLM_ID) {
362  // StoreArray<RawKLM> ary;
363  // (ary.appendNew())->SetBuffer(temp_buf, size_word, 1, 1, 1);
364  // } else {
365 // StoreArray<RawCOPPER> ary;
366 // (ary.appendNew())->SetBuffer(temp_buf, size_word, 1, 1, 1);
367  // }
368 
369 
370  }
371 
372  m_totbytes += size_word * sizeof(int);
373 
374  first_flag = 1;
375  }
376 
377 
378  m_eventMetaDataPtr.create();
379  m_eventMetaDataPtr->setExperiment(0);
380  m_eventMetaDataPtr->setRun(0);
381  m_eventMetaDataPtr->setEvent(prev_eve_num);
382  if (eof_flag == 1) {
383  m_eventMetaDataPtr->setEndOfData();
384  }
385 
386  if (n_basf2evt % 100 == 0) {
387  printf("[DEBUG] Processing Evt # %d...\n", prev_eve_num);
388  }
389 
390  n_basf2evt++;
391  return;
392 }
393 
A class definition of an input module for Sequential ROOT I/O.
A class definition of an input module for Sequential ROOT I/O.
The Raw COPPER class This class stores data received by COPPER via belle2linkt Data from all detector...
Definition: RawCOPPER.h:52
void SetBuffer(int *bufin, int nwords, int delete_flag, int num_events, int num_nodes) OVERRIDE_CPP17
set buffer ( delete_flag : m_buffer is freeed( = 0 )/ not freeed( = 1 ) in Destructer )
Definition: RawCOPPER.cc:141
The RawDataBlock class Base class for rawdata handling.
Definition: RawDataBlock.h:27
virtual int CheckFTSWID(int n)
get FTSW ID to check whether this data block is FTSW data or not
Definition: RawDataBlock.h:101
virtual void SetBuffer(int *bufin, int nwords, int delete_flag, int num_events, int num_nodes)
set buffer ( delete_flag : m_buffer is freeed( = 0 )/ not freeed( = 1 ) in Destructer )
Definition: RawDataBlock.cc:35
The Raw FTSW class.
Definition: RawFTSW.h:30
unsigned int GetEveNo(int n)
Get event #.
Definition: RawFTSW.h:65
void SetBuffer(int *bufin, int nwords, int delete_flag, int num_events, int num_nodes) OVERRIDE_CPP17
set buffer ( delete_flag : m_buffer is freeed( = 0 )/ not freeed( = 1 ) in Destructer )
Definition: RawFTSW.cc:107
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
T * appendNew()
Construct a new T object at the end of the array.
Definition: StoreArray.h:246
bool create(bool replace=false)
Creating StoreArrays is unnecessary, only used internally.
Definition: StoreArray.h:329
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
unsigned int GetEveNo(int n)
get subrun #(8bit)
Definition: RawCOPPER.h:390
void CheckData(int n, unsigned int prev_evenum, unsigned int *cur_evenum, unsigned int prev_copper_ctr, unsigned int *cur_copper_ctr, unsigned int prev_exprunsubrun_no, unsigned int *cur_exprunsubrun_no)
check data contents
Definition: RawCOPPER.h:699
Abstract base class for different kinds of events.