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

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