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

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

modif sur remplissage NTuple/Minos, possibilite de changer les parametres O0,Om,Ol etc... , cmv 09/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 TransfertEisenstein tf(h100,om0-ob0,ob0,T_CMB_Par,false);
84 GrowthFactor growth(om0,ol0);
85 PkSpectrum0 pk0(pkini,tf);
86 PkSpectrumZ pkz(pk0,growth,zref);
87
88 // --- Compute variance and normalize spectrum
89 pkz.SetZ(0.);
90 cout<<endl<<"\n--- Compute variance for top-hat R="<<R
91 <<" at z="<<pkz.GetZ()<<endl;
92 VarianceSpectrum varpk_th(pkz,R,VarianceSpectrum::TOPHAT);
93 double kfind_th = varpk_th.FindMaximum(kmin,kmax,eps);
94 double pkmax_th = varpk_th(kfind_th);
95 cout<<"kfind_th = "<<kfind_th<<" ("<<log10(kfind_th)<<"), integrand="<<pkmax_th<<endl;
96 double k1=kmin, k2=kmax;
97 int rc = varpk_th.FindLimits(pkmax_th/1.e4,k1,k2,eps);
98 cout<<"limit_th: rc="<<rc<<" : "<<k1<<" ("<<log10(k1)<<") , "
99 <<k2<<" ("<<log10(k2)<<")"<<endl;
100
101 double ldlk = (log10(k2)-log10(k1))/npt;
102 varpk_th.SetInteg(0.01,ldlk,-1.,4);
103 double sr2 = varpk_th.Variance(k1,k2);
104 cout<<"varpk_th="<<sr2<<" -> sigma="<<sqrt(sr2)<<endl;
105
106 double normpkz = sigmaR*sigmaR/sr2;
107 pkz.SetScale(normpkz);
108 cout<<"Spectrum normalisation = "<<pkz.GetScale()<<endl;
109 pkz.SetZ(zref);
110
111 // --- Reading reference mean spectrum
112 cout<<endl<<"\n--- Reading spectrum form file "<<fconc<<endl;
113 HistoErr herrconc;
114 {
115 PInPersist pis(fconc.c_str());
116 pis >> PPFNameTag("herrconc") >> herrconc;
117 }
118 cout<<herrconc;
119 if(herrconc.NBins()<1) return -3;
120 double b_ini = herrconc(2*herrconc.NBins()/3);
121 cout<<"b_ini = "<<b_ini<<endl;
122 if(klim[1]<=klim[0]) klim[1] = herrconc.XMax();
123
124 // --- NTuple et ppf
125 POutPersist pos("cmvfitpk.ppf");
126 const int nvar = 6;
127 char *vname[nvar] = {"xi2","a","b","ea","eb","eab"};
128 NTuple nt(nvar,vname);
129 double xnt[nvar];
130
131 // ***
132 // --- Boucle sur les fichiers a fitter
133 // ***
134 int nfile = 0;
135 for(int ifile=optind;ifile<narg;ifile++) {
136
137 // --- lecture du fichier
138 cout<<">>> Reading["<<nfile<<","<<ifile<<"]: "<<arg[ifile]<<endl;
139 HistoErr herr;
140 PInPersist pis(arg[ifile]);
141 pis >> PPFNameTag("hpkrec") >> herr;
142 if(herr.NBins()!=herrconc.NBins()) {
143 cout<<"... bad number of bins: "<<herr.NBins()<<endl;
144 continue;
145 }
146
147 // --- Mise en forme
148 unsigned int nbinok = 0;
149 for(int i=0;i<herr.NBins();i++) {
150 double k = herr.BinCenter(i);
151 if(i==0 || k<klim[0] || k>klim[1]) {herr.SetErr2(i,-1.); continue;}
152 herr.SetErr2(i,herrconc.Error2(i));
153 nbinok++;
154 }
155
156 // --- Initialisation de minuit
157 MnUserParameters upar;
158 const int npar = 2;
159 upar.Add("A",1.,0.01);
160 upar.Add("B",b_ini,b_ini/100.);
161 MyFCN fcn(herr,pkz);
162 if(nbinok<upar.VariableParameters()) {
163 cout<<"... nombre de degres de liberte <0: nbinok="<<nbinok<<endl;
164 continue;
165 }
166
167 // --- Fit minuit
168 unsigned int nfcall = 1000; double tolerance = 0.05;
169 MnMigrad migrad(fcn,upar,MnStrategy(1));
170 FunctionMinimum min = migrad(nfcall,tolerance);
171 if(!min.IsValid()) {
172 cout<<"!!!!!!!!! Echec Minuit"<<endl;
173 //cout<<"minimum: "<<min<<endl;
174 continue;
175 }
176
177 MnUserParameterState ustate = min.UserState();
178
179 // --- Recuperation des informations et remplissage NTuple
180 for(int i=0;i<nvar;i++) xnt[i] = 0.;
181 double xi2r = ustate.Fval()/(nbinok-upar.VariableParameters());
182 double A = ustate.Value((unsigned int)0);
183 double B = ustate.Value((unsigned int)1);
184 xnt[0] = xi2r;
185 xnt[1] = A;
186 xnt[2] = B;
187 xnt[3] = ustate.Error((unsigned int)0);
188 xnt[4] = ustate.Error((unsigned int)1);
189 if(ustate.HasCovariance()) xnt[5] = ustate.Covariance()(0,1);
190 nt.Fill(xnt);
191
192 // --- stoquage des histos
193 if(nfile<10) {
194 HistoErr herrf(herr); herrf.Zero();
195 HistoErr herrd(herr); herrd.Zero();
196 for(int i=1;i<herr.NBins();i++) {
197 double f = A*pkz(herr.BinCenter(i))+B;
198 herrf.SetBin(i,f,herr.Error(i),1.);
199 herrd.SetBin(i,herr(i)-f,herr.Error(i),1.);
200 }
201 char str[64];
202 sprintf(str,"h%d",nfile); pos.PutObject(herr,str);
203 sprintf(str,"hf%d",nfile); pos.PutObject(herrf,str);
204 sprintf(str,"hd%d",nfile); pos.PutObject(herrd,str);
205 }
206
207 // --- Print de debug
208 if(nfile<3) {
209 cout<<"minimum: "<<min<<endl;
210 for(int i=0;i<nvar;i++) cout<<"["<<i<<"] = "<<xnt[i]<<endl;
211
212 MnMinos Minos(fcn,min);
213 pair<double,double> eminos[npar];
214 for(int ip=0;ip<npar;ip++) {
215 eminos[ip].first = eminos[ip].second = 0.;
216 if(!ustate.Parameter(ip).IsFixed()) eminos[ip] = Minos(ip);
217 cout<<"Minos: "<<ip<<" err=["<<eminos[ip].first<<","<<eminos[ip].second<<"]"<<endl;
218 }
219 }
220
221 nfile++;
222 }
223
224 // --- Fin du job
225 if(nfile>1) pos.PutObject(nt,"nt");
226
227 return 0;
228}
229
230//--------------------------------------------------
231MyFCN::MyFCN(HistoErr& herr,GenericFunc& pk)
232 : herr_(herr) , pk_(pk)
233{
234}
235
236double MyFCN::operator()(const std::vector<double>& par) const
237{
238 double xi2 = 0.;
239 for(int i=0;i<herr_.NBins();i++) {
240 double e2 = herr_.Error2(i);
241 if(e2<=0.) continue;
242 double x = herr_.BinCenter(i);
243 double v = herr_(i);
244 double f = par[0]*pk_(x) + par[1];
245 xi2 += (v-f)*(v-f)/e2;
246 }
247 return xi2;
248}
249
250/*
251openppf cmvfitpk.ppf
252
253set i 0
254zone
255disp h$i "nsta hbincont err"
256disp hf$i "nsta same red"
257
258zone
259disp hd$i "nsta hbincont err red"
260disp hd$i "nsta same"
261
262zone
263n/plot nt.xi2
264
265zone
266n/plot nt.a
267n/plot nt.b
268n/plot nt.b%a ! ! "crossmarker5"
269
270n/plot nt.ea
271n/plot nt.eb
272n/plot nt.ea%eb ! ! "crossmarker5"
273
274 */
Note: See TracBrowser for help on using the repository browser.