source: trunk/MagicSoft/Mars/msimcamera/MSimAPD.cc@ 9433

Last change on this file since 9433 was 9427, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 7.7 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// MSimAPD
28//
29// This tasks simulates the individual APDs. Before starting the APD is
30// initialized randomly according to the photon rate hitting the APD. Such
31// it is assumed that the initial condition of the APD is similar to the real
32// one. In this context it is assumed that the events are independent, so
33// that the APD is always in the same condition.
34//
35// For every photon and event the behaviour of the APD is simulated. The
36// output is set as weight to the MPhotonData containers.
37//
38// Remark:
39// - The photons MUST be sorted increasing in time.
40// - The photon rate used to initialize the APD must match the one used
41// to "fill" the random photons. (FIXME: This should be stored somewhere)
42//
43// Input Containers:
44// fNameGeomCam [MGeomCam]
45// MPhotonEvent
46// MPhotonStatistics
47//
48// Output Containers:
49// MPhotonEvent
50//
51//////////////////////////////////////////////////////////////////////////////
52#include "MSimAPD.h"
53
54#include <TH2.h>
55#include <TRandom.h>
56
57#include "MLog.h"
58#include "MLogManip.h"
59
60#include "MMath.h"
61#include "MParList.h"
62
63#include "MGeomCam.h"
64
65#include "MPhotonEvent.h"
66#include "MPhotonData.h"
67
68#include "MPedestalCam.h"
69#include "MPedestalPix.h"
70
71#include "MAvalanchePhotoDiode.h"
72
73ClassImp(MSimAPD);
74
75using namespace std;
76
77// --------------------------------------------------------------------------
78//
79// Default Constructor.
80//
81MSimAPD::MSimAPD(const char* name, const char *title)
82: fGeom(0), fEvt(0), fStat(0), fType(1)
83{
84 fName = name ? name : "MSimAPD";
85 fTitle = title ? title : " Task to simulate the detection behaviour of APDs";
86}
87
88// --------------------------------------------------------------------------
89//
90// Get the necessary parameter containers
91//
92Int_t MSimAPD::PreProcess(MParList *pList)
93{
94 if (fNameGeomCam.IsNull())
95 {
96 *fLog << inf << "No geometry container... skipping." << endl;
97 return kSKIP;
98 }
99
100 fGeom = (MGeomCam*)pList->FindObject(fNameGeomCam, "MGeomCam");
101 if (!fGeom)
102 {
103 *fLog << inf << fNameGeomCam << " [MGeomCam] not found..." << endl;
104
105 fGeom = (MGeomCam*)pList->FindObject("MGeomCam");
106 if (!fGeom)
107 {
108 *fLog << err << "MGeomCam not found... aborting." << endl;
109 return kFALSE;
110 }
111 }
112
113 fStat = (MPhotonStatistics*)pList->FindObject("MPhotonStatistics");
114 if (!fStat)
115 {
116 *fLog << err << "MPhotonStatistics not found... aborting." << endl;
117 return kFALSE;
118 }
119
120 fEvt = (MPhotonEvent*)pList->FindObject("MPhotonEvent");
121 if (!fEvt)
122 {
123 *fLog << err << "MPhotonEvent not found... aborting." << endl;
124 return kFALSE;
125 }
126
127 fRates = (MPedestalCam*)pList->FindObject("AccidentalPhotonRates", "MPedestalCam");
128 if (!fRates)
129 {
130 *fLog << inf;
131 *fLog << "AccidentalPhotonRates [MPedestalCam] not found..." << endl;
132 *fLog << " using " << fFreq << " as default for all G-APDs." << endl;
133 }
134
135 return kTRUE;
136}
137
138// --------------------------------------------------------------------------
139//
140// Initialize as many APDs as we have pixels in the fGeomCam
141//
142Bool_t MSimAPD::ReInit(MParList *plist)
143{
144 if (UInt_t(fAPDs.GetEntriesFast())==fGeom->GetNumPixels())
145 return kTRUE;
146
147 fAPDs.Delete();
148
149 // FIXME:
150 // * initialize an empty APD and read the APD setup from a file to
151 // allow different APDs.
152 // * Make the arguments a data member of MSimAPD
153
154 Int_t ncells = 0;
155 Float_t crosstalk = 0;
156 Float_t deadtime = 0;
157 Float_t recovery = 0;
158
159 switch (fType)
160 {
161 case 1:
162 ncells = 30;
163 crosstalk = 0.2;
164 deadtime = 3;
165 recovery = 8.75*4;
166 break;
167
168 case 2:
169 ncells = 60;
170 crosstalk = 0.2;
171 deadtime = 3;
172 recovery = 8.75;
173 break;
174
175 default:
176 *fLog << err << "ERROR - APD type " << fType << " undefined." << endl;
177 return kFALSE;
178 }
179
180 for (UInt_t i=0; i<fGeom->GetNumPixels(); i++)
181 fAPDs.Add(new APD(ncells, crosstalk, deadtime, recovery));
182
183 return kTRUE;
184}
185
186// --------------------------------------------------------------------------
187//
188// Process all photons through the corresponding APD and set the output
189// (weight) accordingly.
190//
191Int_t MSimAPD::Process()
192{
193 //const Double_t rate = 40e9;
194 // FIXME: Where do we get this number from??
195 // const Double_t rate = 0.04;
196
197 // Make all APDs look neutral for the first hit by a photon according to the
198 // average hit rate
199 const UInt_t npix = fAPDs.GetEntriesFast();
200
201 // Check if we can safely proceed (this can fail if we either haven't been
202 // ReInit'ed or the max index in MPhotonStatistics is wrong)
203 if ((Int_t)npix<fStat->GetMaxIndex())
204 {
205 *fLog << err << "ERROR - MSimAPD::Process: Only " << npix << " APDs initialized. At least " << fStat->GetMaxIndex() << " needed... abort." << endl;
206 return kERROR;
207 }
208
209 for (UInt_t idx=0; idx<npix; idx++)
210 {
211 const Double_t freq = fRates ? (*fRates)[idx].GetPedestal() : fFreq;
212 static_cast<APD*>(fAPDs.UncheckedAt(idx))->FillRandom(freq, fStat->GetTimeFirst());
213 }
214
215 // Get number of photons
216 const Int_t num = fEvt->GetNumPhotons();
217
218 // Loop over all photons
219 for (Int_t i=0; i<num; i++)
220 {
221 // Get i-th photon
222 MPhotonData &ph = (*fEvt)[i];
223
224 // Get arrival time of photon and idx
225 const Double_t t = ph.GetTime();
226 const Int_t idx = ph.GetTag();
227 if (idx<0)
228 {
229 *fLog << err << "ERROR - MSimAPD: Invalid index -1." << endl;
230 return kERROR;
231 }
232
233 if (ph.GetWeight()!=1)
234 {
235 *fLog << err << "ERROR - MSimAPD: Weight of " << i << "-th photon not 1, but " << ph.GetWeight() << endl;
236 ph.Print();
237 return kERROR;
238 }
239 // Simulate hitting the APD (the signal height in effective
240 // "number of photons" is returned)
241 const Double_t hits = static_cast<APD*>(fAPDs.UncheckedAt(idx))->HitRandomCell(t);
242
243 // FIXME: Make a proper simulation of the excess noise!!!
244 //const Double_t signal = gRandom->Gaus(hits, 0.2*TMath::Sqrt(hits));
245
246 // Set the weight to the input
247 ph.SetWeight(hits);
248 }
249
250 return kTRUE;
251}
252
253// --------------------------------------------------------------------------
254//
255// NameGeomCam
256// Type: 1
257//
258Int_t MSimAPD::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
259{
260 Bool_t rc = kFALSE;
261 if (IsEnvDefined(env, prefix, "NameGeomCam", print))
262 {
263 rc = kTRUE;
264 fNameGeomCam = GetEnvValue(env, prefix, "NameGeomCam", fNameGeomCam);
265 }
266
267 if (IsEnvDefined(env, prefix, "Type", print))
268 {
269 rc = kTRUE;
270 fType = GetEnvValue(env, prefix, "Type", fType);
271 }
272
273 return rc;
274}
Note: See TracBrowser for help on using the repository browser.