[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 | #include "Minuit2/FCNBase.h"
|
---|
| 26 | namespace ROOT { namespace Minuit2 {
|
---|
| 27 | class MyFCN : public FCNBase {
|
---|
| 28 | public:
|
---|
| 29 | MyFCN(HistoErr& herr,GenericFunc& pk);
|
---|
| 30 | double operator()(const std::vector<double>& par) const;
|
---|
| 31 | double Up(void) const {return 1.;}
|
---|
| 32 | private:
|
---|
| 33 | HistoErr& herr_;
|
---|
| 34 | GenericFunc& pk_;
|
---|
| 35 | };
|
---|
| 36 | } } // namespace ROOT + Minuit2
|
---|
| 37 |
|
---|
| 38 | void usage(void);
|
---|
| 39 | void usage(void)
|
---|
| 40 | {
|
---|
| 41 | cout<<"usage: cmvfitpk [-k kmin,kmax] -e conchpkrec.ppf observ3d_1.ppf observ3d_2.ppf ..."<<endl;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | int 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 = 10;
|
---|
| 127 | char *vname[nvar] = {"xi2","a","b","ea","eb","eab","ea1","ea2","eb1","eb2"};
|
---|
| 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 | upar.Add("A",1.,0.01);
|
---|
| 159 | upar.Add("B",b_ini,b_ini/100.);
|
---|
| 160 | MyFCN fcn(herr,pkz);
|
---|
| 161 | if(nbinok<upar.VariableParameters()) {
|
---|
| 162 | cout<<"... nombre de degres de liberte <0: nbinok="<<nbinok<<endl;
|
---|
| 163 | continue;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | // --- Fit minuit
|
---|
| 167 | unsigned int nfcall = 1000; double tolerance = 0.05;
|
---|
| 168 | MnMigrad migrad(fcn,upar,MnStrategy(1));
|
---|
| 169 | FunctionMinimum min = migrad(nfcall,tolerance);
|
---|
| 170 | if(!min.IsValid()) {
|
---|
| 171 | cout<<"!!!!!!!!! Echec Minuit"<<endl;
|
---|
| 172 | //cout<<"minimum: "<<min<<endl;
|
---|
| 173 | continue;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | MnUserParameterState ustate = min.UserState();
|
---|
| 177 |
|
---|
| 178 | MnMinos Minos(fcn,min);
|
---|
| 179 | pair<double,double> eminos[2];
|
---|
| 180 | for(int ip=0;ip<2;ip++) {
|
---|
| 181 | eminos[ip].first = eminos[ip].second = 0.;
|
---|
| 182 | if(!ustate.Parameter(ip).IsFixed()) eminos[ip] = Minos(ip);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | // --- Recuperation des informations et remplissage NTuple
|
---|
| 186 | for(int i=0;i<nvar;i++) xnt[i] = 0.;
|
---|
| 187 | double xi2r = ustate.Fval()/(nbinok-upar.VariableParameters());
|
---|
| 188 | xnt[0] = xi2r;
|
---|
| 189 | xnt[1] = ustate.Value((unsigned int)0);
|
---|
| 190 | xnt[2] = ustate.Value((unsigned int)1);
|
---|
| 191 | xnt[3] = ustate.Error((unsigned int)0);
|
---|
| 192 | xnt[4] = ustate.Error((unsigned int)1);
|
---|
| 193 | if(ustate.HasCovariance()) xnt[5] = ustate.Covariance()(0,1);
|
---|
| 194 | xnt[6] = eminos[0].first;
|
---|
| 195 | xnt[7] = eminos[0].second;
|
---|
| 196 | xnt[8] = eminos[1].first;
|
---|
| 197 | xnt[9] = eminos[1].second;
|
---|
| 198 | nt.Fill(xnt);
|
---|
| 199 |
|
---|
| 200 | // --- stoquage des histos
|
---|
| 201 | if(nfile<10) {
|
---|
| 202 | HistoErr herrf(herr); herrf.Zero();
|
---|
| 203 | HistoErr herrd(herr); herrd.Zero();
|
---|
| 204 | for(int i=0;i<herr.NBins();i++) {
|
---|
| 205 | double f = pkz(herr.BinCenter(i));
|
---|
| 206 | herrf.SetBin(i,f,herr.Error(i),1.);
|
---|
| 207 | herrd.SetBin(i,herr(i)-f,herr.Error(i),1.);
|
---|
| 208 | }
|
---|
| 209 | char str[64];
|
---|
| 210 | sprintf(str,"h%d",nfile); pos.PutObject(herr,str);
|
---|
| 211 | sprintf(str,"hf%d",nfile); pos.PutObject(herrf,str);
|
---|
| 212 | sprintf(str,"hd%d",nfile); pos.PutObject(herrd,str);
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | // --- Print de debug
|
---|
| 216 | if(nfile==0) {
|
---|
| 217 | cout<<"minimum: "<<min<<endl;
|
---|
| 218 | for(int i=0;i<nvar;i++) cout<<"["<<i<<"] = "<<xnt[i]<<endl;
|
---|
| 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 | //--------------------------------------------------
|
---|
| 231 | MyFCN::MyFCN(HistoErr& herr,GenericFunc& pk)
|
---|
| 232 | : herr_(herr) , pk_(pk)
|
---|
| 233 | {
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | double 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 |
|
---|