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 |
|
---|
51 | ClassImp(MJob);
|
---|
52 |
|
---|
53 | using namespace std;
|
---|
54 |
|
---|
55 | // --------------------------------------------------------------------------
|
---|
56 | //
|
---|
57 | // Default constructor.
|
---|
58 | //
|
---|
59 | // Sets fDataFlag to 0
|
---|
60 | //
|
---|
61 | MJob::MJob(const char *name, const char *title) : fEnv(0), fEnvDebug(0), fOverwrite(kFALSE), fMaxEvents(0)
|
---|
62 |
|
---|
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 | void MJob::SetPathData(const char *path)
|
---|
143 | {
|
---|
144 | fPathData = path;
|
---|
145 | FixPath(fPathData);
|
---|
146 | }
|
---|
147 |
|
---|
148 | const TEnv *MJob::GetEnv() const
|
---|
149 | {
|
---|
150 | return static_cast<const TEnv *const>(fEnv);
|
---|
151 | }
|
---|
152 |
|
---|
153 | Int_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 |
|
---|
158 | Double_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 |
|
---|
163 | const 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 |
|
---|
168 | Bool_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 |
|
---|
173 | Bool_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 | Bool_t MJob::SetupEnv(MEvtLoop &loop) const
|
---|
199 | {
|
---|
200 | if (!fEnv)
|
---|
201 | return kTRUE;
|
---|
202 |
|
---|
203 | if (!loop.ReadEnv(*fEnv, fEnvPrefix, fEnvDebug>2))
|
---|
204 | return kFALSE;
|
---|
205 |
|
---|
206 | if (fEnvDebug>1)
|
---|
207 | fEnv->PrintUntouched();
|
---|
208 |
|
---|
209 | return kTRUE;
|
---|
210 | }
|
---|
211 |
|
---|
212 | //------------------------------------------------------------------------
|
---|
213 | //
|
---|
214 | // Write containers in list to gFile. Returns kFALSE if no gFile or any
|
---|
215 | // container couldn't be written. kTRUE otherwise.
|
---|
216 | //
|
---|
217 | Bool_t MJob::WriteContainer(TCollection &list) const
|
---|
218 | {
|
---|
219 | if (!gFile)
|
---|
220 | {
|
---|
221 | *fLog << err << dbginf << "ERROR - No file open (gFile==0)" << endl;
|
---|
222 | return kFALSE;
|
---|
223 | }
|
---|
224 |
|
---|
225 | TIter Next(&list);
|
---|
226 | TObject *o=0;
|
---|
227 | while ((o=Next()))
|
---|
228 | {
|
---|
229 | *fLog << inf << " - Writing " << MParContainer::GetDescriptor(*o) << "..." << flush;
|
---|
230 | if (o->Write(o->GetName())<=0)
|
---|
231 | {
|
---|
232 | *fLog << err << dbginf << "ERROR - Writing " << MParContainer::GetDescriptor(*o) << " to file " << gFile->GetName() << endl;
|
---|
233 | return kFALSE;
|
---|
234 | }
|
---|
235 | *fLog << "ok." << endl;
|
---|
236 | }
|
---|
237 | return kTRUE;
|
---|
238 | }
|
---|
239 |
|
---|
240 | //------------------------------------------------------------------------
|
---|
241 | //
|
---|
242 | // Read containers in list into list from gFile
|
---|
243 | // Returns kFALSE if no gFile or any container couldn't be read.
|
---|
244 | //
|
---|
245 | Bool_t MJob::ReadContainer(TCollection &list) const
|
---|
246 | {
|
---|
247 | if (!gFile)
|
---|
248 | {
|
---|
249 | *fLog << err << dbginf << "ERROR - No file open (gFile==0)" << endl;
|
---|
250 | return kFALSE;
|
---|
251 | }
|
---|
252 |
|
---|
253 | MIter Next(&list);
|
---|
254 | MParContainer *o=0;
|
---|
255 | while ((o=Next()))
|
---|
256 | {
|
---|
257 | *fLog << inf << " - Reading " << o->GetDescriptor() << "..." << flush;
|
---|
258 | if (o->Read(o->GetName())<=0)
|
---|
259 | {
|
---|
260 | *fLog << err << dbginf << "ERROR - Reading " << o->GetDescriptor() << " from file " << gFile->GetName() << endl;
|
---|
261 | return kFALSE;
|
---|
262 | }
|
---|
263 | *fLog << "ok." << endl;
|
---|
264 | }
|
---|
265 | return kTRUE;
|
---|
266 | }
|
---|
267 |
|
---|
268 | //------------------------------------------------------------------------
|
---|
269 | //
|
---|
270 | // Write containers in cont (and - if available) the status display to
|
---|
271 | // fPathOut+"/"+name
|
---|
272 | //
|
---|
273 | Bool_t MJob::WriteContainer(TCollection &cont, const char *name, const char *option, const int compr) const
|
---|
274 | {
|
---|
275 | if (fPathOut.IsNull())
|
---|
276 | {
|
---|
277 | *fLog << inf << "No output path specified via SetPathOut - no output written." << endl;
|
---|
278 | return kTRUE;
|
---|
279 | }
|
---|
280 |
|
---|
281 | TString oname(fPathOut);
|
---|
282 | oname += "/";
|
---|
283 | oname += name;
|
---|
284 |
|
---|
285 | *fLog << inf << "Writing to file: " << oname << endl;
|
---|
286 |
|
---|
287 | TString title("File Written by ");
|
---|
288 | title += fName;
|
---|
289 |
|
---|
290 | TFile file(oname, option, title, compr);
|
---|
291 | if (!file.IsOpen())
|
---|
292 | {
|
---|
293 | *fLog << err << "ERROR - Couldn't open file " << oname << " for writing..." << endl;
|
---|
294 | return kFALSE;
|
---|
295 | }
|
---|
296 |
|
---|
297 | if (fDisplay)
|
---|
298 | cont.Add((TObject*)(fDisplay));
|
---|
299 |
|
---|
300 | return WriteContainer(cont);
|
---|
301 | }
|
---|