CMS 3D CMS Logo

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