source: trunk/Mars/mbase/MContinue.cc@ 18679

Last change on this file since 18679 was 9934, checked in by tbretz, 14 years ago
Removed obsolete MTask:: from call to GetFilter() in MContinue
File size: 8.5 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 10/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2003
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MContinue
28//
29// Does nothing than return kCONTINUE in the Process-function
30// (use with filters). For more details see the description of the
31// constructors.
32//
33// To invert the meaning of the contained filter call SetInverted()
34//
35//
36// Input Containers:
37// -/-
38//
39// Output Containers:
40// -/-
41//
42//
43// Class Version 2:
44// ----------------
45// + Int_t fRc;
46//
47/////////////////////////////////////////////////////////////////////////////
48#include "MContinue.h"
49
50#include "MLog.h"
51#include "MLogManip.h"
52
53#include "MF.h"
54#include "MString.h"
55#include "MParList.h"
56#include "MTaskList.h"
57
58ClassImp(MContinue);
59
60using namespace std;
61
62// --------------------------------------------------------------------------
63//
64// Default constructor.
65//
66// Use this constructor if a rule (see MF for more details) shell be used.
67// MContinue will create a MF object and use it as a filter for the
68// instance. The MF-Task is added to the tasklist in front of the MContinue
69// instance and also automatically deleted, eg.
70// MContinue cont("MHillas.fSize<20");
71// tasklist.AddToList(&cont);
72// would skip all events which fullfill "MHillas.fSize<20" from this point
73// in the tasklist.
74// It is not necessary to put the filter in the tasklist. The PreProcess
75// will search for the filter and if it isn't found in the tasklist it
76// is added to the tasklist in front of MContinue.
77//
78// Use the default constructor (or an empty rule) if you want to read the
79// rule from a resource file.
80//
81MContinue::MContinue(const TString rule, const char *name, const char *title)
82 : fTaskList(0), fRc(kCONTINUE)
83{
84 fName = name ? name : "MContinue";
85 fTitle = title ? title : "Task returning kCONTINUE";
86
87 if (rule.IsNull())
88 return;
89
90 SetBit(kIsOwner);
91
92 MTask::SetFilter(new MF(rule, TString("MF(")+fName+")"));
93}
94
95// --------------------------------------------------------------------------
96//
97// Use this if you have a filter. Would be the same as if you would call:
98// MContinue cont;
99// cont.SetFilter(f);
100// It is not necessary to put the filter in the tasklist. The PreProcess
101// will search for the filter and if it isn't found in the tasklist it
102// is added to the tasklist in front of MContinue.
103//
104MContinue::MContinue(MFilter *f, const char *name, const char *title)
105 : fTaskList(0), fRc(kCONTINUE)
106{
107 fName = name ? name : "MContinue";
108 fTitle = title ? title : "Task returning kCONTINUE (or any other return code)";
109
110 SetFilter(f);
111}
112
113// --------------------------------------------------------------------------
114//
115// Delete the filter if it was created automatically
116//
117MContinue::~MContinue()
118{
119 if (TestBit(kIsOwner))
120 delete GetFilter();
121}
122
123// --------------------------------------------------------------------------
124//
125// PreProcess tries to find the tasklist MTaskList, adds the filter
126// before this instance to the tasklist and preprocesses the filter.
127//
128Int_t MContinue::PreProcess(MParList *list)
129{
130 ResetBit(kFilterIsPrivate);
131
132 if (!GetFilter())
133 {
134 if (IsAllowEmpty())
135 {
136 *fLog << warn << GetDescriptor() << " - WARNING: Filter empty... task removed." << endl;
137 return kSKIP;
138 }
139
140 *fLog << inf << "My filter has vanished... skipping." << endl;
141 return kSKIP;
142 }
143
144 fTaskList = (MTaskList*)list->FindTaskListWithTask(this);
145 if (!fTaskList)
146 {
147 *fLog << err << dbginf << "ERROR - MTaskList with this not found... abort." << endl;
148 return kFALSE;
149 }
150
151 if (fTaskList->FindObject(GetFilter()))
152 {
153 *fLog << inf << dbginf << "The filter is already in the tasklist..." << endl;
154 return kTRUE;
155 }
156
157 if ((TString)GetFilter()->GetName()==fName)
158 GetFilter()->SetName(MString::Format("MF:%s", fName.Data()));
159
160 if (!fTaskList->AddToListBefore(GetFilter(), this))
161 {
162 *fLog << err << dbginf << "ERROR - Adding filter before MContinue failed... abort." << endl;
163 return kFALSE;
164 }
165
166 // Make sure, that everything is correctly propageted to the childs
167 GetFilter()->SetDisplay(fDisplay);
168 GetFilter()->SetLogStream(fLog);
169 GetFilter()->SetAccelerator(GetAccelerator());
170
171 // Remeber that the filter is not already in tasklist
172 SetBit(kFilterIsPrivate);
173
174 return GetFilter()->CallPreProcess(list);
175}
176
177// --------------------------------------------------------------------------
178//
179// Propagate display to filter (if set)
180//
181void MContinue::SetDisplay(MStatusDisplay *d)
182{
183 if (GetFilter())
184 GetFilter()->SetDisplay(d);
185
186 MTask::SetDisplay(d);
187}
188
189// --------------------------------------------------------------------------
190//
191// Propagate log stream to filter (if set)
192//
193void MContinue::SetLogStream(MLog *lg)
194{
195 if (GetFilter())
196 GetFilter()->SetLogStream(lg);
197
198 MTask::SetLogStream(lg);
199}
200
201// --------------------------------------------------------------------------
202//
203// If the filter was added to the tasklist automatically it is removed
204// from the tasklist.
205//
206Int_t MContinue::PostProcess()
207{
208 if (!TestBit(kFilterIsPrivate))
209 return kTRUE;
210
211 if (fTaskList->RemoveFromList(GetFilter()))
212 return kTRUE;
213
214 *fLog << err << "ERROR: MContinue::PostProcess - Cannot remove Filter from tasklist" << endl;
215
216 return kFALSE;
217}
218
219// --------------------------------------------------------------------------
220//
221// If a filter is setup, call its 'IsInverted' to invert its meaning
222// (boolean "not")
223//
224void MContinue::SetInverted(Bool_t i)
225{
226 if (GetFilter())
227 GetFilter()->SetInverted(i);
228}
229
230// --------------------------------------------------------------------------
231//
232// If a filter is setup, its IsInverted status is returned. If now filter
233// has been setup yet, kFALSE is returned.
234//
235Bool_t MContinue::IsInverted() const
236{
237 return GetFilter() ? GetFilter()->IsInverted() : kFALSE;
238}
239
240void MContinue::Print(Option_t *) const
241{
242 *fLog << all << GetDescriptor() << ":";
243 if (GetFilter())
244 *fLog << " <" << GetFilter()->GetDescriptor() << ">";
245 *fLog << endl;
246 if (GetFilter())
247 GetFilter()->Print();
248}
249
250// --------------------------------------------------------------------------
251//
252// Check for corresponding entries in resource file and setup filters.
253// Avoid trailing 0's!
254//
255// Example:
256// test.C:
257// MContinue cont("", "MyContinue");
258//
259// test.rc:
260// MyContinue.Condition: {0} && {1}
261// MyContinue.Inverted: yes
262// MyContinue.0: MHillas.fSize>1000
263// MyContinue.1: MHillas.fSize<10000
264// or (the syntax might change in the future!)
265// MyContinue.Condition: <MMyClass>
266// MMyClass.Variable1: ...
267// MMyClass.Variable2: ...
268//
269// For more details see MF::ReadEnv
270//
271Int_t MContinue::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
272{
273 MFilter *f = GetFilter();
274 if (IsEnvDefined(env, prefix, "Condition", print))
275 {
276 TString txt = GetEnvValue(env, prefix, "Condition", "");
277 txt = txt.Strip(TString::kBoth);
278 if (txt.BeginsWith("<") && txt.EndsWith(">"))
279 {
280 const TString name = txt(1, txt.Length()-2);
281 f = (MFilter*)GetNewObject(name, MFilter::Class());
282 if (!f)
283 return kERROR;
284 }
285 }
286
287 if (!f)
288 f = new MF;
289
290 f->SetName(fName);
291
292 const Bool_t rc = f->ReadEnv(env, prefix, print);
293
294 if (rc!=kTRUE)
295 {
296 if (f!=GetFilter())
297 delete f;
298 return rc;
299 }
300
301 if (f != GetFilter())
302 {
303 if (TestBit(kIsOwner))
304 delete GetFilter();
305 SetBit(kIsOwner);
306 }
307
308 MTask::SetFilter(f);
309
310 return kTRUE;
311}
Note: See TracBrowser for help on using the repository browser.