| [3756] | 1 | //  Classes to compute 2D | 
|---|
|  | 2 | // R. Ansari - Nov 2008, May 2010 | 
|---|
|  | 3 |  | 
|---|
|  | 4 | #include "mdish.h" | 
|---|
|  | 5 |  | 
|---|
|  | 6 |  | 
|---|
|  | 7 | //-------------------------------------------------- | 
|---|
|  | 8 | // -- Four2DResponse class | 
|---|
|  | 9 | //-------------------------------------------------- | 
|---|
|  | 10 | // Constructor | 
|---|
|  | 11 | Four2DResponse::Four2DResponse(int typ, double dx, double dy) | 
|---|
|  | 12 | : typ_(typ), dx_((dx>1.e-3)?dx:1.), dy_((dy>1.e-3)?dy:1.) | 
|---|
|  | 13 | { | 
|---|
|  | 14 | } | 
|---|
|  | 15 |  | 
|---|
|  | 16 | // Return the response for the wave vecteor (kx,ky) | 
|---|
|  | 17 | double Four2DResponse::Value(double kx, double ky) | 
|---|
|  | 18 | { | 
|---|
|  | 19 | double wk,wkx,wky; | 
|---|
|  | 20 | switch (typ_) | 
|---|
|  | 21 | { | 
|---|
|  | 22 | case 1:   // Reponse gaussienne parabole diametre D exp[ - 0.5 (lambda  k_g / D )^2 ] | 
|---|
|  | 23 | wk = sqrt(kx*kx+ky*ky)/dx_; | 
|---|
|  | 24 | wk = 0.5*wk*wk; | 
|---|
|  | 25 | return exp(-wk); | 
|---|
|  | 26 | break; | 
|---|
|  | 27 | case 2:   // Reponse parabole diametre D  Triangle <= kmax= 2 pi D / lambda | 
|---|
|  | 28 | wk = sqrt(kx*kx+ky*ky)/dx_/2./M_PI; | 
|---|
|  | 29 | return ( (wk<1.)?(1.-wk):0.); | 
|---|
|  | 30 | break; | 
|---|
|  | 31 | case 3:   // Reponse rectangle Dx x Dy  Triangle (|kx|,|k_y|) <= (2 pi Dx / lambda, 2 pi Dx / lambda) | 
|---|
|  | 32 | wkx = kx/2./M_PI/dx_; | 
|---|
|  | 33 | wky = ky/2./M_PI/dy_; | 
|---|
|  | 34 | return ( ((wkx<1.)&&(wky<1.))?((1.-wkx)*(1-wky)):0.); | 
|---|
|  | 35 | break; | 
|---|
|  | 36 | default: | 
|---|
|  | 37 | return 1.; | 
|---|
|  | 38 | } | 
|---|
|  | 39 | } | 
|---|
|  | 40 | // Return a vector representing the power spectrum (for checking) | 
|---|
|  | 41 | Histo2D Four2DResponse::GetResponse(int nx, int ny) | 
|---|
|  | 42 | { | 
|---|
|  | 43 | double kxmx = 1.2*DeuxPI*dx_; | 
|---|
|  | 44 | double kymx = 1.2*DeuxPI*dy_; | 
|---|
|  | 45 | if (typ_<3) kymx=kxmx; | 
|---|
|  | 46 | Histo2D h2(0.,kxmx,nx,0.,kymx,ny); | 
|---|
|  | 47 |  | 
|---|
|  | 48 | for(int j=0; j<h2.NBinY(); j++) | 
|---|
|  | 49 | for(int i=0; i<h2.NBinX(); i++) | 
|---|
|  | 50 | h2(i,j) = Value((i+0.5)*h2.WBinX(), (j+0.5)*h2.WBinY()); | 
|---|
|  | 51 | return h2; | 
|---|
|  | 52 | } | 
|---|
|  | 53 |  | 
|---|
|  | 54 | //--------------------------------------------------------------- | 
|---|
|  | 55 | // -- Four2DRespTable : Reponse tabulee instrumentale ds le plan k_x,k_y (angles theta,phi) | 
|---|
|  | 56 | //--------------------------------------------------------------- | 
|---|
|  | 57 | Four2DRespTable::Four2DRespTable(Histo2D const & hrep, double d) | 
|---|
|  | 58 | : Four2DResponse(0,d,d) , hrep_(hrep) | 
|---|
|  | 59 | { | 
|---|
|  | 60 | } | 
|---|
|  | 61 |  | 
|---|
|  | 62 | double Four2DRespTable::Value(double kx, double ky) | 
|---|
|  | 63 | { | 
|---|
|  | 64 | int_4 i,j; | 
|---|
|  | 65 | if ( (kx<=hrep_.XMin())||(kx>=hrep_.XMax()) || | 
|---|
|  | 66 | (ky<=hrep_.YMin())||(ky>=hrep_.YMax()) )  return 0.; | 
|---|
|  | 67 | hrep_.FindBin(kx, ky, i, j); | 
|---|
|  | 68 | return hrep_(i, j); | 
|---|
|  | 69 | } | 
|---|
|  | 70 |  | 
|---|
|  | 71 | //--- Classe simple pour le calcul de rotation | 
|---|
|  | 72 | class Rotation { | 
|---|
|  | 73 | public: | 
|---|
|  | 74 | Rotation(double tet, double phi) | 
|---|
|  | 75 | { | 
|---|
|  | 76 | // (Teta,Phi) = Direction de visee | 
|---|
|  | 77 | // Les angles d'Euler correspondants sont Teta, Phi+Pi/2 | 
|---|
|  | 78 | // Le Pi/2 vient que les rotations d'euler se font dans l'ordre | 
|---|
|  | 79 | //  Autour de oZ d'angle Phi, autour de oN (nouvel axe X) d'angle Teta | 
|---|
|  | 80 | //  Autour du nouvel axe Z (x3) d'angle Psi  (Psi=0 -> cp=1, sp=0.; | 
|---|
|  | 81 | double ct = cos(tet); | 
|---|
|  | 82 | double st = sin(tet); | 
|---|
|  | 83 | // Le Pi/2 echange les axes X<>Y pour theta=phi=0 ! | 
|---|
|  | 84 | //  double cf = cos(phi+M_PI/2); | 
|---|
|  | 85 | //  double sf = sin(phi+M_PI/2); | 
|---|
|  | 86 | double cf = cos(phi); | 
|---|
|  | 87 | double sf = sin(phi); | 
|---|
|  | 88 | double cp = 1.; // cos((double)pO); | 
|---|
|  | 89 | double sp = 0.; // sin((double)pO); | 
|---|
|  | 90 | RE[0][0] = cf*cp-sf*ct*sp;     RE[0][1] = sf*cp+cf*ct*sp;      RE[0][2] = st*sp; | 
|---|
|  | 91 | RE[1][0] = -cf*sp-sf*ct*cp;    RE[1][1] = -sf*sp+cf*ct*cp;     RE[1][2] = st*cp; | 
|---|
|  | 92 | RE[2][0] = sf*st;              RE[2][1] = -cf*st;              RE[2][2] = ct; | 
|---|
|  | 93 | } | 
|---|
|  | 94 | inline void Do(double& x, double& y) | 
|---|
|  | 95 | { | 
|---|
|  | 96 | double xx=x; | 
|---|
|  | 97 | double yy=y; | 
|---|
|  | 98 | x = RE[0][0]*xx+RE[0][1]*yy; | 
|---|
|  | 99 | y = RE[1][0]*xx+RE[1][1]*yy; | 
|---|
|  | 100 | } | 
|---|
|  | 101 | double RE[3][3]; | 
|---|
|  | 102 | }; | 
|---|
|  | 103 |  | 
|---|
|  | 104 | // ----------------------------------- | 
|---|
|  | 105 | // -- Classe ressemblant a un histo 2D | 
|---|
|  | 106 | // ----------------------------------- | 
|---|
|  | 107 | QHis2D::QHis2D() | 
|---|
|  | 108 | : nx(0),ny(0),xmin(0),xmax(0),ymin(0),ymax(0),sumw0(0.) | 
|---|
|  | 109 | { | 
|---|
|  | 110 | } | 
|---|
|  | 111 | QHis2D::QHis2D(r_8 xMin,r_8 xMax,int_4 nxb,r_8 yMin,r_8 yMax,int_4 nyb) | 
|---|
|  | 112 | : nx(0),ny(0),xmin(0),xmax(0),ymin(0),ymax(0),sumw0(0.) | 
|---|
|  | 113 | { | 
|---|
|  | 114 | Define(xMin, xMax, nxb, yMin, yMax, nyb); | 
|---|
|  | 115 | } | 
|---|
|  | 116 | void QHis2D::Define(r_8 xMin,r_8 xMax,int_4 nxb,r_8 yMin,r_8 yMax,int_4 nyb) | 
|---|
|  | 117 | { | 
|---|
|  | 118 | nx=nxb; ny=nyb; | 
|---|
|  | 119 | xmin=xMin;  xmax=xMax; | 
|---|
|  | 120 | ymin=yMin;  ymax=yMax; | 
|---|
|  | 121 | dxb=(xmax-xmin)/(double)nx; | 
|---|
|  | 122 | dyb=(ymax-ymin)/(double)ny; | 
|---|
|  | 123 | sa_size_t sz[5];  sz[0]=nx;  sz[1]=ny; | 
|---|
|  | 124 | aw.ReSize(2,sz); | 
|---|
|  | 125 | sumw0=0.; | 
|---|
|  | 126 | return; | 
|---|
|  | 127 | } | 
|---|
|  | 128 | double QHis2D::Add(r_8 x, r_8 y, r_8 w, bool fgfh) | 
|---|
|  | 129 | { | 
|---|
|  | 130 | sa_size_t ix = (sa_size_t)((x-xmin)/dxb); | 
|---|
|  | 131 | sa_size_t jy = (sa_size_t)((y-ymin)/dyb); | 
|---|
|  | 132 | if ((ix<0)||(ix>=nx)||(jy<0)||(jy>=ny))  return 0.; | 
|---|
|  | 133 | double rw = ((ix==0)&&(jy==0)) ? w : 0.; | 
|---|
|  | 134 | sumw0 += rw; | 
|---|
|  | 135 | if (fgfh) aw(ix,jy) += w; | 
|---|
|  | 136 | return rw; | 
|---|
|  | 137 | } | 
|---|
|  | 138 | Histo2D QHis2D::Convert() | 
|---|
|  | 139 | { | 
|---|
|  | 140 | Histo2D h2(xmin,xmax,nx,ymin,ymax,ny); | 
|---|
|  | 141 | for(int_4 j=0; j<ny; j++) | 
|---|
|  | 142 | for(int_4 i=0; i<nx; i++)  h2(i,j) = aw(i,j); | 
|---|
|  | 143 | return h2; | 
|---|
|  | 144 | } | 
|---|
|  | 145 |  | 
|---|
|  | 146 | //---------------------------------------------------------------------- | 
|---|
|  | 147 | //  -- Pour calculer la reponse ds le plan kx,ky d'un system MultiDish | 
|---|
|  | 148 | //---------------------------------------------------------------------- | 
|---|
|  | 149 | MultiDish::MultiDish(double lambda, double dmax, vector<Dish>& dishes, bool fgnoauto) | 
|---|
|  | 150 | : lambda_(lambda), dmax_(dmax), dishes_(dishes), fgnoauto_(fgnoauto) | 
|---|
|  | 151 | { | 
|---|
|  | 152 | SetThetaPhiRange(); | 
|---|
|  | 153 | SetRespHisNBins(); | 
|---|
|  | 154 | mcnt_=0; | 
|---|
|  | 155 | } | 
|---|
|  | 156 |  | 
|---|
|  | 157 | Histo2D MultiDish::GetResponse() | 
|---|
|  | 158 | { | 
|---|
|  | 159 | cout << " MultiDish::GetResponse() - NDishes=" << dishes_.size() << " nx=" << nx_ << " ny=" << ny_ << endl; | 
|---|
|  | 160 | double kmx = 1.2*DeuxPI*dmax_/lambda_; | 
|---|
|  | 161 | /* | 
|---|
|  | 162 | h2w_= Histo2D(0.,kmx,nx_,0.,kmx,ny_); | 
|---|
|  | 163 | h2cnt_= Histo2D(0.,kmx,nx_,0.,kmx,ny_); | 
|---|
|  | 164 | h2w_.Zero(); | 
|---|
|  | 165 | h2cnt_.Zero(); | 
|---|
|  | 166 | */ | 
|---|
|  | 167 | h2w_.Define(0.,kmx,nx_,0.,kmx,ny_); | 
|---|
|  | 168 |  | 
|---|
|  | 169 | double dold = dishes_[0].D/lambda_; | 
|---|
|  | 170 | double dolx = dishes_[0].Dx/lambda_; | 
|---|
|  | 171 | double doly = dishes_[0].Dy/lambda_; | 
|---|
|  | 172 |  | 
|---|
|  | 173 | Four2DResponse rd(2, dold, dold); | 
|---|
|  | 174 | Four2DResponse rdr(3, dolx, doly); | 
|---|
|  | 175 |  | 
|---|
|  | 176 | if (!dishes_[0].isCircular())  rd = rdr; | 
|---|
|  | 177 |  | 
|---|
|  | 178 | double dtet = thetamax_/(double)ntet_; | 
|---|
|  | 179 | double dphi = phimax_/(double)ntet_; | 
|---|
|  | 180 |  | 
|---|
|  | 181 | double sumw = 0.; | 
|---|
|  | 182 | for(int kt=0; kt<ntet_; kt++) | 
|---|
|  | 183 | for(int jp=0; jp<nphi_; jp++) | 
|---|
|  | 184 | sumw += CumulResp(rd, (double)kt*dtet, (double)jp*dphi); | 
|---|
|  | 185 |  | 
|---|
|  | 186 | double kx0 = DeuxPI*fabs(dishes_[1].X-dishes_[0].X)/lambda_; | 
|---|
|  | 187 | double ky0 = DeuxPI*fabs(dishes_[1].Y-dishes_[0].Y)/lambda_; | 
|---|
|  | 188 | int_4 ib, jb; | 
|---|
|  | 189 | //  h2w_ /= h2cnt_; | 
|---|
|  | 190 | Histo2D h2 = h2w_.Convert(); | 
|---|
|  | 191 | h2.FindBin(kx0, ky0, ib, jb); | 
|---|
|  | 192 | cout << " ---- MultiDish::GetResponse() VMin=" << h2.VMin() << " VMax= " << h2.VMax() | 
|---|
|  | 193 | << " h(0,0)=" << h2(0,0) << " h(" << ib <<"," << jb << ")=" << h2(ib,jb) <<endl; | 
|---|
|  | 194 | //  double fnorm=sqrt((double)dishes_.size())/h2.VMax(); | 
|---|
|  | 195 | double fnorm=1.; | 
|---|
|  | 196 | if (h2.VMax() > sumw) { | 
|---|
|  | 197 | fnorm=(double)dishes_.size()/h2.VMax(); | 
|---|
|  | 198 | cout << " ---- MultiDish::GetResponse() NDishes=" << dishes_.size() << " sumw=" << sumw | 
|---|
|  | 199 | << " Renormalizing x NDishes/sumw  " << fnorm << endl; | 
|---|
|  | 200 | } | 
|---|
|  | 201 | else { | 
|---|
|  | 202 | fnorm=(double)dishes_.size()/h2.VMax(); | 
|---|
|  | 203 | cout << " ---- MultiDish::GetResponse() NDishes=" << dishes_.size() << " VMax=" << h2.VMax() | 
|---|
|  | 204 | << " Renormalizing x NDishes/h2.VMax()   " << fnorm << endl; | 
|---|
|  | 205 | } | 
|---|
|  | 206 | h2 *= fnorm; | 
|---|
|  | 207 | cout << " ---- MultiDish::GetResponse() APRES VMin=" << h2.VMin() << " VMax= " << h2.VMax() << " h(0,0)=" | 
|---|
|  | 208 | << h2(0,0) << endl; | 
|---|
|  | 209 | return h2; | 
|---|
|  | 210 | } | 
|---|
|  | 211 |  | 
|---|
|  | 212 | /* | 
|---|
|  | 213 | double MultiDish::AddToHisto(double kx0, double ky0, double x, double y, double w, bool fgfh) | 
|---|
|  | 214 | { | 
|---|
|  | 215 | double xxp = kx0+x; | 
|---|
|  | 216 | double yyp = ky0+y; | 
|---|
|  | 217 | double sumw=0.; | 
|---|
|  | 218 | int_4 ib, jb; | 
|---|
|  | 219 | h2w_.FindBin(xxp, yyp, ib, jb); | 
|---|
|  | 220 | if ((ib==0)&&(jb==0))  sumw+=w; | 
|---|
|  | 221 | if (fgfh) { | 
|---|
|  | 222 | h2w_.Add(xxp, yyp, w); | 
|---|
|  | 223 | h2cnt_.Add(xxp, yyp, 1.); | 
|---|
|  | 224 | } | 
|---|
|  | 225 | double xxm=kx0-x; | 
|---|
|  | 226 | double yym=ky0-y; | 
|---|
|  | 227 | if (xxm>0.)  { | 
|---|
|  | 228 | h2w_.FindBin(xxm, yyp, ib, jb); | 
|---|
|  | 229 | if ((ib==0)&&(jb==0))  sumw+=w; | 
|---|
|  | 230 | if (fgfh) { | 
|---|
|  | 231 | h2w_.Add(xxm, yyp, w); | 
|---|
|  | 232 | h2cnt_.Add(xxm, yyp, 1.); | 
|---|
|  | 233 | } | 
|---|
|  | 234 | if (yym>0.) { | 
|---|
|  | 235 | h2w_.FindBin(xxm, yym, ib, jb); | 
|---|
|  | 236 | if ((ib==0)&&(jb==0))  sumw+=w; | 
|---|
|  | 237 | if (fgfh) { | 
|---|
|  | 238 | h2w_.Add(xxm, yym, w); | 
|---|
|  | 239 | h2cnt_.Add(xxm, yym, 1.); | 
|---|
|  | 240 | } | 
|---|
|  | 241 | } | 
|---|
|  | 242 | } | 
|---|
|  | 243 | if (yym>0.)  { | 
|---|
|  | 244 | h2w_.FindBin(xxp, yym, ib, jb); | 
|---|
|  | 245 | if ((ib==0)&&(jb==0))  sumw+=w; | 
|---|
|  | 246 | if (fgfh) { | 
|---|
|  | 247 | h2w_.Add(xxp, yym, w); | 
|---|
|  | 248 | h2cnt_.Add(xxp, yym, 1.); | 
|---|
|  | 249 | } | 
|---|
|  | 250 | } | 
|---|
|  | 251 | return sumw; | 
|---|
|  | 252 | } | 
|---|
|  | 253 | */ | 
|---|
|  | 254 |  | 
|---|
|  | 255 | double MultiDish::AddToHisto(double kx0, double ky0, double x, double y, double w, bool fgfh) | 
|---|
|  | 256 | { | 
|---|
|  | 257 | double xxp = kx0+x; | 
|---|
|  | 258 | double yyp = ky0+y; | 
|---|
|  | 259 | double sumw=0.; | 
|---|
|  | 260 | sumw += h2w_.Add(xxp, yyp, w, fgfh); | 
|---|
|  | 261 | double xxm=kx0-x; | 
|---|
|  | 262 | double yym=ky0-y; | 
|---|
|  | 263 | if (xxm>0.)  { | 
|---|
|  | 264 | sumw += h2w_.Add(xxm, yyp, w, fgfh); | 
|---|
|  | 265 | if (yym>0.)  sumw += h2w_.Add(xxm, yym, w, fgfh); | 
|---|
|  | 266 | } | 
|---|
|  | 267 | if (yym>0.)  sumw += h2w_.Add(xxp, yym, w, fgfh); | 
|---|
|  | 268 | return sumw; | 
|---|
|  | 269 | } | 
|---|
|  | 270 |  | 
|---|
|  | 271 | double MultiDish::CumulResp(Four2DResponse& rd, double theta, double phi) | 
|---|
|  | 272 | { | 
|---|
|  | 273 | //  cout << " MultiDish::CumulResp()  theta=" << theta << " phi=" << phi << endl; | 
|---|
|  | 274 |  | 
|---|
|  | 275 | double dx = h2w_.WBinX()/5; | 
|---|
|  | 276 | double dy = h2w_.WBinY()/5; | 
|---|
|  | 277 | int nbx = DeuxPI*rd.Dx()/dx; | 
|---|
|  | 278 | int nby = DeuxPI*rd.Dy()/dy; | 
|---|
|  | 279 | dx = DeuxPI*rd.Dx()/(double)nbx; | 
|---|
|  | 280 | dy = DeuxPI*rd.Dy()/(double)nby; | 
|---|
|  | 281 | if (mcnt_==0) | 
|---|
|  | 282 | cout << " CumulResp() nbx=" << nbx << " nby=" << nby << " dx=" << dx << " dy=" << dy << endl; | 
|---|
|  | 283 | mcnt_++; | 
|---|
|  | 284 |  | 
|---|
|  | 285 | double sumw = 0.; | 
|---|
|  | 286 | Rotation rot(theta, phi); | 
|---|
|  | 287 |  | 
|---|
|  | 288 | for(size_t i=0; i<dishes_.size(); i++) { | 
|---|
|  | 289 | for(size_t j=i; j<dishes_.size(); j++) { | 
|---|
|  | 290 | double kx0 = DeuxPI*fabs(dishes_[i].X-dishes_[j].X)/lambda_; | 
|---|
|  | 291 | double ky0 = DeuxPI*fabs(dishes_[i].Y-dishes_[j].Y)/lambda_; | 
|---|
|  | 292 | rot.Do(kx0, ky0); | 
|---|
|  | 293 | if (kx0<0) kx0=-kx0; | 
|---|
|  | 294 | if (ky0<0) ky0=-ky0; | 
|---|
|  | 295 | bool fgfh= (!fgnoauto_ || (dishes_[i].ReflectorId()!=dishes_[j].ReflectorId())); | 
|---|
|  | 296 | for(int ix=0; ix<nbx; ix++) | 
|---|
|  | 297 | for(int jy=0; jy<nby; jy++) { | 
|---|
|  | 298 | double x = ix*dx; | 
|---|
|  | 299 | double y = jy*dy; | 
|---|
|  | 300 | sumw += AddToHisto(kx0, ky0, x, y, rd(x,y), fgfh); | 
|---|
|  | 301 | } | 
|---|
|  | 302 | } | 
|---|
|  | 303 | //    if (i%10==0) | 
|---|
|  | 304 | //      cout << " MultiDish::CumulResp() done i=" << i << " / imax=" << dishes_.size() | 
|---|
|  | 305 | //     << " theta=" << theta << " phi=" << phi << endl; | 
|---|
|  | 306 | } | 
|---|
|  | 307 | return sumw; | 
|---|
|  | 308 | } | 
|---|
|  | 309 |  | 
|---|