Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:08:29

0001 #include "DQM/SiStripCommissioningAnalysis/interface/ApvTimingAlgorithm.h"
0002 #include "CondFormats/SiStripObjects/interface/ApvTimingAnalysis.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 ApvTimingAlgorithm::ApvTimingAlgorithm(const edm::ParameterSet& pset, ApvTimingAnalysis* const anal)
0017     : CommissioningAlgorithm(anal), histo_(nullptr, "") {
0018   ;
0019 }
0020 
0021 // ----------------------------------------------------------------------------
0022 //
0023 void ApvTimingAlgorithm::extract(const std::vector<TH1*>& histos) {
0024   if (!anal()) {
0025     edm::LogWarning(mlCommissioning_) << "[ApvTimingAlgorithm::" << __func__ << "]"
0026                                       << " NULL pointer to Analysis object!";
0027     return;
0028   }
0029 
0030   // Check number of histograms
0031   if (histos.size() != 1) {
0032     anal()->addErrorCode(sistrip::numberOfHistos_);
0033   }
0034 
0035   // Extract FED key from histo title
0036   if (!histos.empty()) {
0037     anal()->fedKey(extractFedKey(histos.front()));
0038   }
0039 
0040   // Extract histograms
0041   std::vector<TH1*>::const_iterator ihis = histos.begin();
0042   for (; ihis != histos.end(); ihis++) {
0043     // Check for NULL pointer
0044     if (!(*ihis)) {
0045       continue;
0046     }
0047 
0048     // Check name
0049     SiStripHistoTitle title((*ihis)->GetName());
0050     if (title.runType() != sistrip::APV_TIMING) {
0051       anal()->addErrorCode(sistrip::unexpectedTask_);
0052       continue;
0053     }
0054 
0055     // Extract timing histo
0056     histo_.first = *ihis;
0057     histo_.second = (*ihis)->GetName();
0058   }
0059 }
0060 
0061 // ----------------------------------------------------------------------------
0062 //
0063 void ApvTimingAlgorithm::analyse() {
0064   if (!anal()) {
0065     edm::LogWarning(mlCommissioning_) << "[ApvTimingAlgorithm::" << __func__ << "]"
0066                                       << " NULL pointer to base Analysis object!";
0067     return;
0068   }
0069 
0070   CommissioningAnalysis* tmp = const_cast<CommissioningAnalysis*>(anal());
0071   ApvTimingAnalysis* anal = dynamic_cast<ApvTimingAnalysis*>(tmp);
0072   if (!anal) {
0073     edm::LogWarning(mlCommissioning_) << "[ApvTimingAlgorithm::" << __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   TProfile* histo = dynamic_cast<TProfile*>(histo_.first);
0084   if (!histo) {
0085     anal->addErrorCode(sistrip::nullPtr_);
0086     return;
0087   }
0088 
0089   // Transfer histogram contents/errors/stats to containers
0090   float max = -1. * sistrip::invalid_;
0091   float min = 1. * sistrip::invalid_;
0092   uint16_t nbins = static_cast<uint16_t>(histo->GetNbinsX());
0093   std::vector<float> bin_contents;
0094   std::vector<float> bin_errors;
0095   std::vector<float> bin_entries;
0096   bin_contents.reserve(nbins);
0097   bin_errors.reserve(nbins);
0098   bin_entries.reserve(nbins);
0099   for (uint16_t ibin = 0; ibin < nbins; ibin++) {
0100     bin_contents.push_back(histo->GetBinContent(ibin + 1));
0101     bin_errors.push_back(histo->GetBinError(ibin + 1));
0102     bin_entries.push_back(histo->GetBinEntries(ibin + 1));
0103     if (bin_entries[ibin]) {
0104       if (bin_contents[ibin] > max) {
0105         max = bin_contents[ibin];
0106       }
0107       if (bin_contents[ibin] < min) {
0108         min = bin_contents[ibin];
0109       }
0110     }
0111   }
0112   if (bin_contents.size() < 100) {
0113     anal->addErrorCode(sistrip::numberOfBins_);
0114     return;
0115   }
0116 
0117   // Calculate range (max-min) and threshold level (range/2)
0118   float threshold = min + (max - min) / 2.;
0119   anal->base_ = min;
0120   anal->peak_ = max;
0121   anal->height_ = max - min;
0122   if (max - min < ApvTimingAnalysis::tickMarkHeightThreshold_) {
0123     anal->addErrorCode(sistrip::smallDataRange_);
0124     return;
0125   }
0126 
0127   // Associate samples with either "tick mark" or "baseline"
0128   std::vector<float> tick;
0129   std::vector<float> base;
0130   for (uint16_t ibin = 0; ibin < nbins; ibin++) {
0131     if (bin_entries[ibin]) {
0132       if (bin_contents[ibin] < threshold) {
0133         base.push_back(bin_contents[ibin]);
0134       } else {
0135         tick.push_back(bin_contents[ibin]);
0136       }
0137     }
0138   }
0139 
0140   // Find median level of tick mark and baseline
0141   float tickmark = 0.;
0142   float baseline = 0.;
0143   sort(tick.begin(), tick.end());
0144   sort(base.begin(), base.end());
0145   if (!tick.empty()) {
0146     tickmark = tick[tick.size() % 2 ? tick.size() / 2 : tick.size() / 2];
0147   }
0148   if (!base.empty()) {
0149     baseline = base[base.size() % 2 ? base.size() / 2 : base.size() / 2];
0150   }
0151   anal->base_ = baseline;
0152   anal->peak_ = tickmark;
0153   anal->height_ = tickmark - baseline;
0154   if (tickmark - baseline < ApvTimingAnalysis::tickMarkHeightThreshold_) {
0155     anal->addErrorCode(sistrip::smallTickMarkHeight_);
0156     return;
0157   }
0158 
0159   // Find rms spread in "baseline" samples
0160   float mean = 0.;
0161   float mean2 = 0.;
0162   for (uint16_t ibin = 0; ibin < base.size(); ibin++) {
0163     mean += base[ibin];
0164     mean2 += base[ibin] * base[ibin];
0165   }
0166   if (!base.empty()) {
0167     mean = mean / base.size();
0168     mean2 = mean2 / base.size();
0169   } else {
0170     mean = 0.;
0171     mean2 = 0.;
0172   }
0173   float baseline_rms = sqrt(fabs(mean2 - mean * mean));
0174 
0175   // Find rising edges (derivative across two bins > threshold)
0176   std::map<uint16_t, float> edges;
0177   for (uint16_t ibin = 1; ibin < nbins - 1; ibin++) {
0178     if (bin_entries[ibin + 1] && bin_entries[ibin - 1]) {
0179       float derivative = bin_contents[ibin + 1] - bin_contents[ibin - 1];
0180       if (derivative > 3. * baseline_rms) {
0181         edges[ibin] = derivative;
0182       }
0183     }
0184   }
0185   if (edges.empty()) {
0186     anal->addErrorCode(sistrip::noRisingEdges_);
0187     return;
0188   }
0189 
0190   // Iterate through "edges" map
0191   uint16_t max_derivative_bin = sistrip::invalid_;
0192   float max_derivative = -1. * sistrip::invalid_;
0193 
0194   bool found = false;
0195   std::map<uint16_t, float>::iterator iter = edges.begin();
0196   while (!found && iter != edges.end()) {
0197     // Iterate through 50 subsequent samples
0198     bool valid = true;
0199     for (uint16_t ii = 0; ii < 50; ii++) {
0200       uint16_t bin = iter->first + ii;
0201 
0202       // Calc local derivative
0203       float temp = 0.;
0204       if (static_cast<uint32_t>(bin) < 1 || static_cast<uint32_t>(bin + 1) >= nbins) {
0205         valid = false;  //@@ require complete plateau is found within histo
0206         anal->addErrorCode(sistrip::incompletePlateau_);
0207         continue;
0208       }
0209       temp = bin_contents[bin + 1] - bin_contents[bin - 1];
0210 
0211       // Store max derivative
0212       if (temp > max_derivative) {
0213         max_derivative = temp;
0214         max_derivative_bin = bin;
0215       }
0216 
0217       // Check if samples following edge are all "high"
0218       if (ii > 10 && ii < 40 && bin_entries[bin] && bin_contents[bin] < baseline + 5. * baseline_rms) {
0219         valid = false;
0220       }
0221     }
0222 
0223     // Break from loop if tick mark found
0224     if (valid) {
0225       found = true;
0226     }
0227 
0228     /*
0229     else {
0230       max_derivative = -1.*sistrip::invalid_;
0231       max_derivative_bin = sistrip::invalid_;
0232       //edges.erase(iter);
0233       anal->addErrorCode(sistrip::rejectedCandidate_);
0234     }
0235     */
0236 
0237     iter++;  // next candidate
0238   }
0239 
0240   if (!found) {  //Try tick mark recovery
0241 
0242     max_derivative_bin = sistrip::invalid_;
0243     max_derivative = -1. * sistrip::invalid_;
0244 
0245     // Find rising edges_r (derivative_r across five bins > threshold)
0246     std::map<uint16_t, float> edges_r;
0247     for (uint16_t ibin_r = 1; ibin_r < nbins - 1; ibin_r++) {
0248       if (bin_entries[ibin_r + 4] && bin_entries[ibin_r + 3] && bin_entries[ibin_r + 2] && bin_entries[ibin_r + 1] &&
0249           bin_entries[ibin_r] && bin_entries[ibin_r - 1]) {
0250         float derivative_r = bin_contents[ibin_r + 1] - bin_contents[ibin_r - 1];
0251         float derivative_r1 = bin_contents[ibin_r + 1] - bin_contents[ibin_r];
0252         float derivative_r2 = bin_contents[ibin_r + 2] - bin_contents[ibin_r + 1];
0253         float derivative_r3 = bin_contents[ibin_r + 3] - bin_contents[ibin_r + 2];
0254 
0255         if (derivative_r > 3. * baseline_rms && derivative_r1 > 1. * baseline_rms &&
0256             derivative_r2 > 1. * baseline_rms && derivative_r3 > 1. * baseline_rms) {
0257           edges_r[ibin_r] = derivative_r;
0258         }
0259       }
0260     }
0261     if (edges_r.empty()) {
0262       anal->addErrorCode(sistrip::noRisingEdges_);
0263       return;
0264     }
0265 
0266     // Iterate through "edges_r" map
0267     float max_derivative_r = -1. * sistrip::invalid_;
0268 
0269     bool found_r = false;
0270     std::map<uint16_t, float>::iterator iter_r = edges_r.begin();
0271     while (!found_r && iter_r != edges_r.end()) {
0272       // Iterate through 50 subsequent samples
0273       bool valid_r = true;
0274       int lowpointcount_r = 0;
0275       const int lowpointallow_r = 25;  //Number of points allowed to fall below threshhold w/o invalidating tick mark
0276       for (uint16_t ii_r = 0; ii_r < 50; ii_r++) {
0277         uint16_t bin_r = iter_r->first + ii_r;
0278 
0279         // Calc local derivative_r
0280         float temp_r = 0.;
0281         if (static_cast<uint32_t>(bin_r) < 1 || static_cast<uint32_t>(bin_r + 1) >= nbins) {
0282           valid_r = false;  //@@ require complete plateau is found_r within histo
0283           anal->addErrorCode(sistrip::incompletePlateau_);
0284           continue;
0285         }
0286         temp_r = bin_contents[bin_r + 1] - bin_contents[bin_r - 1];
0287 
0288         // Store max derivative_r
0289         if (temp_r > max_derivative_r && ii_r < 10) {
0290           max_derivative_r = temp_r;
0291           max_derivative = temp_r;
0292           max_derivative_bin = bin_r;
0293         }
0294 
0295         // Check if majority of samples following edge are all "high"
0296         if (ii_r > 10 && ii_r < 40 && bin_entries[bin_r] && bin_contents[bin_r] < baseline + 5. * baseline_rms) {
0297           lowpointcount_r++;
0298           if (lowpointcount_r > lowpointallow_r) {
0299             valid_r = false;
0300           }
0301         }
0302       }
0303 
0304       // Break from loop if recovery tick mark found
0305       if (valid_r) {
0306         found_r = true;
0307         found = true;
0308         anal->addErrorCode(sistrip::tickMarkRecovered_);
0309       } else {
0310         max_derivative_r = -1. * sistrip::invalid_;
0311         max_derivative = -1. * sistrip::invalid_;
0312         max_derivative_bin = sistrip::invalid_;
0313         //edges_r.erase(iter_r);
0314         anal->addErrorCode(sistrip::rejectedCandidate_);
0315       }
0316 
0317       iter_r++;  // next candidate
0318     }
0319   }  //End tick mark recovery
0320 
0321   // Record time monitorable and check tick mark height
0322   if (max_derivative_bin <= sistrip::valid_) {
0323     anal->time_ = max_derivative_bin * 25. / 24.;
0324     if (anal->height_ < ApvTimingAnalysis::tickMarkHeightThreshold_) {
0325       anal->addErrorCode(sistrip::tickMarkBelowThresh_);
0326     }
0327   } else {
0328     anal->addErrorCode(sistrip::missingTickMark_);
0329   }
0330 }