8 #include "art/Framework/Services/Registry/ServiceDefinitionMacros.h"
14 using std::ostringstream;
15 using rndm::NuRandomService;
16 using CLHEP::HepJamesRandom;
19 constexpr
double kPoissonMean = 3.30762;
26 : fRandomSeed(0), fLogLevel(pset.
get<int>(
"LogLevel")),
27 fGausNoiseHistZ(nullptr), fGausNoiseHistU(nullptr), fGausNoiseHistV(nullptr),
28 fGausNoiseChanHist(nullptr),
29 fMicroBooNoiseHistZ(nullptr), fMicroBooNoiseHistU(nullptr), fMicroBooNoiseHistV(nullptr),
30 fMicroBooNoiseChanHist(nullptr),
31 fCohNoiseHist(nullptr), fCohNoiseChanHist(nullptr),
32 haveSeed(pset.get_if_present<int>(
"RandomSeed", fRandomSeed)),
33 m_pran(ConstructRandomEngine(haveSeed)),
34 fTRandom3(new TRandom3(m_pran->getSeed()))
45 fGausNormU = pset.get<std::vector<float>>(
"GausNormU");
46 fGausMeanU = pset.get<std::vector<float>>(
"GausMeanU");
47 fGausSigmaU = pset.get<std::vector<float>>(
"GausSigmaU");
48 fGausNormV = pset.get<std::vector<float>>(
"GausNormV");
49 fGausMeanV = pset.get<std::vector<float>>(
"GausMeanV");
50 fGausSigmaV = pset.get<std::vector<float>>(
"GausSigmaV");
51 fGausNormZ = pset.get<std::vector<float>>(
"GausNormZ");
52 fGausMeanZ = pset.get<std::vector<float>>(
"GausMeanZ");
53 fGausSigmaZ = pset.get<std::vector<float>>(
"GausSigmaZ");
56 fENOB = pset.get<
double>(
"EffectiveNBits");
70 fCohGausNorm = pset.get<std::vector<float>>(
"CohGausNorm");
71 fCohGausMean = pset.get<std::vector<float>>(
"CohGausMean");
75 art::ServiceHandle<art::TFileService>
tfs;
76 fMicroBooNoiseHistZ = tfs->make<TH1F>(
"MicroBoo znoise",
";Z Noise [ADC counts];", 1000, -10., 10.);
77 fMicroBooNoiseHistU = tfs->make<TH1F>(
"MicroBoo unoise",
";U Noise [ADC counts];", 1000, -10., 10.);
78 fMicroBooNoiseHistV = tfs->make<TH1F>(
"MicroBoo vnoise",
";V Noise [ADC counts];", 1000, -10., 10.);
80 fGausNoiseHistZ = tfs->make<TH1F>(
"Gaussian znoise",
";Z Noise [ADC counts];", 1000, -10., 10.);
81 fGausNoiseHistU = tfs->make<TH1F>(
"Gaussian unoise",
";U Noise [ADC counts];", 1000, -10., 10.);
82 fGausNoiseHistV = tfs->make<TH1F>(
"Gaussian vnoise",
";V Noise [ADC counts];", 1000, -10., 10.);
84 fCohNoiseHist = tfs->make<TH1F>(
"Cohnoise",
";Coherent Noise [ADC counts];", 1000, -10., 10.);
90 _wld_f =
new TF1(
"_wld_f",
"[0] + [1]*x", 0.0, 1000);
95 _poisson =
new TF1(
"_poisson",
"[0]**(x) * exp(-[0]) / ROOT::Math::tgamma(x+1.)", 0, 30);
96 _poisson->SetParameter(0, kPoissonMean);
111 const string myname =
"SBNDuBooNEDataDrivenNoiseService::dtor: ";
113 cout << myname <<
"Deleting random engine with seed " <<
m_pran->getSeed() << endl;
121 string myname =
"SBNDuBooNEDataDrivenNoiseService::ctor: ";
122 string rname =
"SBNDuBooNEDataDrivenNoiseService";
125 if (
fLogLevel > 0 )
cout << myname <<
"WARNING: Using hardwired seed." << endl;
128 if (
fLogLevel > 0 )
cout << myname <<
"Using NuRandomService." << endl;
129 art::ServiceHandle<NuRandomService> seedSvc;
130 m_pran =
new HepJamesRandom;
132 seedSvc->registerEngine(NuRandomService::CLHEPengineSeeder(
m_pran), rname);
140 TRandom* gRandomTemp = gRandom;
142 double randomVal(func->GetRandom());
143 gRandom = gRandomTemp;
150 CLHEP::RandFlat flat(*
m_pran);
161 unsigned int cohNoisechan = -999;
162 unsigned int groupNum = -999;
170 art::ServiceHandle<geo::Geometry> geo;
171 std::vector<geo::WireID> wireIDs = geo->ChannelToWire(chan);
172 unsigned int wireID = wireIDs.front().Wire;
173 unsigned int planeID = wireIDs.front().Plane;
176 double wirelength = wire.
Length();
181 wirelength = wirelength + jumperLength;
196 art::ServiceHandle<util::LArFFT> pfft;
197 unsigned int ntick = pfft->FFTSize();
199 double binWidth = 1.0/(ntick*sampleRate*1.0e-6);
202 unsigned nbin = ntick/2 + 1;
203 std::vector<TComplex> noiseFrequency(nbin, 0.);
206 double rnd[3] = {0.};
208 std::vector<double> noisevector(ntick,0.0);
209 double fitpar[9] = {0.};
212 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);
215 double wldValue =
_wld_f->Eval(wirelength);
223 fitpar[6] = wldValue;
227 _pfn_f1->SetParameters(fitpar);
228 _pfn_f1->SetNpx(1000);
230 for (
unsigned int i=0; i<ntick/2+1; ++i ) {
232 double pfnf1val = _pfn_f1->Eval((i+0.5)*binWidth);
235 double randomizer = randPoisson/kPoissonMean;
236 pval = pfnf1val * randomizer;
238 flat.fireArray(2, rnd, 0, 1);
239 phase = rnd[1]*2.*TMath::Pi();
240 TComplex tc(pval*cos(phase),pval*sin(phase));
241 noiseFrequency[i] += tc;
246 std::vector<double> tmpnoise(noisevector.size());
247 pfft->DoInvFFT(noiseFrequency, tmpnoise);
248 noiseFrequency.clear();
249 for (
unsigned int itck=0; itck<noisevector.size(); ++itck ) {
250 noisevector[itck] = sqrt(ntick)*tmpnoise[itck];
259 for (
unsigned int itck=0; itck<sigs.size(); ++itck ) {
279 sigs[itck] += tnoise;
287 out << prefix <<
"SBNDuBooNEDataDrivenNoiseService: " << endl;
289 out << prefix <<
" LogLevel: " <<
fLogLevel << endl;
290 out << prefix <<
" RandomSeed: " <<
fRandomSeed << endl;
294 out << prefix <<
" WhiteNoiseZ: " <<
fWhiteNoiseZ << endl;
295 out << prefix <<
" WhiteNoiseU: " <<
fWhiteNoiseU << endl;
296 out << prefix <<
" WhiteNoiseV: " <<
fWhiteNoiseV << endl;
299 out << prefix <<
" GausNormU: [ ";
302 out << prefix <<
" GausMeanU: [ ";
305 out << prefix <<
" GausSigmaU: [ ";
308 out << prefix <<
" GausNormV: [ ";
311 out << prefix <<
" GausMeanV: [ ";
314 out << prefix <<
" GausSigmaV: [ ";
317 out << prefix <<
" GausNormZ: [ ";
320 out << prefix <<
" GausMeanZ: [ ";
323 out << prefix <<
" GausSigmaZ: [ ";
328 out << prefix <<
" EffectiveNBits: " <<
fENOB << endl;
331 out << prefix <<
" ULastJumper: " <<
fULastJumper << endl;
333 out << prefix <<
" VLastJumper: " <<
fVLastJumper << endl;
335 out << prefix <<
"MicroBoo model parameters: [ ";
343 out << prefix <<
" CohGausNorm: [ ";
346 out << prefix <<
" CohGausMean: [ ";
349 out << prefix <<
" CohGausSigma: [ ";
353 out << prefix <<
" Actual random seed: " <<
m_pran->getSeed();
362 std::vector<float> gausMean, std::vector<float> gausSigma,
363 TH1* aNoiseHist)
const {
364 const string myname =
"SBNDuBooNEDataDrivenNoiseService::generateGaussianNoise: ";
366 cout << myname <<
"Generating Gaussian noise." << endl;
369 int a = gausNorm.size();
370 int b = gausMean.size();
371 int c = gausSigma.size();
372 int NGausians = a<b?a:b;
373 NGausians = NGausians<c?NGausians:c;
375 std::stringstream
name;
377 for(
int i=0;i<NGausians;i++) {
378 name<<
"["<<3*i<<
"]*exp(-0.5*pow((x-["<<3*i+1<<
"])/["<<3*i+2<<
"],2))+";
381 TF1 *funcGausNoise =
new TF1(
"funcGausInhNoise",name.str().c_str(), 0, 1200);
382 funcGausNoise->SetNpx(12000);
383 for(
int i=0;i<NGausians;i++) {
384 funcGausNoise->SetParameter(3*i, gausNorm.at(i));
385 funcGausNoise->SetParameter(3*i+1, gausMean.at(i));
386 funcGausNoise->SetParameter(3*i+2, gausSigma.at(i));
392 art::ServiceHandle<util::LArFFT> pfft;
393 unsigned int ntick = pfft->FFTSize();
394 CLHEP::RandFlat flat(*
m_pran);
396 unsigned nbin = ntick/2 + 1;
397 std::vector<TComplex> noiseFrequency(nbin, 0.);
400 double rnd[2] = {0.};
402 double binWidth = 1.0/(ntick*sampleRate*1.0e-6);
403 for (
unsigned int i=0; i<ntick/2+1; ++i ) {
405 pval = funcGausNoise->Eval((
double)i*binWidth);
407 flat.fireArray(2, rnd, 0, 1);
408 pval *= 0.9 + 0.2*rnd[0];
410 phase = rnd[1]*2.*TMath::Pi();
411 TComplex tc(pval*cos(phase),pval*sin(phase));
412 noiseFrequency[i] += tc;
416 noise.resize(ntick,0.0);
417 std::vector<double> tmpnoise(noise.size());
418 pfft->DoInvFFT(noiseFrequency, tmpnoise);
419 noiseFrequency.clear();
421 for (
unsigned int itck=0; itck<noise.size(); ++itck ) {
422 noise[itck] = sqrt(ntick)*tmpnoise[itck];
424 for (
unsigned int itck=0; itck<noise.size(); ++itck ) {
425 aNoiseHist->Fill(noise[itck]);
429 delete funcGausNoise; funcGausNoise = 0;
435 std::vector<float> gausMean, std::vector<float> gausSigma,
436 float cohExpNorm,
float cohExpWidth,
float cohExpOffset,
437 TH1* aNoiseHist)
const {
438 const string myname =
"SBNDuBooNEDataDrivenNoiseService::generateCoherentGaussianNoise: ";
440 cout << myname <<
"Generating Coherent Gaussian noise." << endl;
443 int a = gausNorm.size();
444 int b = gausMean.size();
445 int c = gausSigma.size();
446 int NGausians = a<b?a:b;
447 NGausians = NGausians<c?NGausians:c;
449 std::stringstream
name;
451 for(
int i=0;i<NGausians;i++) {
452 name<<
"["<<3*i<<
"]*exp(-0.5*pow((x-["<<3*i+1<<
"])/["<<3*i+2<<
"],2))+";
454 name<<
"["<<3*NGausians<<
"]*exp(-x/["<<3*NGausians+1<<
"]) + ["<<3*NGausians+2<<
"]";
455 TF1 *funcCohNoise =
new TF1(
"funcGausCohsNoise",name.str().c_str(), 0, 1200);
456 funcCohNoise->SetNpx(12000);
457 for(
int i=0;i<NGausians;i++) {
458 funcCohNoise->SetParameter(3*i, gausNorm.at(i));
459 funcCohNoise->SetParameter(3*i+1, gausMean.at(i));
460 funcCohNoise->SetParameter(3*i+2, gausSigma.at(i));
462 funcCohNoise->SetParameter(3*NGausians, cohExpNorm);
463 funcCohNoise->SetParameter(3*NGausians+1, cohExpWidth);
464 funcCohNoise->SetParameter(3*NGausians+2, cohExpOffset);
467 TF1*
_poisson =
new TF1(
"_poisson",
"[0]**(x) * exp(-[0]) / ROOT::Math::tgamma(x+1.)", 0, 30);
469 double params[1] = {0.};
471 _poisson->SetParameters(params);
475 art::ServiceHandle<util::LArFFT> pfft;
476 unsigned int ntick = pfft->FFTSize();
477 CLHEP::RandFlat flat(*
m_pran);
479 unsigned nbin = ntick/2 + 1;
480 std::vector<TComplex> noiseFrequency(nbin, 0.);
483 double rnd[2] = {0.};
485 double binWidth = 1.0/(ntick*sampleRate*1.0e-6);
486 for (
unsigned int i=0; i<ntick/2+1; ++i ) {
488 pval = funcCohNoise->Eval((
double)i*binWidth);
490 flat.fireArray(2, rnd, 0, 1);
491 pval *= 0.9 + 0.2*rnd[0];
492 phase = rnd[1]*2.*TMath::Pi();
493 TComplex tc(pval*cos(phase),pval*sin(phase));
494 noiseFrequency[i] += tc;
498 noise.resize(ntick,0.0);
499 std::vector<double> tmpnoise(noise.size());
500 pfft->DoInvFFT(noiseFrequency, tmpnoise);
501 noiseFrequency.clear();
512 for (
unsigned int itck=0; itck<noise.size(); ++itck ) {
513 noise[itck] = sqrt(ntick)*tmpnoise[itck];
515 for (
unsigned int itck=0; itck<noise.size(); ++itck ) {
516 aNoiseHist->Fill(noise[itck]);
520 delete funcCohNoise; funcCohNoise = 0;
526 CLHEP::RandFlat flat(*
m_pran);
528 art::ServiceHandle<geo::Geometry> geo;
529 const unsigned int nchan = geo->Nchannels();
531 unsigned int numberofgroups = 0;
532 if(nchan%nchpergroup == 0) numberofgroups = nchan/nchpergroup;
533 else numberofgroups = nchan/nchpergroup +1;
534 unsigned int cohGroupNo = 0;
535 for(
unsigned int chan=0; chan<nchan; chan++) {
536 cohGroupNo = chan/nchpergroup;
540 for(
unsigned int ng=0; ng<numberofgroups; ng++) {
float fCohExpOffset
Amplitude offset of the exponential background component in coherent noise.
double GetRandomTF1(TF1 *func) const
Geometry description of a TPC wireThe wire is a single straight segment on a wire plane...
AdcSignalVectorVector fCohNoiseZ
noise on each channel for each time for all planes
TH1 * fGausNoiseHistU
distribution of noise counts for U
bool fEnableGaussianNoise
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 fLogLevel
Log message level: 0=quiet, 1=init only, 2+=every event.
std::vector< float > fGausMeanU
mean of the gaussian component in coherent noise
int addNoise(detinfo::DetectorClocksData const &clockData, Channel chan, AdcSignalVector &sigs) const override
float fWhiteNoiseV
Level (per freq bin) for white noise for V.
TH1 * fMicroBooNoiseHistV
distribution of noise counts for V
void generateGaussianNoise(detinfo::DetectorClocksData const &clockData, AdcSignalVector &noise, std::vector< float > gausNorm, std::vector< float > gausMean, std::vector< float > gausSigma, TH1 *aNoiseHist) const
TH1 * fMicroBooNoiseChanHist
distribution of accessed noise samples
enum geo::_plane_proj View_t
Enumerate the possible plane projections.
AdcSignalVectorVector fCohNoiseU
noise on each channel for each time for all planes
double Length() const
Returns the wire length in centimeters.
void makeCoherentGroupsByOfflineChannel(unsigned int nchpergroup)
std::vector< float > fGausMeanV
mean of the gaussian component in coherent noise
SBNDuBooNEDataDrivenNoiseService(fhicl::ParameterSet const &pset)
TH1 * fCohNoiseChanHist
distribution of accessed noise samples
std::vector< float > fGausNormV
noise scale factor for the gaussian component in coherent noise
std::ostream & print(std::ostream &out=std::cout, std::string prefix="") const override
unsigned int getGroupNumberFromOfflineChannel(unsigned int offlinechan) const
float fJumperCapacitance
Capacitance of jumper cables in pF. Defaults to 0 if not included.
std::vector< int > fGroupCoherentNoiseMap
assign each group a noise
AdcSignalVectorVector fCohNoiseV
noise on each channel for each time for all planes
std::vector< unsigned int > fChannelGroupMap
assign each channel a group number
int fRandomSeed
Seed for random number service. If absent or zero, use SeedSvc.
std::vector< float > fCohGausSigma
sigma of the gaussian component in coherent noise
float fENOB
Effective number of bits.
std::vector< float > fNoiseFunctionParameters
Parameters in the MicroBooNE noise model.
bool fEnableMicroBooNoise
enable MicroBooNE noise model
float fCohExpNorm
noise scale factor for the exponential component component in coherent noise
unsigned int fExpNoiseArrayPoints
number of points in randomly generated noise array
float fWhiteNoiseZ
Level (per freq bin) for white noise for Z.
std::vector< float > fGausSigmaV
sigma of the gaussian component in coherent noise
T gaus(T x, T amplitude, T mean, T sigma)
Tests GausssianFit object with a known input.
bool fEnableCoherentNoise
void generateNoise(detinfo::DetectorClocksData const &clockData) override
~SBNDuBooNEDataDrivenNoiseService()
float fWhiteNoiseU
Level (per freq bin) for white noise for U.
std::vector< float > fGausMeanZ
mean of the gaussian component in coherent noise
AdcSignalVectorVector fGausNoiseV
TH1 * fMicroBooNoiseHistU
distribution of noise counts for U
float fULastJumper
Wire number of last wire on U layer to include a jumper cable. Defaults to 0 if not included...
float fVFirstJumper
Wire number of first wire on V layer to include a jumper cable. Defaults to 0 if not included...
std::vector< float > fGausSigmaU
sigma of the gaussian component in coherent noise
std::vector< AdcSignal > AdcSignalVector
std::vector< float > fGausSigmaZ
sigma of the gaussian component in coherent noise
std::vector< float > fCohGausMean
mean of the gaussian component in coherent noise
AdcSignalVectorVector fGausNoiseU
Contains all timing reference information for the detector.
TH1 * fCohNoiseHist
distribution of noise counts
std::vector< float > fGausNormZ
noise scale factor for the gaussian component in coherent noise
unsigned int fCohNoiseArrayPoints
number of points in randomly generated noise array
float fVLastJumper
Wire number of last wire on V layer to include a jumper cable. Defaults to 0 if not included...
CLHEP::HepRandomEngine * m_pran
TH1 * fMicroBooNoiseHistZ
distribution of noise counts for Z
CLHEP::HepRandomEngine * ConstructRandomEngine(const bool haveSeed)
art::ServiceHandle< art::TFileService > tfs
TH1 * fGausNoiseHistV
distribution of noise counts for V
TH1 * fGausNoiseHistZ
distribution of noise counts for Z
unsigned int fNoiseArrayPoints
number of points in randomly generated noise array
float fUFirstJumper
Wire number of first wire on U layer to include a jumper cable. Defaults to 0 if not included...
float fCohExpWidth
width of the exponential component in coherent noise
std::vector< unsigned int > fNChannelsPerCoherentGroup
double sampling_rate(DetectorClocksData const &data)
Returns the period of the TPC readout electronics clock.
bool fIncludeJumpers
Include jumper term.
std::vector< float > fCohGausNorm
noise scale factor for the gaussian component in coherent noise
unsigned int getCohNoiseChanFromGroup(unsigned int cohgroup) const
TH1 * fGausNoiseChanHist
distribution of accessed noise samples
BEGIN_PROLOG could also be cout
AdcSignalVectorVector fGausNoiseZ
std::vector< float > fGausNormU
noise scale factor for the gaussian component in coherent noise