CMS 3D CMS Logo

HFSimpleTimeCheck.cc
Go to the documentation of this file.
1 #include <cstring>
2 #include <climits>
3 
5 
8 
9 // Phase 1 rechit status bit assignments
11 
12 namespace {
13  inline float build_rechit_time(const float weightedEnergySum,
14  const float weightedSum,
15  const float sum,
16  const unsigned count,
17  const float valueIfNothingWorks,
18  bool* resultComesFromTDC)
19  {
20  if (weightedEnergySum > 0.f)
21  {
22  *resultComesFromTDC = true;
23  return weightedSum/weightedEnergySum;
24  }
25  else if (count)
26  {
27  *resultComesFromTDC = true;
28  return sum/count;
29  }
30  else
31  {
32  *resultComesFromTDC = false;
33  return valueIfNothingWorks;
34  }
35  }
36 }
37 
38 
39 HFSimpleTimeCheck::HFSimpleTimeCheck(const std::pair<float,float> tlimits[2],
41  const unsigned i_soiPhase,
42  const float i_timeShift,
43  const float i_triseIfNoTDC,
44  const float i_tfallIfNoTDC,
45  const bool rejectAllFailures)
46  : soiPhase_(i_soiPhase),
47  timeShift_(i_timeShift),
48  triseIfNoTDC_(i_triseIfNoTDC),
49  tfallIfNoTDC_(i_tfallIfNoTDC),
50  rejectAllFailures_(rejectAllFailures)
51 {
52  tlimits_[0] = tlimits[0];
53  tlimits_[1] = tlimits[1];
54  float* to = &energyWeights_[0][0];
55  const float* from = &energyWeights[0][0];
56  memcpy(to, from, sizeof(energyWeights_));
57 }
58 
60  const unsigned ianode, const HFQIE10Info& anode, bool*) const
61 {
62  // Check if this anode has a dataframe error
63  if (!anode.isDataframeOK())
65 
66  // Check the time limits
67  float trise = anode.timeRising();
68  const bool timeIsKnown = !HcalSpecialTimes::isSpecial(trise);
69  trise += timeShift_;
70  if (timeIsKnown &&
71  tlimits_[ianode].first <= trise && trise <= tlimits_[ianode].second)
72  return HFAnodeStatus::OK;
73  else
75 }
76 
77 unsigned HFSimpleTimeCheck::mapStatusIntoIndex(const unsigned states[2]) const
78 {
79  unsigned eStates[2];
80  eStates[0] = states[0];
81  eStates[1] = states[1];
82  if (!rejectAllFailures_)
83  for (unsigned i=0; i<2; ++i)
84  if (eStates[i] == HFAnodeStatus::FAILED_TIMING ||
85  eStates[i] == HFAnodeStatus::FAILED_OTHER)
86  eStates[i] = HFAnodeStatus::OK;
87  if (eStates[0] == HFAnodeStatus::OK)
88  return eStates[1];
89  else if (eStates[1] == HFAnodeStatus::OK)
90  return HFAnodeStatus::N_POSSIBLE_STATES + eStates[0] - 1;
91  else
92  return UINT_MAX;
93 }
94 
96  const HcalCalibrations& /* calibs */,
97  const bool flaggedBadInDB[2],
98  const bool expectSingleAnodePMT)
99 {
100  HFRecHit rh;
101 
102  // Determine the status of each anode
104  if (expectSingleAnodePMT)
105  states[1] = HFAnodeStatus::NOT_DUAL;
106 
107  bool isTimingReliable[2] = {true, true};
108  for (unsigned ianode=0; ianode<2; ++ianode)
109  {
110  const HFQIE10Info* anodeInfo = prehit.getHFQIE10Info(ianode);
111  if (anodeInfo)
112  {
113  if (flaggedBadInDB[ianode])
114  states[ianode] = HFAnodeStatus::FLAGGED_BAD;
115  else
116  states[ianode] = determineAnodeStatus(ianode, *anodeInfo,
117  &isTimingReliable[ianode]);
118  }
119  }
120 
121  // Reconstruct energy and time
122  const unsigned lookupInd = mapStatusIntoIndex(states);
123  if (lookupInd != UINT_MAX)
124  {
125  // In this scope, at least one of states[i] is HFAnodeStatus::OK
126  // or was mapped into that status by "mapStatusIntoIndex" method
127  //
128  const float* weights = &energyWeights_[lookupInd][0];
129  float energy = 0.f, tfallWeightedEnergySum = 0.f, triseWeightedEnergySum = 0.f;
130  float tfallWeightedSum = 0.f, triseWeightedSum = 0.f;
131  float tfallSum = 0.f, triseSum = 0.f;
132  unsigned tfallCount = 0, triseCount = 0;
133 
134  for (unsigned ianode=0; ianode<2; ++ianode)
135  {
136  const HFQIE10Info* anodeInfo = prehit.getHFQIE10Info(ianode);
137  if (anodeInfo && weights[ianode] > 0.f)
138  {
139  const float weightedEnergy = weights[ianode]*anodeInfo->energy();
140  energy += weightedEnergy;
141 
142  if (isTimingReliable[ianode] &&
143  states[ianode] != HFAnodeStatus::FAILED_TIMING)
144  {
145  float trise = anodeInfo->timeRising();
146  if (!HcalSpecialTimes::isSpecial(trise))
147  {
148  trise += timeShift_;
149  triseSum += trise;
150  ++triseCount;
151  if (weightedEnergy > 0.f)
152  {
153  triseWeightedSum += trise*weightedEnergy;
154  triseWeightedEnergySum += weightedEnergy;
155  }
156  }
157 
158  float tfall = anodeInfo->timeFalling();
159  if (!HcalSpecialTimes::isSpecial(tfall))
160  {
161  tfall += timeShift_;
162  tfallSum += tfall;
163  ++tfallCount;
164  if (weightedEnergy > 0.f)
165  {
166  tfallWeightedSum += tfall*weightedEnergy;
167  tfallWeightedEnergySum += weightedEnergy;
168  }
169  }
170  }
171  }
172  }
173 
174  bool triseFromTDC = false;
175  const float trise = build_rechit_time(
176  triseWeightedEnergySum, triseWeightedSum, triseSum,
177  triseCount, triseIfNoTDC_, &triseFromTDC);
178 
179  bool tfallFromTDC = false;
180  const float tfall = build_rechit_time(
181  tfallWeightedEnergySum, tfallWeightedSum, tfallSum,
182  tfallCount, tfallIfNoTDC_, &tfallFromTDC);
183 
184  rh = HFRecHit(prehit.id(), energy, trise, tfall);
185  HFRecHitAuxSetter::setAux(prehit, states, soiPhase_, &rh);
186 
187  // Set the "timing from TDC" flag
188  const uint32_t flag = triseFromTDC ? 1U : 0U;
190  }
191 
192  return rh;
193 }
std::pair< float, float > tlimits_[2]
float timeRising() const
Definition: HFQIE10Info.h:36
bool isDataframeOK(bool checkAllTimeSlices=false) const
Definition: HFQIE10Info.cc:66
void setFlagField(uint32_t value, int base, int width=1)
Definition: CaloRecHit.cc:20
virtual unsigned determineAnodeStatus(unsigned anodeNumber, const HFQIE10Info &anode, bool *isTimingReliable) const
const HFQIE10Info * getHFQIE10Info(unsigned index) const
Definition: HFPreRecHit.cc:42
U second(std::pair< T, U > const &p)
HcalDetId id() const
Definition: HFPreRecHit.h:26
bool isSpecial(const float t)
unsigned mapStatusIntoIndex(const unsigned states[2]) const
double f[11][100]
float energy() const
Definition: HFQIE10Info.h:35
HFSimpleTimeCheck(const std::pair< float, float > tlimits[2], const float energyWeights[2 *HFAnodeStatus::N_POSSIBLE_STATES-1][2], unsigned soiPhase, float timeShift, float triseIfNoTDC, float tfallIfNoTDC, bool rejectAllFailures=true)
virtual HFRecHit reconstruct(const HFPreRecHit &prehit, const HcalCalibrations &calibs, const bool flaggedBadInDB[2], bool expectSingleAnodePMT) override
float energyWeights_[2 *HFAnodeStatus::N_POSSIBLE_STATES-1][2]
static void setAux(const HFPreRecHit &prehit, const unsigned anodeStates[2], unsigned soiPhase, HFRecHit *rechit)
float timeFalling() const
Definition: HFQIE10Info.h:37