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