ATLAS Offline Software
utils.cxx
Go to the documentation of this file.
1 
22 #include <string>
23 #include <vector>
24 #include <iostream>
25 //#include <math.h>
26 #include <cmath>
27 
28 #include <stdio.h>
29 
30 #include "utils.h"
31 #include <algorithm>
32 // chop tokens off the end of a string, leave string unchanged
33 // return choped string
34 std::string choptoken(std::string& s1, const std::string& s2)
35 {
36  std::string s3 = "";
37  std::string::size_type pos = s1.find(s2);
38  if ( pos != std::string::npos ) {
39  s3 = s1.substr(0, pos+1);
40  s1.erase(0, pos+1);
41  }
42  return s3;
43 }
44 
45 // chop tokens off the end of a string, leave string unchanged
46 // return choped string
47 std::string chomptoken(std::string& s1, const std::string& s2)
48 {
49  std::string s3 = "";
50  std::string::size_type pos = s1.find_last_of(s2);
51  if ( pos != std::string::npos ) {
52  s3 = s1.substr(pos, s1.size());
53  s1.erase(pos, s1.size());
54  }
55  return s3;
56 }
57 
58 
59 
60 std::string chopfirst(std::string& s1, const std::string& s2)
61 {
62  std::string s3;
63  std::string::size_type pos = s1.find_first_not_of(s2);
64  if ( pos != std::string::npos ) {
65  s3 = s1.substr(0, pos);
66  s1.erase(0, pos);
67  }
68  else {
69  s3 = s1;
70  s1.clear();
71  }
72  return s3;
73 }
74 
75 // chop tokens off the front of a string
76 std::string chopends(std::string& s1, const std::string& s2)
77 {
78  chopfirst(s1, s2);
79  choplast(s1, s2);
80  return s1;
81 }
82 
83 
84 
85 std::string choplast(std::string& s1, const std::string& s2)
86 {
87  std::string s3 = "";
88  std::string::size_type pos = s1.find_last_not_of(s2);
89  if ( pos != std::string::npos ) {
90  s3 = s1.substr(pos+1, s1.size());
91  s1.erase(pos+1, s1.size());
92  }
93  return s3;
94 }
95 
96 
97 // chop tokens off the front of a string
98 std::string chop(std::string& s1, const std::string& s2)
99 {
100  std::string::size_type pos = s1.find(s2);
101  std::string s3;
102  if ( pos == std::string::npos ) {
103  s3 = std::move(s1);
104  s1.clear();
105  }
106  else {
107  s3 = s1.substr(0, pos);
108  s1.erase(0, pos+s2.size());
109  }
110  return s3;
111 }
112 
113 
114 // chomp them off the end
115 std::string chomp(std::string& s1, const std::string& s2)
116 {
117  std::string::size_type pos = s1.find(s2);
118  std::string s3;
119  if ( pos == std::string::npos ) {
120  s3 = std::move(s1);
121  s1.clear();
122  }
123  else {
124  s3 = s1.substr(pos+s2.size(),s1.size());
125  s1.erase(pos,s1.size());
126  }
127  return s3;
128 }
129 
130 
131 // remove strings from a string
132 void removespace(std::string& s, const std::string& s2)
133 {
134  std::string::size_type pos;
135  while ( (pos = s.find(s2))!=std::string::npos ) {
136  s.erase(pos, 1);
137  }
138 }
139 
140 
141 // replace from a string
142 void replace(std::string& s, const std::string& s2, const std::string& s3)
143 {
144  std::string::size_type pos;
145  // while ( (pos = s.find(" "))!=std::string::npos ) {
146  // s.replace(pos, 1, "-");
147  while ( (pos = s.find(s2))!=std::string::npos ) {
148  s.replace(pos, 1, s3);
149  }
150 }
151 
152 void replace(std::string& s, char c1, char c2) noexcept {
153  std::replace(s.begin(), s.end(), c1, c2);
154 }
155 
156 // remove colons from a string
157 void depunctuate(std::string& s)
158 {
159  std::string::size_type pos;
160  while ( (pos = s.find(':'))!=std::string::npos ) {
161  s.erase(pos, 1);
162  }
163 }
164 
165 
166 
167 
168 // helper method to get difference in phi
169 double deltaPhi(double phi1, double phi2) {
170  double delta = fabs(phi1-phi2);
171  delta = (delta > M_PI) ? (2.0*M_PI - delta) : delta;
172  return delta;
173 }
174 
175 
176 
177 // checks to see if a particular file can be opened
178 bool canopen(const std::string& s) {
179  FILE* f = fopen(s.c_str(), "r");
180  if ( f ) { fclose(f); return true; }
181  return false;
182 }
183 
184 
185 // converts number into integer string ...
186 std::string number(const double& d, const std::string& s) {
187  char tmp[512];
188  sprintf(tmp, s.c_str(), d);
189  return tmp;
190 }
191 
192 std::string number(const int& i, const std::string& s) {
193  char tmp[512];
194  sprintf(tmp, s.c_str(), i);
195  return tmp;
196 }
197 
198 
199 
200 std::string dirname( std::string name ) {
201  std::string::size_type pos = name.find_last_of( '/' );
202  if ( pos!=std::string::npos ) name.resize( pos );
203  return name;
204 }
205 
206 
207 std::string basename( std::string name ) {
208  std::string::size_type pos = name.find( '/' );
209  while ( pos!=std::string::npos ) {
210  name = name.substr( pos+1, name.size()-pos-1 );
211  pos = name.find( '/' );
212  }
213  return name;
214 }
ReadCellNoiseFromCoolCompare.s1
s1
Definition: ReadCellNoiseFromCoolCompare.py:378
python.CaloRecoConfig.f
f
Definition: CaloRecoConfig.py:128
canopen
bool canopen(const std::string &s)
Definition: utils.cxx:178
chop
std::string chop(std::string &s1, const std::string &s2)
Definition: utils.cxx:98
number
std::string number(const double &d, const std::string &s)
Definition: utils.cxx:186
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
hist_file_dump.d
d
Definition: hist_file_dump.py:137
basename
std::string basename(std::string name)
Definition: utils.cxx:207
depunctuate
void depunctuate(std::string &s)
Definition: utils.cxx:157
extractSporadic.c1
c1
Definition: extractSporadic.py:134
dirname
std::string dirname(std::string name)
Definition: utils.cxx:200
M_PI
#define M_PI
Definition: ActiveFraction.h:11
chopfirst
std::string chopfirst(std::string &s1, const std::string &s2)
Definition: utils.cxx:60
replace
void replace(std::string &s, const std::string &s2, const std::string &s3)
Definition: utils.cxx:142
chomp
std::string chomp(std::string &s1, const std::string &s2)
Definition: utils.cxx:115
choptoken
std::string choptoken(std::string &s1, const std::string &s2)
Definition: utils.cxx:34
utils.h
chomptoken
std::string chomptoken(std::string &s1, const std::string &s2)
Definition: utils.cxx:47
lumiFormat.i
int i
Definition: lumiFormat.py:85
removespace
void removespace(std::string &s, const std::string &s2)
Definition: utils.cxx:132
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
ReadCellNoiseFromCoolCompare.s3
s3
Definition: ReadCellNoiseFromCoolCompare.py:380
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
compileRPVLLRates.c2
c2
Definition: compileRPVLLRates.py:361
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
chopends
std::string chopends(std::string &s1, const std::string &s2)
Definition: utils.cxx:76
ReadCellNoiseFromCoolCompare.s2
s2
Definition: ReadCellNoiseFromCoolCompare.py:379
choplast
std::string choplast(std::string &s1, const std::string &s2)
Definition: utils.cxx:85
deltaPhi
double deltaPhi(double phi1, double phi2)
Definition: utils.cxx:169