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

Last change on this file since 19917 was 19853, checked in by tbretz, 5 years ago
Added a few additional checks. No default file anymore (as the default file might not reside in this relative path)
File size: 19.6 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 if (wmax-wmin<10 || wmax-wmin>=1000)
248 {
249 *fLog << err << "Wavelength range [" << wmin << ";" << wmax << "] out of range [10;1000)" << endl;
250 if (fRunHeader)
251 *fLog << " MCorsikaRunHeader: [" << fRunHeader->GetWavelengthMin() << ";" << fRunHeader->GetWavelengthMax() << "]" << endl;
252 if (fRunHeader)
253 *fLog << " PhotonDetectionEfficiency: [" << s1->GetXmin() << ";" << s1->GetXmax() << "]" << endl;
254 return kFALSE;
255 }
256
257 const Int_t min = TMath::FloorNint(wmin);
258 const Int_t max = TMath::CeilNint(wmax);
259
260 // Multiply all relevant efficiencies to get the total transmission
261 MParSpline eff;
262 eff.SetFunction("1", max-min, min, max);
263
264 eff.Multiply(*s1->GetSpline());
265 eff.Multiply(*s2->GetSpline());
266 eff.Multiply(*s3->GetSpline());
267
268 // Effectively transmitted wavelength band in the simulated range
269 const Double_t nm = eff.GetSpline()->Integral();
270
271 // Angular acceptance of the cones
272 const Double_t sr = s4 && s4->GetSpline() ? s4->GetSpline()->IntegralSolidAngle() : 1;
273
274 {
275 const Double_t d2 = fGeom->GetCameraDist()*fGeom->GetCameraDist();
276 const Double_t conv = fGeom->GetConvMm2Deg()*TMath::DegToRad();
277 const Double_t f1 = TMath::Min(r->GetA()/1e4, sr*d2) * conv*conv;
278
279 // Rate in GHz / mm^2
280 fScale = fFreqNSB * nm * f1; // [GHz/mm^2] efficiency * m^2 *rad^2 *mm^2
281
282 const Double_t freq0 = fScale*(*fGeom)[0].GetA()*1000;
283
284 *fLog << inf << "Resulting Freq. in " << fNameGeomCam << "[0]: " << Form("%.2f", freq0) << "MHz" << endl;
285
286 // FIXME: Scale with the number of pixels
287 if (freq0>1000)
288 {
289 *fLog << err << "ERROR - Frequency exceeds 1GHz, this might leed to too much memory consumption." << endl;
290 return kFALSE;
291 }
292 }
293
294 if (fFileNameNSB.IsNull())
295 return kTRUE;
296
297 // const MMcRunHeader *mcrunheader = (MMcRunHeader*)pList->FindObject("MMcRunHeader");
298 // Set NumPheFromDNSB
299
300 // # Number of photons from the diffuse NSB (nphe / ns 0.1*0.1 deg^2 239 m^2) and
301 // nsb_mean 0.20
302 // Magic pixel: 0.00885361 deg
303 // dnsbpix = 0.2*50/15
304 // ampl = MMcFadcHeader->GetAmplitud()
305 // sqrt(pedrms*pedrms + dnsbpix*ampl*ampl/ratio)
306
307 // Conversion of the y-axis
308 // ------------------------
309 // Double_t ff = 1; // myJy / arcsec^2 per nm
310 // ff *= 1e-6; // Jy / arcsec^2 per nm
311 // ff *= 3600*3600; // Jy / deg^2
312 // ff *= 1./TMath::DegToRad()/TMath::DegToRad(); // Jy/sr = 1e-26J/s/m^2/Hz/sr
313 // ff *= 1e-26; // J/s/m^2/Hz/sr per nm
314
315 const Double_t arcsec2rad = TMath::DegToRad()/3600.;
316 const Double_t f = 1e-32 / (arcsec2rad*arcsec2rad);
317
318 // Read night sky background flux from file
319 MParSpline flux;
320 if (!flux.ReadFile(fFileNameNSB))
321 return kFALSE;
322
323 if (flux.GetXmin()>wmin)
324 {
325 *fLog << err << "ERROR - NSB flux from " << fFileNameNSB << " must be defined down to " << wmin << "nm." << endl;
326 return kFALSE;
327 }
328 if (flux.GetXmax()<wmax)
329 {
330 *fLog << err << "ERROR - NSB flux from " << fFileNameNSB << " must be defined up to " << wmax << "nm." << endl;
331 return kFALSE;
332 }
333
334 MParSpline nsb;
335
336 // Normalization to our units,
337 // conversion from energy flux to photon flux
338 nsb.SetFunction(Form("%.12e/(x*TMath::H())", f), max-min, min, max);
339
340 // multiply night sky background flux with normalization
341 nsb.Multiply(*flux.GetSpline());
342
343 // Multiply with the total transmission
344 nsb.Multiply(*eff.GetSpline());
345
346 // Check if the photon flux is zero at both ends of the NSB
347 if (eff.GetSpline()->Eval(min)>1e-5)
348 {
349 *fLog << warn << "WARNING - Total transmission efficiency at ";
350 *fLog << min << "nm is not zero, but " << eff.GetSpline()->Eval(min) << "... abort." << endl;
351 }
352 if (eff.GetSpline()->Eval(max)>1e-5)
353 {
354 *fLog << warn << "WARNING - Total transmission efficiency at ";
355 *fLog << max << "nm is not zero, but " << eff.GetSpline()->Eval(max) << "... abort." << endl;
356 }
357
358 // Check if the photon flux is zero at both ends of the simulated region
359 if (eff.GetSpline()->Eval(wmin)>1e-5)
360 {
361 *fLog << (fForce?warn:err) << "ERROR - Total transmission efficiency at ";
362 *fLog << wmin << "nm is not zero... abort." << endl;
363 *fLog << " PhotonDetectionEfficency: " << s1->GetSpline()->Eval(wmin) << endl;
364 *fLog << " ConeTransmission: " << s2->GetSpline()->Eval(wmin) << endl;
365 *fLog << " MirrorReflectivity: " << s3->GetSpline()->Eval(wmin) << endl;
366 *fLog << " TotalEfficiency: " << eff.GetSpline()->Eval(wmin) << endl;
367 if (!fForce)
368 return kFALSE;
369 }
370 if (eff.GetSpline()->Eval(wmax)>1e-5)
371 {
372 *fLog << (fForce?warn:err) << "ERROR - Total transmission efficiency at ";
373 *fLog << wmax << "nm is not zero... abort." << endl;
374 *fLog << " PhotonDetectionEfficency: " << s1->GetSpline()->Eval(wmax) << endl;
375 *fLog << " ConeTransmission: " << s2->GetSpline()->Eval(wmax) << endl;
376 *fLog << " MirrorReflectivity: " << s3->GetSpline()->Eval(wmax) << endl;
377 *fLog << " TotalEfficiency: " << eff.GetSpline()->Eval(wmax) << endl;
378 if (!fForce)
379 return kFALSE;
380 }
381
382 // Conversion from m to radians
383 const Double_t conv = fGeom->GetConvMm2Deg()*TMath::DegToRad()*1e3;
384
385 // Angular acceptance of the cones
386 //const Double_t sr = s5.GetSpline()->IntegralSolidAngle(); // sr
387 // Absolute reflector area
388 const Double_t Ar = r->GetA()/1e4; // m^2
389 // Size of the cone's entrance window
390 const Double_t A0 = (*fGeom)[0].GetA()*1e-6; // m^2
391
392 // Rate * m^2 * Solid Angle
393 // -------------------------
394
395 // Angular acceptance Cones (e.g. 20deg) * Cone-Area
396 const Double_t f1 = A0 * sr; // m^2 sr
397
398 // Mirror-Area * Field of view of cones (e.g. 0.1deg)
399 const Double_t f2 = Ar * A0*conv*conv; // m^2 sr
400
401 // FIXME: Calculate the reflectivity of the bottom by replacing
402 // MirrorReflectivity by bottom reflectivity and reflect
403 // and use it to reflect the difference between f1 and f2
404 // if any.
405
406 // Total NSB rate in MHz per m^2 and sr
407 const Double_t rate = nsb.GetSpline()->Integral() * 1e-6;
408
409 *fLog << inf;
410
411 // Resulting rates as if Razmick's constant had been used
412 // *fLog << 1.75e6/(600-300) * f1 * eff.GetSpline()->Integral() << " MHz" << endl;
413 // *fLog << 1.75e6/(600-300) * f2 * eff.GetSpline()->Integral() << " MHz" << endl;
414
415 *fLog << "Conversion factor Fnu: " << f << endl;
416 *fLog << "Total reflective area: " << Form("%.2f", Ar) << " m" << UTF8::kSquare << endl;
417 *fLog << "Acceptance area of cone 0: " << Form("%.2f", A0*1e6) << " mm" << UTF8::kSquare << " = ";
418 *fLog << A0*conv*conv << " sr" << endl;
419 *fLog << "Cones angular acceptance: " << sr << " sr" << endl;
420 *fLog << "ConeArea*ConeSolidAngle (f1): " << f1 << " m^2 sr" << endl;
421 *fLog << "MirrorArea*ConeSkyAngle (f2): " << f2 << " m^2 sr" << endl;
422 *fLog << "Effective. transmission: " << Form("%.1f", nm) << " nm" << endl;
423 *fLog << "NSB freq. in " << fNameGeomCam << "[0] (f1): " << Form("%.2f", rate * f1) << " MHz" << endl;
424 *fLog << "NSB freq. in " << fNameGeomCam << "[0] (f2): " << Form("%.2f", rate * f2) << " MHz" << endl;
425 *fLog << "Using f2." << endl;
426
427 // Scale the rate per mm^2 and to GHz
428 fScale = rate * f2 / (*fGeom)[0].GetA() / 1000;
429
430 // FIXME: Scale with the number of pixels
431 if (rate*f2>1000)
432 {
433 *fLog << err << "ERROR - Frequency exceeds 1GHz, this might leed to too much memory consumption." << endl;
434 return kFALSE;
435 }
436
437 return kTRUE;
438}
439
440Bool_t MSimRandomPhotons::ReInit(MParList *pList)
441{
442 // Overwrite the default set by MGeomApply
443 fRates->Init(*fGeom);
444 return kTRUE;
445}
446
447// --------------------------------------------------------------------------
448//
449// Check for the necessary containers
450//
451Int_t MSimRandomPhotons::Process()
452{
453 // Get array from event container
454 // const Int_t num = fEvt->GetNumPhotons();
455 //
456 // Do not produce pure pedestal events!
457 // if (num==0)
458 // return kTRUE;
459
460 // Get array from event container
461 // FIXME: Use statistics container instead
462 const UInt_t npix = fGeom->GetNumPixels();
463
464 // This is the possible window in which the triggered digitization
465 // may take place.
466 const Double_t start = fStat->GetTimeFirst();
467 const Double_t end = fStat->GetTimeLast();
468
469 // Loop over all pixels
470 for (UInt_t idx=0; idx<npix; idx++)
471 {
472 // Scale the rate with the pixel size.
473 const Double_t rate = fFreqFixed + fScale*(*fGeom)[idx].GetA();
474
475 (*fRates)[idx].SetPedestal(rate);
476
477 // Calculate the average distance between two consequtive photons
478 const Double_t avglen = 1./rate;
479
480 // Start producing photons at time "start"
481 Double_t t = start;
482 while (1)
483 {
484 // Get a random time for the photon.
485 // The differences are exponentially distributed.
486 t += MMath::RndmExp(avglen);
487
488 // Check if we reached the end of the useful time window
489 if (t>end)
490 break;
491
492 // Add a new photon
493 // FIXME: SLOW!
494 MPhotonData &ph = fEvt->Add();
495
496 // Set source to NightSky, time to t and tag to pixel index
497 ph.SetPrimary(MMcEvtBasic::kNightSky);
498 ph.SetWeight();
499 ph.SetTime(t);
500 ph.SetTag(idx);
501
502 // fProductionHeight, fPosX, fPosY, fCosU, fCosV (irrelevant) FIXME: Reset?
503
504 if (fSimulateWavelength)
505 {
506 const Float_t wmin = fRunHeader->GetWavelengthMin();
507 const Float_t wmax = fRunHeader->GetWavelengthMax();
508
509 ph.SetWavelength(TMath::Nint(gRandom->Uniform(wmin, wmax)));
510 }
511 }
512 }
513
514 // Re-sort the photons by time!
515 fEvt->Sort(kTRUE);
516
517 // Update maximum index
518 fStat->SetMaxIndex(npix-1);
519
520 // Shrink
521 return kTRUE;
522}
523
524// --------------------------------------------------------------------------
525//
526// Read the parameters from the resource file.
527//
528// FrequencyFixed: 0.040
529// FrequencyNSB: 5.8
530//
531// The fixed frequency is given in units fitting the units of the time.
532// Usually the time is given in nanoseconds thus, e.g., 0.040 means 40MHz.
533//
534// The FrequencyNSB is scaled by the area of the pixel in cm^2. Therefore
535// 0.040 would mean 40MHz/cm^2
536//
537Int_t MSimRandomPhotons::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
538{
539 Bool_t rc = kFALSE;
540 if (IsEnvDefined(env, prefix, "Force", print))
541 {
542 rc = kTRUE;
543 fForce = GetEnvValue(env, prefix, "Force", fForce);
544 }
545
546 if (IsEnvDefined(env, prefix, "FrequencyFixed", print))
547 {
548 rc = kTRUE;
549 fFreqFixed = GetEnvValue(env, prefix, "FrequencyFixed", fFreqFixed);
550 }
551
552 if (IsEnvDefined(env, prefix, "FrequencyNSB", print))
553 {
554 rc = kTRUE;
555 fFreqNSB = GetEnvValue(env, prefix, "FrequencyNSB", fFreqNSB);
556 }
557
558 if (IsEnvDefined(env, prefix, "FileNameNSB", print))
559 {
560 rc = kTRUE;
561 fFileNameNSB = GetEnvValue(env, prefix, "FileNameNSB", fFileNameNSB);
562 }
563
564 if (IsEnvDefined(env, prefix, "SimulateCherenkovSpectrum", print))
565 {
566 rc = kTRUE;
567 fSimulateWavelength = GetEnvValue(env, prefix, "SimulateCherenkovSpectrum", fSimulateWavelength);
568 }
569
570 return rc;
571}
Note: See TracBrowser for help on using the repository browser.