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

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