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

Last change on this file since 8270 was 8244, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 15.4 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/////////////////////////////////////////////////////////////////////////////
71#include "MDataSet.h"
72
73#include <string.h> // necessary for Fedora core 2 with kernel 2.6.9-1.667 #1 and gcc 3.4.2
74#include <errno.h> // necessary for Fedora core 2 with kernel 2.6.9-1.667 #1 and gcc 3.4.2
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 "MJob.h"
89#include "MEnv.h"
90#include "MAstro.h"
91#include "MDirIter.h"
92#include "MSequence.h"
93#include "MPointingPos.h"
94
95ClassImp(MDataSet);
96
97using namespace std;
98
99const TString MDataSet::fgCatalog = "/magic/datacenter/setup/magic_favorites.edb";
100const TString MDataSet::fgPathDataFiles = "/magic/data/star";
101const TString MDataSet::fgPathSequences = "/magic/sequences";
102
103// --------------------------------------------------------------------------
104//
105// Copy the sequence numbers from the TString runs into the TArrayI data
106// Sequences which are twice in the list are only added once. In this case
107// a warning is emitted.
108//
109void MDataSet::Split(TString &runs, TArrayI &data) const
110{
111 const TRegexp regexp("[0-9]+");
112
113 data.Set(0);
114 runs = runs.Strip(TString::kTrailing);
115
116 while (!runs.IsNull())
117 {
118 const TString num = runs(regexp);
119
120 if (num.IsNull())
121 {
122 *fLog << warn << "WARNING - Sequence is NaN (not a number): " << runs << endl;
123 break;
124 }
125
126 const Int_t seq = atoi(num.Data());
127 const Int_t n = data.GetSize();
128
129 // skip already existing entries
130 int i;
131 for (i=0; i<n; i++)
132 if (data[i] == seq)
133 break;
134
135 if (i<n)
136 *fLog << warn << "WARNING - Sequence #" << seq << " already in list... skipped." << endl;
137 else
138 {
139 // set new entry
140 data.Set(n+1);
141 data[n] = seq;
142 }
143
144 // remove entry from string
145 runs.Remove(0, runs.First(num)+num.Length());
146 }
147
148 MJob::SortArray(data);
149}
150
151// --------------------------------------------------------------------------
152//
153// After resolving the sequence filename and directory either from the
154// default (/magic/data/sequences/0004/sequence00004000.txt) or from
155// the corresponding entries in the dataset file.
156// The entries are sorted by filename.
157//
158void MDataSet::ResolveSequences(TEnv &env, const TArrayI &num, TList &list) const
159{
160 TString sequences = fPathSequences;
161 TString data = fPathDataFiles;
162
163 for (int i=0; i<num.GetSize(); i++)
164 {
165 TString name = env.GetValue(Form("Sequence%08d.File", num[i]), "");
166 TString dir = env.GetValue(Form("Sequence%08d.Dir", num[i]), "");
167
168 // Set default sequence file and dir name
169 if (name.IsNull())
170 name = Form("%s%04d/sequence%08d.txt", sequences.Data(), num[i]/10000, num[i]);
171 if (dir.IsNull())
172 dir = Form("%s%04d/%08d", data.Data(), num[i]/10000, num[i]);
173
174 // FIXME: The sequence number from the sequence file is assigned!!!
175 MSequence *seq = new MSequence(name, dir);
176
177 if (seq->IsValid() && seq->GetSequence()!=(UInt_t)num[i])
178 *fLog << warn << "WARNING - Sequence number " << num[i] << " in dataset file doesn't match sequence number " << seq->GetSequence() << " in sequence file!" << endl;
179
180 list.Add(seq);
181 }
182
183 // For the synchronization we must make sure, that all sequences are
184 // in the correct order...
185 // list.Sort();
186}
187
188// --------------------------------------------------------------------------
189//
190// Read the file fname as setup file for the sequence.
191//
192MDataSet::MDataSet(const char *fname, TString sequences, TString data)
193{
194 fName = fname;
195
196 fSequencesOn.SetOwner();
197 fSequencesOff.SetOwner();
198
199 const char *expname = gSystem->ExpandPathName(fname);
200
201 fTitle = Form("Dataset contained in file %s", expname);
202
203 const Bool_t access = !gSystem->AccessPathName(expname, kFileExists);
204 if (!access)
205 gLog << err << "ERROR - Dataset file " << expname << " not accessible!" << endl;
206
207 MEnv env(expname);
208 delete [] expname;
209
210 fNumAnalysis = env.GetValue("AnalysisNumber", -1);
211
212 TString str;
213 str = env.GetValue("SequencesOn", "");
214 Split(str, fNumSequencesOn);
215 str = env.GetValue("SequencesOff", "");
216 Split(str, fNumSequencesOff);
217
218 SetupDefaultPath(sequences, fgPathSequences);
219 SetupDefaultPath(data, fgPathDataFiles);
220
221 fPathSequences = sequences;
222 fPathDataFiles = data;
223
224 ResolveSequences(env, fNumSequencesOn, fSequencesOn);
225 ResolveSequences(env, fNumSequencesOff, fSequencesOff);
226
227 fNameSource = env.GetValue("SourceName", "");
228 fCatalog = env.GetValue("Catalog", fgCatalog);
229 fIsWobbleMode = env.GetValue("WobbleMode", kFALSE);
230 fComment = env.GetValue("Comment", "");
231
232 fNameSource = fNameSource.Strip(TString::kBoth);
233 fCatalog = fCatalog.Strip(TString::kBoth);
234
235 if (env.GetNumUntouched()>0)
236 {
237 gLog << warn << "WARNING - At least one resource in the dataset-file has not been touched!" << endl;
238 env.PrintUntouched();
239 }
240}
241
242// --------------------------------------------------------------------------
243//
244// Return '+' if both can be accessed, '-' otherwise.
245//
246void MDataSet::PrintFile(const MSequence &seq)
247{
248 const Char_t access =
249 !gSystem->AccessPathName(seq.GetFileName(), kFileExists) &&
250 !gSystem->AccessPathName(seq.GetDataPath(), kFileExists) ? '+' : '-';
251
252 gLog << "# " << access << " " << seq.GetFileName() << " <" << seq.GetDataPath() << ">" << endl;
253}
254
255// --------------------------------------------------------------------------
256//
257// Print the contents of the sequence
258//
259void MDataSet::Print(Option_t *o) const
260{
261 gLog << all;
262 if (!IsValid())
263 {
264 gLog << "Dataset: " << fName << " <invalid - no analysis number available>" << endl;
265 return;
266 }
267 gLog << "AnalysisNumber: " << fNumAnalysis << endl << endl;
268 gLog << "SequencesOn: ";
269 for (int i=0; i<fNumSequencesOn.GetSize(); i++)
270 gLog << " " << fNumSequencesOn[i];
271 gLog << endl;
272 gLog << "SequencesOff: ";
273 for (int i=0; i<fNumSequencesOff.GetSize(); i++)
274 gLog << " " << fNumSequencesOff[i];
275 gLog << endl << endl;
276
277 gLog << "SourceName: " << fNameSource << endl;
278 gLog << "Catalog: " << fCatalog << endl;
279
280 gLog << "WobbleMode: " << (fIsWobbleMode?"On":"Off") << endl << endl;
281
282 gLog << "Comment: " << fComment << endl;
283
284 if (fSequencesOn.GetEntries()>0)
285 gLog << endl;
286
287 TIter NextOn(&fSequencesOn);
288 TIter NextOff(&fSequencesOff);
289 MSequence *seq=0;
290 while ((seq=(MSequence*)NextOn()))
291 {
292 gLog << "Sequence" << Form("%08d", seq->GetSequence()) << ".File: " << seq->GetFileName() << endl;
293 gLog << "Sequence" << Form("%08d", seq->GetSequence()) << ".Dir: " << seq->GetDataPath() << endl;
294 }
295 if (fSequencesOff.GetEntries()>0)
296 gLog << endl;
297 while ((seq=(MSequence*)NextOff()))
298 {
299 gLog << "Sequence" << Form("%08d", seq->GetSequence()) << ".File: " << seq->GetFileName() << endl;
300 gLog << "Sequence" << Form("%08d", seq->GetSequence()) << ".Dir: " << seq->GetDataPath() << endl;
301 }
302
303 if (TString(o).Contains("files", TString::kIgnoreCase))
304 {
305 gLog << endl;
306 gLog << "# On-Data Files:" << endl;
307 NextOn.Reset();
308 while ((seq=(MSequence*)NextOn()))
309 PrintFile(*seq);
310
311 gLog << endl;
312 gLog << "# Off-Data Files:" << endl;
313 NextOff.Reset();
314 while ((seq=(MSequence*)NextOff()))
315 PrintFile(*seq);
316
317 return;
318 }
319}
320
321// --------------------------------------------------------------------------
322//
323// Adds all sequences contained in list to the MDirIter. After adding
324// everything MDirIter::Sort is called to sort all entries by name.
325//
326Bool_t MDataSet::AddSequencesFromList(const TList &list, MDirIter &files)
327{
328 TIter Next(const_cast<TList*>(&list));
329
330 MSequence *seq=0;
331 while ((seq=(MSequence*)Next()))
332 {
333 if (!seq->IsValid())
334 {
335 gLog << err;
336 gLog << "ERROR - MDataSet::AddSequencesFromList: Sequence invalid!" << endl;
337 gLog << " + File: " << seq->GetFileName() << endl;
338 gLog << " + Dir: " << seq->GetDataPath() << endl;
339 return kFALSE;
340 }
341
342 if (seq->SetupDatRuns(files, MSequence::kImages)<=0)
343 return kFALSE;
344 }
345
346 // This is important in case of synchronisation, because the
347 // files in the sequences can be interleaved (eg W1, W2)
348 // Filenames MUST begin with an appropriate string which allow
349 // to order them correctly in time!
350 // files.Sort();
351
352 if (gLog.GetDebugLevel()>4)
353 {
354 gLog << dbg << "Files which are searched:" << endl;
355 files.Print();
356 }
357 return kTRUE;
358}
359
360Bool_t MDataSet::AddFilesOn(MRead &read) const
361{
362 MDirIter files;
363 if (!AddSequencesFromList(fSequencesOn, files))
364 return kFALSE;
365 return read.AddFiles(files)>0;
366}
367
368Bool_t MDataSet::AddFilesOff(MRead &read) const
369{
370 MDirIter files;
371 if (!AddSequencesFromList(fSequencesOff, files))
372 return kFALSE;
373 return read.AddFiles(files)>0;
374}
375
376Bool_t MDataSet::AddFiles(MRead &read) const
377{
378 const Bool_t rc1 = AddFilesOff(read);
379 const Bool_t rc2 = AddFilesOn(read);
380 return rc1 && rc2;
381}
382
383Int_t MDataSet::AddFilesToChain(MDirIter &files, TChain &chain)
384{
385 Int_t num=0;
386 while (1)
387 {
388 const TString fname = files.Next();
389 if (fname.IsNull())
390 break;
391
392 const Int_t n = chain.Add(fname);
393 if (n<=0)
394 return kFALSE;
395 num += n;
396 }
397 return num;
398}
399
400Bool_t MDataSet::AddFilesOn(TChain &chain) const
401{
402 MDirIter files;
403 if (!AddSequencesFromList(fSequencesOn, files))
404 return kFALSE;
405 return AddFilesToChain(files, chain)>0;
406}
407
408Bool_t MDataSet::AddFilesOff(TChain &chain) const
409{
410 MDirIter files;
411 if (!AddSequencesFromList(fSequencesOff, files))
412 return kFALSE;
413 return AddFilesToChain(files, chain)>0;
414}
415
416Bool_t MDataSet::AddFiles(TChain &read) const
417{
418 const Bool_t rc1 = AddFilesOff(read);
419 const Bool_t rc2 = AddFilesOn(read);
420 return rc1 && rc2;
421}
422
423Bool_t MDataSet::GetSourcePos(MPointingPos &pos) const
424{
425 if (!HasSource())
426 {
427 gLog << err << "ERROR - MDataSet::GetSourcePos called, but no source available." << endl;
428 return kFALSE;
429 }
430
431 TString catalog(fCatalog);
432 gSystem->ExpandPathName(catalog);
433
434 ifstream fin(catalog);
435 if (!fin)
436 {
437 gLog << err << "Cannot open file " << catalog << ": ";
438 gLog << strerror(errno) << endl;
439 return kFALSE;
440 }
441
442 TString ra, dec, epoch;
443
444 Int_t n = 0;
445 while (1)
446 {
447 TString line;
448 line.ReadLine(fin);
449 if (!fin)
450 {
451 gLog << err << "ERROR - Source '" << fNameSource << "' not found in " << catalog << "." << endl;
452 return kFALSE;
453 }
454
455 n++;
456
457 TObjArray *arr = line.Tokenize(",");
458
459 if (arr->GetEntries()<6)
460 {
461 gLog << err << "ERROR - Not enough arguments in line #" << n << " of " << catalog << endl;
462 delete arr;
463 return kFALSE;;
464 }
465
466 const TString name = (*arr)[0]->GetName();
467
468 ra = (*arr)[2]->GetName();
469 dec = (*arr)[3]->GetName();
470 epoch = (*arr)[5]->GetName();
471
472 delete arr;
473
474 if (name.Strip(TString::kBoth)==fNameSource)
475 break;
476 }
477
478 if (epoch.Strip(TString::kBoth)!=(TString)"2000")
479 {
480 gLog << err << "ERROR - Epoch not 2000... not supported." << endl;
481 return kFALSE;
482 }
483
484 Double_t r,d;
485 if (!MAstro::Coordinate2Angle(ra, r))
486 {
487 gLog << err << "ERROR - Interpreting right ascension: " << ra << endl;
488 return kFALSE;
489 }
490 if (!MAstro::Coordinate2Angle(dec, d))
491 {
492 gLog << err << "ERROR - Interpreting declination: " << dec << endl;
493 return kFALSE;
494 }
495
496 pos.SetSkyPosition(r, d);
497 pos.SetTitle(fNameSource);
498
499 return kTRUE;
500}
501
502// --------------------------------------------------------------------------
503//
504// Calls ReplaceAll(old, news) for all Dir-entries
505//
506void MDataSet::ReplaceDir(TList &list, const TString &old, const TString &news) const
507{
508 TIter Next(&list);
509 TNamed *name = 0;
510 while ((name=(TNamed*)Next()))
511 {
512 TString dir = name->GetTitle();
513 dir.ReplaceAll(old, news);
514 name->SetTitle(dir);
515 }
516}
517
518// --------------------------------------------------------------------------
519//
520// Calls ReplaceAll(old, news) for all File-entries
521//
522void MDataSet::ReplaceFile(TList &list, const TString &old, const TString &news) const
523{
524 TIter Next(&list);
525 TNamed *name = 0;
526 while ((name=(TNamed*)Next()))
527 {
528 TString file = name->GetName();
529 file.ReplaceAll(old, news);
530 name->SetName(file);
531 }
532}
Note: See TracBrowser for help on using the repository browser.