source: trunk/MagicSoft/Mars/mtemp/mmpi/MAstroCamera.cc@ 4440

Last change on this file since 4440 was 4440, checked in by rwagner, 21 years ago
*** empty log message ***
File size: 16.1 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, 3/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MAstroCamera
28// ============
29//
30// A tools displaying stars from a catalog in the camera display.
31// PRELIMINARY!!
32//
33//
34// Usage:
35// ------
36// For a usage example see macros/starfield.C
37//
38// To be able to reflect the star-light you need the geometry of the
39// mirror and the distance of the plain screen.
40//
41// You can get the mirror geometry from a MC file and the distance of
42// the screen from MGeomCam.
43//
44//
45// Algorithm:
46// ----------
47// The calculation of the position of the reflection in the camera is
48// done by:
49// - Rotation of the star-field such that the camera is looking into
50// the pointing direction
51// - Calculation of the reflected star-light vector by calling
52// MGeomMirror::GetReflection (which returns the point at which
53// the vector hits the camera plain)
54// - Depending on the draw-option you get each reflected point, the
55// reflection on a virtual ideal mirror or the reflection on each
56// individual mirror
57//
58//
59// GUI:
60// ----
61// * You can use the the cursor keys to change the pointing position
62// and plus/minus to change the time by a quarter of an hour.
63//
64// ToDo:
65// -----
66// * possibility to overwrite distance from mirror to screen
67// * read the mirror geometry directly from the MC input file
68//
69/////////////////////////////////////////////////////////////////////////////
70#include "MAstroCamera.h"
71
72#include <errno.h> // strerror
73#include <fstream> // ifstream
74
75#include <KeySymbols.h> // kKey_*
76
77#include <TH2.h> // TH2D
78#include <TMarker.h> // TMarker
79#include <TVirtualPad.h> // gPad
80
81#include "MLog.h"
82#include "MLogManip.h"
83
84#include "MGeomCam.h"
85#include "MGeomMirror.h"
86
87#include "MTime.h"
88#include "MAstroSky2Local.h"
89#include "MObservatory.h"
90
91#include "../mhist/MHCamera.h" // FIXME: This dependancy is very bad!
92 // HOW TO GET RID OF IT? Move MHCamera to mgeom?
93
94#include "MStarLocalPos.h"
95
96ClassImp(MAstroCamera);
97
98using namespace std;
99
100// --------------------------------------------------------------------------
101//
102// Create a virtual MGeomMirror which is in the center of the coordinate
103// system and has a normal vector in z-direction.
104//
105MAstroCamera::MAstroCamera() : fGeom(0), fMirrors(0)
106{
107 fMirror0 = new MGeomMirror;
108 fMirror0->SetMirrorContent(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
109}
110
111// --------------------------------------------------------------------------
112//
113// Delete fGeom, fMirrors and the virtual 0-Mirror fMirror0
114//
115MAstroCamera::~MAstroCamera()
116{
117 if (fGeom)
118 delete fGeom;
119 if (fMirrors)
120 delete fMirrors;
121
122 delete fMirror0;
123
124}
125
126// --------------------------------------------------------------------------
127//
128// Set a list of mirrors. The Mirrors must be of type MGeomMirror and
129// stored in a TClonesArray
130//
131void MAstroCamera::SetMirrors(TClonesArray &arr)
132{
133 if (arr.GetClass()!=MGeomMirror::Class())
134 {
135 cout << "ERROR - TClonesArray doesn't contain objects of type MGeomMirror... ignored." << endl;
136 return;
137 }
138
139 const Int_t n = arr.GetSize();
140
141 if (!fMirrors)
142 fMirrors = new TClonesArray(MGeomMirror::Class(), n);
143
144 fMirrors->ExpandCreate(n);
145
146 for (int i=0; i<n; i++)
147 memcpy((*fMirrors)[i], arr[i], sizeof(MGeomMirror));
148
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 (!fTime || !fObservatory || !fMirrors)
303 {
304 cout << "Missing data..." << endl;
305 return;
306 }
307
308 if (o.IsNull())
309 o = "*.";
310
311 const Bool_t hasnull = o.Contains("0", TString::kIgnoreCase);
312 const Bool_t hashist = o.Contains("h", TString::kIgnoreCase);
313 const Bool_t hasmean = o.Contains("*", TString::kIgnoreCase);
314 const Bool_t hasdot = o.Contains(".", TString::kIgnoreCase);
315 const Bool_t usecam = o.Contains("c", TString::kIgnoreCase);
316 const Bool_t resize = o.Contains("=", TString::kIgnoreCase);
317
318 // Get camera
319 MHCamera *camera=(MHCamera*)FindObjectInPad("MHCamera", gPad);
320 if (camera)
321 {
322 if (!camera->GetGeometry() || camera->GetGeometry()->IsA()!=fGeom->IsA())
323 camera->SetGeometry(*fGeom);
324 }
325 else
326 {
327 camera = new MHCamera(*fGeom);
328 camera->SetName("MHCamera");
329 camera->SetStats(0);
330 camera->SetInvDeepBlueSeaPalette();
331 camera->SetBit(kCanDelete);
332 camera->Draw();
333 }
334
335 camera->SetTitle(GetPadTitle());
336
337 gPad->cd(1);
338
339 if (!usecam)
340 {
341 if (camera->GetEntries()==0)
342 camera->SetBit(MHCamera::kNoLegend);
343 }
344 else
345 {
346 camera->Reset();
347 camera->SetYTitle("arb.cur");
348 }
349
350 TH2 *h=0;
351 if (hashist)
352 {
353 TH2F hist("","", 90, -650, 650, 90, -650, 650);
354 hist.SetMinimum(0);
355 h = (TH2*)hist.DrawCopy("samecont1");
356 }
357
358 const TRotation rot(GetGrid(kTRUE));
359
360 MVector3 *radec;
361 TIter Next(&fList);
362
363 while ((radec=(MVector3*)Next()))
364 {
365 const Double_t mag = radec->Magnitude();
366 if (mag>GetLimMag())
367 continue;
368
369 TVector3 star(*radec);
370
371 // Rotate Star into telescope system
372 star *= rot;
373
374 TVector3 mean;
375
376 Int_t num = 0;
377
378 MGeomMirror *mirror = 0;
379 TIter NextM(fMirrors);
380 while ((mirror=(MGeomMirror*)NextM()))
381 {
382 const TVector3 spot = mirror->GetReflection(star, fGeom->GetCameraDist())*1000;
383
384 // calculate mean of all 'stars' hitting the camera plane
385 // by taking the sum of the intersection points between
386 // the light vector and the camera plane
387 mean += spot;
388
389 if (hasdot)
390 {
391 TMarker *m=new TMarker(spot(0), spot(1), 1);
392 m->SetMarkerColor(kMagenta);
393 m->SetMarkerStyle(kDot);
394 fMapG.Add(m);
395 }
396 if (h)
397 h->Fill(spot(0), spot(1), pow(10, -mag/2.5));
398
399 if (usecam)
400 camera->Fill(spot(0), spot(1), pow(10, -mag/2.5));
401
402 num++;
403 }
404
405 // transform meters into millimeters (camera display works with mm)
406 mean *= 1./num;
407 DrawStar(mean(0), mean(1), *radec, hasmean?kBlack:-1, Form("x=%.1fmm y=%.1fmm", mean(0), mean(1)), resize);
408 if (hasnull)
409 {
410 TVector3 star(*radec);
411 star *= rot;
412 const TVector3 spot = fMirror0->GetReflection(star, fGeom->GetCameraDist())*1000;
413 DrawStar(spot(0), spot(1), *radec, hasmean?kBlack:-1, Form("x=%.1fmm y=%.1fmm", mean(0), mean(1)), resize);
414 // This can be used to get the abberation...
415 //cout << TMath::Hypot(spot(0), spot(1)) << " " << TMath::Hypot(mean(0)-spot(0), mean(1)-spot(1)) << endl;
416 }
417 }
418}
419
420
421// --------------------------------------------------------------------------
422//
423// Fills a TList with all stars found under current presets
424// (Coordinates, Time, FOV). The list is emptied before the filling is done.
425// Currently, the mean spot when averaging over all mirrors is used.
426//
427void MAstroCamera::FillStarList(TList* list)
428{
429 if (!fTime || !fObservatory || !fMirrors) {
430 cout << "Missing data..." << endl;
431 return;
432 }
433
434 if (!list) {
435 cout << "Missing storage list for stars found..." << endl;
436 return;
437 }
438
439 list->Delete(); // dump old list... (otherwise the same stars would pile up)
440
441 const TRotation rot(GetGrid(kTRUE));
442 MVector3 *radec;
443 TIter Next(&fList);
444
445 while ((radec=(MVector3*)Next())) {
446 const Double_t mag = radec->Magnitude();
447 if (mag>GetLimMag())
448 continue;
449 TVector3 star(*radec);
450 // Rotate Star into telescope system
451 star *= rot;
452 TVector3 mean;
453 Int_t num = 0;
454 MGeomMirror *mirror = 0;
455 TIter NextM(fMirrors);
456 while ((mirror=(MGeomMirror*)NextM())) {
457 const TVector3 spot = mirror->GetReflection(star, fGeom->GetCameraDist())*1000;
458 mean += spot;
459 num++;
460 }
461 mean *= 1./num;
462 MStarLocalPos *starpos = new MStarLocalPos;
463 starpos->SetExpValues(mag,mean(0),mean(1));
464
465 TString name = radec->GetName();
466 if (name.Length()==0) {
467 starpos->SetName("unknown");
468 } else {
469 starpos->SetName(radec->GetName());
470 }
471 list->Add(starpos);
472 }
473}
474
475// ------------------------------------------------------------------------
476//
477// Uses fRaDec as a reference point.
478//
479// Return dZd and dAz corresponding to the distance from x,y to fRaDec
480//
481// Before calling this function you should correct for abberation. In
482// case of MAGIC you can do this by:
483// x /= 1.0713;
484// y /= 1.0713;
485//
486// x [mm]: x coordinate in the camera plane (assuming a perfect mirror)
487// y [mm]: y coordinate in the camera plane (assuming a perfect mirror)
488//
489// We assume (0, 0) to be the center of the FOV
490//
491// dzd [deg]: Delta Zd
492// daz [deg]: Delta Az
493//
494void MAstroCamera::GetDiffZdAz(Double_t x, Double_t y, Double_t &dzd, Double_t &daz)
495{
496 // Reflect the corrected pixel on a perfect mirror
497 TVector3 v(x, y, fGeom->GetCameraDist()*1000);
498 TVector3 spot = fMirror0->GetReflection(v);
499
500 // Derotate into the local coordinate system
501 const MAstroSky2Local rot(*fTime, *fObservatory);
502 const TRotation align(AlignCoordinates(rot*fRaDec).Inverse());
503 spot *= align;
504
505 cout << "Zd="<<spot.Theta()*TMath::RadToDeg() << " ";
506 cout << "Az="<<spot.Phi() *TMath::RadToDeg()+360 << endl;
507
508 // Derotatet the center of the camera
509 TVector3 c(0, 0, 1);
510 c *= align;
511
512 dzd = (spot.Theta()-c.Theta())*TMath::RadToDeg();
513 daz = (spot.Phi() -c.Phi()) *TMath::RadToDeg();
514
515 if (daz> 180) daz -= 360;
516 if (daz<-180) daz += 360;
517}
518
519// ------------------------------------------------------------------------
520//
521// Execute a gui event on the camera
522//
523void MAstroCamera::ExecuteEvent(Int_t event, Int_t mp1, Int_t mp2)
524{
525 // if (mp1>0 && mp2>0)
526 // {
527 // // Calculate World coordinates from pixel
528 // Double_t x = gPad->AbsPixeltoX(mp1);
529 // Double_t y = gPad->AbsPixeltoY(mp2);
530 //
531 // // Correct for abberation
532 // x /= 1.0713;
533 // y /= 1.0713;
534 //
535 // Double_t dzd, daz;
536 // GetDiffZdAz(x, y, dzd, daz);
537 //
538 // cout << "dZd="<< dzd << " " << "dAz="<< daz << endl;
539 // }
540 //
541 // For MAGIC the distance of the mean of the light distribution
542 // to the Mirror0 reflection of the star (Abberation) can be
543 // expressed as: dr = 0.0713*r = r/14.03
544 // +-0.0002
545
546 if (event==kKeyPress && fTime)
547 switch (mp2)
548 {
549 case kKey_Plus:
550 fTime->SetMjd(fTime->GetMjd()+0.25/24);
551 Update(kTRUE);
552 return;
553
554 case kKey_Minus:
555 fTime->SetMjd(fTime->GetMjd()-0.25/24);
556 Update(kTRUE);
557 return;
558 }
559
560 MAstroCatalog::ExecuteEvent(event, mp1, mp2);
561}
Note: See TracBrowser for help on using the repository browser.