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

Last change on this file since 2522 was 2496, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 9.2 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// MRawEvtTime
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 return kFALSE;
138}
139
140// --------------------------------------------------------------------------
141//
142// The PreProcess of this task checks for the following containers in the
143// list:
144// MRawRunHeader <output> if not found it is created
145// MRawEvtHeader <output> if not found it is created
146// MRawEvtData <output> if not found it is created
147// MRawCrateArray <output> if not found it is created
148// MRawEvtTime <output> if not found it is created (MTime)
149//
150// If all containers are found or created the run header is read from the
151// binary file and printed. If the Magic-Number (file identification)
152// doesn't match we stop the eventloop.
153//
154// Now the EvtHeader and EvtData containers are initialized.
155//
156Int_t MRawSocketRead::PreProcess(MParList *pList)
157{
158 if (!OpenSocket())
159 return kFALSE;
160
161 //
162 // check if all necessary containers exist in the Parameter list.
163 // if not create one and add them to the list
164 //
165 fRawRunHeader = (MRawRunHeader*)pList->FindCreateObj("MRawRunHeader");
166 if (!fRawRunHeader)
167 return kFALSE;
168
169 fRawEvtHeader = (MRawEvtHeader*)pList->FindCreateObj("MRawEvtHeader");
170 if (!fRawEvtHeader)
171 return kFALSE;
172
173 fRawEvtData = (MRawEvtData*)pList->FindCreateObj("MRawEvtData");
174 if (!fRawEvtData)
175 return kFALSE;
176
177 fRawCrateArray = (MRawCrateArray*)pList->FindCreateObj("MRawCrateArray");
178 if (!fRawCrateArray)
179 return kFALSE;
180
181 fRawEvtTime = (MTime*)pList->FindCreateObj("MTime", "MRawEvtTime");
182 if (!fRawEvtTime)
183 return kTRUE;
184
185 fParList = pList;
186 fRunNumber = (UInt_t)-1;
187 fEvtNumber = (UInt_t)-1;
188
189 /*
190 MEvtLoop *loop=(MEvtLoop*)pList->FindObject("Evtloop");
191 if (loop)
192 loop->SetProgressBar((TGProgressBar*)NULL);
193 */
194 return kTRUE;
195}
196
197// --------------------------------------------------------------------------
198//
199// The Process reads one event from the binary file:
200// - The event header is read
201// - the run header is read
202// - all crate information is read
203// - the raw data information of one event is read
204//
205Int_t MRawSocketRead::Process()
206{
207 /*
208 sprintf(report_str, " %8.8d %8.8d %6.6d",
209 run_header->RunNumber, run.event_number,
210 time.tv_sec - run.time_start_secs);
211 if (send(sd, report_str, strlen(report_str), flags) == -1) {
212 return -1;
213 }
214
215 sprintf(report_str, " %6.1f %6.1f %6.6d %7.4f %8.8d %6.6d ",
216 trigrate_hz, storerate_hz, storerate_kbps, gammarate_hz,
217 diskspace_kb, remtime_sec);
218 if (send(sd, report_str, strlen(report_str), flags) == -1) {
219 return -1;
220 }
221 */
222 char dummy[126];
223 fIn->read(dummy, 4); // \nEVT
224
225 if (!(dummy[0]=='\n' && dummy[1]=='E' && dummy[2]=='V' &&dummy[3]=='T'))
226 {
227 *fLog << warn << "EVT tag not found. Stream out of sync. Please try to restart..." << endl;
228 // FIXME: Synchronization missing...
229 return kFALSE;
230 }
231
232 char size[6] = {0,0,0,0,0,0};
233 fIn->read(size, 5);
234 fIn->read(dummy, 126); //00000[CC-DATA]\n
235
236 /*
237 int numevt;
238 sscanf(dummy, "%*s %*d %*d %*d %*d %*d %*d %*d "
239 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
240 "%d %*d %*f %*f %*d %*f %*d %*d", &numevt);
241 */
242
243 //
244 // Read RUN HEADER (see specification) from input stream
245 //
246 fLog->SetNullOutput();
247 fRawRunHeader->ReadEvt(*fIn);
248 fLog->SetNullOutput(kFALSE);
249
250 if (fRawRunHeader->GetMagicNumber()!=kMagicNumber && fRawRunHeader->GetMagicNumber()!=kMagicNumber+1)
251 {
252 *fLog << err << "Error: Wrong Magic Number (0x" << hex << fRawRunHeader->GetMagicNumber() << "): Not a Magic File!" << endl;
253 return kFALSE;
254 }
255
256 if (fRunNumber!=fRawRunHeader->GetRunNumber())
257 {
258 fRawRunHeader->Print();
259
260 MTaskList *tlist = (MTaskList*)fParList->FindObject("MTaskList");
261 if (!tlist)
262 {
263 *fLog << err << dbginf << "ERROR - Task List not found in Parameter List." << endl;
264 return kFALSE;
265 }
266
267 if (!tlist->ReInit())
268 return kFALSE;
269
270 fRunNumber = fRawRunHeader->GetRunNumber();
271 }
272
273 if (atoi(size)==fRawRunHeader->GetNumTotalBytes())
274 return kCONTINUE;
275
276 //
277 // Give the run header information to the 'sub-classes'
278 //
279 fRawEvtHeader->Init(fRawRunHeader, fRawEvtTime);
280 fRawEvtData ->Init(fRawRunHeader);
281
282 //
283 // Read in the next EVENT HEADER (see specification),
284 // if there is no next event anymore stop eventloop
285 //
286 if (!fRawEvtHeader->ReadEvt(*fIn))
287 return kFALSE;
288
289 //
290 // Get number of crates from the run header
291 //
292 const UShort_t nc = fRawRunHeader->GetNumCrates();
293
294 //
295 // Delete arrays which stores the pixel information (time slices)
296 //
297 fRawEvtData->ResetPixels();
298
299 //
300 // clear the TClonesArray which stores the Crate Information
301 // and create a new array of the correct size
302 //
303 fRawCrateArray->SetSize(nc);
304
305 //
306 // read the CRATE DATA (see specification) from file
307 //
308 for (int i=0; i<nc; i++)
309 {
310 fRawCrateArray->GetEntry(i)->ReadEvt(*fIn);
311 if (!*fIn)
312 return kFALSE;
313
314 fRawEvtData->ReadEvt(*fIn);
315 if (!*fIn)
316 return kFALSE;
317 }
318
319 //
320 // If no new event was recorded the DAQ resends an old event
321 //
322 if (fEvtNumber==fRawEvtHeader->GetDAQEvtNumber())
323 return kCONTINUE;
324
325 fEvtNumber=fRawEvtHeader->GetDAQEvtNumber();
326
327 return kTRUE;
328}
329
330Int_t MRawSocketRead::PostProcess()
331{
332 //
333 // Close Socket connection
334 //
335 fIn->Close();
336
337 return kTRUE;
338}
Note: See TracBrowser for help on using the repository browser.