CMS 3D CMS Logo

VertexTableProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PhysicsTools/NanoAOD
4 // Class: VertexTableProducer
5 //
13 //
14 // Original Author: Andrea Rizzi
15 // Created: Mon, 28 Aug 2017 09:26:39 GMT
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 
22 // user include files
25 
28 
31 
34 
36 
43 
44 //
45 // class declaration
46 //
47 
49 public:
50  explicit VertexTableProducer(const edm::ParameterSet&);
51  ~VertexTableProducer() override;
52 
53  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
54 
55 private:
56  void beginStream(edm::StreamID) override;
57  void produce(edm::Event&, const edm::EventSetup&) override;
58  void endStream() override;
59 
60  //virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
61  //virtual void endRun(edm::Run const&, edm::EventSetup const&) override;
62  //virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
63  //virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
64 
65  // ----------member data ---------------------------
66 
76  const double dlenMin_, dlenSigMin_;
77  const bool storeCharge_;
78 };
79 
80 //
81 // constructors and destructor
82 //
84  : pvs_(consumes<std::vector<reco::Vertex>>(params.getParameter<edm::InputTag>("pvSrc"))),
85  pvsScore_(consumes<edm::ValueMap<float>>(params.getParameter<edm::InputTag>("pvSrc"))),
86  svs_(consumes<edm::View<reco::VertexCompositePtrCandidate>>(params.getParameter<edm::InputTag>("svSrc"))),
87  svCut_(params.getParameter<std::string>("svCut"), true),
88  goodPvCut_(params.getParameter<std::string>("goodPvCut"), true),
89  goodPvCutString_(params.getParameter<std::string>("goodPvCut")),
90  pvName_(params.getParameter<std::string>("pvName")),
91  svName_(params.getParameter<std::string>("svName")),
92  svDoc_(params.getParameter<std::string>("svDoc")),
93  dlenMin_(params.getParameter<double>("dlenMin")),
94  dlenSigMin_(params.getParameter<double>("dlenSigMin")),
95  storeCharge_(params.getParameter<bool>("storeCharge"))
96 
97 {
98  produces<nanoaod::FlatTable>("pv");
99  produces<nanoaod::FlatTable>("otherPVs");
100  produces<nanoaod::FlatTable>("svs");
101  produces<edm::PtrVector<reco::Candidate>>();
102 }
103 
105  // do anything here that needs to be done at destruction time
106  // (e.g. close files, deallocate resources etc.)
107 }
108 
109 //
110 // member functions
111 //
112 
113 // ------------ method called to produce the data ------------
114 
116  using namespace edm;
119  iEvent.getByToken(pvs_, pvsIn);
120  iEvent.getByToken(pvsScore_, pvsScoreIn);
121  auto pvTable = std::make_unique<nanoaod::FlatTable>(1, pvName_, true);
122  pvTable->addColumnValue<float>(
123  "ndof", (*pvsIn)[0].ndof(), "main primary vertex number of degree of freedom", nanoaod::FlatTable::FloatColumn, 8);
124  pvTable->addColumnValue<float>(
125  "x", (*pvsIn)[0].position().x(), "main primary vertex position x coordinate", nanoaod::FlatTable::FloatColumn, 10);
126  pvTable->addColumnValue<float>(
127  "y", (*pvsIn)[0].position().y(), "main primary vertex position y coordinate", nanoaod::FlatTable::FloatColumn, 10);
128  pvTable->addColumnValue<float>(
129  "z", (*pvsIn)[0].position().z(), "main primary vertex position z coordinate", nanoaod::FlatTable::FloatColumn, 16);
130  pvTable->addColumnValue<float>(
131  "chi2", (*pvsIn)[0].normalizedChi2(), "main primary vertex reduced chi2", nanoaod::FlatTable::FloatColumn, 8);
132  int goodPVs = 0;
133  for (const auto& pv : *pvsIn)
134  if (goodPvCut_(pv))
135  goodPVs++;
136  pvTable->addColumnValue<int>(
137  "npvs", (*pvsIn).size(), "total number of reconstructed primary vertices", nanoaod::FlatTable::IntColumn);
138  pvTable->addColumnValue<int>("npvsGood",
139  goodPVs,
140  "number of good reconstructed primary vertices. selection:" + goodPvCutString_,
142  pvTable->addColumnValue<float>("score",
143  (*pvsScoreIn).get(pvsIn.id(), 0),
144  "main primary vertex score, i.e. sum pt2 of clustered objects",
146  8);
147 
148  auto otherPVsTable =
149  std::make_unique<nanoaod::FlatTable>((*pvsIn).size() > 4 ? 3 : (*pvsIn).size() - 1, "Other" + pvName_, false);
150  std::vector<float> pvsz;
151  for (size_t i = 1; i < (*pvsIn).size() && i < 4; i++)
152  pvsz.push_back((*pvsIn)[i - 1].position().z());
153  otherPVsTable->addColumn<float>(
154  "z", pvsz, "Z position of other primary vertices, excluding the main PV", nanoaod::FlatTable::FloatColumn, 8);
155 
157  iEvent.getByToken(svs_, svsIn);
158  auto selCandSv = std::make_unique<PtrVector<reco::Candidate>>();
159  std::vector<float> dlen, dlenSig, pAngle, dxy, dxySig;
160  std::vector<int> charge;
161  VertexDistance3D vdist;
162  VertexDistanceXY vdistXY;
163 
164  size_t i = 0;
165  const auto& PV0 = pvsIn->front();
166  for (const auto& sv : *svsIn) {
167  if (svCut_(sv)) {
168  Measurement1D dl =
169  vdist.distance(PV0, VertexState(RecoVertex::convertPos(sv.position()), RecoVertex::convertError(sv.error())));
170  if (dl.value() > dlenMin_ and dl.significance() > dlenSigMin_) {
171  dlen.push_back(dl.value());
172  dlenSig.push_back(dl.significance());
173  edm::Ptr<reco::Candidate> c = svsIn->ptrAt(i);
174  selCandSv->push_back(c);
175  double dx = (PV0.x() - sv.vx()), dy = (PV0.y() - sv.vy()), dz = (PV0.z() - sv.vz());
176  double pdotv = (dx * sv.px() + dy * sv.py() + dz * sv.pz()) / sv.p() / sqrt(dx * dx + dy * dy + dz * dz);
177  pAngle.push_back(std::acos(pdotv));
178  Measurement1D d2d = vdistXY.distance(
179  PV0, VertexState(RecoVertex::convertPos(sv.position()), RecoVertex::convertError(sv.error())));
180  dxy.push_back(d2d.value());
181  dxySig.push_back(d2d.significance());
182 
183  if (storeCharge_) {
184  int sum_charge = 0;
185  for (unsigned int id = 0; id < sv.numberOfDaughters(); ++id) {
186  const reco::Candidate* daughter = sv.daughter(id);
187  sum_charge += daughter->charge();
188  }
189  charge.push_back(sum_charge);
190  }
191  }
192  }
193  i++;
194  }
195 
196  auto svsTable = std::make_unique<nanoaod::FlatTable>(selCandSv->size(), svName_, false);
197  // For SV we fill from here only stuff that cannot be created with the SimpleFlatTableProducer
198  svsTable->addColumn<float>("dlen", dlen, "decay length in cm", nanoaod::FlatTable::FloatColumn, 10);
199  svsTable->addColumn<float>("dlenSig", dlenSig, "decay length significance", nanoaod::FlatTable::FloatColumn, 10);
200  svsTable->addColumn<float>("dxy", dxy, "2D decay length in cm", nanoaod::FlatTable::FloatColumn, 10);
201  svsTable->addColumn<float>("dxySig", dxySig, "2D decay length significance", nanoaod::FlatTable::FloatColumn, 10);
202  svsTable->addColumn<float>(
203  "pAngle", pAngle, "pointing angle, i.e. acos(p_SV * (SV - PV)) ", nanoaod::FlatTable::FloatColumn, 10);
204  if (storeCharge_) {
205  svsTable->addColumn<int>("charge", charge, "sum of the charge of the SV tracks", nanoaod::FlatTable::IntColumn, 10);
206  }
207 
208  iEvent.put(std::move(pvTable), "pv");
209  iEvent.put(std::move(otherPVsTable), "otherPVs");
210  iEvent.put(std::move(svsTable), "svs");
211  iEvent.put(std::move(selCandSv));
212 }
213 
214 // ------------ method called once each stream before processing any runs, lumis or events ------------
216 
217 // ------------ method called once each stream after processing all runs, lumis and events ------------
219 
220 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
222  //The following says we do not know what parameters are allowed so do no validation
223  // Please change this to state exactly what you do use, even if it is no parameters
225  desc.setUnknown();
226  descriptions.addDefault(desc);
227 }
228 
229 //define this as a plug-in
reco::Vertex::Point convertPos(const GlobalPoint &p)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
const edm::EDGetTokenT< std::vector< reco::Vertex > > pvs_
const std::string pvName_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
const std::string svDoc_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
reco::Vertex::Error convertError(const GlobalError &ge)
Definition: ConvertError.h:8
const edm::EDGetTokenT< edm::ValueMap< float > > pvsScore_
Measurement1D distance(const GlobalPoint &vtx1Position, const GlobalError &vtx1PositionError, const GlobalPoint &vtx2Position, const GlobalError &vtx2PositionError) const override
const std::string goodPvCutString_
Measurement1D distance(const GlobalPoint &vtx1Position, const GlobalError &vtx1PositionError, const GlobalPoint &vtx2Position, const GlobalError &vtx2PositionError) const override
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
goodPVs
Good Primary Vertex Selection.
void addDefault(ParameterSetDescription const &psetDescription)
VertexTableProducer(const edm::ParameterSet &)
T sqrt(T t)
Definition: SSEVec.h:18
const edm::EDGetTokenT< edm::View< reco::VertexCompositePtrCandidate > > svs_
def pv(vc)
Definition: MetAnalyzer.py:7
double significance() const
Definition: Measurement1D.h:29
void produce(edm::Event &, const edm::EventSetup &) override
double value() const
Definition: Measurement1D.h:25
virtual int charge() const =0
electric charge
fixed size matrix
const std::string svName_
HLT enums.
void beginStream(edm::StreamID) override
const StringCutObjectSelector< reco::Vertex > goodPvCut_
const StringCutObjectSelector< reco::Candidate > svCut_
def move(src, dest)
Definition: eostools.py:511