LTF
LTF_Tools.h
1 
2 #include "LTF/LTF.h"
3 
4 #include <fstream>
5 #include <string>
6 #include <vector>
7 #include <map>
8 #include <set>
9 
10 class LTF_Tools {
11 public:
12 
13 // _____________________________________________________________________________________ //
19 static
20  std::vector<std::vector<double > > read_correlations(std::string filename, int ncol, bool DoCorrTest=false) {
21  // read correlation files
22  std::vector<std::vector<double > > ret(ncol);
23  std::ifstream istrm(filename.c_str(), std::ios::binary);
24  if (!istrm.is_open()) {
25  std::cout << "failed to open " << filename << std::endl;
26  exit(1);
27  }
28  for ( int icol=0 ; icol<ncol ; icol++ ) {
29  double value;
30  for ( int i=0 ; i<ncol ; i++ ) {
31  istrm >> value;
32  ret[icol].push_back(value);
33  if ( DoCorrTest && icol==i && value!= 1 ) {
34  std::cout<<"ERROR! diagonal element must be 1, but is: "<<value<<endl;
35  exit(1);
36  }
37  }
38  //std::cout<<"read: "<< ret[icol].size()<<" values in row "<<icol<<std::endl;
39  }
40  return ret;
41 }
42 
43 
44 
45 // _____________________________________________________________________________________ //
51 static
52 std::vector<std::vector<double > > corr_to_cov( const std::vector<std::vector<double > >& corr, const std::vector<double >& percenterr, const std::vector<double >& data) {
53  std::vector<double > err(data.size());
54  for ( int i = 0 ; i<err.size() ; i++ )
55  err[i] = percenterr[i]/100.*data[i];
56 
57  std::vector<std::vector<double > > cov(err.size());
58  for ( int i = 0 ; i<err.size() ; i++ ) {
59  cov[i].resize(err.size());
60  for ( int j = 0 ; j<err.size() ; j++ ) {
61  cov[i][j] = corr[i][j]*err[i]*err[j];
62  //if ( corr[i][j] != 0 ) cout<<i<<", "<<j<<"\t"<<corr[i][j]<<"\t"<<percenterr[i]<<"\t"<<data[i]<<"\te: "<<err[i]<<"\tcov: "<<cov[i][j]<<endl;
63  }
64  }
65 
66  for ( int i = 0 ; i<err.size() ; i++ ) {
67  for ( int j = 0 ; j<i ; j++ ) {
68  if ( fabs((cov[i][j] / cov[j][i]-1.) > 1e-3) )
69  cout<<i<<", "<<j<<"\t"<<corr[i][j]<<"\t"<<percenterr[i]<<"\t"<<data[i]<<"\te: "<<err[i]<<"\tcov: "<<cov[i][j]<<"\t ratio ij/ji: "<<(cov[i][j] / cov[j][i]-1.)<<endl;
70  }
71  }
72 
73  return cov;
74 }
75 
76 
77 // _____________________________________________________________________________________ //
83 static
84 std::map < std::string, std::vector<double> > read_input_table(std::string filename, int ncol ) {
85  std::map < std::string, std::vector<double> > ret;
86  std::vector<std::string> cols;
87  // open file for reading
88  std::ifstream istrm(filename.c_str(), std::ios::binary);
89  if (!istrm.is_open()) {
90  std::cout << "failed to open " << filename << std::endl;
91  exit(1);
92  }
93  for ( int c=0 ; c<ncol ; c++ ) {
94  std::string colname;
95  istrm >> colname;
96  //cout<<colname<<endl;
97  ret[colname] = vector<double>();
98  cols.push_back(colname);
99  }
100  while ( istrm.good()) {
101  double value;
102  for ( int c=0 ; c<ncol ; c++ ) {
103  istrm >> value;
104  if ( !istrm.good() ) break;
105  //cout<<value<<" ";
106  std::string colname = cols[c];
107  ret[colname].push_back(value);
108  }
109  //cout<<endl;
110  }
111  cout<<"Info. [read_input_table] Read "<<ret.size()<<" rows."<<endl;
112  return ret;
113 }
114 
115 };
static std::vector< std::vector< double > > read_correlations(std::string filename, int ncol, bool DoCorrTest=false)
Definition: LTF_Tools.h:20
static std::vector< std::vector< double > > corr_to_cov(const std::vector< std::vector< double > > &corr, const std::vector< double > &percenterr, const std::vector< double > &data)
Definition: LTF_Tools.h:52
static std::map< std::string, std::vector< double > > read_input_table(std::string filename, int ncol)
Definition: LTF_Tools.h:84
Definition: LTF_Tools.h:10