TPCReadoutSimStandardAlg.cxx
Go to the documentation of this file.
1 //
2 // TPCReadoutSimStandardAlg.cxx
3 //
4 // Created by Brian Rebel on 2/14/17.
5 //
6 
8 
11 #include "CoreUtils/ServiceUtil.h"
12 #include "RawDataProducts/raw.h"
13 
14 namespace gar {
15  namespace rosim{
16 
17  //----------------------------------------------------------------------------
18  TPCReadoutSimStandardAlg::TPCReadoutSimStandardAlg(CLHEP::HepRandomEngine & engine,
19  fhicl::ParameterSet const& pset)
20  : TPCReadoutSimAlg(engine, pset)
21  {
22  this->reconfigure(pset);
23 
24  fDetProp = gar::providerFrom<detinfo::DetectorPropertiesService>();
25 
26  fNoiseVec.clear();
27  std::vector<double> noisevecdbl(fNoiseVecSize,0);
28  if (fNoiseSpectrum == 0)
29  {
30  CLHEP::RandGauss GaussRand(fEngine);
31  //std::cout << "noise amplitude: " << fNoiseAmplitude << std::endl;
32  GaussRand.fireArray(fNoiseVecSize, &noisevecdbl[0],0.0,fNoiseAmplitude);
33  for (int i=0; i<fNoiseVecSize; ++i)
34  {
35  long inoise = lrint(noisevecdbl[i]);
36  if (inoise > 32767) inoise = 32767; // to be stored in a signed short.
37  if (inoise < -32767) inoise = -32767; // to be stored in a signed short.
38  fNoiseVec.push_back( (short) inoise );
39  //std::cout << "inoise: " << i << " " << inoise << std::endl;
40  }
41  }
42 
43 
44  return;
45  }
46 
47  //----------------------------------------------------------------------------
49  {
50  return;
51  }
52 
53  //----------------------------------------------------------------------------
55  std::vector<float> const& electrons,
56  bool &todrop)
57  {
58  // make a vector to store the adc information
59  auto numTicks = fDetProp->NumberTimeSamples();
60  std::vector< short> adcs(numTicks, 0);
61 
62 
63  // check that the size of the electrons vector is the same as the
64  // adc vector, they better be
65  if(adcs.size() != electrons.size()){
66  throw cet::exception("TPCReadoutSimStandardAlg")
67  << "The vectors for adc and electrons have different sizes, no idea "
68  << "how to handle that, bail";
69  }
70 
71  for(size_t e = 0; e < electrons.size(); ++e){
72  adcs[e] = this->ElectronsToADCs(electrons[e]);
73 // if(electrons[e] > 0)
74 // LOG_VERBATIM("TPCReadoutSim")
75 // << "ADC: "
76 // << adcs[e]
77 // << " electrons "
78 // << electrons[e]
79 // << " tdc "
80 // << e;
81  }
82 
83  // add noise to the adc vector if we want that
84  if(fAddNoise) this->AddNoiseToADCs(adcs);
85 
86  // check for zero suppression
87  int retblocks = 1;
89  if (fCompressType == 1)
90  {
93  }
94 
95  todrop = (retblocks == 0);
96  return raw::RawDigit(channel, numTicks, adcs, cflag, 0);
97  }
98 
99  //----------------------------------------------------------------------------
101  {
102  fAddNoise = pset.get<bool>("AddNoise", false);
103  fNoiseSpectrum = pset.get<int>("NoiseSpectrum", 0);
104  fNoiseVecSize = pset.get<int>("NoiseVecSize",500000);
105  fNoiseAmplitude = pset.get<float>("NoiseAmplitude", 0);
106  fCompressType = pset.get<int>("CompressType",0);
107  fZSThreshold = pset.get<int>("ZSThreshold",5);
108  fZSTicksBefore = pset.get<unsigned int>("ZSTicksBefore",5);
109  fZSTicksAfter = pset.get<unsigned int>("ZSTicksAfter",5);
110  fPedestal = pset.get<int>("Pedestal",0);
111  fADCSaturation = pset.get<int>("ADCSaturation",4095); // default to 12-bit ADC's
112  return;
113  }
114 
115  //----------------------------------------------------------------------------
116  void TPCReadoutSimStandardAlg::CreateNoiseDigits(std::vector<raw::RawDigit> & digits)
117  {
118  if(!fAddNoise) return;
119 
120  // we'll need the geometry
121  auto geo = gar::providerFrom<geo::GeometryGAr>();
122 
123  // figure out which channels we need to make pure noise
124  std::set<unsigned int> channels;
125  for(auto const& dig : digits) channels.insert(dig.Channel());
126 
127  // now loop over the channels in the detector and make a noise digit
128  // for any channel that does not already have a rawdigit
129  for(unsigned int c = 0; c < geo->NChannels(); ++c){
130 
131  // do nothing if we already have a digit for this channel
132  if(channels.count(c) > 0) continue;
133 
134  //std::cout << "Making noise digit for channel: " << c << std::endl;
135  //std::cout << "ZS Threshold: " << fZSThreshold << std::endl;
136  std::vector<short> adcs(fDetProp->NumberTimeSamples(), 0);
137  this->AddNoiseToADCs(adcs);
138  // check for zero suppression
140  int retblocks = 1;
141  if (fCompressType == 1)
142  {
145  }
146  //std::cout << "retblocks: " << retblocks << std::endl;
147  auto numTicks = fDetProp->NumberTimeSamples();
148  if (retblocks) digits.emplace_back(c, numTicks, adcs, cflag, 0);
149 
150  } // end make noise digits
151 
152 
153  return;
154  }
155 
156  // TODO: Figure out how to parameterize noise that's correlated between channels
157  // This method just works one channel at a time.
158  // TODO: Include more noise models
159  //----------------------------------------------------------------------------
160  void TPCReadoutSimStandardAlg::AddNoiseToADCs(std::vector<short> & adcs)
161  {
162  // start sampling the pregenerated noise vector from a random spot
163  CLHEP::RandFlat FlatRand(fEngine);
164  double xnvi = ((double) fNoiseVec.size() - 1) * FlatRand.fire();
165  if (xnvi < 0) xnvi = 0; // just to be extra sure
166  size_t i = (size_t ) xnvi;
167  if (i >= fNoiseVec.size())
168  {
169  i = fNoiseVec.size() - 1;
170  }
171  //std::cout << "In AddNoiseToADCs: " << i << " " << fNoiseVec.size() << " " << adcs.size() << std::endl;
172  for (size_t j=0; j<adcs.size(); ++j)
173  {
174  adcs[j] += fNoiseVec[i]; // we check the bounds of i already
175  ++i;
176  if (i>=fNoiseVec.size()) i=0;
177  if (adcs[j]>fADCSaturation) adcs[j] = fADCSaturation;
178  }
179 
180  return;
181  }
182 
183  //----------------------------------------------------------------------------
185  {
186  //LOG_DEBUG("TPCReadoutSimStandard")
187  //<< "Electrons to ADC: "
188  //<< fDetProp->ElectronsToADC()
189  //<< " electrons: "
190  //<< electrons
191  //<< " product: "
192  //<< fDetProp->ElectronsToADC() * electrons;
193 
194  int tmpadc = (int) (fDetProp->ElectronsToADC() * electrons);
195  if (tmpadc > fADCSaturation) tmpadc = fADCSaturation;
196  return (short)(tmpadc);
197  }
198 
199  } // rosim
200 } // gar
void AddNoiseToADCs(std::vector< short > &adcs)
const detinfo::DetectorProperties * fDetProp
detector properties
float fNoiseAmplitude
noise amplitdue
enum gar::raw::_compress Compress_t
uint8_t channel
Definition: CRTFragment.hh:201
unsigned int fZSTicksBefore
for ZS Compression, # samples before
TPCReadoutSimStandardAlg(CLHEP::HepRandomEngine &engine, fhicl::ParameterSet const &pset)
unsigned int fZSTicksAfter
for ZS Compression, # samples after
const double e
Zero Suppression algorithm.
Definition: RawTypes.h:18
T get(std::string const &key) const
Definition: ParameterSet.h:271
void CreateNoiseDigits(std::vector< raw::RawDigit > &digits)
virtual double ElectronsToADC() const =0
int fZSThreshold
for ZS Compression, threshold (upwards)
no compression
Definition: RawTypes.h:16
General GArSoft Utilities.
int fNoiseVecSize
how much noise to pre-generate
bool fAddNoise
flag to add noise or not
int fCompressType
Switch to compress raw digits.
int fNoiseSpectrum
0: Gaussian white noise; more to come
CLHEP::HepRandomEngine & fEngine
random number engine
int fADCSaturation
limit of the ADC
void reconfigure(fhicl::ParameterSet const &pset)
LArSoft geometry interface.
Definition: ChannelGeo.h:16
void Compress(gar::raw::ADCvector_t &adc, gar::raw::Compress_t compress)
In-place compression of raw data buffer.
Definition: raw.cxx:23
raw::RawDigit CreateRawDigit(unsigned int channel, std::vector< float > const &electrons, bool &todrop)
virtual unsigned int NumberTimeSamples() const =0
Collection of charge vs time digitized from a single readout channel.
Definition: RawDigit.h:67
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
int fPedestal
Raw Digit Pedestal.