source: Sophya/trunk/Cosmo/SimLSS/cmvschdist.cc@ 4026

Last change on this file since 4026 was 3615, checked in by cmv, 16 years ago

Modifs relatives a l'introduction de RandomGeneratorInterface + delete de srandgen.c, cmv 01/05/2009

File size: 7.2 KB
RevLine 
[3284]1/* Distributions de masse pour n=1,2,3,... tirages */
2#include "sopnamsp.h"
3#include "machdefs.h"
[3319]4// Pour faire les histogrammes des distributions de masses
5// pour ntirages:
6// ex: on a un pixel qui a N galaxies, quelle est la distribution
7// statistique de masse dans un tel pixel ?
[3344]8// > cmvschdist -a -v -m 1e+7,1e+13 -n -100 -r 200 -N 100000
[3320]9// compare with:
[3344]10// > cmvschdist -a -v -m 1e+7,1e+13 -n -100 -r 200 -N 100000 -0
[3322]11// Fabriquer un fichier pour la prod
[3344]12// > cmvschdist -a -m 1e+7,1e+13 -n -100 -r 2000 -N 5000000 -W schdist_2000.ppf
[3284]13#include <iostream>
14#include <stdlib.h>
15#include <stdio.h>
16#include <string.h>
17#include <math.h>
18#include <unistd.h>
[3615]19#include "sophyainit.h"
[3284]20#include "timing.h"
21#include "histos.h"
22#include "ntuple.h"
[3615]23//#include "randr48.h"
24#include "randfmt.h"
[3284]25#include "srandgen.h"
26#include "perandom.h"
27
28#include "geneutils.h"
29#include "cosmocalc.h"
30#include "schechter.h"
31
32#include <vector>
33
34void usage(void);
35void usage(void)
36{
[3320]37 cout<<"cmvschdist -m xmin,xmax -a -0 -n nbin -r ngmax,ngmin -N nalea "
38 <<"-R readfile.ppf -W writefile.ppf"<<endl
39 <<" -m xmin,xmax : limites en masse"<<endl
40 <<" -n nbin : nombre de bins (si <0 alors nb par decade)"<<endl
41 <<" -r ngmax,ngmin : distribution pour tirage de ngmin a ngmax galaxies"<<endl
42 <<" -N nalea : nombre de tirages aleatoires"<<endl
[3319]43 <<" -v : back verification of random trials"<<endl
[3320]44 <<" -0 : only use ngal=1 histogram"<<endl
45 <<" -a : init auto de l\'aleatoire"<<endl
46 <<" -R readfile.ppf : read SchechterMassDist from ppf file"<<endl;
[3284]47}
48
49int main(int narg,char *arg[])
50{
[3615]51SophyaInit();
[3284]52 double h75 = 0.71 / 0.75;
53 double nstar = 0.006*pow(h75,3.); //
[3343]54 double mstar = pow(10.,9.8); // MSol
[3284]55 double alpha = -1.37;
56 cout<<"h75= "<<h75<<" nstar= "<<nstar<<" mstar="<<mstar<<" alpha="<<alpha<<endl;
57
[3344]58 double xmin=1.e7, xmax=1.e13;
[3320]59 int npt = -100;
[3284]60 int ngmax = 200, ngmin = 1;
[3320]61 bool useonly1 = false;
[3284]62 unsigned long long nalea = 10000;
[3319]63 bool verif = false;
[3320]64 bool readfrppf = false;
65 string namefrppf = "";
66 string nametoppf = "";
[3284]67
[3615]68 // --- Choix du generateur aleatoire (a faire ICI imperativement avant AutoInitRand)
69 FMTRandGen *RandGen = new FMTRandGen;
70 RandGen->SelectGaussianAlgo(C_Gaussian_RandLibSNorm);
71 RandGen->SelectPoissonAlgo(C_Poisson_Ahrens);
72 RandomGeneratorInterface::SetGlobalRandGenP(RandGen);
73
74 // --- decodage des arguments
[3284]75 char c;
[3320]76 while((c = getopt(narg,arg,"h0avm:N:n:r:R:W:")) != -1) {
[3284]77 switch (c) {
78 case 'm' :
79 sscanf(optarg,"%lf,%lf",&xmin,&xmax);
80 if(xmin<=0.) xmin=1e6;
81 if(xmax<=0.) xmax=xmin;
82 if(xmin>xmax) xmax=10.*xmin;
83 break;
84 case 'r' :
85 sscanf(optarg,"%d,%d",&ngmax,&ngmin);
86 if(ngmin<=0) ngmin=1;
87 if(ngmax<=0) ngmax=ngmin;
88 if(ngmin>ngmax) ngmin=ngmax;
89 break;
90 case 'n' :
91 sscanf(optarg,"%d",&npt);
[3320]92 if(npt<=0) npt = -100;
[3284]93 break;
94 case 'N' :
95 sscanf(optarg,"%llu",&nalea);
96 if(nalea<=0) nalea=10000;
97 break;
[3319]98 case 'v' :
99 verif = true;
100 break;
[3320]101 case '0' :
102 useonly1 = true;
103 break;
104 case 'R' :
105 readfrppf = true;
106 namefrppf = optarg;
107 break;
108 case 'W' :
109 nametoppf = optarg;
110 break;
[3284]111 case 'a' :
[3615]112 AutoInitRand(5);
[3284]113 break;
114 case 'h' :
115 default :
116 usage(); return -1;
117 }
118 }
119
[3320]120 double lnx1=log10(xmin), lnx2=log10(xmax);
121 cout<<"xmin="<<xmin<<" ("<<lnx1<<") xmax="<<xmax<<" ("<<lnx2<<")"<<endl;
[3284]122 cout<<"npt="<<npt<<", nalea="<<nalea<<endl;
[3320]123 cout<<"useonly1="<<useonly1<<" ngmin="<<ngmin<<" ngmax="<<ngmax<<endl;
[3319]124 cout<<"verif="<<verif<<endl;
[3320]125 if(readfrppf) cout<<"SchechterMassDist will be read from file "<<namefrppf<<endl;
126 if(nametoppf.size()>0) cout<<"SchechterMassDist will be written to file "<<nametoppf<<endl;
[3284]127
128
129 //-------m*dn/dm
130 cout<<"> Schechter m*dn/dm nstar="<<nstar<<" mstar="<<mstar<<" alpha="<<alpha<<endl;
131 Schechter sch(nstar,mstar,alpha);
132
[3320]133 //------- Construct Mass Distribution
134 cout<<"> Creating Mass Distribution"<<endl;
135 SchechterMassDist schdmass(sch,xmin,xmax,npt);
136 if(readfrppf) {
137 cout<<"> Mass Distribution read from "<<namefrppf<<endl;
138 schdmass.ReadPPF(namefrppf);
[3284]139 }
[3320]140 Histo hmdndm = schdmass.GetHmDnDm();
141 FunRan tirhmdndm = schdmass.GetTmDnDm();
[3284]142
143 //------- Random
144 InitTim();
[3320]145 if(!useonly1 && !readfrppf) {
146 cout<<"> Creating "<<ngmax-ngmin+1<<" histos and filling with "<<nalea<<" trials"<<endl;
147 schdmass.SetNgalLim(ngmax,ngmin,nalea);
148 PrtTim("End of filling histos for trials");
[3284]149 }
[3320]150 schdmass.Print();
[3284]151
[3320]152 //------- Generation des histos de verif
153 vector<Histo> vthisto; vector<string> vthname;
[3319]154 if(verif) {
[3320]155 cout<<"> Creating "<<ngmax-ngmin+1<<" Histos for back-verif"<<endl;
[3319]156 for(int i=ngmin;i<=ngmax;i++) {
[3320]157 Histo h(hmdndm); h.Zero();
[3319]158 vthisto.push_back(h);
[3320]159 char str[32]; sprintf(str,"th%d",i);
[3319]160 vthname.push_back(string(str));
161 }
[3320]162 cout<<" "<<vthisto.size()<<" Histos created"<<endl;
163 vthisto[20].Show();
[3319]164
165 cout<<"> Checking tirage"<<endl;
[3320]166 int lpmod = nalea/20; if(lpmod<=0) lpmod=1;
[3319]167 for(unsigned long long ia=0;ia<nalea;ia++) {
[3320]168 if(ia%lpmod==0) cout<<"...filling tirage "<<ia<<endl;
169 for(unsigned int i=0;i<vthisto.size();i++) {
170 int ng = ngmin+i;
171 double v = schdmass.TirMass(ng)/(double)ng;
172 vthisto[i].Add(log10(v));
[3319]173 }
174 }
[3320]175 schdmass.PrintStatus();
[3319]176 PrtTim("End Random loop back-tirage");
177 }
178
[3320]179 //------- Ecritures ppf
180 if(nametoppf.size()>0) {
181 cout<<"Ecriture de l\'objet SchechterMassDist dans "<<nametoppf<<endl;
182 schdmass.WritePPF(nametoppf);
183 }
184
185 cout<<"Ecriture pour verification"<<endl;
[3284]186 string tag = "cmvschdist.ppf";
187 POutPersist pos(tag);
[3320]188 {
189 cout<<" writing hmdndm tirhmdndm"<<endl;
[3284]190 tag = "hmdndm"; pos.PutObject(hmdndm,tag);
[3320]191 Histo hdum(tirhmdndm);
192 tag = "tirhmdndm"; pos.PutObject(hdum,tag);
193 }
194 if(schdmass.GetNgalLim()>0) {
195 cout<<" writing h t"<<endl;
196 for(int i=0;i<schdmass.GetNgalLim();i++) {
197 int ng = schdmass.NGalFrIndex(i);
198 if(ng<=0) continue;
199 char str[32];
200 sprintf(str,"h%d",ng);
201 Histo hdum = schdmass.GetHisto(i);
202 tag = str; pos.PutObject(hdum,tag);
203 sprintf(str,"t%d",ng);
204 Histo hdum2(schdmass.GetFunRan(i));
205 tag = str; pos.PutObject(hdum2,tag);
206 }
207 }
208 if(vthisto.size()>0) {
209 cout<<" writing th"<<endl;
210 for(unsigned int i=0;i<vthisto.size();i++)
[3319]211 if(vthisto[i].NEntries()>0) pos.PutObject(vthisto[i],vthname[i]);
[3320]212 }
[3284]213
[3615]214 delete RandGen;
215
[3320]216 PrtTim("End of Job");
217
[3284]218 return 0;
219}
220
221/*
222delobjs *
223openppf cmvschdist.ppf
224
[3319]225set n 70
226echo ${h70.vmax}
227
228set h h$n
[3320]229set t t$n
[3319]230set th th$n
231
[3320]232disp tirhmdndm
233disp $t "same red"
234
[3284]235disp $h
[3319]236disp $th "same red"
237
[3284]238n/plot $h.val%x ! ! "connectpoints"
[3319]239n/plot $th.val%x ! ! "nsta connectpoints same red"
240
[3284]241n/plot $h.log10(val)%x val>0. ! "connectpoints"
[3319]242n/plot $th.log10(val)%x val>0. ! "nsta connectpoints same red"
[3284]243
[3320]244n/plot $h.val%pow(10.,x) ! ! "connectpoints"
245n/plot $th.val%pow(10.,x) ! ! "nsta connectpoints same red"
246
[3284]247c++exec \
248for(int i=0;i<$h.NBins();i++) \
249 if($h(i)>0.) { \
250 cout<<i<<" m="<<$h.BinCenter(i)<<" ("<<pow(10.,$h.BinCenter(i))<<") h="<<$h(i)<<endl; \
251 break; \
252} \
253for(int i=$h.NBins()-1;i>=0;i--) \
254 if($h(i)>0.) { \
255 cout<<i<<" m="<<$h.BinCenter(i)<<" ("<<pow(10.,$h.BinCenter(i))<<") h="<<$h(i)<<endl; \
256 break; \
257}
258
[3319]259# Compare evolution
[3322]260disp h2000
261disp h1000 "same red"
262disp h500 "same blue"
263disp h200 "same green"
[3319]264disp h100 "same red"
265disp h50 "same blue"
266disp h10 "same green"
267disp h1 "same yellow"
[3284]268*/
Note: See TracBrowser for help on using the repository browser.