source: trunk/MagicSoft/Mars/msimreflector/MReflector.cc@ 9239

Last change on this file since 9239 was 9236, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 7.0 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 11/2008 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: Software Development, 2000-2009
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26//
27// MReflector
28//
29//////////////////////////////////////////////////////////////////////////////
30#include "MReflector.h"
31
32#include <fstream>
33#include <errno.h>
34
35#include <TSystem.h>
36
37#include "MQuaternion.h"
38#include "MMirror.h"
39
40#include "MLog.h"
41#include "MLogManip.h"
42
43ClassImp(MReflector);
44
45using namespace std;
46
47// --------------------------------------------------------------------------
48//
49// Default constructor
50//
51MReflector::MReflector(const char *name, const char *title)
52{
53 fName = name ? name : "MReflector";
54 fTitle = title ? title : "Parameter container storing a collection of several mirrors (reflector)";
55
56 fMirrors.SetOwner();
57}
58
59// --------------------------------------------------------------------------
60//
61// Calculate the maximum radius of th ereflector. This is not meant as
62// a precise number but as a rough estimate e.g. to bin a histogram.
63//
64void MReflector::InitMaxR()
65{
66 fMaxR = 0;
67
68 TIter Next(&fMirrors);
69 MMirror *m = 0;
70 while ((m=static_cast<MMirror*>(Next())))
71 {
72 // Take into account the maximum incident angle 8eg 10deg) and
73 // the theta-angle of the mirror and the z-distance.
74 const Double_t r = m->GetDist()+1.5*m->GetMaxR();
75 if (r > fMaxR)
76 fMaxR = r;
77 }
78}
79
80// --------------------------------------------------------------------------
81//
82// Get the pointer to the first mirror. This is a very dangerous way of
83// access, but the fastest possible. because it is the most often called
84// function in ExecuteReflector we have to have a very fast access.
85//
86const MMirror **MReflector::GetFirstPtr() const
87{
88 return (const MMirror**)fMirrors.GetObjectRef(0);
89}
90
91// --------------------------------------------------------------------------
92//
93// Get number of mirrors. There should be no holes in the array!
94//
95const UInt_t MReflector::GetNumMirrors() const
96{
97 return fMirrors.GetEntriesFast();
98}
99
100// --------------------------------------------------------------------------
101//
102// Check with a rough estimate whether a photon can hit the reflector.
103//
104Bool_t MReflector::CanHit(const MQuaternion &p) const
105{
106 // p is given in the reflectory coordinate frame. This is meant as a
107 // fast check without lengthy calculations to omit all photons which
108 // cannot hit the reflector at all
109 return p.R2()<fMaxR*fMaxR;
110}
111
112// --------------------------------------------------------------------------
113//
114// Read a reflector setup from a file. This needs improvemtn.
115// FIXME: Documentation missing!
116//
117Bool_t MReflector::ReadFile(TString fname)
118{
119 gSystem->ExpandPathName(fname);
120
121 ifstream fin(fname);
122 if (!fin)
123 {
124 *fLog << err << "Cannot open file " << fname << ": ";
125 *fLog << (errno!=0?strerror(errno):"Insufficient memory for decompression") << endl;
126 }
127
128/*
129 Int_t idx[964];
130 Int_t srt[964];
131 for (int i=0; i<964; i++)
132 srt[i] = gRandom->Integer(964);
133
134 TMath::Sort(964, srt, idx);
135 */
136
137 fMirrors.Delete();
138
139 while (1)
140 {
141 TString line;
142 line.ReadLine(fin);
143 if (!fin)
144 break;
145
146 if (line.BeginsWith("#"))
147 {
148 //cout << line << endl;
149 continue;
150 }
151
152 line=line.Strip(TString::kBoth);
153
154 if (line.IsNull())
155 continue;
156
157 TObjArray *arr = line.Tokenize(' ');
158
159 if (arr->GetSize()<8)
160 {
161 cout << "Skip3: " <<line << endl;
162 delete arr;
163 continue;
164 }
165
166 const TVector3 pos(atof((*arr)[0]->GetName()),
167 atof((*arr)[1]->GetName()),
168 atof((*arr)[2]->GetName()));
169
170 const TVector3 norm(atof((*arr)[3]->GetName()),
171 atof((*arr)[4]->GetName()),
172 atof((*arr)[5]->GetName()));
173
174 const Double_t F = atof((*arr)[6]->GetName());
175
176 TString type = (*arr)[7]->GetName();
177 type.Prepend("MMirror");
178
179 TString msg;
180 TClass *cls = MParContainer::GetClass(type);
181 if (!cls)
182 {
183 *fLog << err << dbginf << "ERROR - Class " << type << " not in dictionary." << endl;
184 return kFALSE;
185 }
186
187 if (!cls->InheritsFrom(MMirror::Class()))
188 {
189 *fLog << err << dbginf << "Cannot create new instance of class " << type << ": " << endl;
190 *fLog << "Class doesn't inherit from MMirror." << endl;
191 return kFALSE;
192 }
193
194 MMirror *m = (MMirror*)cls->New();
195 if (!m)
196 {
197 *fLog << err << dbginf << "Cannot create new instance of class " << type << ": " << endl;
198 *fLog << " - Class has no default constructor." << endl;
199 *fLog << " - An abstract member functions of a base class is not overwritten." << endl;
200 return kFALSE;
201 }
202
203 m->SetFocalLength(F);
204 m->SetPosition(pos);
205 m->SetNorm(norm);
206
207 Int_t n = m->ReadM(*arr);
208 if (n<=0)
209 {
210 *fLog << err << dbginf << "ERROR - ReadM failed." << endl;
211 return kFALSE;
212 }
213
214 fMirrors.Add(m);
215
216 //maxr = TMath::Max(maxr, TMath::Hypot(pos[i].X()+24.75, pos[i].Y()+24.75));
217 //maxr = TMath::Max(maxr, TMath::Hypot(pos.X()+24.75, pos.Y()+24.75));
218
219 delete arr;
220 }
221
222 InitMaxR();
223
224 return kTRUE;
225
226// fMirrors.Sort();
227/*
228 for (int i=0; i<964; i++)
229 {
230 MMirror &ref = (MMirror&)*fMirrors[i];
231
232 TArrayD dist(964);
233 for (int j=0; j<964; j++)
234 {
235 const MMirror &mir = (MMirror&)*fMirrors[j];
236 dist[j] = (ref-mir).Mod();
237 }
238
239 TArrayI idx(964);
240 TMath::Sort(964, dist.GetArray(), idx.GetArray(), kFALSE);
241
242 for (int j=0; j<964; j++)
243 {
244 ref.fNeighbors.Add(fMirrors[idx[j]]);
245 }
246 }*/
247}
248
249// --------------------------------------------------------------------------
250//
251// Paint the collection of mirrors
252//
253void MReflector::Paint(Option_t *o)
254{
255 fMirrors.Paint(o);
256 /*
257 TIter Next(&fMirrors);
258 MMirror *m = 0;
259
260 while ((m=static_cast<MMirror*>(Next())))
261 m->Paint(o);*/
262}
263
Note: See TracBrowser for help on using the repository browser.