source: trunk/MagicSoft/Mars/mdata/MDataList.cc@ 4808

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