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