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

Last change on this file since 6000 was 5994, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 4.7 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 MTask *task = (MTask*)GetNewObject(name, MTask::Class());
103 if (!task)
104 return NULL;
105
106 task->SetName(fName);
107 task->SetTitle(fTitle);
108
109 return task;
110}
111
112void MTaskEnv::SetDefault(const char *def)
113{
114 if (TestBit(kIsOwner) && fTask)
115 delete fTask;
116
117 fTask = GetTask(def);
118 if (!fTask)
119 *fLog << err << dbginf << "ERROR - No default Task setup..." << endl;
120
121 SetBit(kIsOwner);
122}
123
124Bool_t MTaskEnv::ReInit(MParList *list)
125{
126 *fLog << fTask->ClassName() << " <MTaskEnv>... " << flush;
127 return fTask->ReInit(list);
128}
129
130Int_t MTaskEnv::PreProcess(MParList *list)
131{
132 if (TestBit(kIsDummy))
133 {
134 *fLog << inf << "Dummy Task... skipped." << endl;
135 return kSKIP;
136 }
137
138 if (!fTask)
139 {
140 *fLog << err << GetDescriptor() << " - ERROR: No task setup." << endl;
141 return kFALSE;
142 }
143
144 *fLog << fTask->ClassName() << " <MTaskEnv>... " << flush;
145 return fTask->CallPreProcess(list);
146}
147
148Int_t MTaskEnv::Process()
149{
150 return fTask->CallProcess();
151}
152
153Int_t MTaskEnv::PostProcess()
154{
155 *fLog << fTask->ClassName() << " <MTaskEnv>... " << flush;
156 return fTask->CallPostProcess();
157}
158
159Int_t MTaskEnv::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
160{
161 if (!IsEnvDefined(env, prefix, print))
162 return fTask ? fTask->ReadEnv(env, prefix, print) : kFALSE;
163
164 TString task = GetEnvValue(env, prefix, "");
165 task.ReplaceAll("\015", "");
166 task = task.Strip(TString::kBoth);
167
168 if (task=="<dummy>")
169 {
170 if (TestBit(kIsOwner) && fTask)
171 delete fTask;
172 fTask = 0;
173 SetBit(kIsDummy);
174 return kTRUE;
175 }
176
177 fTask = GetTask(task.Data());
178 if (!fTask)
179 {
180 *fLog << err << GetDescriptor() << " - ERROR: No task matching '";
181 *fLog << task << "' could be setup." << endl;
182 return kERROR;
183 }
184
185 SetBit(kIsOwner);
186
187 return fTask->ReadEnv(env, prefix, print);
188}
189
190Bool_t MTaskEnv::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
191{
192 return fTask->WriteEnv(env, prefix, print);
193}
Note: See TracBrowser for help on using the repository browser.