CMS 3D CMS Logo

ValidationMisalignedTracker.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: ValidationMisalignedTracker
4 // Class: ValidationMisalignedTracker
5 //
13 //
14 // Original Author: Nicola De Filippis
15 // Created: Thu Dec 14 13:13:32 CET 2006
16 // $Id: ValidationMisalignedTracker.cc,v 1.8 2013/01/07 20:46:23 wmtan Exp $
17 //
18 //
19 
21 
22 // user include files
23 
30 
31 //
32 // constructors and destructor
33 //
35  : geomToken_(esConsumes()),
36  magFieldToken_(esConsumes()),
37  selection_eff(iConfig.getUntrackedParameter<bool>("selection_eff", false)),
38  selection_fake(iConfig.getUntrackedParameter<bool>("selection_fake", true)),
39  ZmassSelection_(iConfig.getUntrackedParameter<bool>("ZmassSelection", false)),
40  simobject(iConfig.getUntrackedParameter<std::string>("simobject", "g4SimHits")),
41  trackassociator(iConfig.getUntrackedParameter<std::string>("TrackAssociator", "ByHits")),
42  associators(iConfig.getParameter<std::vector<std::string> >("associators")),
43  label(iConfig.getParameter<std::vector<edm::InputTag> >("label")),
44  label_tp_effic(iConfig.getParameter<edm::InputTag>("label_tp_effic")),
45  label_tp_fake(iConfig.getParameter<edm::InputTag>("label_tp_fake")),
46 
47  rootfile_(iConfig.getUntrackedParameter<std::string>("rootfile", "myroot.root")),
48  evtToken_(consumes<edm::HepMCProduct>(edm::InputTag("source"))),
49  tpeffToken_(consumes<TrackingParticleCollection>(label_tp_effic)),
50  tpfakeToken_(consumes<TrackingParticleCollection>(label_tp_fake)),
51  trackToken_(consumes<edm::View<reco::Track> >(label[0])),
52  assocToken_{edm::vector_transform(
53  label, [this](const edm::InputTag& lab) { return consumes<reco::TrackToTrackingParticleAssociator>(lab); })} {
54  //now do what ever initialization is needed
55  mzmu = 0., recmzmu = 0., ptzmu = 0., recptzmu = 0., etazmu = 0., recetazmu = 0., thetazmu = 0., recthetazmu = 0.,
56  phizmu = 0., recphizmu = 0.;
57  recenezmu = 0., enezmu = 0., pLzmu = 0., recpLzmu = 0., yzmu = 0., recyzmu = 0., mxptmu = 0., recmxptmu = 0.,
58  minptmu = 0., recminptmu = 0.;
59  // mzele=0.,recmzele=0.
60 
61  flag = 0, flagrec = 0, count = 0, countrec = 0;
62  nAssoc = 0;
63 
64  for (int i = 0; i < 2; i++) {
65  countpart[i] = 0;
66  countpartrec[i] = 0;
67  for (int j = 0; j < 2; j++) {
68  ene[i][j] = 0.;
69  p[i][j] = 0.;
70  px[i][j] = 0.;
71  py[i][j] = 0.;
72  pz[i][j] = 0.;
73  ptmu[i][j] = 0.;
74  recene[i][j] = 0.;
75  recp[i][j] = 0.;
76  recpx[i][j] = 0.;
77  recpy[i][j] = 0.;
78  recpz[i][j] = 0.;
79  recptmu[i][j] = 0.;
80  }
81  }
82 
83  eventCount_ = 0;
84 
85  file_ = new TFile(rootfile_.c_str(), "RECREATE");
86 
87  // initialize the tree
88  tree_eff = new TTree("EffTracks", "Efficiency Tracks Tree");
89 
90  tree_eff->Branch("Run", &irun, "irun/i");
91  tree_eff->Branch("Event", &ievt, "ievt/i");
92 
93  // SimTrack
94  tree_eff->Branch("TrackID", &trackType, "trackType/i");
95  tree_eff->Branch("pt", &pt, "pt/F");
96  tree_eff->Branch("eta", &eta, "eta/F");
97  tree_eff->Branch("CotTheta", &cottheta, "cottheta/F");
98  tree_eff->Branch("phi", &phi, "phi/F");
99  tree_eff->Branch("d0", &d0, "d0/F");
100  tree_eff->Branch("z0", &z0, "z0/F");
101  tree_eff->Branch("nhit", &nhit, "nhit/i");
102 
103  // RecTrack
104  tree_eff->Branch("recpt", &recpt, "recpt/F");
105  tree_eff->Branch("receta", &receta, "receta/F");
106  tree_eff->Branch("CotRecTheta", &reccottheta, "reccottheta/F");
107  tree_eff->Branch("recphi", &recphi, "recphi/F");
108  tree_eff->Branch("recd0", &recd0, "recd0/F");
109  tree_eff->Branch("recz0", &recz0, "recz0/F");
110  tree_eff->Branch("nAssoc", &nAssoc, "nAssoc/i");
111  tree_eff->Branch("recnhit", &recnhit, "recnhit/i");
112  tree_eff->Branch("CHISQ", &recchiq, "recchiq/F");
113 
114  tree_eff->Branch("reseta", &reseta, "reseta/F");
115  tree_eff->Branch("respt", &respt, "respt/F");
116  tree_eff->Branch("resd0", &resd0, "resd0/F");
117  tree_eff->Branch("resz0", &resz0, "resz0/F");
118  tree_eff->Branch("resphi", &resphi, "resphi/F");
119  tree_eff->Branch("rescottheta", &rescottheta, "rescottheta/F");
120  tree_eff->Branch("eff", &eff, "eff/F");
121 
122  // Invariant masses, pt of Z
123  tree_eff->Branch("mzmu", &mzmu, "mzmu/F");
124  tree_eff->Branch("ptzmu", &ptzmu, "ptzmu/F");
125  tree_eff->Branch("pLzmu", &pLzmu, "pLzmu/F");
126  tree_eff->Branch("enezmu", &enezmu, "enezmu/F");
127  tree_eff->Branch("etazmu", &etazmu, "etazmu/F");
128  tree_eff->Branch("thetazmu", &thetazmu, "thetazmu/F");
129  tree_eff->Branch("phizmu", &phizmu, "phizmu/F");
130  tree_eff->Branch("yzmu", &yzmu, "yzmu/F");
131  tree_eff->Branch("mxptmu", &mxptmu, "mxptmu/F");
132  tree_eff->Branch("minptmu", &minptmu, "minptmu/F");
133 
134  tree_eff->Branch("recmzmu", &recmzmu, "recmzmu/F");
135  tree_eff->Branch("recptzmu", &recptzmu, "recptzmu/F");
136  tree_eff->Branch("recpLzmu", &recpLzmu, "recpLzmu/F");
137  tree_eff->Branch("recenezmu", &recenezmu, "recenezmu/F");
138  tree_eff->Branch("recetazmu", &recetazmu, "recetazmu/F");
139  tree_eff->Branch("recthetazmu", &recthetazmu, "recthetazmu/F");
140  tree_eff->Branch("recphizmu", &recphizmu, "recphizmu/F");
141  tree_eff->Branch("recyzmu", &recyzmu, "recyzmu/F");
142  tree_eff->Branch("recmxptmu", &recmxptmu, "recmxptmu/F");
143  tree_eff->Branch("recminptmu", &recminptmu, "recminptmu/F");
144 
145  //tree->Branch("mzele",&ntmzele,"ntmzele/F");
146  //tree->Branch("recmzele",&ntmzeleRec,"ntmzeleRec/F");
147  tree_eff->Branch("chi2Associator", &recchiq, "recchiq/F");
148 
149  // Fake
150 
151  tree_fake = new TTree("FakeTracks", "Fake Rate Tracks Tree");
152 
153  tree_fake->Branch("Run", &irun, "irun/i");
154  tree_fake->Branch("Event", &ievt, "ievt/i");
155 
156  // SimTrack
157  tree_fake->Branch("fakeTrackID", &faketrackType, "faketrackType/i");
158  tree_fake->Branch("fakept", &fakept, "fakept/F");
159  tree_fake->Branch("fakeeta", &fakeeta, "fakeeta/F");
160  tree_fake->Branch("fakeCotTheta", &fakecottheta, "fakecottheta/F");
161  tree_fake->Branch("fakephi", &fakephi, "fakephi/F");
162  tree_fake->Branch("faked0", &faked0, "faked0/F");
163  tree_fake->Branch("fakez0", &fakez0, "fakez0/F");
164  tree_fake->Branch("fakenhit", &fakenhit, "fakenhit/i");
165 
166  // RecTrack
167  tree_fake->Branch("fakerecpt", &fakerecpt, "fakerecpt/F");
168  tree_fake->Branch("fakereceta", &fakereceta, "fakereceta/F");
169  tree_fake->Branch("fakeCotRecTheta", &fakereccottheta, "fakereccottheta/F");
170  tree_fake->Branch("fakerecphi", &fakerecphi, "fakerecphi/F");
171  tree_fake->Branch("fakerecd0", &fakerecd0, "fakerecd0/F");
172  tree_fake->Branch("fakerecz0", &fakerecz0, "fakerecz0/F");
173  tree_fake->Branch("fakenAssoc", &fakenAssoc, "fakenAssoc/i");
174  tree_fake->Branch("fakerecnhit", &fakerecnhit, "fakerecnhit/i");
175  tree_fake->Branch("fakeCHISQ", &fakerecchiq, "fakerecchiq/F");
176 
177  tree_fake->Branch("fakereseta", &fakereseta, "fakereseta/F");
178  tree_fake->Branch("fakerespt", &fakerespt, "fakerespt/F");
179  tree_fake->Branch("fakeresd0", &fakeresd0, "fakeresd0/F");
180  tree_fake->Branch("fakeresz0", &fakeresz0, "fakeresz0/F");
181  tree_fake->Branch("fakeresphi", &fakeresphi, "fakeresphi/F");
182  tree_fake->Branch("fakerescottheta", &fakerescottheta, "fakerescottheta/F");
183  tree_fake->Branch("fake", &fake, "fake/F");
184 
185  // Invariant masses, pt of Z
186  tree_fake->Branch("fakemzmu", &fakemzmu, "fakemzmu/F");
187  tree_fake->Branch("fakeptzmu", &fakeptzmu, "fakeptzmu/F");
188  tree_fake->Branch("fakepLzmu", &fakepLzmu, "fakepLzmu/F");
189  tree_fake->Branch("fakeenezmu", &fakeenezmu, "fakeenezmu/F");
190  tree_fake->Branch("fakeetazmu", &fakeetazmu, "fakeetazmu/F");
191  tree_fake->Branch("fakethetazmu", &fakethetazmu, "fakethetazmu/F");
192  tree_fake->Branch("fakephizmu", &fakephizmu, "fakephizmu/F");
193  tree_fake->Branch("fakeyzmu", &fakeyzmu, "fakeyzmu/F");
194  tree_fake->Branch("fakemxptmu", &fakemxptmu, "fakemxptmu/F");
195  tree_fake->Branch("fakeminptmu", &fakeminptmu, "fakeminptmu/F");
196 
197  tree_fake->Branch("fakerecmzmu", &fakerecmzmu, "fakerecmzmu/F");
198  tree_fake->Branch("fakerecptzmu", &fakerecptzmu, "fakerecptzmu/F");
199  tree_fake->Branch("fakerecpLzmu", &fakerecpLzmu, "fakerecpLzmu/F");
200  tree_fake->Branch("fakerecenezmu", &fakerecenezmu, "fakerecenezmu/F");
201  tree_fake->Branch("fakerecetazmu", &fakerecetazmu, "fakerecetazmu/F");
202  tree_fake->Branch("fakerecthetazmu", &fakerecthetazmu, "fakerecthetazmu/F");
203  tree_fake->Branch("fakerecphizmu", &fakerecphizmu, "fakerecphizmu/F");
204  tree_fake->Branch("fakerecyzmu", &fakerecyzmu, "fakerecyzmu/F");
205  tree_fake->Branch("fakerecmxptmu", &fakerecmxptmu, "fakerecmxptmu/F");
206  tree_fake->Branch("fakerecminptmu", &fakerecminptmu, "fakerecminptmu/F");
207 
208  tree_fake->Branch("fakechi2Associator", &fakerecchiq, "fakerecchiq/F");
209 }
210 
212  edm::LogVerbatim("ValidationMisalignedTracker")
213  << "ValidationMisalignedTracker::endJob Processed " << eventCount_ << " events";
214 
215  // store the tree in the output file
216  file_->Write();
217 
218  // Closing the file deletes the tree.
219  file_->Close();
220  tree_eff = nullptr;
221  tree_fake = nullptr;
222 }
223 
224 //
225 // member functions
226 //
229  desc.add<std::vector<edm::InputTag> >("label", {});
230  desc.add<edm::InputTag>("label_tp_effic", edm::InputTag("FinalTrackRefitter"));
231  desc.add<edm::InputTag>("label_tp_fake", edm::InputTag("TrackRefitter"));
232  desc.add<std::vector<std::string> >("associators", {});
233  desc.addUntracked<bool>("selection_eff", false);
234  desc.addUntracked<bool>("selection_fake", true);
235  desc.addUntracked<bool>("ZmassSelection", false);
236  desc.addUntracked<std::string>("simobject", "g4SimHits");
237  desc.addUntracked<std::string>("TrackAssociator", "ByHits");
238  desc.addUntracked<std::string>("rootfile", "myroot.root");
239  descriptions.add("validationMisAlignedTracker", desc);
240 }
241 
242 // ------------ method called to for each event ------------
244  std::vector<const reco::TrackToTrackingParticleAssociator*> associatore;
245 
246  {
247  for (unsigned int w = 0; w < associators.size(); w++) {
249  associatore.push_back(theAssociator.product());
250  }
251  }
252 
253  edm::LogInfo("Tracker Misalignment Validation") << "\n Starting!";
254 
255  // Monte Carlo Z selection
256  skip = false;
257  std::vector<int> indmu;
258 
260  const edm::Handle<edm::HepMCProduct>& evt = iEvent.getHandle(evtToken_);
261  bool accepted = false;
262  bool foundmuons = false;
263  HepMC::GenEvent* myGenEvent = new HepMC::GenEvent(*(evt->GetEvent()));
264 
265  for (HepMC::GenEvent::particle_iterator p = myGenEvent->particles_begin(); p != myGenEvent->particles_end(); ++p) {
266  if (!accepted && ((*p)->pdg_id() == 23) && (*p)->status() == 3) {
267  accepted = true;
268  for (HepMC::GenVertex::particle_iterator aDaughter = (*p)->end_vertex()->particles_begin(HepMC::descendants);
269  aDaughter != (*p)->end_vertex()->particles_end(HepMC::descendants);
270  aDaughter++) {
271  if (abs((*aDaughter)->pdg_id()) == 13) {
272  foundmuons = true;
273  if ((*aDaughter)->status() != 1) {
274  for (HepMC::GenVertex::particle_iterator byaDaughter =
275  (*aDaughter)->end_vertex()->particles_begin(HepMC::descendants);
276  byaDaughter != (*aDaughter)->end_vertex()->particles_end(HepMC::descendants);
277  byaDaughter++) {
278  if ((*byaDaughter)->status() == 1 && abs((*byaDaughter)->pdg_id()) == 13) {
279  indmu.push_back((*byaDaughter)->barcode());
280  edm::LogVerbatim("ValidationMisalignedTracker")
281  << "Stable muon from Z with charge " << (*byaDaughter)->pdg_id() << " and index "
282  << (*byaDaughter)->barcode();
283  }
284  }
285  } else {
286  indmu.push_back((*aDaughter)->barcode());
287  edm::LogVerbatim("ValidationMisalignedTracker")
288  << "Stable muon from Z with charge " << (*aDaughter)->pdg_id() << " and index "
289  << (*aDaughter)->barcode();
290  }
291  }
292  }
293  if (!foundmuons) {
294  edm::LogVerbatim("ValidationMisalignedTracker") << "No muons from Z ...skip event";
295  skip = true;
296  }
297  }
298  }
299  if (!accepted) {
300  edm::LogVerbatim("ValidationMisalignedTracker") << "No Z particles in the event ...skip event";
301  skip = true;
302  }
303  } else {
304  skip = false;
305  }
306 
307  //
308  // Retrieve tracker geometry from event setup
309  //
310  const TrackerGeometry* trackerGeometry = &iSetup.getData(geomToken_);
311  auto testGeomDet = trackerGeometry->detsTOB().front();
312  edm::LogVerbatim("ValidationMisalignedTracker") << testGeomDet->position();
313 
314  //Dump Run and Event
315  irun = iEvent.id().run();
316  ievt = iEvent.id().event();
317 
318  // Reset tree variables
319  int countpart[2] = {0, 0}, countpartrec[2] = {0, 0}, flag = 0, flagrec = 0, count = 0, countrec = 0;
320  //int countsim=0;
321  float ene[2][2], px[2][2], py[2][2], pz[2][2], ptmu[2][2];
322  float recene[2][2], recp[2][2], recpx[2][2], recpy[2][2], recpz[2][2], recptmu[2][2];
323 
324  for (int i = 0; i < 2; i++) {
325  for (int j = 0; j < 2; j++) {
326  ene[i][j] = 0.;
327  px[i][j] = 0.;
328  py[i][j] = 0.;
329  pz[i][j] = 0.;
330  ptmu[i][j] = 0.;
331  recene[i][j] = 0.;
332  recp[i][j] = 0.;
333  recpx[i][j] = 0.;
334  recpy[i][j] = 0.;
335  recpz[i][j] = 0.;
336  recptmu[i][j] = 0.;
337  }
338  }
339 
340  const edm::Handle<TrackingParticleCollection>& TPCollectionHeff = iEvent.getHandle(tpeffToken_);
341  const TrackingParticleCollection tPCeff = *(TPCollectionHeff.product());
342 
343  const edm::Handle<TrackingParticleCollection>& TPCollectionHfake = iEvent.getHandle(tpfakeToken_);
344  const TrackingParticleCollection tPCfake = *(TPCollectionHfake.product());
345 
346  int w = 0;
347  for (unsigned int ww = 0; ww < associators.size(); ww++) {
348  //
349  //get collections from the event
350  //
351 
353  const edm::View<reco::Track> tC = *(trackCollection.product());
354 
355  //associate tracks
356  LogTrace("TrackValidator") << "Calling associateRecoToSim method\n";
357  reco::RecoToSimCollection recSimColl = associatore[ww]->associateRecoToSim(trackCollection, TPCollectionHfake);
358 
359  LogTrace("TrackValidator") << "Calling associateSimToReco method\n";
360  reco::SimToRecoCollection simRecColl = associatore[ww]->associateSimToReco(trackCollection, TPCollectionHeff);
361 
362  //
363  //compute number of tracks per eta interval
364  //
365 
366  if (selection_eff && !skip) {
367  edm::LogVerbatim("ValidationMisalignedTracker") << "Computing Efficiency";
368 
369  edm::LogVerbatim("TrackValidator") << "\n# of TrackingParticles (before cuts): " << tPCeff.size() << "\n";
370  int ats = 0;
371  int st = 0;
372  for (TrackingParticleCollection::size_type i = 0; i < tPCeff.size(); i++) {
373  // Initialize variables
374  eta = 0., theta = 0., phi = 0., pt = 0., cottheta = 0., costheta = 0.;
375  d0 = 0., z0 = 0.;
376  nhit = 0;
377  receta = 0., rectheta = 0., recphi = 0., recpt = 0., reccottheta = 0., recd0 = 0., recz0 = 0.;
378  respt = 0., resd0 = 0., resz0 = 0., reseta = 0., resphi = 0., rescottheta = 0.;
379  recchiq = 0.;
380  recnhit = 0;
381  trackType = 0;
382  eff = 0;
383 
384  // typedef edm::Ref<TrackingParticleCollection> TrackingParticleRef;
385  TrackingParticleRef tp(TPCollectionHeff, i);
386  if (tp->charge() == 0)
387  continue;
388  st++;
389  //pt=sqrt(tp->momentum().perp2());
390  //eta=tp->momentum().eta();
391  //vpos=tp->vertex().perp2()));
392 
393  const SimTrack* simulatedTrack = &(*tp->g4Track_begin());
394 
395  const MagneticField* theMF = &iSetup.getData(magFieldToken_);
396  FreeTrajectoryState ftsAtProduction(
397  GlobalPoint(tp->vertex().x(), tp->vertex().y(), tp->vertex().z()),
398  GlobalVector(
399  simulatedTrack->momentum().x(), simulatedTrack->momentum().y(), simulatedTrack->momentum().z()),
400  TrackCharge(tp->charge()),
401  theMF);
402  TSCPBuilderNoMaterial tscpBuilder;
403  TrajectoryStateClosestToPoint tsAtClosestApproach =
404  tscpBuilder(ftsAtProduction, GlobalPoint(0, 0, 0)); //as in TrackProducerAlgorithm
405  GlobalPoint v = tsAtClosestApproach.theState().position();
406  GlobalVector p = tsAtClosestApproach.theState().momentum();
407 
408  // double qoverpSim = tsAtClosestApproach.charge()/p.mag();
409  // double lambdaSim = M_PI/2-p.theta();
410  // double phiSim = p.phi();
411  double dxySim = (-v.x() * sin(p.phi()) + v.y() * cos(p.phi()));
412  double dszSim = v.z() * p.perp() / p.mag() - (v.x() * p.x() + v.y() * p.y()) / p.perp() * p.z() / p.mag();
413  d0 = float(-dxySim);
414  z0 = float(dszSim * p.mag() / p.perp());
415 
416  if (abs(simulatedTrack->type()) == 13 && simulatedTrack->genpartIndex() != -1) {
417  edm::LogVerbatim("ValidationMisalignedTracker") << " TRACCIA SIM DI MUONI ";
418  edm::LogVerbatim("ValidationMisalignedTracker") << "Gen part " << simulatedTrack->genpartIndex();
419  trackType = simulatedTrack->type();
420  theta = simulatedTrack->momentum().theta();
421  costheta = cos(theta);
422  cottheta = 1. / tan(theta);
423 
424  eta = simulatedTrack->momentum().eta();
425  phi = simulatedTrack->momentum().phi();
426  pt = simulatedTrack->momentum().pt();
427  nhit = tp->matchedHit();
428 
429  edm::LogVerbatim("ValidationMisalignedTracker")
430  << "3) Before assoc: SimTrack of type = " << simulatedTrack->type() << " ,at eta = " << eta
431  << " ,with pt at vertex = " << simulatedTrack->momentum().pt() << " GeV/c"
432  << " ,d0 =" << d0 << " ,z0 =" << z0 << " ,nhit=" << nhit;
433 
434  if (ZmassSelection_) {
435  if (abs(trackType) == 13 &&
436  (simulatedTrack->genpartIndex() == indmu[0] || simulatedTrack->genpartIndex() == indmu[1])) {
437  edm::LogVerbatim("ValidationMisalignedTracker") << " TRACK sim of muons from Z ";
438  flag = 0;
439  count = countpart[0];
440  countpart[0]++;
441  } else if (abs(trackType) == 11) {
442  //edm::LogVerbatim("ValidationMisalignedTracker") << " TRACCIA SIM DI ELETTRONI ";
443  flag = 1;
444  count = countpart[1];
445  countpart[1]++;
446  }
447 
448  px[flag][count] = simulatedTrack->momentum().x();
449  py[flag][count] = simulatedTrack->momentum().y();
450  pz[flag][count] = simulatedTrack->momentum().z();
451  ptmu[flag][count] = simulatedTrack->momentum().pt();
452  ene[flag][count] = simulatedTrack->momentum().e();
453  }
454 
455  std::vector<std::pair<edm::RefToBase<reco::Track>, double> > rt;
456  if (simRecColl.find(tp) != simRecColl.end()) {
457  rt = simRecColl[tp];
458  if (!rt.empty()) {
459  edm::RefToBase<reco::Track> t = rt.begin()->first;
460  ats++;
461 
462  // bool flagptused=false;
463  // for (unsigned int j=0;j<ptused.size();j++){
464  // if (fabs(t->pt()-ptused[j])<0.001) {
465  // flagptused=true;
466  // }
467  // }
468 
469  edm::LogVerbatim("TrackValidator") << "TrackingParticle #" << st << " with pt=" << t->pt()
470  << " associated with quality:" << rt.begin()->second << "\n";
471  edm::LogVerbatim("ValidationMisalignedTracker") << "Reconstructed Track:" << t->pt();
472  edm::LogVerbatim("ValidationMisalignedTracker") << "\tpT: " << t->pt();
473  edm::LogVerbatim("ValidationMisalignedTracker") << "\timpact parameter:d0: " << t->d0();
474  edm::LogVerbatim("ValidationMisalignedTracker") << "\timpact parameter:z0: " << t->dz();
475  edm::LogVerbatim("ValidationMisalignedTracker")
476  << "\tAzimuthal angle of point of closest approach:" << t->phi();
477  edm::LogVerbatim("ValidationMisalignedTracker") << "\tcharge: " << t->charge();
478  edm::LogVerbatim("ValidationMisalignedTracker") << "\teta: " << t->eta();
479  edm::LogVerbatim("ValidationMisalignedTracker") << "\tnormalizedChi2: " << t->normalizedChi2();
480 
481  recnhit = t->numberOfValidHits();
482  recchiq = t->normalizedChi2();
483  rectheta = t->theta();
484  reccottheta = 1. / tan(rectheta);
485  //receta=-log(tan(rectheta/2.));
486  receta = t->momentum().eta();
487  // reccostheta=cos(matchedrectrack->momentum().theta());
488  recphi = t->phi();
489  recpt = t->pt();
490  ptused.push_back(recpt);
491  recd0 = t->d0();
492  recz0 = t->dz();
493 
494  edm::LogVerbatim("ValidationMisalignedTracker")
495  << "5) After call to associator: the best match has " << recnhit << " hits, Chi2 = " << recchiq
496  << ", pt at vertex = " << recpt << " GeV/c, "
497  << ", recd0 = " << recd0 << ", recz0= " << recz0;
498 
499  respt = recpt - pt;
500  resd0 = recd0 - d0;
501  resz0 = recz0 - z0;
502  reseta = receta - eta;
503  resphi = recphi - phi;
505  eff = 1;
506 
507  edm::LogVerbatim("ValidationMisalignedTracker")
508  << "6) Transverse momentum residual=" << respt << " ,d0 residual=" << resd0
509  << " ,z0 residual=" << resz0 << " with eff=" << eff;
510 
511  if (ZmassSelection_) {
512  if (abs(trackType) == 13) {
513  edm::LogVerbatim("ValidationMisalignedTracker") << " TRACCIA RECO DI MUONI ";
514  flagrec = 0;
515  countrec = countpartrec[0];
516  countpartrec[0]++;
517  } else if (abs(trackType) == 11) {
518  edm::LogVerbatim("ValidationMisalignedTracker") << " TRACCIA RECO DI ELETTRONI ";
519  flagrec = 1;
520  countrec = countpartrec[1];
521  countpartrec[1]++;
522  }
523 
524  recp[flagrec][countrec] = sqrt(t->momentum().mag2());
525  recpx[flagrec][countrec] = t->momentum().x();
526  recpy[flagrec][countrec] = t->momentum().y();
527  recpz[flagrec][countrec] = t->momentum().z();
529  sqrt((t->momentum().x() * t->momentum().x()) + (t->momentum().y() * t->momentum().y()));
530  if (abs(trackType) == 13)
531  recene[flagrec][countrec] = sqrt(recp[flagrec][countrec] * recp[flagrec][countrec] + 0.105 * 0.105);
532  if (abs(trackType) == 11)
533  recene[flagrec][countrec] = sqrt(recp[flagrec][countrec] * recp[flagrec][countrec] + 0.0005 * 0.0005);
534  }
535 
536  edm::LogVerbatim("ValidationMisalignedTracker") << "7) Transverse momentum reconstructed =" << recpt
537  << " at eta= " << receta << " and phi= " << recphi;
538  }
539  } else {
540  edm::LogVerbatim("TrackValidator")
541  << "TrackingParticle #" << st << " with pt=" << sqrt(tp->momentum().perp2())
542  << " NOT associated to any reco::Track"
543  << "\n";
544  receta = -100.;
545  recphi = -100.;
546  recpt = -100.;
547  recd0 = -100.;
548  recz0 = -100;
549  respt = -100.;
550  resd0 = -100.;
551  resz0 = -100.;
552  resphi = -100.;
553  reseta = -100.;
554  rescottheta = -100.;
555  recnhit = 100;
556  recchiq = -100;
557  eff = 0;
558  flagrec = 100;
559  }
560 
561  edm::LogVerbatim("ValidationMisalignedTracker") << "Eff=" << eff;
562 
563  // simulated muons
564 
565  edm::LogVerbatim("ValidationMisalignedTracker") << "Flag is" << flag;
566  edm::LogVerbatim("ValidationMisalignedTracker") << "RecFlag is" << flagrec;
567 
568  if (countpart[0] == 2 && flag == 0) {
569  mzmu =
570  sqrt((ene[0][0] + ene[0][1]) * (ene[0][0] + ene[0][1]) - (px[0][0] + px[0][1]) * (px[0][0] + px[0][1]) -
571  (py[0][0] + py[0][1]) * (py[0][0] + py[0][1]) - (pz[0][0] + pz[0][1]) * (pz[0][0] + pz[0][1]));
572  edm::LogVerbatim("ValidationMisalignedTracker") << "Mzmu " << mzmu;
573  ptzmu = sqrt((px[0][0] + px[0][1]) * (px[0][0] + px[0][1]) + (py[0][0] + py[0][1]) * (py[0][0] + py[0][1]));
574 
575  pLzmu = pz[0][0] + pz[0][1];
576  enezmu = ene[0][0] + ene[0][1];
577  phizmu = atan2((py[0][0] + py[0][1]), (px[0][0] + px[0][1]));
578  thetazmu = atan2(ptzmu, (pz[0][0] + pz[0][1]));
579  etazmu = -log(tan(thetazmu * 3.14 / 360.));
580  yzmu = 0.5 * log((enezmu + pLzmu) / (enezmu - pLzmu));
581  mxptmu = std::max(ptmu[0][0], ptmu[0][1]);
582  minptmu = std::min(ptmu[0][0], ptmu[0][1]);
583  } else {
584  mzmu = -100.;
585  ptzmu = -100.;
586  pLzmu = -100.;
587  enezmu = -100.;
588  etazmu = -100.;
589  phizmu = -100.;
590  thetazmu = -100.;
591  yzmu = -100.;
592  mxptmu = -100.;
593  minptmu = -100.;
594  }
595 
596  // reconstructed muons
597  if (countpartrec[0] == 2 && flagrec == 0) {
598  recmzmu = sqrt((recene[0][0] + recene[0][1]) * (recene[0][0] + recene[0][1]) -
599  (recpx[0][0] + recpx[0][1]) * (recpx[0][0] + recpx[0][1]) -
600  (recpy[0][0] + recpy[0][1]) * (recpy[0][0] + recpy[0][1]) -
601  (recpz[0][0] + recpz[0][1]) * (recpz[0][0] + recpz[0][1]));
602  edm::LogVerbatim("ValidationMisalignedTracker") << "RecMzmu " << recmzmu;
603  recptzmu = sqrt((recpx[0][0] + recpx[0][1]) * (recpx[0][0] + recpx[0][1]) +
604  (recpy[0][0] + recpy[0][1]) * (recpy[0][0] + recpy[0][1]));
605 
606  recpLzmu = recpz[0][0] + recpz[0][1];
607  recenezmu = recene[0][0] + recene[0][1];
608  recphizmu = atan2((recpy[0][0] + recpy[0][1]), (recpx[0][0] + recpx[0][1]));
609  recthetazmu = atan2(recptzmu, (recpz[0][0] + recpz[0][1]));
610  recetazmu = -log(tan(recthetazmu * 3.14 / 360.));
611  recyzmu = 0.5 * log((recenezmu + recpLzmu) / (recenezmu - recpLzmu));
612  recmxptmu = std::max(recptmu[0][0], recptmu[0][1]);
613  recminptmu = std::min(recptmu[0][0], recptmu[0][1]);
614  } else {
615  recmzmu = -100.;
616  recptzmu = -100.;
617  recpLzmu = -100.;
618  recenezmu = -100.;
619  recetazmu = -100.;
620  recphizmu = -100.;
621  recthetazmu = -100.;
622  recyzmu = -100.;
623  recmxptmu = -100;
624  recminptmu = -100.;
625  }
626 
627  tree_eff->Fill();
628 
629  } // end of loop on muons
630  } // end of loop for tracking particle
631  } // end of loop for efficiency
632 
633  //
634  // Fake Rate
635  //
636  if (selection_fake) {
637  edm::LogVerbatim("ValidationMisalignedTracker") << "Computing Fake Rate";
638 
639  fakeeta = 0., faketheta = 0., fakephi = 0., fakept = 0., fakecottheta = 0., fakecostheta = 0.;
640  faked0 = 0., fakez0 = 0.;
641  fakenhit = 0;
642  fakereceta = 0., fakerectheta = 0., fakerecphi = 0., fakerecpt = 0., fakereccottheta = 0., fakerecd0 = 0.,
643  fakerecz0 = 0.;
644  fakerespt = 0., fakeresd0 = 0., fakeresz0 = 0., fakereseta = 0., fakeresphi = 0., fakerescottheta = 0.;
645  fakerecchiq = 0.;
646  fakerecnhit = 0;
647  faketrackType = 0;
648  fake = 0;
649 
650  // int at=0;
651  int rT = 0;
652  for (reco::TrackCollection::size_type i = 0; i < tC.size(); ++i) {
654  rT++;
655 
656  fakeeta = 0., faketheta = 0., fakephi = 0., fakept = 0., fakecottheta = 0., fakecostheta = 0.;
657  faked0 = 0., fakez0 = 0.;
658  fakenhit = 0;
659  fakereceta = 0., fakerectheta = 0., fakerecphi = 0., fakerecpt = 0., fakereccottheta = 0., fakerecd0 = 0.,
660  fakerecz0 = 0.;
661  fakerespt = 0., fakeresd0 = 0., fakeresz0 = 0., fakereseta = 0., fakeresphi = 0., fakerescottheta = 0.;
662  fakerecchiq = 0.;
663  fakerecnhit = 0;
664  faketrackType = 0;
665  fake = 0;
666 
667  fakerecnhit = track->numberOfValidHits();
668  fakerecchiq = track->normalizedChi2();
669  fakerectheta = track->theta();
670  fakereccottheta = 1. / tan(rectheta);
671  //fakereceta=-log(tan(rectheta/2.));
672  fakereceta = track->momentum().eta();
673  // fakereccostheta=cos(track->momentum().theta());
674  fakerecphi = track->phi();
675  fakerecpt = track->pt();
676  fakerecd0 = track->d0();
677  fakerecz0 = track->dz();
678 
679  edm::LogVerbatim("ValidationMisalignedTracker") << "1) Before assoc: TkRecTrack at eta = " << fakereceta;
680  edm::LogVerbatim("ValidationMisalignedTracker") << "Track number " << i;
681  edm::LogVerbatim("ValidationMisalignedTracker") << "\tPT: " << track->pt();
682  edm::LogVerbatim("ValidationMisalignedTracker") << "\timpact parameter:d0: " << track->d0();
683  edm::LogVerbatim("ValidationMisalignedTracker") << "\timpact parameter:z0: " << track->dz();
684  edm::LogVerbatim("ValidationMisalignedTracker")
685  << "\tAzimuthal angle of point of closest approach:" << track->phi();
686  edm::LogVerbatim("ValidationMisalignedTracker") << "\tcharge: " << track->charge();
687  edm::LogVerbatim("ValidationMisalignedTracker") << "\teta: " << track->eta();
688  edm::LogVerbatim("ValidationMisalignedTracker") << "\tnormalizedChi2: " << track->normalizedChi2();
689 
690  std::vector<std::pair<TrackingParticleRef, double> > tp;
691 
692  //Compute fake rate vs eta
693  if (recSimColl.find(track) != recSimColl.end()) {
694  tp = recSimColl[track];
695  if (!tp.empty()) {
696  edm::LogVerbatim("TrackValidator") << "reco::Track #" << rT << " with pt=" << track->pt()
697  << " associated with quality:" << tp.begin()->second << "\n";
698 
699  TrackingParticleRef tpr = tp.begin()->first;
700  const SimTrack* fakeassocTrack = &(*tpr->g4Track_begin());
701 
702  const MagneticField* theMF = &iSetup.getData(magFieldToken_);
703  FreeTrajectoryState ftsAtProduction(
704  GlobalPoint(tpr->vertex().x(), tpr->vertex().y(), tpr->vertex().z()),
705  GlobalVector(
706  fakeassocTrack->momentum().x(), fakeassocTrack->momentum().y(), fakeassocTrack->momentum().z()),
707  TrackCharge(tpr->charge()),
708  theMF);
709  TSCPBuilderNoMaterial tscpBuilder;
710  TrajectoryStateClosestToPoint tsAtClosestApproach =
711  tscpBuilder(ftsAtProduction, GlobalPoint(0, 0, 0)); //as in TrackProducerAlgorithm
712  GlobalPoint v = tsAtClosestApproach.theState().position();
713  GlobalVector p = tsAtClosestApproach.theState().momentum();
714 
715  // double qoverpSim = tsAtClosestApproach.charge()/p.mag();
716  // double lambdaSim = M_PI/2-p.theta();
717  // double phiSim = p.phi();
718  double dxySim = (-v.x() * sin(p.phi()) + v.y() * cos(p.phi()));
719  double dszSim = v.z() * p.perp() / p.mag() - (v.x() * p.x() + v.y() * p.y()) / p.perp() * p.z() / p.mag();
720  faked0 = float(-dxySim);
721  fakez0 = float(dszSim * p.mag() / p.perp());
722 
723  faketrackType = fakeassocTrack->type();
724  faketheta = fakeassocTrack->momentum().theta();
725  fakecottheta = 1. / tan(faketheta);
726  fakeeta = fakeassocTrack->momentum().eta();
727  fakephi = fakeassocTrack->momentum().phi();
728  fakept = fakeassocTrack->momentum().pt();
729  fakenhit = tpr->matchedHit();
730 
731  edm::LogVerbatim("ValidationMisalignedTracker")
732  << "4) After call to associator: the best SimTrack match is of type" << fakeassocTrack->type()
733  << " ,at eta = " << fakeeta << " and phi = " << fakephi << " ,with pt at vertex = " << fakept
734  << " GeV/c"
735  << " ,d0 global = " << faked0 << " ,z0 = " << fakez0;
736  fake = 1;
737 
741  fakereseta = -log(tan(fakerectheta / 2.)) - (-log(tan(faketheta / 2.)));
744  }
745  } else {
746  edm::LogVerbatim("TrackValidator")
747  << "reco::Track #" << rT << " with pt=" << track->pt() << " NOT associated to any TrackingParticle"
748  << "\n";
749 
750  fakeeta = -100.;
751  faketheta = -100;
752  fakephi = -100.;
753  fakept = -100.;
754  faked0 = -100.;
755  fakez0 = -100;
756  fakerespt = -100.;
757  fakeresd0 = -100.;
758  fakeresz0 = -100.;
759  fakeresphi = -100.;
760  fakereseta = -100.;
761  fakerescottheta = -100.;
762  fakenhit = 100;
763  fake = 0;
764  }
765 
766  tree_fake->Fill();
767  }
768 
769  } // End of loop on fakerate
770 
771  w++;
772 
773  } // End of loop on associators
774 }
775 
776 // ------------ method called once each job just after ending the event loop ------------
778  edm::LogVerbatim("ValidationMisalignedTracker") << "\t Misalignment analysis completed \n";
779 }
780 
Log< level::Info, true > LogVerbatim
const edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > magFieldToken_
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
const std::vector< std::string > associators
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
example_stream int eventCount_
T w() const
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
T const * product() const
Definition: Handle.h:70
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
const edm::EDGetTokenT< TrackingParticleCollection > tpeffToken_
auto vector_transform(std::vector< InputType > const &input, Function predicate) -> std::vector< typename std::remove_cv< typename std::remove_reference< decltype(predicate(input.front()))>::type >::type >
Definition: transform.h:11
void analyze(const edm::Event &, const edm::EventSetup &) override
uint16_t size_type
const edm::EDGetTokenT< edm::View< reco::Track > > trackToken_
const math::XYZTLorentzVectorD & momentum() const
Definition: CoreSimTrack.h:19
const_iterator find(const key_type &k) const
find element with specified reference key
#define LogTrace(id)
size_type size() const
const edm::EDGetTokenT< edm::HepMCProduct > evtToken_
const_iterator end() const
last iterator over the map (read only)
ValidationMisalignedTracker(const edm::ParameterSet &)
GlobalPoint position() const
int type() const
particle type (HEP PDT convension)
Definition: CoreSimTrack.h:22
char const * label
int TrackCharge
Definition: TrackCharge.h:4
int iEvent
Definition: GenABIO.cc:224
const edm::EDGetTokenT< TrackingParticleCollection > tpfakeToken_
const DetContainer & detsTOB() const
const edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > geomToken_
const std::vector< edm::EDGetTokenT< reco::TrackToTrackingParticleAssociator > > assocToken_
const std::vector< edm::InputTag > label
T sqrt(T t)
Definition: SSEVec.h:19
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
GlobalVector momentum() const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
trackCollection
Definition: JetHT_cfg.py:51
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:37
Log< level::Info, false > LogInfo
static constexpr float d0
const FreeTrajectoryState & theState() const
void add(std::string const &label, ParameterSetDescription const &psetDescription)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
fixed size matrix
HLT enums.
std::vector< TrackingParticle > TrackingParticleCollection
Global3DVector GlobalVector
Definition: GlobalVector.h:10
int genpartIndex() const
index of the corresponding Generator particle in the Event container (-1 if no Genpart) ...
Definition: SimTrack.h:37