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

Last change on this file since 1474 was 1474, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 15.0 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 {
166 if (!isalnum(txt[i]) && txt[i]!='.' &&
167 ((txt[i]!='-' && txt[i]!='+') || i!=0))
168 return i;
169 }
170
171 return l;
172}
173
174Int_t MDataChain::GetBracket(TString txt)
175{
176 Int_t first=1;
177 for (int cnt=0; first<txt.Length(); first++)
178 {
179 if (txt[first]=='(')
180 cnt++;
181 if (txt[first]==')')
182 cnt--;
183 if (cnt==-1)
184 break;
185 }
186 return first;
187}
188
189// --------------------------------------------------------------------------
190//
191// Compare a given text with the known operators. If no operator match
192// kENoop is retunred, otherwise the corresponding OperatorType_t
193//
194MDataChain::OperatorType_t MDataChain::ParseOperator(TString txt) const
195{
196 txt.ToLower();
197
198 if (txt=="abs") return kEAbs;
199 if (txt=="log") return kELog;
200 if (txt=="log10") return kELog10;
201 if (txt=="sin") return kESin;
202 if (txt=="cos") return kECos;
203 if (txt=="tan") return kETan;
204 if (txt=="sinh") return kESinH;
205 if (txt=="cosh") return kECosH;
206 if (txt=="tanh") return kETanH;
207 if (txt=="asin") return kEASin;
208 if (txt=="acos") return kEACos;
209 if (txt=="atan") return kEATan;
210 if (txt=="sqrt") return kESqrt;
211 if (txt=="exp") return kEExp;
212 if (txt=="pow10") return kEPow10;
213 if (txt=="sgn") return kESgn;
214 if (txt[0]=='-') return kENegative;
215 if (txt[0]=='+') return kEPositive;
216
217 return kENoop;
218}
219
220MData *MDataChain::ParseString(TString txt, Int_t level)
221{
222 MData *member0=NULL;
223
224 char type=0;
225 int nlist = 0;
226
227 while (!txt.IsNull())
228 {
229 MData *newmember = NULL;
230
231 txt = txt.Strip(TString::kBoth);
232
233 switch (txt[0])
234 {
235 case '(':
236 {
237 //
238 // Search for the corresponding bracket
239 //
240 Int_t first=GetBracket(txt);
241
242 if (first==txt.Length())
243 {
244 *fLog << err << dbginf << "Syntax Error: ')' missing." << endl;
245 if (member0)
246 delete member0;
247 return NULL;
248 }
249
250 //
251 // Make a copy of the 'interieur' and delete the substringä
252 // including the brackets
253 //
254 TString sub = txt(1, first-1);
255 txt.Remove(0, first+1);
256
257 //
258 // Parse the substring
259 //
260 newmember = ParseString(sub, level+1);
261 if (!newmember)
262 {
263 *fLog << err << dbginf << "Parsing '" << sub << "' failed." << endl;
264 if (member0)
265 delete member0;
266 return NULL;
267 }
268 }
269 break;
270
271 case ')':
272 *fLog << err << dbginf << "Syntax Error: Too many ')'" << endl;
273 if (member0)
274 delete member0;
275 return NULL;
276
277 case '+':
278 case '-':
279 case '*':
280 case '/':
281 if (member0)
282 {
283 //
284 // Check for the type of the symbol
285 //
286 char is = txt[0];
287 txt.Remove(0, 1);
288
289 //
290 // If no filter is available or the available filter
291 // is of a different symbols we have to create a new
292 // data list with the new symbol
293 //
294 if (/*!member0->InheritsFrom(MDataMember::Class()) ||*/ type!=is)
295 {
296 MDataList *list = new MDataList(is);
297 list->SetName(Form("List_%c_%d", is, 10*level+nlist++));
298
299 list->SetOwner();
300 list->AddToList(member0);
301
302 member0 = list;
303
304 type = is;
305 }
306 continue;
307 }
308
309 if (txt[0]!='-' && txt[0]!='+')
310 {
311 *fLog << err << dbginf << "Syntax Error: First argument of symbol '";
312 *fLog << txt[0] << "' missing." << endl;
313 if (member0)
314 delete member0;
315 return NULL;
316 }
317
318 // FALLTHROU
319
320 case '0':
321 case '1':
322 case '2':
323 case '3':
324 case '4':
325 case '5':
326 case '6':
327 case '7':
328 case '8':
329 case '9':
330 if ((txt[0]!='-' && txt[0]!='+') || isdigit(txt[1]) || txt[1]=='.')
331 {
332 char *end;
333 Double_t num = strtod(txt.Data(), &end);
334 if (!end || txt.Data()==end)
335 {
336 *fLog << err << dbginf << "Error trying to convert '" << txt << "' to value." << endl;
337 if (member0)
338 delete member0;
339 return NULL;
340 }
341
342 txt.Remove(0, end-txt.Data());
343
344 newmember = new MDataValue(num);
345 break;
346 }
347
348 // FALLTHROUH
349
350 default:
351 int i = IsAlNum(txt);
352
353 if (i==0)
354 {
355 *fLog << err << dbginf << "Syntax Error: Name of data member missing in '" << txt << "'" << endl;
356 if (member0)
357 delete member0;
358 return NULL;
359 }
360
361 TString text = txt(0, i);
362
363 txt.Remove(0, i);
364 txt = txt.Strip(TString::kBoth);
365
366 if ((txt.IsNull() || txt[0]!='(') && text[0]!='-' && text[0]!='+')
367 {
368 newmember = new MDataMember(text.Data());
369 break;
370 }
371
372 OperatorType_t op = ParseOperator(text);
373 if (op==kENoop)
374 {
375 *fLog << err << dbginf << "Syntax Error: Operator '" << text << "' unknown." << endl;
376 if (member0)
377 delete member0;
378 return NULL;
379 }
380
381 Int_t first = GetBracket(txt);
382 TString sub = op==kENegative || op==kEPositive ? text.Remove(0,1) + txt : txt(1, first-1);
383 txt.Remove(0, first+1);
384
385 newmember = new MDataChain(sub, op);
386
387 if (!newmember->IsValid())
388 {
389 *fLog << err << dbginf << "Syntax Error: Error parsing contents '" << sub << "' of operator " << text << endl;
390 delete newmember;
391 if (member0)
392 delete member0;
393 return NULL;
394 }
395 }
396
397 if (!member0)
398 {
399 member0 = newmember;
400 continue;
401 }
402
403 if (!member0->InheritsFrom(MDataList::Class()))
404 continue;
405
406 ((MDataList*)member0)->AddToList(newmember);
407 }
408
409 return member0;
410}
411
412Double_t MDataChain::GetValue() const
413{
414 if (!fMember)
415 {
416 *fLog << warn << "MDataChain not valid." << endl;
417 return 0;
418 }
419
420 const Double_t val = fMember->GetValue();
421
422 switch (fOperatorType)
423 {
424 case kEAbs: return fabs(val);
425 case kELog: return log(val);
426 case kELog10: return log10(val);
427 case kESin: return sin(val);
428 case kECos: return cos(val);
429 case kETan: return tan(val);
430 case kESinH: return sinh(val);
431 case kECosH: return cosh(val);
432 case kETanH: return tanh(val);
433 case kEASin: return asin(val);
434 case kEACos: return acos(val);
435 case kEATan: return atan(val);
436 case kESqrt: return sqrt(val);
437 case kEExp: return exp(val);
438 case kEPow10: return pow(10, val);
439 case kESgn: return val<0 ? -1 : 1;
440 case kENegative: return -val;
441 case kEPositive: return val;
442 case kENoop: return val;
443 }
444
445 *fLog << warn << "No Case for " << fOperatorType << " available." << endl;
446
447 return 0;
448}
449
450 /*
451void MDataChain::Print(Option_t *opt) const
452{
453 *fLog << GetRule() << flush;
454 Bool_t bracket = fOperatorType!=kENoop && !fMember->InheritsFrom(MDataList::Class());
455
456 switch (fOperatorType)
457 {
458 case kEAbs: *fLog << "abs" << flush; break;
459 case kELog: *fLog << "log" << flush; break;
460 case kELog10: *fLog << "log10" << flush; break;
461 case kESin: *fLog << "sin" << flush; break;
462 case kECos: *fLog << "cos" << flush; break;
463 case kETan: *fLog << "tan" << flush; break;
464 case kESinH: *fLog << "sinh" << flush; break;
465 case kECosH: *fLog << "cosh" << flush; break;
466 case kETanH: *fLog << "tanh" << flush; break;
467 case kEASin: *fLog << "asin" << flush; break;
468 case kEACos: *fLog << "acos" << flush; break;
469 case kEATan: *fLog << "atan" << flush; break;
470 case kESqrt: *fLog << "sqrt" << flush; break;
471 case kEExp: *fLog << "exp" << flush; break;
472 case kEPow10: *fLog << "pow10" << flush; break;
473 case kESgn: *fLog << "sgn" << flush; break;
474 case kENegative: *fLog << "-" << flush; break;
475 case kEPositive: *fLog << "+" << flush; break;
476 case kENoop:
477 break;
478 }
479
480 if (bracket)
481 *fLog << "(" << flush;
482
483 fMember->Print();
484
485 if (bracket)
486 *fLog << ")" << flush;
487 }
488 */
489
490TString MDataChain::GetRule() const
491{
492 TString str;
493
494 Bool_t bracket = fOperatorType!=kENoop && !fMember->InheritsFrom(MDataList::Class());
495
496 switch (fOperatorType)
497 {
498 case kEAbs: str += "abs" ; break;
499 case kELog: str += "log" ; break;
500 case kELog10: str += "log10" ; break;
501 case kESin: str += "sin" ; break;
502 case kECos: str += "cos" ; break;
503 case kETan: str += "tan" ; break;
504 case kESinH: str += "sinh" ; break;
505 case kECosH: str += "cosh" ; break;
506 case kETanH: str += "tanh" ; break;
507 case kEASin: str += "asin" ; break;
508 case kEACos: str += "acos" ; break;
509 case kEATan: str += "atan" ; break;
510 case kESqrt: str += "sqrt" ; break;
511 case kEExp: str += "exp" ; break;
512 case kEPow10: str += "pow10" ; break;
513 case kESgn: str += "sgn" ; break;
514 case kENegative: str += "-" ; break;
515 case kEPositive: str += "+" ; break;
516 case kENoop:
517 break;
518 }
519
520 if (bracket)
521 str += "(";
522
523 str += fMember->GetRule();
524
525 if (bracket)
526 str += ")";
527
528 return str;
529}
Note: See TracBrowser for help on using the repository browser.