| 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/2011 <mailto:thomas.bretz@epfl.ch> | 
|---|
| 19 | ! | 
|---|
| 20 | !   Copyright: MAGIC Software Development, 2000-2011 | 
|---|
| 21 | ! | 
|---|
| 22 | ! | 
|---|
| 23 | \* ======================================================================== */ | 
|---|
| 24 |  | 
|---|
| 25 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 26 | // | 
|---|
| 27 | //  MReportFitsRead | 
|---|
| 28 | // | 
|---|
| 29 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 30 | #include "MReportFitsRead.h" | 
|---|
| 31 |  | 
|---|
| 32 | #include <errno.h> | 
|---|
| 33 | #include <fstream> | 
|---|
| 34 |  | 
|---|
| 35 | #include <TClass.h> | 
|---|
| 36 | #include <TRegexp.h> | 
|---|
| 37 | #include <THashTable.h> | 
|---|
| 38 |  | 
|---|
| 39 | #include "fits.h" | 
|---|
| 40 |  | 
|---|
| 41 | #include "MLog.h" | 
|---|
| 42 | #include "MLogManip.h" | 
|---|
| 43 |  | 
|---|
| 44 | #include "MParList.h" | 
|---|
| 45 | #include "MReport.h" | 
|---|
| 46 |  | 
|---|
| 47 | ClassImp(MReportFitsRead); | 
|---|
| 48 |  | 
|---|
| 49 | using namespace std; | 
|---|
| 50 |  | 
|---|
| 51 | // -------------------------------------------------------------------------- | 
|---|
| 52 | // | 
|---|
| 53 | // Default constructor. It tries to open the given file and creates a | 
|---|
| 54 | // THashTable which allows faster access to the MReport* objects. | 
|---|
| 55 | // | 
|---|
| 56 | MReportFitsRead::MReportFitsRead(const char *fname, const char *name, const char *title) | 
|---|
| 57 | : fFileName(fname), fReport(NULL), fIn(NULL) | 
|---|
| 58 | { | 
|---|
| 59 | fName  = name  ? name  : "MReportFitsRead"; | 
|---|
| 60 | fTitle = title ? title : "Read task to read general report files"; | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | // -------------------------------------------------------------------------- | 
|---|
| 64 | // | 
|---|
| 65 | // Destructor. Delete input stream and hash table. | 
|---|
| 66 | // | 
|---|
| 67 | MReportFitsRead::~MReportFitsRead() | 
|---|
| 68 | { | 
|---|
| 69 | delete fIn; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | // -------------------------------------------------------------------------- | 
|---|
| 73 | // | 
|---|
| 74 | // Call SetupReading for all MReportHelp objects scheduled. | 
|---|
| 75 | // Try to open the file and check the file header. | 
|---|
| 76 | // | 
|---|
| 77 | Int_t MReportFitsRead::PreProcess(MParList *pList) | 
|---|
| 78 | { | 
|---|
| 79 | // Add the MReport instances first to the paramter list | 
|---|
| 80 | // so that SetupReading can find them if needed | 
|---|
| 81 | //fList->R__FOR_EACH(MReportHelp, AddToList)(*pList); | 
|---|
| 82 |  | 
|---|
| 83 | fReport = (MReport*)pList->FindCreateObj(fReportName); | 
|---|
| 84 | if (!fReport) | 
|---|
| 85 | return kFALSE; | 
|---|
| 86 |  | 
|---|
| 87 | if (!fReport->InheritsFrom(MReport::Class())) | 
|---|
| 88 | { | 
|---|
| 89 | *fLog << err << fReportName << " does not derive from MReport." << endl; | 
|---|
| 90 | return kFALSE; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 |  | 
|---|
| 94 | // | 
|---|
| 95 | // open the input stream | 
|---|
| 96 | // first of all check if opening the file in the constructor was | 
|---|
| 97 | // successfull | 
|---|
| 98 | // | 
|---|
| 99 | if (fIn) | 
|---|
| 100 | delete fIn; | 
|---|
| 101 | fIn = new fits(fFileName.Data()); | 
|---|
| 102 | if (!(*fIn)) | 
|---|
| 103 | { | 
|---|
| 104 | *fLog << err << "Cannot open file " << fFileName << ": "; | 
|---|
| 105 | *fLog << strerror(errno) << endl; | 
|---|
| 106 | return kFALSE; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | if (fIn->GetStr("TELESCOP")!="FACT") | 
|---|
| 110 | { | 
|---|
| 111 | *fLog << err << "Not a valid FACT FITS file (key TELESCOP not 'FACT')." << endl; | 
|---|
| 112 | return kFALSE; | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | fMjdRef = fIn->HasKey("MJDREF") ? fIn->GetUInt("MJDREF") : 0; | 
|---|
| 116 |  | 
|---|
| 117 | if (!fIn->HasKey("MJDREF")) | 
|---|
| 118 | *fLog << warn << "MJDREF header key missing... assuming MJDREF==0" << endl; | 
|---|
| 119 |  | 
|---|
| 120 | return | 
|---|
| 121 | fIn->SetRefAddress("QoS",  fBufQos)  && | 
|---|
| 122 | fIn->SetRefAddress("Time", fBufTime) && | 
|---|
| 123 | fReport->SetupReading(*pList) && | 
|---|
| 124 | fReport->SetupReadingFits(*fIn); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | // -------------------------------------------------------------------------- | 
|---|
| 128 | // | 
|---|
| 129 | // Read the file line by line as long as a matching MReport* class is found. | 
|---|
| 130 | // In this case call its interpreter (Interprete()) and remove the identifier | 
|---|
| 131 | // first (XYZ-REPORT) | 
|---|
| 132 | // | 
|---|
| 133 | Int_t MReportFitsRead::Process() | 
|---|
| 134 | { | 
|---|
| 135 | if (!fIn->GetNextRow()) | 
|---|
| 136 | { | 
|---|
| 137 | *fLog << inf << "End-of-file detected." << endl; | 
|---|
| 138 | return kFALSE; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | if (fBufTime==0) | 
|---|
| 142 | return kCONTINUE; | 
|---|
| 143 |  | 
|---|
| 144 | MTime &time = *fReport->GetTime(); | 
|---|
| 145 |  | 
|---|
| 146 | time.SetMjd(fBufTime+fMjdRef); | 
|---|
| 147 | time.SetReadyToSave(); | 
|---|
| 148 |  | 
|---|
| 149 | // return -1: This is the special case: out of time limit | 
|---|
| 150 | if (fStart && time<fStart) | 
|---|
| 151 | return kCONTINUE; | 
|---|
| 152 | if (fStop  && time>fStop) | 
|---|
| 153 | return kCONTINUE; | 
|---|
| 154 |  | 
|---|
| 155 | const Int_t rc = fReport->InterpreteFits(*fIn); | 
|---|
| 156 |  | 
|---|
| 157 |  | 
|---|
| 158 | /* | 
|---|
| 159 | switch (rc) | 
|---|
| 160 | { | 
|---|
| 161 | case kTRUE:     fNumReports++; break; | 
|---|
| 162 | case kCONTINUE: fNumSkipped++; break; | 
|---|
| 163 | }*/ | 
|---|
| 164 |  | 
|---|
| 165 | switch (rc) | 
|---|
| 166 | { | 
|---|
| 167 | case kFALSE: | 
|---|
| 168 | *fLog << err << "ERROR - Interpreting '" << fReport->GetName() << "' failed (l." << fIn->GetRow() << ")... abort." << endl; | 
|---|
| 169 | break; | 
|---|
| 170 | case kCONTINUE: | 
|---|
| 171 | *fLog << warn << "WARNING - Interpreting '" << fReport->GetName() << "' failed (l." << fIn->GetRow() << ")... skipped." << endl; | 
|---|
| 172 | break; | 
|---|
| 173 | case -1: // This is the special case: out of time limit | 
|---|
| 174 | return kCONTINUE; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | return rc; | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | // -------------------------------------------------------------------------- | 
|---|
| 181 | // | 
|---|
| 182 | //  Close the file and print some execution statistics | 
|---|
| 183 | // | 
|---|
| 184 | Int_t MReportFitsRead::PostProcess() | 
|---|
| 185 | { | 
|---|
| 186 | fIn->close(); | 
|---|
| 187 |  | 
|---|
| 188 | if (!GetNumExecutions()) | 
|---|
| 189 | return kTRUE; | 
|---|
| 190 |  | 
|---|
| 191 | *fLog << inf << endl; | 
|---|
| 192 | *fLog << GetDescriptor() << " statistics:" << endl; | 
|---|
| 193 | *fLog << dec << setfill(' '); | 
|---|
| 194 | /* | 
|---|
| 195 | *fLog << inf; | 
|---|
| 196 | *fLog << " " << setw(7) << rep->GetNumReports() << " ("; | 
|---|
| 197 | *fLog << setw(3) << (int)(100.*rep->GetNumReports()/GetNumExecutions()); | 
|---|
| 198 | *fLog << "%): " << rep->GetName() << endl; | 
|---|
| 199 |  | 
|---|
| 200 | if (rep->GetNumSkipped()==0) | 
|---|
| 201 | continue; | 
|---|
| 202 |  | 
|---|
| 203 | *fLog << warn; | 
|---|
| 204 | *fLog << " " << setw(7) << rep->GetNumSkipped() << " ("; | 
|---|
| 205 | *fLog << setw(3) << (int)(100.*rep->GetNumSkipped()/GetNumExecutions()); | 
|---|
| 206 | *fLog << "%): " << rep->GetName() << " skipped!" << endl; | 
|---|
| 207 | */ | 
|---|
| 208 | return kTRUE; | 
|---|
| 209 | } | 
|---|