Changeset 9230 for trunk/MagicSoft/Mars/mfileio
- Timestamp:
- 01/19/09 19:40:49 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc
r9219 r9230 18 18 ! Author(s): Thomas Bretz, 6/2001 <mailto:tbretz@astro.uni-wuerzburg.de> 19 19 ! 20 ! Copyright: MAGIC Software Development, 2000-200520 ! Copyright: Software Development, 2000-2009 21 21 ! 22 22 ! … … 65 65 #include <TFile.h> 66 66 #include <TTree.h> 67 #include <T Regexp.h>67 #include <TPRegexp.h> 68 68 69 69 #include "MLog.h" … … 810 810 // 811 811 // A rule looks like: 812 // "outputpath{s/source/destination}" 813 // 814 // outpath: the output path into which the files are written 815 // {s/source/destination} a substitution rule for the filename 816 // while source can be a regular expression everything which matches this 817 // regular expression (see TRegexp) will be replaced by destination. 818 // Warning: The algorithm is recursive you may create endless loops! 812 // "s/source/destination/" 813 // 814 // For more details on regular expression see a proper documentation. 815 // 816 // Here is an example: 819 817 // 820 818 // Example: 821 819 // inputfile: /data/MAGIC/Period016/rootdata/20040621_23210_D_Mkn421_E.root 822 // rule: / outpath/{s/_D_/_Y_}820 // rule: /([0-9]+_[0-9]+)_D_(.*[.]root)/\\/outpath\\/$1_Y_$2/ 823 821 // outfile: /outpath/20040621_23210_Y_Mkn421_E.root 824 822 // 825 // If you need more difficult rules please send me an eMail... 823 // Please make sure that all / in your rules are correctly escaped, i.e. 824 // in the string stored in memory it must look like \/ and in the string 825 // your set in your program it must look \\/. 826 826 // 827 827 TString MWriteRootFile::GetNewFileName(const char *inname) const … … 832 832 fname.Remove(0, fname.Last('/')+1); 833 833 834 // Make a copy of the rule 835 TString rule(fSplitRule); 836 837 // [characte class], ^ do not, [^{}] do not match { and }, [^{}]+ match at least not one { or } 838 const TRegexp subst0("{s/[^{}/]+/[^{}/]+}"); 839 840 TString path; 841 Bool_t first = kTRUE; 842 843 Ssiz_t idx=0; 844 while (1) 845 { 846 // Find a substitution exprsssion 847 Ssiz_t len = 0; 848 idx = subst0.Index(rule, &len); 849 if (idx<0) 850 break; 851 852 // If the first substitution expression is found in the rule 853 // determin the path from everything before 854 if (first) 855 { 856 path=rule(0, idx); 857 first = kFALSE; 858 } 859 860 // Extract a substitution expression 861 TString expr = rule(idx, len); 862 rule.Remove(idx, len); 863 864 expr.Remove(0,3); 865 expr.Remove(expr.Length()-1); 866 867 // In all cases this is well defined (see Regexp) 868 const Ssiz_t pos = expr.First('/'); 869 870 // Split substitution rule into source and destination 871 const TString src = expr(0, pos); 872 const TString dest = expr(pos+1, expr.Length()); 873 874 // Replace source by destination 875 const TRegexp regexp(src); 876 877 Ssiz_t ichar = 0; 878 while (1) // Replace all occurrences of src by dest 879 { 880 idx = regexp.Index(fname, &len, ichar); 881 if (idx<0) 882 break; 883 884 fname.Replace(idx, len, dest); 885 ichar = idx + dest.Length(); 886 887 // In next iteration, we start searching the string fname 888 // right after the last character of the previous substitution 889 // (indicated by ichar). This avoids infinite loops in the case 890 // we want to replace, for instance, "_w" by "_Y_w". Without 891 // the use of ichar, the second string would be replaced by 892 // "_Y_Y_w", and this would never end... 893 } 894 } 895 896 // Check if we have a trailing '/' 897 if (!path.IsNull() && path[path.Length()-1]!='/') 898 path.Append("/"); 899 900 //inname.Prepend(path); 901 902 // Create full qualified pathname 903 path += fname; 904 return path; 834 // Regular expression to split the rule into its contents 835 static const TString sed("s/((\\\\/|[^/])*)/((\\\\/|[^/])*)/([gimosxd]*)"); 836 837 // Do splitting 838 TObjArray *subStrL = TPRegexp(sed).MatchS(fSplitRule); 839 if (subStrL->GetEntries()!=6) 840 { 841 *fLog << err << "ERROR - GetNewFileName: Evaluating split rule " << fSplitRule << " failed." << endl; 842 subStrL->Print(); 843 delete subStrL; 844 return ""; 845 } 846 847 /*const*/ TString reg = (*subStrL)[1]->GetName(); // Regular expression to search for 848 /*const*/ TString tar = (*subStrL)[3]->GetName(); // Regular expression for replacing 849 const TString mod = (*subStrL)[5]->GetName(); // Possible modifiers (e.g. 'a') 850 851 delete subStrL; 852 853 // Unescpae slashes in paths 854 reg.ReplaceAll("\\/", "/"); 855 tar.ReplaceAll("\\/", "/"); 856 857 // Do substitution 858 const Int_t nrSub = TPRegexp(reg).Substitute(fname, tar, mod); 859 if (nrSub==0) 860 { 861 gLog << err << "ERROR - Substituting due to SplitRule failed." << endl; 862 gLog << " Source FileName: " << fname << endl; 863 gLog << " Search Rexexp: " << reg << endl; 864 gLog << " Replace Rexexp: " << tar << endl; 865 gLog << " Modifiers: " << mod << endl; 866 return ""; 867 } 868 869 return fname; 905 870 } 906 871
Note:
See TracChangeset
for help on using the changeset viewer.