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,
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 }
Vector3DBase
Definition: Vector3DBase.h:8
MuonSimHitMatcher::simHitsMeanMomentum
GlobalVector simHitsMeanMomentum(const edm::PSimHitContainer &sim_hits) const
Definition: MuonSimHitMatcher.cc:119
MuonSimHitMatcher::clear
void clear()
Definition: MuonSimHitMatcher.cc:139
HLT_FULL_cff.track
track
Definition: HLT_FULL_cff.py:11713
SimVertex
Definition: SimVertex.h:5
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
PV3DBase::x
T x() const
Definition: PV3DBase.h:59
hgcal::association
std::tuple< layerClusterToCaloParticle, caloParticleToLayerCluster > association
Definition: LayerClusterAssociatorByEnergyScoreImpl.h:44
MuonSimHitMatcher::simHitsMeanPosition
GlobalPoint simHitsMeanPosition(const edm::PSimHitContainer &sim_hits) const
Definition: MuonSimHitMatcher.cc:99
TrackCandidateProducer_cfi.simTracks
simTracks
Definition: TrackCandidateProducer_cfi.py:15
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
GlobalVector
Global3DVector GlobalVector
Definition: GlobalVector.h:10
MuonSimHitMatcher.h
PV3DBase::z
T z() const
Definition: PV3DBase.h:61
HLTEgPhaseIITestSequence_cff.simVertex
simVertex
Definition: HLTEgPhaseIITestSequence_cff.py:51
SimTrack::noVertex
bool noVertex() const
Definition: SimTrack.h:31
h
MuonSimHitMatcher::simHits
const edm::PSimHitContainer & simHits(int) const
Definition: MuonSimHitMatcher.cc:85
GlobalPoint
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
Point3DBase< float, GlobalTag >
runTauDisplay.gp
gp
Definition: runTauDisplay.py:431
bphysicsOniaDQM_cfi.vertex
vertex
Definition: bphysicsOniaDQM_cfi.py:7
edm::ParameterSet
Definition: ParameterSet.h:47
MuonSimHitMatcher::match
void match(const SimTrack &t, const SimVertex &v)
do the matching
Definition: MuonSimHitMatcher.cc:26
muonSimHitMatcherPSet.simTrack
simTrack
Definition: muonSimHitMatcherPSet.py:4
PV3DBase::y
T y() const
Definition: PV3DBase.h:60
iEvent
int iEvent
Definition: GenABIO.cc:224
edm::EventSetup
Definition: EventSetup.h:57
MuonSimHitMatcher::hitsInDetId
const edm::PSimHitContainer & hitsInDetId(unsigned int) const
Definition: MuonSimHitMatcher.cc:87
std
Definition: JetResolutionObject.h:76
edm::SimTrackContainer
std::vector< SimTrack > SimTrackContainer
Definition: SimTrackContainer.h:12
SimTrack
Definition: SimTrack.h:6
MuonSimHitMatcher::getIdsOfSimTrackShower
std::vector< unsigned int > getIdsOfSimTrackShower(unsigned trk_id, const edm::SimTrackContainer &simTracks, const edm::SimVertexContainer &simVertices)
Definition: MuonSimHitMatcher.cc:47
SimTrack::vertIndex
int vertIndex() const
index of the vertex in the Event container (-1 if no vertex)
Definition: SimTrack.h:30
MuonSimHitMatcher::hitsInChamber
const edm::PSimHitContainer & hitsInChamber(unsigned int) const
Definition: MuonSimHitMatcher.cc:93
mps_fire.result
result
Definition: mps_fire.py:311
HGCalValidator_cfi.simVertices
simVertices
Definition: HGCalValidator_cfi.py:43
edm::PSimHitContainer
std::vector< PSimHit > PSimHitContainer
Definition: PSimHitContainer.h:11
edm::Event
Definition: Event.h:73
MuonSimHitMatcher::init
void init(const edm::Event &e, const edm::EventSetup &eventSetup)
initialize the event
Definition: MuonSimHitMatcher.cc:17
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644
edm::SimVertexContainer
std::vector< SimVertex > SimVertexContainer
Definition: SimVertexContainer.h:12
edm::InputTag
Definition: InputTag.h:15
edm::ConsumesCollector
Definition: ConsumesCollector.h:45
edm::ParameterSet::getParameterSet
ParameterSet const & getParameterSet(std::string const &) const
Definition: ParameterSet.cc:2128
MuonSimHitMatcher::MuonSimHitMatcher
MuonSimHitMatcher(const edm::ParameterSet &iPS, edm::ConsumesCollector &&iC)
Definition: MuonSimHitMatcher.cc:7