source: trunk/MagicSoft/Mars/melectronics/MAvalanchePhotoDiode.cc@ 9467

Last change on this file since 9467 was 9462, checked in by tbretz, 15 years ago
*** empty log message ***
File size: 10.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// APD
28//
29// All times in this class are relative times. Therefor the unit for the
30// time is not intrinsically fixed. In fact the dead-time and recovery-
31// time given in the constructor must have the same units. This is what
32// defines the unit of the times given in the function and the unit of
33// rates given.
34// For example, if recovery and dead time are given in nanoseconds the
35// all times must be in nanoseconds and rates are given per nanosecond,
36// i.e. GHz.
37//
38// Hamamatsu 30x30 cells: APD(30, 0.2, 3, 35)
39// Hamamatsu 60x60 cells: APD(60, 0.2, 3, 8.75)
40//
41//////////////////////////////////////////////////////////////////////////////
42#include "MAvalanchePhotoDiode.h"
43
44#include <TRandom.h>
45
46#include "MMath.h"
47
48ClassImp(APD);
49
50using namespace std;
51
52/*
53class MyProfile : public TProfile2D
54{
55public:
56 void AddBinEntry(Int_t cell) { fBinEntries.fArray[cell]++; }
57};
58*/
59
60// --------------------------------------------------------------------------
61//
62// Default Constructor.
63//
64// n is the number od cells in x or y. The APD is assumed to
65// be square.
66// prob is the crosstalk probability, i.e., the probability that a
67// photon which produced an avalanche will create another
68// photon in a neighboring cell
69// dt is the deadtime, i.e., the time in which the APD cell will show
70// no response to a photon after a hit
71// rt is the recovering tims, i.e. the exponential (e^(-dt/rt))
72// with which the cell is recovering after being dead
73//
74// prob, dt and ar can be set to 0 to switch the effect off.
75// 0 is also the dfeault for all three.
76//
77APD::APD(Int_t n, Float_t prob, Float_t dt, Float_t rt)
78 : fHist("APD", "", n, 0.5, n+0.5, n, 0.5, n+0.5),
79 fCrosstalkProb(prob), fDeadTime(dt), fRecoveryTime(rt), fTime(-1)
80{
81 fHist.SetDirectory(0);
82}
83
84// --------------------------------------------------------------------------
85//
86// This is the time the cell needs after a hit until less than threshold
87// (0.001 == one permille) of the signal is lost.
88//
89// If all cells of a G-APD have fired simultaneously and they are fired
90// once more after the relaxation time (with the defaultthreshold of 0.001)
91// the chip will show a signal which is one permille too small.
92//
93Float_t APD::GetRelaxationTime(Float_t threshold) const
94{
95 return fDeadTime-TMath::Log(threshold)*fRecoveryTime;
96}
97
98// --------------------------------------------------------------------------
99//
100// This is the recursive implementation of a hit. If a photon hits a cell
101// at x and y (must be a valid cell!) at time t, at first we check if the
102// cell is still dead. If it is not dead we calculate the signal height
103// from the recovery time. Now we check with the crosstalk probability
104// whether another photon is created. If another photon is created we
105// calculate randomly which of the four neighbor cells are hit.
106// If the cell is outside the APD the photon is ignored. As many
107// new photons are created until our random number is below the crosstak-
108// probability.
109//
110// The total height of the signal (in units of photons) is returned.
111// Note, that this can be a fractional number.
112//
113// This function looks a bit fancy accessing the histogram and works around
114// a few histogram functions. This is a speed optimization which works
115// around a lot of sanity checks which are obsolete in our case.
116//
117// The default time is 0.
118//
119Float_t APD::HitCellImp(Int_t x, Int_t y, Float_t t)
120{
121 // if (x<1 || x>fHist.GetNbinsX() ||
122 // y<1 || y>fHist.GetNbinsY())
123 // return 0;
124
125 // const Int_t cell = fHist.GetBin(x, y);
126 const Int_t cell = x + (fHist.GetNbinsX()+2)*y;
127
128 // This is the fastes way to access the bin-contents in fArray
129 Float_t &cont = fHist.GetArray()[cell];
130
131 // const Double_t dt = t-fHist.GetBinContent(x, y)-fDeadTime; //
132 const Float_t dt = t-cont-fDeadTime;
133
134 // Photons within the dead time are just ignored
135 if (/*hx.GetBinContent(x,y)>0 &&*/ dt<=0)
136 return 0;
137
138 // Signal height (in units of one photon) produced after dead time
139 const Float_t weight = fRecoveryTime<=0 ? 1 : 1.-exp(-dt/fRecoveryTime);
140
141 cont = t; // fHist.SetBinContent(x, y, t)
142
143 // Counter for the numbers of produced photons
144 Float_t n = weight;
145
146 /*
147 // Check if a photon in a neighboring cell is produced (crosstalk)
148 while (gRandom->Rndm()<fCrosstalkProb)
149 {
150 // Get a random neighbor which is hit.
151 switch (gRandom->Integer(4))
152 {
153 case 0: x++; if (x>fHist.GetNbinsX()) continue; break;
154 case 1: x--; if (x<1) continue; break;
155 case 2: y++; if (y>fHist.GetNbinsY()) continue; break;
156 case 3: y--; if (y<1) continue; break;
157 }
158
159 n += HitCellImp(x, y, t);
160 }
161 */
162
163 //for (int i=0; i<1; i++)
164 while (1)
165 {
166 const Double_t rndm = gRandom->Rndm();
167 if (rndm>=fCrosstalkProb)
168 break;
169
170 // We can re-use the random number becuase it is uniformely
171 // distributed. This saves cpu power
172 const Int_t dir = TMath::FloorNint(4*rndm/fCrosstalkProb);
173
174 // Get a random neighbor which is hit.
175 switch (dir)
176 {
177 case 0: if (x<fHist.GetNbinsX()) n += HitCellImp(x+1, y, t); break;
178 case 1: if (x>1) n += HitCellImp(x-1, y, t); break;
179 case 2: if (y<fHist.GetNbinsY()) n += HitCellImp(x, y+1, t); break;
180 case 3: if (y>1) n += HitCellImp(x, y-1, t); break;
181 }
182
183 // In the unlikely case the calculated direction is out-of-range,
184 // i.e. <0 or >3, we would just try to fill the same cell again which
185 }
186
187 return n;
188}
189
190// --------------------------------------------------------------------------
191//
192// Check if x and y is a valid cell. If not return 0, otherwise
193// HitCelImp(x, y, t)
194//
195// The default time is 0.
196//
197Float_t APD::HitCell(Int_t x, Int_t y, Float_t t)
198{
199 if (x<1 || x>fHist.GetNbinsX() ||
200 y<1 || y>fHist.GetNbinsY())
201 return 0;
202
203 return HitCellImp(x, y, t);
204}
205
206// --------------------------------------------------------------------------
207//
208// Determine randomly (uniformly) a cell which was hit. Return
209// HitCellImp for this cell and the given time.
210//
211// The default time is 0.
212//
213// If you want t w.r.t. fTime use HitRandomCellRelative istead.
214//
215Float_t APD::HitRandomCell(Float_t t)
216{
217 const UInt_t nx = fHist.GetNbinsX();
218 const UInt_t ny = fHist.GetNbinsY();
219
220 const UInt_t idx = gRandom->Integer(nx*ny);
221
222 const UInt_t x = idx%nx;
223 const UInt_t y = idx/nx;
224
225 return HitCellImp(x+1, y+1, t);
226}
227
228// --------------------------------------------------------------------------
229//
230// Sets all cells with a contents whihc is well before the time t such that
231// the chip is "virgin". Therefore all cells are set to a time which
232// is twice the deadtime before the given time and 1000 times the recovery
233// time.
234//
235// If deadtime and recovery time are 0 then t-1 is set.
236//
237// Sets fTime to t
238//
239// The default time is 0.
240//
241void APD::FillEmpty(Float_t t)
242{
243 const Int_t n = (fHist.GetNbinsX()+2)*(fHist.GetNbinsY()+2);
244
245 const Double_t tm = fDeadTime<=0 && fRecoveryTime<=0 ? t-1 : t-2*fDeadTime-1000*fRecoveryTime;
246
247 for (int i=0; i<n; i++)
248 fHist.GetArray()[i] = tm;
249
250 fHist.SetEntries(1);
251
252 fTime = t;
253}
254
255// --------------------------------------------------------------------------
256//
257// First call FillEmpty for the given time t. Then fill each cell by
258// by calling HitCellImp with time t-gRandom->Exp(n/rate) with n being
259// the total number of cells.
260//
261// Sets fTime to t
262//
263// The default time is 0.
264//
265void APD::FillRandom(Float_t rate, Float_t t)
266{
267 FillEmpty(t);
268
269 const Int_t nx = fHist.GetNbinsX();
270 const Int_t ny = fHist.GetNbinsY();
271
272 const Double_t f = (nx*ny)/rate;
273
274 // FIXME: This is not perfect, is it? What about the dead time?
275
276 for (int x=1; x<=nx; x++)
277 for (int y=1; y<=ny; y++)
278 HitCellImp(x, y, t-MMath::RndmExp(f));
279
280 fTime = t;
281}
282
283// --------------------------------------------------------------------------
284//
285// Evolve the chip from fTime to fTime+dt if it with a given frequency
286// freq. Returns the total signal "recorded".
287//
288// fTime is set to the fTime+dt
289//
290// If you want to evolve over a default relaxation time (relax the chip
291// from a signal) use Relax instead.
292//
293Float_t APD::Evolve(Double_t freq, Double_t dt)
294{
295 const Double_t avglen = 1./freq;
296
297 const Double_t end = fTime+dt;
298
299 Float_t hits = 0;
300
301 Double_t time = fTime;
302 while (1)
303 {
304 time += MMath::RndmExp(avglen);
305 if (time>end)
306 break;
307
308 hits += HitRandomCell(time);
309 }
310
311 fTime = end;
312
313 return hits;
314}
315
316// --------------------------------------------------------------------------
317//
318// Retunrs the number of cells which have a time t<=fDeadTime, i.e. which are
319// dead.
320// The default time is 0.
321//
322Int_t APD::CountDeadCells(Float_t t) const
323{
324 const Int_t nx = fHist.GetNbinsX();
325 const Int_t ny = fHist.GetNbinsY();
326
327 Int_t n=0;
328 for (int x=1; x<=nx; x++)
329 for (int y=1; y<=ny; y++)
330 if ((t-fHist.GetBinContent(x, y))<=fDeadTime)
331 n++;
332
333 return n;
334}
335
336// --------------------------------------------------------------------------
337//
338// Returs the number of cells which have a time t<=fDeadTime+fRecoveryTime.
339// The default time is 0.
340//
341Int_t APD::CountRecoveringCells(Float_t t) const
342{
343 const Int_t nx = fHist.GetNbinsX();
344 const Int_t ny = fHist.GetNbinsY();
345
346 Int_t n=0;
347 for (int x=1; x<=nx; x++)
348 for (int y=1; y<=ny; y++)
349 {
350 Float_t dt = t-fHist.GetBinContent(x, y);
351 if (dt>fDeadTime && dt<=fDeadTime+fRecoveryTime)
352 n++;
353 }
354 return n;
355}
Note: See TracBrowser for help on using the repository browser.