CMS 3D CMS Logo

LhcTrackAnalyzer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: LhcTrackAnalyzer
4 // Class: LhcTrackAnalyzer
5 //
13 //
14 
15 // system include files
16 #include <memory>
17 #include <string>
18 #include <vector>
19 
20 // user include files
34 
35 // ROOT includes
36 #include "TFile.h"
37 #include "TTree.h"
38 
39 //
40 // class decleration
41 //
42 
44 public:
45  explicit LhcTrackAnalyzer(const edm::ParameterSet&);
46  ~LhcTrackAnalyzer() override = default;
47  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
48 
49 private:
50  void beginJob() override;
51  void analyze(const edm::Event&, const edm::EventSetup&) override;
52  void endJob() override;
53 
54  // ----------member data ---------------------------
57  bool debug_;
58  std::vector<unsigned int> acceptedBX_;
59 
62 
63  // Output
65  TFile* rootFile_;
66  TTree* rootTree_;
67 
68  // Root-Tuple variables :
69  //=======================
70  void SetVarToZero();
71 
72  static constexpr int nMaxtracks_ = 3000;
73  int nTracks_;
74  int run_;
75  int event_;
76  double pt_[nMaxtracks_];
77  double eta_[nMaxtracks_];
78  double phi_[nMaxtracks_];
79  double chi2_[nMaxtracks_];
83  double dz_[nMaxtracks_];
84  double dxy_[nMaxtracks_];
85  double dzPV_[nMaxtracks_];
93  double xPCA_[nMaxtracks_];
94  double yPCA_[nMaxtracks_];
95  double zPCA_[nMaxtracks_];
100  bool goodbx_;
101  bool goodvtx_;
102 };
103 
104 // Constructor
105 
107  : TrackCollectionTag_(iConfig.getParameter<edm::InputTag>("TrackCollectionTag")),
108  PVtxCollectionTag_(iConfig.getParameter<edm::InputTag>("PVtxCollectionTag")),
109  debug_(iConfig.getParameter<bool>("Debug")),
110  acceptedBX_(iConfig.getParameter<std::vector<unsigned int>>("acceptedBX")),
111  filename_(iConfig.getParameter<std::string>("OutputFileName")) {
112  //now do what ever initialization is needed
113  theTrackCollectionToken = consumes<reco::TrackCollection>(TrackCollectionTag_);
114  theVertexCollectionToken = consumes<reco::VertexCollection>(PVtxCollectionTag_);
115 }
116 
117 //
118 // member functions
119 //
120 
121 /*****************************************************************************/
123 /*****************************************************************************/
124 {
125  using namespace edm;
126  using namespace reco;
127  using namespace std;
128 
129  //=======================================================
130  // Initialize Root-tuple variables
131  //=======================================================
132 
133  SetVarToZero();
134 
135  //=======================================================
136  // Retrieve the Track information
137  //=======================================================
138 
139  // Declare the handle for the vertex collection
140  const auto& vertexHandle = iEvent.getHandle(theVertexCollectionToken);
141 
142  // Check if the handle is valid
143  if (!vertexHandle.isValid()) {
144  edm::LogError("LhcTrackAnalyzer") << "Vertex collection not found or invalid.";
145  return; // Early return if the vertex collection is not valid
146  }
147 
148  // Retrieve the actual product from the handle
149  const reco::VertexCollection& vertices = *vertexHandle;
150 
151  const auto& vtx = vertices.front();
152  if (vtx.isFake()) {
153  goodvtx_ = false;
154  } else {
155  goodvtx_ = true;
156  }
157 
158  int bx = iEvent.bunchCrossing();
159  if (acceptedBX_.empty()) {
160  goodbx_ = true;
161  } else {
162  if (std::find(acceptedBX_.begin(), acceptedBX_.end(), bx) != acceptedBX_.end()) {
163  goodbx_ = true;
164  }
165  }
166 
167  run_ = iEvent.id().run();
168  event_ = iEvent.id().event();
169 
170  const auto& tracksHandle = iEvent.getHandle(theTrackCollectionToken);
171 
172  // Check if the handle is valid
173  if (!tracksHandle.isValid()) {
174  edm::LogError("LhcTrackAnalyzer") << "Tracks collection not found or invalid.";
175  return; // Early return if the vertex collection is not valid
176  }
177 
178  // Retrieve the actual product from the handle
179  const reco::TrackCollection& tracks = *tracksHandle;
180 
181  if (debug_) {
182  edm::LogInfo("LhcTrackAnalyzer") << "LhcTrackAnalyzer::analyze() looping over " << tracks.size() << "tracks."
183  << endl;
184  }
185 
186  for (const auto& track : tracks) {
187  if (nTracks_ >= nMaxtracks_) {
188  edm::LogWarning("LhcTrackAnalyzer")
189  << " LhcTrackAnalyzer::analyze() : Warning - Run " << run_ << " Event " << event_
190  << "\tNumber of tracks: " << tracks.size() << " , greater than " << nMaxtracks_ << std::endl;
191  continue;
192  }
193  pt_[nTracks_] = track.pt();
194  eta_[nTracks_] = track.eta();
195  phi_[nTracks_] = track.phi();
196  chi2_[nTracks_] = track.chi2();
197  chi2ndof_[nTracks_] = track.normalizedChi2();
198  charge_[nTracks_] = track.charge();
199  qoverp_[nTracks_] = track.qoverp();
200  dz_[nTracks_] = track.dz();
201  dxy_[nTracks_] = track.dxy();
202  dzError_[nTracks_] = track.dzError();
203  dxyError_[nTracks_] = track.dxyError();
204  dzSig_[nTracks_] = track.dz() / track.dzError();
205  dxySig_[nTracks_] = track.dxy() / track.dxyError();
206 
207  const reco::Vertex* closestVertex = nullptr;
209 
210  // Loop over vertices to find the closest one in dz
211  for (const auto& vertex : vertices) {
212  float dz = fabs(track.dz(vertex.position()));
213  if (dz < minDz) {
214  minDz = dz;
215  closestVertex = &vertex;
216  }
217  }
218 
219  float dzPV{-999.};
220  float dxyPV{-999.};
221  if (closestVertex) {
222  // Compute dxy and dz with respect to the closest vertex
223  dzPV = track.dz(closestVertex->position());
224  dxyPV = track.dxy(closestVertex->position());
225  }
226 
227  dzPV_[nTracks_] = dzPV;
228  dxyPV_[nTracks_] = dxyPV;
229 
230  dzPVSig_[nTracks_] = dzPV / track.dzError();
231  dxyPVSig_[nTracks_] = dxyPV / track.dxyError();
232 
233  xPCA_[nTracks_] = track.vertex().x();
234  yPCA_[nTracks_] = track.vertex().y();
235  zPCA_[nTracks_] = track.vertex().z();
236  validhits_[nTracks_][0] = track.numberOfValidHits();
237  validhits_[nTracks_][1] = track.hitPattern().numberOfValidPixelBarrelHits();
238  validhits_[nTracks_][2] = track.hitPattern().numberOfValidPixelEndcapHits();
239  validhits_[nTracks_][3] = track.hitPattern().numberOfValidStripTIBHits();
240  validhits_[nTracks_][4] = track.hitPattern().numberOfValidStripTIDHits();
241  validhits_[nTracks_][5] = track.hitPattern().numberOfValidStripTOBHits();
242  validhits_[nTracks_][6] = track.hitPattern().numberOfValidStripTECHits();
243 
244  int myalgo = -88;
245  if (track.algo() == reco::TrackBase::undefAlgorithm) {
246  myalgo = 0;
247  } else if (track.algo() == reco::TrackBase::ctf) {
248  myalgo = 1;
249  } else if (track.algo() == reco::TrackBase::duplicateMerge) {
250  myalgo = 2;
251  } else if (track.algo() == reco::TrackBase::cosmics) {
252  myalgo = 3;
253  } else if (track.algo() == reco::TrackBase::initialStep) {
254  myalgo = 4;
255  } else if (track.algo() == reco::TrackBase::lowPtTripletStep) {
256  myalgo = 5;
257  } else if (track.algo() == reco::TrackBase::pixelPairStep) {
258  myalgo = 6;
259  } else if (track.algo() == reco::TrackBase::detachedTripletStep) {
260  myalgo = 7;
261  } else if (track.algo() == reco::TrackBase::mixedTripletStep) {
262  myalgo = 8;
263  } else if (track.algo() == reco::TrackBase::pixelLessStep) {
264  myalgo = 9;
265  } else if (track.algo() == reco::TrackBase::tobTecStep) {
266  myalgo = 10;
267  } else if (track.algo() == reco::TrackBase::jetCoreRegionalStep) {
268  myalgo = 11;
269  } else if (track.algo() == reco::TrackBase::muonSeededStepInOut) {
270  myalgo = 13;
271  } else if (track.algo() == reco::TrackBase::muonSeededStepOutIn) {
272  myalgo = 14;
273  } else if (track.algo() == reco::TrackBase::highPtTripletStep) {
274  myalgo = 22;
275  } else if (track.algo() == reco::TrackBase::lowPtQuadStep) {
276  myalgo = 23;
277  } else if (track.algo() == reco::TrackBase::detachedQuadStep) {
278  myalgo = 24;
279  } else if (track.algo() == reco::TrackBase::displacedGeneralStep) {
280  myalgo = 25;
281  } else if (track.algo() == reco::TrackBase::displacedRegionalStep) {
282  myalgo = 26;
283  } else if (track.algo() == reco::TrackBase::hltIter0) {
284  myalgo = 31;
285  } else {
286  myalgo = reco::TrackBase::algoSize;
287  edm::LogWarning("LhcTrackAnalyzer")
288  << "LhcTrackAnalyzer does not support all types of tracks, encountered one from algo "
289  << reco::TrackBase::algoName(track.algo());
290  }
291  trkAlgo_[nTracks_] = myalgo;
292 
293  int myquality = -99;
295  myquality = -1;
296  if (track.quality(reco::TrackBase::loose))
297  myquality = 0;
298  if (track.quality(reco::TrackBase::tight))
299  myquality = 1;
300  if (track.quality(reco::TrackBase::highPurity))
301  myquality = 2;
302  trkQuality_[nTracks_] = myquality;
303 
304  if (track.quality(reco::TrackBase::highPurity))
305  isHighPurity_[nTracks_] = 1;
306  else
307  isHighPurity_[nTracks_] = 0;
308  nTracks_++;
309 
310  } //end loop on tracks
311 
312  for (int d = 0; d < nTracks_; ++d) {
313  if (abs(trkQuality_[d]) > 5)
314  edm::LogInfo("LhcTrackAnalyzer") << "MYQUALITY!!! " << trkQuality_[d] << " at track # " << d << "/" << nTracks_
315  << endl;
316  }
317 
318  rootTree_->Fill();
319 }
320 
321 /*****************************************************************************/
323 /*****************************************************************************/
324 {
325  edm::LogInfo("beginJob") << "Begin Job" << std::endl;
326  // Define TTree for output
327  rootFile_ = new TFile(filename_.c_str(), "recreate");
328  rootTree_ = new TTree("tree", "Lhc Track tree");
329 
330  // Track Paramters
331  rootTree_->Branch("run", &run_, "run/I");
332  rootTree_->Branch("event", &event_, "event/I");
333  rootTree_->Branch("goodbx", &goodbx_, "goodbx/O");
334  rootTree_->Branch("goodvtx", &goodvtx_, "goodvtx/O");
335  rootTree_->Branch("nTracks", &nTracks_, "nTracks/I");
336  rootTree_->Branch("pt", &pt_, "pt[nTracks]/D");
337  rootTree_->Branch("eta", &eta_, "eta[nTracks]/D");
338  rootTree_->Branch("phi", &phi_, "phi[nTracks]/D");
339  rootTree_->Branch("chi2", &chi2_, "chi2[nTracks]/D");
340  rootTree_->Branch("chi2ndof", &chi2ndof_, "chi2ndof[nTracks]/D");
341  rootTree_->Branch("charge", &charge_, "charge[nTracks]/I");
342  rootTree_->Branch("qoverp", &qoverp_, "qoverp[nTracks]/D");
343  rootTree_->Branch("dz", &dz_, "dz[nTracks]/D");
344  rootTree_->Branch("dxy", &dxy_, "dxy[nTracks]/D");
345  rootTree_->Branch("dzPV", &dzPV_, "dzPV[nTracks]/D");
346  rootTree_->Branch("dxyPV", &dxy_, "dxyPV[nTracks]/D");
347  rootTree_->Branch("dzError", &dzError_, "dzError[nTracks]/D");
348  rootTree_->Branch("dxyError", &dxyError_, "dxyError[nTracks]/D");
349  rootTree_->Branch("dzSig", &dzSig_, "dzSig[nTracks]/D");
350  rootTree_->Branch("dxySig", &dxySig_, "dxySig[nTracks]/D");
351  rootTree_->Branch("dzPVSig", &dzPVSig_, "dzPVSig[nTracks]/D");
352  rootTree_->Branch("dxyPVSig", &dxyPVSig_, "dxyPVSig[nTracks]/D");
353  rootTree_->Branch("xPCA", &xPCA_, "xPCA[nTracks]/D");
354  rootTree_->Branch("yPCA", &yPCA_, "yPCA[nTracks]/D");
355  rootTree_->Branch("zPCA", &zPCA_, "zPCA[nTracks]/D");
356  rootTree_->Branch("isHighPurity", &isHighPurity_, "isHighPurity[nTracks]/I");
357  rootTree_->Branch("trkQuality", &trkQuality_, "trkQuality[nTracks]/I");
358  rootTree_->Branch("trkAlgo", &trkAlgo_, "trkAlgo[nTracks]/I");
359  rootTree_->Branch("nValidHits", &validhits_, "nValidHits[nTracks][7]/I");
360 }
361 
362 /*****************************************************************************/
364 /*****************************************************************************/
365 {
366  if (rootFile_) {
367  rootFile_->Write();
368  rootFile_->Close();
369  }
370 }
371 
372 /*****************************************************************************/
374 /*****************************************************************************/
375 {
377  desc.setComment("Ntuplizer for LHC tracks");
378  desc.add<edm::InputTag>("TrackCollectionTag", edm::InputTag("ALCARECOTkAlMinBias"));
379  desc.add<edm::InputTag>("PVtxCollectionTag", edm::InputTag("offlinePrimaryVertices"));
380  desc.add<bool>("Debug", false);
381  desc.add<std::vector<unsigned int>>("acceptedBX", {});
382  desc.add<std::string>("OutputFileName", "LhcTrackAnalyzer_Output_default.root");
383  descriptions.addWithDefaultLabel(desc);
384 }
385 
386 /*****************************************************************************/
388 /*****************************************************************************/
389 {
390  run_ = -1;
391  event_ = -99;
392  nTracks_ = 0;
393  for (int i = 0; i < nMaxtracks_; ++i) {
394  pt_[i] = 0;
395  eta_[i] = 0;
396  phi_[i] = 0;
397  chi2_[i] = 0;
398  chi2ndof_[i] = 0;
399  charge_[i] = 0;
400  qoverp_[i] = 0;
401  dz_[i] = 0;
402  dxy_[i] = 0;
403  xPCA_[i] = 0;
404  yPCA_[i] = 0;
405  zPCA_[i] = 0;
406  trkQuality_[i] = 0;
407  trkAlgo_[i] = -1;
408  isHighPurity_[i] = -3;
409  for (int j = 0; j < 7; j++) {
410  validhits_[nTracks_][j] = -1 * j;
411  }
412  }
413 }
414 
415 //define this as a plug-in
edm::EDGetTokenT< reco::TrackCollection > theTrackCollectionToken
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
int validhits_[nMaxtracks_][7]
const Point & position() const
position
Definition: Vertex.h:128
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
int isHighPurity_[nMaxtracks_]
double dz_[nMaxtracks_]
double zPCA_[nMaxtracks_]
edm::InputTag TrackCollectionTag_
Log< level::Error, false > LogError
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
int trkQuality_[nMaxtracks_]
double qoverp_[nMaxtracks_]
std::vector< unsigned int > acceptedBX_
edm::EDGetTokenT< reco::VertexCollection > theVertexCollectionToken
~LhcTrackAnalyzer() override=default
double eta_[nMaxtracks_]
void endJob() override
int iEvent
Definition: GenABIO.cc:224
double dzPVSig_[nMaxtracks_]
std::string filename_
double phi_[nMaxtracks_]
double dxySig_[nMaxtracks_]
double chi2ndof_[nMaxtracks_]
double yPCA_[nMaxtracks_]
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double dxyPVSig_[nMaxtracks_]
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
double dxyError_[nMaxtracks_]
static constexpr int nMaxtracks_
double dxy_[nMaxtracks_]
d
Definition: ztail.py:151
std::string algoName() const
Definition: TrackBase.h:550
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
double dzSig_[nMaxtracks_]
Log< level::Info, false > LogInfo
int trkAlgo_[nMaxtracks_]
edm::InputTag PVtxCollectionTag_
double dzError_[nMaxtracks_]
double xPCA_[nMaxtracks_]
fixed size matrix
HLT enums.
double chi2_[nMaxtracks_]
int charge_[nMaxtracks_]
double dzPV_[nMaxtracks_]
Log< level::Warning, false > LogWarning
LhcTrackAnalyzer(const edm::ParameterSet &)
double pt_[nMaxtracks_]
void beginJob() override
void analyze(const edm::Event &, const edm::EventSetup &) override
double dxyPV_[nMaxtracks_]