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

Last change on this file since 9386 was 9385, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 19.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-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//
102// To make sure that everything is correctly initialized when
103// cloned we call InitGeometry after cloning. This repairs
104// a few I/O problems mainly with files written with older
105// MARS versions (see also StreamerWorkaround)
106//
107TObject *MGeomCam::Clone(const char *newname) const
108{
109 MGeomCam *clone = (MGeomCam*)MParContainer::Clone(newname);
110 clone->InitGeometry();
111 return clone;
112}
113
114// --------------------------------------------------------------------------
115//
116// Copy function. It does intentionally (see workaround in Streamer)
117// not copy name and title.
118//
119void MGeomCam::Copy(TObject &o) const
120{
121 MGeomCam &c = (MGeomCam&)o;
122
123 c.fNumPixels = fNumPixels;
124 c.fCamDist = fCamDist;
125 c.fConvMm2Deg = fConvMm2Deg;
126
127 c.fMaxRadius = fMaxRadius;
128 c.fMinRadius = fMinRadius;
129 c.fPixRatio = fPixRatio;
130 c.fPixRatioSqrt = fPixRatioSqrt;
131
132 c.fNumPixInSector = fNumPixInSector;
133 c.fNumPixWithAidx = fNumPixWithAidx;
134
135 Int_t n = fPixels.GetEntriesFast();
136 Int_t m = c.fPixels.GetEntriesFast();
137
138 c.fPixels.Delete();
139 c.fPixels.Expand(n);
140
141 for (int i=m; i<n; i++)
142 c.fPixels.AddAt(fPixels[i]->Clone(), i);
143}
144
145// --------------------------------------------------------------------------
146//
147// Returns a reference of the i-th entry (the pixel with the software idx i)
148// The access is unchecked (for speed reasons!) accesing non existing
149// entries may crash the program!
150//
151MGeom &MGeomCam::operator[](Int_t i)
152{
153 return *static_cast<MGeom*>(fPixels.UncheckedAt(i));
154}
155
156// --------------------------------------------------------------------------
157//
158// Returns a reference of the i-th entry (the pixel with the software idx i)
159// The access is unchecked (for speed reasons!) accesing non existing
160// entries may crash the program!
161//
162MGeom &MGeomCam::operator[](Int_t i) const
163{
164 return *static_cast<MGeom*>(fPixels.UncheckedAt(i));
165}
166
167// --------------------------------------------------------------------------
168//
169// Calculate and fill the arrays storing the ratio of the area of a pixel
170// i to the pixel 0 and its square root.
171// The precalculation is done for speed reasons. Having an event the
172// ratio would be calculated at least once for each pixel which is
173// an enormous amount of numerical calculations, which are time
174// consuming and which can be avoided doing the precalculation.
175//
176void MGeomCam::CalcPixRatio()
177{
178 const Double_t a0 = (*this)[0].GetA();
179
180 for (UInt_t i=0; i<fNumPixels; i++)
181 {
182 fPixRatio[i] = a0/(*this)[i].GetA();
183 fPixRatioSqrt[i] = TMath::Sqrt(fPixRatio[i]);
184 }
185}
186
187// --------------------------------------------------------------------------
188//
189// Set the kIsOuterRing flag for all pixels which have a outermost pixel
190// as Next Neighbor and don't have the kIsOutermostRing flag itself.
191//
192void MGeomCam::InitOuterRing()
193{
194 fPixels.R__FOR_EACH(MGeom, CheckOuterRing)(*this);
195}
196
197// --------------------------------------------------------------------------
198//
199// Calculate the highest sector index+1 of all pixels, please make sure
200// the the sector numbers are continous.
201//
202void MGeomCam::CalcNumSectors()
203{
204 for (UInt_t i=0; i<fNumPixels; i++)
205 {
206 const Int_t s = (*this)[i].GetSector();
207
208 if (s>=fNumPixInSector.GetSize())
209 fNumPixInSector.Set(s+1);
210
211 fNumPixInSector[s]++;
212 }
213}
214
215// --------------------------------------------------------------------------
216//
217// Calculate the highest area index+1 of all pixels, please make sure
218// the the area indices are continous.
219//
220void MGeomCam::CalcNumAreas()
221{
222 for (UInt_t i=0; i<fNumPixels; i++)
223 {
224 const Int_t s = (*this)[i].GetAidx();
225
226 if (s>=fNumPixWithAidx.GetSize())
227 fNumPixWithAidx.Set(s+1);
228
229 fNumPixWithAidx[s]++;
230 }
231}
232
233// --------------------------------------------------------------------------
234//
235// Calculate the maximum radius of the camera. This is ment for GUI layout.
236//
237void MGeomCam::CalcMaxRadius()
238{
239 fMaxRadius.Set(GetNumAreas()+1);
240 fMinRadius.Set(GetNumAreas()+1);
241
242 for (UInt_t i=0; i<GetNumAreas()+1; i++)
243 {
244 fMaxRadius[i] = 0.;
245 fMinRadius[i] = FLT_MAX;
246 }
247
248 for (UInt_t i=0; i<fNumPixels; i++)
249 {
250 const MGeom &pix = (*this)[i];
251
252 const UInt_t s = pix.GetAidx();
253
254 const Float_t d = pix.GetT();
255 const Float_t r = pix.GetDist();
256
257 const Float_t maxr = r + d;
258 const Float_t minr = r>d ? r-d : 0;
259
260 if (maxr>fMaxRadius[s+1])
261 fMaxRadius[s+1] = maxr;
262
263 if (minr<fMinRadius[s+1])
264 fMinRadius[s+1] = minr;
265
266 if (minr<fMinRadius[0])
267 fMinRadius[0] = minr;
268
269 if (maxr>fMaxRadius[0])
270 fMaxRadius[0] = maxr;
271 }
272}
273
274// --------------------------------------------------------------------------
275//
276// sort neighbours from angle of -180 degree to -180 degree
277//
278// where right side of main pixel contains negative degrees
279// and left side positive degrees
280//
281// angle measured from top (0 degree) to bottom (180 degree)
282// ^ - | + //
283// | _ | _ //
284// | / \|/ \ //
285// 30 degree -+- | | | //
286// | / \ / \ / \ //
287// 90 degree -+- | | X | | //
288// | \ / \ / \ / //
289// 150 degree -+- | | | //
290// | \_/|\_/ //
291// | | //
292// | - | + //
293// ------------------> //
294// //
295void MGeomCam::SortNeighbors()
296{
297 for (unsigned int i=0; i<fNumPixels; i++)
298 {
299 MGeom &gpix = (*this)[i];
300
301 Double_t phi[6];
302 Int_t idx[7] = { 0, 0, 0, 0, 0, 0, -1 };
303
304 const Int_t n2 = gpix.GetNumNeighbors();
305 for (int j=0; j<n2; j++)
306 {
307 idx[j] = gpix.GetNeighbor(j);
308 phi[j] = (*this)[idx[j]].GetAngle(gpix);
309 }
310
311 Int_t sort[6] = { 6, 6, 6, 6, 6, 6 };
312
313 TMath::Sort(n2, phi, sort, kFALSE);
314
315 gpix.SetNeighbors(idx[sort[0]], idx[sort[1]], idx[sort[2]],
316 idx[sort[3]], idx[sort[4]], idx[sort[5]]);
317 }
318}
319
320// --------------------------------------------------------------------------
321//
322// Returns the distance between the pixels i and j. -1 if an index
323// doesn't exist. The default for j is 0. Assuming that 0 is the index
324// for a central pixel you can get the distance to the camera center.
325//
326Float_t MGeomCam::GetDist(UShort_t i, UShort_t j) const
327{
328 if (i>=fNumPixels || j>=fNumPixels)
329 return -1;
330
331 return (*this)[i].GetDist((*this)[j]);
332}
333
334// --------------------------------------------------------------------------
335//
336// Returns the angle between of pixels i wrt pixel j (default=0). The angle
337// is returned in the range between -pi and pi (atan2) and 2*pi if i or j
338// is out of range.
339//
340Float_t MGeomCam::GetAngle(UShort_t i, UShort_t j) const
341{
342 if (i>=fNumPixels || j>=fNumPixels)
343 return TMath::TwoPi();
344
345 return (*this)[i].GetAngle((*this)[j]);
346}
347
348// --------------------------------------------------------------------------
349//
350// The maximum possible distance from the origin.
351//
352Float_t MGeomCam::GetMaxRadius() const
353{
354 return fMaxRadius[0];
355}
356
357// --------------------------------------------------------------------------
358//
359// The minimum possible distance from the origin.
360//
361Float_t MGeomCam::GetMinRadius() const
362{
363 return fMinRadius[0];
364}
365
366// --------------------------------------------------------------------------
367//
368// Have to call the radii of the subcameras starting to count from 1
369//
370Float_t MGeomCam::GetMaxRadius(const Int_t i) const
371{
372 return i<0 || i>=(Int_t)GetNumAreas() ? -1 : fMaxRadius[i+1];
373}
374
375// --------------------------------------------------------------------------
376//
377// Have to call the radii of the subcameras starting to count from 1
378//
379Float_t MGeomCam::GetMinRadius(const Int_t i) const
380{
381 return i<0 || i>=(Int_t)GetNumAreas() ? -1 : fMinRadius[i+1];
382}
383
384// --------------------------------------------------------------------------
385//
386// returns the ratio of the area of the pixel with index 0 to the pixel
387// with the specified index i. 0 Is returned if the index argument is
388// out of range.
389//
390Float_t MGeomCam::GetPixRatio(UInt_t i) const
391{
392 // Former: (*this)[0].GetA()/(*this)[i].GetA();
393 // The const_cast is necessary to support older root version
394 return i<fNumPixels ? const_cast<TArrayF&>(fPixRatio)[i] : 0;
395}
396
397// --------------------------------------------------------------------------
398//
399// returns the square root of the ratio of the area of the pixel with
400// index 0 to the pixel with the specified index i. 0 Is returned if
401// the index argument is out of range.
402//
403Float_t MGeomCam::GetPixRatioSqrt(UInt_t i) const
404{
405 // The const_cast is necessary to support older root version
406 return i<fNumPixels ? const_cast<TArrayF&>(fPixRatioSqrt)[i] : 0;
407}
408
409// --------------------------------------------------------------------------
410//
411// Check if the position given in the focal plane (so z can be ignored)
412// is a position which might hit the detector. It is meant to be a rough
413// and fast estimate not a precise calculation. All positions dicarded
414// must not hit the detector. All positions accepted might still miss
415// the detector.
416//
417Bool_t MGeomCam::HitDetector(const MQuaternion &v, Double_t offset) const
418{
419 const Double_t max = fMaxRadius[0]/10+offset; // cm --> mm
420 return v.R2()<max*max;
421}
422
423// --------------------------------------------------------------------------
424//
425// Prints the Geometry information of all pixels in the camera.
426// With the option "simple" you can suppress the output of the contents
427// of the individual pixels.
428//
429void MGeomCam::Print(Option_t *o) const
430{
431 //
432 // Print Information about the Geometry of the camera
433 //
434 *fLog << all << " Number of Pixels (" << GetTitle() << "): " << fNumPixels << endl;
435 *fLog << " Number of Sectors: " << GetNumSectors() << " Area-Indices: " << GetNumAreas() << endl;
436 *fLog << " Min.Radius: " << GetMinRadius() << " Max.Radius: " << GetMaxRadius() << endl;
437
438 if (!TString(o).Contains("simple", TString::kIgnoreCase))
439 fPixels.Print();
440}
441
442// --------------------------------------------------------------------------
443//
444// Return the pixel index corresponding to the coordinates given in x, y.
445// The coordinates are given in pixel units (millimeters)
446// If no pixel exists return -1;
447//
448Int_t MGeomCam::GetPixelIdxXY(Float_t x, Float_t y) const
449{
450 for (unsigned int i=0; i<fNumPixels; i++)
451 if ((*this)[i].IsInside(x, y))
452 return i;
453
454 return -1;
455}
456
457Int_t MGeomCam::GetPixelIdx(const TVector2 &v) const
458{
459 return GetPixelIdxXY(v.X(), v.Y());
460}
461
462Int_t MGeomCam::GetPixelIdxDeg(const TVector2 &v) const
463{
464 return GetPixelIdxXYdeg(v.X(), v.Y());
465}
466
467// --------------------------------------------------------------------------
468//
469// Add all indices to arr of all neighbors around pix in a radius r.
470// The center pixel is also returned.
471//
472void MGeomCam::GetNeighbors(TArrayI &arr, const MGeom &pix, Float_t r) const
473{
474 arr.Set(GetNumPixels());
475
476 Int_t n = 0;
477
478 for (unsigned int i=0; i<GetNumPixels(); i++)
479 {
480 if (r>TMath::Hypot(pix.GetX()-(*this)[i].GetX(), pix.GetY()-(*this)[i].GetY()))
481 arr[n++] = i;
482 }
483
484 arr.Set(n);
485}
486
487// --------------------------------------------------------------------------
488//
489// Add all indices to arr of all neighbors around idx in a radius r.
490// The center pixel is also returned.
491//
492void MGeomCam::GetNeighbors(TArrayI &arr, UInt_t idx, Float_t r) const
493{
494 if (idx>=GetNumPixels())
495 {
496 arr.Set(0);
497 return;
498 }
499
500 const MGeom &pix = (*this)[idx];
501 GetNeighbors(arr, pix, r);
502}
503
504// --------------------------------------------------------------------------
505//
506// Add all pixels to list of all neighbors around pix in a radius r.
507// The center pixel is also returned.
508//
509void MGeomCam::GetNeighbors(TList &arr, const MGeom &pix, Float_t r) const
510{
511 for (unsigned int i=0; i<GetNumPixels(); i++)
512 {
513 if (r>TMath::Hypot(pix.GetX()-(*this)[i].GetX(), pix.GetY()-(*this)[i].GetY()))
514 arr.Add(fPixels.UncheckedAt(i));
515 }
516}
517
518// --------------------------------------------------------------------------
519//
520// Add all pixels to list of all neighbors around idx in a radius r.
521// The center pixel is also returned.
522//
523void MGeomCam::GetNeighbors(TList &arr, UInt_t idx, Float_t r) const
524{
525 if (idx>=GetNumPixels())
526 return;
527
528 const MGeom &pix = (*this)[idx];
529 GetNeighbors(arr, pix, r);
530}
531
532// --------------------------------------------------------------------------
533//
534// Return direction of p2 w.r.t. p1. For more details
535// see MGeom::GetDirection
536//
537Int_t MGeomCam::GetDirection(UInt_t p1, UInt_t p2) const
538{
539 if (p1>fNumPixels || p2>fNumPixels)
540 return -1;
541
542 return operator[](p1).GetDirection(operator[](p2));
543}
544
545// --------------------------------------------------------------------------
546//
547// Get index of neighbor of pixel idx in direction dir, if existing.
548//
549Int_t MGeomCam::GetNeighbor(UInt_t idx, Int_t dir) const
550{
551 if (idx>fNumPixels)
552 return -1;
553
554 const MGeom &pix=operator[](idx);
555
556 //
557 // search for the neighbor in the given direction
558 //
559 for (int i=0; i<pix.GetNumNeighbors(); i++)
560 if (GetDirection(idx, pix.GetNeighbor(i))==dir)
561 return pix.GetNeighbor(i);
562
563 return -1;
564}
565
566// --------------------------------------------------------------------------
567//
568// This fills the next neighbor information from a table into the pixel
569// objects. An overloaded function must call SortNeighbors()
570// at the end.
571//
572// All pixel swith a center withing 1.75*GetT() are considered neiighbors,
573// Only six neighbors are allowed.
574//
575void MGeomCam::CreateNN()
576{
577 TArrayI nn(6);
578
579 for (UInt_t i=0; i<GetNumPixels(); i++)
580 {
581 MGeom &pix = (*this)[i];
582
583 Int_t k = 0;
584 nn.Reset(-1);
585
586 for (UInt_t j=0; j<GetNumPixels(); j++)
587 {
588 if (i==j)
589 continue;
590
591 if (pix.GetDist((*this)[j])>pix.GetT()*1.75)
592 continue;
593
594 if (k==6)
595 {
596 *fLog << err << "ERROR - MGeomCam::CreateNN: Pixel " << j << " has too many neighbors." << endl;
597 break;
598 }
599
600 nn[k++] = j;
601 }
602
603 pix.SetNeighbors(nn[0], nn[1], nn[2], nn[3], nn[4], nn[5]);
604 }
605
606 SortNeighbors();
607}
608
609// --------------------------------------------------------------------------
610//
611// This workaround reproduces (as much as possible) the contents
612// of fPixels which got lost writing MGeomCam in which the
613// fPixels were filles with the []-operator instead of AddAt
614// and a root version previous to 5.18.
615// You try to read broken contents from file if fNumPixels is empty
616// but fNumPixels>0.
617// If you ever read broken contents from a split branch you
618// MUST call this function after reading.
619//
620// Furthermore since we moved all contents of MGeomPix to its
621// new base class the contents of fNeighbors is not read anymore
622// although all other contents is read correctly. Therefore we have
623// top recreate the neighbor table.
624//
625void MGeomCam::StreamerWorkaround()
626{
627 if (fNumPixels==0)
628 return;
629
630
631 if (!fPixels.IsEmpty())
632 {
633 CreateNN();
634 return;
635 }
636
637 const TObject *cam = (TObject*)IsA()->New();
638 cam->Copy(*this);
639 delete cam;
640}
641
642// --------------------------------------------------------------------------
643//
644// This is a custom made streamer. Due to a bug in TObjArray::operator[]
645// old root-versions didn't correctly store the contents of the TObjArray.
646// If such a file is detected (TObjArray empty) a new MGeomCam is created
647// with IsA()->New() and its contents is copied to this. Unfortunately
648// this won't work for all MGeomCam derivatives which need arguments
649// in the constructor and MGeomCam itself (no derivative). Fortunately
650// in prodoction we have never stored anything else than MGeomCamMagic yet.
651// The bug in root can be worked around using AddAt instead of operator[].
652//
653void MGeomCam::Streamer(TBuffer &b)
654{
655 if (b.IsReading())
656 {
657 MGeomCam::Class()->ReadBuffer(b, this);
658 StreamerWorkaround();
659 }
660 else
661 MGeomCam::Class()->WriteBuffer(b, this);
662}
663
664// --------------------------------------------------------------------------
665//
666// This deletes the pixel i and sets it to a clone of pix.
667// If i>=fNumPixels it is just ignored.
668//
669void MGeomCam::SetAt(UInt_t i, const MGeom &pix)
670{
671 if (i>=fNumPixels)
672 return;
673
674 if (fPixels[i])
675 delete fPixels.RemoveAt(i);
676
677 // For root versions <5.18 AddAt is mandatory, for newer
678 // root-version the []-operator can be used safely
679 fPixels.AddAt(pix.Clone(), i);
680}
Note: See TracBrowser for help on using the repository browser.