summaryrefslogtreecommitdiff
path: root/process_distributions.cpp
blob: 210b6869cb1a178fdf4600ef0da870efddaa0f7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66


#include <cinttypes>
#include <cmath>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>

int main(int argc, char *argv[]) {
  std::vector<std::string> all_args;

  if (argc > 1) {
    all_args.assign(argv + 1, argv + argc);
  }

  for (std::string filename : all_args) {
    std::ifstream in_file;
    in_file.open(filename);

    if (!in_file.is_open()) {
      std::cout << "Failed to open " << filename << ".\n";
    } else {

      unsigned n_moms = 5;
      std::vector<double> o(n_moms);
      std::vector<double> δo(n_moms);

      uint64_t norm = 0;

      uint64_t j = 0;
      uint64_t a;

      while (in_file >> a) {
        norm += a;
        for (unsigned i = 0; i < n_moms; i++) {
          o[i] += a * pow(j + 1, i + 1);
          δo[i] += a * pow(j + 1, 2 * (i + 1));
        }
        j++;
      }

      in_file.close();

      for (unsigned i = 0; i < n_moms; i++) {
        o[i] /= norm;
        δo[i] = sqrt(δo[i]) / norm;
      }

      std::ofstream out_file;
      out_file.open(filename + ".processed");

      for (unsigned i = 0; i < n_moms; i++) {
        out_file << o[i] << " " << δo[i] << "\n";
      }

      out_file.close();

      std::cout << "Finished processing " << filename << ".\n";
    }
  }

  return 0;
}