source: trunk/MagicSoft/Cosy/tpoint/gui.C@ 6724

Last change on this file since 6724 was 4617, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 35.4 KB
Line 
1#include <fstream.h>
2#include <fstream.h>
3#include <fstream.h>
4#include <iostream.h>
5#include <iomanip.h>
6
7#include <TError.h>
8
9#include <TGFrame.h>
10#include <TGLabel.h>
11#include <TGButton.h>
12#include <TGFileDialog.h>
13
14#include <TF1.h>
15#include <TH1.h>
16#include <TH2.h>
17#include <TGraphErrors.h>
18
19#include <TList.h>
20#include <TStyle.h>
21#include <TMinuit.h>
22
23#include <TView.h>
24#include <TLine.h>
25#include <TMarker.h>
26#include <TCanvas.h>
27
28#include "coord.h"
29
30#include "MGList.h"
31#include "MBending.h"
32
33class Set : public TObject
34{
35 friend ifstream &operator>>(ifstream &fin, Set &set);
36private:
37 Double_t fStarAz;
38 Double_t fStarEl;
39
40 Double_t fRawAz;
41 Double_t fRawEl;
42
43 Double_t fMag;
44public:
45 Set(Double_t sel=0, Double_t saz=0, Double_t rel=0, Double_t raz=0) :
46 fStarAz(saz*TMath::DegToRad()),
47 fStarEl(sel*TMath::DegToRad()),
48 fRawAz(raz*TMath::DegToRad()),
49 fRawEl(rel*TMath::DegToRad()), fMag(-25)
50 {
51 }
52
53 Double_t GetMag() const { return fMag; }
54 Double_t GetResidual(Double_t *err=0) const
55 {
56 /*
57 TVector3 v1, v2;
58 v1.SetMagThetaPhi(1, TMath::Pi()/2-fRawEl, fRawAz);
59 v2.SetMagThetaPhi(1, TMath::Pi()/2-fStarEl, fStarAz);
60
61 return v1.Angle(v2)*TMath::RadToDeg();
62 */
63
64 const Double_t del = fRawEl-fStarEl;
65 const Double_t daz = fRawAz-fStarAz;
66 /*
67 const Double_t dphi2 = daz/2.;
68 const Double_t cos2 = cos(dphi2)*cos(dphi2);
69 const Double_t sin2 = sin(dphi2)*sin(dphi2);
70 const Double_t d = cos(del)*cos2 - cos(fRawEl+fStarEl)*sin2;
71 */
72
73 const Double_t d = cos(del) - cos(fRawEl)*cos(fStarEl)*(1.-cos(daz));
74
75 if (err)
76 {
77 // Error of one pixel in the CCD
78 const Double_t e1 = 32./3600*TMath::DegToRad() * 0.5;
79
80 // Error of one SE unit
81 const Double_t e2 = 360./16384*TMath::DegToRad() * 0.5;
82
83 const Double_t e11 = sin(del)+cos(fRawEl)*sin(fStarEl)*(1-cos(daz));
84 const Double_t e12 = cos(fRawEl)*cos(fStarEl)*sin(daz);
85
86 const Double_t e21 = -sin(del)+sin(fRawEl)*cos(fStarEl)*(1-cos(daz));
87 const Double_t e22 = -cos(fRawEl)*cos(fStarEl)*sin(daz);
88
89 const Double_t err1 = sqrt(1-d*d);
90 const Double_t err2 = (e11*e11 + e12*e12)*e1*e1;
91 const Double_t err3 = (e21*e21 + e22*e22)*e2*e2;
92
93 *err = sqrt(err2+err3)/err1 * TMath::RadToDeg();
94 }
95
96 const Double_t dist = acos(d);
97 return dist * TMath::RadToDeg();
98 }
99
100 void operator=(Set &set)
101 {
102 fStarAz = set.fStarAz;
103 fStarEl = set.fStarEl;
104 fRawAz = set.fRawAz;
105 fRawEl = set.fRawEl;
106 fMag = set.fMag;
107 }
108
109 Double_t GetDEl() const { return (fRawEl-fStarEl)*TMath::RadToDeg(); }
110 Double_t GetDZd() const { return -GetDEl(); }
111 Double_t GetDAz() const { return (fRawAz-fStarAz)*TMath::RadToDeg(); }
112 Double_t GetStarEl() const { return fStarEl*TMath::RadToDeg(); }
113 Double_t GetStarZd() const { return 90.-fStarEl*TMath::RadToDeg(); }
114 Double_t GetStarAz() const { return fStarAz*TMath::RadToDeg(); }
115 Double_t GetRawEl() const { return fRawEl*TMath::RadToDeg(); }
116 Double_t GetRawAz() const { return fRawAz*TMath::RadToDeg(); }
117 Double_t GetRawZd() const { return 90.-fRawEl*TMath::RadToDeg(); }
118
119 ZdAz GetStarZdAz() const { return ZdAz(TMath::Pi()/2-fStarEl, fStarAz); }
120 AltAz GetStarAltAz() const { return AltAz(fStarEl, fStarAz); }
121
122 ZdAz GetRawZdAz() const { return ZdAz(TMath::Pi()/2-fRawEl, fRawAz); }
123 AltAz GetRawAltAz() const { return AltAz(fRawEl, fRawAz); }
124
125 void AdjustEl(Double_t del) { fStarEl += del*TMath::DegToRad(); }
126 void AdjustAz(Double_t daz) { fStarAz += daz*TMath::DegToRad(); }
127
128 void Adjust(const MBending &bend)
129 {
130 AltAz p = bend(GetStarAltAz());
131 fStarEl = p.Alt();
132 fStarAz = p.Az();
133 }
134 void AdjustBack(const MBending &bend)
135 {
136 AltAz p = bend.CorrectBack(GetRawAltAz());
137 fRawEl = p.Alt();
138 fRawAz = p.Az();
139 }
140 ClassDef(Set, 0)
141};
142
143ClassImp(Set);
144
145ifstream &operator>>(ifstream &fin, Set &set)
146{
147 TString str;
148 do
149 {
150 str.ReadLine(fin);
151 if (!fin)
152 return fin;
153 } while (str[0]=='#');
154
155 Float_t v[4], mag;
156 Int_t n = sscanf(str.Data(), "%f %f %f %f %*f %*f %*f %*f %*f %*f %f", v, v+1, v+2, v+3, &mag);
157 if (n<4)
158 {
159 cout << "Read: ERROR - Not enough numbers" << endl;
160 return fin;
161 }
162 set.fMag = n<5 ? -25 : mag;
163
164 set.fStarAz = v[0]*TMath::DegToRad();
165 set.fStarEl = v[1]*TMath::DegToRad();
166
167 set.fRawAz = v[2]*TMath::DegToRad();
168 set.fRawEl = v[3]*TMath::DegToRad();
169
170 if (fin)
171 {
172 Double_t res, err;
173 res = set.GetResidual(&err);
174 cout << "Read: " << v[0] << " " << v[1] << " : " << v[2] << " " << v[3] << " : " << v[2]-v[0] << " " << v[3]-v[1] << " : " << res << " " << err << " " << err/res << endl;
175 }
176
177 return fin;
178}
179
180class MFit : public TGMainFrame
181{
182private:
183 enum {
184 kTbFit = 19, //MBending::GetNumPar(), // FIXME!!!
185 kTbLoad,
186 kTbSave,
187 kTbLoadStars,
188 kTbReset,
189 kTbResetStars
190 };
191
192 MGList *fList;
193
194 TList fOriginal;
195 TList fCoordinates;
196 TList fLabel;
197
198 MBending fBending;
199
200 FontStruct_t fFont;
201
202 void Fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
203 {
204 f = 0;
205
206 MBending bend;
207 bend.SetParameters(par); // Set Parameters [deg] to MBending
208
209 for (int i=0; i<fCoordinates.GetSize(); i++)
210 {
211 Set set = *(Set*)fCoordinates.At(i);
212
213 set.Adjust(bend);
214
215 Double_t err;// = 0.005; // [deg] = 0.25SE
216 Double_t res = set.GetResidual(&err);
217 res /= err;
218
219 f += res*res;
220 }
221
222 //f /= (fCoordinates.GetSize()-7)*(fCoordinates.GetSize()-7);
223 //f /= fCoordinates.GetSize()*fCoordinates.GetSize();
224 //cout << f << ": " << fCoordinates.GetSize() << endl;
225 f /= fCoordinates.GetSize();
226 }
227
228 static void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
229 {
230 ((MFit*)gMinuit->GetObjectFit())->Fcn(npar, gin, f, par, iflag);
231 }
232
233 void DrawMarker(TVirtualPad *pad, Double_t r0, Double_t phi0, Double_t r1, Double_t phi1)
234 {
235 TView *view = pad->GetView();
236
237 if (!view)
238 {
239 cout << "No View!" << endl;
240 return;
241 }
242
243 TMarker mark0;
244 //TMarker mark1;
245 mark0.SetMarkerStyle(kStar);
246 //mark1.SetMarkerStyle(kStar);
247 //mark1.SetMarkerColor(kRed);
248
249 r0 /= 90;
250 //r1 /= 90;
251 phi0 *= TMath::DegToRad();
252 //phi1 *= TMath::DegToRad();
253
254 Double_t x0[3] = { r0*cos(phi0), r0*sin(phi0), 0};
255 //Double_t x1[3] = { r1*cos(phi1), r1*sin(phi1), 0};
256
257 Double_t y0[3], y1[3];
258
259 view->WCtoNDC(x0, y0);
260 //view->WCtoNDC(x1, y1);
261
262 mark0.DrawMarker(y0[0], y0[1]);
263 //mark1.DrawMarker(y1[0], y1[1]);
264 }
265
266 void DrawPolLine(TVirtualPad *pad, Double_t r0, Double_t phi0, Double_t r1, Double_t phi1)
267 {
268 TView *view = pad->GetView();
269
270 if (!view)
271 {
272 cout << "No View!" << endl;
273 return;
274 }
275 /*
276 if (r0<0)
277 {
278 r0 = -r0;
279 phi0 += 180;
280 }
281 if (r1<0)
282 {
283 r1 = -r1;
284 phi1 += 180;
285 }
286
287 phi0 = fmod(phi0+360, 360);
288 phi1 = fmod(phi1+360, 360);
289
290 if (phi1-phi0<-180)
291 phi1+=360;
292 */
293 TLine line;
294 line.SetLineWidth(2);
295 line.SetLineColor(kBlue);
296
297 Double_t p0 = phi0<phi1?phi0:phi1;
298 Double_t p1 = phi0<phi1?phi1:phi0;
299
300 if (phi0>phi1)
301 {
302 Double_t d = r1;
303 r1 = r0;
304 r0 = d;
305 }
306
307 r0 /= 90;
308 r1 /= 90;
309
310 Double_t dr = r1-r0;
311 Double_t dp = p1-p0;
312
313 Double_t x0[3] = { r0*cos(p0*TMath::DegToRad()), r0*sin(p0*TMath::DegToRad()), 0};
314
315 for (double i=p0+10; i<p1+10; i+=10)
316 {
317 if (i>p1)
318 i=p1;
319
320 Double_t r = dr/dp*(i-p0)+r0;
321 Double_t p = TMath::DegToRad()*i;
322
323 Double_t x1[3] = { r*cos(p), r*sin(p), 0};
324
325 Double_t y0[3], y1[3];
326
327 view->WCtoNDC(x0, y0);
328 view->WCtoNDC(x1, y1);
329
330 line.DrawLine(y0[0], y0[1], y1[0], y1[1]);
331
332 x0[0] = x1[0];
333 x0[1] = x1[1];
334 }
335 }
336
337 void DrawSet(TVirtualPad *pad, Set &set, Float_t scale=-1, Float_t angle=0)
338 {
339 Double_t r0 = set.GetRawZd();
340 Double_t phi0 = set.GetRawAz()-angle;
341 Double_t r1 = set.GetStarZd();
342 Double_t phi1 = set.GetStarAz()-angle;
343
344 if (r0<0)
345 {
346 r0 = -r0;
347 phi0 += 180;
348 }
349 if (r1<0)
350 {
351 r1 = -r1;
352 phi1 += 180;
353 }
354
355 phi0 = fmod(phi0+360, 360);
356 phi1 = fmod(phi1+360, 360);
357
358 if (phi1-phi0<-180)
359 phi1+=360;
360
361 if (scale<0 || scale>1000)
362 scale = -1;
363
364 if (scale>0)
365 {
366 Double_t d = r1-r0;
367 r0 += scale*d;
368 r1 -= scale*d;
369 d = phi1-phi0;
370 phi0 += scale*d;
371 phi1 -= scale*d;
372
373 DrawPolLine(pad, r0, phi0, r1, phi1);
374 DrawMarker(pad, r0, phi0, r1, phi1);
375 }
376 else
377 DrawMarker(pad, r1, phi1, 0 ,0);
378 }
379
380 void Fit(Double_t &before, Double_t &after, Double_t &backw)
381 {
382 if (fOriginal.GetSize()==0)
383 {
384 cout << "Sorry, no input data loaded..." << endl;
385 return;
386 }
387
388 fCoordinates.Delete();
389 for (int i=0; i<fOriginal.GetSize(); i++)
390 fCoordinates.Add(new Set(*(Set*)fOriginal.At(i)));
391
392 cout << "-----------------------------------------------------------------------" << endl;
393
394 gStyle->SetOptStat("emro");
395
396 TH1F hres1("Res1", " Residuals before correction ", fOriginal.GetSize()/3, 0, 0.3);
397 TH1F hres2("Res2", " Residuals after correction ", fOriginal.GetSize()/3, 0, 0.3);
398 TH1F hres3("Res3", " Residuals after backward correction ", fOriginal.GetSize()/3, 0, 0.3);
399
400 hres1.SetXTitle("\\Delta [\\circ]");
401 hres1.SetYTitle("Counts");
402
403 hres2.SetXTitle("\\Delta [\\circ]");
404 hres2.SetYTitle("Counts");
405
406 hres3.SetXTitle("\\Delta [\\circ]");
407 hres3.SetYTitle("Counts");
408
409 TGraph gdaz;
410 TGraph gdzd;
411 TGraph gaz;
412 TGraph gzd;
413 TGraph graz;
414 TGraph grzd;
415 TGraph grmag;
416 TGraph gmaz;
417 TGraph gmzd;
418
419 gdaz.SetTitle(" \\Delta Az vs. Zd ");
420 gdzd.SetTitle(" \\Delta Zd vs. Az ");
421
422 gaz.SetTitle(" \\Delta Az vs. Az ");
423 gzd.SetTitle(" \\Delta Zd vs. Zd ");
424
425 gmaz.SetTitle(" \\Delta Az vs. Mag ");
426 gmzd.SetTitle(" \\Delta Zd vs. Mag ");
427
428 graz.SetTitle(" \\Delta vs. Az ");
429 grzd.SetTitle(" \\Delta vs. Zd ");
430 grmag.SetTitle(" \\Delta vs. Mag ");
431
432 TMinuit minuit(MBending::GetNumPar()); //initialize TMinuit with a maximum of 5 params
433 minuit.SetObjectFit(this);
434 minuit.SetPrintLevel(-1);
435 minuit.SetFCN(fcn);
436
437 fBending.SetMinuitParameters(minuit, MBending::GetNumPar()); // Init Parameters [deg]
438
439 for (int i=0; i<MBending::GetNumPar(); i++)
440 {
441 TGButton *l = (TGButton*)fList->FindWidget(i);
442 minuit.FixParameter(i);
443 if (l->GetState()==kButtonDown)
444 minuit.Release(i);
445 }
446
447 //minuit.Command("SHOW PARAMETERS");
448 //minuit.Command("SHOW LIMITS");
449
450 cout << endl;
451 cout << "Starting fit..." << endl;
452 cout << "For the fit an measurement error in the residual of ";
453 cout << "0.02deg (=1SE) is assumed." << endl;
454 cout << endl;
455
456 Int_t ierflg = 0;
457 ierflg = minuit.Migrad();
458 cout << "Migrad returns " << ierflg << endl;
459 // minuit.Release(2);
460 ierflg = minuit.Migrad();
461 cout << "Migrad returns " << ierflg << endl << endl;
462
463 //
464 // Get Fit Results
465 //
466 fBending.GetMinuitParameters(minuit);
467 fBending.PrintMinuitParameters(minuit);
468 //fBending.Save("bending_magic.txt");
469
470
471 //
472 // Make a copy of all list entries
473 //
474 TList list;
475 list.SetOwner();
476 for (int i=0; i<fCoordinates.GetSize(); i++)
477 list.Add(new Set(*(Set*)fCoordinates.At(i)));
478
479 //
480 // Correct for Offsets only
481 //
482 TArrayD par;
483 fBending.GetParameters(par);
484 for (int i=2; i<MBending::GetNumPar(); i++)
485 par[i]=0;
486
487 MBending b2;
488 b2.SetParameters(par);
489
490 //
491 // Calculate correction and residuals
492 //
493 for (int i=0; i<fCoordinates.GetSize(); i++)
494 {
495 Set &set0 = *(Set*)fCoordinates.At(i);
496
497 ZdAz za = set0.GetStarZdAz()*kRad2Deg;
498
499 //
500 // Correct for offsets only
501 //
502 Set set1(set0);
503 set1.Adjust(b2);
504
505 hres1.Fill(set1.GetResidual());
506
507 set0.Adjust(fBending);
508 hres2.Fill(set0.GetResidual());
509
510 Double_t dz = fmod(set0.GetDAz()+720, 360);
511 if (dz>180)
512 dz -= 360;
513
514 gdzd.SetPoint(i, za.Az(), set0.GetDZd());
515 gdaz.SetPoint(i, za.Zd(), dz);
516 graz.SetPoint(i, za.Az(), set0.GetResidual());
517 grzd.SetPoint(i, za.Zd(), set0.GetResidual());
518 gaz.SetPoint( i, za.Az(), dz);
519 gzd.SetPoint( i, za.Zd(), set0.GetDZd());
520 if (set0.GetMag()>=-20)
521 {
522 grmag.SetPoint(i, set0.GetMag(), set0.GetResidual());
523 gmaz.SetPoint( i, set0.GetMag(), dz);
524 gmzd.SetPoint( i, set0.GetMag(), set0.GetDZd());
525 }
526 }
527
528 //
529 // Check for overflows
530 //
531 const Stat_t ov = hres2.GetBinContent(hres2.GetNbinsX()+1);
532 if (ov>0)
533 cout << "WARNING: " << ov << " overflows in residuals." << endl;
534
535
536
537 cout << dec << endl;
538 cout << " Number of calls to FCN: " << minuit.fNfcn << endl;
539 cout << "Minimum value found for FCN (Chi^2?): " << minuit.fAmin << endl;
540 cout << " Fit-Probability(?): " << TMath::Prob(minuit.fAmin/*fOriginal.GetSize()*/, fOriginal.GetSize()-minuit.GetNumFreePars())*100 << "%" << endl;
541 cout << " Chi^2/NDF: " << minuit.fAmin/(fOriginal.GetSize()-minuit.GetNumFreePars()) << endl;
542 //cout << "Prob(?): " << TMath::Prob(fChisquare,ndf);
543
544
545
546 //
547 // Print all data sets for which the backward correction is
548 // twice times worse than the residual gotten from the
549 // bending correction itself
550 //
551 cout << endl;
552 cout << "Checking backward correction (raw-->star):" << endl;
553 for (int i=0; i<fCoordinates.GetSize(); i++)
554 {
555 Set set0(*(Set*)list.At(i));
556 Set &set1 = *(Set*)list.At(i);
557
558 set0.AdjustBack(fBending);
559 set1.Adjust(fBending);
560
561 const Double_t res0 = set0.GetResidual();
562 const Double_t res1 = set1.GetResidual();
563 const Double_t diff = TMath::Abs(res0-res1);
564
565 hres3.Fill(res0);
566
567 if (diff<hres2.GetMean()*0.66)
568 continue;
569
570 cout << "DBack: " << setw(6) << set0.GetStarZd() << " " << setw(7) << set0.GetStarAz() << ": ";
571 cout << "ResB="<< setw(7) << res0*60 << " ResF=" << setw(7) << res1*60 << " |ResB-ResF|=" << setw(7) << diff*60 << " arcmin" << endl;
572 }
573 cout << "OK." << endl;
574 cout << endl;
575
576
577 TCanvas *c1;
578
579 if ((c1 = (TCanvas*)gROOT->FindObject("CanvGraphs")))
580 delete c1;
581 if ((c1 = (TCanvas*)gROOT->FindObject("CanvResiduals")))
582 delete c1;
583
584
585 const Double_t max1 = TMath::Max(gaz.GetHistogram()->GetMaximum(), gdaz.GetHistogram()->GetMaximum());
586 const Double_t max2 = TMath::Max(gzd.GetHistogram()->GetMaximum(), gdzd.GetHistogram()->GetMaximum());
587 const Double_t max3 = TMath::Max(grzd.GetHistogram()->GetMaximum(), graz.GetHistogram()->GetMaximum());
588
589 const Double_t min1 = TMath::Min(gaz.GetHistogram()->GetMinimum(), gdaz.GetHistogram()->GetMinimum());
590 const Double_t min2 = TMath::Min(gzd.GetHistogram()->GetMinimum(), gdzd.GetHistogram()->GetMinimum());
591 const Double_t min3 = TMath::Min(grzd.GetHistogram()->GetMinimum(), graz.GetHistogram()->GetMinimum());
592
593 const Double_t absmax1 = TMath::Max(max1, TMath::Abs(min1));
594 const Double_t absmax2 = TMath::Max(max2, TMath::Abs(min2));
595 const Double_t absmax3 = TMath::Max(max3, TMath::Abs(min3));
596
597 gaz.SetMaximum(absmax1);
598 gzd.SetMaximum(absmax2);
599 gdaz.SetMaximum(absmax1);
600 gdzd.SetMaximum(absmax2);
601 gmaz.SetMaximum(absmax1);
602 gmzd.SetMaximum(absmax2);
603 graz.SetMaximum(absmax3);
604 grzd.SetMaximum(absmax3);
605 grmag.SetMaximum(absmax3);
606 gaz.SetMinimum(-absmax1);
607 gzd.SetMinimum(-absmax2);
608 gdaz.SetMinimum(-absmax1);
609 gdzd.SetMinimum(-absmax2);
610 gmaz.SetMinimum(-absmax1);
611 gmzd.SetMinimum(-absmax2);
612 graz.SetMinimum(0);
613 grzd.SetMinimum(0);
614 grmag.SetMinimum(0);
615
616 c1=new TCanvas("CanvGraphs", "Graphs");
617 c1->Divide(3,3,0,0);
618
619 TLine line;
620 line.SetLineColor(kGreen);
621 line.SetLineWidth(2);
622
623 c1->cd(1);
624 gPad->SetBorderMode(0);
625 gPad->SetGridx();
626 gPad->SetGridy();
627 TGraph *g=(TGraph*)gaz.DrawClone("A*");
628 g->SetBit(kCanDelete);
629 g->GetHistogram()->SetXTitle("Az [\\circ]");
630 g->GetHistogram()->SetYTitle("\\Delta Az [\\circ]");
631 line.DrawLine(g->GetXaxis()->GetXmin(), 360./16384, g->GetXaxis()->GetXmax(), 360./16384);
632 line.DrawLine(g->GetXaxis()->GetXmin(), -360./16384, g->GetXaxis()->GetXmax(), -360./16384);
633
634 c1->cd(2);
635 gPad->SetBorderMode(0);
636 gPad->SetGridx();
637 gPad->SetGridy();
638 g=(TGraph*)gdaz.DrawClone("A*");
639 g->SetBit(kCanDelete);
640 g->GetHistogram()->SetXTitle("Zd [\\circ]");
641 g->GetHistogram()->SetYTitle("\\Delta Az [\\circ]");
642 line.DrawLine(g->GetXaxis()->GetXmin(), 360./16384, g->GetXaxis()->GetXmax(), 360./16384);
643 line.DrawLine(g->GetXaxis()->GetXmin(), -360./16384, g->GetXaxis()->GetXmax(), -360./16384);
644 cout << "Mean dAz: " << g->GetMean(2) << " \xb1 " << g->GetRMS(2) << endl;
645
646 c1->cd(3);
647 gPad->SetBorderMode(0);
648 gPad->SetGridx();
649 gPad->SetGridy();
650 g=(TGraph*)gmaz.DrawClone("A*");
651 g->SetBit(kCanDelete);
652 g->GetHistogram()->SetXTitle("Mag");
653 g->GetHistogram()->SetYTitle("\\Delta Az [\\circ]");
654 line.DrawLine(g->GetXaxis()->GetXmin(), 360./16384, g->GetXaxis()->GetXmax(), 360./16384);
655 line.DrawLine(g->GetXaxis()->GetXmin(), -360./16384, g->GetXaxis()->GetXmax(), -360./16384);
656
657 c1->cd(4);
658 gPad->SetBorderMode(0);
659 gPad->SetGridx();
660 gPad->SetGridy();
661 g=(TGraph*)gdzd.DrawClone("A*");
662 g->SetBit(kCanDelete);
663 g->GetHistogram()->SetXTitle("Az [\\circ]");
664 g->GetHistogram()->SetYTitle("\\Delta Zd [\\circ]");
665 line.DrawLine(g->GetXaxis()->GetXmin(), 360./16384, g->GetXaxis()->GetXmax(), 360./16384);
666 line.DrawLine(g->GetXaxis()->GetXmin(), -360./16384, g->GetXaxis()->GetXmax(), -360./16384);
667 cout << "Mean dZd: " << g->GetMean(2) << " \xb1 " << g->GetRMS(2) << endl;
668 cout << endl;
669
670 c1->cd(5);
671 gPad->SetBorderMode(0);
672 gPad->SetGridx();
673 gPad->SetGridy();
674 g=(TGraph*)gzd.DrawClone("A*");
675 g->SetBit(kCanDelete);
676 g->GetHistogram()->SetXTitle("Zd [\\circ]");
677 g->GetHistogram()->SetYTitle("\\Delta Zd [\\circ]");
678 line.DrawLine(g->GetXaxis()->GetXmin(), 360./16384, g->GetXaxis()->GetXmax(), 360./16384);
679 line.DrawLine(g->GetXaxis()->GetXmin(), -360./16384, g->GetXaxis()->GetXmax(), -360./16384);
680
681 c1->cd(6);
682 gPad->SetBorderMode(0);
683 gPad->SetGridx();
684 gPad->SetGridy();
685 g=(TGraph*)gmzd.DrawClone("A*");
686 g->SetBit(kCanDelete);
687 g->GetHistogram()->SetXTitle("Mag");
688 g->GetHistogram()->SetYTitle("\\Delta Zd [\\circ]");
689 line.DrawLine(g->GetXaxis()->GetXmin(), 360./16384, g->GetXaxis()->GetXmax(), 360./16384);
690 line.DrawLine(g->GetXaxis()->GetXmin(), -360./16384, g->GetXaxis()->GetXmax(), -360./16384);
691
692 c1->cd(7);
693 gPad->SetBorderMode(0);
694 gPad->SetGridx();
695 gPad->SetGridy();
696 g=(TGraph*)graz.DrawClone("A*");
697 g->SetBit(kCanDelete);
698 g->GetHistogram()->SetXTitle("Az [\\circ]");
699 g->GetHistogram()->SetYTitle("\\Delta [\\circ]");
700 line.DrawLine(g->GetXaxis()->GetXmin(), 360./16384, g->GetXaxis()->GetXmax(), 360./16384);
701
702 c1->cd(8);
703 gPad->SetBorderMode(0);
704 gPad->SetGridx();
705 gPad->SetGridy();
706 g=(TGraph*)grzd.DrawClone("A*");
707 g->SetBit(kCanDelete);
708 g->GetHistogram()->SetXTitle("Zd [\\circ]");
709 g->GetHistogram()->SetYTitle("\\Delta [\\circ]");
710 line.DrawLine(g->GetXaxis()->GetXmin(), 360./16384, g->GetXaxis()->GetXmax(), 360./16384);
711
712 c1->cd(9);
713 gPad->SetBorderMode(0);
714 gPad->SetGridx();
715 gPad->SetGridy();
716 g=(TGraph*)grmag.DrawClone("A*");
717 g->SetBit(kCanDelete);
718 g->GetHistogram()->SetXTitle("Mag");
719 g->GetHistogram()->SetYTitle("\\Delta [\\circ]");
720 line.DrawLine(g->GetXaxis()->GetXmin(), 360./16384, g->GetXaxis()->GetXmax(), 360./16384);
721
722
723 //
724 // Print out the residual before and after correction in several
725 // units
726 //
727 cout << fCoordinates.GetSize() << " data sets." << endl << endl;
728 cout << "Total Spread of Residual:" << endl;
729 cout << "-------------------------" << endl;
730 cout << "before: " << Form("%6.4f", hres1.GetMean()) << " \xb1 " << Form("%6.4f", hres1.GetRMS()) << " deg \t";
731 cout << "before: " << Form("%4.1f", hres1.GetMean()*60) << " \xb1 " << Form("%.1f", hres1.GetRMS()*60) << " arcmin" << endl;
732 cout << "after: " << Form("%6.4f", hres2.GetMean()) << " \xb1 " << Form("%6.4f", hres2.GetRMS()) << " deg \t";
733 cout << "after: " << Form("%4.1f", hres2.GetMean()*60) << " \xb1 " << Form("%.1f", hres2.GetRMS()*60) << " arcmin" << endl;
734 cout << "backw: " << Form("%6.4f", hres3.GetMean()) << " \xb1 " << Form("%6.4f", hres3.GetRMS()) << " deg \t";
735 cout << "backw: " << Form("%4.1f", hres3.GetMean()*60) << " \xb1 " << Form("%.1f", hres3.GetRMS()*60) << " arcmin" << endl;
736 cout << endl;
737 cout << "before: " << Form("%4.1f", hres1.GetMean()*16348/360) << " \xb1 " << Form("%.1f", hres1.GetRMS()*16384/360) << " SE \t\t";
738 cout << "before: " << Form("%4.1f", hres1.GetMean()*60*60/23.4) << " \xb1 " << Form("%.1f", hres1.GetRMS()*60*60/23.4) << " pix" << endl;
739 cout << "after: " << Form("%4.1f", hres2.GetMean()*16384/360) << " \xb1 " << Form("%.1f", hres2.GetRMS()*16384/360) << " SE \t\t";
740 cout << "after: " << Form("%4.1f", hres2.GetMean()*60*60/23.4) << " \xb1 " << Form("%.1f", hres2.GetRMS()*60*60/23.4) << " pix" << endl;
741 cout << "backw: " << Form("%4.1f", hres3.GetMean()*16384/360) << " \xb1 " << Form("%.1f", hres3.GetRMS()*16384/360) << " SE \t\t";
742 cout << "backw: " << Form("%4.1f", hres3.GetMean()*60*60/23.4) << " \xb1 " << Form("%.1f", hres3.GetRMS()*60*60/23.4) << " pix" << endl;
743 cout << endl;
744 cout << endl; // ±
745
746
747 before = hres1.GetMean()*16384/360;
748 after = hres2.GetMean()*16384/360;
749 backw = hres3.GetMean()*16384/360;
750
751
752 gStyle->SetOptStat(1110);
753 gStyle->SetStatFormat("6.2g");
754
755
756 c1=new TCanvas("CanvResiduals", "Residuals", 800, 800);
757 c1->Divide(2, 2, 0, 0);
758
759 c1->cd(2);
760 gPad->SetBorderMode(0);
761 hres1.SetLineColor(kRed);
762 hres1.DrawCopy();
763
764 line.DrawLine(360./16384, 0, 360./16384, hres1.GetMaximum());
765
766 c1->cd(4);
767 gPad->SetBorderMode(0);
768 hres2.SetLineColor(kBlue);
769 TH1 *h=hres2.DrawCopy();
770 TF1 f("mygaus", "(gaus)", 0, 1);
771 f.SetLineColor(kMagenta/*6*/);
772 f.SetLineWidth(1);
773 f.SetParameter(0, h->GetBinContent(1));
774 f.FixParameter(1, 0);
775 f.SetParameter(2, h->GetRMS());
776 h->Fit("mygaus", "QR");
777 hres3.SetLineColor(kCyan);
778 hres3.SetLineStyle(kDashed);
779 hres3.DrawCopy("same");
780 cout << "Gaus-Fit Sigma: " << f.GetParameter(2) << "\xb0" << endl;
781 cout << "Fit-Probability: " << f.GetProb()*100 << "%" << endl;
782 cout << " Chi^2/NDF: " << f.GetChisquare() << "/" << f.GetNDF() << " = " << f.GetChisquare()/f.GetNDF() << endl;
783 line.DrawLine(360./16384, 0, 360./16384, h->GetMaximum());
784
785 c1->cd(1);
786 gPad->SetBorderMode(0);
787 gPad->SetTheta(90);
788 gPad->SetPhi(90);
789 TH2F h2res1("Res2D1", " Dataset positions on the sky ", 32, 0, 360, 10, 0, 90);
790 h2res1.SetBit(TH1::kNoStats);
791 h2res1.DrawCopy("surf1pol");
792 gPad->Modified();
793 gPad->Update();
794 for (int i=0; i<fOriginal.GetSize(); i++)
795 DrawSet(gPad, *(Set*)fOriginal.At(i));//, 10./hres1.GetMean());
796
797 c1->cd(3);
798 gPad->SetBorderMode(0);
799 gPad->SetTheta(90);
800 gPad->SetPhi(90);
801 h2res1.SetTitle(" Arb. Residuals after correction (scaled) ");
802 h2res1.DrawCopy("surf1pol");
803 gPad->Modified();
804 gPad->Update();
805// for (int i=0; i<fCoordinates.GetSize(); i++)
806// DrawSet(gPad, *(Set*)fCoordinates.At(i), 10./hres2.GetMean(), par[0]);
807
808 RaiseWindow();
809 }
810
811 void LoadStars(TString fname="tpoint.txt")
812 {
813 const Int_t size = fOriginal.GetSize();
814
815 ifstream fin(fname);
816
817 while (fin && fin.get()!='\n');
818 while (fin && fin.get()!='\n');
819 while (fin && fin.get()!='\n');
820 if (!fin)
821 {
822 cout << "File '" << fname << "' not found!" << endl;
823 return;
824 }
825
826 while (1)
827 {
828 Set set;
829
830 fin >> set; // Read data from file [deg], it is stored in [rad]
831 if (!fin)
832 break;
833
834 fOriginal.Add(new Set(set));
835 }
836
837 cout << "Found " << fOriginal.GetSize()-size << " sets of coordinates ";
838 cout << "(Total=" << fOriginal.GetSize() << ")" << endl;
839 }
840
841 // --------------------------------------------------------------------------
842 //
843 // Opens an open dialog
844 //
845 TString OpenDialog(EFileDialogMode mode=kFDOpen)
846 {
847 static const char *gOpenTypes[] =
848 {
849 "TPoint files", "*.txt",
850 "All files", "*",
851 NULL, NULL
852 };
853
854 static TString dir(".");
855
856 TGFileInfo fi; // fFileName and fIniDir deleted in ~TGFileInfo
857
858 fi.fFileTypes = (const char**)gOpenTypes;
859 fi.fIniDir = StrDup(dir);
860
861 new TGFileDialog(fClient->GetRoot(), this, mode, &fi);
862
863 if (!fi.fFilename)
864 return 0;
865
866 dir = fi.fIniDir;
867
868 return fi.fFilename;
869 }
870
871 Bool_t ProcessMessage(Long_t msg, Long_t mp1, Long_t mp2)
872 {
873 // cout << "Msg: " << hex << GET_MSG(msg) << endl;
874 // cout << "SubMsg: " << hex << GET_SUBMSG(msg) << dec << endl;
875 switch (GET_MSG(msg))
876 {
877 case kC_COMMAND:
878 switch (GET_SUBMSG(msg))
879 {
880 case kCM_BUTTON:
881 switch (mp1)
882 {
883 case kTbFit:
884 {
885 Double_t before=0;
886 Double_t after=0;
887 Double_t backw=0;
888 Fit(before, after, backw);
889 DisplayBending();
890 DisplayResult(before, after, backw);
891 }
892 return kTRUE;
893 case kTbLoad:
894 fBending.Load(OpenDialog());
895 DisplayBending();
896 return kTRUE;
897 case kTbSave:
898 fBending.Save(OpenDialog(kFDSave));
899 return kTRUE;
900 case kTbLoadStars:
901 LoadStars(OpenDialog());
902 DisplayData();
903 return kTRUE;
904 case kTbReset:
905 fBending.Clear();
906 DisplayBending();
907 return kTRUE;
908 case kTbResetStars:
909 fOriginal.Delete();
910 DisplayData();
911 return kTRUE;
912 }
913 return kTRUE;
914 }
915 return kTRUE;
916 }
917 return kTRUE;
918 }
919
920 void AddTextButton(TGCompositeFrame *f, TString txt, Int_t id=-1, TGLayoutHints *h=0)
921 {
922 TGButton *but = new TGTextButton(f, txt, id);
923 but->Associate(this);
924 f->AddFrame(but, h);
925 fList->Add(but);
926
927 }
928
929 void AddCheckButton(TGCompositeFrame *f, TString txt, Int_t id=-1, TGLayoutHints *h=0)
930 {
931 TGButton *but = new TGCheckButton(f, txt, id);
932 but->Associate(this);
933 f->AddFrame(but, h);
934 fList->Add(but);
935 }
936
937 TGLabel *AddLabel(TGCompositeFrame *f, TString txt, TGLayoutHints *h=0)
938 {
939 TGLabel *l = new TGLabel(f, txt/*, TGLabel::GetDefaultGC()(), fFont*/);
940 f->AddFrame(l, h);
941 fList->Add(l);
942 fLabel.Add(l);
943 return l;
944 }
945
946 void DisplayBending()
947 {
948 TArrayD par, err;
949 fBending.GetParameters(par);
950 fBending.GetError(err);
951
952 TGLabel *l;
953
954 for (int i=0; i<MBending::GetNumPar(); i++)
955 {
956 l = (TGLabel*)fLabel.At(i);
957 l->SetText(Form("%.4f\xb0", par[i]));
958
959 l = (TGLabel*)fLabel.At(MBending::GetNumPar()+i);
960 l->SetText(Form("\xb1 %8.4f\xb0", err[i]));
961 }
962 }
963
964 void DisplayData()
965 {
966 TGLabel *l = (TGLabel*)fLabel.At(3*MBending::GetNumPar());
967 l->SetText(Form("%d data sets loaded.", fOriginal.GetSize()));
968 }
969
970 void DisplayResult(Double_t before, Double_t after, Double_t backw)
971 {
972 TGLabel *l1 = (TGLabel*)fLabel.At(3*MBending::GetNumPar()+1);
973 l1->SetText(Form("Before: %.1f +- %.1f SE", before, 0));
974
975 TGLabel *l2 = (TGLabel*)fLabel.At(3*MBending::GetNumPar()+2);
976 l2->SetText(Form("After: %.1f +- %.1f SE", after, 0));
977
978 TGLabel *l3 = (TGLabel*)fLabel.At(3*MBending::GetNumPar()+3);
979 l3->SetText(Form("Backw: %.1f +- %.1f SE", backw, 0));
980 }
981
982public:
983 ~MFit()
984 {
985 if (fFont)
986 gVirtualX->DeleteFont(fFont);
987 }
988 MFit() : TGMainFrame(gClient->GetRoot(), 750, 370, kHorizontalFrame)
989 {
990 fCoordinates.SetOwner();
991 fOriginal.SetOwner();
992
993 fList = new MGList;
994 fList->SetOwner();
995
996 fFont = gVirtualX->LoadQueryFont("7x13bold");
997
998 TGLayoutHints *hints0 = new TGLayoutHints(kLHintsExpandY, 7, 5, 5, 6);
999 TGLayoutHints *hints1 = new TGLayoutHints(kLHintsExpandX|kLHintsExpandY, 5, 7, 5, 6);
1000 TGLayoutHints *hints2 = new TGLayoutHints(kLHintsCenterX|kLHintsCenterY, 5, 5, 5, 5);
1001 fList->Add(hints0);
1002 fList->Add(hints1);
1003 fList->Add(hints2);
1004
1005 TGGroupFrame *grp1 = new TGGroupFrame(this, "Control", kVerticalFrame);
1006 AddFrame(grp1, hints0);
1007 fList->Add(grp1);
1008
1009 TGGroupFrame *grp2 = new TGGroupFrame(this, "Parameters", kHorizontalFrame);
1010 AddFrame(grp2, hints1);
1011 fList->Add(grp2);
1012
1013
1014
1015 TGLayoutHints *hints4 = new TGLayoutHints(kLHintsExpandX, 5, 5, 5);
1016 TGLayoutHints *hints5 = new TGLayoutHints(kLHintsExpandX, 5, 5, 15);
1017 AddTextButton(grp1, "Load Pointing Model", kTbLoad, hints5);
1018 AddTextButton(grp1, "Save Pointing Model", kTbSave, hints4);
1019 AddTextButton(grp1, "Fit Parameters", kTbFit, hints5);
1020 AddTextButton(grp1, "Reset Parameters", kTbReset, hints4);
1021 AddTextButton(grp1, "Load Stars", kTbLoadStars, hints5);
1022 AddTextButton(grp1, "Reset Stars", kTbResetStars, hints4);
1023 fList->Add(hints4);
1024 fList->Add(hints5);
1025
1026
1027 TGHorizontalFrame *comp = new TGHorizontalFrame(grp2, 1, 1);
1028 grp2->AddFrame(comp);
1029 fList->Add(comp);
1030
1031 TGLayoutHints *hints3 = new TGLayoutHints(kLHintsLeft|kLHintsTop, 0, 20, 5, 0);
1032 fList->Add(hints3);
1033
1034 TGLabel *l;
1035
1036 TGVerticalFrame *vframe = new TGVerticalFrame(comp, 1, 1);
1037 comp->AddFrame(vframe, hints3);
1038 fList->Add(vframe);
1039
1040 for (int i=0; i<MBending::GetNumPar(); i++)
1041 AddCheckButton(vframe, fBending.GetName(i), i);
1042
1043 vframe = new TGVerticalFrame(comp, 1, 1);
1044 comp->AddFrame(vframe, hints3);
1045 fList->Add(vframe);
1046
1047 l = new TGLabel(vframe, "+000.0000");
1048 l->SetTextJustify(kTextRight);
1049 fList->Add(l);
1050 fLabel.Add(l);
1051
1052 TGButton *but = (TGButton*)fList->FindWidget(0);
1053
1054 TGLayoutHints *h = new TGLayoutHints(kLHintsCenterY, 0, 0, but->GetHeight()-l->GetHeight());
1055 fList->Add(h);
1056
1057 vframe->AddFrame(l,h);
1058
1059 for (int i=1; i<MBending::GetNumPar(); i++)
1060 AddLabel(vframe, "+000.0000", h)->SetTextJustify(kTextRight);
1061
1062 vframe = new TGVerticalFrame(comp, 1, 1);
1063 comp->AddFrame(vframe, hints3);
1064 fList->Add(vframe);
1065
1066 for (int i=0; i<MBending::GetNumPar(); i++)
1067 AddLabel(vframe, "\xb1 00.0000\xb0", h)->SetTextJustify(kTextRight);
1068
1069 vframe = new TGVerticalFrame(comp, 1, 1);
1070 comp->AddFrame(vframe, hints3);
1071 fList->Add(vframe);
1072
1073 for (int i=0; i<MBending::GetNumPar(); i++)
1074 AddLabel(vframe, fBending.GetDescription(i), h);
1075
1076 l = new TGLabel(grp1, "0000000 Data Sets loaded.");
1077 grp1->AddFrame(l, hints5);
1078 fList->Add(l);
1079 fLabel.Add(l);
1080
1081 l = new TGLabel(grp1, "");
1082 l->SetTextJustify(kTextLeft);
1083 grp1->AddFrame(l, hints5);
1084 fList->Add(l);
1085 fLabel.Add(l);
1086
1087 l = new TGLabel(grp1, "");
1088 l->SetTextJustify(kTextLeft);
1089 grp1->AddFrame(l, hints5);
1090 fList->Add(l);
1091 fLabel.Add(l);
1092
1093 l = new TGLabel(grp1, "");
1094 l->SetTextJustify(kTextLeft);
1095 grp1->AddFrame(l, hints5);
1096 fList->Add(l);
1097 fLabel.Add(l);
1098
1099
1100 ((TGCheckButton*)fList->FindWidget(0))->SetState(kButtonDown);
1101 ((TGCheckButton*)fList->FindWidget(1))->SetState(kButtonDown);
1102 ((TGCheckButton*)fList->FindWidget(6))->SetState(kButtonDown);
1103 ((TGCheckButton*)fList->FindWidget(10))->SetState(kButtonDown);
1104 ((TGCheckButton*)fList->FindWidget(12))->SetState(kButtonDown);
1105 ((TGCheckButton*)fList->FindWidget(17))->SetState(kButtonDown);
1106
1107 SetWindowName("TPoint Fitting Window");
1108 SetIconName("TPoint++");
1109
1110 Layout();
1111
1112 MapSubwindows();
1113 MapWindow();
1114
1115 DisplayBending();
1116 DisplayData();
1117 }
1118 ClassDef(MFit, 0)
1119};
1120
1121ClassImp(MFit);
1122
1123void gui()
1124{
1125 gErrorIgnoreLevel = kError;
1126 new MFit;
1127 // TF1 f1("f1", "[0]/cos((90-x)*3.1415/180)", 0, 90)
1128}
Note: See TracBrowser for help on using the repository browser.