CMS 3D CMS Logo

SimplePlan1RechitCombiner.cc
Go to the documentation of this file.
1 #include <cassert>
2 #include <algorithm>
3 
7 
9 
11 
13 
15  : topo_(nullptr)
16 {
17 }
18 
20 {
21  topo_ = topo;
22 }
23 
25 {
26  rechitMap_.clear();
27 }
28 
30 {
32  rechitMap_.push_back(MapItem(mapRechit(rh.id()), &rh));
33 }
34 
36 {
37  if (!rechitMap_.empty())
38  {
39  std::sort(rechitMap_.begin(), rechitMap_.end());
40 
41  HcalDetId oldId(rechitMap_[0].first);
42  ptrbuf_.clear();
43  ptrbuf_.push_back(rechitMap_[0].second);
44 
45  const std::size_t nInput = rechitMap_.size();
46  for (std::size_t i = 1; i < nInput; ++i)
47  {
48  if (rechitMap_[i].first != oldId)
49  {
50  const HBHERecHit& rh = makeRechit(oldId, ptrbuf_);
51  if (rh.id().rawId())
52  toFill->push_back(rh);
53  oldId = rechitMap_[i].first;
54  ptrbuf_.clear();
55  }
56  ptrbuf_.push_back(rechitMap_[i].second);
57  }
58 
59  const HBHERecHit& rh = makeRechit(oldId, ptrbuf_);
60  if (rh.id().rawId())
61  toFill->push_back(rh);
62  }
63 }
64 
66 {
67  return topo_->mergedDepthDetId(from);
68 }
69 
72  const std::vector<const HBHERecHit*>& rechits) const
73 {
74  constexpr unsigned MAXLEN = 8U; // Should be >= max # of Phase 1 HCAL depths
75  constexpr float TIME_IF_NO_ENERGY = -999.f;
76 
77  const unsigned nRecHits = rechits.size();
78  assert(nRecHits);
79  assert(nRecHits <= MAXLEN);
80 
81  // Combine energies, times, and fit chi-square
82  double energy = 0.0, eraw = 0.0, eaux = 0.0, chisq = 0.0;
83  FPair times[MAXLEN], adctimes[MAXLEN];
84  unsigned nADCTimes = 0;
85 
86  for (unsigned i=0; i<nRecHits; ++i)
87  {
88  const HBHERecHit& rh(*rechits[i]);
89  const float e = rh.energy();
90  energy += e;
91  eraw += rh.eraw();
92  eaux += rh.eaux();
93  chisq += rh.chi2();
94  times[i].first = rh.time();
95  times[i].second = e;
96 
97  const float tADC = rh.timeFalling();
98  if (!HcalSpecialTimes::isSpecial(tADC))
99  {
100  adctimes[nADCTimes].first = tADC;
101  adctimes[nADCTimes].second = e;
102  ++nADCTimes;
103  }
104  }
105 
106  HBHERecHit rh(idToMake, energy,
107  energyWeightedAverage(times, nRecHits, TIME_IF_NO_ENERGY),
109  rh.setRawEnergy(eraw);
110  rh.setAuxEnergy(eaux);
111  rh.setChiSquared(chisq);
112 
113  // Combine the auxiliary information
114  combineAuxInfo(rechits, &rh);
115 
116  return rh;
117 }
118 
120  const std::vector<const HBHERecHit*>& rechits,
121  HBHERecHit* rh) const
122 {
123  using namespace CaloRecHitAuxSetter;
124  using namespace HcalPhase1FlagLabels;
125 
126  // The number of rechits should be not larger than the
127  // number of half-bytes in a 32-bit word
128  constexpr unsigned MAXLEN = 8U;
129 
130  const unsigned nRecHits = rechits.size();
131  assert(nRecHits);
132  assert(nRecHits <= MAXLEN);
133 
134  uint32_t flags = 0, auxPhase1 = 0;
135  unsigned tripleFitCount = 0;
136  unsigned soiVote[HBHERecHitAuxSetter::MASK_SOI + 1U] = {0};
137  unsigned capidVote[HBHERecHitAuxSetter::MASK_CAPID + 1U] = {0};
138 
139  // Combine various status bits
140  for (unsigned i=0; i<nRecHits; ++i)
141  {
142  const HBHERecHit& rh(*rechits[i]);
143  const uint32_t rhflags = rh.flags();
144  const uint32_t rhAuxPhase1 = rh.auxPhase1();
145 
150 
151  const unsigned soi = getField(rhAuxPhase1, HBHERecHitAuxSetter::MASK_SOI,
153  soiVote[soi]++;
154 
155  const unsigned capid = getField(rhAuxPhase1, HBHERecHitAuxSetter::MASK_CAPID,
157  capidVote[capid]++;
158 
160  ++tripleFitCount;
161 
162  // Status flags are simply ORed for now. Might want
163  // to rethink this in the future.
164  flags |= rhflags;
165  }
166 
167  unsigned* pmaxsoi = std::max_element(soiVote, soiVote+sizeof(soiVote)/sizeof(soiVote[0]));
168  const unsigned soi = std::distance(&soiVote[0], pmaxsoi);
170 
171  unsigned* pmaxcapid = std::max_element(capidVote, capidVote+sizeof(capidVote)/sizeof(capidVote[0]));
172  const unsigned capid = std::distance(&capidVote[0], pmaxcapid);
174 
175  // A number that can be later used to calculate chi-square NDoF
177  HBHERecHitAuxSetter::OFF_ADC, tripleFitCount);
178 
179  // How many rechits were combined?
182 
183  // Should combine QIE11 data only
184  setBit(&auxPhase1, HBHERecHitAuxSetter::OFF_TDC_TIME, true);
185 
186  // Indicate that this rechit is combined
187  setBit(&auxPhase1, HBHERecHitAuxSetter::OFF_COMBINED, true);
188 
189  // Copy the aux words into the rechit
190  rh->setFlags(flags);
191  rh->setAuxPhase1(auxPhase1);
192 
193  // Sort the depth values of the combined rechits
194  // in the increasing order
195  unsigned depthValues[MAXLEN];
196  for (unsigned i=0; i<nRecHits; ++i)
197  depthValues[i] = rechits[i]->id().depth();
198  if (nRecHits > 1U)
199  std::sort(depthValues, depthValues+nRecHits);
200 
201  // Pack the information about the depth of the rechits
202  // that we are combining into the "auxHBHE" word
203  uint32_t auxHBHE = 0;
204  for (unsigned i=0; i<nRecHits; ++i)
205  setField(&auxHBHE, 0xf, i*4, depthValues[i]);
206  rh->setAuxHBHE(auxHBHE);
207 }
float chi2() const
Definition: HBHERecHit.h:28
std::vector< MapItem > rechitMap_
std::vector< const HBHERecHit * > ptrbuf_
void orBit(uint32_t *u, const unsigned bitnum, const bool b)
static const unsigned MASK_NSAMPLES
virtual void combineAuxInfo(const std::vector< const HBHERecHit * > &rechits, HBHERecHit *rh) const
static const unsigned OFF_SOI
unsigned getField(const uint32_t u, const unsigned mask, const unsigned offset)
virtual HBHERecHit makeRechit(HcalDetId idToMake, const std::vector< const HBHERecHit * > &rechits) const
HcalDetId id() const
get the id
Definition: HBHERecHit.h:25
static const unsigned MASK_CAPID
uint32_t auxPhase1() const
Definition: HBHERecHit.h:40
void push_back(T const &t)
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
float time() const
Definition: CaloRecHit.h:19
#define nullptr
#define constexpr
static const unsigned OFF_NSAMPLES
static const unsigned OFF_DROPPED
HcalDetId mergedDepthDetId(const HcalDetId &id) const
Definition: HcalTopology.h:169
static const unsigned OFF_CAPID
uint32_t rawId() const
get the raw id
Definition: DetId.h:44
U second(std::pair< T, U > const &p)
static const unsigned OFF_TDC_TIME
void setField(uint32_t *u, const unsigned mask, const unsigned offset, const unsigned value)
static const unsigned OFF_CAPID_ERR
static float energyWeightedAverage(const FPair *data, unsigned len, float valueToReturnOnFailure)
static const unsigned OFF_LINK_ERR
bool isSpecial(const float t)
float energy() const
Definition: CaloRecHit.h:17
static const unsigned OFF_ADC
uint32_t flags() const
Definition: CaloRecHit.h:22
std::pair< HcalDetId, const HBHERecHit * > MapItem
static const unsigned MASK_SOI
std::pair< float, float > FPair
void setAuxHBHE(const uint32_t aux)
Definition: HBHERecHit.h:36
float timeFalling() const
get the hit falling time
Definition: HBHERecHit.h:23
void setFlags(uint32_t flags)
Definition: CaloRecHit.h:23
void setBit(int &_bitArray, unsigned _iBit)
void setAuxEnergy(const float en)
Definition: HBHERecHit.h:33
constexpr float UNKNOWN_T_NOTDC
void setChiSquared(const float chi2)
Definition: HBHERecHit.h:27
static const unsigned OFF_COMBINED
void combine(HBHERecHitCollection *toFill) override
float eraw() const
Definition: HBHERecHit.h:31
bool getBit(const uint32_t u, const unsigned bitnum)
void setRawEnergy(const float en)
Definition: HBHERecHit.h:30
void setAuxPhase1(const uint32_t aux)
Definition: HBHERecHit.h:39
float eaux() const
Definition: HBHERecHit.h:34
bool getBit(int &_bitArray, unsigned _iBit)
virtual HcalDetId mapRechit(HcalDetId from) const
void add(const HBHERecHit &rh) override
void setTopo(const HcalTopology *topo) override