CMS 3D CMS Logo

InputData.cc
Go to the documentation of this file.
13 
14 #include <map>
15 
16 using namespace std;
17 
18 namespace l1tVertexFinder {
19 
20  InputData::InputData() {}
21 
22  InputData::InputData(const edm::Event& iEvent,
23  const edm::EventSetup& iSetup,
24  const AnalysisSettings& settings,
25  const edm::EDGetTokenT<edm::HepMCProduct> hepMCToken,
26  const edm::EDGetTokenT<edm::View<reco::GenParticle>> genParticlesToken,
29  const edm::EDGetTokenT<DetSetVec> stubToken,
32  // Get the tracker geometry info needed to unpack the stub info.
33  const TrackerTopology& tTopo = iSetup.getData(tTopoToken);
34  const TrackerGeometry& tGeom = iSetup.getData(tGeomToken);
35 
36  // Get stub info, by looping over modules and then stubs inside each module.
37  // Also get the association map from stubs to tracking particles.
38  edm::Handle<DetSetVec> ttStubHandle;
39  iEvent.getByToken(stubToken, ttStubHandle);
40 
41  std::set<DetId> lStubDetIds;
42  for (DetSetVec::const_iterator p_module = ttStubHandle->begin(); p_module != ttStubHandle->end(); p_module++) {
43  for (DetSet::const_iterator p_ttstub = p_module->begin(); p_ttstub != p_module->end(); p_ttstub++) {
44  lStubDetIds.insert(p_ttstub->getDetId());
45  }
46  }
47 
48  std::map<DetId, DetId> stubGeoDetIdMap;
49  for (auto gd = tGeom.dets().begin(); gd != tGeom.dets().end(); gd++) {
50  DetId detid = (*gd)->geographicalId();
51  if (detid.subdetId() != StripSubdetector::TOB && detid.subdetId() != StripSubdetector::TID)
52  continue; // only run on OT
53  if (!tTopo.isLower(detid))
54  continue; // loop on the stacks: choose the lower arbitrarily
55  DetId stackDetid = tTopo.stack(detid); // Stub module detid
56 
57  if (lStubDetIds.count(stackDetid) > 0) {
58  assert(stubGeoDetIdMap.count(stackDetid) == 0);
59  stubGeoDetIdMap[stackDetid] = detid;
60  }
61  }
62  assert(lStubDetIds.size() == stubGeoDetIdMap.size());
63 
64  // Get TrackingParticle info
66  edm::Handle<edm::ValueMap<TP>> tpValueMapHandle;
67  iEvent.getByToken(tpToken, tpHandle);
68  iEvent.getByToken(tpValueMapToken, tpValueMapHandle);
69  edm::ValueMap<TP> tpValueMap = *tpValueMapHandle;
70 
71  for (unsigned int i = 0; i < tpHandle->size(); i++) {
72  if (tpValueMap[tpHandle->refAt(i)].use()) {
73  tpPtrToRefMap_[tpHandle->ptrAt(i)] = tpHandle->refAt(i);
74  }
75  }
76 
77  // Find the various vertices
78  genPt_ = 0.;
79  genPt_PU_ = 0.;
80  for (const auto& [edmPtr, edmRef] : tpPtrToRefMap_) {
81  TP tp = tpValueMap[edmRef];
82  if (tp.physicsCollision()) {
83  genPt_ += tp->pt();
84  } else {
85  genPt_PU_ += tp->pt();
86  }
87  if (settings.debug() > 2) {
88  edm::LogInfo("InputData") << "InputData::genPt in the event " << genPt_;
89  }
90 
91  if (tp.useForAlgEff()) {
92  vertex_.insert(tp);
93  } else if (tp.useForVertexReco()) {
94  bool found = false;
95  for (unsigned int i = 0; i < vertices_.size(); ++i) {
96  if (tp->vz() == vertices_[i].vz()) {
97  vertices_[i].insert(tp);
98  found = true;
99  break;
100  }
101  }
102  if (!found) {
103  Vertex vertex(tp->vz());
104  vertex.insert(tp);
105  vertices_.push_back(vertex);
106  }
107  }
108  }
109 
110  for (const Vertex& vertex : vertices_) {
111  if (vertex.numTracks() >= settings.vx_minTracks())
112  recoVertices_.push_back(vertex);
113  }
114 
115  if (settings.debug() > 2)
116  edm::LogInfo("InputData") << "InputData::" << vertices_.size() << " pileup vertices in the event, "
117  << recoVertices_.size() << " reconstructable";
118 
119  vertex_.computeParameters();
120  if (settings.debug() > 2)
121  edm::LogInfo("InputData") << "InputData::Vertex " << vertex_.z0() << " containing " << vertex_.numTracks()
122  << " total pT " << vertex_.pT();
123 
124  for (unsigned int i = 0; i < vertices_.size(); ++i) {
125  vertices_[i].computeParameters();
126  }
127 
128  for (unsigned int i = 0; i < recoVertices_.size(); ++i) {
129  recoVertices_[i].computeParameters();
130  }
131 
132  std::sort(vertices_.begin(), vertices_.end(), SortVertexByPt());
133  std::sort(recoVertices_.begin(), recoVertices_.end(), SortVertexByPt());
134 
135  // Form the HepMC and GenParticle based vertices
137  iEvent.getByToken(hepMCToken, HepMCEvt);
138 
139  edm::Handle<edm::View<reco::GenParticle>> GenParticleHandle;
140  iEvent.getByToken(genParticlesToken, GenParticleHandle);
141 
142  if (!HepMCEvt.isValid() && !GenParticleHandle.isValid()) {
143  throw cms::Exception("Neither the edm::HepMCProduct nor the generator particles are available.");
144  }
145  if (HepMCEvt.isValid()) {
146  const HepMC::GenEvent* MCEvt = HepMCEvt->GetEvent();
147  for (HepMC::GenEvent::vertex_const_iterator ivertex = MCEvt->vertices_begin(); ivertex != MCEvt->vertices_end();
148  ++ivertex) {
149  bool hasParentVertex = false;
150 
151  // Loop over the parents looking to see if they are coming from a production vertex
152  for (HepMC::GenVertex::particle_iterator iparent = (*ivertex)->particles_begin(HepMC::parents);
153  iparent != (*ivertex)->particles_end(HepMC::parents);
154  ++iparent)
155  if ((*iparent)->production_vertex()) {
156  hasParentVertex = true;
157  break;
158  }
159 
160  // Reject those vertices with parent vertices
161  if (hasParentVertex)
162  continue;
163  // Get the position of the vertex
164  HepMC::FourVector pos = (*ivertex)->position();
165  const double mm = 0.1; // [mm] --> [cm]
166  hepMCVertex_ = Vertex(pos.z() * mm);
167  break; // there should be one single primary vertex
168  } // end loop over gen vertices
169  }
170  if (GenParticleHandle.isValid()) {
171  for (const auto& genpart : *GenParticleHandle) {
172  if ((genpart.status() != 1) || (genpart.numberOfMothers() == 0)) // not stable or one of the incoming hadrons
173  continue;
174  genVertex_ = Vertex(genpart.vz());
175  break;
176  }
177  }
178  if ((hepMCVertex_.vz() == 0.0) && (genVertex_.vz() == 0.0)) {
179  throw cms::Exception("Neither the HepMC vertex nor the generator particle vertex were found.");
180  }
181 
182  } // end InputData::InputData
183 
184  InputData::~InputData() {}
185 
186 } // end namespace l1tVertexFinder
TPRegexp parents
Definition: eve_filter.cc:21
const DetContainer & dets() const override
Returm a vector of all GeomDet (including all GeomDetUnits)
data_type const * const_iterator
Definition: DetSetNew.h:31
assert(be >=bs)
unsigned int debug() const
Definition: AlgoSettings.h:77
int iEvent
Definition: GenABIO.cc:224
uint32_t stack(const DetId &id) const
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:48
bool getData(T &iHolder) const
Definition: EventSetup.h:122
static constexpr auto TOB
Log< level::Info, false > LogInfo
boost::transform_iterator< IterHelp, const_IdIter > const_iterator
Definition: DetId.h:17
bool isLower(const DetId &id) const
bool isValid() const
Definition: HandleBase.h:70
unsigned int vx_minTracks() const
Definition: AlgoSettings.h:43
static constexpr auto TID