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

Last change on this file since 7096 was 7096, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 8.5 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::CheckEnv(MParContainer &c) const
198{
199 if (!fEnv)
200 return kTRUE;
201
202 return c.ReadEnv(*fEnv, fEnvPrefix, fEnvDebug>2);
203}
204
205Bool_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//
224Bool_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//
252Bool_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//
280Bool_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
310TString 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}
Note: See TracBrowser for help on using the repository browser.