CMS 3D CMS Logo

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 
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,
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 
88  if (detid_to_hits_.find(detid) == detid_to_hits_.end())
89  return no_hits_;
90  return detid_to_hits_.at(detid);
91 }
92 
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 }
const edm::PSimHitContainer & hitsInChamber(unsigned int) const
GlobalPoint simHitsMeanPosition(const edm::PSimHitContainer &sim_hits) const
T z() const
Definition: PV3DBase.h:61
GlobalVector simHitsMeanMomentum(const edm::PSimHitContainer &sim_hits) const
ParameterSet const & getParameterSet(std::string const &) const
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
const std::vector< float > & sim_hits()
Definition: LSTEff.cc:3312
std::tuple< layerClusterToCaloParticle, caloParticleToLayerCluster > association
const edm::PSimHitContainer & simHits(int) const
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
int vertIndex() const
index of the vertex in the Event container (-1 if no vertex)
Definition: SimTrack.h:33
int iEvent
Definition: GenABIO.cc:224
Log< level::Info, false > LogInfo
bool noVertex() const
Definition: SimTrack.h:34
std::vector< SimVertex > SimVertexContainer
void match(const SimTrack &t, const SimVertex &v)
do the matching
const edm::PSimHitContainer & hitsInDetId(unsigned int) const
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
std::vector< SimTrack > SimTrackContainer
Global3DVector GlobalVector
Definition: GlobalVector.h:10