#include #include #include #include #include #include #include #include #include #include #ifdef NEUTRINOFLUX #include "neutrinoflux/NeutrinoFlux.h" #endif using namespace std; const double cv=M_PI/180; const double xx=1.e-5, zz=1.e-10; int mode=0; double ta=0, tg=0, xt=0, pt=0; double LT=182.5; int FR=10; #ifdef NEUTRINOFLUX bool fnfx=false; bool fpfx=false; NeutrinoFlux * nfx = NULL, * pfx = NULL; #endif struct line{ int nch; double sth, sph, rth, rph, Ep, Em, w, wt, an, sbm, fa, en; double zth, cth, lne, lnp, c, x; void init(){ zth=cos(cv*sth); if(Ep>0){ #ifdef NEUTRINOFLUX if(fnfx){ double nFx = (nfx->getFlux(I3Particle::NuMu, Ep, zth) + nfx->getFlux(I3Particle::NuMuBar, Ep, zth))/2; w=nFx*24*3600*wt; if(fpfx){ double pFx = (pfx->getFlux(I3Particle::NuMu, Ep, zth) + pfx->getFlux(I3Particle::NuMuBar, Ep, zth))/2; c=pFx*24*3600*wt; } else c=0; } else #endif { w-=24*3600*wt*0.99e-8/(Ep*Ep); c=0; } x=24*3600*wt*1.e-8/(Ep*Ep); if(w<0) w=0; w*=LT/FR; if(c<0) c=0; c*=LT/FR; if(x<0) x=0; x*=LT/FR; } cth=1+cos(cv*rth); if(mode==5002) en=Em; else if(mode==5003) en=Ep; lne=log10(en/1.e3); Ep/=1.e3; lnp=log10(Ep); } double rw(double a, double g, double e, double p) const{ a+=1; double r=a>0?w*a*(g==0?1:pow(Ep, g)):0; if(e>0) r+=e*x; if(p>0) r+=p*c; return r; } }; inline bool ls(const line& a, const line& b){ return a.w>(istream& in, line& l){ in>>l.nch>>l.sth>>l.sph>>l.rth>>l.rph>>l.Ep>>l.Em>>l.w>>l.wt>>l.an>>l.sbm>>l.fa>>l.en; return in; } vector dat, sim, dtx, smx; void load(){ line l; while(cin>>l){ int run; if(cin>>run) if(l.sbm>0){ l.init(); // if(0.2<=l.cth && l.cth<0.4){ // if(-20){ sim.push_back(l); } else{ if(run%FR==0) dat.push_back(l); } } } } cerr<& x){ Zstep=(Zmax-Zmin)/Znum; Estep=(Emax-Emin)/Enum; for(int i=0; i::const_iterator i=x.begin(); i!=x.end(); ++i){ int Zi=(int) floor((i->cth-Zmin)/Zstep); if(Zi<0) Zi=0; else if(Zi>=Znum) Zi=Znum-1; int Ei=(int) floor((i->lne-Emin)/Estep); if(Ei<0) Ei=0; else if(Ei>=Enum) Ei=Enum-1; if(0<=Ei && Eirw(a, g, e, p); } } double a, g, e, p; ar(){ a=0; g=0; e=0; p=0; }; void set(double a, double g, double e, double p){ this->a=a; this->g=g; this->e=e; this->p=p; } }; int qdbl(const void *a, const void *b){ double ax=*(double *)a; double bx=*(double *)b; return axbx?1:0; } int var=0; // 0=(a,g); 1=(e,p) for any (a,g) double vax[2]; double llx(const gsl_vector *, void *); struct pr{ ar d, s; double llh(){ double r=0; for(int i=0; i0){ double ni=d.r[i][j]; r+=mu-ni*log(mu)+gsl_sf_lnfact((int) ni); } } return 2*r; } static const int anum=21, gnum=21; double amin, amax; double gmin, gmax; double sa, ga; void init(){ if(var==1){ amin=0; amax=8; gmin=0; gmax=2; } else if(var==2){ amin=0; amax=2; gmin=0; gmax=1; } else{ amin=-0.5; amax=0.5; gmin=-0.5; gmax=0.5; } sa=(amax-amin)/(anum-1); ga=(gmax-gmin)/(gnum-1); } pr(){ init(); } double all[4]; double minv(int n){ const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex; gsl_multimin_fminimizer *s = NULL; gsl_vector *t, *x; gsl_multimin_function F; size_t iter = 0; int status; double size; x = gsl_vector_alloc(n); gsl_vector_set_all(x, 0.); t = gsl_vector_alloc(n); gsl_vector_set_all(t, 0.1); F.n = n; F.f = &llx; F.params = vax; s = gsl_multimin_fminimizer_alloc(T, n); gsl_multimin_fminimizer_set(s, &F, x, t); do{ iter++; status = gsl_multimin_fminimizer_iterate(s); if(status) break; size = gsl_multimin_fminimizer_size(s); status = gsl_multimin_test_size(size, xx); if(status == GSL_SUCCESS) break; } while(status == GSL_CONTINUE && iter < 100); double fc=s->fval; if(n==2){ for(int i=0; i<2; i++) all[i]=gsl_vector_get(s->x, i); for(int i=2; i<4; i++) all[i]=var>0?vax[i-2]:0; } else{ for(int i=0; i<4; i++) all[i]=gsl_vector_get(s->x, i); } cerr<0?4:2); i++) cerr<<" "<0){ vax[0]=e; vax[1]=p; return minv(2); } else{ all[0]=e; all[1]=p; all[2]=0; all[3]=0; return llh(e, p); } } double eval(){ return lly(0, 0)-minv(var>0?4:2); } double eval(double lz[3][anum][gnum]){ int ia, ig; double a, g, min; bool flag=true; for(a=amin, ia=0; ia0?4:2); for(ia=0; ia::iterator i=smx.begin(); i!=smx.end(); ++i) i->w=i->rw(a, g, e, p); sort(smx.begin(), smx.end(), ls); } double random(){ double rnd=rand(); rnd/=RAND_MAX; return rnd; } void draw(){ double sum=0; double sw[smx.size()]; int size=0; for(vector::const_iterator i=smx.begin(); i!=smx.end(); ++i){ sum+=i->w; sw[size++]=sum; } dtx.erase(dtx.begin(), dtx.end()); double lnr=0; while(true){ lnr-=log(random()); if(lnr>sum) break; double rnd=sum*random(); for(int k=0; krnd){ line l=smx[k]; l.w=1; dtx.push_back(l); break; } } } void prof(){ init(); const int tot=100, t90=90; double lx[tot], lz[3][anum][gnum]; if(mode&1){ draw(ta, tg, xt, pt); draw(); d.create(dtx); } else{ d.create(dat); } smx=sim; eval(lz); int ia, ig; double a, g; for(a=amin, ia=0; ia0) draw(lz[1][ia][ig], lz[2][ia][ig], a, g); else draw(a, g, 0, 0); for(int i=0; i0) cout<<" "<0){ if(x->size==2) result=v.llh(gsl_vector_get(x, 0), gsl_vector_get(x, 1), ((double *) p)[0], ((double *) p)[1]); else if(x->size==4) result=v.llh(gsl_vector_get(x, 0), gsl_vector_get(x, 1), gsl_vector_get(x, 2), gsl_vector_get(x, 3)); } else{ result=v.llh(gsl_vector_get(x, 0), gsl_vector_get(x, 1)); } return result; } struct epar{ int num; double val; double sto; void *par; gsl_vector *x; }; double nll_f (const gsl_vector *v, void *par); double svd_err(double x, void *par); double svd_f (const gsl_vector *x, void *par); double svd_fv (const gsl_vector *x, void *par); void svd_df (const gsl_vector *x, void *par, gsl_vector *df); void svd_fdf (const gsl_vector *x, void *par, double *f, gsl_vector *df); struct uf{ static const bool lg=true; static const int Znum=1, Enum=15, Pnum=12, Snum=5; static const int Xnum=Znum*Enum; static const double Zmin=0, Zmax=1.2; double Zstep; static const double Emin=-2, Emax=3; double Estep; static const double Pmin=-1, Pmax=3, Ptop=4; double Pstep; double r[Xnum][Pnum], w[Xnum][Pnum], d[Xnum], e[Pnum], s[Pnum], ave[Pnum], afl[Pnum], af2[Pnum], avl[Pnum], av2[Pnum], stot; double rx[Xnum][Pnum], rp[Xnum][Pnum]; double fA, fD; void create(){ Zstep=(Zmax-Zmin)/Znum; Estep=(Emax-Emin)/Enum; Pstep=(Pmax-Pmin)/Pnum; for(int i=0; i::const_iterator i=smx.begin(); i!=smx.end(); ++i){ int Zi=(int) floor((i->cth-Zmin)/Zstep); if(Zi<0) Zi=0; else if(Zi>=Znum) Zi=Znum-1; int Ei=(int) floor((i->lne-Emin)/Estep); if(Ei<0) Ei=0; else if(Ei>=Enum) Ei=Enum-1; int Pi=(int) floor((i->lnp-Pmin)/Pstep); if(Pi<0) Pi=0; else if(Pi>=Pnum) Pi=Pnum-1; else if(Pi>=Pnum-1) Pi=Pnum-2; if(0<=Ei && Eiw; // i->wt/(i->Ep*i->Ep); r[Zi*Enum+Ei][Pi]+=g; w[Zi*Enum+Ei][Pi]+=g*g; rx[Zi*Enum+Ei][Pi]+=g*i->lne; rp[Zi*Enum+Ei][Pi]+=g*i->lnp; double E=pow(10, i->lnp); e[Pi]+=i->wt; s[Pi]+=i->w; ave[Pi]+=i->w*E; if(i->wt>0){ afl[Pi]+=i->w*i->w/i->wt; if(i->lnpw*i->w/(i->wt/(E*E)); } avl[Pi]+=i->wt; if(i->lnpwt/(E*E); } } for(int p=0; p0?w[x][p]/r[x][p]:0; sum+=r[x][p]; if(r[x][p]>0){ rx[x][p]/=r[x][p]; rp[x][p]/=r[x][p]; } else{ rx[x][p]=Emin+(x%Enum+0.5)*Estep; rp[x][p]=Pmin+(p+0.5)*Pstep; } } if(sum>0) for(int x=0; x0){ afl[p]/=s[p]; afl[p]=s[p]/afl[p]; } if(af2[p]>0){ af2[p]/=s[p]; af2[p]=s[p]/af2[p]; } } if(false){ bool verbose=false; if(verbose) for(int x=0; x0) for(int x=1; x::const_iterator i=dtx.begin(); i!=dtx.end(); ++i){ int Zi=(int) floor((i->cth-Zmin)/Zstep); if(Zi<0) Zi=0; else if(Zi>=Znum) Zi=Znum-1; int Ei=(int) floor((i->lne-Emin)/Estep); if(Ei<0) Ei=0; else if(Ei>=Enum) Ei=Enum-1; if(0<=Ei && Eiw; } { int nA=0, nD=0; double fA=0, fD=0; for(int i=0; i0?d[i]*(1-log(d[i])):0; for(int j=0; j0?w[i][j]:1; nA++; if(s>0){ a/=s; fA+=gsl_sf_lngamma(a+1); fA+=a>0?a*(1-log(a)):0; } } } cerr<<"Normalization: "<fD=fD; this->fA=fA; } } int nl, nf; static const int nx=30; double f2co(double x, double *q, double *p){ if(nf==0) return p[0]+p[1]*x; double yx=pow(10, x); double y1=pow(10, q[0]); double y2=pow(10, q[1]); double p1=1.e-2+p[2]*p[2]; double p2=1.e-2+p[3]*p[3]; return p[0]-(p[1]/p2)*log10(pow(1/pow(pow(yx, p1)+pow(y1, p1), 1/p1), p2)+pow(1/y2, p2)); } double getA(const gsl_vector *s, int i, int j){ double x=rx[i][j], p=rp[i][j]; double v[nx]; for(int i=0; i static const int mg=1; double llh(const gsl_vector *x){ double tot=0; for(int i=mg; i0?w[i][j]:1, A=getA(x, i, j); if(s>0) tot+=a>0?A/s-(a/s)*log(A/s):1/zz; } } return tot; } void smoothr(){ int status; double size; double par[nx]={ -2, 10, -0.1, 1, 1, 1, -0.3, 0, 1, 1, -5, 0.5, 1, 1, 0.3, -0.2, 0.2, 0.2, 1, 0, 1, 1, -0.3, 0, 1, 1, -5, 0.5, 1, 1 }; int ndof=0; double fact=0; for(int i=mg; i0?w[i][j]:1; ndof++; if(s>0) fact+=gsl_sf_lngamma(a/s+1); } } int max=15, inc=10; double sav[nx]={ 0.0145518, 7.88033, -0.107519, 1.28452, 0.791684, 0.31928, -0.058879, -0.0267921, -0.171447, 6.40506, -2.47516, -0.000622433, -0.193963, -0.132379, 0.75498, -0.153069, 0.0557296, 0.104804, -4.52597, -2.37647e-05, 0.722819, 1.65858, -0.0164029, 1.3459, 0.331298, 0.238985, -0.253326, -0.136471, 0.685599, 6.15041 }; if(true){ nl=nx; nf=1; max=-1; gsl_vector * X = gsl_vector_alloc (nl); for(int k=0; kfval+fact)/(ndof-nl))<::const_iterator i=smx.begin(); i!=smx.end(); ++i){ int Zi=(int) floor((i->cth-Zmin)/Zstep); if(Zi<0) Zi=0; else if(Zi>=Znum) Zi=Znum-1; int Ei=(int) floor((i->lne-Emin)/Estep); if(Ei<0) Ei=0; else if(Ei>=Enum) Ei=Enum-1; int Pi=(int) floor((i->lnp-Pmin)/Pstep); if(Pi<0) Pi=0; else if(Pi>=Pnum) Pi=Pnum-1; else if(Pi>=Pnum-1) Pi=Pnum-2; if(0<=Ei && Eiw; } for(int k=0; k0?1/d[j]:0); gsl_vector_set(Y, j, d[j]); } for(int j=0; jfval<gradient, 1e-3); // if (status == GSL_SUCCESS) cerr<<" Minimum found,"; } while (status == GSL_CONTINUE && iter < 10000); // cerr<<" steps="<0){ if(lg){ double pk=gsl_vector_get(X, k); double pn=gsl_vector_get(X, n); if(pk>zz && pn>zz){ double sub=0; if(k==n) for(int i=0; izz?1/aux:0); } gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1, Q, E, 0, A); gsl_blas_dgemm(CblasNoTrans, CblasTrans, 1, A, B, 0, V); gsl_matrix_free(A); gsl_matrix_free(B); gsl_vector_free(work); gsl_matrix_free(E); } else{ // UL inversion int s; gsl_permutation * P = gsl_permutation_alloc (Pnum); gsl_linalg_LU_decomp (Q, P, &s); gsl_linalg_LU_invert (Q, P, V); gsl_permutation_free(P); gsl_vector_set_all(S, 1); } gsl_matrix_free(Q); for(int k=0; k0){ if(verbose) cout<<"Failed to bracket root ("<0){ const size_t n = N; const size_t ncoeffs = NCOEFFS; const size_t nbreak = NBREAK; gsl_bspline_workspace *bw; gsl_vector *B; double dy; gsl_vector *c, *w; gsl_vector *x, *y; gsl_matrix *X, *cov; gsl_multifit_linear_workspace *mw; double chisq; bw = gsl_bspline_alloc(K, nbreak); B = gsl_vector_alloc(ncoeffs); x = gsl_vector_alloc(n); y = gsl_vector_alloc(n); X = gsl_matrix_alloc(n, ncoeffs); c = gsl_vector_alloc(ncoeffs); w = gsl_vector_alloc(n); cov = gsl_matrix_alloc(ncoeffs, ncoeffs); mw = gsl_multifit_linear_alloc(n, ncoeffs); for(int i=0; i0?1/(we*we):0); } if(false) gsl_bspline_knots_uniform(Pmin, Ptop, bw); else{ double bps[NBREAK]={BPSENTRIES}; gsl_vector * breakpts; breakpts = gsl_vector_alloc(NBREAK); for(int i=0; i0) for(int i=0; i0) spline(u, z); for(j=0; j0?u[i]*r[j][i]/qtr[j]:0; for(int k=0; k0){ double cpp=r[s][u]*w[s][u]; #define D(a,b) (a==b?1:0) double mki=M[k][i]*(D(u,k)-M[u][i])*(D(i,s)/r[s][u]-1); double mlj=M[l][j]*(D(u,l)-M[u][j])*(D(j,s)/r[s][u]-1); #undef D cmm+=cpp*mki*mlj; } aux+=d[i]*d[j]*cmm; } if(k==l) y[k]=sqrt(aux); V[k][l]=aux; cerr<<"."; } cerr<0){ double aux=r[j][i]/qtr[j]; ui+=aux*d[j]; vi+=aux*aux*d[j]; } v[i]=sqrt(vi)*u[i]; u[i]*=ui; z[i]=sqrt(v[i]*v[i]+y[i]*y[i]); } for(i=0; ic_init(p); if(barlow){ for(int i=0; ipx) px=ax; double aux=pj*(aij+di*sij); if(aux>0){ aux=(pj*aij-di)/aux; if(f || aux>tx) tx=aux; f=false; } } do{ tf=ti; double f=0, df=0; for(int j=0; j1) ti=(1+tf)/2; if(tixx*(1+px*ti)); } double fi=0; for(int j=0; j0?di*(1+log(max(zz, fi/di))):0); if(barlow) for(int j=0; j0){ Aij/=sij; aij/=sij; F+=Aij-(aij>0?aij*(1+log(max(zz, Aij/aij))):0); } } } if(verbose || u.tau>0) for(int k=0; k0?1/max(zz, fi/di):0); } for(int j=0; j0) if(!u.lg || p[j]>zz){ double sum=0; for(int n=0; nb.nch; } inline bool line_lne(const line& a, const line& b){ return a.lne>b.lne; } struct mu90{ ival acival(double b, double s){ double m=b+s; ival result; if(m>0){ double t=0; int nx=int(m); int ny=nx+1; bool start=true, fx=true, fy=true; double px, py, rx, ry; while(true){ if(start){ px=exp(nx*log(m)-m-gsl_sf_lnfact(nx)); py=px*m/ny; start=false; } else{ if(nx>=0 && fx){ px*=nx/m; nx--; } if(fy){ ny++; py*=m/ny; } } if(nx>=0 && fx){ double mx=nx; if(mx0?log(m/mx):0)-(m-mx); } if(fy){ double my=ny; if(my0?log(m/my):0)-(m-my); } if(nx<0 || ry>rx || ry==rx && py>px){ t+=py; fx=false; fy=true; } else{ t+=px; fx=true; fy=false; } if(t>=0.9){ result.n1=fx?nx:nx+1; result.n2=fy?ny:ny-1; break; } } } return result; } double ulimit(double b, int n){ double s=max(n-b, 0.), d=max(sqrt(b)/3, 1.); ival in=acival(b, s); while(true){ while(in.n1<=n && n<=in.n2){ s+=d; in=acival(b, s); } if(d=0){ px=exp(nx*log(b)-b-gsl_sf_lnfact(nx)); dx=px*ulimit(b, nx); } else{ px=0; dx=0; } { py=exp(ny*log(b)-b-gsl_sf_lnfact(ny)); dy=py*ulimit(b, ny); } { double p=px+py; double d=dx+dy; r+=p; t+=d; if(1-r::iterator i=sim.begin(); i!=sim.end(); ++i){ int obs=mode==0?i->nch:getn(i->lne); if(start) start=false; else if(obsrw(ta, tg, xt, pt); s+=i->x; } int o=0; for(vector::iterator i=dat.begin(); i!=dat.end(); ++i){ int obs=mode==0?i->nch:getn(i->lne); if(obs>mnch) o++; } cerr<<"mrf("<<(mode==0?"nch":"lne")<<" = "<<(mode==0?mnch:gete(mnch))<<") = " <1){ mode=atoi(arg_a[1]); if(arg_c>2){ unsigned int seed=atoi(arg_a[2]); srand(1+seed); if(arg_c>3) ta=atof(arg_a[3]); if(arg_c>4) tg=atof(arg_a[4]); if(arg_c>5) xt=atof(arg_a[5]); if(arg_c>6) FR=atoi(arg_a[6]); #ifdef NEUTRINOFLUX if(arg_c>7){ nfx = new ConventionalNeutrinoFlux(arg_a[7]); fnfx=true; } if(arg_c>8){ pfx = new PromptNeutrinoFlux(arg_a[8]); fpfx=true; } if(arg_c>9) pt = atof(arg_a[9]); if(arg_c>10) var=atoi(arg_a[10]); #endif } load(); if(mode==5010){ u.rw(); } else if(mode>=5000 && mode<=5003){ m.getMRF(mode-5000); } else if(mode<0){ mode=-mode-1; u.all(); } else v.prof(); { #ifdef NEUTRINOFLUX if(arg_c>7) if(nfx) delete nfx; if(arg_c>8) if(pfx) delete pfx; #endif } } else cerr<<"Use: "<