removed useless file
[qmc.git] / analyze.cpp
blob57f76890056e3cf0154e140836bb4f1862f670cb
1 /*
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
11 * conditions:
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.
26 #include <string>
27 #include <vector>
28 #include <iostream>
29 #include <fstream>
30 #include <sstream>
31 #include <cmath>
32 #include <cstdlib>
34 using namespace std;
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++) {
40 //cerr << d[i] << " ";
41 avg += d[i];
42 var += d[i]*d[i];
44 avg /= N;
45 var /= (N);
46 //cerr.precision(10);
47 //cerr << d[0] << " " << d[49] << " " << d[311] << " " << d[N-1] << endl;
48 //cerr << avg*avg << " " << var << endl;
49 var -= avg*avg;
50 var = sqrt(var);
51 //cerr << "=> " << avg << " +- " << var << endl;
52 return std::pair<double, double>(avg, var);
55 std::string analyze_data_file (std::string filename, const int skip, const int len, int w, bool sq = false) {
56 std::vector<double> sums;
57 std::string ret = "";
59 int n = -skip-1;
60 double norm = 0.0;
61 std::string line_string = "";
62 std::string state = "";
63 std::ifstream f(filename.c_str(), std::fstream::in);
64 while (!f.eof()) {
65 double energy = 0.0;
66 double weight = 0.0;
67 getline(f, line_string);
68 std::stringstream line(line_string);
69 if (line_string[0]=='#') {
70 if (line_string[1]=='!') {
71 if (line_string.substr(2, 8)=="prepend ") {
72 ret.insert(0, " ");
73 ret.insert(0, line_string.substr(10, line_string.size()));
74 } else {
77 } else {
78 n++;
79 if (n>=0) {
80 line >> state >> energy;
81 if (sq) energy = energy*energy;
82 weight = 1.0;
83 for (int j=0;j<w;j++) {
84 line >> weight;
86 norm += (weight);
87 if (n%len==0) {
88 if (n>0) sums[sums.size()-1] /= norm;
89 sums.push_back(0.0);
90 norm = 0.0;
92 sums[n/len] += energy*(weight);
96 f.close();
97 sums.pop_back(); // last one may be incomplete.
98 // time to do some thing with the sums (now they are averages)
99 std::pair<double, double> s = compute_stats(sums);
100 std::stringstream rs;
101 rs << s.first << " " << s.second;
102 ret.append(rs.str());
103 return ret;
106 #define NEXTOPT(a, n) ( (a[i][2]=='\0')?(a[++i]):&(a[i][2]) )
108 int main (int argc, char **argv) {
109 int skip = 0;
110 int len = 1000;
111 int w_col = -1;
112 bool square = false;
113 vector<string> files;
114 for (int i=1;i<argc;i++) {
115 if (argv[i][0]=='-') {
116 switch (argv[i][1]) {
117 case 'w':
118 w_col = atoi(NEXTOPT(argv, i));
119 break;
120 case 's':
121 skip = atoi(NEXTOPT(argv, i));
122 break;
123 case 'q':
124 square = true;
125 break;
126 case 'l':
127 len = atoi(NEXTOPT(argv, i));
128 break;
130 } else {
131 files.push_back(argv[i]);
134 for (vector<string>::iterator iter = files.begin();iter != files.end();iter++) {
135 cout << analyze_data_file(*iter, skip, len, w_col, square) << endl;
137 return 0;