/RayTracer.cc
Go to the documentation of this file.
1 #include "RayTracer.h"
2 #include "Filter.h"
3 #include "Lens.h"
4 #include "Mirror.h"
5 #include "Camera.h"
6 
7 #include <fdet/Telescope.h>
8 
9 #include <utl/RandomEngine.h>
10 #include <utl/Photon.h>
11 #include <utl/ErrorLogger.h>
12 #include <utl/AugerUnits.h>
13 
14 
15 #include <fwk/RunController.h>
16 #include <evt/Event.h>
17 #include <evt/Header.h>
18 
19 #include <TPolyLine3D.h>
20 #include <TCanvas.h>
21 #include <TObjArray.h>
22 #include <TFile.h>
23 #include <TRandom.h>
24 
25 #include <string>
26 #include <iostream>
27 
28 using namespace std;
29 using namespace utl;
30 using namespace TelescopeSimulatorKG2;
31 
32 
33 void
34 RayTracer::Track::AddPoint(const utl::Point& p)
35 {
36  fX.push_back(p.GetX(fCS));
37  fY.push_back(p.GetY(fCS));
38  fZ.push_back(p.GetZ(fCS));
39 }
40 
41 TPolyLine3D*
42 RayTracer::Track::GetLine()
43 {
44  TPolyLine3D* track = new TPolyLine3D(fX.size(), &fX.front(), &fY.front(), &fZ.front());
45  track->SetLineColor(fColor);
46  return track;
47 }
48 
49 
50 RayTracer::RayTracer(const int Verbosity,
51  const fdet::Telescope& tel,
52  utl::RandomEngine& random,
53  bool simulateShadow,
54  bool simualteCameraSupportShadow,
55  bool simulateMercedesStars,
56  bool simulateFilterStructure,
57  bool simulateHaloEffects,
58  bool simulateGhostEffects,
59  const int maxMirrorReflections,
60  const double mirrorSize,
61  const double mirrorSegmentSigma,
62  const double mirrorRadiusSigma,
63  const double mirrorAbsorptionTop,
64  const double mirrorAbsorptionBot,
65  utl::TabulatedFunction* mirrorDiffusionTop,
66  utl::TabulatedFunction* mirrorDiffusionBot,
67  const double filterIncreaseReflInside,
68  const double filterIncreaseReflOutside,
69  const double filterPosition,
70  const double filterPositionVertical,
71  const double filterPositionVertical2,
72  const double filterPositionHorizontal,
73  const double filterDustAbsorption,
74  const double lensIncreaseRefl,
75  const double lensPosition,
76  const double minLensThickness,
77  const double torusRadius,
78  const double tubeRadius,
79  const double torusZ0,
80  const double pmt_n,
81  bool plotPhotonTracks,
82  bool plotPhotonTracksAtMercedes,
83  double drawPhotonsProbabilty) :
84  fVerbosity(Verbosity),
85  fTel(&tel),
86  fRandom(&random),
87  fSimulateCameraShadow(simulateShadow),
88  fSimualteCameraSupportShadow(simualteCameraSupportShadow),
89  fSimulateMercedesStars(simulateMercedesStars),
90  fSimulateFilterStructure(simulateFilterStructure),
91  fSimulateHaloEffects(simulateHaloEffects),
92  fSimulateGhostEffects(simulateGhostEffects),
93  fMaxMirrorReflections(maxMirrorReflections),
94  fPlotPhotonTracks(plotPhotonTracks),
95  fDrawPhotonProbability(drawPhotonsProbabilty),
96  fNTooManyReflections(0)
97 {
99 
100  fFilter = new Filter(random,
101  tel,
102  filterIncreaseReflInside,
103  filterIncreaseReflOutside,
104  filterPosition,
105  filterPositionVertical,
106  filterPositionVertical2,
107  filterPositionHorizontal,
108  filterDustAbsorption,
110 
111  fLens = new Lens(random,
112  tel,
114  lensIncreaseRefl,
115  lensPosition,
116  minLensThickness,
117  torusRadius,
118  tubeRadius,
119  torusZ0);
120 
121  fMirror = new Mirror(random,
122  tel,
123  mirrorSize,
124  mirrorSegmentSigma,
125  mirrorRadiusSigma,
126  mirrorAbsorptionTop,
127  mirrorAbsorptionBot,
128  mirrorDiffusionTop,
129  mirrorDiffusionBot);
130 
131  fCamera = new Camera(random,
132  tel,
133  plotPhotonTracksAtMercedes,
137  pmt_n);
138 
139  if (plotPhotonTracks)
140  fObjectsPhotons = new TObjArray(100);
141  else
142  fObjectsPhotons = nullptr;
143 }
144 
145 
147 {
148  if (fPlotPhotonTracks) {
149  ostringstream fnameBase, fnameRoot, fnameCanvas;
150  const string id = fwk::RunController::GetInstance().GetCurrentEvent().GetHeader().GetId();
151  fnameBase << "raytrace_" << id
152  << "_Eye" << fTel->GetEyeId()
153  << "_Tel" << fTel->GetId();
154  fnameRoot << fnameBase.str() << ".root";
155  fnameCanvas << fnameBase.str() << ".eps";
156  TCanvas c("view3d");
157  fCamera->Draw();
158  fLens->Draw();
159  fMirror->Draw();
160  fFilter->Draw();
161  for (int iObj = 0; iObj < fObjectsPhotons->GetEntriesFast(); ++iObj) {
162  TPolyLine3D* photon = dynamic_cast<TPolyLine3D*>((*fObjectsPhotons)[iObj]);
163  if (photon)
164  photon->Draw();
165  }
166  c.Print(fnameCanvas.str().c_str());
167  TFile f(fnameRoot.str().c_str(), "RECREATE");
168  c.Write();
169  f.Close();
170  }
171 
172  delete fFilter;
173  delete fLens;
174  delete fMirror;
175  delete fCamera;
176 
177  if (fVerbosity > 0){
178  if (fNTooManyReflections) {
179  ostringstream bugMsg;
180  bugMsg << fNTooManyReflections
181  << " photon(s) lost because of too many mirror reflections (>" << fMaxMirrorReflections << ")";
182  INFO(bugMsg);
183  }
184  }
185 
186 }
187 
191 RTResult
193  utl::Photon& photon, // OUT
194  int& nMercReflections,
195  int& nBackScattered,
196  int& col, int& row)
197 {
198  // count mirror reflections to not get trapped in infinite reflections while simulate Halo
199  nMercReflections = 0;
200  nBackScattered = 0; // -> halo, ghost
201  //fmaxMirrorReflections (= 5 , 3 = up to "ghost")
202  int nMirrorReflections = 0;
203 
204  bool plotTrack = fPlotPhotonTracks;
205  int trackColor = kBlue;
206  //bool plotThisTrack = false;
207  if (fPlotPhotonTracks) {
208  //plotThisTrack = (gRandom->Rndm() < fDrawPhotonProbability);
210  if (photonIn.GetPosition().GetR(telCS) < 0.85 * utl::meter) {
211  trackColor = kRed;
212  }
213  }
214 
215  // the main ray-tracing loop
216  photon = photonIn;
217  RTNext next = eFilter;
218  bool finished = false;
219  bool detected = false;
220  bool terminate = false;
221 
222  // for plotting
224  if (plotTrack) {
225  photonTrack.SetColor(trackColor);
226  photonTrack.AddPoint(photon.GetPosition());
227  fCamera->SetPhotonTrack(&photonTrack);
228  fLens->SetPhotonTrack(&photonTrack);
229  }
230 
231  if (fVerbosity > 1)
232  cout << "RayTracer ------- new photon ------- " << endl;
233 
234  int nReflect = 0; // to count multi-reflections between lens+filter
235 
236  while (!finished) {
237 
238  if (fVerbosity > 1){
240  cout << "RayTracer: w=" << setprecision(3) << setw(6) << photon.GetWeight()
241  << " x=" << setprecision(3) << setw(6) << photon.GetPosition().GetX(telCS)
242  << " y=" << setprecision(3) << setw(6) << photon.GetPosition().GetY(telCS)
243  << " z=" << setprecision(3) << setw(6) << photon.GetPosition().GetZ(telCS)
244  << " nx=" << setprecision(3) << setw(6) << photon.GetDirection().GetX(telCS)
245  << " ny=" << setprecision(3) << setw(6) << photon.GetDirection().GetY(telCS)
246  << " nz=" << setprecision(3) << setw(6) << photon.GetDirection().GetZ(telCS)
247  << " t=" << ios::fixed << photon.GetTime().GetInterval()
248  << " next=" << RTNextName[next] << endl;
249  }
250 
251  // only for reflection counting:
253  const bool backwards = photon.GetDirection().GetZ(telCS) > 0; // toward aperature
254 
255  // ----- debug
256  /*cout << " Photon nMirRefl=" << nMirrorReflections << " nRefl=" << nReflect
257  << " angle=" << ((backwards ? 1 : -1)*photon.GetDirection()).GetTheta(telCS) * 180/3.141 << " ";
258 */
259 
260  switch (next) {
261 
262  case eFilter:
263  next = fFilter->Trace(photon, photon);
264  //if (backwards && next==eLens)
265  //++nReflect;
266  break;
267 
268  case eLens:
269  //TraceWithTorus can also be called without halo. The difference was negligible in drum simulation.
270  //For now always use TraceWithTorus
271  next = fLens->TraceWithTorus(photon, photon);
272  /*
273  if (fSimulateHaloEffects) {
274  //cout << "fLens->TraceWithTorus" << endl;
275  next = fLens->TraceWithTorus(photon, photon);
276  //next = fLens->Trace(photon, photon);
277  } else
278  next = fLens->TraceWithTorus(photon, photon);
279  */
280  if (!backwards && next==eFilter)
281  ++nReflect;
282  /*if (next == eLost)
283  cout << " Photon with " << nReflect << " reflections nMirrorReflections=" << nMirrorReflections << " w=" << photon.GetWeight() << endl;*/
284  break;
285 
286  case eCameraShadow:
287  next = fCamera->TraceShadow(photon, photon, fSimualteCameraSupportShadow);
288  break;
289 
290  case eMirror:
291  next = fMirror->Trace(photon, photon);
292  ++nMirrorReflections;
293  break;
294 
295  case eCamera:
296  next = fCamera->Trace(photon, photon, nMercReflections, nBackScattered, col,row);
297  break;
298 
299  case eLost:
300  finished = true;
301  if (plotTrack)
302  photonTrack.SetColor(kGray+1);
303  break;
304 
305  case eDetected:
306  detected = true;
307  finished = true;
308  break;
309 
310  case eDrawAndExit:
311  //plotThisTrack = true;
312  if (fObjectsPhotons)
313  fObjectsPhotons->Clear();
314  finished = true;
315  terminate = true;
316  break;
317 
318  case SIZE_RTNext:
319  //just to silence warning do nothing
320  ostringstream err;
321  err << "do not use SIZE_RTNext for anything but output";
322  ERROR(err);
323  next = eLost;
324  break;
325 
326  }; // end switch
327 
328 
329  if (plotTrack /* && next!=eLost*/) { // maybe nicer, but confusing in the details ...
330  photonTrack.AddPoint(photon.GetPosition());
331  }
332 
333  if (!detected && nMirrorReflections > fMaxMirrorReflections) {
334  // cout << "too many mirror reflections: " << nMirrorReflections
335  // << ", max = " << fMaxMirrorReflections << endl;
337  /*if (plotTrack)
338  delete photonTrack;*/
339  return eTooManyReflections;
340  }
341 
342  } // end main raytracing loop
343 
344  // RU: debug
345  //if (nReflect>2) {
346  //photon.SetSource(3333);
347  //}
348 
349  if (/*plotThisTrack ||*/ photon.GetSource() == 3333) {// secret number !!!
350 
352  fLens->SetPhotonTrack(0);
353  //if (nMirrorReflections>1)
354  TPolyLine3D* track = photonTrack.GetLine();
355  if (track && fObjectsPhotons) {
356  fObjectsPhotons->Add(track);
357  }
358  //else
359  //delete photonTrack;
360  }
361 
362  if (terminate)
363  return eTerminate;
364 
365  return detected ? eOK : eAbsorbed;
366 
367 } // end of TelescopeSimulator::RayTracing
368 
369 
TObjArray * Draw()
Definition: /Lens.cc:670
Point object.
Definition: Point.h:32
RTNext TraceShadow(const utl::Photon &photonIn, utl::Photon &photonOut, const bool doSupport=true)
Simulates the UV filter in the raytracing of the TelescopeSimulator module.
Simulates the corrector ring in the raytracing of the TelescopeSimulator module.
Definition: /Lens.h:38
double GetR(const CoordinateSystemPtr &coordinateSystem) const
radius r in spherical coordinates coordinates (distance to origin)
Definition: BasicVector.h:257
Class to hold collection (x,y) points and provide interpolation between them.
RTNext Trace(const utl::Photon &photonIn, utl::Photon &photonOut, int &nreflections, int &nbackscattered, int &col, int &row)
unsigned int GetEyeId() const
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
double GetWeight() const
weight assigned to the photon
Definition: Photon.h:21
int GetSource() const
source of the photons. Should use Eye::LightSource enum types
Definition: Photon.h:24
bool HasCorrectorRing() const
flag for corrector ring presence
RTNext Trace(const utl::Photon &photonIn, utl::Photon &photonOut)
boost::shared_ptr< const CoordinateTransformer > CoordinateSystemPtr
Shared pointer for coordinate systems.
Iterator next(Iterator it)
utl::CoordinateSystemPtr GetTelescopeCoordinateSystem() const
double GetX(const CoordinateSystemPtr &coordinateSystem) const
Definition: BasicVector.h:206
Wraps the random number engine used to generate distributions.
Definition: RandomEngine.h:27
constexpr double meter
Definition: AugerUnits.h:81
RTNext Trace(const utl::Photon &photonIn, utl::Photon &photonOut)
Simulate the filter.
RTNext TraceWithTorus(const utl::Photon &photonIn, utl::Photon &photonOut)
Simulate the lens.
Definition: /Lens.cc:341
RTResult Trace(const utl::Photon &photonIn, utl::Photon &photonOut, int &nreflections, int &nbackscattered, int &col, int &row)
Raytracing through the telescope components.
Definition: /RayTracer.cc:192
double GetInterval() const
Get the time interval as a double (in Auger base units)
Definition: TimeInterval.h:69
const utl::Vector & GetDirection() const
Definition: Photon.h:26
double GetY(const CoordinateSystemPtr &coordinateSystem) const
Definition: BasicVector.h:209
Detector description interface for Telescope-related data.
void SetPhotonTrack(RayTracer::Track *const track)
Definition: /Lens.h:58
const std::string RTNextName[SIZE_RTNext]
Definition: /RTResult.h:20
double GetZ(const CoordinateSystemPtr &coordinateSystem) const
Definition: BasicVector.h:212
Simulates the mirror in the raytracing of the TelescopeSimulator module.
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
utl::TimeInterval GetTime() const
Definition: Photon.h:28
unsigned int GetId() const
const fdet::Telescope * fTel
Definition: /RayTracer.h:113
const utl::Point & GetPosition() const
Definition: Photon.h:25

, generated on Tue Sep 26 2023.