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

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