CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/Alignment/OfflineValidation/plugins/ValidationMisalignedTracker.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    ValidationMisalignedTracker
00004 // Class:      ValidationMisalignedTracker
00005 // 
00013 //
00014 // Original Author:  Nicola De Filippis
00015 //         Created:  Thu Dec 14 13:13:32 CET 2006
00016 // $Id: ValidationMisalignedTracker.cc,v 1.5 2010/09/23 12:06:42 mussgill Exp $
00017 //
00018 //
00019 
00020 
00021 #include "Alignment/OfflineValidation/plugins/ValidationMisalignedTracker.h"
00022 
00023 
00024 // user include files
00025 
00026 #include "DataFormats/TrackReco/interface/Track.h"
00027 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
00028 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
00029 
00030 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00031 #include "FWCore/Framework/interface/MakerMacros.h"
00032 #include "TrackingTools/TrajectoryState/interface/FreeTrajectoryState.h"
00033 #include "TrackingTools/PatternTools/interface/TSCPBuilderNoMaterial.h"
00034 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00035 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
00036 
00037 //
00038 // constructors and destructor
00039 //
00040 ValidationMisalignedTracker::ValidationMisalignedTracker(const edm::ParameterSet& iConfig)
00041 {
00042 
00043   //now do what ever initialization is needed
00044   mzmu=0.,recmzmu=0.,ptzmu=0.,recptzmu=0.,etazmu=0.,recetazmu=0., thetazmu=0.,recthetazmu=0.,phizmu=0.,recphizmu=0.;
00045   recenezmu=0., enezmu=0., pLzmu=0., recpLzmu=0.,yzmu=0.,recyzmu=0.,mxptmu=0.,recmxptmu=0., minptmu=0.,recminptmu=0.;
00046   // mzele=0.,recmzele=0.
00047   
00048   flag=0,flagrec=0,count=0,countrec=0;
00049   nAssoc=0;
00050 
00051   for (int i=0;i<2;i++){
00052     countpart[i]=0;
00053     countpartrec[i]=0; 
00054     for (int j=0;j<2;j++){
00055       ene[i][j]=0.;
00056       p[i][j]=0.;
00057       px[i][j]=0.;
00058       py[i][j]=0.; 
00059       pz[i][j]=0.;
00060       ptmu[i][j]=0.; 
00061       recene[i][j]=0.;
00062       recp[i][j]=0.;
00063       recpx[i][j]=0.;
00064       recpy[i][j]=0.; 
00065       recpz[i][j]=0.;       
00066       recptmu[i][j]=0.;
00067     }
00068   }
00069 
00070 
00071   eventCount_ = 0; 
00072 
00073   selection_eff    = iConfig.getUntrackedParameter<bool>("selection_eff","false");
00074   selection_fake   = iConfig.getUntrackedParameter<bool>("selection_fake","true");
00075   ZmassSelection_  = iConfig.getUntrackedParameter<bool>("ZmassSelection","false");
00076   simobject        = iConfig.getUntrackedParameter<std::string>("simobject","g4SimHits");
00077   trackassociator  = iConfig.getUntrackedParameter<std::string>("TrackAssociator","ByHits");
00078   associators      = iConfig.getParameter< std::vector<std::string> >("associators");
00079   label            = iConfig.getParameter< std::vector<edm::InputTag> >("label");
00080   label_tp_effic   = iConfig.getParameter< edm::InputTag >("label_tp_effic");
00081   label_tp_fake    = iConfig.getParameter< edm::InputTag >("label_tp_fake");
00082 
00083   rootfile_   = iConfig.getUntrackedParameter<std::string>("rootfile","myroot.root");
00084   file_ = new TFile(rootfile_.c_str(),"RECREATE");
00085   
00086   // initialize the tree
00087   tree_eff = new TTree("EffTracks","Efficiency Tracks Tree");
00088 
00089   tree_eff->Branch("Run",&irun,"irun/i");
00090   tree_eff->Branch("Event",&ievt,"ievt/i");
00091 
00092   // SimTrack
00093   tree_eff->Branch("TrackID",&trackType,"trackType/i");
00094   tree_eff->Branch("pt",&pt,"pt/F");
00095   tree_eff->Branch("eta",&eta,"eta/F");
00096   tree_eff->Branch("CotTheta",&cottheta,"cottheta/F");
00097   tree_eff->Branch("phi",&phi,"phi/F");
00098   tree_eff->Branch("d0",&d0,"d0/F");
00099   tree_eff->Branch("z0",&z0,"z0/F"); 
00100   tree_eff->Branch("nhit",&nhit,"nhit/i");
00101     
00102   // RecTrack
00103   tree_eff->Branch("recpt",&recpt,"recpt/F");
00104   tree_eff->Branch("receta",&receta,"receta/F");
00105   tree_eff->Branch("CotRecTheta",&reccottheta,"reccottheta/F");
00106   tree_eff->Branch("recphi",&recphi,"recphi/F");
00107   tree_eff->Branch("recd0",& recd0,"recd0/F");
00108   tree_eff->Branch("recz0",& recz0,"recz0/F");
00109   tree_eff->Branch("nAssoc",&nAssoc,"nAssoc/i");
00110   tree_eff->Branch("recnhit",&recnhit,"recnhit/i");
00111   tree_eff->Branch("CHISQ",&recchiq,"recchiq/F");
00112 
00113   tree_eff->Branch("reseta",&reseta,"reseta/F");
00114   tree_eff->Branch("respt",&respt,"respt/F");
00115   tree_eff->Branch("resd0",&resd0,"resd0/F");
00116   tree_eff->Branch("resz0",&resz0,"resz0/F");
00117   tree_eff->Branch("resphi",&resphi,"resphi/F");
00118   tree_eff->Branch("rescottheta",&rescottheta,"rescottheta/F");
00119   tree_eff->Branch("eff",&eff,"eff/F");
00120 
00121   // Invariant masses, pt of Z
00122   tree_eff->Branch("mzmu",&mzmu,"mzmu/F");
00123   tree_eff->Branch("ptzmu",&ptzmu,"ptzmu/F");
00124   tree_eff->Branch("pLzmu",&pLzmu,"pLzmu/F");
00125   tree_eff->Branch("enezmu",&enezmu,"enezmu/F");
00126   tree_eff->Branch("etazmu",&etazmu,"etazmu/F");
00127   tree_eff->Branch("thetazmu",&thetazmu,"thetazmu/F");
00128   tree_eff->Branch("phizmu",&phizmu,"phizmu/F");
00129   tree_eff->Branch("yzmu",&yzmu,"yzmu/F");
00130   tree_eff->Branch("mxptmu",&mxptmu,"mxptmu/F");
00131   tree_eff->Branch("minptmu",&minptmu,"minptmu/F");
00132 
00133   tree_eff->Branch("recmzmu",&recmzmu,"recmzmu/F");
00134   tree_eff->Branch("recptzmu",&recptzmu,"recptzmu/F");
00135   tree_eff->Branch("recpLzmu",&recpLzmu,"recpLzmu/F");  
00136   tree_eff->Branch("recenezmu",&recenezmu,"recenezmu/F");
00137   tree_eff->Branch("recetazmu",&recetazmu,"recetazmu/F");
00138   tree_eff->Branch("recthetazmu",&recthetazmu,"recthetazmu/F");
00139   tree_eff->Branch("recphizmu",&recphizmu,"recphizmu/F");
00140   tree_eff->Branch("recyzmu",&recyzmu,"recyzmu/F");
00141   tree_eff->Branch("recmxptmu",&recmxptmu,"recmxptmu/F");   
00142   tree_eff->Branch("recminptmu",&recminptmu,"recminptmu/F");       
00143 
00144 
00145   //tree->Branch("mzele",&ntmzele,"ntmzele/F");
00146   //tree->Branch("recmzele",&ntmzeleRec,"ntmzeleRec/F");
00147   tree_eff->Branch("chi2Associator",&recchiq,"recchiq/F");
00148 
00149   // Fake
00150 
00151   tree_fake = new TTree("FakeTracks","Fake Rate Tracks Tree");
00152 
00153   tree_fake->Branch("Run",&irun,"irun/i");
00154   tree_fake->Branch("Event",&ievt,"ievt/i");
00155 
00156   // SimTrack
00157   tree_fake->Branch("fakeTrackID",&faketrackType,"faketrackType/i");
00158   tree_fake->Branch("fakept",&fakept,"fakept/F");
00159   tree_fake->Branch("fakeeta",&fakeeta,"fakeeta/F");
00160   tree_fake->Branch("fakeCotTheta",&fakecottheta,"fakecottheta/F");
00161   tree_fake->Branch("fakephi",&fakephi,"fakephi/F");
00162   tree_fake->Branch("faked0",&faked0,"faked0/F");
00163   tree_fake->Branch("fakez0",&fakez0,"fakez0/F"); 
00164   tree_fake->Branch("fakenhit",&fakenhit,"fakenhit/i");
00165     
00166   // RecTrack
00167   tree_fake->Branch("fakerecpt",&fakerecpt,"fakerecpt/F");
00168   tree_fake->Branch("fakereceta",&fakereceta,"fakereceta/F");
00169   tree_fake->Branch("fakeCotRecTheta",&fakereccottheta,"fakereccottheta/F");
00170   tree_fake->Branch("fakerecphi",&fakerecphi,"fakerecphi/F");
00171   tree_fake->Branch("fakerecd0",& fakerecd0,"fakerecd0/F");
00172   tree_fake->Branch("fakerecz0",& fakerecz0,"fakerecz0/F");
00173   tree_fake->Branch("fakenAssoc",&fakenAssoc,"fakenAssoc/i");
00174   tree_fake->Branch("fakerecnhit",&fakerecnhit,"fakerecnhit/i");
00175   tree_fake->Branch("fakeCHISQ",&fakerecchiq,"fakerecchiq/F");
00176 
00177   tree_fake->Branch("fakereseta",&fakereseta,"fakereseta/F");
00178   tree_fake->Branch("fakerespt",&fakerespt,"fakerespt/F");
00179   tree_fake->Branch("fakeresd0",&fakeresd0,"fakeresd0/F");
00180   tree_fake->Branch("fakeresz0",&fakeresz0,"fakeresz0/F");
00181   tree_fake->Branch("fakeresphi",&fakeresphi,"fakeresphi/F");
00182   tree_fake->Branch("fakerescottheta",&fakerescottheta,"fakerescottheta/F");
00183   tree_fake->Branch("fake",&fake,"fake/F");
00184 
00185   // Invariant masses, pt of Z
00186   tree_fake->Branch("fakemzmu",&fakemzmu,"fakemzmu/F");
00187   tree_fake->Branch("fakeptzmu",&fakeptzmu,"fakeptzmu/F");
00188   tree_fake->Branch("fakepLzmu",&fakepLzmu,"fakepLzmu/F");
00189   tree_fake->Branch("fakeenezmu",&fakeenezmu,"fakeenezmu/F");
00190   tree_fake->Branch("fakeetazmu",&fakeetazmu,"fakeetazmu/F");
00191   tree_fake->Branch("fakethetazmu",&fakethetazmu,"fakethetazmu/F");
00192   tree_fake->Branch("fakephizmu",&fakephizmu,"fakephizmu/F");
00193   tree_fake->Branch("fakeyzmu",&fakeyzmu,"fakeyzmu/F");
00194   tree_fake->Branch("fakemxptmu",&fakemxptmu,"fakemxptmu/F");
00195   tree_fake->Branch("fakeminptmu",&fakeminptmu,"fakeminptmu/F");
00196 
00197   tree_fake->Branch("fakerecmzmu",&fakerecmzmu,"fakerecmzmu/F");
00198   tree_fake->Branch("fakerecptzmu",&fakerecptzmu,"fakerecptzmu/F");
00199   tree_fake->Branch("fakerecpLzmu",&fakerecpLzmu,"fakerecpLzmu/F");  
00200   tree_fake->Branch("fakerecenezmu",&fakerecenezmu,"fakerecenezmu/F");
00201   tree_fake->Branch("fakerecetazmu",&fakerecetazmu,"fakerecetazmu/F");
00202   tree_fake->Branch("fakerecthetazmu",&fakerecthetazmu,"fakerecthetazmu/F");
00203   tree_fake->Branch("fakerecphizmu",&fakerecphizmu,"fakerecphizmu/F");
00204   tree_fake->Branch("fakerecyzmu",&fakerecyzmu,"fakerecyzmu/F");
00205   tree_fake->Branch("fakerecmxptmu",&fakerecmxptmu,"fakerecmxptmu/F");   
00206   tree_fake->Branch("fakerecminptmu",&fakerecminptmu,"fakerecminptmu/F");       
00207 
00208   tree_fake->Branch("fakechi2Associator",&fakerecchiq,"fakerecchiq/F");
00209 
00210 }
00211 
00212 
00213 ValidationMisalignedTracker::~ValidationMisalignedTracker()
00214 {
00215  
00216  
00217   std::cout << "ValidationMisalignedTracker::endJob Processed " << eventCount_
00218             << " events" << std::endl;
00219                                                                                                                    
00220   // store the tree in the output file
00221   file_->Write();
00222                                                                                                                    
00223   
00224   // Closing the file deletes the tree.
00225   file_->Close();
00226   tree_eff=0;
00227   tree_fake=0;
00228 }
00229 
00230 
00231 //
00232 // member functions
00233 //
00234 
00235 // ------------ method called to for each event  ------------
00236 void
00237 ValidationMisalignedTracker::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00238 {
00239    if (watchTrackAssociatorRecord_.check(iSetup)) {
00240      associatore.clear();
00241      edm::ESHandle<TrackAssociatorBase> theAssociator;
00242      for (unsigned int w=0;w<associators.size();w++) {
00243        iSetup.get<TrackAssociatorRecord>().get(associators[w], theAssociator);
00244        associatore.push_back( theAssociator.product() );
00245      }
00246    }
00247    
00248    edm::LogInfo("Tracker Misalignment Validation") << "\n Starting!";
00249 
00250    // Monte Carlo Z selection
00251    skip=false;
00252    std::vector<int> indmu;
00253    
00254    if ( selection_eff && ZmassSelection_){ 
00255      edm::Handle<edm::HepMCProduct> evt;
00256      iEvent.getByLabel("source", evt);
00257      bool accepted = false;
00258      bool skip=false;
00259      bool foundmuons=false;
00260      HepMC::GenEvent * myGenEvent = new  HepMC::GenEvent(*(evt->GetEvent()));
00261      
00262      for ( HepMC::GenEvent::particle_iterator p = myGenEvent->particles_begin(); p != myGenEvent->particles_end(); ++p ) { 
00263        if ( !accepted && ( (*p)->pdg_id() == 23 ) && (*p)->status() == 3 ) { 
00264          accepted=true;
00265          for( HepMC::GenVertex::particle_iterator aDaughter=(*p)->end_vertex()->particles_begin(HepMC::descendants); aDaughter !=(*p)->end_vertex()->particles_end(HepMC::descendants);aDaughter++){
00266            if ( abs((*aDaughter)->pdg_id())==13) {
00267              foundmuons=true;
00268              if ((*aDaughter)->status()!=1 ) {
00269                for( HepMC::GenVertex::particle_iterator byaDaughter=(*aDaughter)->end_vertex()->particles_begin(HepMC::descendants); byaDaughter !=(*aDaughter)->end_vertex()->particles_end(HepMC::descendants);byaDaughter++){
00270                  if ((*byaDaughter)->status()==1 && abs((*byaDaughter)->pdg_id())==13) {                   
00271                    indmu.push_back((*byaDaughter)->barcode());
00272                    std::cout << "Stable muon from Z with charge " <<  (*byaDaughter)->pdg_id()  << " and index "<< (*byaDaughter)->barcode() << std::endl;
00273                  }
00274                }
00275              }
00276              else {            
00277                indmu.push_back((*aDaughter)->barcode());
00278                std::cout << "Stable muon from Z with charge " <<  (*aDaughter)->pdg_id() << " and index "<< (*aDaughter)->barcode() << std::endl; 
00279              }       
00280            }               
00281          }
00282          if (!foundmuons){
00283            std::cout << "No muons from Z ...skip event" << std::endl;
00284            skip=true;
00285          } 
00286        }
00287      }     
00288      if ( !accepted) {
00289        std::cout << "No Z particles in the event ...skip event" << std::endl;
00290        skip=true;
00291      }   
00292    }
00293    else {
00294      skip=false;
00295    }
00296    
00297    //
00298    // Retrieve tracker geometry from event setup
00299    //
00300    edm::ESHandle<TrackerGeometry> trackerGeometry;
00301    iSetup.get<TrackerDigiGeometryRecord>().get( trackerGeometry );
00302    GeomDet* testGeomDet = trackerGeometry->detsTOB().front();
00303    std::cout << testGeomDet->position() << std::endl;
00304    
00305    
00306    //Dump Run and Event
00307    irun=iEvent.id().run();
00308    ievt=iEvent.id().event();
00309 
00310    // Reset tree variables
00311    int countpart[2]={0,0},countpartrec[2]={0,0},flag=0,flagrec=0,count=0,countrec=0;
00312    //int countsim=0;
00313    float ene[2][2],p[2][2],px[2][2],py[2][2],pz[2][2],ptmu[2][2];
00314    float recene[2][2],recp[2][2],recpx[2][2],recpy[2][2],recpz[2][2],recptmu[2][2];
00315    
00316    for (int i=0;i<2;i++){
00317      for (int j=0;j<2;j++){
00318        ene[i][j]=0.;
00319        p[i][j]=0.;
00320        px[i][j]=0.;
00321        py[i][j]=0.; 
00322        pz[i][j]=0.; 
00323        ptmu[i][j]=0.;
00324        recene[i][j]=0.;
00325        recp[i][j]=0.;
00326        recpx[i][j]=0.;
00327        recpy[i][j]=0.; 
00328        recpz[i][j]=0.;       
00329        recptmu[i][j]=0.;
00330      }
00331    }
00332    
00333    
00334    edm::Handle<TrackingParticleCollection>  TPCollectionHeff ;
00335    iEvent.getByLabel(label_tp_effic,TPCollectionHeff);
00336    const TrackingParticleCollection tPCeff = *(TPCollectionHeff.product());
00337    
00338    edm::Handle<TrackingParticleCollection>  TPCollectionHfake ;
00339    iEvent.getByLabel(label_tp_fake,TPCollectionHfake);
00340    const TrackingParticleCollection tPCfake = *(TPCollectionHfake.product());
00341    
00342                
00343    int w=0;
00344    for (unsigned int ww=0;ww<associators.size();ww++){
00345       //
00346       //get collections from the event
00347       //
00348 
00349       edm::InputTag algo = label[0];
00350      
00351       edm::Handle<edm::View<reco::Track> > trackCollection;
00352       iEvent.getByLabel(algo, trackCollection);
00353       const edm::View<reco::Track> tC = *(trackCollection.product());
00354             
00355            
00356       //associate tracks
00357       LogTrace("TrackValidator") << "Calling associateRecoToSim method" << "\n";
00358            reco::RecoToSimCollection recSimColl=associatore[ww]->associateRecoToSim(trackCollection,
00359                                                               TPCollectionHfake,
00360                                                                       &iEvent);
00361       
00362       LogTrace("TrackValidator") << "Calling associateSimToReco method" << "\n";
00363       reco::SimToRecoCollection simRecColl=associatore[ww]->associateSimToReco(trackCollection,
00364                                                                       TPCollectionHeff, 
00365                                                                       &iEvent);
00366 
00367    
00368 
00369       //
00370       //compute number of tracks per eta interval
00371       //
00372 
00373       if (selection_eff && !skip ) {
00374         std::cout << "Computing Efficiency" << std::endl;
00375 
00376         edm::LogVerbatim("TrackValidator") << "\n# of TrackingParticles (before cuts): " << tPCeff.size() << "\n";
00377         int ats = 0;
00378         int st=0;
00379         for (TrackingParticleCollection::size_type i=0; i<tPCeff.size(); i++){
00380           
00381           // Initialize variables
00382           eta = 0.,theta=0.,phi=0.,pt=0.,cottheta=0.,costheta=0.;
00383           d0=0.,z0=0.;
00384           nhit=0;
00385           receta = 0.,rectheta = 0.,recphi = 0.,recpt = 0.,reccottheta=0.,recd0=0.,recz0=0.;
00386           respt = 0.,resd0 = 0.,resz0 = 0.,reseta = 0.,resphi=0.,rescottheta=0.;
00387           recchiq = 0.;
00388           recnhit = 0;
00389           trackType = 0;
00390           eff=0;        
00391           
00392           // typedef edm::Ref<TrackingParticleCollection> TrackingParticleRef;
00393           TrackingParticleRef tp(TPCollectionHeff, i);
00394           if (tp->charge()==0) continue;
00395           st++;
00396           //pt=sqrt(tp->momentum().perp2());
00397           //eta=tp->momentum().eta();
00398           //vpos=tp->vertex().perp2()));
00399           
00400           const SimTrack * simulatedTrack = &(*tp->g4Track_begin());
00401         
00402           edm::ESHandle<MagneticField> theMF;
00403           iSetup.get<IdealMagneticFieldRecord>().get(theMF);
00404           FreeTrajectoryState 
00405             ftsAtProduction(GlobalPoint(tp->vertex().x(),tp->vertex().y(),tp->vertex().z()),
00406                             GlobalVector(simulatedTrack->momentum().x(),simulatedTrack->momentum().y(),simulatedTrack->momentum().z()),
00407                             TrackCharge(tp->charge()),
00408                             theMF.product());
00409           TSCPBuilderNoMaterial tscpBuilder;
00410           TrajectoryStateClosestToPoint tsAtClosestApproach 
00411             = tscpBuilder(ftsAtProduction,GlobalPoint(0,0,0));//as in TrackProducerAlgorithm
00412           GlobalPoint v = tsAtClosestApproach.theState().position();
00413           GlobalVector p = tsAtClosestApproach.theState().momentum();
00414 
00415           //  double qoverpSim = tsAtClosestApproach.charge()/p.mag();
00416           //  double lambdaSim = M_PI/2-p.theta();
00417           //  double phiSim    = p.phi();
00418           double dxySim    = (-v.x()*sin(p.phi())+v.y()*cos(p.phi()));
00419           double dszSim    = v.z()*p.perp()/p.mag() - (v.x()*p.x()+v.y()*p.y())/p.perp() * p.z()/p.mag();
00420           d0     = float(-dxySim);
00421           z0     = float(dszSim*p.mag()/p.perp());
00422 
00423   
00424           if (abs(simulatedTrack->type())==13 && simulatedTrack->genpartIndex() != -1 ) {
00425             std::cout << " TRACCIA SIM DI MUONI " << std::endl;  
00426             std::cout << "Gen part " << simulatedTrack->genpartIndex()<< std::endl;
00427             trackType=simulatedTrack->type();
00428             theta=simulatedTrack->momentum().theta();
00429             costheta=cos(theta);
00430             cottheta=1./tan(theta);
00431             
00432             eta=simulatedTrack->momentum().eta();
00433             phi=simulatedTrack->momentum().phi();
00434             pt=simulatedTrack->momentum().pt();
00435             nhit=tp->matchedHit();
00436             
00437 
00438             std::cout << "3) Before assoc: SimTrack of type = " << simulatedTrack->type() 
00439                       << " ,at eta = " << eta 
00440                       << " ,with pt at vertex = " << simulatedTrack->momentum().pt() << " GeV/c"
00441                       << " ,d0 =" << d0 
00442                       << " ,z0 =" << z0 
00443                       << " ,nhit=" << nhit
00444                       << std::endl;
00445 
00446             if ( ZmassSelection_ ){
00447               if (abs(trackType)==13 && (simulatedTrack->genpartIndex()==indmu[0] || simulatedTrack->genpartIndex()==indmu[1] )) { 
00448                 std::cout << " TRACK sim of muons from Z " << std::endl;  
00449                 flag=0;
00450                 count=countpart[0];
00451                 countpart[0]++;
00452               }
00453               else if (abs(trackType)==11) {
00454                 //std::cout << " TRACCIA SIM DI ELETTRONI " << std::endl;  
00455                 flag=1;
00456                 count=countpart[1];
00457                 countpart[1]++;
00458               }
00459               
00460               
00461               px[flag][count]=simulatedTrack->momentum().x();   
00462               py[flag][count]=simulatedTrack->momentum().y();
00463               pz[flag][count]=simulatedTrack->momentum().z();
00464               ptmu[flag][count]=simulatedTrack->momentum().pt(); 
00465               ene[flag][count]=simulatedTrack->momentum().e();
00466             }
00467             
00468             
00469             std::vector<std::pair<edm::RefToBase<reco::Track>, double> > rt;
00470             if(simRecColl.find(tp) != simRecColl.end()){
00471               
00472               rt = simRecColl[tp];
00473               if (rt.size()!=0) {
00474                 
00475                 edm::RefToBase<reco::Track> t = rt.begin()->first;
00476                 ats++;
00477 
00478                 bool flagptused=false;
00479                 for (unsigned int j=0;j<ptused.size();j++){
00480                   if (fabs(t->pt()-ptused[j])<0.001) {
00481                     flagptused=true;
00482                   }
00483                 }
00484 
00485                 edm::LogVerbatim("TrackValidator") << "TrackingParticle #" << st << " with pt=" << t->pt() 
00486                                                    << " associated with quality:" << rt.begin()->second <<"\n";
00487                 std::cout << "Reconstructed Track:" << t->pt()<< std::endl;
00488                 std::cout << "\tpT: " << t->pt()<< std::endl;
00489                 std::cout << "\timpact parameter:d0: " << t->d0()<< std::endl;
00490                 std::cout << "\timpact parameter:z0: " << t->dz()<< std::endl;
00491                 std::cout << "\tAzimuthal angle of point of closest approach:" << t->phi()<< std::endl;
00492                 std::cout << "\tcharge: " << t->charge()<< std::endl;
00493                 std::cout << "\teta: " << t->eta()<< std::endl;
00494                 std::cout << "\tnormalizedChi2: " << t->normalizedChi2()<< std::endl;
00495 
00496                 recnhit=t->numberOfValidHits();
00497                 recchiq=t->normalizedChi2();
00498                 rectheta=t->theta();
00499                 reccottheta=1./tan(rectheta);
00500                 //receta=-log(tan(rectheta/2.));
00501                 receta=t->momentum().eta();
00502                 //         reccostheta=cos(matchedrectrack->momentum().theta());
00503                 recphi=t->phi(); 
00504                 recpt=t->pt();
00505                 ptused.push_back(recpt);
00506                 recd0=t->d0();
00507                 recz0=t->dz();
00508 
00509                 std::cout << "5) After call to associator: the best match has " 
00510                           << recnhit << " hits, Chi2 = " 
00511                           << recchiq << ", pt at vertex = " 
00512                           << recpt << " GeV/c, " 
00513                           << ", recd0 = " << recd0 
00514                           << ", recz0= " << recz0
00515                           << std::endl;
00516 
00517 
00518                 respt=recpt - pt;
00519                 resd0=recd0-d0;
00520                 resz0=recz0-z0;
00521                 reseta=receta-eta;
00522                 resphi=recphi-phi;
00523                 rescottheta=reccottheta-cottheta;
00524                 eff=1;
00525 
00526                 std::cout << "6) Transverse momentum residual=" << respt 
00527                           << " ,d0 residual=" << resd0 
00528                           << " ,z0 residual=" << resz0 
00529                           << " with eff=" << eff << std::endl;
00530                 
00531                 if ( ZmassSelection_ ){
00532                   
00533                   if (abs(trackType)==13) { 
00534                     std::cout << " TRACCIA RECO DI MUONI " << std::endl;  
00535                     flagrec=0;
00536                     countrec=countpartrec[0];
00537                     countpartrec[0]++;
00538                   }
00539                   else if (abs(trackType)==11) {
00540                     std::cout << " TRACCIA RECO DI ELETTRONI " << std::endl;  
00541                     flagrec=1;
00542                     countrec=countpartrec[1];
00543                     countpartrec[1]++;
00544                   }
00545                   
00546                   recp[flagrec][countrec]=sqrt(t->momentum().mag2());
00547                   recpx[flagrec][countrec]=t->momentum().x();   
00548                   recpy[flagrec][countrec]=t->momentum().y();
00549                   recpz[flagrec][countrec]=t->momentum().z();
00550                   recptmu[flagrec][countrec]=sqrt( (t->momentum().x()*t->momentum().x()) + (t->momentum().y()*t->momentum().y()) );
00551                   if (abs(trackType)==13) recene[flagrec][countrec]=sqrt(recp[flagrec][countrec]*recp[flagrec][countrec]+0.105*0.105);
00552                   if (abs(trackType)==11) recene[flagrec][countrec]=sqrt(recp[flagrec][countrec]*recp[flagrec][countrec]+0.0005*0.0005);
00553                 }
00554                 
00555                 std::cout << "7) Transverse momentum reconstructed =" << recpt 
00556                           << " at  eta= " << receta 
00557                           << " and phi= " << recphi 
00558                           << std::endl;
00559                 
00560               }
00561             }
00562             else{
00563               edm::LogVerbatim("TrackValidator") << "TrackingParticle #" << st
00564                                                  << " with pt=" << sqrt(tp->momentum().perp2())
00565                                                  << " NOT associated to any reco::Track" << "\n";
00566               receta =-100.;
00567               recphi =-100.;
00568               recpt  =-100.;
00569               recd0  =-100.;
00570               recz0  =-100;
00571               respt  =-100.;
00572               resd0  =-100.;
00573               resz0  =-100.;
00574               resphi =-100.;
00575               reseta =-100.;
00576               rescottheta=-100.;
00577               recnhit=100;
00578               recchiq=-100;
00579               eff=0;
00580               flagrec=100;            
00581             }
00582             
00583             std::cout << "Eff=" << eff << std::endl;
00584                
00585                // simulated muons
00586                
00587                std::cout <<"Flag is" << flag << std::endl;
00588                std::cout <<"RecFlag is" << flagrec << std::endl;
00589                
00590                if (countpart[0]==2 && flag==0) {
00591                  mzmu=sqrt(
00592                            (ene[0][0]+ene[0][1])*(ene[0][0]+ene[0][1])-
00593                            (px[0][0]+px[0][1])*(px[0][0]+px[0][1])-
00594                            (py[0][0]+py[0][1])*(py[0][0]+py[0][1])-
00595                            (pz[0][0]+pz[0][1])*(pz[0][0]+pz[0][1])
00596                            );
00597                  std::cout << "Mzmu " << mzmu << std::endl;
00598                  ptzmu=sqrt(
00599                            (px[0][0]+px[0][1])*(px[0][0]+px[0][1])+
00600                            (py[0][0]+py[0][1])*(py[0][0]+py[0][1])
00601                            );
00602                  
00603                  pLzmu=pz[0][0]+pz[0][1];
00604                  enezmu=ene[0][0]+ene[0][1];
00605                  phizmu=atan2((py[0][0]+py[0][1]),(px[0][0]+px[0][1]));
00606                  thetazmu=atan2(ptzmu,(pz[0][0]+pz[0][1]));
00607                  etazmu=-log(tan(thetazmu*3.14/360.));
00608                  yzmu=0.5*log((enezmu+pLzmu)/(enezmu-pLzmu));
00609                  mxptmu=std::max( ptmu[0][0], ptmu[0][1]);
00610                  minptmu=std::min( ptmu[0][0], ptmu[0][1]);
00611                }
00612                else {
00613                  mzmu=-100.;
00614                  ptzmu=-100.;
00615                  pLzmu=-100.;
00616                  enezmu=-100.;
00617                  etazmu=-100.;
00618                  phizmu=-100.;
00619                  thetazmu=-100.;
00620                  yzmu=-100.;
00621                  mxptmu=-100.;
00622                  minptmu=-100.;
00623                }      
00624 
00625                // reconstructed muons
00626                if (countpartrec[0]==2 && flagrec==0 ){
00627                  recmzmu=sqrt(
00628                               (recene[0][0]+recene[0][1])*(recene[0][0]+recene[0][1])-
00629                               (recpx[0][0]+recpx[0][1])*(recpx[0][0]+recpx[0][1])-
00630                               (recpy[0][0]+recpy[0][1])*(recpy[0][0]+recpy[0][1])-
00631                               (recpz[0][0]+recpz[0][1])*(recpz[0][0]+recpz[0][1])
00632                               );
00633                  std::cout << "RecMzmu " << recmzmu << std::endl;
00634                  recptzmu=sqrt(
00635                                (recpx[0][0]+recpx[0][1])*(recpx[0][0]+recpx[0][1])+
00636                                (recpy[0][0]+recpy[0][1])*(recpy[0][0]+recpy[0][1])
00637                                );
00638                  
00639                  recpLzmu=recpz[0][0]+recpz[0][1];      
00640                  recenezmu=recene[0][0]+recene[0][1];
00641                  recphizmu=atan2((recpy[0][0]+recpy[0][1]),(recpx[0][0]+recpx[0][1]));
00642                  recthetazmu=atan2(recptzmu,(recpz[0][0]+recpz[0][1]));
00643                  recetazmu=-log(tan(recthetazmu*3.14/360.));
00644                  recyzmu=0.5*log((recenezmu+recpLzmu)/(recenezmu-recpLzmu));
00645                  recmxptmu=std::max(recptmu[0][0], recptmu[0][1]);
00646                  recminptmu=std::min( recptmu[0][0], recptmu[0][1]);
00647                }
00648                else {
00649                  recmzmu=-100.;
00650                  recptzmu=-100.; 
00651                  recpLzmu=-100.;                 
00652                  recenezmu=-100.;       
00653                  recetazmu=-100.;
00654                  recphizmu=-100.;
00655                  recthetazmu=-100.;
00656                  recyzmu=-100.;
00657                  recmxptmu=-100;
00658                  recminptmu=-100.;
00659                }               
00660                
00661                tree_eff->Fill();
00662 
00663           } // end of loop on muons
00664         } // end of loop for tracking particle
00665       } // end of loop for efficiency
00666 
00667       //
00668       // Fake Rate
00669       // 
00670       if (selection_fake ) {
00671         std::cout << "Computing Fake Rate" << std::endl;
00672 
00673         fakeeta = 0.,faketheta=0.,fakephi=0.,fakept=0.,fakecottheta=0.,fakecostheta=0.;
00674         faked0=0.,fakez0=0.;
00675         fakenhit=0;
00676         fakereceta = 0.,fakerectheta = 0.,fakerecphi = 0.,fakerecpt = 0.,fakereccottheta=0.,fakerecd0=0.,fakerecz0=0.;
00677         fakerespt = 0.,fakeresd0 = 0.,fakeresz0 = 0.,fakereseta = 0.,fakeresphi=0.,fakerescottheta=0.;
00678         fakerecchiq = 0.;
00679         fakerecnhit = 0;
00680         faketrackType = 0;
00681         fake=0;
00682 
00683         
00684         //      int at=0;
00685         int rT=0;
00686         for(reco::TrackCollection::size_type i=0; i<tC.size(); ++i){
00687           edm::RefToBase<reco::Track> track(trackCollection, i);
00688           rT++;
00689 
00690           fakeeta = 0.,faketheta=0.,fakephi=0.,fakept=0.,fakecottheta=0.,fakecostheta=0.;
00691           faked0=0.,fakez0=0.;
00692           fakenhit=0;
00693           fakereceta = 0.,fakerectheta = 0.,fakerecphi = 0.,fakerecpt = 0.,fakereccottheta=0.,fakerecd0=0.,fakerecz0=0.;
00694           fakerespt = 0.,fakeresd0 = 0.,fakeresz0 = 0.,fakereseta = 0.,fakeresphi=0.,fakerescottheta=0.;
00695           fakerecchiq = 0.;
00696           fakerecnhit = 0;
00697           faketrackType = 0;
00698           fake=0;
00699           
00700           fakerecnhit=track->numberOfValidHits();
00701           fakerecchiq=track->normalizedChi2();
00702           fakerectheta=track->theta();
00703           fakereccottheta=1./tan(rectheta);
00704           //fakereceta=-log(tan(rectheta/2.));
00705           fakereceta=track->momentum().eta();
00706           //       fakereccostheta=cos(track->momentum().theta());
00707           fakerecphi=track->phi(); 
00708           fakerecpt=track->pt();
00709           fakerecd0=track->d0();
00710           fakerecz0=track->dz();
00711                   
00712           std::cout << "1) Before assoc: TkRecTrack at eta = " << fakereceta << std::endl;
00713           std::cout << "Track number "<< i << std::endl ;
00714           std::cout << "\tPT: " << track->pt()<< std::endl;
00715           std::cout << "\timpact parameter:d0: " << track->d0()<< std::endl;
00716           std::cout << "\timpact parameter:z0: " << track->dz()<< std::endl;
00717           std::cout << "\tAzimuthal angle of point of closest approach:" << track->phi()<< std::endl;
00718           std::cout << "\tcharge: " << track->charge()<< std::endl;
00719           std::cout << "\teta: " << track->eta()<< std::endl;
00720           std::cout << "\tnormalizedChi2: " << track->normalizedChi2()<< std::endl;
00721            
00722            
00723           std::vector<std::pair<TrackingParticleRef, double> > tp;
00724           
00725           //Compute fake rate vs eta
00726           if(recSimColl.find(track) != recSimColl.end()){
00727             tp = recSimColl[track];
00728             if (tp.size()!=0) {
00729               edm::LogVerbatim("TrackValidator") << "reco::Track #" << rT << " with pt=" << track->pt() 
00730                                                  << " associated with quality:" << tp.begin()->second <<"\n";
00731               
00732               
00733               TrackingParticleRef tpr = tp.begin()->first;
00734               const SimTrack * fakeassocTrack = &(*tpr->g4Track_begin());
00735 
00736               edm::ESHandle<MagneticField> theMF;
00737               iSetup.get<IdealMagneticFieldRecord>().get(theMF);
00738               FreeTrajectoryState 
00739                 ftsAtProduction(GlobalPoint(tpr->vertex().x(),tpr->vertex().y(),tpr->vertex().z()),
00740                                 GlobalVector(fakeassocTrack->momentum().x(),fakeassocTrack->momentum().y(),fakeassocTrack->momentum().z()),
00741                                 TrackCharge(tpr->charge()),
00742                                 theMF.product());
00743               TSCPBuilderNoMaterial tscpBuilder;
00744               TrajectoryStateClosestToPoint tsAtClosestApproach 
00745                 = tscpBuilder(ftsAtProduction,GlobalPoint(0,0,0));//as in TrackProducerAlgorithm
00746               GlobalPoint v = tsAtClosestApproach.theState().position();
00747               GlobalVector p = tsAtClosestApproach.theState().momentum();
00748               
00749               //  double qoverpSim = tsAtClosestApproach.charge()/p.mag();
00750               //  double lambdaSim = M_PI/2-p.theta();
00751               //  double phiSim    = p.phi();
00752               double dxySim    = (-v.x()*sin(p.phi())+v.y()*cos(p.phi()));
00753               double dszSim    = v.z()*p.perp()/p.mag() - (v.x()*p.x()+v.y()*p.y())/p.perp() * p.z()/p.mag();
00754               faked0     = float(-dxySim);
00755               fakez0     = float(dszSim*p.mag()/p.perp());
00756 
00757 
00758               faketrackType=fakeassocTrack->type();
00759               faketheta=fakeassocTrack->momentum().theta();
00760               fakecottheta=1./tan(faketheta);
00761               fakeeta=fakeassocTrack->momentum().eta();
00762               fakephi=fakeassocTrack->momentum().phi();
00763               fakept=fakeassocTrack->momentum().pt();
00764               fakenhit=tpr->matchedHit();
00765 
00766               std::cout << "4) After call to associator: the best SimTrack match is of type" << fakeassocTrack->type() 
00767                         << " ,at eta = " << fakeeta 
00768                         << " and phi = " << fakephi  
00769                         << " ,with pt at vertex = " << fakept << " GeV/c" 
00770                         << " ,d0 global = " << faked0 
00771                         << " ,z0 = " << fakez0
00772                         << std::endl;
00773               fake=1;
00774 
00775               fakerespt=fakerecpt-fakept;
00776               fakeresd0=fakerecd0-faked0;
00777               fakeresz0=fakerecz0-fakez0;
00778               fakereseta=-log(tan(fakerectheta/2.))-(-log(tan(faketheta/2.)));
00779               fakeresphi=fakerecphi-fakephi;
00780               fakerescottheta=fakereccottheta-fakecottheta;
00781               
00782             }
00783           }
00784           else{
00785             edm::LogVerbatim("TrackValidator") << "reco::Track #" << rT << " with pt=" << track->pt()
00786                                                << " NOT associated to any TrackingParticle" << "\n";
00787             
00788             fakeeta =-100.;
00789             faketheta=-100;
00790             fakephi =-100.;
00791             fakept  =-100.;
00792             faked0  =-100.;
00793             fakez0  =-100;
00794             fakerespt  =-100.;
00795             fakeresd0  =-100.;
00796             fakeresz0  =-100.;
00797             fakeresphi =-100.;
00798             fakereseta =-100.;
00799             fakerescottheta=-100.;
00800             fakenhit=100;
00801             fake=0;
00802           }
00803           
00804           tree_fake->Fill();
00805         }       
00806         
00807       } // End of loop on fakerate       
00808       
00809       w++;
00810       
00811    } // End of loop on associators
00812 }
00813 
00814 // ------------ method called once each job just after ending the event loop  ------------
00815 void ValidationMisalignedTracker::endJob() {
00816 
00817   std::cout << "\t Misalignment analysis completed \n" << std::endl;  
00818 
00819 }
00820 
00821 DEFINE_FWK_MODULE(ValidationMisalignedTracker);