ROOTPWA
selector.cc
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 // Reads in the results of N fits, calculates the best fits and
23 // creates output file with likelihood values
24 
25 #include <vector>
26 #include <map>
27 #include <iostream>
28 #include <fstream>
29 #include <iomanip>
30 #include <limits>
31 #include "TString.h"
32 #include "TChain.h"
33 #include "TH2D.h"
34 #include "TFile.h"
35 #include "fitResult.h"
36 using namespace std;
37 using namespace rpwa;
38 
39 
40 int
41 main(int argc, char** argv){
42 
43  if(argc<4){
44  cerr<<"Usage: selector nbins nsurvivors preselection fitdir1 fitdir2 fitdir3 ..."<<endl;
45  return 1;
46  }
47 
48  unsigned int nbins=atoi(argv[1]);
49  unsigned int nsurv=atoi(argv[2]);
50  string pre=argv[3];
51 
52  vector<TString> inputdirectories;
53 
54  map<double,TString> results; // <logli,index>
55  unsigned int bestfit=0;
56  double bestLogli=0;
57  unsigned int counter=0;
58  // load list with preselected waves
59  ifstream prefile(argv[3]);
60  cerr << "Opened Preselectionfile " << pre << endl;
61  while(!prefile.eof() && prefile.good()){
62  string dir;
63  double likeli;
64  prefile >> dir >> likeli;
65  if(dir=="")continue;
66  cerr << "Pre: " << dir << " " << likeli << endl;
67  inputdirectories.push_back(dir);
68  results[likeli]=dir;
69  }
70  counter=inputdirectories.size();
71  prefile.close();
72 
73  // register new fit directories
74  for(int i=4; i<argc; ++i){
75  inputdirectories.push_back(argv[i]);
76  }
77 
78  if(nsurv>inputdirectories.size()){
79  cerr << "Not enough fits to create " << nsurv << " survivors." << endl;
80  }
81 
82  double minevi=1.78E6;
83  double maxevi=1.84E6;
84 
85  //double minevi=712000;
86  //double maxevi=700000;
87 
88  //double minevi=680000;
89  //double maxevi=715000;
90 
91  unsigned int ngen=100;
92 
93  TH2D* hWavesetSize=new TH2D("hWS","Waveset sizes evolution",ngen,-0.5,(double)ngen-.5,100,0,100);
94  TH2D* hEvidences=new TH2D("hEvi","Evidence evolution",ngen,-0.5,(double)ngen-.5,1000,minevi,maxevi);
95  TH2D* hEviSize=new TH2D("hEviSize","Evidence vs Waveset size",100,0,100,1000,minevi,maxevi);
96 
97 TH2D* hLogliSize=new TH2D("hLogliSize","LogLikelihood vs Waveset size",100,0,100,1000,minevi,minevi);
98 
99 
100 
101 
102 
103  //loop over fits and extract quality information
104  // we are using the sum of loglikelyhood per event for this
105  // start at position counter-1!
106  for(unsigned int j=counter; j<inputdirectories.size(); ++j){
107  cerr << "Examining "<<inputdirectories[j]<<endl;
108  // get generation
109  unsigned int startgen=inputdirectories[j].Index("gen",0)+3;
110  unsigned int endgen=inputdirectories[j].Index("/",startgen);
111  TString genS=inputdirectories[j](startgen,endgen-startgen);
112  cerr << "generation=" << genS << endl;
113  double gen=atof(genS.Data());
114  fitResult* bin=new fitResult;
115  TChain* chain=new TChain("pwa");
116 
117  TString f=inputdirectories[j];
118  f+="/*.result.root";
119  cerr << "Loading " << f << endl;
120  if(chain->Add(f)==0){
121  cerr << "No fitoutput files found." << nbins
122  << ". Skipping fit!" << endl;
123  delete chain;
124  delete bin;
125  continue;
126  }
127  //chain->Print();
128  cerr << "Set up Chain"<< endl;
129 
130  chain->SetBranchAddress("fitResult_v2",&bin);
131  cerr << "Set Branch"<< endl;
132 
133  unsigned int n=chain->GetEntries();
134  cerr << "Got Entries"<< endl;
135  if(n!=nbins){
136  cerr << n << " bins in this fit. Expected " << nbins
137  << ". Skipping fit!" << endl;
138  delete chain;
139  delete bin;
140  continue;
141  }
142  double sumlogli=0;
143  double sumevi=0;
144  unsigned int nwaves=0;
145  cerr << "Reading data..."<< endl;
146  bool allconverged=true;
147  for(unsigned int k=0;k<n;++k){ // loop over bins
148  chain->GetEntry(k);
149  if(!bin->converged()){
150  cerr<<"Fit not converged. Will skip this."<< endl;
151  allconverged=false;
152  }
153  if(k==0)nwaves=bin->nmbWaves();
154  sumevi+=bin->evidence();
155  sumlogli+=-bin->logLikelihood();//+bin->nmbEvents();
156  }// end loop over bins
157 
158  cerr<<"SumLogli ="<<setprecision(9)<<sumlogli<<endl;
159  cerr<<"SumEvidence ="<<setprecision(9)<<sumevi<<endl;
160  if(sumevi==0 || !(sumevi>std::numeric_limits<double>::min() && sumevi < std::numeric_limits<double>::max())){
161  cerr<<"Invalid value. Skipping."<< endl;
162  delete chain;
163  delete bin;
164  continue;
165  }
166 
167  // only register result if it all bins converged!
168  if(allconverged){
169  hWavesetSize->Fill(gen,nwaves);
170  hEvidences->Fill(gen,sumevi);
171  hEviSize->Fill(nwaves,sumevi);
172  hLogliSize->Fill(nwaves,sumlogli);
173 
174  results[sumevi]=inputdirectories[j];
175  if(sumevi>bestLogli){
176  bestLogli=sumevi;
177  bestfit=j;
178  }
179  }
180  delete chain;
181  delete bin;
182  } // end loop over fits
183 
184 
185  map<double,TString>::iterator mit=results.begin();
186  while(mit!=results.end()){
187  cerr <<mit->second<<": "<<mit->first<<endl;
188  ++mit;
189  }
190 
191  cerr << "Bestfit: "<<inputdirectories[bestfit]
192  <<" with loglikely/event: "<<bestLogli<<endl;
193 
194  map<double,TString>::iterator mrit=results.end();
195  if(nsurv>results.size())nsurv=results.size();
196  for(unsigned int i=0;i<nsurv;++i){
197  --mrit;
198  cout << mrit->second;
199  if(!(mrit->second).Contains("wavelist"))cout<<"/wavelist";
200  cout<<" "<<maxPrecision(mrit->first) << endl;
201  }
202 
203  TFile* outfile=TFile::Open("genetic_stats.root","RECREATE");
204  hWavesetSize->Write();
205  hEvidences->Write();
206  hEviSize->Write();
207 hLogliSize->Write();
208  outfile->Close();
209 
210  return 0;
211 }