SampleTailer.h
Go to the documentation of this file.
1 // SampleTailer.h
2 //
3 // David Adams
4 // April 2019
5 //
6 // Class that adds or remove exponential tails from a sampled
7 // distribution, i.e. a vector of floats.
8 //
9 // The data is a sum of signal, tail and pedestal:
10 // d[i] = s[i] + t[i] + p
11 // Or solving for the signal:
12 // s[i] = d[i] - t[i] - p
13 // The tail decays and has contribution from signal as follows:
14 // t[i] = beta t[i-1] - alpha s[i-1]
15 // Tails are added or removed from a distribution by combining this
16 // in iteration with the repective preceding expression.
17 //
18 // The tails are characterized by four parameters:
19 // tdec (or beta) - Decay time in ticks. By definition beta = exp(-1/tdec).
20 // alpha - Starting tail fraction of the signal (see above).
21 // pedestal - Data pedestal (p in the expression above).
22 // There is also the option to provide a pedestal vector.
23 // tail0 - Tail level in the first sample, i.e. t[0] above.
24 // Note that if
25 // alpha = 1/beta - 1
26 // then the tail precisely cancels the signal:
27 // SUM_i (s[i] + t[i]) = 0
28 
29 #ifndef SampleTailer_H
30 #define SampleTailer_H
31 
32 #include <vector>
33 #include <string>
34 
35 class TH1;
36 
37 class SampleTailer {
38 
39 public:
40 
41  using Index = unsigned int;
42  using FloatVector = std::vector<float>;
43  using Name = std::string;
44 
45  // Ctor from the decay time.
46  // The tail fraction is chosen so signal cancels tail.
47  // The pedestal and t0 are set to zero.
48  SampleTailer(float a_decayTime);
49 
50  // Ctor from decay time and alpha.
51  // The pedestal and t0 are set to zero.
52  SampleTailer(float a_decayTime, float a_alpha);
53 
54  // Dtor.
55  ~SampleTailer();
56 
57  // Clear the data, signal and tail vectors.
58  void clear();
59 
60  // Parameter setters.
61  // Data, signal and tail are cleared.
62  int setDecayTime(float val, bool cancelSignal);
63  int setBeta(float val, bool cancelSignal);
64  int setAlpha(float val);
65  int setPedestal(float val);
67  int setTail0(float val);
68  int setUnit(Name val) { m_unit = val; return 0; }
69 
70  // Evaluate the signal and tail for provided data.
71  int setData(const FloatVector& inData);
72 
73  // Evaluate the signal and tail for data value zero for nsam smples..
74  int setDataZero(Index nsam);
75 
76  // Evaluate the data and tail for provided signal.
77  int setSignal(const FloatVector& inSignal);
78 
79  // Return if the parameters are valid, e.g. beta > 0.
80  bool isValid() const { return m_beta > 0.0; }
81 
82  // Getters.
83  float decayTime() const { return m_decayTime; }
84  float beta() const { return m_beta; }
85  float alpha() const { return m_alpha; }
86  float pedestal() const { return m_pedestal; }
87  float tail0() const { return m_tail0; }
88  const FloatVector& data() const { return m_d; }
89  float data(Index isam) const { return isam < m_d.size() ? m_d[isam] : 0.0; }
90  const FloatVector& signal() const { return m_s; }
91  float signal(Index isam) const { return isam < m_s.size() ? m_s[isam] : 0.0; }
92  const FloatVector& tail() const { return m_t; }
93  float tail(Index isam) const { return isam < m_t.size() ? m_t[isam] : 0.0; }
94  Index size() const { return m_d.size(); }
95  Name unit() const { return m_unit; }
96  TH1* dataHist() const { return m_phd; }
97  TH1* signalHist() const { return m_phs; }
98  TH1* tailHist() const { return m_pht; }
99 
100  // Return histograms.
101  TH1* dataHist();
102  TH1* signalHist();
103  TH1* tailHist();
104  void makeHists();
105 
106 private:
107 
108  float m_decayTime;
109  float m_beta;
110  float m_alpha;
111  float m_pedestal;
113  float m_tail0;
118  TH1* m_phd =nullptr;
119  TH1* m_phs =nullptr;
120  TH1* m_pht =nullptr;
121 
122 };
123 
124 #endif
float data(Index isam) const
Definition: SampleTailer.h:89
std::vector< float > FloatVector
Definition: SampleTailer.h:42
bool isValid() const
Definition: SampleTailer.h:80
int setData(const FloatVector &inData)
const FloatVector * m_ppedvec
Definition: SampleTailer.h:112
TH1 * signalHist() const
Definition: SampleTailer.h:97
std::string string
Definition: nybbler.cc:12
int setSignal(const FloatVector &inSignal)
TH1 * dataHist() const
Definition: SampleTailer.h:96
Name unit() const
Definition: SampleTailer.h:95
int setPedestal(float val)
float m_pedestal
Definition: SampleTailer.h:111
FloatVector m_t
Definition: SampleTailer.h:117
FloatVector m_d
Definition: SampleTailer.h:115
int setBeta(float val, bool cancelSignal)
const FloatVector & data() const
Definition: SampleTailer.h:88
float decayTime() const
Definition: SampleTailer.h:83
const FloatVector & signal() const
Definition: SampleTailer.h:90
float beta() const
Definition: SampleTailer.h:84
int setDecayTime(float val, bool cancelSignal)
int setTail0(float val)
int setPedestalVector(const FloatVector *pval)
float tail(Index isam) const
Definition: SampleTailer.h:93
pval
Definition: tracks.py:168
std::string Name
Definition: SampleTailer.h:43
float signal(Index isam) const
Definition: SampleTailer.h:91
FloatVector m_s
Definition: SampleTailer.h:116
float m_decayTime
Definition: SampleTailer.h:108
float pedestal() const
Definition: SampleTailer.h:86
float alpha() const
Definition: SampleTailer.h:85
const FloatVector & tail() const
Definition: SampleTailer.h:92
float tail0() const
Definition: SampleTailer.h:87
int setAlpha(float val)
unsigned int Index
Definition: SampleTailer.h:41
int setUnit(Name val)
Definition: SampleTailer.h:68
int setDataZero(Index nsam)
Index size() const
Definition: SampleTailer.h:94
SampleTailer(float a_decayTime)
TH1 * tailHist() const
Definition: SampleTailer.h:98