#include #include #include #include #include #include #include #include #include #include #include #include "ppc.h" using namespace std; const int num=200; const double bin=25; const double qsat=2500; double rr=0, dR=10, dA=30, dI=0; const double cv=M_PI/180; void step(){ cerr<<"rr="<0){ double dls; do{ double f=nsim*ls-s-log(ld/ls)/s2; double df=1+(1/(ndat*ld)+1/(nsim*ls))/s2; dls=-f/(nsim*df); ls+=dls; ld=(sd-nsim*ls)/ndat; if(ls<0 || ld<0){ cerr<<"Error!"<tol*ls); llx=square(log(ld/ls))/(2*s2); } else llx=0; double lls=0; if(s>0) lls+=s*log((s/nsim)/ls); double lld=0; if(d>0) lld+=d*log((d/ndat)/ld); return llx+lls+lld; } } dat; map bad; map all; double nohit=0; struct xyz{ double x, y, z; xyz(){ x=0, y=0, z=0; } xyz(double x, double y, double z){ this->x=x, this->y=y, this->z=z; } void ini(){ { FILE * f = fopen("bad", "r"); if(f){ int str, dom; while(fscanf(f, "%d %d", &str, &dom)==2) bad[make_pair(str, dom)]++; fclose(f); } } { const double dt=0; double t0=NAN, tx=NAN; FILE * f = fopen("dat", "r"); if(f){ int str, dom; double t, q; while(fscanf(f, "%d %d %lf %lf", &str, &dom, &t, &q)==4){ key om=make_pair(str, dom); if(bad.find(om)==bad.end()){ q=round(q); if(q<=0) q=1; all[om].draw.push_back(make_pair(t, q)); if(isnan(t0) || ttx) tx=t; } } fclose(f); } cerr<<"T="<<(tx-t0)<<" "<<(num*bin)<<" "; for(map::iterator i=all.begin(); i!=all.end(); ++i) i->second.prep(t0-dt); } { map geo; for(vector::const_iterator i=xppc::i3oms.begin(); i!=xppc::i3oms.end(); ++i) geo.insert(make_pair(make_pair(i->str, i->dom), xyz(i->r[0], i->r[1], i->r[2]))); x=0, y=0, z=0; double qtot=0; for(map::iterator i=all.begin(); i!=all.end(); ++i){ xyz g = geo[i->first]; double q=i->second.dtot; x+=q*g.x; y+=q*g.y; z+=q*g.z; qtot+=q; } if(qtot>0) x/=qtot, y/=qtot, z/=qtot; cerr<<"COG="<th, ph=cv*this->ph; double st=sin(th), ct=cos(th); double sp=sin(ph), cp=cos(ph); n[0]=-st*cp, n[1]=-st*sp, n[2]=-ct; } void seta(){ th=acos(-n[2])/cv; ph=atan2(-n[1], -n[0])/cv; } void setp(){ { { int i0=0; double n0=n[0]; for(int i=1; i<3; i++) if(fabs(n[i])>fabs(n0)){ i0=i; n0=n[i]; } int i1=(i0+1)%3, i2=(i0+2)%3; double n1=n[i1], n2=n[i2], nq=n0*n0; double r1=1/sqrt(nq+n1*n1), r2=1/sqrt(nq+n2*n2); p1[i0]=-n1*r1; p1[i1]=n0*r1; p1[i2]=0; p2[i0]=-n2*r2; p2[i1]=0; p2[i2]=n0*r2; } { double q1[3], q2[3], r1=0, r2=0; for(int i=0; i<3; i++){ double a1=p1[i]-p2[i]; q1[i]=a1; r1+=a1*a1; double a2=p1[i]+p2[i]; q2[i]=a2; r2+=a2*a2; } r1=1/sqrt(r1); r2=1/sqrt(r2); for(int i=0; i<3; i++){ p1[i]=q1[i]*r1; p2[i]=q2[i]*r2; } } } } void setu(){ double zi=xrnd(); zi*=2*M_PI; double px=cos(zi), py=sin(zi); for(int i=0; i<3; i++) u[i]=px*p1[i]+py*p2[i]; } void rotate(double z){ double cs=cos(z), si=sin(z); { double r=0; for(int i=0; i<3; i++){ double a=n[i]*cs+u[i]*si; n[i]=a; r+=a*a; } { r=1/sqrt(r); for(int i=0; i<3; i++) n[i]*=r; } } } }; struct cascade : xyz, dir{ double e, t, s, a; cascade() : xyz(), dir(){ e=1.e5, t=0, s=1, a=1; } bool ini(){ xyz::ini(); bool flag=false; FILE * h = fopen("ini", "r"); if(h){ cascade c; if(fscanf(h, "%lf %lf %lf %lf %lf %lf %lf %lf %lf", &c.x, &c.y, &c.z, &c.th, &c.ph, &c.e, &c.t, &c.s, &c.a)==9){ c.setn(); * this = c; flag=true; cerr<<"Setting initial cascade parameters from file ini-f2k."< > *); double llh(); void refine(const multimap &, int, double); }; ostream& operator<<(ostream& o, cascade& c){ c.seta(); o<::iterator i=all.begin(); i!=all.end(); ++i) llh+=i->second.llh(dt, nsim), fnum+=1+i->second.fnum; { static double ndof=0; if(ndof==0){ ndof=fnum; cerr<<"Ndof="< > * sav = NULL){ map< key, vector > sim; for(map::const_iterator i=xppc::hvs.begin(); i!=xppc::hvs.end(); ++i){ float t=-1000; key om=make_pair(i->first.str, i->first.dom); while((t-=(1.e9/500)*log(xrnd()))<6000) sim[om].push_back(t); } { xppc::eini(); pair id=make_pair(0, 0); xppc::sett(n[0], n[1], n[2], id, 0); xppc::addp(x, y, z, t, e, 0); xppc::eout(); for(unsigned int i=0; i >::const_iterator i=sim.begin(); i!=sim.end(); ++i){ const key& om = i->first; const int lc=2; if(lc>0){ int sp; for(sp=-lc; sp<=lc; sp++) if(sp!=0) if(sim.find(make_pair(om.first, om.second+sp))!=sim.end()) break; if(sp>lc) continue; } if(bad.find(om)==bad.end()){ const vector& ts = i->second; map::iterator i=all.find(om); for(vector::const_iterator j=ts.begin(); j!=ts.end(); ++j){ if(sav==NULL){ double t=*j, q=1.; q=round(q); if(q<=0) q=1; if(i==all.end()) nohit+=q; else i->second.sraw.push_back(make_pair(t, q)); } else (*sav)[om][(int) floor(*j/bin)]++; } } } } double cascade::llh(){ ppc(); double llh; { bool verbose=false; const int num=2; double xi[num]={0, 0}, ei[num]={100, 1}; gsl_vector * x = gsl_vector_alloc(num); gsl_vector * u = gsl_vector_alloc(num); for(int i=0; ifval<<" "<x, i); cout<x, 0), nsim=exp(gsl_vector_get(s->x, 1)); t+=dt; e/=nsim; llh=fllh(dt, 1); if(verbose) cout<<"fval="<fval<<" "<::iterator i=all.begin(); i!=all.end(); ++i) i->second.clear(); } return llh; } void cascade::refine(const multimap & v, int top, double fn){ vector< V<7> > vs; int num=0; for(multimap::const_iterator j=v.begin(); j!=v.end() && numsecond; V<7> w; w[0]=n[0]*cj.p1[0]+n[1]*cj.p1[1]+n[2]*cj.p1[2]; w[1]=n[0]*cj.p2[0]+n[1]*cj.p2[1]+n[2]*cj.p2[2]; w[2]=cj.x-x, w[3]=cj.y-y, w[4]=cj.z-z; w[5]=1-(n[0]*cj.n[0]+n[1]*cj.n[1]+n[2]*cj.n[2]); w[6]=j->first; vs.push_back(w); } const bool verbose=false; const int dim=5; const int siz=(dim+1)*(dim+2)/2; double q[num][siz], f[num]; double par[dim][dim], val[dim]; for(int m=0; mzx) nx*=zx/nz, ny*=zx/nz, nz=zx; nz=sqrt(1-nz*nz); for(int i=0; i<3; i++) n[i]=nx*p1[i]+ny*p2[i]+nz*n[i]; } { double dx=p[2], dy=p[3], dz=p[4]; double dr=sqrt(dx*dx+dy*dy+dz*dz); if(dr>dR) dx*=dR/dr, dy*=dR/dr, dz*=dR/dr; x+=dx, y+=dy, z+=dz; } } } void loc_rs(cascade & ci){ // localized random search const int steps=40, tries=25, upto=steps/2; multimap bag; for(int k=0; k v; for(int i=0; isecond; int num=0; double rn=0, r2=0, r[3]={0}, n[3]={0}; for(multimap::const_iterator i=bag.begin(); numsecond; for(int i=0; i<3; i++) n[i]+=c.n[i]; r[0]+=c.x, r[1]+=c.y, r[2]+=c.z; r2+=c.x*c.x+c.y*c.y+c.z*c.z; rn+=c.x*c.n[0]+c.y*c.n[1]+c.z*c.n[2]; } { double rna=0, r2a=0, n2a=0; for(int i=0; i<3; i++){ r[i]/=num, n[i]/=num; rna+=r[i]*n[i], r2a+=r[i]*r[i], n2a+=n[i]*n[i]; } rn/=num; rr=(rna-rn)/(1-n2a); dR=sqrt(r2/num-r2a+2*rr*(rn-rna)+rr*rr*(1-n2a)); dA=1/(sqrt(sqrt(n2a)*(3-n2a)/(1-n2a))*cv); step(); } } void spsa(cascade & ci){ // simultaneous perturbation stochastic approximation const int steps=1000, start=1; const double dL0=40; for(int k=0; kcn) fr*=cn/fabs(fr); dr=fr*dR, da=fr*dA, di=fr*dI; ci.x-=dr*nx, ci.y-=dr*ny, ci.z-=dr*nz; ci.rotate(-cv*da); ci.s/=exp(di*is), ci.a/=exp(di*ia); llh=ci.llh(); cout<1?1:x; } } void spsawh(cascade & ci){ // simultaneous perturbation stochastic approximation with Hessian const int steps=1000, start=1; double llh=ci.llh(); for(int k=0; k0){ ci.x=fr*c2.x+(1-fr)*ci.x; ci.y=fr*c2.y+(1-fr)*ci.y; ci.z=fr*c2.z+(1-fr)*ci.z; for(int i=0; i<3; i++) ci.n[i]=fr*c2.n[i]+(1-fr)*ci.n[i]; ci.s=pow(c2.s, fr)*pow(ci.s, 1-fr); ci.a=pow(c2.a, fr)*pow(ci.a, 1-fr); } ci.normalize(); llh=ci.llh(); cout<25) half(), step(), n=0; } cout<=10){ cascade ci; ci.x=atof(arg_a[1]); ci.y=atof(arg_a[2]); ci.z=atof(arg_a[3]); ci.th=atof(arg_a[4]); ci.ph=atof(arg_a[5]); ci.e=atof(arg_a[6]); ci.t=atof(arg_a[7]); ci.s=atof(arg_a[8]); ci.a=atof(arg_a[9]); ci.setn(); int num=arg_c>10?atof(arg_a[10]):1; map< key, map > sav; for(int i=0; i >::iterator i=sav.begin(); i!=sav.end(); i++) for(map::iterator j=i->second.begin(); j!=i->second.end(); j++) cout<first.first<<" "<first.second<<" "<<(j->first+0.5)*bin<<" "<second<1?atoi(arg_a[1]):0); else method(ci, 0), method(ci, 1); } xppc::fin(); xppc::stop(); }