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

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