00001 #include "RecoEgamma/EgammaPhotonProducers/interface/TrackProducerWithSCAssociation.h"
00002
00003 #include <memory>
00004
00005 #include "FWCore/Framework/interface/Frameworkfwd.h"
00006 #include "FWCore/Framework/interface/MakerMacros.h"
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 #include "TrackingTools/GeomPropagators/interface/Propagator.h"
00009 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00010 #include "TrackingTools/PatternTools/interface/TrajectoryFitter.h"
00011 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
00012 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
00013 #include "TrackingTools/TransientTrackingRecHit/interface/TransientTrackingRecHitBuilder.h"
00014 #include "RecoTracker/TrackProducer/interface/TrackProducerBase.h"
00015 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00016 #include "DataFormats/EgammaTrackReco/interface/TrackCandidateCaloClusterAssociation.h"
00017 #include "DataFormats/EgammaTrackReco/interface/TrackCaloClusterAssociation.h"
00018 #include "DataFormats/CaloRecHit/interface/CaloCluster.h"
00019
00020 TrackProducerWithSCAssociation::TrackProducerWithSCAssociation(const edm::ParameterSet& iConfig):
00021 TrackProducerBase<reco::Track>(iConfig.getParameter<bool>("TrajectoryInEvent")),
00022 theAlgo(iConfig)
00023 {
00024 setConf(iConfig);
00025 setSrc( iConfig.getParameter<edm::InputTag>( "src" ),
00026 iConfig.getParameter<edm::InputTag>( "beamSpot" ));
00027 setAlias( iConfig.getParameter<std::string>( "@module_label" ) );
00028
00029 if ( iConfig.exists("clusterRemovalInfo") ) {
00030 edm::InputTag tag = iConfig.getParameter<edm::InputTag>("clusterRemovalInfo");
00031 if (!(tag == edm::InputTag())) { setClusterRemovalInfo( tag ); }
00032 }
00033
00034
00035 myname_ = iConfig.getParameter<std::string>("ComponentName");
00036 conversionTrackCandidateProducer_ = iConfig.getParameter<std::string>("producer");
00037 trackCSuperClusterAssociationCollection_ = iConfig.getParameter<std::string>("trackCandidateSCAssociationCollection");
00038 trackSuperClusterAssociationCollection_ = iConfig.getParameter<std::string>("recoTrackSCAssociationCollection");
00039 myTrajectoryInEvent_ = iConfig.getParameter<bool>("TrajectoryInEvent");
00040
00041
00042
00043 produces<reco::TrackCollection>().setBranchAlias( alias_ + "Tracks" );
00044 produces<reco::TrackExtraCollection>().setBranchAlias( alias_ + "TrackExtras" );
00045 produces<TrackingRecHitCollection>().setBranchAlias( alias_ + "RecHits" );
00046 produces<std::vector<Trajectory> >() ;
00047 produces<TrajTrackAssociationCollection>();
00048
00049 produces< reco::TrackCaloClusterPtrAssociation > (trackSuperClusterAssociationCollection_ );
00050
00051 }
00052
00053
00054 void TrackProducerWithSCAssociation::produce(edm::Event& theEvent, const edm::EventSetup& setup)
00055 {
00056 edm::LogInfo("TrackProducerWithSCAssociation") << "Analyzing event number: " << theEvent.id() << "\n";
00057
00058
00059
00060
00061
00062
00063
00064
00065 std::auto_ptr<TrackingRecHitCollection> outputRHColl (new TrackingRecHitCollection);
00066 std::auto_ptr<reco::TrackCollection> outputTColl(new reco::TrackCollection);
00067 std::auto_ptr<reco::TrackExtraCollection> outputTEColl(new reco::TrackExtraCollection);
00068 std::auto_ptr<std::vector<Trajectory> > outputTrajectoryColl(new std::vector<Trajectory>);
00069
00070 std::auto_ptr<reco::TrackCaloClusterPtrAssociation> scTrkAssoc_p(new reco::TrackCaloClusterPtrAssociation);
00071
00072
00073
00074
00075 edm::ESHandle<TrackerGeometry> theG;
00076 edm::ESHandle<MagneticField> theMF;
00077 edm::ESHandle<TrajectoryFitter> theFitter;
00078 edm::ESHandle<Propagator> thePropagator;
00079 edm::ESHandle<TransientTrackingRecHitBuilder> theBuilder;
00080 getFromES(setup,theG,theMF,theFitter,thePropagator,theBuilder);
00081
00082
00083
00084
00085
00086 edm::Handle<TrackCandidateCollection> theTCCollection;
00088 validTrackCandidateSCAssociationInput_=true;
00089 edm::Handle<reco::TrackCandidateCaloClusterPtrAssociation> trkCandidateSCAssocHandle;
00090 theEvent.getByLabel(conversionTrackCandidateProducer_, trackCSuperClusterAssociationCollection_ , trkCandidateSCAssocHandle);
00091 if ( !trkCandidateSCAssocHandle.isValid() ) {
00092 std::cout << "Error! Can't get the product "<<trackCSuperClusterAssociationCollection_.c_str() << " but keep running. Empty collection will be produced " << "\n";
00093 edm::LogError("TrackProducerWithSCAssociation") << "Error! Can't get the product "<<trackCSuperClusterAssociationCollection_.c_str() << " but keep running. Empty collection will be produced " << "\n";
00094 validTrackCandidateSCAssociationInput_=false;
00095 }
00096 reco::TrackCandidateCaloClusterPtrAssociation scTrkCandAssCollection = *(trkCandidateSCAssocHandle.product());
00097 if ( scTrkCandAssCollection.size() ==0 ) validTrackCandidateSCAssociationInput_=false;
00098
00099
00100 std::vector<int> tccLocations;
00101 AlgoProductCollection algoResults;
00102 reco::BeamSpot bs;
00103
00104
00105 getFromEvt(theEvent,theTCCollection,bs);
00106
00107 if (theTCCollection.failedToGet()){
00108 edm::LogError("TrackProducerWithSCAssociation") <<"TrackProducerWithSCAssociation could not get the TrackCandidateCollection.";}
00109 else{
00110
00111
00112
00113 LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation run the algorithm" << "\n";
00114
00115
00116
00117
00118
00119
00120
00121 try{
00122 int cont = 0;
00123 int tcc=0;
00124
00125 for (TrackCandidateCollection::const_iterator i=theTCCollection->begin(); i!=theTCCollection->end();i++)
00126 {
00127
00128 const TrackCandidate * theTC = &(*i);
00129 PTrajectoryStateOnDet state = theTC->trajectoryStateOnDet();
00130 const TrackCandidate::range& recHitVec=theTC->recHits();
00131 const TrajectorySeed& seed = theTC->seed();
00132
00133
00134 TrajectoryStateTransform transformer;
00135
00136 DetId detId(state.detId());
00137 TrajectoryStateOnSurface theTSOS = transformer.transientState( state,
00138 &(theG.product()->idToDet(detId)->surface()),
00139 theMF.product());
00140
00141 LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation Initial TSOS\n" << theTSOS << "\n";
00142
00143
00144
00145 TransientTrackingRecHit::RecHitContainer hits;
00146
00147 float ndof=0;
00148
00149 for (edm::OwnVector<TrackingRecHit>::const_iterator i=recHitVec.first;
00150 i!=recHitVec.second; i++){
00151 hits.push_back(theBuilder.product()->build(&(*i) ));
00152 }
00153
00154
00155
00156 LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation going to buildTrack"<< "\n";
00157 bool ok = theAlgo.buildTrack(theFitter.product(),thePropagator.product(),algoResults, hits, theTSOS, seed, ndof, bs, theTC->seedRef());
00158 LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation buildTrack result: " << ok << "\n";
00159 if(ok) {
00160 cont++;
00161 tccLocations.push_back(tcc);
00162 }
00163 tcc++;
00164 }
00165 edm::LogInfo("TrackProducerWithSCAssociation") << "Number of Tracks found: " << cont << "\n";
00166 LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation Number of Tracks found: " << cont << "\n";
00167
00168
00169 } catch (cms::Exception &e){ edm::LogInfo("TrackProducerWithSCAssociation") << "cms::Exception caught!!!" << "\n" << e << "\n";}
00170
00171
00172
00173 putInEvt(theEvent, outputRHColl, outputTColl, outputTEColl, outputTrajectoryColl, algoResults);
00174
00175
00176 if ( validTrackCandidateSCAssociationInput_ ) {
00177 int itrack=0;
00178 std::vector<edm::Ptr<reco::CaloCluster> > caloPtrVec;
00179 for(AlgoProductCollection::iterator i=algoResults.begin(); i!=algoResults.end();i++){
00180 edm::Ref<TrackCandidateCollection> trackCRef(theTCCollection,tccLocations[itrack]);
00181 const edm::Ptr<reco::CaloCluster>& aClus = (*trkCandidateSCAssocHandle)[trackCRef];
00182 caloPtrVec.push_back( aClus );
00183 itrack++;
00184 }
00185
00186
00187 edm::ValueMap<reco::CaloClusterPtr>::Filler filler(*scTrkAssoc_p);
00188 filler.insert(rTracks_, caloPtrVec.begin(), caloPtrVec.end());
00189 filler.fill();
00190 }
00191
00192 theEvent.put(scTrkAssoc_p,trackSuperClusterAssociationCollection_ );
00193
00194 }
00195
00196 }
00197
00198 std::vector<reco::TransientTrack> TrackProducerWithSCAssociation::getTransient(edm::Event& theEvent, const edm::EventSetup& setup)
00199 {
00200 edm::LogInfo("TrackProducerWithSCAssociation") << "Analyzing event number: " << theEvent.id() << "\n";
00201
00202
00203
00204 std::vector<reco::TransientTrack> ttks;
00205
00206
00207
00208
00209 edm::ESHandle<TrackerGeometry> theG;
00210 edm::ESHandle<MagneticField> theMF;
00211 edm::ESHandle<TrajectoryFitter> theFitter;
00212 edm::ESHandle<Propagator> thePropagator;
00213 edm::ESHandle<TransientTrackingRecHitBuilder> theBuilder;
00214 getFromES(setup,theG,theMF,theFitter,thePropagator,theBuilder);
00215
00216
00217
00218
00219 AlgoProductCollection algoResults;
00220 reco::BeamSpot bs;
00221
00222 try{
00223 edm::Handle<TrackCandidateCollection> theTCCollection;
00224 getFromEvt(theEvent,theTCCollection,bs);
00225
00226
00227
00228
00229 LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation run the algorithm" << "\n";
00230 theAlgo.runWithCandidate(theG.product(), theMF.product(), *theTCCollection,
00231 theFitter.product(), thePropagator.product(), theBuilder.product(), bs, algoResults);
00232
00233 } catch (cms::Exception &e){ edm::LogInfo("TrackProducerWithSCAssociation") << "cms::Exception caught!!!" << "\n" << e << "\n";}
00234
00235
00236 for (AlgoProductCollection::iterator prod=algoResults.begin();prod!=algoResults.end(); prod++){
00237 ttks.push_back( reco::TransientTrack(*(((*prod).second).first),thePropagator.product()->magneticField() ));
00238 }
00239
00240 LogDebug("TrackProducerWithSCAssociation") << "TrackProducerWithSCAssociation end" << "\n";
00241
00242 return ttks;
00243 }
00244
00245
00246
00247
00248 void TrackProducerWithSCAssociation::putInEvt(edm::Event& evt,
00249 std::auto_ptr<TrackingRecHitCollection>& selHits,
00250 std::auto_ptr<reco::TrackCollection>& selTracks,
00251 std::auto_ptr<reco::TrackExtraCollection>& selTrackExtras,
00252 std::auto_ptr<std::vector<Trajectory> >& selTrajectories,
00253 AlgoProductCollection& algoResults)
00254 {
00255
00256 TrackingRecHitRefProd rHits = evt.getRefBeforePut<TrackingRecHitCollection>();
00257 reco::TrackExtraRefProd rTrackExtras = evt.getRefBeforePut<reco::TrackExtraCollection>();
00258
00259 edm::Ref<reco::TrackExtraCollection>::key_type idx = 0;
00260 edm::Ref<reco::TrackExtraCollection>::key_type hidx = 0;
00261 edm::Ref<reco::TrackCollection>::key_type iTkRef = 0;
00262 edm::Ref< std::vector<Trajectory> >::key_type iTjRef = 0;
00263 std::map<unsigned int, unsigned int> tjTkMap;
00264
00265 for(AlgoProductCollection::iterator i=algoResults.begin(); i!=algoResults.end();i++){
00266 Trajectory * theTraj = (*i).first;
00267 if(myTrajectoryInEvent_) {
00268 selTrajectories->push_back(*theTraj);
00269 iTjRef++;
00270 }
00271 const TrajectoryFitter::RecHitContainer& transHits = theTraj->recHits();
00272
00273 reco::Track * theTrack = (*i).second.first;
00274 PropagationDirection seedDir = (*i).second.second;
00275
00276 LogDebug("TrackProducer") << "In KfTrackProducerBase::putInEvt - seedDir=" << seedDir;
00277
00278 reco::Track t = * theTrack;
00279 selTracks->push_back( t );
00280 iTkRef++;
00281
00282
00283 if(trajectoryInEvent_) tjTkMap[iTjRef-1] = iTkRef-1;
00284
00285
00286
00287 TrajectoryStateOnSurface outertsos;
00288 TrajectoryStateOnSurface innertsos;
00289 unsigned int innerId, outerId;
00290
00291
00292
00293 if (theTraj->direction() == alongMomentum) {
00294 outertsos = theTraj->lastMeasurement().updatedState();
00295 innertsos = theTraj->firstMeasurement().updatedState();
00296 outerId = theTraj->lastMeasurement().recHit()->geographicalId().rawId();
00297 innerId = theTraj->firstMeasurement().recHit()->geographicalId().rawId();
00298 } else {
00299 outertsos = theTraj->firstMeasurement().updatedState();
00300 innertsos = theTraj->lastMeasurement().updatedState();
00301 outerId = theTraj->firstMeasurement().recHit()->geographicalId().rawId();
00302 innerId = theTraj->lastMeasurement().recHit()->geographicalId().rawId();
00303 }
00304
00305
00306 GlobalPoint v = outertsos.globalParameters().position();
00307 GlobalVector p = outertsos.globalParameters().momentum();
00308 math::XYZVector outmom( p.x(), p.y(), p.z() );
00309 math::XYZPoint outpos( v.x(), v.y(), v.z() );
00310 v = innertsos.globalParameters().position();
00311 p = innertsos.globalParameters().momentum();
00312 math::XYZVector inmom( p.x(), p.y(), p.z() );
00313 math::XYZPoint inpos( v.x(), v.y(), v.z() );
00314
00315 reco::TrackExtraRef teref= reco::TrackExtraRef ( rTrackExtras, idx ++ );
00316 reco::Track & track = selTracks->back();
00317 track.setExtra( teref );
00318
00319
00320 selTrackExtras->push_back( reco::TrackExtra (outpos, outmom, true, inpos, inmom, true,
00321 outertsos.curvilinearError(), outerId,
00322 innertsos.curvilinearError(), innerId,
00323 seedDir,theTraj->seedRef()));
00324
00325
00326 reco::TrackExtra & tx = selTrackExtras->back();
00327
00328
00329 size_t i = 0;
00330 if (theTraj->direction() == alongMomentum) {
00331 for( TrajectoryFitter::RecHitContainer::const_iterator j = transHits.begin();
00332 j != transHits.end(); j ++ ) {
00333 if ((**j).hit()!=0){
00334 TrackingRecHit * hit = (**j).hit()->clone();
00335 track.setHitPattern( * hit, i ++ );
00336 selHits->push_back( hit );
00337 tx.add( TrackingRecHitRef( rHits, hidx ++ ) );
00338 }
00339 }
00340 }else{
00341 for( TrajectoryFitter::RecHitContainer::const_iterator j = transHits.end()-1;
00342 j != transHits.begin()-1; --j ) {
00343 if ((**j).hit()!=0){
00344 TrackingRecHit * hit = (**j).hit()->clone();
00345 track.setHitPattern( * hit, i ++ );
00346 selHits->push_back( hit );
00347 tx.add( TrackingRecHitRef( rHits, hidx ++ ) );
00348 }
00349 }
00350 }
00351
00352
00353 delete theTrack;
00354 delete theTraj;
00355 }
00356
00357 LogTrace("TrackingRegressionTest") << "========== TrackProducer Info ===================";
00358 LogDebug("TrackProducerWithSCAssociation") << "number of finalTracks: " << selTracks->size() << std::endl;
00359 for (reco::TrackCollection::const_iterator it = selTracks->begin(); it != selTracks->end(); it++) {
00360 LogDebug("TrackProducerWithSCAssociation") << "track's n valid and invalid hit, chi2, pt : "
00361 << it->found() << " , "
00362 << it->lost() <<" , "
00363 << it->normalizedChi2() << " , "
00364 << it->pt() << std::endl;
00365 }
00366 LogTrace("TrackingRegressionTest") << "=================================================";
00367
00368
00369 rTracks_ = evt.put( selTracks );
00370
00371
00372 evt.put( selTrackExtras );
00373 evt.put( selHits );
00374
00375 if(myTrajectoryInEvent_) {
00376 edm::OrphanHandle<std::vector<Trajectory> > rTrajs = evt.put(selTrajectories);
00377
00378
00379 std::auto_ptr<TrajTrackAssociationCollection> trajTrackMap( new TrajTrackAssociationCollection() );
00380 for ( std::map<unsigned int, unsigned int>::iterator i = tjTkMap.begin();
00381 i != tjTkMap.end(); i++ ) {
00382 edm::Ref<std::vector<Trajectory> > trajRef( rTrajs, (*i).first );
00383 edm::Ref<reco::TrackCollection> tkRef( rTracks_, (*i).second );
00384 trajTrackMap->insert( edm::Ref<std::vector<Trajectory> >( rTrajs, (*i).first ),
00385 edm::Ref<reco::TrackCollection>( rTracks_, (*i).second ) );
00386 }
00387 evt.put( trajTrackMap );
00388 }
00389
00390
00391
00392
00393
00394
00395 }