ROOTPWA
mcmc2fitbin.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 // ROOT Macro
22 // converts Narkov Chain MonteCarlo output into TFitBins
23 
24 //#include <fitlog.h>
25 //#include <integral.h>
26 #include <iostream>
27 #include <list>
28 #include <vector>
29 #include <string>
30 #include <algorithm>
31 #include <map>
32 #include <complex>
33 #include <assert.h>
34 #include "TBranch.h"
35 #include "TTree.h"
36 #include "TFile.h"
37 #include "TDirectory.h"
38 #include "TString.h"
39 #include "TComplex.h"
40 #include "TRandom.h"
41 #include "TFitBin.h"
42 #include "TMCMCMeta.h"
43 #include "TPWALikelihood.h"
44 
45 using namespace std;
46 
47 
48 int lineno = 1; // global variables needed for lex (not understood)
49 char *progname;
50 
51 int main(int argc, char** argv){
52 
53  if(argc<2){
54  cout << "Usage: mcmc2fitbin <infile> <outfile>"<<endl;
55  return 1;
56  }
57 
58  // oopen input file
59  TFile* infile=TFile::Open(argv[1],"READ");
60  TTree* input=(TTree*)infile->Get("MarkovChain");
61  TMCMCMeta* meta=(TMCMCMeta*)infile->Get("MetaInfo");
62  assert(input!=0);
63  assert(meta!=0);
64 
65 
66  TString ofile_name(argv[2]);
67  unsigned int evt=meta->NEvents;
68 
69 
70  double low_mass=meta->low_mass;
71  double high_mass=meta->high_mass;
72  //double step=meta->stepsize;
73  unsigned int rank=2;//ACHTUNG!!!meta->rank; // rank
74 
75 
76  int ndim;
77  input->SetBranchAddress("N",&ndim);
78  input->GetEntry(0);
79 
80  double dL[ndim]; // gradient of loglikelihood
81  //double dLlast[ndim];
82  double E; // likelihood-value
83  double H; // Hamiltonian
84  double X[ndim]; // position in phasespace
85  //double Xlast[ndim]; // position in PS for last MCMC step
86  double R[ndim]; // vector of velocities in PS
87  double Rlast[ndim];
88 
89 
90  input->SetBranchAddress("dL",&dL);
91  input->SetBranchAddress("X",&X);
92  input->SetBranchAddress("L",&E);
93  input->SetBranchAddress("H",&H);
94  input->SetBranchAddress("R",&R);
95  input->SetBranchAddress("Rlast",&Rlast);
96 
97 
98  TFitBin* result=new TFitBin();
99  TFile* outfile=new TFile(ofile_name,"UPDATE");
100  // check if tree exists
101 
102  TTree* tree=(TTree*)outfile->Get("pwa");
103  if(tree==NULL){ // create new tree
104  std::cout << "File empty. Creating new results tree!!!" << std::endl;
105  tree=new TTree("pwa","pwa");
106  tree->Branch("fitbin",&result);
107  }
108  else tree->SetBranchAddress("fitbin",&result);
109 
110 
111  double mass=0.5*(low_mass+high_mass);
112 
113  vector<TString> wavetitles; // without rank
114  vector<TString> wavenames;
115  const vector<TString>& parnames=meta->parnames;
116 
117  int j=0;
118  for(unsigned int i=0;i<parnames.size();++i){
119  TString title;
120  unsigned int l=parnames[i].Length()-3;
121  TString name;
122  {if(parnames[i].Contains("V_"))name=parnames[i](0,l+4); else name=parnames[i](0,l);}
123  cout << "Name= " << name << endl;
124 
125  if(std::find(wavenames.begin(),wavenames.end(),name)==wavenames.end()){
126  wavenames.push_back(name);
127  ++j;
128  }
129  else continue;
130 
131  l=wavenames[j-1].Length();
132  {if(wavenames[j-1].Contains("V_"))title=wavenames[j-1](2,l-2); else title=wavenames[j-1](3,l-3);}
133 
134  //cout << "Title=" << title << endl;
135  // only fill in title once!!!
136  if(std::find(wavetitles.begin(),wavetitles.end(),title)==wavetitles.end()){
137  wavetitles.push_back(title);
138  cout << title << endl;
139  }
140 
141  }
142 
143  // Loop over mcmc samples
144  int nmc=input->GetEntriesFast();
145  for(int mci=0;mci<nmc;++mci){
146 
147  input->GetEntry(mci);
148 
149  vector<TComplex> amps;
150  vector<complex<double> > V;
151 
152  double re,im;
153  unsigned int k=0;
154  unsigned int nwaves=wavetitles.size()-1; // -1 ... because of flat
155  for(unsigned int r=0;r<rank;++r){
156  for(unsigned int i=r;i<nwaves;++i){
157  if(i<r){re=0;im=0;}
158  else if(i==r){re=X[k++];im=0;} // real parameter
159  else {
160  re=X[k++];
161  im=X[k++];
162  }
163  V.push_back(complex<double>(re,im));
164  }
165  } // end loop over rank
166  V.push_back(complex<double>(X[k],0));
167 
168  // convert to TComplex;
169  for(unsigned int i=0;i<V.size();++i)amps.push_back(TComplex(V[i].real(),V[i].imag()));
170 
171  //cout << " Created amps." << endl;
172 
173  // error matrix -> No errors for mcmc
174  TMatrixD errm(0,0);
175 
176 
177 
178  vector<pair<int,int> > indices; // no errm => no indices
179 
180 
181  // get normalization integral
182 
183 
184  int n=wavetitles.size()-1;
185 
186  TCMatrix integr(n,n);
187  integr=meta->Norm;
188 
189 
190  cout << "filling TFitBin" << endl;
191  cout << "Number of amps=" << amps.size()<< endl;
192  cout << "Number of indices=" << indices.size() << endl;
193  cout << "Number of wavenames=" << wavenames.size()<< endl;
194  cout << "Number of wavetitles=" << wavetitles.size()<< endl;
195  cout << "Dimension of errormatrix=" << errm.GetNrows() << endl;
196  cout << "Dimension of integral matrix=" << integr.nrows() << endl;
197 
198 
199  result->fill(amps,
200  indices,
201  wavenames,
202  evt,
203  evt,
204  mass,
205  integr,
206  errm,
207  E,
208  rank);
209 
210  //result->PrintParameters();
211 
212 
213  TString binname="fitbin";binname+=low_mass;binname+="_";binname+=high_mass;
214  binname.ReplaceAll(" ","");
215  tree->Fill();
216 
217  }// end loop over mcmcsamples
218  tree->Write("",TObject::kOverwrite);
219  outfile->Close();
220 
221  return 0;
222  }
223 
224 // dummy function needed since we link to but do not use minuit.o
225 int mnparm(int, string, double, double, double, double) {
226  cerr << "this is impossible" << endl;
227  throw "aFit";
228  return 0;
229 }