source: trunk/MagicSoft/Mars/mjobs/MDataSet.cc@ 6906

Last change on this file since 6906 was 6906, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 10.0 KB
Line 
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
85ClassImp(MDataSet);
86
87using namespace std;
88
89// --------------------------------------------------------------------------
90//
91// Copy the run numbers from the TString runs into the TArrayI data
92//
93void 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
112void 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 // Set default sequence file and dir name
123 if (name.IsNull())
124 name = Form("/magic/sequences/%04d/sequence%08d.txt", num[i]/1000, num[i]);
125 if (dir.IsNull())
126 name = Form("/magic/data/star/%04d/%08d", num[i]/1000, num[i]);
127
128 if (gSystem->AccessPathName(name, kFileExists))
129 gLog << warn << "WARNING - Sequence file '" << name << "' doesn't exist." << endl;
130
131 if (gSystem->AccessPathName(dir, kFileExists))
132 gLog << warn << "WARNING - Directory '" << dir << "' doesn't exist." << endl;
133
134 list.Add(new TNamed(name, dir));
135 }
136
137 // For the synchronization we must make sure, that all sequences are
138 // in the correct order...
139 list.Sort();
140}
141
142// --------------------------------------------------------------------------
143//
144// Read the file fname as setup file for the sequence.
145//
146MDataSet::MDataSet(const char *fname)
147{
148 fName = fname;
149
150 const char *expname = gSystem->ExpandPathName(fname);
151
152 fTitle = Form("Sequences contained in file %s", expname);
153
154 TEnv env(expname);
155 delete [] expname;
156
157 TString str;
158
159 fNumAnalysis = env.GetValue("AnalysisNumber", -1);
160
161 str = env.GetValue("SequencesOn", "");
162 Split(str, fNumSequencesOn);
163 str = env.GetValue("SequencesOff", "");
164 Split(str, fNumSequencesOff);
165
166 ResolveSequences(env, fNumSequencesOn, fSequencesOn);
167 ResolveSequences(env, fNumSequencesOff, fSequencesOff);
168
169
170 fNameSource = env.GetValue("SourceName", "");
171 fCatalog = env.GetValue("Catalog", "~/Software/data/magic_favorites.edb");
172 fIsWobbleMode = env.GetValue("WobbleMode", kFALSE);
173
174 //Print();
175 /*
176 GetFileNames(env, fSequencesOn);
177 GetFileNames(env, fSequencesOff);
178 */
179}
180
181// --------------------------------------------------------------------------
182//
183// Return '+' if both can be accessed, '-' otherwise.
184//
185void MDataSet::PrintFile(const TObject &obj)
186{
187 const Bool_t access = !gSystem->AccessPathName(obj.GetName(), kFileExists) && !gSystem->AccessPathName(obj.GetTitle(), kFileExists) ? '+' : '-';
188 gLog << " " << (access?"+":"-") << " " << obj.GetName() << " <" << obj.GetTitle() << ">" << endl;
189}
190
191// --------------------------------------------------------------------------
192//
193// Print the contents of the sequence
194//
195void MDataSet::Print(Option_t *o) const
196{
197 gLog << all;
198 if (!IsValid())
199 {
200 gLog << "Sequence: " << fName << " <invalid>" << endl;
201 return;
202 }
203 gLog << "Analysis Number: " << fNumAnalysis << endl;
204 gLog << "Sequences On: ";
205 for (int i=0; i<fNumSequencesOn.GetSize(); i++)
206 gLog << " " << fNumSequencesOn[i];
207 gLog << endl;
208 gLog << "Sequences Off: ";
209 for (int i=0; i<fNumSequencesOff.GetSize(); i++)
210 gLog << " " << fNumSequencesOff[i];
211 gLog << endl;
212
213 gLog << "SourceName: " << fNameSource << endl;
214 gLog << "Catalog: " << fCatalog << endl;
215
216 gLog << "WobbleMode: " << (fIsWobbleMode?"On":"Off") << endl;
217
218 if (!TString(o).Contains("files", TString::kIgnoreCase))
219 return;
220
221 TObject *obj=0;
222
223 gLog << endl;
224 gLog << "On-Data Files:" << endl;
225 TIter NextOn(&fSequencesOn);
226 while ((obj=NextOn()))
227 PrintFile(*obj);
228
229 gLog << endl;
230 gLog << "Off-Data Files:" << endl;
231 TIter NextOff(&fSequencesOff);
232 while ((obj=NextOff()))
233 PrintFile(*obj);
234}
235
236Bool_t MDataSet::AddSequencesToList(const TList &list, MRead &read)
237{
238 MDirIter files;
239
240 TIter Next(const_cast<TList*>(&list));
241 TObject *o=0;
242 while ((o=Next()))
243 {
244 MSequence seq(o->GetName());
245 if (!seq.IsValid())
246 {
247 gLog << warn << "WARNING - Sequence " << o->GetName() << " invalid!" << endl;
248 return kFALSE;
249 }
250
251 const TString dir(o->GetTitle());
252 seq.SetupDatRuns(files, MSequence::kImages, dir.IsNull() ? 0 : dir.Data());
253 }
254
255 if (gLog.GetDebugLevel()>4)
256 {
257 gLog << dbg << "Files which are searched:" << endl;
258 files.Print();
259 }
260
261 return read.AddFiles(files)>0;
262}
263
264Bool_t MDataSet::AddFiles(MRead &read) const
265{
266 const Bool_t rc1 = AddFilesOff(read);
267 const Bool_t rc2 = AddFilesOn(read);
268 return rc1 && rc2;
269}
270
271Bool_t MDataSet::AddFilesOn(MRead &read) const
272{
273 return AddSequencesToList(fSequencesOn, read);
274}
275
276Bool_t MDataSet::AddFilesOff(MRead &read) const
277{
278 return AddSequencesToList(fSequencesOff, read);
279}
280
281Bool_t MDataSet::GetSourcePos(MPointingPos &pos) const
282{
283 if (!HasSource())
284 return kFALSE;
285
286 TString catalog(fCatalog);
287 gSystem->ExpandPathName(catalog);
288
289 ifstream fin(catalog);
290 if (!fin)
291 {
292 gLog << err << "Cannot open file " << catalog << ": ";
293 gLog << strerror(errno) << endl;
294 return kFALSE;
295 }
296
297 TString ra,dec,epoch;
298
299 Int_t n = 0;
300 while (1)
301 {
302 TString line;
303 line.ReadLine(fin);
304 if (!fin)
305 break;
306
307 n++;
308 line = line.Strip(TString::kBoth);
309
310 if (!line.BeginsWith(fNameSource))
311 continue;
312
313 // CrabNebula,f|L|K0,5:34:32.0,22:0:52,-1.0,2000
314
315 for (int i=0; i<6; i++)
316 {
317 const Ssiz_t p = line.First(',');
318 if (p<0 && i<5)
319 {
320 gLog << err << "Not enough arguments in line #" << n << endl;
321 return kFALSE;
322 }
323
324 switch (i)
325 {
326 case 0:
327 case 1:
328 case 4:
329 break;
330 case 2:
331 ra = line(0, p);
332 break;
333 case 3:
334 dec = line(0, p);
335 break;
336 case 5:
337 epoch = line;
338 break;
339 }
340 line.Remove(0, p+1);
341 }
342
343 if (line.First(',')>=0)
344 {
345 gLog << err << "Too much arguments in line #" << n << endl;
346 return kFALSE;
347 }
348 break;
349 }
350
351 if (epoch!=(TString)"2000")
352 {
353 gLog << err << "Epoch not 2000... not supported." << endl;
354 return kFALSE;
355 }
356
357 Double_t r,d;
358 if (!MAstro::Coordinate2Angle(ra, r))
359 {
360 gLog << err << "ERROR - Interpreting right ascension: " << ra << endl;
361 return kFALSE;
362 }
363 if (!MAstro::Coordinate2Angle(dec, d))
364 {
365 gLog << err << "ERROR - Interpreting declination: " << dec << endl;
366 return kFALSE;
367 }
368
369 pos.SetSkyPosition(r, d);
370 pos.SetTitle(fNameSource);
371
372 return kTRUE;
373}
Note: See TracBrowser for help on using the repository browser.