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

Last change on this file since 9930 was 9913, checked in by tbretz, 14 years ago
Improved the range checks in MSimRandomPhotons.
File size: 17.8 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 "MReflector.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")
147{
148 fName = name ? name : "MSimRandomPhotons";
149 fTitle = title ? title : "Simulate possonian photons (like NSB or dark current)";
150}
151
152Bool_t MSimRandomPhotons::CheckWavelengthRange(const MParSpline &sp, const char *txt) const
153{
154 const Float_t min = sp.GetXmin();
155 const Float_t max = sp.GetXmax();
156
157 if (min>fRunHeader->GetWavelengthMin())
158 {
159 *fLog << err << "ERROR - Minimum wavelength (" << min << "nm)";
160 *fLog << " defined for " << txt;
161 *fLog << " exceeds minimum wavelength simulated (";
162 *fLog << fRunHeader->GetWavelengthMin() << "nm)." << endl;
163 return kFALSE;
164 }
165 if (max<fRunHeader->GetWavelengthMax())
166 {
167 *fLog << err << "ERROR - Maximum wavelength (" << max << "nm)";
168 *fLog << " defined for " << txt;
169 *fLog << " undershoots maximum wavelength simulated (";
170 *fLog << fRunHeader->GetWavelengthMax() << "nm)." << endl;
171 return kFALSE;
172 }
173
174 return kTRUE;
175}
176
177// --------------------------------------------------------------------------
178//
179// Check for the necessary containers
180//
181Int_t MSimRandomPhotons::PreProcess(MParList *pList)
182{
183 fGeom = (MGeomCam*)pList->FindObject(fNameGeomCam, "MGeomCam");
184 if (!fGeom)
185 {
186 *fLog << inf << fNameGeomCam << " [MGeomCam] not found..." << endl;
187
188 fGeom = (MGeomCam*)pList->FindObject("MGeomCam");
189 if (!fGeom)
190 {
191 *fLog << err << "MGeomCam not found... aborting." << endl;
192 return kFALSE;
193 }
194 }
195
196 fEvt = (MPhotonEvent*)pList->FindObject("MPhotonEvent");
197 if (!fEvt)
198 {
199 *fLog << err << "MPhotonEvent not found... aborting." << endl;
200 return kFALSE;
201 }
202
203 fStat = (MPhotonStatistics*)pList->FindObject("MPhotonStatistics");
204 if (!fStat)
205 {
206 *fLog << err << "MPhotonStatistics not found... aborting." << endl;
207 return kFALSE;
208 }
209
210 fRates = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", "AccidentalPhotonRates");
211 if (!fRates)
212 return kFALSE;
213
214 /*
215 fEvtHeader = (MCorsikaEvtHeader*)pList->FindObject("MCorsikaEvtHeader");
216 if (!fEvtHeader)
217 {
218 *fLog << err << "MCorsikaEvtHeader not found... aborting." << endl;
219 return kFALSE;
220 }*/
221
222 fRunHeader = (MCorsikaRunHeader*)pList->FindObject("MCorsikaRunHeader");
223 if (!fRunHeader)
224 {
225 *fLog << err << "MCorsikaRunHeader not found... aborting." << endl;
226 return kFALSE;
227 }
228
229 MReflector *r = (MReflector*)pList->FindObject("Reflector", "MReflector");
230 if (!r)
231 {
232 *fLog << err << "Reflector [MReflector] not found... aborting." << endl;
233 return kFALSE;
234 }
235
236 const MParSpline *s1 = (MParSpline*)pList->FindObject("PhotonDetectionEfficiency", "MParSpline");
237 const MParSpline *s2 = (MParSpline*)pList->FindObject("ConesTransmission", "MParSpline");
238 const MParSpline *s3 = (MParSpline*)pList->FindObject("MirrorReflectivity", "MParSpline");
239 const MParSpline *s4 = (MParSpline*)pList->FindObject("ConesAngularAcceptance", "MParSpline");
240
241 // Check if all splines are defined in the relevant range
242 if (!CheckWavelengthRange(*s1, "PhotonDetectionEfficiency [MParSpline]"))
243 return kFALSE;
244 if (!CheckWavelengthRange(*s2, "ConesTransmission [MParSpline]"))
245 return kFALSE;
246 if (!CheckWavelengthRange(*s3, "MirrorReflectivity [MParSpline]"))
247 return kFALSE;
248
249 const Float_t wmin = fRunHeader->GetWavelengthMin();
250 const Float_t wmax = fRunHeader->GetWavelengthMax();
251
252 const Int_t min = TMath::FloorNint(wmin);
253 const Int_t max = TMath::CeilNint(wmax);
254
255 // Multiply all relevant efficiencies to get the total transmission
256 MParSpline eff;
257 eff.SetFunction("1", max-min, min, max);
258
259 eff.Multiply(*s1->GetSpline());
260 eff.Multiply(*s2->GetSpline());
261 eff.Multiply(*s3->GetSpline());
262
263 // Effectively transmitted wavelength band in the simulated range
264 const Double_t nm = eff.GetSpline()->Integral();
265
266 // Angular acceptance of the cones
267 const Double_t sr = s4 && s4->GetSpline() ? s4->GetSpline()->IntegralSolidAngle() : 1;
268
269 {
270 const Double_t d2 = fGeom->GetCameraDist()*fGeom->GetCameraDist();
271 const Double_t conv = fGeom->GetConvMm2Deg()*TMath::DegToRad();
272 const Double_t f1 = TMath::Min(r->GetA()/1e4, sr*d2) * conv*conv;
273
274 // Rate in GHz / mm^2
275 fScale = fFreqNSB * nm * f1; // [GHz/mm^2] efficiency * m^2 *rad^2 *mm^2
276
277 const Double_t freq0 = fScale*(*fGeom)[0].GetA()*1000;
278
279 *fLog << inf << "Resulting Freq. in " << fNameGeomCam << "[0]: " << Form("%.2f", freq0) << "MHz" << endl;
280
281 // FIXME: Scale with the number of pixels
282 if (freq0>1000)
283 {
284 *fLog << err << "ERROR - Frequency exceeds 1GHz, this might leed to too much memory consumption." << endl;
285 return kFALSE;
286 }
287 }
288
289 if (fFileNameNSB.IsNull())
290 return kTRUE;
291
292 // const MMcRunHeader *mcrunheader = (MMcRunHeader*)pList->FindObject("MMcRunHeader");
293 // Set NumPheFromDNSB
294
295 // # Number of photons from the diffuse NSB (nphe / ns 0.1*0.1 deg^2 239 m^2) and
296 // nsb_mean 0.20
297 // Magic pixel: 0.00885361 deg
298 // dnsbpix = 0.2*50/15
299 // ampl = MMcFadcHeader->GetAmplitud()
300 // sqrt(pedrms*pedrms + dnsbpix*ampl*ampl/ratio)
301
302 // Conversion of the y-axis
303 // ------------------------
304 // Double_t ff = 1; // myJy / arcsec^2 per nm
305 // ff *= 1e-6; // Jy / arcsec^2 per nm
306 // ff *= 3600*3600; // Jy / deg^2
307 // ff *= 1./TMath::DegToRad()/TMath::DegToRad(); // Jy/sr = 1e-26J/s/m^2/Hz/sr
308 // ff *= 1e-26; // J/s/m^2/Hz/sr per nm
309
310 const Double_t arcsec2rad = TMath::DegToRad()/3600.;
311 const Double_t f = 1e-32 / (arcsec2rad*arcsec2rad);
312
313 // Read night sky background flux from file
314 MParSpline flux;
315 if (!flux.ReadFile(fFileNameNSB))
316 return kFALSE;
317
318 if (!CheckWavelengthRange(flux, TString("night sky background flux from ")+fFileNameNSB))
319 return kFALSE;
320
321 MParSpline nsb;
322
323 // Normalization to our units,
324 // conversion from energy flux to photon flux
325 nsb.SetFunction(Form("%.12e/(x*TMath::H())", f), max-min, min, max);
326
327 // multiply night sky background flux with normalization
328 nsb.Multiply(*flux.GetSpline());
329
330 // Multiply with the total transmission
331 nsb.Multiply(*eff.GetSpline());
332
333 // Check if the photon flux is zero at both ends of the NSB
334 if (nsb.GetSpline()->Eval(min)>1e-5)
335 {
336 *fLog << warn << "WARNING - Transmitted NSB spectrum at detector at ";
337 *fLog << wmin << "nm is not zero, but " << nsb.GetSpline()->Eval(wmin) << "... abort." << endl;
338 }
339 if (nsb.GetSpline()->Eval(max)>1e-5)
340 {
341 *fLog << warn << "WARNING - Transmitted NSB spectrum at detector at ";
342 *fLog << wmax << "nm is not zero, but " << nsb.GetSpline()->Eval(wmax) << "... abort." << endl;
343 }
344
345 // Check if the photon flux is zero at both ends of the simulated region
346 if (nsb.GetSpline()->Eval(fRunHeader->GetWavelengthMin())>1e-5)
347 {
348 *fLog << err << "ERROR - Transmitted NSB spectrum at detector at minimum simulated wavelength ";
349 *fLog << fRunHeader->GetWavelengthMin() << "nm is not zero... abort." << endl;
350 return kFALSE;
351 }
352 if (nsb.GetSpline()->Eval(fRunHeader->GetWavelengthMax())>1e-5)
353 {
354 *fLog << err << "ERROR - Transmitted NSB spectrum at detector at maximum simulated wavelength ";
355 *fLog << fRunHeader->GetWavelengthMax() << "nm is not zero... abort." << endl;
356 return kFALSE;
357 }
358
359 // Conversion from m to radians
360 const Double_t conv = fGeom->GetConvMm2Deg()*TMath::DegToRad()*1e3;
361
362 // Angular acceptance of the cones
363 //const Double_t sr = s5.GetSpline()->IntegralSolidAngle(); // sr
364 // Absolute reflector area
365 const Double_t Ar = r->GetA()/1e4; // m^2
366 // Size of the cone's entrance window
367 const Double_t A0 = (*fGeom)[0].GetA()*1e-6; // m^2
368
369 // Rate * m^2 * Solid Angle
370 // -------------------------
371
372 // Angular acceptance Cones (e.g. 20deg) * Cone-Area
373 const Double_t f1 = A0 * sr; // m^2 sr
374
375 // Mirror-Area * Field of view of cones (e.g. 0.1deg)
376 const Double_t f2 = Ar * A0*conv*conv; // m^2 sr
377
378 // FIXME: Calculate the reflectivity of the bottom by replacing
379 // MirrorReflectivity by bottom reflectivity and reflect
380 // and use it to reflect the difference between f1 and f2
381 // if any.
382
383 // Total NSB rate in MHz per m^2 and sr
384 const Double_t rate = nsb.GetSpline()->Integral() * 1e-6;
385
386 *fLog << inf;
387
388 // Resulting rates as if Razmick's constant had been used
389 // *fLog << 1.75e6/(600-300) * f1 * eff.GetSpline()->Integral() << " MHz" << endl;
390 // *fLog << 1.75e6/(600-300) * f2 * eff.GetSpline()->Integral() << " MHz" << endl;
391
392 *fLog << "Conversion factor Fnu: " << f << endl;
393 *fLog << "Total reflective area: " << Form("%.2f", Ar) << " m" << UTF8::kSquare << endl;
394 *fLog << "Acceptance area of cone 0: " << Form("%.2f", A0*1e6) << " mm" << UTF8::kSquare << " = ";
395 *fLog << A0*conv*conv << " sr" << endl;
396 *fLog << "Cones angular acceptance: " << sr << " sr" << endl;
397 *fLog << "ConeArea*ConeSolidAngle (f1): " << f1 << " m^2 sr" << endl;
398 *fLog << "MirrorArea*ConeSkyAngle (f2): " << f2 << " m^2 sr" << endl;
399 *fLog << "Effective. transmission: " << Form("%.1f", nm) << " nm" << endl;
400 *fLog << "NSB freq. in " << fNameGeomCam << "[0] (f1): " << Form("%.2f", rate * f1) << " MHz" << endl;
401 *fLog << "NSB freq. in " << fNameGeomCam << "[0] (f2): " << Form("%.2f", rate * f2) << " MHz" << endl;
402 *fLog << "Using f2." << endl;
403
404 // Scale the rate per mm^2 and to GHz
405 fScale = rate * f2 / (*fGeom)[0].GetA() / 1000;
406
407 // FIXME: Scale with the number of pixels
408 if (rate*f2>1000)
409 {
410 *fLog << err << "ERROR - Frequency exceeds 1GHz, this might leed to too much memory consumption." << endl;
411 return kFALSE;
412 }
413
414 return kTRUE;
415}
416
417Bool_t MSimRandomPhotons::ReInit(MParList *pList)
418{
419 // Overwrite the default set by MGeomApply
420 fRates->Init(*fGeom);
421 return kTRUE;
422}
423
424// --------------------------------------------------------------------------
425//
426// Check for the necessary containers
427//
428Int_t MSimRandomPhotons::Process()
429{
430 // Get array from event container
431 // const Int_t num = fEvt->GetNumPhotons();
432 //
433 // Do not produce pure pedestal events!
434 // if (num==0)
435 // return kTRUE;
436
437 // Get array from event container
438 // FIXME: Use statistics container instead
439 const UInt_t npix = fGeom->GetNumPixels();
440
441 // This is the possible window in which the triggered digitization
442 // may take place.
443 const Double_t start = fStat->GetTimeFirst();
444 const Double_t end = fStat->GetTimeLast();
445
446 // Loop over all pixels
447 for (UInt_t idx=0; idx<npix; idx++)
448 {
449 // Scale the rate with the pixel size.
450 const Double_t rate = fFreqFixed + fScale*(*fGeom)[idx].GetA();
451
452 (*fRates)[idx].SetPedestal(rate);
453
454 // Calculate the average distance between two consequtive photons
455 const Double_t avglen = 1./rate;
456
457 // Start producing photons at time "start"
458 Double_t t = start;
459 while (1)
460 {
461 // Get a random time for the photon.
462 // The differences are exponentially distributed.
463 t += MMath::RndmExp(avglen);
464
465 // Check if we reached the end of the useful time window
466 if (t>end)
467 break;
468
469 // Add a new photon
470 // FIXME: SLOW!
471 MPhotonData &ph = fEvt->Add();
472
473 // Set source to NightSky, time to t and tag to pixel index
474 ph.SetPrimary(MMcEvtBasic::kNightSky);
475 ph.SetWeight();
476 ph.SetTime(t);
477 ph.SetTag(idx);
478
479 // fProductionHeight, fPosX, fPosY, fCosU, fCosV (irrelevant) FIXME: Reset?
480
481 if (fSimulateWavelength)
482 {
483 const Float_t wmin = fRunHeader->GetWavelengthMin();
484 const Float_t wmax = fRunHeader->GetWavelengthMax();
485
486 ph.SetWavelength(TMath::Nint(gRandom->Uniform(wmin, wmax)));
487 }
488 }
489 }
490
491 // Re-sort the photons by time!
492 fEvt->Sort(kTRUE);
493
494 // Update maximum index
495 fStat->SetMaxIndex(npix-1);
496
497 // Shrink
498 return kTRUE;
499}
500
501// --------------------------------------------------------------------------
502//
503// Read the parameters from the resource file.
504//
505// FrequencyFixed: 0.040
506// FrequencyNSB: 5.8
507//
508// The fixed frequency is given in units fitting the units of the time.
509// Usually the time is given in nanoseconds thus, e.g., 0.040 means 40MHz.
510//
511// The FrequencyNSB is scaled by the area of the pixel in cm^2. Therefore
512// 0.040 would mean 40MHz/cm^2
513//
514Int_t MSimRandomPhotons::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
515{
516 Bool_t rc = kFALSE;
517 if (IsEnvDefined(env, prefix, "FrequencyFixed", print))
518 {
519 rc = kTRUE;
520 fFreqFixed = GetEnvValue(env, prefix, "FrequencyFixed", fFreqFixed);
521 }
522
523 if (IsEnvDefined(env, prefix, "FrequencyNSB", print))
524 {
525 rc = kTRUE;
526 fFreqNSB = GetEnvValue(env, prefix, "FrequencyNSB", fFreqNSB);
527 }
528
529 if (IsEnvDefined(env, prefix, "FileNameNSB", print))
530 {
531 rc = kTRUE;
532 fFileNameNSB = GetEnvValue(env, prefix, "FileNameNSB", fFileNameNSB);
533 }
534
535 if (IsEnvDefined(env, prefix, "SimulateCherenkovSpectrum", print))
536 {
537 rc = kTRUE;
538 fSimulateWavelength = GetEnvValue(env, prefix, "SimulateCherenkovSpectrum", fSimulateWavelength);
539 }
540
541 return rc;
542}
Note: See TracBrowser for help on using the repository browser.