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

Last change on this file since 9368 was 9356, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 6.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 "MAvalanchePhotoDiode.h"
69
70ClassImp(MSimAPD);
71
72using namespace std;
73
74// --------------------------------------------------------------------------
75//
76// Default Constructor.
77//
78MSimAPD::MSimAPD(const char* name, const char *title)
79: fGeom(0), fEvt(0), fStat(0)
80{
81 fName = name ? name : "MSimAPD";
82 fTitle = title ? title : " Task to simulate the detection behaviour of APDs";
83}
84
85// --------------------------------------------------------------------------
86//
87// Get the necessary parameter containers
88//
89Int_t MSimAPD::PreProcess(MParList *pList)
90{
91 if (fNameGeomCam.IsNull())
92 {
93 *fLog << inf << "No geometry container... skipping." << endl;
94 return kSKIP;
95 }
96
97 fGeom = (MGeomCam*)pList->FindObject(fNameGeomCam, "MGeomCam");
98 if (!fGeom)
99 {
100 *fLog << inf << fNameGeomCam << " [MGeomCam] not found..." << endl;
101
102 fGeom = (MGeomCam*)pList->FindObject("MGeomCam");
103 if (!fGeom)
104 {
105 *fLog << err << "MGeomCam not found... aborting." << endl;
106 return kFALSE;
107 }
108 }
109
110 fStat = (MPhotonStatistics*)pList->FindObject("MPhotonStatistics");
111 if (!fStat)
112 {
113 *fLog << err << "MPhotonStatistics not found... aborting." << endl;
114 return kFALSE;
115 }
116
117 fEvt = (MPhotonEvent*)pList->FindObject("MPhotonEvent");
118 if (!fEvt)
119 {
120 *fLog << err << "MPhotonEvent not found... aborting." << endl;
121 return kFALSE;
122 }
123
124 return kTRUE;
125}
126
127// --------------------------------------------------------------------------
128//
129// Initialize as many APDs as we have pixels in the fGeomCam
130//
131Bool_t MSimAPD::ReInit(MParList *plist)
132{
133 if (UInt_t(fAPDs.GetEntriesFast())!=fGeom->GetNumPixels())
134 {
135 fAPDs.Delete();
136
137 // FIXME:
138 // * initialize an empty APD and read the APD setup from a file to
139 // allow different APDs.
140 // * Make the arguments a data member of MSimAPD
141
142 for (UInt_t i=0; i<fGeom->GetNumPixels(); i++)
143 //fAPDs.Add(new APD(60, 0.2, 3, 8.75));
144 //fAPDs.Add(new APD(60/2, 0.2, 3e-9, 8.75e-9*4));
145 fAPDs.Add(new APD(60/2, 0.2, 3, 8.75*4));
146 }
147
148 return kTRUE;
149}
150
151// --------------------------------------------------------------------------
152//
153// Process all photons through the corresponding APD and set the output
154// (weight) accordingly.
155//
156Int_t MSimAPD::Process()
157{
158 //const Double_t rate = 40e9;
159 // FIXME: Where do we get this number from??
160 // const Double_t rate = 0.04;
161
162 // Make all APDs look neutral for the first hit by a photon according to the
163 // average hit rate
164 const UInt_t npix = fAPDs.GetEntriesFast();
165
166 // Check if we can safely proceed (this can fail if we either haven't been
167 // ReInit'ed or the max index in MPhotonStatistics is wrong)
168 if ((Int_t)npix<fStat->GetMaxIndex())
169 {
170 *fLog << err << "ERROR - MSimAPD::Process: Only " << npix << " APDs initialized. At least " << fStat->GetMaxIndex() << " needed... abort." << endl;
171 return kERROR;
172 }
173
174 for (UInt_t idx=0; idx<npix; idx++)
175 static_cast<APD*>(fAPDs.UncheckedAt(idx))->FillRandom(fFreq, fStat->GetTimeFirst());
176
177 // Get number of photons
178 const Int_t num = fEvt->GetNumPhotons();
179
180 // Loop over all photons
181 for (Int_t i=0; i<num; i++)
182 {
183 // Get i-th photon
184 MPhotonData &ph = (*fEvt)[i];
185
186 // Get arrival time of photon and idx
187 const Double_t t = ph.GetTime();
188 const Int_t idx = ph.GetTag();
189 if (idx<0)
190 {
191 *fLog << err << "ERROR - MSimAPD: Invalid index -1." << endl;
192 return kERROR;
193 }
194
195 if (ph.GetWeight()!=1)
196 {
197 *fLog << err << "ERROR - MSimAPD: Weight of " << i << "-th photon not 1, but " << ph.GetWeight() << endl;
198 ph.Print();
199 return kERROR;
200 }
201 // Simulate hitting the APD (the signal height in effective
202 // "number of photons" is returned)
203 const Double_t hits = static_cast<APD*>(fAPDs.UncheckedAt(idx))->HitRandomCell(t);
204
205 // FIXME: Make a proper simulation of the excess noise!!!
206 //const Double_t signal = gRandom->Gaus(hits, 0.2*TMath::Sqrt(hits));
207
208 // Set the weight to the input
209 ph.SetWeight(hits);
210 }
211
212 return kTRUE;
213}
214
215// --------------------------------------------------------------------------
216//
217// NameGeomCam
218//
219Int_t MSimAPD::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
220{
221 Bool_t rc = kFALSE;
222 if (IsEnvDefined(env, prefix, "NameGeomCam", print))
223 {
224 rc = kTRUE;
225 fNameGeomCam = GetEnvValue(env, prefix, "NameGeomCam", fNameGeomCam);
226 }
227
228 return rc;
229}
Note: See TracBrowser for help on using the repository browser.