2 * Copyright (c) 2009 Mauro Iazzi
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
36 std::pair
<double, double> compute_stats (const std::vector
<double> &d
) {
37 double avg
= 0.0, var
= 0.0;
38 const int N
= d
.size();
39 for (int i
=0;i
<N
;i
++) {
47 return std::pair
<double, double>(avg
, var
);
50 std::string
analyze_data_file (std::string filename
, const int skip
, const int len
) {
51 std::vector
<double> sums
;
55 std::string line_string
= "";
56 std::string state
= "";
57 std::ifstream
f(filename
.c_str(), std::fstream::in
);
61 getline(f
, line_string
);
62 std::stringstream
line(line_string
);
63 if (line_string
[0]=='#') {
64 if (line_string
[1]=='!') {
65 if (line_string
.substr(2, 8)=="prepend ") {
66 ret
.append(line_string
.substr(10, line_string
.size()));
74 line
>> state
>> energy
>> weight
;
78 sums
[n
/len
] += energy
;
83 sums
.pop_back(); // last one may be incomplete.
84 for (int i
=0;i
<sums
.size();i
++) {
85 sums
[i
] /= double(len
);
87 // time to do some thing with the sums (now they are averages)
88 std::pair
<double, double> s
= compute_stats(sums
);
90 rs
<< s
.first
<< " " << s
.second
;
95 int main (int argc
, char **argv
) {
99 for (int i
=1;i
<argc
;i
++) {
100 if (argv
[i
][0]=='-') {
101 switch (argv
[i
][1]) {
103 skip
= atoi(argv
[++i
]);
106 len
= atoi(argv
[++i
]);
110 files
.push_back(argv
[i
]);
113 for (vector
<string
>::iterator iter
= files
.begin();iter
!= files
.end();iter
++) {
114 cout
<< analyze_data_file(*iter
, skip
, len
) << endl
;