Berkeley Nuclear Data Software
CalibrationClasses.h
Go to the documentation of this file.
1 #ifndef _CALIB_CLASSES_H_
2 #define _CALIB_CLASSES_H_
3 //project includes
4 #include "NSDPhysicsCalcs.h"
5 //c++ includes
6 #include <iostream>
7 #include <cstdint>
8 #include <vector>
9 #include <map>
10 
12 {
17 };
18 
19 //abstract base class for managing detector calibrations
21 {
22 public:
23  //default constructor sets the tDiv to 1800;
24  DetectorCalib();
27  virtual double applyCalib(double a_value) const = 0;
30  virtual DetectorCalib* clone() = 0;
34  virtual double invertCalib(double a_value) const;
36  virtual std::ostream& print( std::ostream& a_stream =std::cout) const;
41  void setTime(uint32_t a_t);
43  void setTDiv(int a_timeDiv);
44  virtual ~DetectorCalib();
45 protected:
47  uint32_t m_t;
48  int m_tDiv;
49 };
52 class ScalarCalib : public DetectorCalib
53 {
54 public:
57  ScalarCalib();
59  inline double applyCalib(double a_value) const;
60 
61  void setScalar(double a_scalar);
62 
63  virtual ScalarCalib* clone();
64 private:
66  double m_scalar;
67 };
70 class OffsetCalib : public DetectorCalib
71 {
72 public:
75  OffsetCalib();
77  inline double applyCalib(double a_value) const;
78  void setOffset(double a_offset);
79  virtual std::ostream& print( std::ostream& a_stream = std::cout) const;
80  virtual OffsetCalib* clone();
81 protected:
83  double m_offset;
84 };
85 
88 class LinearCalib : public DetectorCalib
89 {
90 public:
94  LinearCalib();
96  virtual inline double applyCalib(double a_value) const;
97  inline double invertCalib(double a_value) const;
98  void setCalibration(double a_scalar, double a_offset);
99  void setScalar(double a_scalar);
100  void setOffset(double a_offset);
101  virtual std::ostream& print( std::ostream& a_stream = std::cout) const;
102  virtual LinearCalib* clone();
103 protected:
105  double m_scalar;
107  double m_offset;
108 };
112 {
113 public:
118  QuadraticCalib();
120  inline double applyCalib(double a_value) const;
121  void setCalibration(double a_quadScalar,double a_scalar, double a_offset);
122  void setQuadScalar(double a_quadScalar);
123  void setScalar(double a_scalar);
124  void setOffset(double a_offset);
125  virtual std::ostream& print( std::ostream& a_stream = std::cout) const;
126  virtual QuadraticCalib* clone();
127 protected:
128  //
129  double m_quadScalar;
131  double m_scalar;
133  double m_offset;
134 };
137 class PolyCalib : public DetectorCalib
138 {
139 public:
143  PolyCalib();
145  inline double applyCalib(double a_value) const;
146  void setCoeffs(std::vector<double> a_calibCoeffs);
147  virtual std::ostream& print( std::ostream& a_stream = std::cout) const;
148  virtual PolyCalib* clone();
149 private:
150  std::vector< double > m_calibCoeff;
151 };
155 {
156 public:
160  TDepLinearCalib();
162  inline double applyCalib(double a_value) const;
163 
164  void setGainCorrectionFactors(const std::map<uint32_t, double>& a_gcMap);
165  virtual std::ostream& print( std::ostream& a_stream = std::cout) const;
166  virtual TDepLinearCalib* clone();
167 private:
168  std::map<uint32_t,double> m_gcFactorMap;
169 };
170 
174 {
175 public:
179  TDepOffsetCalib();
181  inline double applyCalib(double a_value) const;
182 
183  void setOffsetCorrectionFactors(const std::map<uint32_t, double>& a_tdMap);
184  virtual std::ostream& print( std::ostream& a_stream = std::cout) const;
185  virtual TDepOffsetCalib* clone();
186 private:
187  std::map<uint32_t,double> m_tdOffset;
188 };
189 
190 
193 class TDepPolyCalib : public PolyCalib
194 {
195 public:
199  TDepPolyCalib();
201  inline double applyCalib(double a_value) const;
202 
203  void setGainCorrectionFactors(const std::map<uint32_t, double>& a_gcMap);
204  virtual std::ostream& print( std::ostream& a_stream = std::cout) const;
205  virtual TDepPolyCalib* clone();
206 private:
207  std::map<uint32_t,double> m_gcFactorMap;
208 };
209 
210 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
211 //implementations to allow for compiler to respect inline attempt
212 double ScalarCalib::applyCalib(double a_value) const
213 {
214  return a_value*m_scalar;
215 }
216 double OffsetCalib::applyCalib(double a_value) const
217 {
218  return a_value + m_offset;
219 }
220 double LinearCalib::applyCalib(double a_value) const
221 {
222  return a_value*m_scalar+m_offset;
223 }
224 double LinearCalib::invertCalib(double a_value) const
225 {
226  return (a_value-m_offset)/m_scalar;
227 }
228 double QuadraticCalib::applyCalib(double a_value) const
229 {
230  return a_value*a_value*m_quadScalar + a_value*m_scalar;
231 }
232 double PolyCalib::applyCalib(double a_value) const
233 {
234  return NSDPhysicsCalcs::GenExtraCalcs::polyEval(m_calibCoeff,a_value);
235 }
236 
237 double TDepLinearCalib::applyCalib(double a_value) const
238 {
239  auto mpIter = m_gcFactorMap.find(m_t);
240  if(mpIter!=m_gcFactorMap.end())
241  {
242  double factor = mpIter->second;
243  return a_value * factor * m_scalar + m_offset;
244 
245  }
246  return a_value* m_scalar+m_offset;
247 }
248 double TDepOffsetCalib::applyCalib(double a_value) const
249 {
251  auto mpIter = m_tdOffset.upper_bound(m_t);
252  auto secondIter = m_tdOffset.begin();
253  secondIter++;
254 
255  if(mpIter != m_tdOffset.end() //it is contained in the map
256  //&& mpIter != secondIter //its not the second element ?? why?
257  && mpIter != m_tdOffset.begin()
258  )
259  {
260  double tAfter = mpIter->first;
261  double offsetAfter = mpIter->second;
262  mpIter--;
263  double tBefore = mpIter->first;
264  double offsetBefore = mpIter->second;
265  double dt = tAfter-tBefore;
266  double slope = (offsetAfter-offsetBefore)/dt;
267  double step = m_t-tBefore;
268  double additionalOffset = step*slope;
269  return a_value + m_offset+additionalOffset+offsetBefore;
270  }
271 
272  //implicit else due to return ie if we aren't within the range of the
273  //specified time series
274  return a_value + m_offset;
275 }
276 
277 double TDepPolyCalib::applyCalib(double a_value) const
278 {
279  auto mpIter = m_gcFactorMap.find(m_t);
280  if(mpIter!=m_gcFactorMap.end())
281  {
282  double factor = mpIter->second;
283  //time dependent corrected value
284  double cV = factor*a_value;
285  return PolyCalib::applyCalib(cV);
286 
287  }
288  return PolyCalib::applyCalib(a_value);
289 }
290 
291 #endif
CalibType
Definition: CalibrationClasses.h:12
@ offset
Definition: CalibrationClasses.h:14
@ linear
Definition: CalibrationClasses.h:15
@ quadratic
Definition: CalibrationClasses.h:16
@ scalar
Definition: CalibrationClasses.h:13
Definition: CalibrationClasses.h:21
void setTDiv(int a_timeDiv)
sets the divisor for which to consider the given time
Definition: CalibrationClasses.cpp:39
int m_tDiv
Definition: CalibrationClasses.h:48
void setTime(uint32_t a_t)
sets the experiment time during the processing in time since epoch
Definition: CalibrationClasses.cpp:32
uint32_t m_t
Definition: CalibrationClasses.h:47
CalibType m_type
Definition: CalibrationClasses.h:46
virtual DetectorCalib * clone()=0
CalibType getCalibType()
Definition: CalibrationClasses.cpp:27
virtual ~DetectorCalib()
Definition: CalibrationClasses.cpp:17
DetectorCalib()
Definition: CalibrationClasses.cpp:9
virtual double applyCalib(double a_value) const =0
virtual std::ostream & print(std::ostream &a_stream=std::cout) const
prints nothing by default
Definition: CalibrationClasses.cpp:3
virtual double invertCalib(double a_value) const
Definition: CalibrationClasses.cpp:22
Definition: CalibrationClasses.h:89
double invertCalib(double a_value) const
Definition: CalibrationClasses.h:224
LinearCalib()
Definition: CalibrationClasses.cpp:83
virtual std::ostream & print(std::ostream &a_stream=std::cout) const
prints nothing by default
Definition: CalibrationClasses.cpp:245
void setCalibration(double a_scalar, double a_offset)
Definition: CalibrationClasses.cpp:90
void setOffset(double a_offset)
Definition: CalibrationClasses.cpp:100
virtual LinearCalib * clone()
Definition: CalibrationClasses.cpp:105
double m_scalar
scalar for the calibration
Definition: CalibrationClasses.h:105
void setScalar(double a_scalar)
Definition: CalibrationClasses.cpp:95
virtual double applyCalib(double a_value) const
returns a_value*m_scalar+m_offset
Definition: CalibrationClasses.h:220
double m_offset
offset for the calibration
Definition: CalibrationClasses.h:107
static double polyEval(const std::vector< double > &a_coeffs, double a_val)
Definition: NSDPhysicsCalcs.h:289
Definition: CalibrationClasses.h:71
virtual std::ostream & print(std::ostream &a_stream=std::cout) const
prints nothing by default
Definition: CalibrationClasses.cpp:238
double m_offset
offset for the calibration
Definition: CalibrationClasses.h:83
virtual OffsetCalib * clone()
Definition: CalibrationClasses.cpp:77
double applyCalib(double a_value) const
returns a_value + m_offset
Definition: CalibrationClasses.h:216
OffsetCalib()
Definition: CalibrationClasses.cpp:64
void setOffset(double a_offset)
returns a_value + m_offset
Definition: CalibrationClasses.cpp:72
Definition: CalibrationClasses.h:138
virtual std::ostream & print(std::ostream &a_stream=std::cout) const
prints nothing by default
Definition: CalibrationClasses.cpp:264
void setCoeffs(std::vector< double > a_calibCoeffs)
Definition: CalibrationClasses.cpp:153
virtual PolyCalib * clone()
Definition: CalibrationClasses.cpp:159
PolyCalib()
Definition: CalibrationClasses.cpp:148
double applyCalib(double a_value) const
returns gc(t)*a_value*m_scalar+m_offset
Definition: CalibrationClasses.h:232
Definition: CalibrationClasses.h:112
void setCalibration(double a_quadScalar, double a_scalar, double a_offset)
Definition: CalibrationClasses.cpp:119
double m_offset
scalar for the calibration
Definition: CalibrationClasses.h:133
void setQuadScalar(double a_quadScalar)
Definition: CalibrationClasses.cpp:128
virtual QuadraticCalib * clone()
Definition: CalibrationClasses.cpp:143
void setScalar(double a_scalar)
Definition: CalibrationClasses.cpp:133
virtual std::ostream & print(std::ostream &a_stream=std::cout) const
prints nothing by default
Definition: CalibrationClasses.cpp:253
double m_scalar
scalar for the calibration
Definition: CalibrationClasses.h:131
void setOffset(double a_offset)
Definition: CalibrationClasses.cpp:138
double m_quadScalar
Definition: CalibrationClasses.h:129
double applyCalib(double a_value) const
returns a_value*a_value*m_quadScalar + a_value*m_scalar
Definition: CalibrationClasses.h:228
QuadraticCalib()
returns a_value*a_value*m_quadScalar + a_value*m_scalar
Definition: CalibrationClasses.cpp:111
Definition: CalibrationClasses.h:53
virtual ScalarCalib * clone()
Definition: CalibrationClasses.cpp:58
void setScalar(double a_scalar)
Definition: CalibrationClasses.cpp:52
ScalarCalib()
Definition: CalibrationClasses.cpp:44
double applyCalib(double a_value) const
returns a_value*m_scalar
Definition: CalibrationClasses.h:212
Definition: CalibrationClasses.h:155
virtual std::ostream & print(std::ostream &a_stream=std::cout) const
prints nothing by default
Definition: CalibrationClasses.cpp:180
double applyCalib(double a_value) const
returns gc(t)*a_value*m_scalar+m_offset
Definition: CalibrationClasses.h:237
virtual TDepLinearCalib * clone()
Definition: CalibrationClasses.cpp:168
TDepLinearCalib()
Definition: CalibrationClasses.cpp:163
void setGainCorrectionFactors(const std::map< uint32_t, double > &a_gcMap)
Definition: CalibrationClasses.cpp:175
Definition: CalibrationClasses.h:174
void setOffsetCorrectionFactors(const std::map< uint32_t, double > &a_tdMap)
Definition: CalibrationClasses.cpp:194
double applyCalib(double a_value) const
returns gc(t)*a_value*m_scalar+m_offset
Definition: CalibrationClasses.h:248
virtual std::ostream & print(std::ostream &a_stream=std::cout) const
prints nothing by default
Definition: CalibrationClasses.cpp:199
virtual TDepOffsetCalib * clone()
Definition: CalibrationClasses.cpp:208
TDepOffsetCalib()
Definition: CalibrationClasses.cpp:189
Definition: CalibrationClasses.h:194
virtual TDepPolyCalib * clone()
Definition: CalibrationClasses.cpp:233
void setGainCorrectionFactors(const std::map< uint32_t, double > &a_gcMap)
Definition: CalibrationClasses.cpp:219
virtual std::ostream & print(std::ostream &a_stream=std::cout) const
prints nothing by default
Definition: CalibrationClasses.cpp:224
TDepPolyCalib()
Definition: CalibrationClasses.cpp:214
double applyCalib(double a_value) const
returns gc(t)*a_value*m_scalar+m_offset
Definition: CalibrationClasses.h:277