source: trunk/MagicSoft/Mars/msimcamera/MSimRandomPhotons.cc@ 9576

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