source: trunk/MagicSoft/Mars/mjobs/MJob.cc@ 7013

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