source: branches/MarsGapdTimeJitter/msimcamera/MSimCamera.cc@ 18108

Last change on this file since 18108 was 18108, checked in by Jens Buss, 10 years ago
added feature GapdTimeJitter to give individual photons/gapds an offset with a gaussian sampling
File size: 19.3 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@phys.ethz.ch>
19!
20! Copyright: CheObs Software Development, 2000-2013
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> // Needed for TRandom
45
46#include "MLog.h"
47#include "MLogManip.h"
48
49#include "MTruePhotonsPerPixelCont.h"
50
51#include "MSpline3.h"
52#include "MParSpline.h"
53
54#include "MParList.h"
55
56#include "MPhotonEvent.h"
57#include "MPhotonData.h"
58
59#include "MPedestalCam.h"
60#include "MPedestalPix.h"
61
62#include "MAnalogSignal.h"
63#include "MAnalogChannels.h"
64
65#include "MParameters.h"
66
67#include "MMcEvt.hxx" // To be replaced by a CheObs class
68#include "MRawRunHeader.h"
69
70ClassImp(MSimCamera);
71
72using namespace std;
73
74// --------------------------------------------------------------------------
75//
76// Default Constructor.
77//
78MSimCamera::MSimCamera(const char* name, const char *title)
79 : fEvt(0), fStat(0), fRunHeader(0), fElectronicNoise(0), fGain(0),
80 fCamera(0), fMcEvt(0),fCrosstalkCoeffParam(0), fSpline(0), fBaselineGain(kFALSE),
81 fDefaultOffset(-1), fDefaultNoise(-1), fDefaultGain(-1), fACFudgeFactor(0),
82 fACTimeConstant(0)
83
84{
85 fName = name ? name : "MSimCamera";
86 fTitle = title ? title : "Task to simulate the electronic noise and to convert photons into pulses";
87}
88
89// --------------------------------------------------------------------------
90//
91// Search for the necessayr parameter containers.
92// Setup spline for pulse shape.
93//
94Int_t MSimCamera::PreProcess(MParList *pList)
95{
96 fMcEvt = (MMcEvt*)pList->FindCreateObj("MMcEvt");
97 if (!fMcEvt)
98 return kFALSE;
99
100 fCamera = (MAnalogChannels*)pList->FindCreateObj("MAnalogChannels");
101 if (!fCamera)
102 return kFALSE;
103
104 fEvt = (MPhotonEvent*)pList->FindObject("MPhotonEvent");
105 if (!fEvt)
106 {
107 *fLog << err << "MPhotonEvent not found... aborting." << endl;
108 return kFALSE;
109 }
110
111 fStat = (MPhotonStatistics*)pList->FindObject("MPhotonStatistics");
112 if (!fStat)
113 {
114 *fLog << err << "MPhotonStatistics not found... aborting." << endl;
115 return kFALSE;
116 }
117
118 fRunHeader = (MRawRunHeader *)pList->FindObject("MRawRunHeader");
119 if (!fRunHeader)
120 {
121 *fLog << err << "MRawRunHeader not found... aborting." << endl;
122 return kFALSE;
123 }
124 // -------------------------------------------------------------------
125 // Dominik Neise and Sebastian Mueller on fix time offsets:
126 // We obtain the fix temporal offsets for the FACT camera pixels out of
127 // a text file. The textfile must be mentioned in the ceres.rc file.
128 // There are no default offsets on purporse. The filename must be specified
129 // in ceres.rc and the file must be parsed without errors and it must
130 // provide exactly 1440 floating point numbers.
131 fFixTimeOffsetsBetweenPixelsInNs =
132 (MMatrix*)pList->FindObject("MFixTimeOffset");
133 if (!fFixTimeOffsetsBetweenPixelsInNs)
134 {
135 // the key value pair providing the text file is not present in the
136 // environment env.
137 *fLog << err << "In Source: "<< __FILE__ <<" in line: "<< __LINE__;
138 *fLog << " in function: "<< __func__ <<"\n";
139 *fLog << "MFixTimeOffset not found... aborting." << endl;
140 return kFALSE;
141
142 }
143 else if ( fFixTimeOffsetsBetweenPixelsInNs->fM.size() != 1440 )
144 {
145 // The number of time offsets must match the number of pixels in the
146 // FACT camera.
147 *fLog << err << "In Source: "<< __FILE__ <<" in line: "<< __LINE__;
148 *fLog << " in function: "<< __func__ <<"\n";
149 *fLog << "MFixTimeOffset has the wrong dimension! ";
150 *fLog << "There should be "<< 1440 <<" time offsets ";
151 *fLog << "(one for each pixel in FACT) but there are: ";
152 *fLog << fFixTimeOffsetsBetweenPixelsInNs->fM.size() << "! ";
153 *fLog << "... aborting." << endl;
154 return kFALSE;
155 }
156 // Check all entries for inf and nan. Those are not accepted here.
157 for( std::vector< double > row : fFixTimeOffsetsBetweenPixelsInNs->fM ){
158 for( double number : row){
159
160 if( std::isnan(number) || std::isinf(number) ){
161
162 *fLog << err << "In Source: "<< __FILE__ <<" in line: ";
163 *fLog << __LINE__;
164 *fLog << " in function: "<< __func__ <<"\n";
165 *fLog << "There is a non normal number in the fix temporal ";
166 *fLog << "pixel offsets. This is at least one number is ";
167 *fLog << "NaN or Inf. This here is >"<< number;
168 *fLog << "<... aborting." << endl;
169 return kFALSE;
170 }
171 }
172 }
173 // -------------------------------------------------------------------
174/*
175 fPulsePos = (MParameterD*)pList->FindObject("IntendedPulsePos", "MParameterD");
176 if (!fPulsePos)
177 {
178 *fLog << err << "IntendedPulsePos [MParameterD] not found... aborting." << endl;
179 return kFALSE;
180 }
181 */
182 fResidualTimeSpread = (MParameterD*)pList->FindObject("ResidualTimeSpread");
183 if (!fResidualTimeSpread)
184 {
185 *fLog << err << "ResidualTimeSpread [MParameterD] not found... aborting." << endl;
186 return kFALSE;
187 }
188
189 // Get GapdTimeJitter from parameter list
190 fGapdTimeJitter = (MParameterD*)pList->FindObject("GapdTimeJitter");
191 if (!fGapdTimeJitter)
192 {
193 *fLog << err << "GapdTimeJitter [MParameterD] not found... aborting." << endl;
194 return kFALSE;
195 }
196
197 // Create it here to make sure that MGeomApply will set the correct size
198 fElectronicNoise = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", "ElectronicNoise");
199 if (!fElectronicNoise)
200 return kFALSE;
201
202 fGain = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", "Gain");
203 if (!fGain)
204 return kFALSE;
205
206 fAccidentalPhotons = (MPedestalCam*)pList->FindObject("AccidentalPhotonRates","MPedestalCam");
207 if(!fAccidentalPhotons)
208 {
209 *fLog << err << "AccidentalPhotonRates [MPedestalCam] not found... aborting." << endl;
210 return kFALSE;
211 }
212
213 fCrosstalkCoeffParam = (MParameterD*)pList->FindCreateObj("MParameterD","CrosstalkCoeffParam");
214 if (!fCrosstalkCoeffParam)
215 {
216 *fLog << err << "CrosstalkCoeffParam [MParameterD] not found... aborting." << endl;
217 return kFALSE;
218 }
219
220 fTruePhotons = (MTruePhotonsPerPixelCont*)pList->FindCreateObj("MTruePhotonsPerPixelCont");
221 if (!fTruePhotons)
222 {
223 *fLog << err << "MTruePhotonsPerPixelCont not found... aborting." << endl;
224 return kFALSE;
225 }
226
227 MParSpline *pulse = (MParSpline*)pList->FindObject("PulseShape", "MParSpline");
228 if (!pulse)
229 {
230 *fLog << err << "PulseShape [MParSpline] not found... aborting." << endl;
231 return kFALSE;
232 }
233
234// if (fRunHeader->GetFreqSampling()!=1000)
235// {
236// *fLog << err << "ERROR - Sampling frequencies others than 1GHz are not yet supported." << endl;
237// *fLog << warn << "FIXME - SCALE MPulsShape WITH THE SAMPLING FREQUENCY." << endl;
238// return kFALSE;
239// }
240
241 fSpline = pulse->GetSpline();
242 if (!fSpline)
243 {
244 *fLog << err << "No spline initialized." << endl;
245 return kFALSE;
246 }
247
248 // ---------------- Information output ----------------------
249
250 if (fBaselineGain)
251 *fLog << inf << "Gain is also applied to the electronic noise." << endl;
252
253 return kTRUE;
254}
255
256// --------------------------------------------------------------------------
257//
258// FIXME: For now this is a workaround to set a baseline and the
259// electronic (guassian noise)
260//
261Bool_t MSimCamera::ReInit(MParList *plist)
262{
263 for (int i=0; i<fElectronicNoise->GetSize(); i++)
264 {
265 MPedestalPix &ped = (*fElectronicNoise)[i];
266 ped.SetPedestal(fDefaultOffset);
267 if (fDefaultNoise>0)
268 ped.SetPedestalRms(fDefaultNoise);
269
270 ped.SetPedestalABoffset(0);
271 ped.SetNumEvents(0);
272
273
274 MPedestalPix &gain = (*fGain)[i];
275 if (fDefaultGain>0)
276 gain.SetPedestal(fDefaultGain);
277
278 gain.SetPedestalRms(0);
279 gain.SetPedestalABoffset(0);
280 gain.SetNumEvents(0);
281 }
282
283 return kTRUE;
284}
285
286// --------------------------------------------------------------------------
287//
288// fStat->GetMaxIndex must return the maximum index possible
289// (equiv. number of pixels) not just the maximum index stored!
290//
291Int_t MSimCamera::Process()
292{
293 // Calculate start time, end time and corresponding number of samples
294 const Double_t freq = fRunHeader->GetFreqSampling()/1000.;
295
296 // FIXME: Should we use a higher sampling here?
297
298 const Double_t start = fStat->GetTimeFirst()*freq;
299 const Double_t end = fStat->GetTimeLast() *freq;
300
301 const UInt_t nlen = TMath::CeilNint(end-start);
302
303 // Get number of pixels/channels
304 const UInt_t npix = fStat->GetMaxIndex()+1;
305
306 if (npix>(UInt_t)fElectronicNoise->GetSize())
307 {
308 *fLog << err << "ERROR - More indices (" << npix << ") ";
309 *fLog << "assigned than existing in camera (";
310 *fLog << fElectronicNoise->GetSize() << ")!" << endl;
311 return kERROR;
312 }
313
314 const Double_t pl = fSpline->GetXmin()*freq;
315 const Double_t pr = fSpline->GetXmax()*freq;
316
317 // Init the arrays and set the range which will contain valid data
318 fCamera->Init(npix, nlen);
319 fCamera->SetValidRange(TMath::FloorNint(pr), TMath::CeilNint(nlen+pl));
320
321 Double_t timeoffset[npix];
322
323
324 // Add electronic noise to empty channels
325 for (UInt_t i=0; i<npix; i++)
326 {
327
328 // Get the ResidualTimeSpread Parameter
329 const Double_t residualTimeSpread = fResidualTimeSpread->GetVal();
330
331 // Jens Buss on residual time spread:
332 // randomly draw an additional time offset to be added to the arrivaltime
333 // from a gaussian normal distribution with a given standard deviation
334 timeoffset[i] = gRandom->Gaus(0.0, residualTimeSpread);
335 const MPedestalPix &pix = (*fElectronicNoise)[i];
336
337 const Double_t val = pix.GetPedestal();
338 const Double_t rms = pix.GetPedestalRms();
339
340 // FTemme: Implementation of AC-coupling:
341 // to calculate the value of the accoupling per slice I use the
342 // following equation:
343 // accouplingPerSlice = accidentalPhotonRate * (1 + crossTalkProb)
344 // * areaOfOnePulse / samplingRate;
345 // Therefore I need the following variables
346 // Double_t accidentalPhotonRate; // [MHz]
347 // Float_t crossTalkProb; // [1]
348 // Double_t areaOfOnePulse; // [ADC-Counts * s]
349 // Double_t samplingRate; // [slices * MHz]
350
351 // The accidental photon rate is stored in GHz, so we have to multiply
352 // with 1E3 to get MHz:
353 const MPedestalPix &accPhoPix = (*fAccidentalPhotons)[i];
354
355 const Double_t accidentalPhotonRate = accPhoPix.GetPedestal() * 1e3; //[MHz]
356
357 Double_t currentAccidentalPhotonRate = accidentalPhotonRate;
358 if (fACTimeConstant!=0)
359 {
360 const Double_t accidentalPhotons = fACTimeConstant * accidentalPhotonRate;
361 const Double_t sigmaAccidentalPhotons = TMath::Sqrt(accidentalPhotons);
362
363 const Double_t gaus = gRandom->Gaus(accidentalPhotons,sigmaAccidentalPhotons);
364
365 currentAccidentalPhotonRate = gaus / fACTimeConstant;
366 }
367
368 // Get the CrosstalkCoefficient Parameter
369 const Double_t crossTalkProb = fCrosstalkCoeffParam->GetVal();
370
371 // To get the area of one Pulse, I only need to calculate the Integral
372 // of the Pulse Shape, which is stored in fSpline. Because the spline is
373 // normalized to a maximal amplitude of 1.0, I had to multiply it with
374 // the Default gain [ADC-Counts * s]
375 const Double_t areaOfOnePulse = fSpline->Integral() * fDefaultGain;
376
377 // The sampling rate I get from the RunHeader:
378 const Double_t samplingRate = fRunHeader->GetFreqSampling(); // [slices * MHz]
379
380 const Double_t accouplingPerSlice = currentAccidentalPhotonRate
381 * (1 + crossTalkProb + fACFudgeFactor)
382 * areaOfOnePulse / samplingRate;
383
384 // The accoupling is substracted from the timeline by decreasing the
385 // mean of the gaussian noise which is added
386
387 if (!fBaselineGain)
388 {
389 (*fCamera)[i].AddGaussianNoise(rms, val - accouplingPerSlice);
390 continue;
391 }
392 // Sorry, the name "pedestal" is misleading here
393 // FIXME: Simulate gain fluctuations
394 const Double_t gain = (*fGain)[i].GetPedestal();
395
396 // FIXME: We might add the base line here already.
397 // FIXME: How stable is the offset?
398 // FIXME: Should we write a container AppliedGain for MSImTrigger?
399
400 (*fCamera)[i].AddGaussianNoise(rms*gain, (val - accouplingPerSlice)*gain);
401 }
402
403 // FIXME: Simulate correlations with neighboring pixels
404
405 const Int_t num = fEvt->GetNumPhotons();
406
407 // A random shift, uniformely distributed within one slice, to make sure that
408 // the first photon is not always aligned identically with a sample edge.
409 // FIXME: Make it switchable
410 const Float_t rndm = gRandom->Uniform();
411
412 // FIXME: Shell we add a random shift of [0,1] samples per channel?
413 // Or maybe per channel and run?
414
415 Double_t tot = 0;
416
417 for (int i=0 ; i<1440 ; i++)
418 {
419 (*fTruePhotons->cherenkov_photons_weight)[i] = 0;
420 (*fTruePhotons->cherenkov_photons_number)[i] = 0;
421 (*fTruePhotons->cherenkov_arrival_time_mean)[i] = 0;
422 (*fTruePhotons->cherenkov_arrival_time_variance)[i] = 0;
423 (*fTruePhotons->muon_cherenkov_photons_weight)[i] = 0;
424 (*fTruePhotons->muon_cherenkov_photons_number)[i] = 0;
425 (*fTruePhotons->cherenkov_arrival_time_min)[i] = 10000;
426 (*fTruePhotons->cherenkov_arrival_time_max)[i] = 0;
427 (*fTruePhotons->noise_photons_weight)[i] = 0;
428 }
429
430 //--------------------------------------------------------------------------
431
432 // Get the ResidualTimeSpread Parameter
433 const Double_t gapdTimeJitter = fGapdTimeJitter->GetVal();
434
435 // Simulate pulses
436 for (Int_t i=0; i<num; i++)
437 {
438 const MPhotonData &ph = (*fEvt)[i];
439
440 const UInt_t idx = ph.GetTag();
441 Double_t t = (ph.GetTime()-fStat->GetTimeFirst())*freq+rndm;// - fSpline->GetXmin();
442
443 // Sebastian Mueller and Dominik Neise on fix time offsets:
444 // We add a fix temporal offset to the relative arrival time of the
445 // individual pixel. The offsets are stored in the
446 // fFixTimeOffsetsBetweenPixelsInNs -> fM matrix. We identify the first
447 // column to hold the offsets in ns.
448 t = t + freq*fFixTimeOffsetsBetweenPixelsInNs->fM[idx][0];
449
450 // Jens Buss on residual time spread:
451 // add random time offset to the arrivaltimes
452 t = t + timeoffset[idx];
453
454 // Jens Buss on GapdTimeJitter
455 // add also a time offset to arrival times of single photons
456 Double_t timeJitter = gRandom->Gaus(0.0, gapdTimeJitter);
457 t = t + timeJitter;
458
459 // FIXME: Time jitter?
460 // FIXME: Add additional routing here?
461 // FIMXE: How stable is the gain?
462
463 if (ph.GetPrimary()!=MMcEvt::kNightSky && ph.GetPrimary()!=MMcEvt::kArtificial)
464 {
465 tot += ph.GetWeight();
466 (*fTruePhotons->cherenkov_photons_weight)[idx] += ph.GetWeight();
467 (*fTruePhotons->cherenkov_photons_number)[idx] += 1;
468
469 (*fTruePhotons->cherenkov_arrival_time_mean)[idx] += t;
470 (*fTruePhotons->cherenkov_arrival_time_variance)[idx] += t*t;
471
472 if (ph.GetPrimary()==MMcEvt::kMUON)
473 {
474 (*fTruePhotons->muon_cherenkov_photons_weight)[idx] += ph.GetWeight();
475 (*fTruePhotons->muon_cherenkov_photons_number)[idx] += 1;
476 }
477
478 // find min
479 if (t < (*fTruePhotons->cherenkov_arrival_time_min)[idx] )
480 {
481 (*fTruePhotons->cherenkov_arrival_time_min)[idx] = t;
482 }
483 // find max
484 if (t > (*fTruePhotons->cherenkov_arrival_time_max)[idx] )
485 {
486 (*fTruePhotons->cherenkov_arrival_time_max)[idx] = t;
487 }
488 }
489 else
490 {
491 (*fTruePhotons->noise_photons_weight)[idx] += ph.GetWeight();
492 }
493
494 // Sorry, the name "pedestal" is misleading here
495 // FIXME: Simulate gain fluctuations
496 const Double_t gain = (*fGain)[idx].GetPedestal();
497
498 // === FIXME === FIXME === FIXME === Frequency!!!!
499 (*fCamera)[idx].AddPulse(*fSpline, t, ph.GetWeight()*gain);
500 }
501
502 for (unsigned int i=0 ; i < 1440 ; i++)
503 {
504 float number = (*fTruePhotons->cherenkov_photons_number)[i];
505 (*fTruePhotons->cherenkov_arrival_time_mean)[i] /= number;
506 float mean = (*fTruePhotons->cherenkov_arrival_time_mean)[i];
507 float sum_tt = (*fTruePhotons->cherenkov_arrival_time_variance)[i];
508 (*fTruePhotons->cherenkov_arrival_time_variance)[i] = (sum_tt / number - mean*mean) /(number - 1);
509 }
510
511 fMcEvt->SetPhotElfromShower(TMath::Nint(tot));
512
513 return kTRUE;
514}
515
516// --------------------------------------------------------------------------
517//
518// BaselineGain: Off
519//
520Int_t MSimCamera::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
521{
522 Bool_t rc = kFALSE;
523 if (IsEnvDefined(env, prefix, "BaselineGain", print))
524 {
525 rc = kTRUE;
526 fBaselineGain = GetEnvValue(env, prefix, "BaselineGain", fBaselineGain);
527 }
528
529 if (IsEnvDefined(env, prefix, "DefaultOffset", print))
530 {
531 rc = kTRUE;
532 fDefaultOffset = GetEnvValue(env, prefix, "DefaultOffset", fDefaultOffset);
533 }
534 if (IsEnvDefined(env, prefix, "DefaultNoise", print))
535 {
536 rc = kTRUE;
537 fDefaultNoise = GetEnvValue(env, prefix, "DefaultNoise", fDefaultNoise);
538 }
539 if (IsEnvDefined(env, prefix, "DefaultGain", print))
540 {
541 rc = kTRUE;
542 fDefaultGain = GetEnvValue(env, prefix, "DefaultGain", fDefaultGain);
543 }
544 if (IsEnvDefined(env, prefix, "ACFudgeFactor", print))
545 {
546 rc = kTRUE;
547 fACFudgeFactor = GetEnvValue(env, prefix, "ACFudgeFactor", fACFudgeFactor);
548 }
549 if (IsEnvDefined(env, prefix, "ACTimeConstant", print))
550 {
551 rc = kTRUE;
552 fACTimeConstant = GetEnvValue(env, prefix, "ACTimeConstant", fACTimeConstant);
553 }
554
555 return rc;
556}
Note: See TracBrowser for help on using the repository browser.