ROOTPWA
compare_ints.C
Go to the documentation of this file.
1 // jasinski@kph.uni-mainz.de, promme@web.de
2 // created: 03.11.10
3 // comparing to integral files with each other
4 // needed since the matrix layout is following the input of amplitudes to "int"
5 // call with root for example like:
6 // root -l compare_ints.C+\(\"1400.1420/ACCAMPS/normwo0.int\"\,\"../SET_20MeV/1400.1420/ACCAMPS/norm.int\"\)
7 // with all slashes!
8 // copy rights: do what ever you want with it as long as you don't blame me!
9 #include <complex>
10 #include <iostream>
11 #include <string>
12 #include <fstream>
13 #include <map>
14 
15 using namespace std;
16 
17 
18 void compare_ints(string file1, string file2){
19  ifstream filestream[2];
20  filestream[0].open(file1.c_str());
21  filestream[1].open(file2.c_str());
22  if (!filestream[0] || !filestream[1]){
23  cout << " Error, could not load files " << endl;
24  return;
25  }
26 
27  int nentries[2];
28  int nwaves[2];
29  int dimensionx[2];
30  int dimensiony[2];
31 
32  // header must be the same
33  for (int ifile = 0; ifile < 2; ifile++){
34  filestream[ifile] >> nwaves[ifile];
35  filestream[ifile] >> nentries[ifile];
36  filestream[ifile] >> dimensionx[ifile] >> dimensiony[ifile];
37  }
38 
39  if (!filestream[0].good() || !filestream[1].good()){
40  cout << " Error, could not read header of files " << endl;
41  return;
42  }
43 
44  if (nwaves[0] != nwaves[1] ||
45  nentries[0] != nentries[1] ||
46  dimensionx[0] != dimensionx[1] ||
47  dimensiony[0] != dimensiony[1])
48  {
49  cout << " Error, the header differ! " << endl;
50  return;
51  }
52  if (dimensionx[0] > 100 || dimensiony[0] > 100){
53  cout << " cannot process more than 100 x 100 entries, sorry " << endl;
54  return;
55  }
56  complex<double> entries[2][100][100];
57 
58  cout << " reading matrix of size " << dimensionx[0] << " X " << dimensiony[0] << endl;
59 
60  // now read all entries into memory
61  for (int ix = 0; ix < dimensionx[0] ; ix++){
62  for (int iy = 0; iy < dimensiony[0]; iy++){
63  for (int ifile = 0; ifile < 2; ifile++){
64  filestream[ifile] >> entries[ifile][ix][iy];
65  if (!filestream[ifile].good()){
66  cout << " Error reading matrix, aborting " << endl;
67  return;
68  }
69  }
70  }
71  }
72 
73  // the number of waves
74  for (int ifile = 0; ifile < 2; ifile++){
75  filestream[ifile] >> nwaves[ifile];
76  if (!filestream[ifile].good()){
77  cout << " Error reading number of waves, aborting " << endl;
78  return;
79  }
80  }
81 
82  if (nwaves[0] != nwaves[1]){
83  cout << " Error: number of waves differs, aborting " << endl;
84  return;
85  }
86  cout << " reading " << nwaves[0] << " waves" << endl;
87 
88  map<string,int> waves[2];
89  // finaly read the corresponding waves
90  for (int ifile = 0; ifile < 2; ifile++){
91  for (int iwave = 0; iwave < nwaves[ifile]; iwave++){
92  string key;
93  int position;
94  filestream[ifile] >> key >> position;
95  if (!filestream[ifile].good()){
96  cout << " Error reading waves, aborting " << endl;
97  return;
98  }
99  // get rid of an eventual path
100  int slashpos = key.rfind('/');
101  if (slashpos != (int) string::npos){
102  key.erase(0, slashpos+1);
103  }
104  waves[ifile][key] = position;
105  }
106  }
107 
108  // check mapping by size
109  if (waves[0].size() != waves[1].size()){
110  cout << " Error: waves differ " << endl;
111  return;
112  }
113  if (waves[0].size() != (unsigned) nwaves[0]){
114  cout << " Error: number of different waves is not correct, aborting " << endl;
115  return;
116  }
117 
118  int ncomperisons(0);
119  // compare all entries according to the key
120  for ( map<string,int>::iterator itx = waves[0].begin(); itx != waves[0].end(); itx++){
121  for ( map<string,int>::iterator ity = waves[0].begin(); ity != waves[0].end(); ity++){
122  if (entries[0][itx->second][ity->second] != entries[1][waves[1][itx->first]][waves[1][ity->first]]){
123  cout << " Error: wave " << itx->first << " differs " << endl;
124  return;
125  }
126  ncomperisons++;
127  }
128  }
129  // just a check since everything until now went too smooth!
130  if (ncomperisons != nwaves[0]*nwaves[0]){
131  cout << " Unexpected error: number of comparisons is too low! " << endl;
132  }
133 
134  filestream[0].close();
135  filestream[1].close();
136 
137  cout << " files seem to be equivalent " << endl;
138 }