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

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