Index: trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc	(revision 6949)
+++ trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc	(revision 6994)
@@ -856,11 +856,21 @@
         // Replace source by destination
         const TRegexp regexp(src);
-        while (1)
-        {
-            idx = regexp.Index(fname, &len);
+
+        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... 
         }
     }
