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