LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
OptDetDigitizer_module.cc
Go to the documentation of this file.
1 // OptDetDigitizer_module.cc
2 // Kazuhiro Terao <kazuhiro@nevis.columbia.edu>, Jul 2013
3 // based on code by Ben Jones and Christie Chiu, MIT, Sept 2012
4 // bjpjones@mit.edu, cschiu@mit.edu
5 //
6 // This module starts from MC truth sim::OnePhoton objects
7 // and produces a digitized waveform.
8 
9 // LArSoft includes
17 
18 // ART includes
23 #include "fhiclcpp/ParameterSet.h"
24 
25 // nurandom
27 
28 // CLHEP includes
29 #include "CLHEP/Random/RandFlat.h"
30 #include "CLHEP/Random/RandPoisson.h"
31 
32 // C++ language includes
33 #include <cstring>
34 
35 namespace opdet {
36 
38  public:
39  explicit OptDetDigitizer(const fhicl::ParameterSet&);
40 
41  private:
42  void produce(art::Event&) override;
43 
44  // The parameters we'll read from the .fcl file.
45  std::string fInputModule; // Input tag for OpDet collection
46  float fSampleFreq; // in MHz
47  float fTimeBegin; // in us
48  float fTimeEnd; // in us
49  float fQE; // quantum efficiency of opdet
50  optdata::ADC_Count_t fSaturationScale; // adc count w/ saturation occurs
51  std::vector<optdata::ADC_Count_t> fPedMeanArray; // Array of pedestal baseline (per ch)
52  float fDarkRate; // Noise rate in Hz
53  optdata::ADC_Count_t fPedFlucAmp; // Pedestal fluctuation amplitude
54  float fPedFlucRate; // Pedestal fluctuation rate
55  // float fWFRandTimeOffsetLow; // The lower bound of WF's T=0 offset from Trigger
56  // float fWFRandTimeOffsetHigh; // The upper bound of WF's T=0 offset from Trigger
57  std::vector<double> fSinglePEWaveform;
58 
60 
61  CLHEP::HepRandomEngine& fEngine;
62  CLHEP::RandFlat fFlatRandom;
63  CLHEP::RandPoisson fPoissonRandom;
64  void AddDarkNoise(std::vector<double>& RawWF, double gain);
66  std::vector<double>& OldPulse,
67  std::vector<double>& NewPulse,
68  double factor,
69  bool extend = false);
70  optdata::ChannelData ApplyDigitization(std::vector<double> const RawWF,
71  optdata::Channel_t const ch) const;
74  };
75 } // namespace opdet
76 
77 namespace opdet {
78 
80 
81 } //end namespace opdet
82 
83 namespace opdet {
84 
86  : EDProducer{pset}
88  pset,
89  "Seed"))
92  {
93  // Infrastructure piece
94  produces<std::vector<optdata::ChannelDataGroup>>();
95 
97  // Input Module and histogram parameters come from .fcl
98  fInputModule = pset.get<std::string>("InputModule");
99  fSimGainSpread = pset.get<bool>("SimGainSpread");
100  fTimeBegin = fOpDigiProperties->TimeBegin();
101  fTimeEnd = fOpDigiProperties->TimeEnd();
102  fSampleFreq = fOpDigiProperties->SampleFreq();
103  fQE = fOpDigiProperties->QE();
104  fDarkRate = fOpDigiProperties->DarkRate();
105  fPedFlucAmp = fOpDigiProperties->PedFlucAmp();
106  fPedFlucRate = fOpDigiProperties->PedFlucRate();
107  fSaturationScale = fOpDigiProperties->SaturationScale();
108  fPedMeanArray = fOpDigiProperties->PedMeanArray();
109 
110  fSinglePEWaveform = fOpDigiProperties->SinglePEWaveform();
111  }
112 
113  //-------------------------------------------------
114 
116  std::vector<double>& OldPulse,
117  std::vector<double>& NewPulse,
118  double const factor,
119  bool const extend)
120  {
121  if ((time + NewPulse.size()) > OldPulse.size() && extend)
122  OldPulse.resize(time + NewPulse.size());
123  for (size_t i = 0; i < NewPulse.size() && (time + i) < OldPulse.size(); ++i)
124  OldPulse[time + i] += NewPulse[i] * factor;
125  }
126 
127  //-------------------------------------------------
128 
129  void OptDetDigitizer::AddDarkNoise(std::vector<double>& RawWF, double gain)
130  {
131  // Add dark noise
132  double MeanDarkPulses = fDarkRate * (fTimeEnd - fTimeBegin) / 1000000;
133 
134  unsigned int NumberOfPulses = fPoissonRandom.fire(MeanDarkPulses);
135  for (size_t i = 0; i != NumberOfPulses; ++i) {
136  double PulseTime_ns = fTimeBegin * 1000 + (fTimeEnd - fTimeBegin) * 1000 *
137  (fFlatRandom.fire(1.0)); // Should be in ns
138  optdata::TimeSlice_t PulseTime_ts = fOpDigiProperties->GetTimeSlice(PulseTime_ns);
139  AddWaveform(PulseTime_ts, RawWF, fSinglePEWaveform, gain);
140  }
141  }
142 
144  optdata::Channel_t const ch) const
145  {
146  //
147  // Digitization includes...
148  // (a) amplitude digitization
149  // (b) saturation
150  // (c) pedestal fluctuation
151  //
152 
153  // prepare return data container
154  optdata::ChannelData chData(ch);
155  chData.reserve(rawWF.size());
156  optdata::ADC_Count_t baseMean(fPedMeanArray.at(ch));
157  for (optdata::TimeSlice_t time = 0; time < rawWF.size(); ++time) {
158  double thisSample = rawWF[time];
159 
160  optdata::ADC_Count_t thisCount = (optdata::ADC_Count_t)(thisSample) + baseMean;
161 
162  // (a) amplitude digitization
163  if (CLHEP::RandFlat::shoot(1.0) < (thisSample - int(thisSample))) thisCount += 1;
164 
165  // (b) saturation
166  if (thisCount > fSaturationScale) thisCount = fSaturationScale;
167 
168  chData.push_back(thisCount);
169  }
170 
171  // (c) pedestal fluctuation
172  double timeSpan = chData.size() * 1.e-6 / (fOpDigiProperties->SampleFreq());
173  unsigned int nFluc = CLHEP::RandPoisson::shoot(fPedFlucRate * timeSpan);
174  for (size_t i = 0; i < nFluc; ++i) {
175  optdata::TimeSlice_t pulseTime(CLHEP::RandFlat::shoot(0.0, (double)(chData.size())));
176  optdata::ADC_Count_t amp = chData[pulseTime];
177  if (CLHEP::RandFlat::shoot(0., 1.) > 0.5) {
178  amp += fPedFlucAmp;
179  if (amp > fSaturationScale) amp = fSaturationScale;
180  }
181  else
182  amp -= fPedFlucAmp;
183  chData[pulseTime] = amp;
184  }
185 
186  return chData;
187  }
188 
189  //-------------------------------------------------
190 
192  {
193 
194  //
195  // Event-wise initialization
196  //
197 
198  // Infrastructure piece
199  std::unique_ptr<std::vector<optdata::ChannelDataGroup>> StoragePtr(
200  new std::vector<optdata::ChannelDataGroup>);
201 
202  // Read in the Sim Photons
203  sim::SimPhotonsCollection ThePhotCollection =
205 
206  // Convert units into ns from us/MHz
207  double timeBegin_ns = fTimeBegin * 1000;
208  double timeEnd_ns = fTimeEnd * 1000;
209  double sampleFreq_ns = fSampleFreq / 1000;
210 
211  // Compute # of timeslices to be stored in the output. This is defined by a user input (fcl file)
212  optdata::TimeSlice_t timeSliceWindow(fOpDigiProperties->GetTimeSlice(timeEnd_ns));
213 
214  /*
215  Create output data product, optdata::ChannelDataGroup for each gain channel.
216  Note : Although the frame + sample number in DATA should have a reference of T=0 @ DAQ start time, this is
217  not handled in MC. Hence we do not set them here (use constructor default)
218  */
219  optdata::ChannelDataGroup rawWFGroup_HighGain(optdata::kHighGain);
220  optdata::ChannelDataGroup rawWFGroup_LowGain(optdata::kLowGain);
221  // Reserve entries equal to # of channels
222  rawWFGroup_HighGain.reserve(fGeom->NOpChannels());
223  rawWFGroup_LowGain.reserve(fGeom->NOpChannels());
224 
225  /*
226  Define "raw" waveform container which will be filled based on G4 photon timing + SPE waveform information.
227  Note this is not completely an analog waveform because it is digitized in terms of time (as it is using std::vector).
228  */
229  std::vector<std::vector<double>> rawWF_HighGain(fGeom->NOpChannels(),
230  std::vector<double>(timeSliceWindow, 0.0));
231  std::vector<std::vector<double>> rawWF_LowGain(fGeom->NOpChannels(),
232  std::vector<double>(timeSliceWindow, 0.0));
233 
234  /*
235  Start data processing ... see following steps
236  (1) Loop over input array of optical photons & fill "raw" waveform container w/ corresponding SPE waveform
237  (2) Loop over filled "raw" waveform and process (digitization, adding noise, baseline spread, etc)
238  */
239 
240  //
241  // Step (1) ... loop over G4 optical photons
242  //
243 
244  // For every OpDet, convert PE into waveform and combine all together
245  for (sim::SimPhotonsCollection::const_iterator itOpDet = ThePhotCollection.begin();
246  itOpDet != ThePhotCollection.end();
247  itOpDet++) {
248  const sim::SimPhotons& ThePhot = itOpDet->second;
249 
250  int ch = ThePhot.OpChannel();
251  // For every photon in the hit:
252  for (const sim::OnePhoton& Phot : ThePhot) {
253  // Sample a random subset according to QE
254  if (fFlatRandom.fire(1.0) <= fQE) {
255  optdata::TimeSlice_t PhotonTime(fOpDigiProperties->GetTimeSlice(Phot.Time));
256  if (Phot.Time > timeBegin_ns && Phot.Time < timeEnd_ns) {
257  if (fSimGainSpread) {
258  AddWaveform(
259  PhotonTime, rawWF_HighGain[ch], fSinglePEWaveform, fOpDigiProperties->HighGain(ch));
260  AddWaveform(
261  PhotonTime, rawWF_LowGain[ch], fSinglePEWaveform, fOpDigiProperties->LowGain(ch));
262  }
263  else {
264  AddWaveform(PhotonTime,
265  rawWF_HighGain[ch],
267  fOpDigiProperties->HighGainMean(ch));
268  AddWaveform(PhotonTime,
269  rawWF_LowGain[ch],
271  fOpDigiProperties->LowGainMean(ch));
272  }
273  }
274  } // random QE cut
275  } // for each Photon in SimPhotons
276  }
277 
278  //
279  // Loop over "raw" waveform (channel-wise)
280  //
281  for (unsigned short iCh = 0; iCh < rawWF_LowGain.size(); ++iCh) {
282  rawWF_LowGain[iCh].resize((timeEnd_ns - timeBegin_ns) * sampleFreq_ns);
283  rawWF_HighGain[iCh].resize((timeEnd_ns - timeBegin_ns) * sampleFreq_ns);
284 
285  // Add dark noise
286  if (fSimGainSpread) {
287  AddDarkNoise(rawWF_LowGain[iCh], fOpDigiProperties->LowGain(iCh));
288  AddDarkNoise(rawWF_HighGain[iCh], fOpDigiProperties->HighGain(iCh));
289  }
290  else {
291  AddDarkNoise(rawWF_LowGain[iCh], fOpDigiProperties->LowGainMean(iCh));
292  AddDarkNoise(rawWF_HighGain[iCh], fOpDigiProperties->HighGainMean(iCh));
293  }
294 
295  // Apply digitization and make channel data
296  optdata::ChannelData chData_HighGain(ApplyDigitization(rawWF_HighGain[iCh], iCh));
297  optdata::ChannelData chData_LowGain(ApplyDigitization(rawWF_LowGain[iCh], iCh));
298 
299  rawWFGroup_HighGain.push_back(chData_HighGain);
300  rawWFGroup_LowGain.push_back(chData_LowGain);
301  } // for each OpDet in SimPhotonsCollection
302 
303  StoragePtr->push_back(rawWFGroup_HighGain);
304  StoragePtr->push_back(rawWFGroup_LowGain);
305 
306  evt.put(std::move(StoragePtr));
307  }
308 }
art::ServiceHandle< geo::Geometry const > fGeom
optdata::ChannelData ApplyDigitization(std::vector< double > const RawWF, optdata::Channel_t const ch) const
base_engine_t & createEngine(seed_t seed)
optdata::ADC_Count_t fSaturationScale
int OpChannel() const
Returns the optical channel number this object is associated to.
Definition: SimPhotons.h:239
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.cc:6
All information of a photon entering the sensitive optical detector volume.
Definition: SimPhotons.h:60
OptDetDigitizer(const fhicl::ParameterSet &)
unsigned int NOpChannels() const
Number of electronics channels for all the optical detectors.
void produce(art::Event &) override
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Event.h:77
uint16_t ADC_Count_t
Definition: OpticalTypes.h:16
Simulation objects for optical detectors.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
optdata::ADC_Count_t fPedFlucAmp
An art service to assist in the distribution of guaranteed unique seeds to all engines within an art ...
void AddWaveform(optdata::TimeSlice_t time, std::vector< double > &OldPulse, std::vector< double > &NewPulse, double factor, bool extend=false)
CLHEP::RandPoisson fPoissonRandom
art::ServiceHandle< OpDigiProperties > fOpDigiProperties
Collection of photons which recorded on one channel.
Definition: SimPhotons.h:127
list_type::const_iterator const_iterator
Definition: SimPhotons.h:191
unsigned int TimeSlice_t
Definition: OpticalTypes.h:20
std::vector< double > fSinglePEWaveform
std::vector< optdata::ADC_Count_t > fPedMeanArray
TCEvent evt
Definition: DataStructs.cxx:8
unsigned int Channel_t
Definition: OpticalTypes.h:19
CLHEP::HepRandomEngine & fEngine
Collection of sim::SimPhotons, indexed by channel number.
Definition: SimPhotons.h:178
void AddDarkNoise(std::vector< double > &RawWF, double gain)
static sim::SimPhotonsCollection GetSimPhotonsCollection(const art::Event &evt, std::string moduleLabel)
art framework interface to geometry description