00001 #include <memory>
00002 #include "RecoParticleFlow/PFTracking/interface/PFTrackProducer.h"
00003 #include "RecoParticleFlow/PFTracking/interface/PFTrackTransformer.h"
00004 #include "DataFormats/ParticleFlowReco/interface/PFRecTrack.h"
00005 #include "DataFormats/ParticleFlowReco/interface/PFRecTrackFwd.h"
00006 #include "DataFormats/TrackReco/interface/Track.h"
00007 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00008 #include "DataFormats/GsfTrackReco/interface/GsfTrack.h"
00009 #include "DataFormats/GsfTrackReco/interface/GsfTrackFwd.h"
00010 #include "DataFormats/EgammaReco/interface/ElectronSeed.h"
00011 #include "DataFormats/EgammaReco/interface/ElectronSeedFwd.h"
00012 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00013 #include "FWCore/Framework/interface/ESHandle.h"
00014 #include "MagneticField/Engine/interface/MagneticField.h"
00015 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00016 #include "DataFormats/MuonReco/interface/MuonFwd.h"
00017 #include "DataFormats/MuonReco/interface/Muon.h"
00018 #include "TrackingTools/TransientTrack/interface/TransientTrackBuilder.h"
00019 #include "TrackingTools/Records/interface/TransientTrackRecord.h"
00020 #include "TrackingTools/IPTools/interface/IPTools.h"
00021 #include "DataFormats/VertexReco/interface/VertexFwd.h"
00022 #include "DataFormats/VertexReco/interface/Vertex.h"
00023
00024 using namespace std;
00025 using namespace edm;
00026 using namespace reco;
00027 PFTrackProducer::PFTrackProducer(const ParameterSet& iConfig):
00028 pfTransformer_(0)
00029 {
00030 produces<reco::PFRecTrackCollection>();
00031
00032 tracksContainers_ =
00033 iConfig.getParameter< vector < InputTag > >("TkColList");
00034
00035 useQuality_ = iConfig.getParameter<bool>("UseQuality");
00036
00037 gsfTrackLabel_ = iConfig.getParameter<InputTag>
00038 ("GsfTrackModuleLabel");
00039
00040 trackQuality_=reco::TrackBase::qualityByName(iConfig.getParameter<std::string>("TrackQuality"));
00041
00042 muonColl_ = iConfig.getParameter< InputTag >("MuColl");
00043
00044 trajinev_ = iConfig.getParameter<bool>("TrajInEvents");
00045
00046 gsfinev_ = iConfig.getParameter<bool>("GsfTracksInEvents");
00047 vtx_h=iConfig.getParameter<edm::InputTag>("PrimaryVertexLabel");
00048 }
00049
00050 PFTrackProducer::~PFTrackProducer()
00051 {
00052 delete pfTransformer_;
00053 }
00054
00055 void
00056 PFTrackProducer::produce(Event& iEvent, const EventSetup& iSetup)
00057 {
00058
00059
00060 auto_ptr< reco::PFRecTrackCollection >
00061 PfTrColl (new reco::PFRecTrackCollection);
00062
00063
00064 Handle<GsfTrackCollection> gsftrackcoll;
00065 bool foundgsf = iEvent.getByLabel(gsfTrackLabel_,gsftrackcoll);
00066 GsfTrackCollection gsftracks;
00067
00068 Handle<reco::VertexCollection> vertex;
00069 iEvent.getByLabel(vtx_h, vertex);
00070 reco::Vertex dummy;
00071 const reco::Vertex* pv=&dummy;
00072 if (vertex.isValid())
00073 {
00074 pv = &*vertex->begin();
00075 }
00076 else
00077 {
00078 reco::Vertex::Error e;
00079 e(0, 0) = 0.0015 * 0.0015;
00080 e(1, 1) = 0.0015 * 0.0015;
00081 e(2, 2) = 15. * 15.;
00082 reco::Vertex::Point p(0, 0, 0);
00083 dummy = reco::Vertex(p, e, 0, 0, 0);
00084 }
00085
00086 edm::ESHandle<TransientTrackBuilder> builder;
00087 iSetup.get<TransientTrackRecord>().get("TransientTrackBuilder", builder);
00088 TransientTrackBuilder thebuilder = *(builder.product());
00089
00090
00091 if(gsfinev_) {
00092 if(!foundgsf )
00093 LogError("PFTrackProducer")
00094 <<" cannot get GsfTracks (probably in HI events): "
00095 << " please set GsfTracksInEvents = False in RecoParticleFlow/PFTracking/python/pfTrack_cfi.py" << endl;
00096 else
00097 gsftracks = *(gsftrackcoll.product());
00098 }
00099
00100
00101 Handle< reco::MuonCollection > recMuons;
00102 iEvent.getByLabel(muonColl_, recMuons);
00103
00104
00105 for (unsigned int istr=0; istr<tracksContainers_.size();istr++){
00106
00107
00108 Handle<reco::TrackCollection> tkRefCollection;
00109 iEvent.getByLabel(tracksContainers_[istr], tkRefCollection);
00110 reco::TrackCollection Tk=*(tkRefCollection.product());
00111
00112 vector<Trajectory> Tj(0);
00113 if(trajinev_) {
00114
00115 Handle<vector<Trajectory> > tjCollection;
00116 bool found = iEvent.getByLabel(tracksContainers_[istr], tjCollection);
00117 if(!found )
00118 LogError("PFTrackProducer")
00119 <<" cannot get Trajectories of: "
00120 << tracksContainers_[istr]
00121 << " please set TrajInEvents = False in RecoParticleFlow/PFTracking/python/pfTrack_cfi.py" << endl;
00122
00123 Tj =*(tjCollection.product());
00124 }
00125
00126
00127 for(unsigned int i=0;i<Tk.size();i++){
00128
00129 reco::TrackRef trackRef(tkRefCollection, i);
00130
00131 if (useQuality_ &&
00132 (!(Tk[i].quality(trackQuality_)))){
00133
00134 bool isMuCandidate = false;
00135
00136
00137
00138 if(recMuons.isValid() ) {
00139 for(unsigned j=0;j<recMuons->size(); j++) {
00140 reco::MuonRef muonref( recMuons, j );
00141 if (muonref->track().isNonnull())
00142 if( muonref->track() == trackRef && muonref->isGlobalMuon()){
00143 isMuCandidate=true;
00144
00145 break;
00146 }
00147 }
00148 }
00149 else{
00150 edm::LogError("MissingInput")<<"there is no valide:"<<muonColl_<<" to be used.";
00151 }
00152
00153 if(!isMuCandidate)
00154 {
00155 continue;
00156 }
00157
00158 }
00159
00160
00161 bool preId = false;
00162 if(foundgsf) {
00163 for (unsigned int igsf=0; igsf<gsftracks.size();igsf++) {
00164 GsfTrackRef gsfTrackRef(gsftrackcoll, igsf);
00165 if (gsfTrackRef->seedRef().isNull()) continue;
00166 ElectronSeedRef ElSeedRef= gsfTrackRef->extra()->seedRef().castTo<ElectronSeedRef>();
00167 if (ElSeedRef->ctfTrack().isNonnull()) {
00168 if(ElSeedRef->ctfTrack() == trackRef) preId = true;
00169 }
00170 }
00171 }
00172 if(preId) {
00173
00174 reco::PFRecTrack pftrack( trackRef->charge(),
00175 reco::PFRecTrack::KF_ELCAND,
00176 i, trackRef );
00177
00178 bool valid = false;
00179 if(trajinev_) {
00180 valid = pfTransformer_->addPoints( pftrack, *trackRef, Tj[i]);
00181 }
00182 else {
00183 Trajectory FakeTraj;
00184 valid = pfTransformer_->addPoints( pftrack, *trackRef, FakeTraj);
00185 }
00186 if(valid) {
00187
00188 double stip=-999;
00189 const reco::PFTrajectoryPoint& atECAL=pftrack.extrapolatedPoint(reco::PFTrajectoryPoint::ECALEntrance);
00190 if(atECAL.isValid())
00191 {
00192 GlobalVector direction(pftrack.extrapolatedPoint(reco::PFTrajectoryPoint::ECALEntrance).position().x(),
00193 pftrack.extrapolatedPoint(reco::PFTrajectoryPoint::ECALEntrance).position().y(),
00194 pftrack.extrapolatedPoint(reco::PFTrajectoryPoint::ECALEntrance).position().z());
00195 stip = IPTools::signedTransverseImpactParameter(thebuilder.build(*trackRef), direction, *pv).second.significance();
00196 }
00197 pftrack.setSTIP(stip);
00198 PfTrColl->push_back(pftrack);
00199 }
00200 }
00201 else {
00202 reco::PFRecTrack pftrack( trackRef->charge(),
00203 reco::PFRecTrack::KF,
00204 i, trackRef );
00205 bool valid = false;
00206 if(trajinev_) {
00207 valid = pfTransformer_->addPoints( pftrack, *trackRef, Tj[i]);
00208 }
00209 else {
00210 Trajectory FakeTraj;
00211 valid = pfTransformer_->addPoints( pftrack, *trackRef, FakeTraj);
00212 }
00213
00214 if(valid) {
00215 double stip=-999;
00216 const reco::PFTrajectoryPoint& atECAL=pftrack.extrapolatedPoint(reco::PFTrajectoryPoint::ECALEntrance);
00217 if(atECAL.isValid())
00218 {
00219 GlobalVector direction(pftrack.extrapolatedPoint(reco::PFTrajectoryPoint::ECALEntrance).position().x(),
00220 pftrack.extrapolatedPoint(reco::PFTrajectoryPoint::ECALEntrance).position().y(),
00221 pftrack.extrapolatedPoint(reco::PFTrajectoryPoint::ECALEntrance).position().z());
00222 stip = IPTools::signedTransverseImpactParameter(thebuilder.build(*trackRef), direction, *pv).second.significance();
00223 }
00224 pftrack.setSTIP(stip);
00225 PfTrColl->push_back(pftrack);
00226 }
00227 }
00228 }
00229 }
00230 iEvent.put(PfTrColl);
00231 }
00232
00233
00234 void
00235 PFTrackProducer::beginRun(const edm::Run& run,
00236 const EventSetup& iSetup)
00237 {
00238 ESHandle<MagneticField> magneticField;
00239 iSetup.get<IdealMagneticFieldRecord>().get(magneticField);
00240 pfTransformer_= new PFTrackTransformer(math::XYZVector(magneticField->inTesla(GlobalPoint(0,0,0))));
00241 if(!trajinev_)
00242 pfTransformer_->OnlyProp();
00243 }
00244
00245
00246 void
00247 PFTrackProducer::endRun(const edm::Run& run,
00248 const EventSetup& iSetup) {
00249 delete pfTransformer_;
00250 pfTransformer_=nullptr;
00251 }