FembLinearCalibration_tool.cc
Go to the documentation of this file.
1 // FembLinearCalibration_tool.cc
2 
4 #include <iostream>
5 #include <sstream>
6 #include <iomanip>
7 
8 using std::string;
9 using std::cout;
10 using std::endl;
11 using std::ostringstream;
12 using std::setw;
13 
14 using Index = unsigned int;
15 
16 //**********************************************************************
17 
19 : m_LogLevel(ps.get<int>("LogLevel")),
20  m_Units(ps.get<string>("Units")),
21  m_Gains(ps.get<AdcSignalVector>("Gains")),
22  m_AdcMin(ps.get<AdcCount>("AdcMin")),
23  m_AdcMins(ps.get<AdcCountVector>("AdcMins")),
24  m_AdcMax(ps.get<AdcCount>("AdcMax")),
25  m_AdcMaxs(ps.get<AdcCountVector>("AdcMaxs")) {
26  const string myname = "FembLinearCalibration::ctor: ";
27  int w = 28;
28  if ( m_LogLevel >= 1 ) {
29  cout << myname << setw(w) << "Log level: " << m_LogLevel << endl;
30  ostringstream sslab;
31  sslab << "Gains[" << m_Gains.size() << "]" << (m_Gains.size() ? ": " : " ");;
32  cout << myname << setw(w) << sslab.str();
33  for ( Index icha=0; icha<m_Gains.size(); ++icha ) {
34  if ( icha > 0 ) cout << ", ";
35  if ( icha >= 10 ) { cout << "..."; break; }
36  cout << m_Gains[icha];
37  }
38  cout << endl;
39  cout << myname << setw(w) << "Default ADC min: " << m_AdcMin << endl;
40  sslab.str("");
41  sslab << "Channel ADC min[" << m_AdcMins.size() << "]" << (m_AdcMins.size() ? ": " : " ");;
42  cout << myname << setw(w) << sslab.str();
43  for ( Index icha=0; icha<m_AdcMins.size(); ++icha ) {
44  if ( icha > 0 ) cout << ", ";
45  if ( icha >= 10 ) { cout << "..."; break; }
46  cout << m_AdcMins[icha];
47  }
48  cout << endl;
49  cout << myname << setw(w) << "Default ADC max: " << m_AdcMax << endl;
50  sslab.str("");
51  sslab << "Channel ADC max[" << m_AdcMaxs.size() << "]" << (m_AdcMaxs.size() ? ": " : " ");
52  cout << myname << setw(w) << sslab.str();
53  for ( Index icha=0; icha<m_AdcMaxs.size(); ++icha ) {
54  if ( icha > 0 ) cout << ", ";
55  if ( icha >= 10 ) { cout << "..."; break; }
56  cout << m_AdcMaxs[icha];
57  }
58  cout << endl;
59  }
60 }
61 
62 //**********************************************************************
63 
66  AdcChannelData acdtmp(acd);
67  return update(acdtmp);
68 }
69 
70 //**********************************************************************
71 
73  const string myname = "FembLinearCalibration::update: ";
74  DataMap res;
75  AdcChannel icha = acd.channel();
76  if ( icha == AdcChannelData::badChannel() ) {
77  if ( m_LogLevel >= 2 ) {
78  cout << myname << "Data does not have a channel ID." << endl;
79  }
80  return res.setStatus(2);
81  }
82  if ( icha >= m_Gains.size() ) {
83  if ( m_LogLevel >= 2 ) {
84  cout << myname << "Gain not found for channel " << icha << endl;
85  }
86  return res.setStatus(4);
87  }
88  if ( m_AdcMins.size() && icha >= m_AdcMins.size() ) {
89  if ( m_LogLevel >= 2 ) {
90  cout << myname << "ADC min not found for channel " << icha << endl;
91  }
92  return res.setStatus(5);
93  }
94  if ( m_AdcMaxs.size() && icha >= m_AdcMaxs.size() ) {
95  if ( m_LogLevel >= 2 ) {
96  cout << myname << "ADC max not found for channel " << icha << endl;
97  }
98  return res.setStatus(6);
99  }
100  AdcSignal gain = m_Gains[icha];
101  if ( gain > 0.0 ) {
102  acd.samples.resize(acd.raw.size(), 0.0);
103  acd.flags.resize(acd.raw.size(), AdcGood);
104  AdcCount adcmin = m_AdcMins.size() ? m_AdcMins[icha] : m_AdcMin;
105  AdcCount adcmax = m_AdcMaxs.size() ? m_AdcMaxs[icha] : m_AdcMax;
106  Index nunder = 0;
107  Index nover = 0;
108  Index nsam = acd.raw.size();
109  for ( Index isam=0; isam<nsam; ++isam ) {
110  acd.samples[isam] = gain*(acd.raw[isam] - acd.pedestal);
111  if ( acd.raw[isam] <= adcmin ) {
112  acd.flags[isam] = AdcUnderflow;
113  ++nunder;
114  } else if ( acd.raw[isam] >= adcmax ) {
115  acd.flags[isam] = AdcOverflow;
116  ++nover;
117  }
118  }
119  acd.sampleUnit = m_Units;
120  res.setInt("calibSampleCount", nsam);
121  res.setInt("calibUnderflowCount", nunder);
122  res.setInt("calibOverflowCount", nover);
123  res.setInt("calibAdcMin", adcmin);
124  res.setInt("calibAdcMax", adcmax);
125  } else {
126  acd.samples.resize(0);
127  }
128  res.setFloat("calibGain", gain);
129  return res;
130 }
131 
132 //**********************************************************************
133 
std::vector< AdcCount > AdcCountVector
Definition: AdcTypes.h:19
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
FembLinearCalibration(fhicl::ParameterSet const &ps)
static QCString result
void setFloat(Name name, float val)
Definition: DataMap.h:133
unsigned int Index
DataMap & setStatus(int stat)
Definition: DataMap.h:130
std::string string
Definition: nybbler.cc:12
float AdcSignal
Definition: AdcTypes.h:21
const AdcFlag AdcUnderflow
Definition: AdcTypes.h:33
unsigned int Index
const AdcFlag AdcGood
Definition: AdcTypes.h:32
DataMap update(AdcChannelData &acd) const override
const AdcFlag AdcOverflow
Definition: AdcTypes.h:34
void setInt(Name name, int val)
Definition: DataMap.h:131
static constexpr double ps
Definition: Units.h:99
AdcCountVector raw
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
Channel channel() const
AdcSignal pedestal
unsigned int AdcChannel
Definition: AdcTypes.h:50
std::vector< AdcSignal > AdcSignalVector
Definition: AdcTypes.h:22
static Index badChannel()
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
short AdcCount
Definition: AdcTypes.h:18
AdcSignalVector samples
QTextStream & endl(QTextStream &s)
DataMap view(const AdcChannelData &acd) const override
AdcFlagVector flags