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

Last change on this file since 8154 was 7804, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 13.6 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-2005
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//
51/////////////////////////////////////////////////////////////////////////////
52#include "MGeomCam.h"
53
54#include <TClass.h> // IsA()->New()
55#include <TArrayI.h> // TArrayI
56#include <TVector2.h> // TVector2
57
58#include "MLog.h"
59#include "MLogManip.h"
60
61#include "MGeomPix.h"
62
63ClassImp(MGeomCam);
64
65using namespace std;
66
67// --------------------------------------------------------------------------
68//
69// Default Constructor. Initializes a Camera Geometry with npix pixels. All
70// pixels are deleted when the corresponding array is deleted.
71//
72MGeomCam::MGeomCam(UInt_t npix, Float_t dist, const char *name, const char *title)
73 : fNumPixels(npix), fCamDist(dist), fConvMm2Deg(kRad2Deg/(dist*1000)),
74 fPixels(npix), fMaxRadius(1), fMinRadius(1), fPixRatio(npix), fPixRatioSqrt(npix)
75{
76 fName = name ? name : "MGeomCam";
77 fTitle = title ? title : "Storage container for a camera geometry";
78
79 //
80 // make sure that the destructor delete all contained objects
81 //
82 fPixels.SetOwner();
83
84 for (UInt_t i=0; i<npix; i++)
85 fPixels[i] = new MGeomPix;
86}
87
88// --------------------------------------------------------------------------
89//
90// Returns a reference of the i-th entry (the pixel with the software idx i)
91// The access is unchecked (for speed reasons!) accesing non existing
92// entries may crash the program!
93//
94MGeomPix &MGeomCam::operator[](Int_t i)
95{
96 return *static_cast<MGeomPix*>(fPixels.UncheckedAt(i));
97}
98
99// --------------------------------------------------------------------------
100//
101// Returns a reference of the i-th entry (the pixel with the software idx i)
102// The access is unchecked (for speed reasons!) accesing non existing
103// entries may crash the program!
104//
105MGeomPix &MGeomCam::operator[](Int_t i) const
106{
107 return *static_cast<MGeomPix*>(fPixels.UncheckedAt(i));
108}
109
110// --------------------------------------------------------------------------
111//
112// Calculate and fill the arrays storing the ratio of the area of a pixel
113// i to the pixel 0 and its square root.
114// The precalculation is done for speed reasons. Having an event the
115// ratio would be calculated at least once for each pixel which is
116// an enormous amount of numerical calculations, which are time
117// consuming and which can be avoided doing the precalculation.
118//
119void MGeomCam::CalcPixRatio()
120{
121 const Double_t a0 = (*this)[0].GetA();
122
123 for (UInt_t i=0; i<fNumPixels; i++)
124 {
125 fPixRatio[i] = a0/(*this)[i].GetA();
126 fPixRatioSqrt[i] = TMath::Sqrt(fPixRatio[i]);
127 }
128}
129
130// --------------------------------------------------------------------------
131//
132// Set the kIsOuterRing flag for all pixels which have a outermost pixel
133// as Next Neighbor and don't have the kIsOutermostRing flag itself.
134//
135void MGeomCam::InitOuterRing()
136{
137 fPixels.R__FOR_EACH(MGeomPix, CheckOuterRing)(*this);
138}
139
140// --------------------------------------------------------------------------
141//
142// Calculate the highest sector index+1 of all pixels, please make sure
143// the the sector numbers are continous.
144//
145void MGeomCam::CalcNumSectors()
146{
147 fNumSectors = 0;
148
149 for (UInt_t i=0; i<fNumPixels; i++)
150 {
151 const Int_t s = (*this)[i].GetSector();
152
153 if (s>fNumSectors)
154 fNumSectors = s;
155 }
156
157 fNumSectors++;
158}
159
160// --------------------------------------------------------------------------
161//
162// Calculate the highest area index+1 of all pixels, please make sure
163// the the area indices are continous.
164//
165void MGeomCam::CalcNumAreas()
166{
167 fNumAreas = 0;
168
169 for (UInt_t i=0; i<fNumPixels; i++)
170 {
171 const Int_t s = (*this)[i].GetAidx();
172
173 if (s>fNumAreas)
174 fNumAreas = s;
175 }
176
177 fNumAreas++;
178}
179
180// --------------------------------------------------------------------------
181//
182// Calculate the maximum radius of the camera. This is ment for GUI layout.
183//
184void MGeomCam::CalcMaxRadius()
185{
186 fMaxRadius.Set(fNumAreas+1);
187 fMinRadius.Set(fNumAreas+1);
188
189 for (Int_t i=0; i<fNumAreas+1; i++)
190 {
191 fMaxRadius[i] = 0.;
192 fMinRadius[i] = FLT_MAX;
193 }
194
195 for (UInt_t i=0; i<fNumPixels; i++)
196 {
197 const MGeomPix &pix = (*this)[i];
198
199 const UInt_t s = pix.GetAidx();
200 const Float_t x = pix.GetX();
201 const Float_t y = pix.GetY();
202 const Float_t d = pix.GetD();
203
204 const Float_t r = TMath::Hypot(x, y);
205
206 const Float_t maxr = r + d;
207 const Float_t minr = r>d ? r-d : 0;
208
209 if (maxr>fMaxRadius[s+1])
210 fMaxRadius[s+1] = maxr;
211
212 if (minr<fMinRadius[s+1])
213 fMinRadius[s+1] = minr;
214
215 if (minr<fMinRadius[0])
216 fMinRadius[0] = minr;
217
218 if (maxr>fMaxRadius[0])
219 fMaxRadius[0] = maxr;
220 }
221}
222
223// --------------------------------------------------------------------------
224//
225// sort neighbours from angle of -180 degree to -180 degree
226//
227// where right side of main pixel contains negative degrees
228// and left side positive degrees
229//
230// angle measured from top (0 degree) to bottom (180 degree)
231// ^ - | + //
232// | _ | _ //
233// | / \|/ \ //
234// 30 degree -+- | | | //
235// | / \ / \ / \ //
236// 90 degree -+- | | X | | //
237// | \ / \ / \ / //
238// 150 degree -+- | | | //
239// | \_/|\_/ //
240// | | //
241// | - | + //
242// ------------------> //
243// //
244void MGeomCam::SortNeighbors()
245{
246 for (unsigned int i=0; i<fNumPixels; i++)
247 {
248 MGeomPix &gpix = (*this)[i];
249
250 Double_t phi[6];
251 Int_t idx[7] = { 0, 0, 0, 0, 0, 0, -1 };
252
253 const Int_t n2 = gpix.GetNumNeighbors();
254 for (int j=0; j<n2; j++)
255 {
256 idx[j] = gpix.GetNeighbor(j);
257 phi[j] = (*this)[idx[j]].GetAngle(gpix);
258 }
259
260 Int_t sort[6] = { 6, 6, 6, 6, 6, 6 };
261
262 TMath::Sort(n2, phi, sort, kFALSE);
263
264 gpix.SetNeighbors(idx[sort[0]], idx[sort[1]], idx[sort[2]],
265 idx[sort[3]], idx[sort[4]], idx[sort[5]]);
266 }
267}
268
269// --------------------------------------------------------------------------
270//
271// Returns the distance between the pixels i and j. -1 if an index
272// doesn't exist. The default for j is 0. Assuming that 0 is the index
273// for a central pixel you can get the distance to the camera center.
274//
275Float_t MGeomCam::GetDist(UShort_t i, UShort_t j) const
276{
277 if (i>=fNumPixels || j>=fNumPixels)
278 return -1;
279
280 return (*this)[i].GetDist((*this)[j]);
281}
282
283// --------------------------------------------------------------------------
284//
285// Returns the angle between of pixels i wrt pixel j (default=0). The angle
286// is returned in the range between -pi and pi (atan2) and 2*pi if i or j
287// is out of range.
288//
289Float_t MGeomCam::GetAngle(UShort_t i, UShort_t j) const
290{
291 if (i>=fNumPixels || j>=fNumPixels)
292 return TMath::TwoPi();
293
294 return (*this)[i].GetAngle((*this)[j]);
295}
296
297// --------------------------------------------------------------------------
298//
299// Have to call the radii of the subcameras starting to count from 1
300//
301Float_t MGeomCam::GetMaxRadius(const Int_t i) const
302{
303 return i<-1 || i>fNumAreas ? -1 : fMaxRadius[i+1];
304}
305
306// --------------------------------------------------------------------------
307//
308// Have to call the radii of the subcameras starting to count from 1
309//
310Float_t MGeomCam::GetMinRadius(const Int_t i) const
311{
312 return i<-1 || i>fNumAreas ? -1 : fMinRadius[i+1];
313}
314
315
316// --------------------------------------------------------------------------
317//
318// returns the ratio of the area of the pixel with index 0 to the pixel
319// with the specified index i. 0 Is returned if the index argument is
320// out of range.
321//
322Float_t MGeomCam::GetPixRatio(UInt_t i) const
323{
324 // Former: (*this)[0].GetA()/(*this)[i].GetA();
325 // The const_cast is necessary to support older root version
326 return i<fNumPixels ? const_cast<TArrayF&>(fPixRatio)[i] : 0;
327}
328
329// --------------------------------------------------------------------------
330//
331// returns the square root of the ratio of the area of the pixel with
332// index 0 to the pixel with the specified index i. 0 Is returned if
333// the index argument is out of range.
334//
335Float_t MGeomCam::GetPixRatioSqrt(UInt_t i) const
336{
337 // The const_cast is necessary to support older root version
338 return i<fNumPixels ? const_cast<TArrayF&>(fPixRatioSqrt)[i] : 0;
339}
340
341// --------------------------------------------------------------------------
342//
343// Prints the Geometry information of all pixels in the camera
344//
345void MGeomCam::Print(Option_t *) const
346{
347 //
348 // Print Information about the Geometry of the camera
349 //
350 *fLog << all << " Number of Pixels (" << GetTitle() << "): " << fNumPixels << endl;
351 *fLog << " Number of Sectors: " << GetNumSectors() << " Area-Indices: " << GetNumAreas() << endl;
352 *fLog << " Min.Radius: " << GetMinRadius() << " Max.Radius: " << GetMaxRadius() << endl;
353
354 fPixels.Print();
355}
356
357// --------------------------------------------------------------------------
358//
359// Create a clone of this container. This is very easy, because we
360// simply have to create a new object of the same type.
361//
362TObject *MGeomCam::Clone(const char *newname) const
363{
364 if (IsA()==MGeomCam::Class())
365 {
366 MGeomCam *cam = new MGeomCam(fNumPixels, fCamDist);
367 for (UInt_t i=0; i<fNumPixels; i++)
368 {
369 //if (fPixels.UncheckedAt(i))
370 (*this)[i].Copy((*cam)[i]);
371 }
372 cam->InitGeometry();
373 return cam;
374 }
375
376 return (TObject*)IsA()->New();
377}
378
379// --------------------------------------------------------------------------
380//
381// Return the pixel index corresponding to the coordinates given in x, y.
382// The coordinates are given in pixel units (millimeters)
383// If no pixel exists return -1;
384//
385Int_t MGeomCam::GetPixelIdxXY(Float_t x, Float_t y) const
386{
387 for (unsigned int i=0; i<fNumPixels; i++)
388 if ((*this)[i].IsInside(x, y))
389 return i;
390
391 return -1;
392}
393
394Int_t MGeomCam::GetPixelIdx(const TVector2 &v) const
395{
396 return GetPixelIdxXY(v.X(), v.Y());
397}
398
399Int_t MGeomCam::GetPixelIdxDeg(const TVector2 &v) const
400{
401 return GetPixelIdxXYdeg(v.X(), v.Y());
402}
403
404// --------------------------------------------------------------------------
405//
406// Add all indices to arr of all neighbors around pix in a radius r.
407// The center pixel is also returned.
408//
409void MGeomCam::GetNeighbors(TArrayI &arr, const MGeomPix &pix, Float_t r) const
410{
411 arr.Set(GetNumPixels());
412
413 Int_t n = 0;
414
415 for (unsigned int i=0; i<GetNumPixels(); i++)
416 {
417 if (r>TMath::Hypot(pix.GetX()-(*this)[i].GetX(), pix.GetY()-(*this)[i].GetY()))
418 arr[n++] = i;
419 }
420
421 arr.Set(n);
422}
423
424// --------------------------------------------------------------------------
425//
426// Add all indices to arr of all neighbors around idx in a radius r.
427// The center pixel is also returned.
428//
429void MGeomCam::GetNeighbors(TArrayI &arr, UInt_t idx, Float_t r) const
430{
431 if (idx>=GetNumPixels())
432 {
433 arr.Set(0);
434 return;
435 }
436
437
438 const MGeomPix &pix = (*this)[idx];
439 GetNeighbors(arr, pix, r);
440}
441
442// --------------------------------------------------------------------------
443//
444// Add all pixels to list of all neighbors around pix in a radius r.
445// The center pixel is also returned.
446//
447void MGeomCam::GetNeighbors(TList &arr, const MGeomPix &pix, Float_t r) const
448{
449 for (unsigned int i=0; i<GetNumPixels(); i++)
450 {
451 if (r>TMath::Hypot(pix.GetX()-(*this)[i].GetX(), pix.GetY()-(*this)[i].GetY()))
452 arr.Add(fPixels.UncheckedAt(i));
453 }
454}
455
456// --------------------------------------------------------------------------
457//
458// Add all pixels to list of all neighbors around idx in a radius r.
459// The center pixel is also returned.
460//
461void MGeomCam::GetNeighbors(TList &arr, UInt_t idx, Float_t r) const
462{
463 if (idx>=GetNumPixels())
464 return;
465
466 const MGeomPix &pix = (*this)[idx];
467 GetNeighbors(arr, pix, r);
468}
Note: See TracBrowser for help on using the repository browser.