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

Last change on this file since 1656 was 1629, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 16.8 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-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// floor(x) round down to the nearest integer (floor(9.9)=9)
73//
74//
75// FIXME: The possibility to use other objects inheriting from MData
76// is missing.
77// Maybe we can use gInterpreter->Calc("") for this.
78// gROOT->ProcessLineFast("line");
79//
80/////////////////////////////////////////////////////////////////////////////
81
82#include "MDataChain.h"
83
84#include <math.h> // fabs on Alpha
85#include <ctype.h> // isalnum, ...
86#include <stdlib.h> // strtod, ...
87
88#include "MLog.h"
89#include "MLogManip.h"
90
91#include "MDataList.h"
92#include "MDataValue.h"
93#include "MDataMember.h"
94#include "MDataElement.h"
95
96ClassImp(MDataChain);
97
98// --------------------------------------------------------------------------
99//
100// Constructor which takes a rule and a surrounding operator as argument
101//
102MDataChain::MDataChain(const char *rule, OperatorType_t op)
103 : fOperatorType(op)
104{
105 fName = "MDataChain";
106 fTitle = rule;
107
108 fMember=ParseString(rule, 1);
109}
110
111// --------------------------------------------------------------------------
112//
113// Default constructor
114//
115MDataChain::MDataChain()
116 : fMember(NULL), fOperatorType(kENoop)
117{
118}
119
120// --------------------------------------------------------------------------
121//
122// Constructor taking a rule as an argument. For more details see
123// class description
124//
125MDataChain::MDataChain(const char *rule, const char *name, const char *title)
126 : fOperatorType(kENoop)
127{
128 fName = name ? name : "MDataChain";
129 fTitle = title ? title : rule;
130
131 *fLog << inf << "Trying to resolve rule... " << flush;
132 if (!(fMember=ParseString(rule, 1)))
133 {
134 *fLog << err << dbginf << "Parsing '" << rule << "' failed." << endl;
135 return;
136 }
137 *fLog << inf << "found: " << flush;
138 fMember->Print();
139 *fLog << endl;
140}
141
142// --------------------------------------------------------------------------
143//
144// PreProcesses all members in the list
145//
146Bool_t MDataChain::PreProcess(const MParList *pList)
147{
148 return fMember ? fMember->PreProcess(pList) : kFALSE;
149}
150
151// --------------------------------------------------------------------------
152//
153// Checks whether at least one member has the ready-to-save flag.
154//
155Bool_t MDataChain::IsReadyToSave() const
156{
157 *fLog << all << "fM=" << fMember << "/" << (int)fMember->IsReadyToSave() << " " << endl;
158 return fMember ? fMember->IsReadyToSave() : kFALSE;
159}
160
161// --------------------------------------------------------------------------
162//
163// Destructor. Delete filters.
164//
165MDataChain::~MDataChain()
166{
167 if (fMember)
168 delete fMember;
169}
170
171// --------------------------------------------------------------------------
172//
173// Returns the number of alphanumeric characters (including '.')
174// in the given string
175//
176Int_t MDataChain::IsAlNum(TString txt)
177{
178 int l = txt.Length();
179 for (int i=0; i<l; i++)
180 {
181 if (!isalnum(txt[i]) && txt[i]!='.' && /*txt[i]!='['&&txt[i]!=']'&&*/
182 ((txt[i]!='-' && txt[i]!='+') || i!=0))
183 return i;
184 }
185
186 return l;
187}
188
189Int_t MDataChain::GetBracket(TString txt, char open, char close)
190{
191 Int_t first=1;
192 for (int cnt=0; first<txt.Length(); first++)
193 {
194 if (txt[first]==open)
195 cnt++;
196 if (txt[first]==close)
197 cnt--;
198 if (cnt==-1)
199 break;
200 }
201 return first;
202}
203
204// --------------------------------------------------------------------------
205//
206// Compare a given text with the known operators. If no operator match
207// kENoop is retunred, otherwise the corresponding OperatorType_t
208//
209MDataChain::OperatorType_t MDataChain::ParseOperator(TString txt) const
210{
211 txt.ToLower();
212
213 if (txt=="abs") return kEAbs;
214 if (txt=="fabs") return kEAbs;
215 if (txt=="log") return kELog;
216 if (txt=="log10") return kELog10;
217 if (txt=="sin") return kESin;
218 if (txt=="cos") return kECos;
219 if (txt=="tan") return kETan;
220 if (txt=="sinh") return kESinH;
221 if (txt=="cosh") return kECosH;
222 if (txt=="tanh") return kETanH;
223 if (txt=="asin") return kEASin;
224 if (txt=="acos") return kEACos;
225 if (txt=="atan") return kEATan;
226 if (txt=="sqrt") return kESqrt;
227 if (txt=="exp") return kEExp;
228 if (txt=="pow10") return kEPow10;
229 if (txt=="sgn") return kESgn;
230 if (txt=="floor") return kEFloor;
231 if (txt[0]=='-') return kENegative;
232 if (txt[0]=='+') return kEPositive;
233
234 return kENoop;
235}
236
237// --------------------------------------------------------------------------
238//
239// Core of the data chain. Here the chain is constructed out of the rule.
240//
241MData *MDataChain::ParseString(TString txt, Int_t level)
242{
243 MData *member0=NULL;
244
245 char type=0;
246 int nlist = 0;
247
248 while (!txt.IsNull())
249 {
250 MData *newmember = NULL;
251
252 txt = txt.Strip(TString::kBoth);
253
254 switch (txt[0])
255 {
256 case '(':
257 {
258 //
259 // Search for the corresponding bracket
260 //
261 Int_t first=GetBracket(txt, '(', ')');
262
263 if (first==txt.Length())
264 {
265 *fLog << err << dbginf << "Syntax Error: ')' missing." << endl;
266 if (member0)
267 delete member0;
268 return NULL;
269 }
270
271 //
272 // Make a copy of the 'interieur' and delete the substringä
273 // including the brackets
274 //
275 TString sub = txt(1, first-1);
276 txt.Remove(0, first+1);
277
278 //
279 // Parse the substring
280 //
281 newmember = ParseString(sub, level+1);
282 if (!newmember)
283 {
284 *fLog << err << dbginf << "Parsing '" << sub << "' failed." << endl;
285 if (member0)
286 delete member0;
287 return NULL;
288 }
289 }
290 break;
291
292 case ')':
293 *fLog << err << dbginf << "Syntax Error: Too many ')'" << endl;
294 if (member0)
295 delete member0;
296 return NULL;
297
298 case '+':
299 case '-':
300 case '*':
301 case '/':
302 if (member0)
303 {
304 //
305 // Check for the type of the symbol
306 //
307 char is = txt[0];
308 txt.Remove(0, 1);
309
310 //
311 // If no filter is available or the available filter
312 // is of a different symbols we have to create a new
313 // data list with the new symbol
314 //
315 if (/*!member0->InheritsFrom(MDataMember::Class()) ||*/ type!=is)
316 {
317 MDataList *list = new MDataList(is);
318 list->SetName(Form("List_%c_%d", is, 10*level+nlist++));
319
320 list->SetOwner();
321 list->AddToList(member0);
322
323 member0 = list;
324
325 type = is;
326 }
327 continue;
328 }
329
330 if (txt[0]!='-' && txt[0]!='+')
331 {
332 *fLog << err << dbginf << "Syntax Error: First argument of symbol '";
333 *fLog << txt[0] << "' missing." << endl;
334 if (member0)
335 delete member0;
336 return NULL;
337 }
338
339 // FALLTHROU
340
341 case '0':
342 case '1':
343 case '2':
344 case '3':
345 case '4':
346 case '5':
347 case '6':
348 case '7':
349 case '8':
350 case '9':
351 if ((txt[0]!='-' && txt[0]!='+') || isdigit(txt[1]) || txt[1]=='.')
352 {
353 char *end;
354 Double_t num = strtod(txt.Data(), &end);
355 if (!end || txt.Data()==end)
356 {
357 *fLog << err << dbginf << "Error trying to convert '" << txt << "' to value." << endl;
358 if (member0)
359 delete member0;
360 return NULL;
361 }
362
363 txt.Remove(0, end-txt.Data());
364
365 newmember = new MDataValue(num);
366 break;
367 }
368
369 // FALLTHROUH
370
371 default:
372 int i = IsAlNum(txt);
373
374 if (i==0)
375 {
376 *fLog << err << dbginf << "Syntax Error: Name of data member missing in '" << txt << "'" << endl;
377 if (member0)
378 delete member0;
379 return NULL;
380 }
381
382 TString text = txt(0, i);
383
384 txt.Remove(0, i);
385 txt = txt.Strip(TString::kBoth);
386
387 if (!txt.IsNull() && txt[0]=='[')
388 {
389 Int_t first = GetBracket(txt, '[', ']');
390 TString op = txt(1, first-1);
391 txt.Remove(0, first+1);
392
393 newmember = new MDataElement(text, atoi(op));
394 break;
395 }
396 if ((txt.IsNull() || txt[0]!='(') && text[0]!='-' && text[0]!='+')
397 {
398 newmember = new MDataMember(text.Data());
399 break;
400 }
401
402 OperatorType_t op = ParseOperator(text);
403 if (op==kENoop)
404 {
405 *fLog << err << dbginf << "Syntax Error: Operator '" << text << "' unknown." << endl;
406 if (member0)
407 delete member0;
408 return NULL;
409 }
410
411 Int_t first = GetBracket(txt, '(', ')');
412 TString sub = op==kENegative || op==kEPositive ? text.Remove(0,1) + txt : txt(1, first-1);
413 txt.Remove(0, first+1);
414
415 newmember = new MDataChain(sub, op);
416 if (!newmember->IsValid())
417 {
418 *fLog << err << dbginf << "Syntax Error: Error parsing contents '" << sub << "' of operator " << text << endl;
419 delete newmember;
420 if (member0)
421 delete member0;
422 return NULL;
423 }
424 }
425
426 if (!member0)
427 {
428 member0 = newmember;
429 continue;
430 }
431
432 if (!member0->InheritsFrom(MDataList::Class()))
433 continue;
434
435 ((MDataList*)member0)->AddToList(newmember);
436 }
437
438 return member0;
439}
440
441// --------------------------------------------------------------------------
442//
443// Returns the value described by the rule
444//
445Double_t MDataChain::GetValue() const
446{
447 if (!fMember)
448 {
449 *fLog << warn << "MDataChain not valid." << endl;
450 return 0;
451 }
452
453 const Double_t val = fMember->GetValue();
454
455 switch (fOperatorType)
456 {
457 case kEAbs: return fabs(val);
458 case kELog: return log(val);
459 case kELog10: return log10(val);
460 case kESin: return sin(val);
461 case kECos: return cos(val);
462 case kETan: return tan(val);
463 case kESinH: return sinh(val);
464 case kECosH: return cosh(val);
465 case kETanH: return tanh(val);
466 case kEASin: return asin(val);
467 case kEACos: return acos(val);
468 case kEATan: return atan(val);
469 case kESqrt: return sqrt(val);
470 case kEExp: return exp(val);
471 case kEPow10: return pow(10, val);
472 case kESgn: return val<0 ? -1 : 1;
473 case kENegative: return -val;
474 case kEPositive: return val;
475 case kEFloor: return floor(val);
476 case kENoop: return val;
477 }
478
479 *fLog << warn << "No Case for " << fOperatorType << " available." << endl;
480
481 return 0;
482}
483
484 /*
485void MDataChain::Print(Option_t *opt) const
486{
487 *fLog << GetRule() << flush;
488 Bool_t bracket = fOperatorType!=kENoop && !fMember->InheritsFrom(MDataList::Class());
489
490 switch (fOperatorType)
491 {
492 case kEAbs: *fLog << "abs" << flush; break;
493 case kELog: *fLog << "log" << flush; break;
494 case kELog10: *fLog << "log10" << flush; break;
495 case kESin: *fLog << "sin" << flush; break;
496 case kECos: *fLog << "cos" << flush; break;
497 case kETan: *fLog << "tan" << flush; break;
498 case kESinH: *fLog << "sinh" << flush; break;
499 case kECosH: *fLog << "cosh" << flush; break;
500 case kETanH: *fLog << "tanh" << flush; break;
501 case kEASin: *fLog << "asin" << flush; break;
502 case kEACos: *fLog << "acos" << flush; break;
503 case kEATan: *fLog << "atan" << flush; break;
504 case kESqrt: *fLog << "sqrt" << flush; break;
505 case kEExp: *fLog << "exp" << flush; break;
506 case kEPow10: *fLog << "pow10" << flush; break;
507 case kESgn: *fLog << "sgn" << flush; break;
508 case kENegative: *fLog << "-" << flush; break;
509 case kEPositive: *fLog << "+" << flush; break;
510 case kENoop:
511 break;
512 }
513
514 if (bracket)
515 *fLog << "(" << flush;
516
517 fMember->Print();
518
519 if (bracket)
520 *fLog << ")" << flush;
521 }
522 */
523
524// --------------------------------------------------------------------------
525//
526// Builds a rule from all the chain members. This is a rule which could
527// be used to rebuild the chain.
528//
529TString MDataChain::GetRule() const
530{
531 if (!fMember)
532 return "<n/a>";
533
534 TString str;
535
536 Bool_t bracket = fOperatorType!=kENoop && !fMember->InheritsFrom(MDataList::Class());
537
538 switch (fOperatorType)
539 {
540 case kEAbs: str += "abs" ; break;
541 case kELog: str += "log" ; break;
542 case kELog10: str += "log10"; break;
543 case kESin: str += "sin" ; break;
544 case kECos: str += "cos" ; break;
545 case kETan: str += "tan" ; break;
546 case kESinH: str += "sinh" ; break;
547 case kECosH: str += "cosh" ; break;
548 case kETanH: str += "tanh" ; break;
549 case kEASin: str += "asin" ; break;
550 case kEACos: str += "acos" ; break;
551 case kEATan: str += "atan" ; break;
552 case kESqrt: str += "sqrt" ; break;
553 case kEExp: str += "exp" ; break;
554 case kEPow10: str += "pow10"; break;
555 case kESgn: str += "sgn" ; break;
556 case kENegative: str += "-" ; break;
557 case kEPositive: str += "+" ; break;
558 case kEFloor: str += "floor"; break;
559 case kENoop:
560 break;
561 }
562
563 if (bracket)
564 str += "(";
565
566 str += fMember->GetRule();
567
568 if (bracket)
569 str += ")";
570
571 return str;
572}
573
574// --------------------------------------------------------------------------
575//
576// Return a comma seperated list of all data members used in the chain.
577// This is mainly used in MTask::AddToBranchList
578//
579TString MDataChain::GetDataMember() const
580{
581 return fMember->GetDataMember();
582}
Note: See TracBrowser for help on using the repository browser.