Ignore:
Timestamp:
02/17/05 17:09:00 (20 years ago)
Author:
tbretz
Message:
*** empty log message ***
Location:
trunk/MagicSoft/Mars/mdata
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/mdata/MDataChain.cc

    r6515 r6569  
    5252// ---------
    5353//
    54 // The allowed operations are: +, -, *, /, %, ^
     54// The allowed operations are: +, -, *, /, %, ^ and ** for ^
    5555//
    5656// While a%b returns the floating point reminder of a/b.
    5757// While a^b returns a to the power of b
    5858//
    59 // Warning: There is no priority rule build in. So better use parantheses
    60 //   to get correct results. The rule is parsed/evaluated from the left
    61 //   to the right, which means:
    62 //
    63 //   "MHillas.fWidth + MHillas.fLength / HillasSource.fDist"
    64 //
    65 //    is parses as
    66 //
    67 //   "(MHillas.fWidth + MHillas.fLength) / HillasSource.fDist"
     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!
    6868//
    6969// You can also use mathmatical operators, eg:
     
    168168#include <stdlib.h>       // strtod, ...
    169169
     170#include <TRegexp.h>
    170171#include <TRandom.h>
    171172#include <TMethodCall.h>
     
    371372}
    372373
     374// --------------------------------------------------------------------------
     375//
     376// Remove all whitespaces
     377// Replace all kind of double +/- by a single + or -
     378//
    373379void MDataChain::SimplifyString(TString &txt) const
    374380{
     381    txt.ReplaceAll(" ",  "");
     382    txt.ReplaceAll("**", "^");
     383
    375384    while (txt.First("--")>=0 || txt.First("++")>=0 ||
    376385           txt.First("+-")>=0 || txt.First("-+")>=0)
     
    385394// --------------------------------------------------------------------------
    386395//
     396// Add Parenthesis depending on the priority of the operators. The
     397// priorities are defined by the default of the second argument.
     398//
     399void 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    //cout << "--- " << Form("[%c]", op[0]) << " ---" << endl;
     406
     407    int offset = 0;
     408    while (1)
     409    {
     410        const TString check = test(offset, test.Length());
     411        if (check.IsNull())
     412            break;
     413
     414        const Ssiz_t pos = check.First(op[0]);
     415        if (pos<0)
     416            break;
     417
     418        int cnt=0;
     419
     420        int i;
     421        for (i=pos-1; i>=0; i--)
     422        {
     423            if (check[i]==')')
     424                cnt++;
     425            if (check[i]=='(')
     426                cnt--;
     427            if (cnt>0 || low.First(check[i])<0)
     428                continue;
     429            break;
     430        }
     431
     432        cnt=0;
     433        int j;
     434        for (j=pos; j<check.Length(); j++)
     435        {
     436            if (check[j]=='(')
     437                cnt++;
     438            if (check[j]==')')
     439                cnt--;
     440            if (cnt>0 || low.First(check[j])<0)
     441                continue;
     442            break;
     443        }
     444
     445        const TString sub = test(offset+i+1, j-i-1);
     446
     447        // Check if it contains operators,
     448        // otherwise we can simply skip it
     449        static const TRegexp regexp("[%/*^+-]");
     450        if (sub.Index(regexp)>0)
     451        {
     452            test.Insert(offset+j,   ")");
     453            test.Insert(offset+i+1, "(");
     454            offset += 2;
     455        }
     456        offset += j+1;
     457    }
     458
     459    AddParenthesis(test, low);
     460}
     461
     462// --------------------------------------------------------------------------
     463//
    387464// Core of the data chain. Here the chain is constructed out of the rule.
    388465//
     
    390467{
    391468    if (level==0)
     469    {
    392470        SimplifyString(txt);
     471        AddParenthesis(txt);
     472    }
    393473
    394474    MData *member0=NULL;
     
    828908    return kTRUE;
    829909}
     910/*
     911void MDataChain::ReconstructElements()
     912{
     913    if (!fMember)
     914        return;
     915
     916    if (fMember->InheritsFrom(MDataElement::Class()))
     917    {
     918        MData *data = ((MDataElement*)fMember)->CloneColumn();
     919        delete fMember;
     920        fMember = data;
     921    }
     922
     923    fMember->ReconstructElements();
     924}
     925*/
  • trunk/MagicSoft/Mars/mdata/MDataChain.h

    r5956 r6569  
    4545
    4646    void   SimplifyString(TString &txt) const;
     47    void   AddParenthesis(TString &test, const TString op="^%*/+-") const;
    4748    MData *ParseString(TString txt, Int_t level);
    4849    MData *ParseDataMember(TString txt);
     
    6263    TString GetRule() const;
    6364    TString GetDataMember() const;
     65    //void    ReconstructElements() { if (fMember) fMember->ReconstructElements(); }
    6466
    6567    // MParContainer
Note: See TracChangeset for help on using the changeset viewer.