All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NoiseFromHist_tool.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file NoiseFromHist.cc
3 /// \author F. Varanini
4 ////////////////////////////////////////////////////////////////////////
5 
6 #include <cmath>
7 #include "IGenNoise.h"
8 #include "art/Framework/Services/Registry/ServiceHandle.h"
9 #include "art/Persistency/Provenance/ModuleContext.h"
10 #include "art/Framework/Principal/Event.h"
11 #include "art/Framework/Services/Optional/RandomNumberGenerator.h"
12 #include "art/Utilities/ToolMacros.h"
13 #include "messagefacility/MessageLogger/MessageLogger.h"
14 #include "cetlib_except/exception.h"
15 
16 // art extensions
17 #include "nurandom/RandomUtils/NuRandomService.h"
20 
21 // FFT
22 #include "icarus_signal_processing/Filters/ICARUSFFT.h"
23 
24 // CLHEP libraries
25 #include "CLHEP/Random/RandFlat.h"
26 #include "CLHEP/Random/RandGaussQ.h"
27 
28 #include "TH1D.h"
29 #include "TFile.h"
30 
31 #include <fstream>
32 
33 namespace icarus_tool
34 {
35 
37 {
38 public:
39  explicit NoiseFromHist(const fhicl::ParameterSet& pset);
40 
42 
43  void configure(const fhicl::ParameterSet& pset) override;
44 
45  void nextEvent() override {return;};
46 
47  void generateNoise(CLHEP::HepRandomEngine&,
48  CLHEP::HepRandomEngine&,
51  double,
52  const geo::PlaneID&,
53  unsigned int) override;
54 
55 private:
56  // Member variables from the fhicl file
57  double fNoiseRand;
59  std::string fHistogramName;
60 
62 
63  // We'll recover the bin contents and store in a vector
64  // with the likely false hope this will be faster...
65  std::vector<double> fNoiseHistVec;
66 
67  std::unique_ptr<icarus_signal_processing::ICARUSFFT<double>> fFFT;
68 };
69 
70 //----------------------------------------------------------------------
71 // Constructor.
72 NoiseFromHist::NoiseFromHist(const fhicl::ParameterSet& pset)
73 {
74  configure(pset);
75 }
76 
78 {
79 }
80 
81 void NoiseFromHist::configure(const fhicl::ParameterSet& pset)
82 {
83  // Recover the histogram used for noise generation
84  fNoiseRand = pset.get< double>("NoiseRand");
85  fInputNoiseHistFileName = pset.get<std::string>("NoiseHistFileName");
86  fHistogramName = pset.get<std::string>("HistogramName");
87  fHistNormFactor = pset.get<double>("HistNormFactor");
88 
89  std::string fullFileName;
90  cet::search_path searchPath("FW_SEARCH_PATH");
91  searchPath.find_file(fInputNoiseHistFileName, fullFileName);
92 
93  TFile inputFile(fullFileName.c_str(), "READ");
94 
95  if (!inputFile.IsOpen())
96  throw cet::exception("NoiseFromHist::configure") << "Unable to open input file: " << fInputNoiseHistFileName << std::endl;
97 
98  TH1D* histPtr = (TH1D*)inputFile.Get(fHistogramName.c_str());
99 
100  if (!histPtr)
101  throw cet::exception("NoiseFromHist::configure") << "Unable to recover desired histogram: " << fHistogramName << std::endl;
102 
103  fNoiseHistVec.resize(histPtr->GetNbinsX(), 0.);
104 
105  for(size_t histIdx = 0; histIdx < size_t(histPtr->GetNbinsX()); histIdx++)
106  fNoiseHistVec[histIdx] = histPtr->GetBinContent(histIdx+1);
107 
108  // Close the input file
109  inputFile.Close();
110 
111  // Now set up our plans for doing the convolution
112  auto const clockData = art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataForJob();
113  int numberTimeSamples = clockData.NumberTimeSamples();
114 
115  fFFT = std::make_unique<icarus_signal_processing::ICARUSFFT<double>>(numberTimeSamples);
116 
117  return;
118 }
119 
120 void NoiseFromHist::generateNoise(CLHEP::HepRandomEngine& engine,
121  CLHEP::HepRandomEngine&,
122  icarusutil::TimeVec& noise,
124  double noise_factor,
125  const geo::PlaneID& planeID,
126  unsigned int board)
127 {
128  CLHEP::RandFlat flat(engine,-1,1);
129 
130  size_t nFFTTicks = detProp.NumberTimeSamples();
131 
132  if(noise.size() != nFFTTicks)
133  throw cet::exception("SimWireICARUS")
134  << "\033[93m"
135  << "Frequency noise vector length must match FFT Ticks (FFT size)"
136  << " ... " << noise.size() << " != " << nFFTTicks
137  << "\033[00m"
138  << std::endl;
139 
140  // noise in frequency space
141  std::vector<std::complex<double>> noiseFrequency(nFFTTicks/2+1, 0.);
142 
143  double pval = 0.;
144  double phase = 0.;
145  double rnd[2] = {0.};
146  double scaleFactor = fHistNormFactor * noise_factor;
147 
148  // width of frequencyBin in kHz
149 
150  for(size_t i=0; i< nFFTTicks/2 + 1; ++i)
151  {
152  // exponential noise spectrum
153  flat.fireArray(2,rnd,0,1);
154 
155  pval = fNoiseHistVec[i] * ((1-fNoiseRand) + 2 * fNoiseRand*rnd[0]) * scaleFactor;
156 
157  phase = rnd[1] * 2. * M_PI;
158 
159  std::complex<double> tc(pval*cos(phase),pval*sin(phase));
160 
161  noiseFrequency.at(i) += tc;
162  }
163 
164  // inverse FFT MCSignal
165  fFFT->inverseFFT(noiseFrequency, noise);
166 
167  noiseFrequency.clear();
168 
169  return;
170 }
171 
172 DEFINE_ART_CLASS_TOOL(NoiseFromHist)
173 }
Utilities related to art service access.
The data type to uniquely identify a Plane.
Definition: geo_types.h:472
void configure(const fhicl::ParameterSet &pset) override
std::vector< double > fNoiseHistVec
std::vector< SigProcPrecision > TimeVec
std::unique_ptr< icarus_signal_processing::ICARUSFFT< double > > fFFT
This is the interface class for a tool to handle a GenNoise for the overall response.
auto const detProp
void generateNoise(CLHEP::HepRandomEngine &, CLHEP::HepRandomEngine &, icarusutil::TimeVec &, detinfo::DetectorPropertiesData const &, double, const geo::PlaneID &, unsigned int) override
NoiseFromHist(const fhicl::ParameterSet &pset)