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-2005
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // MDataChain
|
---|
28 | // ==========
|
---|
29 | //
|
---|
30 | // With this chain you can concatenate simple mathematical operations on
|
---|
31 | // members of mars containers.
|
---|
32 | //
|
---|
33 | //
|
---|
34 | // Rules
|
---|
35 | // -----
|
---|
36 | //
|
---|
37 | // In the constructor you can give rule, like
|
---|
38 | // "HillasSource.fDist / MHillas.fLength"
|
---|
39 | // Where MHillas/HillasSource is the name of the parameter container in
|
---|
40 | // the parameter list and fDist/fLength is the name of the data members
|
---|
41 | // in the containers. The result will be fDist divided by fLength.
|
---|
42 | //
|
---|
43 | // In case you want to access a data-member which is a data member object
|
---|
44 | // you can acces it with (Remark: it must derive from MParContainer):
|
---|
45 | // "MCameraLV.fPowerSupplyA.fVoltagePos5V"
|
---|
46 | //
|
---|
47 | // You can also use parantheses:
|
---|
48 | // "HillasDource.fDist / (MHillas.fLength + MHillas.fWidth)"
|
---|
49 | //
|
---|
50 | //
|
---|
51 | // Operators
|
---|
52 | // ---------
|
---|
53 | //
|
---|
54 | // The allowed operations are: +, -, *, /, %, ^ and ** for ^
|
---|
55 | //
|
---|
56 | // While a%b returns the floating point reminder of a/b.
|
---|
57 | // While a^b returns a to the power of b
|
---|
58 | //
|
---|
59 | // Priority
|
---|
60 | // --------
|
---|
61 | //
|
---|
62 | // The priority of the operators is build in as:
|
---|
63 | // ^ (highest), %, *, /, +, -
|
---|
64 | //
|
---|
65 | // Priorities are evaluated before parsing the rule by inserting
|
---|
66 | // artificial paranthesis. If you find ANY problem with this please
|
---|
67 | // report it immediatly! It is highly recommended to check the result!
|
---|
68 | //
|
---|
69 | // You can also use mathmatical operators, eg:
|
---|
70 | // "5*log10(MMcEvt.fEnergy*MHillas.fSize)"
|
---|
71 | //
|
---|
72 | // The allowed operators are:
|
---|
73 | // exp(x) e^x
|
---|
74 | // log(x) natural logarithm of x
|
---|
75 | // pow10(x) 10^x
|
---|
76 | // log2(x) logarithm of x to base two
|
---|
77 | // log10(x) logarithm of x to base ten
|
---|
78 | // cos(x) cosine of x
|
---|
79 | // sin(x) sine of x
|
---|
80 | // tan(x) tangent of x
|
---|
81 | // cosh(x) hyperbolic cosine of x
|
---|
82 | // sinh(x) hyperbolic sine of x
|
---|
83 | // tanh(x) hyperbolic tangent of x
|
---|
84 | // acosh(x) arc hyperbolic cosine of x
|
---|
85 | // asinh(x) arc hyperbolic sine of x
|
---|
86 | // atanh(x) arc hyperbolic tangent of x
|
---|
87 | // acos(x) arc cosine (inverse cosine) of x
|
---|
88 | // asin(x) arc sine (inverse sine) of x
|
---|
89 | // atan(x) arc tangent (inverse tangent) of x
|
---|
90 | // sqrt(x) square root of x
|
---|
91 | // sqr(x) square of x
|
---|
92 | // abs(x) absolute value of x, |x|
|
---|
93 | // floor(x) round down to the nearest integer (floor(9.9)=9)
|
---|
94 | // ceil(x) round up to the nearest integer (floor(9.1)=10)
|
---|
95 | // round(x) round to the nearest integer
|
---|
96 | // r2d(x) transform radians to degrees
|
---|
97 | // d2r(x) transform degrees to radians
|
---|
98 | // rand(x) returns a uniform deviate on the interval ( 0, x ].
|
---|
99 | // (gRandom->Uniform(x) is returned)
|
---|
100 | // randp(x) returns gRandom->Poisson(x)
|
---|
101 | // rande(x) returns gRandom->Exp(x)
|
---|
102 | // randi(x) returns gRandom->Integer(x)
|
---|
103 | // randg(x) returns gRandom->Gaus(0, x)
|
---|
104 | // randl(x) returns gRandom->Landau(0, x)
|
---|
105 | // isnan(x) return 1 if x is NaN (Not a Number) otherwise 0
|
---|
106 | // finite(x) return 1 if the number is a valid double (not NaN, inf)
|
---|
107 | //
|
---|
108 | // NaN (Not a Number) means normally a number which is to small to be
|
---|
109 | // stored in a floating point variable (eg. 0<x<1e-56 or similar) or
|
---|
110 | // a number which function is not defined (like asin(1.5))
|
---|
111 | //
|
---|
112 | // inf is the symbol for an infinite number.
|
---|
113 | //
|
---|
114 | //
|
---|
115 | // Constants
|
---|
116 | // ---------
|
---|
117 | //
|
---|
118 | // Constants are implemented in ParseDataMember, namely:
|
---|
119 | // kPi: TMath::Pi()
|
---|
120 | // kRad2Deg: 180/kPi
|
---|
121 | // kDeg2Rad: kPi/180
|
---|
122 | //
|
---|
123 | // You can also defined constants which are defined in TMath by:
|
---|
124 | // kLn10 for static Double_t TMath::Ln10();
|
---|
125 | // kLogE for static Double_t TMath::LogE();
|
---|
126 | // kRadToDeg for static Double_t TMath::RadToDeg();
|
---|
127 | // kDegToRad for static Double_t TMath::DegToRad();
|
---|
128 | // ...
|
---|
129 | //
|
---|
130 | // Remark:
|
---|
131 | // In older root versions only Pi() and E() are implemented
|
---|
132 | // in TMath.
|
---|
133 | //
|
---|
134 | //
|
---|
135 | // Variable Parameters
|
---|
136 | // ------------------------
|
---|
137 | // If you want to use variables, eg for fits you can use [0], [1], ...
|
---|
138 | // These values are initialized with 0 and set by calling
|
---|
139 | // SetVariables().
|
---|
140 | //
|
---|
141 | //
|
---|
142 | // Multi-argument functions
|
---|
143 | // ------------------------
|
---|
144 | // You can use multi-argument functions, too. The access is implemented
|
---|
145 | // via TFormula, which slows down thing a lot. If you can avoid usage
|
---|
146 | // of such expression you accelerate the access a lot. Example:
|
---|
147 | // "TMath::Hypot(MHillas.fMeanX, MHillas.MeanY)"
|
---|
148 | // "pow(MHillas.fMeanX*MHillas.MeanY, -1.2)"
|
---|
149 | // It should be possible to use all functions which are accessible
|
---|
150 | // via the root-dictionary.
|
---|
151 | //
|
---|
152 | //
|
---|
153 | // REMARK:
|
---|
154 | // - All the random functions are returning 0 if gRandom==0
|
---|
155 | // - You may get better results if you are using a TRandom3
|
---|
156 | //
|
---|
157 | // To Do:
|
---|
158 | // - The possibility to use other objects inheriting from MData
|
---|
159 | // is missing.
|
---|
160 | // - By automatic pre-adding parantheses to the rule it would be possible
|
---|
161 | // to implement priorities for operators.
|
---|
162 | //
|
---|
163 | /////////////////////////////////////////////////////////////////////////////
|
---|
164 |
|
---|
165 | #include "MDataChain.h"
|
---|
166 |
|
---|
167 | #include <ctype.h> // isalnum, ...
|
---|
168 | #include <stdlib.h> // strtod, ...
|
---|
169 |
|
---|
170 | #include <TRegexp.h>
|
---|
171 | #include <TRandom.h>
|
---|
172 | #include <TMethodCall.h>
|
---|
173 |
|
---|
174 | #include "MLog.h"
|
---|
175 | #include "MLogManip.h"
|
---|
176 |
|
---|
177 | #include "MDataList.h"
|
---|
178 | #include "MDataValue.h"
|
---|
179 | #include "MDataMember.h"
|
---|
180 | #include "MDataFormula.h"
|
---|
181 | #include "MDataElement.h"
|
---|
182 |
|
---|
183 | ClassImp(MDataChain);
|
---|
184 |
|
---|
185 | using namespace std;
|
---|
186 |
|
---|
187 | // --------------------------------------------------------------------------
|
---|
188 | //
|
---|
189 | // Constructor which takes a rule and a surrounding operator as argument
|
---|
190 | //
|
---|
191 | MDataChain::MDataChain(const char *rule, OperatorType_t op)
|
---|
192 | : fOperatorType(op)
|
---|
193 | {
|
---|
194 | fName = "MDataChain";
|
---|
195 | fTitle = rule;
|
---|
196 |
|
---|
197 | fMember=ParseString(rule);
|
---|
198 | }
|
---|
199 |
|
---|
200 | // --------------------------------------------------------------------------
|
---|
201 | //
|
---|
202 | // Constructor taking a rule as an argument. For more details see
|
---|
203 | // class description
|
---|
204 | //
|
---|
205 | MDataChain::MDataChain(const char *rule, const char *name, const char *title)
|
---|
206 | : fMember(NULL), fOperatorType(kENoop)
|
---|
207 | {
|
---|
208 | fName = name ? name : "MDataChain";
|
---|
209 | fTitle = title ? title : rule;
|
---|
210 |
|
---|
211 | if (TString(rule).IsNull())
|
---|
212 | return;
|
---|
213 |
|
---|
214 | *fLog << inf << "Parsing rule... " << flush;
|
---|
215 | if (!(fMember=ParseString(rule)))
|
---|
216 | {
|
---|
217 | *fLog << err << dbginf << "Parsing '" << rule << "' failed." << endl;
|
---|
218 | return;
|
---|
219 | }
|
---|
220 | *fLog << inf << "found: " << GetRule() << endl;
|
---|
221 | }
|
---|
222 |
|
---|
223 | // --------------------------------------------------------------------------
|
---|
224 | //
|
---|
225 | // PreProcesses all members in the list
|
---|
226 | //
|
---|
227 | Bool_t MDataChain::PreProcess(const MParList *pList)
|
---|
228 | {
|
---|
229 | return fMember ? fMember->PreProcess(pList) : kFALSE;
|
---|
230 | }
|
---|
231 |
|
---|
232 | // --------------------------------------------------------------------------
|
---|
233 | //
|
---|
234 | // Checks whether at least one member has the ready-to-save flag.
|
---|
235 | //
|
---|
236 | Bool_t MDataChain::IsReadyToSave() const
|
---|
237 | {
|
---|
238 | *fLog << all << "fM=" << fMember << "/" << (int)fMember->IsReadyToSave() << " " << endl;
|
---|
239 | return fMember ? fMember->IsReadyToSave() : kFALSE;
|
---|
240 | }
|
---|
241 |
|
---|
242 | // --------------------------------------------------------------------------
|
---|
243 | //
|
---|
244 | // Destructor. Delete filters.
|
---|
245 | //
|
---|
246 | MDataChain::~MDataChain()
|
---|
247 | {
|
---|
248 | if (fMember)
|
---|
249 | delete fMember;
|
---|
250 | }
|
---|
251 |
|
---|
252 | // --------------------------------------------------------------------------
|
---|
253 | //
|
---|
254 | // Returns the number of alphanumeric characters (including '.' and ';')
|
---|
255 | // in the given string
|
---|
256 | //
|
---|
257 | Int_t MDataChain::IsAlNum(TString txt)
|
---|
258 | {
|
---|
259 | int l = txt.Length();
|
---|
260 | for (int i=0; i<l; i++)
|
---|
261 | {
|
---|
262 | if (!isalnum(txt[i]) && txt[i]!='.' && txt[i]!=':' && txt[i]!=';' &&
|
---|
263 | /*txt[i]!='['&&txt[i]!=']'&&*/
|
---|
264 | ((txt[i]!='-' && txt[i]!='+') || i!=0))
|
---|
265 | return i;
|
---|
266 | }
|
---|
267 |
|
---|
268 | return l;
|
---|
269 | }
|
---|
270 |
|
---|
271 | Int_t MDataChain::GetBracket(TString txt, char open, char close)
|
---|
272 | {
|
---|
273 | Int_t first=1;
|
---|
274 | for (int cnt=0; first<txt.Length(); first++)
|
---|
275 | {
|
---|
276 | if (txt[first]==open)
|
---|
277 | cnt++;
|
---|
278 | if (txt[first]==close)
|
---|
279 | cnt--;
|
---|
280 | if (cnt==-1)
|
---|
281 | break;
|
---|
282 | }
|
---|
283 | return first;
|
---|
284 | }
|
---|
285 |
|
---|
286 | // --------------------------------------------------------------------------
|
---|
287 | //
|
---|
288 | // Compare a given text with the known operators. If no operator match
|
---|
289 | // kENoop is retunred, otherwise the corresponding OperatorType_t
|
---|
290 | //
|
---|
291 | MDataChain::OperatorType_t MDataChain::ParseOperator(TString txt) const
|
---|
292 | {
|
---|
293 | txt.ToLower();
|
---|
294 |
|
---|
295 | if (txt=="abs") return kEAbs;
|
---|
296 | if (txt=="fabs") return kEAbs;
|
---|
297 | if (txt=="log") return kELog;
|
---|
298 | if (txt=="log2") return kELog2;
|
---|
299 | if (txt=="log10") return kELog10;
|
---|
300 | if (txt=="sin") return kESin;
|
---|
301 | if (txt=="cos") return kECos;
|
---|
302 | if (txt=="tan") return kETan;
|
---|
303 | if (txt=="sinh") return kESinH;
|
---|
304 | if (txt=="cosh") return kECosH;
|
---|
305 | if (txt=="tanh") return kETanH;
|
---|
306 | if (txt=="asin") return kEASin;
|
---|
307 | if (txt=="acos") return kEACos;
|
---|
308 | if (txt=="atan") return kEATan;
|
---|
309 | if (txt=="asinh") return kEASinH;
|
---|
310 | if (txt=="acosh") return kEACosH;
|
---|
311 | if (txt=="atanh") return kEATanH;
|
---|
312 | if (txt=="sqrt") return kESqrt;
|
---|
313 | if (txt=="sqr") return kESqr;
|
---|
314 | if (txt=="exp") return kEExp;
|
---|
315 | if (txt=="pow10") return kEPow10;
|
---|
316 | if (txt=="sgn") return kESgn;
|
---|
317 | if (txt=="floor") return kEFloor;
|
---|
318 | if (txt=="r2d") return kERad2Deg;
|
---|
319 | if (txt=="d2r") return kEDeg2Rad;
|
---|
320 | if (txt=="rand") return kERandom;
|
---|
321 | if (txt=="randp") return kERandomP;
|
---|
322 | if (txt=="rande") return kERandomE;
|
---|
323 | if (txt=="randi") return kERandomI;
|
---|
324 | if (txt=="randg") return kERandomG;
|
---|
325 | if (txt=="randl") return kERandomL;
|
---|
326 | if (txt=="isnan") return kEIsNaN;
|
---|
327 | if (txt=="finite") return kEFinite;
|
---|
328 | if (txt[0]=='-') return kENegative;
|
---|
329 | if (txt[0]=='+') return kEPositive;
|
---|
330 |
|
---|
331 | return kENoop;
|
---|
332 | }
|
---|
333 |
|
---|
334 | // --------------------------------------------------------------------------
|
---|
335 | //
|
---|
336 | // Here the names of data members are interpreted. This can be used to
|
---|
337 | // check for constants.
|
---|
338 | //
|
---|
339 | MData *MDataChain::ParseDataMember(TString txt)
|
---|
340 | {
|
---|
341 | //txt.ToLower();
|
---|
342 |
|
---|
343 | if (txt=="kRad2Deg") return new MDataValue(kRad2Deg);
|
---|
344 | if (txt=="kDeg2Rad") return new MDataValue(1./kRad2Deg);
|
---|
345 |
|
---|
346 | if (!txt.BeginsWith("k"))
|
---|
347 | return new MDataMember(txt.Data());
|
---|
348 |
|
---|
349 | const TString name = txt(1, txt.Length());
|
---|
350 | #if ROOT_VERSION_CODE < ROOT_VERSION(4,01,00)
|
---|
351 | TMethodCall call(TMath::Class(), name, "");
|
---|
352 | #else
|
---|
353 | static TClass *const tmath = TClass::GetClass("TMath");
|
---|
354 | TMethodCall call(tmath, name, "");
|
---|
355 | #endif
|
---|
356 |
|
---|
357 | switch (call.ReturnType())
|
---|
358 | {
|
---|
359 | case TMethodCall::kLong:
|
---|
360 | Long_t l;
|
---|
361 | call.Execute(l);
|
---|
362 | return new MDataValue(l);
|
---|
363 | case TMethodCall::kDouble:
|
---|
364 | Double_t d;
|
---|
365 | call.Execute(d);
|
---|
366 | return new MDataValue(d);
|
---|
367 | default:
|
---|
368 | break;
|
---|
369 | }
|
---|
370 |
|
---|
371 | return new MDataMember(txt.Data());
|
---|
372 | }
|
---|
373 |
|
---|
374 | // --------------------------------------------------------------------------
|
---|
375 | //
|
---|
376 | // Remove all whitespaces
|
---|
377 | // Replace all kind of double +/- by a single + or -
|
---|
378 | //
|
---|
379 | void MDataChain::SimplifyString(TString &txt) const
|
---|
380 | {
|
---|
381 | txt.ReplaceAll(" ", "");
|
---|
382 | txt.ReplaceAll("**", "^");
|
---|
383 |
|
---|
384 | while (txt.Contains("--") || txt.Contains("++") ||
|
---|
385 | txt.Contains("+-") || txt.Contains("-+"))
|
---|
386 | {
|
---|
387 | txt.ReplaceAll("--", "+");
|
---|
388 | txt.ReplaceAll("++", "+");
|
---|
389 | txt.ReplaceAll("-+", "-");
|
---|
390 | txt.ReplaceAll("+-", "-");
|
---|
391 | }
|
---|
392 | }
|
---|
393 |
|
---|
394 | // --------------------------------------------------------------------------
|
---|
395 | //
|
---|
396 | // Add Parenthesis depending on the priority of the operators. The
|
---|
397 | // priorities are defined by the default of the second argument.
|
---|
398 | //
|
---|
399 | void MDataChain::AddParenthesis(TString &test, const TString op) const
|
---|
400 | {
|
---|
401 | // Signiture of an exponential number 1e12
|
---|
402 | static const TRegexp rexp("[.0123456789]e[+-][0123456789]");
|
---|
403 |
|
---|
404 | const TString low = op(1, op.Length());
|
---|
405 | if (low.Length()==0)
|
---|
406 | return;
|
---|
407 |
|
---|
408 | int offset = 0;
|
---|
409 | while (1)
|
---|
410 | {
|
---|
411 | const TString check = test(offset, test.Length());
|
---|
412 | if (check.IsNull())
|
---|
413 | break;
|
---|
414 |
|
---|
415 | const Ssiz_t pos = check.First(op[0]);
|
---|
416 | if (pos<0)
|
---|
417 | break;
|
---|
418 |
|
---|
419 | int cnt=0;
|
---|
420 |
|
---|
421 | int i;
|
---|
422 | for (i=pos-1; i>=0; i--)
|
---|
423 | {
|
---|
424 | if (check[i]==')')
|
---|
425 | cnt++;
|
---|
426 | if (check[i]=='(')
|
---|
427 | cnt--;
|
---|
428 | if (cnt>0 || low.First(check[i])<0)
|
---|
429 | continue;
|
---|
430 |
|
---|
431 | // Check if operator belongs to an exponential number
|
---|
432 | const TString sub(check(i-2, 4));
|
---|
433 | if (!sub(rexp).IsNull())
|
---|
434 | continue;
|
---|
435 | break;
|
---|
436 | }
|
---|
437 |
|
---|
438 | cnt=0;
|
---|
439 | int j;
|
---|
440 | for (j=pos; j<check.Length(); j++)
|
---|
441 | {
|
---|
442 | if (check[j]=='(')
|
---|
443 | cnt++;
|
---|
444 | if (check[j]==')')
|
---|
445 | cnt--;
|
---|
446 | if (cnt>0 || low.First(check[j])<0)
|
---|
447 | continue;
|
---|
448 |
|
---|
449 | // Check if operator belongs to an exponential number
|
---|
450 | const TString sub(check(j-2, 4));
|
---|
451 | if (!sub(rexp).IsNull())
|
---|
452 | continue;
|
---|
453 | break;
|
---|
454 | }
|
---|
455 |
|
---|
456 | const TString sub = test(offset+i+1, j-i-1);
|
---|
457 |
|
---|
458 | // Check if it contains operators,
|
---|
459 | // otherwise we can simply skip it
|
---|
460 | if (sub.First("%^/*+-")>=0)
|
---|
461 | {
|
---|
462 | test.Insert(offset+j, ")");
|
---|
463 | test.Insert(offset+i+1, "(");
|
---|
464 | offset += 2;
|
---|
465 | }
|
---|
466 | offset += j+1;
|
---|
467 | }
|
---|
468 |
|
---|
469 | AddParenthesis(test, low);
|
---|
470 | }
|
---|
471 |
|
---|
472 | // --------------------------------------------------------------------------
|
---|
473 | //
|
---|
474 | // Core of the data chain. Here the chain is constructed out of the rule.
|
---|
475 | //
|
---|
476 | MData *MDataChain::ParseString(TString txt, Int_t level)
|
---|
477 | {
|
---|
478 | if (level==0)
|
---|
479 | {
|
---|
480 | SimplifyString(txt);
|
---|
481 | AddParenthesis(txt);
|
---|
482 | }
|
---|
483 |
|
---|
484 | MData *member0=NULL;
|
---|
485 |
|
---|
486 | char type=0;
|
---|
487 | int nlist = 0;
|
---|
488 |
|
---|
489 | while (!txt.IsNull())
|
---|
490 | {
|
---|
491 | MData *newmember = NULL;
|
---|
492 |
|
---|
493 | txt = txt.Strip(TString::kBoth);
|
---|
494 |
|
---|
495 | switch (txt[0])
|
---|
496 | {
|
---|
497 | case '(':
|
---|
498 | {
|
---|
499 | //
|
---|
500 | // Search for the corresponding parantheses
|
---|
501 | //
|
---|
502 | const Int_t first=GetBracket(txt, '(', ')');
|
---|
503 | if (first==txt.Length())
|
---|
504 | {
|
---|
505 | *fLog << err << dbginf << "Syntax Error: ')' missing." << endl;
|
---|
506 | if (member0)
|
---|
507 | delete member0;
|
---|
508 | return NULL;
|
---|
509 | }
|
---|
510 |
|
---|
511 | //
|
---|
512 | // Make a copy of the 'interieur' and delete the substring
|
---|
513 | // including the brackets
|
---|
514 | //
|
---|
515 | TString sub = txt(1, first-1);
|
---|
516 | txt.Remove(0, first+1);
|
---|
517 |
|
---|
518 | //
|
---|
519 | // Parse the substring
|
---|
520 | //
|
---|
521 | newmember = ParseString(sub, level+1);
|
---|
522 | if (!newmember)
|
---|
523 | {
|
---|
524 | *fLog << err << dbginf << "Parsing '" << sub << "' failed." << endl;
|
---|
525 | if (member0)
|
---|
526 | delete member0;
|
---|
527 | return NULL;
|
---|
528 | }
|
---|
529 | }
|
---|
530 | break;
|
---|
531 |
|
---|
532 | case ')':
|
---|
533 | *fLog << err << dbginf << "Syntax Error: Too many ')'" << endl;
|
---|
534 | if (member0)
|
---|
535 | delete member0;
|
---|
536 | return NULL;
|
---|
537 |
|
---|
538 | case '+':
|
---|
539 | case '-':
|
---|
540 | case '*':
|
---|
541 | case '/':
|
---|
542 | case '%':
|
---|
543 | case '^':
|
---|
544 | if (member0)
|
---|
545 | {
|
---|
546 | //
|
---|
547 | // Check for the type of the symbol
|
---|
548 | //
|
---|
549 | char is = txt[0];
|
---|
550 | txt.Remove(0, 1);
|
---|
551 |
|
---|
552 | //
|
---|
553 | // If no filter is available or the available filter
|
---|
554 | // is of a different symbols we have to create a new
|
---|
555 | // data list with the new symbol
|
---|
556 | //
|
---|
557 | if (/*!member0->InheritsFrom(MDataMember::Class()) ||*/ type!=is)
|
---|
558 | {
|
---|
559 | MDataList *list = new MDataList(is);
|
---|
560 | list->SetName(Form("List_%c_%d", is, 10*level+nlist++));
|
---|
561 |
|
---|
562 | list->SetOwner();
|
---|
563 | list->AddToList(member0);
|
---|
564 |
|
---|
565 | member0 = list;
|
---|
566 |
|
---|
567 | type = is;
|
---|
568 | }
|
---|
569 | continue;
|
---|
570 | }
|
---|
571 |
|
---|
572 | if (txt[0]!='-' && txt[0]!='+')
|
---|
573 | {
|
---|
574 | *fLog << err << dbginf << "Syntax Error: First argument of '";
|
---|
575 | *fLog << txt[0] << "' opartor missing." << endl;
|
---|
576 | if (member0)
|
---|
577 | delete member0;
|
---|
578 | return NULL;
|
---|
579 | }
|
---|
580 |
|
---|
581 | // FALLTHROU
|
---|
582 |
|
---|
583 | case '0':
|
---|
584 | case '1':
|
---|
585 | case '2':
|
---|
586 | case '3':
|
---|
587 | case '4':
|
---|
588 | case '5':
|
---|
589 | case '6':
|
---|
590 | case '7':
|
---|
591 | case '8':
|
---|
592 | case '9':
|
---|
593 | case '[':
|
---|
594 | case ']':
|
---|
595 | if ((txt[0]!='-' && txt[0]!='+') || isdigit(txt[1]) || txt[1]=='.')
|
---|
596 | {
|
---|
597 | if (!txt.IsNull() && txt[0]=='[')
|
---|
598 | {
|
---|
599 | Int_t first = GetBracket(txt, '[', ']');
|
---|
600 | TString op = txt(1, first-1);
|
---|
601 | txt.Remove(0, first+1);
|
---|
602 |
|
---|
603 | newmember = new MDataValue(0, atoi(op));
|
---|
604 | break;
|
---|
605 | }
|
---|
606 |
|
---|
607 | char *end;
|
---|
608 | Double_t num = strtod(txt.Data(), &end);
|
---|
609 | if (!end || txt.Data()==end)
|
---|
610 | {
|
---|
611 | *fLog << err << dbginf << "Error trying to convert '" << txt << "' to value." << endl;
|
---|
612 | if (member0)
|
---|
613 | delete member0;
|
---|
614 | return NULL;
|
---|
615 | }
|
---|
616 |
|
---|
617 | txt.Remove(0, end-txt.Data());
|
---|
618 |
|
---|
619 | newmember = new MDataValue(num);
|
---|
620 | break;
|
---|
621 | }
|
---|
622 |
|
---|
623 | // FALLTHROUH
|
---|
624 |
|
---|
625 | default:
|
---|
626 | int i = IsAlNum(txt);
|
---|
627 |
|
---|
628 | if (i==0)
|
---|
629 | {
|
---|
630 | *fLog << err << dbginf << "Syntax Error: Name of data member missing in '" << txt << "'" << endl;
|
---|
631 | if (member0)
|
---|
632 | delete member0;
|
---|
633 | return NULL;
|
---|
634 | }
|
---|
635 |
|
---|
636 | TString text = txt(0, i);
|
---|
637 |
|
---|
638 | txt.Remove(0, i);
|
---|
639 | txt = txt.Strip(TString::kBoth);
|
---|
640 |
|
---|
641 | if (!txt.IsNull() && txt[0]=='[')
|
---|
642 | {
|
---|
643 | Int_t first = GetBracket(txt, '[', ']');
|
---|
644 | TString op = txt(1, first-1);
|
---|
645 | txt.Remove(0, first+1);
|
---|
646 |
|
---|
647 | newmember = new MDataElement(text, atoi(op));
|
---|
648 | break;
|
---|
649 | }
|
---|
650 | if ((txt.IsNull() || txt[0]!='(') && text[0]!='-' && text[0]!='+')
|
---|
651 | {
|
---|
652 | newmember = ParseDataMember(text.Data());
|
---|
653 | break;
|
---|
654 | }
|
---|
655 |
|
---|
656 | OperatorType_t op = ParseOperator(text);
|
---|
657 |
|
---|
658 | Int_t first = GetBracket(txt, '(', ')');
|
---|
659 | TString sub = op==kENegative || op==kEPositive ? text.Remove(0,1) + txt : txt(1, first-1);
|
---|
660 | txt.Remove(0, first+1);
|
---|
661 |
|
---|
662 | if (op==kENoop)
|
---|
663 | {
|
---|
664 | newmember = new MDataFormula(Form("%s(%s)", (const char*)text, (const char*)sub));
|
---|
665 | if (newmember->IsValid())
|
---|
666 | break;
|
---|
667 |
|
---|
668 | *fLog << err << dbginf << "Syntax Error: Operator '" << text << "' unknown." << endl;
|
---|
669 | if (member0)
|
---|
670 | delete member0;
|
---|
671 | return NULL;
|
---|
672 | }
|
---|
673 |
|
---|
674 | newmember = new MDataChain(sub, op);
|
---|
675 | if (!newmember->IsValid())
|
---|
676 | {
|
---|
677 | *fLog << err << dbginf << "Syntax Error: Error parsing contents '" << sub << "' of operator " << text << endl;
|
---|
678 | delete newmember;
|
---|
679 | if (member0)
|
---|
680 | delete member0;
|
---|
681 | return NULL;
|
---|
682 | }
|
---|
683 | }
|
---|
684 |
|
---|
685 | if (!member0)
|
---|
686 | {
|
---|
687 | member0 = newmember;
|
---|
688 | continue;
|
---|
689 | }
|
---|
690 |
|
---|
691 | if (!member0->InheritsFrom(MDataList::Class()))
|
---|
692 | continue;
|
---|
693 |
|
---|
694 | ((MDataList*)member0)->AddToList(newmember);
|
---|
695 | }
|
---|
696 |
|
---|
697 | return member0;
|
---|
698 | }
|
---|
699 |
|
---|
700 | // --------------------------------------------------------------------------
|
---|
701 | //
|
---|
702 | // Returns the value described by the rule
|
---|
703 | //
|
---|
704 | Double_t MDataChain::GetValue() const
|
---|
705 | {
|
---|
706 | if (!fMember)
|
---|
707 | {
|
---|
708 | //*fLog << warn << "MDataChain not valid." << endl;
|
---|
709 | return 0;
|
---|
710 | }
|
---|
711 |
|
---|
712 | const Double_t val = fMember->GetValue();
|
---|
713 |
|
---|
714 | switch (fOperatorType)
|
---|
715 | {
|
---|
716 | case kEAbs: return TMath::Abs(val);
|
---|
717 | case kELog: return TMath::Log(val);
|
---|
718 | case kELog2: return TMath::Log2(val);
|
---|
719 | case kELog10: return TMath::Log10(val);
|
---|
720 | case kESin: return TMath::Sin(val);
|
---|
721 | case kECos: return TMath::Cos(val);
|
---|
722 | case kETan: return TMath::Tan(val);
|
---|
723 | case kESinH: return TMath::SinH(val);
|
---|
724 | case kECosH: return TMath::CosH(val);
|
---|
725 | case kETanH: return TMath::TanH(val);
|
---|
726 | case kEASin: return TMath::ASin(val);
|
---|
727 | case kEACos: return TMath::ACos(val);
|
---|
728 | case kEATan: return TMath::ATan(val);
|
---|
729 | case kEASinH: return TMath::ASinH(val);
|
---|
730 | case kEACosH: return TMath::ACosH(val);
|
---|
731 | case kEATanH: return TMath::ATanH(val);
|
---|
732 | case kESqrt: return TMath::Sqrt(val);
|
---|
733 | case kESqr: return val*val;
|
---|
734 | case kEExp: return TMath::Exp(val);
|
---|
735 | case kEPow10: return TMath::Power(10, val);
|
---|
736 | case kESgn: return val<0 ? -1 : 1;
|
---|
737 | case kENegative: return -val;
|
---|
738 | case kEPositive: return val;
|
---|
739 | case kEFloor: return TMath::Floor(val);
|
---|
740 | case kECeil: return TMath::Ceil(val);
|
---|
741 | case kERound: return TMath::Nint(val);
|
---|
742 | case kERad2Deg: return val*180/TMath::Pi();
|
---|
743 | case kEDeg2Rad: return val*TMath::Pi()/180;
|
---|
744 | case kERandom: return gRandom ? gRandom->Uniform(val) : 0;
|
---|
745 | case kERandomP: return gRandom ? gRandom->Poisson(val) : 0;
|
---|
746 | case kERandomE: return gRandom ? gRandom->Exp(val) : 0;
|
---|
747 | case kERandomI: return gRandom ? gRandom->Integer((int)val) : 0;
|
---|
748 | case kERandomG: return gRandom ? gRandom->Gaus(0, val) : 0;
|
---|
749 | case kERandomL: return gRandom ? gRandom->Landau(0, val) : 0;
|
---|
750 | case kEIsNaN: return TMath::IsNaN(val);
|
---|
751 | case kEFinite: return TMath::Finite(val);
|
---|
752 | case kENoop: return val;
|
---|
753 | }
|
---|
754 |
|
---|
755 | *fLog << warn << "No Case for " << fOperatorType << " available." << endl;
|
---|
756 |
|
---|
757 | return 0;
|
---|
758 | }
|
---|
759 |
|
---|
760 | // --------------------------------------------------------------------------
|
---|
761 | //
|
---|
762 | // Builds a rule from all the chain members. This is a rule which could
|
---|
763 | // be used to rebuild the chain.
|
---|
764 | //
|
---|
765 | TString MDataChain::GetRule() const
|
---|
766 | {
|
---|
767 | if (!fMember)
|
---|
768 | return "<n/a>";
|
---|
769 |
|
---|
770 | TString str;
|
---|
771 |
|
---|
772 | Bool_t bracket = fOperatorType!=kENoop && !fMember->InheritsFrom(MDataList::Class());
|
---|
773 |
|
---|
774 | switch (fOperatorType)
|
---|
775 | {
|
---|
776 | case kEAbs: str += "abs" ; break;
|
---|
777 | case kELog: str += "log" ; break;
|
---|
778 | case kELog2: str += "log2" ; break;
|
---|
779 | case kELog10: str += "log10" ; break;
|
---|
780 | case kESin: str += "sin" ; break;
|
---|
781 | case kECos: str += "cos" ; break;
|
---|
782 | case kETan: str += "tan" ; break;
|
---|
783 | case kESinH: str += "sinh" ; break;
|
---|
784 | case kECosH: str += "cosh" ; break;
|
---|
785 | case kETanH: str += "tanh" ; break;
|
---|
786 | case kEASin: str += "asin" ; break;
|
---|
787 | case kEACos: str += "acos" ; break;
|
---|
788 | case kEATan: str += "atan" ; break;
|
---|
789 | case kEASinH: str += "asinh" ; break;
|
---|
790 | case kEACosH: str += "acosh" ; break;
|
---|
791 | case kEATanH: str += "atanh" ; break;
|
---|
792 | case kESqrt: str += "sqrt" ; break;
|
---|
793 | case kESqr: str += "sqr" ; break;
|
---|
794 | case kEExp: str += "exp" ; break;
|
---|
795 | case kEPow10: str += "pow10" ; break;
|
---|
796 | case kESgn: str += "sgn" ; break;
|
---|
797 | case kENegative: str += "-" ; break;
|
---|
798 | case kEPositive: str += "+" ; break;
|
---|
799 | case kEFloor: str += "floor" ; break;
|
---|
800 | case kECeil: str += "ceil" ; break;
|
---|
801 | case kERound: str += "round" ; break;
|
---|
802 | case kERad2Deg: str += "r2d" ; break;
|
---|
803 | case kEDeg2Rad: str += "d2r" ; break;
|
---|
804 | case kERandom: str += "rand" ; break;
|
---|
805 | case kERandomP: str += "randp" ; break;
|
---|
806 | case kERandomE: str += "rande" ; break;
|
---|
807 | case kERandomI: str += "randi" ; break;
|
---|
808 | case kERandomG: str += "randg" ; break;
|
---|
809 | case kERandomL: str += "randl" ; break;
|
---|
810 | case kEIsNaN: str += "isnan" ; break;
|
---|
811 | case kEFinite: str += "finite"; break;
|
---|
812 | case kENoop:
|
---|
813 | break;
|
---|
814 | }
|
---|
815 |
|
---|
816 | if (bracket)
|
---|
817 | str += "(";
|
---|
818 |
|
---|
819 | str += fMember->GetRule();
|
---|
820 |
|
---|
821 | if (bracket)
|
---|
822 | str += ")";
|
---|
823 |
|
---|
824 | return str;
|
---|
825 | }
|
---|
826 |
|
---|
827 | // --------------------------------------------------------------------------
|
---|
828 | //
|
---|
829 | // Return a comma seperated list of all data members used in the chain.
|
---|
830 | // This is mainly used in MTask::AddToBranchList
|
---|
831 | //
|
---|
832 | TString MDataChain::GetDataMember() const
|
---|
833 | {
|
---|
834 | return fMember ? fMember->GetDataMember() : "";
|
---|
835 | }
|
---|
836 |
|
---|
837 | void MDataChain::SetVariables(const TArrayD &arr)
|
---|
838 | {
|
---|
839 | if (fMember)
|
---|
840 | fMember->SetVariables(arr);
|
---|
841 | }
|
---|
842 |
|
---|
843 | // --------------------------------------------------------------------------
|
---|
844 | //
|
---|
845 | // Check for corresponding entries in resource file and setup data chain.
|
---|
846 | //
|
---|
847 | // Assuming your MDataChain is called (Set/GetName): MyData
|
---|
848 | //
|
---|
849 | // Now setup the condition, eg:
|
---|
850 | // MyData.Rule: log10(MHillas.fSize)
|
---|
851 | // or
|
---|
852 | // MyData.Rule: log10(MHillas.fSize) - 4.1
|
---|
853 | //
|
---|
854 | // If you want to use more difficult rules you can split the
|
---|
855 | // condition into subrules. Subrules are identified
|
---|
856 | // by {}-brackets. Avoid trailing 0's! For example:
|
---|
857 | //
|
---|
858 | // MyData.Rule: log10(MHillas.fSize) + {0} - {1}
|
---|
859 | // MyData.0: 5.5*MHillas.fSize
|
---|
860 | // MyData.1: 2.3*log10(MHillas.fSize)
|
---|
861 | //
|
---|
862 | // The numbering must be continous and start with 0. You can use
|
---|
863 | // a subrules more than once. All {}-brackets are simply replaced
|
---|
864 | // by the corresponding conditions. The rules how conditions can
|
---|
865 | // be written can be found in the class description of MDataChain.
|
---|
866 | //
|
---|
867 | Int_t MDataChain::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
|
---|
868 | {
|
---|
869 | Bool_t rc = kFALSE;
|
---|
870 | if (!IsEnvDefined(env, prefix, "Rule", print))
|
---|
871 | return rc;
|
---|
872 |
|
---|
873 | TString rule = GetEnvValue(env, prefix, "Rule", "");
|
---|
874 | rule.ReplaceAll(" ", "");
|
---|
875 |
|
---|
876 | Int_t idx=0;
|
---|
877 | while (1)
|
---|
878 | {
|
---|
879 | TString cond;
|
---|
880 | if (IsEnvDefined(env, prefix, Form("%d", idx), print))
|
---|
881 | {
|
---|
882 | cond += "(";
|
---|
883 | cond += GetEnvValue(env, prefix, Form("%d", idx), "");
|
---|
884 | cond += ")";
|
---|
885 | }
|
---|
886 |
|
---|
887 | if (cond.IsNull())
|
---|
888 | break;
|
---|
889 |
|
---|
890 | rule.ReplaceAll(Form("{%d}", idx), cond);
|
---|
891 | idx++;
|
---|
892 | }
|
---|
893 |
|
---|
894 | if (fMember)
|
---|
895 | {
|
---|
896 | delete fMember;
|
---|
897 | fMember = 0;
|
---|
898 | }
|
---|
899 |
|
---|
900 | if (rule.IsNull())
|
---|
901 | {
|
---|
902 | *fLog << warn << "MDataChain::ReadEnv - ERROR: Empty rule found." << endl;
|
---|
903 | return kERROR;
|
---|
904 | }
|
---|
905 |
|
---|
906 | if (!(fMember=ParseString(rule)))
|
---|
907 | {
|
---|
908 | *fLog << err << "MDataChain::ReadEnv - ERROR: Parsing '" << rule << "' failed." << endl;
|
---|
909 | return kERROR;
|
---|
910 | }
|
---|
911 |
|
---|
912 | if (print)
|
---|
913 | {
|
---|
914 | *fLog << inf << "found: ";
|
---|
915 | fMember->Print();
|
---|
916 | *fLog << endl;
|
---|
917 | }
|
---|
918 |
|
---|
919 | return kTRUE;
|
---|
920 | }
|
---|
921 | /*
|
---|
922 | void MDataChain::ReconstructElements()
|
---|
923 | {
|
---|
924 | if (!fMember)
|
---|
925 | return;
|
---|
926 |
|
---|
927 | if (fMember->InheritsFrom(MDataElement::Class()))
|
---|
928 | {
|
---|
929 | MData *data = ((MDataElement*)fMember)->CloneColumn();
|
---|
930 | delete fMember;
|
---|
931 | fMember = data;
|
---|
932 | }
|
---|
933 |
|
---|
934 | fMember->ReconstructElements();
|
---|
935 | }
|
---|
936 | */
|
---|