21 #include "nurandom/RandomUtils/NuRandomService.h" 22 #include "art_root_io/TFileService.h" 23 #include "CLHEP/Random/JamesRandom.h" 24 #include "CLHEP/Random/RandFlat.h" 25 #include "CLHEP/Random/RandGauss.h" 38 using std::ostringstream;
39 using rndm::NuRandomService;
40 using CLHEP::HepJamesRandom;
45 : fRandomSeed(0), fLogLevel(1),
46 fNoiseHistX(nullptr), fNoiseHistY(nullptr),
47 fNoiseChanHist(nullptr),
49 const string myname =
"DPhaseRealisticNoiseService::ctor: ";
61 fNoiseX.resize(fNoiseArrayPoints);
62 fNoiseY.resize(fNoiseArrayPoints);
65 fNoiseHistX = tfs->make<TH1F>(
"xnoise",
";X Noise [ADC counts];", 1000, -0.1, 0.1);
66 fNoiseHistY = tfs->make<TH1F>(
"ynoise",
";Y Noise [ADC counts];", 1000, -0.1, 0.1);
70 string rname =
"DPhaseRealisticNoiseService";
72 if (
fLogLevel > 0 ) cout << myname <<
"WARNING: Using hardwired seed." <<
endl;
73 m_pran =
new HepJamesRandom(seed);
75 if (
fLogLevel > 0 ) cout << myname <<
"Using NuRandomService." <<
endl;
77 m_pran =
new HepJamesRandom;
79 seedSvc->registerEngine(NuRandomService::CLHEPengineSeeder(
m_pran), rname);
104 const string myname =
"DPhaseRealisticNoiseService::dtor: ";
106 cout << myname <<
"Deleting random engine with seed " <<
m_pran->getSeed() <<
endl;
124 std::map<Channel, double> fPhaseChannelMap;
129 TF1 *
_sin =
new TF1(
"_sin",
"[0]*TMath::Sin( [1]*x + [2] )", 0.,
132 params[1] = (2*TMath::Pi())/(2*model_tick*dt);
133 params[2] = fPhaseChannelMap[chan];
135 _sin->SetParameters(params);
138 unsigned int noisechan = 0;
153 for (
unsigned int itck=0; itck<ntick; ++itck ) {
156 tnoise =
fNoiseY[noisechan][itck];
158 tnoise =
fNoiseX[noisechan][itck];
160 tnoise =
fNoiseX[noisechan][itck];
164 sigs[itck] += _sin->Eval((
double)itck*dt);
167 sigs[itck] += tnoise;
177 out << prefix <<
"DPhaseRealisticNoiseService: " <<
endl;
186 out << prefix <<
" Actual random seed: " <<
m_pran->getSeed();
194 std::vector<double> & frequencyArrayX, std::vector<double> & frequencyArrayY)
const {
199 TFile *fin = TFile::Open(noiseModel.c_str(),
"READ");
201 cout <<
"ERROR!: Can't open file: " << noiseModel <<
endl;
205 cout <<
"File: " << noiseModel <<
" successfully opened!" <<
endl;
208 TIter next( fin->GetListOfKeys() );
210 TProfile *inputHist =
new TProfile();
212 while( (key = (TKey*)next()) ){
214 string name( key->GetName() );
216 if( keyName ==
"TProfile"){
217 inputHist = (TProfile*)fin->Get(key->GetName());
220 std::cout <<
"ERROR! Object: " << keyName <<
" in file " << noiseModel
221 <<
"has not the right format!" <<
std::endl;
229 frequencyArrayX.resize( inputHist->GetNbinsX() );
230 frequencyArrayY.resize( inputHist->GetNbinsX() );
232 for(
size_t f=0;
f<(size_t)inputHist->GetNbinsX() ;
f++){
236 frequencyArrayY.at(
f) = inputHist->GetBinContent(
f);
237 frequencyArrayX.at(
f) = inputHist->GetBinContent(
f);
239 else if(
name.
find(
"_1")!=string::npos){
244 cout <<
"not valid view " <<
endl;
252 frequencyArrayX[0]=0;
253 frequencyArrayY[0]=0;
278 double phase = flat.fire();
279 double dph = (TMath::Pi()*0.5)/62;
291 PhaseChannelMap[chan] = phase*2*TMath::Pi();
299 int window_length)
const{
302 int size = time_vector.size();
304 for(
int i = size - window_length; i<
size; i++){ sum+=time_vector[i]; }
305 double shift = sum/window_length;
313 int TimeSamples)
const{
318 int ArraySize = noise.size();
325 noise.resize(2*ArraySize);
328 for(
int s = ArraySize;
s<2*ArraySize;
s++){
329 if(
s>TimeSamples){
break; }
330 noise[
s] = -noise[2*ArraySize -
s - 1 ] + shift;
334 if(TimeSamples > 2*ArraySize){
336 int n = ceil(TimeSamples/ArraySize);
337 int n_pass = ceil(log(n)/log(2))+1;
341 if(noise.size() > (size_t)TimeSamples){
break; }
345 noise.resize(
pow(2,(
pass+1))*ArraySize );
348 if(
s > TimeSamples){
break; }
349 noise[
s] = -noise[
pow(2,(
pass+1))*ArraySize -
s - 1 ] + shift;
355 double y, sx = 0, sy = 0, sxy = 0, sx2 = 0, sy2 = 0;
357 for (
size_t s = 0;
s < (size_t)TimeSamples; ++
s)
359 y = noise[
s]; sx +=
s; sy +=
y; sxy +=
s*
y; sx2 +=
s*
s; sy2 += y*
y;
361 double ssx = sx2 - ((sx * sx) / TimeSamples);
362 double c = sxy - ((sx * sy) / TimeSamples);
363 double mx = sx / TimeSamples;
364 double my = sy / TimeSamples;
365 double b = my - ((c / ssx) * mx);
368 for (
size_t s = 0;
s < (size_t)TimeSamples; ++
s) { noise[
s] -= (a*
s +
b); }
376 std::vector<double> frequencyVector,
379 const string myname =
"DPRealisticNoiseService::generateNoise: ";
381 cout << myname <<
"Generating noise." <<
endl;
391 unsigned int inputFreqSize = frequencyVector.size();
392 unsigned int pointFFT = 2*(inputFreqSize-1);
397 unsigned int model_tick = pfft->
FFTSize();
398 unsigned nbin = model_tick/2 + 1;
401 std::vector<TComplex> noiseFrequency(nbin, 0.);
405 for (
unsigned int i=0; i<model_tick/2+1; ++i ) {
407 pval = frequencyVector[i];
410 pval += customRandom*gaus.fire();
413 phase = flat.fire()*2.*TMath::Pi();
414 TComplex tc(pval*cos(phase),pval*sin(phase));
415 noiseFrequency[i] += tc;
420 noise.resize(model_tick, 0.0);
421 std::vector<double> tmpnoise(noise.size());
422 pfft->
DoInvFFT(noiseFrequency, tmpnoise);
423 noiseFrequency.clear();
426 for (
unsigned int itck=0; itck<noise.size(); ++itck ) {
427 noise[itck] = tmpnoise[itck];
434 if(noise.size()<(size_t)ntick){
438 for (
unsigned int itck=0; itck<noise.size(); ++itck ) {
439 aNoiseHist->Fill(noise[itck]);
TH1 * fNoiseHistY
distribution of noise counts for Y
AdcSignalVectorVector fNoiseX
noise on each channel for each time for X plane
std::ostream & print(std::ostream &out=std::cout, std::string prefix="") const
unsigned int GetModelSize() const
TH1 * fNoiseHistX
distribution of noise counts for X
bool fSetBaseline
< Sum baseline model to the data
void generateNoise(detinfo::DetectorPropertiesData const &detProp, std::vector< double > frequencyVector, AdcSignalVector &noise, TH1 *aNoiseHist, double customRandom)
CLHEP::HepRandomEngine * m_pran
enum geo::_plane_proj View_t
Enumerate the possible plane projections.
void mirrorWaveform(AdcSignalVector &noise, int TimeSamples) const
unsigned int fNoiseArrayPoints
number of points in randomly generated noise array
Planes which measure Z direction.
double fRandomizeY
< randomization of the average frequency spectrum (on kY)
double fRandomizeX
randomization of the average frequency spectrum (on kX or kZ)
int find(char c, int index=0, bool cs=TRUE) const
int fRandomSeed
Seed for random number service. If absent or zero, use SeedSvc.
AdcSignalVectorVector fNoiseY
noise on each channel for each time for Y plane
Planes which measure Y direction.
art framework interface to geometry description
void Chan2Phase(std::map< Channel, double > &PhaseChannelMap) const
int fLogLevel
Log message level: 0=quiet, 1=init only, 2+=every event.
void DoInvFFT(std::vector< TComplex > &input, std::vector< T > &output)
unsigned int Nchannels() const
Returns the number of TPC readout channels in the detector.
void SetModelSize(unsigned int size)
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
T gaus(T x, T amplitude, T mean, T sigma)
Tests GausssianFit object with a known input.
double GetShift(AdcSignalVector time_vector, int window_length) const
TH1 * fNoiseChanHist
distribution of accessed noise samples
T get(std::string const &key) const
std::string fNoiseModel
noise model root file
unsigned int NumberTimeSamples() const
View_t View(geo::PlaneID const &pid) const
Returns the view (wire orientation) on the channels of specified TPC plane.
std::vector< double > fNoiseModelFrequenciesX
Array storing the frequencies imported from the model in kHz for plane kX (kZ)
bool fOldNoiseIndex
Use old selection of noise array index.
Contains all timing reference information for the detector.
std::optional< T > get_if_present(std::string const &key) const
void importNoiseModel(std::string noiseModel, std::vector< double > &frequencyArrayX, std::vector< double > &frequencyArrayY) const
std::vector< double > fNoiseModelFrequenciesY
Array storing the frequencies imported from the model in kHz for plane kY (kZ)
std::vector< AdcSignal > AdcSignalVector
~DPhaseRealisticNoiseService()
int addNoise(detinfo::DetectorClocksData const &clockData, detinfo::DetectorPropertiesData const &detProp, Channel chan, AdcSignalVector &sigs) const
double sampling_rate(DetectorClocksData const &data)
Returns the period of the TPC readout electronics clock.
DPhaseRealisticNoiseService(fhicl::ParameterSet const &pset)
LArSoft geometry interface.
void ReinitializeFFT(int, std::string, int)
QTextStream & endl(QTextStream &s)
bool fSetFirst0
< set the first bin of the frequency array to 0
#define DEFINE_ART_SERVICE_INTERFACE_IMPL(svc, iface)