Index: trunk/MagicSoft/Mars/mdata/MDataChain.cc
===================================================================
--- trunk/MagicSoft/Mars/mdata/MDataChain.cc	(revision 6515)
+++ trunk/MagicSoft/Mars/mdata/MDataChain.cc	(revision 6569)
@@ -52,18 +52,18 @@
 // ---------
 //
-// The allowed operations are: +, -, *, /, %, ^
+// The allowed operations are: +, -, *, /, %, ^ and ** for ^
 //
 // While a%b returns the floating point reminder of a/b.
 // While a^b returns a to the power of b
 //
-// Warning: There is no priority rule build in. So better use parantheses
-//   to get correct results. The rule is parsed/evaluated from the left
-//   to the right, which means:
-//
-//   "MHillas.fWidth + MHillas.fLength / HillasSource.fDist"
-//
-//    is parses as
-//
-//   "(MHillas.fWidth + MHillas.fLength) / HillasSource.fDist"
+// Priority
+// --------
+//
+//  The priority of the operators is build in as:
+//    ^ (highest),  %, *, /, +, -
+//
+//  Priorities are evaluated before parsing the rule by inserting
+//  artificial paranthesis. If you find ANY problem with this please
+//  report it immediatly! It is highly recommended to check the result!
 //
 // You can also use mathmatical operators, eg:
@@ -168,4 +168,5 @@
 #include <stdlib.h>       // strtod, ...
 
+#include <TRegexp.h>
 #include <TRandom.h>
 #include <TMethodCall.h>
@@ -371,6 +372,14 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Remove all whitespaces
+// Replace all kind of double +/- by a single + or -
+//
 void MDataChain::SimplifyString(TString &txt) const
 {
+    txt.ReplaceAll(" ",  "");
+    txt.ReplaceAll("**", "^");
+
     while (txt.First("--")>=0 || txt.First("++")>=0 ||
            txt.First("+-")>=0 || txt.First("-+")>=0)
@@ -385,4 +394,72 @@
 // --------------------------------------------------------------------------
 //
+// Add Parenthesis depending on the priority of the operators. The
+// priorities are defined by the default of the second argument.
+//
+void MDataChain::AddParenthesis(TString &test, const TString op) const
+{
+    const TString low = op(1, op.Length());
+    if (low.Length()==0)
+        return;
+
+    //cout << "--- " << Form("[%c]", op[0]) << " ---" << endl;
+
+    int offset = 0;
+    while (1)
+    {
+        const TString check = test(offset, test.Length());
+        if (check.IsNull())
+            break;
+
+        const Ssiz_t pos = check.First(op[0]);
+        if (pos<0)
+            break;
+
+        int cnt=0;
+
+        int i;
+        for (i=pos-1; i>=0; i--)
+        {
+            if (check[i]==')')
+                cnt++;
+            if (check[i]=='(')
+                cnt--;
+            if (cnt>0 || low.First(check[i])<0)
+                continue;
+            break;
+        }
+
+        cnt=0;
+        int j;
+        for (j=pos; j<check.Length(); j++)
+        {
+            if (check[j]=='(')
+                cnt++;
+            if (check[j]==')')
+                cnt--;
+            if (cnt>0 || low.First(check[j])<0)
+                continue;
+            break;
+        }
+
+        const TString sub = test(offset+i+1, j-i-1);
+
+        // Check if it contains operators,
+        // otherwise we can simply skip it
+        static const TRegexp regexp("[%/*^+-]");
+        if (sub.Index(regexp)>0)
+        {
+            test.Insert(offset+j,   ")");
+            test.Insert(offset+i+1, "(");
+            offset += 2;
+        }
+        offset += j+1;
+    }
+
+    AddParenthesis(test, low);
+}
+
+// --------------------------------------------------------------------------
+//
 // Core of the data chain. Here the chain is constructed out of the rule.
 //
@@ -390,5 +467,8 @@
 {
     if (level==0)
+    {
         SimplifyString(txt);
+        AddParenthesis(txt);
+    }
 
     MData *member0=NULL;
@@ -828,2 +908,18 @@
     return kTRUE;
 }
+/*
+void MDataChain::ReconstructElements()
+{
+    if (!fMember)
+        return;
+
+    if (fMember->InheritsFrom(MDataElement::Class()))
+    {
+        MData *data = ((MDataElement*)fMember)->CloneColumn();
+        delete fMember;
+        fMember = data;
+    }
+
+    fMember->ReconstructElements();
+}
+*/
Index: trunk/MagicSoft/Mars/mdata/MDataChain.h
===================================================================
--- trunk/MagicSoft/Mars/mdata/MDataChain.h	(revision 6515)
+++ trunk/MagicSoft/Mars/mdata/MDataChain.h	(revision 6569)
@@ -45,4 +45,5 @@
 
     void   SimplifyString(TString &txt) const;
+    void   AddParenthesis(TString &test, const TString op="^%*/+-") const;
     MData *ParseString(TString txt, Int_t level);
     MData *ParseDataMember(TString txt);
@@ -62,4 +63,5 @@
     TString GetRule() const;
     TString GetDataMember() const;
+    //void    ReconstructElements() { if (fMember) fMember->ReconstructElements(); }
 
     // MParContainer
