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

Last change on this file since 8076 was 7358, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 9.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 "MParList.h"
50#include "MEvtLoop.h"
51
52ClassImp(MJob);
53
54using namespace std;
55
56// --------------------------------------------------------------------------
57//
58// Default constructor.
59//
60// Sets fDataFlag to 0
61//
62MJob::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
68void MJob::ClearEnv()
69{
70 if (fEnv && TestBit(kIsOwner))
71 delete fEnv;
72 ResetBit(kIsOwner);
73 fEnv=0;
74}
75
76MJob::~MJob()
77{
78 ClearEnv();
79}
80
81Bool_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
105void 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
119void 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
130void MJob::SetPathOut(const char *path)
131{
132 fPathOut = path;
133 FixPath(fPathOut);
134}
135
136void MJob::SetPathIn(const char *path)
137{
138 fPathIn = path;
139 FixPath(fPathIn);
140}
141
142void MJob::SetPathData(const char *path)
143{
144 fPathData = path;
145 FixPath(fPathData);
146}
147
148const TEnv *MJob::GetEnv() const
149{
150 return static_cast<const TEnv *const>(fEnv);
151}
152
153Int_t MJob::GetEnv(const char *name, Int_t dflt) const
154{
155 return GetEnvValue(*fEnv, fEnvPrefix, name, dflt); // return fEnv->GetValue(Form("%s%s", fEnvPrefix.Data(), name), dflt);
156}
157
158Double_t MJob::GetEnv(const char *name, Double_t dflt) const
159{
160 return GetEnvValue(*fEnv, fEnvPrefix, name, dflt); // return fEnv->GetValue(Form("%s%s", fEnvPrefix.Data(), name), dflt);
161}
162
163const char *MJob::GetEnv(const char *name, const char *dflt) const
164{
165 return GetEnvValue(*fEnv, fEnvPrefix, name, dflt); //fEnv->GetValue(Form("%s%s", fEnvPrefix.Data(), name), dflt);
166}
167
168Bool_t MJob::HasEnv(const char *name) const
169{
170 return IsEnvDefined(*fEnv, fEnvPrefix, name, fEnvDebug>2);//fEnv->Lookup(Form("%s%s", fEnvPrefix.Data(), name));
171}
172
173Bool_t MJob::CheckEnv()
174{
175 if (!fEnv)
176 return kTRUE;
177
178 TString p;
179 p = GetEnv("PathOut", "");
180 if (!p.IsNull())
181 SetPathOut(p);
182
183 p = GetEnv("PathIn", "");
184 if (!p.IsNull())
185 SetPathIn(p);
186
187 p = GetEnv("PathData", "");
188 if (!p.IsNull())
189 SetPathData(p);
190
191 SetMaxEvents(GetEnv("MaxEvents", fMaxEvents));
192 SetOverwrite(GetEnv("Overwrite", fOverwrite));
193 SetEnvDebug( GetEnv("EnvDebug", fEnvDebug));
194
195 return CheckEnvLocal();
196}
197
198//------------------------------------------------------------------------
199//
200// Returns the result of c.ReadEnv(*fEnv, fEnvPrefix, fEnvDebug>2)
201// By adding the container first to a MParList it is ensured that
202// all levels are checked.
203//
204Bool_t MJob::CheckEnv(MParContainer &c) const
205{
206 if (!fEnv)
207 return kTRUE;
208
209 // Make sure that all levels are checked
210 MParList l;
211 l.AddToList(&c);
212 return l.ReadEnv(*fEnv, fEnvPrefix+".", fEnvDebug>2);
213}
214
215Bool_t MJob::SetupEnv(MEvtLoop &loop) const
216{
217 if (!fEnv)
218 return kTRUE;
219
220 if (!loop.ReadEnv(*fEnv, fEnvPrefix, fEnvDebug>2))
221 return kFALSE;
222
223 if (fEnvDebug>1)
224 fEnv->PrintUntouched();
225
226 return kTRUE;
227}
228
229//------------------------------------------------------------------------
230//
231// Write containers in list to gFile. Returns kFALSE if no gFile or any
232// container couldn't be written. kTRUE otherwise.
233//
234Bool_t MJob::WriteContainer(TCollection &list) const
235{
236 if (!gFile)
237 {
238 *fLog << err << dbginf << "ERROR - No file open (gFile==0)" << endl;
239 return kFALSE;
240 }
241
242 TIter Next(&list);
243 TObject *o=0;
244 while ((o=Next()))
245 {
246 *fLog << inf << " - Writing " << MParContainer::GetDescriptor(*o) << "..." << flush;
247 if (o->Write(o->GetName())<=0)
248 {
249 *fLog << err << dbginf << "ERROR - Writing " << MParContainer::GetDescriptor(*o) << " to file " << gFile->GetName() << endl;
250 return kFALSE;
251 }
252 *fLog << "ok." << endl;
253 }
254 return kTRUE;
255}
256
257//------------------------------------------------------------------------
258//
259// Read containers in list into list from gFile
260// Returns kFALSE if no gFile or any container couldn't be read.
261//
262Bool_t MJob::ReadContainer(TCollection &list) const
263{
264 if (!gFile)
265 {
266 *fLog << err << dbginf << "ERROR - No file open (gFile==0)" << endl;
267 return kFALSE;
268 }
269
270 MIter Next(&list);
271 MParContainer *o=0;
272 while ((o=Next()))
273 {
274 *fLog << inf << " - Reading " << o->GetDescriptor() << "..." << flush;
275 if (o->Read(o->GetName())<=0)
276 {
277 *fLog << err << dbginf << "ERROR - Reading " << o->GetDescriptor() << " from file " << gFile->GetName() << endl;
278 return kFALSE;
279 }
280 *fLog << "ok." << endl;
281 }
282 return kTRUE;
283}
284
285//------------------------------------------------------------------------
286//
287// Write containers in cont (and - if available) the status display to
288// fPathOut+"/"+name
289//
290Bool_t MJob::WriteContainer(TCollection &cont, const char *name, const char *option, const int compr) const
291{
292 if (fPathOut.IsNull())
293 {
294 *fLog << inf << "No output path specified via SetPathOut - no output written." << endl;
295 return kTRUE;
296 }
297
298 TString oname(fPathOut);
299 oname += "/";
300 oname += name;
301
302 *fLog << inf << "Writing to file: " << oname << endl;
303
304 TString title("File Written by ");
305 title += fName;
306
307 TFile file(oname, option, title, compr);
308 if (!file.IsOpen())
309 {
310 *fLog << err << "ERROR - Couldn't open file " << oname << " for writing..." << endl;
311 return kFALSE;
312 }
313
314 if (fDisplay)
315 cont.Add((TObject*)(fDisplay));
316
317 return WriteContainer(cont);
318}
319
320TString MJob::ExpandPath(TString fname)
321{
322 // empty
323 if (fname.IsNull())
324 return "";
325
326 // Expand path using environment
327 gSystem->ExpandPathName(fname);
328
329 // Absolute path
330 if (fname[0]=='/')
331 {
332 gLog << dbg << "MJob::ExpandPath - Path " << fname << " is absolute." << endl;
333 return fname;
334 }
335
336 // relative path to file and file could be found
337 if (!gSystem->AccessPathName(fname, kFileExists))
338 {
339 gLog << dbg << "MJob::ExpandPath - Relative path " << fname << " found..." << endl;
340 return fname;
341 }
342
343 // Now check gEnv and MARSSYS. gEnv can overwrite MARSSYS
344 TString path(gEnv ? gEnv->GetValue("Mars.Path", "$MARSSYS") : "$MARSSYS");
345
346 // Expand path using environment
347 gSystem->ExpandPathName(path);
348
349 // check if path ends with a slash
350 if (!path.EndsWith("/"))
351 path += "/";
352
353 // compile full qualified path
354 path += fname;
355
356 gLog << dbg << "MJob::ExpandPath - Filename expanded to " << path << endl;
357
358 // return new path
359 return path;
360}
361
362void MJob::SortArray(TArrayI &arr)
363{
364 TArrayI idx(arr.GetSize());
365 TArrayI srt(arr);
366
367 TMath::Sort(arr.GetSize(), srt.GetArray(), idx.GetArray(), kFALSE);
368
369 for (int i=0; i<arr.GetSize(); i++)
370 arr[i] = srt[idx[i]];
371}
Note: See TracBrowser for help on using the repository browser.