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, 7/2008 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2008
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // MReadFiles
|
---|
28 | //
|
---|
29 | /////////////////////////////////////////////////////////////////////////////
|
---|
30 | #include "MReadFiles.h"
|
---|
31 |
|
---|
32 | #include <errno.h>
|
---|
33 |
|
---|
34 | #include <TObjString.h>
|
---|
35 |
|
---|
36 | #include "MLog.h"
|
---|
37 | #include "MLogManip.h"
|
---|
38 |
|
---|
39 | #include "izstream.h"
|
---|
40 | #include "MDirIter.h"
|
---|
41 |
|
---|
42 | ClassImp(MReadFiles);
|
---|
43 |
|
---|
44 | using namespace std;
|
---|
45 |
|
---|
46 | // --------------------------------------------------------------------------
|
---|
47 | //
|
---|
48 | // Default constructor. Creates an array which stores the file names of
|
---|
49 | // the files which should be read. If a filename is given it is added
|
---|
50 | // to the list.
|
---|
51 | //
|
---|
52 | MReadFiles::MReadFiles(const char *fname, const char *name, const char *title)
|
---|
53 | : fNumFile(0), fNumLine(0), fParList(0), fIn(0)
|
---|
54 | {
|
---|
55 | fName = name ? name : "MRead";
|
---|
56 | fTitle = title ? title : "Reads a Reflector output file";
|
---|
57 |
|
---|
58 | //
|
---|
59 | // remember file name for opening the file in the preprocessor
|
---|
60 | //
|
---|
61 | fFileNames.SetOwner();
|
---|
62 |
|
---|
63 | if (fname)
|
---|
64 | AddFile(fname);
|
---|
65 | }
|
---|
66 |
|
---|
67 | // --------------------------------------------------------------------------
|
---|
68 | //
|
---|
69 | // Delete the filename list and the input stream if one exists.
|
---|
70 | //
|
---|
71 | MReadFiles::~MReadFiles()
|
---|
72 | {
|
---|
73 | if (fIn)
|
---|
74 | delete fIn;
|
---|
75 | }
|
---|
76 |
|
---|
77 | // --------------------------------------------------------------------------
|
---|
78 | //
|
---|
79 | // Call Close() and reset fNumLine and fNumFile
|
---|
80 | //
|
---|
81 | Bool_t MReadFiles::Rewind()
|
---|
82 | {
|
---|
83 | Close();
|
---|
84 |
|
---|
85 | fNumFile = 0;
|
---|
86 | fNumLine = 0;
|
---|
87 |
|
---|
88 | return kTRUE;
|
---|
89 | }
|
---|
90 |
|
---|
91 | // --------------------------------------------------------------------------
|
---|
92 | //
|
---|
93 | // Read a line from the file, return kFALSE if stream is at the end,
|
---|
94 | // kTRUE in case of success. Increase fNumLine in case of success.
|
---|
95 | //
|
---|
96 | Bool_t MReadFiles::ReadLine(TString &line)
|
---|
97 | {
|
---|
98 | line.ReadLine(*fIn);
|
---|
99 | if (!*fIn)
|
---|
100 | return kFALSE;
|
---|
101 |
|
---|
102 | fNumLine++;
|
---|
103 | return kTRUE;
|
---|
104 | }
|
---|
105 |
|
---|
106 | // --------------------------------------------------------------------------
|
---|
107 | //
|
---|
108 | // Close an open stream and set fIn to NULL
|
---|
109 | //
|
---|
110 | void MReadFiles::Close()
|
---|
111 | {
|
---|
112 | if (fIn)
|
---|
113 | delete fIn;
|
---|
114 |
|
---|
115 | fIn = NULL;
|
---|
116 | fFileName = "";
|
---|
117 | }
|
---|
118 |
|
---|
119 | // --------------------------------------------------------------------------
|
---|
120 | //
|
---|
121 | // This opens the next file in the list and deletes its name from the list.
|
---|
122 | //
|
---|
123 | Bool_t MReadFiles::OpenNextFile(Int_t num)
|
---|
124 | {
|
---|
125 | // First close an open file, if any
|
---|
126 | Close();
|
---|
127 |
|
---|
128 | if (num>=0)
|
---|
129 | fNumFile=num;
|
---|
130 |
|
---|
131 | // Check for the existence of a next file to read
|
---|
132 | if (fNumFile >= (UInt_t)fFileNames.GetSize())
|
---|
133 | {
|
---|
134 | *fLog << inf << GetDescriptor() << ": No unread files anymore..." << endl;
|
---|
135 | return kFALSE;
|
---|
136 | }
|
---|
137 |
|
---|
138 | // open the file which is the first one in the chain
|
---|
139 | fFileName = fFileNames.At(fNumFile)->GetName();
|
---|
140 |
|
---|
141 | *fLog << inf << "Open file: " << fFileName << endl;
|
---|
142 |
|
---|
143 | // open a stream to a zipped or unzipped file
|
---|
144 | fIn = new izstream(fFileName);
|
---|
145 | if (!*fIn)
|
---|
146 | {
|
---|
147 | *fLog << err << "Cannot open file " << fFileName << ": ";
|
---|
148 | *fLog << strerror(errno) << endl;
|
---|
149 | return kFALSE;
|
---|
150 | }
|
---|
151 |
|
---|
152 | //*fLog << inf;
|
---|
153 | //fLog->Separator(fFileName);
|
---|
154 |
|
---|
155 | fNumFile++;
|
---|
156 | fNumLine=0;
|
---|
157 |
|
---|
158 | // return stream status
|
---|
159 | return fIn->good();
|
---|
160 | }
|
---|
161 |
|
---|
162 | // --------------------------------------------------------------------------
|
---|
163 | //
|
---|
164 | // Open next file (the first one) and call AnalyzeHeader.
|
---|
165 | // Rewind() afterwards.
|
---|
166 | //
|
---|
167 | Int_t MReadFiles::PreProcess(MParList *plist)
|
---|
168 | {
|
---|
169 | fParList = plist;
|
---|
170 |
|
---|
171 | Rewind();
|
---|
172 |
|
---|
173 | if (!OpenNextFile())
|
---|
174 | return kFALSE;
|
---|
175 |
|
---|
176 | if (!AnalyzeHeader(*plist))
|
---|
177 | return kFALSE;
|
---|
178 |
|
---|
179 | Rewind();
|
---|
180 |
|
---|
181 | return kTRUE;
|
---|
182 | }
|
---|
183 |
|
---|
184 | Int_t MReadFiles::Process()
|
---|
185 | {
|
---|
186 | // if no file open or there was a failure
|
---|
187 | if (!fIn || !*fIn)
|
---|
188 | {
|
---|
189 | // try to open the next file
|
---|
190 | if (!OpenNextFile())
|
---|
191 | return kFALSE;
|
---|
192 |
|
---|
193 | // read the header from the file
|
---|
194 | if (!ReadHeader())
|
---|
195 | return kERROR;
|
---|
196 |
|
---|
197 | // after reading the header ReInit tasklist
|
---|
198 | if (!ReInit(fParList))
|
---|
199 | return kERROR;
|
---|
200 | }
|
---|
201 |
|
---|
202 | // Read one event
|
---|
203 | return ReadEvent();
|
---|
204 | }
|
---|
205 |
|
---|
206 | Int_t MReadFiles::PostProcess()
|
---|
207 | {
|
---|
208 | Close();
|
---|
209 | return kTRUE;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | // --------------------------------------------------------------------------
|
---|
214 | //
|
---|
215 | // Check if the file is accessible
|
---|
216 | //
|
---|
217 | Bool_t MReadFiles::CheckFile(TString name) const
|
---|
218 | {
|
---|
219 | gSystem->ExpandPathName(name);
|
---|
220 |
|
---|
221 | if (IsFileValid(name))
|
---|
222 | return kTRUE;
|
---|
223 |
|
---|
224 | *fLog << err;
|
---|
225 | *fLog << "ERROR - File " << name<< " not accessible!" << endl;
|
---|
226 | return kFALSE;;
|
---|
227 | }
|
---|
228 |
|
---|
229 | // --------------------------------------------------------------------------
|
---|
230 | //
|
---|
231 | // Add this file as the last entry in the chain. Wildcards as accepted
|
---|
232 | // by MDirIter are allowed.
|
---|
233 | //
|
---|
234 | Int_t MReadFiles::AddFile(const char *txt, int)
|
---|
235 | {
|
---|
236 | const TString dir = gSystem->DirName(txt);
|
---|
237 | const TString base = gSystem->BaseName(txt);
|
---|
238 |
|
---|
239 | Int_t num = 0;
|
---|
240 |
|
---|
241 | MDirIter Next(dir, base);
|
---|
242 | while (1)
|
---|
243 | {
|
---|
244 | const TString fname = Next();
|
---|
245 | if (fname.IsNull())
|
---|
246 | break;
|
---|
247 |
|
---|
248 | if (!CheckFile(fname))
|
---|
249 | continue;
|
---|
250 |
|
---|
251 | if (fFileNames.FindObject(fname))
|
---|
252 | *fLog << warn << "WARNING - " << fname << " already in list." << endl;
|
---|
253 |
|
---|
254 | *fLog << inf2 << fname << " added to list." << endl;
|
---|
255 | fFileNames.AddLast(new TObjString(fname));
|
---|
256 | num++;
|
---|
257 | }
|
---|
258 |
|
---|
259 | if (num>1)
|
---|
260 | *fLog << inf2 << num << " files added to list." << endl;
|
---|
261 |
|
---|
262 | if (num==0)
|
---|
263 | *fLog << warn << "WARNING - No files found at " << txt << endl;
|
---|
264 |
|
---|
265 | return num;
|
---|
266 | }
|
---|