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 | const TString low = op(1, op.Length());
|
---|
402 | if (low.Length()==0)
|
---|
403 | return;
|
---|
404 |
|
---|
405 | int offset = 0;
|
---|
406 | while (1)
|
---|
407 | {
|
---|
408 | const TString check = test(offset, test.Length());
|
---|
409 | if (check.IsNull())
|
---|
410 | break;
|
---|
411 |
|
---|
412 | const Ssiz_t pos = check.First(op[0]);
|
---|
413 | if (pos<0)
|
---|
414 | break;
|
---|
415 |
|
---|
416 | int cnt=0;
|
---|
417 |
|
---|
418 | int i;
|
---|
419 | for (i=pos-1; i>=0; i--)
|
---|
420 | {
|
---|
421 | if (check[i]==')')
|
---|
422 | cnt++;
|
---|
423 | if (check[i]=='(')
|
---|
424 | cnt--;
|
---|
425 | if (cnt>0 || low.First(check[i])<0)
|
---|
426 | continue;
|
---|
427 | break;
|
---|
428 | }
|
---|
429 |
|
---|
430 | cnt=0;
|
---|
431 | int j;
|
---|
432 | for (j=pos; j<check.Length(); j++)
|
---|
433 | {
|
---|
434 | if (check[j]=='(')
|
---|
435 | cnt++;
|
---|
436 | if (check[j]==')')
|
---|
437 | cnt--;
|
---|
438 | if (cnt>0 || low.First(check[j])<0)
|
---|
439 | continue;
|
---|
440 | break;
|
---|
441 | }
|
---|
442 |
|
---|
443 | const TString sub = test(offset+i+1, j-i-1);
|
---|
444 |
|
---|
445 | // Check if it contains operators,
|
---|
446 | // otherwise we can simply skip it
|
---|
447 | if (sub.First("%^/*+-")>=0)
|
---|
448 | {
|
---|
449 | test.Insert(offset+j, ")");
|
---|
450 | test.Insert(offset+i+1, "(");
|
---|
451 | offset += 2;
|
---|
452 | }
|
---|
453 | offset += j+1;
|
---|
454 | }
|
---|
455 |
|
---|
456 | AddParenthesis(test, low);
|
---|
457 | }
|
---|
458 |
|
---|
459 | // --------------------------------------------------------------------------
|
---|
460 | //
|
---|
461 | // Core of the data chain. Here the chain is constructed out of the rule.
|
---|
462 | //
|
---|
463 | MData *MDataChain::ParseString(TString txt, Int_t level)
|
---|
464 | {
|
---|
465 | if (level==0)
|
---|
466 | {
|
---|
467 | SimplifyString(txt);
|
---|
468 | AddParenthesis(txt);
|
---|
469 | }
|
---|
470 |
|
---|
471 | MData *member0=NULL;
|
---|
472 |
|
---|
473 | char type=0;
|
---|
474 | int nlist = 0;
|
---|
475 |
|
---|
476 | while (!txt.IsNull())
|
---|
477 | {
|
---|
478 | MData *newmember = NULL;
|
---|
479 |
|
---|
480 | txt = txt.Strip(TString::kBoth);
|
---|
481 |
|
---|
482 | switch (txt[0])
|
---|
483 | {
|
---|
484 | case '(':
|
---|
485 | {
|
---|
486 | //
|
---|
487 | // Search for the corresponding parantheses
|
---|
488 | //
|
---|
489 | const Int_t first=GetBracket(txt, '(', ')');
|
---|
490 | if (first==txt.Length())
|
---|
491 | {
|
---|
492 | *fLog << err << dbginf << "Syntax Error: ')' missing." << endl;
|
---|
493 | if (member0)
|
---|
494 | delete member0;
|
---|
495 | return NULL;
|
---|
496 | }
|
---|
497 |
|
---|
498 | //
|
---|
499 | // Make a copy of the 'interieur' and delete the substring
|
---|
500 | // including the brackets
|
---|
501 | //
|
---|
502 | TString sub = txt(1, first-1);
|
---|
503 | txt.Remove(0, first+1);
|
---|
504 |
|
---|
505 | //
|
---|
506 | // Parse the substring
|
---|
507 | //
|
---|
508 | newmember = ParseString(sub, level+1);
|
---|
509 | if (!newmember)
|
---|
510 | {
|
---|
511 | *fLog << err << dbginf << "Parsing '" << sub << "' failed." << endl;
|
---|
512 | if (member0)
|
---|
513 | delete member0;
|
---|
514 | return NULL;
|
---|
515 | }
|
---|
516 | }
|
---|
517 | break;
|
---|
518 |
|
---|
519 | case ')':
|
---|
520 | *fLog << err << dbginf << "Syntax Error: Too many ')'" << endl;
|
---|
521 | if (member0)
|
---|
522 | delete member0;
|
---|
523 | return NULL;
|
---|
524 |
|
---|
525 | case '+':
|
---|
526 | case '-':
|
---|
527 | case '*':
|
---|
528 | case '/':
|
---|
529 | case '%':
|
---|
530 | case '^':
|
---|
531 | if (member0)
|
---|
532 | {
|
---|
533 | //
|
---|
534 | // Check for the type of the symbol
|
---|
535 | //
|
---|
536 | char is = txt[0];
|
---|
537 | txt.Remove(0, 1);
|
---|
538 |
|
---|
539 | //
|
---|
540 | // If no filter is available or the available filter
|
---|
541 | // is of a different symbols we have to create a new
|
---|
542 | // data list with the new symbol
|
---|
543 | //
|
---|
544 | if (/*!member0->InheritsFrom(MDataMember::Class()) ||*/ type!=is)
|
---|
545 | {
|
---|
546 | MDataList *list = new MDataList(is);
|
---|
547 | list->SetName(Form("List_%c_%d", is, 10*level+nlist++));
|
---|
548 |
|
---|
549 | list->SetOwner();
|
---|
550 | list->AddToList(member0);
|
---|
551 |
|
---|
552 | member0 = list;
|
---|
553 |
|
---|
554 | type = is;
|
---|
555 | }
|
---|
556 | continue;
|
---|
557 | }
|
---|
558 |
|
---|
559 | if (txt[0]!='-' && txt[0]!='+')
|
---|
560 | {
|
---|
561 | *fLog << err << dbginf << "Syntax Error: First argument of '";
|
---|
562 | *fLog << txt[0] << "' opartor missing." << endl;
|
---|
563 | if (member0)
|
---|
564 | delete member0;
|
---|
565 | return NULL;
|
---|
566 | }
|
---|
567 |
|
---|
568 | // FALLTHROU
|
---|
569 |
|
---|
570 | case '0':
|
---|
571 | case '1':
|
---|
572 | case '2':
|
---|
573 | case '3':
|
---|
574 | case '4':
|
---|
575 | case '5':
|
---|
576 | case '6':
|
---|
577 | case '7':
|
---|
578 | case '8':
|
---|
579 | case '9':
|
---|
580 | case '[':
|
---|
581 | case ']':
|
---|
582 | if ((txt[0]!='-' && txt[0]!='+') || isdigit(txt[1]) || txt[1]=='.')
|
---|
583 | {
|
---|
584 | if (!txt.IsNull() && txt[0]=='[')
|
---|
585 | {
|
---|
586 | Int_t first = GetBracket(txt, '[', ']');
|
---|
587 | TString op = txt(1, first-1);
|
---|
588 | txt.Remove(0, first+1);
|
---|
589 |
|
---|
590 | newmember = new MDataValue(0, atoi(op));
|
---|
591 | break;
|
---|
592 | }
|
---|
593 |
|
---|
594 | char *end;
|
---|
595 | Double_t num = strtod(txt.Data(), &end);
|
---|
596 | if (!end || txt.Data()==end)
|
---|
597 | {
|
---|
598 | *fLog << err << dbginf << "Error trying to convert '" << txt << "' to value." << endl;
|
---|
599 | if (member0)
|
---|
600 | delete member0;
|
---|
601 | return NULL;
|
---|
602 | }
|
---|
603 |
|
---|
604 | txt.Remove(0, end-txt.Data());
|
---|
605 |
|
---|
606 | newmember = new MDataValue(num);
|
---|
607 | break;
|
---|
608 | }
|
---|
609 |
|
---|
610 | // FALLTHROUH
|
---|
611 |
|
---|
612 | default:
|
---|
613 | int i = IsAlNum(txt);
|
---|
614 |
|
---|
615 | if (i==0)
|
---|
616 | {
|
---|
617 | *fLog << err << dbginf << "Syntax Error: Name of data member missing in '" << txt << "'" << endl;
|
---|
618 | if (member0)
|
---|
619 | delete member0;
|
---|
620 | return NULL;
|
---|
621 | }
|
---|
622 |
|
---|
623 | TString text = txt(0, i);
|
---|
624 |
|
---|
625 | txt.Remove(0, i);
|
---|
626 | txt = txt.Strip(TString::kBoth);
|
---|
627 |
|
---|
628 | if (!txt.IsNull() && txt[0]=='[')
|
---|
629 | {
|
---|
630 | Int_t first = GetBracket(txt, '[', ']');
|
---|
631 | TString op = txt(1, first-1);
|
---|
632 | txt.Remove(0, first+1);
|
---|
633 |
|
---|
634 | newmember = new MDataElement(text, atoi(op));
|
---|
635 | break;
|
---|
636 | }
|
---|
637 | if ((txt.IsNull() || txt[0]!='(') && text[0]!='-' && text[0]!='+')
|
---|
638 | {
|
---|
639 | newmember = ParseDataMember(text.Data());
|
---|
640 | break;
|
---|
641 | }
|
---|
642 |
|
---|
643 | OperatorType_t op = ParseOperator(text);
|
---|
644 |
|
---|
645 | Int_t first = GetBracket(txt, '(', ')');
|
---|
646 | TString sub = op==kENegative || op==kEPositive ? text.Remove(0,1) + txt : txt(1, first-1);
|
---|
647 | txt.Remove(0, first+1);
|
---|
648 |
|
---|
649 | if (op==kENoop)
|
---|
650 | {
|
---|
651 | newmember = new MDataFormula(Form("%s(%s)", (const char*)text, (const char*)sub));
|
---|
652 | if (newmember->IsValid())
|
---|
653 | break;
|
---|
654 |
|
---|
655 | *fLog << err << dbginf << "Syntax Error: Operator '" << text << "' unknown." << endl;
|
---|
656 | if (member0)
|
---|
657 | delete member0;
|
---|
658 | return NULL;
|
---|
659 | }
|
---|
660 |
|
---|
661 | newmember = new MDataChain(sub, op);
|
---|
662 | if (!newmember->IsValid())
|
---|
663 | {
|
---|
664 | *fLog << err << dbginf << "Syntax Error: Error parsing contents '" << sub << "' of operator " << text << endl;
|
---|
665 | delete newmember;
|
---|
666 | if (member0)
|
---|
667 | delete member0;
|
---|
668 | return NULL;
|
---|
669 | }
|
---|
670 | }
|
---|
671 |
|
---|
672 | if (!member0)
|
---|
673 | {
|
---|
674 | member0 = newmember;
|
---|
675 | continue;
|
---|
676 | }
|
---|
677 |
|
---|
678 | if (!member0->InheritsFrom(MDataList::Class()))
|
---|
679 | continue;
|
---|
680 |
|
---|
681 | ((MDataList*)member0)->AddToList(newmember);
|
---|
682 | }
|
---|
683 |
|
---|
684 | return member0;
|
---|
685 | }
|
---|
686 |
|
---|
687 | // --------------------------------------------------------------------------
|
---|
688 | //
|
---|
689 | // Returns the value described by the rule
|
---|
690 | //
|
---|
691 | Double_t MDataChain::GetValue() const
|
---|
692 | {
|
---|
693 | if (!fMember)
|
---|
694 | {
|
---|
695 | //*fLog << warn << "MDataChain not valid." << endl;
|
---|
696 | return 0;
|
---|
697 | }
|
---|
698 |
|
---|
699 | const Double_t val = fMember->GetValue();
|
---|
700 |
|
---|
701 | switch (fOperatorType)
|
---|
702 | {
|
---|
703 | case kEAbs: return TMath::Abs(val);
|
---|
704 | case kELog: return TMath::Log(val);
|
---|
705 | case kELog2: return TMath::Log2(val);
|
---|
706 | case kELog10: return TMath::Log10(val);
|
---|
707 | case kESin: return TMath::Sin(val);
|
---|
708 | case kECos: return TMath::Cos(val);
|
---|
709 | case kETan: return TMath::Tan(val);
|
---|
710 | case kESinH: return TMath::SinH(val);
|
---|
711 | case kECosH: return TMath::CosH(val);
|
---|
712 | case kETanH: return TMath::TanH(val);
|
---|
713 | case kEASin: return TMath::ASin(val);
|
---|
714 | case kEACos: return TMath::ACos(val);
|
---|
715 | case kEATan: return TMath::ATan(val);
|
---|
716 | case kEASinH: return TMath::ASinH(val);
|
---|
717 | case kEACosH: return TMath::ACosH(val);
|
---|
718 | case kEATanH: return TMath::ATanH(val);
|
---|
719 | case kESqrt: return TMath::Sqrt(val);
|
---|
720 | case kESqr: return val*val;
|
---|
721 | case kEExp: return TMath::Exp(val);
|
---|
722 | case kEPow10: return TMath::Power(10, val);
|
---|
723 | case kESgn: return val<0 ? -1 : 1;
|
---|
724 | case kENegative: return -val;
|
---|
725 | case kEPositive: return val;
|
---|
726 | case kEFloor: return TMath::Floor(val);
|
---|
727 | case kECeil: return TMath::Ceil(val);
|
---|
728 | case kERound: return TMath::Nint(val);
|
---|
729 | case kERad2Deg: return val*180/TMath::Pi();
|
---|
730 | case kEDeg2Rad: return val*TMath::Pi()/180;
|
---|
731 | case kERandom: return gRandom ? gRandom->Uniform(val) : 0;
|
---|
732 | case kERandomP: return gRandom ? gRandom->Poisson(val) : 0;
|
---|
733 | case kERandomE: return gRandom ? gRandom->Exp(val) : 0;
|
---|
734 | case kERandomI: return gRandom ? gRandom->Integer((int)val) : 0;
|
---|
735 | case kERandomG: return gRandom ? gRandom->Gaus(0, val) : 0;
|
---|
736 | case kERandomL: return gRandom ? gRandom->Landau(0, val) : 0;
|
---|
737 | case kEIsNaN: return TMath::IsNaN(val);
|
---|
738 | case kEFinite: return TMath::Finite(val);
|
---|
739 | case kENoop: return val;
|
---|
740 | }
|
---|
741 |
|
---|
742 | *fLog << warn << "No Case for " << fOperatorType << " available." << endl;
|
---|
743 |
|
---|
744 | return 0;
|
---|
745 | }
|
---|
746 |
|
---|
747 | // --------------------------------------------------------------------------
|
---|
748 | //
|
---|
749 | // Builds a rule from all the chain members. This is a rule which could
|
---|
750 | // be used to rebuild the chain.
|
---|
751 | //
|
---|
752 | TString MDataChain::GetRule() const
|
---|
753 | {
|
---|
754 | if (!fMember)
|
---|
755 | return "<n/a>";
|
---|
756 |
|
---|
757 | TString str;
|
---|
758 |
|
---|
759 | Bool_t bracket = fOperatorType!=kENoop && !fMember->InheritsFrom(MDataList::Class());
|
---|
760 |
|
---|
761 | switch (fOperatorType)
|
---|
762 | {
|
---|
763 | case kEAbs: str += "abs" ; break;
|
---|
764 | case kELog: str += "log" ; break;
|
---|
765 | case kELog2: str += "log2" ; break;
|
---|
766 | case kELog10: str += "log10" ; break;
|
---|
767 | case kESin: str += "sin" ; break;
|
---|
768 | case kECos: str += "cos" ; break;
|
---|
769 | case kETan: str += "tan" ; break;
|
---|
770 | case kESinH: str += "sinh" ; break;
|
---|
771 | case kECosH: str += "cosh" ; break;
|
---|
772 | case kETanH: str += "tanh" ; break;
|
---|
773 | case kEASin: str += "asin" ; break;
|
---|
774 | case kEACos: str += "acos" ; break;
|
---|
775 | case kEATan: str += "atan" ; break;
|
---|
776 | case kEASinH: str += "asinh" ; break;
|
---|
777 | case kEACosH: str += "acosh" ; break;
|
---|
778 | case kEATanH: str += "atanh" ; break;
|
---|
779 | case kESqrt: str += "sqrt" ; break;
|
---|
780 | case kESqr: str += "sqr" ; break;
|
---|
781 | case kEExp: str += "exp" ; break;
|
---|
782 | case kEPow10: str += "pow10" ; break;
|
---|
783 | case kESgn: str += "sgn" ; break;
|
---|
784 | case kENegative: str += "-" ; break;
|
---|
785 | case kEPositive: str += "+" ; break;
|
---|
786 | case kEFloor: str += "floor" ; break;
|
---|
787 | case kECeil: str += "ceil" ; break;
|
---|
788 | case kERound: str += "round" ; break;
|
---|
789 | case kERad2Deg: str += "r2d" ; break;
|
---|
790 | case kEDeg2Rad: str += "d2r" ; break;
|
---|
791 | case kERandom: str += "rand" ; break;
|
---|
792 | case kERandomP: str += "randp" ; break;
|
---|
793 | case kERandomE: str += "rande" ; break;
|
---|
794 | case kERandomI: str += "randi" ; break;
|
---|
795 | case kERandomG: str += "randg" ; break;
|
---|
796 | case kERandomL: str += "randl" ; break;
|
---|
797 | case kEIsNaN: str += "isnan" ; break;
|
---|
798 | case kEFinite: str += "finite"; break;
|
---|
799 | case kENoop:
|
---|
800 | break;
|
---|
801 | }
|
---|
802 |
|
---|
803 | if (bracket)
|
---|
804 | str += "(";
|
---|
805 |
|
---|
806 | str += fMember->GetRule();
|
---|
807 |
|
---|
808 | if (bracket)
|
---|
809 | str += ")";
|
---|
810 |
|
---|
811 | return str;
|
---|
812 | }
|
---|
813 |
|
---|
814 | // --------------------------------------------------------------------------
|
---|
815 | //
|
---|
816 | // Return a comma seperated list of all data members used in the chain.
|
---|
817 | // This is mainly used in MTask::AddToBranchList
|
---|
818 | //
|
---|
819 | TString MDataChain::GetDataMember() const
|
---|
820 | {
|
---|
821 | return fMember->GetDataMember();
|
---|
822 | }
|
---|
823 |
|
---|
824 | void MDataChain::SetVariables(const TArrayD &arr)
|
---|
825 | {
|
---|
826 | return fMember->SetVariables(arr);
|
---|
827 | }
|
---|
828 |
|
---|
829 | // --------------------------------------------------------------------------
|
---|
830 | //
|
---|
831 | // Check for corresponding entries in resource file and setup data chain.
|
---|
832 | //
|
---|
833 | // Assuming your MDataChain is called (Set/GetName): MyData
|
---|
834 | //
|
---|
835 | // Now setup the condition, eg:
|
---|
836 | // MyData.Rule: log10(MHillas.fSize)
|
---|
837 | // or
|
---|
838 | // MyData.Rule: log10(MHillas.fSize) - 4.1
|
---|
839 | //
|
---|
840 | // If you want to use more difficult rules you can split the
|
---|
841 | // condition into subrules. Subrules are identified
|
---|
842 | // by {}-brackets. Avoid trailing 0's! For example:
|
---|
843 | //
|
---|
844 | // MyData.Rule: log10(MHillas.fSize) + {0} - {1}
|
---|
845 | // MyData.0: 5.5*MHillas.fSize
|
---|
846 | // MyData.1: 2.3*log10(MHillas.fSize)
|
---|
847 | //
|
---|
848 | // The numbering must be continous and start with 0. You can use
|
---|
849 | // a subrules more than once. All {}-brackets are simply replaced
|
---|
850 | // by the corresponding conditions. The rules how conditions can
|
---|
851 | // be written can be found in the class description of MDataChain.
|
---|
852 | //
|
---|
853 | Int_t MDataChain::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
|
---|
854 | {
|
---|
855 | Bool_t rc = kFALSE;
|
---|
856 | if (!IsEnvDefined(env, prefix, "Rule", print))
|
---|
857 | return rc;
|
---|
858 |
|
---|
859 | TString rule = GetEnvValue(env, prefix, "Rule", "");
|
---|
860 | rule.ReplaceAll(" ", "");
|
---|
861 |
|
---|
862 | Int_t idx=0;
|
---|
863 | while (1)
|
---|
864 | {
|
---|
865 | TString cond;
|
---|
866 | if (IsEnvDefined(env, prefix, Form("%d", idx), print))
|
---|
867 | {
|
---|
868 | cond += "(";
|
---|
869 | cond += GetEnvValue(env, prefix, Form("%d", idx), "");
|
---|
870 | cond += ")";
|
---|
871 | }
|
---|
872 |
|
---|
873 | if (cond.IsNull())
|
---|
874 | break;
|
---|
875 |
|
---|
876 | rule.ReplaceAll(Form("{%d}", idx), cond);
|
---|
877 | idx++;
|
---|
878 | }
|
---|
879 |
|
---|
880 | if (fMember)
|
---|
881 | {
|
---|
882 | delete fMember;
|
---|
883 | fMember = 0;
|
---|
884 | }
|
---|
885 |
|
---|
886 | if (rule.IsNull())
|
---|
887 | {
|
---|
888 | *fLog << warn << "MDataChain::ReadEnv - ERROR: Empty rule found." << endl;
|
---|
889 | return kERROR;
|
---|
890 | }
|
---|
891 |
|
---|
892 | if (!(fMember=ParseString(rule)))
|
---|
893 | {
|
---|
894 | *fLog << err << "MDataChain::ReadEnv - ERROR: Parsing '" << rule << "' failed." << endl;
|
---|
895 | return kERROR;
|
---|
896 | }
|
---|
897 |
|
---|
898 | if (print)
|
---|
899 | {
|
---|
900 | *fLog << inf << "found: ";
|
---|
901 | fMember->Print();
|
---|
902 | *fLog << endl;
|
---|
903 | }
|
---|
904 |
|
---|
905 | return kTRUE;
|
---|
906 | }
|
---|
907 | /*
|
---|
908 | void MDataChain::ReconstructElements()
|
---|
909 | {
|
---|
910 | if (!fMember)
|
---|
911 | return;
|
---|
912 |
|
---|
913 | if (fMember->InheritsFrom(MDataElement::Class()))
|
---|
914 | {
|
---|
915 | MData *data = ((MDataElement*)fMember)->CloneColumn();
|
---|
916 | delete fMember;
|
---|
917 | fMember = data;
|
---|
918 | }
|
---|
919 |
|
---|
920 | fMember->ReconstructElements();
|
---|
921 | }
|
---|
922 | */
|
---|