source: trunk/MagicSoft/Mars/mraw/MRawSocketRead.cc@ 2652

Last change on this file since 2652 was 2645, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 9.3 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 10/2003 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2003
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26//
27// MRawSocketRead
28//
29// This tasks reads the raw binary file like specified in the TDAS???
30// and writes the data in the corresponding containers which are
31// either retrieved from the parameter list or created and added.
32//
33// Input Containers:
34// -/-
35//
36// Output Containers:
37// MRawRunHeader
38// MRawEvtHeader
39// MRawEvtData
40// MRawCrateArray
41// MTime
42//
43//////////////////////////////////////////////////////////////////////////////
44#include "MRawSocketRead.h"
45
46#include <stdlib.h> // atoi
47
48#include "MReadSocket.h"
49
50#include "MLog.h"
51#include "MLogManip.h"
52
53#include "MParList.h"
54#include "MTaskList.h"
55#include "MEvtLoop.h"
56
57#include "MTime.h"
58#include "MRawRunHeader.h"
59#include "MRawEvtHeader.h"
60#include "MRawEvtData.h"
61#include "MRawCrateData.h"
62#include "MRawCrateArray.h"
63
64#include "MStatusDisplay.h"
65
66ClassImp(MRawSocketRead);
67
68using namespace std;
69
70// --------------------------------------------------------------------------
71//
72// Default constructor. It tries to open the given file.
73//
74MRawSocketRead::MRawSocketRead(const char *name, const char *title)
75 : fIn(NULL), fPort(-1)
76{
77 fName = name ? name : "MRawSocketRead";
78 fTitle = title ? title : "Task to read DAQ binary data from tcp/ip socket";
79
80 fIn = new MReadSocket;
81}
82
83// --------------------------------------------------------------------------
84//
85// Destructor. Delete input stream.
86//
87MRawSocketRead::~MRawSocketRead()
88{
89 delete fIn;
90}
91
92// --------------------------------------------------------------------------
93//
94// Open the socket. This blocks until the connection has been established,
95// an error occured opening the connection or the user requested to
96// quit the application.
97//
98Bool_t MRawSocketRead::OpenSocket()
99{
100 if (fDisplay)
101 fDisplay->SetStatusLine2(Form("Waiting for connection on port #%d...", fPort));
102
103 //
104 // Open socket connection
105 //
106 while (1)
107 {
108 //
109 // If port could be opened eveything is ok
110 //
111 if (fIn->Open(fPort))
112 return kTRUE;
113
114 //
115 // If a MStatusDisplay is attached the user might have
116 // requested to quit the application
117 //
118 if (fDisplay)
119 switch (fDisplay->CheckStatus())
120 {
121 case MStatusDisplay::kFileClose:
122 case MStatusDisplay::kFileExit:
123 *fLog << inf << "MRawSocketRead::PreProcess - MStatusDisplay triggered exit." << endl;
124 return kFALSE;
125 default:
126 break;
127 }
128
129 //
130 // If an error occured during opening the socket stop
131 //
132 if (fIn->fail())
133 break;
134 }
135
136 *fLog << err << "ERROR - Cannot open port #" << fPort << endl;
137
138 return kFALSE;
139}
140
141// --------------------------------------------------------------------------
142//
143// The PreProcess of this task checks for the following containers in the
144// list:
145// MRawRunHeader <output> if not found it is created
146// MRawEvtHeader <output> if not found it is created
147// MRawEvtData <output> if not found it is created
148// MRawCrateArray <output> if not found it is created
149// MTime <output> if not found it is created
150//
151// If all containers are found or created the run header is read from the
152// binary file and printed. If the Magic-Number (file identification)
153// doesn't match we stop the eventloop.
154//
155// Now the EvtHeader and EvtData containers are initialized.
156//
157Int_t MRawSocketRead::PreProcess(MParList *pList)
158{
159 if (!OpenSocket())
160 return kFALSE;
161
162 //
163 // check if all necessary containers exist in the Parameter list.
164 // if not create one and add them to the list
165 //
166 fRawRunHeader = (MRawRunHeader*)pList->FindCreateObj("MRawRunHeader");
167 if (!fRawRunHeader)
168 return kFALSE;
169
170 fRawEvtHeader = (MRawEvtHeader*)pList->FindCreateObj("MRawEvtHeader");
171 if (!fRawEvtHeader)
172 return kFALSE;
173
174 fRawEvtData = (MRawEvtData*)pList->FindCreateObj("MRawEvtData");
175 if (!fRawEvtData)
176 return kFALSE;
177
178 fRawCrateArray = (MRawCrateArray*)pList->FindCreateObj("MRawCrateArray");
179 if (!fRawCrateArray)
180 return kFALSE;
181
182 fTime = (MTime*)pList->FindCreateObj("MTime");
183 if (!fTime)
184 return kTRUE;
185
186 fParList = pList;
187 fRunNumber = (UInt_t)-1;
188 fEvtNumber = (UInt_t)-1;
189
190 return kTRUE;
191}
192
193// --------------------------------------------------------------------------
194//
195// The Process reads one event from the binary file:
196// - The event header is read
197// - the run header is read
198// - all crate information is read
199// - the raw data information of one event is read
200//
201Int_t MRawSocketRead::Process()
202{
203 /*
204 sprintf(report_str, " %8.8d %8.8d %6.6d",
205 run_header->RunNumber, run.event_number,
206 time.tv_sec - run.time_start_secs);
207 if (send(sd, report_str, strlen(report_str), flags) == -1) {
208 return -1;
209 }
210
211 sprintf(report_str, " %6.1f %6.1f %6.6d %7.4f %8.8d %6.6d ",
212 trigrate_hz, storerate_hz, storerate_kbps, gammarate_hz,
213 diskspace_kb, remtime_sec);
214 if (send(sd, report_str, strlen(report_str), flags) == -1) {
215 return -1;
216 }
217 */
218 char dummy[126];
219 fIn->read(dummy, 4); // \nEVT
220
221 if (!(dummy[0]=='\n' && dummy[1]=='E' && dummy[2]=='V' &&dummy[3]=='T'))
222 {
223 *fLog << warn << "EVT tag not found. Stream out of sync. Please try to restart..." << endl;
224 // FIXME: Synchronization missing...
225 return kFALSE;
226 }
227
228 char size[6] = {0,0,0,0,0,0};
229 fIn->read(size, 5);
230 fIn->read(dummy, 126); //00000[CC-DATA]\n
231
232 /*
233 int numevt;
234 sscanf(dummy, "%*s %*d %*d %*d %*d %*d %*d %*d "
235 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
236 "%d %*d %*f %*f %*d %*f %*d %*d", &numevt);
237 */
238
239 //
240 // Read RUN HEADER (see specification) from input stream
241 //
242 fLog->SetNullOutput();
243 fRawRunHeader->ReadEvt(*fIn);
244 fLog->SetNullOutput(kFALSE);
245
246 if (fRawRunHeader->GetMagicNumber()!=kMagicNumber && fRawRunHeader->GetMagicNumber()!=kMagicNumber+1)
247 {
248 *fLog << err << "Error: Wrong Magic Number (0x" << hex << fRawRunHeader->GetMagicNumber() << "): Not a Magic File!" << endl;
249 return kFALSE;
250 }
251
252 if (fRunNumber!=fRawRunHeader->GetRunNumber())
253 {
254 fRawRunHeader->Print();
255
256 MTaskList *tlist = (MTaskList*)fParList->FindObject("MTaskList");
257 if (!tlist)
258 {
259 *fLog << err << dbginf << "ERROR - Task List not found in Parameter List." << endl;
260 return kFALSE;
261 }
262
263 if (!tlist->ReInit())
264 return kFALSE;
265
266 fRunNumber = fRawRunHeader->GetRunNumber();
267 }
268
269 if (atoi(size)==fRawRunHeader->GetNumTotalBytes())
270 {
271 *fLog << dbg << "Event contains only run header information... skipped." << endl;
272 return kCONTINUE;
273 }
274
275 //
276 // Give the run header information to the 'sub-classes'
277 //
278 fRawEvtHeader->Init(fRawRunHeader, fTime);
279 fRawEvtData ->Init(fRawRunHeader, fRawCrateArray);
280
281 //
282 // Read in the next EVENT HEADER (see specification),
283 // if there is no next event anymore stop eventloop
284 //
285 if (!fRawEvtHeader->ReadEvt(*fIn))
286 return kFALSE;
287
288 //
289 // Get number of crates from the run header
290 //
291 const UShort_t nc = fRawRunHeader->GetNumCrates();
292
293 //
294 // Delete arrays which stores the pixel information (time slices)
295 //
296 fRawEvtData->ResetPixels();
297
298 //
299 // clear the TClonesArray which stores the Crate Information
300 // and create a new array of the correct size
301 //
302 fRawCrateArray->SetSize(nc);
303
304 //
305 // Get file format version
306 //
307 const UShort_t ver = fRawRunHeader->GetFormatVersion();
308
309 //
310 // read the CRATE DATA (see specification) from file
311 //
312 for (int i=0; i<nc; i++)
313 {
314 fRawCrateArray->GetEntry(i)->ReadEvt(*fIn, ver);
315 if (!*fIn)
316 return kFALSE;
317
318 fRawEvtData->ReadEvt(*fIn);
319 if (!*fIn)
320 return kFALSE;
321 }
322
323 //
324 // If no new event was recorded the DAQ resends an old event
325 //
326 if (fEvtNumber==fRawEvtHeader->GetDAQEvtNumber())
327 {
328 *fLog << dbg << "Event number #" << dec << fEvtNumber << " didn't change... skipped." << endl;
329 return kCONTINUE;
330 }
331
332 fEvtNumber=fRawEvtHeader->GetDAQEvtNumber();
333
334 return kTRUE;
335}
336
337Int_t MRawSocketRead::PostProcess()
338{
339 //
340 // Close Socket connection
341 //
342 fIn->Close();
343
344 return kTRUE;
345}
Note: See TracBrowser for help on using the repository browser.