source: trunk/Mars/mbase/MTaskEnv.cc@ 20095

Last change on this file since 20095 was 7601, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 4.8 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
146 fTask->SetAccelerator(GetAccelerator());
147 return fTask->CallPreProcess(list);
148}
149
150Int_t MTaskEnv::Process()
151{
152 return fTask->CallProcess();
153}
154
155Int_t MTaskEnv::PostProcess()
156{
157 *fLog << fTask->ClassName() << " <MTaskEnv>... " << flush;
158 return fTask->CallPostProcess();
159}
160
161Int_t MTaskEnv::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
162{
163 if (!IsEnvDefined(env, prefix, print))
164 return fTask ? fTask->ReadEnv(env, prefix, print) : kFALSE;
165
166 TString task = GetEnvValue(env, prefix, "");
167 task.ReplaceAll("\015", "");
168 task = task.Strip(TString::kBoth);
169
170 if (task=="<dummy>")
171 {
172 if (TestBit(kIsOwner) && fTask)
173 delete fTask;
174 fTask = 0;
175 SetBit(kIsDummy);
176 return kTRUE;
177 }
178
179 fTask = GetTask(task.Data());
180 if (!fTask)
181 {
182 *fLog << err << GetDescriptor() << " - ERROR: No task matching '";
183 *fLog << task << "' could be setup." << endl;
184 return kERROR;
185 }
186
187 ResetBit(kIsDummy);
188 SetBit(kIsOwner);
189
190 return fTask->ReadEnv(env, prefix, print);
191}
192
193Bool_t MTaskEnv::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
194{
195 return fTask->WriteEnv(env, prefix, print);
196}
Note: See TracBrowser for help on using the repository browser.