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