LArSoft  v10_04_05
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 
78 
79 namespace opdet {
80 
82  : EDProducer{pset}
84  pset,
85  "Seed"))
89  {
90  // Infrastructure piece
91  produces<std::vector<optdata::ChannelDataGroup>>();
92 
94  // Input Module and histogram parameters come from .fcl
95  fInputModule = pset.get<std::string>("InputModule");
96  fSimGainSpread = pset.get<bool>("SimGainSpread");
97  fTimeBegin = fOpDigiProperties->TimeBegin();
98  fTimeEnd = fOpDigiProperties->TimeEnd();
99  fSampleFreq = fOpDigiProperties->SampleFreq();
100  fQE = fOpDigiProperties->QE();
101  fDarkRate = fOpDigiProperties->DarkRate();
102  fPedFlucAmp = fOpDigiProperties->PedFlucAmp();
103  fPedFlucRate = fOpDigiProperties->PedFlucRate();
104  fSaturationScale = fOpDigiProperties->SaturationScale();
105  fPedMeanArray = fOpDigiProperties->PedMeanArray();
106 
107  fSinglePEWaveform = fOpDigiProperties->SinglePEWaveform();
108  }
109 
110  //-------------------------------------------------
111 
113  std::vector<double>& OldPulse,
114  std::vector<double>& NewPulse,
115  double const factor,
116  bool const extend)
117  {
118  if ((time + NewPulse.size()) > OldPulse.size() && extend)
119  OldPulse.resize(time + NewPulse.size());
120  for (size_t i = 0; i < NewPulse.size() && (time + i) < OldPulse.size(); ++i)
121  OldPulse[time + i] += NewPulse[i] * factor;
122  }
123 
124  //-------------------------------------------------
125 
126  void OptDetDigitizer::AddDarkNoise(std::vector<double>& RawWF, double gain)
127  {
128  // Add dark noise
129  double MeanDarkPulses = fDarkRate * (fTimeEnd - fTimeBegin) / 1000000;
130 
131  unsigned int NumberOfPulses = fPoissonRandom.fire(MeanDarkPulses);
132  for (size_t i = 0; i != NumberOfPulses; ++i) {
133  double PulseTime_ns = fTimeBegin * 1000 + (fTimeEnd - fTimeBegin) * 1000 *
134  (fFlatRandom.fire(1.0)); // Should be in ns
135  optdata::TimeSlice_t PulseTime_ts = fOpDigiProperties->GetTimeSlice(PulseTime_ns);
136  AddWaveform(PulseTime_ts, RawWF, fSinglePEWaveform, gain);
137  }
138  }
139 
141  optdata::Channel_t const ch) const
142  {
143  //
144  // Digitization includes...
145  // (a) amplitude digitization
146  // (b) saturation
147  // (c) pedestal fluctuation
148  //
149 
150  // prepare return data container
151  optdata::ChannelData chData(ch);
152  chData.reserve(rawWF.size());
153  optdata::ADC_Count_t baseMean(fPedMeanArray.at(ch));
154  for (optdata::TimeSlice_t time = 0; time < rawWF.size(); ++time) {
155  double thisSample = rawWF[time];
156 
157  optdata::ADC_Count_t thisCount = (optdata::ADC_Count_t)(thisSample) + baseMean;
158 
159  // (a) amplitude digitization
160  if (CLHEP::RandFlat::shoot(1.0) < (thisSample - int(thisSample))) thisCount += 1;
161 
162  // (b) saturation
163  if (thisCount > fSaturationScale) thisCount = fSaturationScale;
164 
165  chData.push_back(thisCount);
166  }
167 
168  // (c) pedestal fluctuation
169  double timeSpan = chData.size() * 1.e-6 / (fOpDigiProperties->SampleFreq());
170  unsigned int nFluc = CLHEP::RandPoisson::shoot(fPedFlucRate * timeSpan);
171  for (size_t i = 0; i < nFluc; ++i) {
172  optdata::TimeSlice_t pulseTime(CLHEP::RandFlat::shoot(0.0, (double)(chData.size())));
173  optdata::ADC_Count_t amp = chData[pulseTime];
174  if (CLHEP::RandFlat::shoot(0., 1.) > 0.5) {
175  amp += fPedFlucAmp;
176  if (amp > fSaturationScale) amp = fSaturationScale;
177  }
178  else
179  amp -= fPedFlucAmp;
180  chData[pulseTime] = amp;
181  }
182 
183  return chData;
184  }
185 
186  //-------------------------------------------------
187 
189  {
190  //
191  // Event-wise initialization
192  //
193 
194  // Infrastructure piece
195  std::unique_ptr<std::vector<optdata::ChannelDataGroup>> StoragePtr(
196  new std::vector<optdata::ChannelDataGroup>);
197 
198  // Read in the Sim Photons
199  sim::SimPhotonsCollection ThePhotCollection =
201 
202  // Convert units into ns from us/MHz
203  double timeBegin_ns = fTimeBegin * 1000;
204  double timeEnd_ns = fTimeEnd * 1000;
205  double sampleFreq_ns = fSampleFreq / 1000;
206 
207  // Compute # of timeslices to be stored in the output. This is defined by a user input (fcl file)
208  optdata::TimeSlice_t timeSliceWindow(fOpDigiProperties->GetTimeSlice(timeEnd_ns));
209 
210  /*
211  Create output data product, optdata::ChannelDataGroup for each gain channel.
212  Note : Although the frame + sample number in DATA should have a reference of T=0 @ DAQ start time, this is
213  not handled in MC. Hence we do not set them here (use constructor default)
214  */
215  optdata::ChannelDataGroup rawWFGroup_HighGain(optdata::kHighGain);
216  optdata::ChannelDataGroup rawWFGroup_LowGain(optdata::kLowGain);
217  // Reserve entries equal to # of channels
218  auto const nOpChannels = fWireReadoutGeom->NOpChannels();
219  rawWFGroup_HighGain.reserve(nOpChannels);
220  rawWFGroup_LowGain.reserve(nOpChannels);
221 
222  /*
223  Define "raw" waveform container which will be filled based on G4 photon timing + SPE waveform information.
224  Note this is not completely an analog waveform because it is digitized in terms of time (as it is using std::vector).
225  */
226  std::vector<std::vector<double>> rawWF_HighGain(nOpChannels,
227  std::vector<double>(timeSliceWindow, 0.0));
228  std::vector<std::vector<double>> rawWF_LowGain(nOpChannels,
229  std::vector<double>(timeSliceWindow, 0.0));
230 
231  /*
232  Start data processing ... see following steps
233  (1) Loop over input array of optical photons & fill "raw" waveform container w/ corresponding SPE waveform
234  (2) Loop over filled "raw" waveform and process (digitization, adding noise, baseline spread, etc)
235  */
236 
237  //
238  // Step (1) ... loop over G4 optical photons
239  //
240 
241  // For every OpDet, convert PE into waveform and combine all together
242  for (sim::SimPhotonsCollection::const_iterator itOpDet = ThePhotCollection.begin();
243  itOpDet != ThePhotCollection.end();
244  itOpDet++) {
245  const sim::SimPhotons& ThePhot = itOpDet->second;
246 
247  int ch = ThePhot.OpChannel();
248  // For every photon in the hit:
249  for (const sim::OnePhoton& Phot : ThePhot) {
250  // Sample a random subset according to QE
251  if (fFlatRandom.fire(1.0) <= fQE) {
252  optdata::TimeSlice_t PhotonTime(fOpDigiProperties->GetTimeSlice(Phot.Time));
253  if (Phot.Time > timeBegin_ns && Phot.Time < timeEnd_ns) {
254  if (fSimGainSpread) {
255  AddWaveform(
256  PhotonTime, rawWF_HighGain[ch], fSinglePEWaveform, fOpDigiProperties->HighGain(ch));
257  AddWaveform(
258  PhotonTime, rawWF_LowGain[ch], fSinglePEWaveform, fOpDigiProperties->LowGain(ch));
259  }
260  else {
261  AddWaveform(PhotonTime,
262  rawWF_HighGain[ch],
264  fOpDigiProperties->HighGainMean(ch));
265  AddWaveform(PhotonTime,
266  rawWF_LowGain[ch],
268  fOpDigiProperties->LowGainMean(ch));
269  }
270  }
271  } // random QE cut
272  } // for each Photon in SimPhotons
273  }
274 
275  //
276  // Loop over "raw" waveform (channel-wise)
277  //
278  for (unsigned short iCh = 0; iCh < rawWF_LowGain.size(); ++iCh) {
279  rawWF_LowGain[iCh].resize((timeEnd_ns - timeBegin_ns) * sampleFreq_ns);
280  rawWF_HighGain[iCh].resize((timeEnd_ns - timeBegin_ns) * sampleFreq_ns);
281 
282  // Add dark noise
283  if (fSimGainSpread) {
284  AddDarkNoise(rawWF_LowGain[iCh], fOpDigiProperties->LowGain(iCh));
285  AddDarkNoise(rawWF_HighGain[iCh], fOpDigiProperties->HighGain(iCh));
286  }
287  else {
288  AddDarkNoise(rawWF_LowGain[iCh], fOpDigiProperties->LowGainMean(iCh));
289  AddDarkNoise(rawWF_HighGain[iCh], fOpDigiProperties->HighGainMean(iCh));
290  }
291 
292  // Apply digitization and make channel data
293  optdata::ChannelData chData_HighGain(ApplyDigitization(rawWF_HighGain[iCh], iCh));
294  optdata::ChannelData chData_LowGain(ApplyDigitization(rawWF_LowGain[iCh], iCh));
295 
296  rawWFGroup_HighGain.push_back(chData_HighGain);
297  rawWFGroup_LowGain.push_back(chData_LowGain);
298  } // for each OpDet in SimPhotonsCollection
299 
300  StoragePtr->push_back(rawWFGroup_HighGain);
301  StoragePtr->push_back(rawWFGroup_LowGain);
302 
303  evt.put(std::move(StoragePtr));
304  }
305 }
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
cout<< "Opened file "<< fin<< " ixs= "<< ixs<< endl;if(ixs==0) hhh=(TH1F *) fff-> Get("h1")
Definition: AddMC.C:8
OptDetDigitizer(const fhicl::ParameterSet &)
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
Interface for a class providing readout channel mapping to geometry.
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)
geo::WireReadoutGeom const * fWireReadoutGeom
CLHEP::RandPoisson fPoissonRandom
art::ServiceHandle< OpDigiProperties > fOpDigiProperties
Collection of photons which recorded on one channel.
Definition: SimPhotons.h:127
virtual unsigned int NOpChannels(unsigned int NOpDets) const
Returns the number of optical channels contained in some detectors.
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)