11 #include "nurandom/RandomUtils/NuRandomService.h" 12 #include "art_root_io/TFileService.h" 13 #include "CLHEP/Random/JamesRandom.h" 14 #include "CLHEP/Random/RandFlat.h" 15 #include "CLHEP/Random/RandGauss.h" 25 using std::ostringstream;
26 using rndm::NuRandomService;
27 using CLHEP::HepJamesRandom;
33 : fRandomSeed(0), fLogLevel(1),
34 fGausNoiseHistZ(nullptr), fGausNoiseHistU(nullptr), fGausNoiseHistV(nullptr),
35 fGausNoiseChanHist(nullptr),
36 fMicroBooNoiseHistZ(nullptr), fMicroBooNoiseHistU(nullptr), fMicroBooNoiseHistV(nullptr),
37 fMicroBooNoiseChanHist(nullptr),
38 fCohNoiseHist(nullptr), fCohNoiseChanHist(nullptr),
40 const string myname =
"SPhaseChannelNoiseService::ctor: ";
61 fENOB = pset.
get<
double>(
"EffectiveNBits");
81 fMicroBooNoiseHistZ = tfs->make<TH1F>(
"MicroBoo znoise",
";Z Noise [ADC counts];", 1000, -10., 10.);
82 fMicroBooNoiseHistU = tfs->make<TH1F>(
"MicroBoo unoise",
";U Noise [ADC counts];", 1000, -10., 10.);
83 fMicroBooNoiseHistV = tfs->make<TH1F>(
"MicroBoo vnoise",
";V Noise [ADC counts];", 1000, -10., 10.);
85 fGausNoiseHistZ = tfs->make<TH1F>(
"Gaussian znoise",
";Z Noise [ADC counts];", 1000, -10., 10.);
86 fGausNoiseHistU = tfs->make<TH1F>(
"Gaussian unoise",
";U Noise [ADC counts];", 1000, -10., 10.);
87 fGausNoiseHistV = tfs->make<TH1F>(
"Gaussian vnoise",
";V Noise [ADC counts];", 1000, -10., 10.);
89 fCohNoiseHist = tfs->make<TH1F>(
"Cohnoise",
";Coherent Noise [ADC counts];", 1000, -10., 10.);
91 string rname =
"SPhaseChannelNoiseService";
93 if (
fLogLevel > 0 ) cout << myname <<
"WARNING: Using hardwired seed." <<
endl;
94 m_pran =
new HepJamesRandom(seed);
96 if (
fLogLevel > 0 ) cout << myname <<
"Using NuRandomService." <<
endl;
98 m_pran =
new HepJamesRandom;
100 seedSvc->registerEngine(NuRandomService::CLHEPengineSeeder(
m_pran), rname);
117 const string myname =
"SPhaseChannelNoiseService::dtor: ";
119 cout << myname <<
"Deleting random engine with seed " <<
m_pran->getSeed() <<
endl;
140 unsigned int cohNoisechan = -999;
141 unsigned int groupNum = -999;
151 for (
unsigned int itck=0; itck<sigs.size(); ++itck ) {
171 sigs[itck] += tnoise;
179 out << prefix <<
"SPhaseChannelNoiseService: " <<
endl;
191 out << prefix <<
" GausNormU: [ ";
194 out << prefix <<
" GausMeanU: [ ";
197 out << prefix <<
" GausSigmaU: [ ";
200 out << prefix <<
" GausNormV: [ ";
203 out << prefix <<
" GausMeanV: [ ";
206 out << prefix <<
" GausSigmaV: [ ";
209 out << prefix <<
" GausNormZ: [ ";
212 out << prefix <<
" GausMeanZ: [ ";
215 out << prefix <<
" GausSigmaZ: [ ";
220 out << prefix <<
" EffectiveNBits: " <<
fENOB <<
endl;
228 out << prefix <<
"MicroBoo model parameters: [ ";
232 out << prefix <<
" CohGausNorm: [ ";
235 out << prefix <<
" CohGausMean: [ ";
238 out << prefix <<
" CohGausSigma: [ ";
242 out << prefix <<
" Actual random seed: " <<
m_pran->getSeed();
250 float wirelength,
float ENOB,
252 const string myname =
"SPhaseChannelNoiseService::generateCoherentNoise: ";
254 cout << myname <<
"Generating noise." <<
endl;
256 cout << myname <<
" Seed: " <<
m_pran->getSeed() <<
endl;
263 unsigned int ntick = pfft->
FFTSize();
265 double binWidth = 1.0/(ntick*sampleRate*1.0e-6);
268 unsigned nbin = ntick/2 + 1;
269 std::vector<TComplex> noiseFrequency(nbin, 0.);
272 double rnd[3] = {0.};
277 double fitpar[9] = {0.};
278 double wldparams[2] = {0.};
283 TF1* _wld_f =
new TF1(
"_wld_f",
"[0] + [1]*x", 0.0, 1000);
285 TF1* _poisson =
new TF1(
"_poisson",
"[0]**(x) * exp(-[0]) / ROOT::Math::tgamma(x+1.)", 0, 30);
287 TF1* _pfn_f1 =
new TF1(
"_pfn_f1",
"([0]*1/(x/1000*[8]/2) + ([1]*exp(-0.5*(((x/1000*[8]/2)-[2])/[3])**2)*exp(-0.5*pow(x/1000*[8]/(2*[4]),[5])))*[6]) + [7]", 0.0, 0.5*ntick*binWidth);
292 wldparams[0] = 0.395;
293 wldparams[1] = 0.001304;
294 _wld_f->SetParameters(wldparams);
295 double wldValue = _wld_f->Eval(wirelength);
302 fitpar[6] = wldValue;
305 _pfn_f1->SetParameters(fitpar);
306 _poisson->SetParameters(params);
307 _pfn_f1->SetNpx(1000);
309 for (
unsigned int i=0; i<ntick/2+1; ++i ) {
311 double pfnf1val = _pfn_f1->Eval((i+0.5)*binWidth);
313 double randomizer = _poisson->GetRandom()/params[0];
314 pval = pfnf1val * randomizer;
316 flat.fireArray(2, rnd, 0, 1);
317 phase = rnd[1]*2.*TMath::Pi();
318 TComplex tc(pval*cos(phase),pval*sin(phase));
319 noiseFrequency[i] += tc;
323 noise.resize(ntick,0.0);
324 std::vector<double> tmpnoise(noise.size());
325 pfft->
DoInvFFT(noiseFrequency, tmpnoise);
326 noiseFrequency.clear();
327 for (
unsigned int itck=0; itck<noise.size(); ++itck ) {
328 noise[itck] = sqrt(ntick)*tmpnoise[itck];
330 for (
unsigned int itck=0; itck<noise.size(); ++itck ) {
331 aNoiseHist->Fill(noise[itck]);
340 std::vector<float> gausMean, std::vector<float> gausSigma,
341 TH1* aNoiseHist)
const {
342 const string myname =
"SPhaseChannelNoiseService::generateGaussianNoise: ";
344 cout << myname <<
"Generating Gaussian noise." <<
endl;
347 int a = gausNorm.size();
348 int b = gausMean.size();
349 int c = gausSigma.size();
350 int NGausians = a<b?a:
b;
351 NGausians = NGausians<c?NGausians:
c;
353 std::stringstream
name;
355 for(
int i=0;i<NGausians;i++) {
356 name<<
"["<<3*i<<
"]*exp(-0.5*pow((x-["<<3*i+1<<
"])/["<<3*i+2<<
"],2))+";
359 TF1 *funcGausNoise =
new TF1(
"funcGausInhNoise",name.str().c_str(), 0, 1200);
360 funcGausNoise->SetNpx(12000);
361 for(
int i=0;i<NGausians;i++) {
362 funcGausNoise->SetParameter(3*i, gausNorm.at(i));
363 funcGausNoise->SetParameter(3*i+1, gausMean.at(i));
364 funcGausNoise->SetParameter(3*i+2, gausSigma.at(i));
371 unsigned int ntick = pfft->
FFTSize();
374 unsigned nbin = ntick/2 + 1;
375 std::vector<TComplex> noiseFrequency(nbin, 0.);
378 double rnd[2] = {0.};
380 double binWidth = 1.0/(ntick*sampleRate*1.0e-6);
381 for (
unsigned int i=0; i<ntick/2+1; ++i ) {
383 pval = funcGausNoise->Eval((
double)i*binWidth);
385 flat.fireArray(2, rnd, 0, 1);
386 pval *= 0.9 + 0.2*rnd[0];
388 phase = rnd[1]*2.*TMath::Pi();
389 TComplex tc(pval*cos(phase),pval*sin(phase));
390 noiseFrequency[i] += tc;
394 noise.resize(ntick,0.0);
395 std::vector<double> tmpnoise(noise.size());
396 pfft->
DoInvFFT(noiseFrequency, tmpnoise);
397 noiseFrequency.clear();
399 for (
unsigned int itck=0; itck<noise.size(); ++itck ) {
400 noise[itck] = sqrt(ntick)*tmpnoise[itck];
402 for (
unsigned int itck=0; itck<noise.size(); ++itck ) {
403 aNoiseHist->Fill(noise[itck]);
407 delete funcGausNoise; funcGausNoise = 0;
414 std::vector<float> gausMean, std::vector<float> gausSigma,
415 float cohExpNorm,
float cohExpWidth,
float cohExpOffset,
416 TH1* aNoiseHist)
const {
417 const string myname =
"SPhaseChannelNoiseService::generateCoherentGaussianNoise: ";
419 cout << myname <<
"Generating Coherent Gaussian noise." <<
endl;
422 int a = gausNorm.size();
423 int b = gausMean.size();
424 int c = gausSigma.size();
425 int NGausians = a<b?a:
b;
426 NGausians = NGausians<c?NGausians:
c;
428 std::stringstream
name;
430 for(
int i=0;i<NGausians;i++) {
431 name<<
"["<<3*i<<
"]*exp(-0.5*pow((x-["<<3*i+1<<
"])/["<<3*i+2<<
"],2))+";
433 name<<
"["<<3*NGausians<<
"]*exp(-x/["<<3*NGausians+1<<
"]) + ["<<3*NGausians+2<<
"]";
434 TF1 *funcCohNoise =
new TF1(
"funcGausCohsNoise",name.str().c_str(), 0, 1200);
435 funcCohNoise->SetNpx(12000);
436 for(
int i=0;i<NGausians;i++) {
437 funcCohNoise->SetParameter(3*i, gausNorm.at(i));
438 funcCohNoise->SetParameter(3*i+1, gausMean.at(i));
439 funcCohNoise->SetParameter(3*i+2, gausSigma.at(i));
441 funcCohNoise->SetParameter(3*NGausians, cohExpNorm);
442 funcCohNoise->SetParameter(3*NGausians+1, cohExpWidth);
443 funcCohNoise->SetParameter(3*NGausians+2, cohExpOffset);
446 TF1* _poisson =
new TF1(
"_poisson",
"[0]**(x) * exp(-[0]) / ROOT::Math::tgamma(x+1.)", 0, 30);
450 _poisson->SetParameters(params);
455 unsigned int ntick = pfft->
FFTSize();
458 unsigned nbin = ntick/2 + 1;
459 std::vector<TComplex> noiseFrequency(nbin, 0.);
462 double rnd[2] = {0.};
464 double binWidth = 1.0/(ntick*sampleRate*1.0e-6);
465 for (
unsigned int i=0; i<ntick/2+1; ++i ) {
467 pval = funcCohNoise->Eval((
double)i*binWidth);
469 flat.fireArray(2, rnd, 0, 1);
470 pval *= 0.9 + 0.2*rnd[0];
476 phase = rnd[1]*2.*TMath::Pi();
477 TComplex tc(pval*cos(phase),pval*sin(phase));
478 noiseFrequency[i] += tc;
482 noise.resize(ntick,0.0);
483 std::vector<double> tmpnoise(noise.size());
484 pfft->
DoInvFFT(noiseFrequency, tmpnoise);
485 noiseFrequency.clear();
496 for (
unsigned int itck=0; itck<noise.size(); ++itck ) {
497 noise[itck] = sqrt(ntick)*tmpnoise[itck];
499 for (
unsigned int itck=0; itck<noise.size(); ++itck ) {
500 aNoiseHist->Fill(noise[itck]);
504 delete funcCohNoise; funcCohNoise = 0;
513 const unsigned int nchan = geo->
Nchannels();
515 unsigned int numberofgroups = 0;
516 if(nchan%nchpergroup == 0) numberofgroups = nchan/nchpergroup;
517 else numberofgroups = nchan/nchpergroup +1;
518 unsigned int cohGroupNo = 0;
519 for(
unsigned int chan=0; chan<nchan; chan++) {
520 cohGroupNo = chan/nchpergroup;
524 for(
unsigned int ng=0; ng<numberofgroups; ng++) {
std::vector< float > fGausNormV
noise scale factor for the gaussian component in coherent noise
bool fEnableGaussianNoise
std::vector< float > fGausSigmaV
sigma of the gaussian component in coherent noise
void generateNoise(detinfo::DetectorClocksData const &clockData)
AdcSignalVectorVector fMicroBooNoiseU
std::vector< float > fGausMeanV
mean of the gaussian component in coherent noise
std::vector< int > fGroupCoherentNoiseMap
assign each group a noise
enum geo::_plane_proj View_t
Enumerate the possible plane projections.
int fLogLevel
Log message level: 0=quiet, 1=init only, 2+=every event.
TH1 * fGausNoiseHistU
distribution of noise counts for U
TH1 * fGausNoiseHistZ
distribution of noise counts for Z
TH1 * fGausNoiseChanHist
distribution of accessed noise samples
std::ostream & print(std::ostream &out=std::cout, std::string prefix="") const
unsigned int fNoiseArrayPoints
number of points in randomly generated noise array
std::vector< unsigned int > fChannelGroupMap
assign each channel a group number
int addNoise(detinfo::DetectorClocksData const &, detinfo::DetectorPropertiesData const &, Channel chan, AdcSignalVector &sigs) const
unsigned int fCohNoiseArrayPoints
number of points in randomly generated noise array
AdcSignalVectorVector fMicroBooNoiseZ
float fCohExpOffset
Amplitude offset of the exponential background component in coherent noise.
std::vector< float > fGausMeanU
mean of the gaussian component in coherent noise
void generateGaussianNoise(detinfo::DetectorClocksData const &clockData, AdcSignalVector &noise, std::vector< float > gausNorm, std::vector< float > gausMean, std::vector< float > gausSigma, TH1 *aNoiseHist) const
bool fEnableCoherentNoise
float fCohExpNorm
noise scale factor for the exponential component component in coherent noise
float fWhiteNoiseV
Level (per freq bin) for white noise for V.
std::vector< float > fNoiseFunctionParameters
Parameters in the MicroBooNE noise model.
std::vector< float > fGausSigmaZ
sigma of the gaussian component in coherent noise
float fWhiteNoiseZ
Level (per freq bin) for white noise for Z.
void makeCoherentGroupsByOfflineChannel(unsigned int nchpergroup)
art framework interface to geometry description
unsigned int fExpNoiseArrayPoints
number of points in randomly generated noise array
void DoInvFFT(std::vector< TComplex > &input, std::vector< T > &output)
unsigned int Nchannels() const
Returns the number of TPC readout channels in the detector.
TH1 * fCohNoiseChanHist
distribution of accessed noise samples
CLHEP::HepRandomEngine * m_pran
TH1 * fMicroBooNoiseHistZ
distribution of noise counts for Z
std::vector< float > fGausNormU
noise scale factor for the gaussian component in coherent noise
std::vector< float > fCohGausMean
mean of the gaussian component in coherent noise
~SPhaseChannelNoiseService()
std::vector< float > fGausNormZ
noise scale factor for the gaussian component in coherent noise
AdcSignalVectorVector fGausNoiseV
unsigned int getGroupNumberFromOfflineChannel(unsigned int offlinechan) const
T gaus(T x, T amplitude, T mean, T sigma)
Tests GausssianFit object with a known input.
T get(std::string const &key) const
bool fEnableMicroBooNoise
enable MicroBooNE noise model
std::vector< float > fGausSigmaU
sigma of the gaussian component in coherent noise
std::vector< float > fGausMeanZ
mean of the gaussian component in coherent noise
SPhaseChannelNoiseService(fhicl::ParameterSet const &pset)
AdcSignalVectorVector fGausNoiseZ
View_t View(geo::PlaneID const &pid) const
Returns the view (wire orientation) on the channels of specified TPC plane.
float fCohExpWidth
width of the exponential component in coherent noise
unsigned int fNChannelsPerCoherentGroup
std::vector< float > fCohGausSigma
sigma of the gaussian component in coherent noise
AdcSignalVectorVector fGausNoiseU
AdcSignalVectorVector fCohNoise
noise on each channel for each time for all planes
TH1 * fMicroBooNoiseHistV
distribution of noise counts for V
unsigned int getCohNoiseChanFromGroup(unsigned int cohgroup) const
std::vector< float > fCohGausNorm
noise scale factor for the gaussian component in coherent noise
TH1 * fGausNoiseHistV
distribution of noise counts for V
Contains all timing reference information for the detector.
float fWhiteNoiseU
Level (per freq bin) for white noise for U.
std::optional< T > get_if_present(std::string const &key) const
void generateCoherentNoise(detinfo::DetectorClocksData const &clockData, AdcSignalVector &noise, std::vector< float > gausNorm, std::vector< float > gausMean, std::vector< float > gausSigma, float cohExpNorm, float cohExpWidth, float cohExpOffset, TH1 *aNoiseHist) const
int fRandomSeed
Seed for random number service. If absent or zero, use SeedSvc.
float fENOB
Effective number of bits.
std::vector< AdcSignal > AdcSignalVector
void generateMicroBooNoise(detinfo::DetectorClocksData const &clockData, float wirelength, float ENOB, AdcSignalVector &noise, TH1 *aNoiseHist) const
double sampling_rate(DetectorClocksData const &data)
Returns the period of the TPC readout electronics clock.
TH1 * fMicroBooNoiseHistU
distribution of noise counts for U
LArSoft geometry interface.
AdcSignalVectorVector fMicroBooNoiseV
QTextStream & endl(QTextStream &s)
TH1 * fCohNoiseHist
distribution of noise counts
TH1 * fMicroBooNoiseChanHist
distribution of accessed noise samples
#define DEFINE_ART_SERVICE_INTERFACE_IMPL(svc, iface)