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

Last change on this file since 9492 was 9398, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 19.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 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// Return the total area of the camera
323//
324Float_t MGeomCam::GetA() const
325{
326 Double_t A = 0;
327 for (unsigned int i=0; i<fNumPixels; i++)
328 A += (*this)[i].GetA();
329
330 return A;
331}
332
333// --------------------------------------------------------------------------
334//
335// Returns the distance between the pixels i and j. -1 if an index
336// doesn't exist. The default for j is 0. Assuming that 0 is the index
337// for a central pixel you can get the distance to the camera center.
338//
339Float_t MGeomCam::GetDist(UShort_t i, UShort_t j) const
340{
341 if (i>=fNumPixels || j>=fNumPixels)
342 return -1;
343
344 return (*this)[i].GetDist((*this)[j]);
345}
346
347// --------------------------------------------------------------------------
348//
349// Returns the angle between of pixels i wrt pixel j (default=0). The angle
350// is returned in the range between -pi and pi (atan2) and 2*pi if i or j
351// is out of range.
352//
353Float_t MGeomCam::GetAngle(UShort_t i, UShort_t j) const
354{
355 if (i>=fNumPixels || j>=fNumPixels)
356 return TMath::TwoPi();
357
358 return (*this)[i].GetAngle((*this)[j]);
359}
360
361// --------------------------------------------------------------------------
362//
363// The maximum possible distance from the origin.
364//
365Float_t MGeomCam::GetMaxRadius() const
366{
367 return fMaxRadius[0];
368}
369
370// --------------------------------------------------------------------------
371//
372// The minimum possible distance from the origin.
373//
374Float_t MGeomCam::GetMinRadius() const
375{
376 return fMinRadius[0];
377}
378
379// --------------------------------------------------------------------------
380//
381// Have to call the radii of the subcameras starting to count from 1
382//
383Float_t MGeomCam::GetMaxRadius(const Int_t i) const
384{
385 return i<0 || i>=(Int_t)GetNumAreas() ? -1 : fMaxRadius[i+1];
386}
387
388// --------------------------------------------------------------------------
389//
390// Have to call the radii of the subcameras starting to count from 1
391//
392Float_t MGeomCam::GetMinRadius(const Int_t i) const
393{
394 return i<0 || i>=(Int_t)GetNumAreas() ? -1 : fMinRadius[i+1];
395}
396
397// --------------------------------------------------------------------------
398//
399// returns the ratio of the area of the pixel with index 0 to the pixel
400// with the specified index i. 0 Is returned if the index argument is
401// out of range.
402//
403Float_t MGeomCam::GetPixRatio(UInt_t i) const
404{
405 // Former: (*this)[0].GetA()/(*this)[i].GetA();
406 // The const_cast is necessary to support older root version
407 return i<fNumPixels ? const_cast<TArrayF&>(fPixRatio)[i] : 0;
408}
409
410// --------------------------------------------------------------------------
411//
412// returns the square root of the ratio of the area of the pixel with
413// index 0 to the pixel with the specified index i. 0 Is returned if
414// the index argument is out of range.
415//
416Float_t MGeomCam::GetPixRatioSqrt(UInt_t i) const
417{
418 // The const_cast is necessary to support older root version
419 return i<fNumPixels ? const_cast<TArrayF&>(fPixRatioSqrt)[i] : 0;
420}
421
422// --------------------------------------------------------------------------
423//
424// Check if the position given in the focal plane (so z can be ignored)
425// is a position which might hit the detector. It is meant to be a rough
426// and fast estimate not a precise calculation. All positions dicarded
427// must not hit the detector. All positions accepted might still miss
428// the detector.
429//
430Bool_t MGeomCam::HitDetector(const MQuaternion &v, Double_t offset) const
431{
432 const Double_t max = fMaxRadius[0]/10+offset; // cm --> mm
433 return v.R2()<max*max;
434}
435
436// --------------------------------------------------------------------------
437//
438// Prints the Geometry information of all pixels in the camera.
439// With the option "simple" you can suppress the output of the contents
440// of the individual pixels.
441//
442void MGeomCam::Print(Option_t *o) const
443{
444 //
445 // Print Information about the Geometry of the camera
446 //
447 *fLog << all << " Number of Pixels (" << GetTitle() << "): " << fNumPixels << endl;
448 *fLog << " Number of Sectors: " << GetNumSectors() << " Area-Indices: " << GetNumAreas() << endl;
449 *fLog << " Min.Radius: " << GetMinRadius() << " Max.Radius: " << GetMaxRadius() << endl;
450
451 if (!TString(o).Contains("simple", TString::kIgnoreCase))
452 fPixels.Print();
453}
454
455// --------------------------------------------------------------------------
456//
457// Return the pixel index corresponding to the coordinates given in x, y.
458// The coordinates are given in pixel units (millimeters)
459// If no pixel exists return -1;
460//
461Int_t MGeomCam::GetPixelIdxXY(Float_t x, Float_t y) const
462{
463 for (unsigned int i=0; i<fNumPixels; i++)
464 if ((*this)[i].IsInside(x, y))
465 return i;
466
467 return -1;
468}
469
470Int_t MGeomCam::GetPixelIdx(const TVector2 &v) const
471{
472 return GetPixelIdxXY(v.X(), v.Y());
473}
474
475Int_t MGeomCam::GetPixelIdxDeg(const TVector2 &v) const
476{
477 return GetPixelIdxXYdeg(v.X(), v.Y());
478}
479
480// --------------------------------------------------------------------------
481//
482// Add all indices to arr of all neighbors around pix in a radius r.
483// The center pixel is also returned.
484//
485void MGeomCam::GetNeighbors(TArrayI &arr, const MGeom &pix, Float_t r) const
486{
487 arr.Set(GetNumPixels());
488
489 Int_t n = 0;
490
491 for (unsigned int i=0; i<GetNumPixels(); i++)
492 {
493 if (r>TMath::Hypot(pix.GetX()-(*this)[i].GetX(), pix.GetY()-(*this)[i].GetY()))
494 arr[n++] = i;
495 }
496
497 arr.Set(n);
498}
499
500// --------------------------------------------------------------------------
501//
502// Add all indices to arr of all neighbors around idx in a radius r.
503// The center pixel is also returned.
504//
505void MGeomCam::GetNeighbors(TArrayI &arr, UInt_t idx, Float_t r) const
506{
507 if (idx>=GetNumPixels())
508 {
509 arr.Set(0);
510 return;
511 }
512
513 const MGeom &pix = (*this)[idx];
514 GetNeighbors(arr, pix, r);
515}
516
517// --------------------------------------------------------------------------
518//
519// Add all pixels to list of all neighbors around pix in a radius r.
520// The center pixel is also returned.
521//
522void MGeomCam::GetNeighbors(TList &arr, const MGeom &pix, Float_t r) const
523{
524 for (unsigned int i=0; i<GetNumPixels(); i++)
525 {
526 if (r>TMath::Hypot(pix.GetX()-(*this)[i].GetX(), pix.GetY()-(*this)[i].GetY()))
527 arr.Add(fPixels.UncheckedAt(i));
528 }
529}
530
531// --------------------------------------------------------------------------
532//
533// Add all pixels to list of all neighbors around idx in a radius r.
534// The center pixel is also returned.
535//
536void MGeomCam::GetNeighbors(TList &arr, UInt_t idx, Float_t r) const
537{
538 if (idx>=GetNumPixels())
539 return;
540
541 const MGeom &pix = (*this)[idx];
542 GetNeighbors(arr, pix, r);
543}
544
545// --------------------------------------------------------------------------
546//
547// Return direction of p2 w.r.t. p1. For more details
548// see MGeom::GetDirection
549//
550Int_t MGeomCam::GetDirection(UInt_t p1, UInt_t p2) const
551{
552 if (p1>fNumPixels || p2>fNumPixels)
553 return -1;
554
555 return operator[](p1).GetDirection(operator[](p2));
556}
557
558// --------------------------------------------------------------------------
559//
560// Get index of neighbor of pixel idx in direction dir, if existing.
561//
562Int_t MGeomCam::GetNeighbor(UInt_t idx, Int_t dir) const
563{
564 if (idx>fNumPixels)
565 return -1;
566
567 const MGeom &pix=operator[](idx);
568
569 //
570 // search for the neighbor in the given direction
571 //
572 for (int i=0; i<pix.GetNumNeighbors(); i++)
573 if (GetDirection(idx, pix.GetNeighbor(i))==dir)
574 return pix.GetNeighbor(i);
575
576 return -1;
577}
578
579// --------------------------------------------------------------------------
580//
581// This fills the next neighbor information from a table into the pixel
582// objects. An overloaded function must call SortNeighbors()
583// at the end.
584//
585// All pixel swith a center withing 1.75*GetT() are considered neiighbors,
586// Only six neighbors are allowed.
587//
588void MGeomCam::CreateNN()
589{
590 TArrayI nn(6);
591
592 for (UInt_t i=0; i<GetNumPixels(); i++)
593 {
594 MGeom &pix = (*this)[i];
595
596 Int_t k = 0;
597 nn.Reset(-1);
598
599 for (UInt_t j=0; j<GetNumPixels(); j++)
600 {
601 if (i==j)
602 continue;
603
604 if (pix.GetDist((*this)[j])>pix.GetT()*1.75)
605 continue;
606
607 if (k==6)
608 {
609 *fLog << err << "ERROR - MGeomCam::CreateNN: Pixel " << j << " has too many neighbors." << endl;
610 break;
611 }
612
613 nn[k++] = j;
614 }
615
616 pix.SetNeighbors(nn[0], nn[1], nn[2], nn[3], nn[4], nn[5]);
617 }
618
619 SortNeighbors();
620}
621
622// --------------------------------------------------------------------------
623//
624// This workaround reproduces (as much as possible) the contents
625// of fPixels which got lost writing MGeomCam in which the
626// fPixels were filles with the []-operator instead of AddAt
627// and a root version previous to 5.18.
628// You try to read broken contents from file if fNumPixels is empty
629// but fNumPixels>0.
630// If you ever read broken contents from a split branch you
631// MUST call this function after reading.
632//
633// Furthermore since we moved all contents of MGeomPix to its
634// new base class the contents of fNeighbors is not read anymore
635// although all other contents is read correctly. Therefore we have
636// top recreate the neighbor table.
637//
638void MGeomCam::StreamerWorkaround()
639{
640 if (fNumPixels==0)
641 return;
642
643
644 if (!fPixels.IsEmpty())
645 {
646 CreateNN();
647 return;
648 }
649
650 const TObject *cam = (TObject*)IsA()->New();
651 cam->Copy(*this);
652 delete cam;
653}
654
655// --------------------------------------------------------------------------
656//
657// This is a custom made streamer. Due to a bug in TObjArray::operator[]
658// old root-versions didn't correctly store the contents of the TObjArray.
659// If such a file is detected (TObjArray empty) a new MGeomCam is created
660// with IsA()->New() and its contents is copied to this. Unfortunately
661// this won't work for all MGeomCam derivatives which need arguments
662// in the constructor and MGeomCam itself (no derivative). Fortunately
663// in prodoction we have never stored anything else than MGeomCamMagic yet.
664// The bug in root can be worked around using AddAt instead of operator[].
665//
666void MGeomCam::Streamer(TBuffer &b)
667{
668 if (b.IsReading())
669 {
670 MGeomCam::Class()->ReadBuffer(b, this);
671 StreamerWorkaround();
672 }
673 else
674 MGeomCam::Class()->WriteBuffer(b, this);
675}
676
677// --------------------------------------------------------------------------
678//
679// This deletes the pixel i and sets it to a clone of pix.
680// If i>=fNumPixels it is just ignored.
681//
682void MGeomCam::SetAt(UInt_t i, const MGeom &pix)
683{
684 if (i>=fNumPixels)
685 return;
686
687 if (fPixels[i])
688 delete fPixels.RemoveAt(i);
689
690 // For root versions <5.18 AddAt is mandatory, for newer
691 // root-version the []-operator can be used safely
692 fPixels.AddAt(pix.Clone(), i);
693}
Note: See TracBrowser for help on using the repository browser.