Belle II Software development
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
16using namespace std;
17using namespace Belle2;
18
19//-----------------------------------------------------------------
20// Register the Module
21//-----------------------------------------------------------------
22REG_MODULE(RawInput);
23
24//-----------------------------------------------------------------
25// Implementation
26//-----------------------------------------------------------------
27
29{
30 //Set module properties
31 setDescription("Raw data file input module");
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
44RawInputModule::~RawInputModule()
45{
46}
47
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
81
82 B2INFO("RawInput: initialized.");
83}
84
85
86
88{
89 m_nevt = 0;
90 B2INFO("RawInput: beginRun called.");
91}
92
93
95{
96 m_nevt++;
97 // First event is already loaded
98 if (m_nevt == 0) return;
99
101}
102
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) {
175 (ary.appendNew())->SetBuffer(cprbuf, nwds_buf, 1, 1, 1);
176 } else if ((subsysid & DETECTOR_MASK) == SVD_ID) {
178 (ary.appendNew())->SetBuffer(cprbuf, nwds_buf, 1, 1, 1);
179 } else if ((subsysid & DETECTOR_MASK) == BECL_ID) {
181 (ary.appendNew())->SetBuffer(cprbuf, nwds_buf, 1, 1, 1);
182 } else if ((subsysid & DETECTOR_MASK) == EECL_ID) {
184 (ary.appendNew())->SetBuffer(cprbuf, nwds_buf, 1, 1, 1);
185 } else if ((subsysid & DETECTOR_MASK) == TOP_ID) {
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) {
193 (ary.appendNew())->SetBuffer(cprbuf, nwds_buf, 1, 1, 1);
194 } else if ((subsysid & DETECTOR_MASK) == EKLM_ID) {
196 (ary.appendNew())->SetBuffer(cprbuf, nwds_buf, 1, 1, 1);
197 } else if (((subsysid & DETECTOR_MASK) & 0xF0000000) == TRGDATA_ID) {
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
224{
225 B2INFO("RawInput: endRun done.");
226}
227
228
230{
231 close(m_fd);
232 B2INFO("RawInput: terminate called");
233}
234
@ c_B2LinkEventCRCError
HSLB_COPPER CRC error is detected in the event.
Definition: EventMetaData.h:45
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
@ c_Input
This module is an input module (reads data).
Definition: Module.h:78
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 * GetBuffer(int n)
get nth buffer pointer
Definition: RawDataBlock.h:53
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
virtual int GetBlockNwords(int n)
get size of a data block
Definition: RawDataBlock.h:94
void registerRawCOPPERs()
function to read raw data
int m_nevt
Total nr. of events in the file.
StoreArray< RawSVD > m_rawSVD
RawSVD.
void initialize() override
Module functions to be called from main process.
void event() override
This method is the core of the module.
RawInputModule()
Constructor / Destructor.
void endRun() override
This method is called if the current run ends.
StoreArray< RawFTSW > m_rawFTSW
RawFTSW.
void terminate() override
This method is called at the end of the event processing.
std::string m_inputFileName
File name.
StoreObjPtr< EventMetaData > m_eventMetaData
EventMetaData.
StoreArray< RawCOPPER > m_rawCOPPER
RawCOPPER.
StoreArray< RawARICH > m_rawARICH
RawARICH.
StoreArray< RawTRG > m_rawTRG
RawTRG.
void beginRun() override
Module functions to be called from event process.
StoreArray< RawKLM > m_rawKLM
RawKLM.
StoreArray< RawTOP > m_rawTOP
RawTOP.
StoreArray< RawDataBlock > m_rawDataBlock
RawDataBlock.
StoreArray< RawECL > m_rawECL
RawECL.
StoreArray< RawCDC > m_rawCDC
RawCDC.
int m_fd
File handle.
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
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 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.
STL namespace.