Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 9229)
+++ trunk/MagicSoft/Mars/Changelog	(revision 9230)
@@ -18,4 +18,15 @@
 
                                                  -*-*- END OF LINE -*-*-
+
+ 2009/01/19 Thomas Bretz
+
+   * mfileio/MWriteRootFile.cc:
+     - simplified how the rules are evaluated using TPregexp
+     - by this the rules got more powerful
+
+   * mjobs/MJCalibrateSignal.cc, mjobs/MJStar.cc:
+     - changed ols style rule to new style
+
+
 
  2009/01/18 Thomas Bretz
Index: trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc	(revision 9229)
+++ trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc	(revision 9230)
@@ -18,5 +18,5 @@
 !   Author(s): Thomas Bretz, 6/2001 <mailto:tbretz@astro.uni-wuerzburg.de>
 !
-!   Copyright: MAGIC Software Development, 2000-2005
+!   Copyright: Software Development, 2000-2009
 !
 !
@@ -65,5 +65,5 @@
 #include <TFile.h>
 #include <TTree.h>
-#include <TRegexp.h>
+#include <TPRegexp.h>
 
 #include "MLog.h"
@@ -810,18 +810,18 @@
 //
 // A rule looks like:
-//   "outputpath{s/source/destination}"
-//
-// outpath:                the output path into which the files are written
-// {s/source/destination}  a substitution rule for the filename
-//   while source can be a regular expression everything which matches this
-//   regular expression (see TRegexp) will be replaced by destination.
-//   Warning: The algorithm is recursive you may create endless loops!
+//   "s/source/destination/"
+//
+// For more details on regular expression see a proper documentation.
+//
+// Here is an example:
 //
 // Example:
 //   inputfile: /data/MAGIC/Period016/rootdata/20040621_23210_D_Mkn421_E.root
-//   rule:      /outpath/{s/_D_/_Y_}
+//   rule:      /([0-9]+_[0-9]+)_D_(.*[.]root)/\\/outpath\\/$1_Y_$2/
 //   outfile:   /outpath/20040621_23210_Y_Mkn421_E.root
 //
-// If you need more difficult rules please send me an eMail...
+// Please make sure that all / in your rules are correctly escaped, i.e.
+// in the string stored in memory it must look like \/ and in the string
+// your set in your program it must look \\/.
 //
 TString MWriteRootFile::GetNewFileName(const char *inname) const
@@ -832,75 +832,40 @@
         fname.Remove(0, fname.Last('/')+1);
 
-    // Make a copy of the rule
-    TString rule(fSplitRule);
-
-    // [characte class], ^ do not, [^{}] do not match { and }, [^{}]+ match at least not one { or }
-    const TRegexp subst0("{s/[^{}/]+/[^{}/]+}");
-
-    TString path;
-    Bool_t first = kTRUE;
-
-    Ssiz_t idx=0;
-    while (1)
-    {
-        // Find a substitution exprsssion
-        Ssiz_t len = 0;
-        idx = subst0.Index(rule, &len);
-        if (idx<0)
-            break;
-
-        // If the first substitution expression is found in the rule
-        // determin the path from everything before
-        if (first)
-        {
-            path=rule(0, idx);
-            first = kFALSE;
-        }
-
-        // Extract a substitution expression
-        TString expr = rule(idx, len);
-        rule.Remove(idx, len);
-
-        expr.Remove(0,3);
-        expr.Remove(expr.Length()-1);
-
-        // In all cases this is well defined (see Regexp)
-        const Ssiz_t pos = expr.First('/');
-
-        // Split substitution rule into source and destination
-        const TString src  = expr(0, pos);
-        const TString dest = expr(pos+1, expr.Length());
-
-        // Replace source by destination
-        const TRegexp regexp(src);
-
-        Ssiz_t ichar = 0;
-        while (1) // Replace all occurrences of src by dest
-        {
-            idx = regexp.Index(fname, &len, ichar);
-            if (idx<0)
-                break;
-
-            fname.Replace(idx, len, dest);
-	    ichar = idx + dest.Length();
-
-            // In next iteration, we start searching the string fname
-	    // right after the last character of the previous substitution
-	    // (indicated by ichar). This avoids infinite loops in the case 
-	    // we want to replace, for instance, "_w" by "_Y_w". Without 
-	    // the use of ichar, the second string would be replaced by 
-	    // "_Y_Y_w", and this would never end... 
-        }
-    }
-
-    // Check if we have a trailing '/'
-    if (!path.IsNull() && path[path.Length()-1]!='/')
-        path.Append("/");
-
-    //inname.Prepend(path);
-
-    // Create full qualified pathname
-    path += fname;
-    return path;
+    // Regular expression to split the rule into its contents
+    static const TString sed("s/((\\\\/|[^/])*)/((\\\\/|[^/])*)/([gimosxd]*)");
+
+    // Do splitting
+    TObjArray *subStrL = TPRegexp(sed).MatchS(fSplitRule);
+    if (subStrL->GetEntries()!=6)
+    {
+        *fLog << err << "ERROR - GetNewFileName: Evaluating split rule " << fSplitRule << " failed." << endl;
+        subStrL->Print();
+        delete subStrL;
+        return "";
+    }
+
+    /*const*/ TString reg = (*subStrL)[1]->GetName(); // Regular expression to search for
+    /*const*/ TString tar = (*subStrL)[3]->GetName(); // Regular expression for replacing
+      const   TString mod = (*subStrL)[5]->GetName(); // Possible modifiers (e.g. 'a')
+
+    delete subStrL;
+
+    // Unescpae slashes in paths
+    reg.ReplaceAll("\\/", "/");
+    tar.ReplaceAll("\\/", "/");
+
+    // Do substitution
+    const Int_t nrSub = TPRegexp(reg).Substitute(fname, tar, mod);
+    if (nrSub==0)
+    {
+        gLog << err << "ERROR - Substituting due to SplitRule failed." << endl;
+        gLog << " Source FileName:  " << fname << endl;
+        gLog << " Search  Rexexp:   " << reg   << endl;
+        gLog << " Replace Rexexp:   " << tar   << endl;
+        gLog << " Modifiers:        " << mod   << endl;
+        return "";
+    }
+
+    return fname;
 }
 
Index: trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.cc	(revision 9229)
+++ trunk/MagicSoft/Mars/mjobs/MJCalibrateSignal.cc	(revision 9230)
@@ -380,5 +380,6 @@
     read->AddFiles(iter);
 
-    const TString fname(Form("%s{s/_D_/_Y_}{s/\\.raw$/.root}{s/\\.raw\\.gz$/.root}", fPathOut.Data()));
+    const TString fname(Form("s/([0-9]+_[0-9]+)_D_(.*[.])(raw([.]gz)?)$/%s\\/$1_Y_$2root/",
+                             Esc(fPathOut).Data()));
 
     // Skips MC which have no contents. This are precisely the
@@ -389,5 +390,5 @@
     //MPointingPosInterpolate pextr;
     //pextr.AddFiles(&iter);
-    
+
     MGeomApply             apply; // Only necessary to create geometry
     if (!geom.IsNull())
Index: trunk/MagicSoft/Mars/mjobs/MJStar.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJStar.cc	(revision 9229)
+++ trunk/MagicSoft/Mars/mjobs/MJStar.cc	(revision 9230)
@@ -302,5 +302,5 @@
     // Effective on-time need its own not to be skipped by (eg) image cleaning
     // Muons needs its own to have a unique SetReadyToSave
-    const TString rule(Form("%s{s/_Y_/_I_}", fPathOut.Data()));
+    const TString rule(Form("s/([0-9]+_[0-9]+)_Y_(.*[.]root)$/%s\\/$1_I_$2/", Esc(fPathOut).Data()));
     MWriteRootFile write( 2, rule, fOverwrite?"RECREATE":"NEW");
     MWriteRootFile writet(2, rule, fOverwrite?"RECREATE":"NEW"); // EffectiveOnTime
