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

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