CMS 3D CMS Logo

JetHTAnalyzer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: TkAlTools/JetHTAnalyzer
4 // Class: JetHTAnalyzer
5 //
13 //
14 // Original Author: Marco Musich
15 // Created: Fri, 29 Mar 2019 14:54:59 GMT
16 //
17 // Updated by: Jussi Viinikainen
18 //
19 
20 // system include files
21 #include <algorithm> // std::sort
22 #include <memory>
23 #include <string>
24 #include <vector> // std::vector
25 
26 // CMSSW includes
47 #include "DataFormats/Common/interface/TriggerResults.h" // Classes needed to print trigger results
49 
50 // ROOT includes
51 #include "TRandom.h"
52 #include "TTree.h"
53 #include "TString.h"
54 #include "TMath.h"
55 
56 //
57 // class declaration
58 //
59 
60 // If the analyzer does not use TFileService, please remove
61 // the template argument to the base class so the class inherits
62 // from edm::one::EDAnalyzer<>
63 // This will improve performance in multithreaded jobs.
64 
66 
67 class JetHTAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
68 public:
69  explicit JetHTAnalyzer(const edm::ParameterSet&);
70  ~JetHTAnalyzer() override;
71 
72  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
73  static bool mysorter(reco::Track i, reco::Track j) { return (i.pt() > j.pt()); }
74 
75 private:
76  void beginJob() override;
77  void analyze(const edm::Event&, const edm::EventSetup&) override;
78  void endJob() override;
79 
80  // ----------member data ---------------------------
81 
84 
87 
90 
92  double minVtxNdf_;
93  double minVtxWgt_;
94 
95  std::vector<double> profilePtBorders_;
96  std::vector<int> iovList_;
97 
99  TH1F* h_ntrks;
100  TH1F* h_probePt;
101  TH1F* h_probeEta;
102  TH1F* h_probePhi;
103  TH1F* h_probeDxy;
104  TH1F* h_probeDz;
107 
109 };
110 
111 //
112 // Constructor
113 //
115  : pvsTag_(iConfig.getParameter<edm::InputTag>("vtxCollection")),
116  pvsToken_(consumes<reco::VertexCollection>(pvsTag_)),
117  tracksTag_(iConfig.getParameter<edm::InputTag>("trackCollection")),
118  tracksToken_(consumes<reco::TrackCollection>(tracksTag_)),
119  triggerTag_(iConfig.getParameter<edm::InputTag>("triggerResults")),
120  triggerToken_(consumes<edm::TriggerResults>(triggerTag_)),
121  printTriggerTable_(iConfig.getUntrackedParameter<int>("printTriggerTable")),
122  minVtxNdf_(iConfig.getUntrackedParameter<double>("minVertexNdf")),
123  minVtxWgt_(iConfig.getUntrackedParameter<double>("minVertexMeanWeight")),
124  profilePtBorders_(iConfig.getUntrackedParameter<std::vector<double>>("profilePtBorders")),
125  iovList_(iConfig.getUntrackedParameter<std::vector<int>>("iovList")) {
126  // Specify that TFileService is used by the class
127  usesResource(TFileService::kSharedResource);
128 }
129 
130 //
131 // Default destructor
132 //
134 
135 //
136 // member functions
137 //
138 
139 // ------------ method called for each event ------------
141  using namespace edm;
142 
143  const double cmToum = 10000;
144 
145  const auto& vertices = iEvent.get(pvsToken_);
146  const reco::VertexCollection& pvtx = vertices;
147 
149 
150  // Find the IOV of the current event so that we can tag a histogram with the IOV
151  const int runNumber = iEvent.id().run();
152  std::string iovString = "iovNotFound";
153 
154  // Find the current IOV from the IOV list
155  if (runNumber >= iovList_.at(0)) { // If run number is smaller than the first item in the list, it is not in any IOV
156  for (std::vector<int>::size_type i = 1; i < iovList_.size(); i++) {
157  if (iovList_.at(i) > runNumber) {
158  iovString = Form("iov%d-%d", iovList_.at(i - 1), iovList_.at(i) - 1);
159  break;
160  }
161  }
162  }
163 
164  // Print the triggers to console
165  if (printTriggerTable_) {
166  const auto& triggerResults = iEvent.get(triggerToken_);
167 
168  const edm::TriggerNames& triggerNames = iEvent.triggerNames(triggerResults);
169  for (unsigned i = 0; i < triggerNames.size(); i++) {
170  const std::string& hltName = triggerNames.triggerName(i);
171  bool decision = triggerResults.accept(triggerNames.triggerIndex(hltName));
172  std::cout << hltName << " " << decision << std::endl;
173  }
174  }
175 
176  int counter = 0;
177  for (reco::VertexCollection::const_iterator pvIt = pvtx.begin(); pvIt != pvtx.end(); pvIt++) {
178  reco::Vertex iPV = *pvIt;
179  counter++;
180 
181  if (iPV.isFake())
182  continue;
184 
185  const math::XYZPoint pos_(iPV.x(), iPV.y(), iPV.z());
186 
187  // vertex selection as in bs code
188  if (iPV.ndof() < minVtxNdf_ || (iPV.ndof() + 3.) / iPV.tracksSize() < 2 * minVtxWgt_)
189  continue;
190 
192  for (trki = iPV.tracks_begin(); trki != iPV.tracks_end(); ++trki) {
193  if (trki->isNonnull()) {
194  reco::TrackRef trk_now(tracks, (*trki).key());
195  allTracks.push_back(*trk_now);
196  }
197  }
198 
199  // order with decreasing pt
200  std::sort(allTracks.begin(), allTracks.end(), mysorter);
201  uint ntrks = allTracks.size();
202  h_ntrks->Fill(ntrks);
203 
204  for (uint tracksIt = 0; tracksIt < ntrks; tracksIt++) {
205  auto tk = allTracks.at(tracksIt);
206 
207  double dxyRes = tk.dxy(pos_) * cmToum;
208  double dzRes = tk.dz(pos_) * cmToum;
209 
210  double dxy_err = tk.dxyError() * cmToum;
211  double dz_err = tk.dzError() * cmToum;
212 
213  float trackphi = tk.phi();
214  float tracketa = tk.eta();
215  float trackpt = tk.pt();
216 
217  h_probePt->Fill(trackpt);
218  h_probeEta->Fill(tracketa);
219  h_probePhi->Fill(trackphi);
220 
221  h_probeDxy->Fill(dxyRes);
222  h_probeDz->Fill(dzRes);
223  h_probeDxyErr->Fill(dxy_err);
224  h_probeDzErr->Fill(dz_err);
225 
226  mon.fillHisto("dxy", "all", dxyRes, 1.);
227  mon.fillHisto("dz", "all", dzRes, 1.);
228  mon.fillHisto("dxyerr", "all", dxy_err, 1.);
229  mon.fillHisto("dzerr", "all", dz_err, 1.);
230 
231  mon.fillProfile("dxyErrVsPt", "all", trackpt, dxy_err, 1.);
232  mon.fillProfile("dzErrVsPt", "all", trackpt, dz_err, 1.);
233 
234  mon.fillProfile("dxyErrVsPhi", "all", trackphi, dxy_err, 1.);
235  mon.fillProfile("dzErrVsPhi", "all", trackphi, dz_err, 1.);
236 
237  mon.fillProfile("dxyErrVsEta", "all", tracketa, dxy_err, 1.);
238  mon.fillProfile("dzErrVsEta", "all", tracketa, dz_err, 1.);
239 
240  // Integrated pT bins
241  for (std::vector<double>::size_type i = 0; i < profilePtBorders_.size(); i++) {
242  if (trackpt < profilePtBorders_.at(i))
243  break;
244  mon.fillProfile("dxyErrVsPtWide", "all", i, dxy_err, 1.);
245  mon.fillProfile("dzErrVsPtWide", "all", i, dz_err, 1.);
246  }
247 
248  // Fill IOV specific histograms
249  mon.fillHisto("dxy", iovString, dxyRes, 1.);
250  mon.fillHisto("dz", iovString, dzRes, 1.);
251  mon.fillHisto("dxyerr", iovString, dxy_err, 1.);
252  mon.fillHisto("dzerr", iovString, dz_err, 1.);
253 
254  mon.fillProfile("dxyErrVsPt", iovString, trackpt, dxy_err, 1.);
255  mon.fillProfile("dzErrVsPt", iovString, trackpt, dz_err, 1.);
256 
257  mon.fillProfile("dxyErrVsPhi", iovString, trackphi, dxy_err, 1.);
258  mon.fillProfile("dzErrVsPhi", iovString, trackphi, dz_err, 1.);
259 
260  mon.fillProfile("dxyErrVsEta", iovString, tracketa, dxy_err, 1.);
261  mon.fillProfile("dzErrVsEta", iovString, tracketa, dz_err, 1.);
262 
263  // Integrated pT bins
264  for (std::vector<double>::size_type i = 0; i < profilePtBorders_.size(); i++) {
265  if (trackpt < profilePtBorders_.at(i))
266  break;
267  mon.fillProfile("dxyErrVsPtWide", iovString, i, dxy_err, 1.);
268  mon.fillProfile("dzErrVsPtWide", iovString, i, dz_err, 1.);
269  }
270 
271  if (std::abs(tracketa) < 1.) {
272  mon.fillHisto("dxy", "central", dxyRes, 1.);
273  mon.fillHisto("dz", "central", dzRes, 1.);
274  mon.fillHisto("dxyerr", "central", dxy_err, 1.);
275  mon.fillHisto("dzerr", "central", dz_err, 1.);
276 
277  mon.fillProfile("dxyErrVsPt", "central", trackpt, dxy_err, 1.);
278  mon.fillProfile("dzErrVsPt", "central", trackpt, dz_err, 1.);
279 
280  mon.fillProfile("dxyErrVsPhi", "central", trackphi, dxy_err, 1.);
281  mon.fillProfile("dzErrVsPhi", "central", trackphi, dz_err, 1.);
282 
283  // Integrated pT bins
284  for (std::vector<double>::size_type i = 0; i < profilePtBorders_.size(); i++) {
285  if (trackpt < profilePtBorders_.at(i))
286  break;
287  mon.fillProfile("dxyErrVsPtWide", "central", i, dxy_err, 1.);
288  mon.fillProfile("dzErrVsPtWide", "central", i, dz_err, 1.);
289  }
290  }
291 
292  } // loop on tracks in vertex
293  } // loop on vertices
294 
295  mon.fillHisto("nvtx", "all", counter, 1.);
296 }
297 
298 // ------------ method called once each job just before starting event loop ------------
300  h_ntrks = outfile_->make<TH1F>("h_ntrks", "n. trks;n. of tracks/vertex;n. vertices", 100, 0, 100);
301  h_probePt = outfile_->make<TH1F>("h_probePt", "p_{T} of probe track;track p_{T} (GeV); tracks", 100, 0., 500.);
302  h_probeEta = outfile_->make<TH1F>("h_probeEta", "#eta of the probe track;track #eta;tracks", 54, -2.8, 2.8);
303  h_probePhi = outfile_->make<TH1F>("h_probePhi", "#phi of probe track;track #phi (rad);tracks", 100, -3.15, 3.15);
304 
305  h_probeDxy =
306  outfile_->make<TH1F>("h_probeDxy", "d_{xy}(PV) of the probe track;track d_{xy}(PV);tracks", 200, -100, 100);
307  h_probeDz = outfile_->make<TH1F>("h_probeDz", "d_{z}(PV) of the probe track;track d_{z}(PV);tracks", 200, -100, 100);
308  h_probeDxyErr = outfile_->make<TH1F>(
309  "h_probeDxyErr", "error on d_{xy}(PV) of the probe track;track error on d_{xy}(PV);tracks", 100, 0., 100);
310  h_probeDzErr = outfile_->make<TH1F>(
311  "h_probeDzErr", "error on d_{z}(PV) of the probe track;track error on d_{z}(PV);tracks", 100, 0., 100);
312 
313  mon.addHistogram(new TH1F("nvtx", ";Vertices;Events", 50, 0, 50));
314  mon.addHistogram(new TH1F("dxy", ";d_{xy};tracks", 100, -100, 100));
315  mon.addHistogram(new TH1F("dz", ";d_{z};tracks", 100, -100, 100));
316  mon.addHistogram(new TH1F("dxyerr", ";d_{xy} error;tracks", 100, 0., 200));
317  mon.addHistogram(new TH1F("dzerr", ";d_{z} error;tracks", 100, 0., 200));
318  mon.addHistogram(new TProfile("dxyErrVsPt", ";track p_{T};d_{xy} error", 100, 0., 200, 0., 100.));
319  mon.addHistogram(new TProfile("dzErrVsPt", ";track p_{T};d_{z} error", 100, 0., 200, 0., 100.));
321  new TProfile("dxyErrVsPhi", ";track #varphi;d_{xy} error", 100, -TMath::Pi(), TMath::Pi(), 0., 100.));
322  mon.addHistogram(new TProfile("dzErrVsPhi", ";track #varphi;d_{z} error", 100, -TMath::Pi(), TMath::Pi(), 0., 100.));
323  mon.addHistogram(new TProfile("dxyErrVsEta", ";track #eta;d_{xy} error", 100, -2.5, 2.5, 0., 100.));
324  mon.addHistogram(new TProfile("dzErrVsEta", ";track #eta;d_{z} error", 100, -2.5, 2.5, 0., 100.));
325 
326  // Variable size histogram depending on the given pT bin borders
327  int nBins = profilePtBorders_.size();
329  new TProfile("dxyErrVsPtWide", ";track p_{T} wide bin;d_{xy} error", nBins, -0.5, nBins - 0.5, 0.0, 100.0));
331  new TProfile("dzErrVsPtWide", ";track p_{T} wide bin;d_{z} error", nBins, -0.5, nBins - 0.5, 0.0, 100.0));
332 }
333 
334 // ------------ method called once each job just after ending the event loop ------------
336 
337 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
340  desc.setComment("JetHT validation analyzer plugin.");
341  desc.add<edm::InputTag>("vtxCollection", edm::InputTag("offlinePrimaryVerticesFromRefittedTrks"));
342  desc.add<edm::InputTag>("triggerResults", edm::InputTag("TriggerResults", "", "HLT"));
343  desc.add<edm::InputTag>("trackCollection", edm::InputTag("TrackRefitter"));
344  descriptions.add("JetHTAnalyzer", desc);
345 }
346 
347 //define this as a plug-in
static const std::string kSharedResource
Definition: TFileService.h:76
const double Pi
static bool mysorter(reco::Track i, reco::Track j)
bool fillHisto(std::string name, std::string tag, double valx, double weight, bool useBinWidth=false)
double z() const
z coordinate
Definition: Vertex.h:133
bool isFake() const
Definition: Vertex.h:76
std::vector< int > iovList_
TH1F * h_probeDxyErr
JetHTAnalyzer(const edm::ParameterSet &)
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
edm::InputTag tracksTag_
std::vector< double > profilePtBorders_
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
double ndof() const
Definition: Vertex.h:123
std::vector< Vertex > VertexCollection
Definition: Vertex.h:31
uint16_t size_type
trackRef_iterator tracks_end() const
last iterator over tracks
Definition: Vertex.h:110
SmartSelectionMonitor mon
size_t tracksSize() const
number of tracks
Definition: Vertex.h:112
edm::InputTag triggerTag_
int iEvent
Definition: GenABIO.cc:224
TH1 * addHistogram(TH1 *h, std::string tag)
void endJob() override
bool fillProfile(std::string name, std::string tag, double valx, double valy, double weight)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
trackRef_iterator tracks_begin() const
first iterator over tracks
Definition: Vertex.h:108
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
double x() const
x coordinate
Definition: Vertex.h:129
static std::string const triggerResults
Definition: EdmProvDump.cc:47
edm::Service< TFileService > outfile_
double y() const
y coordinate
Definition: Vertex.h:131
void analyze(const edm::Event &, const edm::EventSetup &) override
auto const & tracks
cannot be loose
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
void add(std::string const &label, ParameterSetDescription const &psetDescription)
~JetHTAnalyzer() override
void beginJob() override
fixed size matrix
HLT enums.
edm::InputTag pvsTag_
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
edm::EDGetTokenT< reco::TrackCollection > tracksToken_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
static constexpr float cmToum
edm::EDGetTokenT< reco::VertexCollection > pvsToken_
std::vector< TrackBaseRef >::const_iterator trackRef_iterator
The iteratator for the vector<TrackRef>
Definition: Vertex.h:38
edm::EDGetTokenT< edm::TriggerResults > triggerToken_