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

Last change on this file since 3873 was 3183, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 7.6 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-2004
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 if (!MRawRead::PreProcess(pList))
163 return kFALSE;
164
165 fParList = pList;
166 fRunNumber = (UInt_t)-1;
167 fEvtNumber = (UInt_t)-1;
168
169 return kTRUE;
170}
171
172// --------------------------------------------------------------------------
173//
174// The Process reads one event from the binary file:
175// - The event header is read
176// - the run header is read
177// - all crate information is read
178// - the raw data information of one event is read
179//
180Int_t MRawSocketRead::Process()
181{
182 /*
183 sprintf(report_str, " %8.8d %8.8d %6.6d",
184 run_header->RunNumber, run.event_number,
185 time.tv_sec - run.time_start_secs);
186 if (send(sd, report_str, strlen(report_str), flags) == -1) {
187 return -1;
188 }
189
190 sprintf(report_str, " %6.1f %6.1f %6.6d %7.4f %8.8d %6.6d ",
191 trigrate_hz, storerate_hz, storerate_kbps, gammarate_hz,
192 diskspace_kb, remtime_sec);
193 if (send(sd, report_str, strlen(report_str), flags) == -1) {
194 return -1;
195 }
196 */
197 char dummy[126];
198 fIn->read(dummy, 4); // \nEVT
199
200 if (!(dummy[0]=='\n' && dummy[1]=='E' && dummy[2]=='V' &&dummy[3]=='T'))
201 {
202 *fLog << warn << "EVT tag not found. Stream out of sync. Please try to restart..." << endl;
203 // FIXME: Synchronization missing...
204 return kFALSE;
205 }
206
207 char size[6] = {0,0,0,0,0,0};
208 fIn->read(size, 5);
209 fIn->read(dummy, 126); //00000[CC-DATA]\n
210
211 /*
212 int numevt;
213 sscanf(dummy, "%*s %*d %*d %*d %*d %*d %*d %*d "
214 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
215 "%d %*d %*f %*f %*d %*f %*d %*d", &numevt);
216 */
217
218 //
219 // Read RUN HEADER (see specification) from input stream
220 //
221 fLog->SetNullOutput();
222 const Bool_t rc = fRawRunHeader->ReadEvt(*fIn);
223 fLog->SetNullOutput(kFALSE);
224
225 if (!rc)
226 return kFALSE;
227
228 if (fRunNumber!=fRawRunHeader->GetRunNumber())
229 {
230 fRawRunHeader->Print();
231
232 MTaskList *tlist = (MTaskList*)fParList->FindObject("MTaskList");
233 if (!tlist)
234 {
235 *fLog << err << dbginf << "ERROR - Task List not found in Parameter List." << endl;
236 return kFALSE;
237 }
238
239 if (!tlist->ReInit())
240 return kFALSE;
241
242 fRunNumber = fRawRunHeader->GetRunNumber();
243 }
244
245 if (atoi(size)==fRawRunHeader->GetNumTotalBytes())
246 {
247 *fLog << dbg << "Event contains only run header information... skipped." << endl;
248 return kCONTINUE;
249 }
250
251 *fRawEvtTime = fRawRunHeader->GetRunStart();
252
253 //
254 // Give the run header information to the 'sub-classes'
255 // Run header must be valid!
256 //
257 fRawEvtHeader->Init(fRawRunHeader, fRawEvtTime);
258 fRawEvtData ->Init(fRawRunHeader, fRawCrateArray);
259
260 if (!ReadEvent(*fIn))
261 return kFALSE;
262
263 //
264 // If no new event was recorded the DAQ resends an old event
265 //
266 if (fEvtNumber==fRawEvtHeader->GetDAQEvtNumber())
267 {
268 *fLog << dbg << "Event number #" << dec << fEvtNumber << " didn't change... skipped." << endl;
269 return kCONTINUE;
270 }
271
272 fEvtNumber=fRawEvtHeader->GetDAQEvtNumber();
273
274 return kTRUE;
275}
276
277Int_t MRawSocketRead::PostProcess()
278{
279 //
280 // Close Socket connection
281 //
282 fIn->Close();
283
284 return kTRUE;
285}
Note: See TracBrowser for help on using the repository browser.