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

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