source: trunk/Mars/msimcamera/MSimRandomPhotons.cc@ 19848

Last change on this file since 19848 was 19542, checked in by tbretz, 5 years ago
Allow to turn some checks off for testing purposes (only\!), replaces MReflector by the new base class MOptics
File size: 19.2 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// MSimRandomPhotons
28//
29// Simulate poissonian photons. Since the distribution of the arrival time
30// differences of these photons is an exonential we can simulate them
31// using exponentially distributed time differences between two consecutive
32// photons.
33//
34// FIXME: We should add the wavelength distribution.
35//
36// The artificial night sky background rate is calculated as follows:
37//
38// * The photon detection efficiency vs. wavelength of the detector is obtained
39// from "PhotonDetectionEfficiency" of type "MParSpline"
40//
41// * The angular acceptance of the light collectors is obtained
42// from "ConesAngularAcceptance" of type "MParSpline"
43//
44// * The spectral acceptance of the light collectors is obtained
45// from "ConesTransmission" of type "MParSpline"
46//
47// * The reflectivity of the mirrors vs wavelength is obtained
48// from "MirrorReflectivity" of type "MParSpline"
49//
50// The rate is then calculated as
51//
52// R = R0 * Ai * f
53//
54// R0 is the night sky background rate as given in Eckart's paper (divided
55// by the wavelength window). Ai the area of the cones acceptance window,
56// f is given as:
57//
58// f = nm * Min(Ar, sr*d^2)
59//
60// with
61//
62// nm being the integral of the product of the mirror reflectivity, the cone
63// transmission and the photon detection efficiency.
64//
65// d the distance of the focal plane to the mirror
66//
67// Ar is the total reflective area of the reflector
68//
69// sr is the effective solid angle corresponding to the integral of the
70// cones angular acceptance
71//
72// Alternatively, the night-sky background rate can be calculated from
73// a spectrum as given in Fig. 1 (but versus Nanometers) in
74//
75// Chris R. Benn & Sara L. Ellison La Palma Night-Sky Brightness
76//
77// After proper conversion of the units, the rate of the pixel 0
78// is then calculated by
79//
80// rate = f * nsb
81//
82// With nsb
83//
84// nsb = Integral(nsb spectrum * combines efficiencies)
85//
86// and f can be either
87//
88// Eff. angular acceptance Cones (e.g. 20deg) * Cone-Area (mm^2)
89// f = sr * A0
90//
91// or
92//
93// Mirror-Area * Field of view of cones (deg^2)
94// f = Ar * A0;
95//
96//
97// Input Containers:
98// fNameGeomCam [MGeomCam]
99// MPhotonEvent
100// MPhotonStatistics
101// MCorsikaEvtHeader
102// [MCorsikaRunHeader]
103//
104// Output Containers:
105// MPhotonEvent
106// AccidentalPhotonRate [MPedestalCam]
107//
108//////////////////////////////////////////////////////////////////////////////
109#include "MSimRandomPhotons.h"
110
111#include <TRandom.h>
112
113#include "MMath.h" // RndmExp
114
115#include "MLog.h"
116#include "MLogManip.h"
117
118#include "MParList.h"
119
120#include "MGeomCam.h"
121#include "MGeom.h"
122
123#include "MPhotonEvent.h"
124#include "MPhotonData.h"
125
126#include "MPedestalCam.h"
127#include "MPedestalPix.h"
128
129#include "MCorsikaRunHeader.h"
130
131#include "MSpline3.h"
132#include "MParSpline.h"
133#include "MOptics.h"
134
135ClassImp(MSimRandomPhotons);
136
137using namespace std;
138
139// --------------------------------------------------------------------------
140//
141// Default Constructor.
142//
143MSimRandomPhotons::MSimRandomPhotons(const char* name, const char *title)
144 : fGeom(0), fEvt(0), fStat(0), /*fEvtHeader(0),*/ fRunHeader(0),
145 fRates(0), fSimulateWavelength(kFALSE), fNameGeomCam("MGeomCam"),
146 fFileNameNSB("resmc/night-sky-la-palma.txt"), fForce(kFALSE)
147{
148 fName = name ? name : "MSimRandomPhotons";
149 fTitle = title ? title : "Simulate possonian photons (like NSB or dark current)";
150}
151
152// --------------------------------------------------------------------------
153//
154// Check for the necessary containers
155//
156Int_t MSimRandomPhotons::PreProcess(MParList *pList)
157{
158 fGeom = (MGeomCam*)pList->FindObject(fNameGeomCam, "MGeomCam");
159 if (!fGeom)
160 {
161 *fLog << inf << fNameGeomCam << " [MGeomCam] not found..." << endl;
162
163 fGeom = (MGeomCam*)pList->FindObject("MGeomCam");
164 if (!fGeom)
165 {
166 *fLog << err << "MGeomCam not found... aborting." << endl;
167 return kFALSE;
168 }
169 }
170
171 fEvt = (MPhotonEvent*)pList->FindObject("MPhotonEvent");
172 if (!fEvt)
173 {
174 *fLog << err << "MPhotonEvent not found... aborting." << endl;
175 return kFALSE;
176 }
177
178 fStat = (MPhotonStatistics*)pList->FindObject("MPhotonStatistics");
179 if (!fStat)
180 {
181 *fLog << err << "MPhotonStatistics not found... aborting." << endl;
182 return kFALSE;
183 }
184
185 fRates = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", "AccidentalPhotonRates");
186 if (!fRates)
187 return kFALSE;
188
189 /*
190 fEvtHeader = (MCorsikaEvtHeader*)pList->FindObject("MCorsikaEvtHeader");
191 if (!fEvtHeader)
192 {
193 *fLog << err << "MCorsikaEvtHeader not found... aborting." << endl;
194 return kFALSE;
195 }*/
196
197 fRunHeader = (MCorsikaRunHeader*)pList->FindObject("MCorsikaRunHeader");
198 if (fSimulateWavelength && !fRunHeader)
199 {
200 *fLog << err << "MCorsikaRunHeader not found... aborting." << endl;
201 return kFALSE;
202 }
203
204 MOptics *r = (MOptics*)pList->FindObject("Reflector", "MOptics");
205 if (!r)
206 {
207 *fLog << err << "Reflector [MOptics] not found... aborting." << endl;
208 return kFALSE;
209 }
210
211 const MParSpline *s1 = (MParSpline*)pList->FindObject("PhotonDetectionEfficiency", "MParSpline");
212 const MParSpline *s2 = (MParSpline*)pList->FindObject("ConesTransmission", "MParSpline");
213 const MParSpline *s3 = (MParSpline*)pList->FindObject("MirrorReflectivity", "MParSpline");
214 const MParSpline *s4 = (MParSpline*)pList->FindObject("ConesAngularAcceptance", "MParSpline");
215
216 // Ensure that all efficiencies are at least defined in the raneg of the
217 // photon detection efficiency. We assume that this is the limiting factor
218 // and has to be zero at both ends.
219 if (s2->GetXmin()>s1->GetXmin())
220 {
221 *fLog << err << "ERROR - ConeTransmission range must be defined down to " << s1->GetXmin() << "nm (PhotonDetectionEffciency)." << endl;
222 return kFALSE;
223 }
224 if (s2->GetXmax()<s1->GetXmax())
225 {
226 *fLog << err << "ERROR - ConeTransmission range must be defined up to " << s1->GetXmax() << "nm (PhotonDetectionEffciency)." << endl;
227 return kFALSE;
228 }
229 if (s3->GetXmin()>s1->GetXmin())
230 {
231 *fLog << err << "ERROR - MirrorReflectivity range must be defined down to " << s1->GetXmin() << "nm (PhotonDetectionEffciency)." << endl;
232 return kFALSE;
233 }
234 if (s3->GetXmax()<s1->GetXmax())
235 {
236 *fLog << err << "ERROR - MirrorReflectivity range must be defined up to " << s1->GetXmax() << "nm (PhotonDetectionEffciency)." << endl;
237 return kFALSE;
238 }
239
240 // If the simulated wavelength range exists and is smaller, reduce the
241 // range to it. Later it is checked that at both edges the transmission
242 // is 0. This must be true in both cases: The simulated wavelength range
243 // exceed the PDE or the PDE range exceeds the simulated waveband.
244 const Float_t wmin = fRunHeader && fRunHeader->GetWavelengthMin()>s1->GetXmin() ? fRunHeader->GetWavelengthMin() : s1->GetXmin();
245 const Float_t wmax = fRunHeader && fRunHeader->GetWavelengthMax()<s1->GetXmax() ? fRunHeader->GetWavelengthMax() : s1->GetXmax();
246
247 const Int_t min = TMath::FloorNint(wmin);
248 const Int_t max = TMath::CeilNint(wmax);
249
250 // Multiply all relevant efficiencies to get the total transmission
251 MParSpline eff;
252 eff.SetFunction("1", max-min, min, max);
253
254 eff.Multiply(*s1->GetSpline());
255 eff.Multiply(*s2->GetSpline());
256 eff.Multiply(*s3->GetSpline());
257
258 // Effectively transmitted wavelength band in the simulated range
259 const Double_t nm = eff.GetSpline()->Integral();
260
261 // Angular acceptance of the cones
262 const Double_t sr = s4 && s4->GetSpline() ? s4->GetSpline()->IntegralSolidAngle() : 1;
263
264 {
265 const Double_t d2 = fGeom->GetCameraDist()*fGeom->GetCameraDist();
266 const Double_t conv = fGeom->GetConvMm2Deg()*TMath::DegToRad();
267 const Double_t f1 = TMath::Min(r->GetA()/1e4, sr*d2) * conv*conv;
268
269 // Rate in GHz / mm^2
270 fScale = fFreqNSB * nm * f1; // [GHz/mm^2] efficiency * m^2 *rad^2 *mm^2
271
272 const Double_t freq0 = fScale*(*fGeom)[0].GetA()*1000;
273
274 *fLog << inf << "Resulting Freq. in " << fNameGeomCam << "[0]: " << Form("%.2f", freq0) << "MHz" << endl;
275
276 // FIXME: Scale with the number of pixels
277 if (freq0>1000)
278 {
279 *fLog << err << "ERROR - Frequency exceeds 1GHz, this might leed to too much memory consumption." << endl;
280 return kFALSE;
281 }
282 }
283
284 if (fFileNameNSB.IsNull())
285 return kTRUE;
286
287 // const MMcRunHeader *mcrunheader = (MMcRunHeader*)pList->FindObject("MMcRunHeader");
288 // Set NumPheFromDNSB
289
290 // # Number of photons from the diffuse NSB (nphe / ns 0.1*0.1 deg^2 239 m^2) and
291 // nsb_mean 0.20
292 // Magic pixel: 0.00885361 deg
293 // dnsbpix = 0.2*50/15
294 // ampl = MMcFadcHeader->GetAmplitud()
295 // sqrt(pedrms*pedrms + dnsbpix*ampl*ampl/ratio)
296
297 // Conversion of the y-axis
298 // ------------------------
299 // Double_t ff = 1; // myJy / arcsec^2 per nm
300 // ff *= 1e-6; // Jy / arcsec^2 per nm
301 // ff *= 3600*3600; // Jy / deg^2
302 // ff *= 1./TMath::DegToRad()/TMath::DegToRad(); // Jy/sr = 1e-26J/s/m^2/Hz/sr
303 // ff *= 1e-26; // J/s/m^2/Hz/sr per nm
304
305 const Double_t arcsec2rad = TMath::DegToRad()/3600.;
306 const Double_t f = 1e-32 / (arcsec2rad*arcsec2rad);
307
308 // Read night sky background flux from file
309 MParSpline flux;
310 if (!flux.ReadFile(fFileNameNSB))
311 return kFALSE;
312
313 if (flux.GetXmin()>wmin)
314 {
315 *fLog << err << "ERROR - NSB flux from " << fFileNameNSB << " must be defined down to " << wmin << "nm." << endl;
316 return kFALSE;
317 }
318 if (flux.GetXmax()<wmax)
319 {
320 *fLog << err << "ERROR - NSB flux from " << fFileNameNSB << " must be defined up to " << wmax << "nm." << endl;
321 return kFALSE;
322 }
323
324 MParSpline nsb;
325
326 // Normalization to our units,
327 // conversion from energy flux to photon flux
328 nsb.SetFunction(Form("%.12e/(x*TMath::H())", f), max-min, min, max);
329
330 // multiply night sky background flux with normalization
331 nsb.Multiply(*flux.GetSpline());
332
333 // Multiply with the total transmission
334 nsb.Multiply(*eff.GetSpline());
335
336 // Check if the photon flux is zero at both ends of the NSB
337 if (eff.GetSpline()->Eval(min)>1e-5)
338 {
339 *fLog << warn << "WARNING - Total transmission efficiency at ";
340 *fLog << min << "nm is not zero, but " << eff.GetSpline()->Eval(min) << "... abort." << endl;
341 }
342 if (eff.GetSpline()->Eval(max)>1e-5)
343 {
344 *fLog << warn << "WARNING - Total transmission efficiency at ";
345 *fLog << max << "nm is not zero, but " << eff.GetSpline()->Eval(max) << "... abort." << endl;
346 }
347
348 // Check if the photon flux is zero at both ends of the simulated region
349 if (eff.GetSpline()->Eval(wmin)>1e-5)
350 {
351 *fLog << (fForce?warn:err) << "ERROR - Total transmission efficiency at ";
352 *fLog << wmin << "nm is not zero... abort." << endl;
353 *fLog << " PhotonDetectionEfficency: " << s1->GetSpline()->Eval(wmin) << endl;
354 *fLog << " ConeTransmission: " << s2->GetSpline()->Eval(wmin) << endl;
355 *fLog << " MirrorReflectivity: " << s3->GetSpline()->Eval(wmin) << endl;
356 *fLog << " TotalEfficiency: " << eff.GetSpline()->Eval(wmin) << endl;
357 if (!fForce)
358 return kFALSE;
359 }
360 if (eff.GetSpline()->Eval(wmax)>1e-5)
361 {
362 *fLog << (fForce?warn:err) << "ERROR - Total transmission efficiency at ";
363 *fLog << wmax << "nm is not zero... abort." << endl;
364 *fLog << " PhotonDetectionEfficency: " << s1->GetSpline()->Eval(wmax) << endl;
365 *fLog << " ConeTransmission: " << s2->GetSpline()->Eval(wmax) << endl;
366 *fLog << " MirrorReflectivity: " << s3->GetSpline()->Eval(wmax) << endl;
367 *fLog << " TotalEfficiency: " << eff.GetSpline()->Eval(wmax) << endl;
368 if (!fForce)
369 return kFALSE;
370 }
371
372 // Conversion from m to radians
373 const Double_t conv = fGeom->GetConvMm2Deg()*TMath::DegToRad()*1e3;
374
375 // Angular acceptance of the cones
376 //const Double_t sr = s5.GetSpline()->IntegralSolidAngle(); // sr
377 // Absolute reflector area
378 const Double_t Ar = r->GetA()/1e4; // m^2
379 // Size of the cone's entrance window
380 const Double_t A0 = (*fGeom)[0].GetA()*1e-6; // m^2
381
382 // Rate * m^2 * Solid Angle
383 // -------------------------
384
385 // Angular acceptance Cones (e.g. 20deg) * Cone-Area
386 const Double_t f1 = A0 * sr; // m^2 sr
387
388 // Mirror-Area * Field of view of cones (e.g. 0.1deg)
389 const Double_t f2 = Ar * A0*conv*conv; // m^2 sr
390
391 // FIXME: Calculate the reflectivity of the bottom by replacing
392 // MirrorReflectivity by bottom reflectivity and reflect
393 // and use it to reflect the difference between f1 and f2
394 // if any.
395
396 // Total NSB rate in MHz per m^2 and sr
397 const Double_t rate = nsb.GetSpline()->Integral() * 1e-6;
398
399 *fLog << inf;
400
401 // Resulting rates as if Razmick's constant had been used
402 // *fLog << 1.75e6/(600-300) * f1 * eff.GetSpline()->Integral() << " MHz" << endl;
403 // *fLog << 1.75e6/(600-300) * f2 * eff.GetSpline()->Integral() << " MHz" << endl;
404
405 *fLog << "Conversion factor Fnu: " << f << endl;
406 *fLog << "Total reflective area: " << Form("%.2f", Ar) << " m" << UTF8::kSquare << endl;
407 *fLog << "Acceptance area of cone 0: " << Form("%.2f", A0*1e6) << " mm" << UTF8::kSquare << " = ";
408 *fLog << A0*conv*conv << " sr" << endl;
409 *fLog << "Cones angular acceptance: " << sr << " sr" << endl;
410 *fLog << "ConeArea*ConeSolidAngle (f1): " << f1 << " m^2 sr" << endl;
411 *fLog << "MirrorArea*ConeSkyAngle (f2): " << f2 << " m^2 sr" << endl;
412 *fLog << "Effective. transmission: " << Form("%.1f", nm) << " nm" << endl;
413 *fLog << "NSB freq. in " << fNameGeomCam << "[0] (f1): " << Form("%.2f", rate * f1) << " MHz" << endl;
414 *fLog << "NSB freq. in " << fNameGeomCam << "[0] (f2): " << Form("%.2f", rate * f2) << " MHz" << endl;
415 *fLog << "Using f2." << endl;
416
417 // Scale the rate per mm^2 and to GHz
418 fScale = rate * f2 / (*fGeom)[0].GetA() / 1000;
419
420 // FIXME: Scale with the number of pixels
421 if (rate*f2>1000)
422 {
423 *fLog << err << "ERROR - Frequency exceeds 1GHz, this might leed to too much memory consumption." << endl;
424 return kFALSE;
425 }
426
427 return kTRUE;
428}
429
430Bool_t MSimRandomPhotons::ReInit(MParList *pList)
431{
432 // Overwrite the default set by MGeomApply
433 fRates->Init(*fGeom);
434 return kTRUE;
435}
436
437// --------------------------------------------------------------------------
438//
439// Check for the necessary containers
440//
441Int_t MSimRandomPhotons::Process()
442{
443 // Get array from event container
444 // const Int_t num = fEvt->GetNumPhotons();
445 //
446 // Do not produce pure pedestal events!
447 // if (num==0)
448 // return kTRUE;
449
450 // Get array from event container
451 // FIXME: Use statistics container instead
452 const UInt_t npix = fGeom->GetNumPixels();
453
454 // This is the possible window in which the triggered digitization
455 // may take place.
456 const Double_t start = fStat->GetTimeFirst();
457 const Double_t end = fStat->GetTimeLast();
458
459 // Loop over all pixels
460 for (UInt_t idx=0; idx<npix; idx++)
461 {
462 // Scale the rate with the pixel size.
463 const Double_t rate = fFreqFixed + fScale*(*fGeom)[idx].GetA();
464
465 (*fRates)[idx].SetPedestal(rate);
466
467 // Calculate the average distance between two consequtive photons
468 const Double_t avglen = 1./rate;
469
470 // Start producing photons at time "start"
471 Double_t t = start;
472 while (1)
473 {
474 // Get a random time for the photon.
475 // The differences are exponentially distributed.
476 t += MMath::RndmExp(avglen);
477
478 // Check if we reached the end of the useful time window
479 if (t>end)
480 break;
481
482 // Add a new photon
483 // FIXME: SLOW!
484 MPhotonData &ph = fEvt->Add();
485
486 // Set source to NightSky, time to t and tag to pixel index
487 ph.SetPrimary(MMcEvtBasic::kNightSky);
488 ph.SetWeight();
489 ph.SetTime(t);
490 ph.SetTag(idx);
491
492 // fProductionHeight, fPosX, fPosY, fCosU, fCosV (irrelevant) FIXME: Reset?
493
494 if (fSimulateWavelength)
495 {
496 const Float_t wmin = fRunHeader->GetWavelengthMin();
497 const Float_t wmax = fRunHeader->GetWavelengthMax();
498
499 ph.SetWavelength(TMath::Nint(gRandom->Uniform(wmin, wmax)));
500 }
501 }
502 }
503
504 // Re-sort the photons by time!
505 fEvt->Sort(kTRUE);
506
507 // Update maximum index
508 fStat->SetMaxIndex(npix-1);
509
510 // Shrink
511 return kTRUE;
512}
513
514// --------------------------------------------------------------------------
515//
516// Read the parameters from the resource file.
517//
518// FrequencyFixed: 0.040
519// FrequencyNSB: 5.8
520//
521// The fixed frequency is given in units fitting the units of the time.
522// Usually the time is given in nanoseconds thus, e.g., 0.040 means 40MHz.
523//
524// The FrequencyNSB is scaled by the area of the pixel in cm^2. Therefore
525// 0.040 would mean 40MHz/cm^2
526//
527Int_t MSimRandomPhotons::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
528{
529 Bool_t rc = kFALSE;
530 if (IsEnvDefined(env, prefix, "Force", print))
531 {
532 rc = kTRUE;
533 fForce = GetEnvValue(env, prefix, "Force", fForce);
534 }
535
536 if (IsEnvDefined(env, prefix, "FrequencyFixed", print))
537 {
538 rc = kTRUE;
539 fFreqFixed = GetEnvValue(env, prefix, "FrequencyFixed", fFreqFixed);
540 }
541
542 if (IsEnvDefined(env, prefix, "FrequencyNSB", print))
543 {
544 rc = kTRUE;
545 fFreqNSB = GetEnvValue(env, prefix, "FrequencyNSB", fFreqNSB);
546 }
547
548 if (IsEnvDefined(env, prefix, "FileNameNSB", print))
549 {
550 rc = kTRUE;
551 fFileNameNSB = GetEnvValue(env, prefix, "FileNameNSB", fFileNameNSB);
552 }
553
554 if (IsEnvDefined(env, prefix, "SimulateCherenkovSpectrum", print))
555 {
556 rc = kTRUE;
557 fSimulateWavelength = GetEnvValue(env, prefix, "SimulateCherenkovSpectrum", fSimulateWavelength);
558 }
559
560 return rc;
561}
Note: See TracBrowser for help on using the repository browser.