| 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, 8/2004 <mailto:tbretz@astro.uni-wuerzburg.de> | 
|---|
| 19 | ! | 
|---|
| 20 | !   Copyright: MAGIC Software Development, 2000-2004 | 
|---|
| 21 | ! | 
|---|
| 22 | ! | 
|---|
| 23 | \* ======================================================================== */ | 
|---|
| 24 |  | 
|---|
| 25 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 26 | // | 
|---|
| 27 | //  MJob | 
|---|
| 28 | // | 
|---|
| 29 | // A base class for jobs | 
|---|
| 30 | // | 
|---|
| 31 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 32 | #include "MJob.h" | 
|---|
| 33 |  | 
|---|
| 34 | #include <TEnv.h> | 
|---|
| 35 | #include <TFile.h> | 
|---|
| 36 | #include <TSystem.h> | 
|---|
| 37 |  | 
|---|
| 38 | #include "MIter.h" | 
|---|
| 39 |  | 
|---|
| 40 | #include "MLog.h" | 
|---|
| 41 | #include "MLogManip.h" | 
|---|
| 42 |  | 
|---|
| 43 | #include "MEvtLoop.h" | 
|---|
| 44 |  | 
|---|
| 45 | ClassImp(MJob); | 
|---|
| 46 |  | 
|---|
| 47 | using namespace std; | 
|---|
| 48 |  | 
|---|
| 49 | // -------------------------------------------------------------------------- | 
|---|
| 50 | // | 
|---|
| 51 | // Default constructor. | 
|---|
| 52 | // | 
|---|
| 53 | // Sets fRuns to 0, fExtractor to NULL, fDataCheck to kFALSE | 
|---|
| 54 | // | 
|---|
| 55 | MJob::MJob(const char *name, const char *title) : fEnv(0), fOverwrite(kFALSE), fMaxEvents(0) | 
|---|
| 56 | { | 
|---|
| 57 | fName  = name  ? name  : "MJob"; | 
|---|
| 58 | fTitle = title ? title : "Base class for jobs"; | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | MJob::~MJob() | 
|---|
| 62 | { | 
|---|
| 63 | if (fEnv) | 
|---|
| 64 | delete fEnv; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | Bool_t MJob::SetEnv(const char *env, const char *prefix) | 
|---|
| 68 | { | 
|---|
| 69 | if (fEnv) | 
|---|
| 70 | { | 
|---|
| 71 | delete fEnv; | 
|---|
| 72 | fEnv = 0; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | const Bool_t fileexist = !gSystem->AccessPathName(env, kFileExists); | 
|---|
| 76 | if (!fileexist) | 
|---|
| 77 | { | 
|---|
| 78 | *fLog << err << "ERROR - Resource file '" << env << "' not found... no resources applied." << endl; | 
|---|
| 79 | return kFALSE; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | fEnv = new TEnv(env); | 
|---|
| 83 |  | 
|---|
| 84 | fEnvPrefix = prefix; | 
|---|
| 85 | if (!prefix) | 
|---|
| 86 | fEnvPrefix = fName.First(' ')>0 ? fName(0, fName.First(' ')) : fName; | 
|---|
| 87 |  | 
|---|
| 88 | if (fEnvPrefix.EndsWith(".")) | 
|---|
| 89 | fEnvPrefix.Remove(fEnvPrefix.Length()-1); | 
|---|
| 90 |  | 
|---|
| 91 | return kTRUE; | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | void MJob::FixPath(TString &path) const | 
|---|
| 95 | { | 
|---|
| 96 | path.ReplaceAll("\015", ""); | 
|---|
| 97 |  | 
|---|
| 98 | if (path==(TString)"/") | 
|---|
| 99 | return; | 
|---|
| 100 |  | 
|---|
| 101 | if (path.EndsWith("/")) | 
|---|
| 102 | path.Remove(path.Length()-1); | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | void MJob::SetPathOut(const char *path) | 
|---|
| 106 | { | 
|---|
| 107 | fPathOut = path; | 
|---|
| 108 | FixPath(fPathOut); | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | void MJob::SetPathIn(const char *path) | 
|---|
| 112 | { | 
|---|
| 113 | fPathIn = path; | 
|---|
| 114 | FixPath(fPathIn); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | void MJob::SetPathData(const char *path) | 
|---|
| 118 | { | 
|---|
| 119 | fPathData = path; | 
|---|
| 120 | FixPath(fPathData); | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | Int_t MJob::GetEnv(const char *name, Int_t dflt) const | 
|---|
| 124 | { | 
|---|
| 125 | return GetEnvValue(*fEnv, fEnvPrefix, name, dflt); //    return fEnv->GetValue(Form("%s%s", fEnvPrefix.Data(), name), dflt); | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | Double_t MJob::GetEnv(const char *name, Double_t dflt) const | 
|---|
| 129 | { | 
|---|
| 130 | return GetEnvValue(*fEnv, fEnvPrefix, name, dflt); //    return fEnv->GetValue(Form("%s%s", fEnvPrefix.Data(), name), dflt); | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | const char *MJob::GetEnv(const char *name, const char *dflt) const | 
|---|
| 134 | { | 
|---|
| 135 | return GetEnvValue(*fEnv, fEnvPrefix, name, dflt); //fEnv->GetValue(Form("%s%s", fEnvPrefix.Data(), name), dflt); | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | Bool_t MJob::HasEnv(const char *name) const | 
|---|
| 139 | { | 
|---|
| 140 | return IsEnvDefined(*fEnv, fEnvPrefix, name, fEnvDebug);//fEnv->Lookup(Form("%s%s", fEnvPrefix.Data(), name)); | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | Bool_t MJob::CheckEnv() | 
|---|
| 144 | { | 
|---|
| 145 | if (!fEnv) | 
|---|
| 146 | return kTRUE; | 
|---|
| 147 |  | 
|---|
| 148 | TString p; | 
|---|
| 149 | p = GetEnv("PathOut", ""); | 
|---|
| 150 | if (!p.IsNull()) | 
|---|
| 151 | SetPathOut(p); | 
|---|
| 152 |  | 
|---|
| 153 | p = GetEnv("PathIn", ""); | 
|---|
| 154 | if (!p.IsNull()) | 
|---|
| 155 | SetPathIn(p); | 
|---|
| 156 |  | 
|---|
| 157 | p = GetEnv("PathData", ""); | 
|---|
| 158 | if (!p.IsNull()) | 
|---|
| 159 | SetPathData(p); | 
|---|
| 160 |  | 
|---|
| 161 | SetMaxEvents(GetEnv("MaxEvents", fMaxEvents)); | 
|---|
| 162 | SetOverwrite(GetEnv("Overwrite", fOverwrite)); | 
|---|
| 163 |  | 
|---|
| 164 | return CheckEnvLocal(); | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | Bool_t MJob::SetupEnv(MEvtLoop &loop) const | 
|---|
| 168 | { | 
|---|
| 169 | if (!fEnv) | 
|---|
| 170 | return kTRUE; | 
|---|
| 171 |  | 
|---|
| 172 | return loop.ReadEnv(*fEnv, fEnvPrefix, fEnvDebug) ? kTRUE : kFALSE; | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 | Bool_t MJob::WriteContainer(TCollection &list) const | 
|---|
| 176 | { | 
|---|
| 177 | if (!gFile) | 
|---|
| 178 | { | 
|---|
| 179 | *fLog << err << dbginf << "ERROR - No file open (gFile==0)" << endl; | 
|---|
| 180 | return kFALSE; | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | TIter Next(&list); | 
|---|
| 184 | TObject *o=0; | 
|---|
| 185 | while ((o=Next())) | 
|---|
| 186 | { | 
|---|
| 187 | *fLog << inf << " - Writing " << MParContainer::GetDescriptor(*o) << "..." << flush; | 
|---|
| 188 | if (o->Write(o->GetName())<=0) | 
|---|
| 189 | { | 
|---|
| 190 | *fLog << err << dbginf << "ERROR - Writing " << MParContainer::GetDescriptor(*o) << " to file " << gFile->GetName() << endl; | 
|---|
| 191 | return kFALSE; | 
|---|
| 192 | } | 
|---|
| 193 | *fLog << "ok." << endl; | 
|---|
| 194 | } | 
|---|
| 195 | return kTRUE; | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | Bool_t MJob::ReadContainer(TCollection &list) const | 
|---|
| 199 | { | 
|---|
| 200 | if (!gFile) | 
|---|
| 201 | { | 
|---|
| 202 | *fLog << err << dbginf << "ERROR - No file open (gFile==0)" << endl; | 
|---|
| 203 | return kFALSE; | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | MIter Next(&list); | 
|---|
| 207 | MParContainer *o=0; | 
|---|
| 208 | while ((o=Next())) | 
|---|
| 209 | { | 
|---|
| 210 | *fLog << inf << " - Reading " << o->GetDescriptor() << "..." << flush; | 
|---|
| 211 | if (o->Read(o->GetName())<=0) | 
|---|
| 212 | { | 
|---|
| 213 | *fLog << err << dbginf << "ERROR - Reading " << o->GetDescriptor() << " from file " << gFile->GetName() << endl; | 
|---|
| 214 | return kFALSE; | 
|---|
| 215 | } | 
|---|
| 216 | *fLog << "ok." << endl; | 
|---|
| 217 | } | 
|---|
| 218 | return kTRUE; | 
|---|
| 219 | } | 
|---|