| 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 | // SetDebugEnv(0) // switch off debugging
|
|---|
| 32 | // SetDebugEnv(1) // reserved
|
|---|
| 33 | // SetDebugEnv(2) // print untouched resources after evtloop resources setup
|
|---|
| 34 | // SetDebugEnv(3) // do 2) and debug setting env completely
|
|---|
| 35 | //
|
|---|
| 36 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 37 | #include "MJob.h"
|
|---|
| 38 |
|
|---|
| 39 | #include "MEnv.h"
|
|---|
| 40 | #include <TFile.h>
|
|---|
| 41 | #include <TSystem.h>
|
|---|
| 42 | #include <TObjArray.h>
|
|---|
| 43 |
|
|---|
| 44 | #include "MIter.h"
|
|---|
| 45 |
|
|---|
| 46 | #include "MLog.h"
|
|---|
| 47 | #include "MLogManip.h"
|
|---|
| 48 |
|
|---|
| 49 | #include "MParList.h"
|
|---|
| 50 | #include "MEvtLoop.h"
|
|---|
| 51 |
|
|---|
| 52 | ClassImp(MJob);
|
|---|
| 53 |
|
|---|
| 54 | using namespace std;
|
|---|
| 55 |
|
|---|
| 56 | // --------------------------------------------------------------------------
|
|---|
| 57 | //
|
|---|
| 58 | // Default constructor.
|
|---|
| 59 | //
|
|---|
| 60 | // Sets fDataFlag to 0
|
|---|
| 61 | //
|
|---|
| 62 | MJob::MJob(const char *name, const char *title) : fEnv(0), fEnvDebug(0), fOverwrite(kFALSE), fMaxEvents(0)
|
|---|
| 63 | {
|
|---|
| 64 | fName = name ? name : "MJob";
|
|---|
| 65 | fTitle = title ? title : "Base class for jobs";
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | void MJob::ClearEnv()
|
|---|
| 69 | {
|
|---|
| 70 | if (fEnv && TestBit(kIsOwner))
|
|---|
| 71 | delete fEnv;
|
|---|
| 72 | ResetBit(kIsOwner);
|
|---|
| 73 | fEnv=0;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | MJob::~MJob()
|
|---|
| 77 | {
|
|---|
| 78 | ClearEnv();
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | Bool_t MJob::SetEnv(const char *env, const char *prefix)
|
|---|
| 82 | {
|
|---|
| 83 | ClearEnv();
|
|---|
| 84 |
|
|---|
| 85 | const Bool_t fileexist = !gSystem->AccessPathName(env, kFileExists);
|
|---|
| 86 | if (!fileexist)
|
|---|
| 87 | {
|
|---|
| 88 | *fLog << err << "ERROR - Resource file '" << env << "' not found... no resources applied." << endl;
|
|---|
| 89 | return kFALSE;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | fEnv = new MEnv(env);
|
|---|
| 93 | SetBit(kIsOwner);
|
|---|
| 94 |
|
|---|
| 95 | fEnvPrefix = prefix;
|
|---|
| 96 | if (!prefix)
|
|---|
| 97 | fEnvPrefix = fName.First(' ')>0 ? fName(0, fName.First(' ')) : fName;
|
|---|
| 98 |
|
|---|
| 99 | if (fEnvPrefix.EndsWith("."))
|
|---|
| 100 | fEnvPrefix.Remove(fEnvPrefix.Length()-1);
|
|---|
| 101 |
|
|---|
| 102 | return kTRUE;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | void MJob::SetEnv(MEnv *env, const char *prefix)
|
|---|
| 106 | {
|
|---|
| 107 | ClearEnv();
|
|---|
| 108 |
|
|---|
| 109 | fEnv = env;
|
|---|
| 110 |
|
|---|
| 111 | fEnvPrefix = prefix;
|
|---|
| 112 | if (!prefix)
|
|---|
| 113 | fEnvPrefix = fName.First(' ')>0 ? fName(0, fName.First(' ')) : fName;
|
|---|
| 114 |
|
|---|
| 115 | if (fEnvPrefix.EndsWith("."))
|
|---|
| 116 | fEnvPrefix.Remove(fEnvPrefix.Length()-1);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | void MJob::FixPath(TString &path) const
|
|---|
| 120 | {
|
|---|
| 121 | path.ReplaceAll("\015", "");
|
|---|
| 122 |
|
|---|
| 123 | if (path==(TString)"/")
|
|---|
| 124 | return;
|
|---|
| 125 |
|
|---|
| 126 | if (path.EndsWith("/"))
|
|---|
| 127 | path.Remove(path.Length()-1);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | void MJob::SetPathOut(const char *path)
|
|---|
| 131 | {
|
|---|
| 132 | fPathOut = path;
|
|---|
| 133 | FixPath(fPathOut);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | void MJob::SetPathIn(const char *path)
|
|---|
| 137 | {
|
|---|
| 138 | fPathIn = path;
|
|---|
| 139 | FixPath(fPathIn);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | const TEnv *MJob::GetEnv() const
|
|---|
| 143 | {
|
|---|
| 144 | return static_cast<const TEnv *const>(fEnv);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | Int_t MJob::GetEnv(const char *name, Int_t dflt) const
|
|---|
| 148 | {
|
|---|
| 149 | return GetEnvValue(*fEnv, fEnvPrefix, name, dflt); // return fEnv->GetValue(Form("%s%s", fEnvPrefix.Data(), name), dflt);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | Double_t MJob::GetEnv(const char *name, Double_t dflt) const
|
|---|
| 153 | {
|
|---|
| 154 | return GetEnvValue(*fEnv, fEnvPrefix, name, dflt); // return fEnv->GetValue(Form("%s%s", fEnvPrefix.Data(), name), dflt);
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | const char *MJob::GetEnv(const char *name, const char *dflt) const
|
|---|
| 158 | {
|
|---|
| 159 | return GetEnvValue(*fEnv, fEnvPrefix, name, dflt); //fEnv->GetValue(Form("%s%s", fEnvPrefix.Data(), name), dflt);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | Bool_t MJob::HasEnv(const char *name) const
|
|---|
| 163 | {
|
|---|
| 164 | return IsEnvDefined(*fEnv, fEnvPrefix, name, fEnvDebug>2);//fEnv->Lookup(Form("%s%s", fEnvPrefix.Data(), name));
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | Bool_t MJob::CheckEnv()
|
|---|
| 168 | {
|
|---|
| 169 | if (!fEnv)
|
|---|
| 170 | return kTRUE;
|
|---|
| 171 |
|
|---|
| 172 | TString p;
|
|---|
| 173 | p = GetEnv("PathOut", "");
|
|---|
| 174 | if (!p.IsNull())
|
|---|
| 175 | SetPathOut(p);
|
|---|
| 176 |
|
|---|
| 177 | p = GetEnv("PathIn", "");
|
|---|
| 178 | if (!p.IsNull())
|
|---|
| 179 | SetPathIn(p);
|
|---|
| 180 |
|
|---|
| 181 | SetMaxEvents(GetEnv("MaxEvents", fMaxEvents));
|
|---|
| 182 | SetOverwrite(GetEnv("Overwrite", fOverwrite));
|
|---|
| 183 | SetEnvDebug( GetEnv("EnvDebug", fEnvDebug));
|
|---|
| 184 |
|
|---|
| 185 | return CheckEnvLocal();
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | //------------------------------------------------------------------------
|
|---|
| 189 | //
|
|---|
| 190 | // Returns the result of c.ReadEnv(*fEnv, fEnvPrefix, fEnvDebug>2)
|
|---|
| 191 | // By adding the container first to a MParList it is ensured that
|
|---|
| 192 | // all levels are checked.
|
|---|
| 193 | //
|
|---|
| 194 | Bool_t MJob::CheckEnv(MParContainer &c) const
|
|---|
| 195 | {
|
|---|
| 196 | if (!fEnv)
|
|---|
| 197 | return kTRUE;
|
|---|
| 198 |
|
|---|
| 199 | // Make sure that all levels are checked
|
|---|
| 200 | MParList l;
|
|---|
| 201 | l.AddToList(&c);
|
|---|
| 202 | return l.ReadEnv(*fEnv, fEnvPrefix+".", fEnvDebug>2);
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | Bool_t MJob::SetupEnv(MEvtLoop &loop) const
|
|---|
| 206 | {
|
|---|
| 207 | if (!fEnv)
|
|---|
| 208 | return kTRUE;
|
|---|
| 209 |
|
|---|
| 210 | if (!loop.ReadEnv(*fEnv, fEnvPrefix, fEnvDebug>2))
|
|---|
| 211 | return kFALSE;
|
|---|
| 212 |
|
|---|
| 213 | if (fEnvDebug>1)
|
|---|
| 214 | fEnv->PrintUntouched();
|
|---|
| 215 |
|
|---|
| 216 | return kTRUE;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | //------------------------------------------------------------------------
|
|---|
| 220 | //
|
|---|
| 221 | // Write containers in list to gFile. Returns kFALSE if no gFile or any
|
|---|
| 222 | // container couldn't be written. kTRUE otherwise.
|
|---|
| 223 | //
|
|---|
| 224 | Bool_t MJob::WriteContainer(TCollection &list) const
|
|---|
| 225 | {
|
|---|
| 226 | if (!gFile)
|
|---|
| 227 | {
|
|---|
| 228 | *fLog << err << dbginf << "ERROR - No file open (gFile==0)" << endl;
|
|---|
| 229 | return kFALSE;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | TIter Next(&list);
|
|---|
| 233 | TObject *o=0;
|
|---|
| 234 | while ((o=Next()))
|
|---|
| 235 | {
|
|---|
| 236 | *fLog << inf << " - Writing " << MParContainer::GetDescriptor(*o) << "..." << flush;
|
|---|
| 237 | if (o->Write(o->GetName())<=0)
|
|---|
| 238 | {
|
|---|
| 239 | *fLog << err << dbginf << "ERROR - Writing " << MParContainer::GetDescriptor(*o) << " to file " << gFile->GetName() << endl;
|
|---|
| 240 | return kFALSE;
|
|---|
| 241 | }
|
|---|
| 242 | *fLog << "ok." << endl;
|
|---|
| 243 | }
|
|---|
| 244 | return kTRUE;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | //------------------------------------------------------------------------
|
|---|
| 248 | //
|
|---|
| 249 | // Read containers in list into list from gFile
|
|---|
| 250 | // Returns kFALSE if no gFile or any container couldn't be read.
|
|---|
| 251 | //
|
|---|
| 252 | Bool_t MJob::ReadContainer(TCollection &list) const
|
|---|
| 253 | {
|
|---|
| 254 | if (!gFile)
|
|---|
| 255 | {
|
|---|
| 256 | *fLog << err << dbginf << "ERROR - No file open (gFile==0)" << endl;
|
|---|
| 257 | return kFALSE;
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | MIter Next(&list);
|
|---|
| 261 | MParContainer *o=0;
|
|---|
| 262 | while ((o=Next()))
|
|---|
| 263 | {
|
|---|
| 264 | *fLog << inf << " - Reading " << o->GetDescriptor() << "..." << flush;
|
|---|
| 265 | if (o->Read(o->GetName())<=0)
|
|---|
| 266 | {
|
|---|
| 267 | *fLog << err << dbginf << "ERROR - Reading " << o->GetDescriptor() << " from file " << gFile->GetName() << endl;
|
|---|
| 268 | return kFALSE;
|
|---|
| 269 | }
|
|---|
| 270 | *fLog << "ok." << endl;
|
|---|
| 271 | }
|
|---|
| 272 | return kTRUE;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | //------------------------------------------------------------------------
|
|---|
| 276 | //
|
|---|
| 277 | // Write containers in cont (and - if available) the status display to
|
|---|
| 278 | // fPathOut+"/"+name
|
|---|
| 279 | //
|
|---|
| 280 | Bool_t MJob::WriteContainer(TCollection &cont, const char *name, const char *option, const int compr) const
|
|---|
| 281 | {
|
|---|
| 282 | if (fPathOut.IsNull())
|
|---|
| 283 | {
|
|---|
| 284 | *fLog << inf << "No output path specified via SetPathOut - no output written." << endl;
|
|---|
| 285 | return kTRUE;
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | TString oname(fPathOut);
|
|---|
| 289 | oname += "/";
|
|---|
| 290 | oname += name;
|
|---|
| 291 |
|
|---|
| 292 | *fLog << inf << "Writing to file: " << oname << endl;
|
|---|
| 293 |
|
|---|
| 294 | TString title("File Written by ");
|
|---|
| 295 | title += fName;
|
|---|
| 296 |
|
|---|
| 297 | TFile file(oname, option, title, compr);
|
|---|
| 298 | if (!file.IsOpen())
|
|---|
| 299 | {
|
|---|
| 300 | *fLog << err << "ERROR - Couldn't open file " << oname << " for writing..." << endl;
|
|---|
| 301 | return kFALSE;
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | if (fDisplay)
|
|---|
| 305 | cont.Add((TObject*)(fDisplay));
|
|---|
| 306 |
|
|---|
| 307 | return WriteContainer(cont);
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | TString MJob::ExpandPath(TString fname)
|
|---|
| 311 | {
|
|---|
| 312 | // empty
|
|---|
| 313 | if (fname.IsNull())
|
|---|
| 314 | return "";
|
|---|
| 315 |
|
|---|
| 316 | // Expand path using environment
|
|---|
| 317 | gSystem->ExpandPathName(fname);
|
|---|
| 318 |
|
|---|
| 319 | // Absolute path
|
|---|
| 320 | if (fname[0]=='/')
|
|---|
| 321 | {
|
|---|
| 322 | gLog << dbg << "MJob::ExpandPath - Path " << fname << " is absolute." << endl;
|
|---|
| 323 | return fname;
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | // relative path to file and file could be found
|
|---|
| 327 | if (!gSystem->AccessPathName(fname, kFileExists))
|
|---|
| 328 | {
|
|---|
| 329 | gLog << dbg << "MJob::ExpandPath - Relative path " << fname << " found..." << endl;
|
|---|
| 330 | return fname;
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | // Now check gEnv and MARSSYS. gEnv can overwrite MARSSYS
|
|---|
| 334 | TString path(gEnv ? gEnv->GetValue("Mars.Path", "$MARSSYS") : "$MARSSYS");
|
|---|
| 335 |
|
|---|
| 336 | // Expand path using environment
|
|---|
| 337 | gSystem->ExpandPathName(path);
|
|---|
| 338 |
|
|---|
| 339 | // check if path ends with a slash
|
|---|
| 340 | if (!path.EndsWith("/"))
|
|---|
| 341 | path += "/";
|
|---|
| 342 |
|
|---|
| 343 | // compile full qualified path
|
|---|
| 344 | path += fname;
|
|---|
| 345 |
|
|---|
| 346 | gLog << dbg << "MJob::ExpandPath - Filename expanded to " << path << endl;
|
|---|
| 347 |
|
|---|
| 348 | // return new path
|
|---|
| 349 | return path;
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | void MJob::SortArray(TArrayI &arr)
|
|---|
| 353 | {
|
|---|
| 354 | TArrayI idx(arr.GetSize());
|
|---|
| 355 | TArrayI srt(arr);
|
|---|
| 356 |
|
|---|
| 357 | TMath::Sort(arr.GetSize(), srt.GetArray(), idx.GetArray(), kFALSE);
|
|---|
| 358 |
|
|---|
| 359 | for (int i=0; i<arr.GetSize(); i++)
|
|---|
| 360 | arr[i] = srt[idx[i]];
|
|---|
| 361 | }
|
|---|