| 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 | //  MRawRead | 
|---|
| 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 | //   MRawEvtData2 [MRawEvtData] | 
|---|
| 41 | //   MRawCrateArray | 
|---|
| 42 | //   MTime | 
|---|
| 43 | // | 
|---|
| 44 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 45 | #include "MRawRead.h" | 
|---|
| 46 |  | 
|---|
| 47 | #include <fstream> | 
|---|
| 48 |  | 
|---|
| 49 | #include "MLog.h" | 
|---|
| 50 | #include "MLogManip.h" | 
|---|
| 51 |  | 
|---|
| 52 | #include "MTime.h" | 
|---|
| 53 | #include "MParList.h" | 
|---|
| 54 | #include "MRawRunHeader.h" | 
|---|
| 55 | #include "MRawEvtHeader.h" | 
|---|
| 56 | #include "MRawEvtData.h" | 
|---|
| 57 | #include "MRawCrateData.h" | 
|---|
| 58 | #include "MRawCrateArray.h" | 
|---|
| 59 |  | 
|---|
| 60 | ClassImp(MRawRead); | 
|---|
| 61 |  | 
|---|
| 62 | using namespace std; | 
|---|
| 63 |  | 
|---|
| 64 | // -------------------------------------------------------------------------- | 
|---|
| 65 | // | 
|---|
| 66 | // Default constructor. It tries to open the given file. | 
|---|
| 67 | // | 
|---|
| 68 | MRawRead::MRawRead(const char *name, const char *title) | 
|---|
| 69 | : fForceMode(kFALSE) | 
|---|
| 70 | { | 
|---|
| 71 | fName  = name  ? name  : "MRawRead"; | 
|---|
| 72 | fTitle = title ? title : "Bas class for reading DAQ files"; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | // -------------------------------------------------------------------------- | 
|---|
| 76 | // | 
|---|
| 77 | // The PreProcess of this task checks for the following containers in the | 
|---|
| 78 | // list: | 
|---|
| 79 | //   MRawRunHeader <output>   if not found it is created | 
|---|
| 80 | //   MRawEvtHeader <output>   if not found it is created | 
|---|
| 81 | //   MRawEvtData <output>     if not found it is created | 
|---|
| 82 | //   MRawCrateArray <output>  if not found it is created | 
|---|
| 83 | //   MRawEvtTime <output>     if not found it is created (MTime) | 
|---|
| 84 | // | 
|---|
| 85 | // If all containers are found or created the run header is read from the | 
|---|
| 86 | // binary file and printed.  If the Magic-Number (file identification) | 
|---|
| 87 | // doesn't match we stop the eventloop. | 
|---|
| 88 | // | 
|---|
| 89 | // Now the EvtHeader and EvtData containers are initialized. | 
|---|
| 90 | // | 
|---|
| 91 | Int_t MRawRead::PreProcess(MParList *pList) | 
|---|
| 92 | { | 
|---|
| 93 | if (!OpenStream()) | 
|---|
| 94 | return kFALSE; | 
|---|
| 95 |  | 
|---|
| 96 | // | 
|---|
| 97 | //  check if all necessary containers exist in the Parameter list. | 
|---|
| 98 | //  if not create one and add them to the list | 
|---|
| 99 | // | 
|---|
| 100 | fRawRunHeader = (MRawRunHeader*)pList->FindCreateObj("MRawRunHeader"); | 
|---|
| 101 | if (!fRawRunHeader) | 
|---|
| 102 | return kFALSE; | 
|---|
| 103 |  | 
|---|
| 104 | fRawEvtHeader = (MRawEvtHeader*)pList->FindCreateObj("MRawEvtHeader"); | 
|---|
| 105 | if (!fRawEvtHeader) | 
|---|
| 106 | return kFALSE; | 
|---|
| 107 |  | 
|---|
| 108 | fRawEvtData1 = (MRawEvtData*)pList->FindCreateObj("MRawEvtData"); | 
|---|
| 109 | if (!fRawEvtData1) | 
|---|
| 110 | return kFALSE; | 
|---|
| 111 |  | 
|---|
| 112 | fRawEvtData2 = (MRawEvtData*)pList->FindCreateObj("MRawEvtData", "MRawEvtData2"); | 
|---|
| 113 | if (!fRawEvtData2) | 
|---|
| 114 | return kFALSE; | 
|---|
| 115 |  | 
|---|
| 116 | fRawCrateArray = (MRawCrateArray*)pList->FindCreateObj("MRawCrateArray"); | 
|---|
| 117 | if (!fRawCrateArray) | 
|---|
| 118 | return kFALSE; | 
|---|
| 119 |  | 
|---|
| 120 | fRawEvtTime = (MTime*)pList->FindCreateObj("MTime"); | 
|---|
| 121 | if (!fRawEvtTime) | 
|---|
| 122 | return kFALSE; | 
|---|
| 123 |  | 
|---|
| 124 | return kTRUE; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | // -------------------------------------------------------------------------- | 
|---|
| 128 | // | 
|---|
| 129 | // This is a workaround for the oldest runs (file version<3) | 
|---|
| 130 | // for which no time stamp was available. | 
|---|
| 131 | // For this runs a fake time stamp is created | 
|---|
| 132 | // | 
|---|
| 133 | // Be carefull: This is NOT thread safe! | 
|---|
| 134 | // | 
|---|
| 135 | void MRawRead::CreateFakeTime() const | 
|---|
| 136 | { | 
|---|
| 137 | static Double_t tm = 0; // Range of roughly 8min | 
|---|
| 138 | const UInt_t ct = (*fRawCrateArray)[0]->GetFADCClockTick(); | 
|---|
| 139 |  | 
|---|
| 140 | tm = ct<tm ? fmod(tm, (UInt_t)(-1))+(UInt_t)(-1)+ct : ct; | 
|---|
| 141 |  | 
|---|
| 142 | const Double_t mhz = 9.375;                        // [1e6 ticks/s] | 
|---|
| 143 | const Double_t t   = tm/mhz;                       // [us] | 
|---|
| 144 | const UInt_t ns    = (UInt_t)fmod(t*1e3, 1e6); | 
|---|
| 145 | //const UShort_t ms  = (UShort_t)fmod(t/1e3, 1e3); | 
|---|
| 146 | const Byte_t s     = (Byte_t)fmod(t/1e6, 60); | 
|---|
| 147 |  | 
|---|
| 148 | // Create an artificial time stamp! | 
|---|
| 149 | UInt_t m = (Byte_t)fmod(t/60e6, 60); | 
|---|
| 150 | //const Byte_t h     = (Byte_t)(t/3600e6); | 
|---|
| 151 | m += fRawRunHeader->GetRunNumber()*10; | 
|---|
| 152 | m %= 360; // 6h | 
|---|
| 153 |  | 
|---|
| 154 | fRawEvtTime->UpdMagicTime(m/60, m%60, s, ns); | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | // -------------------------------------------------------------------------- | 
|---|
| 158 | // | 
|---|
| 159 | // Read a single event from the stream | 
|---|
| 160 | // | 
|---|
| 161 | Bool_t MRawRead::ReadEvent(istream &fin) | 
|---|
| 162 | { | 
|---|
| 163 | // | 
|---|
| 164 | //  Get file format version | 
|---|
| 165 | // | 
|---|
| 166 | const UShort_t ver = fRawRunHeader->GetFormatVersion(); | 
|---|
| 167 |  | 
|---|
| 168 | // | 
|---|
| 169 | // Read in the next EVENT HEADER (see specification), | 
|---|
| 170 | // if there is no next event anymore stop eventloop | 
|---|
| 171 | // | 
|---|
| 172 | const Int_t rc = fRawEvtHeader->ReadEvt(fin, ver); | 
|---|
| 173 | if (rc==kCONTINUE && fForceMode==kFALSE) | 
|---|
| 174 | { | 
|---|
| 175 | *fLog << err << "Problem found reading the event header... abort." << endl; | 
|---|
| 176 | return kFALSE; | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | // | 
|---|
| 180 | //  Get number of crates from the run header | 
|---|
| 181 | // | 
|---|
| 182 | const UShort_t nc = fRawRunHeader->GetNumCrates(); | 
|---|
| 183 |  | 
|---|
| 184 | // | 
|---|
| 185 | // Delete arrays which stores the pixel information (time slices) | 
|---|
| 186 | // | 
|---|
| 187 | fRawEvtData1->ResetPixels(fRawRunHeader->GetNumNormalPixels(),  fRawRunHeader->GetMaxPixId()); | 
|---|
| 188 | fRawEvtData2->ResetPixels(fRawRunHeader->GetNumSpecialPixels(), fRawRunHeader->GetMinPixId()); | 
|---|
| 189 |  | 
|---|
| 190 | // | 
|---|
| 191 | // clear the TClonesArray which stores the Crate Information | 
|---|
| 192 | // and create a new array of the correct size | 
|---|
| 193 | // | 
|---|
| 194 | fRawCrateArray->SetSize(nc); | 
|---|
| 195 |  | 
|---|
| 196 | // | 
|---|
| 197 | // Calculate the number of byte to be skipped in a file if a pixel is not connected | 
|---|
| 198 | // | 
|---|
| 199 | const UShort_t nskip = fRawRunHeader->GetNumSamplesLoGain()+fRawRunHeader->GetNumSamplesHiGain(); | 
|---|
| 200 |  | 
|---|
| 201 | // | 
|---|
| 202 | // read the CRATE DATA (see specification) from file | 
|---|
| 203 | // | 
|---|
| 204 | Int_t posinarray=0; | 
|---|
| 205 | for (int i=0; i<nc; i++) | 
|---|
| 206 | { | 
|---|
| 207 | fRawCrateArray->GetEntry(i)->ReadEvt(fin, ver); | 
|---|
| 208 | if (!fin) | 
|---|
| 209 | return kFALSE; | 
|---|
| 210 |  | 
|---|
| 211 | //fRawEvtData1->ReadEvt(fin, posinarray++); | 
|---|
| 212 |  | 
|---|
| 213 | const UShort_t npic = fRawRunHeader->GetNumPixInCrate(); | 
|---|
| 214 | const Byte_t   ab   = fRawCrateArray->GetEntry(posinarray)->GetABFlags(); | 
|---|
| 215 | for (int j=0; j<npic; j++) | 
|---|
| 216 | { | 
|---|
| 217 | // calc the spiral hardware pixel number | 
|---|
| 218 | const UShort_t ipos = posinarray*npic+j; | 
|---|
| 219 |  | 
|---|
| 220 | // Get Hardware Id | 
|---|
| 221 | const Short_t hwid = fRawRunHeader->GetPixAssignment(ipos); | 
|---|
| 222 |  | 
|---|
| 223 | // Check whether the pixel is connected or not | 
|---|
| 224 | if (hwid==0) | 
|---|
| 225 | { | 
|---|
| 226 | fin.seekg(nskip, ios::cur); | 
|---|
| 227 | continue; | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 | // -1 converts the hardware pixel Id into the software pixel index | 
|---|
| 231 | if (hwid>0) | 
|---|
| 232 | fRawEvtData1->ReadPixel(fin,   hwid-1,  TESTBIT(ab, j)); | 
|---|
| 233 | else | 
|---|
| 234 | fRawEvtData2->ReadPixel(fin, -(hwid+1), TESTBIT(ab, j)); | 
|---|
| 235 |  | 
|---|
| 236 | } | 
|---|
| 237 | if (!fin) | 
|---|
| 238 | return kFALSE; | 
|---|
| 239 |  | 
|---|
| 240 | posinarray++; | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | // This is a workaround for the oldest runs (version<3) | 
|---|
| 244 | // for which no time stamp was available. | 
|---|
| 245 | // For this runs a fake time stamp is created | 
|---|
| 246 | if (ver<3) | 
|---|
| 247 | CreateFakeTime(); | 
|---|
| 248 |  | 
|---|
| 249 | if (rc==kCONTINUE && fForceMode==kTRUE) | 
|---|
| 250 | return kCONTINUE; | 
|---|
| 251 |  | 
|---|
| 252 | return kTRUE; | 
|---|
| 253 | } | 
|---|
| 254 |  | 
|---|
| 255 | void MRawRead::SkipEvent(istream &fin) | 
|---|
| 256 | { | 
|---|
| 257 | // | 
|---|
| 258 | //  Get file format version | 
|---|
| 259 | // | 
|---|
| 260 | const UShort_t ver = fRawRunHeader->GetFormatVersion(); | 
|---|
| 261 | fRawEvtHeader->SkipEvt(fin, ver); | 
|---|
| 262 |  | 
|---|
| 263 | const UShort_t nc = fRawRunHeader->GetNumCrates(); | 
|---|
| 264 | for (int i=0; i<nc; i++) | 
|---|
| 265 | { | 
|---|
| 266 | fRawCrateArray->GetEntry(i)->SkipEvt(fin, ver); | 
|---|
| 267 | fRawEvtData1->SkipEvt(fin); | 
|---|
| 268 | } | 
|---|
| 269 | } | 
|---|