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 }
constexpr float energy() const
Definition: CaloRecHit.h:31
float chi2() const
Definition: HBHERecHit.h:45
std::vector< MapItem > rechitMap_
std::vector< const HBHERecHit * > ptrbuf_
static const unsigned MASK_NSAMPLES
constexpr void orBit(uint32_t *u, const unsigned bitnum, const bool b)
constexpr void setField(uint32_t *u, const unsigned mask, const unsigned offset, const unsigned value)
virtual void combineAuxInfo(const std::vector< const HBHERecHit * > &rechits, HBHERecHit *rh) const
static const unsigned OFF_SOI
virtual HBHERecHit makeRechit(HcalDetId idToMake, const std::vector< const HBHERecHit * > &rechits) const
HcalDetId id() const
get the id
Definition: HBHERecHit.h:42
static const unsigned MASK_CAPID
#define nullptr
uint32_t auxPhase1() const
Definition: HBHERecHit.h:57
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:50
void push_back(T const &t)
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
static const unsigned OFF_NSAMPLES
static const unsigned OFF_DROPPED
HcalDetId mergedDepthDetId(const HcalDetId &id) const
Definition: HcalTopology.h:170
static const unsigned OFF_CAPID
U second(std::pair< T, U > const &p)
static const unsigned OFF_TDC_TIME
static const unsigned OFF_CAPID_ERR
static float energyWeightedAverage(const FPair *data, unsigned len, float valueToReturnOnFailure)
static const unsigned OFF_LINK_ERR
static const unsigned OFF_ADC
constexpr float time() const
Definition: CaloRecHit.h:33
constexpr unsigned getField(const uint32_t u, const unsigned mask, const unsigned offset)
std::pair< HcalDetId, const HBHERecHit * > MapItem
static const unsigned MASK_SOI
std::pair< float, float > FPair
constexpr bool isSpecial(const float t)
void setAuxHBHE(const uint32_t aux)
Definition: HBHERecHit.h:53
float timeFalling() const
get the hit falling time
Definition: HBHERecHit.h:39
void setBit(int &_bitArray, unsigned _iBit)
constexpr bool getBit(const uint32_t u, const unsigned bitnum)
void setAuxEnergy(const float en)
Definition: HBHERecHit.h:50
constexpr float UNKNOWN_T_NOTDC
void setChiSquared(const float chi2)
Definition: HBHERecHit.h:44
static const unsigned OFF_COMBINED
void combine(HBHERecHitCollection *toFill) override
constexpr void setFlags(uint32_t flags)
Definition: CaloRecHit.h:37
float eraw() const
Definition: HBHERecHit.h:48
void setRawEnergy(const float en)
Definition: HBHERecHit.h:47
void setAuxPhase1(const uint32_t aux)
Definition: HBHERecHit.h:56
constexpr uint32_t flags() const
Definition: CaloRecHit.h:36
float eaux() const
Definition: HBHERecHit.h:51
bool getBit(int &_bitArray, unsigned _iBit)
#define constexpr
virtual HcalDetId mapRechit(HcalDetId from) const
void add(const HBHERecHit &rh) override
void setTopo(const HcalTopology *topo) override