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

Last change on this file since 6517 was 6177, checked in by gaug, 20 years ago
*** empty log message ***
File size: 6.6 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/////////////////////////////////////////////////////////////////////////////
32#include "MJob.h"
33
34#include <TEnv.h>
35#include <TFile.h>
36#include <TSystem.h>
37#include <TObjArray.h>
38
39#include "MIter.h"
40
41#include "MLog.h"
42#include "MLogManip.h"
43
44#include "MEvtLoop.h"
45
46ClassImp(MJob);
47
48using namespace std;
49
50// --------------------------------------------------------------------------
51//
52// Default constructor.
53//
54// Sets fDataFlag to 0
55//
56MJob::MJob(const char *name, const char *title) : fEnv(0), fOverwrite(kFALSE), fMaxEvents(0)
57
58{
59 fName = name ? name : "MJob";
60 fTitle = title ? title : "Base class for jobs";
61}
62
63MJob::~MJob()
64{
65 if (fEnv)
66 delete fEnv;
67}
68
69Bool_t MJob::SetEnv(const char *env, const char *prefix)
70{
71 if (fEnv)
72 {
73 delete fEnv;
74 fEnv = 0;
75 }
76
77 const Bool_t fileexist = !gSystem->AccessPathName(env, kFileExists);
78 if (!fileexist)
79 {
80 *fLog << err << "ERROR - Resource file '" << env << "' not found... no resources applied." << endl;
81 return kFALSE;
82 }
83
84 fEnv = new TEnv(env);
85
86 fEnvPrefix = prefix;
87 if (!prefix)
88 fEnvPrefix = fName.First(' ')>0 ? fName(0, fName.First(' ')) : fName;
89
90 if (fEnvPrefix.EndsWith("."))
91 fEnvPrefix.Remove(fEnvPrefix.Length()-1);
92
93 return kTRUE;
94}
95
96void MJob::FixPath(TString &path) const
97{
98 path.ReplaceAll("\015", "");
99
100 if (path==(TString)"/")
101 return;
102
103 if (path.EndsWith("/"))
104 path.Remove(path.Length()-1);
105}
106
107void MJob::SetPathOut(const char *path)
108{
109 fPathOut = path;
110 FixPath(fPathOut);
111}
112
113void MJob::SetPathIn(const char *path)
114{
115 fPathIn = path;
116 FixPath(fPathIn);
117}
118
119void MJob::SetPathData(const char *path)
120{
121 fPathData = path;
122 FixPath(fPathData);
123}
124
125Int_t MJob::GetEnv(const char *name, Int_t dflt) const
126{
127 return GetEnvValue(*fEnv, fEnvPrefix, name, dflt); // return fEnv->GetValue(Form("%s%s", fEnvPrefix.Data(), name), dflt);
128}
129
130Double_t MJob::GetEnv(const char *name, Double_t dflt) const
131{
132 return GetEnvValue(*fEnv, fEnvPrefix, name, dflt); // return fEnv->GetValue(Form("%s%s", fEnvPrefix.Data(), name), dflt);
133}
134
135const char *MJob::GetEnv(const char *name, const char *dflt) const
136{
137 return GetEnvValue(*fEnv, fEnvPrefix, name, dflt); //fEnv->GetValue(Form("%s%s", fEnvPrefix.Data(), name), dflt);
138}
139
140Bool_t MJob::HasEnv(const char *name) const
141{
142 return IsEnvDefined(*fEnv, fEnvPrefix, name, fEnvDebug);//fEnv->Lookup(Form("%s%s", fEnvPrefix.Data(), name));
143}
144
145Bool_t MJob::CheckEnv()
146{
147 if (!fEnv)
148 return kTRUE;
149
150 TString p;
151 p = GetEnv("PathOut", "");
152 if (!p.IsNull())
153 SetPathOut(p);
154
155 p = GetEnv("PathIn", "");
156 if (!p.IsNull())
157 SetPathIn(p);
158
159 p = GetEnv("PathData", "");
160 if (!p.IsNull())
161 SetPathData(p);
162
163 SetMaxEvents(GetEnv("MaxEvents", fMaxEvents));
164 SetOverwrite(GetEnv("Overwrite", fOverwrite));
165
166 return CheckEnvLocal();
167}
168
169Bool_t MJob::SetupEnv(MEvtLoop &loop) const
170{
171 if (!fEnv)
172 return kTRUE;
173
174 return loop.ReadEnv(*fEnv, fEnvPrefix, fEnvDebug) ? kTRUE : kFALSE;
175}
176
177//------------------------------------------------------------------------
178//
179// Write containers in list to gFile. Returns kFALSE if no gFile or any
180// container couldn't be written. kTRUE otherwise.
181//
182Bool_t MJob::WriteContainer(TCollection &list) const
183{
184 if (!gFile)
185 {
186 *fLog << err << dbginf << "ERROR - No file open (gFile==0)" << endl;
187 return kFALSE;
188 }
189
190 TIter Next(&list);
191 TObject *o=0;
192 while ((o=Next()))
193 {
194 *fLog << inf << " - Writing " << MParContainer::GetDescriptor(*o) << "..." << flush;
195 if (o->Write(o->GetName())<=0)
196 {
197 *fLog << err << dbginf << "ERROR - Writing " << MParContainer::GetDescriptor(*o) << " to file " << gFile->GetName() << endl;
198 return kFALSE;
199 }
200 *fLog << "ok." << endl;
201 }
202 return kTRUE;
203}
204
205//------------------------------------------------------------------------
206//
207// Read containers in list into list from gFile
208// Returns kFALSE if no gFile or any container couldn't be read.
209//
210Bool_t MJob::ReadContainer(TCollection &list) const
211{
212 if (!gFile)
213 {
214 *fLog << err << dbginf << "ERROR - No file open (gFile==0)" << endl;
215 return kFALSE;
216 }
217
218 MIter Next(&list);
219 MParContainer *o=0;
220 while ((o=Next()))
221 {
222 *fLog << inf << " - Reading " << o->GetDescriptor() << "..." << flush;
223 if (o->Read(o->GetName())<=0)
224 {
225 *fLog << err << dbginf << "ERROR - Reading " << o->GetDescriptor() << " from file " << gFile->GetName() << endl;
226 return kFALSE;
227 }
228 *fLog << "ok." << endl;
229 }
230 return kTRUE;
231}
232
233//------------------------------------------------------------------------
234//
235// Write containers in cont (and - if available) the status display to
236// fPathOut+"/"+name
237//
238Bool_t MJob::WriteContainer(TCollection &cont, const char *name, const char *option, const int compr) const
239{
240 if (fPathOut.IsNull())
241 {
242 *fLog << inf << "No output path specified via SetPathOut - no output written." << endl;
243 return kTRUE;
244 }
245
246 TString oname(fPathOut);
247 oname += "/";
248 oname += name;
249
250 *fLog << inf << "Writing to file: " << oname << endl;
251
252 TString title("File Written by ");
253 title += fName;
254
255 TFile file(oname, option, title, compr);
256 if (!file.IsOpen())
257 {
258 *fLog << err << "ERROR - Couldn't open file " << oname << " for writing..." << endl;
259 return kFALSE;
260 }
261
262 if (fDisplay)
263 cont.Add((TObject*)(fDisplay));
264
265 return WriteContainer(cont);
266}
Note: See TracBrowser for help on using the repository browser.