source: trunk/MagicSoft/Mars/mbase/MContinue.cc@ 9301

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