File indexing completed on 2024-04-06 12:08:29
0001 #include "DQM/SiStripCommissioningAnalysis/interface/FedTimingAlgorithm.h"
0002 #include "CondFormats/SiStripObjects/interface/FedTimingAnalysis.h"
0003 #include "DataFormats/SiStripCommon/interface/SiStripHistoTitle.h"
0004 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006 #include "TProfile.h"
0007 #include "TH1.h"
0008 #include <iostream>
0009 #include <iomanip>
0010 #include <cmath>
0011
0012 using namespace sistrip;
0013
0014
0015
0016 FedTimingAlgorithm::FedTimingAlgorithm(const edm::ParameterSet& pset, FedTimingAnalysis* const anal)
0017 : CommissioningAlgorithm(anal), histo_(nullptr, "") {
0018 ;
0019 }
0020
0021
0022
0023 void FedTimingAlgorithm::extract(const std::vector<TH1*>& histos) {
0024 if (!anal()) {
0025 edm::LogWarning(mlCommissioning_) << "[FedTimingAlgorithm::" << __func__ << "]"
0026 << " NULL pointer to Analysis object!";
0027 return;
0028 }
0029
0030
0031 if (histos.size() != 1) {
0032 anal()->addErrorCode(sistrip::numberOfHistos_);
0033 }
0034
0035
0036 if (!histos.empty()) {
0037 anal()->fedKey(extractFedKey(histos.front()));
0038 }
0039
0040
0041 std::vector<TH1*>::const_iterator ihis = histos.begin();
0042 for (; ihis != histos.end(); ihis++) {
0043
0044 if (!(*ihis)) {
0045 continue;
0046 }
0047
0048
0049 SiStripHistoTitle title((*ihis)->GetName());
0050 if (title.runType() != sistrip::FED_TIMING) {
0051 anal()->addErrorCode(sistrip::unexpectedTask_);
0052 continue;
0053 }
0054
0055
0056 histo_.first = *ihis;
0057 histo_.second = (*ihis)->GetName();
0058 }
0059 }
0060
0061
0062
0063 void FedTimingAlgorithm::analyse() {
0064 if (!anal()) {
0065 edm::LogWarning(mlCommissioning_) << "[FedTimingAlgorithm::" << __func__ << "]"
0066 << " NULL pointer to base Analysis object!";
0067 return;
0068 }
0069
0070 CommissioningAnalysis* tmp = const_cast<CommissioningAnalysis*>(anal());
0071 FedTimingAnalysis* anal = dynamic_cast<FedTimingAnalysis*>(tmp);
0072 if (!anal) {
0073 edm::LogWarning(mlCommissioning_) << "[FedTimingAlgorithm::" << __func__ << "]"
0074 << " NULL pointer to derived Analysis object!";
0075 return;
0076 }
0077
0078 if (!histo_.first) {
0079 anal->addErrorCode(sistrip::nullPtr_);
0080 return;
0081 }
0082
0083
0084 float max = -1.e9;
0085 float min = 1.e9;
0086 uint16_t nbins = static_cast<uint16_t>(histo_.first->GetNbinsX());
0087 std::vector<float> bin_contents;
0088 std::vector<float> bin_errors;
0089 std::vector<float> bin_entries;
0090 bin_contents.reserve(nbins);
0091 bin_errors.reserve(nbins);
0092 bin_entries.reserve(nbins);
0093 for (uint16_t ibin = 0; ibin < nbins; ibin++) {
0094 bin_contents.push_back(histo_.first->GetBinContent(ibin + 1));
0095 bin_errors.push_back(histo_.first->GetBinError(ibin + 1));
0096
0097 if (bin_entries[ibin]) {
0098 if (bin_contents[ibin] > max) {
0099 max = bin_contents[ibin];
0100 }
0101 if (bin_contents[ibin] < min) {
0102 min = bin_contents[ibin];
0103 }
0104 }
0105 }
0106
0107 if (bin_contents.size() < 100) {
0108 anal->addErrorCode(sistrip::numberOfBins_);
0109 return;
0110 }
0111
0112
0113 float range = max - min;
0114 float threshold = min + range / 2.;
0115 if (range < 50.) {
0116 anal->addErrorCode(sistrip::smallDataRange_);
0117 return;
0118 }
0119
0120
0121
0122
0123 std::vector<float> tick;
0124 std::vector<float> base;
0125 for (uint16_t ibin = 0; ibin < nbins; ibin++) {
0126 if (bin_entries[ibin]) {
0127 if (bin_contents[ibin] < threshold) {
0128 base.push_back(bin_contents[ibin]);
0129 } else {
0130 tick.push_back(bin_contents[ibin]);
0131 }
0132 }
0133 }
0134
0135
0136
0137
0138 float tickmark = 0.;
0139 float baseline = 0.;
0140 sort(tick.begin(), tick.end());
0141 sort(base.begin(), base.end());
0142 if (!tick.empty()) {
0143 tickmark = tick[tick.size() % 2 ? tick.size() / 2 : tick.size() / 2];
0144 }
0145 if (!base.empty()) {
0146 baseline = base[base.size() % 2 ? base.size() / 2 : base.size() / 2];
0147 }
0148
0149
0150 if ((tickmark - baseline) < 50.) {
0151 anal->addErrorCode(sistrip::smallDataRange_);
0152 return;
0153 }
0154
0155
0156 float mean = 0.;
0157 float mean2 = 0.;
0158 for (uint16_t ibin = 0; ibin < base.size(); ibin++) {
0159 mean += base[ibin];
0160 mean2 += base[ibin] * base[ibin];
0161 }
0162 if (!base.empty()) {
0163 mean = mean / base.size();
0164 mean2 = mean2 / base.size();
0165 } else {
0166 mean = 0.;
0167 mean2 = 0.;
0168 }
0169 float baseline_rms = 0.;
0170 if (mean2 > mean * mean) {
0171 baseline_rms = sqrt(mean2 - mean * mean);
0172 } else {
0173 baseline_rms = 0.;
0174 }
0175
0176
0177
0178 std::map<uint16_t, float> edges;
0179 for (uint16_t ibin = 1; ibin < nbins - 1; ibin++) {
0180 if (bin_entries[ibin + 1] && bin_entries[ibin - 1]) {
0181 float derivative = bin_contents[ibin + 1] - bin_contents[ibin - 1];
0182 if (derivative > 5. * baseline_rms) {
0183 edges[ibin] = derivative;
0184
0185
0186 }
0187 }
0188 }
0189
0190
0191 bool found = false;
0192 uint16_t deriv_bin = sistrip::invalid_;
0193 float max_deriv = -1. * sistrip::invalid_;
0194 std::map<uint16_t, float>::iterator iter = edges.begin();
0195 while (!found && iter != edges.end()) {
0196
0197 bool valid = true;
0198 for (uint16_t ii = 0; ii < 50; ii++) {
0199 uint16_t bin = iter->first + ii;
0200
0201
0202 float temp_deriv = 0;
0203 if (static_cast<uint32_t>(bin) < 1 || static_cast<uint32_t>(bin + 1) >= nbins) {
0204 continue;
0205 }
0206 temp_deriv = bin_contents[bin + 1] - bin_contents[bin - 1];
0207
0208
0209 if (temp_deriv > max_deriv) {
0210 max_deriv = temp_deriv;
0211 deriv_bin = bin;
0212 }
0213
0214
0215 if (ii > 10 && ii < 40 && bin_entries[bin] && bin_contents[bin] < baseline + 5 * baseline_rms) {
0216 valid = false;
0217 }
0218 }
0219
0220
0221 if (valid) {
0222 found = true;
0223 } else {
0224 max_deriv = -1. * sistrip::invalid_;
0225 deriv_bin = sistrip::invalid_;
0226 edges.erase(iter);
0227 }
0228
0229 iter++;
0230 }
0231
0232
0233 if (!edges.empty()) {
0234 anal->time_ = deriv_bin;
0235 anal->error_ = 0.;
0236 anal->base_ = baseline;
0237 anal->peak_ = tickmark;
0238 anal->height_ = tickmark - baseline;
0239 } else {
0240 anal->addErrorCode(sistrip::missingTickMark_);
0241 anal->base_ = baseline;
0242 anal->peak_ = tickmark;
0243 anal->height_ = tickmark - baseline;
0244 }
0245 }