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

Last change on this file since 2445 was 2386, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 8.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 12/2000 <mailto:tbretz@uni-sw.gwdg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2001
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, MRawEvtHeader, MRawEvtData, MRawCrateArray, MRawEvtTime //
38// //
39//////////////////////////////////////////////////////////////////////////////
40#include "MRawSocketRead.h"
41
42#include <TMutex.h>
43
44#include "MReadSocket.h"
45
46#include "MLog.h"
47#include "MLogManip.h"
48
49#include "MParList.h"
50#include "MTaskList.h"
51#include "MEvtLoop.h"
52
53#include "MTime.h"
54#include "MRawRunHeader.h"
55#include "MRawEvtHeader.h"
56#include "MRawEvtData.h"
57#include "MRawCrateData.h"
58#include "MRawCrateArray.h"
59
60ClassImp(MRawSocketRead);
61
62using namespace std;
63
64// --------------------------------------------------------------------------
65//
66// Default constructor. It tries to open the given file.
67//
68MRawSocketRead::MRawSocketRead(const char *name, const char *title)
69 : fIn(NULL)
70{
71 fName = name ? name : "MRawSocketRead";
72 fTitle = title ? title : "Task to read DAQ binary data from tcp/ip socket";
73
74 fIn = new MReadSocket(7000);
75 fMutex = new TMutex;
76}
77
78// --------------------------------------------------------------------------
79//
80// Destructor. Delete input stream.
81//
82MRawSocketRead::~MRawSocketRead()
83{
84 delete fMutex;
85 delete fIn;
86}
87
88// --------------------------------------------------------------------------
89//
90// The PreProcess of this task checks for the following containers in the
91// list:
92// MRawRunHeader <output> if not found it is created
93// MRawEvtHeader <output> if not found it is created
94// MRawEvtData <output> if not found it is created
95// MRawCrateArray <output> if not found it is created
96// MRawEvtTime <output> if not found it is created (MTime)
97//
98// If all containers are found or created the run header is read from the
99// binary file and printed. If the Magic-Number (file identification)
100// doesn't match we stop the eventloop.
101//
102// Now the EvtHeader and EvtData containers are initialized.
103//
104Int_t MRawSocketRead::PreProcess(MParList *pList)
105{
106 //
107 // check if all necessary containers exist in the Parameter list.
108 // if not create one and add them to the list
109 //
110 fRawRunHeader = (MRawRunHeader*)pList->FindCreateObj("MRawRunHeader");
111 if (!fRawRunHeader)
112 return kFALSE;
113
114 fRawEvtHeader = (MRawEvtHeader*)pList->FindCreateObj("MRawEvtHeader");
115 if (!fRawEvtHeader)
116 return kFALSE;
117
118 fRawEvtData = (MRawEvtData*)pList->FindCreateObj("MRawEvtData");
119 if (!fRawEvtData)
120 return kFALSE;
121
122 fRawCrateArray = (MRawCrateArray*)pList->FindCreateObj("MRawCrateArray");
123 if (!fRawCrateArray)
124 return kFALSE;
125
126 fRawEvtTime = (MTime*)pList->FindCreateObj("MTime", "MRawEvtTime");
127 if (!fRawEvtTime)
128 return kTRUE;
129
130 fParList = pList;
131 fRunNumber = (UInt_t)-1;
132 fEvtNumber = (UInt_t)-1;
133
134 /*
135 MEvtLoop *loop=(MEvtLoop*)pList->FindObject("Evtloop");
136 if (loop)
137 loop->SetProgressBar((TGProgressBar*)NULL);
138 */
139 return kTRUE;
140}
141
142Int_t MRawSocketRead::Do()
143{
144 /*
145 sprintf(report_str, " %8.8d %8.8d %6.6d",
146 run_header->RunNumber, run.event_number,
147 time.tv_sec - run.time_start_secs);
148 if (send(sd, report_str, strlen(report_str), flags) == -1) {
149 return -1;
150 }
151
152 sprintf(report_str, " %6.1f %6.1f %6.6d %7.4f %8.8d %6.6d ",
153 trigrate_hz, storerate_hz, storerate_kbps, gammarate_hz,
154 diskspace_kb, remtime_sec);
155 if (send(sd, report_str, strlen(report_str), flags) == -1) {
156 return -1;
157 }
158 */
159 char dummy[126];
160 fIn->read(dummy, 4); // \nEVT
161
162 if (!(dummy[0]=='\n' && dummy[1]=='E' && dummy[2]=='V' &&dummy[3]=='T'))
163 {
164 *fLog << warn << "EVT tag not found. Stream out of sync. Please try to restart..." << endl;
165 // FIXME: Synchronization missing...
166 return kFALSE;
167 }
168
169 char size[6] = {0,0,0,0,0,0};
170 fIn->read(size, 5);
171 fIn->read(dummy, 126); //00000[CC-DATA]\n
172
173 /*
174 int numevt;
175 sscanf(dummy, "%*s %*d %*d %*d %*d %*d %*d %*d "
176 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
177 "%d %*d %*f %*f %*d %*f %*d %*d", &numevt);
178 */
179
180 //
181 // Read RUN HEADER (see specification) from input stream
182 //
183 fLog->SetNullOutput();
184 fRawRunHeader->ReadEvt(*fIn);
185 fLog->SetNullOutput(kFALSE);
186
187 if (fRawRunHeader->GetMagicNumber()!=kMagicNumber && fRawRunHeader->GetMagicNumber()!=kMagicNumber+1)
188 {
189 *fLog << err << "Error: Wrong Magic Number (0x" << hex << fRawRunHeader->GetMagicNumber() << "): Not a Magic File!" << endl;
190 return kFALSE;
191 }
192
193 if (fRunNumber!=fRawRunHeader->GetRunNumber())
194 {
195 fRawRunHeader->Print();
196
197 MTaskList *tlist = (MTaskList*)fParList->FindObject("MTaskList");
198 if (!tlist)
199 {
200 *fLog << err << dbginf << "ERROR - Task List not found in Parameter List." << endl;
201 return kFALSE;
202 }
203
204 if (!tlist->ReInit())
205 return kFALSE;
206
207 fRunNumber = fRawRunHeader->GetRunNumber();
208 }
209
210 if (atoi(size)==fRawRunHeader->GetNumTotalBytes())
211 return kCONTINUE;
212
213 //
214 // Give the run header information to the 'sub-classes'
215 //
216 fRawEvtHeader->Init(fRawRunHeader, fRawEvtTime);
217 fRawEvtData ->Init(fRawRunHeader);
218
219 //
220 // Read in the next EVENT HEADER (see specification),
221 // if there is no next event anymore stop eventloop
222 //
223 if (!fRawEvtHeader->ReadEvt(*fIn))
224 return kFALSE;
225
226 //
227 // Get number of crates from the run header
228 //
229 const UShort_t nc = fRawRunHeader->GetNumCrates();
230
231 //
232 // Delete arrays which stores the pixel information (time slices)
233 //
234 fRawEvtData->ResetPixels();
235
236 //
237 // clear the TClonesArray which stores the Crate Information
238 // and create a new array of the correct size
239 //
240 fRawCrateArray->SetSize(nc);
241
242 //
243 // read the CRATE DATA (see specification) from file
244 //
245 for (int i=0; i<nc; i++)
246 {
247 fRawCrateArray->GetEntry(i)->ReadEvt(*fIn);
248 if (!*fIn)
249 return kFALSE;
250
251 fRawEvtData->ReadEvt(*fIn);
252 if (!*fIn)
253 return kFALSE;
254 }
255
256 if (fEvtNumber==fRawEvtHeader->GetDAQEvtNumber())
257 return kCONTINUE;
258
259 fEvtNumber=fRawEvtHeader->GetDAQEvtNumber();
260
261 //*fLog << dbg << "Evt " << fRawEvtHeader->GetDAQEvtNumber() << endl;
262
263 return kTRUE;
264}
265
266// --------------------------------------------------------------------------
267//
268// The Process reads one event from the binary file:
269// - The event header is read
270// - the run header is read
271// - all crate information is read
272// - the raw data information of one event is read
273//
274Int_t MRawSocketRead::Process()
275{
276 Lock();
277 Int_t rc = Do();
278 UnLock();
279 return rc;
280}
281
282Int_t MRawSocketRead::Lock()
283{
284 return fMutex->Lock();
285}
286
287Int_t MRawSocketRead::UnLock()
288{
289 return fMutex->UnLock();
290}
Note: See TracBrowser for help on using the repository browser.