25 #include "nurandom/RandomUtils/NuRandomService.h" 26 #include "art_root_io/TFileService.h" 27 #include "CLHEP/Random/JamesRandom.h" 28 #include "CLHEP/Random/RandFlat.h" 29 #include "CLHEP/Random/RandGauss.h" 35 #include "TProfile2D.h" 44 using std::ostringstream;
45 using rndm::NuRandomService;
46 using CLHEP::HepJamesRandom;
52 : fRandomSeed(0), fLogLevel(1),
55 const string myname =
"DPhaseCoherentNoiseService::ctor: ";
74 string rname =
"DPhaseCoherentNoiseService";
77 if ( fLogLevel > 0 ) cout << myname <<
"WARNING: Using hardwired seed." <<
endl;
78 m_pran =
new HepJamesRandom(seed);
82 if ( fLogLevel > 0 ) cout << myname <<
"Using NuRandomService." <<
endl;
84 m_pran =
new HepJamesRandom;
85 if ( fLogLevel > 0 ) cout << myname <<
" Initial seed: " <<
m_pran->getSeed() <<
endl;
86 seedSvc->registerEngine(NuRandomService::CLHEPengineSeeder(
m_pran), rname);
88 if ( fLogLevel > 0 ) cout << myname <<
" Registered seed: " <<
m_pran->getSeed() <<
endl;
104 if( frequencyArray.
size() != amplitudeArray.
size() ){
105 if ( fLogLevel > 0 ){ cout << myname <<
"frequency array and amplitude array have not the same size in chan: " << chan <<
endl; }
109 if( max < (
int)frequencyArray.
size() ){ max = frequencyArray.
size(); }
133 const string myname =
"DPhaseCoherentNoiseService::dtor: ";
135 cout << myname <<
"Deleting random engine with seed " <<
m_pran->getSeed() <<
endl;
146 const string myname =
"DPhaseCoherentNoiseService::addNoise: ";
148 cout << myname <<
" Processing channel: " << chan <<
endl;
162 cout << myname <<
" Map number: " << num <<
endl;
173 if( frequencyArray.
size() == 0 && amplitudeArray.
size() ==0 ){
175 nullArray.resize( ntick );
179 cout << myname <<
" No freq and amplitude arrays for " << chan <<
endl;
187 sigs.resize( ntick );
202 cout << myname <<
"Invalid plane" <<
endl;
207 getNoiseArray(clockData, sigs, amplitudeArray, frequencyArray, phaseArray, randAmp);
216 for (
unsigned int itck=0; itck<ntick; ++itck )
221 for (
unsigned int itck=0; itck<ntick; ++itck )
226 cout << myname <<
"Invalid plane" <<
endl;
232 cout << myname <<
" All done " <<
endl;
243 out << prefix <<
"DPhaseCoherentNoiseService: " <<
endl;
247 out << prefix <<
" Actual random seed: " <<
m_pran->getSeed();
304 Map & chFrequencyMap,
Map & chAmplitudeMap,
double cut,
int &normalization )
const {
310 TFile *fin = TFile::Open(noiseModel.c_str(),
"READ");
312 cout <<
"ERROR!: Can't open file: " << noiseModel <<
endl;
316 cout <<
"File: " << noiseModel <<
" successfully opened!" <<
endl;
320 TIter next( fin->GetListOfKeys() );
322 TProfile2D *inputHist =
new TProfile2D();
324 while( (key = (TKey*)next()) ){
326 string name( key->GetName() );
327 string keyName( key->GetClassName() );
328 if( keyName ==
"TProfile2D"){
329 inputHist = (TProfile2D*)fin->Get(key->GetName());
332 std::cout <<
"ERROR! Object: " << keyName <<
" in file " << noiseModel
333 <<
"has not the right format!" <<
std::endl;
340 for(
int xx=1; xx<inputHist->GetNbinsX()+1; xx++){
341 for(
int yy=1; yy<inputHist->GetNbinsY()+1; yy++){
343 double amplitude = inputHist->GetBinContent( xx, yy );
344 double ch = (
int)inputHist->GetXaxis()->GetBinLowEdge(xx)+1;
345 double frequency = inputHist->GetYaxis()->GetBinCenter(yy);
347 if(amplitude >= cut){
348 chFrequencyMap[ch].push_back( frequency );
349 chAmplitudeMap[ch].push_back( amplitude );
363 float minShift,
float maxShift ){
371 phaseVector.resize(size);
372 phaseArray.resize(size);
376 double phase = flat.fire()*2.*TMath::Pi();
377 phaseArray.at(
f) = phase;
393 double phase = minShift + flat.fire()*( maxShift - minShift );
394 phaseVector.at(
f) = phase + phaseArray.at(
f);
400 phaseMap[chan] = phaseVector;
414 const string myname =
"DPhaseCoherentNoiseService::getNoiseArray: ";
422 if( (ampArray.
size() != freqArray.
size()) || (ampArray.
size() < freqArray.
size()) ){
424 const string myname =
"DPhaseCoherentNoiseService::getNoiseArray: ";
426 cout << myname <<
"ERROR: amplitude array and frequency array have not the same size." <<
endl;
432 for(
size_t t=0;
t<noiseArray.
size();
t++ ){
435 for(
size_t f=0;
f<freqArray.
size();
f++ ){
438 double amp = ampArray.at(
f) + gaus.fire( 0, randAmp );
441 double argument = 2*TMath::Pi()*
sampling_rate(clockData)*(1.e-3)*freqArray.at(
f)*
t + phaseArray.at(
f);
int getNumber(Channel chan) const
std::string fNoiseModel
< noise model root file
std::vector< float > fRandomize
< randomization of the amplitude
enum geo::_plane_proj View_t
Enumerate the possible plane projections.
int fLogLevel
< Log message level: 0=quiet, 1=init only, 2+=every event
int fNumberOfPhases
< Number of pregenerated phase shift maps
Map fChFrequencyMap
Map storing the frequency vector for each channel.
int fNormalization
< Normalization factor ( similar to the one for the InFFT )
std::vector< int > fChannelGroup
< Channels in the same group get the same phase
Planes which measure Z direction.
void importNoiseModel(std::string noiseModel, Map &chFrequencyMap, Map &chAmplitudeMap, double cut, int &normalization) const
~DPhaseCoherentNoiseService()
void makePhaseMap(Map &phaseMap, int size, float minShift, float maxShift)
Planes which measure Y direction.
DPhaseCoherentNoiseService(fhicl::ParameterSet const &pset)
std::ostream & print(std::ostream &out=std::cout, std::string prefix="") const
art framework interface to geometry description
unsigned int Nchannels() const
Returns the number of TPC readout channels in the detector.
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Map fChAmplitudeMap
Map holding the amplitude of the frequency for each channel.
T gaus(T x, T amplitude, T mean, T sigma)
Tests GausssianFit object with a known input.
T get(std::string const &key) const
void getAmplitudeArray(Channel chan, std::vector< float > &array) const
std::vector< float > fInchoerentNoise
< Mean and std of the incoherent noise
int addNoise(detinfo::DetectorClocksData const &clockData, detinfo::DetectorPropertiesData const &detProp, Channel chan, AdcSignalVector &sigs) const
unsigned int NumberTimeSamples() const
int fNum
Hold the correct event number.
static int max(int a, int b)
View_t View(geo::PlaneID const &pid) const
Returns the view (wire orientation) on the channels of specified TPC plane.
int fRandomSeed
< Seed for random number service. If absent or zero, use SeedSvc.
void getNoiseArray(detinfo::DetectorClocksData const &clockData, std::vector< float > &noiseArray, std::vector< float > ampArray, std::vector< float > freqArray, std::vector< float > phaseArray, float randAmp) const
auto array(Array const &a)
Returns a manipulator which will print the specified array.
void getFrequencyArray(Channel chan, std::vector< float > &array) const
Contains all timing reference information for the detector.
std::optional< T > get_if_present(std::string const &key) const
std::vector< Map > fPhaseMap
Pregenerated phase shift maps.
std::vector< AdcSignal > AdcSignalVector
double fAmplitudeCut
< only frequencies with amplitude above this cut will be considered
double sampling_rate(DetectorClocksData const &data)
Returns the period of the TPC readout electronics clock.
LArSoft geometry interface.
std::vector< float > fPhaseShift
< Phase shift for each group of 30 channels
std::map< int, std::vector< float > > Map
CLHEP::HepRandomEngine * m_pran
QTextStream & endl(QTextStream &s)
#define DEFINE_ART_SERVICE_INTERFACE_IMPL(svc, iface)