source: Sophya/trunk/Cosmo/SimLSS/cmvfitpk.cc@ 3380

Last change on this file since 3380 was 3380, checked in by cmv, 18 years ago

genericfunc.h deplace dans NTools , cmv 12/11/2007

File size: 6.9 KB
Line 
1#include "sopnamsp.h"
2#include "machdefs.h"
3#include <iostream>
4#include <stdlib.h>
5#include <stdio.h>
6#include <string.h>
7#include <math.h>
8#include <unistd.h>
9
10#include "ntuple.h"
11#include "histerr.h"
12
13#include "constcosmo.h"
14#include "pkspectrum.h"
15
16#include "Minuit2/FunctionMinimum.h"
17#include "Minuit2/MnMigrad.h"
18#include "Minuit2/MnUserParameters.h"
19#include "Minuit2/MnPrint.h"
20#include "Minuit2/MnMinos.h"
21#include "Minuit2/MinosError.h"
22#include "Minuit2/FCNBase.h"
23using namespace ROOT::Minuit2;
24
25#include "Minuit2/FCNBase.h"
26namespace ROOT { namespace Minuit2 {
27class MyFCN : public FCNBase {
28public:
29 MyFCN(HistoErr& herr,GenericFunc& pk);
30 double operator()(const std::vector<double>& par) const;
31 double Up(void) const {return 1.;}
32private:
33 HistoErr& herr_;
34 GenericFunc& pk_;
35};
36} } // namespace ROOT + Minuit2
37
38void usage(void);
39void usage(void)
40{
41cout<<"usage: cmvfitpk [-k kmin,kmax] -e conchpkrec.ppf observ3d_1.ppf observ3d_2.ppf ..."<<endl;
42}
43
44int main(int narg,char *arg[])
45{
46 // --- Cosmography definition (WMAP)
47 double zref = 0.175;
48 double ob0 = 0.0444356;
49 double h100=0.71, om0=0.267804, ol0=0.73;
50
51 // --- Spectrum and variance definition
52 double ns = 1., as = 1.;
53 double R=8./h100, sigmaR = 1.;
54 double kmin=1e-5,kmax=1000.;
55 int npt = 10000;
56 double eps=1.e-3;
57
58 // --- les fichiers
59 double klim[2]={0.,-1.};
60 string fconc = "";
61
62 // --- Reading arguments
63 char c;
64 while((c = getopt(narg,arg,"hk:e:")) != -1) {
65 switch (c) {
66 case 'k' :
67 sscanf(optarg,"%lf,%lf",&klim[0],&klim[1]);
68 break;
69 case 'e' :
70 fconc = optarg;
71 break;
72 case 'h' :
73 default :
74 usage(); return -1;
75 }
76 }
77
78 if(optind>=narg) return -2;
79
80 // --- Create spectrum
81 cout<<endl<<"\n--- Create Spectrum"<<endl;
82 InitialSpectrum pkini(ns,as);
83 bool nobaryon = false;
84 double ocdm0 = om0-ob0;
85 TransfertEisenstein tf(h100,ocdm0,ob0,T_CMB_Par,nobaryon);
86 GrowthFactor growth(om0,ol0);
87 PkSpectrum0 pk0(pkini,tf);
88 PkSpectrumZ pkz(pk0,growth,zref);
89
90 // --- Compute variance and normalize spectrum
91 pkz.SetZ(0.);
92 cout<<endl<<"\n--- Compute variance for top-hat R="<<R
93 <<" at z="<<pkz.GetZ()<<endl;
94 VarianceSpectrum varpk_th(pkz,R,VarianceSpectrum::TOPHAT);
95 double kfind_th = varpk_th.FindMaximum(kmin,kmax,eps);
96 double pkmax_th = varpk_th(kfind_th);
97 cout<<"kfind_th = "<<kfind_th<<" ("<<log10(kfind_th)<<"), integrand="<<pkmax_th<<endl;
98 double k1=kmin, k2=kmax;
99 int rc = varpk_th.FindLimits(pkmax_th/1.e4,k1,k2,eps);
100 cout<<"limit_th: rc="<<rc<<" : "<<k1<<" ("<<log10(k1)<<") , "
101 <<k2<<" ("<<log10(k2)<<")"<<endl;
102
103 double ldlk = (log10(k2)-log10(k1))/npt;
104 varpk_th.SetInteg(0.01,ldlk,-1.,4);
105 double sr2 = varpk_th.Variance(k1,k2);
106 cout<<"varpk_th="<<sr2<<" -> sigma="<<sqrt(sr2)<<endl;
107
108 double normpkz = sigmaR*sigmaR/sr2;
109 pkz.SetScale(normpkz);
110 cout<<"Spectrum normalisation = "<<pkz.GetScale()<<endl;
111 pkz.SetZ(zref);
112
113 // --- Reading reference mean spectrum
114 cout<<endl<<"\n--- Reading spectrum form file "<<fconc<<endl;
115 HistoErr herrconc;
116 {
117 PInPersist pis(fconc.c_str());
118 pis >> PPFNameTag("herrconc") >> herrconc;
119 }
120 cout<<herrconc;
121 if(herrconc.NBins()<1) return -3;
122 double b_ini = herrconc(2*herrconc.NBins()/3);
123 cout<<"b_ini = "<<b_ini<<endl;
124 if(klim[1]<=klim[0]) klim[1] = herrconc.XMax();
125
126 // --- NTuple et ppf
127 POutPersist pos("cmvfitpk.ppf");
128 const int nvar = 6;
129 char *vname[nvar] = {"xi2","a","b","ea","eb","eab"};
130 NTuple nt(nvar,vname);
131 double xnt[nvar];
132
133 // ***
134 // --- Boucle sur les fichiers a fitter
135 // ***
136 int nfile = 0;
137 for(int ifile=optind;ifile<narg;ifile++) {
138
139 // --- lecture du fichier
140 cout<<">>> Reading["<<nfile<<","<<ifile<<"]: "<<arg[ifile]<<endl;
141 HistoErr herr;
142 PInPersist pis(arg[ifile]);
143 pis >> PPFNameTag("hpkrec") >> herr;
144 if(herr.NBins()!=herrconc.NBins()) {
145 cout<<"... bad number of bins: "<<herr.NBins()<<endl;
146 continue;
147 }
148
149 // --- Mise en forme
150 unsigned int nbinok = 0;
151 for(int i=0;i<herr.NBins();i++) {
152 double k = herr.BinCenter(i);
153 if(i==0 || k<klim[0] || k>klim[1]) {herr.SetErr2(i,-1.); continue;}
154 herr.SetErr2(i,herrconc.Error2(i));
155 nbinok++;
156 }
157
158 // --- Initialisation de minuit
159 MnUserParameters upar;
160 const int npar = 2;
161 upar.Add("A",1.,0.01);
162 upar.Add("B",b_ini,b_ini/100.);
163 MyFCN fcn(herr,pkz);
164 if(nbinok<upar.VariableParameters()) {
165 cout<<"... nombre de degres de liberte <0: nbinok="<<nbinok<<endl;
166 continue;
167 }
168
169 // --- Fit minuit
170 unsigned int nfcall = 1000; double tolerance = 0.05;
171 MnMigrad migrad(fcn,upar,MnStrategy(1));
172 FunctionMinimum min = migrad(nfcall,tolerance);
173 if(!min.IsValid()) {
174 cout<<"!!!!!!!!! Echec Minuit"<<endl;
175 //cout<<"minimum: "<<min<<endl;
176 continue;
177 }
178
179 MnUserParameterState ustate = min.UserState();
180
181 // --- Recuperation des informations et remplissage NTuple
182 for(int i=0;i<nvar;i++) xnt[i] = 0.;
183 double xi2r = ustate.Fval()/(nbinok-upar.VariableParameters());
184 double A = ustate.Value((unsigned int)0);
185 double B = ustate.Value((unsigned int)1);
186 xnt[0] = xi2r;
187 xnt[1] = A;
188 xnt[2] = B;
189 xnt[3] = ustate.Error((unsigned int)0);
190 xnt[4] = ustate.Error((unsigned int)1);
191 if(ustate.HasCovariance()) xnt[5] = ustate.Covariance()(0,1);
192 nt.Fill(xnt);
193
194 // --- stoquage des histos
195 if(nfile<10) {
196 HistoErr herrf(herr); herrf.Zero();
197 HistoErr herrd(herr); herrd.Zero();
198 for(int i=1;i<herr.NBins();i++) {
199 double f = A*pkz(herr.BinCenter(i))+B;
200 herrf.SetBin(i,f,herr.Error(i),1.);
201 herrd.SetBin(i,herr(i)-f,herr.Error(i),1.);
202 }
203 char str[64];
204 sprintf(str,"h%d",nfile); pos.PutObject(herr,str);
205 sprintf(str,"hf%d",nfile); pos.PutObject(herrf,str);
206 sprintf(str,"hd%d",nfile); pos.PutObject(herrd,str);
207 }
208
209 // --- Print de debug
210 if(nfile<3) {
211 cout<<"minimum: "<<min<<endl;
212 for(int i=0;i<nvar;i++) cout<<"["<<i<<"] = "<<xnt[i]<<endl;
213
214 MnMinos Minos(fcn,min);
215 pair<double,double> eminos[npar];
216 for(int ip=0;ip<npar;ip++) {
217 eminos[ip].first = eminos[ip].second = 0.;
218 if(!ustate.Parameter(ip).IsFixed()) eminos[ip] = Minos(ip);
219 cout<<"Minos: "<<ip<<" err=["<<eminos[ip].first<<","<<eminos[ip].second<<"]"<<endl;
220 }
221 }
222
223 nfile++;
224 }
225
226 // --- Fin du job
227 if(nfile>1) pos.PutObject(nt,"nt");
228
229 return 0;
230}
231
232//--------------------------------------------------
233MyFCN::MyFCN(HistoErr& herr,GenericFunc& pk)
234 : herr_(herr) , pk_(pk)
235{
236}
237
238double MyFCN::operator()(const std::vector<double>& par) const
239{
240 double xi2 = 0.;
241 for(int i=0;i<herr_.NBins();i++) {
242 double e2 = herr_.Error2(i);
243 if(e2<=0.) continue;
244 double x = herr_.BinCenter(i);
245 double v = herr_(i);
246 double f = par[0]*pk_(x) + par[1];
247 xi2 += (v-f)*(v-f)/e2;
248 }
249 return xi2;
250}
251
252/*
253openppf cmvfitpk.ppf
254
255set i 0
256zone
257disp h$i "nsta hbincont err"
258disp hf$i "nsta same red"
259
260zone
261disp hd$i "nsta hbincont err red"
262disp hd$i "nsta same"
263
264zone
265n/plot nt.xi2
266
267zone
268n/plot nt.a
269n/plot nt.b
270n/plot nt.b%a ! ! "crossmarker5"
271
272n/plot nt.ea
273n/plot nt.eb
274n/plot nt.ea%eb ! ! "crossmarker5"
275
276 */
Note: See TracBrowser for help on using the repository browser.