CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MuonSimHitMatcher.cc
Go to the documentation of this file.
2 
3 #include <algorithm>
4 
5 using namespace std;
6 
8  const auto& simVertex = ps.getParameterSet("simVertex");
9  const auto& simTrack = ps.getParameterSet("simTrack");
10  verboseSimTrack_ = simTrack.getParameter<int>("verbose");
11 
12  simVertexInput_ = iC.consumes<edm::SimVertexContainer>(simVertex.getParameter<edm::InputTag>("inputTag"));
13  simTrackInput_ = iC.consumes<edm::SimTrackContainer>(simTrack.getParameter<edm::InputTag>("inputTag"));
14 }
15 
18  hasGeometry_ = true;
19 
20  iEvent.getByToken(simTrackInput_, simTracksH_);
21  iEvent.getByToken(simVertexInput_, simVerticesH_);
22  iEvent.getByToken(simHitInput_, simHitsH_);
23 }
24 
26 void MuonSimHitMatcher::match(const SimTrack& track, const SimVertex& vertex) {
27  simTracks_ = *simTracksH_.product();
28  simVertices_ = *simVerticesH_.product();
29  simHits_ = *simHitsH_.product();
30 
31  // fill trkId2Index association:
32  int no = 0;
33  trkid_to_index_.clear();
34  for (const auto& t : simTracks_) {
35  trkid_to_index_[t.trackId()] = no;
36  no++;
37  }
38 
39  track_ids_ = getIdsOfSimTrackShower(track.trackId(), simTracks_, simVertices_);
40  if (verboseSimTrack_) {
41  edm::LogInfo("MuonSimHitMatcher") << "Printing track_ids" << std::endl;
42  for (const auto& id : track_ids_)
43  edm::LogInfo("MuonSimHitMatcher") << "id: " << id << std::endl;
44  }
45 }
46 
47 std::vector<unsigned int> MuonSimHitMatcher::getIdsOfSimTrackShower(unsigned int initial_trk_id,
48  const edm::SimTrackContainer& simTracks,
49  const edm::SimVertexContainer& simVertices) {
50  vector<unsigned int> result;
51  result.push_back(initial_trk_id);
52 
53  if (!simMuOnly_)
54  return result;
55 
56  for (const auto& t : simTracks_) {
57  SimTrack last_trk = t;
58  // if (std::abs(t.type()) != 13) continue;
59  bool is_child = false;
60  while (true) {
61  if (last_trk.noVertex())
62  break;
63  if (simVertices_[last_trk.vertIndex()].noParent())
64  break;
65 
66  unsigned parentId = simVertices_[last_trk.vertIndex()].parentIndex();
67  if (parentId == initial_trk_id) {
68  is_child = true;
69  break;
70  }
71 
72  const auto& association = trkid_to_index_.find(parentId);
73  if (association == trkid_to_index_.end())
74  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 
85 const edm::PSimHitContainer& MuonSimHitMatcher::simHits(int sub) const { return hits_; }
86 
87 const edm::PSimHitContainer& MuonSimHitMatcher::hitsInDetId(unsigned int detid) const {
88  if (detid_to_hits_.find(detid) == detid_to_hits_.end())
89  return no_hits_;
90  return detid_to_hits_.at(detid);
91 }
92 
93 const edm::PSimHitContainer& MuonSimHitMatcher::hitsInChamber(unsigned int detid) const {
94  if (chamber_to_hits_.find(detid) == chamber_to_hits_.end())
95  return no_hits_;
96  return chamber_to_hits_.at(detid);
97 }
98 
100  if (sim_hits.empty())
101  return GlobalPoint(); // point "zero"
102 
103  float sumx, sumy, sumz;
104  sumx = sumy = sumz = 0.f;
105  size_t n = 0;
106  for (const auto& h : sim_hits) {
107  const LocalPoint& lp = h.entryPoint();
108  const GlobalPoint& gp = geometry_->idToDet(h.detUnitId())->surface().toGlobal(lp);
109  sumx += gp.x();
110  sumy += gp.y();
111  sumz += gp.z();
112  ++n;
113  }
114  if (n == 0)
115  return GlobalPoint();
116  return GlobalPoint(sumx / n, sumy / n, sumz / n);
117 }
118 
120  if (sim_hits.empty())
121  return GlobalVector(); // point "zero"
122 
123  float sumx, sumy, sumz;
124  sumx = sumy = sumz = 0.f;
125  size_t n = 0;
126  for (const auto& h : sim_hits) {
127  const LocalVector& lv = h.momentumAtEntry();
128  const GlobalVector& gv = geometry_->idToDet(h.detUnitId())->surface().toGlobal(lv);
129  sumx += gv.x();
130  sumy += gv.y();
131  sumz += gv.z();
132  ++n;
133  }
134  if (n == 0)
135  return GlobalVector();
136  return GlobalVector(sumx / n, sumy / n, sumz / n);
137 }
138 
140  track_ids_.clear();
141  trkid_to_index_.clear();
142  hits_.clear();
143  detid_to_hits_.clear();
144  chamber_to_hits_.clear();
145 }
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
T y() const
Definition: PV3DBase.h:60
tuple result
Definition: mps_fire.py:311
int iEvent
Definition: GenABIO.cc:224
std::tuple< layerClusterToCaloParticle, caloParticleToLayerCluster > association
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:61
bool noVertex() const
Definition: SimTrack.h:34
int vertIndex() const
index of the vertex in the Event container (-1 if no vertex)
Definition: SimTrack.h:33
Log< level::Info, false > LogInfo
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
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
const edm::PSimHitContainer & hitsInChamber(unsigned int) const
T x() const
Definition: PV3DBase.h:59
std::vector< SimTrack > SimTrackContainer
Global3DVector GlobalVector
Definition: GlobalVector.h:10