#include #include #include #include #include #include #include #include #include using namespace std; const int num=200; const double bin=25; const double qsat=2500; double rr=0, dR=10, dA=30; const double cv=M_PI/180; void step(int i){ cerr<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 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(){ { 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){ q=round(q); if(q<=0) q=1; all[make_pair(str, dom)].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; FILE * f = fopen("../ice/geo-f2k", "r"); if(f){ int str, dom; double x, y, z; char mbid[32], omid[32]; const double zoff=1948.07; while(fscanf(f, "%31s %31s %lf %lf %lf %d %d", mbid, omid, &x, &y, &z, &str, &dom)==7) geo.insert(make_pair(make_pair(str, dom), xyz(x, y, z+zoff))); fclose(f); } else{ cerr<<"Cannot read geometry file!"<::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; } x/=qtot; y/=qtot; z/=qtot; cerr<<"COG="<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; } } for(int i=0; i<3; i++) u[i]=px*p1[i]+py*p2[i]; } { 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; cascade() : xyz(), dir(){ e=1.e5, t=0; } double gaus(){ static bool flag=false; static double r1, r2; flag=!flag; if(flag){ double ra=sqrt(-2*log(xrnd())), rb=2*M_PI*xrnd(); r1=ra*cos(rb); r2=ra*sin(rb); return r1; } else{ flag=false; return r2; } } void move(){ double ka=square(dA*cv); th*=cv, ph*=cv; 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; x+=rr*n[0], y+=rr*n[1], z+=rr*n[2]; x+=dR*gaus(), y+=dR*gaus(), z+=dR*gaus(); double xi; do{ xi=1+ka*log(xrnd()); } while (xi<-1); double si=sqrt(1-xi*xi); rotate(xi, si); th=acos(n[2])/cv; ph=atan2(n[1], n[0])/cv; x-=rr*n[0], y-=rr*n[1], z-=rr*n[2]; } }; ostream& operator<<(ostream& o, const cascade& c){ o<::iterator i=all.begin(); i!=all.end(); ++i) llh+=i->second.llh(dt, nsim); return llh; } double llh(cascade & c, bool flag){ if(flag) c.move(); { ostringstream o; o<<"./ppc "< p=make_pair(str, dom); map::iterator i=all.find(p); if(i==all.end()) nohit+=q; else i->second.sraw.push_back(make_pair(t, q)); } pclose(f); } } double llh; { bool verbose=false; const int num=2; double xi[num]={0, 0}, ei[num]={1, 1}; gsl_vector * x = gsl_vector_alloc(num); gsl_vector * e = gsl_vector_alloc(num); for(int i=0; ifval<<" "<x, i); cout<x, 0)), c.e/=exp(atan(gsl_vector_get(s->x, 1))); llh=s->fval; if(verbose) cout<<"fval="<::iterator i=all.begin(); i!=all.end(); ++i) i->second.clear(); cout<<"|\t"< bag; for(int k=0; ksecond; 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(-1); } } { const int steps=1000, upto=steps/2; for(int k=0, n=0; k25) half(), step(k), n=0; } cout<