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

Last change on this file since 6380 was 4977, checked in by tbretz, 20 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 MString str;
736 str.Print("%s: Ra=%.2fh Dec=%.1fd Mag=%.1f", v.GetName(), ra, dec, mag);
737 if (txt)
738 {
739 str += " (";
740 str += txt;
741 str += ")";
742 }
743
744 // draw star on the camera display
745 TMarker *tip=new TMarker(x, y, col<0 ? kDot : kFullDotMedium);;
746 tip->SetMarkerColor(col);
747 fMapG.Add(tip, new TString(str));
748
749 if (!resize)
750 return;
751
752 tip->SetMarkerSize((10 - (mag>1 ? mag : 1))/15);
753 tip->SetMarkerStyle(kCircle);
754}
755
756// --------------------------------------------------------------------------
757//
758// Set pad as modified.
759//
760void MAstroCatalog::Update(Bool_t upd)
761{
762 SetBit(kHasChanged);
763 if (gPad && TestBit(kMustCleanup))
764 {
765 gPad->Modified();
766 if (upd)
767 gPad->Update();
768 }
769}
770
771// --------------------------------------------------------------------------
772//
773// Set the observation time. Necessary to use local coordinate
774// system. The MTime object is cloned.
775//
776void MAstroCatalog::SetTime(const MTime &time)
777{
778 if (fTime)
779 delete fTime;
780 fTime=(MTime*)time.Clone();
781}
782
783// --------------------------------------------------------------------------
784//
785// Set the observatory location. Necessary to use local coordinate
786// system. The MObservatory object is cloned.
787//
788void MAstroCatalog::SetObservatory(const MObservatory &obs)
789{
790 if (fObservatory)
791 delete fObservatory;
792 fObservatory=new MObservatory;
793 obs.Copy(*fObservatory);
794}
795
796// --------------------------------------------------------------------------
797//
798// Convert the vector to pad coordinates. After conversion
799// the x- coordinate of the vector must be the x coordinate
800// of the pad - the same for y. If the coordinate is inside
801// the current draw area return kTRUE, otherwise kFALSE.
802// If it is an invalid coordinate return kERROR
803//
804Int_t MAstroCatalog::ConvertToPad(const TVector3 &w0, TVector2 &v) const
805{
806 TVector3 w(w0);
807
808 // Stretch such, that the Z-component is alwas the same. Now
809 // X and Y contains the intersection point between the star-light
810 // and the plain of a virtual plain screen (ccd...)
811 if (TestBit(kPlainScreen))
812 w *= 1./w(2);
813
814 w *= TMath::RadToDeg(); // FIXME: *conversion factor?
815 v.Set(TestBit(kMirrorX) ? -w(0) : w(0),
816 TestBit(kMirrorY) ? -w(1) : w(1));
817
818 if (w(2)<0)
819 return kERROR;
820
821 if (TestBit(kDrawingImage) || !gPad)
822 return v.Mod2()<fRadiusFOV*fRadiusFOV;
823
824 return v.X()>gPad->GetX1() && v.Y()>gPad->GetY1() &&
825 v.X()<gPad->GetX2() && v.Y()<gPad->GetY2();
826}
827
828// --------------------------------------------------------------------------
829//
830// Convert theta/phi coordinates of v by TRotation into new coordinate
831// system and convert the coordinated to pad by ConvertToPad.
832// The result is retunred in v.
833//
834Int_t MAstroCatalog::Convert(const TRotation &rot, TVector2 &v) const
835{
836 MVector3 w;
837 w.SetMagThetaPhi(1, v.Y(), v.X());
838 w *= rot;
839
840 return ConvertToPad(w, v);
841}
842
843// --------------------------------------------------------------------------
844//
845// Draw a line from v to v+(dx,dy) using Convert/ConvertToPad to get the
846// corresponding pad coordinates.
847//
848Bool_t MAstroCatalog::DrawLine(const TVector2 &v, Int_t dx, Int_t dy, const TRotation &rot, Int_t type)
849{
850 const TVector2 add(dx*TMath::DegToRad(), dy*TMath::DegToRad());
851
852 // Define all lines in the same direction
853 const TVector2 va(dy==1?v:v+add);
854 const TVector2 vb(dy==1?v+add:v);
855
856 TVector2 v0(va);
857 TVector2 v1(vb);
858
859 const Int_t rc0 = Convert(rot, v0);
860 const Int_t rc1 = Convert(rot, v1);
861
862 // Both are kFALSE or both are kERROR
863 if ((rc0|rc1)==kFALSE || (rc0&rc1)==kERROR)
864 return kFALSE;
865
866 TLine *line = new TLine(v0.X(), v0.Y(), v1.X(), v1.Y());
867 line->SetLineStyle(kDashDotted); //kDashed, kDotted, kDashDotted
868 line->SetLineColor(kWhite+type*2);
869 fMapG.Add(line);
870
871 if (dx!=0)
872 return kTRUE;
873
874 const TVector2 deg = va*TMath::RadToDeg();
875
876 MString txt;
877 if (type==1)
878 txt.Print("Ra=%.2fh Dec=%.1fd", fmod(deg.X()/15+48, 24), fmod(90-deg.Y()+270,180)-90);
879 else
880 txt.Print("Zd=%.1fd Az=%.1fd", fmod(deg.Y()+270,180)-90, fmod(deg.X()+720, 360));
881
882 TMarker *tip=new TMarker(v0.X(), v0.Y(), kDot);
883 tip->SetMarkerColor(kWhite+type*2);
884 fMapG.Add(tip, new TString(txt));
885
886 return kTRUE;
887}
888
889// --------------------------------------------------------------------------
890//
891// Use "local" draw option to align the display to the local
892// coordinate system instead of the sky coordinate system.
893// dx, dy are arrays storing recuresively all touched points
894// stepx, stepy are the step-size of the current grid.
895//
896void MAstroCatalog::Draw(const TVector2 &v0, const TRotation &rot, TArrayI &dx, TArrayI &dy, Int_t stepx, Int_t stepy, Int_t type)
897{
898 // Calculate the end point
899 const TVector2 v1 = v0 + TVector2(dx[0]*TMath::DegToRad(), dy[0]*TMath::DegToRad());
900
901 // Check whether the point has already been touched.
902 Int_t idx[] = {1, 1, 1, 1};
903
904 Int_t dirs[4][2] = { {0, stepy}, {stepx, 0}, {0, -stepy}, {-stepx, 0} };
905
906 // Check for ambiguities.
907 for (int i=0; i<dx.GetSize(); i++)
908 {
909 for (int j=0; j<4; j++)
910 {
911 const Bool_t rcx0 = (dx[i]+720)%360==(dx[0]+dirs[j][0]+720)%360;
912 const Bool_t rcy0 = (dy[i]+360)%180==(dy[0]+dirs[j][1]+360)%180;
913 if (rcx0&&rcy0)
914 idx[j] = 0;
915 }
916 }
917
918 // Enhance size of array by 1, copy current
919 // position as last entry
920 dx.Set(dx.GetSize()+1);
921 dy.Set(dy.GetSize()+1);
922
923 dx[dx.GetSize()-1] = dx[0];
924 dy[dy.GetSize()-1] = dy[0];
925
926 // Store current positon
927 const Int_t d[2] = { dx[0], dy[0] };
928
929 for (int i=0; i<4; i++)
930 if (idx[i])
931 {
932 // Calculate new position
933 dx[0] = d[0]+dirs[i][0];
934 dy[0] = d[1]+dirs[i][1];
935
936 // Draw corresponding line and iterate through grid
937 if (DrawLine(v1, dirs[i][0], dirs[i][1], rot, type))
938 Draw(v0, rot, dx, dy, stepx, stepy, type);
939
940 dx[0]=d[0];
941 dy[0]=d[1];
942 }
943}
944
945// --------------------------------------------------------------------------
946//
947// Draw a grid recursively around the point v0 (either Ra/Dec or Zd/Az)
948// The points in the grid are converted by a TRotation and CovertToPad
949// to pad coordinates. The type arguemnts is neccessary to create the
950// correct tooltip (Ra/Dec, Zd/Az) at the grid-points.
951// From the pointing position the step-size of teh gris is caluclated.
952//
953void MAstroCatalog::DrawGrid(const TVector3 &v0, const TRotation &rot, Int_t type)
954{
955 TArrayI dx(1);
956 TArrayI dy(1);
957
958 // align to 1deg boundary
959 TVector2 v(v0.Phi()*TMath::RadToDeg(), v0.Theta()*TMath::RadToDeg());
960 v.Set((Float_t)TMath::Nint(v.X()), (Float_t)TMath::Nint(v.Y()));
961
962 // calculate stepsizes based on visible FOV
963 Int_t stepx = 1;
964
965 if (v.Y()<fRadiusFOV || v.Y()>180-fRadiusFOV)
966 stepx=36;
967 else
968 {
969 // This is a rough estimate how many degrees are visible
970 const Float_t m = log(fRadiusFOV/180.)/log(90./(fRadiusFOV+1)+1);
971 const Float_t t = log(180.)-m*log(fRadiusFOV);
972 const Float_t f = m*log(90-fabs(90-v.Y()))+t;
973 const Int_t nx = (Int_t)(exp(f)+0.5);
974 stepx = nx<4 ? 1 : nx/4;
975 if (stepx>36)
976 stepx=36;
977 }
978
979 const Int_t ny = (Int_t)(fRadiusFOV+1);
980 Int_t stepy = ny<4 ? 1 : ny/4;
981
982 // align stepsizes to be devisor or 180 and 90
983 while (180%stepx)
984 stepx++;
985 while (90%stepy)
986 stepy++;
987
988 // align to step-size boundary (search for the nearest one)
989 Int_t dv = 1;
990 while ((int)(v.X())%stepx)
991 {
992 v.Set(v.X()+dv, v.Y());
993 dv = -TMath::Sign(TMath::Abs(dv)+1, dv);
994 }
995
996 dv = 1;
997 while ((int)(v.Y())%stepy)
998 {
999 v.Set(v.X(), v.Y()+dv);
1000 dv = -TMath::Sign(TMath::Abs(dv)+1, dv);
1001 }
1002
1003 // draw...
1004 v *= TMath::DegToRad();
1005
1006 Draw(v, rot, dx, dy, stepx, stepy, type);
1007}
1008
1009// --------------------------------------------------------------------------
1010//
1011// Get a rotation matrix which aligns the pointing position
1012// to the center of the x,y plain
1013//
1014TRotation MAstroCatalog::AlignCoordinates(const TVector3 &v) const
1015{
1016 TRotation trans;
1017 trans.RotateZ(-v.Phi());
1018 trans.RotateY(-v.Theta());
1019 trans.RotateZ(-TMath::Pi()/2);
1020 return trans;
1021}
1022
1023// --------------------------------------------------------------------------
1024//
1025// Return the rotation matrix which converts either sky or
1026// local coordinates to coordinates which pole is the current
1027// pointing direction.
1028//
1029TRotation MAstroCatalog::GetGrid(Bool_t local)
1030{
1031 const Bool_t enable = fTime && fObservatory;
1032
1033 // If sky coordinate view is requested get rotation matrix and
1034 // draw corresponding sky-grid and if possible local grid
1035 if (!local)
1036 {
1037 const TRotation trans(AlignCoordinates(fRaDec));
1038
1039 DrawGrid(fRaDec, trans, 1);
1040
1041 if (enable)
1042 {
1043 const MAstroSky2Local rot(*fTime, *fObservatory);
1044 DrawGrid(rot*fRaDec, trans*rot.Inverse(), 2);
1045 }
1046
1047 // Return the correct rotation matrix
1048 return trans;
1049 }
1050
1051 // If local coordinate view is requested get rotation matrix and
1052 // draw corresponding sky-grid and if possible local grid
1053 if (local && enable)
1054 {
1055 const MAstroSky2Local rot(*fTime, *fObservatory);
1056
1057 const TRotation trans(AlignCoordinates(rot*fRaDec));
1058
1059 DrawGrid(fRaDec, trans*rot, 1);
1060 DrawGrid(rot*fRaDec, trans, 2);
1061
1062 // Return the correct rotation matrix
1063 return trans*rot;
1064 }
1065
1066 return TRotation();
1067}
1068
1069// --------------------------------------------------------------------------
1070//
1071// Create the title for the pad.
1072//
1073TString MAstroCatalog::GetPadTitle() const
1074{
1075 const Double_t ra = fRaDec.Phi()*TMath::RadToDeg();
1076 const Double_t dec = (TMath::Pi()/2-fRaDec.Theta())*TMath::RadToDeg();
1077
1078 TString txt;
1079 txt += Form("\\alpha=%.2fh ", fmod(ra/15+48, 24));
1080 txt += Form("\\delta=%.1f\\circ ", fmod(dec+270,180)-90);
1081 txt += Form("/ FOV=%.1f\\circ", fRadiusFOV);
1082
1083 if (!fTime || !fObservatory)
1084 return txt;
1085
1086 const MAstroSky2Local rot(*fTime, *fObservatory);
1087 const TVector3 loc = rot*fRaDec;
1088
1089 const Double_t rho = rot.RotationAngle(fRaDec.Phi(), TMath::Pi()/2-fRaDec.Theta());
1090
1091 const Double_t zd = TMath::RadToDeg()*loc.Theta();
1092 const Double_t az = TMath::RadToDeg()*loc.Phi();
1093
1094 txt.Prepend("#splitline{");
1095 txt += Form(" \\theta=%.1f\\circ ", fmod(zd+270,180)-90);
1096 txt += Form("\\phi=%.1f\\circ ", fmod(az+720, 360));
1097 txt += Form(" / \\rho=%.1f\\circ", rho*TMath::RadToDeg());
1098 txt += "}{<";
1099 txt += fTime->GetSqlDateTime();
1100 txt += ">}";
1101 return txt;
1102}
1103
1104// --------------------------------------------------------------------------
1105//
1106// To overlay the catalog make sure, that in any case you are using
1107// the 'same' option.
1108//
1109// If you want to overlay this on top of any picture which is created
1110// by derotation of the camera plain you have to use the 'mirror' option
1111// the compensate the mirroring of the image in the camera plain.
1112//
1113// If you have already compensated this by x=-x and y=-y when creating
1114// the histogram you can simply overlay the catalog.
1115//
1116// To overlay the catalog on a 2D histogram the histogram must have
1117// units of degrees (which are plain, like you directly convert the
1118// camera units by multiplication to degrees)
1119//
1120// To be 100% exact you must use the option 'plain' which assumes a plain
1121// screen. This is not necessary for the MAGIC-camera because the
1122// difference between both is less than 1e-3.
1123//
1124// You should always be aware of the fact, that the shown stars and the
1125// displayed grid is the ideal case, like a reflection on a virtual
1126// perfectly aligned central mirror. In reality the star-positions are
1127// smeared to the edge of the camera the more the distance to the center
1128// is, such that the center of gravity of the light distribution might
1129// be more far away from the center than the display shows.
1130//
1131// If you want the stars to be displayed as circles with a size
1132// showing their magnitude use "*" as an option.
1133//
1134// Use 'white' to display white instead of black stars
1135// Use 'yellow' to display white instead of black stars
1136//
1137//
1138void MAstroCatalog::AddPrimitives(TString o)
1139{
1140 const Bool_t same = o.Contains("same", TString::kIgnoreCase);
1141 const Bool_t local = o.Contains("local", TString::kIgnoreCase);
1142 const Bool_t mirx = o.Contains("mirrorx", TString::kIgnoreCase);
1143 const Bool_t miry = o.Contains("mirrory", TString::kIgnoreCase);
1144 const Bool_t mirror = o.Contains("mirror", TString::kIgnoreCase) && !mirx && !miry;
1145 const Bool_t size = o.Contains("*", TString::kIgnoreCase);
1146 const Bool_t white = o.Contains("white", TString::kIgnoreCase);
1147 const Bool_t yellow = o.Contains("yellow", TString::kIgnoreCase) && !white;
1148
1149 // X is vice versa, because ra is defined anti-clockwise
1150 mirx || mirror ? ResetBit(kMirrorX) : SetBit(kMirrorX);
1151 miry || mirror ? SetBit(kMirrorY) : ResetBit(kMirrorY);
1152
1153 const TRotation rot(GetGrid(local));
1154
1155 TIter Next(&fList);
1156 MVector3 *v=0;
1157 while ((v=(MVector3*)Next()))
1158 {
1159 if (v->Magnitude()>fLimMag)
1160 continue;
1161
1162 TVector2 s(v->Phi(), v->Theta());
1163 if (Convert(rot, s)==kTRUE)
1164 DrawStar(s.X(), s.Y(), *v, yellow?kYellow:(white?kWhite:kBlack), 0, size);
1165 }
1166
1167 if (!same && !TestBit(kDrawingImage) && gPad)
1168 {
1169 TPaveText *pv = new TPaveText(0.01, 0.90, 0.63, 0.99, "brNDC");
1170 pv->AddText(GetPadTitle());
1171 fMapG.Add(pv);
1172 }
1173
1174 TMarker *mk=new TMarker(0, 0, kMultiply);
1175 mk->SetMarkerColor(white||yellow?kWhite:kBlack);
1176 mk->SetMarkerSize(1.5);
1177 fMapG.Add(mk);
1178}
1179
1180// --------------------------------------------------------------------------
1181//
1182// Do nothing if 'same' option given.
1183// Otherwise set pad-range such that x- and y- coordinates have the same
1184// step-size
1185//
1186void MAstroCatalog::SetRangePad(Option_t *o)
1187{
1188 if (TString(o).Contains("same", TString::kIgnoreCase))
1189 return;
1190
1191 const Double_t edge = fRadiusFOV/TMath::Sqrt(2.);
1192 //gPad->Range(-edge, -edge, edge, edge);
1193
1194 const Float_t w = gPad->GetWw();
1195 const Float_t h = gPad->GetWh();
1196
1197 if (w<h)
1198 gPad->Range(-edge, -edge*h/w, edge, edge*h/w);
1199 else
1200 gPad->Range(-edge*w/h, -edge, edge*w/h, edge);
1201}
1202
1203// --------------------------------------------------------------------------
1204//
1205// First delete all gui elements.
1206// Set the correct range of the pad.
1207// Create all gui primitives
1208// If "this" is not in the current pad add it to the current pad.
1209// Reset vit kHasChanged
1210//
1211void MAstroCatalog::DrawPrimitives(Option_t *o)
1212{
1213 fMapG.Delete();
1214
1215 if (!TestBit(kDrawingImage) && gPad)
1216 SetRangePad(o);
1217
1218#ifdef DEBUG
1219 TStopwatch clk;
1220 clk.Start();
1221#endif
1222 AddPrimitives(o);
1223#ifdef DEBUG
1224 clk.Stop();
1225 clk.Print();
1226#endif
1227
1228 // Append to a possible second pad
1229 if (!TestBit(kDrawingImage) && gPad && !gPad->GetListOfPrimitives()->FindObject(this))
1230 AppendPad(o);
1231
1232 ResetBit(kHasChanged);
1233}
1234
1235// --------------------------------------------------------------------------
1236//
1237// Append "this" to current pad
1238// set bit kHasChanged to recreate all gui elements
1239// Connect signal
1240//
1241void MAstroCatalog::Draw(Option_t *o)
1242{
1243 // Append to first pad
1244 AppendPad(o);
1245
1246 // If contents have not previously changed make sure that
1247 // all primitives are recreated.
1248 SetBit(kHasChanged);
1249
1250 // Connect all TCanvas::ProcessedEvent to this->EventInfo
1251 // This means, that after TCanvas has processed an event
1252 // EventInfo of this class is called, see TCanvas::HandleInput
1253 gPad->GetCanvas()->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)",
1254 "MAstroCatalog", this,
1255 "EventInfo(Int_t,Int_t,Int_t,TObject*)");
1256}
1257
1258// --------------------------------------------------------------------------
1259//
1260// This function was connected to all created canvases. It is used
1261// to redirect GetObjectInfo into our own status bar.
1262//
1263// The 'connection' is done in Draw. It seems that 'connected'
1264// functions must be public.
1265//
1266void MAstroCatalog::EventInfo(Int_t event, Int_t px, Int_t py, TObject *selected)
1267{
1268 TCanvas *c = (TCanvas*)gTQSender;
1269
1270 gPad = c ? c->GetSelectedPad() : NULL;
1271 if (!gPad)
1272 return;
1273
1274
1275 // Try to find a corresponding object with kCannotPick set and
1276 // an available TString (for a tool tip)
1277 TString str;
1278 if (!selected || selected==this)
1279 selected = fMapG.PickObject(px, py, str);
1280
1281 if (!selected)
1282 return;
1283
1284 // Handle some gui events
1285 switch (event)
1286 {
1287 case kMouseMotion:
1288 if (fToolTip && !fToolTip->IsMapped() && !str.IsNull())
1289 ShowToolTip(px, py, str);
1290 break;
1291
1292 case kMouseLeave:
1293 if (fToolTip && fToolTip->IsMapped())
1294 fToolTip->Hide();
1295 break;
1296
1297 case kKeyPress:
1298 ExecuteEvent(kKeyPress, px, py);
1299 break;
1300 }
1301}
1302
1303// --------------------------------------------------------------------------
1304//
1305// Handle keyboard events.
1306//
1307void MAstroCatalog::ExecuteEventKbd(Int_t keycode, Int_t keysym)
1308{
1309 Double_t dra =0;
1310 Double_t ddec=0;
1311
1312 switch (keysym)
1313 {
1314 case kKey_Left:
1315 dra = -TMath::DegToRad();
1316 break;
1317 case kKey_Right:
1318 dra = +TMath::DegToRad();
1319 break;
1320 case kKey_Up:
1321 ddec = +TMath::DegToRad();
1322 break;
1323 case kKey_Down:
1324 ddec = -TMath::DegToRad();
1325 break;
1326 case kKey_Plus:
1327 SetRadiusFOV(fRadiusFOV+1);
1328 break;
1329 case kKey_Minus:
1330 SetRadiusFOV(fRadiusFOV-1);
1331 break;
1332
1333 default:
1334 return;
1335 }
1336
1337 const Double_t r = fRaDec.Phi();
1338 const Double_t d = TMath::Pi()/2-fRaDec.Theta();
1339
1340 SetRaDec(r+dra, d+ddec);
1341
1342 gPad->Update();
1343}
1344
1345// ------------------------------------------------------------------------
1346//
1347// Execute a gui event on the camera
1348//
1349void MAstroCatalog::ExecuteEvent(Int_t event, Int_t mp1, Int_t mp2)
1350{
1351 if (!TestBit(kGuiActive))
1352 return;
1353
1354 if (event==kKeyPress)
1355 ExecuteEventKbd(mp1, mp2);
1356}
1357
1358// --------------------------------------------------------------------------
1359//
1360// Displays a tooltip
1361//
1362void MAstroCatalog::ShowToolTip(Int_t px, Int_t py, const char *txt)
1363{
1364 if (TestBit(kNoToolTips))
1365 return;
1366
1367 Int_t x=0;
1368 Int_t y=0;
1369
1370 const Window_t id1 = gVirtualX->GetWindowID(gPad->GetCanvasID());
1371 const Window_t id2 = fToolTip->GetParent()->GetId();
1372
1373 Window_t id3;
1374 gVirtualX->TranslateCoordinates(id1, id2, px, py, x, y, id3);
1375
1376 // Show tool tip
1377 fToolTip->SetText(txt);
1378 fToolTip->Show(x+4, y+4);
1379}
1380
1381// --------------------------------------------------------------------------
1382//
1383// Calculate distance to primitive by checking all gui elements
1384//
1385Int_t MAstroCatalog::DistancetoPrimitive(Int_t px, Int_t py)
1386{
1387 return fMapG.DistancetoPrimitive(px, py);
1388}
1389
1390// ------------------------------------------------------------------------
1391//
1392// Returns string containing info about the object at position (px,py).
1393// Returned string will be re-used (lock in MT environment).
1394//
1395char *MAstroCatalog::GetObjectInfo(Int_t px, Int_t py) const
1396{
1397 return fMapG.GetObjectInfo(px, py);
1398}
Note: See TracBrowser for help on using the repository browser.