StickyCodeMetrics.h
Go to the documentation of this file.
1 // StickyCodeMetrics.h
2 //
3 // David Adams
4 // June 2018
5 //
6 // Class to evaluate sticky code metrics from a distribution
7 // of ADC codes presumed to correpond to the same input signal,
8 // e.g. a pedestal or tickmod distrobution.
9 //
10 // The metrics are:
11 //
12 // maxAdc = most common ADC code
13 // maxAdc2 = 2nd most common ADC code
14 // meanAdc = mean ADC
15 // meanAdc2 = mean ADC without the most common code
16 // maxFraction (s1) = fraction of ticks with the most common code
17 // zeroFraction = fraction of ticks with ADC%64=0
18 // oneFraction = fraction of ticks with ADC%64=1
19 // highFraction = fraction of ticks with ADC%64=63
20 // classicFraction (s2) = fraction of ticks with ADC%64 = 0 or 63
21 // fitStatus = status code from fit (0 for OK)
22 // fitMean = mean ADC from fit
23 // fitSigma = ADC sigma from fit
24 // fitExcess = fraction with the most common code above fit
25 //
26 // If a histogram is created, the following are also defined:
27 // hist - pointer to histogram accessed through getHist() or getSharedHist()
28 //
29 // Classic sticky codes are code%64 = 0 or 63
30 //
31 // Caller passes a distribution of ADC codes in one of three forms:
32 // Bin counter - Map with count for each code
33 // Code vector - Vector of codes (order is not used)
34 // Histogram - TH1 with count or likelihood for each code
35 //
36 // The histogram must have one bin per ADC code.
37 
38 #ifndef StickyCodeMetrics_H
39 #define StickyCodeMetrics_H
40 
43 #include <map>
44 #include <memory>
45 
46 class TH1;
47 
49 
50 public:
51 
52  using Index = unsigned int;
53  using BinCounter = std::map<Index, double>;
54  using HistPtr = std::shared_ptr<TH1>;
55  using Name = std::string;
56 
57  // Ctor. No histogram is connfigured.
58  StickyCodeMetrics() = default;
59 
60  // Ctor to configure for histogram with nbin bins with low edge
61  // a multiple of lowbin;
62  StickyCodeMetrics(Name hnam, Name httl, Index nbin, Index lowbin, float sigmaMin, float sigmaMax);
63 
64  // Evaluate a count map.
65  int evaluate(const BinCounter& counts);
66 
67  // Evaluate a vector of ADC codes.
68  int evaluate(const AdcCountVector& adcs);
69 
70  // Evaluate a histogram.
71  // Each histogram bin must correspond to one ADC bin.
72  int evaluate(const TH1* pha);
73 
74  // Clear the data and metrics.
75  void clear();
76 
77  // Metrics.
78  Index nsample() const { return m_nsample; }
79  AdcIndex maxAdc() const { return m_maxAdc; }
80  AdcIndex maxAdc2() const { return m_maxAdc2; }
81  double meanAdc() const { return m_meanAdc; }
82  double meanAdc2() const { return m_meanAdc2; }
83  double maxFraction() const { return m_maxFraction; }
84  double zeroFraction() const { return m_zeroFraction; }
85  double oneFraction() const { return m_oneFraction; }
86  double highFraction() const { return m_highFraction; }
87  double classicFraction() const { return m_zeroFraction + m_highFraction; }
88  int fitStatus() const { return m_fitStatus; }
89  double fitMean() const { return m_fitMean; }
90  double fitSigma() const { return m_fitSigma; }
91  double fitExcess() const { return m_fitExcess; }
92 
93  // Return the ADC bin count map.
94  const BinCounter& getBinCounts() { return m_counts; }
95 
96  // Return the metrics in a data map (typed name-value pairs).
97  // Sets int or float values.
98  // Name is prefix + capitalized metric name, e.g. scmMaxAdc
99  DataMap getMetrics(std::string prefix ="scm") const;
100 
101  // Return the histogram.
102  TH1* getHist() { return getSharedHist().get(); };
103  HistPtr getSharedHist() { return m_ph; }
104 
105  // Display results.
106  void print(std::string prefix ="") const;
107 
108 private:
109 
110  // Configuration.
113  int m_nbin =0;
115  float m_sigmaMin = 0.1;
116  float m_sigmaMax = 100.0;
117 
118  // Metrics.
122  double m_meanAdc;
123  double m_meanAdc2;
129  double m_fitMean;
130  double m_fitSigma;
131  double m_fitExcess;
132 
135 
136  int evaluateMetrics();
137 
138 };
139 
140 #endif
std::shared_ptr< TH1 > HistPtr
std::vector< AdcCount > AdcCountVector
Definition: AdcTypes.h:19
AdcIndex maxAdc2() const
DataMap getMetrics(std::string prefix="scm") const
double meanAdc() const
std::string string
Definition: nybbler.cc:12
int fitStatus() const
double fitExcess() const
counts_as<> counts
Number of ADC counts, represented by signed short int.
Definition: electronics.h:116
unsigned int AdcIndex
Definition: AdcTypes.h:15
const BinCounter & getBinCounts()
double fitMean() const
double oneFraction() const
double classicFraction() const
Index nsample() const
AdcIndex maxAdc() const
std::map< Index, double > BinCounter
double highFraction() const
double maxFraction() const
double fitSigma() const
double zeroFraction() const
double meanAdc2() const
void print(std::string prefix="") const
StickyCodeMetrics()=default
int evaluate(const BinCounter &counts)