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

Last change on this file since 4602 was 4601, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 5.2 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// If you want to create a new task inside a macro you will have to compile
30// your macro using macro.C++, because the root interpreter cannot use
31// uncompiled classes. To workaround this problem you can write simple
32// funcions (which can be handled by CINT) and use MTaskEnv.
33//
34// This is a simple way to develop new code in a macro without need
35// to compile it.
36//
37// Example:
38// Int_t Process()
39// {
40// gLog << "Processing..." << endl;
41// return kTRUE;
42// }
43//
44// void main()
45// {
46// MTaskEnv task;
47// task.SetProcess(Process);
48// MTaskList list;
49// list.AddToList(&task);
50// }
51//
52//
53// Input Containers:
54// -/-
55//
56// Output Containers:
57// -/-
58//
59/////////////////////////////////////////////////////////////////////////////
60#include "MTaskEnv.h"
61
62#include <TClass.h>
63
64#include "MLog.h"
65#include "MLogManip.h"
66
67ClassImp(MTaskEnv);
68
69using namespace std;
70// --------------------------------------------------------------------------
71//
72// Default Constructor. Takes name and title of the interactive task
73//
74MTaskEnv::MTaskEnv(const char *name, const char *title) : fTask(0)
75{
76 fName = name ? name : "MTaskEnv";
77 fTitle = title ? title : "Task setup from Environment file";
78}
79
80MTaskEnv::~MTaskEnv()
81{
82 if (TestBit(kIsOwner))
83 delete fTask;
84}
85
86MTask *MTaskEnv::GetTask(const char *name) const
87{
88 //
89 // try to get class from root environment
90 //
91 TClass *cls = gROOT->GetClass(name);
92 Int_t rc = 0;
93 if (!cls)
94 rc =1;
95 else
96 {
97 if (!cls->Property())
98 rc = 5;
99 if (!cls->Size())
100 rc = 4;
101 if (!cls->IsLoaded())
102 rc = 3;
103 if (!cls->HasDefaultConstructor())
104 rc = 2;
105 }
106
107 if (rc)
108 {
109 *fLog << err << dbginf << "Cannot create new instance of class '" << name << "': ";
110 switch (rc)
111 {
112 case 1:
113 *fLog << "gROOT->GetClass() returned NULL." << endl;
114 return NULL;
115 case 2:
116 *fLog << "no default constructor." << endl;
117 return NULL;
118 case 3:
119 *fLog << "not loaded." << endl;
120 return NULL;
121 case 4:
122 *fLog << "zero size." << endl;
123 return NULL;
124 case 5:
125 *fLog << "no property." << endl;
126 return NULL;
127 }
128 }
129
130 if (!cls->InheritsFrom(MTask::Class()))
131 {
132 *fLog << " - Class doesn't inherit from MTask." << endl;
133 return NULL;
134 }
135
136 //
137 // create the parameter container of the the given class type
138 //
139 MTask *task = (MTask*)cls->New();
140 if (!task)
141 {
142 *fLog << " - Class has no default constructor." << endl;
143 *fLog << " - An abstract member functions of a base class is not overwritten." << endl;
144 return NULL;
145 }
146
147 task->SetName(fName);
148 task->SetTitle(fTitle);
149
150 return task;
151}
152
153void MTaskEnv::SetDefault(const char *def)
154{
155 if (TestBit(kIsOwner) && fTask)
156 delete fTask;
157
158 fTask = GetTask(def);
159 if (!fTask)
160 *fLog << err << dbginf << "ERROR - No default Task setup..." << endl;
161
162 SetBit(kIsOwner);
163}
164
165Bool_t MTaskEnv::ReInit(MParList *list)
166{
167 return fTask->ReInit(list);
168}
169
170Int_t MTaskEnv::PreProcess(MParList *list)
171{
172 if (!fTask)
173 {
174 *fLog << err << GetDescriptor() << " - ERROR: Not task setup." << endl;
175 return kFALSE;
176 }
177
178 *fLog << fTask->ClassName() << " - " << flush;
179 return fTask->CallPreProcess(list);
180}
181
182Int_t MTaskEnv::Process()
183{
184 return fTask->CallProcess();
185}
186
187Int_t MTaskEnv::PostProcess()
188{
189 return fTask->CallPostProcess();
190}
191
192Int_t MTaskEnv::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
193{
194 if (!IsEnvDefined(env, prefix, print))
195 return kFALSE;
196
197 TString task = GetEnvValue(env, prefix, "");
198 task.ReplaceAll("\015", "");
199 task = task.Strip(TString::kBoth);
200 fTask = GetTask(task.Data());
201 if (!fTask)
202 {
203 *fLog << err << GetDescriptor() << " - ERROR: No task matching '";
204 *fLog << task << "' could be setup." << endl;
205 return kERROR;
206 }
207
208 SetBit(kIsOwner);
209
210 return fTask->ReadEnv(env, prefix, print);
211}
212
213Bool_t MTaskEnv::WriteEnv(TEnv &env, TString prefix, Bool_t print) const
214{
215 return fTask->WriteEnv(env, prefix, print);
216}
Note: See TracBrowser for help on using the repository browser.