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

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