Belle II Software  release-08-01-10
HexDataPacker.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 // The format of readable hex dump
10 // ************************************************
11 // Program to read events by DMA
12 // Data mode
13 // SUCCESS: Device opened for ECS 0
14 // SUCCESS: Device opened for ECS 2
15 // SUCCESS: Device opened for DMA
16 // Header : eeee000c aaaaeeee 0180aaaa 00000000 00010000 00000000 00000000 00000000
17 // data 0 : eeee000c aaaaeeee 0180aaaa 00000000 00010000 00000000 00000000 00000000
18 // data 7 : 0000004e 7f7f0438 0343cd00 00000000 21ed8e47 5f7e6a02 00000000 00000001
19 // data 15 : 00000038 00000038 00000038 00000038 00000038 00000038 00000038 00000038
20 // data 23 : 00000038 00000038 00000038 00000038 0000004a 0000004a 0000004a 0000004a
21 // data 31 : 0000004a 0000004a 0000004a 0000004a 0000004a 0000004a 0000004a 0000004a
22 // data 39 : 0000004a 0000004a 0000004a 0000004a 0000004a 0000004a 0000004a 0000004a
23 // data 47 : 0000004a 0000004a 0000004a 0000004a 0000004a 0000004a 0000004a 0000004a
24 // data 55 : 0000004a 0000004a 0000004a 0000004a 0000004a 0000004a 0000004a 0000004a
25 // data 63 : ffaa0b00 21ed8e47 00000000 5f7e6a02 0343cd00 21ed9c30 4898121d 06a40000
26 // data 71 : 4cb01218 06840000 540110de 04980000 5401120f 060c0000 12ee0000 21ed8e47
27 // data 79 : 00006408 ff550000 7fff1775 7fff0006 7fff0007 7fff0008 7fff0009 7fff000a
28 // data 87 : Trailer : 0 0 0 0 0 0 0 C
29 // Bad event number prev 0 cur 0
30 // Header : eeee000c aaaaeeee 0180aaaa 00000000 00010000 00000001 00000000 00000000
31 // data 0 : eeee000c aaaaeeee 0180aaaa 00000000 00010000 00000001 00000000 00000000
32 // data 7 : 0000004c 7f7f0438 0343cd00 00000001 21fc9f57 5f7e6a02 00000000 00000001
33 // ************************************************
34 #include <iostream>
35 #include <rawdata/modules/HexDataPacker.h>
36 
37 using namespace std;
38 using namespace Belle2;
39 
40 #define USE_PCIE40
41 
42 //-----------------------------------------------------------------
43 // Register the Module
44 //-----------------------------------------------------------------
45 REG_MODULE(HexDataPacker);
46 
47 //-----------------------------------------------------------------
48 // Implementation
49 //-----------------------------------------------------------------
50 
51 HexDataPackerModule::HexDataPackerModule() : Module()
52 {
54  setDescription("an Example to pack data to a RawCOPPER object");
55  addParam("inputFileName", m_fileName, "Output binary filename", string(""));
56  B2INFO("HexDataPacker: Constructor done.");
57  // initialize event #
58  n_basf2evt = 0;
59 }
60 
61 
62 
64 {
65 
66 }
67 
68 
70 {
71  B2INFO("HexDataPacker: initialize() started.");
72 
73  // Open message handler
74  // m_msghandler = new MsgHandler(m_compressionLevel);
75 
77  m_eventMetaDataPtr.registerInDataStore();
78 
79  m_raw_cprarray.registerInDataStore();
80  m_raw_svdarray.registerInDataStore();
81  m_raw_cdcarray.registerInDataStore();
82  m_raw_bpidarray.registerInDataStore();
83  m_raw_epidarray.registerInDataStore();
84  m_raw_eclarray.registerInDataStore();
85  m_raw_klmarray.registerInDataStore();
86 
87  B2INFO("HexDataPacker: initialize() done.");
88 
89  if (m_fileName == "") {
90  B2FATAL("HexDataPacker can't fine a filename: ");
91  }
92 
93  m_ifs.open(m_fileName.c_str());
94  if (!m_ifs) {
95  B2FATAL("HexDataPacker can't open a file " << m_fileName.c_str());
96  }
97 }
98 
99 
100 
101 
103 {
104  StoreArray<RawKLM> ary; // You need to change if you want to make a different Raw*** object.
105  const int MAX_CPRBUF_WORDS = 5000;
106  int* evtbuf = new int[MAX_CPRBUF_WORDS];
107  char char1[50], char2[50], char3[50], char4[50];
108 
109  unsigned int val[10];
110  int size = 0;
111  int runEnd = 0;
112  int word_count = 0;
113  int event_end = 0;
114  while (true) {
115 
116  if (m_ifs.eof()) {
117  runEnd = 1;
118  break;
119  }
120  string strin;
121  getline(m_ifs, strin);
122  sscanf(strin.c_str(), "%49s %49s %49s %49s",
123  char1, char2, char3, char4);
124  if (strcmp(char1, "data") == 0) {
125  if (strcmp(char4, "Trailer") == 0) {
126  continue;
127  }
128  if (strcmp(char2, "0") == 0) {
129  continue;
130  } else if (strcmp(char2, "7") == 0) {
131  sscanf(strin.c_str(), "%49s %u %49s %x %x %x %x %x %x %x %x",
132  char1, &(val[0]), char2, &(val[1]), &(val[2]), &(val[3]), &(val[4]), &(val[5]), &(val[6]), &(val[7]), &(val[8]));
133  size = val[1];
134  val[2] = val[2] | 0x00008000; // For data which was not reduced in FPGA
135  if (size < 0 || size > MAX_CPRBUF_WORDS) {
136  B2FATAL("The size of an event is too large." << LogVar("size", size) << "Exiting...");
137  }
138  } else {
139  sscanf(strin.c_str(), "%20s %u %20s %x %x %x %x %x %x %x %x",
140  char1, &(val[0]), char2, &(val[1]), &(val[2]), &(val[3]), &(val[4]), &(val[5]), &(val[6]), &(val[7]), &(val[8]));
141  }
142  if (size <= 0) {
143  B2FATAL("The size of an event (=" << size << ") is too large. Exiting...");
144  } else {
145  for (int i = 1; i <= 8 ; i++) {
146  if (word_count >= MAX_CPRBUF_WORDS) {
147  B2FATAL("The size of an event (=" << size << ") is too large. Exiting...");
148  }
149  evtbuf[word_count] = val[i];
150  word_count++;
151  if (word_count == size) {
152  event_end = 1;
153  break;
154  }
155  }
156  if (event_end == 1) {break;}
157  }
158  }
159  }
160 
161 #ifdef DEBUG
162  if (size > 0 && size < MAX_CPRBUF_WORDS) {
163  for (int i = 0; i < size; i++) {
164  printf("%.8x ", evtbuf[i]);
165  if (i % 8 == 7) printf("\n");
166  }
167  printf("\n");
168  }
169 #endif
170  StoreObjPtr<EventMetaData> evtmetadata;
171  if (runEnd == 0) {
172  (ary.appendNew())->SetBuffer(evtbuf, size, 1, 1, 1);
173 
174  //
175  // Update EventMetaData :
176  //
177  RawCOPPER tempcpr;
178  tempcpr.SetBuffer(evtbuf, size, false, 1, 1);
179 
180 
181  evtmetadata.create();
182  evtmetadata->setExperiment(tempcpr.GetExpNo(0));
183  evtmetadata->setRun(tempcpr.GetRunNo(0));
184  evtmetadata->setSubrun(tempcpr.GetSubRunNo(0));
185  evtmetadata->setEvent(tempcpr.GetEveNo(0));
186  // evtmetadata->setTime(mtime); //time(NULL));
187 
188  } else {
189  delete[] evtbuf;
190  evtmetadata.create();
191  evtmetadata->setEndOfData();
192  }
193 
194  printf("Event counter %8d\n", n_basf2evt); fflush(stdout);
195  n_basf2evt++;
196 
197 }
StoreArray< RawSVD > m_raw_svdarray
RawSVD array.
Definition: HexDataPacker.h:64
StoreArray< RawCDC > m_raw_cdcarray
RawCDC array.
Definition: HexDataPacker.h:67
StoreArray< RawARICH > m_raw_epidarray
RawARICH array.
Definition: HexDataPacker.h:73
virtual void initialize() override
initialization
virtual void event() override
event module
StoreArray< RawKLM > m_raw_klmarray
RawKLM array.
Definition: HexDataPacker.h:79
int n_basf2evt
event counter
Definition: HexDataPacker.h:55
std::string m_fileName
input-file name
Definition: HexDataPacker.h:89
StoreArray< RawCOPPER > m_raw_cprarray
RawCOPPER array.
Definition: HexDataPacker.h:61
StoreObjPtr< EventMetaData > m_eventMetaDataPtr
Event Meta Data.
Definition: HexDataPacker.h:58
virtual ~HexDataPackerModule()
Destructor.
std::ifstream m_ifs
input-file stream
Definition: HexDataPacker.h:86
StoreArray< RawECL > m_raw_eclarray
RawECL array.
Definition: HexDataPacker.h:76
StoreArray< RawTOP > m_raw_bpidarray
RawTOP array.
Definition: HexDataPacker.h:70
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
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
bool create(bool replace=false)
Create a default object in the data store.
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
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
Class to store variables with their name which were sent to the logging service.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
int GetExpNo(int n)
get Experimental # from header
Definition: RawCOPPER.h:365
unsigned int GetEveNo(int n)
get subrun #(8bit)
Definition: RawCOPPER.h:390
int GetSubRunNo(int n)
get run # (14bit)
Definition: RawCOPPER.h:384
int GetRunNo(int n)
Exp# (10bit) run# (14bit) restart # (8bit)
Definition: RawCOPPER.h:377
Abstract base class for different kinds of events.