Code for MUON Station1 (I.Hrivnacova)
[u/mrichter/AliRoot.git] / MUON / AliMUONSt1Decoder.cxx
CommitLineData
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
16/*
17$Log$
5f91c9e8 18Revision 1.1 2003/01/28 13:21:06 morsch
19Improved response simulation for station 1.
20(M. Mac Cormick, I. Hrivnacova, D. Guez)
21
ba030c0e 22*/
23
24// Authors: David Guez, Ivana Hrivnacova, Marion MacCormick; IPN Orsay
25//
26// Class AliMUONSt1Decoder
27// -----------------------
28// A generic set of functions (defined in the <decoder> namespace).
29// Used to decode formatted strings, eg. a list of integer ranges,
30// or a list of sub-strings separated by delimiters such as '(','{','[', ... .
31// Example:
32// (string 1) (string 2) [string3] {string4} [ (string5.1) (string5.2) ]
33// note : |_____________________|
34// |
35// this is just ONE substring.
36
37
38#include "AliMUONSt1Decoder.h"
39
ba030c0e 40//_____________________________________________________________________
41vector<string> decoder::SplitNtuples(const string& s,
42 const string& leftSep,
43 const string& rightSep)
44
45{
46// separate substrings in <s>, by using the first level of delimiters
47// given in the argument
48// Example:
49// (string 1) (string 2) [string3] {string4} [ (string5.1) (string5.2) ]
50// returns a list of 5 substrings
51// "string 1"
52// "string 2"
53// "string 3"
54// "string 4" and
55// " (string5.1) (string5.2) "
56// --
57 vector<string> ans;
58 string::size_type idx = 0;
59 do {
60 idx = s.find_first_of(leftSep,idx);
61 if (idx != string::npos) {
62 string::size_type sepNum = leftSep.find_first_of(s[idx],0);
63 if (sepNum>=rightSep.length()){
64 idx++;
65 continue;
66 }
67 int count=1;
68 string::size_type idx2 = idx+1;
69 while ((count>0) && (idx2<s.length())){
70 if (s[idx2] == leftSep[sepNum]) count++;
71 if (s[idx2] == rightSep[sepNum]) count--;
72 idx2++;
73 }
74 if (count != 0) return ans; // bad format...stop here
75 ans.push_back(s.substr(idx+1,idx2-idx-2));
76 idx=idx2;
77 }
78 } while (idx != string::npos);
79 return ans;
80}
81
82//_____________________________________________________________________
83vector<string> decoder::SplitList(const string& s,const string& sep)
84{
85// split <s> into several substrings, by using any of the delimters in <sep>
86// Example : str1 ; str2 , str3
87// gives a list of 3 substrings ("str1", "str2" and "str3") if
88// the delimiter parameter is ",;"
89// and a list of 2 substrings ("str1","str2,str3") if
90// the delimiter parameter is ";"
91// --
92 vector<string> ans;
93 string::size_type i=0,j;
94 while ((j=s.find_first_of(sep,i))!=string::npos){
95 ans.push_back(s.substr(i,j-i));
96 i=j+1;
97 }
98 ans.push_back(s.substr(i,s.length()-i));
99 return ans;
100}
101
102//_____________________________________________________________________
103vector<int>
104decoder::DecodeListRanges(const string& s,const string& sep,const string& rangeSep)
105{
106// decode <s> as a list of integers
107// Example: 192/199 ; -10/-7
108// gives a list of 12 integers:
109// 192, 193, 194, 195, 196, 197, 198, 199, -10, -9, -8, -7
110// --
111 vector<int> ans;
112 vector< pair <int,int> > rangeList = DecodeListOfIntRanges(s,sep,rangeSep);
113 for (unsigned int i = 0 ;i<rangeList.size();i++){
114 for (int j=rangeList[i].first;j<=rangeList[i].second;j++){
115 ans.push_back(j);
116 }
117 }
118 return ans;
119}
120
121//_____________________________________________________________________
122vector< pair <int,int> >
123decoder::DecodeListOfIntRanges(const string& s,const string& sep,const string& rangeSep)
124{
125// decodes <s> as a list of int ranges
126// Example: 192/303 ; -10/-1
127// gives a list of two int pairs:
128// pair(192,303) and another pair (-10,-1)
129// --
130 string::size_type i=0;
131 vector< pair <int,int> > ans;
132 if (s.empty()) return ans;
133
134 vector<string> parts = decoder::SplitList(s,sep);
135
136 for (unsigned int k=0;k<parts.size();k++){
137 i=parts[k].find_first_of(rangeSep);
138 int from,to;
139 if (i == string::npos) {
140 from = atoi(parts[k].c_str());
141 to = from;
142 } else {
143 from=atoi(parts[k].substr(0,i).c_str());
144 to =atoi(parts[k].substr(i+1,parts[k].length()-i-1).c_str());
145 }
146 ans.push_back(pair<int,int>(from,to));
147 }
148 return ans;
149}
150
151//_____________________________________________________________________
152vector< pair <double,double> >
153decoder::DecodeListOfFloatRanges(const string& s,const string& sep,const string& rangeSep)
154{
155// decodes <s> as a list of double (floating point) ranges
156// Example : 192.33/303.26 ; -10.2/-1.41
157// gives a list of two double precision floating point (phew!) pairs:
158// pair (192.33,303.26) and another pair (-10.2,-1.41)
159// --
160 string::size_type i=0;
161 vector< pair <double,double> > ans;
162 if (s.empty()) return ans;
163
164 vector<string> parts = decoder::SplitList(s,sep);
165
166 for (unsigned int k=0;k<parts.size();k++){
167 i=parts[k].find_first_of(rangeSep);
168 double from,to;
169 if (i == string::npos) {
170 from = atof(parts[k].c_str());
171 to = from;
172 } else {
173 from=atof(parts[k].substr(0,i).c_str());
174 to =atof(parts[k].substr(i+1,parts[k].length()-i-1).c_str());
175 }
176 ans.push_back(pair<double,double>(from,to));
177 }
178 return ans;
179}