|
| 1 | +#include <string> |
| 2 | + |
| 3 | +// ToolAnalysis includes |
| 4 | +#include "PhaseIIADCCalibrator.h" |
| 5 | + |
| 6 | +PhaseIIADCCalibrator::PhaseIIADCCalibrator() : Tool() {} |
| 7 | + |
| 8 | +bool PhaseIIADCCalibrator::Initialise(std::string config_filename, DataModel& data) |
| 9 | +{ |
| 10 | + // Load configuration file variables |
| 11 | + if ( !config_filename.empty() ) m_variables.Initialise(config_filename); |
| 12 | + |
| 13 | + // Assign a transient data pointer |
| 14 | + m_data = &data; |
| 15 | + |
| 16 | + return true; |
| 17 | +} |
| 18 | + |
| 19 | +bool PhaseIIADCCalibrator::Execute() { |
| 20 | + |
| 21 | + if(verbosity) std::cout<<"Initializing Tool PhaseIIADCCalibrator"<<std::endl; |
| 22 | + |
| 23 | + m_variables.Get("verbosity", verbosity); |
| 24 | + m_variables.Get("PCritical", p_critical); |
| 25 | + m_variables.Get("NumBaselineSamples", num_baseline_samples); |
| 26 | + |
| 27 | + // Get a pointer to the ANNIEEvent Store |
| 28 | + auto* annie_event = m_data->Stores["ANNIEEvent"]; |
| 29 | + |
| 30 | + if (!annie_event) { |
| 31 | + Log("Error: The PhaseIIADCCalibrator tool could not find the ANNIEEvent Store", 0, |
| 32 | + verbosity); |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + // Load the map containing the ADC raw waveform data |
| 37 | + std::map<unsigned long, std::vector<Waveform<unsigned short> > > |
| 38 | + raw_waveform_map; |
| 39 | + |
| 40 | + bool got_raw_data = annie_event->Get("RawADCData", raw_waveform_map); |
| 41 | + |
| 42 | + // Check for problems |
| 43 | + if ( !got_raw_data ) { |
| 44 | + Log("Error: The PhaseIIADCCalibrator tool could not find the RawADCData entry", 0, |
| 45 | + verbosity); |
| 46 | + return false; |
| 47 | + } |
| 48 | + else if ( raw_waveform_map.empty() ) { |
| 49 | + Log("Error: The PhaseIIADCCalibrator tool found an empty RawADCData entry", 0, |
| 50 | + verbosity); |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + // Build the calibrated waveforms |
| 55 | + std::map<unsigned long, std::vector<CalibratedADCWaveform<double> > > |
| 56 | + calibrated_waveform_map; |
| 57 | + |
| 58 | + for (const auto& temp_pair : raw_waveform_map) { |
| 59 | + const auto& channel_key = temp_pair.first; |
| 60 | + const auto& raw_waveforms = temp_pair.second; |
| 61 | + |
| 62 | + Log("Making calibrated waveforms for ADC channel " + |
| 63 | + std::to_string(channel_key), 3, verbosity); |
| 64 | + |
| 65 | + calibrated_waveform_map[channel_key] = make_calibrated_waveforms( |
| 66 | + raw_waveforms); |
| 67 | + } |
| 68 | + |
| 69 | + annie_event->Set("CalibratedADCData", calibrated_waveform_map); |
| 70 | + |
| 71 | + return true; |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +bool PhaseIIADCCalibrator::Finalise() { |
| 76 | + return true; |
| 77 | +} |
| 78 | + |
| 79 | +void PhaseIIADCCalibrator::ze3ra_baseline( |
| 80 | + const std::vector< Waveform<unsigned short> >& raw_data, |
| 81 | + double& baseline, double& sigma_baseline, size_t num_baseline_samples) |
| 82 | +{ |
| 83 | + |
| 84 | + // Signal ADC means, variances, and F-distribution probability values |
| 85 | + // ("P") for the first num_baseline_samples from each minibuffer |
| 86 | + // (in Hefty mode) or from each sub-minibuffer (in non-Hefty mode) |
| 87 | + std::vector<double> means; |
| 88 | + std::vector<double> variances; |
| 89 | + std::vector<double> Ps; |
| 90 | + |
| 91 | + // Compute the signal ADC mean and variance for each raw data minibuffer |
| 92 | + for (size_t mb = 0; mb < raw_data.size(); ++mb) { |
| 93 | + const auto& mb_data = raw_data.at(mb).Samples(); |
| 94 | + |
| 95 | + double mean, var; |
| 96 | + ComputeMeanAndVariance(mb_data, mean, var, num_baseline_samples); |
| 97 | + |
| 98 | + means.push_back(mean); |
| 99 | + variances.push_back(var); |
| 100 | + } |
| 101 | + |
| 102 | + // Compute probabilities for the F-distribution test for each minibuffer |
| 103 | + for (size_t j = 0; j < variances.size() - 1; ++j) { |
| 104 | + double sigma2_j = variances.at(j); |
| 105 | + double sigma2_jp1 = variances.at(j + 1); |
| 106 | + double F; |
| 107 | + if (sigma2_j > sigma2_jp1) F = sigma2_j / sigma2_jp1; |
| 108 | + else F = sigma2_jp1 / sigma2_j; |
| 109 | + |
| 110 | + double nu = (num_baseline_samples - 1) / 2.; |
| 111 | + double P = annie_math::Regularized_Beta_Function(1. / (1. + F), nu, nu); |
| 112 | + |
| 113 | + // Two-tailed hypothesis test (we need to exclude unusually small values |
| 114 | + // as well as unusually large ones). The tails have equal sizes, so we |
| 115 | + // may use symmetry and simply multiply our earlier result by 2. |
| 116 | + P *= 2.; |
| 117 | + |
| 118 | + // I've never seen this problem (the numerical values for the regularized |
| 119 | + // beta function that I've checked all fall within [0,1]), but the book |
| 120 | + // Numerical Recipes includes this check in a similar block of code, |
| 121 | + // so I'll add it just in case. |
| 122 | + if (P > 1.) P = 2. - P; |
| 123 | + |
| 124 | + Ps.push_back(P); |
| 125 | + } |
| 126 | + |
| 127 | + // Compute the mean and standard deviation of the baseline signal |
| 128 | + // for this RawChannel using the mean and standard deviation from |
| 129 | + // each minibuffer whose F-distribution probability falls below |
| 130 | + // the critical value. |
| 131 | + baseline = 0.; |
| 132 | + sigma_baseline = 0.; |
| 133 | + double variance_baseline = 0.; |
| 134 | + size_t num_passing = 0; |
| 135 | + for (size_t k = 0; k < Ps.size(); ++k) { |
| 136 | + if (Ps.at(k) > p_critical) { |
| 137 | + ++num_passing; |
| 138 | + baseline += means.at(k); |
| 139 | + variance_baseline += variances.at(k); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + if (num_passing > 1) { |
| 144 | + baseline /= num_passing; |
| 145 | + |
| 146 | + variance_baseline *= static_cast<double>(num_baseline_samples - 1) |
| 147 | + / (num_passing*num_baseline_samples - 1); |
| 148 | + // Now that we've combined the sample variances correctly, take the |
| 149 | + // square root to get the standard deviation |
| 150 | + sigma_baseline = std::sqrt( variance_baseline ); |
| 151 | + } |
| 152 | + else if (num_passing == 1) { |
| 153 | + // We only have one set of sample statistics, so all we need to |
| 154 | + // do is take the square root of the variance to get the standard |
| 155 | + // deviation. |
| 156 | + sigma_baseline = std::sqrt( variance_baseline ); |
| 157 | + } |
| 158 | + else { |
| 159 | + // If none of the minibuffers passed the F-distribution test, |
| 160 | + // choose the one closest to passing (i.e., the one with the largest |
| 161 | + // P-value) and adopt its baseline statistics. For a sufficiently large |
| 162 | + // number of minibuffers (e.g., 40), such a situation should be very rare. |
| 163 | + // TODO: consider changing this approach |
| 164 | + auto max_iter = std::max_element(Ps.cbegin(), Ps.cend()); |
| 165 | + int max_index = std::distance(Ps.cbegin(), max_iter); |
| 166 | + |
| 167 | + baseline = means.at(max_index); |
| 168 | + sigma_baseline = std::sqrt( variances.at(max_index) ); |
| 169 | + } |
| 170 | + |
| 171 | + std::string mb_temp_string = "minibuffer"; |
| 172 | + |
| 173 | + if (verbosity >= 4) { |
| 174 | + for ( size_t x = 0; x < Ps.size(); ++x ) { |
| 175 | + Log(" " + mb_temp_string + " " + std::to_string(x) + ", mean = " |
| 176 | + + std::to_string(means.at(x)) + ", var = " |
| 177 | + + std::to_string(variances.at(x)) + ", p-value = " |
| 178 | + + std::to_string(Ps.at(x)), 4, verbosity); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + Log(std::to_string(num_passing) + " " + mb_temp_string + " pairs passed the" |
| 183 | + " F-test", 3, verbosity); |
| 184 | + Log("Baseline estimate: " + std::to_string(baseline) + " ± " |
| 185 | + + std::to_string(sigma_baseline) + " ADC counts", 3, verbosity); |
| 186 | + |
| 187 | +} |
| 188 | + |
| 189 | +std::vector< CalibratedADCWaveform<double> > |
| 190 | +PhaseIIADCCalibrator::make_calibrated_waveforms( |
| 191 | + const std::vector< Waveform<unsigned short> >& raw_waveforms) |
| 192 | +{ |
| 193 | + |
| 194 | + // Determine the baseline for the set of raw waveforms (assumed to all |
| 195 | + // come from the same readout for the same channel) |
| 196 | + double baseline, sigma_baseline; |
| 197 | + ze3ra_baseline(raw_waveforms, baseline, sigma_baseline, |
| 198 | + num_baseline_samples); |
| 199 | + |
| 200 | + std::vector< CalibratedADCWaveform<double> > calibrated_waveforms; |
| 201 | + for (const auto& raw_waveform : raw_waveforms) { |
| 202 | + |
| 203 | + std::vector<double> cal_data; |
| 204 | + const std::vector<unsigned short>& raw_data = raw_waveform.Samples(); |
| 205 | + |
| 206 | + for (const auto& sample : raw_data) { |
| 207 | + cal_data.push_back((static_cast<double>(sample) - baseline) |
| 208 | + * ADC_TO_VOLT); |
| 209 | + } |
| 210 | + |
| 211 | + calibrated_waveforms.emplace_back(raw_waveform.GetStartTime(), |
| 212 | + cal_data, baseline, sigma_baseline); |
| 213 | + } |
| 214 | + |
| 215 | + return calibrated_waveforms; |
| 216 | +} |
0 commit comments