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

Last change on this file since 7184 was 7058, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 8.3 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 ((TString)GetFilter()->GetName()==fName)
151 GetFilter()->SetName(Form("MF:%s", fName.Data()));
152
153 if (!fTaskList->AddToListBefore(GetFilter(), this))
154 {
155 *fLog << err << dbginf << "ERROR - Adding filter before MContinue... abort." << endl;
156 return kFALSE;
157 }
158
159 // Make sure, that everything is correctly propageted to the childs
160 GetFilter()->SetDisplay(fDisplay);
161 GetFilter()->SetLogStream(fLog);
162
163 // Remeber that the filter is not already in tasklist
164 SetBit(kFilterIsPrivate);
165
166 return GetFilter()->CallPreProcess(list);
167}
168
169// --------------------------------------------------------------------------
170//
171// Propagate display to filter (if set)
172//
173void MContinue::SetDisplay(MStatusDisplay *d)
174{
175 if (GetFilter())
176 GetFilter()->SetDisplay(d);
177
178 MTask::SetDisplay(d);
179}
180
181// --------------------------------------------------------------------------
182//
183// Propagate log stream to filter (if set)
184//
185void MContinue::SetLogStream(MLog *lg)
186{
187 if (GetFilter())
188 GetFilter()->SetLogStream(lg);
189
190 MTask::SetLogStream(lg);
191}
192
193// --------------------------------------------------------------------------
194//
195// If the filter was added to the tasklist automatically it is removed
196// from the tasklist.
197//
198Int_t MContinue::PostProcess()
199{
200 if (!TestBit(kFilterIsPrivate))
201 return kTRUE;
202
203 if (fTaskList->RemoveFromList(GetFilter()))
204 return kTRUE;
205
206 *fLog << err << "ERROR: MContinue::PostProcess - Cannot remove Filter from tasklist" << endl;
207
208 return kFALSE;
209}
210
211// --------------------------------------------------------------------------
212//
213// If a filter is setup, call its 'IsInverted' to invert its meaning
214// (boolean "not")
215//
216void MContinue::SetInverted(Bool_t i)
217{
218 if (GetFilter())
219 GetFilter()->SetInverted(i);
220}
221
222// --------------------------------------------------------------------------
223//
224// If a filter is setup, its IsInverted status is returned. If now filter
225// has been setup yet, kFALSE is returned.
226//
227Bool_t MContinue::IsInverted() const
228{
229 return GetFilter() ? GetFilter()->IsInverted() : kFALSE;
230}
231
232void MContinue::Print(Option_t *o) const
233{
234 *fLog << all << GetDescriptor() << ":";
235 if (GetFilter())
236 *fLog << " <" << GetFilter()->GetDescriptor() << ">";
237 *fLog << endl;
238 if (GetFilter())
239 GetFilter()->Print();
240}
241
242// --------------------------------------------------------------------------
243//
244// Check for corresponding entries in resource file and setup filters.
245// Avoid trailing 0's!
246//
247// Example:
248// test.C:
249// MContinue cont("", "MyContinue");
250//
251// test.rc:
252// MyContinue.Condition: {0} && {1}
253// MyContinue.Inverted: yes
254// MyContinue.0: MHillas.fSize>1000
255// MyContinue.1: MHillas.fSize<10000
256// or (the syntax might change in the future!)
257// MyContinue.Condition: <MMyClass>
258// MMyClass.Variable1: ...
259// MMyClass.Variable2: ...
260//
261// For more details see MF::ReadEnv
262//
263Int_t MContinue::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
264{
265 MFilter *f = MTask::GetFilter();
266 if (IsEnvDefined(env, prefix, "Condition", print))
267 {
268 TString txt = GetEnvValue(env, prefix, "Condition", "");
269 txt = txt.Strip(TString::kBoth);
270 if (txt.BeginsWith("<") && txt.EndsWith(">"))
271 {
272 const TString name = txt(1, txt.Length()-2);
273 f = (MFilter*)GetNewObject(name, MFilter::Class());
274 if (!f)
275 return kERROR;
276 }
277 }
278
279 if (!f)
280 f = new MF;
281
282 f->SetName(fName);
283
284 const Bool_t rc = f->ReadEnv(env, prefix, print);
285 if (rc!=kTRUE)
286 {
287 if (f!=MTask::GetFilter())
288 delete f;
289 return rc;
290 }
291
292 if (f != MTask::GetFilter())
293 {
294 if (TestBit(kIsOwner))
295 delete GetFilter();
296 SetBit(kIsOwner);
297 }
298 MTask::SetFilter(f);
299
300 return kTRUE;
301}
Note: See TracBrowser for help on using the repository browser.