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

Last change on this file since 7203 was 7179, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 13.1 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. Each sequence
52// can be added to the on and off data at the same time but only once.
53//
54// If you have different file names you can overwrite the default file names
55// using Sequence%08d.File (make sure you have 8 digits!)
56//
57// In standard coditions (datacenter file system) paths are concatenated
58// by using the information in the sequence files (date, etc). You can
59// overwrite the directories in which the sequence-files (eg I-files) are
60// stored using Sequence%08d.Dir (make sure you have 8 digits!)
61//
62// Resource file entries are case sensitive!
63//
64// IMPORTANT:
65// * Run filenames must begin with a string which allows correct
66// ordering in time, otherwise synchronization might fail.
67// * Sequence filenames should also have names allowing to order them
68// in time, but it is not necessary.
69//
70// MISSING (27/01/04): The default name and paths cannot be used yet, because
71// they have to be defined soon.
72//
73/////////////////////////////////////////////////////////////////////////////
74#include "MDataSet.h"
75
76#include <stdlib.h>
77#include <fstream>
78
79#include <TEnv.h>
80#include <TChain.h>
81#include <TRegexp.h>
82#include <TSystem.h> // TSystem::ExpandPath
83
84#include "MLog.h"
85#include "MLogManip.h"
86
87#include "MRead.h"
88#include "MAstro.h"
89#include "MDirIter.h"
90#include "MSequence.h"
91#include "MPointingPos.h"
92
93ClassImp(MDataSet);
94
95using namespace std;
96
97// --------------------------------------------------------------------------
98//
99// Copy the sequence numbers from the TString runs into the TArrayI data
100// Sequences which are twice in the list are only added once. In this case
101// a warning is emitted.
102//
103void MDataSet::Split(TString &runs, TArrayI &data) const
104{
105 const TRegexp regexp("[0-9]+");
106
107 data.Set(0);
108 runs = runs.Strip(TString::kTrailing);
109
110 while (!runs.IsNull())
111 {
112 TString num = runs(regexp);
113
114 const Int_t seq = atoi(num.Data());
115 const Int_t n = data.GetSize();
116
117 // skip already existing entries
118 int i;
119 for (i=0; i<n; i++)
120 if (data[i] == seq)
121 break;
122
123 if (i<n)
124 {
125 *fLog << warn << "WARNING - Sequence #" << seq << " alraedy in list... skipped." << endl;
126 continue;
127 }
128
129 // set new entry
130 data.Set(n+1);
131 data[n] = seq;
132
133 // remove entry from string
134 runs.Remove(0, runs.First(num)+num.Length());
135 }
136}
137
138// --------------------------------------------------------------------------
139//
140// After resolving the sequence filename and directory either from the
141// default (/magic/data/sequences/0004/sequence00004000.txt) or from
142// the corresponding entries in the dataset file.
143// The entries are sorted by filename.
144//
145void MDataSet::ResolveSequences(TEnv &env, const TArrayI &num, TList &list) const
146{
147 for (int i=0; i<num.GetSize(); i++)
148 {
149 TString name = env.GetValue(Form("Sequence%08d.File", num[i]), "");
150 TString dir = env.GetValue(Form("Sequence%08d.Dir", num[i]), "");
151
152 gSystem->ExpandPathName(name);
153 gSystem->ExpandPathName(dir);
154
155 // Set default sequence file and dir name
156 if (name.IsNull())
157 name = Form("/magic/sequences/%04d/sequence%08d.txt", num[i]/10000, num[i]);
158 if (dir.IsNull())
159 dir = Form("/magic/data/star/%04d/%08d", num[i]/10000, num[i]);
160
161 if (gSystem->AccessPathName(name, kFileExists))
162 gLog << warn << "WARNING - Sequence file '" << name << "' doesn't exist." << endl;
163
164 if (gSystem->AccessPathName(dir, kFileExists))
165 gLog << warn << "WARNING - Directory '" << dir << "' doesn't exist." << endl;
166
167 list.Add(new TNamed(name, dir));
168 }
169
170 // For the synchronization we must make sure, that all sequences are
171 // in the correct order...
172 list.Sort();
173}
174
175// --------------------------------------------------------------------------
176//
177// Read the file fname as setup file for the sequence.
178//
179MDataSet::MDataSet(const char *fname)
180{
181 fName = fname;
182
183 const char *expname = gSystem->ExpandPathName(fname);
184
185 fTitle = Form("Sequences contained in file %s", expname);
186
187 TEnv env(expname);
188 delete [] expname;
189
190 TString str;
191
192 fNumAnalysis = env.GetValue("AnalysisNumber", -1);
193
194 str = env.GetValue("SequencesOn", "");
195 Split(str, fNumSequencesOn);
196 str = env.GetValue("SequencesOff", "");
197 Split(str, fNumSequencesOff);
198
199 ResolveSequences(env, fNumSequencesOn, fSequencesOn);
200 ResolveSequences(env, fNumSequencesOff, fSequencesOff);
201
202
203 fNameSource = env.GetValue("SourceName", "");
204 fCatalog = env.GetValue("Catalog", "~/Software/data/magic_favorites.edb");
205 fIsWobbleMode = env.GetValue("WobbleMode", kFALSE);
206
207 //Print();
208 /*
209 GetFileNames(env, fSequencesOn);
210 GetFileNames(env, fSequencesOff);
211 */
212}
213
214// --------------------------------------------------------------------------
215//
216// Return '+' if both can be accessed, '-' otherwise.
217//
218void MDataSet::PrintFile(const TObject &obj)
219{
220 const Bool_t access = !gSystem->AccessPathName(obj.GetName(), kFileExists) && !gSystem->AccessPathName(obj.GetTitle(), kFileExists) ? '+' : '-';
221 gLog << " " << (access?"+":"-") << " " << obj.GetName() << " <" << obj.GetTitle() << ">" << endl;
222}
223
224// --------------------------------------------------------------------------
225//
226// Print the contents of the sequence
227//
228void MDataSet::Print(Option_t *o) const
229{
230 gLog << all;
231 if (!IsValid())
232 {
233 gLog << "Sequence: " << fName << " <invalid>" << endl;
234 return;
235 }
236 gLog << "Analysis Number: " << fNumAnalysis << endl;
237 gLog << "Sequences On: ";
238 for (int i=0; i<fNumSequencesOn.GetSize(); i++)
239 gLog << " " << fNumSequencesOn[i];
240 gLog << endl;
241 gLog << "Sequences Off: ";
242 for (int i=0; i<fNumSequencesOff.GetSize(); i++)
243 gLog << " " << fNumSequencesOff[i];
244 gLog << endl;
245
246 gLog << "SourceName: " << fNameSource << endl;
247 gLog << "Catalog: " << fCatalog << endl;
248
249 gLog << "WobbleMode: " << (fIsWobbleMode?"On":"Off") << endl;
250
251 if (!TString(o).Contains("files", TString::kIgnoreCase))
252 return;
253
254 TObject *obj=0;
255
256 gLog << endl;
257 gLog << "On-Data Files:" << endl;
258 TIter NextOn(&fSequencesOn);
259 while ((obj=NextOn()))
260 PrintFile(*obj);
261
262 gLog << endl;
263 gLog << "Off-Data Files:" << endl;
264 TIter NextOff(&fSequencesOff);
265 while ((obj=NextOff()))
266 PrintFile(*obj);
267}
268
269// --------------------------------------------------------------------------
270//
271// Adds all sequences contained in list to the MDirIter. After adding
272// everything MDirIter::Sort is called to sort all entries by name.
273//
274Bool_t MDataSet::AddSequencesFromList(const TList &list, MDirIter &files)
275{
276 TIter Next(const_cast<TList*>(&list));
277 TObject *o=0;
278 while ((o=Next()))
279 {
280 MSequence seq(o->GetName());
281 if (!seq.IsValid())
282 {
283 gLog << warn << "WARNING - Sequence " << o->GetName() << " invalid!" << endl;
284 return kFALSE;
285 }
286
287 const TString dir(o->GetTitle());
288 seq.SetupDatRuns(files, MSequence::kImages, dir.IsNull() ? 0 : dir.Data());
289 }
290
291 // This is important in case of synchronisation, because the
292 // files in the sequences can be interleaved (eg W1, W2)
293 // Filenames MUST begin with an appropriate string which allow
294 // to order them correctly in time!
295 files.Sort();
296
297 if (gLog.GetDebugLevel()>4)
298 {
299 gLog << dbg << "Files which are searched:" << endl;
300 files.Print();
301 }
302 return kTRUE;
303}
304
305Bool_t MDataSet::AddFilesOn(MRead &read) const
306{
307 MDirIter files;
308 if (!AddSequencesFromList(fSequencesOn, files))
309 return kFALSE;
310 return read.AddFiles(files)>0;
311}
312
313Bool_t MDataSet::AddFilesOff(MRead &read) const
314{
315 MDirIter files;
316 if (!AddSequencesFromList(fSequencesOff, files))
317 return kFALSE;
318 return read.AddFiles(files)>0;
319}
320
321Bool_t MDataSet::AddFiles(MRead &read) const
322{
323 const Bool_t rc1 = AddFilesOff(read);
324 const Bool_t rc2 = AddFilesOn(read);
325 return rc1 && rc2;
326}
327
328Int_t MDataSet::AddFilesToChain(MDirIter &files, TChain &chain)
329{
330 Int_t num=0;
331 while (1)
332 {
333 const TString fname = files.Next();
334 if (fname.IsNull())
335 break;
336
337 const Int_t n = chain.Add(fname);
338 if (n<=0)
339 return kFALSE;
340 num += n;
341 }
342 return num;
343}
344
345Bool_t MDataSet::AddFilesOn(TChain &chain) const
346{
347 MDirIter files;
348 if (!AddSequencesFromList(fSequencesOn, files))
349 return kFALSE;
350 return AddFilesToChain(files, chain)>0;
351}
352
353Bool_t MDataSet::AddFilesOff(TChain &chain) const
354{
355 MDirIter files;
356 if (!AddSequencesFromList(fSequencesOff, files))
357 return kFALSE;
358 return AddFilesToChain(files, chain)>0;
359}
360
361Bool_t MDataSet::AddFiles(TChain &read) const
362{
363 const Bool_t rc1 = AddFilesOff(read);
364 const Bool_t rc2 = AddFilesOn(read);
365 return rc1 && rc2;
366}
367
368Bool_t MDataSet::GetSourcePos(MPointingPos &pos) const
369{
370 if (!HasSource())
371 {
372 gLog << err << "ERROR - MDataSet::GetSourcePos called, but no source available." << endl;
373 return kFALSE;
374 }
375
376 TString catalog(fCatalog);
377 gSystem->ExpandPathName(catalog);
378
379 ifstream fin(catalog);
380 if (!fin)
381 {
382 gLog << err << "Cannot open file " << catalog << ": ";
383 gLog << strerror(errno) << endl;
384 return kFALSE;
385 }
386
387 TString ra, dec, epoch;
388
389 Int_t n = 0;
390 while (1)
391 {
392 TString line;
393 line.ReadLine(fin);
394 if (!fin)
395 break;
396
397 n++;
398
399 // Strip all spaces from line
400 for (int i=0; i<line.Length(); i++)
401 if (line[i]==' ')
402 line.Remove(i--, 1);
403
404 if (fNameSource!=line(0, fNameSource.Length()))
405 continue;
406
407 // CrabNebula,f|L|K0,5:34:32.0,22:0:52,-1.0,2000
408
409 for (int i=0; i<6; i++)
410 {
411 const Ssiz_t p = line.First(',');
412 if (p<0 && i<5)
413 {
414 gLog << err << "ERROR - Not enough arguments in line #" << n << " of " << catalog << endl;
415 return kFALSE;;
416 }
417
418 switch (i)
419 {
420 case 0:
421 case 1:
422 case 4:
423 break;
424 case 2:
425 ra = line(0, p);
426 break;
427 case 3:
428 dec = line(0, p);
429 break;
430 case 5:
431 epoch = line;
432 break;
433 }
434 line.Remove(0, p+1);
435 }
436
437 if (line.First(',')>=0)
438 {
439 gLog << err << "ERROR - Too much arguments in line #" << n << " of " << catalog << endl;
440 return kFALSE;
441 }
442
443 break;
444 }
445
446 if (epoch.IsNull())
447 {
448 gLog << err << "ERROR - No entry " << fNameSource << " in " << catalog << endl;
449 return kFALSE;
450 }
451
452 if (epoch!=(TString)"2000")
453 {
454 gLog << err << "ERROR - Epoch not 2000... not supported." << endl;
455 return kFALSE;
456 }
457
458 Double_t r,d;
459 if (!MAstro::Coordinate2Angle(ra, r))
460 {
461 gLog << err << "ERROR - Interpreting right ascension: " << ra << endl;
462 return kFALSE;
463 }
464 if (!MAstro::Coordinate2Angle(dec, d))
465 {
466 gLog << err << "ERROR - Interpreting declination: " << dec << endl;
467 return kFALSE;
468 }
469
470 pos.SetSkyPosition(r, d);
471 pos.SetTitle(fNameSource);
472
473 return kTRUE;
474}
Note: See TracBrowser for help on using the repository browser.