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 | return
|
---|
118 | fIn->SetRefAddress("QoS", fBufQos) &&
|
---|
119 | fIn->SetRefAddress("Time", fBufTime) &&
|
---|
120 | fReport->SetupReading(*pList) &&
|
---|
121 | fReport->SetupReadingFits(*fIn);
|
---|
122 | }
|
---|
123 |
|
---|
124 | // --------------------------------------------------------------------------
|
---|
125 | //
|
---|
126 | // Read the file line by line as long as a matching MReport* class is found.
|
---|
127 | // In this case call its interpreter (Interprete()) and remove the identifier
|
---|
128 | // first (XYZ-REPORT)
|
---|
129 | //
|
---|
130 | Int_t MReportFitsRead::Process()
|
---|
131 | {
|
---|
132 | if (!fIn->GetNextRow())
|
---|
133 | {
|
---|
134 | *fLog << inf << "End-of-file detected." << endl;
|
---|
135 | return kFALSE;
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (fBufTime==0)
|
---|
139 | return kCONTINUE;
|
---|
140 |
|
---|
141 | MTime &time = *fReport->GetTime();
|
---|
142 |
|
---|
143 | time.SetMjd(fBufTime+fMjdRef);
|
---|
144 |
|
---|
145 | // return -1: This is the special case: out of time limit
|
---|
146 | if (fStart && time<fStart)
|
---|
147 | return kCONTINUE;
|
---|
148 | if (fStop && time>fStop)
|
---|
149 | return kCONTINUE;
|
---|
150 |
|
---|
151 | const Int_t rc = fReport->InterpreteFits(*fIn);
|
---|
152 |
|
---|
153 | /*
|
---|
154 | switch (rc)
|
---|
155 | {
|
---|
156 | case kTRUE: fNumReports++; break;
|
---|
157 | case kCONTINUE: fNumSkipped++; break;
|
---|
158 | }*/
|
---|
159 |
|
---|
160 | switch (rc)
|
---|
161 | {
|
---|
162 | case kFALSE:
|
---|
163 | *fLog << err << "ERROR - Interpreting '" << fReport->GetName() << "' failed (l." << fIn->GetRow() << ")... abort." << endl;
|
---|
164 | break;
|
---|
165 | case kCONTINUE:
|
---|
166 | *fLog << warn << "WARNING - Interpreting '" << fReport->GetName() << "' failed (l." << fIn->GetRow() << ")... skipped." << endl;
|
---|
167 | break;
|
---|
168 | case -1: // This is the special case: out of time limit
|
---|
169 | return kCONTINUE;
|
---|
170 | }
|
---|
171 |
|
---|
172 | return rc;
|
---|
173 | }
|
---|
174 |
|
---|
175 | // --------------------------------------------------------------------------
|
---|
176 | //
|
---|
177 | // Close the file and print some execution statistics
|
---|
178 | //
|
---|
179 | Int_t MReportFitsRead::PostProcess()
|
---|
180 | {
|
---|
181 | fIn->close();
|
---|
182 |
|
---|
183 | if (!GetNumExecutions())
|
---|
184 | return kTRUE;
|
---|
185 |
|
---|
186 | *fLog << inf << endl;
|
---|
187 | *fLog << GetDescriptor() << " statistics:" << endl;
|
---|
188 | *fLog << dec << setfill(' ');
|
---|
189 | /*
|
---|
190 | *fLog << inf;
|
---|
191 | *fLog << " " << setw(7) << rep->GetNumReports() << " (";
|
---|
192 | *fLog << setw(3) << (int)(100.*rep->GetNumReports()/GetNumExecutions());
|
---|
193 | *fLog << "%): " << rep->GetName() << endl;
|
---|
194 |
|
---|
195 | if (rep->GetNumSkipped()==0)
|
---|
196 | continue;
|
---|
197 |
|
---|
198 | *fLog << warn;
|
---|
199 | *fLog << " " << setw(7) << rep->GetNumSkipped() << " (";
|
---|
200 | *fLog << setw(3) << (int)(100.*rep->GetNumSkipped()/GetNumExecutions());
|
---|
201 | *fLog << "%): " << rep->GetName() << " skipped!" << endl;
|
---|
202 | */
|
---|
203 | return kTRUE;
|
---|
204 | }
|
---|