FloatArrayGainCalibration_tool.cc
Go to the documentation of this file.
1 // FloatArrayGainCalibration_tool.cc
2 
9 #include <iostream>
10 #include <sstream>
11 #include <iomanip>
12 
13 using std::string;
14 using std::cout;
15 using std::endl;
16 using std::ostringstream;
17 using std::setw;
18 
19 using Index = unsigned int;
20 
21 //**********************************************************************
22 
23 // Stream a vector as CSV.
24 template<class T>
25 std::ostream& operator<<(std::ostream& lhs, const std::vector<T>& vals) {
26  bool first = true;
27  for ( const T& val : vals ) {
28  if ( first ) first = false;
29  else lhs << ", ";
30  lhs << val;
31  }
32  return lhs;
33 }
34 
35 //**********************************************************************
36 
38 : m_LogLevel(ps.get<int>("LogLevel")),
39  m_Unit(ps.get<string>("Unit")),
40  m_GainDefault(new RootParFormula("GainDefault", ps.get<Name>("GainDefault"))),
41  m_AdcUnderflowDefault(ps.get<unsigned int>("AdcUnderflowDefault")),
42  m_AdcOverflowDefault(ps.get<unsigned int>("AdcOverflowDefault")),
43  m_GainTool(ps.get<string>("GainTool")),
44  m_ScaleFactor(new RootParFormula("ScaleFactor", ps.get<Name>("ScaleFactor"))),
45  m_pgains(nullptr),
46  m_prdtool(nullptr) {
47  const string myname = "FloatArrayGainCalibration::ctor: ";
49  if ( m_GainTool.size() == 0 ) {
50  cout << myname << "INFO: No gain array tool. Default gain will be used." << endl;
51  } else if ( pdtm == nullptr ) {
52  cout << myname << "ERROR: Unable to retrieve tool manager." << endl;
53  } else {
55  if ( ! m_pgains ) {
56  cout << myname << "ERROR: Unable to retrieve gains tool " << m_GainTool << endl;
57  }
58  }
59  // Fetch run data tool.
60  if ( m_GainDefault->npar() || m_ScaleFactor->npar() ) {
61  string stnam = "runDataTool";
62  m_prdtool = pdtm->getShared<RunDataTool>(stnam);
63  if ( m_prdtool == nullptr ) {
64  cout << myname << "ERROR: RunDataTool " << stnam
65  << " not found. Formulas will not be evaluated." << endl;
66  } else {
67  cout << myname << "RunDataTool retrieved." << endl;
68  }
69  }
72  if ( m_LogLevel >= 1 ) {
73  cout << myname << " LogLevel: " << m_LogLevel << endl;
74  cout << myname << " Unit: " << m_Unit << endl;
75  cout << myname << " GainDefault: " << m_GainDefault->formulaString() << endl;
76  if ( m_pgains == nullptr ) {
77  cout << myname << " No GainTool." << endl;
78  } else {
79  cout << myname << " GainTool: " << m_GainTool << " (@" << m_pgains << ")" << endl;
80  }
81  cout << myname << " ScaleFactor: " << m_ScaleFactor->formulaString() << endl;
82  }
83 }
84 
85 //**********************************************************************
86 
89  AdcChannelData acdtmp(acd);
90  acdtmp.raw = acd.raw;
91  return update(acdtmp);
92 }
93 
94 //**********************************************************************
95 
97  const string myname = "FloatArrayGainCalibration::update: ";
98  DataMap res;
99  AdcChannel icha = acd.channel();
100  if ( icha == AdcChannelData::badChannel() ) {
101  if ( m_LogLevel >= 2 ) {
102  cout << myname << "Data does not have a channel ID." << endl;
103  }
104  return res.setStatus(2);
105  }
106  if ( m_LogLevel >= 2 ) {
107  if ( m_pgains != nullptr && ! m_pgains->inRange(icha) ) {
108  cout << myname << "Gain not found for channel " << icha << endl;
109  }
110  }
111  if ( m_prdtool != nullptr ) {
112  RunData rdat = m_prdtool->runData(acd.run());
113  if ( ! rdat.isValid() ) cout << myname << "WARNING: RunData not found." << endl;
114  else {
117  }
118  }
119  if ( ! m_GainDefault->ready() ) {
120  cout << myname << "WARNING: Unset parameters in GainDefault: [" << m_GainDefault->unsetPars()
121  << "]. Using default value " << m_GainDefault->defaultEval() << endl;
122  }
123  if ( ! m_ScaleFactor->ready() ) {
124  cout << myname << "WARNING: Unset parameters in ScaleFactor: [" << m_ScaleFactor->unsetPars()
125  << "]. Using default value " << m_ScaleFactor->defaultEval() << endl;
126  }
127  float gdef = m_GainDefault->eval();
128  float gain = m_pgains == nullptr ? gdef :
129  gdef >= 0 ? m_pgains->value(icha, gdef) :
130  m_pgains->value(icha);
131  gain *= m_ScaleFactor->eval();
134  acd.samples.resize(acd.raw.size(), 0.0);
135  acd.flags.resize(acd.raw.size(), AdcGood);
136  AdcCount adcmin = 0;
137  AdcCount adcmax = 0;
138  Index nunder = 0;
139  Index nover = 0;
140  Index nsam = acd.raw.size();
141  Name unit = m_Unit;
142  if ( m_LogLevel >= 3 ) cout << myname << "Processing " << nsam << " samples for channel " << icha << endl;
143  for ( Index isam=0; isam<nsam; ++isam ) {
144  AdcCount adcin = acd.raw[isam];
145  if ( adcin <= adcudr ) {
146  acd.flags[isam] = AdcUnderflow;
147  ++nunder;
148  } else if ( adcin >= adcovr ) {
149  acd.flags[isam] = AdcOverflow;
150  ++nover;
151  }
152  if ( isam ==0 || adcin < adcmin ) adcmin = adcin;
153  if ( isam ==0 || adcin > adcmax ) adcmax = adcin;
154  float sigout = gain*(adcin - acd.pedestal);
155  acd.samples[isam] = sigout;
156  }
157  acd.sampleUnit = unit;
158  res.setFloat("calibGain", gain);
159  res.setInt("calibSampleCount", nsam);
160  res.setInt("calibUnderflowCount", nunder);
161  res.setInt("calibOverflowCount", nover);
162  res.setInt("calibAdcMin", adcmin);
163  res.setInt("calibAdcMax", adcmax);
164  return res;
165 }
166 
167 //**********************************************************************
168 
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
DataMap update(AdcChannelData &acd) const override
DataMap view(const AdcChannelData &acd) const override
static QCString result
void setFloat(Name name, float val)
Definition: DataMap.h:133
virtual int setDefaultEval(Value val)
Definition: ParFormula.h:74
unsigned int Index
DataMap & setStatus(int stat)
Definition: DataMap.h:130
std::string string
Definition: nybbler.cc:12
virtual double eval(const Values &vars) const =0
virtual Name formulaString() const =0
virtual bool ready() const
Definition: ParFormula.h:56
const AdcFlag AdcUnderflow
Definition: AdcTypes.h:33
virtual Names unsetPars() const =0
unsigned int Index
bool isValid() const
Definition: RunData.h:48
const AdcFlag AdcGood
Definition: AdcTypes.h:32
virtual Index npar() const
Definition: ParFormula.h:44
const AdcFlag AdcOverflow
Definition: AdcTypes.h:34
AdcIndex run() const
void setInt(Name name, int val)
Definition: DataMap.h:131
virtual RunData runData(Index run, Index subRun=0) const =0
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
FloatArrayGainCalibration(fhicl::ParameterSet const &ps)
virtual Value defaultEval() const
Definition: ParFormula.h:59
virtual float value(Index ival) const
static Index badChannel()
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
short AdcCount
Definition: AdcTypes.h:18
SetStat setFormulaPars(TFormula *form)
Definition: RunData.h:87
virtual bool inRange(Index ival) const
static DuneToolManager * instance(std::string fclname="", int dbg=1)
AdcSignalVector samples
T * getShared(std::string name)
QTextStream & endl(QTextStream &s)
AdcFlagVector flags