]>
Commit | Line | Data |
---|---|---|
ba030c0e | 1 | /************************************************************************** |
2 | * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. * | |
3 | * * | |
4 | * Author: The ALICE Off-line Project. * | |
5 | * Contributors are mentioned in the code where appropriate. * | |
6 | * * | |
7 | * Permission to use, copy, modify and distribute this software and its * | |
8 | * documentation strictly for non-commercial purposes is hereby granted * | |
9 | * without fee, provided that the above copyright notice appears in all * | |
10 | * copies and that both the copyright notice and this permission notice * | |
11 | * appear in the supporting documentation. The authors make no claims * | |
12 | * about the suitability of this software for any purpose. It is * | |
13 | * provided "as is" without express or implied warranty. * | |
14 | **************************************************************************/ | |
15 | ||
88cb7938 | 16 | /* $Id$ */ |
ba030c0e | 17 | |
18 | // Authors: David Guez, Ivana Hrivnacova, Marion MacCormick; IPN Orsay | |
19 | // | |
20 | // Class AliMUONSt1Decoder | |
21 | // ----------------------- | |
22 | // A generic set of functions (defined in the <decoder> namespace). | |
23 | // Used to decode formatted strings, eg. a list of integer ranges, | |
24 | // or a list of sub-strings separated by delimiters such as '(','{','[', ... . | |
25 | // Example: | |
26 | // (string 1) (string 2) [string3] {string4} [ (string5.1) (string5.2) ] | |
27 | // note : |_____________________| | |
28 | // | | |
29 | // this is just ONE substring. | |
5f1df83a | 30 | // Included in AliRoot 2003/01/28 |
ba030c0e | 31 | |
32 | #include "AliMUONSt1Decoder.h" | |
33 | ||
ba030c0e | 34 | //_____________________________________________________________________ |
1eb40170 | 35 | StringVector decoder::SplitNtuples(const string& s, |
36 | const string& leftSep, | |
37 | const string& rightSep) | |
ba030c0e | 38 | |
39 | { | |
40 | // separate substrings in <s>, by using the first level of delimiters | |
41 | // given in the argument | |
42 | // Example: | |
43 | // (string 1) (string 2) [string3] {string4} [ (string5.1) (string5.2) ] | |
44 | // returns a list of 5 substrings | |
45 | // "string 1" | |
46 | // "string 2" | |
47 | // "string 3" | |
48 | // "string 4" and | |
49 | // " (string5.1) (string5.2) " | |
50 | // -- | |
1eb40170 | 51 | StringVector ans; |
ba030c0e | 52 | string::size_type idx = 0; |
53 | do { | |
54 | idx = s.find_first_of(leftSep,idx); | |
55 | if (idx != string::npos) { | |
56 | string::size_type sepNum = leftSep.find_first_of(s[idx],0); | |
57 | if (sepNum>=rightSep.length()){ | |
58 | idx++; | |
59 | continue; | |
60 | } | |
1eb40170 | 61 | Int_t count=1; |
ba030c0e | 62 | string::size_type idx2 = idx+1; |
63 | while ((count>0) && (idx2<s.length())){ | |
64 | if (s[idx2] == leftSep[sepNum]) count++; | |
65 | if (s[idx2] == rightSep[sepNum]) count--; | |
66 | idx2++; | |
67 | } | |
68 | if (count != 0) return ans; // bad format...stop here | |
69 | ans.push_back(s.substr(idx+1,idx2-idx-2)); | |
70 | idx=idx2; | |
71 | } | |
72 | } while (idx != string::npos); | |
73 | return ans; | |
74 | } | |
75 | ||
76 | //_____________________________________________________________________ | |
1eb40170 | 77 | StringVector decoder::SplitList(const string& s,const string& sep) |
ba030c0e | 78 | { |
79 | // split <s> into several substrings, by using any of the delimters in <sep> | |
80 | // Example : str1 ; str2 , str3 | |
81 | // gives a list of 3 substrings ("str1", "str2" and "str3") if | |
82 | // the delimiter parameter is ",;" | |
83 | // and a list of 2 substrings ("str1","str2,str3") if | |
84 | // the delimiter parameter is ";" | |
85 | // -- | |
1eb40170 | 86 | StringVector ans; |
ba030c0e | 87 | string::size_type i=0,j; |
88 | while ((j=s.find_first_of(sep,i))!=string::npos){ | |
89 | ans.push_back(s.substr(i,j-i)); | |
90 | i=j+1; | |
91 | } | |
92 | ans.push_back(s.substr(i,s.length()-i)); | |
93 | return ans; | |
94 | } | |
95 | ||
96 | //_____________________________________________________________________ | |
1eb40170 | 97 | IntVector |
98 | decoder::DecodeListRanges(const string& s, const string& sep, | |
99 | const string& rangeSep) | |
ba030c0e | 100 | { |
101 | // decode <s> as a list of integers | |
102 | // Example: 192/199 ; -10/-7 | |
103 | // gives a list of 12 integers: | |
104 | // 192, 193, 194, 195, 196, 197, 198, 199, -10, -9, -8, -7 | |
105 | // -- | |
1eb40170 | 106 | IntVector ans; |
107 | IntPairVector rangeList = DecodeListOfIntRanges(s,sep,rangeSep); | |
108 | for (UInt_t i = 0 ;i<rangeList.size();i++){ | |
109 | for (Int_t j=rangeList[i].first;j<=rangeList[i].second;j++){ | |
ba030c0e | 110 | ans.push_back(j); |
111 | } | |
112 | } | |
113 | return ans; | |
114 | } | |
115 | ||
116 | //_____________________________________________________________________ | |
1eb40170 | 117 | IntPairVector |
118 | decoder::DecodeListOfIntRanges(const string& s, const string& sep, | |
119 | const string& rangeSep) | |
ba030c0e | 120 | { |
121 | // decodes <s> as a list of int ranges | |
122 | // Example: 192/303 ; -10/-1 | |
123 | // gives a list of two int pairs: | |
124 | // pair(192,303) and another pair (-10,-1) | |
125 | // -- | |
126 | string::size_type i=0; | |
1eb40170 | 127 | IntPairVector ans; |
ba030c0e | 128 | if (s.empty()) return ans; |
129 | ||
1eb40170 | 130 | StringVector parts = decoder::SplitList(s,sep); |
ba030c0e | 131 | |
1eb40170 | 132 | for (UInt_t k=0;k<parts.size();k++){ |
ba030c0e | 133 | i=parts[k].find_first_of(rangeSep); |
1eb40170 | 134 | Int_t from,to; |
ba030c0e | 135 | if (i == string::npos) { |
136 | from = atoi(parts[k].c_str()); | |
137 | to = from; | |
138 | } else { | |
139 | from=atoi(parts[k].substr(0,i).c_str()); | |
140 | to =atoi(parts[k].substr(i+1,parts[k].length()-i-1).c_str()); | |
141 | } | |
1eb40170 | 142 | ans.push_back(IntPair(from,to)); |
ba030c0e | 143 | } |
144 | return ans; | |
145 | } | |
146 | ||
147 | //_____________________________________________________________________ | |
1eb40170 | 148 | DoublePairVector |
149 | decoder::DecodeListOfFloatRanges(const string& s, const string& sep, | |
150 | const string& rangeSep) | |
ba030c0e | 151 | { |
152 | // decodes <s> as a list of double (floating point) ranges | |
153 | // Example : 192.33/303.26 ; -10.2/-1.41 | |
154 | // gives a list of two double precision floating point (phew!) pairs: | |
155 | // pair (192.33,303.26) and another pair (-10.2,-1.41) | |
156 | // -- | |
157 | string::size_type i=0; | |
1eb40170 | 158 | DoublePairVector ans; |
ba030c0e | 159 | if (s.empty()) return ans; |
160 | ||
1eb40170 | 161 | StringVector parts = decoder::SplitList(s,sep); |
ba030c0e | 162 | |
1eb40170 | 163 | for (UInt_t k=0;k<parts.size();k++){ |
ba030c0e | 164 | i=parts[k].find_first_of(rangeSep); |
1eb40170 | 165 | Double_t from,to; |
ba030c0e | 166 | if (i == string::npos) { |
167 | from = atof(parts[k].c_str()); | |
168 | to = from; | |
169 | } else { | |
170 | from=atof(parts[k].substr(0,i).c_str()); | |
171 | to =atof(parts[k].substr(i+1,parts[k].length()-i-1).c_str()); | |
172 | } | |
1eb40170 | 173 | ans.push_back(DoublePair(from,to)); |
ba030c0e | 174 | } |
175 | return ans; | |
176 | } |