StuckBitAdcDistortionService_service.cc
Go to the documentation of this file.
1 // StuckBitAdcDistortionService_service.cc
2 
6 #include "nurandom/RandomUtils/NuRandomService.h"
9 #include "CLHEP/Random/JamesRandom.h"
10 #include "CLHEP/Random/RandFlat.h"
11 #include "TFile.h"
12 #include "TString.h"
13 #include "TProfile.h"
14 
15 using std::string;
16 using std::cout;
17 using std::ostream;
18 using std::endl;
19 using rndm::NuRandomService;
20 using CLHEP::HepJamesRandom;
21 
22 //**********************************************************************
23 
26 : m_RandomSeed(0), m_LogLevel(1),
27  m_pran(nullptr) {
28  const string myname = "StuckBitAdcDistortionService::ctor: ";
29  // Fetch parameters.
30  fStuckBitsProbabilitiesFname = pset.get<string>("StuckBitsProbabilitiesFname");
31  fStuckBitsOverflowProbHistoName = pset.get<string>("StuckBitsOverflowProbHistoName");
32  fStuckBitsUnderflowProbHistoName = pset.get<string>("StuckBitsUnderflowProbHistoName");
33  bool haveSeed = pset.get_if_present<int>("RandomSeed", m_RandomSeed);
34  pset.get_if_present<int>("LogLevel", m_LogLevel);
35  if ( m_RandomSeed == 0 ) haveSeed = false;
36  // Create random number engine.
37  if ( haveSeed ) {
38  if ( m_LogLevel > 0 ) cout << myname << "WARNING: Using hardwired seed." << endl;
39  m_pran = new HepJamesRandom(m_RandomSeed);
40  } else {
41  string rname = "StuckBitAdcDistortionService";
42  if ( m_LogLevel > 0 ) cout << myname << "Using NuRandomService." << endl;
44  m_pran = new HepJamesRandom;
45  if ( m_LogLevel > 0 ) cout << myname << " Initial seed: " << m_pran->getSeed() << endl;
46  seedSvc->registerEngine(NuRandomService::CLHEPengineSeeder(m_pran), rname);
47  }
48  if ( m_LogLevel > 0 ) cout << myname << " Registered seed: " << m_pran->getSeed() << endl;
49  // Fetch the probabilities.
50  mf::LogInfo("SimWireDUNE") << " using ADC stuck code probabilities from .root file " ;
52  cet::search_path sp("FW_SEARCH_PATH");
54  std::unique_ptr<TFile> fin(new TFile(fname.c_str(), "READ"));
55  if ( !fin->IsOpen() )
57  << "Could not find the ADC stuck code probabilities file " << fname;
58  TString iOverflowHistoName = Form( "%s", fStuckBitsOverflowProbHistoName.c_str());
59  TProfile *overflowtemp = (TProfile*) fin->Get( iOverflowHistoName );
60  if ( !overflowtemp )
62  << "Could not find the ADC code overflow probabilities histogram "
64  if ( overflowtemp->GetNbinsX() != 64 )
66  << "Overflow ADC stuck code probability histograms must have 64 bins.";
67  TString iUnderflowHistoName = Form( "%s", fStuckBitsUnderflowProbHistoName.c_str());
68  TProfile *underflowtemp = (TProfile*) fin->Get(iUnderflowHistoName);
69  if ( !underflowtemp )
71  << "Could not find the ADC code underflow probabilities histogram "
73  if ( underflowtemp->GetNbinsX() != 64 )
75  << "Underflow ADC stuck code probability histograms must have 64 bins.";
76  for ( unsigned int cellnumber=0; cellnumber < 64; ++cellnumber ) {
77  fOverflowProbs[cellnumber] = overflowtemp->GetBinContent(cellnumber+1);
78  fUnderflowProbs[cellnumber] = underflowtemp->GetBinContent(cellnumber+1);
79  }
80  fin->Close();
81 }
82 
83 //**********************************************************************
84 
86  const string myname = "StuckBitAdcDistortionService:dtor: ";
87  if ( m_LogLevel > 0 ) {
88  cout << myname << "Deleting random engine with seed " << m_pran->getSeed() << endl;
89  }
90  delete m_pran;
91 }
92 
93 //**********************************************************************
94 
96  CLHEP::RandFlat stuck_flat(*m_pran);
97  for ( size_t itck = 0; itck<adcvec.size(); ++itck ) {
98  double rnd = stuck_flat.fire(0,1);
99  const unsigned int zeromask = 0xffc0;
100  const unsigned int onemask = 0x003f;
101  unsigned int sixlsbs = adcvec[itck] & onemask;
102  int probability_index = (int)sixlsbs;
103  if ( rnd < fUnderflowProbs[probability_index] ) {
104  adcvec[itck] = adcvec[itck] | onemask; // 6 LSBs are stuck at 3F
105  adcvec[itck] -= 64; // correct 1st MSB value by subtracting 64
106  } else if ( rnd > fUnderflowProbs[probability_index] &&
107  rnd < fUnderflowProbs[probability_index] + fOverflowProbs[probability_index] ) {
108  adcvec[itck] = adcvec[itck] & zeromask; // 6 LSBs are stuck at 0
109  adcvec[itck] += 64; // correct 1st MSB value by adding 64
110  }
111  }
112  return 0;
113 }
114 
115 //**********************************************************************
116 
117 ostream& StuckBitAdcDistortionService::print(ostream& out, string prefix) const {
118  out << prefix << "StuckBitAdcDistortionService:" << endl;
119  out << prefix << " StuckBitsProbabilitiesFname: " << fStuckBitsProbabilitiesFname << endl;
120  out << prefix << " fStuckBitsOverflowProbHistoName: " << fStuckBitsOverflowProbHistoName << endl;
121  out << prefix << " fStuckBitsUnderflowProbHistoName: " << fStuckBitsUnderflowProbHistoName << endl;
122  return out;
123 }
124 
125 //**********************************************************************
126 
128 
129 //**********************************************************************
std::vector< AdcCount > AdcCountVector
Definition: AdcTypes.h:19
int modify(Channel chan, AdcCountVector &adcvec) const
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
const unsigned int onemask
Definition: raw.h:139
double fOverflowProbs[64]
array of probs for LSF bits getting stuck at 000000
std::ostream & print(std::ostream &out=std::cout, std::string prefix=" ") const
StuckBitAdcDistortionService(fhicl::ParameterSet const &pset, art::ActivityRegistry &)
std::string fStuckBitsOverflowProbHistoName
Name of hist with ADC stuck code overflow probs.
double fUnderflowProbs[64]
array of probs for LSF bits getting stuck at 111111
T get(std::string const &key) const
Definition: ParameterSet.h:271
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::string fStuckBitsProbabilitiesFname
file holding ADC stuck code probabilities
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:224
std::string find_file(std::string const &filename) const
Definition: search_path.cc:96
std::string fStuckBitsUnderflowProbHistoName
Name of hist with ADC stuck code underflow probs.
QTextStream & endl(QTextStream &s)
#define DEFINE_ART_SERVICE_INTERFACE_IMPL(svc, iface)