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  for (unsigned int ww = 0; ww < associators.size(); ww++) {
347  //
348  //get collections from the event
349  //
350 
352  const edm::View<reco::Track> tC = *(trackCollection.product());
353 
354  //associate tracks
355  LogTrace("TrackValidator") << "Calling associateRecoToSim method\n";
356  reco::RecoToSimCollection recSimColl = associatore[ww]->associateRecoToSim(trackCollection, TPCollectionHfake);
357 
358  LogTrace("TrackValidator") << "Calling associateSimToReco method\n";
359  reco::SimToRecoCollection simRecColl = associatore[ww]->associateSimToReco(trackCollection, TPCollectionHeff);
360 
361  //
362  //compute number of tracks per eta interval
363  //
364 
365  if (selection_eff && !skip) {
366  edm::LogVerbatim("ValidationMisalignedTracker") << "Computing Efficiency";
367 
368  edm::LogVerbatim("TrackValidator") << "\n# of TrackingParticles (before cuts): " << tPCeff.size() << "\n";
369  int st = 0;
370  for (TrackingParticleCollection::size_type i = 0; i < tPCeff.size(); i++) {
371  // Initialize variables
372  eta = 0., theta = 0., phi = 0., pt = 0., cottheta = 0., costheta = 0.;
373  d0 = 0., z0 = 0.;
374  nhit = 0;
375  receta = 0., rectheta = 0., recphi = 0., recpt = 0., reccottheta = 0., recd0 = 0., recz0 = 0.;
376  respt = 0., resd0 = 0., resz0 = 0., reseta = 0., resphi = 0., rescottheta = 0.;
377  recchiq = 0.;
378  recnhit = 0;
379  trackType = 0;
380  eff = 0;
381 
382  // typedef edm::Ref<TrackingParticleCollection> TrackingParticleRef;
383  TrackingParticleRef tp(TPCollectionHeff, i);
384  if (tp->charge() == 0)
385  continue;
386  st++;
387  //pt=sqrt(tp->momentum().perp2());
388  //eta=tp->momentum().eta();
389  //vpos=tp->vertex().perp2()));
390 
391  const SimTrack* simulatedTrack = &(*tp->g4Track_begin());
392 
393  const MagneticField* theMF = &iSetup.getData(magFieldToken_);
394  FreeTrajectoryState ftsAtProduction(
395  GlobalPoint(tp->vertex().x(), tp->vertex().y(), tp->vertex().z()),
396  GlobalVector(
397  simulatedTrack->momentum().x(), simulatedTrack->momentum().y(), simulatedTrack->momentum().z()),
398  TrackCharge(tp->charge()),
399  theMF);
400  TSCPBuilderNoMaterial tscpBuilder;
401  TrajectoryStateClosestToPoint tsAtClosestApproach =
402  tscpBuilder(ftsAtProduction, GlobalPoint(0, 0, 0)); //as in TrackProducerAlgorithm
403  GlobalPoint v = tsAtClosestApproach.theState().position();
404  GlobalVector p = tsAtClosestApproach.theState().momentum();
405 
406  // double qoverpSim = tsAtClosestApproach.charge()/p.mag();
407  // double lambdaSim = M_PI/2-p.theta();
408  // double phiSim = p.phi();
409  double dxySim = (-v.x() * sin(p.phi()) + v.y() * cos(p.phi()));
410  double dszSim = v.z() * p.perp() / p.mag() - (v.x() * p.x() + v.y() * p.y()) / p.perp() * p.z() / p.mag();
411  d0 = float(-dxySim);
412  z0 = float(dszSim * p.mag() / p.perp());
413 
414  if (abs(simulatedTrack->type()) == 13 && simulatedTrack->genpartIndex() != -1) {
415  edm::LogVerbatim("ValidationMisalignedTracker") << " TRACCIA SIM DI MUONI ";
416  edm::LogVerbatim("ValidationMisalignedTracker") << "Gen part " << simulatedTrack->genpartIndex();
417  trackType = simulatedTrack->type();
418  theta = simulatedTrack->momentum().theta();
419  costheta = cos(theta);
420  cottheta = 1. / tan(theta);
421 
422  eta = simulatedTrack->momentum().eta();
423  phi = simulatedTrack->momentum().phi();
424  pt = simulatedTrack->momentum().pt();
425  nhit = tp->matchedHit();
426 
427  edm::LogVerbatim("ValidationMisalignedTracker")
428  << "3) Before assoc: SimTrack of type = " << simulatedTrack->type() << " ,at eta = " << eta
429  << " ,with pt at vertex = " << simulatedTrack->momentum().pt() << " GeV/c"
430  << " ,d0 =" << d0 << " ,z0 =" << z0 << " ,nhit=" << nhit;
431 
432  if (ZmassSelection_) {
433  if (abs(trackType) == 13 &&
434  (simulatedTrack->genpartIndex() == indmu[0] || simulatedTrack->genpartIndex() == indmu[1])) {
435  edm::LogVerbatim("ValidationMisalignedTracker") << " TRACK sim of muons from Z ";
436  flag = 0;
437  count = countpart[0];
438  countpart[0]++;
439  } else if (abs(trackType) == 11) {
440  //edm::LogVerbatim("ValidationMisalignedTracker") << " TRACCIA SIM DI ELETTRONI ";
441  flag = 1;
442  count = countpart[1];
443  countpart[1]++;
444  }
445 
446  px[flag][count] = simulatedTrack->momentum().x();
447  py[flag][count] = simulatedTrack->momentum().y();
448  pz[flag][count] = simulatedTrack->momentum().z();
449  ptmu[flag][count] = simulatedTrack->momentum().pt();
450  ene[flag][count] = simulatedTrack->momentum().e();
451  }
452 
453  std::vector<std::pair<edm::RefToBase<reco::Track>, double> > rt;
454  if (simRecColl.find(tp) != simRecColl.end()) {
455  rt = simRecColl[tp];
456  if (!rt.empty()) {
457  edm::RefToBase<reco::Track> t = rt.begin()->first;
458 
459  // bool flagptused=false;
460  // for (unsigned int j=0;j<ptused.size();j++){
461  // if (fabs(t->pt()-ptused[j])<0.001) {
462  // flagptused=true;
463  // }
464  // }
465 
466  edm::LogVerbatim("TrackValidator") << "TrackingParticle #" << st << " with pt=" << t->pt()
467  << " associated with quality:" << rt.begin()->second << "\n";
468  edm::LogVerbatim("ValidationMisalignedTracker") << "Reconstructed Track:" << t->pt();
469  edm::LogVerbatim("ValidationMisalignedTracker") << "\tpT: " << t->pt();
470  edm::LogVerbatim("ValidationMisalignedTracker") << "\timpact parameter:d0: " << t->d0();
471  edm::LogVerbatim("ValidationMisalignedTracker") << "\timpact parameter:z0: " << t->dz();
472  edm::LogVerbatim("ValidationMisalignedTracker")
473  << "\tAzimuthal angle of point of closest approach:" << t->phi();
474  edm::LogVerbatim("ValidationMisalignedTracker") << "\tcharge: " << t->charge();
475  edm::LogVerbatim("ValidationMisalignedTracker") << "\teta: " << t->eta();
476  edm::LogVerbatim("ValidationMisalignedTracker") << "\tnormalizedChi2: " << t->normalizedChi2();
477 
478  recnhit = t->numberOfValidHits();
479  recchiq = t->normalizedChi2();
480  rectheta = t->theta();
481  reccottheta = 1. / tan(rectheta);
482  //receta=-log(tan(rectheta/2.));
483  receta = t->momentum().eta();
484  // reccostheta=cos(matchedrectrack->momentum().theta());
485  recphi = t->phi();
486  recpt = t->pt();
487  ptused.push_back(recpt);
488  recd0 = t->d0();
489  recz0 = t->dz();
490 
491  edm::LogVerbatim("ValidationMisalignedTracker")
492  << "5) After call to associator: the best match has " << recnhit << " hits, Chi2 = " << recchiq
493  << ", pt at vertex = " << recpt << " GeV/c, "
494  << ", recd0 = " << recd0 << ", recz0= " << recz0;
495 
496  respt = recpt - pt;
497  resd0 = recd0 - d0;
498  resz0 = recz0 - z0;
499  reseta = receta - eta;
500  resphi = recphi - phi;
502  eff = 1;
503 
504  edm::LogVerbatim("ValidationMisalignedTracker")
505  << "6) Transverse momentum residual=" << respt << " ,d0 residual=" << resd0
506  << " ,z0 residual=" << resz0 << " with eff=" << eff;
507 
508  if (ZmassSelection_) {
509  if (abs(trackType) == 13) {
510  edm::LogVerbatim("ValidationMisalignedTracker") << " TRACCIA RECO DI MUONI ";
511  flagrec = 0;
512  countrec = countpartrec[0];
513  countpartrec[0]++;
514  } else if (abs(trackType) == 11) {
515  edm::LogVerbatim("ValidationMisalignedTracker") << " TRACCIA RECO DI ELETTRONI ";
516  flagrec = 1;
517  countrec = countpartrec[1];
518  countpartrec[1]++;
519  }
520 
521  recp[flagrec][countrec] = sqrt(t->momentum().mag2());
522  recpx[flagrec][countrec] = t->momentum().x();
523  recpy[flagrec][countrec] = t->momentum().y();
524  recpz[flagrec][countrec] = t->momentum().z();
526  sqrt((t->momentum().x() * t->momentum().x()) + (t->momentum().y() * t->momentum().y()));
527  if (abs(trackType) == 13)
528  recene[flagrec][countrec] = sqrt(recp[flagrec][countrec] * recp[flagrec][countrec] + 0.105 * 0.105);
529  if (abs(trackType) == 11)
530  recene[flagrec][countrec] = sqrt(recp[flagrec][countrec] * recp[flagrec][countrec] + 0.0005 * 0.0005);
531  }
532 
533  edm::LogVerbatim("ValidationMisalignedTracker") << "7) Transverse momentum reconstructed =" << recpt
534  << " at eta= " << receta << " and phi= " << recphi;
535  }
536  } else {
537  edm::LogVerbatim("TrackValidator")
538  << "TrackingParticle #" << st << " with pt=" << sqrt(tp->momentum().perp2())
539  << " NOT associated to any reco::Track"
540  << "\n";
541  receta = -100.;
542  recphi = -100.;
543  recpt = -100.;
544  recd0 = -100.;
545  recz0 = -100;
546  respt = -100.;
547  resd0 = -100.;
548  resz0 = -100.;
549  resphi = -100.;
550  reseta = -100.;
551  rescottheta = -100.;
552  recnhit = 100;
553  recchiq = -100;
554  eff = 0;
555  flagrec = 100;
556  }
557 
558  edm::LogVerbatim("ValidationMisalignedTracker") << "Eff=" << eff;
559 
560  // simulated muons
561 
562  edm::LogVerbatim("ValidationMisalignedTracker") << "Flag is" << flag;
563  edm::LogVerbatim("ValidationMisalignedTracker") << "RecFlag is" << flagrec;
564 
565  if (countpart[0] == 2 && flag == 0) {
566  mzmu =
567  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]) -
568  (py[0][0] + py[0][1]) * (py[0][0] + py[0][1]) - (pz[0][0] + pz[0][1]) * (pz[0][0] + pz[0][1]));
569  edm::LogVerbatim("ValidationMisalignedTracker") << "Mzmu " << mzmu;
570  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]));
571 
572  pLzmu = pz[0][0] + pz[0][1];
573  enezmu = ene[0][0] + ene[0][1];
574  phizmu = atan2((py[0][0] + py[0][1]), (px[0][0] + px[0][1]));
575  thetazmu = atan2(ptzmu, (pz[0][0] + pz[0][1]));
576  etazmu = -log(tan(thetazmu * 3.14 / 360.));
577  yzmu = 0.5 * log((enezmu + pLzmu) / (enezmu - pLzmu));
578  mxptmu = std::max(ptmu[0][0], ptmu[0][1]);
579  minptmu = std::min(ptmu[0][0], ptmu[0][1]);
580  } else {
581  mzmu = -100.;
582  ptzmu = -100.;
583  pLzmu = -100.;
584  enezmu = -100.;
585  etazmu = -100.;
586  phizmu = -100.;
587  thetazmu = -100.;
588  yzmu = -100.;
589  mxptmu = -100.;
590  minptmu = -100.;
591  }
592 
593  // reconstructed muons
594  if (countpartrec[0] == 2 && flagrec == 0) {
595  recmzmu = sqrt((recene[0][0] + recene[0][1]) * (recene[0][0] + recene[0][1]) -
596  (recpx[0][0] + recpx[0][1]) * (recpx[0][0] + recpx[0][1]) -
597  (recpy[0][0] + recpy[0][1]) * (recpy[0][0] + recpy[0][1]) -
598  (recpz[0][0] + recpz[0][1]) * (recpz[0][0] + recpz[0][1]));
599  edm::LogVerbatim("ValidationMisalignedTracker") << "RecMzmu " << recmzmu;
600  recptzmu = sqrt((recpx[0][0] + recpx[0][1]) * (recpx[0][0] + recpx[0][1]) +
601  (recpy[0][0] + recpy[0][1]) * (recpy[0][0] + recpy[0][1]));
602 
603  recpLzmu = recpz[0][0] + recpz[0][1];
604  recenezmu = recene[0][0] + recene[0][1];
605  recphizmu = atan2((recpy[0][0] + recpy[0][1]), (recpx[0][0] + recpx[0][1]));
606  recthetazmu = atan2(recptzmu, (recpz[0][0] + recpz[0][1]));
607  recetazmu = -log(tan(recthetazmu * 3.14 / 360.));
608  recyzmu = 0.5 * log((recenezmu + recpLzmu) / (recenezmu - recpLzmu));
609  recmxptmu = std::max(recptmu[0][0], recptmu[0][1]);
610  recminptmu = std::min(recptmu[0][0], recptmu[0][1]);
611  } else {
612  recmzmu = -100.;
613  recptzmu = -100.;
614  recpLzmu = -100.;
615  recenezmu = -100.;
616  recetazmu = -100.;
617  recphizmu = -100.;
618  recthetazmu = -100.;
619  recyzmu = -100.;
620  recmxptmu = -100;
621  recminptmu = -100.;
622  }
623 
624  tree_eff->Fill();
625 
626  } // end of loop on muons
627  } // end of loop for tracking particle
628  } // end of loop for efficiency
629 
630  //
631  // Fake Rate
632  //
633  if (selection_fake) {
634  edm::LogVerbatim("ValidationMisalignedTracker") << "Computing Fake Rate";
635 
636  fakeeta = 0., faketheta = 0., fakephi = 0., fakept = 0., fakecottheta = 0., fakecostheta = 0.;
637  faked0 = 0., fakez0 = 0.;
638  fakenhit = 0;
639  fakereceta = 0., fakerectheta = 0., fakerecphi = 0., fakerecpt = 0., fakereccottheta = 0., fakerecd0 = 0.,
640  fakerecz0 = 0.;
641  fakerespt = 0., fakeresd0 = 0., fakeresz0 = 0., fakereseta = 0., fakeresphi = 0., fakerescottheta = 0.;
642  fakerecchiq = 0.;
643  fakerecnhit = 0;
644  faketrackType = 0;
645  fake = 0;
646 
647  // int at=0;
648  int rT = 0;
649  for (reco::TrackCollection::size_type i = 0; i < tC.size(); ++i) {
651  rT++;
652 
653  fakeeta = 0., faketheta = 0., fakephi = 0., fakept = 0., fakecottheta = 0., fakecostheta = 0.;
654  faked0 = 0., fakez0 = 0.;
655  fakenhit = 0;
656  fakereceta = 0., fakerectheta = 0., fakerecphi = 0., fakerecpt = 0., fakereccottheta = 0., fakerecd0 = 0.,
657  fakerecz0 = 0.;
658  fakerespt = 0., fakeresd0 = 0., fakeresz0 = 0., fakereseta = 0., fakeresphi = 0., fakerescottheta = 0.;
659  fakerecchiq = 0.;
660  fakerecnhit = 0;
661  faketrackType = 0;
662  fake = 0;
663 
664  fakerecnhit = track->numberOfValidHits();
665  fakerecchiq = track->normalizedChi2();
666  fakerectheta = track->theta();
667  fakereccottheta = 1. / tan(rectheta);
668  //fakereceta=-log(tan(rectheta/2.));
669  fakereceta = track->momentum().eta();
670  // fakereccostheta=cos(track->momentum().theta());
671  fakerecphi = track->phi();
672  fakerecpt = track->pt();
673  fakerecd0 = track->d0();
674  fakerecz0 = track->dz();
675 
676  edm::LogVerbatim("ValidationMisalignedTracker") << "1) Before assoc: TkRecTrack at eta = " << fakereceta;
677  edm::LogVerbatim("ValidationMisalignedTracker") << "Track number " << i;
678  edm::LogVerbatim("ValidationMisalignedTracker") << "\tPT: " << track->pt();
679  edm::LogVerbatim("ValidationMisalignedTracker") << "\timpact parameter:d0: " << track->d0();
680  edm::LogVerbatim("ValidationMisalignedTracker") << "\timpact parameter:z0: " << track->dz();
681  edm::LogVerbatim("ValidationMisalignedTracker")
682  << "\tAzimuthal angle of point of closest approach:" << track->phi();
683  edm::LogVerbatim("ValidationMisalignedTracker") << "\tcharge: " << track->charge();
684  edm::LogVerbatim("ValidationMisalignedTracker") << "\teta: " << track->eta();
685  edm::LogVerbatim("ValidationMisalignedTracker") << "\tnormalizedChi2: " << track->normalizedChi2();
686 
687  std::vector<std::pair<TrackingParticleRef, double> > tp;
688 
689  //Compute fake rate vs eta
690  if (recSimColl.find(track) != recSimColl.end()) {
691  tp = recSimColl[track];
692  if (!tp.empty()) {
693  edm::LogVerbatim("TrackValidator") << "reco::Track #" << rT << " with pt=" << track->pt()
694  << " associated with quality:" << tp.begin()->second << "\n";
695 
696  TrackingParticleRef tpr = tp.begin()->first;
697  const SimTrack* fakeassocTrack = &(*tpr->g4Track_begin());
698 
699  const MagneticField* theMF = &iSetup.getData(magFieldToken_);
700  FreeTrajectoryState ftsAtProduction(
701  GlobalPoint(tpr->vertex().x(), tpr->vertex().y(), tpr->vertex().z()),
702  GlobalVector(
703  fakeassocTrack->momentum().x(), fakeassocTrack->momentum().y(), fakeassocTrack->momentum().z()),
704  TrackCharge(tpr->charge()),
705  theMF);
706  TSCPBuilderNoMaterial tscpBuilder;
707  TrajectoryStateClosestToPoint tsAtClosestApproach =
708  tscpBuilder(ftsAtProduction, GlobalPoint(0, 0, 0)); //as in TrackProducerAlgorithm
709  GlobalPoint v = tsAtClosestApproach.theState().position();
710  GlobalVector p = tsAtClosestApproach.theState().momentum();
711 
712  // double qoverpSim = tsAtClosestApproach.charge()/p.mag();
713  // double lambdaSim = M_PI/2-p.theta();
714  // double phiSim = p.phi();
715  double dxySim = (-v.x() * sin(p.phi()) + v.y() * cos(p.phi()));
716  double dszSim = v.z() * p.perp() / p.mag() - (v.x() * p.x() + v.y() * p.y()) / p.perp() * p.z() / p.mag();
717  faked0 = float(-dxySim);
718  fakez0 = float(dszSim * p.mag() / p.perp());
719 
720  faketrackType = fakeassocTrack->type();
721  faketheta = fakeassocTrack->momentum().theta();
722  fakecottheta = 1. / tan(faketheta);
723  fakeeta = fakeassocTrack->momentum().eta();
724  fakephi = fakeassocTrack->momentum().phi();
725  fakept = fakeassocTrack->momentum().pt();
726  fakenhit = tpr->matchedHit();
727 
728  edm::LogVerbatim("ValidationMisalignedTracker")
729  << "4) After call to associator: the best SimTrack match is of type" << fakeassocTrack->type()
730  << " ,at eta = " << fakeeta << " and phi = " << fakephi << " ,with pt at vertex = " << fakept
731  << " GeV/c"
732  << " ,d0 global = " << faked0 << " ,z0 = " << fakez0;
733  fake = 1;
734 
738  fakereseta = -log(tan(fakerectheta / 2.)) - (-log(tan(faketheta / 2.)));
741  }
742  } else {
743  edm::LogVerbatim("TrackValidator")
744  << "reco::Track #" << rT << " with pt=" << track->pt() << " NOT associated to any TrackingParticle"
745  << "\n";
746 
747  fakeeta = -100.;
748  faketheta = -100;
749  fakephi = -100.;
750  fakept = -100.;
751  faked0 = -100.;
752  fakez0 = -100;
753  fakerespt = -100.;
754  fakeresd0 = -100.;
755  fakeresz0 = -100.;
756  fakeresphi = -100.;
757  fakereseta = -100.;
758  fakerescottheta = -100.;
759  fakenhit = 100;
760  fake = 0;
761  }
762 
763  tree_fake->Fill();
764  }
765 
766  } // End of loop on fakerate
767 
768  } // End of loop on associators
769 }
770 
771 // ------------ method called once each job just after ending the event loop ------------
773  edm::LogVerbatim("ValidationMisalignedTracker") << "\t Misalignment analysis completed \n";
774 }
775 
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