source: trunk/MagicSoft/Mars/mgeom/MGeomCam.cc@ 9374

Last change on this file since 9374 was 9374, checked in by tbretz, 15 years ago
*** empty log message ***
File size: 17.7 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 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Author(s): Harald Kornmayer 01/2001
20! Author(s): Markus Gaug 03/2004 <mailto:markus@ifae.es>
21!
22! Copyright: MAGIC Software Development, 2000-2008
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// MGeomCam
29//
30// This is the base class of different camera geometries. It creates
31// a pixel object for a given number of pixels and defines the
32// interface of how to acccess the geometry information.
33//
34// We use a TObjArray for possible future usage (it is much more flexible
35// than a TClonesArray so that it can store more types of pixels (eg
36// fake pixels which are not really existing)
37//
38// Version 1:
39// ----------
40// - first implementation
41//
42// Version 2:
43// ----------
44// - added fPixRatio
45// - added fPixRatioSqrt
46//
47// Version 3:
48// ----------
49// - added fNumAreas
50// - added fNumSectors
51//
52// Version 4:
53// ----------
54// - added fMaxRadius
55// - added fMinRadius
56//
57// Version 5:
58// ----------
59// - added fNumPixInSector
60// - added fNumPixWithAidx
61// - removed fNumSectors
62// - removed fNumAreas
63//
64/////////////////////////////////////////////////////////////////////////////
65#include "MGeomCam.h"
66
67#include <TMath.h> // TMath
68#include <TClass.h> // IsA()->New()
69#include <TArrayI.h> // TArrayI
70#include <TVector2.h> // TVector2
71
72#include "MLog.h"
73#include "MLogManip.h"
74
75#include "MGeom.h"
76
77ClassImp(MGeomCam);
78
79using namespace std;
80
81// --------------------------------------------------------------------------
82//
83// Default Constructor. Initializes a Camera Geometry with npix pixels. All
84// pixels are deleted when the corresponding array is deleted.
85//
86MGeomCam::MGeomCam(UInt_t npix, Float_t dist, const char *name, const char *title)
87 : fNumPixels(npix), fCamDist(dist), fConvMm2Deg(kRad2Deg/(dist*1000)), fPixels(npix),
88 fMaxRadius(1), fMinRadius(1), fPixRatio(npix), fPixRatioSqrt(npix)
89{
90 fName = name ? name : "MGeomCam";
91 fTitle = title ? title : "Storage container for a camera geometry";
92
93 //
94 // make sure that the destructor delete all contained objects
95 //
96 fPixels.SetOwner();
97}
98
99// --------------------------------------------------------------------------
100//
101// Copy function. It does intentionally (see workaround in Streamer)
102// not copy name and title.
103//
104void MGeomCam::Copy(TObject &o) const
105{
106 MGeomCam &c = (MGeomCam&)o;
107
108 c.fNumPixels = fNumPixels;
109 c.fCamDist = fCamDist;
110 c.fConvMm2Deg = fConvMm2Deg;
111
112 c.fMaxRadius = fMaxRadius;
113 c.fMinRadius = fMinRadius;
114 c.fPixRatio = fPixRatio;
115 c.fPixRatioSqrt = fPixRatioSqrt;
116
117 c.fNumPixInSector = fNumPixInSector;
118 c.fNumPixWithAidx = fNumPixWithAidx;
119
120 Int_t n = fPixels.GetEntriesFast();
121 Int_t m = c.fPixels.GetEntriesFast();
122
123 c.fPixels.Delete();
124 c.fPixels.Expand(n);
125
126 for (int i=m; i<n; i++)
127 c.fPixels.AddAt(fPixels[i]->Clone(), i);
128}
129
130// --------------------------------------------------------------------------
131//
132// Returns a reference of the i-th entry (the pixel with the software idx i)
133// The access is unchecked (for speed reasons!) accesing non existing
134// entries may crash the program!
135//
136MGeom &MGeomCam::operator[](Int_t i)
137{
138 return *static_cast<MGeom*>(fPixels.UncheckedAt(i));
139}
140
141// --------------------------------------------------------------------------
142//
143// Returns a reference of the i-th entry (the pixel with the software idx i)
144// The access is unchecked (for speed reasons!) accesing non existing
145// entries may crash the program!
146//
147MGeom &MGeomCam::operator[](Int_t i) const
148{
149 return *static_cast<MGeom*>(fPixels.UncheckedAt(i));
150}
151
152// --------------------------------------------------------------------------
153//
154// Calculate and fill the arrays storing the ratio of the area of a pixel
155// i to the pixel 0 and its square root.
156// The precalculation is done for speed reasons. Having an event the
157// ratio would be calculated at least once for each pixel which is
158// an enormous amount of numerical calculations, which are time
159// consuming and which can be avoided doing the precalculation.
160//
161void MGeomCam::CalcPixRatio()
162{
163 const Double_t a0 = (*this)[0].GetA();
164
165 for (UInt_t i=0; i<fNumPixels; i++)
166 {
167 fPixRatio[i] = a0/(*this)[i].GetA();
168 fPixRatioSqrt[i] = TMath::Sqrt(fPixRatio[i]);
169 }
170}
171
172// --------------------------------------------------------------------------
173//
174// Set the kIsOuterRing flag for all pixels which have a outermost pixel
175// as Next Neighbor and don't have the kIsOutermostRing flag itself.
176//
177void MGeomCam::InitOuterRing()
178{
179 fPixels.R__FOR_EACH(MGeom, CheckOuterRing)(*this);
180}
181
182// --------------------------------------------------------------------------
183//
184// Calculate the highest sector index+1 of all pixels, please make sure
185// the the sector numbers are continous.
186//
187void MGeomCam::CalcNumSectors()
188{
189 for (UInt_t i=0; i<fNumPixels; i++)
190 {
191 const Int_t s = (*this)[i].GetSector();
192
193 if (s>=fNumPixInSector.GetSize())
194 fNumPixInSector.Set(s+1);
195
196 fNumPixInSector[s]++;
197 }
198}
199
200// --------------------------------------------------------------------------
201//
202// Calculate the highest area index+1 of all pixels, please make sure
203// the the area indices are continous.
204//
205void MGeomCam::CalcNumAreas()
206{
207 for (UInt_t i=0; i<fNumPixels; i++)
208 {
209 const Int_t s = (*this)[i].GetAidx();
210
211 if (s>=fNumPixWithAidx.GetSize())
212 fNumPixWithAidx.Set(s+1);
213
214 fNumPixWithAidx[s]++;
215 }
216}
217
218// --------------------------------------------------------------------------
219//
220// Calculate the maximum radius of the camera. This is ment for GUI layout.
221//
222void MGeomCam::CalcMaxRadius()
223{
224 fMaxRadius.Set(GetNumAreas()+1);
225 fMinRadius.Set(GetNumAreas()+1);
226
227 for (UInt_t i=0; i<GetNumAreas()+1; i++)
228 {
229 fMaxRadius[i] = 0.;
230 fMinRadius[i] = FLT_MAX;
231 }
232
233 for (UInt_t i=0; i<fNumPixels; i++)
234 {
235 const MGeom &pix = (*this)[i];
236
237 const UInt_t s = pix.GetAidx();
238
239 const Float_t d = pix.GetT();
240 const Float_t r = pix.GetDist();
241
242 const Float_t maxr = r + d;
243 const Float_t minr = r>d ? r-d : 0;
244
245 if (maxr>fMaxRadius[s+1])
246 fMaxRadius[s+1] = maxr;
247
248 if (minr<fMinRadius[s+1])
249 fMinRadius[s+1] = minr;
250
251 if (minr<fMinRadius[0])
252 fMinRadius[0] = minr;
253
254 if (maxr>fMaxRadius[0])
255 fMaxRadius[0] = maxr;
256 }
257}
258
259// --------------------------------------------------------------------------
260//
261// sort neighbours from angle of -180 degree to -180 degree
262//
263// where right side of main pixel contains negative degrees
264// and left side positive degrees
265//
266// angle measured from top (0 degree) to bottom (180 degree)
267// ^ - | + //
268// | _ | _ //
269// | / \|/ \ //
270// 30 degree -+- | | | //
271// | / \ / \ / \ //
272// 90 degree -+- | | X | | //
273// | \ / \ / \ / //
274// 150 degree -+- | | | //
275// | \_/|\_/ //
276// | | //
277// | - | + //
278// ------------------> //
279// //
280void MGeomCam::SortNeighbors()
281{
282 for (unsigned int i=0; i<fNumPixels; i++)
283 {
284 MGeom &gpix = (*this)[i];
285
286 Double_t phi[6];
287 Int_t idx[7] = { 0, 0, 0, 0, 0, 0, -1 };
288
289 const Int_t n2 = gpix.GetNumNeighbors();
290 for (int j=0; j<n2; j++)
291 {
292 idx[j] = gpix.GetNeighbor(j);
293 phi[j] = (*this)[idx[j]].GetAngle(gpix);
294 }
295
296 Int_t sort[6] = { 6, 6, 6, 6, 6, 6 };
297
298 TMath::Sort(n2, phi, sort, kFALSE);
299
300 gpix.SetNeighbors(idx[sort[0]], idx[sort[1]], idx[sort[2]],
301 idx[sort[3]], idx[sort[4]], idx[sort[5]]);
302 }
303}
304
305// --------------------------------------------------------------------------
306//
307// Returns the distance between the pixels i and j. -1 if an index
308// doesn't exist. The default for j is 0. Assuming that 0 is the index
309// for a central pixel you can get the distance to the camera center.
310//
311Float_t MGeomCam::GetDist(UShort_t i, UShort_t j) const
312{
313 if (i>=fNumPixels || j>=fNumPixels)
314 return -1;
315
316 return (*this)[i].GetDist((*this)[j]);
317}
318
319// --------------------------------------------------------------------------
320//
321// Returns the angle between of pixels i wrt pixel j (default=0). The angle
322// is returned in the range between -pi and pi (atan2) and 2*pi if i or j
323// is out of range.
324//
325Float_t MGeomCam::GetAngle(UShort_t i, UShort_t j) const
326{
327 if (i>=fNumPixels || j>=fNumPixels)
328 return TMath::TwoPi();
329
330 return (*this)[i].GetAngle((*this)[j]);
331}
332
333// --------------------------------------------------------------------------
334//
335// The maximum possible distance from the origin.
336//
337Float_t MGeomCam::GetMaxRadius() const
338{
339 return fMaxRadius[0];
340}
341
342// --------------------------------------------------------------------------
343//
344// The minimum possible distance from the origin.
345//
346Float_t MGeomCam::GetMinRadius() const
347{
348 return fMinRadius[0];
349}
350
351// --------------------------------------------------------------------------
352//
353// Have to call the radii of the subcameras starting to count from 1
354//
355Float_t MGeomCam::GetMaxRadius(const Int_t i) const
356{
357 return i<0 || i>=(Int_t)GetNumAreas() ? -1 : fMaxRadius[i+1];
358}
359
360// --------------------------------------------------------------------------
361//
362// Have to call the radii of the subcameras starting to count from 1
363//
364Float_t MGeomCam::GetMinRadius(const Int_t i) const
365{
366 return i<0 || i>=(Int_t)GetNumAreas() ? -1 : fMinRadius[i+1];
367}
368
369// --------------------------------------------------------------------------
370//
371// returns the ratio of the area of the pixel with index 0 to the pixel
372// with the specified index i. 0 Is returned if the index argument is
373// out of range.
374//
375Float_t MGeomCam::GetPixRatio(UInt_t i) const
376{
377 // Former: (*this)[0].GetA()/(*this)[i].GetA();
378 // The const_cast is necessary to support older root version
379 return i<fNumPixels ? const_cast<TArrayF&>(fPixRatio)[i] : 0;
380}
381
382// --------------------------------------------------------------------------
383//
384// returns the square root of the ratio of the area of the pixel with
385// index 0 to the pixel with the specified index i. 0 Is returned if
386// the index argument is out of range.
387//
388Float_t MGeomCam::GetPixRatioSqrt(UInt_t i) const
389{
390 // The const_cast is necessary to support older root version
391 return i<fNumPixels ? const_cast<TArrayF&>(fPixRatioSqrt)[i] : 0;
392}
393
394// --------------------------------------------------------------------------
395//
396// Check if the position given in the focal plane (so z can be ignored)
397// is a position which might hit the detector. It is meant to be a rough
398// and fast estimate not a precise calculation. All positions dicarded
399// must not hit the detector. All positions accepted might still miss
400// the detector.
401//
402Bool_t MGeomCam::HitDetector(const MQuaternion &v, Double_t offset) const
403{
404 const Double_t max = fMaxRadius[0]/10+offset; // cm --> mm
405 return v.R2()<max*max;
406}
407
408// --------------------------------------------------------------------------
409//
410// Prints the Geometry information of all pixels in the camera.
411// With the option "simple" you can suppress the output of the contents
412// of the individual pixels.
413//
414void MGeomCam::Print(Option_t *o) const
415{
416 //
417 // Print Information about the Geometry of the camera
418 //
419 *fLog << all << " Number of Pixels (" << GetTitle() << "): " << fNumPixels << endl;
420 *fLog << " Number of Sectors: " << GetNumSectors() << " Area-Indices: " << GetNumAreas() << endl;
421 *fLog << " Min.Radius: " << GetMinRadius() << " Max.Radius: " << GetMaxRadius() << endl;
422
423 if (!TString(o).Contains("simple", TString::kIgnoreCase))
424 fPixels.Print();
425}
426
427// --------------------------------------------------------------------------
428//
429// Return the pixel index corresponding to the coordinates given in x, y.
430// The coordinates are given in pixel units (millimeters)
431// If no pixel exists return -1;
432//
433Int_t MGeomCam::GetPixelIdxXY(Float_t x, Float_t y) const
434{
435 for (unsigned int i=0; i<fNumPixels; i++)
436 if ((*this)[i].IsInside(x, y))
437 return i;
438
439 return -1;
440}
441
442Int_t MGeomCam::GetPixelIdx(const TVector2 &v) const
443{
444 return GetPixelIdxXY(v.X(), v.Y());
445}
446
447Int_t MGeomCam::GetPixelIdxDeg(const TVector2 &v) const
448{
449 return GetPixelIdxXYdeg(v.X(), v.Y());
450}
451
452// --------------------------------------------------------------------------
453//
454// Add all indices to arr of all neighbors around pix in a radius r.
455// The center pixel is also returned.
456//
457void MGeomCam::GetNeighbors(TArrayI &arr, const MGeom &pix, Float_t r) const
458{
459 arr.Set(GetNumPixels());
460
461 Int_t n = 0;
462
463 for (unsigned int i=0; i<GetNumPixels(); i++)
464 {
465 if (r>TMath::Hypot(pix.GetX()-(*this)[i].GetX(), pix.GetY()-(*this)[i].GetY()))
466 arr[n++] = i;
467 }
468
469 arr.Set(n);
470}
471
472// --------------------------------------------------------------------------
473//
474// Add all indices to arr of all neighbors around idx in a radius r.
475// The center pixel is also returned.
476//
477void MGeomCam::GetNeighbors(TArrayI &arr, UInt_t idx, Float_t r) const
478{
479 if (idx>=GetNumPixels())
480 {
481 arr.Set(0);
482 return;
483 }
484
485 const MGeom &pix = (*this)[idx];
486 GetNeighbors(arr, pix, r);
487}
488
489// --------------------------------------------------------------------------
490//
491// Add all pixels to list of all neighbors around pix in a radius r.
492// The center pixel is also returned.
493//
494void MGeomCam::GetNeighbors(TList &arr, const MGeom &pix, Float_t r) const
495{
496 for (unsigned int i=0; i<GetNumPixels(); i++)
497 {
498 if (r>TMath::Hypot(pix.GetX()-(*this)[i].GetX(), pix.GetY()-(*this)[i].GetY()))
499 arr.Add(fPixels.UncheckedAt(i));
500 }
501}
502
503// --------------------------------------------------------------------------
504//
505// Add all pixels to list of all neighbors around idx in a radius r.
506// The center pixel is also returned.
507//
508void MGeomCam::GetNeighbors(TList &arr, UInt_t idx, Float_t r) const
509{
510 if (idx>=GetNumPixels())
511 return;
512
513 const MGeom &pix = (*this)[idx];
514 GetNeighbors(arr, pix, r);
515}
516
517// --------------------------------------------------------------------------
518//
519// Return direction of p2 w.r.t. p1. For more details
520// see MGeom::GetDirection
521//
522Int_t MGeomCam::GetDirection(UInt_t p1, UInt_t p2) const
523{
524 if (p1>fNumPixels || p2>fNumPixels)
525 return -1;
526
527 return operator[](p1).GetDirection(operator[](p2));
528}
529
530// --------------------------------------------------------------------------
531//
532// Get index of neighbor of pixel idx in direction dir, if existing.
533//
534Int_t MGeomCam::GetNeighbor(UInt_t idx, Int_t dir) const
535{
536 if (idx>fNumPixels)
537 return -1;
538
539 const MGeom &pix=operator[](idx);
540
541 //
542 // search for the neighbor in the given direction
543 //
544 for (int i=0; i<pix.GetNumNeighbors(); i++)
545 if (GetDirection(idx, pix.GetNeighbor(i))==dir)
546 return pix.GetNeighbor(i);
547
548 return -1;
549}
550
551// --------------------------------------------------------------------------
552//
553// This workaround reproduces (as much as possible) the contents
554// of fPixels which got lost writing MGeomCam in which the
555// fPixels were filles with the []-operator instead of AddAt
556// and a root version previous to 5.18.
557// You try to read broken contents from file if fNumPixels is empty
558// but fNumPixels>0.
559// If you ever read broken contents from a split branch you
560// MUST call this function after reading.
561//
562void MGeomCam::StreamerWorkaround()
563{
564 if (fNumPixels==0 || !fPixels.IsEmpty())
565 return;
566
567 TObject *cam = (TObject*)IsA()->New();
568 cam->Copy(*this);
569 delete cam;
570}
571
572// --------------------------------------------------------------------------
573//
574// This is a custom made streamer. Due to a bug in TObjArray::operator[]
575// old root-versions didn't correctly store the contents of the TObjArray.
576// If such a file is detected (TObjArray empty) a new MGeomCam is created
577// with IsA()->New() and its contents is copied to this. Unfortunately
578// this won't work for all MGeomCam derivatives which need arguments
579// in the constructor and MGeomCam itself (no derivative). Fortunately
580// in prodoction we have never stored anything else than MGeomCamMagic yet.
581// The bug in root can be worked around using AddAt instead of operator[].
582//
583void MGeomCam::Streamer(TBuffer &b)
584{
585 if (b.IsReading())
586 {
587 MGeomCam::Class()->ReadBuffer(b, this);
588 StreamerWorkaround();
589 }
590 else
591 MGeomCam::Class()->WriteBuffer(b, this);
592}
593
594void MGeomCam::SetAt(UInt_t i, const MGeom &pix)
595{
596 if (i>=fNumPixels)
597 return;
598
599 if (fPixels[i])
600 delete fPixels.RemoveAt(i);
601
602 // For root versions <5.18 AddAt is mandatory, for newer
603 // root-version the []-operator can be used safely
604 fPixels.AddAt(pix.Clone(), i);
605}
Note: See TracBrowser for help on using the repository browser.