source: trunk/MagicSoft/Mars/mtemp/meth/MAstroCamera.cc@ 4415

Last change on this file since 4415 was 4415, checked in by stark, 20 years ago
*** empty log message ***
File size: 17.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 express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 3/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Author(s): Sabrina Stark, 7/2004 <mailto:stark@particle.phys.ethz.ch>
20!
21! Copyright: MAGIC Software Development, 2000-2004
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// MAstroCamera
29// ============
30//
31// A tools displaying stars from a catalog in the camera display.
32// PRELIMINARY!!
33//
34//
35// Usage:
36// ------
37// For a usage example see macros/starfield.C
38//
39// To be able to reflect the star-light you need the geometry of the
40// mirror and the distance of the plain screen.
41//
42// You can get the mirror geometry from a MC file and the distance of
43// the screen from MGeomCam.
44//
45//
46// Algorithm:
47// ----------
48// The caluclation of the position of the reflection in the camera is
49// done by:
50// - Rotation of the star-field such that the camera is looking into
51// the pointing direction
52// - Calculation of the reflected star-light vector by calling
53// MGeomMirror::GetReflection (which returns the point at which
54// the vector hits the camera plain)
55// - Depending on the draw-option you get each reflected point, the
56// reflection on a virtual ideal mirror or the reflection on each
57// individual mirror
58//
59//
60// GUI:
61// ----
62// * You can use the the cursor keys to change the pointing position
63// and plus/minus to change the time by a quarter of an hour.
64//
65// ToDo:
66// -----
67// * possibility to overwrite distance from mirror to screen
68// * read the mirror geometry directly from the MC input file
69//
70/////////////////////////////////////////////////////////////////////////////
71#include "MAstroCamera.h"
72
73#include <errno.h> // strerror
74#include <fstream> // ifstream
75
76#include <KeySymbols.h> // kKey_*
77
78#include <TH2.h> // TH2D
79#include <TMarker.h> // TMarker
80#include <TVirtualPad.h> // gPad
81
82#include "MLog.h"
83#include "MLogManip.h"
84
85#include "MGeomCam.h"
86#include "MGeomMirror.h"
87
88#include "MTime.h"
89#include "MAstroSky2Local.h"
90#include "MObservatory.h"
91
92#include "../mhist/MHCamera.h" // FIXME: This dependancy is very bad!
93 // HOW TO GET RID OF IT? Move MHCamera to mgeom?
94
95//#include "MStarLocalPos.h"
96
97ClassImp(MAstroCamera);
98
99using namespace std;
100
101// --------------------------------------------------------------------------
102//
103// Create a virtual MGeomMirror which is in the center of the coordinate
104// system and has a normal vector in z-direction.
105//
106MAstroCamera::MAstroCamera() : fGeom(0), fMirrors(0)
107{
108 fMirror0 = new MGeomMirror;
109 fMirror0->SetMirrorContent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
110}
111
112// --------------------------------------------------------------------------
113//
114// Delete fGeom, fMirrors and the virtual 0-Mirror fMirror0
115//
116MAstroCamera::~MAstroCamera()
117{
118 if (fGeom)
119 delete fGeom;
120 if (fMirrors)
121 delete fMirrors;
122
123 delete fMirror0;
124
125}
126
127// --------------------------------------------------------------------------
128//
129// Set a list of mirrors. The Mirrors must be of type MGeomMirror and
130// stored in a TClonesArray
131//
132void MAstroCamera::SetMirrors(TClonesArray &arr)
133{
134 if (arr.GetClass()!=MGeomMirror::Class())
135 {
136 cout << "ERROR - TClonesArray doesn't contain objects of type MGeomMirror... ignored." << endl;
137 return;
138 }
139
140 const Int_t n = arr.GetSize();
141
142 if (!fMirrors)
143 fMirrors = new TClonesArray(MGeomMirror::Class(), n);
144
145 fMirrors->ExpandCreate(n);
146
147 for (int i=0; i<n; i++)
148 memcpy((*fMirrors)[i], arr[i], sizeof(MGeomMirror));
149
150}
151
152// --------------------------------------------------------------------------
153//
154// Read the mirror geometry from a MC .def file. The following
155// structure is expected:
156//
157// #* TYPE=1 (MAGIC)
158// #* i f sx sy x y z thetan phin
159// #*
160// #* i : number of the mirror
161// #* f : focal distance of that mirror
162// #* sx : curvilinear coordinate of mirror's center in X[cm]
163// #* sy : curvilinear coordinate of mirror's center in X[cm]
164// #* x : x coordinate of the center of the mirror [cm]
165// #* y : y coordinate of the center of the mirror [cm]
166// #* z : z coordinate of the center of the mirror [cm]
167// #* thetan : polar theta angle of the direction where the mirror points to
168// #* phin : polar phi angle of the direction where the mirror points to
169// #* xn : xn coordinate of the normal vector in the center (normalized)
170// #* yn : yn coordinate of the normal vector in the center (normalized)
171// #* zn : zn coordinate of the normal vector in the center (normalized)
172// #
173// define_mirrors
174// 1 1700.9200 25.0002 75.0061 25.0000 75.0000 0.9207 0.02328894 1.24904577 -0.00736394 -0.02209183 0.99972882
175// 2 ...
176//
177void MAstroCamera::SetMirrors(const char *fname)
178{
179 ifstream fin(fname);
180 if (!fin)
181 {
182 gLog << err << "Cannot open file " << fname << ": ";
183 gLog << strerror(errno) << endl;
184 return;
185 }
186
187 TString line;
188 while (1)
189 {
190 line.ReadLine(fin);
191 if (!fin)
192 return;
193
194 line = line.Strip(TString::kBoth);
195
196 if (line.BeginsWith("n_mirrors"))
197 {
198 Int_t n;
199 sscanf(line.Data(), "%*s %d", &n);
200
201 if (!fMirrors)
202 fMirrors = new TClonesArray(MGeomMirror::Class(), n);
203
204 fMirrors->ExpandCreate(n);
205 continue;
206 }
207
208
209 Int_t id;
210 Float_t f, sx, sy, x, y, z, thetan, phin, xn, yn, zn;
211
212 const Int_t n = sscanf(line.Data(), "%d %f %f %f %f %f %f %f %f %f %f %f",
213 &id, &f, &sx, &sy, &x, &y, &z, &thetan,
214 &phin, &xn, &yn, &zn);
215 if (n!=12)
216 continue;
217
218 new ((*fMirrors)[id-1]) MGeomMirror;
219 ((MGeomMirror*)(*fMirrors)[id-1])->SetMirrorContent(id, f, sx, sy, x, y, z, thetan, phin, xn, yn, zn);
220 }
221}
222
223// --------------------------------------------------------------------------
224//
225// Set the camera geometry. The MGeomCam object is cloned.
226//
227void MAstroCamera::SetGeom(const MGeomCam &cam)
228{
229 if (fGeom)
230 delete fGeom;
231
232 fGeom = (MGeomCam*)cam.Clone();
233}
234
235// --------------------------------------------------------------------------
236//
237// Convert To Pad coordinates (see MAstroCatalog)
238//
239Int_t MAstroCamera::ConvertToPad(const TVector3 &w, TVector2 &v) const
240{
241 /*
242 --- Use this to plot the 'mean grid' instead of the grid of a
243 theoretical central mirror ---
244
245 TVector3 spot;
246 const Int_t num = fConfig->GetNumMirror();
247 for (int i=0; i<num; i++)
248 spot += fConfig->GetMirror(i).GetReflection(w, fGeom->GetCameraDist())*1000;
249 spot *= 1./num;
250 */
251
252 const TVector3 spot = fMirror0->GetReflection(w, fGeom->GetCameraDist())*1000;
253 v.Set(spot(0), spot(1));
254
255 const Float_t max = fGeom->GetMaxRadius()*0.70;
256 return v.X()>-max && v.Y()>-max && v.X()<max && v.Y()<max;
257}
258
259// --------------------------------------------------------------------------
260//
261// Find an object with a given name in the list of primitives of this pad.
262//
263TObject *FindObjectInPad(const char *name, TVirtualPad *pad)
264{
265 if (!pad)
266 pad = gPad;
267
268 if (!pad)
269 return NULL;
270
271 TObject *o;
272
273 TIter Next(pad->GetListOfPrimitives());
274 while ((o=Next()))
275 {
276 if (o->InheritsFrom(gROOT->GetClass(name)))
277 return o;
278
279 if (o->InheritsFrom("TPad"))
280 if ((o = FindObjectInPad(name, (TVirtualPad*)o)))
281 return o;
282 }
283 return NULL;
284}
285
286// --------------------------------------------------------------------------
287//
288// Options:
289//
290// '*' Draw the mean of the reflections on all mirrors
291// '.' Draw a dot for the reflection on each individual mirror
292// 'h' To create a TH2D of the star-light which is displayed
293// 'c' Use the underlaying MHCamera as histogram
294// '0' Draw the reflection on a virtual perfect mirror
295// '=' Draw '0' or '*' propotional to the star magnitude
296//
297// If the Pad contains an object MHCamera of type MHCamera it is used.
298// Otherwise a new object is created.
299//
300void MAstroCamera::AddPrimitives(TString o)
301{
302 if (!fMirrors)
303 {
304 cout << "Missing Mirror data..." << endl;
305 }
306 if (!fObservatory)
307 {
308 cout << "Missing data (Observatory)..." << endl;
309 return;
310 }
311 if (!fTime)
312 {
313 cout << "Missing data (Time)..." << endl;
314 return;
315 }
316
317 if (o.IsNull())
318 o = "*.";
319
320 const Bool_t hasnull = o.Contains("0", TString::kIgnoreCase);
321 const Bool_t hashist = o.Contains("h", TString::kIgnoreCase);
322 const Bool_t hasmean = o.Contains("*", TString::kIgnoreCase);
323 const Bool_t hasdot = o.Contains(".", TString::kIgnoreCase);
324 const Bool_t usecam = o.Contains("c", TString::kIgnoreCase);
325 const Bool_t resize = o.Contains("=", TString::kIgnoreCase);
326
327 // Get camera
328 MHCamera *camera=(MHCamera*)FindObjectInPad("MHCamera", gPad);
329 if (camera)
330 {
331 if (!camera->GetGeometry() || camera->GetGeometry()->IsA()!=fGeom->IsA())
332 camera->SetGeometry(*fGeom);
333 }
334 else
335 {
336 camera = new MHCamera(*fGeom);
337 camera->SetName("MHCamera");
338 camera->SetStats(0);
339 camera->SetInvDeepBlueSeaPalette();
340 camera->SetBit(kCanDelete);
341 camera->Draw();
342 }
343
344 camera->SetTitle(GetPadTitle());
345
346 gPad->cd(1);
347
348 if (!usecam)
349 {
350 if (camera->GetEntries()==0)
351 camera->SetBit(MHCamera::kNoLegend);
352 }
353 else
354 {
355 camera->Reset();
356 camera->SetYTitle("arb.cur");
357 }
358
359 TH2 *h=0;
360 if (hashist)
361 {
362 TH2F hist("","", 90, -650, 650, 90, -650, 650);
363 hist.SetMinimum(0);
364 h = (TH2*)hist.DrawCopy("samecont1");
365 }
366
367 const TRotation rot(GetGrid(kTRUE));
368
369 MVector3 *radec;
370 TIter Next(&fList);
371
372 while ((radec=(MVector3*)Next()))
373 {
374 const Double_t mag = radec->Magnitude();
375 if (mag>GetLimMag())
376 continue;
377
378 TVector3 star(*radec);
379
380 // Rotate Star into telescope system
381 star *= rot;
382
383 TVector3 mean;
384
385 Int_t num = 0;
386
387 MGeomMirror *mirror = 0;
388 TIter NextM(fMirrors);
389 while ((mirror=(MGeomMirror*)NextM()))
390 {
391 const TVector3 spot = mirror->GetReflection(star, fGeom->GetCameraDist())*1000;
392
393 // calculate mean of all 'stars' hitting the camera plane
394 // by taking the sum of the intersection points between
395 // the light vector and the camera plane
396 mean += spot;
397
398 if (hasdot)
399 {
400 TMarker *m=new TMarker(spot(0), spot(1), 1);
401 m->SetMarkerColor(kMagenta);
402 m->SetMarkerStyle(kDot);
403 fMapG.Add(m);
404 }
405 if (h)
406 h->Fill(spot(0), spot(1), pow(10, -mag/2.5));
407
408 if (usecam)
409 camera->Fill(spot(0), spot(1), pow(10, -mag/2.5));
410
411 num++;
412 }
413
414 // transform meters into millimeters (camera display works with mm)
415 mean *= 1./num;
416 DrawStar(mean(0), mean(1), *radec, hasmean?kBlack:-1, Form("x=%.1fmm y=%.1fmm", mean(0), mean(1)), resize);
417
418 if (hasnull)
419 {
420 TVector3 star(*radec);
421 star *= rot;
422 const TVector3 spot = fMirror0->GetReflection(star, fGeom->GetCameraDist())*1000;
423 DrawStar(spot(0), spot(1), *radec, hasmean?kBlack:-1, Form("x=%.1fmm y=%.1fmm", mean(0), mean(1)), resize);
424 // This can be used to get the abberation...
425 //cout << TMath::Hypot(spot(0), spot(1)) << " " << TMath::Hypot(mean(0)-spot(0), mean(1)-spot(1)) << endl;
426 }
427 }
428}
429
430// --------------------------------------------------------------------------
431//
432// Options:
433//
434// '*' Draw the mean of the reflections on all mirrors
435// '.' Draw a dot for the reflection on each individual mirror
436// 'h' To create a TH2D of the star-light which is displayed
437// 'c' Use the underlaying MHCamera as histogram
438// '0' Draw the reflection on a virtual perfect mirror
439//
440// If the Pad contains an object MHCamera of type MHCamera it is used.
441// Otherwise a new object is created.
442//
443
444void MAstroCamera::StarPosInCamera()
445{
446 if (!fMirrors)
447 {
448 cout << "Missing Mirror data..." << endl;
449 }
450
451 const TRotation rot(GetGrid(kTRUE));
452
453 TIter Next(&fList);
454 MVector3 *v=0;
455 while ((v=(MVector3*)Next()))
456 {
457 const Double_t mag = v->Magnitude();
458 if (mag>GetLimMag())
459 continue;
460
461 TVector3 star(*v);
462
463 // Rotate Star into telescope system
464 star *= rot;
465
466 TVector3 *mean = new TVector3;
467
468 Int_t num = 0;
469
470 // Aberration:
471 MGeomMirror *mirror = 0;
472 TIter NextM(fMirrors);
473 while ((mirror=(MGeomMirror*)NextM()))
474 {
475 const TVector3 spot = mirror->GetReflection(star, fGeom->GetCameraDist())*1000;
476
477 *mean += spot;
478 num++;
479 }
480
481 *mean *= 1./num;
482 fCatList.Add(mean);
483 }
484}
485/*void MAstroCamera::FillStarList(TList *list)
486{
487 list->SetOwner();
488 list->Delete();
489
490 if (!fTime || !fObservatory || !fMirrors || !list)
491 {
492 cout << "Missing data..." << endl;
493 return;
494 }
495
496 const MAstroSky2Local s2l(*fTime, *fObservatory);
497 const TRotation trans(AlignCoordinates(rot*fRaDec));
498
499 // Return the correct rotation matrix
500 const TRotation rot = trans*s2l;
501
502 MVector3 *radec;
503 TIter Next(&fList);
504
505 while ((radec=(MVector3*)Next()))
506 {
507 const Double_t mag = radec->Magnitude();
508
509 TVector3 mean;
510 TVector3 star(*radec);
511 star *= rot;
512 const TVector3 spot = fMirror0->GetReflection(star, fGeom->GetCameraDist())*1000;
513
514 MStarLocalPos *starpos = new MStarLocalPos;
515 starpos->SetExpValues(mag,mean(0),mean(1));
516 list->Add(starpos);
517 }
518 // For MAGIC the distance of the mean of the light distribution
519 // to the Mirror0 reflection of the star (Abberation) can be
520 // expressed as: dr = (0.0713 +/- 0.0002) * r = r/14.03
521 // with r = hypot(mean(0), mean(1))
522}
523*/
524
525// ------------------------------------------------------------------------
526//
527// Uses fRaDec as a reference point.
528//
529// Return dZd and dAz corresponding to the distance from x,y to fRaDec
530//
531// Before calling this function you should correct for abberation. In
532// case of MAGIC you can do this by:
533// x /= 1.0713;
534// y /= 1.0713;
535//
536// x [mm]: x coordinate in the camera plane (assuming a perfect mirror)
537// y [mm]: y coordinate in the camera plane (assuming a perfect mirror)
538//
539// We assume (0, 0) to be the center of the FOV
540//
541// dzd [deg]: Delta Zd
542// daz [deg]: Delta Az
543//
544void MAstroCamera::GetDiffZdAz(Double_t x, Double_t y, Double_t &dzd, Double_t &daz)
545{
546 // Reflect the corrected pixel on a perfect mirror
547 TVector3 v(x, y, fGeom->GetCameraDist()*1000);
548 TVector3 spot = fMirror0->GetReflection(v);
549
550 // Derotate into the local coordinate system
551 const MAstroSky2Local rot(*fTime, *fObservatory);
552 const TRotation align(AlignCoordinates(rot*fRaDec).Inverse());
553 spot *= align;
554
555 cout << "Zd="<<spot.Theta()*TMath::RadToDeg() << " ";
556 cout << "Az="<<spot.Phi() *TMath::RadToDeg()+360 << endl;
557
558 // Derotatet the center of the camera
559 TVector3 c(0, 0, 1);
560 c *= align;
561
562 dzd = (spot.Theta()-c.Theta())*TMath::RadToDeg();
563 daz = (spot.Phi() -c.Phi()) *TMath::RadToDeg();
564
565 if (daz> 180) daz -= 360;
566 if (daz<-180) daz += 360;
567}
568
569// ------------------------------------------------------------------------
570//
571// Execute a gui event on the camera
572//
573void MAstroCamera::ExecuteEvent(Int_t event, Int_t mp1, Int_t mp2)
574{
575 // if (mp1>0 && mp2>0)
576 // {
577 // // Calculate World coordinates from pixel
578 // Double_t x = gPad->AbsPixeltoX(mp1);
579 // Double_t y = gPad->AbsPixeltoY(mp2);
580 //
581 // // Correct for abberation
582 // x /= 1.0713;
583 // y /= 1.0713;
584 //
585 // Double_t dzd, daz;
586 // GetDiffZdAz(x, y, dzd, daz);
587 //
588 // cout << "dZd="<< dzd << " " << "dAz="<< daz << endl;
589 // }
590 //
591 // For MAGIC the distance of the mean of the light distribution
592 // to the Mirror0 reflection of the star (Abberation) can be
593 // expressed as: dr = 0.0713*r = r/14.03
594 // +-0.0002
595
596 if (event==kKeyPress && fTime)
597 switch (mp2)
598 {
599 case kKey_Plus:
600 fTime->SetMjd(fTime->GetMjd()+0.25/24);
601 Update(kTRUE);
602 return;
603
604 case kKey_Minus:
605 fTime->SetMjd(fTime->GetMjd()-0.25/24);
606 Update(kTRUE);
607 return;
608 }
609
610 MAstroCatalog::ExecuteEvent(event, mp1, mp2);
611}
Note: See TracBrowser for help on using the repository browser.