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

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