00001 #ifndef RecoSelectors_CosmicTrackingParticleSelector_h
00002 #define RecoSelectors_CosmicTrackingParticleSelector_h
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
00012 #include "FWCore/Utilities/interface/InputTag.h"
00013 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
00014 #include "FWCore/Framework/interface/Event.h"
00015 #include "FWCore/Framework/interface/ESHandle.h"
00016 #include "TrackingTools/TrajectoryState/interface/FreeTrajectoryState.h"
00017 #include "TrackingTools/PatternTools/interface/TSCBLBuilderNoMaterial.h"
00018 #include "TrackingTools/PatternTools/interface/TSCPBuilderNoMaterial.h"
00019 #include "MagneticField/Engine/interface/MagneticField.h"
00020 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00021
00022 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00023 #include <Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h>
00024 #include <DataFormats/GeometrySurface/interface/Surface.h>
00025 #include <DataFormats/GeometrySurface/interface/GloballyPositioned.h>
00026 #include <Geometry/CommonDetUnit/interface/GeomDet.h>
00027 #include "DataFormats/GeometryVector/interface/GlobalVector.h"
00028 #include "DataFormats/GeometryVector/interface/GlobalPoint.h"
00029
00030 #include "SimGeneral/TrackingAnalysis/interface/SimHitTPAssociationProducer.h"
00031 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00032
00033 class TrajectoryStateClosestToBeamLineBuilder;
00034
00035 class CosmicTrackingParticleSelector {
00036
00037 public:
00038 typedef TrackingParticleCollection collection;
00039 typedef std::vector<const TrackingParticle *> container;
00040 typedef container::const_iterator const_iterator;
00041
00042 CosmicTrackingParticleSelector(){}
00043
00044 CosmicTrackingParticleSelector ( double ptMin,double minRapidity,double maxRapidity,
00045 double tip,double lip,int minHit, bool chargedOnly,
00046 std::vector<int> pdgId = std::vector<int>()) :
00047 ptMin_( ptMin ), minRapidity_( minRapidity ), maxRapidity_( maxRapidity ),
00048 tip_( tip ), lip_( lip ), minHit_( minHit ), chargedOnly_(chargedOnly), pdgId_( pdgId ) { }
00049
00050
00051 CosmicTrackingParticleSelector ( const edm::ParameterSet & cfg ) :
00052 ptMin_(cfg.getParameter<double>("ptMin")),
00053 minRapidity_(cfg.getParameter<double>("minRapidity")),
00054 maxRapidity_(cfg.getParameter<double>("maxRapidity")),
00055 tip_(cfg.getParameter<double>("tip")),
00056 lip_(cfg.getParameter<double>("lip")),
00057 minHit_(cfg.getParameter<int>("minHit")),
00058 chargedOnly_(cfg.getParameter<bool>("chargedOnly")),
00059 pdgId_(cfg.getParameter<std::vector<int> >("pdgId")) { }
00060
00061
00062 void select( const edm::Handle<collection>& c, const edm::Event & event, const edm::EventSetup& setup) {
00063 selected_.clear();
00064 edm::Handle<reco::BeamSpot> beamSpot;
00065 event.getByLabel(edm::InputTag("offlineBeamSpot"), beamSpot);
00066 for( TrackingParticleCollection::const_iterator itp = c->begin();
00067 itp != c->end(); ++ itp )
00068 if ( operator()(TrackingParticleRef(c,itp-c->begin()),beamSpot.product(),event,setup) ) {
00069 selected_.push_back( & * itp );
00070 }
00071 }
00072
00073 const_iterator begin() const { return selected_.begin(); }
00074 const_iterator end() const { return selected_.end(); }
00075
00076 void initEvent(edm::Handle<SimHitTPAssociationProducer::SimHitTPAssociationList> simHitsTPAssocToSet) const {
00077 simHitsTPAssoc = simHitsTPAssocToSet;
00078 }
00079
00080
00081 bool operator()( const TrackingParticleRef tpr, const reco::BeamSpot* bs, const edm::Event &iEvent, const edm::EventSetup& iSetup ) const {
00082 if (chargedOnly_ && tpr->charge()==0) return false;
00083
00084
00085
00086
00087
00088
00089
00090 edm::ESHandle<TrackerGeometry> tracker;
00091 iSetup.get<TrackerDigiGeometryRecord>().get(tracker);
00092
00093 edm::ESHandle<MagneticField> theMF;
00094 iSetup.get<IdealMagneticFieldRecord>().get(theMF);
00095
00096 GlobalVector finalGV (0,0,0);
00097 GlobalPoint finalGP(0,0,0);
00098 GlobalVector momentum(0,0,0);
00099 GlobalPoint vertex(0,0,0);
00100 double radius(9999);
00101 bool found(0);
00102
00103
00104 if (simHitsTPAssoc.isValid()==0) {
00105 edm::LogError("TrackAssociation") << "Invalid handle!";
00106 return false;
00107 }
00108 std::pair<TrackingParticleRef, TrackPSimHitRef> clusterTPpairWithDummyTP(tpr,TrackPSimHitRef());
00109
00110 auto range = std::equal_range(simHitsTPAssoc->begin(), simHitsTPAssoc->end(),
00111 clusterTPpairWithDummyTP, SimHitTPAssociationProducer::simHitTPAssociationListGreater);
00112 for(auto ip = range.first; ip != range.second; ++ip) {
00113 TrackPSimHitRef it = ip->second;
00114 const GeomDet* tmpDet = tracker->idToDet( DetId(it->detUnitId()) ) ;
00115 LocalVector lv = it->momentumAtEntry();
00116 Local3DPoint lp = it->localPosition ();
00117 GlobalVector gv = tmpDet->surface().toGlobal( lv );
00118 GlobalPoint gp = tmpDet->surface().toGlobal( lp );
00119 if(gp.perp()<radius){
00120 found=true;
00121 radius = gp.perp();
00122 finalGV = gv;
00123 finalGP = gp;
00124 }
00125 }
00126 if(!found) return 0;
00127 else
00128 {
00129 FreeTrajectoryState ftsAtProduction(finalGP,finalGV,TrackCharge(tpr->charge()),theMF.product());
00130 TSCBLBuilderNoMaterial tscblBuilder;
00131 TrajectoryStateClosestToBeamLine tsAtClosestApproach = tscblBuilder(ftsAtProduction,*bs);
00132 if(!tsAtClosestApproach.isValid()){
00133 std::cout << "WARNING: tsAtClosestApproach is not valid" << std::endl;
00134 return 0;
00135 }
00136 else
00137 {
00138 momentum = tsAtClosestApproach.trackStateAtPCA().momentum();
00139 vertex = tsAtClosestApproach.trackStateAtPCA().position();
00140 return (
00141 tpr->numberOfTrackerLayers() >= minHit_ &&
00142 sqrt(momentum.perp2()) >= ptMin_ &&
00143 momentum.eta() >= minRapidity_ && momentum.eta() <= maxRapidity_ &&
00144 sqrt(vertex.perp2()) <= tip_ &&
00145 fabs(vertex.z()) <= lip_
00146 );
00147 }
00148 }
00149 }
00150
00151 size_t size() const { return selected_.size(); }
00152
00153 private:
00154
00155 double ptMin_;
00156 double minRapidity_;
00157 double maxRapidity_;
00158 double tip_;
00159 double lip_;
00160 int minHit_;
00161 bool chargedOnly_;
00162 std::vector<int> pdgId_;
00163 container selected_;
00164
00165 mutable edm::Handle<SimHitTPAssociationProducer::SimHitTPAssociationList> simHitsTPAssoc;
00166
00167 };
00168
00169 #endif