| 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 | //  MRawFileRead                                                            // | 
|---|
| 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 |  | 
|---|
| 41 | #include "MRawFileRead.h" | 
|---|
| 42 |  | 
|---|
| 43 | #include <fstream> | 
|---|
| 44 |  | 
|---|
| 45 | #include "MLog.h" | 
|---|
| 46 | #include "MLogManip.h" | 
|---|
| 47 |  | 
|---|
| 48 | #include "MTime.h" | 
|---|
| 49 | #include "MParList.h" | 
|---|
| 50 | #include "MRawRunHeader.h" | 
|---|
| 51 | #include "MRawEvtHeader.h" | 
|---|
| 52 | #include "MRawEvtData.h" | 
|---|
| 53 | #include "MRawCrateData.h" | 
|---|
| 54 | #include "MRawCrateArray.h" | 
|---|
| 55 |  | 
|---|
| 56 | ClassImp(MRawFileRead); | 
|---|
| 57 |  | 
|---|
| 58 | using namespace std; | 
|---|
| 59 |  | 
|---|
| 60 | /*  ----------- please don't delete and don't care about (Thomas) ------------ | 
|---|
| 61 | #define kBUFSZ 64 //1024*1024*64 | 
|---|
| 62 | #include <iomanip.h> | 
|---|
| 63 | class bifstream : public istream, public streambuf | 
|---|
| 64 | { | 
|---|
| 65 | private: | 
|---|
| 66 | char fBuffer[kBUFSZ]; //! | 
|---|
| 67 | FILE *fd; | 
|---|
| 68 |  | 
|---|
| 69 | int sync() | 
|---|
| 70 | { | 
|---|
| 71 | memset(fBuffer, 0, kBUFSZ); | 
|---|
| 72 | return 0; | 
|---|
| 73 | } | 
|---|
| 74 | int underflow() | 
|---|
| 75 | { | 
|---|
| 76 | int sz=fread(fBuffer, kBUFSZ, 1, fd); | 
|---|
| 77 | //int sz=fread(fBuffer, 1, kBUFSZ, fd); | 
|---|
| 78 | setg(fBuffer, fBuffer, fBuffer+kBUFSZ); | 
|---|
| 79 |  | 
|---|
| 80 | return sz==1 ? *(unsigned char*)fBuffer : EOF;//EOF; | 
|---|
| 81 | //return sz==kBUFSZ ? *(unsigned char*)fBuffer : EOF;//EOF; | 
|---|
| 82 | } | 
|---|
| 83 | public: | 
|---|
| 84 | bifstream(const char *name) : istream(this) | 
|---|
| 85 | { | 
|---|
| 86 | fd = fopen(name, "rb"); | 
|---|
| 87 | setbuf(fBuffer, kBUFSZ); | 
|---|
| 88 | } | 
|---|
| 89 | }; | 
|---|
| 90 | */ | 
|---|
| 91 |  | 
|---|
| 92 | // -------------------------------------------------------------------------- | 
|---|
| 93 | // | 
|---|
| 94 | // Default constructor. It tries to open the given file. | 
|---|
| 95 | // | 
|---|
| 96 | MRawFileRead::MRawFileRead(const char *fname, const char *name, const char *title) | 
|---|
| 97 | : fFileName(fname), fIn(NULL) | 
|---|
| 98 | { | 
|---|
| 99 | fName  = name  ? name  : "MRawFileRead"; | 
|---|
| 100 | fTitle = title ? title : "Read task to read DAQ binary files"; | 
|---|
| 101 |  | 
|---|
| 102 | fIn = new ifstream; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | // -------------------------------------------------------------------------- | 
|---|
| 106 | // | 
|---|
| 107 | // Destructor. Delete input stream. | 
|---|
| 108 | // | 
|---|
| 109 | MRawFileRead::~MRawFileRead() | 
|---|
| 110 | { | 
|---|
| 111 | delete fIn; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | // -------------------------------------------------------------------------- | 
|---|
| 115 | // | 
|---|
| 116 | // The PreProcess of this task checks for the following containers in the | 
|---|
| 117 | // list: | 
|---|
| 118 | //   MRawRunHeader <output>   if not found it is created | 
|---|
| 119 | //   MRawEvtHeader <output>   if not found it is created | 
|---|
| 120 | //   MRawEvtData <output>     if not found it is created | 
|---|
| 121 | //   MRawCrateArray <output>  if not found it is created | 
|---|
| 122 | //   MRawEvtTime <output>     if not found it is created (MTime) | 
|---|
| 123 | // | 
|---|
| 124 | // If all containers are found or created the run header is read from the | 
|---|
| 125 | // binary file and printed.  If the Magic-Number (file identification) | 
|---|
| 126 | // doesn't match we stop the eventloop. | 
|---|
| 127 | // | 
|---|
| 128 | // Now the EvtHeader and EvtData containers are initialized. | 
|---|
| 129 | // | 
|---|
| 130 | Int_t MRawFileRead::PreProcess(MParList *pList) | 
|---|
| 131 | { | 
|---|
| 132 | // | 
|---|
| 133 | // open the input stream | 
|---|
| 134 | // first of all check if opening the file in the constructor was | 
|---|
| 135 | // successfull | 
|---|
| 136 | // | 
|---|
| 137 | fIn->open(fFileName); | 
|---|
| 138 | if (!(*fIn)) | 
|---|
| 139 | { | 
|---|
| 140 | *fLog << err << "Error: Cannot open file '" << fFileName << "'" << endl; | 
|---|
| 141 | return kFALSE; | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | // | 
|---|
| 145 | //  check if all necessary containers exist in the Parameter list. | 
|---|
| 146 | //  if not create one and add them to the list | 
|---|
| 147 | // | 
|---|
| 148 | fRawRunHeader = (MRawRunHeader*)pList->FindCreateObj("MRawRunHeader"); | 
|---|
| 149 | if (!fRawRunHeader) | 
|---|
| 150 | return kFALSE; | 
|---|
| 151 |  | 
|---|
| 152 | fRawEvtHeader = (MRawEvtHeader*)pList->FindCreateObj("MRawEvtHeader"); | 
|---|
| 153 | if (!fRawEvtHeader) | 
|---|
| 154 | return kFALSE; | 
|---|
| 155 |  | 
|---|
| 156 | fRawEvtData = (MRawEvtData*)pList->FindCreateObj("MRawEvtData"); | 
|---|
| 157 | if (!fRawEvtData) | 
|---|
| 158 | return kFALSE; | 
|---|
| 159 |  | 
|---|
| 160 | fRawCrateArray = (MRawCrateArray*)pList->FindCreateObj("MRawCrateArray"); | 
|---|
| 161 | if (!fRawCrateArray) | 
|---|
| 162 | return kFALSE; | 
|---|
| 163 |  | 
|---|
| 164 | fRawEvtTime = (MTime*)pList->FindCreateObj("MTime", "MRawEvtTime"); | 
|---|
| 165 | if (!fRawEvtTime) | 
|---|
| 166 | return kTRUE; | 
|---|
| 167 |  | 
|---|
| 168 | // | 
|---|
| 169 | // Read RUN HEADER (see specification) from input stream | 
|---|
| 170 | // | 
|---|
| 171 | fRawRunHeader->ReadEvt(*fIn); | 
|---|
| 172 | fRawRunHeader->Print(); | 
|---|
| 173 |  | 
|---|
| 174 | if (fRawRunHeader->GetMagicNumber()!=kMagicNumber) | 
|---|
| 175 | return kFALSE; | 
|---|
| 176 |  | 
|---|
| 177 | // | 
|---|
| 178 | // Give the run header information to the 'sub-classes' | 
|---|
| 179 | // | 
|---|
| 180 | fRawEvtHeader->Init(fRawRunHeader, fRawEvtTime); | 
|---|
| 181 | fRawEvtData  ->Init(fRawRunHeader); | 
|---|
| 182 |  | 
|---|
| 183 | return kTRUE; | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | // -------------------------------------------------------------------------- | 
|---|
| 187 | // | 
|---|
| 188 | // The Process reads one event from the binary file: | 
|---|
| 189 | //  - The event header is read | 
|---|
| 190 | //  - the run header is read | 
|---|
| 191 | //  - all crate information is read | 
|---|
| 192 | //  - the raw data information of one event is read | 
|---|
| 193 | // | 
|---|
| 194 | Int_t MRawFileRead::Process() | 
|---|
| 195 | { | 
|---|
| 196 | // | 
|---|
| 197 | //  Read in the next EVENT HEADER (see specification), | 
|---|
| 198 | // if there is no next event anymore stop eventloop | 
|---|
| 199 | // | 
|---|
| 200 | if (!fRawEvtHeader->ReadEvt(*fIn)) | 
|---|
| 201 | return kFALSE; | 
|---|
| 202 |  | 
|---|
| 203 | // | 
|---|
| 204 | //  Get number of crates from the run header | 
|---|
| 205 | // | 
|---|
| 206 | const UShort_t nc = fRawRunHeader->GetNumCrates(); | 
|---|
| 207 |  | 
|---|
| 208 | // | 
|---|
| 209 | // Delete arrays which stores the pixel information (time slices) | 
|---|
| 210 | // | 
|---|
| 211 | fRawEvtData->ResetPixels(); | 
|---|
| 212 |  | 
|---|
| 213 | // | 
|---|
| 214 | // clear the TClonesArray which stores the Crate Information | 
|---|
| 215 | // and create a new array of the correct size | 
|---|
| 216 | // | 
|---|
| 217 | fRawCrateArray->SetSize(nc); | 
|---|
| 218 |  | 
|---|
| 219 | // | 
|---|
| 220 | // read the CRATE DATA (see specification) from file | 
|---|
| 221 | // | 
|---|
| 222 | for (int i=0; i<nc; i++) | 
|---|
| 223 | { | 
|---|
| 224 | fRawCrateArray->GetEntry(i)->ReadEvt(*fIn); | 
|---|
| 225 | if (!*fIn) | 
|---|
| 226 | return kFALSE; | 
|---|
| 227 |  | 
|---|
| 228 | fRawEvtData->ReadEvt(*fIn); | 
|---|
| 229 | if (!*fIn) | 
|---|
| 230 | return kFALSE; | 
|---|
| 231 | } | 
|---|
| 232 |  | 
|---|
| 233 | return kTRUE; | 
|---|
| 234 | } | 
|---|
| 235 |  | 
|---|
| 236 | // -------------------------------------------------------------------------- | 
|---|
| 237 | // | 
|---|
| 238 | //  Close the file. Check whether the number of read events differs from | 
|---|
| 239 | //  the number the file should containe (MRawRunHeader). Prints a warning | 
|---|
| 240 | //  if it doesn't match. | 
|---|
| 241 | // | 
|---|
| 242 | Int_t MRawFileRead::PostProcess() | 
|---|
| 243 | { | 
|---|
| 244 | // | 
|---|
| 245 | // Sanity check for the number of events | 
|---|
| 246 | // | 
|---|
| 247 | if (fRawRunHeader->GetNumEvents() == GetNumExecutions()-1) | 
|---|
| 248 | return kTRUE; | 
|---|
| 249 |  | 
|---|
| 250 | *fLog << warn << "Warning - number of read events (" << GetNumExecutions()-1; | 
|---|
| 251 | *fLog << ") doesn't match number in run header ("; | 
|---|
| 252 | *fLog << fRawRunHeader->GetNumEvents() << ")." << endl; | 
|---|
| 253 |  | 
|---|
| 254 | return kTRUE; | 
|---|
| 255 | } | 
|---|