source: trunk/MagicSoft/Mars/mdata/MDataChain.cc@ 1348

Last change on this file since 1348 was 1348, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 12.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 04/2002 <mailto:tbretz@uni-sw.gwdg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2002
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MDataChain
28//
29// With this chain you can concatenate simple mathematical operations on
30// members of mars containers.
31//
32// In the constructor you can give rule, like
33// "HillasSource.fDist / MHillas.fLength"
34// Where MHillas/HillasSource is the name of the parameter container in
35// the parameter list and fDist/fLength is the name of the data members
36// in the containers. The result will be fDist divided by fLength.
37//
38// You can also use brackets:
39// "HillasDource.fDist / (MHillas.fLength + MHillas.fWidth)"
40//
41// The allowed operations are: +, -, *, /
42//
43// Warning: There is no priority rule build in. So better use brackets
44// to get correct results. The rule is parsed/evaluated from the left
45// to the right, which means:
46//
47// "MHillas.fWidth + MHillas.fLength / HillasSource.fDist"
48//
49// is parses as
50//
51// "(MHillas.fWidth + MHillas.fLength) / HillasSource.fDist"
52//
53// You can also use mathmatical operators, eg:
54// "5*log10(MMcEvt.fEnergy*MHillas.fSize)"
55//
56// The allowed operators are:
57// exp(x) e^x
58// log(x) natural logarithm of x
59// pow10(x) 10^x
60// log10(x) logarithm of x to base ten
61// cos(x) cosine of x
62// sin(x) sine of x
63// tan(x) tangent of x
64// cosh(x) hyperbolic cosine of x
65// sinh(x) hyperbolic sine of x
66// tanh(x) hyperbolic tangent of x
67// acos(x) arc cosine (inverse cosine) of x
68// asin(x) arc sine (inverse sine) of x
69// atan(x) arc tangent (inverse tangent) of x
70// sqrt(x) square root of x
71// abs(x) absolute value of x, |x|
72//
73//
74// FIXME: The possibility to use other objects inheriting from MData
75// is missing.
76// Maybe we can use gInterpreter->Calc("") for this.
77// gROOT->ProcessLineFast("line");
78//
79/////////////////////////////////////////////////////////////////////////////
80
81#include "MDataChain.h"
82
83#include <math.h> // fabs on Alpha
84#include <ctype.h> // isalnum, ...
85#include <stdlib.h> // strtod, ...
86
87#include "MLog.h"
88#include "MLogManip.h"
89
90#include "MDataList.h"
91#include "MDataValue.h"
92#include "MDataMember.h"
93#
94ClassImp(MDataChain);
95
96MDataChain::MDataChain(const char *rule, OperatorType_t op)
97 : fOperatorType(op)
98{
99 fName = "MDataChain";
100 fTitle = rule;
101
102 fMember=ParseString(rule, 1);
103}
104
105MDataChain::MDataChain()
106 : fMember(NULL), fOperatorType(kENoop)
107{
108}
109
110MDataChain::MDataChain(const char *rule, const char *name, const char *title)
111 : fOperatorType(kENoop)
112{
113 fName = name ? name : "MDataChain";
114 fTitle = title ? title : rule;
115
116 *fLog << inf << "Trying to resolve rule... " << flush;
117 if (!(fMember=ParseString(rule, 1)))
118 {
119 *fLog << err << dbginf << "Parsing '" << rule << "' failed." << endl;
120 return;
121 }
122 *fLog << inf << "found: " << flush;
123 fMember->Print();
124 *fLog << endl;
125}
126
127// --------------------------------------------------------------------------
128//
129// PreProcesses all members in the list
130//
131Bool_t MDataChain::PreProcess(const MParList *pList)
132{
133 return fMember ? fMember->PreProcess(pList) : kFALSE;
134}
135
136// --------------------------------------------------------------------------
137//
138// Checks whether at least one member has the ready-to-save flag.
139//
140Bool_t MDataChain::IsReadyToSave() const
141{
142 *fLog << all << "fM=" << fMember << "/" << (int)fMember->IsReadyToSave() << " " << endl;
143 return fMember ? fMember->IsReadyToSave() : kFALSE;
144}
145
146// --------------------------------------------------------------------------
147//
148// Destructor. Delete filters.
149//
150MDataChain::~MDataChain()
151{
152 if (fMember)
153 delete fMember;
154}
155
156// --------------------------------------------------------------------------
157//
158// Returns the number of alphanumeric characters (including '.')
159// in the given string
160//
161Int_t MDataChain::IsAlNum(TString txt)
162{
163 int l = txt.Length();
164 for (int i = 0; i<l; i++)
165 if (!isalnum(txt[i]) && txt[i]!='.')
166 return i;
167
168 return l;
169}
170
171Int_t MDataChain::GetBracket(TString txt)
172{
173 Int_t first=1;
174 for (int cnt=0; first<txt.Length(); first++)
175 {
176 if (txt[first]=='(')
177 cnt++;
178 if (txt[first]==')')
179 cnt--;
180 if (cnt==-1)
181 break;
182 }
183 return first;
184}
185
186// --------------------------------------------------------------------------
187//
188// Compare a given text with the known operators. If no operator match
189// kENoop is retunred, otherwise the corresponding OperatorType_t
190//
191MDataChain::OperatorType_t MDataChain::ParseOperator(TString txt) const
192{
193 txt.ToLower();
194
195 if (txt=="abs") return kEAbs;
196 if (txt=="log") return kELog;
197 if (txt=="log10") return kELog10;
198 if (txt=="sin") return kESin;
199 if (txt=="cos") return kECos;
200 if (txt=="tan") return kETan;
201 if (txt=="sinh") return kESinH;
202 if (txt=="cosh") return kECosH;
203 if (txt=="tanh") return kETanH;
204 if (txt=="asin") return kEASin;
205 if (txt=="acos") return kEACos;
206 if (txt=="atan") return kEATan;
207 if (txt=="sqrt") return kESqrt;
208 if (txt=="exp") return kEExp;
209 if (txt=="pow10") return kEPow10;
210
211 return kENoop;
212}
213
214MData *MDataChain::ParseString(TString txt, Int_t level)
215{
216 MData *member0=NULL;
217
218 char type=0;
219 int nlist = 0;
220
221 while (!txt.IsNull())
222 {
223 MData *newmember = NULL;
224
225 txt = txt.Strip(TString::kBoth);
226
227 switch (txt[0])
228 {
229 case '(':
230 {
231 //
232 // Search for the corresponding bracket
233 //
234 Int_t first=GetBracket(txt);
235
236 if (first==txt.Length())
237 {
238 *fLog << err << dbginf << "Syntax Error: ')' missing." << endl;
239 if (member0)
240 delete member0;
241 return NULL;
242 }
243
244 //
245 // Make a copy of the 'interieur' and delete the substringä
246 // including the brackets
247 //
248 TString sub = txt(1, first-1);
249 txt.Remove(0, first+1);
250
251 //
252 // Parse the substring
253 //
254 newmember = ParseString(sub, level+1);
255 if (!newmember)
256 {
257 *fLog << err << dbginf << "Parsing '" << sub << "' failed." << endl;
258 if (member0)
259 delete member0;
260 return NULL;
261 }
262 }
263 break;
264
265 case ')':
266 *fLog << err << dbginf << "Syntax Error: Too many ')'" << endl;
267 if (member0)
268 delete member0;
269 return NULL;
270
271 case '+':
272 case '-':
273 case '*':
274 case '/':
275 if (member0)
276 {
277 //
278 // Check for the type of the conditional
279 //
280 char is = txt[0];
281 txt.Remove(0, 1);
282
283 //
284 // If no filter is available or the available filter
285 // is of a different conditional we have to create a new
286 // filter list with the new conditional
287 //
288 if (/*!member0->InheritsFrom(MDataMember::Class()) ||*/ type!=is)
289 {
290 MDataList *list = new MDataList(is);
291 list->SetName(Form("List_%c_%d", is, 10*level+nlist++));
292
293 list->SetOwner();
294 list->AddToList(member0);
295
296 member0 = list;
297
298 type = is;
299 }
300 continue;
301 }
302
303 *fLog << err << dbginf << "Syntax Error: First argument of condition missing." << endl;
304 if (member0)
305 delete member0;
306 return NULL;
307
308 case '0':
309 case '1':
310 case '2':
311 case '3':
312 case '4':
313 case '5':
314 case '6':
315 case '7':
316 case '8':
317 case '9':
318 {
319 char *end;
320 Double_t num = strtod(txt.Data(), &end);
321 if (!end || txt.Data()==end)
322 {
323 *fLog << err << dbginf << "Error trying to convert '" << txt << "' to value." << endl;
324 if (member0)
325 delete member0;
326 return NULL;
327 }
328
329 txt.Remove(0, end-txt.Data());
330
331 newmember = new MDataValue(num);
332 }
333 break;
334
335 default:
336 int i = IsAlNum(txt);
337
338 if (i==0)
339 {
340 *fLog << err << dbginf << "Syntax Error: Name of data member missing in '" << txt << "'" << endl;
341 if (member0)
342 delete member0;
343 return NULL;
344 }
345
346 TString text = txt(0, i);
347
348 txt.Remove(0, i);
349 txt = txt.Strip(TString::kBoth);
350
351 if (txt.IsNull() || txt[0]!='(')
352 {
353 newmember = new MDataMember(text.Data());
354 break;
355 }
356
357 OperatorType_t op = ParseOperator(text);
358 if (op==kENoop)
359 {
360 *fLog << err << dbginf << "Syntax Error: Operator '" << text << "' unknown." << endl;
361 if (member0)
362 delete member0;
363 return NULL;
364 }
365
366 Int_t first = GetBracket(txt);
367 TString sub = txt(1, first-1);
368 txt.Remove(0, first+1);
369
370 newmember = new MDataChain(sub, op);
371
372 if (!newmember->IsValid())
373 {
374 *fLog << err << dbginf << "Syntax Error: Error parsing contents '" << sub << "' of operator " << text << endl;
375 delete newmember;
376 if (member0)
377 delete member0;
378 return NULL;
379 }
380 }
381
382 if (!member0)
383 {
384 member0 = newmember;
385 continue;
386 }
387
388 if (!member0->InheritsFrom(MDataList::Class()))
389 continue;
390
391 ((MDataList*)member0)->AddToList(newmember);
392 }
393
394 return member0;
395}
396
397Double_t MDataChain::GetValue() const
398{
399 if (!fMember)
400 {
401 *fLog << warn << "MDataChain not valid." << endl;
402 return 0;
403 }
404
405 const Double_t val = fMember->GetValue();
406
407 switch (fOperatorType)
408 {
409 case kEAbs: return fabs(val);
410 case kELog: return log(val);
411 case kELog10: return log10(val);
412 case kESin: return sin(val);
413 case kECos: return cos(val);
414 case kETan: return tan(val);
415 case kESinH: return sinh(val);
416 case kECosH: return cosh(val);
417 case kETanH: return tanh(val);
418 case kEASin: return asin(val);
419 case kEACos: return acos(val);
420 case kEATan: return atan(val);
421 case kESqrt: return sqrt(val);
422 case kEExp: return exp(val);
423 case kEPow10: return pow(10, val);
424 case kENoop: return val;
425 }
426
427 *fLog << warn << "No Case for " << fOperatorType << " available." << endl;
428
429 return 0;
430}
431
432void MDataChain::Print(Option_t *opt) const
433{
434 Bool_t bracket = fOperatorType!=kENoop && !fMember->InheritsFrom(MDataList::Class());
435
436 switch (fOperatorType)
437 {
438 case kEAbs: *fLog << "abs" << flush; break;
439 case kELog: *fLog << "log" << flush; break;
440 case kELog10: *fLog << "log10" << flush; break;
441 case kESin: *fLog << "sin" << flush; break;
442 case kECos: *fLog << "cos" << flush; break;
443 case kETan: *fLog << "tan" << flush; break;
444 case kESinH: *fLog << "sinh" << flush; break;
445 case kECosH: *fLog << "cosh" << flush; break;
446 case kETanH: *fLog << "tanh" << flush; break;
447 case kEASin: *fLog << "asin" << flush; break;
448 case kEACos: *fLog << "acos" << flush; break;
449 case kEATan: *fLog << "atan" << flush; break;
450 case kESqrt: *fLog << "sqrt" << flush; break;
451 case kEExp: *fLog << "exp" << flush; break;
452 case kEPow10: *fLog << "pow10" << flush; break;
453 case kENoop:
454 break;
455 }
456
457 if (bracket)
458 *fLog << "(" << flush;
459
460 fMember->Print();
461
462 if (bracket)
463 *fLog << ")" << flush;
464}
Note: See TracBrowser for help on using the repository browser.