CMS 3D CMS Logo

PatBJetVertexAnalyzer.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <iostream>
3 #include <cmath>
4 #include <vector>
5 #include <string>
6 
7 #include <TH1.h>
8 #include <TProfile.h>
9 
10 #include <Math/VectorUtil.h>
11 #include <Math/GenVector/PxPyPzE4D.h>
12 #include <Math/GenVector/PxPyPzM4D.h>
13 
21 
23 
32 
34 public:
37  ~PatBJetVertexAnalyzer() override;
38 
39  // virtual methods called from base class EDAnalyzer
40  void beginJob() override;
41  void analyze(const edm::Event &event, const edm::EventSetup &es) override;
42 
43 private:
44  // configuration parameters
46 
47  double jetPtCut_; // minimum (uncorrected) jet energy
48  double jetEtaCut_; // maximum |eta| for jet
49  double maxDeltaR_; // angle between jet and tracks
50 
51  // jet flavour constants
52 
54 
55  TH1 *flavours_;
56 
57  // one group of plots per jet flavour;
58  struct Plots {
59  TH1 *nVertices;
60  TH1 *deltaR, *mass, *dist, *distErr, *distSig;
61  TH1 *nTracks, *chi2;
63 };
64 
66  : jetsToken_(consumes<pat::JetCollection>(params.getParameter<edm::InputTag>("jets"))),
67  jetPtCut_(params.getParameter<double>("jetPtCut")),
68  jetEtaCut_(params.getParameter<double>("jetEtaCut")) {}
69 
71 
73  // retrieve handle to auxiliary service
74  // used for storing histograms into ROOT file
76 
77  flavours_ = fs->make<TH1F>("flavours", "jet flavours", 5, 0, 5);
78 
79  // book histograms for all jet flavours
80  for (unsigned int i = 0; i < N_JET_TYPES; i++) {
81  Plots &plots = plots_[i];
82  const char *flavour, *name;
83 
84  switch ((Flavour)i) {
85  case ALL_JETS:
86  flavour = "all jets";
87  name = "all";
88  break;
89  case UDSG_JETS:
90  flavour = "light flavour jets";
91  name = "udsg";
92  break;
93  case C_JETS:
94  flavour = "charm jets";
95  name = "c";
96  break;
97  case B_JETS:
98  flavour = "bottom jets";
99  name = "b";
100  break;
101  default:
102  flavour = "unidentified jets";
103  name = "ni";
104  break;
105  }
106 
107  plots.nVertices =
108  fs->make<TH1F>(Form("nVertices_%s", name), Form("number of secondary vertices in %s", flavour), 5, 0, 5);
109  plots.deltaR = fs->make<TH1F>(Form("deltaR_%s", name),
110  Form("\\DeltaR between vertex direction and jet direction in %s", flavour),
111  100,
112  0,
113  0.5);
114  plots.mass = fs->make<TH1F>(Form("mass_%s", name), Form("vertex mass in %s", flavour), 100, 0, 10);
115  plots.dist =
116  fs->make<TH1F>(Form("dist_%s", name), Form("Transverse distance between PV and SV in %s", flavour), 100, 0, 2);
117  plots.distErr = fs->make<TH1F>(
118  Form("distErr_%s", name), Form("Transverse distance error between PV and SV in %s", flavour), 100, 0, 0.5);
119  plots.distSig = fs->make<TH1F>(
120  Form("distSig_%s", name), Form("Transverse distance significance between PV and SV in %s", flavour), 100, 0, 50);
121  plots.nTracks = fs->make<TH1F>(
122  Form("nTracks_%s", name), Form("number of tracks at secondary vertex in %s", flavour), 20, 0, 20);
123  plots.chi2 =
124  fs->make<TH1F>(Form("chi2_%s", name), Form("secondary vertex fit \\chi^{2} in %s", flavour), 100, 0, 50);
125  }
126 }
127 
128 // helper function to sort the tracks by impact parameter significance
129 
131  // handle to the jets collection
133  event.getByToken(jetsToken_, jetsHandle);
134 
135  // now go through all jets
136  for (pat::JetCollection::const_iterator jet = jetsHandle->begin(); jet != jetsHandle->end(); ++jet) {
137  // only look at jets that pass the pt and eta cut
138  if (jet->pt() < jetPtCut_ || std::abs(jet->eta()) > jetEtaCut_)
139  continue;
140 
142  // find out the jet flavour (differs between quark and anti-quark)
143  switch (std::abs(jet->partonFlavour())) {
144  case 1:
145  case 2:
146  case 3:
147  case 21:
148  flavour = UDSG_JETS;
149  break;
150  case 4:
151  flavour = C_JETS;
152  break;
153  case 5:
154  flavour = B_JETS;
155  break;
156  default:
157  flavour = NONID_JETS;
158  }
159 
160  // simply count the number of accepted jets
161  flavours_->Fill(ALL_JETS);
162  flavours_->Fill(flavour);
163 
164  // retrieve the "secondary vertex tag infos"
165  // this is the output of the b-tagging reconstruction code
166  // and contains secondary vertices in the jets
167  const reco::SecondaryVertexTagInfo &svTagInfo = *jet->tagInfoSecondaryVertex();
168 
169  // count the number of secondary vertices
170  plots_[ALL_JETS].nVertices->Fill(svTagInfo.nVertices());
171  plots_[flavour].nVertices->Fill(svTagInfo.nVertices());
172 
173  // ignore jets without SV from now on
174  if (svTagInfo.nVertices() < 1)
175  continue;
176 
177  // pick the first secondary vertex (the "best" one)
178  const reco::Vertex &sv = svTagInfo.secondaryVertex(0);
179 
180  // and plot number of tracks and chi^2
181  plots_[ALL_JETS].nTracks->Fill(sv.tracksSize());
182  plots_[flavour].nTracks->Fill(sv.tracksSize());
183 
184  plots_[ALL_JETS].chi2->Fill(sv.chi2());
185  plots_[flavour].chi2->Fill(sv.chi2());
186 
187  // the precomputed transverse distance to the primary vertex
188  Measurement1D distance = svTagInfo.flightDistance(0, true);
189 
190  plots_[ALL_JETS].dist->Fill(distance.value());
191  plots_[flavour].dist->Fill(distance.value());
192 
193  plots_[ALL_JETS].distErr->Fill(distance.error());
194  plots_[flavour].distErr->Fill(distance.error());
195 
196  plots_[ALL_JETS].distSig->Fill(distance.significance());
197  plots_[flavour].distSig->Fill(distance.significance());
198 
199  // the precomputed direction with respect to the primary vertex
200  const GlobalVector &dir = svTagInfo.flightDirection(0);
201 
202  // unfortunately CMSSW hsa all kinds of vectors,
203  // and sometimes we need to convert them *sigh*
204  math::XYZVector dir2(dir.x(), dir.y(), dir.z());
205 
206  // compute a few variables that we are plotting
207  double deltaR = ROOT::Math::VectorUtil::DeltaR(jet->momentum(), dir2);
208 
209  plots_[ALL_JETS].deltaR->Fill(deltaR);
210  plots_[flavour].deltaR->Fill(deltaR);
211 
212  // compute the invariant mass from a four-vector sum
213  math::XYZTLorentzVector trackFourVectorSum;
214 
215  // loop over all tracks in the vertex
217  ROOT::Math::LorentzVector<ROOT::Math::PxPyPzM4D<double> > vec;
218  vec.SetPx((*track)->px());
219  vec.SetPy((*track)->py());
220  vec.SetPz((*track)->pz());
221  vec.SetM(0.13957); // pion mass
222  trackFourVectorSum += vec;
223  }
224 
225  // get the invariant mass: sqrt(E² - px² - py² - pz²)
226  double vertexMass = trackFourVectorSum.M();
227 
228  plots_[ALL_JETS].mass->Fill(vertexMass);
229  plots_[flavour].mass->Fill(vertexMass);
230  }
231 }
232 
234 
std::vector< Jet > JetCollection
Definition: Jet.h:53
trackRef_iterator tracks_end() const
last iterator over tracks
Definition: Vertex.cc:73
const VTX & secondaryVertex(unsigned int index) const
T y() const
Definition: PV3DBase.h:60
double error() const
Definition: Measurement1D.h:27
edm::EDGetTokenT< pat::JetCollection > jetsToken_
void analyze(const edm::Event &event, const edm::EventSetup &es) override
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: HeavyIon.h:7
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
struct PatBJetVertexAnalyzer::Plots plots_[N_JET_TYPES]
PatBJetVertexAnalyzer(const edm::ParameterSet &params)
constructor and destructor
T z() const
Definition: PV3DBase.h:61
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double chi2() const
chi-squares
Definition: Vertex.h:102
const GlobalVector & flightDirection(unsigned int index) const
double significance() const
Definition: Measurement1D.h:29
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
double value() const
Definition: Measurement1D.h:25
HLT enums.
Measurement1D flightDistance(unsigned int index, int dim=0) const
trackRef_iterator tracks_begin() const
first iterator over tracks
Definition: Vertex.cc:71
T x() const
Definition: PV3DBase.h:59
std::vector< TrackBaseRef >::const_iterator trackRef_iterator
The iteratator for the vector<TrackRef>
Definition: Vertex.h:37
size_t tracksSize() const
number of tracks
Definition: Vertex.cc:69
Definition: event.py:1