Belle II Software  release-05-02-19
Root2Binary.cc
1 //+
2 // File : PrintData.cc
3 // Description : Module to get data from DataStore and send it to another network node
4 //
5 // Author : Satoru Yamada Itoh, IPNS, KEK
6 // Date : 2 - Aug - 2013
7 //-
8 #include <rawdata/modules/Root2Binary.h>
9 
10 using namespace std;
11 using namespace Belle2;
12 
13 //#define DEBUG
14 
15 //-----------------------------------------------------------------
16 // Register the Module
17 //-----------------------------------------------------------------
18 REG_MODULE(Root2Binary)
19 
20 //-----------------------------------------------------------------
21 // Implementation
22 //-----------------------------------------------------------------
23 
25 {
26  m_fp_out = NULL;
27  addParam("outputFileName", m_fname_out, "Output binary filename", string(""));
28 }
29 
30 Root2BinaryModule::~Root2BinaryModule()
31 {
32 }
33 
34 void Root2BinaryModule::initialize()
35 {
36  B2INFO("Root2Binary: initialize() started.");
37 
38  m_fp_out = fopen(m_fname_out.c_str(), "w");
39  if (!m_fp_out) {
40  char err_buf[500];
41  sprintf(err_buf, "Cannot open an output file(%s): %s : Exiting...\n",
42  strerror(errno), m_fname_out.c_str());
43  printf("%s\n", err_buf);
44  // print_err.PrintError(err_buf, __FILE__, __PRETTY_FUNCTION__, __LINE__);
45  exit(-1);
46  }
47 
48  B2INFO("Root2Binary: initialize() done.");
49 }
50 
51 void Root2BinaryModule::endRun()
52 {
53  //fill Run data
54  // fclose( m_fp_out );
55  B2INFO("endRun done.");
56 }
57 
58 
59 void Root2BinaryModule::terminate()
60 {
61  fclose(m_fp_out);
62  B2INFO("terminate called");
63 }
64 
65 
66 void Root2BinaryModule::writeEvent(RawDataBlock* raw_dblk, int* first_flag, int* break_flag, int* dblk_pos, unsigned int* dblk_eve)
67 {
68  //
69  // Data Block
70  //
71  *break_flag = 0;
72  *first_flag = 0;
73 
74 
75  for (int j = *dblk_pos; j < raw_dblk->GetNumEntries(); j++) {
76  printf("numentries %d %d\n", raw_dblk->GetNumEntries(), j);
77 
78 
79  unsigned int prev_eve = *dblk_eve;
80  int num_nodes = 1;
81  int num_events = 1;
82  int delete_flag = 0;
83  int nwords = raw_dblk->GetBlockNwords(j);
84  int* temp_buf = raw_dblk->GetBuffer(j);
85 
86 
87  if (raw_dblk->CheckFTSWID(j)) {
88  RawFTSW temp_raw_ftsw;
89  temp_raw_ftsw.SetBuffer(temp_buf, nwords, delete_flag, num_nodes, num_events);
90  *dblk_eve = temp_raw_ftsw.GetEveNo(0);
91  } else {
92  RawCOPPER temp_raw_copper;
93  temp_raw_copper.SetBuffer(temp_buf, nwords, delete_flag, num_nodes, num_events);
94  *dblk_eve = temp_raw_copper.GetEveNo(0);
95  }
96 
97  if (*dblk_eve != prev_eve && *first_flag == 1) {
98  *dblk_pos = j;
99  return;
100  } else {
101  fwrite((char*)temp_buf, 1, nwords * 4, m_fp_out);
102  printf("eve %u size %d j %d\n", *dblk_eve, nwords * 4, j);
103  }
104 
105  *first_flag = 1;
106  }
107  *dblk_pos = 0;
108 
109  return;
110 }
111 
112 
113 
114 
115 void Root2BinaryModule::event()
116 {
117 
118 
119  B2INFO("Root2Binary: event() started.");
120 
121 
122 
123  //
124  // Data Block
125  //
126  StoreArray<RawDataBlock> raw_dblkarray;
127  StoreArray<RawFTSW> raw_ftswarray;
128  StoreArray<RawCOPPER> raw_copperarray;
129  StoreArray<RawSVD> raw_svdarray;
130  StoreArray<RawCDC> raw_cdcarray;
131  StoreArray<RawTOP> raw_bpidarray;
132  StoreArray<RawARICH> raw_epidarray;
133  StoreArray<RawKLM> raw_klmarray;
134  StoreArray<RawECL> raw_eclarray;
135 
136  int dblk_array = 0;
137  int ftsw_array = 0;
138  int copper_array = 0;
139  int svd_array = 0;
140  int cdc_array = 0;
141  int ecl_array = 0;
142  int bpid_array = 0;
143  int epid_array = 0;
144  int klm_array = 0;
145 
146  int dblk_pos = 0;
147  int ftsw_pos = 0;
148  int copper_pos = 0;
149  int svd_pos = 0;
150  int cdc_pos = 0;
151  int ecl_pos = 0;
152  int bpid_pos = 0;
153  int epid_pos = 0;
154  int klm_pos = 0;
155 
156  unsigned int dblk_eve = 0;
157  unsigned int ftsw_eve = 0;
158  unsigned int copper_eve = 0;
159  unsigned int svd_eve = 0;
160  unsigned int cdc_eve = 0;
161  unsigned int ecl_eve = 0;
162  unsigned int bpid_eve = 0;
163  unsigned int epid_eve = 0;
164  unsigned int klm_eve = 0;
165 
166 
167 
168  while (true) {
169  int write_flag = 0;
170 
171  //
172  // DataBlock
173  //
174  int break_flag;
175  int first_flag;
176 
177 
178  break_flag = 0;
179  first_flag = 0;
180  dblk_eve = 0;
181  int array_entries = raw_dblkarray.getEntries();
182  for (int i = dblk_array; i < array_entries; i++) {
183  write_flag = 1;
184  writeEvent(raw_dblkarray[ i ], &first_flag, &break_flag, &dblk_pos, &dblk_eve);
185  if (break_flag == 1) {
186  dblk_array = i;
187  break;
188  }
189  if (i == array_entries - 1) {
190  dblk_array = array_entries;
191  }
192  }
193 
194  break_flag = 0;
195  first_flag = 0;
196  ftsw_eve = 0;
197  array_entries = raw_ftswarray.getEntries();
198  for (int i = ftsw_array; i < array_entries; i++) {
199  write_flag = 1;
200  writeEvent(raw_ftswarray[ i ], &first_flag, &break_flag, &ftsw_pos, &ftsw_eve);
201  if (break_flag == 1) {
202  ftsw_array = i;
203  break;
204  }
205  if (i == array_entries - 1) {
206  ftsw_array = array_entries;
207  }
208  }
209 
210 
211  break_flag = 0;
212  first_flag = 0;
213  copper_eve = 0;
214  array_entries = raw_copperarray.getEntries();
215  for (int i = copper_array; i < array_entries; i++) {
216  write_flag = 1;
217  writeEvent(raw_copperarray[ i ], &first_flag, &break_flag, &copper_pos, &copper_eve);
218  if (break_flag == 1) {
219  copper_array = i;
220  break;
221  }
222  if (i == array_entries - 1) {
223  copper_array = array_entries;
224  }
225  }
226 
227 
228  break_flag = 0;
229  first_flag = 0;
230  svd_eve = 0;
231  array_entries = raw_svdarray.getEntries();
232  for (int i = svd_array; i < array_entries; i++) {
233  write_flag = 1;
234  writeEvent(raw_svdarray[ i ], &first_flag, &break_flag, &svd_pos, &svd_eve);
235  if (break_flag == 1) {
236  svd_array = i;
237  break;
238  }
239  if (i == array_entries - 1) {
240  svd_array = array_entries;
241  }
242  }
243 
244 
245  break_flag = 0;
246  first_flag = 0;
247  cdc_eve = 0;
248  array_entries = raw_cdcarray.getEntries();
249  for (int i = cdc_array; i < array_entries; i++) {
250  write_flag = 1;
251  writeEvent(raw_cdcarray[ i ], &first_flag, &break_flag, &cdc_pos, &cdc_eve);
252  if (break_flag == 1) {
253  cdc_array = i;
254  break;
255  }
256  if (i == array_entries - 1) {
257  cdc_array = array_entries;
258  }
259  }
260 
261 
262  break_flag = 0;
263  first_flag = 0;
264  bpid_eve = 0;
265  array_entries = raw_bpidarray.getEntries();
266  for (int i = bpid_array; i < array_entries; i++) {
267  write_flag = 1;
268  writeEvent(raw_bpidarray[ i ], &first_flag, &break_flag, &bpid_pos, &bpid_eve);
269  if (break_flag == 1) {
270  bpid_array = i;
271  break;
272  }
273  if (i == array_entries - 1) {
274  bpid_array = array_entries;
275  }
276  }
277 
278 
279 
280  break_flag = 0;
281  first_flag = 0;
282  epid_eve = 0;
283  array_entries = raw_epidarray.getEntries();
284  for (int i = epid_array; i < array_entries; i++) {
285  write_flag = 1;
286  writeEvent(raw_epidarray[ i ], &first_flag, &break_flag, &epid_pos, &epid_eve);
287  if (break_flag == 1) {
288  epid_array = i;
289  break;
290  }
291  if (i == array_entries - 1) {
292  epid_array = array_entries;
293  }
294  }
295 
296 
297 
298  break_flag = 0;
299  first_flag = 0;
300  ecl_eve = 0;
301  array_entries = raw_eclarray.getEntries();
302  for (int i = ecl_array; i < array_entries; i++) {
303  write_flag = 1;
304  writeEvent(raw_eclarray[ i ], &first_flag, &break_flag, &ecl_pos, &ecl_eve);
305  if (break_flag == 1) {
306  ecl_array = i;
307  break;
308  }
309  if (i == array_entries - 1) {
310  ecl_array = array_entries;
311  }
312  }
313 
314  break_flag = 0;
315  first_flag = 0;
316  klm_eve = 0;
317  array_entries = raw_klmarray.getEntries();
318  for (int i = klm_array; i < array_entries; i++) {
319  write_flag = 1;
320  writeEvent(raw_klmarray[ i ], &first_flag, &break_flag, &klm_pos, &klm_eve);
321  if (break_flag == 1) {
322  klm_array = i;
323  break;
324  }
325  if (i == array_entries - 1) {
326  klm_array = array_entries;
327  }
328  }
329  if (write_flag == 0)break;
330  }
331 
332  printf("loop %d\n", n_basf2evt);
333  n_basf2evt++;
334  return;
335 
336 }
Belle2::RawCOPPER
The Raw COPPER class This class stores data received by COPPER via belle2linkt Data from all detector...
Definition: RawCOPPER.h:52
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::RawDataBlock::GetNumEntries
virtual int GetNumEntries()
get # of data blocks = (# of nodes)*(# of events)
Definition: RawDataBlock.h:67
Belle2::RawDataBlock::CheckFTSWID
virtual int CheckFTSWID(int n)
get FTSW ID to check whether this data block is FTSW data or not
Definition: RawDataBlock.h:101
Belle2::RawDataBlock::GetBlockNwords
virtual int GetBlockNwords(int n)
get size of a data block
Definition: RawDataBlock.h:94
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Root2BinaryModule
Dump basf2 objects to a binary file.
Definition: Root2Binary.h:23
Belle2::RawCOPPER::GetEveNo
unsigned int GetEveNo(int n)
get subrun #(8bit)
Definition: RawCOPPER.h:385
Belle2::RawCOPPER::SetBuffer
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
Belle2::RawFTSW::GetEveNo
unsigned int GetEveNo(int n)
Get event #.
Definition: RawFTSW.h:64
Belle2::PrintDataTemplateModule
Module to get data from DataStore and send it to another network node.
Definition: PrintDataTemplate.h:45
Belle2::StoreArray
Accessor to arrays stored in the data store.
Definition: ECLMatchingPerformanceExpertModule.h:33
Belle2::RawFTSW::SetBuffer
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
Belle2::RawDataBlock
The RawDataBlock class Base class for rawdata handling.
Definition: RawDataBlock.h:27
Belle2::StoreArray::getEntries
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:226
Belle2::RawFTSW
The Raw FTSW class.
Definition: RawFTSW.h:30
Belle2::RawDataBlock::GetBuffer
virtual int * GetBuffer(int n)
get nth buffer pointer
Definition: RawDataBlock.h:53