source: trunk/MagicSoft/Mars/mastro/MAstroCatalog.cc@ 4643

Last change on this file since 4643 was 4521, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 39.3 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without expressed
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 03/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Author(s): Robert Wagner, 08/2004 <mailto:rwagner@mppmu.mpg.de>
20!
21! Copyright: MAGIC Software Development, 2002-2004
22!
23!
24\* ======================================================================== */
25
26//////////////////////////////////////////////////////////////////////////////
27//
28// MAstroCatalog
29// =============
30//
31// THIS IMPLEMENTATION IS PRELIMINARY AND WILL BE MERGED WITH
32// SOME PARTS OF THE DRIVE SOFTWARE SOON!
33//
34//
35// Catalogs:
36// ---------
37//
38// To be able to use this class you need a catalog file suppored by
39// MAstroCatalog.
40// Catalog files can be found at
41// http://magic.astro.uni-wuerzburg.de/mars/catalogs.html
42// You must copy the file into the directory from which you start your macro
43// or give an abolute path loading the catalog.
44//
45//
46// Usage:
47// ------
48//
49// To display a starfield you must have a supported catalog, then do:
50//
51// MTime time;
52// // Time for which to get the picture
53// time.Set(2004, 2, 28, 20, 14, 7);
54// // Current observatory
55// MObservatory magic1;
56// // Right Ascension [h] and declination [deg] of source
57// // Currently 'perfect' pointing is assumed
58// const Double_t ra = MAstro::Hms2Rad(5, 34, 31.9);
59// const Double_t dec = MAstro::Dms2Rad(22, 0, 52.0);
60// MAstroCatalog stars;
61// // Magnitude up to which the stars are loaded from the catalog
62// stars.SetLimMag(6);
63// // Radius of FOV around the source position to load the stars
64// stars.SetRadiusFOV(3);
65// // Source position
66// stars.SetRaDec(ra, dec);
67// // Catalog to load (here: Bright Star Catalog V5)
68// stars.ReadBSC("bsc5.dat");
69// // Obersavatory and time to also get local coordinate information
70// stars.SetObservatory(magic1);
71// stars.SetTime(time);
72// // Enable interactive GUI
73// stars.SetGuiActive();
74// //Clone the catalog due to the validity range of the instance
75// TObject *o = stars.Clone();
76// o->SetBit(kCanDelete);
77// o->Draw();
78//
79// If no time and/or Obervatory location is given no local coordinate
80// information is displayed.
81//
82//
83// Coordinate Transformation:
84// -------------------------
85// The conversion from sky coordinates to local coordinates is done using
86// MAstroSky2Local which does a simple rotation of the coordinate system.
87// This is inaccurate in the order of 30arcsec due to ignorance of all
88// astrometrical corrections (nutation, precission, abberation, ...)
89//
90//
91// GUI:
92// ----
93// * If the gui is interactive you can use the cursor keys to change
94// the position you are looking at and with plus/minus you
95// can (un)zoom the FOV (Field Of View)
96// * The displayed values mean the following:
97// + alpha: Right Ascension
98// + delta: Declination
99// + theta: zenith distance / zenith angle
100// + phi: azimuth angle
101// + rho: angle of rotation sky-coordinate system vs local-
102// coordinate system
103// + time of display
104// * Move the mouse on top of the grid points or the stars to get
105// more setailed information.
106// * Enable the event-info in a canvas to see the current
107// ObjectInfo=tooltip-text
108// * call SetNoToolTips to supress the tooltips
109// * the blue lines are the local coordinat system
110// * the red lines are sky coordinate system
111//
112//
113// ToDo:
114// -----
115// - replace MVetcor3 by a more convinient class. Maybe use TExMap, too.
116// - change tooltips to multi-line tools tips as soon as root
117// supports them
118// - a derived class is missing which supports all astrometrical
119// correction (base on slalib and useable in Cosy)
120// - Implement a general loader for heasarc catlogs, see
121// http://heasarc.gsfc.nasa.gov/W3Browse/star-catalog/
122//
123//////////////////////////////////////////////////////////////////////////////
124#include "MAstroCatalog.h"
125
126#include <errno.h> // strerror
127#include <fstream> // ifstream
128#include <stdlib.h> // ati, atof
129#include <limits.h> // INT_MAX (Suse 7.3/gcc 2.95)
130
131#include <KeySymbols.h> // kKey_*
132
133#include <TLine.h> // TLine
134#include <TMarker.h> // TMarker
135#include <TCanvas.h> // TCanvas
136#include <TArrayI.h> // TArrayI
137#include <TGToolTip.h> // TGToolTip
138#include <TPaveText.h> // TPaveText
139
140#include "MLog.h"
141#include "MLogManip.h"
142
143#include "MTime.h"
144#include "MString.h"
145#include "MAstro.h"
146#include "MAstroSky2Local.h"
147#include "MObservatory.h"
148
149#undef DEBUG
150//#define DEBUG
151
152#ifdef DEBUG
153#include <TStopwatch.h>
154#endif
155
156ClassImp(MVector3);
157ClassImp(MAstroCatalog);
158
159using namespace std;
160
161void MVector3::WriteBinary(ostream &out) const
162{
163 const Double_t t = Theta();
164 const Double_t p = Phi();
165 const Float_t m = Mag();
166 out.write((char*)&t, 8);
167 out.write((char*)&p, 8);
168 out.write((char*)&m, 4);
169 out << fName << endl;
170}
171
172void MVector3::ReadBinary(istream &in)
173{
174 Double_t t, p;
175 Float_t m;
176
177 in.read((char*)&t, 8);
178 in.read((char*)&p, 8);
179 in.read((char*)&m, 4);
180 fName.ReadLine(in);
181
182 SetMagThetaPhi(m, t, p);
183}
184
185// --------------------------------------------------------------------------
186//
187// Default Constructor. Set Default values:
188// fLimMag = 99
189// fRadiusFOV = 90
190//
191MAstroCatalog::MAstroCatalog() : fLimMag(99), fRadiusFOV(90), fToolTip(0), fObservatory(0), fTime(0)
192{
193 fList.SetOwner();
194 fMapG.SetOwner();
195 fToolTip = gROOT->IsBatch() || !gClient ? 0 : new TGToolTip(0, "", 0);
196}
197
198// --------------------------------------------------------------------------
199//
200// Destructor. Delete fTime, fObservatory. Disconnect signal. delete tooltip.
201// Delete Map with gui primitives
202//
203MAstroCatalog::~MAstroCatalog()
204{
205 // First disconnect the EventInfo...
206 // FIXME: There must be an easier way!
207 TIter Next(gROOT->GetListOfCanvases());
208 TCanvas *c;
209 while ((c=(TCanvas*)Next()))
210 c->Disconnect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", this,
211 "EventInfo(Int_t,Int_t,Int_t,TObject*)");
212
213 // Now delete the data members
214 if (fTime)
215 delete fTime;
216 if (fObservatory)
217 delete fObservatory;
218
219 if (fToolTip)
220 {
221 fToolTip->Hide();
222 delete fToolTip;
223 }
224}
225
226// --------------------------------------------------------------------------
227//
228// Snippet to for reading catalog files.
229//
230TString MAstroCatalog::FindToken(TString &line, Char_t tok)
231{
232 Ssiz_t token = line.First(tok);
233 if (token<0)
234 {
235 const TString copy(line);
236 line = "";
237 return copy;
238 }
239
240 const TString res = line(0, token);
241 line.Remove(0, token+1);
242 return res;
243}
244
245// --------------------------------------------------------------------------
246//
247// return int correspoding to TSubString
248//
249Int_t MAstroCatalog::atoi(const TSubString &sub)
250{
251 return atoi(TString(sub));
252}
253
254// --------------------------------------------------------------------------
255//
256// return float correspoding to TSubString
257//
258Float_t MAstroCatalog::atof(const TSubString &sub)
259{
260 return atof(TString(sub));
261}
262
263// --------------------------------------------------------------------------
264//
265// return int correspoding to TString
266//
267Int_t MAstroCatalog::atoi(const TString &s)
268{
269 return std::atoi(s);
270}
271
272// --------------------------------------------------------------------------
273//
274// return float correspoding to TString
275//
276Float_t MAstroCatalog::atof(const TString &s)
277{
278 return std::atof(s);
279}
280
281// --------------------------------------------------------------------------
282//
283// Read from a xephem catalog, set bit kHasChahanged.
284// Already read data is not deleted. To delete the stored data call
285// Delete().
286//
287Int_t MAstroCatalog::ReadXephem(TString catalog)
288{
289 gLog << inf << "Reading Xephem catalog: " << catalog << endl;
290
291 ifstream fin(catalog);
292 if (!fin)
293 {
294 gLog << err << "Cannot open catalog file " << catalog << ": ";
295 gLog << strerror(errno) << endl;
296 return 0;
297 }
298
299 Int_t add =0;
300 Int_t cnt =0;
301 Int_t line=0;
302
303 Double_t maxmag=0;
304
305 while (1)
306 {
307 TString row;
308 row.ReadLine(fin);
309 if (!fin)
310 break;
311
312 line++;
313
314 if (row[0]=='#')
315 continue;
316
317 TString line(row);
318
319 TString name = FindToken(line);
320 TString dummy = FindToken(line);
321 TString r = FindToken(line);
322 TString d = FindToken(line);
323 TString m = FindToken(line);
324 TString epoch = FindToken(line);
325
326 if (name.IsNull() || r.IsNull() || d.IsNull() || m.IsNull() || epoch.IsNull())
327 {
328 gLog << warn << "Invalid Entry Line #" << line << ": " << row << endl;
329 continue;
330 }
331
332 cnt++;
333
334 const Double_t mag = atof(m);
335
336 maxmag = TMath::Max(maxmag, mag);
337
338 if (mag>fLimMag)
339 continue;
340
341 if (epoch!="2000")
342 {
343 gLog << warn << "Epoch != 2000... skipped." << endl;
344 continue;
345 }
346
347 Double_t ra0, dec0;
348 MAstro::Coordinate2Angle(r, ra0);
349 MAstro::Coordinate2Angle(d, dec0);
350
351 ra0 *= TMath::Pi()/12;
352 dec0 *= TMath::Pi()/180;
353
354 if (AddObject(ra0, dec0, mag, name))
355 add++;
356 }
357 gLog << inf << "Read " << add << " out of " << cnt << " (Total max mag=" << maxmag << ")" << endl;
358
359 return add;
360}
361
362// --------------------------------------------------------------------------
363//
364// Read from a NGC2000 catalog. set bit kHasChanged
365// Already read data is not deleted. To delete the stored data call
366// Delete().
367//
368Int_t MAstroCatalog::ReadNGC2000(TString catalog)
369{
370 gLog << inf << "Reading NGC2000 catalog: " << catalog << endl;
371
372 ifstream fin(catalog);
373 if (!fin)
374 {
375 gLog << err << "Cannot open catalog file " << catalog << ": ";
376 gLog << strerror(errno) << endl;
377 return 0;
378 }
379
380 Int_t add=0;
381 Int_t cnt=0;
382 Int_t n =0;
383
384 Double_t maxmag=0;
385
386 while (1)
387 {
388 TString row;
389 row.ReadLine(fin);
390 if (!fin)
391 break;
392
393 cnt++;
394
395 const Int_t rah = atoi(row(13, 2));
396 const Float_t ram = atof(row(16, 4));
397 const Char_t decs = row(22);
398 const Int_t decd = atoi(row(23, 2));
399 const Int_t decm = atoi(row(26, 2));
400 const TString m = row(43, 4);
401
402 if (m.Strip().IsNull())
403 continue;
404
405 n++;
406
407 const Double_t mag = atof(m);
408
409 maxmag = TMath::Max(maxmag, mag);
410
411 if (mag>fLimMag)
412 continue;
413
414 const Double_t ra = MAstro::Hms2Rad(rah, (int)ram, fmod(ram, 1)*60);
415 const Double_t dec = MAstro::Dms2Rad(decd, decm, 0, decs);
416
417 if (AddObject(ra, dec, mag, row(0,8)))
418 add++;
419 }
420
421 gLog << inf << "Read " << add << " out of " << n << " (Total max mag=" << maxmag << ")" << endl;
422
423 return add;
424}
425
426// --------------------------------------------------------------------------
427//
428// Read from a Bright Star Catalog catalog. set bit kHasChanged
429// Already read data is not deleted. To delete the stored data call
430// Delete().
431//
432Int_t MAstroCatalog::ReadBSC(TString catalog)
433{
434 gLog << inf << "Reading Bright Star Catalog (BSC5) catalog: " << catalog << endl;
435
436 ifstream fin(catalog);
437 if (!fin)
438 {
439 gLog << err << "Cannot open catalog file " << catalog << ": ";
440 gLog << strerror(errno) << endl;
441 return 0;
442 }
443
444 Int_t add=0;
445 Int_t cnt=0;
446 Int_t n =0;
447
448 Double_t maxmag=0;
449
450 while (1)
451 {
452 TString row;
453 row.ReadLine(fin);
454 if (!fin)
455 break;
456
457 cnt++;
458
459 const Int_t rah = atoi(row(75, 2));
460 const Int_t ram = atoi(row(77, 2));
461 const Float_t ras = atof(row(79, 4));
462 const Char_t decsgn = row(83);
463 const Int_t decd = atoi(row(84, 2));
464 const Int_t decm = atoi(row(86, 2));
465 const Int_t decs = atoi(row(88, 2));
466 const TString m = row(102, 5);
467
468 if (m.Strip().IsNull())
469 continue;
470
471 n++;
472
473 const Double_t mag = atof(m.Data());
474
475 maxmag = TMath::Max(maxmag, mag);
476
477 if (mag>fLimMag)
478 continue;
479
480 const Double_t ra = MAstro::Hms2Rad(rah, ram, ras);
481 const Double_t dec = MAstro::Dms2Rad(decd, decm, decs, decsgn);
482
483 if (AddObject(ra, dec, mag, row(4,9)))
484 add++;
485 }
486
487 gLog << inf << "Read " << add << " out of " << n << " (Total max mag=" << maxmag << ")" << endl;
488
489 return add;
490}
491
492// --------------------------------------------------------------------------
493//
494// Read from a ascii heasarc ppm catalog. set bit kHasChanged
495// Already read data is not deleted. To delete the stored data call
496// Delete().
497// If the second argument is given all survived stars are written
498// to a file outname. This files will contain an apropriate compressed
499// file format. You can read such files again using ReadCompressed.
500//
501// FIXME: A General loader for heasarc catlogs is missing, see
502// http://heasarc.gsfc.nasa.gov/W3Browse/star-catalog/
503//
504Int_t MAstroCatalog::ReadHeasarcPPM(TString catalog, TString outname)
505{
506 gLog << inf << "Reading Heasarc PPM catalog: " << catalog << endl;
507
508 ifstream fin(catalog);
509 if (!fin)
510 {
511 gLog << err << "Cannot open catalog file " << catalog << ": ";
512 gLog << strerror(errno) << endl;
513 return 0;
514 }
515
516 ofstream *fout = outname.IsNull() ? 0 : new ofstream(outname);
517 if (fout && !*fout)
518 {
519 gLog << warn << "Cannot open output file " << outname << ": ";
520 gLog << strerror(errno) << endl;
521 delete fout;
522 fout = 0;
523 }
524
525 Int_t add=0;
526 Int_t cnt=0;
527 Int_t n =0;
528
529 Double_t maxmag=0;
530
531 while (1)
532 {
533 TString row;
534 row.ReadLine(fin);
535 if (!fin)
536 break;
537
538 cnt++;
539
540 if (!row.BeginsWith("PPM "))
541 continue;
542
543 const TString name = row(0, row.First('|'));
544 row = row(row.First('|')+1, row.Length());
545 row = row(row.First('|')+1, row.Length());
546
547 const TString vmag = row(0, row.First('|'));
548
549 n++;
550 const Double_t mag = atof(vmag.Data());
551 maxmag = TMath::Max(maxmag, mag);
552 if (mag>fLimMag)
553 continue;
554
555 row = row(row.First('|')+1, row.Length());
556 row = row(row.First('|')+1, row.Length());
557
558 row = row(row.First('|')+1, row.Length());
559 row = row(row.First('|')+1, row.Length());
560
561 row = row(row.First('|')+1, row.Length());
562 row = row(row.First('|')+1, row.Length());
563
564 const TString ra = row(0, row.First('|'));
565 row = row(row.First('|')+1, row.Length());
566 const TString de = row(0, row.First('|'));
567 row = row(row.First('|')+1, row.Length());
568
569 Char_t sgn;
570 Int_t d, m;
571 Float_t s;
572 if (sscanf(ra.Data(), "%d %d %f", &d, &m, &s)!=3)
573 {
574 // gLog << "Error loading entry in line " << i << endl;
575 continue;
576 }
577 const Double_t ra0 = MAstro::Hms2Rad(d, m, s);
578
579 if (sscanf(de.Data(), "%c%d %d %f", &sgn, &d, &m, &s)!=4)
580 {
581 // gLog << "Error loading entry in line " << i << endl;
582 continue;
583 }
584 const Double_t de0 = MAstro::Dms2Rad(d, m, s, sgn);
585
586 if (!AddObject(ra0, de0, mag, name))
587 continue;
588
589 add++;
590
591 if (fout)
592 ((MVector3*)fList.Last())->WriteBinary(*fout);
593 }
594
595 gLog << inf << "Read " << add << " out of " << n << " (Total max mag=" << maxmag << ")" << endl;
596
597 return add;
598}
599
600// --------------------------------------------------------------------------
601//
602// Read from a MAstroCatalog compressed catalog. set bit kHasChanged
603// Already read data is not deleted. To delete the stored data call
604// Delete().
605//
606Int_t MAstroCatalog::ReadCompressed(TString catalog)
607{
608 SetBit(kHasChanged);
609
610 gLog << inf << "Reading MAstroCatalog compressed catalog: " << catalog << endl;
611
612 ifstream fin(catalog);
613 if (!fin)
614 {
615 gLog << err << "Cannot open catalog file " << catalog << ": ";
616 gLog << strerror(errno) << endl;
617 return 0;
618 }
619
620 Int_t add=0;
621 Int_t cnt=0;
622 Int_t n =0;
623
624 Double_t maxmag=0;
625
626 MVector3 entry;
627
628 while (1)
629 {
630 cnt++;
631
632 entry.ReadBinary(fin);
633 if (!fin)
634 break;
635
636 n++;
637
638 const Double_t mag = entry.Magnitude();
639 maxmag = TMath::Max(maxmag, mag);
640 if (mag>fLimMag)
641 continue;
642
643 if (entry.Angle(fRaDec)*TMath::RadToDeg()>fRadiusFOV)
644 continue;
645
646 fList.Add(entry.Clone());
647 add++;
648 }
649
650 gLog << inf << "Read " << add << " out of " << n << " (Total max mag=" << maxmag << ")" << endl;
651
652 return add;
653}
654
655// --------------------------------------------------------------------------
656//
657// Add an object to the star catalog manually. Return true if the object
658// was added and false otherwise (criteria is the FOV)
659//
660Bool_t MAstroCatalog::AddObject(Float_t ra, Float_t dec, Float_t mag, TString name)
661{
662 MVector3 *star = new MVector3;
663 star->SetRaDec(ra, dec, mag);
664 star->SetName(name);
665
666 if (star->Angle(fRaDec)*TMath::RadToDeg()>fRadiusFOV)
667 {
668 delete star;
669 return 0;
670 }
671
672 SetBit(kHasChanged);
673 fList.AddLast(star);
674 return 1;
675}
676
677// --------------------------------------------------------------------------
678//
679// Set Range of pad. If something has changed create and draw new primitives.
680// Paint all gui primitives.
681//
682void MAstroCatalog::Paint(Option_t *o)
683{
684 SetRangePad(o);
685
686 if (TestBit(kHasChanged))
687 DrawPrimitives(o);
688
689 fMapG.Paint();
690}
691
692// --------------------------------------------------------------------------
693//
694// Set Range of pad if pad available. If something has changed create
695// and draw new primitives. Paint all gui primitives to the Drawable with
696// Id id. This can be used to be able to
697//
698/*
699void MAstroCatalog::PaintImg(Int_t id, Option_t *o)
700{
701 if (gPad)
702 SetRangePad(o);
703
704 if (TestBit(kHasChanged))
705 {
706 if (id>0)
707 gPad=0;
708 DrawPrimitives(o);
709 }
710
711 fMapG.Paint(id, fRadiusFOV);
712}
713*/
714
715// --------------------------------------------------------------------------
716//
717// Set Range of pad. If something has changed create and draw new primitives.
718// Paint all gui primitives.
719//
720// Because in some kind of multi-threaded environments gPad doesn't stay
721// the same in a single thread (because it might be changed in the same
722// thread inside a gui updating timer for example) we have to secure the
723// usage of gPad with a bit. This is also not multi-thread safe against
724// calling this function, but the function should work well in multi-
725// threaded environments. Never call this function from different threads
726// simultaneously.
727//
728void MAstroCatalog::PaintImg(unsigned char *buf, int w, int h, Option_t *o)
729{
730 if (!o)
731 o = "local mirrorx yellow * =";
732
733 if (TestBit(kHasChanged))
734 {
735 SetBit(kDrawingImage);
736 DrawPrimitives(o);
737 ResetBit(kDrawingImage);
738 }
739
740 fMapG.Paint(buf, w, h, fRadiusFOV);
741}
742
743// --------------------------------------------------------------------------
744//
745// Draw a black marker at the position of the star. Create a corresponding
746// tooltip with the coordinates.
747// x, y: Pad Coordinates to draw star
748// v: Sky position (Ra/Dec) of the star
749// col: Color of marker (<0 mean transparent)
750// txt: additional tooltip text
751// resize: means resize the marker according to the magnitude
752//
753void MAstroCatalog::DrawStar(Double_t x, Double_t y, const TVector3 &v, Int_t col, const char *txt, Bool_t resize)
754{
755 const Double_t ra = v.Phi()*TMath::RadToDeg()/15;
756 const Double_t dec = (TMath::Pi()/2-v.Theta())*TMath::RadToDeg();
757
758 const Double_t mag = -2.5*log10(v.Mag());
759
760 MString str;
761 str.Print("%s: Ra=%.2fh Dec=%.1fd Mag=%.1f", v.GetName(), ra, dec, mag);
762 if (txt)
763 {
764 str += " (";
765 str += txt;
766 str += ")";
767 }
768
769 // draw star on the camera display
770 TMarker *tip=new TMarker(x, y, col<0 ? kDot : kFullDotMedium);;
771 tip->SetMarkerColor(col);
772 fMapG.Add(tip, new TString(str));
773
774 if (!resize)
775 return;
776
777 tip->SetMarkerSize((10 - (mag>1 ? mag : 1))/15);
778 tip->SetMarkerStyle(kCircle);
779}
780
781// --------------------------------------------------------------------------
782//
783// Set pad as modified.
784//
785void MAstroCatalog::Update(Bool_t upd)
786{
787 SetBit(kHasChanged);
788 if (gPad && TestBit(kMustCleanup))
789 {
790 gPad->Modified();
791 if (upd)
792 gPad->Update();
793 }
794}
795
796// --------------------------------------------------------------------------
797//
798// Set the observation time. Necessary to use local coordinate
799// system. The MTime object is cloned.
800//
801void MAstroCatalog::SetTime(const MTime &time)
802{
803 if (fTime)
804 delete fTime;
805 fTime=(MTime*)time.Clone();
806}
807
808// --------------------------------------------------------------------------
809//
810// Set the observatory location. Necessary to use local coordinate
811// system. The MObservatory object is cloned.
812//
813void MAstroCatalog::SetObservatory(const MObservatory &obs)
814{
815 if (fObservatory)
816 delete fObservatory;
817 fObservatory=new MObservatory;
818 obs.Copy(*fObservatory);
819}
820
821// --------------------------------------------------------------------------
822//
823// Convert the vector to pad coordinates. After conversion
824// the x- coordinate of the vector must be the x coordinate
825// of the pad - the same for y. If the coordinate is inside
826// the current draw area return kTRUE, otherwise kFALSE.
827// If it is an invalid coordinate return kERROR
828//
829Int_t MAstroCatalog::ConvertToPad(const TVector3 &w0, TVector2 &v) const
830{
831 TVector3 w(w0);
832
833 // Stretch such, that the Z-component is alwas the same. Now
834 // X and Y contains the intersection point between the star-light
835 // and the plain of a virtual plain screen (ccd...)
836 if (TestBit(kPlainScreen))
837 w *= 1./w(2);
838
839 w *= TMath::RadToDeg(); // FIXME: *conversion factor?
840 v.Set(TestBit(kMirrorX) ? -w(0) : w(0),
841 TestBit(kMirrorY) ? -w(1) : w(1));
842
843 if (w(2)<0)
844 return kERROR;
845
846 if (TestBit(kDrawingImage) || !gPad)
847 return v.Mod2()<fRadiusFOV*fRadiusFOV;
848
849 return v.X()>gPad->GetX1() && v.Y()>gPad->GetY1() &&
850 v.X()<gPad->GetX2() && v.Y()<gPad->GetY2();
851}
852
853// --------------------------------------------------------------------------
854//
855// Convert theta/phi coordinates of v by TRotation into new coordinate
856// system and convert the coordinated to pad by ConvertToPad.
857// The result is retunred in v.
858//
859Int_t MAstroCatalog::Convert(const TRotation &rot, TVector2 &v) const
860{
861 MVector3 w;
862 w.SetMagThetaPhi(1, v.Y(), v.X());
863 w *= rot;
864
865 return ConvertToPad(w, v);
866}
867
868// --------------------------------------------------------------------------
869//
870// Draw a line from v to v+(dx,dy) using Convert/ConvertToPad to get the
871// corresponding pad coordinates.
872//
873Bool_t MAstroCatalog::DrawLine(const TVector2 &v, Int_t dx, Int_t dy, const TRotation &rot, Int_t type)
874{
875 const TVector2 add(dx*TMath::DegToRad(), dy*TMath::DegToRad());
876
877 // Define all lines in the same direction
878 const TVector2 va(dy==1?v:v+add);
879 const TVector2 vb(dy==1?v+add:v);
880
881 TVector2 v0(va);
882 TVector2 v1(vb);
883
884 const Int_t rc0 = Convert(rot, v0);
885 const Int_t rc1 = Convert(rot, v1);
886
887 // Both are kFALSE or both are kERROR
888 if ((rc0|rc1)==kFALSE || (rc0&rc1)==kERROR)
889 return kFALSE;
890
891 TLine *line = new TLine(v0.X(), v0.Y(), v1.X(), v1.Y());
892 line->SetLineStyle(kDashDotted); //kDashed, kDotted, kDashDotted
893 line->SetLineColor(kWhite+type*2);
894 fMapG.Add(line);
895
896 if (dx!=0)
897 return kTRUE;
898
899 const TVector2 deg = va*TMath::RadToDeg();
900
901 MString txt;
902 if (type==1)
903 txt.Print("Ra=%.2fh Dec=%.1fd", fmod(deg.X()/15+48, 24), fmod(90-deg.Y()+270,180)-90);
904 else
905 txt.Print("Zd=%.1fd Az=%.1fd", fmod(deg.Y()+270,180)-90, fmod(deg.X()+720, 360));
906
907 TMarker *tip=new TMarker(v0.X(), v0.Y(), kDot);
908 tip->SetMarkerColor(kWhite+type*2);
909 fMapG.Add(tip, new TString(txt));
910
911 return kTRUE;
912}
913
914// --------------------------------------------------------------------------
915//
916// Use "local" draw option to align the display to the local
917// coordinate system instead of the sky coordinate system.
918// dx, dy are arrays storing recuresively all touched points
919// stepx, stepy are the step-size of the current grid.
920//
921void MAstroCatalog::Draw(const TVector2 &v0, const TRotation &rot, TArrayI &dx, TArrayI &dy, Int_t stepx, Int_t stepy, Int_t type)
922{
923 // Calculate the end point
924 const TVector2 v1 = v0 + TVector2(dx[0]*TMath::DegToRad(), dy[0]*TMath::DegToRad());
925
926 // Check whether the point has already been touched.
927 Int_t idx[] = {1, 1, 1, 1};
928
929 Int_t dirs[4][2] = { {0, stepy}, {stepx, 0}, {0, -stepy}, {-stepx, 0} };
930
931 // Check for ambiguities.
932 for (int i=0; i<dx.GetSize(); i++)
933 {
934 for (int j=0; j<4; j++)
935 {
936 const Bool_t rcx0 = (dx[i]+720)%360==(dx[0]+dirs[j][0]+720)%360;
937 const Bool_t rcy0 = (dy[i]+360)%180==(dy[0]+dirs[j][1]+360)%180;
938 if (rcx0&&rcy0)
939 idx[j] = 0;
940 }
941 }
942
943 // Enhance size of array by 1, copy current
944 // position as last entry
945 dx.Set(dx.GetSize()+1);
946 dy.Set(dy.GetSize()+1);
947
948 dx[dx.GetSize()-1] = dx[0];
949 dy[dy.GetSize()-1] = dy[0];
950
951 // Store current positon
952 const Int_t d[2] = { dx[0], dy[0] };
953
954 for (int i=0; i<4; i++)
955 if (idx[i])
956 {
957 // Calculate new position
958 dx[0] = d[0]+dirs[i][0];
959 dy[0] = d[1]+dirs[i][1];
960
961 // Draw corresponding line and iterate through grid
962 if (DrawLine(v1, dirs[i][0], dirs[i][1], rot, type))
963 Draw(v0, rot, dx, dy, stepx, stepy, type);
964
965 dx[0]=d[0];
966 dy[0]=d[1];
967 }
968}
969
970// --------------------------------------------------------------------------
971//
972// Draw a grid recursively around the point v0 (either Ra/Dec or Zd/Az)
973// The points in the grid are converted by a TRotation and CovertToPad
974// to pad coordinates. The type arguemnts is neccessary to create the
975// correct tooltip (Ra/Dec, Zd/Az) at the grid-points.
976// From the pointing position the step-size of teh gris is caluclated.
977//
978void MAstroCatalog::DrawGrid(const TVector3 &v0, const TRotation &rot, Int_t type)
979{
980 TArrayI dx(1);
981 TArrayI dy(1);
982
983 // align to 1deg boundary
984 TVector2 v(v0.Phi()*TMath::RadToDeg(), v0.Theta()*TMath::RadToDeg());
985 v.Set((Float_t)TMath::Nint(v.X()), (Float_t)TMath::Nint(v.Y()));
986
987 // calculate stepsizes based on visible FOV
988 Int_t stepx = 1;
989
990 if (v.Y()<fRadiusFOV || v.Y()>180-fRadiusFOV)
991 stepx=36;
992 else
993 {
994 // This is a rough estimate how many degrees are visible
995 const Float_t m = log(fRadiusFOV/180.)/log(90./(fRadiusFOV+1)+1);
996 const Float_t t = log(180.)-m*log(fRadiusFOV);
997 const Float_t f = m*log(90-fabs(90-v.Y()))+t;
998 const Int_t nx = (Int_t)(exp(f)+0.5);
999 stepx = nx<4 ? 1 : nx/4;
1000 if (stepx>36)
1001 stepx=36;
1002 }
1003
1004 const Int_t ny = (Int_t)(fRadiusFOV+1);
1005 Int_t stepy = ny<4 ? 1 : ny/4;
1006
1007 // align stepsizes to be devisor or 180 and 90
1008 while (180%stepx)
1009 stepx++;
1010 while (90%stepy)
1011 stepy++;
1012
1013 // align to step-size boundary (search for the nearest one)
1014 Int_t dv = 1;
1015 while ((int)(v.X())%stepx)
1016 {
1017 v.Set(v.X()+dv, v.Y());
1018 dv = -TMath::Sign(TMath::Abs(dv)+1, dv);
1019 }
1020
1021 dv = 1;
1022 while ((int)(v.Y())%stepy)
1023 {
1024 v.Set(v.X(), v.Y()+dv);
1025 dv = -TMath::Sign(TMath::Abs(dv)+1, dv);
1026 }
1027
1028 // draw...
1029 v *= TMath::DegToRad();
1030
1031 Draw(v, rot, dx, dy, stepx, stepy, type);
1032}
1033
1034// --------------------------------------------------------------------------
1035//
1036// Get a rotation matrix which aligns the pointing position
1037// to the center of the x,y plain
1038//
1039TRotation MAstroCatalog::AlignCoordinates(const TVector3 &v) const
1040{
1041 TRotation trans;
1042 trans.RotateZ(-v.Phi());
1043 trans.RotateY(-v.Theta());
1044 trans.RotateZ(-TMath::Pi()/2);
1045 return trans;
1046}
1047
1048// --------------------------------------------------------------------------
1049//
1050// Return the rotation matrix which converts either sky or
1051// local coordinates to coordinates which pole is the current
1052// pointing direction.
1053//
1054TRotation MAstroCatalog::GetGrid(Bool_t local)
1055{
1056 const Bool_t enable = fTime && fObservatory;
1057
1058 // If sky coordinate view is requested get rotation matrix and
1059 // draw corresponding sky-grid and if possible local grid
1060 if (!local)
1061 {
1062 const TRotation trans(AlignCoordinates(fRaDec));
1063
1064 DrawGrid(fRaDec, trans, 1);
1065
1066 if (enable)
1067 {
1068 const MAstroSky2Local rot(*fTime, *fObservatory);
1069 DrawGrid(rot*fRaDec, trans*rot.Inverse(), 2);
1070 }
1071
1072 // Return the correct rotation matrix
1073 return trans;
1074 }
1075
1076 // If local coordinate view is requested get rotation matrix and
1077 // draw corresponding sky-grid and if possible local grid
1078 if (local && enable)
1079 {
1080 const MAstroSky2Local rot(*fTime, *fObservatory);
1081
1082 const TRotation trans(AlignCoordinates(rot*fRaDec));
1083
1084 DrawGrid(fRaDec, trans*rot, 1);
1085 DrawGrid(rot*fRaDec, trans, 2);
1086
1087 // Return the correct rotation matrix
1088 return trans*rot;
1089 }
1090
1091 return TRotation();
1092}
1093
1094// --------------------------------------------------------------------------
1095//
1096// Create the title for the pad.
1097//
1098TString MAstroCatalog::GetPadTitle() const
1099{
1100 const Double_t ra = fRaDec.Phi()*TMath::RadToDeg();
1101 const Double_t dec = (TMath::Pi()/2-fRaDec.Theta())*TMath::RadToDeg();
1102
1103 TString txt;
1104 txt += Form("\\alpha=%.2fh ", fmod(ra/15+48, 24));
1105 txt += Form("\\delta=%.1f\\circ ", fmod(dec+270,180)-90);
1106 txt += Form("/ FOV=%.1f\\circ", fRadiusFOV);
1107
1108 if (!fTime || !fObservatory)
1109 return txt;
1110
1111 const MAstroSky2Local rot(*fTime, *fObservatory);
1112 const TVector3 loc = rot*fRaDec;
1113
1114 const Double_t rho = rot.RotationAngle(fRaDec.Phi(), TMath::Pi()/2-fRaDec.Theta());
1115
1116 const Double_t zd = TMath::RadToDeg()*loc.Theta();
1117 const Double_t az = TMath::RadToDeg()*loc.Phi();
1118
1119 txt.Prepend("#splitline{");
1120 txt += Form(" \\theta=%.1f\\circ ", fmod(zd+270,180)-90);
1121 txt += Form("\\phi=%.1f\\circ ", fmod(az+720, 360));
1122 txt += Form(" / \\rho=%.1f\\circ", rho*TMath::RadToDeg());
1123 txt += "}{<";
1124 txt += fTime->GetSqlDateTime();
1125 txt += ">}";
1126 return txt;
1127}
1128
1129// --------------------------------------------------------------------------
1130//
1131// To overlay the catalog make sure, that in any case you are using
1132// the 'same' option.
1133//
1134// If you want to overlay this on top of any picture which is created
1135// by derotation of the camera plain you have to use the 'mirror' option
1136// the compensate the mirroring of the image in the camera plain.
1137//
1138// If you have already compensated this by x=-x and y=-y when creating
1139// the histogram you can simply overlay the catalog.
1140//
1141// To overlay the catalog on a 2D histogram the histogram must have
1142// units of degrees (which are plain, like you directly convert the
1143// camera units by multiplication to degrees)
1144//
1145// To be 100% exact you must use the option 'plain' which assumes a plain
1146// screen. This is not necessary for the MAGIC-camera because the
1147// difference between both is less than 1e-3.
1148//
1149// You should always be aware of the fact, that the shown stars and the
1150// displayed grid is the ideal case, like a reflection on a virtual
1151// perfectly aligned central mirror. In reality the star-positions are
1152// smeared to the edge of the camera the more the distance to the center
1153// is, such that the center of gravity of the light distribution might
1154// be more far away from the center than the display shows.
1155//
1156// If you want the stars to be displayed as circles with a size
1157// showing their magnitude use "*" as an option.
1158//
1159// Use 'white' to display white instead of black stars
1160// Use 'yellow' to display white instead of black stars
1161//
1162//
1163void MAstroCatalog::AddPrimitives(TString o)
1164{
1165 const Bool_t same = o.Contains("same", TString::kIgnoreCase);
1166 const Bool_t local = o.Contains("local", TString::kIgnoreCase);
1167 const Bool_t mirx = o.Contains("mirrorx", TString::kIgnoreCase);
1168 const Bool_t miry = o.Contains("mirrory", TString::kIgnoreCase);
1169 const Bool_t mirror = o.Contains("mirror", TString::kIgnoreCase) && !mirx && !miry;
1170 const Bool_t size = o.Contains("*", TString::kIgnoreCase);
1171 const Bool_t white = o.Contains("white", TString::kIgnoreCase);
1172 const Bool_t yellow = o.Contains("yellow", TString::kIgnoreCase) && !white;
1173
1174 // X is vice versa, because ra is defined anti-clockwise
1175 mirx || mirror ? ResetBit(kMirrorX) : SetBit(kMirrorX);
1176 miry || mirror ? SetBit(kMirrorY) : ResetBit(kMirrorY);
1177
1178 const TRotation rot(GetGrid(local));
1179
1180 TIter Next(&fList);
1181 MVector3 *v=0;
1182 while ((v=(MVector3*)Next()))
1183 {
1184 if (v->Magnitude()>fLimMag)
1185 continue;
1186
1187 TVector2 s(v->Phi(), v->Theta());
1188 if (Convert(rot, s)==kTRUE)
1189 DrawStar(s.X(), s.Y(), *v, yellow?kYellow:(white?kWhite:kBlack), 0, size);
1190 }
1191
1192 if (!same && !TestBit(kDrawingImage) && gPad)
1193 {
1194 TPaveText *pv = new TPaveText(0.01, 0.90, 0.63, 0.99, "brNDC");
1195 pv->AddText(GetPadTitle());
1196 fMapG.Add(pv);
1197 }
1198
1199 TMarker *mk=new TMarker(0, 0, kMultiply);
1200 mk->SetMarkerColor(white||yellow?kWhite:kBlack);
1201 mk->SetMarkerSize(1.5);
1202 fMapG.Add(mk);
1203}
1204
1205// --------------------------------------------------------------------------
1206//
1207// Do nothing if 'same' option given.
1208// Otherwise set pad-range such that x- and y- coordinates have the same
1209// step-size
1210//
1211void MAstroCatalog::SetRangePad(Option_t *o)
1212{
1213 if (TString(o).Contains("same", TString::kIgnoreCase))
1214 return;
1215
1216 const Double_t edge = fRadiusFOV/TMath::Sqrt(2.);
1217 //gPad->Range(-edge, -edge, edge, edge);
1218
1219 const Float_t w = gPad->GetWw();
1220 const Float_t h = gPad->GetWh();
1221
1222 if (w<h)
1223 gPad->Range(-edge, -edge*h/w, edge, edge*h/w);
1224 else
1225 gPad->Range(-edge*w/h, -edge, edge*w/h, edge);
1226}
1227
1228// --------------------------------------------------------------------------
1229//
1230// First delete all gui elements.
1231// Set the correct range of the pad.
1232// Create all gui primitives
1233// If "this" is not in the current pad add it to the current pad.
1234// Reset vit kHasChanged
1235//
1236void MAstroCatalog::DrawPrimitives(Option_t *o)
1237{
1238 fMapG.Delete();
1239
1240 if (!TestBit(kDrawingImage) && gPad)
1241 SetRangePad(o);
1242
1243#ifdef DEBUG
1244 TStopwatch clk;
1245 clk.Start();
1246#endif
1247 AddPrimitives(o);
1248#ifdef DEBUG
1249 clk.Stop();
1250 clk.Print();
1251#endif
1252
1253 // Append to a possible second pad
1254 if (!TestBit(kDrawingImage) && gPad && !gPad->GetListOfPrimitives()->FindObject(this))
1255 AppendPad(o);
1256
1257 ResetBit(kHasChanged);
1258}
1259
1260// --------------------------------------------------------------------------
1261//
1262// Append "this" to current pad
1263// set bit kHasChanged to recreate all gui elements
1264// Connect signal
1265//
1266void MAstroCatalog::Draw(Option_t *o)
1267{
1268 // Append to first pad
1269 AppendPad(o);
1270
1271 // If contents have not previously changed make sure that
1272 // all primitives are recreated.
1273 SetBit(kHasChanged);
1274
1275 // Connect all TCanvas::ProcessedEvent to this->EventInfo
1276 // This means, that after TCanvas has processed an event
1277 // EventInfo of this class is called, see TCanvas::HandleInput
1278 gPad->GetCanvas()->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)",
1279 "MAstroCatalog", this,
1280 "EventInfo(Int_t,Int_t,Int_t,TObject*)");
1281}
1282
1283// --------------------------------------------------------------------------
1284//
1285// This function was connected to all created canvases. It is used
1286// to redirect GetObjectInfo into our own status bar.
1287//
1288// The 'connection' is done in Draw. It seems that 'connected'
1289// functions must be public.
1290//
1291void MAstroCatalog::EventInfo(Int_t event, Int_t px, Int_t py, TObject *selected)
1292{
1293 TCanvas *c = (TCanvas*)gTQSender;
1294
1295 gPad = c ? c->GetSelectedPad() : NULL;
1296 if (!gPad)
1297 return;
1298
1299
1300 // Try to find a corresponding object with kCannotPick set and
1301 // an available TString (for a tool tip)
1302 TString str;
1303 if (!selected || selected==this)
1304 selected = fMapG.PickObject(px, py, str);
1305
1306 if (!selected)
1307 return;
1308
1309 // Handle some gui events
1310 switch (event)
1311 {
1312 case kMouseMotion:
1313 if (fToolTip && !fToolTip->IsMapped() && !str.IsNull())
1314 ShowToolTip(px, py, str);
1315 break;
1316
1317 case kMouseLeave:
1318 if (fToolTip && fToolTip->IsMapped())
1319 fToolTip->Hide();
1320 break;
1321
1322 case kKeyPress:
1323 ExecuteEvent(kKeyPress, px, py);
1324 break;
1325 }
1326}
1327
1328// --------------------------------------------------------------------------
1329//
1330// Handle keyboard events.
1331//
1332void MAstroCatalog::ExecuteEventKbd(Int_t keycode, Int_t keysym)
1333{
1334 Double_t dra =0;
1335 Double_t ddec=0;
1336
1337 switch (keysym)
1338 {
1339 case kKey_Left:
1340 dra = -TMath::DegToRad();
1341 break;
1342 case kKey_Right:
1343 dra = +TMath::DegToRad();
1344 break;
1345 case kKey_Up:
1346 ddec = +TMath::DegToRad();
1347 break;
1348 case kKey_Down:
1349 ddec = -TMath::DegToRad();
1350 break;
1351 case kKey_Plus:
1352 SetRadiusFOV(fRadiusFOV+1);
1353 break;
1354 case kKey_Minus:
1355 SetRadiusFOV(fRadiusFOV-1);
1356 break;
1357
1358 default:
1359 return;
1360 }
1361
1362 const Double_t r = fRaDec.Phi();
1363 const Double_t d = TMath::Pi()/2-fRaDec.Theta();
1364
1365 SetRaDec(r+dra, d+ddec);
1366
1367 gPad->Update();
1368}
1369
1370// ------------------------------------------------------------------------
1371//
1372// Execute a gui event on the camera
1373//
1374void MAstroCatalog::ExecuteEvent(Int_t event, Int_t mp1, Int_t mp2)
1375{
1376 if (!TestBit(kGuiActive))
1377 return;
1378
1379 if (event==kKeyPress)
1380 ExecuteEventKbd(mp1, mp2);
1381}
1382
1383// --------------------------------------------------------------------------
1384//
1385// Displays a tooltip
1386//
1387void MAstroCatalog::ShowToolTip(Int_t px, Int_t py, const char *txt)
1388{
1389 if (TestBit(kNoToolTips))
1390 return;
1391
1392 Int_t x=0;
1393 Int_t y=0;
1394
1395 const Window_t id1 = gVirtualX->GetWindowID(gPad->GetCanvasID());
1396 const Window_t id2 = fToolTip->GetParent()->GetId();
1397
1398 Window_t id3;
1399 gVirtualX->TranslateCoordinates(id1, id2, px, py, x, y, id3);
1400
1401 // Show tool tip
1402 fToolTip->SetText(txt);
1403 fToolTip->Show(x+4, y+4);
1404}
1405
1406// --------------------------------------------------------------------------
1407//
1408// Calculate distance to primitive by checking all gui elements
1409//
1410Int_t MAstroCatalog::DistancetoPrimitive(Int_t px, Int_t py)
1411{
1412 return fMapG.DistancetoPrimitive(px, py);
1413}
1414
1415// ------------------------------------------------------------------------
1416//
1417// Returns string containing info about the object at position (px,py).
1418// Returned string will be re-used (lock in MT environment).
1419//
1420char *MAstroCatalog::GetObjectInfo(Int_t px, Int_t py) const
1421{
1422 return fMapG.GetObjectInfo(px, py);
1423}
Note: See TracBrowser for help on using the repository browser.