source: trunk/MagicSoft/Mars/mbase/MTaskEnv.cc@ 5468

Last change on this file since 5468 was 5307, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 6.0 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// MTaskEnv
28//
29// This class is used if a task - at runtime - should be replaced or setup
30// from a resource file, eg.
31//
32//
33// resources.rc:
34// MyTask: MThisIsMyClass
35// MyTask.Resource1: yes
36// MyTask.Resource2: 50
37//
38// macro.C:
39// MTaskList list;
40//
41// MDefaultTask def;
42// MTaskEnv taskenv("MyTask");
43// taskenv.SetDefault(&def);
44//
45// list.AddToList(&taskenv);
46// [...]
47// evtloop.ReadEnv("resource.rc");
48//
49// In this case MTaskEnv will act like MDefaultTask if nothing is found in the
50// resource file. If the task is setup via the resource file like in the
51// example above it will act like a MThisIsMyClass setup with Resource1 and
52// Resource2.
53//
54//
55// You can also skip the task completely if you setup (in lower case letters!)
56//
57// resources.rc:
58// MyTask: <dummy>
59//
60//
61// A third option is to setup a MTaskEnv to be skipped except a task is
62// initialized through the resource file:
63// MTaskEnv taskenv("MyTask");
64// taskenv.SetDefault(0);
65//
66//
67// Input Containers:
68// -/-
69//
70// Output Containers:
71// -/-
72//
73/////////////////////////////////////////////////////////////////////////////
74#include "MTaskEnv.h"
75
76#include <TClass.h>
77
78#include "MLog.h"
79#include "MLogManip.h"
80
81ClassImp(MTaskEnv);
82
83using namespace std;
84// --------------------------------------------------------------------------
85//
86// Default Constructor. Takes name and title of the interactive task
87//
88MTaskEnv::MTaskEnv(const char *name, const char *title) : fTask(0)
89{
90 fName = name ? name : "MTaskEnv";
91 fTitle = title ? title : "Task setup from Environment file";
92}
93
94MTaskEnv::~MTaskEnv()
95{
96 if (TestBit(kIsOwner))
97 delete fTask;
98}
99
100MTask *MTaskEnv::GetTask(const char *name) const
101{
102 //
103 // try to get class from root environment
104 //
105 TClass *cls = gROOT->GetClass(name);
106 Int_t rc = 0;
107 if (!cls)
108 rc =1;
109 else
110 {
111 if (!cls->Property())
112 rc = 5;
113 if (!cls->Size())
114 rc = 4;
115 if (!cls->IsLoaded())
116 rc = 3;
117 if (!cls->HasDefaultConstructor())
118 rc = 2;
119 }
120
121 if (rc)
122 {
123 *fLog << err << dbginf << "Cannot create new instance of class '" << name << "': ";
124 switch (rc)
125 {
126 case 1:
127 *fLog << "gROOT->GetClass() returned NULL." << endl;
128 return NULL;
129 case 2:
130 *fLog << "no default constructor." << endl;
131 return NULL;
132 case 3:
133 *fLog << "not loaded." << endl;
134 return NULL;
135 case 4:
136 *fLog << "zero size." << endl;
137 return NULL;
138 case 5:
139 *fLog << "no property." << endl;
140 return NULL;
141 }
142 }
143
144 if (!cls->InheritsFrom(MTask::Class()))
145 {
146 *fLog << " - Class doesn't inherit from MTask." << endl;
147 return NULL;
148 }
149
150 //
151 // create the parameter container of the the given class type
152 //
153 MTask *task = (MTask*)cls->New();
154 if (!task)
155 {
156 *fLog << " - Class has no default constructor." << endl;
157 *fLog << " - An abstract member functions of a base class is not overwritten." << endl;
158 return NULL;
159 }
160
161 task->SetName(fName);
162 task->SetTitle(fTitle);
163
164 return task;
165}
166
167void MTaskEnv::SetDefault(const char *def)
168{
169 if (TestBit(kIsOwner) && fTask)
170 delete fTask;
171
172 fTask = GetTask(def);
173 if (!fTask)
174 *fLog << err << dbginf << "ERROR - No default Task setup..." << endl;
175
176 SetBit(kIsOwner);
177}
178
179Bool_t MTaskEnv::ReInit(MParList *list)
180{
181 *fLog << fTask->ClassName() << " <MTaskEnv>... " << flush;
182 return fTask->ReInit(list);
183}
184
185Int_t MTaskEnv::PreProcess(MParList *list)
186{
187 if (TestBit(kIsDummy))
188 {
189 *fLog << inf << "Dummy Task... skipped." << endl;
190 return kSKIP;
191 }
192
193 if (!fTask)
194 {
195 *fLog << err << GetDescriptor() << " - ERROR: No task setup." << endl;
196 return kFALSE;
197 }
198
199 *fLog << fTask->ClassName() << " <MTaskEnv>... " << flush;
200 return fTask->CallPreProcess(list);
201}
202
203Int_t MTaskEnv::Process()
204{
205 return fTask->CallProcess();
206}
207
208Int_t MTaskEnv::PostProcess()
209{
210 *fLog << fTask->ClassName() << " <MTaskEnv>... " << flush;
211 return fTask->CallPostProcess();
212}
213
214Int_t MTaskEnv::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
215{
216 if (!IsEnvDefined(env, prefix, print))
217 return fTask ? fTask->ReadEnv(env, prefix, print) : kFALSE;
218
219 TString task = GetEnvValue(env, prefix, "");
220 task.ReplaceAll("\015", "");
221 task = task.Strip(TString::kBoth);
222
223 if (task=="<dummy>")
224 {
225 if (TestBit(kIsOwner) && fTask)
226 delete fTask;
227 fTask = 0;
228 SetBit(kIsDummy);
229 return kTRUE;
230 }
231
232 fTask = GetTask(task.Data());
233 if (!fTask)
234 {
235 *fLog << err << GetDescriptor() << " - ERROR: No task matching '";
236 *fLog << task << "' could be setup." << endl;
237 return kERROR;
238 }
239
240 SetBit(kIsOwner);
241
242 return fTask->ReadEnv(env, prefix, print);
243}
244
245Bool_t MTaskEnv::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
246{
247 return fTask->WriteEnv(env, prefix, print);
248}
Note: See TracBrowser for help on using the repository browser.