| 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, 5/2003 <mailto:tbretz@astro.uni-wuerzburg.de>
|
|---|
| 19 | !
|
|---|
| 20 | ! Copyright: MAGIC Software Development, 2000-2003
|
|---|
| 21 | !
|
|---|
| 22 | !
|
|---|
| 23 | \* ======================================================================== */
|
|---|
| 24 |
|
|---|
| 25 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 26 | //
|
|---|
| 27 | // MReadRflFile
|
|---|
| 28 | //
|
|---|
| 29 | // Reads a output file of the reflector program
|
|---|
| 30 | //
|
|---|
| 31 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 32 | #include "MReadRflFile.h"
|
|---|
| 33 |
|
|---|
| 34 | #include <errno.h>
|
|---|
| 35 | #include <fstream>
|
|---|
| 36 |
|
|---|
| 37 | #include <TSystem.h>
|
|---|
| 38 |
|
|---|
| 39 | #include "structures_rfl.h"
|
|---|
| 40 |
|
|---|
| 41 | #include "MParList.h"
|
|---|
| 42 | #include "MRflEvtData.h"
|
|---|
| 43 | #include "MRflEvtHeader.h"
|
|---|
| 44 | #include "MRflRunHeader.h"
|
|---|
| 45 | #include "MRflSinglePhoton.h"
|
|---|
| 46 |
|
|---|
| 47 | #include "MLog.h"
|
|---|
| 48 | #include "MLogManip.h"
|
|---|
| 49 |
|
|---|
| 50 | ClassImp(MReadRflFile);
|
|---|
| 51 |
|
|---|
| 52 | using namespace std;
|
|---|
| 53 |
|
|---|
| 54 | // ------------------------------------------------
|
|---|
| 55 | const char PROGNAME[] = "reflector";
|
|---|
| 56 | #define SIZE_OF_FLAGS 13
|
|---|
| 57 | #define SIZE_OF_SIGNATURE 13
|
|---|
| 58 | #define FLAG_START_OF_RUN "\nSTART---RUN\n"
|
|---|
| 59 | #define FLAG_START_OF_EVENT "\nSTART-EVENT\n"
|
|---|
| 60 | #define FLAG_END_OF_EVENT "\nEND---EVENT\n"
|
|---|
| 61 | #define FLAG_END_OF_RUN "\nEND-----RUN\n"
|
|---|
| 62 | #define FLAG_END_OF_FILE "\nEND----FILE\n"
|
|---|
| 63 | #define FLAG_END_OF_STDIN "\nEND---STDIN\n"
|
|---|
| 64 | // ------------------------------------------------
|
|---|
| 65 |
|
|---|
| 66 | Bool_t MReadRflFile::FlagIsA(const char *s1, const char *flag)
|
|---|
| 67 | {
|
|---|
| 68 | return strncmp(s1, flag, SIZE_OF_FLAGS)==0;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | Bool_t MReadRflFile::ReadEvtHeader()
|
|---|
| 72 | {
|
|---|
| 73 | if (fCurrentVersion <= 0.5)
|
|---|
| 74 | {
|
|---|
| 75 | RflEventHeader_old revth;
|
|---|
| 76 | fIn->read((char*)&revth, sizeof(RflEventHeader_old));
|
|---|
| 77 | fEvtHeader->SetEvtNumber((int)revth.EvtNumber);
|
|---|
| 78 | }
|
|---|
| 79 | else
|
|---|
| 80 | {
|
|---|
| 81 | RflEventHeader revth;
|
|---|
| 82 | fIn->read((char*)&revth, sizeof(RflEventHeader));
|
|---|
| 83 | fEvtHeader->SetEvtNumber((int)revth.EvtNumber);
|
|---|
| 84 | fEvtHeader->SetEnergy((int)revth.Etotal);
|
|---|
| 85 | fEvtHeader->SetMomentum(revth.p[0], revth.p[1], revth.p[2]);
|
|---|
| 86 | fEvtHeader->SetCorePosition(revth.CorePos[0][0], revth.CorePos[1][0]);
|
|---|
| 87 |
|
|---|
| 88 | fEvtHeader->SetHeightFirstInt((Float_t)revth.zFirstInt);
|
|---|
| 89 |
|
|---|
| 90 | fEvtHeader->SetPhi((Float_t)revth.telescopePhi);
|
|---|
| 91 | fEvtHeader->SetTheta((Float_t)revth.telescopeTheta);
|
|---|
| 92 |
|
|---|
| 93 | fEvtHeader->SetNmax((Float_t)revth.longi_Nmax);
|
|---|
| 94 | fEvtHeader->SetT0((Float_t)revth.longi_t0);
|
|---|
| 95 | fEvtHeader->SetTmax((Float_t)revth.longi_tmax);
|
|---|
| 96 | fEvtHeader->SetChi2((Float_t)revth.longi_chi2);
|
|---|
| 97 |
|
|---|
| 98 | fEvtHeader->SetEFraction((Float_t)revth.elec_cph_fraction);
|
|---|
| 99 | fEvtHeader->SetMFraction((Float_t)revth.muon_cph_fraction);
|
|---|
| 100 | fEvtHeader->SetOFraction((Float_t)revth.other_cph_fraction);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | return (bool)*fIn;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | int MReadRflFile::ReadFlag()
|
|---|
| 107 | {
|
|---|
| 108 | char flag[SIZE_OF_FLAGS];
|
|---|
| 109 | fIn->read(flag, SIZE_OF_FLAGS);
|
|---|
| 110 |
|
|---|
| 111 | if (!fIn)
|
|---|
| 112 | return kError;
|
|---|
| 113 |
|
|---|
| 114 | if (FlagIsA(flag, FLAG_END_OF_FILE))
|
|---|
| 115 | return kEndOfFile;
|
|---|
| 116 | if (FlagIsA(flag, FLAG_END_OF_RUN))
|
|---|
| 117 | return kEndOfRun;
|
|---|
| 118 | if (FlagIsA(flag, FLAG_START_OF_RUN))
|
|---|
| 119 | return kStartOfRun;
|
|---|
| 120 | if (FlagIsA(flag, FLAG_END_OF_EVENT))
|
|---|
| 121 | return kEndOfEvtData;
|
|---|
| 122 | if (FlagIsA(flag, FLAG_START_OF_EVENT))
|
|---|
| 123 | return kStartOfEvtData;
|
|---|
| 124 |
|
|---|
| 125 | return kUndefined;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | Bool_t MReadRflFile::ReadEvtData()
|
|---|
| 129 | {
|
|---|
| 130 | Bool_t rc = kFALSE;
|
|---|
| 131 | while (1)
|
|---|
| 132 | {
|
|---|
| 133 | cphoton data; // FIRST READ "START OF EVENT"
|
|---|
| 134 | fIn->read((char*)&data, SIZE_OF_FLAGS);
|
|---|
| 135 | if (!*fIn)
|
|---|
| 136 | break;
|
|---|
| 137 |
|
|---|
| 138 | if (FlagIsA((char*)&data, FLAG_END_OF_EVENT))
|
|---|
| 139 | {
|
|---|
| 140 | rc = kTRUE;
|
|---|
| 141 | break;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | fIn->read((char*)&data+SIZE_OF_FLAGS, sizeof(cphoton)-SIZE_OF_FLAGS);
|
|---|
| 145 | if (!*fIn)
|
|---|
| 146 | break;
|
|---|
| 147 |
|
|---|
| 148 | MRflSinglePhoton &ph = fEvtData->GetNewPhoton();
|
|---|
| 149 | ph.SetXY(data.x*10, data.y*10);
|
|---|
| 150 | ph.SetCosUV(data.u, data.v);
|
|---|
| 151 | ph.SetTime(data.t);
|
|---|
| 152 | ph.SetHeight(data.h);
|
|---|
| 153 | ph.SetInclinationAngle(data.phi);
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | fEvtData->FixSize();
|
|---|
| 157 | return rc;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | Int_t MReadRflFile::EvalFlag()
|
|---|
| 161 | {
|
|---|
| 162 | const Int_t flag = ReadFlag();
|
|---|
| 163 |
|
|---|
| 164 | switch (flag)
|
|---|
| 165 | {
|
|---|
| 166 | case kEndOfFile:
|
|---|
| 167 | fCurrentVersion = ReadVersion();
|
|---|
| 168 | if (fCurrentVersion<0)
|
|---|
| 169 | {
|
|---|
| 170 | *fLog << inf << "Found end of file...Everything OK." << endl;
|
|---|
| 171 | break;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | *fLog << warn << "Found flag of end of file, but file goes on..." << endl;
|
|---|
| 175 | if (ReadFlag()<0)
|
|---|
| 176 | return kError;
|
|---|
| 177 | /* FALLTHROU */
|
|---|
| 178 | case kStartOfRun:
|
|---|
| 179 | if (fCurrentVersion>0.5)
|
|---|
| 180 | {
|
|---|
| 181 | RflRunHeader rrunh;
|
|---|
| 182 | fIn->read((char*)&rrunh, sizeof(RflRunHeader));
|
|---|
| 183 | if (*fIn)
|
|---|
| 184 | {
|
|---|
| 185 | *fLog << inf << "FIXME: Call ReInit" << endl;
|
|---|
| 186 |
|
|---|
| 187 | fRunHeader->SetRunNumber((int)rrunh.RunNumber);
|
|---|
| 188 | *fLog << underline << "RunHeader:" << endl;
|
|---|
| 189 | *fLog << " Run Number: " << rrunh.RunNumber << endl;
|
|---|
| 190 | *fLog << " Date: " << rrunh.date << endl;
|
|---|
| 191 | *fLog << " Corsika Ver.: " << rrunh.Corsika_version << endl;
|
|---|
| 192 |
|
|---|
| 193 | break;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | *fLog << err << "Error! found end of file... But no EOF flag. Exiting." << endl;
|
|---|
| 197 | return kError;
|
|---|
| 198 | }
|
|---|
| 199 | return kUndefined;
|
|---|
| 200 |
|
|---|
| 201 | case kStartOfEvtData:
|
|---|
| 202 | case kEndOfRun:
|
|---|
| 203 | break;
|
|---|
| 204 |
|
|---|
| 205 | case kError:
|
|---|
| 206 | *fLog << err << "ERROR - Flag 'error'" << endl;
|
|---|
| 207 | return kError;
|
|---|
| 208 |
|
|---|
| 209 | case kUndefined:
|
|---|
| 210 | *fLog << err << "ERROR - Flag 'undefined'" << endl;
|
|---|
| 211 | return kError;
|
|---|
| 212 |
|
|---|
| 213 | default:
|
|---|
| 214 | *fLog << err << "ERROR - Unhandled flag" << endl;
|
|---|
| 215 | return kError;
|
|---|
| 216 |
|
|---|
| 217 | }
|
|---|
| 218 | return flag;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | Int_t MReadRflFile::Process()
|
|---|
| 222 | {
|
|---|
| 223 | for (;;)
|
|---|
| 224 | {
|
|---|
| 225 | switch (EvalFlag())
|
|---|
| 226 | {
|
|---|
| 227 | case kError:
|
|---|
| 228 | return kFALSE;
|
|---|
| 229 |
|
|---|
| 230 | case kEndOfFile:
|
|---|
| 231 | if (!OpenNextFile())
|
|---|
| 232 | return kFALSE;
|
|---|
| 233 | /* FALLTHROU */
|
|---|
| 234 | case kStartOfRun:
|
|---|
| 235 | case kEndOfRun:
|
|---|
| 236 | continue;
|
|---|
| 237 |
|
|---|
| 238 | case kStartOfEvtData:
|
|---|
| 239 | break;
|
|---|
| 240 | }
|
|---|
| 241 | break;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | if (!ReadEvtHeader())
|
|---|
| 245 | return kFALSE;
|
|---|
| 246 |
|
|---|
| 247 | return ReadEvtData();
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | Int_t MReadRflFile::PreProcess(MParList *plist)
|
|---|
| 251 | {
|
|---|
| 252 | fEvtData=(MRflEvtData*)plist->FindCreateObj("MRflEvtData");
|
|---|
| 253 | if (!fEvtData)
|
|---|
| 254 | return kFALSE;
|
|---|
| 255 |
|
|---|
| 256 | fEvtHeader=(MRflEvtHeader*)plist->FindCreateObj("MRflEvtHeader");
|
|---|
| 257 | if (!fEvtHeader)
|
|---|
| 258 | return kFALSE;
|
|---|
| 259 |
|
|---|
| 260 | fRunHeader=(MRflRunHeader*)plist->FindCreateObj("MRflRunHeader");
|
|---|
| 261 | if (!fRunHeader)
|
|---|
| 262 | return kFALSE;
|
|---|
| 263 |
|
|---|
| 264 | Rewind();
|
|---|
| 265 |
|
|---|
| 266 | return OpenNextFile();
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | // --------------------------------------------------------------------------
|
|---|
| 270 | //
|
|---|
| 271 | // This opens the next file in the list and deletes its name from the list.
|
|---|
| 272 | //
|
|---|
| 273 | Bool_t MReadRflFile::OpenNextFile()
|
|---|
| 274 | {
|
|---|
| 275 | //
|
|---|
| 276 | // open the input stream and check if it is really open (file exists?)
|
|---|
| 277 | //
|
|---|
| 278 | if (fIn)
|
|---|
| 279 | delete fIn;
|
|---|
| 280 | fIn = NULL;
|
|---|
| 281 |
|
|---|
| 282 | //
|
|---|
| 283 | // Check for the existence of a next file to read
|
|---|
| 284 | //
|
|---|
| 285 | if (fNumFile >= (UInt_t)fFileNames->GetSize())
|
|---|
| 286 | {
|
|---|
| 287 | *fLog << inf << GetDescriptor() << ": No unread files anymore..." << endl;
|
|---|
| 288 | return kFALSE;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | TNamed *file = (TNamed*)fFileNames->At(fNumFile);
|
|---|
| 292 |
|
|---|
| 293 | //TNamed *file = (TNamed*)fFileNames->GetFirst();
|
|---|
| 294 | //if (!file)
|
|---|
| 295 | // return kFALSE;
|
|---|
| 296 |
|
|---|
| 297 | //
|
|---|
| 298 | // open the file which is the first one in the chain
|
|---|
| 299 | //
|
|---|
| 300 | fFileName = file->GetName();
|
|---|
| 301 | const TString expname = fFileName;
|
|---|
| 302 | gSystem->ExpandPathName(expname);
|
|---|
| 303 |
|
|---|
| 304 | //
|
|---|
| 305 | // Remove this file from the list of pending files
|
|---|
| 306 | //
|
|---|
| 307 | //fFileNames->Remove(file);
|
|---|
| 308 |
|
|---|
| 309 | *fLog << inf << "Open file: '" << fFileName << "'" << endl;
|
|---|
| 310 |
|
|---|
| 311 | fIn = new ifstream(expname);
|
|---|
| 312 | if (!*fIn)
|
|---|
| 313 | {
|
|---|
| 314 | *fLog << err << "Cannot open file " << expname << ": ";
|
|---|
| 315 | *fLog << strerror(errno) << endl;
|
|---|
| 316 | return kFALSE;
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | *fLog << inf;
|
|---|
| 320 | fLog->Separator(fFileName);
|
|---|
| 321 |
|
|---|
| 322 | fCurrentVersion = ReadVersion();
|
|---|
| 323 | if (fCurrentVersion<0)
|
|---|
| 324 | {
|
|---|
| 325 | cout << "ERROR reading signature." << endl;
|
|---|
| 326 | return kFALSE;
|
|---|
| 327 | }
|
|---|
| 328 | cout << "Version " << fCurrentVersion << endl << endl;
|
|---|
| 329 |
|
|---|
| 330 | fNumFile++;
|
|---|
| 331 | return kTRUE;
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | /****************************************************/
|
|---|
| 335 |
|
|---|
| 336 | float MReadRflFile::ReadVersion()
|
|---|
| 337 | {
|
|---|
| 338 | char sign[20];
|
|---|
| 339 | fIn->read(sign, SIZE_OF_SIGNATURE);
|
|---|
| 340 | if (!*fIn)
|
|---|
| 341 | return -1;
|
|---|
| 342 |
|
|---|
| 343 | if (strncmp(sign, PROGNAME, strlen(PROGNAME)) != 0)
|
|---|
| 344 | {
|
|---|
| 345 | /* For the ascii tail of the file! : */
|
|---|
| 346 | if (strncmp(sign, "\n############", SIZE_OF_SIGNATURE))
|
|---|
| 347 | cout << "ERROR: Signature of .rfl file is not correct: " << sign << endl;
|
|---|
| 348 |
|
|---|
| 349 | return -1;
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | float version;
|
|---|
| 353 | sscanf(sign, "%*s %f", &version);
|
|---|
| 354 |
|
|---|
| 355 | //If the version is < 0.6 the signature had one more byte
|
|---|
| 356 | if (version < 0.6)
|
|---|
| 357 | *fIn >> sign[0];
|
|---|
| 358 |
|
|---|
| 359 | return version;
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | // --------------------------------------------------------------------------
|
|---|
| 363 | //
|
|---|
| 364 | // Default constructor. Creates an array which stores the file names of
|
|---|
| 365 | // the files which should be read. If a filename is given it is added
|
|---|
| 366 | // to the list.
|
|---|
| 367 | //
|
|---|
| 368 | MReadRflFile::MReadRflFile(const char *fname, const char *name,
|
|---|
| 369 | const char *title) : fIn(NULL), fEntries(0)
|
|---|
| 370 | {
|
|---|
| 371 | fName = name ? name : "MRead";
|
|---|
| 372 | fTitle = title ? title : "Reads a Reflector output file";
|
|---|
| 373 |
|
|---|
| 374 | //
|
|---|
| 375 | // remember file name for opening the file in the preprocessor
|
|---|
| 376 | //
|
|---|
| 377 | fFileNames = new TList;
|
|---|
| 378 | fFileNames->SetOwner();
|
|---|
| 379 |
|
|---|
| 380 | if (fname)
|
|---|
| 381 | AddFile(fname);
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | // --------------------------------------------------------------------------
|
|---|
| 385 | //
|
|---|
| 386 | // Delete the filename list and the input stream if one exists.
|
|---|
| 387 | //
|
|---|
| 388 | MReadRflFile::~MReadRflFile()
|
|---|
| 389 | {
|
|---|
| 390 | delete fFileNames;
|
|---|
| 391 | if (fIn)
|
|---|
| 392 | delete fIn;
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | // --------------------------------------------------------------------------
|
|---|
| 396 | //
|
|---|
| 397 | // Add this file as the last entry in the chain
|
|---|
| 398 | //
|
|---|
| 399 | Int_t MReadRflFile::AddFile(const char *txt, int)
|
|---|
| 400 | {
|
|---|
| 401 | const char *name = gSystem->ExpandPathName(txt);
|
|---|
| 402 |
|
|---|
| 403 | TString fname(name);
|
|---|
| 404 | delete [] name;
|
|---|
| 405 | /*
|
|---|
| 406 | if (!CheckHeader(fname))
|
|---|
| 407 | {
|
|---|
| 408 | *fLog << warn << "WARNING - Problem reading header... ignored." << endl;
|
|---|
| 409 | return;
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | const Int_t n = GetNumEvents(fname);
|
|---|
| 413 | if (n==0)
|
|---|
| 414 | {
|
|---|
| 415 | *fLog << warn << "WARNING - File contains no data... ignored." << endl;
|
|---|
| 416 | return;
|
|---|
| 417 | }
|
|---|
| 418 |
|
|---|
| 419 | fEntries += n;
|
|---|
| 420 |
|
|---|
| 421 | *fLog << inf << "File " << txt << " contains " << n << " events (Total=" << fEntries << ")" << endl;
|
|---|
| 422 | */
|
|---|
| 423 | fFileNames->AddLast(new TNamed(txt, ""));
|
|---|
| 424 | return 1;
|
|---|
| 425 | }
|
|---|
| 426 |
|
|---|
| 427 |
|
|---|
| 428 | Bool_t MReadRflFile::SearchFor(Int_t runno, Int_t eventno)
|
|---|
| 429 | {
|
|---|
| 430 | if (!fEvtHeader)
|
|---|
| 431 | return kFALSE;
|
|---|
| 432 |
|
|---|
| 433 | fNumFile = 0;
|
|---|
| 434 | if (!OpenNextFile())
|
|---|
| 435 | return kFALSE;
|
|---|
| 436 |
|
|---|
| 437 | while (1)
|
|---|
| 438 | {
|
|---|
| 439 | fEvtData->Reset();
|
|---|
| 440 | if (!Process())
|
|---|
| 441 | return kFALSE;
|
|---|
| 442 |
|
|---|
| 443 | if (fEvtHeader->GetEvtNumber()==eventno &&
|
|---|
| 444 | fRunHeader->GetRunNumber()==runno)
|
|---|
| 445 | return kTRUE;
|
|---|
| 446 | }
|
|---|
| 447 | }
|
|---|