source: trunk/Mars/mdata/MDataList.cc@ 10113

Last change on this file since 10113 was 8907, checked in by tbretz, 17 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 04/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MDataList
28//
29/////////////////////////////////////////////////////////////////////////////
30
31#include "MDataList.h"
32
33#include <math.h>
34
35#include "MLog.h"
36#include "MLogManip.h"
37
38ClassImp(MDataList);
39
40using namespace std;
41
42// --------------------------------------------------------------------------
43//
44// Default Constructor. Not for usage!
45//
46MDataList::MDataList()
47{
48 fSign = kENone;
49
50 gROOT->GetListOfCleanups()->Add(&fMembers);
51 fMembers.SetBit(kMustCleanup);
52}
53
54// --------------------------------------------------------------------------
55//
56// Constructor.
57//
58// Specify the operation which is used to evaluate the
59// result of this list.
60//
61// Options:
62// *, /, -, +
63//
64MDataList::MDataList(char type)
65{
66 switch (type)
67 {
68 case '*':
69 fSign = kEMult;
70 return;
71 case '/':
72 fSign = kEDiv;
73 return;
74 case '-':
75 fSign = kEMinus;
76 return;
77 case '+':
78 fSign = kEPlus;
79 return;
80 case '%':
81 fSign = kEModul;
82 return;
83 case '^':
84 fSign = kEPow;
85 return;
86 default:
87 fSign = kENone;
88 }
89
90 gROOT->GetListOfCleanups()->Add(&fMembers);
91 fMembers.SetBit(kMustCleanup);
92}
93
94// --------------------------------------------------------------------------
95//
96// CopyConstructor
97//
98MDataList::MDataList(MDataList &ts)
99{
100 fMembers.AddAll(&ts.fMembers);
101 fSign = ts.fSign;
102}
103
104// --------------------------------------------------------------------------
105//
106// Evaluates and returns the result of the member list.
107// The expression is evaluated step by step, eg:
108// ((member[0] # member[1]) # member[3]) # member[4])
109// The '#' stands for the boolean operation which is specified in
110// the constructor.
111//
112Double_t MDataList::GetValue() const
113{
114 TIter Next(&fMembers);
115
116 MData *member=(MData*)Next();
117
118 if (!member)
119 return kTRUE;
120
121 Double_t val = member->GetValue();
122
123 //
124 // loop over all members
125 //
126 switch (fSign)
127 {
128 case kENone:
129 return 0;
130
131 case kEPlus:
132 while ((member=(MData*)Next()))
133 val += member->GetValue();
134 break;
135
136 case kEMinus:
137 while ((member=(MData*)Next()))
138 val -= member->GetValue();
139 break;
140
141 case kEMult:
142 while ((member=(MData*)Next()))
143 val *= member->GetValue();
144 break;
145
146 case kEDiv:
147 while ((member=(MData*)Next()))
148 {
149 const Double_t d = member->GetValue();
150 if (d==0)
151 {
152 *fLog << warn << "Warning: Division by zero: " << member->GetRule() << endl;
153 return 0;
154 }
155 val /= d;
156 }
157 break;
158
159 case kEModul:
160 while ((member=(MData*)Next()))
161 {
162 const Double_t d = member->GetValue();
163 if (d==0)
164 {
165 *fLog << warn << "Warning: Modulo division by zero: " << member->GetRule() << endl;
166 return 0;
167 }
168 val = fmod(val, d);
169 }
170 break;
171 case kEPow:
172 while ((member=(MData*)Next()))
173 val = pow(val, member->GetValue());
174 break;
175 }
176 return val;
177}
178
179// --------------------------------------------------------------------------
180//
181// Checks whether at least one member has the ready-to-save flag.
182//
183Bool_t MDataList::IsReadyToSave() const
184{
185 TIter Next(&fMembers);
186
187 MData *data = NULL;
188
189 while ((data=(MData*)Next()))
190 if (data->IsReadyToSave())
191 return kTRUE;
192
193 return kFALSE;
194}
195
196Bool_t MDataList::IsValid() const
197{
198 TIter Next(&fMembers);
199
200 MData *data = NULL;
201 while ((data=(MData*)Next()))
202 if (!data->IsValid())
203 return kFALSE;
204
205 return kTRUE;
206}
207
208// --------------------------------------------------------------------------
209//
210// If you want to add a new member to the list call this function with the
211// pointer to the member to be added.
212//
213Bool_t MDataList::AddToList(MData *member)
214{
215 if (!member)
216 return kTRUE;
217
218 if (fMembers.FindObject(member))
219 {
220 *fLog << warn << dbginf << "Filter already existing... skipped." << endl;
221 return kTRUE;
222 }
223
224 member->SetBit(kMustCleanup);
225 fMembers.Add(member);
226
227 return kTRUE;
228}
229
230// --------------------------------------------------------------------------
231//
232// PreProcesses all members in the list
233//
234Bool_t MDataList::PreProcess(const MParList *plist)
235{
236 TIter Next(&fMembers);
237
238 MData *member=NULL;
239
240 //
241 // loop over all members
242 //
243 while ((member=(MData*)Next()))
244 if (!member->PreProcess(plist))
245 {
246 *fLog << err << "Error - Preprocessing Data Member ";
247 *fLog << member->GetName() << " in " << fName << endl;
248 return kFALSE;
249 }
250
251 return kTRUE;
252}
253
254
255// --------------------------------------------------------------------------
256//
257// If you want to use a verbose output ("and") instead of a symbolic ("&")
258// one the option string must conatin a "v"
259//
260/*
261void MDataList::Print(Option_t *opt) const
262{
263 *fLog << "(";
264
265 TIter Next(&fMembers);
266
267 TObject *member=Next();
268
269 //
270 // loop over all members
271 //
272 if (!member)
273 {
274 *fLog << "<empty>)" << flush;
275 return;
276 }
277
278 member->Print();
279
280 while ((member=Next()))
281 {
282 switch (fSign)
283 {
284 case kENone:
285 break;
286
287 case kEPlus:
288 *fLog << "+";
289 break;
290
291 case kEMinus:
292 *fLog << "-";
293 break;
294
295 case kEMult:
296 *fLog << "*";
297 break;
298
299 case kEDiv:
300 *fLog << "/";
301 break;
302 }
303
304 member->Print();
305 }
306
307 *fLog << ")" << flush;
308 }
309 */
310
311// --------------------------------------------------------------------------
312//
313// Builds a rule from all the list members. This is a rule which could
314// be used to rebuild the list using the constructor of a MDataPhrase
315//
316TString MDataList::GetRule() const
317{
318 TIter Next(&fMembers);
319
320 MData *member=(MData*)Next();
321
322 //
323 // loop over all members
324 //
325 if (!member)
326 return "(<empty>)";
327
328 TString str = "(";
329
330 str += member->GetRule();
331
332 while ((member=(MData*)Next()))
333 {
334 switch (fSign)
335 {
336 case kENone:
337 break;
338
339 case kEPlus:
340 str += "+";
341 break;
342
343 case kEMinus:
344 str += "-";
345 break;
346
347 case kEMult:
348 str += "*";
349 break;
350
351 case kEDiv:
352 str += "/";
353 break;
354
355 case kEModul:
356 str += "%";
357 break;
358
359 case kEPow:
360 str += "^";
361 break;
362 }
363
364 str += member->GetRule();
365 }
366
367 str += ")";
368
369 return str;
370}
371
372// --------------------------------------------------------------------------
373//
374// Return a comma seperated list of all data members used in the chain.
375// This is mainly used in MTask::AddToBranchList
376//
377TString MDataList::GetDataMember() const
378{
379 TString str;
380
381 TIter Next(&fMembers);
382
383 MData *member=(MData*)Next();
384
385 if (!member->GetDataMember().IsNull())
386 str += member->GetDataMember();
387
388 while ((member=(MData*)Next()))
389 {
390 if (!member->GetDataMember().IsNull())
391 {
392 str += ",";
393 str += member->GetDataMember();
394 }
395 }
396
397 return str;
398}
399
400void MDataList::SetVariables(const TArrayD &arr)
401{
402 fMembers.R__FOR_EACH(MData, SetVariables)(arr);
403}
Note: See TracBrowser for help on using the repository browser.