CMS 3D CMS Logo

MuonSimHitMatcher.cc
Go to the documentation of this file.
2 
3 #include <algorithm>
4 
5 using namespace std;
6 
9  const auto& simVertex = ps.getParameterSet("simVertex");
10  const auto& simTrack = ps.getParameterSet("simTrack");
11  verboseSimTrack_ = simTrack.getParameter<int>("verbose");
12 
13  simVertexInput_ = iC.consumes<edm::SimVertexContainer>(
14  simVertex.getParameter<edm::InputTag>("inputTag"));
15  simTrackInput_ = iC.consumes<edm::SimTrackContainer>(
16  simTrack.getParameter<edm::InputTag>("inputTag"));
17 }
18 
21  const edm::EventSetup& iSetup) {
22  hasGeometry_ = true;
23 
24  iEvent.getByToken(simTrackInput_, simTracksH_);
25  iEvent.getByToken(simVertexInput_, simVerticesH_);
26  iEvent.getByToken(simHitInput_, simHitsH_);
27 }
28 
30 void MuonSimHitMatcher::match(const SimTrack& track, const SimVertex& vertex) {
31  simTracks_ = *simTracksH_.product();
32  simVertices_ = *simVerticesH_.product();
33  simHits_ = *simHitsH_.product();
34 
35  // fill trkId2Index association:
36  int no = 0;
37  trkid_to_index_.clear();
38  for (const auto& t : simTracks_) {
39  trkid_to_index_[t.trackId()] = no;
40  no++;
41  }
42 
43  track_ids_ =
44  getIdsOfSimTrackShower(track.trackId(), simTracks_, simVertices_);
45  if (verboseSimTrack_) {
46  edm::LogInfo("MuonSimHitMatcher") << "Printing track_ids" << std::endl;
47  for (const auto& id : track_ids_) edm::LogInfo("MuonSimHitMatcher") << "id: " << id << std::endl;
48  }
49 }
50 
52  unsigned int initial_trk_id, const edm::SimTrackContainer& simTracks,
54  vector<unsigned int> result;
55  result.push_back(initial_trk_id);
56 
57  if (!simMuOnly_) return result;
58 
59  for (const auto& t : simTracks_) {
60  SimTrack last_trk = t;
61  // if (std::abs(t.type()) != 13) continue;
62  bool is_child = false;
63  while (true) {
64  if (last_trk.noVertex()) break;
65  if (simVertices_[last_trk.vertIndex()].noParent()) break;
66 
67  unsigned parentId = simVertices_[last_trk.vertIndex()].parentIndex();
68  if (parentId == initial_trk_id) {
69  is_child = true;
70  break;
71  }
72 
73  const auto& association = trkid_to_index_.find(parentId);
74  if (association == trkid_to_index_.end()) break;
75 
76  last_trk = simTracks_[association->second];
77  }
78  if (is_child) {
79  result.push_back(t.trackId());
80  }
81  }
82  return result;
83 }
84 
86  return hits_;
87 }
88 
90  unsigned int detid) const {
91  if (detid_to_hits_.find(detid) == detid_to_hits_.end()) return no_hits_;
92  return detid_to_hits_.at(detid);
93 }
94 
96  unsigned int detid) const {
97  if (chamber_to_hits_.find(detid) == chamber_to_hits_.end()) return no_hits_;
98  return chamber_to_hits_.at(detid);
99 }
100 
102  const edm::PSimHitContainer& sim_hits) const {
103  if (sim_hits.empty()) return GlobalPoint(); // point "zero"
104 
105  float sumx, sumy, sumz;
106  sumx = sumy = sumz = 0.f;
107  size_t n = 0;
108  for (const auto& h : sim_hits) {
109  const LocalPoint& lp = h.entryPoint();
110  const GlobalPoint& gp =
111  geometry_->idToDet(h.detUnitId())->surface().toGlobal(lp);
112  sumx += gp.x();
113  sumy += gp.y();
114  sumz += gp.z();
115  ++n;
116  }
117  if (n == 0) return GlobalPoint();
118  return GlobalPoint(sumx / n, sumy / n, sumz / n);
119 }
120 
122  const edm::PSimHitContainer& sim_hits) const {
123  if (sim_hits.empty()) return GlobalVector(); // point "zero"
124 
125  float sumx, sumy, sumz;
126  sumx = sumy = sumz = 0.f;
127  size_t n = 0;
128  for (const auto& h : sim_hits) {
129  const LocalVector& lv = h.momentumAtEntry();
130  const GlobalVector& gv =
131  geometry_->idToDet(h.detUnitId())->surface().toGlobal(lv);
132  sumx += gv.x();
133  sumy += gv.y();
134  sumz += gv.z();
135  ++n;
136  }
137  if (n == 0) return GlobalVector();
138  return GlobalVector(sumx / n, sumy / n, sumz / n);
139 }
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
T y() const
Definition: PV3DBase.h:63
int iEvent
Definition: GenABIO.cc:224
GlobalPoint simHitsMeanPosition(const edm::PSimHitContainer &sim_hits) const
const edm::PSimHitContainer & simHits(int) const
GlobalVector simHitsMeanMomentum(const edm::PSimHitContainer &sim_hits) const
T z() const
Definition: PV3DBase.h:64
bool noVertex() const
Definition: SimTrack.h:31
int vertIndex() const
index of the vertex in the Event container (-1 if no vertex)
Definition: SimTrack.h:30
unsigned int trackId() const
Definition: CoreSimTrack.h:31
ParameterSet const & getParameterSet(std::string const &) const
const edm::PSimHitContainer & hitsInDetId(unsigned int) const
std::vector< SimVertex > SimVertexContainer
void match(const SimTrack &t, const SimVertex &v)
do the matching
MuonSimHitMatcher(const edm::ParameterSet &iPS, edm::ConsumesCollector &&iC)
std::vector< PSimHit > PSimHitContainer
std::vector< unsigned int > getIdsOfSimTrackShower(unsigned trk_id, const edm::SimTrackContainer &simTracks, const edm::SimVertexContainer &simVertices)
void init(const edm::Event &e, const edm::EventSetup &eventSetup)
initialize the event
const edm::PSimHitContainer & hitsInChamber(unsigned int) const
T x() const
Definition: PV3DBase.h:62
std::vector< SimTrack > SimTrackContainer
Global3DVector GlobalVector
Definition: GlobalVector.h:10