CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/Validation/RecoTrack/plugins/MultiTrackValidator.cc

Go to the documentation of this file.
00001 #include "Validation/RecoTrack/interface/MultiTrackValidator.h"
00002 #include "DQMServices/ClientConfig/interface/FitSlicesYTool.h"
00003 
00004 #include "FWCore/Framework/interface/MakerMacros.h"
00005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00006 
00007 #include "DataFormats/TrackReco/interface/Track.h"
00008 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00009 #include "DataFormats/GsfTrackReco/interface/GsfTrack.h"
00010 #include "DataFormats/GsfTrackReco/interface/GsfTrackFwd.h"
00011 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
00012 #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
00013 #include "SimTracker/TrackAssociation/interface/TrackAssociatorByChi2.h"
00014 #include "SimTracker/TrackAssociation/interface/TrackAssociatorByHits.h"
00015 #include "SimTracker/TrackerHitAssociation/interface/TrackerHitAssociator.h"
00016 #include "SimTracker/Records/interface/TrackAssociatorRecord.h"
00017 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
00018 #include "TrackingTools/TrajectoryState/interface/FreeTrajectoryState.h"
00019 #include "TrackingTools/PatternTools/interface/TSCBLBuilderNoMaterial.h"
00020 #include "SimTracker/TrackAssociation/plugins/ParametersDefinerForTPESProducer.h"
00021 #include "SimTracker/TrackAssociation/plugins/CosmicParametersDefinerForTPESProducer.h"
00022 #include "Validation/RecoTrack/interface/MTVHistoProducerAlgoFactory.h"
00023 
00024 #include "DataFormats/TrackReco/interface/DeDxData.h"
00025 #include "DataFormats/Common/interface/ValueMap.h"
00026 #include "DataFormats/Common/interface/Ref.h"
00027 
00028 #include "TMath.h"
00029 #include <TF1.h>
00030 
00031 using namespace std;
00032 using namespace edm;
00033 
00034 MultiTrackValidator::MultiTrackValidator(const edm::ParameterSet& pset):MultiTrackValidatorBase(pset){
00035   //theExtractor = IsoDepositExtractorFactory::get()->create( extractorName, extractorPSet);
00036 
00037   ParameterSet psetForHistoProducerAlgo = pset.getParameter<ParameterSet>("histoProducerAlgoBlock");
00038   string histoProducerAlgoName = psetForHistoProducerAlgo.getParameter<string>("ComponentName");
00039   histoProducerAlgo_ = MTVHistoProducerAlgoFactory::get()->create(histoProducerAlgoName ,psetForHistoProducerAlgo);
00040   histoProducerAlgo_->setDQMStore(dbe_);
00041 
00042   dirName_ = pset.getParameter<std::string>("dirName");
00043   associatormap = pset.getParameter< edm::InputTag >("associatormap");
00044   UseAssociators = pset.getParameter< bool >("UseAssociators");
00045 
00046   m_dEdx1Tag = pset.getParameter< edm::InputTag >("dEdx1Tag");
00047   m_dEdx2Tag = pset.getParameter< edm::InputTag >("dEdx2Tag");
00048 
00049   tpSelector = TrackingParticleSelector(pset.getParameter<double>("ptMinTP"),
00050                                         pset.getParameter<double>("minRapidityTP"),
00051                                         pset.getParameter<double>("maxRapidityTP"),
00052                                         pset.getParameter<double>("tipTP"),
00053                                         pset.getParameter<double>("lipTP"),
00054                                         pset.getParameter<int>("minHitTP"),
00055                                         pset.getParameter<bool>("signalOnlyTP"),
00056                                         pset.getParameter<bool>("chargedOnlyTP"),
00057                                         pset.getParameter<std::vector<int> >("pdgIdTP"));
00058 
00059   cosmictpSelector = CosmicTrackingParticleSelector(pset.getParameter<double>("ptMinTP"),
00060                                                     pset.getParameter<double>("minRapidityTP"),
00061                                                     pset.getParameter<double>("maxRapidityTP"),
00062                                                     pset.getParameter<double>("tipTP"),
00063                                                     pset.getParameter<double>("lipTP"),
00064                                                     pset.getParameter<int>("minHitTP"),
00065                                                     pset.getParameter<bool>("chargedOnlyTP"),
00066                                                     pset.getParameter<std::vector<int> >("pdgIdTP"));
00067 
00068   useGsf = pset.getParameter<bool>("useGsf");
00069   runStandalone = pset.getParameter<bool>("runStandalone");
00070 
00071 
00072     
00073   if (!UseAssociators) {
00074     associators.clear();
00075     associators.push_back(associatormap.label());
00076   }
00077 }
00078 
00079 
00080 MultiTrackValidator::~MultiTrackValidator(){delete histoProducerAlgo_;}
00081 
00082 void MultiTrackValidator::beginRun(Run const&, EventSetup const& setup) {
00083   //  dbe_->showDirStructure();
00084 
00085   //int j=0;  //is This Necessary ???
00086   for (unsigned int ww=0;ww<associators.size();ww++){
00087     for (unsigned int www=0;www<label.size();www++){
00088       dbe_->cd();
00089       InputTag algo = label[www];
00090       string dirName=dirName_;
00091       if (algo.process()!="")
00092         dirName+=algo.process()+"_";
00093       if(algo.label()!="")
00094         dirName+=algo.label()+"_";
00095       if(algo.instance()!="")
00096         dirName+=algo.instance()+"_";      
00097       if (dirName.find("Tracks")<dirName.length()){
00098         dirName.replace(dirName.find("Tracks"),6,"");
00099       }
00100       string assoc= associators[ww];
00101       if (assoc.find("Track")<assoc.length()){
00102         assoc.replace(assoc.find("Track"),5,"");
00103       }
00104       dirName+=assoc;
00105       std::replace(dirName.begin(), dirName.end(), ':', '_');
00106 
00107       dbe_->setCurrentFolder(dirName.c_str());
00108       
00109       // vector of vector initialization
00110       histoProducerAlgo_->initialize(); //TO BE FIXED. I'D LIKE TO AVOID THIS CALL
00111 
00112       dbe_->goUp(); //Is this really necessary ???
00113       string subDirName = dirName + "/simulation";
00114       dbe_->setCurrentFolder(subDirName.c_str());
00115 
00116       //Booking histograms concerning with simulated tracks
00117       histoProducerAlgo_->bookSimHistos();
00118 
00119       dbe_->cd();
00120       dbe_->setCurrentFolder(dirName.c_str());
00121 
00122       //Booking histograms concerning with reconstructed tracks
00123       histoProducerAlgo_->bookRecoHistos();
00124       if (runStandalone) histoProducerAlgo_->bookRecoHistosForStandaloneRunning();
00125 
00126       if (UseAssociators) {
00127         edm::ESHandle<TrackAssociatorBase> theAssociator;
00128         for (unsigned int w=0;w<associators.size();w++) {
00129           setup.get<TrackAssociatorRecord>().get(associators[w],theAssociator);
00130           associator.push_back( theAssociator.product() );
00131         }//end loop w
00132       }
00133     }//end loop www
00134   }// end loop ww
00135 }
00136 
00137 void MultiTrackValidator::analyze(const edm::Event& event, const edm::EventSetup& setup){
00138   using namespace reco;
00139   
00140   edm::LogInfo("TrackValidator") << "\n====================================================" << "\n"
00141                                  << "Analyzing new event" << "\n"
00142                                  << "====================================================\n" << "\n";
00143   edm::ESHandle<ParametersDefinerForTP> parametersDefinerTP; 
00144   setup.get<TrackAssociatorRecord>().get(parametersDefiner,parametersDefinerTP);    
00145   
00146   edm::Handle<TrackingParticleCollection>  TPCollectionHeff ;
00147   event.getByLabel(label_tp_effic,TPCollectionHeff);
00148   const TrackingParticleCollection tPCeff = *(TPCollectionHeff.product());
00149   
00150   edm::Handle<TrackingParticleCollection>  TPCollectionHfake ;
00151   event.getByLabel(label_tp_fake,TPCollectionHfake);
00152   const TrackingParticleCollection tPCfake = *(TPCollectionHfake.product());
00153   
00154   //if (tPCeff.size()==0) {edm::LogInfo("TrackValidator") 
00155   //<< "TP Collection for efficiency studies has size = 0! Skipping Event." ; return;}
00156   //if (tPCfake.size()==0) {edm::LogInfo("TrackValidator") 
00157   //<< "TP Collection for fake rate studies has size = 0! Skipping Event." ; return;}
00158   
00159   edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
00160   event.getByLabel(bsSrc,recoBeamSpotHandle);
00161   reco::BeamSpot bs = *recoBeamSpotHandle;      
00162   
00163   int w=0; //counter counting the number of sets of histograms
00164   for (unsigned int ww=0;ww<associators.size();ww++){
00165     for (unsigned int www=0;www<label.size();www++){
00166       //
00167       //get collections from the event
00168       //
00169       edm::Handle<View<Track> >  trackCollection;
00170       if(!event.getByLabel(label[www], trackCollection)&&ignoremissingtkcollection_)continue;
00171       //if (trackCollection->size()==0) {
00172       //edm::LogInfo("TrackValidator") << "TrackCollection size = 0!" ; 
00173       //continue;
00174       //}
00175       reco::RecoToSimCollection recSimColl;
00176       reco::SimToRecoCollection simRecColl;
00177       //associate tracks
00178       if(UseAssociators){
00179         edm::LogVerbatim("TrackValidator") << "Analyzing " 
00180                                            << label[www].process()<<":"
00181                                            << label[www].label()<<":"
00182                                            << label[www].instance()<<" with "
00183                                            << associators[ww].c_str() <<"\n";
00184         
00185         LogTrace("TrackValidator") << "Calling associateRecoToSim method" << "\n";
00186         recSimColl=associator[ww]->associateRecoToSim(trackCollection,
00187                                                       TPCollectionHfake,
00188                                                       &event);
00189         LogTrace("TrackValidator") << "Calling associateSimToReco method" << "\n";
00190         simRecColl=associator[ww]->associateSimToReco(trackCollection,
00191                                                       TPCollectionHeff, 
00192                                                       &event);
00193       }
00194       else{
00195         edm::LogVerbatim("TrackValidator") << "Analyzing " 
00196                                            << label[www].process()<<":"
00197                                            << label[www].label()<<":"
00198                                            << label[www].instance()<<" with "
00199                                            << associatormap.process()<<":"
00200                                            << associatormap.label()<<":"
00201                                            << associatormap.instance()<<"\n";
00202         
00203         Handle<reco::SimToRecoCollection > simtorecoCollectionH;
00204         event.getByLabel(associatormap,simtorecoCollectionH);
00205         simRecColl= *(simtorecoCollectionH.product()); 
00206         
00207         Handle<reco::RecoToSimCollection > recotosimCollectionH;
00208         event.getByLabel(associatormap,recotosimCollectionH);
00209         recSimColl= *(recotosimCollectionH.product()); 
00210       }
00211 
00212 
00213 
00214       // ########################################################
00215       // fill simulation histograms (LOOP OVER TRACKINGPARTICLES)
00216       // ########################################################
00217 
00218       //compute number of tracks per eta interval
00219       //
00220       edm::LogVerbatim("TrackValidator") << "\n# of TrackingParticles: " << tPCeff.size() << "\n";
00221       int ats(0);  //This counter counts the number of simTracks that are "associated" to recoTracks
00222       int st(0);   //This counter counts the number of simulated tracks passing the MTV selection (i.e. tpSelector(tp) )
00223       for (TrackingParticleCollection::size_type i=0; i<tPCeff.size(); i++){ //loop over TPs collection for tracking efficiency
00224         TrackingParticleRef tpr(TPCollectionHeff, i);
00225         TrackingParticle* tp=const_cast<TrackingParticle*>(tpr.get());
00226         ParticleBase::Vector momentumTP; 
00227         ParticleBase::Point vertexTP;
00228         double dxySim(0);
00229         double dzSim(0); 
00230 
00231         
00232         //---------- THIS PART HAS TO BE CLEANED UP. THE PARAMETER DEFINER WAS NOT MEANT TO BE USED IN THIS WAY ----------
00233         //If the TrackingParticle is collison like, get the momentum and vertex at production state
00234         if(parametersDefiner=="LhcParametersDefinerForTP")
00235           {
00236             if(! tpSelector(*tp)) continue;
00237             momentumTP = tp->momentum();
00238             vertexTP = tp->vertex();
00239             //Calcualte the impact parameters w.r.t. PCA
00240             ParticleBase::Vector momentum = parametersDefinerTP->momentum(event,setup,*tp);
00241             ParticleBase::Point vertex = parametersDefinerTP->vertex(event,setup,*tp);
00242             dxySim = (-vertex.x()*sin(momentum.phi())+vertex.y()*cos(momentum.phi()));
00243             dzSim = vertex.z() - (vertex.x()*momentum.x()+vertex.y()*momentum.y())/sqrt(momentum.perp2()) 
00244               * momentum.z()/sqrt(momentum.perp2());
00245           }
00246         //If the TrackingParticle is comics, get the momentum and vertex at PCA
00247         if(parametersDefiner=="CosmicParametersDefinerForTP")
00248           {
00249             if(! cosmictpSelector(*tp,&bs,event,setup)) continue;       
00250             momentumTP = parametersDefinerTP->momentum(event,setup,*tp);
00251             vertexTP = parametersDefinerTP->vertex(event,setup,*tp);
00252             dxySim = (-vertexTP.x()*sin(momentumTP.phi())+vertexTP.y()*cos(momentumTP.phi()));
00253             dzSim = vertexTP.z() - (vertexTP.x()*momentumTP.x()+vertexTP.y()*momentumTP.y())/sqrt(momentumTP.perp2()) 
00254               * momentumTP.z()/sqrt(momentumTP.perp2());
00255           }
00256         //---------- THE PART ABOVE HAS TO BE CLEANED UP. THE PARAMETER DEFINER WAS NOT MEANT TO BE USED IN THIS WAY ----------
00257 
00258         st++;   //This counter counts the number of simulated tracks passing the MTV selection (i.e. tpSelector(tp) )
00259 
00260         // in the coming lines, histos are filled using as input
00261         // - momentumTP 
00262         // - vertexTP 
00263         // - dxySim
00264         // - dzSim
00265 
00266         histoProducerAlgo_->fill_generic_simTrack_histos(w,momentumTP,vertexTP);
00267 
00268 
00269         // ##############################################
00270         // fill RecoAssociated SimTracks' histograms
00271         // ##############################################
00272         bool isRecoMatched(false);
00273         const reco::Track* matchedTrackPointer=0;
00274         std::vector<std::pair<RefToBase<Track>, double> > rt;
00275         if(simRecColl.find(tpr) != simRecColl.end()){
00276           rt = (std::vector<std::pair<RefToBase<Track>, double> >) simRecColl[tpr];
00277           if (rt.size()!=0) {
00278             ats++; //This counter counts the number of simTracks that have a recoTrack associated
00279             isRecoMatched = true; matchedTrackPointer = rt.begin()->first.get();
00280             edm::LogVerbatim("TrackValidator") << "TrackingParticle #" << st 
00281                                                << " with pt=" << sqrt(momentumTP.perp2()) 
00282                                                << " associated with quality:" << rt.begin()->second <<"\n";
00283           }
00284         }else{
00285           edm::LogVerbatim("TrackValidator") 
00286             << "TrackingParticle #" << st
00287             << " with pt,eta,phi: " 
00288             << sqrt(momentumTP.perp2()) << " , "
00289             << momentumTP.eta() << " , "
00290             << momentumTP.phi() << " , "
00291             << " NOT associated to any reco::Track" << "\n";
00292         }
00293         
00294 
00295         
00296 
00297         std::vector<PSimHit> simhits=tp->trackPSimHit(DetId::Tracker);
00298         int nSimHits = simhits.end()-simhits.begin();
00299 
00300         histoProducerAlgo_->fill_recoAssociated_simTrack_histos(w,*tp,momentumTP,vertexTP,dxySim,dzSim,nSimHits,matchedTrackPointer);
00301 
00302 
00303 
00304 
00305       } // End  for (TrackingParticleCollection::size_type i=0; i<tPCeff.size(); i++){
00306 
00307       //if (st!=0) h_tracksSIM[w]->Fill(st);  // TO BE FIXED
00308       
00309       
00310       // ##############################################
00311       // fill recoTracks histograms (LOOP OVER TRACKS)
00312       // ##############################################
00313       edm::LogVerbatim("TrackValidator") << "\n# of reco::Tracks with "
00314                                          << label[www].process()<<":"
00315                                          << label[www].label()<<":"
00316                                          << label[www].instance()
00317                                          << ": " << trackCollection->size() << "\n";
00318 
00319       int at(0); //This counter counts the number of recoTracks that are associated to SimTracks
00320       int rT(0); //This counter counts the number of recoTracks in general
00321 
00322 
00323       // dE/dx
00324       // at some point this could be generalized, with a vector of tags and a corresponding vector of Handles
00325       // I'm writing the interface such to take vectors of ValueMaps
00326       edm::Handle<edm::ValueMap<reco::DeDxData> > dEdx1Handle;
00327       edm::Handle<edm::ValueMap<reco::DeDxData> > dEdx2Handle;
00328       std::vector<edm::ValueMap<reco::DeDxData> > v_dEdx;
00329       v_dEdx.clear();
00330       //std::cout << "PIPPO: label is " << label[www] << std::endl;
00331       if (label[www].label()=="generalTracks") {
00332         try {
00333           event.getByLabel(m_dEdx1Tag, dEdx1Handle);
00334           const edm::ValueMap<reco::DeDxData> dEdx1 = *dEdx1Handle.product();
00335           event.getByLabel(m_dEdx2Tag, dEdx2Handle);
00336           const edm::ValueMap<reco::DeDxData> dEdx2 = *dEdx2Handle.product();
00337           v_dEdx.push_back(dEdx1);
00338           v_dEdx.push_back(dEdx2);
00339         } catch (cms::Exception e){
00340           LogTrace("TrackValidator") << "exception found: " << e.what() << "\n";
00341         }
00342       }
00343       //end dE/dx
00344 
00345       for(View<Track>::size_type i=0; i<trackCollection->size(); ++i){
00346         RefToBase<Track> track(trackCollection, i);
00347         rT++;
00348         
00349         bool isSimMatched(false);
00350         std::vector<std::pair<TrackingParticleRef, double> > tp;
00351         if(recSimColl.find(track) != recSimColl.end()){
00352           tp = recSimColl[track];
00353           if (tp.size()!=0) {
00354             isSimMatched = true;
00355             at++;
00356             edm::LogVerbatim("TrackValidator") << "reco::Track #" << rT << " with pt=" << track->pt() 
00357                                                << " associated with quality:" << tp.begin()->second <<"\n";
00358           }
00359         } else {
00360           edm::LogVerbatim("TrackValidator") << "reco::Track #" << rT << " with pt=" << track->pt()
00361                                              << " NOT associated to any TrackingParticle" << "\n";                
00362         }
00363         
00364 
00365         histoProducerAlgo_->fill_generic_recoTrack_histos(w,*track,bs.position(),isSimMatched);
00366 
00367         // dE/dx
00368         //      reco::TrackRef track2  = reco::TrackRef( trackCollection, i );
00369         if (v_dEdx.size() > 0) histoProducerAlgo_->fill_dedx_recoTrack_histos(w,track, v_dEdx);
00370         //if (v_dEdx.size() > 0) histoProducerAlgo_->fill_dedx_recoTrack_histos(track2, v_dEdx);
00371 
00372 
00373         //Fill other histos
00374         //try{ //Is this really necessary ????
00375 
00376         if (tp.size()==0) continue;     
00377 
00378         histoProducerAlgo_->fill_simAssociated_recoTrack_histos(w,*track);
00379 
00380         TrackingParticleRef tpr = tp.begin()->first;
00381         
00382         /* TO BE FIXED LATER
00383         if (associators[ww]=="TrackAssociatorByChi2"){
00384           //association chi2
00385           double assocChi2 = -tp.begin()->second;//in association map is stored -chi2
00386           h_assochi2[www]->Fill(assocChi2);
00387           h_assochi2_prob[www]->Fill(TMath::Prob((assocChi2)*5,5));
00388         }
00389         else if (associators[ww]=="TrackAssociatorByHits"){
00390           double fraction = tp.begin()->second;
00391           h_assocFraction[www]->Fill(fraction);
00392           h_assocSharedHit[www]->Fill(fraction*track->numberOfValidHits());
00393         }
00394         */
00395 
00396           
00397         //Get tracking particle parameters at point of closest approach to the beamline
00398         ParticleBase::Vector momentumTP = parametersDefinerTP->momentum(event,setup,*(tpr.get()));
00399         ParticleBase::Point vertexTP = parametersDefinerTP->vertex(event,setup,*(tpr.get()));                    
00400         int chargeTP = tpr->charge();
00401 
00402         histoProducerAlgo_->fill_ResoAndPull_recoTrack_histos(w,momentumTP,vertexTP,chargeTP,
00403                                                              *track,bs.position());
00404         
00405         
00406         //TO BE FIXED
00407         //std::vector<PSimHit> simhits=tpr.get()->trackPSimHit(DetId::Tracker);
00408         //nrecHit_vs_nsimHit_rec2sim[w]->Fill(track->numberOfValidHits(), (int)(simhits.end()-simhits.begin() ));
00409         
00410         /*
00411           } // End of try{
00412           catch (cms::Exception e){
00413           LogTrace("TrackValidator") << "exception found: " << e.what() << "\n";
00414           }
00415         */
00416         
00417       } // End of for(View<Track>::size_type i=0; i<trackCollection->size(); ++i){
00418 
00419       //TO BE FIXED
00420       //if (at!=0) h_tracks[w]->Fill(at);
00421       //h_fakes[w]->Fill(rT-at);
00422       //nrec_vs_nsim[w]->Fill(rT,st);
00423 
00424       edm::LogVerbatim("TrackValidator") << "Total Simulated: " << st << "\n"
00425                                          << "Total Associated (simToReco): " << ats << "\n"
00426                                          << "Total Reconstructed: " << rT << "\n"
00427                                          << "Total Associated (recoToSim): " << at << "\n"
00428                                          << "Total Fakes: " << rT-at << "\n";
00429 
00430       w++;
00431     } // End of  for (unsigned int www=0;www<label.size();www++){
00432   } //END of for (unsigned int ww=0;ww<associators.size();ww++){
00433 }
00434 
00435 void MultiTrackValidator::endRun(Run const&, EventSetup const&) {
00436   int w=0;
00437   for (unsigned int ww=0;ww<associators.size();ww++){
00438     for (unsigned int www=0;www<label.size();www++){
00439       if(!skipHistoFit && runStandalone) histoProducerAlgo_->finalHistoFits(w);
00440       if (runStandalone) histoProducerAlgo_->fillProfileHistosFromVectors(w);
00441       histoProducerAlgo_->fillHistosFromVectors(w);
00442       w++;
00443     }    
00444   }
00445   if ( out.size() != 0 && dbe_ ) dbe_->save(out);
00446 }
00447 
00448 
00449