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