source: Sophya/trunk/Cosmo/RadioBeam/mdish.cc@ 4030

Last change on this file since 4030 was 4030, checked in by ansari, 14 years ago

Corrections papiers avec les commentaires du referee (1) - Reza 26/10/2011

File size: 14.1 KB
RevLine 
[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]11Four2DResponse::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)
19double 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 {
[3974]26 case 1: // Reponse gaussienne parabole diametre D exp[ -(1/8) (lambda k_g / D )^2 ]
[3756]27 wk = sqrt(kx*kx+ky*ky)/dx_;
[3974]28 wk = wk*wk/8.;
[3756]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}
[4030]50// Return a 2 D histrogram as the response(kx,ky)
[3756]51Histo2D 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;
[3930]56 Histo2D h2(-kxmx,kxmx,nx,-kymx,kymx,ny);
[3756]57
[3930]58 double xbc,ybc;
59 for(int_4 j=0; j<h2.NBinY(); j++)
60 for(int_4 i=0; i<h2.NBinX(); i++) {
61 h2.BinCenter(i,j,xbc,ybc);
62 h2(i,j) = Value(xbc,ybc);
63 }
[3756]64 return h2;
65}
66
[4030]67// Return a 2 D histrogram as the response(u=kx/2 pi, v=ky/2 pi)
68Histo2D Four2DResponse::GetResponseUV(int nx, int ny)
69{
70 double kxmx = 1.2*dx_;
71 double kymx = 1.2*dy_;
72 if (typ_<3) kymx=kxmx;
73 Histo2D h2(-kxmx,kxmx,nx,-kymx,kymx,ny);
74
75 double xbc,ybc;
76 for(int_4 j=0; j<h2.NBinY(); j++)
77 for(int_4 i=0; i<h2.NBinX(); i++) {
78 h2.BinCenter(i,j,xbc,ybc);
79 h2(i,j) = Value(xbc*DeuxPI,ybc*DeuxPI);
80 }
81 return h2;
82}
83
[3947]84HProf Four2DResponse::GetProjNoiseLevel(int nbin, bool fgnorm1)
85{
86 Histo2D h2w = GetResponse(2*nbin, 2*nbin);
87 r_8 vmin=h2w.VMin();
88 r_8 vmax=h2w.VMax();
89 double seuil=vmax/10000.;
90 if (seuil<1.e-6) seuil=1.e-6;
91 r_8 facnorm=((fgnorm1)?vmax:1.);
92 cout << " Four2DResponse::GetProjNoiseLevel Min,Max=" << vmin << " , " << vmax
93 << " facnorm=" << facnorm << " seuil=" << seuil << endl;
94 double kmax=2.*M_PI*D();
95 HProf hp(0,kmax,nbin);
96 double x,y;
97 for(sa_size_t j=0; j<h2w.NBinY(); j++) {
98 for(sa_size_t i=0; i<h2w.NBinX(); i++) {
99 h2w.BinCenter(i,j,x,y);
100 double yw=h2w(i,j);
101 if (yw<seuil) continue;
102 hp.Add(sqrt(x*x+y*y),facnorm/yw);
103 }
104 }
105 return hp;
106}
107
108HProf Four2DResponse::GetProjResponse(int nbin, bool fgnorm1)
109{
110 Histo2D h2w = GetResponse(2*nbin, 2*nbin);
111 r_8 vmin=h2w.VMin();
112 r_8 vmax=h2w.VMax();
113 r_8 facnorm=((fgnorm1)?(1./vmax):1.);
114 cout << " Four2DResponse::GetProjResponse Min,Max=" << vmin << " , " << vmax
115 << " facnorm=" << facnorm << endl;
116 double kmax=2.*M_PI*D();
117 HProf hp(0,kmax,nbin);
118 double x,y;
119 for(sa_size_t j=0; j<h2w.NBinY(); j++) {
120 for(sa_size_t i=0; i<h2w.NBinX(); i++) {
121 h2w.BinCenter(i,j,x,y);
122 hp.Add(sqrt(x*x+y*y),h2w(i,j)*facnorm);
123 }
124 }
125 return hp;
126}
127
[3756]128//---------------------------------------------------------------
129// -- Four2DRespTable : Reponse tabulee instrumentale ds le plan k_x,k_y (angles theta,phi)
130//---------------------------------------------------------------
[3792]131Four2DRespTable::Four2DRespTable()
132 : Four2DResponse(0,1.,1.)
[3756]133{
134}
135
[3792]136Four2DRespTable::Four2DRespTable(Histo2D const & hrep, double d, double lambda)
137 : Four2DResponse(0,d,d,lambda) , hrep_(hrep)
138{
139}
140
[3756]141double Four2DRespTable::Value(double kx, double ky)
142{
[3787]143 kx *= lambda_ratio_;
144 ky *= lambda_ratio_;
[3756]145 int_4 i,j;
146 if ( (kx<=hrep_.XMin())||(kx>=hrep_.XMax()) ||
147 (ky<=hrep_.YMin())||(ky>=hrep_.YMax()) ) return 0.;
148 hrep_.FindBin(kx, ky, i, j);
149 return hrep_(i, j);
150}
151
[3796]152double Four2DRespTable::renormalize(double max)
153{
154 double cmx = hrep_.VMax();
155 hrep_ *= (max/cmx);
156 return cmx;
157}
158
[3792]159void Four2DRespTable::writeToPPF(string flnm)
160{
161 DVList dvinfo;
162 dvinfo["DoL"] = dx_;
163 dvinfo["LambdaRef"] = lambdaref_;
164 dvinfo["Lambda"] = lambda_;
165 POutPersist po(flnm);
166 po << hrep_;
167 po << dvinfo;
168}
169
170void Four2DRespTable::readFromPPF(string flnm)
171{
172 PInPersist pin(flnm);
173 DVList dvinfo;
174 pin >> hrep_;
175 pin >> dvinfo;
176 dx_ = dy_ = dvinfo["DoL"];
177 setLambdaRef((double)dvinfo["LambdaRef"]);
178 setLambda((double)dvinfo["Lambda"]);
179}
180
181
182
[3788]183//---------------------------------------------------------------
184// -- Four2DRespRatio : rapport de la reponse entre deux objets Four2DResponse
185//---------------------------------------------------------------
[3986]186Four2DRespRatio::Four2DRespRatio(Four2DResponse& a, Four2DResponse& b, double maxratio)
[3991]187 : Four2DResponse(0, a.D(), a.D()), a_(a), b_(b), maxratio_(maxratio), zerothr_(.5/maxratio)
[3788]188{
189}
190
191double Four2DRespRatio::Value(double kx, double ky)
192{
193 double ra = a_.Value(kx,ky);
194 double rb = b_.Value(kx,ky);
[3991]195 if ((ra<zerothr_)||(rb<zerothr_)) return 0.;
196 double rval=ra/rb;
[3986]197 if (rval<maxratio_) return rval;
198 return maxratio_;
[3788]199}
200
201//---------------------------------------------------------------
[3756]202//--- Classe simple pour le calcul de rotation
203class Rotation {
204public:
205 Rotation(double tet, double phi)
206 {
207// (Teta,Phi) = Direction de visee
208// Les angles d'Euler correspondants sont Teta, Phi+Pi/2
209// Le Pi/2 vient que les rotations d'euler se font dans l'ordre
210// Autour de oZ d'angle Phi, autour de oN (nouvel axe X) d'angle Teta
211// Autour du nouvel axe Z (x3) d'angle Psi (Psi=0 -> cp=1, sp=0.;
212 double ct = cos(tet);
213 double st = sin(tet);
214 // Le Pi/2 echange les axes X<>Y pour theta=phi=0 !
215 // double cf = cos(phi+M_PI/2);
216 // double sf = sin(phi+M_PI/2);
217 double cf = cos(phi);
218 double sf = sin(phi);
219 double cp = 1.; // cos((double)pO);
220 double sp = 0.; // sin((double)pO);
221 RE[0][0] = cf*cp-sf*ct*sp; RE[0][1] = sf*cp+cf*ct*sp; RE[0][2] = st*sp;
222 RE[1][0] = -cf*sp-sf*ct*cp; RE[1][1] = -sf*sp+cf*ct*cp; RE[1][2] = st*cp;
223 RE[2][0] = sf*st; RE[2][1] = -cf*st; RE[2][2] = ct;
224 }
225 inline void Do(double& x, double& y)
226 {
227 double xx=x;
228 double yy=y;
229 x = RE[0][0]*xx+RE[0][1]*yy;
230 y = RE[1][0]*xx+RE[1][1]*yy;
231 }
232 double RE[3][3];
233};
234
235
236//----------------------------------------------------------------------
237// -- Pour calculer la reponse ds le plan kx,ky d'un system MultiDish
238//----------------------------------------------------------------------
239MultiDish::MultiDish(double lambda, double dmax, vector<Dish>& dishes, bool fgnoauto)
240 : lambda_(lambda), dmax_(dmax), dishes_(dishes), fgnoauto_(fgnoauto)
241{
242 SetThetaPhiRange();
243 SetRespHisNBins();
[3933]244 SetBeamNSamples();
[3932]245 SetPrtLevel();
[3947]246 fgcomputedone_=false;
[3756]247 mcnt_=0;
248}
249
[3947]250void MultiDish::ComputeResponse()
[3756]251{
[3947]252 cout << " MultiDish::ComputeResponse() - NDishes=" << dishes_.size() << " nx=" << nx_ << " ny=" << ny_ << endl;
[3756]253 double kmx = 1.2*DeuxPI*dmax_/lambda_;
[3769]254 double dkmx = kmx/(double)nx_;
255 double dkmy = kmx/(double)ny_;
256 double kmxx = ((double)nx_+0.5)*dkmx;
257 double kmxy = ((double)ny_+0.5)*dkmy;
258 h2w_.Define(-kmxx,kmxx,2*nx_+1,-kmxy,kmxy,2*ny_+1);
259 h2w_.SetZeroBin(0.,0.);
[3947]260 kmax_=kmx;
[3756]261
[3933]262 double dold = dishes_[0].Diameter()/lambda_;
263 double dolx = dishes_[0].DiameterX()/lambda_;
264 double doly = dishes_[0].DiameterY()/lambda_;
[3756]265
266 Four2DResponse rd(2, dold, dold);
267 Four2DResponse rdr(3, dolx, doly);
268
269 if (!dishes_[0].isCircular()) rd = rdr;
270
271 double dtet = thetamax_/(double)ntet_;
[3932]272 double dphi = phimax_/(double)nphi_;
[3947]273 cout << " MultiDish::ComputeResponse() - ThetaMax=" << thetamax_ << " NTheta=" << ntet_
[3932]274 << " PhiMax=" << phimax_ << " NPhi=" << nphi_ << endl;
[3756]275
276 double sumw = 0.;
[3932]277 for(int kt=0; kt<ntet_; kt++) {
278 double theta=(double)kt*dtet;
[3931]279 for(int jp=0; jp<nphi_; jp++) {
[3932]280 double phi=(double)jp*dphi;
281 sumw += CumulResp(rd, theta, phi);
282 if (theta<1.e-9) continue;
283 sumw += CumulResp(rd, theta, -phi);
284 sumw += CumulResp(rd, theta, phi+M_PI);
285 sumw += CumulResp(rd, theta, -(phi+M_PI));
[3931]286 }
[3947]287 if (prtlev_>0)
288 cout << " MultiDish::ComputeResponse() done ktheta=" << kt << " / MaxNTheta= "
289 << ntet_ << endl;
[3932]290 }
[3947]291 r_8 rmin,rmax;
292 h2w_.GetMinMax(rmin,rmax);
293 cout << " MultiDish::ComputeResponse() finished : Rep_min,max=" << rmin << "," << rmax << " sumW0="
294 << sumw << " ?=" << h2w_.SumWBinZero() << endl;
295 fgcomputedone_=true;
296 return;
297}
298
299Histo2D MultiDish::GetResponse()
300{
301 if (!fgcomputedone_) ComputeResponse();
302
[3769]303 double kx1 = DeuxPI*(dishes_[0].DiameterX())/lambda_;
304 double ky1 = DeuxPI*(dishes_[0].DiameterY())/lambda_;
305 int_4 ib,jb;
[3756]306 // h2w_ /= h2cnt_;
307 Histo2D h2 = h2w_.Convert();
[3769]308 h2.FindBin(kx1, ky1, ib, jb);
309 if ((kx1<0)||(ky1<0)||(kx1>=h2.NBinX())||(ky1>=h2.NBinY())) {
310 cout << " MultiDish::GetResponse[1]/ERROR kx1,ky1=" << kx1 <<","<< ky1 << " --> ib,jb=" << ib <<","<< jb << endl;
311 ib=jb=0;
312 }
[3947]313 double sumw=h2w_.SumWBinZero();
[3769]314 double vmax=h2.VMax();
315 cout << " MultiDish::GetResponse[1] VMin=" << h2.VMin() << " VMax= " << vmax
316 << " h(0,0)=" << h2(0,0) << " kx1,ky1->h(" << ib <<"," << jb << ")=" << h2(ib,jb) <<endl;
[3756]317 // double fnorm=sqrt((double)dishes_.size())/h2.VMax();
318 double fnorm=1.;
[3769]319 if (vmax > sumw) {
[3756]320 fnorm=(double)dishes_.size()/h2.VMax();
[3769]321 cout << " MultiDish::GetResponse[2]/Warning h2.VMax()=" << vmax << " > sumw=" << sumw << endl;
322 cout << " ... NDishes=" << dishes_.size() << " sumw=" << sumw
323 << " Renormalizing x NDishes/VMax " << fnorm << endl;
[3756]324 }
325 else {
[3769]326 fnorm=(double)dishes_.size()/sumw;
327 cout << " MultiDish::GetResponse[3] NDishes=" << dishes_.size() << " sumw=" << sumw
328 << " Renormalizing x NDishes/sumw " << fnorm << endl;
[3756]329 }
330 h2 *= fnorm;
[3769]331 cout << " ---- MultiDish::GetResponse/[4] APRES VMin=" << h2.VMin() << " VMax= " << h2.VMax() << " h(0,0)="
[3756]332 << h2(0,0) << endl;
333 return h2;
334}
335
[3947]336HProf MultiDish::GetProjNoiseLevel(int nbin, bool fgnorm1)
337{
338 r_8 vmin,vmax;
339 h2w_.GetMinMax(vmin,vmax);
340 double seuil=vmax/10000.;
341 if (seuil<1.e-6) seuil=1.e-6;
342 r_8 facnorm=((fgnorm1)?vmax:1.);
343 cout << " MultiDish::GetProjNoiseLevel Min,Max=" << vmin << " , " << vmax
344 << " facnorm=" << facnorm << " seuil=" << seuil << endl;
345 HProf hp(0,kmax_,nbin);
346 for(sa_size_t j=0; j<h2w_.NBinY(); j++) {
347 double y=h2w_.Y(j);
348 for(sa_size_t i=0; i<h2w_.NBinX(); i++) {
349 double x=h2w_.X(i);
350 double yw=h2w_(i,j);
351 if (yw<seuil) continue;
352 hp.Add(sqrt(x*x+y*y),facnorm/yw);
353 }
354 }
355 return hp;
356}
357
358HProf MultiDish::GetProjResponse(int nbin, bool fgnorm1)
359{
360 r_8 vmin,vmax;
361 h2w_.GetMinMax(vmin,vmax);
362 r_8 facnorm=((fgnorm1)?(1./vmax):1.);
363 cout << " MultiDish::GetProjResponse Min,Max=" << vmin << " , " << vmax
364 << " facnorm=" << facnorm << endl;
365 HProf hp(0,kmax_,nbin);
366 for(sa_size_t j=0; j<h2w_.NBinY(); j++) {
367 double y=h2w_.Y(j);
368 for(sa_size_t i=0; i<h2w_.NBinX(); i++) {
369 double x=h2w_.X(i);
370 hp.Add(sqrt(x*x+y*y),h2w_(i,j)*facnorm);
371 }
372 }
373 return hp;
374}
375
376
[3769]377Histo2D MultiDish::PosDist(int nx, int ny, double dmax)
[3756]378{
[3769]379 if (dmax<1e-3) dmax=nx*dishes_[0].Diameter();
380 double dd = dmax/nx/2.;
381 Histo2D hpos(-dd,dmax+dd,nx+1,-dd,dmax+dd,ny+1);
382 for(size_t i=0; i<NbDishes(); i++) {
383 hpos.Add(dishes_[i].X, dishes_[i].Y);
[3756]384 }
[3769]385 return hpos;
[3756]386}
387
388double MultiDish::AddToHisto(double kx0, double ky0, double x, double y, double w, bool fgfh)
389{
390 double xxp = kx0+x;
391 double yyp = ky0+y;
392 double sumw=0.;
393 sumw += h2w_.Add(xxp, yyp, w, fgfh);
394 double xxm=kx0-x;
395 double yym=ky0-y;
[3769]396 // if (xxm>0.) {
397 sumw += h2w_.Add(xxm, yyp, w, fgfh);
398 // if (yym>0.)
399 sumw += h2w_.Add(xxm, yym, w, fgfh);
400 // }
401 // if (yym>0.)
402 sumw += h2w_.Add(xxp, yym, w, fgfh);
[3756]403 return sumw;
404}
405
406double MultiDish::CumulResp(Four2DResponse& rd, double theta, double phi)
407{
408 // cout << " MultiDish::CumulResp() theta=" << theta << " phi=" << phi << endl;
[3933]409 /*
[3756]410 double dx = h2w_.WBinX()/5;
411 double dy = h2w_.WBinY()/5;
[3769]412 int nbx = DeuxPI*rd.Dx()/dx+1;
413 int nby = DeuxPI*rd.Dy()/dy+1;
[3933]414 */
415 double dx,dy;
416 int nbx=beamnx_;
417 int nby=beamny_;
[3756]418 dx = DeuxPI*rd.Dx()/(double)nbx;
419 dy = DeuxPI*rd.Dy()/(double)nby;
420 if (mcnt_==0)
421 cout << " CumulResp() nbx=" << nbx << " nby=" << nby << " dx=" << dx << " dy=" << dy << endl;
422 mcnt_++;
423
424 double sumw = 0.;
425 Rotation rot(theta, phi);
426
427 for(size_t i=0; i<dishes_.size(); i++) {
[3934]428 for(size_t j=i; j<dishes_.size(); j++) {
[3769]429 double kx0 = DeuxPI*(dishes_[i].X-dishes_[j].X)/lambda_;
430 double ky0 = DeuxPI*(dishes_[i].Y-dishes_[j].Y)/lambda_;
[3947]431 double pgain=dishes_[i].Gain()*dishes_[j].Gain();
[3756]432 rot.Do(kx0, ky0);
[3769]433 // if (kx0<0) kx0=-kx0;
434 // if (ky0<0) ky0=-ky0;
[3756]435 bool fgfh= (!fgnoauto_ || (dishes_[i].ReflectorId()!=dishes_[j].ReflectorId()));
436 for(int ix=0; ix<nbx; ix++)
437 for(int jy=0; jy<nby; jy++) {
438 double x = ix*dx;
[3769]439 double y = jy*dy;
440 if ((ix>0)&&(jy>0)) {
[3947]441 sumw += AddToHisto(kx0, ky0, x, y, rd(x,y)*pgain, fgfh);
442 if (j!=i) sumw += AddToHisto(-kx0, -ky0, x, y, rd(x,y)*pgain, fgfh);
[3769]443 }
444 else {
[3934]445 if ((ix==0)&&(jy==0)) {
[3947]446 sumw += h2w_.Add(kx0, ky0, rd(0.,0.)*pgain, fgfh);
447 if (j!=i) sumw += h2w_.Add(-kx0, -ky0, rd(0.,0.)*pgain, fgfh);
[3934]448 }
[3769]449 else {
[3947]450 double w = rd(x,y)*pgain;
[3769]451 if (ix==0) {
452 sumw += h2w_.Add(kx0, ky0+y, w, fgfh);
453 sumw += h2w_.Add(kx0, ky0-y, w, fgfh);
[3934]454 if (j!=i) {
455 sumw += h2w_.Add(-kx0, -ky0+y, w, fgfh);
456 sumw += h2w_.Add(-kx0, -ky0-y, w, fgfh);
457 }
[3769]458 }
459 else {
460 sumw += h2w_.Add(kx0+x, ky0, w, fgfh);
461 sumw += h2w_.Add(kx0-x, ky0, w, fgfh);
[3934]462 if (j!=i) {
463 sumw += h2w_.Add(-kx0+x, -ky0, w, fgfh);
464 sumw += h2w_.Add(-kx0-x, -ky0, w, fgfh);
465 }
[3769]466 }
467 }
468 //
469 }
[3756]470 }
471 // if (i%10==0)
472 // cout << " MultiDish::CumulResp() done i=" << i << " / imax=" << dishes_.size()
473 // << " theta=" << theta << " phi=" << phi << endl;
[3769]474 }
[3756]475 }
476 return sumw;
477}
478
Note: See TracBrowser for help on using the repository browser.