| 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.
|
|---|
| 52 | //
|
|---|
| 53 | // If you have different file names you can overwrite the default file names
|
|---|
| 54 | // using Sequence%08d.File (make sure you have 8 digits!)
|
|---|
| 55 | //
|
|---|
| 56 | // In standard coditions (datacenter file system) paths are concatenated
|
|---|
| 57 | // by using the information in the sequence files (date, etc). You can
|
|---|
| 58 | // overwrite the directories in which the sequence-files (eg I-files) are
|
|---|
| 59 | // stored using Sequence%08d.Dir (make sure you have 8 digits!)
|
|---|
| 60 | //
|
|---|
| 61 | // Resource file entries are case sensitive!
|
|---|
| 62 | //
|
|---|
| 63 | // MISSING (27/01/04): The default name and paths cannot be used yet, because
|
|---|
| 64 | // they have to be defined soon.
|
|---|
| 65 | //
|
|---|
| 66 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 67 | #include "MDataSet.h"
|
|---|
| 68 |
|
|---|
| 69 | #include <stdlib.h>
|
|---|
| 70 | #include <fstream>
|
|---|
| 71 |
|
|---|
| 72 | #include <TEnv.h>
|
|---|
| 73 | #include <TRegexp.h>
|
|---|
| 74 | #include <TSystem.h> // TSystem::ExpandPath
|
|---|
| 75 |
|
|---|
| 76 | #include "MLog.h"
|
|---|
| 77 | #include "MLogManip.h"
|
|---|
| 78 |
|
|---|
| 79 | #include "MRead.h"
|
|---|
| 80 | #include "MAstro.h"
|
|---|
| 81 | #include "MDirIter.h"
|
|---|
| 82 | #include "MSequence.h"
|
|---|
| 83 | #include "MPointingPos.h"
|
|---|
| 84 |
|
|---|
| 85 | ClassImp(MDataSet);
|
|---|
| 86 |
|
|---|
| 87 | using namespace std;
|
|---|
| 88 |
|
|---|
| 89 | // --------------------------------------------------------------------------
|
|---|
| 90 | //
|
|---|
| 91 | // Copy the run numbers from the TString runs into the TArrayI data
|
|---|
| 92 | //
|
|---|
| 93 | void MDataSet::Split(TString &runs, TArrayI &data) const
|
|---|
| 94 | {
|
|---|
| 95 | const TRegexp regexp("[0-9]+");
|
|---|
| 96 |
|
|---|
| 97 | data.Set(0);
|
|---|
| 98 | runs = runs.Strip(TString::kTrailing);
|
|---|
| 99 |
|
|---|
| 100 | while (!runs.IsNull())
|
|---|
| 101 | {
|
|---|
| 102 | TString num = runs(regexp);
|
|---|
| 103 |
|
|---|
| 104 | const Int_t n = data.GetSize();
|
|---|
| 105 | data.Set(n+1);
|
|---|
| 106 | data[n] = atoi(num.Data());
|
|---|
| 107 |
|
|---|
| 108 | runs.Remove(0, runs.First(num)+num.Length());
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | void MDataSet::ResolveSequences(TEnv &env, const TArrayI &num, TList &list) const
|
|---|
| 113 | {
|
|---|
| 114 | for (int i=0; i<num.GetSize(); i++)
|
|---|
| 115 | {
|
|---|
| 116 | TString name = env.GetValue(Form("Sequence%08d.File", num[i]), "");
|
|---|
| 117 | TString dir = env.GetValue(Form("Sequence%08d.Dir", num[i]), "");
|
|---|
| 118 |
|
|---|
| 119 | gSystem->ExpandPathName(name);
|
|---|
| 120 | gSystem->ExpandPathName(dir);
|
|---|
| 121 |
|
|---|
| 122 | if (name.IsNull())
|
|---|
| 123 | {
|
|---|
| 124 | // Replace with correct default name
|
|---|
| 125 | name = Form("/data2/wuerzburg/sequences/sequence%08d.txt", num[i]);
|
|---|
| 126 | }
|
|---|
| 127 | /*
|
|---|
| 128 | if (dir.IsNull())
|
|---|
| 129 | {
|
|---|
| 130 | // Replace with default dir
|
|---|
| 131 | }
|
|---|
| 132 | */
|
|---|
| 133 |
|
|---|
| 134 | if (gSystem->AccessPathName(name, kFileExists))
|
|---|
| 135 | gLog << warn << "WARNING - Sequence file '" << name << "' doesn't exist." << endl;
|
|---|
| 136 |
|
|---|
| 137 | if (gSystem->AccessPathName(dir, kFileExists))
|
|---|
| 138 | gLog << warn << "WARNING - Directory '" << dir << "' doesn't exist." << endl;
|
|---|
| 139 |
|
|---|
| 140 | list.Add(new TNamed(name, dir));
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | // --------------------------------------------------------------------------
|
|---|
| 145 | //
|
|---|
| 146 | // Read the file fname as setup file for the sequence.
|
|---|
| 147 | //
|
|---|
| 148 | MDataSet::MDataSet(const char *fname)
|
|---|
| 149 | {
|
|---|
| 150 | fName = fname;
|
|---|
| 151 |
|
|---|
| 152 | const char *expname = gSystem->ExpandPathName(fname);
|
|---|
| 153 |
|
|---|
| 154 | fTitle = Form("Sequences contained in file %s", expname);
|
|---|
| 155 |
|
|---|
| 156 | TEnv env(expname);
|
|---|
| 157 | delete [] expname;
|
|---|
| 158 |
|
|---|
| 159 | TString str;
|
|---|
| 160 |
|
|---|
| 161 | fNumAnalysis = env.GetValue("AnalysisNumber", -1);
|
|---|
| 162 |
|
|---|
| 163 | str = env.GetValue("SequencesOn", "");
|
|---|
| 164 | Split(str, fNumSequencesOn);
|
|---|
| 165 | str = env.GetValue("SequencesOff", "");
|
|---|
| 166 | Split(str, fNumSequencesOff);
|
|---|
| 167 |
|
|---|
| 168 | ResolveSequences(env, fNumSequencesOn, fSequencesOn);
|
|---|
| 169 | ResolveSequences(env, fNumSequencesOff, fSequencesOff);
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 | fNameSource = env.GetValue("SourceName", "");
|
|---|
| 173 | fCatalog = env.GetValue("Catalog", "~/Software/data/magic_favorites.edb");
|
|---|
| 174 | fIsWobbleMode = env.GetValue("WobbleMode", kFALSE);
|
|---|
| 175 |
|
|---|
| 176 | //Print();
|
|---|
| 177 | /*
|
|---|
| 178 | GetFileNames(env, fSequencesOn);
|
|---|
| 179 | GetFileNames(env, fSequencesOff);
|
|---|
| 180 | */
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | // --------------------------------------------------------------------------
|
|---|
| 184 | //
|
|---|
| 185 | // Return '+' if both can be accessed, '-' otherwise.
|
|---|
| 186 | //
|
|---|
| 187 | void MDataSet::PrintFile(const TObject &obj)
|
|---|
| 188 | {
|
|---|
| 189 | const Bool_t access = !gSystem->AccessPathName(obj.GetName(), kFileExists) && !gSystem->AccessPathName(obj.GetTitle(), kFileExists) ? '+' : '-';
|
|---|
| 190 | gLog << " " << (access?"+":"-") << " " << obj.GetName() << " <" << obj.GetTitle() << ">" << endl;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | // --------------------------------------------------------------------------
|
|---|
| 194 | //
|
|---|
| 195 | // Print the contents of the sequence
|
|---|
| 196 | //
|
|---|
| 197 | void MDataSet::Print(Option_t *o) const
|
|---|
| 198 | {
|
|---|
| 199 | gLog << all;
|
|---|
| 200 | if (!IsValid())
|
|---|
| 201 | {
|
|---|
| 202 | gLog << "Sequence: " << fName << " <invalid>" << endl;
|
|---|
| 203 | return;
|
|---|
| 204 | }
|
|---|
| 205 | gLog << "Analysis Number: " << fNumAnalysis << endl;
|
|---|
| 206 | gLog << "Sequences On: ";
|
|---|
| 207 | for (int i=0; i<fNumSequencesOn.GetSize(); i++)
|
|---|
| 208 | gLog << " " << fNumSequencesOn[i];
|
|---|
| 209 | gLog << endl;
|
|---|
| 210 | gLog << "Sequences Off: ";
|
|---|
| 211 | for (int i=0; i<fNumSequencesOff.GetSize(); i++)
|
|---|
| 212 | gLog << " " << fNumSequencesOff[i];
|
|---|
| 213 | gLog << endl;
|
|---|
| 214 |
|
|---|
| 215 | gLog << "SourceName: " << fNameSource << endl;
|
|---|
| 216 | gLog << "Catalog: " << fCatalog << endl;
|
|---|
| 217 |
|
|---|
| 218 | gLog << "WobbleMode: " << (fIsWobbleMode?"On":"Off") << endl;
|
|---|
| 219 |
|
|---|
| 220 | if (!TString(o).Contains("files", TString::kIgnoreCase))
|
|---|
| 221 | return;
|
|---|
| 222 |
|
|---|
| 223 | TObject *obj=0;
|
|---|
| 224 |
|
|---|
| 225 | gLog << endl;
|
|---|
| 226 | gLog << "On-Data Files:" << endl;
|
|---|
| 227 | TIter NextOn(&fSequencesOn);
|
|---|
| 228 | while ((obj=NextOn()))
|
|---|
| 229 | PrintFile(*obj);
|
|---|
| 230 |
|
|---|
| 231 | gLog << endl;
|
|---|
| 232 | gLog << "Off-Data Files:" << endl;
|
|---|
| 233 | TIter NextOff(&fSequencesOff);
|
|---|
| 234 | while ((obj=NextOff()))
|
|---|
| 235 | PrintFile(*obj);
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | Bool_t MDataSet::AddSequencesToList(const TList &list, MRead &read, char *id, Bool_t raw)
|
|---|
| 239 | {
|
|---|
| 240 | MDirIter files;
|
|---|
| 241 |
|
|---|
| 242 | TIter Next(const_cast<TList*>(&list));
|
|---|
| 243 | TObject *o=0;
|
|---|
| 244 | while ((o=Next()))
|
|---|
| 245 | {
|
|---|
| 246 | MSequence seq(o->GetName());
|
|---|
| 247 | if (!seq.IsValid())
|
|---|
| 248 | {
|
|---|
| 249 | gLog << warn << "WARNING - Sequence " << o->GetName() << " invalid!" << endl;
|
|---|
| 250 | return kFALSE;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | const TString dir(o->GetTitle());
|
|---|
| 254 | seq.SetupDatRuns(files, dir.IsNull() ? 0 : dir.Data(), id, raw);
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | if (gLog.GetDebugLevel()>4)
|
|---|
| 258 | {
|
|---|
| 259 | gLog << dbg << "Files which are searched:" << endl;
|
|---|
| 260 | files.Print();
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | return read.AddFiles(files)>0;
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | Bool_t MDataSet::AddFiles(MRead &read, char *id, Bool_t raw) const
|
|---|
| 267 | {
|
|---|
| 268 | const Bool_t rc1 = AddFilesOff(read, id, raw);
|
|---|
| 269 | const Bool_t rc2 = AddFilesOn(read, id, raw);
|
|---|
| 270 | return rc1 && rc2;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | Bool_t MDataSet::AddFilesOn(MRead &read, char *id, Bool_t raw) const
|
|---|
| 274 | {
|
|---|
| 275 | return AddSequencesToList(fSequencesOn, read, id, raw);
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | Bool_t MDataSet::AddFilesOff(MRead &read, char *id, Bool_t raw) const
|
|---|
| 279 | {
|
|---|
| 280 | return AddSequencesToList(fSequencesOff, read, id, raw);
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | Bool_t MDataSet::GetSourcePos(MPointingPos &pos) const
|
|---|
| 284 | {
|
|---|
| 285 | if (!HasSource())
|
|---|
| 286 | return kFALSE;
|
|---|
| 287 |
|
|---|
| 288 | TString catalog(fCatalog);
|
|---|
| 289 | gSystem->ExpandPathName(catalog);
|
|---|
| 290 |
|
|---|
| 291 | ifstream fin(catalog);
|
|---|
| 292 | if (!fin)
|
|---|
| 293 | {
|
|---|
| 294 | gLog << err << "Cannot open file " << catalog << ": ";
|
|---|
| 295 | gLog << strerror(errno) << endl;
|
|---|
| 296 | return kFALSE;
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | TString ra,dec,epoch;
|
|---|
| 300 |
|
|---|
| 301 | Int_t n = 0;
|
|---|
| 302 | while (1)
|
|---|
| 303 | {
|
|---|
| 304 | TString line;
|
|---|
| 305 | line.ReadLine(fin);
|
|---|
| 306 | if (!fin)
|
|---|
| 307 | break;
|
|---|
| 308 |
|
|---|
| 309 | n++;
|
|---|
| 310 | line = line.Strip(TString::kBoth);
|
|---|
| 311 |
|
|---|
| 312 | if (!line.BeginsWith(fNameSource))
|
|---|
| 313 | continue;
|
|---|
| 314 |
|
|---|
| 315 | // CrabNebula,f|L|K0,5:34:32.0,22:0:52,-1.0,2000
|
|---|
| 316 |
|
|---|
| 317 | for (int i=0; i<6; i++)
|
|---|
| 318 | {
|
|---|
| 319 | const Ssiz_t p = line.First(',');
|
|---|
| 320 | if (p<0 && i<5)
|
|---|
| 321 | {
|
|---|
| 322 | gLog << err << "Not enough arguments in line #" << n << endl;
|
|---|
| 323 | return kFALSE;
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | switch (i)
|
|---|
| 327 | {
|
|---|
| 328 | case 0:
|
|---|
| 329 | case 1:
|
|---|
| 330 | case 4:
|
|---|
| 331 | break;
|
|---|
| 332 | case 2:
|
|---|
| 333 | ra = line(0, p);
|
|---|
| 334 | break;
|
|---|
| 335 | case 3:
|
|---|
| 336 | dec = line(0, p);
|
|---|
| 337 | break;
|
|---|
| 338 | case 5:
|
|---|
| 339 | epoch = line;
|
|---|
| 340 | break;
|
|---|
| 341 | }
|
|---|
| 342 | line.Remove(0, p+1);
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | if (line.First(',')>=0)
|
|---|
| 346 | {
|
|---|
| 347 | gLog << err << "Too much arguments in line #" << n << endl;
|
|---|
| 348 | return kFALSE;
|
|---|
| 349 | }
|
|---|
| 350 | break;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | if (epoch!=(TString)"2000")
|
|---|
| 354 | {
|
|---|
| 355 | gLog << err << "Epoch not 2000... not supported." << endl;
|
|---|
| 356 | return kFALSE;
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | Double_t r,d;
|
|---|
| 360 | if (!MAstro::Coordinate2Angle(ra, r))
|
|---|
| 361 | {
|
|---|
| 362 | gLog << err << "ERROR - Interpreting right ascension: " << ra << endl;
|
|---|
| 363 | return kFALSE;
|
|---|
| 364 | }
|
|---|
| 365 | if (!MAstro::Coordinate2Angle(dec, d))
|
|---|
| 366 | {
|
|---|
| 367 | gLog << err << "ERROR - Interpreting declination: " << dec << endl;
|
|---|
| 368 | return kFALSE;
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | pos.SetSkyPosition(r, d);
|
|---|
| 372 | pos.SetTitle(fNameSource);
|
|---|
| 373 |
|
|---|
| 374 | return kTRUE;
|
|---|
| 375 | }
|
|---|