CMS 3D CMS Logo

TotemTimingRecHitProducerAlgorithm.cc
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * This is a part of CTPPS offline software.
4  * Authors:
5  * Laurent Forthomme (laurent.forthomme@cern.ch)
6  * Nicola Minafra
7  *
8  ****************************************************************************/
9 
11 
12 //----------------------------------------------------------------------------------------------------
13 
14 const float TotemTimingRecHitProducerAlgorithm::SINC_COEFFICIENT = std::acos(-1) * 2 / 7.8;
15 
16 //----------------------------------------------------------------------------------------------------
17 
19  const edm::ParameterSet &iConfig)
20  : sampicConversions_(iConfig.getParameter<std::string>("calibrationFile")),
21  baselinePoints_(iConfig.getParameter<int>("baselinePoints")),
22  saturationLimit_(iConfig.getParameter<double>("saturationLimit")),
23  cfdFraction_(iConfig.getParameter<double>("cfdFraction")),
24  smoothingPoints_(iConfig.getParameter<int>("smoothingPoints")),
25  lowPassFrequency_(iConfig.getParameter<double>("lowPassFrequency")),
26  hysteresis_(iConfig.getParameter<double>("hysteresis")) {}
27 
31  for (const auto &vec : input) {
32  const TotemTimingDetId detid(vec.detId());
33 
34  float x_pos = 0, y_pos = 0, z_pos = 0, x_width = 0, y_width = 0,
35  z_width = 0;
36 
37  // retrieve the geometry element associated to this DetID ( if present )
38  const DetGeomDesc *det = geom->getSensorNoThrow(detid);
39 
40  if (det) {
41  x_pos = det->translation().x(), y_pos = det->translation().y();
42  if (det->parents().empty())
43  edm::LogWarning("TotemTimingRecHitProducerAlgorithm")
44  << "The geometry element for " << detid
45  << " has no parents. Check the geometry hierarchy!";
46  else
47  z_pos = det->parents()[det->parents().size() - 1]
48  .absTranslation()
49  .z(); // retrieve the plane position;
50 
51  x_width = 2.0 * det->params().at(0), // parameters stand for half the size
52  y_width = 2.0 * det->params().at(1),
53  z_width = 2.0 * det->params().at(2);
54  } else
55  edm::LogWarning("TotemTimingRecHitProducerAlgorithm")
56  << "Failed to retrieve a sensor for " << detid;
57 
58  edm::DetSet<TotemTimingRecHit> &rec_hits = output.find_or_insert(detid);
59 
60  for (const auto &digi : vec) {
61  const float triggerCellTimeInstant(
63  const std::vector<float> time(sampicConversions_.getTimeSamples(digi));
64  std::vector<float> data(sampicConversions_.getVoltSamples(digi));
65 
66  auto max_it = std::max_element(data.begin(), data.end());
67 
68  RegressionResults baselineRegression =
70 
71  // remove baseline
72  std::vector<float> dataCorrected(data.size());
73  for (unsigned int i = 0; i < data.size(); ++i)
74  dataCorrected.at(i) = data.at(i) -
75  (baselineRegression.q + baselineRegression.m * time.at(i));
76  auto max_corrected_it =
77  std::max_element(dataCorrected.begin(), dataCorrected.end());
78 
80  if (*max_it < saturationLimit_)
81  t = constantFractionDiscriminator(time, dataCorrected);
82 
84 
86  x_pos, x_width, y_pos, y_width, z_pos, z_width, // spatial information
87  t, triggerCellTimeInstant, .0, *max_corrected_it,
88  baselineRegression.rms, mode_));
89  }
90  }
91 }
92 
95  const std::vector<float> &time, const std::vector<float> &data,
96  const unsigned int start_at, const unsigned int points) const {
98  if (time.size() != data.size()) {
99  return results;
100  }
101  if (start_at > time.size()) {
102  return results;
103  }
104  unsigned int stop_at = std::min((unsigned int)time.size(), start_at + points);
105  unsigned int realPoints = stop_at - start_at;
106  auto t_begin = std::next(time.begin(), start_at);
107  auto t_end = std::next(time.begin(), stop_at);
108  auto d_begin = std::next(data.begin(), start_at);
109  auto d_end = std::next(data.begin(), stop_at);
110 
111  float sx = .0;
112  std::for_each(t_begin, t_end, [&](float value) { sx += value; });
113  float sxx = .0;
114  std::for_each(t_begin, t_end, [&](float value) { sxx += value * value; });
115 
116  float sy = .0;
117  std::for_each(d_begin, d_end, [&](float value) { sy += value; });
118  float syy = .0;
119  std::for_each(d_begin, d_end, [&](float value) { syy += value * value; });
120 
121  float sxy = .0;
122  for (unsigned int i = 0; i < realPoints; ++i)
123  sxy += (time.at(i)) * (data.at(i));
124 
125  // y = mx + q
126  results.m = (sxy * realPoints - sx * sy) / (sxx * realPoints - sx * sx);
127  results.q = sy / realPoints - results.m * sx / realPoints;
128 
129  float correctedSyy = .0;
130  for (unsigned int i = 0; i < realPoints; ++i)
131  correctedSyy += pow(data.at(i) - (results.m * time.at(i) + results.q), 2);
132  results.rms = sqrt(correctedSyy / realPoints);
133 
134  return results;
135 }
136 
138  const std::vector<float> &data, const float &threshold) const {
139  int threholdCrossingIndex = -1;
140  bool above = false;
141  bool lockForHysteresis = false;
142 
143  for (unsigned int i = 0; i < data.size(); ++i) {
144  // Look for first edge
145  if (!above && !lockForHysteresis && data.at(i) > threshold) {
146  threholdCrossingIndex = i;
147  above = true;
148  lockForHysteresis = true;
149  }
150  if (above && lockForHysteresis) // NOTE: not else if!, "above" can be set in
151  // the previous if
152  {
153  // Lock until above threshold_+hysteresis
154  if (lockForHysteresis && data.at(i) > threshold + hysteresis_) {
155  lockForHysteresis = false;
156  }
157  // Ignore noise peaks
158  if (lockForHysteresis && data.at(i) < threshold) {
159  above = false;
160  lockForHysteresis = false;
161  threholdCrossingIndex = -1; // assigned because of noise
162  }
163  }
164  }
165 
166  return threholdCrossingIndex;
167 }
168 
170  const std::vector<float> &time, const std::vector<float> &data) {
171  std::vector<float> dataProcessed(data);
172  if (lowPassFrequency_ != 0) {
173  // Smoothing
174  for (int i = 0; i < (int)data.size(); ++i) {
175  for (int j = -smoothingPoints_ / 2;
176  j <= +smoothingPoints_ / 2; ++j) {
177  if ((i + j) >= 0 && (i + j) < (int)data.size() && j != 0) {
178  float x = SINC_COEFFICIENT * lowPassFrequency_ * j;
179  dataProcessed.at(i) += data.at(i + j) * std::sin(x) / x;
180  }
181  }
182  }
183  }
184  auto max_it = std::max_element(dataProcessed.begin(), dataProcessed.end());
185  float max = *max_it;
186 
187  float threshold = cfdFraction_ * max;
188  int indexOfThresholdCrossing = fastDiscriminator(dataProcessed, threshold);
189 
191  if (indexOfThresholdCrossing >= baselinePoints_ &&
192  indexOfThresholdCrossing < (int)time.size()) {
193  t = (time.at(indexOfThresholdCrossing - 1) -
194  time.at(indexOfThresholdCrossing)) /
195  (dataProcessed.at(indexOfThresholdCrossing - 1) -
196  dataProcessed.at(indexOfThresholdCrossing)) *
197  (threshold - dataProcessed.at(indexOfThresholdCrossing)) +
198  time.at(indexOfThresholdCrossing);
199  }
200 
201  return t;
202 }
void push_back(const T &t)
Definition: DetSet.h:68
std::vector< float > getTimeSamples(const TotemTimingDigi &digi) const
const DetGeomDesc * getSensorNoThrow(unsigned int id) const
std::vector< float > getVoltSamples(const TotemTimingDigi &digi) const
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
const float getTriggerTime(const TotemTimingDigi &digi) const
static std::string const input
Definition: EdmProvDump.cc:45
reference find_or_insert(det_id_type id)
Definition: DetSetVector.h:254
float constantFractionDiscriminator(const std::vector< float > &time, const std::vector< float > &data)
T sqrt(T t)
Definition: SSEVec.h:18
Geometrical description of a sensor.
Definition: DetGeomDesc.h:37
virtual std::vector< DDExpandedNode > parents() const
returns all the components below
Definition: DetGeomDesc.h:67
DDTranslation translation() const
Definition: DetGeomDesc.h:84
TotemTimingRecHitProducerAlgorithm(const edm::ParameterSet &conf)
Definition: value.py:1
T min(T a, T b)
Definition: MathUtil.h:58
RegressionResults simplifiedLinearRegression(const std::vector< float > &time, const std::vector< float > &data, const unsigned int start_at, const unsigned int points) const
The manager class for TOTEM RP geometry.
Definition: CTPPSGeometry.h:33
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
std::vector< double > params() const
Definition: DetGeomDesc.h:89
void build(const CTPPSGeometry *, const edm::DetSetVector< TotemTimingDigi > &, edm::DetSetVector< TotemTimingRecHit > &)
int fastDiscriminator(const std::vector< float > &data, const float &threshold) const
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
Detector ID class for CTPPS Totem Timing detectors. Bits [19:31] : Assigend in CTPPSDetId Calss Bits ...