CMS 3D CMS Logo

ApvTimingAlgorithm.cc
Go to the documentation of this file.
6 #include "TProfile.h"
7 #include "TH1.h"
8 #include <iostream>
9 #include <iomanip>
10 #include <cmath>
11 
12 using namespace sistrip;
13 
14 // ----------------------------------------------------------------------------
15 //
17  : CommissioningAlgorithm(anal), histo_(nullptr, "") {
18  ;
19 }
20 
21 // ----------------------------------------------------------------------------
22 //
23 void ApvTimingAlgorithm::extract(const std::vector<TH1*>& histos) {
24  if (!anal()) {
25  edm::LogWarning(mlCommissioning_) << "[ApvTimingAlgorithm::" << __func__ << "]"
26  << " NULL pointer to Analysis object!";
27  return;
28  }
29 
30  // Check number of histograms
31  if (histos.size() != 1) {
33  }
34 
35  // Extract FED key from histo title
36  if (!histos.empty()) {
37  anal()->fedKey(extractFedKey(histos.front()));
38  }
39 
40  // Extract histograms
41  std::vector<TH1*>::const_iterator ihis = histos.begin();
42  for (; ihis != histos.end(); ihis++) {
43  // Check for NULL pointer
44  if (!(*ihis)) {
45  continue;
46  }
47 
48  // Check name
49  SiStripHistoTitle title((*ihis)->GetName());
50  if (title.runType() != sistrip::APV_TIMING) {
52  continue;
53  }
54 
55  // Extract timing histo
56  histo_.first = *ihis;
57  histo_.second = (*ihis)->GetName();
58  }
59 }
60 
61 // ----------------------------------------------------------------------------
62 //
64  if (!anal()) {
65  edm::LogWarning(mlCommissioning_) << "[ApvTimingAlgorithm::" << __func__ << "]"
66  << " NULL pointer to base Analysis object!";
67  return;
68  }
69 
71  ApvTimingAnalysis* anal = dynamic_cast<ApvTimingAnalysis*>(tmp);
72  if (!anal) {
73  edm::LogWarning(mlCommissioning_) << "[ApvTimingAlgorithm::" << __func__ << "]"
74  << " NULL pointer to derived Analysis object!";
75  return;
76  }
77 
78  if (!histo_.first) {
80  return;
81  }
82 
83  TProfile* histo = dynamic_cast<TProfile*>(histo_.first);
84  if (!histo) {
86  return;
87  }
88 
89  // Transfer histogram contents/errors/stats to containers
90  float max = -1. * sistrip::invalid_;
91  float min = 1. * sistrip::invalid_;
92  uint16_t nbins = static_cast<uint16_t>(histo->GetNbinsX());
93  std::vector<float> bin_contents;
94  std::vector<float> bin_errors;
95  std::vector<float> bin_entries;
96  bin_contents.reserve(nbins);
97  bin_errors.reserve(nbins);
98  bin_entries.reserve(nbins);
99  for (uint16_t ibin = 0; ibin < nbins; ibin++) {
100  bin_contents.push_back(histo->GetBinContent(ibin + 1));
101  bin_errors.push_back(histo->GetBinError(ibin + 1));
102  bin_entries.push_back(histo->GetBinEntries(ibin + 1));
103  if (bin_entries[ibin]) {
104  if (bin_contents[ibin] > max) {
105  max = bin_contents[ibin];
106  }
107  if (bin_contents[ibin] < min) {
108  min = bin_contents[ibin];
109  }
110  }
111  }
112  if (bin_contents.size() < 100) {
114  return;
115  }
116 
117  // Calculate range (max-min) and threshold level (range/2)
118  float threshold = min + (max - min) / 2.;
119  anal->base_ = min;
120  anal->peak_ = max;
121  anal->height_ = max - min;
124  return;
125  }
126 
127  // Associate samples with either "tick mark" or "baseline"
128  std::vector<float> tick;
129  std::vector<float> base;
130  for (uint16_t ibin = 0; ibin < nbins; ibin++) {
131  if (bin_entries[ibin]) {
132  if (bin_contents[ibin] < threshold) {
133  base.push_back(bin_contents[ibin]);
134  } else {
135  tick.push_back(bin_contents[ibin]);
136  }
137  }
138  }
139 
140  // Find median level of tick mark and baseline
141  float tickmark = 0.;
142  float baseline = 0.;
143  sort(tick.begin(), tick.end());
144  sort(base.begin(), base.end());
145  if (!tick.empty()) {
146  tickmark = tick[tick.size() % 2 ? tick.size() / 2 : tick.size() / 2];
147  }
148  if (!base.empty()) {
149  baseline = base[base.size() % 2 ? base.size() / 2 : base.size() / 2];
150  }
151  anal->base_ = baseline;
152  anal->peak_ = tickmark;
153  anal->height_ = tickmark - baseline;
154  if (tickmark - baseline < ApvTimingAnalysis::tickMarkHeightThreshold_) {
156  return;
157  }
158 
159  // Find rms spread in "baseline" samples
160  float mean = 0.;
161  float mean2 = 0.;
162  for (uint16_t ibin = 0; ibin < base.size(); ibin++) {
163  mean += base[ibin];
164  mean2 += base[ibin] * base[ibin];
165  }
166  if (!base.empty()) {
167  mean = mean / base.size();
168  mean2 = mean2 / base.size();
169  } else {
170  mean = 0.;
171  mean2 = 0.;
172  }
173  float baseline_rms = sqrt(fabs(mean2 - mean * mean));
174 
175  // Find rising edges (derivative across two bins > threshold)
176  std::map<uint16_t, float> edges;
177  for (uint16_t ibin = 1; ibin < nbins - 1; ibin++) {
178  if (bin_entries[ibin + 1] && bin_entries[ibin - 1]) {
179  float derivative = bin_contents[ibin + 1] - bin_contents[ibin - 1];
180  if (derivative > 3. * baseline_rms) {
181  edges[ibin] = derivative;
182  }
183  }
184  }
185  if (edges.empty()) {
187  return;
188  }
189 
190  // Iterate through "edges" map
191  uint16_t max_derivative_bin = sistrip::invalid_;
192  float max_derivative = -1. * sistrip::invalid_;
193 
194  bool found = false;
195  std::map<uint16_t, float>::iterator iter = edges.begin();
196  while (!found && iter != edges.end()) {
197  // Iterate through 50 subsequent samples
198  bool valid = true;
199  for (uint16_t ii = 0; ii < 50; ii++) {
200  uint16_t bin = iter->first + ii;
201 
202  // Calc local derivative
203  float temp = 0.;
204  if (static_cast<uint32_t>(bin) < 1 || static_cast<uint32_t>(bin + 1) >= nbins) {
205  valid = false; //@@ require complete plateau is found within histo
207  continue;
208  }
209  temp = bin_contents[bin + 1] - bin_contents[bin - 1];
210 
211  // Store max derivative
212  if (temp > max_derivative) {
213  max_derivative = temp;
214  max_derivative_bin = bin;
215  }
216 
217  // Check if samples following edge are all "high"
218  if (ii > 10 && ii < 40 && bin_entries[bin] && bin_contents[bin] < baseline + 5. * baseline_rms) {
219  valid = false;
220  }
221  }
222 
223  // Break from loop if tick mark found
224  if (valid) {
225  found = true;
226  }
227 
228  /*
229  else {
230  max_derivative = -1.*sistrip::invalid_;
231  max_derivative_bin = sistrip::invalid_;
232  //edges.erase(iter);
233  anal->addErrorCode(sistrip::rejectedCandidate_);
234  }
235  */
236 
237  iter++; // next candidate
238  }
239 
240  if (!found) { //Try tick mark recovery
241 
242  max_derivative_bin = sistrip::invalid_;
243  max_derivative = -1. * sistrip::invalid_;
244 
245  // Find rising edges_r (derivative_r across five bins > threshold)
246  std::map<uint16_t, float> edges_r;
247  for (uint16_t ibin_r = 1; ibin_r < nbins - 1; ibin_r++) {
248  if (bin_entries[ibin_r + 4] && bin_entries[ibin_r + 3] && bin_entries[ibin_r + 2] && bin_entries[ibin_r + 1] &&
249  bin_entries[ibin_r] && bin_entries[ibin_r - 1]) {
250  float derivative_r = bin_contents[ibin_r + 1] - bin_contents[ibin_r - 1];
251  float derivative_r1 = bin_contents[ibin_r + 1] - bin_contents[ibin_r];
252  float derivative_r2 = bin_contents[ibin_r + 2] - bin_contents[ibin_r + 1];
253  float derivative_r3 = bin_contents[ibin_r + 3] - bin_contents[ibin_r + 2];
254 
255  if (derivative_r > 3. * baseline_rms && derivative_r1 > 1. * baseline_rms &&
256  derivative_r2 > 1. * baseline_rms && derivative_r3 > 1. * baseline_rms) {
257  edges_r[ibin_r] = derivative_r;
258  }
259  }
260  }
261  if (edges_r.empty()) {
263  return;
264  }
265 
266  // Iterate through "edges_r" map
267  float max_derivative_r = -1. * sistrip::invalid_;
268 
269  bool found_r = false;
270  std::map<uint16_t, float>::iterator iter_r = edges_r.begin();
271  while (!found_r && iter_r != edges_r.end()) {
272  // Iterate through 50 subsequent samples
273  bool valid_r = true;
274  int lowpointcount_r = 0;
275  const int lowpointallow_r = 25; //Number of points allowed to fall below threshhold w/o invalidating tick mark
276  for (uint16_t ii_r = 0; ii_r < 50; ii_r++) {
277  uint16_t bin_r = iter_r->first + ii_r;
278 
279  // Calc local derivative_r
280  float temp_r = 0.;
281  if (static_cast<uint32_t>(bin_r) < 1 || static_cast<uint32_t>(bin_r + 1) >= nbins) {
282  valid_r = false; //@@ require complete plateau is found_r within histo
284  continue;
285  }
286  temp_r = bin_contents[bin_r + 1] - bin_contents[bin_r - 1];
287 
288  // Store max derivative_r
289  if (temp_r > max_derivative_r && ii_r < 10) {
290  max_derivative_r = temp_r;
291  max_derivative = temp_r;
292  max_derivative_bin = bin_r;
293  }
294 
295  // Check if majority of samples following edge are all "high"
296  if (ii_r > 10 && ii_r < 40 && bin_entries[bin_r] && bin_contents[bin_r] < baseline + 5. * baseline_rms) {
297  lowpointcount_r++;
298  if (lowpointcount_r > lowpointallow_r) {
299  valid_r = false;
300  }
301  }
302  }
303 
304  // Break from loop if recovery tick mark found
305  if (valid_r) {
306  found_r = true;
307  found = true;
309  } else {
310  max_derivative_r = -1. * sistrip::invalid_;
311  max_derivative = -1. * sistrip::invalid_;
312  max_derivative_bin = sistrip::invalid_;
313  //edges_r.erase(iter_r);
315  }
316 
317  iter_r++; // next candidate
318  }
319  } //End tick mark recovery
320 
321  // Record time monitorable and check tick mark height
322  if (max_derivative_bin <= sistrip::valid_) {
323  anal->time_ = max_derivative_bin * 25. / 24.;
326  }
327  } else {
329  }
330 }
CommissioningAnalysis *const anal() const
void extract(const std::vector< TH1 *> &) override
static const char unexpectedTask_[]
Derivative< X, A >::type derivative(const A &_)
Definition: Derivative.h:18
Utility class that holds histogram title.
static const char numberOfHistos_[]
base
Main Program
Definition: newFWLiteAna.py:92
static const float tickMarkHeightThreshold_
static const uint16_t valid_
Definition: Constants.h:17
static const char incompletePlateau_[]
static const char numberOfBins_[]
sistrip classes
uint32_t extractFedKey(const TH1 *const)
static const char missingTickMark_[]
static const char rejectedCandidate_[]
static const char mlCommissioning_[]
static const char tickMarkRecovered_[]
T sqrt(T t)
Definition: SSEVec.h:19
static const char tickMarkBelowThresh_[]
const Histo & histo() const
virtual void addErrorCode(const std::string &error)
const uint32_t & fedKey() const
ii
Definition: cuy.py:589
static const char smallTickMarkHeight_[]
static const uint16_t invalid_
Definition: Constants.h:16
static const char noRisingEdges_[]
static const char smallDataRange_[]
histos
Definition: combine.py:4
void analyse() override
Abstract base for derived classes that provide analysis of commissioning histograms.
Log< level::Warning, false > LogWarning
tmp
align.sh
Definition: createJobs.py:716
Analysis for timing run using APV tick marks.
static const char nullPtr_[]