Belle II Software  release-08-01-10
rawinputModule.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/rfarm/event/modules/rawinputModule.h>
10 
11 #include <daq/dataobjects/SendHeader.h>
12 #include <daq/dataobjects/SendTrailer.h>
13 
14 #include "TSystem.h"
15 
16 using namespace std;
17 using namespace Belle2;
18 
19 //-----------------------------------------------------------------
20 // Register the Module
21 //-----------------------------------------------------------------
22 REG_MODULE(RawInput)
23 
24 //-----------------------------------------------------------------
25 // Implementation
26 //-----------------------------------------------------------------
27 
29 {
30  //Set module properties
31  setDescription("Raw data file input module");
32  setPropertyFlags(c_Input);
33 
34  m_fd = 0;
35  m_nevt = -1;
36 
37  //Parameter definition
38  addParam("inputFileName" , m_inputFileName, "Raw file name.", string("RawInput.root"));
39 
40  B2DEBUG(1, "RawInput: Constructor done.");
41 }
42 
43 
44 RawInputModule::~RawInputModule()
45 {
46 }
47 
48 void RawInputModule::initialize()
49 {
50  gSystem->Load("libdataobjects");
51 
52  // Initialize EvtMetaData
53  m_eventMetaData.registerInDataStore();
54 
55  // Initialize Array of RawCOPPER
56  m_rawDataBlock.registerInDataStore();
57  m_rawCOPPER.registerInDataStore();
58  m_rawSVD.registerInDataStore();
59  m_rawCDC.registerInDataStore();
60  m_rawTOP.registerInDataStore();
61  m_rawARICH.registerInDataStore();
62  m_rawECL.registerInDataStore();
63  m_rawKLM.registerInDataStore();
64  m_rawTRG.registerInDataStore();
65  m_rawFTSW.registerInDataStore();
66 
67  // Open input file
68  printf("RawInput : Opening file %s\n", m_inputFileName.c_str());
69  m_fd = open(m_inputFileName.c_str(), O_RDONLY);
70  if (m_fd <= 0) {
71  perror("fopen");
72  exit(-1);
73  }
74  printf("Done. m_fd = %d\n", m_fd);
75 
76  // Read the first event in RingBuffer and restore in DataStore.
77  // This is necessary to create object tables before TTree initialization
78  // if used together with SimpleOutput.
79  // ---- Prefetch the first event
80  registerRawCOPPERs();
81 
82  B2INFO("RawInput: initialized.");
83 }
84 
85 
86 
87 void RawInputModule::beginRun()
88 {
89  m_nevt = 0;
90  B2INFO("RawInput: beginRun called.");
91 }
92 
93 
94 void RawInputModule::event()
95 {
96  m_nevt++;
97  // First event is already loaded
98  if (m_nevt == 0) return;
99 
100  registerRawCOPPERs();
101 }
102 
103 void RawInputModule::registerRawCOPPERs()
104 {
105 
106  // Get a record from a file
107  int error_flag = 0;
108  char* evtbuf = new char[MAXEVTSIZE];
109  // Get a record from file
110  int sstat = read(m_fd, evtbuf, sizeof(int));
111  if (sstat <= 0) {
112  delete[] evtbuf;
113  return;
114  }
115  int* recsize = (int*)evtbuf;
116  int rstat = read(m_fd, evtbuf + sizeof(int), (*recsize - 1) * 4);
117  if (rstat <= 0) {
118  delete[] evtbuf;
119  return;
120  }
121 
122  B2INFO("RawInput: got an event from a file, size=" << recsize <<
123  " (proc= " << (int)getpid() << ")");
124  // printf ( "%8.8x %8.8x %8.8x %8.8x ", *evtbuf, *(evtbuf+1), *(evtbuf+2), *(evtbuf+3) );
125  // printf ( "%8.8x %8.8x %8.8x %8.8x\n", *(evtbuf+4), *(evtbuf+5), *(evtbuf+6), *(evtbuf+7) );
126 
127  // Unpack SendHeader
128  SendHeader sndhdr;
129  sndhdr.SetBuffer((int*)evtbuf);
130  int npackedevts = sndhdr.GetNumEventsinPacket();
131  if (npackedevts != 1) {
132  B2FATAL("Raw2DsModule::number of events in packet is not 1 " << npackedevts);
133  }
134  int ncprs = sndhdr.GetNumNodesinPacket();
135  int nwords = sndhdr.GetTotalNwords() - SendHeader::SENDHDR_NWORDS - SendTrailer::SENDTRL_NWORDS;
136  B2INFO("RawInput: Ncprs=" << ncprs << " Nwords=" << nwords);
137 
138  // Get buffer header
139  int* bufbody = (int*)evtbuf + SendHeader::SENDHDR_NWORDS;
140 
141  //
142  // Copy from Raw2DsModule.cc -- From here
143  //
144 
145  // Unpack buffer
146  RawDataBlock tempdblk;
147  tempdblk.SetBuffer(bufbody, nwords, false, npackedevts, ncprs);
148 
149  // Store data contents in Corresponding RawXXXX
150  for (int cprid = 0; cprid < ncprs * npackedevts; cprid++) {
151  // Pick up one COPPER and copy data in a temporary buffer
152  int nwds_buf = tempdblk.GetBlockNwords(cprid);
153  int* cprbuf = new int[nwds_buf];
154  memcpy(cprbuf, tempdblk.GetBuffer(cprid), nwds_buf * 4);
155 
156  // Check FTSW
157  if (tempdblk.CheckFTSWID(cprid)) {
159  (ary.appendNew())->SetBuffer(cprbuf, nwds_buf, 1, 1, 1);
160  continue;
161  }
162 
163  RawCOPPER tempcpr;
164  // tempcpr.SetBuffer(bufbody, nwords, false, npackedevts, ncprs); -> bug. If RawFTSW is stored in bufbody, tempcpr's version becomes 0 and getNodeID fails.
165  tempcpr.SetBuffer(cprbuf, nwds_buf, false, 1, 1);
166 
167  // int subsysid = ((RawCOPPER&)tempdblk).GetNodeID(cprid);
168  // int subsysid = tempcpr.GetNodeID(cprid);
169  int subsysid = tempcpr.GetNodeID(0);
170  if (tempcpr.GetEventCRCError(0) != 0) error_flag = 1;
171 
172  // Switch to each detector and register RawXXX
173  if ((subsysid & DETECTOR_MASK) == CDC_ID) {
174  StoreArray<RawCDC> ary;
175  (ary.appendNew())->SetBuffer(cprbuf, nwds_buf, 1, 1, 1);
176  } else if ((subsysid & DETECTOR_MASK) == SVD_ID) {
177  StoreArray<RawSVD> ary;
178  (ary.appendNew())->SetBuffer(cprbuf, nwds_buf, 1, 1, 1);
179  } else if ((subsysid & DETECTOR_MASK) == BECL_ID) {
180  StoreArray<RawECL> ary;
181  (ary.appendNew())->SetBuffer(cprbuf, nwds_buf, 1, 1, 1);
182  } else if ((subsysid & DETECTOR_MASK) == EECL_ID) {
183  StoreArray<RawECL> ary;
184  (ary.appendNew())->SetBuffer(cprbuf, nwds_buf, 1, 1, 1);
185  } else if ((subsysid & DETECTOR_MASK) == TOP_ID) {
186  StoreArray<RawTOP> ary;
187  (ary.appendNew())->SetBuffer(cprbuf, nwds_buf, 1, 1, 1);
188  } else if ((subsysid & DETECTOR_MASK) == ARICH_ID) {
190  (ary.appendNew())->SetBuffer(cprbuf, nwds_buf, 1, 1, 1);
191  } else if ((subsysid & DETECTOR_MASK) == BKLM_ID) {
192  StoreArray<RawKLM> ary;
193  (ary.appendNew())->SetBuffer(cprbuf, nwds_buf, 1, 1, 1);
194  } else if ((subsysid & DETECTOR_MASK) == EKLM_ID) {
195  StoreArray<RawKLM> ary;
196  (ary.appendNew())->SetBuffer(cprbuf, nwds_buf, 1, 1, 1);
197  } else if (((subsysid & DETECTOR_MASK) & 0xF0000000) == TRGDATA_ID) {
198  StoreArray<RawTRG> ary;
199  (ary.appendNew())->SetBuffer(cprbuf, nwds_buf, 1, 1, 1);
200  } else {
202  (ary.appendNew())->SetBuffer(cprbuf, nwds_buf, 1, 1, 1);
203  }
204  // delete[] cprbuf;
205  }
206 
207  StoreObjPtr<EventMetaData> evtmetadata;
208  evtmetadata.create();
209  evtmetadata->setExperiment(sndhdr.GetExpNum());
210  evtmetadata->setRun(sndhdr.GetRunNum());
211  evtmetadata->setSubrun(sndhdr.GetSubRunNum());
212  evtmetadata->setEvent(sndhdr.GetEventNumber());
213  if (error_flag) evtmetadata->addErrorFlag(EventMetaData::c_B2LinkEventCRCError);
214 
215  delete[] evtbuf;
216  // Copy from Raw2DsModule.cc -- Up to here
217 
218  B2INFO("RawInput: DataStore Restored!!");
219  return;
220 }
221 
222 
223 void RawInputModule::endRun()
224 {
225  B2INFO("RawInput: endRun done.");
226 }
227 
228 
229 void RawInputModule::terminate()
230 {
231  close(m_fd);
232  B2INFO("RawInput: terminate called");
233 }
234 
Base class for Modules.
Definition: Module.h:72
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 int * GetBuffer(int n)
get nth buffer pointer
Definition: RawDataBlock.h:53
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
virtual int GetBlockNwords(int n)
get size of a data block
Definition: RawDataBlock.h:94
A class definition of an input module for Sequential ROOT I/O.
void SetBuffer(int *hdr)
set buffer
Definition: SendHeader.cc:37
int GetNumEventsinPacket()
get contents of Header
Definition: SendHeader.cc:125
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
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
@ c_Input
Input Process.
int GetEventCRCError(int n)
check CRC event Error
Definition: RawCOPPER.h:434
unsigned int GetNodeID(int n)
get node-ID from data
Definition: RawCOPPER.h:397
Abstract base class for different kinds of events.