SpotPhotonGenerator.cc
Go to the documentation of this file.
1 
11 #include <utl/config.h>
12 
13 #include "SpotPhotonGenerator.h"
14 
15 #include <utl/Reader.h>
16 #include <utl/ErrorLogger.h>
17 #include <utl/AugerUnits.h>
18 #include <utl/MathConstants.h>
19 #include <utl/Math.h>
20 #include <utl/PhysicalConstants.h>
21 #include <utl/PhysicalFunctions.h>
22 #include <utl/Point.h>
23 #include <utl/TabulatedFunction.h>
24 #include <utl/TabulatedFunctionErrors.h>
25 #include <utl/Trace.h>
26 #include <utl/MultiTabulatedFunction.h>
27 #include <utl/Photon.h>
28 #include <utl/RandomEngine.h>
29 #include <utl/UTMPoint.h>
30 #include <utl/Photon.h>
31 
32 #include <fwk/CentralConfig.h>
33 #include <fwk/RunController.h>
34 #include <fwk/CoordinateSystemRegistry.h>
35 #include <fwk/RandomEngineRegistry.h>
36 
37 #include <det/Detector.h>
38 
39 #include <fdet/FDetector.h>
40 #include <fdet/Eye.h>
41 #include <fdet/Telescope.h>
42 #include <fdet/Camera.h>
43 #include <fdet/Pixel.h>
44 #include <fdet/Channel.h>
45 #include <fdet/Mirror.h>
46 #include <fdet/Filter.h>
47 #include <fdet/Corrector.h>
48 
49 #include <evt/Event.h>
50 #include <evt/ShowerSimData.h>
51 
52 #include <fevt/FEvent.h>
53 #include <fevt/Eye.h>
54 #include <fevt/TelescopeSimData.h>
55 #include <fevt/Telescope.h>
56 #include <fevt/PixelSimData.h>
57 #include <fevt/Pixel.h>
58 
59 #include <atm/ProfileResult.h>
60 #include <atm/InclinedAtmosphericProfile.h>
61 
62 #include <CLHEP/Random/Randomize.h>
63 
64 #include <boost/tuple/tuple.hpp>
65 
66 #include <TH2D.h>
67 #include <TDirectory.h>
68 #include <TFile.h>
69 #include <TCanvas.h>
70 #include <TStyle.h>
71 
72 #include <iomanip>
73 #include <fstream>
74 #include <sstream>
75 
76 using namespace SpotPhotonGeneratorOG;
77 using namespace std;
78 using namespace utl;
79 using namespace evt;
80 using namespace fwk;
81 using namespace det;
82 using namespace fdet;
83 using namespace atm;
84 
85 using CLHEP::RandFlat;
86 
87 
88 // -----------------------------------------------------------------------
91 {
92  Branch topB =
93  CentralConfig::GetInstance()->GetTopBranch("SpotPhotonGenerator");
94 
95  if (!topB) {
96  ostringstream err;
97  err << "Missing configuration file for SpotPhotonGenerator";
98  ERROR(err);
99  return eFailure;
100  }
101 
102  fRandomEngine = &RandomEngineRegistry::GetInstance().Get(RandomEngineRegistry::eDetector);
103 
104  //
105  // Initialize the spot simulation mode
106  //
107 
108  topB.GetChild("NSpotPhotons").GetData(fNSpotPhotons);
109  topB.GetChild("MinRDiaPhotons").GetData(fRDiaMin);
110  topB.GetChild("MaxBin").GetData(fMaxBin);
111  topB.GetChild("MinBin").GetData(fMinBin);
112 
113  // ------------------------------------------------
114  //
115  // do some final output
116 
117  // info output
118  ostringstream info;
119  info << " Version: "
120  << GetVersionInfo(VModule::eRevisionNumber) << "\n"
121  " Parameters:\n"
122  " number of photons: " << fNSpotPhotons << "\n"
123  " min radius: " << setw(3) << fRDiaMin/m << "m\n"
124  " min bin: " << fMinBin << "\n"
125  " max bin: " << fMaxBin;
126  INFO(info);
127 
128  return eSuccess;
129 }
130 
131 
132 // -----------------------------------------------------------------------
135 {
136  Detector& detector = Detector::GetInstance();
137  const FDetector& detFD = detector.GetFDetector();
138 
139  if (!event.HasFEvent()) { // initialize event
140  ERROR("No FEvent. Check your ModuleSequence.");
141  return eBreakLoop;
142  }
143 
144  fevt::FEvent& fEvent = event.GetFEvent();
145 
146  static bool initialized = false;
147  if (!initialized) {
148  initialized = true;
149 
150  const TimeStamp& simTime = detector.GetTime();
151 
152  // set the FD event times
153  fEvent.GetHeader().SetTime(simTime);
154  event.GetHeader().SetTime(simTime);
155  ostringstream idStr;
156  //idStr << "SpotSimulation_" << fEventCounter;
157  event.GetHeader().SetId(idStr.str());
158 
159  for (FDetector::EyeIterator iEye = detFD.EyesBegin(); iEye != detFD.EyesEnd(); ++iEye) {
160  const unsigned int eyeId = iEye->GetId();
162 
163  fevt::Eye& eyeEvent = fEvent.GetEye(eyeId, fevt::ComponentSelector::eInDAQ);
164  for (fdet::Eye::TelescopeIterator iTel = iEye->TelescopesBegin();
165  iTel != iEye->TelescopesEnd(); ++iTel) {
166  const unsigned int telId = iTel->GetId();
167 
168  if (eyeEvent.HasTelescope(telId, fevt::ComponentSelector::eInDAQ))
169  ERROR("Telescope already there");
170 
172  fevt::Telescope& telEvent = eyeEvent.GetTelescope(telId, fevt::ComponentSelector::eInDAQ);
173  telEvent.MakeSimData();
174 
175  fevt::TelescopeSimData& telSim = telEvent.GetSimData();
176  telSim.SetPhotonsStartTime(simTime);
177  telSim.SetNumberOfPhotonBins(fMaxBin);
178 
179  }
180  }
181  } // end creating FEvent
182 
183  if (DoSpot(event))
184  return eSuccess;
185 
186  return eBreakLoop;
187 }
188 
189 
190 // -----------------------------------------------------------------------
191 bool
193 {
194  fevt::FEvent& fEvent = event.GetFEvent();
195  Detector& detector = Detector::GetInstance();
196  const FDetector& detFD = detector.GetFDetector();
197 
198  static bool finishedOneSpot = false;
199  if (finishedOneSpot) {
200  FinishSpot();
201  // clean up
202  fevt::TelescopeSimData& telSim =
203  fEvent.GetEye(fCurrentEyeId,
206  telSim.ClearPhotons();
207  goto innerLoop; // jump into the innermost loop to continue with spot list
208  }
209 
210  static fevt::FEvent::EyeIterator iEye;
211  for (iEye = fEvent.EyesBegin(fevt::ComponentSelector::eInDAQ);
212  iEye != fEvent.EyesEnd(fevt::ComponentSelector::eInDAQ); ++iEye) {
213 
214  static fevt::Eye::TelescopeIterator iTel;
215  for (iTel = iEye->TelescopesBegin(fevt::ComponentSelector::eInDAQ);
216  iTel != iEye->TelescopesEnd(fevt::ComponentSelector::eInDAQ); ++iTel) {
217 
218  static unsigned int iPix;
219  for ( iPix = detFD.GetTelescope(*iTel).GetFirstPixelId() ;
220  iPix <= detFD.GetTelescope(*iTel).GetLastPixelId(); ++iPix) {
221  { // need extra scope here
222 
223  const fdet::Telescope& detTel = detFD.GetTelescope(*iTel);
224  const fdet::Pixel& detPixel = detTel.GetPixel(iPix);
225 
226  fCurrentEyeId = iEye->GetId();
227  fCurrentTelId = iTel->GetId();
228  fCurrentPixId = iPix;
229 
230  fevt::TelescopeSimData& telSim = iTel->GetSimData();
231  TelescopeSimulatorKG::TelescopeSimulator* const raytraceModule =
232  dynamic_cast<TelescopeSimulatorKG::TelescopeSimulator*>(&RunController::GetInstance().GetModule("TelescopeSimulatorKG"));
233  fSpotPhotonList.clear();
234  raytraceModule->SetSpotPhotonList(fSpotPhotonList);
235 
236  ostringstream info1;
237  info1 << "generating spot for: eye: " << fCurrentEyeId
238  << ", telescope: " << fCurrentTelId
239  << ", pixel: " << fCurrentPixId;
240  INFO(info1);
241 
242  const double rDia = detTel.GetDiaphragmRadius();
243  const double binWidth = detTel.GetCamera().GetFADCBinSize();
244  const double normWavelength = detFD.GetReferenceLambda();
245 
246  const CoordinateSystemPtr& telCS = detTel.GetTelescopeCoordinateSystem();
247 
248  map<int, TraceD*> traces;
249  for (int i = 0; i < fNSpotPhotons; ++i) {
250  // Generating random point on the diaphragm
251  const double diaPh = RandFlat::shoot(&fRandomEngine->GetEngine(), 0, 2*kPi);
252  const double diaR = sqrt(RandFlat::shoot(&fRandomEngine->GetEngine(),
253  fRDiaMin*fRDiaMin, rDia*rDia));
254 
255  const double xDia = diaR*cos(diaPh);
256  const double yDia = diaR*sin(diaPh);
257  const Point pIn(xDia, yDia, 0, telCS);
258 
259  const double phi = detPixel.GetDirection().GetPhi(telCS);
260  const double theta = detPixel.GetDirection().GetTheta(telCS);
261 
262  Vector nIn (1, theta, phi, telCS, Vector::kSpherical);
263  nIn = -nIn;
264 
265  const double time = binWidth * RandFlat::shoot(&fRandomEngine->GetEngine(),
266  fMinBin, fMaxBin);
267  const double weight = 1;
268  utl::Photon photonIn(pIn, nIn, normWavelength, weight);
269  photonIn.SetTime(TimeInterval(time));
270  telSim.AddPhoton(photonIn);
271  } // loop over raytraces
272  } // end extra scope
273 
274  finishedOneSpot = true;
275  return true;
276 
277  innerLoop:
278  continue;
279 
280  } // end loop over pixels
281 
282  iPix = 1;
283  FinishTelescope();
284 
285  } // end loop tels
286 
287  } // End loop over Eyes
288 
289  return false;
290 }
291 
292 
293 // -----------------------------------------------------------------------
294 void
296 {
297  if (fNSpotPhotons <= 0) {
298  ostringstream err;
299  err << "Cannot continue with fNSpotPhotons=" << fNSpotPhotons;
300  ERROR(err);
301  return;
302  }
303 
304  Detector& detector = Detector::GetInstance();
305  const FDetector& detFD = detector.GetFDetector();
306  const fdet::Pixel& detPixel = detFD.GetEye(fCurrentEyeId).GetTelescope(fCurrentTelId).GetPixel(fCurrentPixId);
307  const CoordinateSystemPtr& pixelCS = detPixel.GetPixelCoordinateSystem();
308 
309  ostringstream spotname;
310  spotname << "Eye_" << fCurrentEyeId << "_Tel_" << fCurrentTelId << "_Pix_" << fCurrentPixId;
311  TH2D* const hSpot = new TH2D(spotname.str().c_str(), spotname.str().c_str(),
312  150, -2.75, 2.75, 150, -2.75, 2.75);
313  hSpot->SetXTitle("X [cm]");
314  hSpot->SetYTitle("Y [cm]");
315  hSpot->SetZTitle("Photons");
316 
317 
318 
319  int nPhotons = 0;
321  iPh != fSpotPhotonList.end(); ++iPh) {
322 
323  if (RandFlat::shoot(&fRandomEngine->GetEngine(), 0., 1.) > iPh->first.GetWeight())
324  continue;
325 
326  ++nPhotons;
327  hSpot->Fill(iPh->first.GetPosition().GetX(pixelCS)/cm,
328  iPh->first.GetPosition().GetY(pixelCS)/cm);
329  }
330 
331  const double efficiency = double(nPhotons) / fNSpotPhotons;
332  const double efficiencyErr = sqrt((efficiency - efficiency*efficiency) / fNSpotPhotons);
333  ostringstream info;
334  info << " Efficiency is : " << setw(9) << setprecision(7) << efficiency
335  << " +- " << efficiencyErr
336  << " for " << fCurrentPixId;
337  INFO(info);
338 
339  fEfficiencies[fCurrentPixId] = efficiency;
340  fEfficienciesErr[fCurrentPixId] = efficiencyErr;
341 
342  gStyle->SetPalette(1);
343  gStyle->SetOptStat(0);
344  gStyle->SetOptTitle(1);
345  TCanvas* const cSpot = new TCanvas("spot", "spot", 500, 500);
346  cSpot->SetFillColor(0);
347  hSpot->Draw("colz");
348  spotname << ".eps";
349  cSpot->Print(spotname.str().c_str());
350 
351  delete hSpot;
352 }
353 
354 
355 // -------------------------------------------------------------------
356 void
358 {
359  ostringstream filename;
360  filename << "Efficiencies_Eye_" << fCurrentEyeId << "_Tel_" << fCurrentTelId << ".dat";
361  ofstream file(filename.str().c_str());
362  ostringstream info;
363  info << " Pixel-Efficiencies for Eye: " << fCurrentEyeId << " Tel: " << fCurrentTelId << "\n";
364 
365  const int lineBreak = 10;
366 
367  double meanErr = 0;
368  for (unsigned int i = 1; i <= fEfficiencies.size(); ++i) {
369  meanErr += fEfficienciesErr[i];
370  if ((i % lineBreak) == 1) {
371  info << " ";
372  file << " ";
373  }
374  info << setw(9) << setprecision(7) << fEfficiencies[i] << " ";
375  file << setw(9) << setprecision(7) << fEfficiencies[i] << " ";
376  if (((i + 1) % lineBreak) == 1) {
377  info << endl;
378  file << endl;
379  }
380  }
381 
382  if (fEfficiencies.size())
383  meanErr /= fEfficiencies.size();
384 
385  info << " <!-- Mean uncertainty on efficiencies is: " << meanErr << " --> \n";
386  file << " <!-- Mean uncertainty on efficiencies is: " << meanErr << " --> \n";
387  info << "\n";
388  file.close();
389  INFO(info);
390 
391  fEfficiencies.clear();
392  fEfficienciesErr.clear();
393 }
394 
395 
396 // -----------------------------------------------------------------------
399 {
400  return eSuccess;
401 }
402 
403 
404 // Configure (x)emacs for this file ...
405 // Local Variables:
406 // mode: c++
407 // compile-command: "make -C .. -k"
408 // End:
Telescope & GetTelescope(const unsigned int telescopeId, const ComponentSelector::Status status=ComponentSelector::eHasData)
Retrieve Telescope by Id, throw exception if not existent.
Definition: FEvent/Eye.cc:57
Branch GetTopBranch() const
Definition: Branch.cc:63
unsigned int GetId() const
Definition: FEvent/Eye.h:54
unsigned int GetId() const
By default from 1..440.
const utl::Vector & GetDirection() const
pointing direction of this pixel
double GetFADCBinSize() const
Point object.
Definition: Point.h:32
double GetPhi(const CoordinateSystemPtr &coordinateSystem) const
azimuth (phi) angle in spherical and cylindrical coordinates
Definition: BasicVector.h:254
Header & GetHeader()
Definition: FEvent.h:92
Fluorescence Detector Eye Event.
Definition: FEvent/Eye.h:29
bool HasFEvent() const
unsigned int GetFirstPixelId() const
double GetTheta(const CoordinateSystemPtr &coordinateSystem) const
zenith (theta) angle in spherical coordinates
Definition: BasicVector.h:248
utl::TimeStamp GetTime() const
Get time pertaining to the detector description.
Definition: Detector.h:134
EyeIterator EyesEnd(const ComponentSelector::Status status)
Definition: FEvent.h:66
double GetDiaphragmRadius() const
#define INFO(message)
Macro for logging informational messages.
Definition: ErrorLogger.h:161
fwk::VModule::ResultFlag Run(evt::Event &event)
Run: invoked once per event.
void AddPhoton(const utl::Photon &p)
void MakeEye(const unsigned int eyeId, const ComponentSelector::Status status=ComponentSelector::eHasData)
Definition: FEvent.cc:115
fwk::VModule::ResultFlag Init()
Initialize: invoked at beginning of run (NOT beginning of event)
const Eye & GetEye(const unsigned int eyeId) const
Find eye by numerical Id.
Definition: FDetector.cc:68
EyeIterator EyesBegin(const FDetComponentSelector::Type type=FDetComponentSelector::ePhysical) const
iterator pointing to first eye of given type (ePhysical, eVirtual, eAll)
Definition: FDetector.h:72
boost::filter_iterator< ComponentSelector, AllEyeIterator > EyeIterator
selective Eye iterators
Definition: FEvent.h:55
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Definition: Branch.cc:211
void MakeTelescope(const unsigned int telescopeId, const ComponentSelector::Status status=ComponentSelector::eHasData)
Make Telescope telescopeId.
Definition: FEvent/Eye.cc:102
const Camera & GetCamera() const
Get the Camera object that belongs to the telescope.
Detector description interface for FDetector-related data.
Definition: FDetector.h:44
A TimeStamp holds GPS second and nanosecond for some event.
Definition: TimeStamp.h:110
const Pixel & GetPixel(const unsigned int pixelId) const
Get Pixel by id, throw utl::NonExistentComponentException if n.a.
void SetPhotonsStartTime(const utl::TimeStamp &ts)
boost::shared_ptr< const CoordinateTransformer > CoordinateSystemPtr
Shared pointer for coordinate systems.
fevt::TelescopeSimData & GetSimData()
Description of simulated data for one Telescope.
Class representing a document branch.
Definition: Branch.h:107
unsigned int GetLastPixelId() const
utl::CoordinateSystemPtr GetTelescopeCoordinateSystem() const
void SetTime(const utl::TimeStamp &time)
Definition: FEvent/Header.h:29
boost::filter_iterator< FDetComponentSelector, AllEyeIterator > EyeIterator
Definition: FDetector.h:69
constexpr double kPi
Definition: MathConstants.h:24
const Telescope & GetTelescope(const unsigned int telescopeId) const
Find Telescope by numerical Id.
Top of the hierarchy of the detector description interface.
Definition: Detector.h:81
EyeIterator EyesBegin(const ComponentSelector::Status status)
Definition: FEvent.h:58
const fdet::FDetector & GetFDetector() const
Definition: Detector.cc:131
fwk::VModule::ResultFlag Finish()
Finish: invoked at end of the run (NOT end of the event)
boost::filter_iterator< ComponentSelector, AllTelescopeIterator > TelescopeIterator
selective Telescope iterators
Definition: FEvent/Eye.h:72
void GetData(bool &b) const
Overloads of the GetData member template function.
Definition: Branch.cc:644
Top of Fluorescence Detector event hierarchy.
Definition: FEvent.h:33
const string file
Eye & GetEye(const unsigned int eyeId, const ComponentSelector::Status status=ComponentSelector::eHasData)
return Eye by id
Definition: FEvent.cc:70
double GetReferenceLambda() const
Definition: FDetector.cc:332
boost::filter_iterator< TelIsCommissioned, InternalConstTelescopeIterator > TelescopeIterator
An iterator over telescopes.
Definition: FDetector/Eye.h:76
const utl::CoordinateSystemPtr & GetPixelCoordinateSystem() const
Detector description interface for Telescope-related data.
A TimeInterval is used to represent time elapsed between two events.
Definition: TimeInterval.h:43
ResultFlag
Flag returned by module methods to the RunController.
Definition: VModule.h:60
const Telescope & GetTelescope(const fevt::Telescope &eventTel) const
Get fdet::Telescope from fevt::Telescope.
Definition: FDetector.cc:150
bool HasTelescope(const unsigned int telescopeId, const ComponentSelector::Status status=ComponentSelector::eHasData) const
Check if the telescope is in the event.
Definition: FEvent/Eye.cc:117
Description of a pixel.
constexpr double cm
Definition: AugerUnits.h:117
Vector object.
Definition: Vector.h:30
Fluorescence Detector Telescope Event.
char * filename
Definition: dump1090.h:266
void SetTime(const utl::TimeInterval &t)
Definition: Photon.h:36
void SetNumberOfPhotonBins(const int n)
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
constexpr double m
Definition: AugerUnits.h:121
EyeIterator EyesEnd(const FDetComponentSelector::Type type=FDetComponentSelector::ePhysical) const
iterator pointing to end of available eyes of given type (ePhysical, eVirtual, eAll) ...
Definition: FDetector.h:76

, generated on Tue Sep 26 2023.