LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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
18 
19 // ART includes
23 #include "fhiclcpp/ParameterSet.h"
29 
30 // art extensions
32 
33 // CLHEP includes
34 #include "CLHEP/Random/RandFlat.h"
35 #include "CLHEP/Random/RandGaussQ.h"
36 #include "CLHEP/Random/RandPoisson.h"
37 
38 // C++ language includes
39 #include <iostream>
40 #include <sstream>
41 #include <cstring>
42 #include <vector>
43 
44 namespace opdet {
45 
47  public:
48 
50  virtual ~OptDetDigitizer();
51 
52  void produce(art::Event&);
53 
54  void beginJob();
55 
56  private:
57 
58  // The parameters we'll read from the .fcl file.
59  std::string fInputModule; // Input tag for OpDet collection
60  float fSampleFreq; // in MHz
61  float fTimeBegin; // in us
62  float fTimeEnd; // in us
63  float fQE; // quantum efficiency of opdet
64  optdata::ADC_Count_t fSaturationScale; // adc count w/ saturation occurs
65  std::vector<optdata::ADC_Count_t> fPedMeanArray; // Array of pedestal baseline (per ch)
66  float fDarkRate; // Noise rate in Hz
67  optdata::ADC_Count_t fPedFlucAmp; // Pedestal fluctuation amplitude
68  float fPedFlucRate; // Pedestal fluctuation rate
69  // float fWFRandTimeOffsetLow; // The lower bound of WF's T=0 offset from Trigger
70  // float fWFRandTimeOffsetHigh; // The upper bound of WF's T=0 offset from Trigger
71  std::vector<double> fSinglePEWaveform;
72 
74 
75  CLHEP::RandFlat * fFlatRandom;
76  CLHEP::RandPoisson * fPoissonRandom;
77  void AddDarkNoise (std::vector<double> &RawWF,double gain);
79  std::vector<double>& OldPulse,
80  std::vector<double>& NewPulse,
81  double factor,
82  bool extend=false);
83  optdata::ChannelData ApplyDigitization (std::vector<double> const RawWF,
84  optdata::Channel_t const ch) const;
87 
88  };
89 } // namespace opdet
90 
91 namespace opdet{
92 
94 
95 } //end namespace opdet
96 
97 namespace opdet {
98 
100  {
101  // Infrastructure piece
102  produces<std::vector< optdata::ChannelDataGroup> >();
103 
105  // Input Module and histogram parameters come from .fcl
106  fInputModule = pset.get<std::string>("InputModule");
107  fSimGainSpread = pset.get<bool >("SimGainSpread");
108  fTimeBegin = fOpDigiProperties->TimeBegin();
109  fTimeEnd = fOpDigiProperties->TimeEnd();
110  fSampleFreq = fOpDigiProperties->SampleFreq();
111  fQE = fOpDigiProperties->QE();
112  fDarkRate = fOpDigiProperties->DarkRate();
113  fPedFlucAmp = fOpDigiProperties->PedFlucAmp();
114  fPedFlucRate= fOpDigiProperties->PedFlucRate();
115  fSaturationScale = fOpDigiProperties->SaturationScale();
116  fPedMeanArray = fOpDigiProperties->PedMeanArray();
117 
118  // Initialize toy waveform vector fSinglePEWaveform
119  fSinglePEWaveform = fOpDigiProperties->SinglePEWaveform();
120 
121  // create a default random engine; obtain the random seed from NuRandomService,
122  // unless overridden in configuration with key "Seed"
124  ->createEngine(*this, pset, "Seed");
125 
126  // Sample a random fraction of detected photons
128  CLHEP::HepRandomEngine &engine = rng->getEngine();
129  fFlatRandom = new CLHEP::RandFlat(engine);
130  fPoissonRandom = new CLHEP::RandPoisson(rng->getEngine());
131 
132  }
133 
134  //-------------------------------------------------
135 
136 
138  {
139  }
140 
141 
142  //-------------------------------------------------
143 
145  {
146  }
147 
148 
149  //-------------------------------------------------
150 
151 
153  std::vector<double> &OldPulse,
154  std::vector<double> &NewPulse,
155  double factor,
156  bool extend)
157  {
158  if( (time+NewPulse.size()) > OldPulse.size() && extend )
159  OldPulse.resize(time + NewPulse.size());
160  for(size_t i = 0; i<NewPulse.size() && (time+i)<OldPulse.size(); ++i)
161  OldPulse[time+i] += NewPulse[i] * factor;
162  }
163 
164  //-------------------------------------------------
165 
166  void OptDetDigitizer::AddDarkNoise(std::vector<double> &RawWF, double gain){
167  // Add dark noise
168  double MeanDarkPulses = fDarkRate * (fTimeEnd-fTimeBegin) / 1000000;
169 
170  unsigned int NumberOfPulses = fPoissonRandom->fire(MeanDarkPulses);
171  for(size_t i=0; i!=NumberOfPulses; ++i)
172  {
173  double PulseTime_ns = fTimeBegin*1000 + (fTimeEnd-fTimeBegin)*1000*(fFlatRandom->fire(1.0)); // Should be in ns
174  optdata::TimeSlice_t PulseTime_ts = fOpDigiProperties->GetTimeSlice(PulseTime_ns);
175  AddWaveform( PulseTime_ts,
176  RawWF,
178  gain);
179  }
180 
181  }
182 
184  optdata::Channel_t const ch ) const
185  {
186  //
187  // Digitization includes...
188  // (a) amplitude digitization
189  // (b) saturation
190  // (c) pedestal fluctuation
191  //
192 
193  // prepare return data container
194  optdata::ChannelData chData(ch);
195  chData.reserve(rawWF.size());
196  optdata::ADC_Count_t baseMean(fPedMeanArray.at(ch));
197  for(optdata::TimeSlice_t time=0; time<rawWF.size(); ++time)
198  {
199  double thisSample = rawWF[time];
200 
201  optdata::ADC_Count_t thisCount = (optdata::ADC_Count_t)(thisSample)+baseMean;
202 
203  // (a) amplitude digitization
204  if(CLHEP::RandFlat::shoot(1.0) < (thisSample - int(thisSample)))
205  thisCount+=1;
206 
207  // (b) saturation
208  if(thisCount > fSaturationScale) thisCount = fSaturationScale;
209 
210  chData.push_back(thisCount);
211  }
212 
213  // (c) pedestal fluctuation
214  double timeSpan = chData.size() * 1.e-6/(fOpDigiProperties->SampleFreq());
215  unsigned int nFluc = CLHEP::RandPoisson::shoot(fPedFlucRate * timeSpan);
216  for(size_t i=0; i<nFluc; ++i)
217  {
218  optdata::TimeSlice_t pulseTime(CLHEP::RandFlat::shoot(0.0,(double)(chData.size())));
219  optdata::ADC_Count_t amp = chData[pulseTime];
220  if( CLHEP::RandFlat::shoot(0.,1.) > 0.5)
221  {
222  amp += fPedFlucAmp;
223  if(amp > fSaturationScale) amp=fSaturationScale;
224  }
225  else
226  amp -= fPedFlucAmp;
227  chData[pulseTime] = amp;
228  }
229 
230  return chData;
231  }
232 
233  //-------------------------------------------------
234 
236  {
237 
238  //
239  // Event-wise initialization
240  //
241 
242  // Infrastructure piece
243  std::unique_ptr< std::vector<optdata::ChannelDataGroup > > StoragePtr (new std::vector<optdata::ChannelDataGroup>);
244 
245  // Read in the Sim Photons
247 
248  // Convert units into ns from us/MHz
249  double timeBegin_ns = fTimeBegin * 1000;
250  double timeEnd_ns = fTimeEnd * 1000;
251  double sampleFreq_ns = fSampleFreq / 1000;
252 
253  // Compute # of timeslices to be stored in the output. This is defined by a user input (fcl file)
254  optdata::TimeSlice_t timeSliceWindow(fOpDigiProperties->GetTimeSlice(timeEnd_ns));
255 
256  /*
257  Create output data product, optdata::ChannelDataGroup for each gain channel.
258  Note : Although the frame + sample number in DATA should have a reference of T=0 @ DAQ start time, this is
259  not handled in MC. Hence we do not set them here (use constructor default)
260  */
261  optdata::ChannelDataGroup rawWFGroup_HighGain(optdata::kHighGain);
262  optdata::ChannelDataGroup rawWFGroup_LowGain(optdata::kLowGain);
263  // Reserve entries equal to # of channels
264  rawWFGroup_HighGain.reserve(fGeom->NOpChannels());
265  rawWFGroup_LowGain.reserve(fGeom->NOpChannels());
266 
267  /*
268  Define "raw" waveform container which will be filled based on G4 photon timing + SPE waveform information.
269  Note this is not completely an analog waveform because it is digitized in terms of time (as it is using std::vector).
270  */
271  std::vector<std::vector<double> > rawWF_HighGain(fGeom->NOpChannels(),std::vector<double>(timeSliceWindow,0.0));
272  std::vector<std::vector<double> > rawWF_LowGain(fGeom->NOpChannels(),std::vector<double>(timeSliceWindow,0.0));
273 
274  /*
275  Start data processing ... see following steps
276  (1) Loop over input array of optical photons & fill "raw" waveform container w/ corresponding SPE waveform
277  (2) Loop over filled "raw" waveform and process (digitization, adding noise, baseline spread, etc)
278  */
279 
280  //
281  // Step (1) ... loop over G4 optical photons
282  //
283 
284  // For every OpDet, convert PE into waveform and combine all together
285  for(sim::SimPhotonsCollection::const_iterator itOpDet=ThePhotCollection.begin(); itOpDet!=ThePhotCollection.end(); itOpDet++)
286  {
287  const sim::SimPhotons& ThePhot=itOpDet->second;
288 
289  int ch = ThePhot.OpChannel();
290  // For every photon in the hit:
291  for(const sim::OnePhoton& Phot: ThePhot)
292  {
293  // Sample a random subset according to QE
294  if(fFlatRandom->fire(1.0)<=fQE)
295  {
296  optdata::TimeSlice_t PhotonTime(fOpDigiProperties->GetTimeSlice(Phot.Time));
297  if( Phot.Time > timeBegin_ns && Phot.Time < timeEnd_ns )
298  {
299  if(fSimGainSpread)
300  {
301  AddWaveform( PhotonTime, rawWF_HighGain[ch], fSinglePEWaveform, fOpDigiProperties->HighGain(ch));
302  AddWaveform( PhotonTime, rawWF_LowGain[ch], fSinglePEWaveform, fOpDigiProperties->LowGain(ch));
303  }
304  else
305  {
306  AddWaveform( PhotonTime, rawWF_HighGain[ch], fSinglePEWaveform, fOpDigiProperties->HighGainMean(ch));
307  AddWaveform( PhotonTime, rawWF_LowGain[ch], fSinglePEWaveform, fOpDigiProperties->LowGainMean(ch));
308  }
309  }
310  } // random QE cut
311  } // for each Photon in SimPhotons
312  }
313 
314  //
315  // Loop over "raw" waveform (channel-wise)
316  //
317  for(unsigned short iCh = 0; iCh < rawWF_LowGain.size(); ++iCh){
318  rawWF_LowGain[iCh].resize((timeEnd_ns - timeBegin_ns) * sampleFreq_ns);
319  rawWF_HighGain[iCh].resize((timeEnd_ns - timeBegin_ns) * sampleFreq_ns);
320 
321  // Add dark noise
322  if(fSimGainSpread){
323  AddDarkNoise(rawWF_LowGain[iCh],fOpDigiProperties->LowGain(iCh));
324  AddDarkNoise(rawWF_HighGain[iCh],fOpDigiProperties->HighGain(iCh));
325  }else{
326  AddDarkNoise(rawWF_LowGain[iCh],fOpDigiProperties->LowGainMean(iCh));
327  AddDarkNoise(rawWF_HighGain[iCh],fOpDigiProperties->HighGainMean(iCh));
328  }
329 
330  // Apply digitization and make channel data
331  optdata::ChannelData chData_HighGain(ApplyDigitization(rawWF_HighGain[iCh],iCh));
332  optdata::ChannelData chData_LowGain(ApplyDigitization(rawWF_LowGain[iCh],iCh));
333 
334  rawWFGroup_HighGain.push_back(chData_HighGain);
335  rawWFGroup_LowGain.push_back(chData_LowGain);
336  } // for each OpDet in SimPhotonsCollection
337 
338  StoragePtr->push_back(rawWFGroup_HighGain);
339  StoragePtr->push_back(rawWFGroup_LowGain);
340 
341  evt.put(std::move(StoragePtr));
342  }
343 }
344 
optdata::ChannelData ApplyDigitization(std::vector< double > const RawWF, optdata::Channel_t const ch) const
optdata::ADC_Count_t fSaturationScale
int OpChannel() const
Definition: SimPhotons.h:161
OptDetDigitizer(const fhicl::ParameterSet &)
art::ServiceHandle< geo::Geometry > fGeom
unsigned int NOpChannels() const
Number of electronics channels for all the optical detectors.
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
uint16_t ADC_Count_t
Definition: OpticalTypes.h:16
base_engine_t & getEngine() const
contains objects relating to OpDet hits
base_engine_t & createEngine(seed_t seed)
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
T get(std::string const &key) const
Definition: ParameterSet.h:231
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)
art::ServiceHandle< OpDigiProperties > fOpDigiProperties
static art::ServiceHandle< art::RandomNumberGenerator > & rng()
list_type::const_iterator const_iterator
Definition: SimPhotons.h:134
unsigned int TimeSlice_t
Definition: OpticalTypes.h:20
CLHEP::RandPoisson * fPoissonRandom
std::vector< double > fSinglePEWaveform
std::vector< optdata::ADC_Count_t > fPedMeanArray
TCEvent evt
Definition: DataStructs.cxx:5
unsigned int Channel_t
Definition: OpticalTypes.h:19
Tools and modules for checking out the basics of the Monte Carlo.
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