source: trunk/MagicSoft/Cosy/tpoint/tpointfit.C@ 8149

Last change on this file since 8149 was 2278, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 12.9 KB
Line 
1#include <fstream.h>
2#include <iostream.h>
3#include <iomanip.h>
4
5#include <TF1.h>
6#include <TH1.h>
7#include <TH2.h>
8#include <TGraph.h>
9
10#include <TList.h>
11#include <TMinuit.h>
12
13#include <TView.h>
14#include <TLine.h>
15#include <TMarker.h>
16#include <TCanvas.h>
17
18#include "coord.h"
19#include "MBending.h"
20
21//
22// Sekans = 1/cos
23//
24
25class Set : public TObject
26{
27 friend ifstream &operator>>(ifstream &fin, Set &set);
28private:
29 Double_t fStarAz;
30 Double_t fStarEl;
31
32 Double_t fRawAz;
33 Double_t fRawEl;
34
35public:
36 Set(Double_t sel=0, Double_t saz=0, Double_t rel=0, Double_t raz=0) :
37 fStarAz(saz*TMath::Pi()/180),
38 fStarEl(sel*TMath::Pi()/180),
39 fRawAz(raz*TMath::Pi()/180),
40 fRawEl(rel*TMath::Pi()/180)
41 {
42 }
43
44 Double_t GetResidual() const
45 {
46 Double_t del = fRawEl-fStarEl;
47 Double_t daz = fRawAz-fStarAz;
48 Double_t dphi2 = daz/2.;
49 Double_t cos2 = cos(dphi2)*cos(dphi2);
50 Double_t sin2 = sin(dphi2)*sin(dphi2);
51 Double_t d = cos(del)*cos2 - cos(fRawEl+fStarEl)*sin2;
52
53 Double_t dist = acos(d);
54
55 return dist * 180 / TMath::Pi();
56 }
57
58 void operator=(Set &set)
59 {
60 fStarAz = set.fStarAz;
61 fStarEl = set.fStarEl;
62 fRawAz = set.fRawAz;
63 fRawEl = set.fRawEl;
64 }
65
66 Double_t GetDEl() const { return (fRawEl-fStarEl)*180/TMath::Pi(); }
67 Double_t GetDZd() const { return -GetDEl(); }
68 Double_t GetDAz() const { return (fRawAz-fStarAz)*180/TMath::Pi(); }
69 Double_t GetStarEl() const { return fStarEl*180/TMath::Pi(); }
70 Double_t GetStarZd() const { return 90.-fStarEl*180/TMath::Pi(); }
71 Double_t GetStarAz() const { return fStarAz*180/TMath::Pi(); }
72 Double_t GetRawEl() const { return fRawEl*180/TMath::Pi(); }
73 Double_t GetRawAz() const { return fRawAz*180/TMath::Pi(); }
74 Double_t GetRawZd() const { return 90.-fRawEl*180/TMath::Pi(); }
75
76 ZdAz GetStarZdAz() const { return ZdAz(TMath::Pi()/2-fStarEl, fStarAz); }
77 AltAz GetStarAltAz() const { return AltAz(fStarEl, fStarAz); }
78
79 ZdAz GetRawZdAz() const { return ZdAz(TMath::Pi()/2-fRawEl, fRawAz); }
80 AltAz GetRawAltAz() const { return AltAz(fRawEl, fRawAz); }
81
82 void AdjustEl(Double_t del) { fStarEl += del*TMath::Pi()/180; }
83 void AdjustAz(Double_t daz) { fStarAz += daz*TMath::Pi()/180; }
84
85 void Adjust(const MBending &bend)
86 {
87 AltAz p = bend(GetStarAltAz());
88 fStarEl = p.Alt();
89 fStarAz = p.Az();
90 }
91 void AdjustBack(const MBending &bend)
92 {
93 AltAz p = bend.CorrectBack(GetRawAltAz());
94 fRawEl = p.Alt();
95 fRawAz = p.Az();
96 }
97};
98
99ifstream &operator>>(ifstream &fin, Set &set)
100{
101 Double_t v[4];
102 fin >> v[0];
103 fin >> v[1];
104 fin >> v[2];
105 fin >> v[3];
106
107 Double_t dummy;
108 fin>>dummy;
109 fin>>dummy;
110 fin>>dummy;
111
112 set.fStarAz = v[0]*TMath::Pi()/180;
113 set.fStarEl = v[1]*TMath::Pi()/180;
114
115 set.fRawAz = v[2]*TMath::Pi()/180;
116 set.fRawEl = v[3]*TMath::Pi()/180;
117
118 return fin;
119}
120
121TList gCoordinates;
122
123void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
124{
125 f = 0;
126
127 MBending bend;
128 bend.SetParameters(par); // Set Parameters [deg] to MBending
129
130 for (int i=0; i<gCoordinates.GetSize(); i++)
131 {
132 Set set = *(Set*)gCoordinates.At(i);
133
134 set.Adjust(bend);
135
136 Double_t err = 1;
137 Double_t res = set.GetResidual()/err;
138
139 f += res*res;
140 }
141
142 f /= gCoordinates.GetSize()*gCoordinates.GetSize();
143}
144
145void DrawMarker(TVirtualPad *pad, Double_t r0, Double_t phi0, Double_t r1, Double_t phi1)
146{
147 TView *view = pad->GetView();
148
149 if (!view)
150 {
151 cout << "No View!" << endl;
152 return;
153 }
154
155 TMarker mark0;
156 TMarker mark1;
157 mark0.SetMarkerStyle(kStar);
158 mark1.SetMarkerStyle(kStar);
159 mark1.SetMarkerColor(kRed);
160
161 r0 /= 90;
162 r1 /= 90;
163 phi0 *= TMath::Pi()/180;
164 phi1 *= TMath::Pi()/180;
165
166 Double_t x0[3] = { r0*cos(phi0), r0*sin(phi0), 0};
167 Double_t x1[3] = { r1*cos(phi1), r1*sin(phi1), 0};
168
169 Double_t y0[3], y1[3];
170
171 view->WCtoNDC(x0, y0);
172 view->WCtoNDC(x1, y1);
173
174 mark0.DrawMarker(y0[0], y0[1]);
175 mark1.DrawMarker(y1[0], y1[1]);
176}
177
178void DrawPolLine(TVirtualPad *pad, Double_t r0, Double_t phi0, Double_t r1, Double_t phi1)
179{
180 TView *view = pad->GetView();
181
182 if (!view)
183 {
184 cout << "No View!" << endl;
185 return;
186 }
187 /*
188 if (r0<0)
189 {
190 r0 = -r0;
191 phi0 += 180;
192 }
193 if (r1<0)
194 {
195 r1 = -r1;
196 phi1 += 180;
197 }
198
199 phi0 = fmod(phi0+360, 360);
200 phi1 = fmod(phi1+360, 360);
201
202 if (phi1-phi0<-180)
203 phi1+=360;
204 */
205 TLine line;
206 line.SetLineWidth(2);
207 line.SetLineColor(kBlue);
208
209 Double_t p0 = phi0<phi1?phi0:phi1;
210 Double_t p1 = phi0<phi1?phi1:phi0;
211
212 if (phi0>phi1)
213 {
214 Double_t d = r1;
215 r1 = r0;
216 r0 = d;
217 }
218
219 r0 /= 90;
220 r1 /= 90;
221
222 Double_t dr = r1-r0;
223 Double_t dp = p1-p0;
224
225 Double_t x0[3] = { r0*cos(p0*TMath::Pi()/180), r0*sin(p0*TMath::Pi()/180), 0};
226
227 for (double i=p0+10; i<p1+10; i+=10)
228 {
229 if (i>p1)
230 i=p1;
231
232 Double_t r = dr/dp*(i-p0)+r0;
233 Double_t p = TMath::Pi()/180*i;
234
235 Double_t x1[3] = { r*cos(p), r*sin(p), 0};
236
237 Double_t y0[3], y1[3];
238
239 view->WCtoNDC(x0, y0);
240 view->WCtoNDC(x1, y1);
241
242 line.DrawLine(y0[0], y0[1], y1[0], y1[1]);
243
244 x0[0] = x1[0];
245 x0[1] = x1[1];
246 }
247}
248
249void DrawSet(TVirtualPad *pad, Set &set, Float_t scale=-1)
250{
251 Double_t r0 = set.GetRawZd();
252 Double_t phi0 = set.GetRawAz();
253 Double_t r1 = set.GetStarZd();
254 Double_t phi1 = set.GetStarAz();
255
256 if (r0<0)
257 {
258 r0 = -r0;
259 phi0 += 180;
260 }
261 if (r1<0)
262 {
263 r1 = -r1;
264 phi1 += 180;
265 }
266
267 phi0 = fmod(phi0+360, 360);
268 phi1 = fmod(phi1+360, 360);
269
270 if (phi1-phi0<-180)
271 phi1+=360;
272
273 if (scale<0 || scale>1000)
274 scale = -1;
275
276 if (scale>0)
277 {
278 Double_t d = r1-r0;
279 r0 += scale*d;
280 r1 -= scale*d;
281 d = phi1-phi0;
282 phi0 += scale*d;
283 phi1 -= scale*d;
284
285 DrawPolLine(pad, r0, phi0, r1, phi1);
286 DrawMarker(pad, r0, phi0, r1, phi1);
287 }
288 else
289 DrawMarker(pad, r1, phi1, 0 ,0);
290}
291
292void tpointfit()
293{
294 cout << "Starting..." << endl;
295
296 gCoordinates.SetOwner();
297
298 TH1F hres1("Res1", " Residuals before correction ", 10, 0, 4);
299 TH1F hres2("Res2", " Residuals after correction ", 40, 0, 4);
300
301 hres1.SetXTitle("\\Delta [\\circ]");
302 hres1.SetYTitle("Counts");
303
304 hres2.SetXTitle("\\Delta [\\circ]");
305 hres2.SetYTitle("Counts");
306
307 TGraph gdaz;
308 TGraph gdzd;
309 TGraph gaz;
310 TGraph gzd;
311
312 gdaz.SetTitle(" \\Delta Az vs. Zd ");
313 gdzd.SetTitle(" \\Delta Zd vs. Az ");
314
315 gaz.SetTitle(" \\Delta Az vs. Az ");
316 gzd.SetTitle(" \\Delta Zd vs. Zd ");
317
318 ifstream fin("tpoint.txt");
319
320 while (fin && fin.get()!='\n');
321 while (fin && fin.get()!='\n');
322 while (fin && fin.get()!='\n');
323 if (!fin)
324 {
325 cout << "File not found!" << endl;
326 return;
327 }
328
329 while (1)
330 {
331 Set set;
332
333 fin >> set; // Read data from file [deg], it is stored in [rad]
334
335 if (!fin)
336 break;
337
338 gCoordinates.Add(new Set(set));
339 }
340
341 cout << "Found " << gCoordinates.GetSize() << " sets of coordinates." << endl;
342
343 TMinuit minuit(16); //initialize TMinuit with a maximum of 5 params
344 minuit.SetPrintLevel(-1);
345 minuit.SetFCN(fcn);
346
347 MBending bending;
348 bending.SetMinuitParameters(minuit, 16); // Init Parameters [deg]
349
350 //minuit.FixParameter(0); //(1) IA
351 //minuit.FixParameter(1); //(1) IE
352 minuit.FixParameter(2); //(2) NPAE
353 //minuit.FixParameter(3); //(1) CA
354 minuit.FixParameter(4); //(2) AN
355 //minuit.FixParameter(5); //(1) AW
356
357 minuit.FixParameter(6); // NRX
358 minuit.FixParameter(7); // NRY
359 minuit.FixParameter(8); // CRX
360 minuit.FixParameter(9); // CRY
361 minuit.FixParameter(10); // ECES
362 minuit.FixParameter(11); //(2) ACES
363 minuit.FixParameter(12); // ECEC
364 minuit.FixParameter(13); //(2) ACEC
365 minuit.FixParameter(14); // MAGIC1
366 minuit.FixParameter(15); // MAGIC2
367
368
369 //minuit.Command("SHOW PARAMETERS");
370 //minuit.Command("SHOW LIMITS");
371 cout << endl;
372
373 Int_t ierflg = 0;
374 ierflg = minuit.Migrad();
375 cout << "Migrad returns " << ierflg << endl;
376 // minuit.Release(2);
377 ierflg = minuit.Migrad();
378 cout << "Migrad returns " << ierflg << endl << endl;
379
380 //
381 // Get Fit Results
382 //
383 bending.GetMinuitParameters(minuit);
384 bending.PrintMinuitParameters(minuit);
385 bending.Save("bending_magic.txt");
386
387
388 //
389 // Make a copy of all list entries
390 //
391 TList list;
392 list.SetOwner();
393 for (int i=0; i<gCoordinates.GetSize(); i++)
394 list.Add(new Set(*(Set*)gCoordinates.At(i)));
395
396 //
397 // Correct for Offsets only
398 //
399 double par[16];
400 bending.GetParameters(par);
401 for (int i=2; i<16; i++) par[i]=0;
402 MBending b2;
403 b2.SetParameters(par);
404
405 //
406 // Calculate correction and residuals
407 //
408 for (int i=0; i<gCoordinates.GetSize(); i++)
409 {
410 Set &set0 = *(Set*)gCoordinates.At(i);
411
412 ZdAz za = set0.GetStarZdAz()*kRad2Deg;
413
414 //
415 // Correct for offsets only
416 //
417 Set set1(set0);
418 set1.Adjust(b2);
419
420 hres1.Fill(set1.GetResidual());
421
422 set0.Adjust(bending);
423 hres2.Fill(set0.GetResidual());
424
425 Double_t dz = fmod(set0.GetDAz()+720, 360);
426 if (dz>180)
427 dz -= 360;
428
429 static int j=0;
430 gdzd.SetPoint(j, za.Az(), set0.GetDZd());
431 gaz.SetPoint( j, za.Az(), dz);
432 gdaz.SetPoint(j, za.Zd(), dz);
433 gzd.SetPoint( j, za.Zd(), set0.GetDZd());
434 j++;
435 }
436
437 //
438 // Check for overflows
439 //
440 const Stat_t ov = hres2.GetBinContent(hres2.GetNbinsX()+1);
441 if (ov>0)
442 cout << "WARNING: " << ov << " overflows in residuals." << endl;
443
444 //
445 // Print all data sets for which the backward correction is
446 // twice times worse than the residual gotten from the
447 // bending correction itself
448 //
449 cout << endl;
450 cout << "Checking backward correction (raw-->star):" << endl;
451 for (int i=0; i<gCoordinates.GetSize(); i++)
452 {
453 Set set0(*(Set*)list.At(i));
454 Set set1(*(Set*)list.At(i));
455
456 set0.AdjustBack(bending);
457 set1.Adjust(bending);
458
459 Double_t res0 = set0.GetResidual();
460 Double_t res1 = set1.GetResidual();
461
462 Double_t diff = fabs(res0-res1);
463
464 if (diff<hres2.GetMean()*0.66)
465 continue;
466
467 cout << "DBack: " << setw(7) << set0.GetStarZd() << " " << setw(8) << set0.GetStarAz() << ": ";
468 cout << "ResB="<< setw(7) << res0*60 << " ResF=" << setw(7) << res1*60 << " |ResB-ResF|=" << setw(7) << diff*60 << " arcmin" << endl;
469 }
470 cout << "OK." << endl;
471
472 //
473 // Print out the residual before and after correction in several
474 // units
475 //
476 cout << endl;
477 cout << gCoordinates.GetSize() << " data sets." << endl << endl;
478 cout << "Spead before: " << hres1.GetMean() << "deg +- " << hres1.GetRMS() << "deg" << endl;
479 cout << "Spead after: " << hres2.GetMean() << "deg +- " << hres2.GetRMS() << "deg" << endl;
480 cout << "Spead before: " << (int)(hres1.GetMean()*60+.5) << "' +- " << (int)(hres1.GetRMS()*60+.5) << "'" << endl;
481 cout << "Spead before: " << (int)(hres1.GetMean()*60*60/23.4+.5) << "pix +- " << (int)(hres1.GetRMS()*60*60/23.4+.5) << "pix" << endl;
482 cout << "Spead after: " << (int)(hres2.GetMean()*60+.5) << "' +- " << (int)(hres2.GetRMS()*60+.5) << "'" << endl;
483 cout << "Spead after: " << (int)(hres2.GetMean()*60*60/23.4+.5) << "pix +- " << (int)(hres2.GetRMS()*60*60/23.4+.5) << "pix" << endl;
484 cout << "Spead after: " << (int)(hres2.GetMean()*16384/360+.5) << "SE +- " << (int)(hres2.GetRMS()*16384/360+.5) << "SE" << endl;
485 cout << endl;
486
487
488
489 TCanvas *c1=new TCanvas("c1", "c");
490 c1->Divide(2,2);
491
492 c1->cd(2);
493 gdaz.DrawClone("A*");
494
495 c1->cd(1);
496 gaz.DrawClone("A*");
497
498 c1->cd(3);
499 gdzd.DrawClone("A*");
500
501 c1->cd(4);
502 gzd.DrawClone("A*");
503
504
505
506 c1=new TCanvas("c2", "c", 800, 800);
507 c1->Divide(2,2);
508
509 c1->cd(2);
510 hres1.SetLineColor(kRed);
511 hres1.DrawCopy();
512
513 c1->cd(4);
514 hres2.SetLineColor(kBlue);
515 hres2.DrawCopy();
516
517 c1->cd(1);
518 gPad->SetTheta(90);
519 gPad->SetPhi(90);
520 TH2F h2res1("Res2D1", " Arb. Residuals before correction (scaled) ", 32, 0, 360, 10, 0, 90);
521 h2res1.DrawCopy("surf1pol");
522 gPad->Modified();
523 gPad->Update();
524 for (int i=0; i<gCoordinates.GetSize(); i++)
525 DrawSet(gPad, *(Set*)list.At(i));//, 10./hres1.GetMean());
526
527 c1->cd(3);
528 gPad->SetTheta(90);
529 gPad->SetPhi(90);
530 h2res1.SetTitle(" Arb. Residuals after correction (scaled) ");
531 h2res1.DrawCopy("surf1pol");
532 gPad->Modified();
533 gPad->Update();
534 for (int i=0; i<gCoordinates.GetSize(); i++)
535 DrawSet(gPad, *(Set*)gCoordinates.At(i), 10./hres2.GetMean());
536
537}
Note: See TracBrowser for help on using the repository browser.