| 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 <TArrayC.h> // TAraayC | 
|---|
| 49 |  | 
|---|
| 50 | #include "MReadSocket.h" | 
|---|
| 51 |  | 
|---|
| 52 | #include "MLog.h" | 
|---|
| 53 | #include "MLogManip.h" | 
|---|
| 54 |  | 
|---|
| 55 | #include "MParList.h" | 
|---|
| 56 | #include "MTaskList.h" | 
|---|
| 57 | #include "MEvtLoop.h" | 
|---|
| 58 |  | 
|---|
| 59 | #include "MTime.h" | 
|---|
| 60 | #include "MRawRunHeader.h" | 
|---|
| 61 | #include "MRawEvtHeader.h" | 
|---|
| 62 | #include "MRawEvtData.h" | 
|---|
| 63 | #include "MRawCrateData.h" | 
|---|
| 64 | #include "MRawCrateArray.h" | 
|---|
| 65 |  | 
|---|
| 66 | #include "MStatusDisplay.h" | 
|---|
| 67 |  | 
|---|
| 68 | ClassImp(MRawSocketRead); | 
|---|
| 69 |  | 
|---|
| 70 | using namespace std; | 
|---|
| 71 |  | 
|---|
| 72 | // -------------------------------------------------------------------------- | 
|---|
| 73 | // | 
|---|
| 74 | // Default constructor. It tries to open the given file. | 
|---|
| 75 | // | 
|---|
| 76 | MRawSocketRead::MRawSocketRead(const char *name, const char *title) | 
|---|
| 77 | : fIn(NULL), fPort(-1) | 
|---|
| 78 | { | 
|---|
| 79 | fName  = name  ? name  : "MRawSocketRead"; | 
|---|
| 80 | fTitle = title ? title : "Task to read DAQ binary data from tcp/ip socket"; | 
|---|
| 81 |  | 
|---|
| 82 | fIn = new MReadSocket; | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | // -------------------------------------------------------------------------- | 
|---|
| 86 | // | 
|---|
| 87 | // Destructor. Delete input stream. | 
|---|
| 88 | // | 
|---|
| 89 | MRawSocketRead::~MRawSocketRead() | 
|---|
| 90 | { | 
|---|
| 91 | delete fIn; | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | // -------------------------------------------------------------------------- | 
|---|
| 95 | // | 
|---|
| 96 | // Open the socket. This blocks until the connection has been established, | 
|---|
| 97 | // an error occured opening the connection or the user requested to | 
|---|
| 98 | // quit the application. | 
|---|
| 99 | // | 
|---|
| 100 | Bool_t MRawSocketRead::OpenSocket() | 
|---|
| 101 | { | 
|---|
| 102 | if (fDisplay) | 
|---|
| 103 | fDisplay->SetStatusLine2(Form("Waiting for connection on port #%d...", fPort)); | 
|---|
| 104 |  | 
|---|
| 105 | // | 
|---|
| 106 | // Open socket connection | 
|---|
| 107 | // | 
|---|
| 108 | while (1) | 
|---|
| 109 | { | 
|---|
| 110 | // | 
|---|
| 111 | // If port could be opened eveything is ok | 
|---|
| 112 | // | 
|---|
| 113 | if (fIn->Open(fPort)) | 
|---|
| 114 | return kTRUE; | 
|---|
| 115 |  | 
|---|
| 116 | // | 
|---|
| 117 | // If a MStatusDisplay is attached the user might have | 
|---|
| 118 | // requested to quit the application | 
|---|
| 119 | // | 
|---|
| 120 | if (fDisplay) | 
|---|
| 121 | switch (fDisplay->CheckStatus()) | 
|---|
| 122 | { | 
|---|
| 123 | case MStatusDisplay::kFileClose: | 
|---|
| 124 | case MStatusDisplay::kFileExit: | 
|---|
| 125 | *fLog << inf << "MRawSocketRead::PreProcess - MStatusDisplay triggered exit." << endl; | 
|---|
| 126 | return kFALSE; | 
|---|
| 127 | default: | 
|---|
| 128 | break; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | // | 
|---|
| 132 | // If an error occured during opening the socket stop | 
|---|
| 133 | // | 
|---|
| 134 | if (fIn->fail()) | 
|---|
| 135 | break; | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | *fLog << err << "ERROR - Cannot open port #" << fPort << endl; | 
|---|
| 139 |  | 
|---|
| 140 | return kFALSE; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | // -------------------------------------------------------------------------- | 
|---|
| 144 | // | 
|---|
| 145 | // The PreProcess of this task checks for the following containers in the | 
|---|
| 146 | // list: | 
|---|
| 147 | //   MRawRunHeader <output>   if not found it is created | 
|---|
| 148 | //   MRawEvtHeader <output>   if not found it is created | 
|---|
| 149 | //   MRawEvtData <output>     if not found it is created | 
|---|
| 150 | //   MRawCrateArray <output>  if not found it is created | 
|---|
| 151 | //   MTime <output>           if not found it is created | 
|---|
| 152 | // | 
|---|
| 153 | // If all containers are found or created the run header is read from the | 
|---|
| 154 | // binary file and printed.  If the Magic-Number (file identification) | 
|---|
| 155 | // doesn't match we stop the eventloop. | 
|---|
| 156 | // | 
|---|
| 157 | // Now the EvtHeader and EvtData containers are initialized. | 
|---|
| 158 | // | 
|---|
| 159 | Int_t MRawSocketRead::PreProcess(MParList *pList) | 
|---|
| 160 | { | 
|---|
| 161 | if (!OpenSocket()) | 
|---|
| 162 | return kFALSE; | 
|---|
| 163 |  | 
|---|
| 164 | if (!MRawRead::PreProcess(pList)) | 
|---|
| 165 | return kFALSE; | 
|---|
| 166 |  | 
|---|
| 167 | fParList = pList; | 
|---|
| 168 | fRunNumber = (UInt_t)-1; | 
|---|
| 169 | fEvtNumber = (UInt_t)-1; | 
|---|
| 170 |  | 
|---|
| 171 | return kTRUE; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | // -------------------------------------------------------------------------- | 
|---|
| 175 | // | 
|---|
| 176 | // The Process reads one event from the binary file: | 
|---|
| 177 | //  - The event header is read | 
|---|
| 178 | //  - the run header is read | 
|---|
| 179 | //  - all crate information is read | 
|---|
| 180 | //  - the raw data information of one event is read | 
|---|
| 181 | // | 
|---|
| 182 | Int_t MRawSocketRead::Process() | 
|---|
| 183 | { | 
|---|
| 184 | // | 
|---|
| 185 | // Tag which could possibly be used for synchronizing the | 
|---|
| 186 | // data stream. At the moment we check only its correctness. | 
|---|
| 187 | // Synchronisation seems to work well - Why? | 
|---|
| 188 | // | 
|---|
| 189 | char dummy[4]; | 
|---|
| 190 | fIn->read(dummy, 4);   // \nEVT | 
|---|
| 191 |  | 
|---|
| 192 | if (!(dummy[0]=='\n' && dummy[1]=='E' && dummy[2]=='V' &&dummy[3]=='T')) | 
|---|
| 193 | { | 
|---|
| 194 | *fLog << warn << "EVT tag not found. Stream out of sync. Please try to restart..." << endl; | 
|---|
| 195 | // FIXME: Synchronization missing... | 
|---|
| 196 | return kFALSE; | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 199 | // | 
|---|
| 200 | // No we get some size information (each 5 bytes ascii) | 
|---|
| 201 | // | 
|---|
| 202 | char sizecc[6] = {0,0,0,0,0,0}; // 5 bytes plus trailing 0-byte | 
|---|
| 203 | char sizerh[6] = {0,0,0,0,0,0}; // 5 bytes plus trailing 0-byte | 
|---|
| 204 | char sizeev[6] = {0,0,0,0,0,0}; // 5 bytes plus trailing 0-byte | 
|---|
| 205 | fIn->read(sizecc, 5); // Size CC info string | 
|---|
| 206 | fIn->read(sizerh, 5); // Size run header | 
|---|
| 207 | fIn->read(sizeev, 5); // Size Event (+ event header) | 
|---|
| 208 |  | 
|---|
| 209 | // | 
|---|
| 210 | // Currently we skip the CC info string. We may decode this string | 
|---|
| 211 | // in the future to get additional information | 
|---|
| 212 | // | 
|---|
| 213 | TArrayC dummy2(atoi(sizecc)); | 
|---|
| 214 | fIn->read(dummy2.GetArray(), dummy2.GetSize()); | 
|---|
| 215 |  | 
|---|
| 216 | // | 
|---|
| 217 | // Read RUN HEADER (see specification) from input stream | 
|---|
| 218 | // | 
|---|
| 219 | fLog->SetNullOutput(); | 
|---|
| 220 | const Bool_t rc = fRawRunHeader->ReadEvt(*fIn); | 
|---|
| 221 | fLog->SetNullOutput(kFALSE); | 
|---|
| 222 |  | 
|---|
| 223 | if (!rc) | 
|---|
| 224 | { | 
|---|
| 225 | *fLog << err << "Reading MRawRunHeader failed." << endl; | 
|---|
| 226 | return kFALSE; | 
|---|
| 227 | } | 
|---|
| 228 |  | 
|---|
| 229 | if (fRunNumber!=fRawRunHeader->GetRunNumber()) | 
|---|
| 230 | { | 
|---|
| 231 | fRawRunHeader->Print(); | 
|---|
| 232 |  | 
|---|
| 233 | MTaskList *tlist = (MTaskList*)fParList->FindObject("MTaskList"); | 
|---|
| 234 | if (!tlist) | 
|---|
| 235 | { | 
|---|
| 236 | *fLog << err << dbginf << "ERROR - Task List not found in Parameter List." << endl; | 
|---|
| 237 | return kFALSE; | 
|---|
| 238 | } | 
|---|
| 239 |  | 
|---|
| 240 | if (!tlist->ReInit()) | 
|---|
| 241 | return kFALSE; | 
|---|
| 242 |  | 
|---|
| 243 | fRunNumber = fRawRunHeader->GetRunNumber(); | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | if (atoi(sizerh)==fRawRunHeader->GetNumTotalBytes()) | 
|---|
| 247 | { | 
|---|
| 248 | *fLog << err << "Retrieved size of run header mismatch... stopped." << endl; | 
|---|
| 249 | return kFALSE; | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 | if (atoi(sizeev)==0) | 
|---|
| 253 | { | 
|---|
| 254 | *fLog << dbg << "Event contains only run header information... skipped." << endl; | 
|---|
| 255 | return kCONTINUE; | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 | *fRawEvtTime = fRawRunHeader->GetRunStart(); | 
|---|
| 259 |  | 
|---|
| 260 | // | 
|---|
| 261 | // Give the run header information to the 'sub-classes' | 
|---|
| 262 | // Run header must be valid! | 
|---|
| 263 | // | 
|---|
| 264 | fRawEvtHeader->InitRead(fRawRunHeader, fRawEvtTime); | 
|---|
| 265 | fRawEvtData1 ->InitRead(fRawRunHeader); | 
|---|
| 266 | fRawEvtData2 ->InitRead(fRawRunHeader); | 
|---|
| 267 |  | 
|---|
| 268 | if (!ReadEvent(*fIn)) | 
|---|
| 269 | return kFALSE; | 
|---|
| 270 |  | 
|---|
| 271 | // | 
|---|
| 272 | // If no new event was recorded the DAQ resends an old event | 
|---|
| 273 | // | 
|---|
| 274 | if (fEvtNumber==fRawEvtHeader->GetDAQEvtNumber()) | 
|---|
| 275 | { | 
|---|
| 276 | *fLog << dbg << "Event number #" << dec << fEvtNumber << " didn't change... skipped." << endl; | 
|---|
| 277 | return kCONTINUE; | 
|---|
| 278 | } | 
|---|
| 279 |  | 
|---|
| 280 | fEvtNumber=fRawEvtHeader->GetDAQEvtNumber(); | 
|---|
| 281 |  | 
|---|
| 282 | return kTRUE; | 
|---|
| 283 | } | 
|---|
| 284 |  | 
|---|
| 285 | Int_t MRawSocketRead::PostProcess() | 
|---|
| 286 | { | 
|---|
| 287 | // | 
|---|
| 288 | // Close Socket connection | 
|---|
| 289 | // | 
|---|
| 290 | fIn->Close(); | 
|---|
| 291 |  | 
|---|
| 292 | return kTRUE; | 
|---|
| 293 | } | 
|---|