CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
CSCRecHitMatcher.cc
Go to the documentation of this file.
1 #include <memory>
2 
4 
5 using namespace std;
6 
8  const auto& cscRecHit2D = pset.getParameter<edm::ParameterSet>("cscRecHit");
9  maxBXCSCRecHit2D_ = cscRecHit2D.getParameter<int>("maxBX");
10  minBXCSCRecHit2D_ = cscRecHit2D.getParameter<int>("minBX");
11  verboseCSCRecHit2D_ = cscRecHit2D.getParameter<int>("verbose");
12 
13  const auto& cscSegment = pset.getParameter<edm::ParameterSet>("cscSegment");
14  maxBXCSCSegment_ = cscSegment.getParameter<int>("maxBX");
15  minBXCSCSegment_ = cscSegment.getParameter<int>("minBX");
16  verboseCSCSegment_ = cscSegment.getParameter<int>("verbose");
17 
18  // make a new digi matcher
19  cscDigiMatcher_ = std::make_unique<CSCDigiMatcher>(pset, std::move(iC));
20 
21  cscRecHit2DToken_ = iC.consumes<CSCRecHit2DCollection>(cscRecHit2D.getParameter<edm::InputTag>("inputTag"));
22  cscSegmentToken_ = iC.consumes<CSCSegmentCollection>(cscSegment.getParameter<edm::InputTag>("inputTag"));
23 
24  cscGeomToken_ = iC.esConsumes<CSCGeometry, MuonGeometryRecord>();
25 }
26 
28  cscDigiMatcher_->init(iEvent, iSetup);
29 
30  iEvent.getByToken(cscRecHit2DToken_, cscRecHit2DH_);
31  iEvent.getByToken(cscSegmentToken_, cscSegmentH_);
32 
33  const auto& csc_geom = iSetup.getHandle(cscGeomToken_);
34  if (csc_geom.isValid()) {
35  cscGeometry_ = csc_geom.product();
36  } else {
37  edm::LogWarning("CSCSimHitMatcher") << "+++ Info: CSC geometry is unavailable. +++\n";
38  }
39 }
40 
43  // match digis first
44  cscDigiMatcher_->match(t, v);
45 
46  // get the rechit collection
47  const CSCRecHit2DCollection& cscRecHit2Ds = *cscRecHit2DH_.product();
48  const CSCSegmentCollection& cscSegments = *cscSegmentH_.product();
49 
50  // now match the rechits
51  matchCSCRecHit2DsToSimTrack(cscRecHit2Ds);
52  matchCSCSegmentsToSimTrack(cscSegments);
53 }
54 
56  if (verboseCSCRecHit2D_)
57  edm::LogInfo("CSCRecHitMatcher") << "Matching simtrack to CSC rechits";
58 
59  // fetch all layerIds with digis
60  const auto& strip_ids = cscDigiMatcher_->detIdsStrip();
61  const auto& wire_ids = cscDigiMatcher_->detIdsWire();
62 
63  // merge the two collections
64  std::set<unsigned int> layer_ids;
65  layer_ids.insert(strip_ids.begin(), strip_ids.end());
66  layer_ids.insert(wire_ids.begin(), wire_ids.end());
67 
68  if (verboseCSCRecHit2D_)
69  edm::LogInfo("CSCRecHitMatcher") << "Number of matched csc layer_ids " << layer_ids.size();
70 
71  for (const auto& id : layer_ids) {
72  CSCDetId p_id(id);
73 
74  // print all the wires in the CSCChamber
75  const auto& hit_wg(cscDigiMatcher_->wiregroupsInDetId(id));
76  if (verboseCSCRecHit2D_) {
77  edm::LogInfo("CSCRecHitMatcher") << "hit wg csc from simhit" << endl;
78  for (const auto& p : hit_wg) {
79  edm::LogInfo("CSCRecHitMatcher") << p;
80  }
81  }
82 
83  // print all the strips in the CSCChamber
84  const auto& hit_strips(cscDigiMatcher_->stripsInDetId(id));
85  if (verboseCSCRecHit2D_) {
86  edm::LogInfo("CSCRecHitMatcher") << "hit strip csc from simhit" << endl;
87  for (const auto& p : hit_strips) {
88  edm::LogInfo("CSCRecHitMatcher") << p;
89  }
90  }
91 
92  // get the rechits
93  const auto& rechits_in_det = rechits.get(p_id);
94  for (auto d = rechits_in_det.first; d != rechits_in_det.second; ++d) {
95  if (verboseCSCRecHit2D_)
96  edm::LogInfo("CSCRecHitMatcher") << "rechit " << p_id << " " << *d;
97 
98  // does the wire number match?
99  const bool wireMatch(std::find(hit_wg.begin(), hit_wg.end(), d->hitWire()) != hit_wg.end());
100 
101  // does the strip number match?
102  bool stripMatch(false);
103  for (size_t iS = 0; iS < d->nStrips(); ++iS) {
104  if (std::find(hit_strips.begin(), hit_strips.end(), d->channels(iS)) != hit_strips.end())
105  stripMatch = true;
106  }
107 
108  // this rechit was matched to a matching simhit
109  if (wireMatch and stripMatch) {
110  if (verboseCSCRecHit2D_)
111  edm::LogInfo("CSCRecHitMatcher") << "\t...was matched!";
112  layer_to_cscRecHit2D_[id].push_back(*d);
113  chamber_to_cscRecHit2D_[p_id.chamberId().rawId()].push_back(*d);
114  }
115  }
116  }
117 }
118 
120  if (verboseCSCSegment_)
121  edm::LogInfo("CSCRecHitMatcher") << "Matching simtrack to segments";
122  // fetch all chamberIds with 2D rechits
123 
124  const auto& chamber_ids = chamberIdsCSCRecHit2D();
125  if (verboseCSCSegment_)
126  edm::LogInfo("CSCRecHitMatcher") << "Number of matched csc segments " << chamber_ids.size();
127  for (const auto& id : chamber_ids) {
128  CSCDetId p_id(id);
129 
130  // print all CSCRecHit2D in the CSCChamber
131  const auto& csc_rechits(cscRecHit2DsInChamber(id));
132  if (verboseCSCSegment_) {
133  edm::LogInfo("CSCRecHitMatcher") << "hit csc rechits" << endl;
134  for (const auto& p : csc_rechits) {
135  edm::LogInfo("CSCRecHitMatcher") << p;
136  }
137  }
138 
139  // get the segments
140  const auto& segments_in_det = cscSegments.get(p_id);
141  for (auto d = segments_in_det.first; d != segments_in_det.second; ++d) {
142  if (verboseCSCSegment_)
143  edm::LogInfo("CSCRecHitMatcher") << "segment " << p_id << " " << *d << endl;
144 
145  //access the rechits
146  const auto& recHits(d->recHits());
147 
148  int rechitsFound = 0;
149  if (verboseCSCSegment_)
150  edm::LogInfo("CSCRecHitMatcher") << recHits.size() << " csc rechits from segment " << endl;
151  for (const auto& rh : recHits) {
152  const CSCRecHit2D* cscrh(dynamic_cast<const CSCRecHit2D*>(rh));
153  if (isCSCRecHit2DMatched(*cscrh))
154  ++rechitsFound;
155  }
156  if (rechitsFound == 0)
157  continue;
158  if (verboseCSCSegment_) {
159  edm::LogInfo("CSCRecHitMatcher") << "Found " << rechitsFound << " rechits out of "
160  << cscRecHit2DsInChamber(id).size();
161  edm::LogInfo("CSCRecHitMatcher") << "\t...was matched!";
162  }
163  chamber_to_cscSegment_[p_id.rawId()].push_back(*d);
164  }
165  }
166 }
167 
168 std::set<unsigned int> CSCRecHitMatcher::layerIdsCSCRecHit2D() const {
169  std::set<unsigned int> result;
170  for (const auto& p : layer_to_cscRecHit2D_)
171  result.insert(p.first);
172  return result;
173 }
174 
175 std::set<unsigned int> CSCRecHitMatcher::chamberIdsCSCRecHit2D() const {
176  std::set<unsigned int> result;
177  for (const auto& p : chamber_to_cscRecHit2D_)
178  result.insert(p.first);
179  return result;
180 }
181 
182 std::set<unsigned int> CSCRecHitMatcher::chamberIdsCSCSegment() const {
183  std::set<unsigned int> result;
184  for (const auto& p : chamber_to_cscSegment_)
185  result.insert(p.first);
186  return result;
187 }
188 
190  if (layer_to_cscRecHit2D_.find(detid) == layer_to_cscRecHit2D_.end())
191  return no_cscRecHit2Ds_;
192  return layer_to_cscRecHit2D_.at(detid);
193 }
194 
196  if (chamber_to_cscRecHit2D_.find(detid) == chamber_to_cscRecHit2D_.end())
197  return no_cscRecHit2Ds_;
198  return chamber_to_cscRecHit2D_.at(detid);
199 }
200 
202  if (chamber_to_cscSegment_.find(detid) == chamber_to_cscSegment_.end())
203  return no_cscSegments_;
204  return chamber_to_cscSegment_.at(detid);
205 }
206 
207 int CSCRecHitMatcher::nCSCRecHit2DsInLayer(unsigned int detid) const { return cscRecHit2DsInLayer(detid).size(); }
208 
209 int CSCRecHitMatcher::nCSCRecHit2DsInChamber(unsigned int detid) const { return cscRecHit2DsInChamber(detid).size(); }
210 
211 int CSCRecHitMatcher::nCSCSegmentsInChamber(unsigned int detid) const { return cscSegmentsInChamber(detid).size(); }
212 
215  for (const auto& id : chamberIdsCSCRecHit2D()) {
216  const auto& segmentsInChamber(cscRecHit2DsInChamber(id));
217  result.insert(result.end(), segmentsInChamber.begin(), segmentsInChamber.end());
218  }
219  return result;
220 }
221 
224  for (const auto& id : chamberIdsCSCSegment()) {
225  const auto& segmentsInChamber(cscSegmentsInChamber(id));
226  result.insert(result.end(), segmentsInChamber.begin(), segmentsInChamber.end());
227  }
228  return result;
229 }
230 
232  bool isSame = false;
233  for (const auto& segment : c)
234  if (areCSCRecHit2DsSame(sg, segment))
235  isSame = true;
236  return isSame;
237 }
238 
240  bool isSame = false;
241  for (const auto& segment : c)
242  if (areCSCSegmentsSame(sg, segment))
243  isSame = true;
244  return isSame;
245 }
246 
248  return cscRecHit2DInContainer(thisSg, cscRecHit2Ds());
249 }
250 
252  return cscSegmentInContainer(thisSg, cscSegments());
253 }
254 
256  int n = 0;
257  const auto& ids = chamberIdsCSCRecHit2D();
258  for (const auto& id : ids)
259  n += cscRecHit2DsInChamber(id).size();
260  return n;
261 }
262 
264  int n = 0;
265  const auto& ids = chamberIdsCSCSegment();
266  for (const auto& id : ids)
267  n += cscSegmentsInChamber(id).size();
268  return n;
269 }
270 
272  return l.localPosition() == r.localPosition();
273 }
274 
276  return (l.localPosition() == r.localPosition() and l.localDirection() == r.localDirection());
277 }
278 
280  CSCSegment emptySegment;
281  double chi2overNdf = 99;
282  int index = 0;
283  int foundIndex = -99;
284 
285  for (const auto& seg : chamber_to_cscSegment_[id]) {
286  double newChi2overNdf(seg.chi2() / seg.degreesOfFreedom());
287  if (newChi2overNdf < chi2overNdf) {
288  chi2overNdf = newChi2overNdf;
289  foundIndex = index;
290  }
291  ++index;
292  }
293  if (foundIndex != -99)
294  return chamber_to_cscSegment_[id][foundIndex];
295  return emptySegment;
296 }
297 
299  return cscGeometry_->idToDet(c.cscDetId())->surface().toGlobal(c.localPosition());
300 }
bool cscRecHit2DInContainer(const CSCRecHit2D &, const CSCRecHit2DContainer &) const
const edm::EventSetup & c
LocalPoint localPosition() const override
Definition: CSCSegment.h:39
CSCDetId cscDetId() const
Definition: CSCSegment.h:70
uint16_t *__restrict__ id
bool areCSCSegmentsSame(const CSCSegment &, const CSCSegment &) const
void matchCSCRecHit2DsToSimTrack(const CSCRecHit2DCollection &)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
const CSCSegmentContainer & cscSegmentsInChamber(unsigned int) const
CSCRecHitMatcher(edm::ParameterSet const &iPS, edm::ConsumesCollector &&iC)
const CSCRecHit2DContainer & cscRecHit2DsInChamber(unsigned int) const
bool cscSegmentInContainer(const CSCSegment &, const CSCSegmentContainer &) const
std::set< unsigned int > chamberIdsCSCSegment() const
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
bool isCSCRecHit2DMatched(const CSCRecHit2D &) const
std::set< unsigned int > layerIdsCSCRecHit2D() const
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
bool isCSCSegmentMatched(const CSCSegment &) const
int nCSCRecHit2DsInChamber(unsigned int) const
tuple result
Definition: mps_fire.py:311
CSCSegment bestCSCSegment(unsigned int)
tuple d
Definition: ztail.py:151
LocalVector localDirection() const override
Local direction.
Definition: CSCSegment.h:42
std::set< unsigned int > chamberIdsCSCRecHit2D() const
int iEvent
Definition: GenABIO.cc:224
int nCSCRecHit2DsInLayer(unsigned int) const
def move
Definition: eostools.py:511
CSCDetId chamberId() const
Definition: CSCDetId.h:47
int nCSCRecHit2Ds() const
LocalPoint localPosition() const override
Definition: CSCRecHit2D.h:56
int nCSCSegments() const
Log< level::Info, false > LogInfo
void matchCSCSegmentsToSimTrack(const CSCSegmentCollection &)
int nCSCSegmentsInChamber(unsigned int) const
std::vector< CSCSegment > CSCSegmentContainer
void init(const edm::Event &e, const edm::EventSetup &eventSetup)
const CSCRecHit2DContainer & cscRecHit2DsInLayer(unsigned int) const
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
const CSCSegmentContainer cscSegments() const
bool areCSCRecHit2DsSame(const CSCRecHit2D &, const CSCRecHit2D &) const
const CSCRecHit2DContainer cscRecHit2Ds() const
void match(const SimTrack &t, const SimVertex &v)
do the matching
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:151
Log< level::Warning, false > LogWarning
GlobalPoint globalPoint(const CSCSegment &) const
std::vector< CSCRecHit2D > CSCRecHit2DContainer