ROOTPWA
plotgenetics.C
Go to the documentation of this file.
1 
2 //
3 // Copyright 2009 Sebastian Neubert
4 //
5 // This file is part of rootpwa
6 //
7 // rootpwa is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // rootpwa is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with rootpwa. If not, see <http://www.gnu.org/licenses/>.
19 //
21 
22 #include <map>
23 #include <string>
24 #include <iostream>
25 #include <fstream>
26 #include <vector>
27 #include "../TFitBin.h"
28 #include "TTree.h"
29 #include "TCanvas.h"
30 #include "TLatex.h"
31 #include "TSystem.h"
32 #include "TPostScript.h"
33 #include "TMath.h"
34 #include "TMatrixD.h"
35 #include "TMatrixDSym.h"
36 #include "TMatrixDSymEigen.h"
37 #include "TVectorD.h"
38 #include "TPad.h"
39 #include "TGraph.h"
40 #include "TString.h"
41 #include "TFile.h"
42 #include "TList.h"
43 #include "TROOT.h"
44 #include "TH1D.h"
45 #include "TH2D.h"
46 
47 #include "TPrincipal.h"
48 
49 
50 #include "wset.h"
51 
52 
53 
54 void plotgenetics(TString resultfile, TString wavepool){
55 
56  // load list of all possible waves in this run
57 
58  map<TString,unsigned int> waveindex;
59 
60  ifstream file(wavepool.Data());
61  string line;
62  // note that the index is starting at 1 !!!
63  unsigned int index=1;
64  while(file.good()){
65  getline(file,line);
66  unsigned int pos=line.find(" ");
67  string name=line.substr(0,pos);
68  double thres;
69  if(pos<line.length()){
70  thres=atof(line.substr(pos,line.length()).c_str());
71  } else thres=0;
72  if(line.length()>1){
73  waveindex[name.c_str()]=index;
74  ++index;
75  }
76  }
77 
78  unsigned int d=waveindex.size();
79  cout << d << " waves in wavepool." << endl;
80 
81  // read results
82  vector<double> fitness;
83 
84 
85  // setup matrix for mean value
86  TMatrixD mean(d,1);
87  // list of chromosomes:
88  vector<TMatrixD> csomes;
89 
90  // read in the wavelist of the individual fits
91  // and analyse the waveset of this fit
92  ifstream results(resultfile.Data());
93  while(results.good()){
94  getline(results,line);
95  unsigned int pos=line.find(" ");
96  string name=line.substr(0,pos);
97  double fit=0;
98  if(pos<line.length()){
99  fit=atof(line.substr(pos,line.length()).c_str());
100  }
101  if(line.length()>1){
102  fitness.push_back(fit);
103  set<wsetentry> myset;
104  readWavelist(myset,name);
105  TMatrixD ch(d,1);
106  // build chromosome
107  unsigned int iw=0;
108  set<wsetentry>::iterator it=myset.begin();
109  while(it!=myset.end()){
110  unsigned int index=waveindex[it->name];
111  ch(index,0)=1;
112  ++it;++iw;
113  }
114  csomes.push_back(ch);
115  ch.Print();
116  mean+=ch;
117 
118  }
119  }// end loop over results files
120 
121 
122 
123 
124 
125  //pca.Test();
126 
127 
128  // renormalize mean
129  mean*=1./(double)csomes.size();
130 
131 
132 
133 
134 
135  // calculate covariances:
136  TMatrixD cov(d,d);
137  for(unsigned int i=0;i<csomes.size();++i){
138  TMatrixD u=csomes[i]-mean;
139  TMatrixD c(u,TMatrixD::kMultTranspose,u);
140  cov+=c;
141  }
142 
143  cov*=1./(double)csomes.size();
144 
145  //cov.Print();
146 
147  // calculate eigenvectors
148 
149  TMatrixDSym sym; sym.Use(cov.GetNrows(),cov.GetMatrixArray());
150  TMatrixDSymEigen eigen(sym);
151  TMatrixD EigenVectors = eigen.GetEigenVectors();
152  TVectorD EigenValues = eigen.GetEigenValues();
153 
154  EigenValues.Print();
155 
156  // TMatrixD uc=cov.EigenVectors(lamda);
157 
158  // projections onto first and second component:
159  TVectorD u1=TMatrixDColumn(EigenVectors,0);
160  TVectorD u2=TMatrixDColumn(EigenVectors,1);
161 
162  TCanvas* c=new TCanvas("c","PCA",10,10,1200,800);
163  c->Divide(2,1);
164  c->cd(1);
165  TH2D* hpca=new TH2D("hpca","PCA",100,-5,5,100,-5,5);
166  hpca->Draw();
167 
168  unsigned int n=csomes.size();
169  TMatrixD* dist=new TMatrixD(n,n);
170 
171  for(unsigned int i=0;i<n;++i){
172  TVectorD x=TMatrixDColumn(csomes[i],0);
173  for(unsigned int j=0;j<n;++j){
174  TVectorD y=TMatrixDColumn(csomes[j],0);
175  TVectorD dis=x-y;
176  (*dist)(i,j)=dis.Norm1();
177  }
178  double x1=x*u1;
179  double x2=x*u2;
180  cout << x1 << " " << x2 << endl;
181  TString mark;
182  mark+=i;
183  int col=4-i;
184  if(col<-10)col=-10;
185  col+=kBlue;
186  TLatex* maker=new TLatex(x1,x2,mark);
187  maker->SetTextColor(col);
188  maker->SetTextSize(0.02);
189  maker->Draw();
190  }
191 
192  c->cd(2);
193  dist->DrawClone("COL");
194  dist->DrawClone("TEXT SAME");
195  // hpca->Draw();
196 
197 }