CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/CommonTools/RecoAlgos/interface/TrackFullCloneSelectorBase.h

Go to the documentation of this file.
00001 #ifndef RecoAlgos_TrackFullCloneSelectorBase_h
00002 #define RecoAlgos_TrackFullCloneSelectorBase_h
00003 
00015 #include <utility>
00016 #include <vector>
00017 #include <memory>
00018 #include <algorithm>
00019 #include <map>
00020 #include "FWCore/Framework/interface/EDProducer.h"
00021 #include "FWCore/Framework/interface/Event.h"
00022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00023 #include "FWCore/Utilities/interface/InputTag.h"
00024 
00025 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00026 #include "DataFormats/TrackReco/interface/Track.h"
00027 #include "DataFormats/TrackReco/interface/TrackExtra.h"
00028 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00029 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
00030 
00031 
00032 namespace reco { namespace modules {
00033 
00034 template<typename Selector>
00035 class TrackFullCloneSelectorBase : public edm::EDProducer {
00036 public:
00038   explicit TrackFullCloneSelectorBase( const edm::ParameterSet & cfg ) :
00039     src_( cfg.template getParameter<edm::InputTag>( "src" ) ),
00040     copyExtras_(cfg.template getUntrackedParameter<bool>("copyExtras", false)),
00041     copyTrajectories_(cfg.template getUntrackedParameter<bool>("copyTrajectories", false)),
00042     selector_( cfg ) {
00043       std::string alias( cfg.getParameter<std::string>( "@module_label" ) );
00044       produces<reco::TrackCollection>().setBranchAlias( alias + "Tracks" );
00045       if (copyExtras_) {
00046           produces<reco::TrackExtraCollection>().setBranchAlias( alias + "TrackExtras" );
00047           produces<TrackingRecHitCollection>().setBranchAlias( alias + "RecHits" );
00048           if (copyTrajectories_) {
00049               produces< std::vector<Trajectory> >().setBranchAlias( alias + "Trajectories" );
00050               produces< TrajTrackAssociationCollection >().setBranchAlias( alias + "TrajectoryTrackAssociations" );
00051           }
00052       }
00053    }
00055   virtual ~TrackFullCloneSelectorBase() { }
00056   
00057 private:
00059   void produce( edm::Event& evt, const edm::EventSetup& es ) {
00060       edm::Handle<reco::TrackCollection> hSrcTrack;
00061       edm::Handle< std::vector<Trajectory> > hTraj;
00062       edm::Handle< TrajTrackAssociationCollection > hTTAss;
00063       evt.getByLabel( src_, hSrcTrack );
00064 
00065       selTracks_ = std::auto_ptr<reco::TrackCollection>(new reco::TrackCollection());
00066       if (copyExtras_) {
00067           selTrackExtras_ = std::auto_ptr<reco::TrackExtraCollection>(new reco::TrackExtraCollection());
00068           selHits_ = std::auto_ptr<TrackingRecHitCollection>(new TrackingRecHitCollection());
00069       }
00070 
00071       TrackRefProd rTracks = evt.template getRefBeforePut<TrackCollection>();      
00072 
00073       TrackingRecHitRefProd rHits;
00074       TrackExtraRefProd rTrackExtras;
00075       if (copyExtras_) {
00076           rHits = evt.template getRefBeforePut<TrackingRecHitCollection>();
00077           rTrackExtras = evt.template getRefBeforePut<TrackExtraCollection>();
00078       }
00079 
00080       typedef reco::TrackRef::key_type TrackRefKey;
00081       std::map<TrackRefKey, reco::TrackRef  > goodTracks;
00082       TrackRefKey current = 0;
00083 
00084       for (reco::TrackCollection::const_iterator it = hSrcTrack->begin(), ed = hSrcTrack->end(); it != ed; ++it, ++current) {
00085           const reco::Track & trk = * it;
00086           if (!selector_(trk, evt)) continue;
00087 
00088           selTracks_->push_back( Track( trk ) ); // clone and store
00089           if (!copyExtras_) continue;
00090 
00091           // TrackExtras
00092           selTrackExtras_->push_back( TrackExtra( trk.outerPosition(), trk.outerMomentum(), trk.outerOk(),
00093                       trk.innerPosition(), trk.innerMomentum(), trk.innerOk(),
00094                       trk.outerStateCovariance(), trk.outerDetId(),
00095                       trk.innerStateCovariance(), trk.innerDetId(),
00096                       trk.seedDirection() ) );
00097           selTracks_->back().setExtra( TrackExtraRef( rTrackExtras, selTrackExtras_->size() - 1) );
00098           TrackExtra & tx = selTrackExtras_->back();
00099           // TrackingRecHits
00100           for( trackingRecHit_iterator hit = trk.recHitsBegin(); hit != trk.recHitsEnd(); ++ hit ) {
00101               selHits_->push_back( (*hit)->clone() );
00102               tx.add( TrackingRecHitRef( rHits, selHits_->size() - 1) );
00103           }
00104           if (copyTrajectories_) {
00105               goodTracks[current] = reco::TrackRef(rTracks, selTracks_->size() - 1);
00106           }
00107       }
00108       if ( copyTrajectories_ ) {
00109           edm::Handle< std::vector<Trajectory> > hTraj;
00110           edm::Handle< TrajTrackAssociationCollection > hTTAss;
00111           evt.getByLabel(src_, hTTAss);
00112           evt.getByLabel(src_, hTraj);
00113           edm::RefProd< std::vector<Trajectory> > TrajRefProd = evt.template getRefBeforePut< std::vector<Trajectory> >();
00114           selTrajs_ = std::auto_ptr< std::vector<Trajectory> >(new std::vector<Trajectory>()); 
00115           selTTAss_ = std::auto_ptr< TrajTrackAssociationCollection >(new TrajTrackAssociationCollection());
00116           for (size_t i = 0, n = hTraj->size(); i < n; ++i) {
00117               edm::Ref< std::vector<Trajectory> > trajRef(hTraj, i);
00118               TrajTrackAssociationCollection::const_iterator match = hTTAss->find(trajRef);
00119               if (match != hTTAss->end()) {
00120                   const edm::Ref<reco::TrackCollection> &trkRef = match->val; 
00121                   TrackRefKey oldKey = trkRef.key();
00122                   std::map<TrackRefKey, reco::TrackRef>::iterator getref = goodTracks.find(oldKey);        
00123                   if (getref != goodTracks.end()) {
00124                       // do the clone
00125                       selTrajs_->push_back( Trajectory(*trajRef) );
00126                       selTTAss_->insert ( edm::Ref< std::vector<Trajectory> >(TrajRefProd, selTrajs_->size() - 1),
00127                                           getref->second );
00128                   }
00129               }
00130           }
00131       }
00132       
00133       evt.put(selTracks_);
00134       if (copyExtras_) {
00135             evt.put(selTrackExtras_); 
00136             evt.put(selHits_);
00137             if ( copyTrajectories_ ) {
00138                 evt.put(selTrajs_);
00139                 evt.put(selTTAss_);
00140             }
00141       }
00142   }
00144   edm::InputTag src_;
00146   bool copyExtras_;
00148   bool copyTrajectories_;
00150   Selector selector_;
00151   // some space
00152   std::auto_ptr<reco::TrackCollection> selTracks_;
00153   std::auto_ptr<reco::TrackExtraCollection> selTrackExtras_;
00154   std::auto_ptr<TrackingRecHitCollection> selHits_;
00155   std::auto_ptr< std::vector<Trajectory> > selTrajs_;
00156   std::auto_ptr< TrajTrackAssociationCollection > selTTAss_;
00157 };
00158 
00159 } }
00160 #endif