| 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, 7/2008 <mailto:tbretz@astro.uni-wuerzburg.de> | 
|---|
| 19 | ! | 
|---|
| 20 | !   Copyright: MAGIC Software Development, 2000-2008 | 
|---|
| 21 | ! | 
|---|
| 22 | ! | 
|---|
| 23 | \* ======================================================================== */ | 
|---|
| 24 |  | 
|---|
| 25 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 26 | // | 
|---|
| 27 | //  MJMerpp | 
|---|
| 28 | // | 
|---|
| 29 | // Merging and preprocessing | 
|---|
| 30 | // | 
|---|
| 31 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 32 | #include "MJMerpp.h" | 
|---|
| 33 |  | 
|---|
| 34 | #include <TFile.h> | 
|---|
| 35 | #include <TTree.h> | 
|---|
| 36 |  | 
|---|
| 37 | #include "MLogManip.h" | 
|---|
| 38 |  | 
|---|
| 39 | #include "MParList.h" | 
|---|
| 40 | #include "MTaskList.h" | 
|---|
| 41 | #include "MEvtLoop.h" | 
|---|
| 42 |  | 
|---|
| 43 | #include "MFDataPhrase.h" | 
|---|
| 44 |  | 
|---|
| 45 | #include "MRawRunHeader.h" | 
|---|
| 46 |  | 
|---|
| 47 | #include "MRawFileRead.h" | 
|---|
| 48 | #include "MRawFitsRead.h" | 
|---|
| 49 | #include "MReportFileReadCC.h" | 
|---|
| 50 | #include "MWriteRootFile.h" | 
|---|
| 51 |  | 
|---|
| 52 | ClassImp(MJMerpp); | 
|---|
| 53 |  | 
|---|
| 54 | using namespace std; | 
|---|
| 55 |  | 
|---|
| 56 | MJMerpp::MJMerpp(const char *name, const char *title) | 
|---|
| 57 | { | 
|---|
| 58 | fName  = name  ? name  : "MJMerpp"; | 
|---|
| 59 | fTitle = title ? title : "Standard program for merging subsystem data"; | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | void MJMerpp::AddTree(MReportFileReadCC &read, const TString &rep, const TString &only) const | 
|---|
| 63 | { | 
|---|
| 64 | if (!only.IsNull() && rep!=only) | 
|---|
| 65 | return; | 
|---|
| 66 |  | 
|---|
| 67 | read.AddToList(Form("MReport%s", rep.Data())); | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | void MJMerpp::SetupCurrents(MReportFileReadCC &read, MWriteRootFile *write) const | 
|---|
| 71 | { | 
|---|
| 72 | read.SetHasNoHeader(); | 
|---|
| 73 | read.AddToList("MReportCurrents"); | 
|---|
| 74 |  | 
|---|
| 75 | if (!write) | 
|---|
| 76 | return; | 
|---|
| 77 |  | 
|---|
| 78 | write->AddContainer("MTimeCurrents",      "Currents"); | 
|---|
| 79 | write->AddContainer("MCameraDC",          "Currents"); | 
|---|
| 80 | write->AddContainer("MReportCurrents",    "Currents"); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | MFilter *MJMerpp::SetupReports(MReportFileReadCC &read, MWriteRootFile *write) const | 
|---|
| 84 | { | 
|---|
| 85 | read.SetTelescope(fTelescope); | 
|---|
| 86 |  | 
|---|
| 87 | read.SetRunNumber(fHeaderRun); | 
|---|
| 88 | read.SetFileNumber(fHeaderFile); | 
|---|
| 89 |  | 
|---|
| 90 | AddTree(read, "CC",         fOnly); | 
|---|
| 91 | AddTree(read, "Rec",        fOnly); | 
|---|
| 92 | AddTree(read, "Drive",      fOnly); | 
|---|
| 93 | AddTree(read, "Camera",     fOnly); | 
|---|
| 94 | AddTree(read, "Trigger",    fOnly); | 
|---|
| 95 | AddTree(read, "Starguider", fOnly); | 
|---|
| 96 | AddTree(read, "Pyrometer",  fOnly); | 
|---|
| 97 | // AddTree(read, "DAQ",  fOnly); | 
|---|
| 98 |  | 
|---|
| 99 | if (fReportRun!=(UInt_t)-1) | 
|---|
| 100 | read.AddToList("MReportRun"); | 
|---|
| 101 |  | 
|---|
| 102 | if (!write) | 
|---|
| 103 | return 0; | 
|---|
| 104 |  | 
|---|
| 105 | const Bool_t required = fOnly.IsNull(); | 
|---|
| 106 | write->AddContainer("MReportCamera",      "Camera",     required); | 
|---|
| 107 | write->AddContainer("MTimeCamera",        "Camera",     required); | 
|---|
| 108 | write->AddContainer("MCameraAUX",         "Camera",     required); | 
|---|
| 109 | write->AddContainer("MCameraCalibration", "Camera",     required); | 
|---|
| 110 | write->AddContainer("MCameraCooling",     "Camera",     required); | 
|---|
| 111 | write->AddContainer("MCameraActiveLoad",  "Camera",     required); | 
|---|
| 112 | write->AddContainer("MCameraHV",          "Camera",     required); | 
|---|
| 113 | write->AddContainer("MCameraDC",          "Camera",     required); | 
|---|
| 114 | write->AddContainer("MCameraLV",          "Camera",     required); | 
|---|
| 115 | write->AddContainer("MCameraLids",        "Camera",     required); | 
|---|
| 116 | write->AddContainer("MReportTrigger",     "Trigger",    required); | 
|---|
| 117 | write->AddContainer("MTimeTrigger",       "Trigger",    required); | 
|---|
| 118 | write->AddContainer("MTriggerBit",        "Trigger",    required); | 
|---|
| 119 | write->AddContainer("MTriggerIPR",        "Trigger",    required); | 
|---|
| 120 | write->AddContainer("MTriggerCell",       "Trigger",    required); | 
|---|
| 121 | write->AddContainer("MTriggerPrescFact",  "Trigger",    required); | 
|---|
| 122 | write->AddContainer("MTriggerLiveTime",   "Trigger",    required); | 
|---|
| 123 | write->AddContainer("MReportDrive",       "Drive",      required); | 
|---|
| 124 | write->AddContainer("MTimeDrive",         "Drive",      required); | 
|---|
| 125 | write->AddContainer("MCameraTH",          "Rec",        required); | 
|---|
| 126 | write->AddContainer("MCameraTD",          "Rec",        required); | 
|---|
| 127 | write->AddContainer("MCameraRecTemp",     "Rec",        required); | 
|---|
| 128 | write->AddContainer("MReportRec",         "Rec",        required); | 
|---|
| 129 | write->AddContainer("MTimeRec",           "Rec",        required); | 
|---|
| 130 | write->AddContainer("MReportCC",          "CC",         required); | 
|---|
| 131 | write->AddContainer("MTimeCC",            "CC",         required); | 
|---|
| 132 | write->AddContainer("MReportStarguider",  "Starguider", required); | 
|---|
| 133 | write->AddContainer("MTimeStarguider",    "Starguider", required); | 
|---|
| 134 | write->AddContainer("MReportPyrometer",   "Pyrometer",  required); | 
|---|
| 135 | write->AddContainer("MTimePyrometer",     "Pyrometer",  required); | 
|---|
| 136 | // write->AddContainer("MReportDAQ",         "DAQ"); | 
|---|
| 137 | // write->AddContainer("MTimeDAQ",           "DAQ"); | 
|---|
| 138 |  | 
|---|
| 139 | if (fReportRun==(UInt_t)-1) | 
|---|
| 140 | return 0; | 
|---|
| 141 |  | 
|---|
| 142 | const TString f1 = fReportRun>0   ? Form("MReportRun.fRunNumber==%ud",  fReportRun)  : ""; | 
|---|
| 143 | const TString f2 = fReportFile>=0 ? Form("MReportRun.fFileNumber==%ud", fReportFile) : ""; | 
|---|
| 144 |  | 
|---|
| 145 | const TString f = Form(fReportRun>0 && fReportFile>=0 ? "%s && %s" : "%s%s", | 
|---|
| 146 | f1.Data(), f2.Data()); | 
|---|
| 147 |  | 
|---|
| 148 | MFilter *filter = new MFDataPhrase(f.Data()); | 
|---|
| 149 | write->SetFilter(filter); | 
|---|
| 150 | return filter; | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | void MJMerpp::SetupRaw(MRawFileRead &read, MWriteRootFile *write) const | 
|---|
| 154 | { | 
|---|
| 155 | //    read.SetInterleave(fInterleave); | 
|---|
| 156 | //    read.SetForceMode(fForceProcessing); | 
|---|
| 157 |  | 
|---|
| 158 | if (!write) | 
|---|
| 159 | return; | 
|---|
| 160 |  | 
|---|
| 161 | write->AddContainer("MRawRunHeader",  "RunHeaders"); | 
|---|
| 162 | write->AddContainer("MTime",          "Events"); | 
|---|
| 163 | write->AddContainer("MRawEvtHeader",  "Events"); | 
|---|
| 164 | write->AddContainer("MRawEvtData",    "Events"); | 
|---|
| 165 | write->AddContainer("MRawEvtData2",   "Events"); | 
|---|
| 166 | write->AddContainer("MRawCrateArray", "Events"); | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | Bool_t MJMerpp::GetTimeFromFile() | 
|---|
| 170 | { | 
|---|
| 171 | if (!fAutoStartTime && !fAutoStopTime) | 
|---|
| 172 | return kTRUE; | 
|---|
| 173 |  | 
|---|
| 174 | TFile f(fPathOut, "READ"); | 
|---|
| 175 | if (f.IsZombie()) | 
|---|
| 176 | { | 
|---|
| 177 | *fLog << err << "ERROR - File " << fPathOut << " could not be opened." << endl; | 
|---|
| 178 | return kFALSE; | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | TTree *t = (TTree*)f.Get("RunHeaders"); | 
|---|
| 182 | if (t->GetEntries()!=1) | 
|---|
| 183 | { | 
|---|
| 184 | *fLog << err << "ERROR - File " << fPathOut << " contains no or more than one entry in RunHeaders... Times unchanged." << endl; | 
|---|
| 185 | return kFALSE; | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | MRawRunHeader *h = 0; | 
|---|
| 189 | t->SetBranchAddress("MRawRunHeader.", &h); | 
|---|
| 190 | t->GetEntry(0); | 
|---|
| 191 | if (!h) | 
|---|
| 192 | { | 
|---|
| 193 | *fLog << err << "ERROR - File " << fPathOut << " did not contain RunHeaders.MRawRunHeader... Times unchanged." << endl; | 
|---|
| 194 | return kFALSE; | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | if (fAutoStartTime) | 
|---|
| 198 | fTimeStart = h->GetRunStart(); | 
|---|
| 199 | if (fAutoStopTime) | 
|---|
| 200 | fTimeStop = h->GetRunEnd(); | 
|---|
| 201 |  | 
|---|
| 202 | return kTRUE; | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | Bool_t MJMerpp::CheckFilePermissions() | 
|---|
| 206 | { | 
|---|
| 207 | // | 
|---|
| 208 | // check whether the given files are OK. | 
|---|
| 209 | // | 
|---|
| 210 | if (gSystem->AccessPathName(fPathIn, kFileExists)) | 
|---|
| 211 | { | 
|---|
| 212 | *fLog << err << "ERROR - Input file '" << fPathIn << "' not accessible." << endl; | 
|---|
| 213 | return kFALSE; | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | if (HasNullOut()) | 
|---|
| 217 | return kTRUE; | 
|---|
| 218 |  | 
|---|
| 219 | const Bool_t fileexist = !gSystem->AccessPathName(fPathOut, kFileExists); | 
|---|
| 220 | const Bool_t writeperm = !gSystem->AccessPathName(fPathOut, kWritePermission); | 
|---|
| 221 |  | 
|---|
| 222 | if (fileexist && !writeperm) | 
|---|
| 223 | { | 
|---|
| 224 | *fLog << err << "ERROR - No write permission for '" << fPathOut << "'." << endl; | 
|---|
| 225 | return kFALSE; | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 | if (fileexist && !fUpdate && !fOverwrite) | 
|---|
| 229 | { | 
|---|
| 230 | *fLog << err << "ERROR - File '" << fPathOut << "' already existing." << endl; | 
|---|
| 231 | return kFALSE; | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | if (!fileexist && fUpdate) | 
|---|
| 235 | { | 
|---|
| 236 | *fLog << err << "ERROR - File '" << fPathOut << "' doesn't yet exist... no update possible." << endl; | 
|---|
| 237 | return kFALSE; | 
|---|
| 238 | } | 
|---|
| 239 |  | 
|---|
| 240 | return kTRUE; | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | Int_t MJMerpp::Process() | 
|---|
| 244 | { | 
|---|
| 245 | *fLog << inf; | 
|---|
| 246 | fLog->Separator(GetDescriptor()); | 
|---|
| 247 | *fLog << "In:  " << fPathIn << endl; | 
|---|
| 248 | if (!HasNullOut()) | 
|---|
| 249 | *fLog << (fUpdate?"Upd":"Out") << ": " << fPathOut << endl;; | 
|---|
| 250 | *fLog << endl; | 
|---|
| 251 |  | 
|---|
| 252 | if (!CheckFilePermissions()) | 
|---|
| 253 | return 1; | 
|---|
| 254 |  | 
|---|
| 255 | // FIXME: Add a check whether the file has already been merpped!!! | 
|---|
| 256 |  | 
|---|
| 257 | const Bool_t isreport = fPathIn.EndsWith(".rep"); | 
|---|
| 258 | const Bool_t isdc     = fPathIn.EndsWith(".txt"); | 
|---|
| 259 |  | 
|---|
| 260 | // | 
|---|
| 261 | // Evaluate possible start-/stop-time | 
|---|
| 262 | // | 
|---|
| 263 | if (fUpdate && (isreport || isdc)) | 
|---|
| 264 | if (!GetTimeFromFile()) | 
|---|
| 265 | return 2; | 
|---|
| 266 |  | 
|---|
| 267 | if (fTimeStart) | 
|---|
| 268 | *fLog << inf << "Start Time: " << fTimeStart << endl; | 
|---|
| 269 | if (fTimeStop) | 
|---|
| 270 | *fLog << inf << "Stop  Time: " << fTimeStop << endl; | 
|---|
| 271 |  | 
|---|
| 272 | // | 
|---|
| 273 | // create a (empty) list of parameters which can be used by the tasks | 
|---|
| 274 | // and an (empty) list of tasks which should be executed | 
|---|
| 275 | // | 
|---|
| 276 | MParList plist; | 
|---|
| 277 |  | 
|---|
| 278 | MTaskList tasks; | 
|---|
| 279 | tasks.SetOwner(); | 
|---|
| 280 | plist.AddToList(&tasks); | 
|---|
| 281 |  | 
|---|
| 282 | // | 
|---|
| 283 | // create the tasks which should be executed and add them to the list | 
|---|
| 284 | // in the case you don't need parameter containers, all of them can | 
|---|
| 285 | // be created by MRawFileRead::PreProcess | 
|---|
| 286 | // | 
|---|
| 287 | const TString option(fUpdate?"UPDATE":(fOverwrite?"RECREATE":"NEW")); | 
|---|
| 288 |  | 
|---|
| 289 | MTask          *read   = 0; | 
|---|
| 290 | MFilter        *filter = 0; | 
|---|
| 291 | MWriteRootFile *write  = HasNullOut() ? 0 : new MWriteRootFile(fPathOut, option, "Magic root-file", fCompression); | 
|---|
| 292 |  | 
|---|
| 293 | if (isreport || isdc) | 
|---|
| 294 | { | 
|---|
| 295 | MReportFileReadCC *r = new MReportFileReadCC(fPathIn); | 
|---|
| 296 | r->SetTimeStart(fTimeStart); | 
|---|
| 297 | r->SetTimeStop(fTimeStop); | 
|---|
| 298 |  | 
|---|
| 299 | if (isdc) | 
|---|
| 300 | SetupCurrents(*r, write); | 
|---|
| 301 | else | 
|---|
| 302 | filter = SetupReports(*r, write); | 
|---|
| 303 |  | 
|---|
| 304 | read = r; | 
|---|
| 305 | } | 
|---|
| 306 | else | 
|---|
| 307 | { | 
|---|
| 308 | MRawFileRead *r = MRawFitsRead::IsFits(fPathIn) ? new MRawFitsRead(fPathIn) : new MRawFileRead(fPathIn); | 
|---|
| 309 | SetupRaw(*r, write); | 
|---|
| 310 | read = r; | 
|---|
| 311 | } | 
|---|
| 312 |  | 
|---|
| 313 | tasks.AddToList(read); | 
|---|
| 314 | if (filter) | 
|---|
| 315 | tasks.AddToList(filter); | 
|---|
| 316 |  | 
|---|
| 317 | if (write) | 
|---|
| 318 | tasks.AddToList(write); | 
|---|
| 319 |  | 
|---|
| 320 | // Create and setup the eventloop | 
|---|
| 321 | MEvtLoop evtloop(fName); | 
|---|
| 322 | evtloop.SetParList(&plist); | 
|---|
| 323 | evtloop.SetLogStream(fLog); | 
|---|
| 324 | //evtloop.SetDisplay(fDisplay); | 
|---|
| 325 | //if (!SetupEnv(evtloop)) | 
|---|
| 326 | //    return kFALSE; | 
|---|
| 327 |  | 
|---|
| 328 | // Execute first analysis | 
|---|
| 329 | if (!evtloop.Eventloop(fMaxEvents)) | 
|---|
| 330 | { | 
|---|
| 331 | *fLog << err << GetDescriptor() << ": Failed." << endl; | 
|---|
| 332 | return 3; | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | *fLog << all << GetDescriptor() << ": Done." << endl << endl << endl;; | 
|---|
| 336 |  | 
|---|
| 337 | return 0; | 
|---|
| 338 | } | 
|---|
| 339 |  | 
|---|
| 340 | Int_t MJMerpp::ProcessSeq(TString fname) | 
|---|
| 341 | { | 
|---|
| 342 | if (!MSequence::InflateSeq(fname)) | 
|---|
| 343 | return 4; | 
|---|
| 344 |  | 
|---|
| 345 | MSequence seq(fname); | 
|---|
| 346 | if (!seq.IsValid()) | 
|---|
| 347 | return 5; | 
|---|
| 348 |  | 
|---|
| 349 | const UInt_t num = seq.GetNumEntries(MSequence::kDat); | 
|---|
| 350 |  | 
|---|
| 351 | const TString reppath = fPathIn; | 
|---|
| 352 | const TString calpath = fPathOut; | 
|---|
| 353 |  | 
|---|
| 354 | // PreCheck: We don't wantto start merpp if we | 
|---|
| 355 | // know that an error will occur | 
|---|
| 356 | for (UInt_t i=0; i<num; i++) | 
|---|
| 357 | { | 
|---|
| 358 | const TString name1 = fHeaderRun==0 ? reppath : seq.GetFileName(i, MSequence::kReportDat, reppath); | 
|---|
| 359 | const TString name2 = seq.GetFileName(i, MSequence::kCalibrated, calpath); | 
|---|
| 360 |  | 
|---|
| 361 | // Full qualified name could not be determined or file is not | 
|---|
| 362 | // accessible. For excluded files "0" is returned. | 
|---|
| 363 | if (name1.IsNull() || name2.IsNull()) | 
|---|
| 364 | return 6; | 
|---|
| 365 | } | 
|---|
| 366 |  | 
|---|
| 367 | // Now we can safely start processing | 
|---|
| 368 | TString rc; | 
|---|
| 369 | for (UInt_t i=0; i<num; i++) | 
|---|
| 370 | { | 
|---|
| 371 | UInt_t run, file; | 
|---|
| 372 |  | 
|---|
| 373 | // Excluded | 
|---|
| 374 | if (seq.GetFile(i, MSequence::kDat, run, file)==0) | 
|---|
| 375 | continue; | 
|---|
| 376 |  | 
|---|
| 377 | // fHeaderRun==0 means: All day summary file | 
|---|
| 378 | const TString name1 = fHeaderRun==0 ? reppath : seq.GetFileName(i, MSequence::kReportDat, reppath); | 
|---|
| 379 | const TString name2 = seq.GetFileName(i, MSequence::kCalibrated, calpath); | 
|---|
| 380 |  | 
|---|
| 381 | // FIXME: check runcallisto | 
|---|
| 382 | if (fHeaderRun==0)                 // extract valid range from summary file | 
|---|
| 383 | SetConstrainRunRep(run, file); // (this is not guranteed to work :-( ) | 
|---|
| 384 | else                               // check for the correct header | 
|---|
| 385 | SetConstrainHeader(seq.GetTelescope(), run, file); | 
|---|
| 386 |  | 
|---|
| 387 | SetTime(MTime(), MTime()); // Raise error if set? | 
|---|
| 388 | SetPathIn(name1); | 
|---|
| 389 | SetPathOut(name2); | 
|---|
| 390 |  | 
|---|
| 391 | const Int_t rc = Process(); | 
|---|
| 392 | if (rc==0) | 
|---|
| 393 | continue; | 
|---|
| 394 |  | 
|---|
| 395 | return rc; | 
|---|
| 396 | } | 
|---|
| 397 | return 0; | 
|---|
| 398 | } | 
|---|