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