source: trunk/MagicSoft/Mars/mastro/MAstroCamera.cc@ 4017

Last change on this file since 4017 was 3957, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 15.9 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!
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 caluclation 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// Read the mirror geometry from a MC .def file. The following
154// structure is expected:
155//
156// #* TYPE=1 (MAGIC)
157// #* i f sx sy x y z thetan phin
158// #*
159// #* i : number of the mirror
160// #* f : focal distance of that mirror
161// #* sx : curvilinear coordinate of mirror's center in X[cm]
162// #* sy : curvilinear coordinate of mirror's center in X[cm]
163// #* x : x coordinate of the center of the mirror [cm]
164// #* y : y coordinate of the center of the mirror [cm]
165// #* z : z coordinate of the center of the mirror [cm]
166// #* thetan : polar theta angle of the direction where the mirror points to
167// #* phin : polar phi angle of the direction where the mirror points to
168// #* xn : xn coordinate of the normal vector in the center (normalized)
169// #* yn : yn coordinate of the normal vector in the center (normalized)
170// #* zn : zn coordinate of the normal vector in the center (normalized)
171// #
172// define_mirrors
173// 1 1700.9200 25.0002 75.0061 25.0000 75.0000 0.9207 0.02328894 1.24904577 -0.00736394 -0.02209183 0.99972882
174// 2 ...
175//
176void MAstroCamera::SetMirrors(const char *fname)
177{
178 ifstream fin(fname);
179 if (!fin)
180 {
181 gLog << err << "Cannot open file " << fname << ": ";
182 gLog << strerror(errno) << endl;
183 return;
184 }
185
186 TString line;
187 while (1)
188 {
189 line.ReadLine(fin);
190 if (!fin)
191 return;
192
193 line = line.Strip(TString::kBoth);
194
195 if (line.BeginsWith("n_mirrors"))
196 {
197 Int_t n;
198 sscanf(line.Data(), "%*s %d", &n);
199
200 if (!fMirrors)
201 fMirrors = new TClonesArray(MGeomMirror::Class(), n);
202
203 fMirrors->ExpandCreate(n);
204 continue;
205 }
206
207
208 Int_t id;
209 Float_t f, sx, sy, x, y, z, thetan, phin, xn, yn, zn;
210
211 const Int_t n = sscanf(line.Data(), "%d %f %f %f %f %f %f %f %f %f %f %f",
212 &id, &f, &sx, &sy, &x, &y, &z, &thetan,
213 &phin, &xn, &yn, &zn);
214 if (n!=12)
215 continue;
216
217 new ((*fMirrors)[id-1]) MGeomMirror;
218 ((MGeomMirror*)(*fMirrors)[id-1])->SetMirrorContent(id, f, sx, sy, x, y, z, thetan, phin, xn, yn, zn);
219 }
220}
221
222// --------------------------------------------------------------------------
223//
224// Set the camera geometry. The MGeomCam object is cloned.
225//
226void MAstroCamera::SetGeom(const MGeomCam &cam)
227{
228 if (fGeom)
229 delete fGeom;
230
231 fGeom = (MGeomCam*)cam.Clone();
232}
233
234// --------------------------------------------------------------------------
235//
236// Convert To Pad coordinates (see MAstroCatalog)
237//
238Int_t MAstroCamera::ConvertToPad(const TVector3 &w, TVector2 &v) const
239{
240 /*
241 --- Use this to plot the 'mean grid' instead of the grid of a
242 theoretical central mirror ---
243
244 TVector3 spot;
245 const Int_t num = fConfig->GetNumMirror();
246 for (int i=0; i<num; i++)
247 spot += fConfig->GetMirror(i).GetReflection(w, fGeom->GetCameraDist())*1000;
248 spot *= 1./num;
249 */
250
251 const TVector3 spot = fMirror0->GetReflection(w, fGeom->GetCameraDist())*1000;
252 v.Set(spot(0), spot(1));
253
254 const Float_t max = fGeom->GetMaxRadius()*0.70;
255 return v.X()>-max && v.Y()>-max && v.X()<max && v.Y()<max;
256}
257
258// --------------------------------------------------------------------------
259//
260// Find an object with a given name in the list of primitives of this pad.
261//
262TObject *FindObjectInPad(const char *name, TVirtualPad *pad)
263{
264 if (!pad)
265 pad = gPad;
266
267 if (!pad)
268 return NULL;
269
270 TObject *o;
271
272 TIter Next(pad->GetListOfPrimitives());
273 while ((o=Next()))
274 {
275 if (o->InheritsFrom(gROOT->GetClass(name)))
276 return o;
277
278 if (o->InheritsFrom("TPad"))
279 if ((o = FindObjectInPad(name, (TVirtualPad*)o)))
280 return o;
281 }
282 return NULL;
283}
284
285// --------------------------------------------------------------------------
286//
287// Options:
288//
289// '*' Draw the mean of the reflections on all mirrors
290// '.' Draw a dot for the reflection on each individual mirror
291// 'h' To create a TH2D of the star-light which is displayed
292// 'c' Use the underlaying MHCamera as histogram
293// '0' Draw the reflection on a virtual perfect mirror
294//
295// If the Pad contains an object MHCamera of type MHCamera it is used.
296// Otherwise a new object is created.
297//
298void MAstroCamera::AddPrimitives(TString o)
299{
300 if (!fTime || !fObservatory || !fMirrors)
301 {
302 cout << "Missing data..." << endl;
303 return;
304 }
305
306 if (o.IsNull())
307 o = "*.";
308
309 const Bool_t hasnull = o.Contains("0", TString::kIgnoreCase);
310 const Bool_t hashist = o.Contains("h", TString::kIgnoreCase);
311 const Bool_t hasmean = o.Contains("*", TString::kIgnoreCase);
312 const Bool_t hasdot = o.Contains(".", TString::kIgnoreCase);
313 const Bool_t usecam = o.Contains("c", TString::kIgnoreCase);
314
315 // Get camera
316 MHCamera *camera=(MHCamera*)FindObjectInPad("MHCamera", gPad);
317 if (camera)
318 {
319 if (!camera->GetGeometry() || camera->GetGeometry()->IsA()!=fGeom->IsA())
320 camera->SetGeometry(*fGeom);
321 }
322 else
323 {
324 camera = new MHCamera(*fGeom);
325 camera->SetName("MHCamera");
326 camera->SetStats(0);
327 camera->SetInvDeepBlueSeaPalette();
328 camera->SetBit(kCanDelete);
329 camera->Draw();
330 }
331
332 camera->SetTitle(GetPadTitle());
333
334 gPad->cd(1);
335
336 if (!usecam)
337 {
338 if (camera->GetEntries()==0)
339 camera->SetBit(MHCamera::kNoLegend);
340 }
341 else
342 {
343 camera->Reset();
344 camera->SetYTitle("arb.cur");
345 }
346
347 TH2 *h=0;
348 if (hashist)
349 {
350 TH2F hist("","", 90, -650, 650, 90, -650, 650);
351 hist.SetMinimum(0);
352 h = (TH2*)hist.DrawCopy("samecont1");
353 }
354
355 const TRotation rot(GetGrid(kTRUE));
356
357 MVector3 *radec;
358 TIter Next(&fList);
359
360 while ((radec=(MVector3*)Next()))
361 {
362 const Double_t mag = radec->Magnitude();
363 if (mag>GetLimMag())
364 continue;
365
366 TVector3 star(*radec);
367
368 // Rotate Star into telescope system
369 star *= rot;
370
371 TVector3 mean;
372
373 Int_t num = 0;
374
375 MGeomMirror *mirror = 0;
376 TIter NextM(fMirrors);
377 while ((mirror=(MGeomMirror*)NextM()))
378 {
379 const TVector3 spot = mirror->GetReflection(star, fGeom->GetCameraDist())*1000;
380
381 // calculate mean of all 'stars' hitting the camera plane
382 // by taking the sum of the intersection points between
383 // the light vector and the camera plane
384 mean += spot;
385
386 if (hasdot)
387 {
388 TMarker *m=new TMarker(spot(0), spot(1), 1);
389 m->SetMarkerColor(kMagenta);
390 m->SetMarkerStyle(kDot);
391 AddMap(m);
392 }
393 if (h)
394 h->Fill(spot(0), spot(1), pow(10, -mag/2.5));
395
396 if (usecam)
397 camera->Fill(spot(0), spot(1), pow(10, -mag/2.5));
398
399 num++;
400 }
401
402 // transform meters into millimeters (camera display works with mm)
403 mean *= 1./num;
404 DrawStar(mean(0), mean(1), *radec, !hasmean, Form("x=%.1fmm y=%.1fmm", mean(0), mean(1)));
405
406 if (hasnull)
407 {
408 TVector3 star(*radec);
409 star *= rot;
410 const TVector3 spot = fMirror0->GetReflection(star, fGeom->GetCameraDist())*1000;
411 DrawStar(spot(0), spot(1), *radec, !hasmean, Form("x=%.1fmm y=%.1fmm", mean(0), mean(1)));
412 //cout << TMath::Hypot(spot(0), spot(1)) << " " << TMath::Hypot(mean(0)-spot(0), mean(1)-spot(1)) << endl;
413 }
414 }
415}
416
417// --------------------------------------------------------------------------
418//
419// Options:
420//
421// '*' Draw the mean of the reflections on all mirrors
422// '.' Draw a dot for the reflection on each individual mirror
423// 'h' To create a TH2D of the star-light which is displayed
424// 'c' Use the underlaying MHCamera as histogram
425// '0' Draw the reflection on a virtual perfect mirror
426//
427// If the Pad contains an object MHCamera of type MHCamera it is used.
428// Otherwise a new object is created.
429//
430/*void MAstroCamera::FillStarList(TList *list)
431{
432 list->SetOwner();
433 list->Delete();
434
435 if (!fTime || !fObservatory || !fMirrors || !list)
436 {
437 cout << "Missing data..." << endl;
438 return;
439 }
440
441 const MAstroSky2Local s2l(*fTime, *fObservatory);
442 const TRotation trans(AlignCoordinates(rot*fRaDec));
443
444 // Return the correct rotation matrix
445 const TRotation rot = trans*s2l;
446
447 MVector3 *radec;
448 TIter Next(&fList);
449
450 while ((radec=(MVector3*)Next()))
451 {
452 const Double_t mag = radec->Magnitude();
453
454 TVector3 mean;
455 TVector3 star(*radec);
456 star *= rot;
457 const TVector3 spot = fMirror0->GetReflection(star, fGeom->GetCameraDist())*1000;
458
459 MStarLocalPos *starpos = new MStarLocalPos;
460 starpos->SetExpValues(mag,mean(0),mean(1));
461 list->Add(starpos);
462 }
463 // For MAGIC the distance of the mean of the light distribution
464 // to the Mirror0 reflection of the star (Abberation) can be
465 // expressed as: dr = (0.0713 +/- 0.0002) * r = r/14.03
466 // with r = hypot(mean(0), mean(1))
467}
468*/
469
470// ------------------------------------------------------------------------
471//
472// Uses fRaDec as a reference point.
473//
474// Return dZd and dAz corresponding to the distance from x,y to fRaDec
475//
476// Before calling this function you should correct for abberation. In
477// case of MAGIC you can do this by:
478// x /= 1.0713;
479// y /= 1.0713;
480//
481// x [mm]: x coordinate in the camera plane (assuming a perfect mirror)
482// y [mm]: y coordinate in the camera plane (assuming a perfect mirror)
483//
484// dzd [deg]: Delta Zd
485// daz [deg]: Delta Az
486//
487void MAstroCamera::GetDiffZdAz(Double_t x, Double_t y, Double_t &dzd, Double_t &daz)
488{
489 // Reflect the corrected pixel on a perfect mirror
490 TVector3 v(x, y, fGeom->GetCameraDist()*1000);
491 TVector3 spot = fMirror0->GetReflection(v);
492
493 // Derotate into the local coordinate system
494 const MAstroSky2Local rot(*fTime, *fObservatory);
495 const TRotation align(AlignCoordinates(rot*fRaDec).Inverse());
496 spot *= align;
497
498 cout << "Zd="<<spot.Theta()*TMath::RadToDeg() << " ";
499 cout << "Az="<<spot.Phi() *TMath::RadToDeg()+360 << endl;
500
501 // Derotatet the center of the camera
502 TVector3 c(0, 0, 1);
503 c *= align;
504
505 dzd = (spot.Theta()-c.Theta())*TMath::RadToDeg();
506 daz = (spot.Phi() -c.Phi()) *TMath::RadToDeg();
507
508 if (daz> 180) daz -= 360;
509 if (daz<-180) daz += 360;
510}
511
512// ------------------------------------------------------------------------
513//
514// Execute a gui event on the camera
515//
516void MAstroCamera::ExecuteEvent(Int_t event, Int_t mp1, Int_t mp2)
517{
518 // if (mp1>0 && mp2>0)
519 // {
520 // // Calculate World coordinates from pixel
521 // Double_t x = gPad->AbsPixeltoX(mp1);
522 // Double_t y = gPad->AbsPixeltoY(mp2);
523 //
524 // // Correct for abberation
525 // x /= 1.0713;
526 // y /= 1.0713;
527 //
528 // Double_t dzd, daz;
529 // GetDiffZdAz(x, y, dzd, daz);
530 //
531 // cout << "dZd="<< dzd << " " << "dAz="<< daz << endl;
532 // }
533 //
534 // For MAGIC the distance of the mean of the light distribution
535 // to the Mirror0 reflection of the star (Abberation) can be
536 // expressed as: dr = 0.0713*r = r/14.03
537 // +-0.0002
538
539 if (event==kKeyPress && fTime)
540 switch (mp2)
541 {
542 case kKey_Plus:
543 fTime->SetMjd(fTime->GetMjd()+0.25/24);
544 Update(kTRUE);
545 return;
546
547 case kKey_Minus:
548 fTime->SetMjd(fTime->GetMjd()-0.25/24);
549 Update(kTRUE);
550 return;
551 }
552
553 MAstroCatalog::ExecuteEvent(event, mp1, mp2);
554}
Note: See TracBrowser for help on using the repository browser.