| 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 |
|
|---|
| 48 | ClassImp(APD);
|
|---|
| 49 |
|
|---|
| 50 | using namespace std;
|
|---|
| 51 |
|
|---|
| 52 | /*
|
|---|
| 53 | class MyProfile : public TProfile2D
|
|---|
| 54 | {
|
|---|
| 55 | public:
|
|---|
| 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 | //
|
|---|
| 77 | APD::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)
|
|---|
| 80 | {
|
|---|
| 81 | fHist.SetDirectory(0);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | // --------------------------------------------------------------------------
|
|---|
| 85 | //
|
|---|
| 86 | // This is the recursive implementation of a hit. If a photon hits a cell
|
|---|
| 87 | // at x and y (must be a valid cell!) at time t, at first we check if the
|
|---|
| 88 | // cell is still dead. If it is not dead we calculate the signal height
|
|---|
| 89 | // from the recovery time. Now we check with the crosstalk probability
|
|---|
| 90 | // whether another photon is created. If another photon is created we
|
|---|
| 91 | // calculate randomly which of the four neighbor cells are hit.
|
|---|
| 92 | // If the cell is outside the APD the photon is ignored. As many
|
|---|
| 93 | // new photons are created until our random number is below the crosstak-
|
|---|
| 94 | // probability.
|
|---|
| 95 | //
|
|---|
| 96 | // The total height of the signal (in units of photons) is returned.
|
|---|
| 97 | // Note, that this can be a fractional number.
|
|---|
| 98 | //
|
|---|
| 99 | // This function looks a bit fancy accessing the histogram and works around
|
|---|
| 100 | // a few histogram functions. This is a speed optimization which works
|
|---|
| 101 | // around a lot of sanity checks which are obsolete in our case.
|
|---|
| 102 | //
|
|---|
| 103 | // The default time is 0.
|
|---|
| 104 | //
|
|---|
| 105 | Float_t APD::HitCellImp(Int_t x, Int_t y, Float_t t)
|
|---|
| 106 | {
|
|---|
| 107 | // if (x<1 || x>fHist.GetNbinsX() ||
|
|---|
| 108 | // y<1 || y>fHist.GetNbinsY())
|
|---|
| 109 | // return 0;
|
|---|
| 110 |
|
|---|
| 111 | // const Int_t cell = fHist.GetBin(x, y);
|
|---|
| 112 | const Int_t cell = x + (fHist.GetNbinsX()+2)*y;
|
|---|
| 113 |
|
|---|
| 114 | // This is the fastes way to access the bin-contents in fArray
|
|---|
| 115 | Float_t &cont = fHist.GetArray()[cell];
|
|---|
| 116 |
|
|---|
| 117 | // const Double_t dt = t-fHist.GetBinContent(x, y)-fDeadTime; //
|
|---|
| 118 | const Float_t dt = t-cont-fDeadTime;
|
|---|
| 119 |
|
|---|
| 120 | // Photons within the dead time are just ignored
|
|---|
| 121 | if (/*hx.GetBinContent(x,y)>0 &&*/ dt<=0)
|
|---|
| 122 | return 0;
|
|---|
| 123 |
|
|---|
| 124 | // Signal height (in units of one photon) produced after dead time
|
|---|
| 125 | const Float_t weight = fRecoveryTime<=0 ? 1 : 1.-exp(-dt/fRecoveryTime);
|
|---|
| 126 |
|
|---|
| 127 | cont = t; // fHist.SetBinContent(x, y, t)
|
|---|
| 128 |
|
|---|
| 129 | // Counter for the numbers of produced photons
|
|---|
| 130 | Float_t n = weight;
|
|---|
| 131 |
|
|---|
| 132 | /*
|
|---|
| 133 | // Check if a photon in a neighboring cell is produced (crosstalk)
|
|---|
| 134 | while (gRandom->Rndm()<fCrosstalkProb)
|
|---|
| 135 | {
|
|---|
| 136 | // Get a random neighbor which is hit.
|
|---|
| 137 | switch (gRandom->Integer(4))
|
|---|
| 138 | {
|
|---|
| 139 | case 0: x++; if (x>fHist.GetNbinsX()) continue; break;
|
|---|
| 140 | case 1: x--; if (x<1) continue; break;
|
|---|
| 141 | case 2: y++; if (y>fHist.GetNbinsY()) continue; break;
|
|---|
| 142 | case 3: y--; if (y<1) continue; break;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | n += HitCellImp(x, y, t);
|
|---|
| 146 | }
|
|---|
| 147 | */
|
|---|
| 148 |
|
|---|
| 149 | //for (int i=0; i<1; i++)
|
|---|
| 150 | while (1)
|
|---|
| 151 | {
|
|---|
| 152 | const Double_t rndm = gRandom->Rndm();
|
|---|
| 153 | if (rndm>=fCrosstalkProb)
|
|---|
| 154 | break;
|
|---|
| 155 |
|
|---|
| 156 | // We can re-use the random number becuase it is uniformely
|
|---|
| 157 | // distributed. This saves cpu power
|
|---|
| 158 | const Int_t dir = TMath::FloorNint(4*rndm/fCrosstalkProb);
|
|---|
| 159 |
|
|---|
| 160 | // Get a random neighbor which is hit.
|
|---|
| 161 | switch (dir)
|
|---|
| 162 | {
|
|---|
| 163 | case 0: if (x<fHist.GetNbinsX()) n += HitCellImp(x+1, y, t); break;
|
|---|
| 164 | case 1: if (x>1) n += HitCellImp(x-1, y, t); break;
|
|---|
| 165 | case 2: if (y<fHist.GetNbinsY()) n += HitCellImp(x, y+1, t); break;
|
|---|
| 166 | case 3: if (y>1) n += HitCellImp(x, y-1, t); break;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | // In the unlikely case the calculated direction is out-of-range,
|
|---|
| 170 | // i.e. <0 or >3, we would just try to fill the same cell again which
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | return n;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | // --------------------------------------------------------------------------
|
|---|
| 177 | //
|
|---|
| 178 | // Check if x and y is a valid cell. If not return 0, otherwise
|
|---|
| 179 | // HitCelImp(x, y, t)
|
|---|
| 180 | //
|
|---|
| 181 | // The default time is 0.
|
|---|
| 182 | //
|
|---|
| 183 | Float_t APD::HitCell(Int_t x, Int_t y, Float_t t)
|
|---|
| 184 | {
|
|---|
| 185 | if (x<1 || x>fHist.GetNbinsX() ||
|
|---|
| 186 | y<1 || y>fHist.GetNbinsY())
|
|---|
| 187 | return 0;
|
|---|
| 188 |
|
|---|
| 189 | return HitCellImp(x, y, t);
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | // --------------------------------------------------------------------------
|
|---|
| 193 | //
|
|---|
| 194 | // Determine randomly (uniformly) a cell which was hit. Return
|
|---|
| 195 | // HitCellImp for this cell and the given time.
|
|---|
| 196 | //
|
|---|
| 197 | // The default time is 0.
|
|---|
| 198 | //
|
|---|
| 199 | Float_t APD::HitRandomCell(Float_t t)
|
|---|
| 200 | {
|
|---|
| 201 | const UInt_t nx = fHist.GetNbinsX();
|
|---|
| 202 | const UInt_t ny = fHist.GetNbinsY();
|
|---|
| 203 |
|
|---|
| 204 | const UInt_t idx = gRandom->Integer(nx*ny);
|
|---|
| 205 |
|
|---|
| 206 | const UInt_t x = idx%nx;
|
|---|
| 207 | const UInt_t y = idx/nx;
|
|---|
| 208 |
|
|---|
| 209 | return HitCellImp(x+1, y+1, t);
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | // --------------------------------------------------------------------------
|
|---|
| 213 | //
|
|---|
| 214 | // Sets all cells with a contents whihc is well before the time t such that
|
|---|
| 215 | // the chip is "virgin". Therefore all cells are set to a time which
|
|---|
| 216 | // is twice the deadtime before the given time and 1000 times the recovery
|
|---|
| 217 | // time.
|
|---|
| 218 | //
|
|---|
| 219 | // If deadtime and recovery time are 0 then t-1 is set.
|
|---|
| 220 | //
|
|---|
| 221 | // The default time is 0.
|
|---|
| 222 | //
|
|---|
| 223 | void APD::FillEmpty(Float_t t)
|
|---|
| 224 | {
|
|---|
| 225 | const Int_t n = (fHist.GetNbinsX()+2)*(fHist.GetNbinsY()+2);
|
|---|
| 226 |
|
|---|
| 227 | const Double_t tm = fDeadTime<=0 && fRecoveryTime<=0 ? t-1 : t-2*fDeadTime-1000*fRecoveryTime;
|
|---|
| 228 |
|
|---|
| 229 | for (int i=0; i<n; i++)
|
|---|
| 230 | fHist.GetArray()[i] = tm;
|
|---|
| 231 |
|
|---|
| 232 | fHist.SetEntries(1);
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | // --------------------------------------------------------------------------
|
|---|
| 236 | //
|
|---|
| 237 | // First call FillEmpty for the given time t. Then fill each cell by
|
|---|
| 238 | // by calling HitCellImp with time t-gRandom->Exp(n/rate) with n being
|
|---|
| 239 | // the total number of cells.
|
|---|
| 240 | // The default time is 0.
|
|---|
| 241 | //
|
|---|
| 242 | void APD::FillRandom(Float_t rate, Float_t t)
|
|---|
| 243 | {
|
|---|
| 244 | FillEmpty(t);
|
|---|
| 245 |
|
|---|
| 246 | const Int_t nx = fHist.GetNbinsX();
|
|---|
| 247 | const Int_t ny = fHist.GetNbinsY();
|
|---|
| 248 |
|
|---|
| 249 | const Double_t f = (nx*ny)/rate;
|
|---|
| 250 |
|
|---|
| 251 | // FIXME: This is not perfect, is it? What about the dead time?
|
|---|
| 252 |
|
|---|
| 253 | for (int x=1; x<=nx; x++)
|
|---|
| 254 | for (int y=1; y<=ny; y++)
|
|---|
| 255 | {
|
|---|
| 256 | HitCellImp(x, y, t-MMath::RndmExp(f));
|
|---|
| 257 | }
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | // --------------------------------------------------------------------------
|
|---|
| 261 | //
|
|---|
| 262 | // Retunrs the number of cells which have a time t<=fDeadTime, i.e. which are
|
|---|
| 263 | // dead.
|
|---|
| 264 | // The default time is 0.
|
|---|
| 265 | //
|
|---|
| 266 | Int_t APD::CountDeadCells(Float_t t) const
|
|---|
| 267 | {
|
|---|
| 268 | const Int_t nx = fHist.GetNbinsX();
|
|---|
| 269 | const Int_t ny = fHist.GetNbinsY();
|
|---|
| 270 |
|
|---|
| 271 | Int_t n=0;
|
|---|
| 272 | for (int x=1; x<=nx; x++)
|
|---|
| 273 | for (int y=1; y<=ny; y++)
|
|---|
| 274 | if ((t-fHist.GetBinContent(x, y))<=fDeadTime)
|
|---|
| 275 | n++;
|
|---|
| 276 |
|
|---|
| 277 | return n;
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | // --------------------------------------------------------------------------
|
|---|
| 281 | //
|
|---|
| 282 | // Returs the number of cells which have a time t<=fDeadTime+fRecoveryTime.
|
|---|
| 283 | // The default time is 0.
|
|---|
| 284 | //
|
|---|
| 285 | Int_t APD::CountRecoveringCells(Float_t t) const
|
|---|
| 286 | {
|
|---|
| 287 | const Int_t nx = fHist.GetNbinsX();
|
|---|
| 288 | const Int_t ny = fHist.GetNbinsY();
|
|---|
| 289 |
|
|---|
| 290 | Int_t n=0;
|
|---|
| 291 | for (int x=1; x<=nx; x++)
|
|---|
| 292 | for (int y=1; y<=ny; y++)
|
|---|
| 293 | {
|
|---|
| 294 | Float_t dt = t-fHist.GetBinContent(x, y);
|
|---|
| 295 | if (dt>fDeadTime && dt<=fDeadTime+fRecoveryTime)
|
|---|
| 296 | n++;
|
|---|
| 297 | }
|
|---|
| 298 | return n;
|
|---|
| 299 | }
|
|---|