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

Last change on this file since 9932 was 9932, checked in by tbretz, 14 years ago
Improved output in MSimRandomPhotons
File size: 18.4 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 (eff.GetSpline()->Eval(min)>1e-5)
335 {
336 *fLog << warn << "WARNING - Total transmission efficiency at detector at ";
337 *fLog << min << "nm is not zero, but " << nsb.GetSpline()->Eval(min) << "... abort." << endl;
338 }
339 if (eff.GetSpline()->Eval(max)>1e-5)
340 {
341 *fLog << warn << "WARNING - Total transmission efficiency at detector at ";
342 *fLog << max << "nm is not zero, but " << nsb.GetSpline()->Eval(max) << "... abort." << endl;
343 }
344
345 // Check if the photon flux is zero at both ends of the simulated region
346 if (eff.GetSpline()->Eval(wmin)>1e-5)
347 {
348 *fLog << err << "ERROR - Total transmission efficiency at detector at minimum simulated wavelength ";
349 *fLog << wmin << "nm is not zero... abort." << endl;
350 *fLog << " PhotonDetectionEfficency: " << s1->GetSpline()->Eval(wmin) << endl;
351 *fLog << " ConeTransmission: " << s2->GetSpline()->Eval(wmin) << endl;
352 *fLog << " MirrorReflectivity: " << s3->GetSpline()->Eval(wmin) << endl;
353 *fLog << " TotalEfficiency: " << eff.GetSpline()->Eval(wmin) << endl;
354 return kFALSE;
355 }
356 if (eff.GetSpline()->Eval(wmax)>1e-5)
357 {
358 *fLog << err << "ERROR - Total transmission efficiency at detector at maximum simulated wavelength ";
359 *fLog << wmax << "nm is not zero... abort." << endl;
360 *fLog << " PhotonDetectionEfficency: " << s1->GetSpline()->Eval(wmax) << endl;
361 *fLog << " ConeTransmission: " << s2->GetSpline()->Eval(wmax) << endl;
362 *fLog << " MirrorReflectivity: " << s3->GetSpline()->Eval(wmax) << endl;
363 *fLog << " TotalEfficiency: " << eff.GetSpline()->Eval(wmax) << endl;
364 return kFALSE;
365 }
366
367 // Conversion from m to radians
368 const Double_t conv = fGeom->GetConvMm2Deg()*TMath::DegToRad()*1e3;
369
370 // Angular acceptance of the cones
371 //const Double_t sr = s5.GetSpline()->IntegralSolidAngle(); // sr
372 // Absolute reflector area
373 const Double_t Ar = r->GetA()/1e4; // m^2
374 // Size of the cone's entrance window
375 const Double_t A0 = (*fGeom)[0].GetA()*1e-6; // m^2
376
377 // Rate * m^2 * Solid Angle
378 // -------------------------
379
380 // Angular acceptance Cones (e.g. 20deg) * Cone-Area
381 const Double_t f1 = A0 * sr; // m^2 sr
382
383 // Mirror-Area * Field of view of cones (e.g. 0.1deg)
384 const Double_t f2 = Ar * A0*conv*conv; // m^2 sr
385
386 // FIXME: Calculate the reflectivity of the bottom by replacing
387 // MirrorReflectivity by bottom reflectivity and reflect
388 // and use it to reflect the difference between f1 and f2
389 // if any.
390
391 // Total NSB rate in MHz per m^2 and sr
392 const Double_t rate = nsb.GetSpline()->Integral() * 1e-6;
393
394 *fLog << inf;
395
396 // Resulting rates as if Razmick's constant had been used
397 // *fLog << 1.75e6/(600-300) * f1 * eff.GetSpline()->Integral() << " MHz" << endl;
398 // *fLog << 1.75e6/(600-300) * f2 * eff.GetSpline()->Integral() << " MHz" << endl;
399
400 *fLog << "Conversion factor Fnu: " << f << endl;
401 *fLog << "Total reflective area: " << Form("%.2f", Ar) << " m" << UTF8::kSquare << endl;
402 *fLog << "Acceptance area of cone 0: " << Form("%.2f", A0*1e6) << " mm" << UTF8::kSquare << " = ";
403 *fLog << A0*conv*conv << " sr" << endl;
404 *fLog << "Cones angular acceptance: " << sr << " sr" << endl;
405 *fLog << "ConeArea*ConeSolidAngle (f1): " << f1 << " m^2 sr" << endl;
406 *fLog << "MirrorArea*ConeSkyAngle (f2): " << f2 << " m^2 sr" << endl;
407 *fLog << "Effective. transmission: " << Form("%.1f", nm) << " nm" << endl;
408 *fLog << "NSB freq. in " << fNameGeomCam << "[0] (f1): " << Form("%.2f", rate * f1) << " MHz" << endl;
409 *fLog << "NSB freq. in " << fNameGeomCam << "[0] (f2): " << Form("%.2f", rate * f2) << " MHz" << endl;
410 *fLog << "Using f2." << endl;
411
412 // Scale the rate per mm^2 and to GHz
413 fScale = rate * f2 / (*fGeom)[0].GetA() / 1000;
414
415 // FIXME: Scale with the number of pixels
416 if (rate*f2>1000)
417 {
418 *fLog << err << "ERROR - Frequency exceeds 1GHz, this might leed to too much memory consumption." << endl;
419 return kFALSE;
420 }
421
422 return kTRUE;
423}
424
425Bool_t MSimRandomPhotons::ReInit(MParList *pList)
426{
427 // Overwrite the default set by MGeomApply
428 fRates->Init(*fGeom);
429 return kTRUE;
430}
431
432// --------------------------------------------------------------------------
433//
434// Check for the necessary containers
435//
436Int_t MSimRandomPhotons::Process()
437{
438 // Get array from event container
439 // const Int_t num = fEvt->GetNumPhotons();
440 //
441 // Do not produce pure pedestal events!
442 // if (num==0)
443 // return kTRUE;
444
445 // Get array from event container
446 // FIXME: Use statistics container instead
447 const UInt_t npix = fGeom->GetNumPixels();
448
449 // This is the possible window in which the triggered digitization
450 // may take place.
451 const Double_t start = fStat->GetTimeFirst();
452 const Double_t end = fStat->GetTimeLast();
453
454 // Loop over all pixels
455 for (UInt_t idx=0; idx<npix; idx++)
456 {
457 // Scale the rate with the pixel size.
458 const Double_t rate = fFreqFixed + fScale*(*fGeom)[idx].GetA();
459
460 (*fRates)[idx].SetPedestal(rate);
461
462 // Calculate the average distance between two consequtive photons
463 const Double_t avglen = 1./rate;
464
465 // Start producing photons at time "start"
466 Double_t t = start;
467 while (1)
468 {
469 // Get a random time for the photon.
470 // The differences are exponentially distributed.
471 t += MMath::RndmExp(avglen);
472
473 // Check if we reached the end of the useful time window
474 if (t>end)
475 break;
476
477 // Add a new photon
478 // FIXME: SLOW!
479 MPhotonData &ph = fEvt->Add();
480
481 // Set source to NightSky, time to t and tag to pixel index
482 ph.SetPrimary(MMcEvtBasic::kNightSky);
483 ph.SetWeight();
484 ph.SetTime(t);
485 ph.SetTag(idx);
486
487 // fProductionHeight, fPosX, fPosY, fCosU, fCosV (irrelevant) FIXME: Reset?
488
489 if (fSimulateWavelength)
490 {
491 const Float_t wmin = fRunHeader->GetWavelengthMin();
492 const Float_t wmax = fRunHeader->GetWavelengthMax();
493
494 ph.SetWavelength(TMath::Nint(gRandom->Uniform(wmin, wmax)));
495 }
496 }
497 }
498
499 // Re-sort the photons by time!
500 fEvt->Sort(kTRUE);
501
502 // Update maximum index
503 fStat->SetMaxIndex(npix-1);
504
505 // Shrink
506 return kTRUE;
507}
508
509// --------------------------------------------------------------------------
510//
511// Read the parameters from the resource file.
512//
513// FrequencyFixed: 0.040
514// FrequencyNSB: 5.8
515//
516// The fixed frequency is given in units fitting the units of the time.
517// Usually the time is given in nanoseconds thus, e.g., 0.040 means 40MHz.
518//
519// The FrequencyNSB is scaled by the area of the pixel in cm^2. Therefore
520// 0.040 would mean 40MHz/cm^2
521//
522Int_t MSimRandomPhotons::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
523{
524 Bool_t rc = kFALSE;
525 if (IsEnvDefined(env, prefix, "FrequencyFixed", print))
526 {
527 rc = kTRUE;
528 fFreqFixed = GetEnvValue(env, prefix, "FrequencyFixed", fFreqFixed);
529 }
530
531 if (IsEnvDefined(env, prefix, "FrequencyNSB", print))
532 {
533 rc = kTRUE;
534 fFreqNSB = GetEnvValue(env, prefix, "FrequencyNSB", fFreqNSB);
535 }
536
537 if (IsEnvDefined(env, prefix, "FileNameNSB", print))
538 {
539 rc = kTRUE;
540 fFileNameNSB = GetEnvValue(env, prefix, "FileNameNSB", fFileNameNSB);
541 }
542
543 if (IsEnvDefined(env, prefix, "SimulateCherenkovSpectrum", print))
544 {
545 rc = kTRUE;
546 fSimulateWavelength = GetEnvValue(env, prefix, "SimulateCherenkovSpectrum", fSimulateWavelength);
547 }
548
549 return rc;
550}
Note: See TracBrowser for help on using the repository browser.