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, 1/2005 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2004-2005
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // MDataSet
|
---|
28 | //
|
---|
29 | // This class describes a collection of sequences.
|
---|
30 | //
|
---|
31 | // Such an input file looks like:
|
---|
32 | //
|
---|
33 | // crab.seq:
|
---|
34 | // ---------
|
---|
35 | // AnalysisNumber: 1
|
---|
36 | //
|
---|
37 | // SequencesOn: 35222
|
---|
38 | // SequencesOff: 36817
|
---|
39 | //
|
---|
40 | // Sequence00035222.File: sequences/sequence035222.txt
|
---|
41 | // Sequence00036817.File: sequences/sequence036817.txt
|
---|
42 | //
|
---|
43 | // Sequence00035222.Dir: /data2/wuerzburg/Crab-Analyse/images/035222
|
---|
44 | // Sequence00036817.Dir: /data2/wuerzburg/Crab-Analyse/images/036817
|
---|
45 | //
|
---|
46 | // The analysis number is an artifical number used to name the output
|
---|
47 | // files automatically if the names are not overwritten in the corresponding
|
---|
48 | // programs.
|
---|
49 | //
|
---|
50 | // The sequence number are used to concatenate the filenames of the
|
---|
51 | // sequences using the file structure used in the datacenter.
|
---|
52 | //
|
---|
53 | // If you have different file names you can overwrite the default file names
|
---|
54 | // using Sequence%08d.File (make sure you have 8 digits!)
|
---|
55 | //
|
---|
56 | // In standard coditions (datacenter file system) paths are concatenated
|
---|
57 | // by using the information in the sequence files (date, etc). You can
|
---|
58 | // overwrite the directories in which the sequence-files (eg I-files) are
|
---|
59 | // stored using Sequence%08d.Dir (make sure you have 8 digits!)
|
---|
60 | //
|
---|
61 | // Resource file entries are case sensitive!
|
---|
62 | //
|
---|
63 | // MISSING (27/01/04): The default name and paths cannot be used yet, because
|
---|
64 | // they have to be defined soon.
|
---|
65 | //
|
---|
66 | /////////////////////////////////////////////////////////////////////////////
|
---|
67 | #include "MDataSet.h"
|
---|
68 |
|
---|
69 | #include <stdlib.h>
|
---|
70 | #include <fstream>
|
---|
71 |
|
---|
72 | #include <TEnv.h>
|
---|
73 | #include <TRegexp.h>
|
---|
74 | #include <TSystem.h> // TSystem::ExpandPath
|
---|
75 |
|
---|
76 | #include "MLog.h"
|
---|
77 | #include "MLogManip.h"
|
---|
78 |
|
---|
79 | #include "MRead.h"
|
---|
80 | #include "MAstro.h"
|
---|
81 | #include "MDirIter.h"
|
---|
82 | #include "MSequence.h"
|
---|
83 | #include "MPointingPos.h"
|
---|
84 |
|
---|
85 | ClassImp(MDataSet);
|
---|
86 |
|
---|
87 | using namespace std;
|
---|
88 |
|
---|
89 | // --------------------------------------------------------------------------
|
---|
90 | //
|
---|
91 | // Copy the run numbers from the TString runs into the TArrayI data
|
---|
92 | //
|
---|
93 | void MDataSet::Split(TString &runs, TArrayI &data) const
|
---|
94 | {
|
---|
95 | const TRegexp regexp("[0-9]+");
|
---|
96 |
|
---|
97 | data.Set(0);
|
---|
98 | runs = runs.Strip(TString::kTrailing);
|
---|
99 |
|
---|
100 | while (!runs.IsNull())
|
---|
101 | {
|
---|
102 | TString num = runs(regexp);
|
---|
103 |
|
---|
104 | const Int_t n = data.GetSize();
|
---|
105 | data.Set(n+1);
|
---|
106 | data[n] = atoi(num.Data());
|
---|
107 |
|
---|
108 | runs.Remove(0, runs.First(num)+num.Length());
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | void MDataSet::ResolveSequences(TEnv &env, const TArrayI &num, TList &list) const
|
---|
113 | {
|
---|
114 | for (int i=0; i<num.GetSize(); i++)
|
---|
115 | {
|
---|
116 | TString name = env.GetValue(Form("Sequence%08d.File", num[i]), "");
|
---|
117 | TString dir = env.GetValue(Form("Sequence%08d.Dir", num[i]), "");
|
---|
118 |
|
---|
119 | gSystem->ExpandPathName(name);
|
---|
120 | gSystem->ExpandPathName(dir);
|
---|
121 |
|
---|
122 | if (name.IsNull())
|
---|
123 | {
|
---|
124 | // Replace with correct default name
|
---|
125 | name = Form("/data2/wuerzburg/sequences/sequence%08d.txt", num[i]);
|
---|
126 | }
|
---|
127 | /*
|
---|
128 | if (dir.IsNull())
|
---|
129 | {
|
---|
130 | // Replace with default dir
|
---|
131 | }
|
---|
132 | */
|
---|
133 |
|
---|
134 | if (gSystem->AccessPathName(name, kFileExists))
|
---|
135 | gLog << warn << "WARNING - Sequence file '" << name << "' doesn't exist." << endl;
|
---|
136 |
|
---|
137 | if (gSystem->AccessPathName(dir, kFileExists))
|
---|
138 | gLog << warn << "WARNING - Directory '" << dir << "' doesn't exist." << endl;
|
---|
139 |
|
---|
140 | list.Add(new TNamed(name, dir));
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | // --------------------------------------------------------------------------
|
---|
145 | //
|
---|
146 | // Read the file fname as setup file for the sequence.
|
---|
147 | //
|
---|
148 | MDataSet::MDataSet(const char *fname)
|
---|
149 | {
|
---|
150 | fName = fname;
|
---|
151 |
|
---|
152 | const char *expname = gSystem->ExpandPathName(fname);
|
---|
153 |
|
---|
154 | fTitle = Form("Sequences contained in file %s", expname);
|
---|
155 |
|
---|
156 | TEnv env(expname);
|
---|
157 | delete [] expname;
|
---|
158 |
|
---|
159 | TString str;
|
---|
160 |
|
---|
161 | fNumAnalysis = env.GetValue("AnalysisNumber", -1);
|
---|
162 |
|
---|
163 | str = env.GetValue("SequencesOn", "");
|
---|
164 | Split(str, fNumSequencesOn);
|
---|
165 | str = env.GetValue("SequencesOff", "");
|
---|
166 | Split(str, fNumSequencesOff);
|
---|
167 |
|
---|
168 | ResolveSequences(env, fNumSequencesOn, fSequencesOn);
|
---|
169 | ResolveSequences(env, fNumSequencesOff, fSequencesOff);
|
---|
170 |
|
---|
171 |
|
---|
172 | fNameSource = env.GetValue("SourceName", "");
|
---|
173 | fCatalog = env.GetValue("Catalog", "~/Software/data/magic_favorites.edb");
|
---|
174 |
|
---|
175 | //Print();
|
---|
176 | /*
|
---|
177 | GetFileNames(env, fSequencesOn);
|
---|
178 | GetFileNames(env, fSequencesOff);
|
---|
179 | */
|
---|
180 | }
|
---|
181 |
|
---|
182 | // --------------------------------------------------------------------------
|
---|
183 | //
|
---|
184 | // Return '+' if both can be accessed, '-' otherwise.
|
---|
185 | //
|
---|
186 | void MDataSet::PrintFile(const TObject &obj)
|
---|
187 | {
|
---|
188 | const Bool_t access = !gSystem->AccessPathName(obj.GetName(), kFileExists) && !gSystem->AccessPathName(obj.GetTitle(), kFileExists) ? '+' : '-';
|
---|
189 | gLog << " " << (access?"+":"-") << " " << obj.GetName() << " <" << obj.GetTitle() << ">" << endl;
|
---|
190 | }
|
---|
191 |
|
---|
192 | // --------------------------------------------------------------------------
|
---|
193 | //
|
---|
194 | // Print the contents of the sequence
|
---|
195 | //
|
---|
196 | void MDataSet::Print(Option_t *o) const
|
---|
197 | {
|
---|
198 | gLog << all;
|
---|
199 | if (!IsValid())
|
---|
200 | {
|
---|
201 | gLog << "Sequence: " << fName << " <invalid>" << endl;
|
---|
202 | return;
|
---|
203 | }
|
---|
204 | gLog << "Analysis Number: " << fNumAnalysis << endl;
|
---|
205 | gLog << "Sequences On: ";
|
---|
206 | for (int i=0; i<fNumSequencesOn.GetSize(); i++)
|
---|
207 | gLog << " " << fNumSequencesOn[i];
|
---|
208 | gLog << endl;
|
---|
209 | gLog << "Sequences Off: ";
|
---|
210 | for (int i=0; i<fNumSequencesOff.GetSize(); i++)
|
---|
211 | gLog << " " << fNumSequencesOff[i];
|
---|
212 | gLog << endl;
|
---|
213 |
|
---|
214 | gLog << "SourceName: " << fNameSource << endl;
|
---|
215 | gLog << "Catalog: " << fCatalog << endl;
|
---|
216 |
|
---|
217 | if (!TString(o).Contains("files", TString::kIgnoreCase))
|
---|
218 | return;
|
---|
219 |
|
---|
220 | TObject *obj=0;
|
---|
221 |
|
---|
222 | gLog << endl;
|
---|
223 | gLog << "On-Data Files:" << endl;
|
---|
224 | TIter NextOn(&fSequencesOn);
|
---|
225 | while ((obj=NextOn()))
|
---|
226 | PrintFile(*obj);
|
---|
227 |
|
---|
228 | gLog << endl;
|
---|
229 | gLog << "Off-Data Files:" << endl;
|
---|
230 | TIter NextOff(&fSequencesOff);
|
---|
231 | while ((obj=NextOff()))
|
---|
232 | PrintFile(*obj);
|
---|
233 | }
|
---|
234 |
|
---|
235 | Bool_t MDataSet::AddSequencesToList(const TList &list, MRead &read, char *id, Bool_t raw)
|
---|
236 | {
|
---|
237 | MDirIter files;
|
---|
238 |
|
---|
239 | TIter Next(const_cast<TList*>(&list));
|
---|
240 | TObject *o=0;
|
---|
241 | while ((o=Next()))
|
---|
242 | {
|
---|
243 | MSequence seq(o->GetName());
|
---|
244 | if (!seq.IsValid())
|
---|
245 | {
|
---|
246 | gLog << warn << "WARNING - Sequence " << o->GetName() << " invalid!" << endl;
|
---|
247 | return kFALSE;
|
---|
248 | }
|
---|
249 |
|
---|
250 | const TString dir(o->GetTitle());
|
---|
251 | seq.SetupDatRuns(files, dir.IsNull() ? 0 : dir.Data(), id, raw);
|
---|
252 | }
|
---|
253 |
|
---|
254 | if (gLog.GetDebugLevel()>4)
|
---|
255 | {
|
---|
256 | gLog << dbg << "Files which are searched:" << endl;
|
---|
257 | files.Print();
|
---|
258 | }
|
---|
259 |
|
---|
260 | return read.AddFiles(files)>0;
|
---|
261 | }
|
---|
262 |
|
---|
263 | Bool_t MDataSet::AddFiles(MRead &read, char *id, Bool_t raw) const
|
---|
264 | {
|
---|
265 | const Bool_t rc1 = AddFilesOff(read, id, raw);
|
---|
266 | const Bool_t rc2 = AddFilesOn(read, id, raw);
|
---|
267 | return rc1 && rc2;
|
---|
268 | }
|
---|
269 |
|
---|
270 | Bool_t MDataSet::AddFilesOn(MRead &read, char *id, Bool_t raw) const
|
---|
271 | {
|
---|
272 | return AddSequencesToList(fSequencesOn, read, id, raw);
|
---|
273 | }
|
---|
274 |
|
---|
275 | Bool_t MDataSet::AddFilesOff(MRead &read, char *id, Bool_t raw) const
|
---|
276 | {
|
---|
277 | return AddSequencesToList(fSequencesOff, read, id, raw);
|
---|
278 | }
|
---|
279 |
|
---|
280 | Bool_t MDataSet::GetSourcePos(MPointingPos &pos) const
|
---|
281 | {
|
---|
282 | if (!HasSource())
|
---|
283 | return kFALSE;
|
---|
284 |
|
---|
285 | TString catalog(fCatalog);
|
---|
286 | gSystem->ExpandPathName(catalog);
|
---|
287 |
|
---|
288 | ifstream fin(catalog);
|
---|
289 | if (!fin)
|
---|
290 | {
|
---|
291 | gLog << err << "Cannot open file " << catalog << ": ";
|
---|
292 | gLog << strerror(errno) << endl;
|
---|
293 | return kFALSE;
|
---|
294 | }
|
---|
295 |
|
---|
296 | TString ra,dec,epoch;
|
---|
297 |
|
---|
298 | Int_t n = 0;
|
---|
299 | while (1)
|
---|
300 | {
|
---|
301 | TString line;
|
---|
302 | line.ReadLine(fin);
|
---|
303 | if (!fin)
|
---|
304 | break;
|
---|
305 |
|
---|
306 | n++;
|
---|
307 | line = line.Strip(TString::kBoth);
|
---|
308 |
|
---|
309 | if (!line.BeginsWith(fNameSource))
|
---|
310 | continue;
|
---|
311 |
|
---|
312 | // CrabNebula,f|L|K0,5:34:32.0,22:0:52,-1.0,2000
|
---|
313 |
|
---|
314 | for (int i=0; i<6; i++)
|
---|
315 | {
|
---|
316 | const Ssiz_t p = line.First(',');
|
---|
317 | if (p<0 && i<5)
|
---|
318 | {
|
---|
319 | gLog << err << "Not enough arguments in line #" << n << endl;
|
---|
320 | return kFALSE;
|
---|
321 | }
|
---|
322 |
|
---|
323 | switch (i)
|
---|
324 | {
|
---|
325 | case 0:
|
---|
326 | case 1:
|
---|
327 | case 4:
|
---|
328 | break;
|
---|
329 | case 2:
|
---|
330 | ra = line(0, p);
|
---|
331 | break;
|
---|
332 | case 3:
|
---|
333 | dec = line(0, p);
|
---|
334 | break;
|
---|
335 | case 5:
|
---|
336 | epoch = line;
|
---|
337 | break;
|
---|
338 | }
|
---|
339 | line.Remove(0, p+1);
|
---|
340 | }
|
---|
341 |
|
---|
342 | if (line.First(',')>=0)
|
---|
343 | {
|
---|
344 | gLog << err << "Too much arguments in line #" << n << endl;
|
---|
345 | return kFALSE;
|
---|
346 | }
|
---|
347 | break;
|
---|
348 | }
|
---|
349 |
|
---|
350 | if (epoch!=(TString)"2000")
|
---|
351 | {
|
---|
352 | gLog << err << "Epoch not 2000... not supported." << endl;
|
---|
353 | return kFALSE;
|
---|
354 | }
|
---|
355 |
|
---|
356 | Double_t r,d;
|
---|
357 | if (!MAstro::Coordinate2Angle(ra, r))
|
---|
358 | {
|
---|
359 | gLog << err << "ERROR - Interpreting right ascension: " << ra << endl;
|
---|
360 | return kFALSE;
|
---|
361 | }
|
---|
362 | if (!MAstro::Coordinate2Angle(dec, d))
|
---|
363 | {
|
---|
364 | gLog << err << "ERROR - Interpreting declination: " << dec << endl;
|
---|
365 | return kFALSE;
|
---|
366 | }
|
---|
367 |
|
---|
368 | pos.SetSkyPosition(r, d);
|
---|
369 | pos.SetTitle(fNameSource);
|
---|
370 |
|
---|
371 | return kTRUE;
|
---|
372 | }
|
---|