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

Last change on this file since 3784 was 3572, checked in by cmv, 17 years ago

char* -> const char* pour regler les problemes de deprecated string const... + comparaison unsigned signed + suppression EVOL_PLANCK rz+cmv 07/02/2009

File size: 8.8 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
25namespace ROOT { namespace Minuit2 {
26class MyFCN : public FCNBase {
27public:
28 MyFCN(HistoErr& herr,PkSpectrumZ& pk);
29 void Init(const vector<double>& par) const;
30 double Func(double x,const vector<double>& par) const;
31 double operator()(const vector<double>& par) const;
32 double Up(void) const {return 1.;}
33private:
34 HistoErr& herr_;
35 PkSpectrumZ& pk_;
36};
37} } // namespace ROOT + Minuit2
38
39void usage(void);
40void usage(void)
41{
42cout<<"usage: cmvfitpk [-k kmin,kmax] -e conchpkrec.ppf observ3d_1.ppf observ3d_2.ppf ..."<<endl;
43}
44
45int main(int narg,char *arg[])
46{
47 // --- Cosmography definition (WMAP)
48 double zref = 0.175;
49 double ob0 = 0.0444356;
50 double h100=0.71, om0=0.267804, ol0=0.73;
51
52 // --- Spectrum and variance definition
53 double ns = 1., as = 1.;
54 double R=8./h100, sigmaR = 1.;
55 double kmin=1e-5,kmax=1000.;
56 int npt = 10000;
57 double eps=1.e-3;
58
59 // --- les fichiers
60 double klim[2]={0.,-1.};
61 string fconc = "";
62
63 // --- les options de print/plot
64 int nprint = 3, nplot = -1; // -1 = all
65
66 // --- Reading arguments
67 char c;
68 while((c = getopt(narg,arg,"hk:e:")) != -1) {
69 switch (c) {
70 case 'k' :
71 sscanf(optarg,"%lf,%lf",&klim[0],&klim[1]);
72 break;
73 case 'e' :
74 fconc = optarg;
75 break;
76 case 'h' :
77 default :
78 usage(); return -1;
79 }
80 }
81
82 if(optind>=narg) return -2;
83
84 // --- Create spectrum
85 cout<<endl<<"\n--- Create Spectrum"<<endl;
86 InitialSpectrum pkini(ns,as);
87 bool nobaryon = false;
88 double ocdm0 = om0-ob0;
89 TransfertEisenstein tf(h100,ocdm0,ob0,T_CMB_Par,nobaryon);
90 GrowthFactor growth(om0,ol0); cout<<"...Growth="<<growth(zref)<<endl;
91 PkSpectrum0 pk0(pkini,tf);
92 PkSpectrumZ pkz(pk0,growth,zref);
93
94 // --- Compute variance and normalize spectrum
95 pkz.SetZ(0.);
96 cout<<endl<<"\n--- Compute variance for top-hat R="<<R
97 <<" at z="<<pkz.GetZ()<<endl;
98 VarianceSpectrum varpk_th(pkz,R,VarianceSpectrum::TOPHAT);
99 double kfind_th = varpk_th.FindMaximum(kmin,kmax,eps);
100 double pkmax_th = varpk_th(kfind_th);
101 cout<<"kfind_th = "<<kfind_th<<" ("<<log10(kfind_th)<<"), integrand="<<pkmax_th<<endl;
102 double k1=kmin, k2=kmax;
103 int rc = varpk_th.FindLimits(pkmax_th/1.e4,k1,k2,eps);
104 cout<<"limit_th: rc="<<rc<<" : "<<k1<<" ("<<log10(k1)<<") , "
105 <<k2<<" ("<<log10(k2)<<")"<<endl;
106
107 double ldlk = (log10(k2)-log10(k1))/npt;
108 varpk_th.SetInteg(0.01,ldlk,-1.,4);
109 double sr2 = varpk_th.Variance(k1,k2);
110 cout<<"varpk_th="<<sr2<<" -> sigma="<<sqrt(sr2)<<endl;
111
112 double normpkz = sigmaR*sigmaR/sr2;
113 pkz.SetScale(normpkz);
114 cout<<"Spectrum normalisation = "<<pkz.GetScale()<<endl;
115 pkz.SetZ(zref);
116
117 // --- Reading reference mean spectrum
118 cout<<endl<<"\n--- Reading spectrum form file "<<fconc<<endl;
119 HistoErr herrconc;
120 {
121 PInPersist pis(fconc.c_str());
122 pis >> PPFNameTag("herrconc") >> herrconc;
123 }
124 cout<<herrconc;
125 if(herrconc.NBins()<1) return -3;
126 double b_ini = herrconc(2*herrconc.NBins()/3);
127 cout<<"b_ini = "<<b_ini<<endl;
128 if(klim[1]<=klim[0]) klim[1] = herrconc.XMax();
129
130 // --- NTuple et ppf
131 const int npar = 7; // Number of parameters in the fit
132 TMatrix<r_8> Cov(npar,npar); Cov = 0.;
133 POutPersist pos("cmvfitpk.ppf");
134 const int nvar = 2*npar+1;
135 const char *vname[nvar] = {
136 "xi2"
137 ,"b","a","oc","ob","ol","h","n"
138 ,"eb","ea","eoc","eob","eol","eh","en"
139 };
140 NTuple nt(nvar,vname);
141 double xnt[nvar];
142
143 // ***
144 // --- Boucle sur les fichiers a fitter
145 // ***
146 int nfile=0, ncov=0;
147 for(int ifile=optind;ifile<narg;ifile++) {
148
149 // --- lecture du fichier
150 cout<<">>> Reading["<<nfile<<","<<ifile<<"]: "<<arg[ifile]<<endl;
151 HistoErr herr;
152 PInPersist pis(arg[ifile]);
153 pis >> PPFNameTag("hpkrec") >> herr;
154 if(herr.NBins()!=herrconc.NBins()) {
155 cout<<"... bad number of bins: "<<herr.NBins()<<endl;
156 continue;
157 }
158
159 // --- Mise en forme
160 unsigned int nbinok = 0;
161 for(int i=0;i<herr.NBins();i++) {
162 double k = herr.BinCenter(i);
163 if(i==0 || k<klim[0] || k>klim[1]) {herr.SetErr2(i,-1.); continue;}
164 herr.SetErr2(i,herrconc.Error2(i));
165 nbinok++;
166 }
167
168 // --- Initialisation de minuit
169 MnUserParameters upar;
170 upar.Add("B",b_ini,b_ini/100.);
171 upar.Add("A",1.,0.01);
172 upar.Add("Ocdm",ocdm0,0.001,ocdm0/2.,2.*ocdm0);
173 //upar.Fix("Ocdm");
174 upar.Add("Ob",ob0,0.001,ob0/10.,10.*ob0);
175 //upar.Fix("Ob");
176 upar.Add("Ol",ol0,0.001,0.1,2.);
177 upar.Fix("Ol");
178 upar.Add("h100",h100,0.01,10.,150.);
179 upar.Fix("h100");
180 upar.Add("ns",ns,0.001,0.7,1.5);
181 upar.Fix("ns");
182 MyFCN fcn(herr,pkz);
183 if(nbinok<upar.VariableParameters()) {
184 cout<<"... nombre de degres de liberte <0: nbinok="<<nbinok<<endl;
185 continue;
186 }
187
188 // --- Fit minuit
189 unsigned int nfcall = 1000; double tolerance = 0.05;
190 MnMigrad migrad(fcn,upar,MnStrategy(1));
191 FunctionMinimum min = migrad(nfcall,tolerance);
192 if(!min.IsValid()) {
193 cout<<"!!!!!!!!! Echec Minuit"<<endl;
194 //cout<<"minimum: "<<min<<endl;
195 continue;
196 }
197
198 MnUserParameterState ustate = min.UserState();
199 double xi2r = ustate.Fval()/(nbinok-ustate.VariableParameters());
200 vector<double> parfit = ustate.Parameters().Params();
201 vector<double> eparfit = ustate.Parameters().Errors();
202 if(ustate.HasCovariance()) {
203 MnUserCovariance ucov = ustate.Covariance();
204 for(unsigned int i=0;i<upar.VariableParameters();i++)
205 for(unsigned int j=0;j<upar.VariableParameters();j++)
206 Cov(ustate.ExtOfInt(i),ustate.ExtOfInt(j)) += ucov(i,j);
207 ncov++;
208 }
209
210 // --- Recuperation des informations et remplissage NTuple
211 for(int i=0;i<nvar;i++) xnt[i] = 0.;
212 xnt[0] = xi2r;
213 for(int i=0;i<npar;i++) xnt[1+i] = parfit[i];
214 for(int i=0;i<npar;i++) xnt[npar+1+i] = eparfit[i];
215 nt.Fill(xnt);
216
217 // --- stoquage des histos de plots
218 if(nfile<nplot || nplot<0) {
219 HistoErr herrf(herr); herrf.Zero();
220 HistoErr herrd(herr); herrd.Zero();
221 fcn.Init(parfit);
222 for(int i=1;i<herr.NBins();i++) {
223 double f = fcn.Func(herr.BinCenter(i),parfit);
224 herrf.SetBin(i,f,herr.Error(i),1.);
225 herrd.SetBin(i,herr(i)-f,herr.Error(i),1.);
226 }
227 char str[64];
228 sprintf(str,"h%d",nfile); pos.PutObject(herr,str);
229 sprintf(str,"hf%d",nfile); pos.PutObject(herrf,str);
230 sprintf(str,"hd%d",nfile); pos.PutObject(herrd,str);
231 }
232
233 // --- Print de debug
234 if(nfile<nprint) {
235 cout<<"minimum: "<<min<<endl;
236 for(int i=0;i<nvar;i++) cout<<"["<<i<<"] = "<<xnt[i]<<endl;
237
238 MnMinos Minos(fcn,min);
239 pair<double,double> eminos[npar];
240 for(int ip=0;ip<npar;ip++) {
241 eminos[ip].first = eminos[ip].second = 0.;
242 if(!ustate.Parameter(ip).IsFixed()) eminos[ip] = Minos(ip);
243 cout<<"Minos: "<<ip<<" err=["<<eminos[ip].first<<","<<eminos[ip].second<<"]"<<endl;
244 }
245 }
246
247 nfile++;
248 }
249
250 // --- Fin du job
251 if(nfile>0) pos.PutObject(nt,"nt");
252 if(ncov>0) {Cov /= (double)ncov; pos.PutObject(Cov,"cov");}
253
254 return 0;
255}
256
257//--------------------------------------------------
258MyFCN::MyFCN(HistoErr& herr,PkSpectrumZ& pk)
259 : herr_(herr) , pk_(pk)
260{
261}
262
263void MyFCN::Init(const vector<double>& par) const
264{
265 double A=par[1], Ocdm0=par[2], Ob0=par[3], Ol0=par[4], h100=par[5], ns=par[6];
266 pk_.GetPk0().GetPkIni().SetSlope(ns);
267 pk_.GetPk0().GetPkIni().SetNorm(A);
268 pk_.GetPk0().GetTransfert().SetParTo(h100,Ocdm0,Ob0);
269 pk_.GetGrowthFactor().SetParTo(Ocdm0+Ob0,Ol0);
270}
271
272double MyFCN::Func(double x,const vector<double>& par) const
273// par: [0]=B, [1]=A, [2]=Ocdm0, [3]=Ob0, [4]=Ol0, [5]=h100, [6]=npow
274{
275 Init(par);
276 return pk_(x) + par[0];
277}
278
279double MyFCN::operator()(const vector<double>& par) const
280{
281 Init(par);
282 double xi2 = 0.;
283 for(int i=0;i<herr_.NBins();i++) {
284 double e2 = herr_.Error2(i);
285 if(e2<=0.) continue;
286 double x = herr_.BinCenter(i);
287 double v = herr_(i);
288 double f = Func(x,par);
289 xi2 += (v-f)*(v-f)/e2;
290 }
291 return xi2;
292}
293
294/*
295openppf cmvfitpk.ppf
296
297print nt
298
299set i 0
300zone
301disp h$i "nsta hbincont err"
302disp hf$i "nsta same red"
303
304zone
305disp hd$i "nsta hbincont err red"
306disp hd$i "nsta same"
307
308zone
309n/plot nt.xi2
310
311zone
312n/plot nt.a
313n/plot nt.b
314n/plot nt.b%a ! ! "crossmarker5"
315
316n/plot nt.oc
317n/plot nt.ob
318n/plot nt.oc+ob
319n/plot nt.ob%oc ! ! "crossmarker5"
320
321n/plot nt.ea
322n/plot nt.eb
323n/plot nt.ea%eb ! ! "crossmarker5"
324
325disp cov "zoomx30"
326del cor
327c++exec \
328TMatrix<r_8> cor(cov,false); cor = 0.; \
329for(int i=0;i<cor.NRows();i++) { \
330 for(int j=0;j<cor.NCols();j++) { \
331 double v = cov(i,i)*cov(j,j); \
332 if(v<=0.) continue; \
333 cor(i,j) = cov(i,j)/sqrt(v); \
334} } \
335KeepObj(cor); \
336cout<<"Matrice cor cree "<<endl;
337
338disp cor "zoomx30"
339
340 */
Note: See TracBrowser for help on using the repository browser.