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

Last change on this file since 7455 was 7451, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 14.2 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 << "WARNING - Sequence " << o->GetName() << " invalid!" << endl;
309 return kFALSE;
310 }
311
312 const TString dir(o->GetTitle());
313 seq.SetupDatRuns(files, MSequence::kImages, dir.IsNull() ? 0 : dir.Data());
314 }
315
316 // This is important in case of synchronisation, because the
317 // files in the sequences can be interleaved (eg W1, W2)
318 // Filenames MUST begin with an appropriate string which allow
319 // to order them correctly in time!
320 // files.Sort();
321
322 if (gLog.GetDebugLevel()>4)
323 {
324 gLog << dbg << "Files which are searched:" << endl;
325 files.Print();
326 }
327 return kTRUE;
328}
329
330Bool_t MDataSet::AddFilesOn(MRead &read) const
331{
332 MDirIter files;
333 if (!AddSequencesFromList(fSequencesOn, files))
334 return kFALSE;
335 return read.AddFiles(files)>0;
336}
337
338Bool_t MDataSet::AddFilesOff(MRead &read) const
339{
340 MDirIter files;
341 if (!AddSequencesFromList(fSequencesOff, files))
342 return kFALSE;
343 return read.AddFiles(files)>0;
344}
345
346Bool_t MDataSet::AddFiles(MRead &read) const
347{
348 const Bool_t rc1 = AddFilesOff(read);
349 const Bool_t rc2 = AddFilesOn(read);
350 return rc1 && rc2;
351}
352
353Int_t MDataSet::AddFilesToChain(MDirIter &files, TChain &chain)
354{
355 Int_t num=0;
356 while (1)
357 {
358 const TString fname = files.Next();
359 if (fname.IsNull())
360 break;
361
362 const Int_t n = chain.Add(fname);
363 if (n<=0)
364 return kFALSE;
365 num += n;
366 }
367 return num;
368}
369
370Bool_t MDataSet::AddFilesOn(TChain &chain) const
371{
372 MDirIter files;
373 if (!AddSequencesFromList(fSequencesOn, files))
374 return kFALSE;
375 return AddFilesToChain(files, chain)>0;
376}
377
378Bool_t MDataSet::AddFilesOff(TChain &chain) const
379{
380 MDirIter files;
381 if (!AddSequencesFromList(fSequencesOff, files))
382 return kFALSE;
383 return AddFilesToChain(files, chain)>0;
384}
385
386Bool_t MDataSet::AddFiles(TChain &read) const
387{
388 const Bool_t rc1 = AddFilesOff(read);
389 const Bool_t rc2 = AddFilesOn(read);
390 return rc1 && rc2;
391}
392
393Bool_t MDataSet::GetSourcePos(MPointingPos &pos) const
394{
395 if (!HasSource())
396 {
397 gLog << err << "ERROR - MDataSet::GetSourcePos called, but no source available." << endl;
398 return kFALSE;
399 }
400
401 TString catalog(fCatalog);
402 gSystem->ExpandPathName(catalog);
403
404 ifstream fin(catalog);
405 if (!fin)
406 {
407 gLog << err << "Cannot open file " << catalog << ": ";
408 gLog << strerror(errno) << endl;
409 return kFALSE;
410 }
411
412 TString ra, dec, epoch;
413
414 Int_t n = 0;
415 while (1)
416 {
417 TString line;
418 line.ReadLine(fin);
419 if (!fin)
420 break;
421
422 n++;
423
424 TObjArray *arr = line.Tokenize(",");
425
426 if (arr->GetEntries()<6)
427 {
428 gLog << err << "ERROR - Not enough arguments in line #" << n << " of " << catalog << endl;
429 delete arr;
430 return kFALSE;;
431 }
432
433 const TString name = (*arr)[0]->GetName();
434
435 ra = (*arr)[2]->GetName();
436 dec = (*arr)[3]->GetName();
437 epoch = (*arr)[5]->GetName();
438
439 delete arr;
440
441 if (name.Strip(TString::kBoth)==fNameSource)
442 break;
443 }
444
445 if (epoch.Strip(TString::kBoth)!=(TString)"2000")
446 {
447 gLog << err << "ERROR - Epoch not 2000... not supported." << endl;
448 return kFALSE;
449 }
450
451 Double_t r,d;
452 if (!MAstro::Coordinate2Angle(ra, r))
453 {
454 gLog << err << "ERROR - Interpreting right ascension: " << ra << endl;
455 return kFALSE;
456 }
457 if (!MAstro::Coordinate2Angle(dec, d))
458 {
459 gLog << err << "ERROR - Interpreting declination: " << dec << endl;
460 return kFALSE;
461 }
462
463 pos.SetSkyPosition(r, d);
464 pos.SetTitle(fNameSource);
465
466 return kTRUE;
467}
468
469// --------------------------------------------------------------------------
470//
471// Calls ReplaceAll(old, news) for all Dir-entries
472//
473void MDataSet::ReplaceDir(TList &list, const TString &old, const TString &news) const
474{
475 TIter Next(&list);
476 TNamed *name = 0;
477 while ((name=(TNamed*)Next()))
478 {
479 TString dir = name->GetTitle();
480 dir.ReplaceAll(old, news);
481 name->SetTitle(dir);
482 }
483}
484
485// --------------------------------------------------------------------------
486//
487// Calls ReplaceAll(old, news) for all File-entries
488//
489void MDataSet::ReplaceFile(TList &list, const TString &old, const TString &news) const
490{
491 TIter Next(&list);
492 TNamed *name = 0;
493 while ((name=(TNamed*)Next()))
494 {
495 TString file = name->GetName();
496 file.ReplaceAll(old, news);
497 name->SetName(file);
498 }
499}
Note: See TracBrowser for help on using the repository browser.