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

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