source: trunk/Mars/msimcamera/MSimCamera.cc@ 17050

Last change on this file since 17050 was 10167, checked in by tbretz, 14 years ago
Treat kArtificial in addition to kNightSky.
File size: 9.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// MSimCamera
28//
29// This task initializes the analog channels with analog noise and simulated
30// the analog pulses from the photon signal.
31//
32// Input Containers:
33// MPhotonEvent
34// MPhotonStatistics
35// MRawRunHeader
36//
37// Output Containers:
38// MAnalogChannels
39//
40//////////////////////////////////////////////////////////////////////////////
41#include "MSimCamera.h"
42
43#include <TF1.h>
44#include <TRandom.h>
45
46#include "MLog.h"
47#include "MLogManip.h"
48
49#include "MSpline3.h"
50#include "MParSpline.h"
51
52#include "MParList.h"
53
54#include "MPhotonEvent.h"
55#include "MPhotonData.h"
56
57#include "MPedestalCam.h"
58#include "MPedestalPix.h"
59
60#include "MAnalogSignal.h"
61#include "MAnalogChannels.h"
62
63#include "MMcEvt.hxx" // To be replaced by a CheObs class
64#include "MRawRunHeader.h"
65
66ClassImp(MSimCamera);
67
68using namespace std;
69
70// --------------------------------------------------------------------------
71//
72// Default Constructor.
73//
74MSimCamera::MSimCamera(const char* name, const char *title)
75 : fEvt(0), fStat(0), fRunHeader(0), fElectronicNoise(0), fGain(0),
76 fCamera(0), fMcEvt(0), fSpline(0), fBaselineGain(kFALSE),
77 fDefaultOffset(-1), fDefaultNoise(-1), fDefaultGain(-1)
78
79{
80 fName = name ? name : "MSimCamera";
81 fTitle = title ? title : "Task to simulate the electronic noise and to convert photons into pulses";
82}
83
84// --------------------------------------------------------------------------
85//
86// Search for the necessayr parameter containers.
87// Setup spline for pulse shape.
88//
89Int_t MSimCamera::PreProcess(MParList *pList)
90{
91 fMcEvt = (MMcEvt*)pList->FindCreateObj("MMcEvt");
92 if (!fMcEvt)
93 return kFALSE;
94
95 fCamera = (MAnalogChannels*)pList->FindCreateObj("MAnalogChannels");
96 if (!fCamera)
97 return kFALSE;
98
99 fEvt = (MPhotonEvent*)pList->FindObject("MPhotonEvent");
100 if (!fEvt)
101 {
102 *fLog << err << "MPhotonEvent not found... aborting." << endl;
103 return kFALSE;
104 }
105
106 fStat = (MPhotonStatistics*)pList->FindObject("MPhotonStatistics");
107 if (!fStat)
108 {
109 *fLog << err << "MPhotonStatistics not found... aborting." << endl;
110 return kFALSE;
111 }
112
113 fRunHeader = (MRawRunHeader *)pList->FindObject("MRawRunHeader");
114 if (!fRunHeader)
115 {
116 *fLog << err << "MRawRunHeader not found... aborting." << endl;
117 return kFALSE;
118 }
119/*
120 fPulsePos = (MParameterD*)pList->FindObject("IntendedPulsePos", "MParameterD");
121 if (!fPulsePos)
122 {
123 *fLog << err << "IntendedPulsePos [MParameterD] not found... aborting." << endl;
124 return kFALSE;
125 }
126 */
127
128 // Create it here to make sure that MGeomApply will set the correct size
129 fElectronicNoise = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", "ElectronicNoise");
130 if (!fElectronicNoise)
131 return kFALSE;
132
133 fGain = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", "Gain");
134 if (!fGain)
135 return kFALSE;
136
137 MParSpline *pulse = (MParSpline*)pList->FindObject("PulseShape", "MParSpline");
138 if (!pulse)
139 {
140 *fLog << err << "PulseShape [MParSpline] not found... aborting." << endl;
141 return kFALSE;
142 }
143
144 if (fRunHeader->GetFreqSampling()!=1000)
145 {
146 *fLog << err << "ERROR - Sampling frequencies others than 1GHz are not yet supported." << endl;
147 *fLog << warn << "FIXME - SCALE MPulsShape WITH THE SAMPLING FREQUENCY." << endl;
148 return kFALSE;
149 }
150
151 fSpline = pulse->GetSpline();
152 if (!fSpline)
153 {
154 *fLog << err << "No spline initialized." << endl;
155 return kFALSE;
156 }
157
158 // ---------------- Information output ----------------------
159
160 if (fBaselineGain)
161 *fLog << inf << "Gain is also applied to the electronic noise." << endl;
162
163 return kTRUE;
164}
165
166// --------------------------------------------------------------------------
167//
168// FIXME: For now this is a workaround to set a baseline and the
169// electronic (guassian noise)
170//
171Bool_t MSimCamera::ReInit(MParList *plist)
172{
173 for (int i=0; i<fElectronicNoise->GetSize(); i++)
174 {
175 MPedestalPix &ped = (*fElectronicNoise)[i];
176 if (fDefaultOffset>0)
177 ped.SetPedestal(fDefaultOffset);
178 if (fDefaultNoise>0)
179 ped.SetPedestalRms(fDefaultNoise);
180
181 ped.SetPedestalABoffset(0);
182 ped.SetNumEvents(0);
183
184
185 MPedestalPix &gain = (*fGain)[i];
186 if (fDefaultGain>0)
187 gain.SetPedestal(fDefaultGain);
188
189 gain.SetPedestalRms(0);
190 gain.SetPedestalABoffset(0);
191 gain.SetNumEvents(0);
192 }
193
194 return kTRUE;
195}
196
197// --------------------------------------------------------------------------
198//
199// fStat->GetMaxIndex must return the maximum index possible
200// (equiv. number of pixels) not just the maximum index stored!
201//
202Int_t MSimCamera::Process()
203{
204 // Calculate start time, end time and corresponding number of samples
205 const Double_t freq = fRunHeader->GetFreqSampling()/1000.;
206
207 // FIXME: Should we use a higher sampling here?
208
209 const Double_t start = fStat->GetTimeFirst()*freq;
210 const Double_t end = fStat->GetTimeLast() *freq;
211
212 const UInt_t nlen = TMath::CeilNint(end-start);
213
214 // Get number of pixels/channels
215 const UInt_t npix = fStat->GetMaxIndex()+1;
216
217 if (npix>(UInt_t)fElectronicNoise->GetSize())
218 {
219 *fLog << err << "ERROR - More indices (" << npix << ") ";
220 *fLog << "assigned than existing in camera (";
221 *fLog << fElectronicNoise->GetSize() << ")!" << endl;
222 return kERROR;
223 }
224
225 const Double_t pl = fSpline->GetXmin()*freq;
226 const Double_t pr = fSpline->GetXmax()*freq;
227
228 // Init the arrays and set the range which will contain valid data
229 fCamera->Init(npix, nlen);
230 fCamera->SetValidRange(TMath::FloorNint(pr), TMath::CeilNint(nlen+pl));
231
232 // Add electronic noise to empty channels
233 for (UInt_t i=0; i<npix; i++)
234 {
235 const MPedestalPix &pix = (*fElectronicNoise)[i];
236
237 const Double_t val = pix.GetPedestal();
238 const Double_t rms = pix.GetPedestalRms();
239
240 if (!fBaselineGain)
241 {
242 (*fCamera)[i].AddGaussianNoise(rms, val);
243 continue;
244 }
245 // Sorry, the name "pedestal" is misleading here
246 // FIXME: Simulate gain fluctuations
247 const Double_t gain = (*fGain)[i].GetPedestal();
248
249 // FIXME: We might add the base line here already.
250 // FIXME: How stable is the offset?
251 // FIXME: Should we write a container AppliedGain for MSImTrigger?
252 (*fCamera)[i].AddGaussianNoise(rms*gain, val*gain);
253 }
254
255 // FIXME: Simulate correlations with neighboring pixels
256
257 const Int_t num = fEvt->GetNumPhotons();
258
259 // A random shift, uniformely distributed within one slice, to make sure that
260 // the first photon is not always aligned identically with a sample edge.
261 // FIXME: Make it switchable
262 const Float_t rndm = gRandom->Uniform();
263
264 // FIXME: Shell we add a random shift of [0,1] samples per channel?
265 // Or maybe per channel and run?
266
267 Double_t tot = 0;
268
269 // Simulate pulses
270 for (Int_t i=0; i<num; i++)
271 {
272 const MPhotonData &ph = (*fEvt)[i];
273
274 const UInt_t idx = ph.GetTag();
275 const Double_t t = (ph.GetTime()-fStat->GetTimeFirst())*freq+rndm;// - fSpline->GetXmin();
276
277 // FIXME: Time jitter?
278 // FIXME: Add additional routing here?
279 // FIMXE: How stable is the gain?
280
281 if (ph.GetPrimary()!=MMcEvt::kNightSky && ph.GetPrimary()!=MMcEvt::kArtificial)
282 tot += ph.GetWeight();
283
284 // Sorry, the name "pedestal" is misleading here
285 // FIXME: Simulate gain fluctuations
286 const Double_t gain = (*fGain)[idx].GetPedestal();
287
288 // === FIXME === FIXME === FIXME === Frequency!!!!
289 (*fCamera)[idx].AddPulse(*fSpline, t, ph.GetWeight()*gain);
290 }
291
292 fMcEvt->SetPhotElfromShower(TMath::Nint(tot));
293
294 return kTRUE;
295}
296
297// --------------------------------------------------------------------------
298//
299// BaselineGain: Off
300//
301Int_t MSimCamera::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
302{
303 Bool_t rc = kFALSE;
304 if (IsEnvDefined(env, prefix, "BaselineGain", print))
305 {
306 rc = kTRUE;
307 fBaselineGain = GetEnvValue(env, prefix, "BaselineGain", fBaselineGain);
308 }
309
310 if (IsEnvDefined(env, prefix, "DefaultOffset", print))
311 {
312 rc = kTRUE;
313 fDefaultOffset = GetEnvValue(env, prefix, "DefaultOffset", fDefaultOffset);
314 }
315 if (IsEnvDefined(env, prefix, "DefaultNoise", print))
316 {
317 rc = kTRUE;
318 fDefaultNoise = GetEnvValue(env, prefix, "DefaultNoise", fDefaultNoise);
319 }
320 if (IsEnvDefined(env, prefix, "DefaultGain", print))
321 {
322 rc = kTRUE;
323 fDefaultGain = GetEnvValue(env, prefix, "DefaultGain", fDefaultGain);
324 }
325
326 return rc;
327}
Note: See TracBrowser for help on using the repository browser.