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

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