Changeset 3381 in Sophya for trunk/Cosmo


Ignore:
Timestamp:
Nov 14, 2007, 7:25:34 PM (18 years ago)
Author:
cmv
Message:

1-/ pkspectrum: GrowthFactor + PkSpectrum on enleve les memorisations

des calculs precedents, tout est recalcule

2-/ cmvfitpk: mise de Ob+Ol+Oc+h100+ns dans le fit

cmv 14/11/2007

Location:
trunk/Cosmo/SimLSS
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cosmo/SimLSS/cmvfitpk.cc

    r3380 r3381  
    2323using namespace ROOT::Minuit2;
    2424
    25 #include "Minuit2/FCNBase.h"
    2625namespace ROOT { namespace Minuit2 {
    2726class MyFCN : public FCNBase {
    2827public:
    29   MyFCN(HistoErr& herr,GenericFunc& pk);
    30   double operator()(const std::vector<double>& par) const;
     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;
    3132  double Up(void) const {return 1.;}
    3233private:
    3334  HistoErr& herr_;
    34   GenericFunc& pk_;
     35  PkSpectrumZ& pk_;
    3536};
    3637} }  // namespace ROOT + Minuit2
     
    5960 double klim[2]={0.,-1.};
    6061 string fconc = "";
     62
     63 // --- les options de print/plot
     64 int nprint = 3, nplot = -1;  // -1 = all
    6165
    6266 // --- Reading arguments
     
    8488 double ocdm0 = om0-ob0;
    8589 TransfertEisenstein tf(h100,ocdm0,ob0,T_CMB_Par,nobaryon);
    86  GrowthFactor growth(om0,ol0);
     90 GrowthFactor growth(om0,ol0); cout<<"...Growth="<<growth(zref)<<endl;
    8791 PkSpectrum0 pk0(pkini,tf);
    8892 PkSpectrumZ pkz(pk0,growth,zref);
     
    125129
    126130 // --- NTuple et ppf
     131 const int npar = 7;  // Number of parameters in the fit
     132 TMatrix<r_8> Cov(npar,npar); Cov = 0.;
    127133 POutPersist pos("cmvfitpk.ppf");
    128  const int nvar = 6;
    129  char *vname[nvar] = {"xi2","a","b","ea","eb","eab"};
     134 const int nvar = 2*npar+1;
     135 char *vname[nvar] = {
     136       "xi2"
     137       ,"b","a","oc","ob","ol","h","n"
     138       ,"eb","ea","eoc","eob","eol","eh","en"
     139       };
    130140 NTuple nt(nvar,vname);
    131141 double xnt[nvar];
     
    134144 // --- Boucle sur les fichiers a fitter
    135145 // ***
    136  int nfile = 0;
     146 int nfile=0, ncov=0;
    137147 for(int ifile=optind;ifile<narg;ifile++) {
    138148
     
    158168   // --- Initialisation de minuit
    159169    MnUserParameters upar;
    160     const int npar = 2;
     170    upar.Add("B",b_ini,b_ini/100.);
    161171    upar.Add("A",1.,0.01);
    162     upar.Add("B",b_ini,b_ini/100.);
     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");
    163182    MyFCN fcn(herr,pkz);
    164183    if(nbinok<upar.VariableParameters()) {
     
    178197
    179198   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   }
    180209
    181210   // --- Recuperation des informations et remplissage NTuple
    182211   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);
    186212   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);
     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];
    192215   nt.Fill(xnt);
    193216
    194    // --- stoquage des histos
    195    if(nfile<10) {
     217   // --- stoquage des histos de plots
     218   if(nfile<nplot || nplot<0) {
    196219     HistoErr herrf(herr); herrf.Zero();
    197220     HistoErr herrd(herr); herrd.Zero();
     221     fcn.Init(parfit);
    198222     for(int i=1;i<herr.NBins();i++) {
    199        double f = A*pkz(herr.BinCenter(i))+B;
     223       double f = fcn.Func(herr.BinCenter(i),parfit);
    200224       herrf.SetBin(i,f,herr.Error(i),1.);
    201225       herrd.SetBin(i,herr(i)-f,herr.Error(i),1.);
     
    208232
    209233   // --- Print de debug
    210    if(nfile<3) {
     234   if(nfile<nprint) {
    211235     cout<<"minimum: "<<min<<endl;
    212236     for(int i=0;i<nvar;i++) cout<<"["<<i<<"] = "<<xnt[i]<<endl;
     
    225249
    226250 // --- Fin du job
    227  if(nfile>1) pos.PutObject(nt,"nt");
     251 if(nfile>0) pos.PutObject(nt,"nt");
     252 if(ncov>0) {Cov /= (double)ncov; pos.PutObject(Cov,"cov");}
    228253
    229254 return 0;
     
    231256
    232257//--------------------------------------------------
    233 MyFCN::MyFCN(HistoErr& herr,GenericFunc& pk)
     258MyFCN::MyFCN(HistoErr& herr,PkSpectrumZ& pk)
    234259  : herr_(herr) , pk_(pk)
    235260{
    236261}
    237262
    238 double MyFCN::operator()(const std::vector<double>& par) const
    239 {
     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);
    240282 double xi2 = 0.;
    241283 for(int i=0;i<herr_.NBins();i++) {
     
    244286   double x = herr_.BinCenter(i);
    245287   double v = herr_(i);
    246    double f = par[0]*pk_(x) + par[1];
     288   double f = Func(x,par);
    247289   xi2 += (v-f)*(v-f)/e2;
    248290 }
     
    252294/*
    253295openppf cmvfitpk.ppf
     296
     297print nt
    254298
    255299set i 0
     
    270314n/plot nt.b%a ! ! "crossmarker5"
    271315
     316n/plot nt.oc
     317n/plot nt.ob
     318n/plot nt.oc+ob
     319n/plot nt.ob%oc ! ! "crossmarker5"
     320
    272321n/plot nt.ea
    273322n/plot nt.eb
    274323n/plot nt.ea%eb ! ! "crossmarker5"
    275324
     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
    276340 */
  • trunk/Cosmo/SimLSS/pkspectrum.cc

    r3378 r3381  
    3131InitialSpectrum::~InitialSpectrum(void)
    3232{
    33 }
    34 
    35 void InitialSpectrum::SetNorm(double a)
    36 {
    37   A_ = a;
    38 }
    39 
    40 void InitialSpectrum::SetSlope(double n)
    41 {
    42   n_ = n;
    4333}
    4434
     
    177167}
    178168
    179 bool TransfertEisenstein::SetParTo(double h100,double OmegaCDM0,double OmegaBaryon0,double tcmb,bool nobaryon)
     169bool TransfertEisenstein::SetParTo(double h100,double OmegaCDM0,double OmegaBaryon0)
    180170// Changement des valeurs des parametres (suivi de re-init eventuel)
     171// Si h100,Omega...<=0. alors pas de changement, on garde l'ancienne valeur
    181172{
    182173 bool haschanged = false;
     
    185176 if(OmegaCDM0>0.) {Oc_ = OmegaCDM0; haschanged = true;}
    186177 if(OmegaBaryon0>0.) {Ob_ = OmegaBaryon0; haschanged = true;}
    187  if(tcmb>0.) {tcmb_ = tcmb; haschanged = true;}
    188  if(nobaryon!=nobaryon_) {nobaryon_ = nobaryon; haschanged = true;}
    189178
    190179 // et on recalcule les initialisations
     
    371360// Pour avoir D(z) = 1/(1+z) faire: OmegaMatter0=1 OmegaLambda0=0
    372361GrowthFactor::GrowthFactor(double OmegaMatter0,double OmegaLambda0)
    373   : O0_(OmegaMatter0) , Ol_(OmegaLambda0) , Ok_(1.-OmegaMatter0-OmegaLambda0)
     362  : O0_(OmegaMatter0) , Ol_(OmegaLambda0)
    374363{
    375364  if(OmegaMatter0==0.) {
     
    377366    throw ParmError("GrowthFactor::GrowthFactor: Error badOmegaMatter0  value");
    378367  }
    379   norm_ = 1.; // puisque (*this)(0.) a besoin de norm_
    380   norm_ = (*this)(0.);
    381   cout<<"GrowthFactor::GrowthFactor : norm="<<norm_<<endl;
    382368}
    383369
    384370GrowthFactor::GrowthFactor(GrowthFactor& d1)
    385   : O0_(d1.O0_) , Ol_(d1.Ol_) , Ok_(d1.Ok_) , norm_(d1.norm_)
     371  : O0_(d1.O0_) , Ol_(d1.Ol_)
    386372{
    387373}
     
    396382 z += 1.;
    397383 double z2 = z*z, z3 = z2*z;
    398  double den = Ol_ + Ok_*z2 + O0_*z3;
     384
     385 // Calcul de la normalisation (pour z=0 -> growth=1.)
     386 double D1z0 = pow(O0_,4./7.) - Ol_ + (1.+O0_/2.)*(1.+Ol_/70.);
     387 D1z0 = 2.5*O0_ / D1z0;
     388
     389 // Calcul du growthfactor pour z
     390 double Ok = 1. - O0_ - Ol_;
     391 double den = Ol_ + Ok*z2 + O0_*z3;
    399392 double o0z = O0_ *z3 / den;
    400393 double olz = Ol_ / den;
    401394
    402  // 4./7. = 0.571429
    403  double D1z = pow(o0z,0.571429) - olz + (1.+o0z/2.)*(1.+olz/70.);
     395 double D1z = pow(o0z,4./7.) - olz + (1.+o0z/2.)*(1.+olz/70.);
    404396 D1z = 2.5*o0z / z / D1z;
    405397
    406  return D1z / norm_;
    407 }
    408 
    409 bool GrowthFactor::SetParTo(double OmegaMatter0,double OmegaLambda0)
    410 {
    411  bool haschanged = false;
    412 
    413  if(OmegaMatter0>0.) {O0_ = OmegaMatter0; haschanged = true;}
    414  if(fabs(OmegaLambda0+12345.)>1e-6) {Ol_ = OmegaLambda0; haschanged = true;}
    415 
    416  // et on recalcule les initialisations
    417  if(haschanged) {
    418    Ok_ = 1. - O0_ - Ol_;
    419    norm_ = 1.; // puisque (*this)(0.) a besoin de norm_
    420    norm_ = (*this)(0.);
    421  }
    422 
    423  return haschanged;
     398 return D1z / D1z0;
     399}
     400
     401void GrowthFactor::SetParTo(double OmegaMatter0,double OmegaLambda0)
     402{
     403 if(OmegaMatter0>0.) O0_ = OmegaMatter0;
     404 Ol_ = OmegaLambda0;
     405}
     406
     407bool GrowthFactor::SetParTo(double OmegaMatter0)
     408// idem precedent sans changer OmegaLambda0
     409{
     410 if(OmegaMatter0<=0.) return false;
     411 O0_ = OmegaMatter0;
     412 return true;
    424413}
    425414
     
    453442PkSpectrumZ::PkSpectrumZ(PkSpectrum0& pk0,GrowthFactor& d1,double zref)
    454443  : pk0_(pk0) , d1_(d1) , zref_(zref) , scale_(1.) , typspec_(PK)
    455   , zold_(-1.) , d1old_(1.)
    456444{
    457445}
     
    459447PkSpectrumZ::PkSpectrumZ(PkSpectrumZ& pkz)
    460448  : pk0_(pkz.pk0_) , d1_(pkz.d1_) , zref_(pkz.zref_) , scale_(pkz.scale_) , typspec_(PK)
    461   , zold_(pkz.zold_) , d1old_(pkz.d1old_)
    462449{
    463450}
     
    481468double PkSpectrumZ::operator() (double k,double z)
    482469{
    483   double d1;
    484   if(z == zold_) d1 = d1old_;
    485     else {d1 = d1old_ = d1_(z); zold_ = z;}
     470  double d1 = d1_(z);
    486471
    487472  double v = pk0_(k) * d1*d1;
  • trunk/Cosmo/SimLSS/pkspectrum.h

    r3378 r3381  
    1414  virtual ~InitialSpectrum(void);
    1515  virtual double operator() (double k) {return A_ * pow(k,n_);}
    16   void SetNorm(double a);
    17   void SetSlope(double n);
     16  void SetNorm(double a) {A_ = a;}
     17  void SetSlope(double n) {n_ = n;}
    1818protected:
    1919  double n_, A_;
     
    2929  TransfertEisenstein(TransfertEisenstein& tf);
    3030  virtual ~TransfertEisenstein(void);
    31   bool SetParTo(double h100=-1.,double OmegaCDM0=-1.,double OmegaBaryon0=-1.,double tcmb=-1.,bool nobaryon=false);
     31  bool SetParTo(double h100,double OmegaCDM0,double OmegaBaryon0);
    3232  virtual double operator() (double k);
    3333  double KPeak(void);
     
    7777  virtual ~GrowthFactor(void);
    7878  virtual double operator() (double z);
    79   bool SetParTo(double OmegaMatter0=-1.,double OmegaLambda0=-12345.);
     79  void SetParTo(double OmegaMatter0,double OmegaLambda0);
     80  bool SetParTo(double OmegaMatter0);
    8081protected:
    81   double O0_,Ol_,Ok_;
    82   double norm_;
     82  double O0_,Ol_;
    8383};
    8484
     
    107107  virtual double operator() (double k);
    108108  virtual double operator() (double k,double z);
    109   inline void   SetZ(double z) {zref_ = z;}
    110   inline double GetZ(void) {return zref_;}
     109  void   SetZ(double z) {zref_ = z;}
     110  double GetZ(void) {return zref_;}
    111111  void SetTypSpec(ReturnSpectrum typspec=PK);
    112   inline void SetScale(double scale=1.) {scale_=scale; zold_=-1.;}
    113   inline double GetScale(void) {return scale_;}
     112  void SetScale(double scale=1.) {scale_=scale;}
     113  double GetScale(void) {return scale_;}
    114114  PkSpectrum0& GetPk0(void) {return pk0_;}
    115115  GrowthFactor& GetGrowthFactor(void) {return d1_;}
     
    119119  double zref_, scale_;
    120120  ReturnSpectrum typspec_;
    121   mutable double zold_, d1old_;
    122121};
    123122
Note: See TracChangeset for help on using the changeset viewer.