00001 #include "FWCore/Framework/interface/EDProducer.h"
00002 #include "FWCore/Framework/interface/EventPrincipal.h"
00003 #include "FWCore/Framework/interface/Event.h"
00004 #include "FWCore/Framework/interface/EventSetup.h"
00005 #include "FWCore/Framework/interface/ESHandle.h"
00006
00007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00008 #include "FWCore/ParameterSet/interface/InputTag.h"
00009
00010 #include "DataFormats/Common/interface/View.h"
00011 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00012 #include "DataFormats/TrackReco/interface/Track.h"
00013 #include "DataFormats/TrackCandidate/interface/TrackCandidateCollection.h"
00014 #include "DataFormats/TrackingRecHit/interface/InvalidTrackingRecHit.h"
00015
00016 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00017 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00018 #include "MagneticField/Engine/interface/MagneticField.h"
00019 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00020 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
00021 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00022
00023 #include "DataFormats/SiPixelDetId/interface/PXBDetId.h"
00024 #include "DataFormats/SiPixelDetId/interface/PXBDetId.h"
00025 #include "DataFormats/SiPixelDetId/interface/PXFDetId.h"
00026 #include "DataFormats/SiStripDetId/interface/TIBDetId.h"
00027 #include "DataFormats/SiStripDetId/interface/TIDDetId.h"
00028 #include "DataFormats/SiStripDetId/interface/TOBDetId.h"
00029 #include "DataFormats/SiStripDetId/interface/TECDetId.h"
00030
00031
00032
00033 #include "Geometry/CommonDetUnit/interface/TrackingGeometry.h"
00034 #include "Geometry/CommonDetUnit/interface/GeomDetUnit.h"
00035 #include "DataFormats/GeometrySurface/interface/Surface.h"
00036 #include "DataFormats/GeometrySurface/interface/GloballyPositioned.h"
00037 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
00038 #include "TrackingTools/TrajectoryState/interface/FreeTrajectoryState.h"
00039
00040 #include "TrackingTools/TrajectoryState/interface/FreeTrajectoryState.h"
00041 #include "DataFormats/GeometrySurface/interface/Surface.h"
00042 #include "DataFormats/CLHEP/interface/AlgebraicObjects.h"
00043 #include "DataFormats/Math/interface/Vector.h"
00044 #include "DataFormats/Math/interface/Error.h"
00045 #include "TrackingTools/TrajectoryState/interface/CopyUsingClone.h"
00046 #include "RecoVertex/VertexTools/interface/PerigeeLinearizedTrackState.h"
00047 #include "Alignment/ReferenceTrajectories/interface/TrajectoryFactoryPlugin.h"
00048
00049 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
00050
00051 #include "DataFormats/GeometryCommonDetAlgo/interface/ErrorFrameTransformer.h"
00052 #include "DataFormats/TrackingRecHit/interface/AlignmentPositionError.h"
00053
00054 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00055
00056 #include "FWCore/Framework/interface/ESHandle.h"
00057 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00058
00059
00060 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
00061 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00062 #include "TrackingTools/TrackFitters/interface/TrajectoryStateCombiner.h"
00063
00064
00065 #include <boost/regex.hpp>
00066
00088 namespace reco { namespace modules {
00089 class CosmicTrackSplitter : public edm::EDProducer {
00090 public:
00091 CosmicTrackSplitter(const edm::ParameterSet &iConfig) ;
00092 virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) ;
00093
00094 private:
00095 edm::InputTag tracks_;
00096 edm::InputTag tjTag_;
00097 int totalTracks_;
00098 size_t minimumHits_;
00099
00100 bool replaceWithInactiveHits_;
00101 bool stripFrontInvalidHits_;
00102 bool stripBackInvalidHits_;
00103 bool stripAllInvalidHits_;
00104
00105 double dZcut_;
00106 double dXYcut_;
00107
00108 std::vector<uint32_t> detsToIgnore_;
00109
00110 edm::ESHandle<TrackerGeometry> theGeometry;
00111 edm::ESHandle<MagneticField> theMagField;
00112
00113 TrackCandidate makeCandidate(const reco::Track &tk, std::vector<TrackingRecHit *>::iterator hitsBegin, std::vector<TrackingRecHit *>::iterator hitsEnd) ;
00114
00115 };
00116
00117
00118 CosmicTrackSplitter::CosmicTrackSplitter(const edm::ParameterSet &iConfig) :
00119 tracks_(iConfig.getParameter<edm::InputTag>("tracks")),
00120 tjTag_(iConfig.getParameter<edm::InputTag>("tjTkAssociationMapTag") ),
00121 minimumHits_(iConfig.getParameter<uint32_t>("minimumHits")),
00122 replaceWithInactiveHits_(iConfig.getParameter<bool>("replaceWithInactiveHits")),
00123 stripFrontInvalidHits_(iConfig.getParameter<bool>("stripFrontInvalidHits")),
00124 stripBackInvalidHits_( iConfig.getParameter<bool>("stripBackInvalidHits") ),
00125 stripAllInvalidHits_( iConfig.getParameter<bool>("stripAllInvalidHits") ),
00126 dZcut_(iConfig.getParameter<double>("dzCut") ),
00127 dXYcut_(iConfig.getParameter<double>("dxyCut") ),
00128 detsToIgnore_( iConfig.getParameter<std::vector<uint32_t> >("detsToIgnore") )
00129 {
00130
00131 if (stripAllInvalidHits_ && replaceWithInactiveHits_) {
00132 throw cms::Exception("Configuration") << "Inconsistent Configuration: you can't set both 'stripAllInvalidHits' and 'replaceWithInactiveHits' to true\n";
00133 }
00134
00135 LogDebug("CosmicTrackSplitter") << "sanity check";
00136
00137
00138 std::sort(detsToIgnore_.begin(), detsToIgnore_.end());
00139
00140 totalTracks_ = 0;
00141
00142
00143 produces<TrackCandidateCollection>();
00144 }
00145
00146 void
00147 CosmicTrackSplitter::produce(edm::Event &iEvent, const edm::EventSetup &iSetup)
00148 {
00149 LogDebug("CosmicTrackSplitter") << "IN THE SPLITTER!!!!!";
00150
00151
00152 edm::Handle<std::vector<reco::Track> > tracks;
00153 iEvent.getByLabel(tracks_, tracks);
00154
00155
00156
00157
00158 edm::Handle<TrajTrackAssociationCollection> m_TrajTracksMap;
00159 iEvent.getByLabel( tjTag_, m_TrajTracksMap );
00160
00161
00162 iSetup.get<TrackerDigiGeometryRecord>().get(theGeometry);
00163 iSetup.get<IdealMagneticFieldRecord>().get(theMagField);
00164
00165
00166 std::auto_ptr<TrackCandidateCollection> output(new TrackCandidateCollection());
00167 output->reserve(tracks->size());
00168
00169
00170 std::vector<TrackingRecHit *> hits;
00171
00172
00173
00174 LogDebug("CosmicTrackSplitter") << "size of map: " << m_TrajTracksMap->size();
00175 int HITTOSPLITFROM = 0;
00176 for ( TrajTrackAssociationCollection::const_iterator iPair = m_TrajTracksMap->begin(); iPair != m_TrajTracksMap->end(); iPair++ ){
00177 const Trajectory* trajFromMap = &(*(*iPair).key);
00178 const reco::Track* trackFromMap = &(*(*iPair).val);
00179
00180
00181 std::vector<TrajectoryMeasurement> measurements = trajFromMap->measurements();
00182 int totalNumberOfHits = measurements.size();
00183 int numberOfHits = 0;
00184 double previousDotProduct = 0;
00185 for (trackingRecHit_iterator ith = trackFromMap->recHitsBegin(), edh = trackFromMap->recHitsEnd(); ith != edh; ++ith) {
00186
00187 GlobalVector stateMomentum = measurements[numberOfHits].forwardPredictedState().globalMomentum();
00188 GlobalPoint statePosition = measurements[numberOfHits].forwardPredictedState().globalPosition();
00189 double dotProduct = stateMomentum.x()*statePosition.x() + stateMomentum.y()*statePosition.y();
00190 if ( dotProduct*previousDotProduct < 0 ){
00191
00192 HITTOSPLITFROM = numberOfHits;
00193 }
00194
00195 previousDotProduct = dotProduct;
00196 numberOfHits++;
00197
00198 }
00199 LogDebug("CosmicTrackSplitter") << "number of rechits: " << numberOfHits;
00200
00201
00202 trackingRecHit_iterator bIt = trackFromMap->recHitsBegin();
00203 trackingRecHit_iterator fIt = trackFromMap->recHitsEnd() - 1;
00204 const TrackingRecHit* bHit = bIt->get();
00205 const TrackingRecHit* fHit = fIt->get();
00206 DetId bdetid = bHit->geographicalId();
00207 DetId fdetid = fHit->geographicalId();
00208 GlobalPoint bPosHit = theGeometry->idToDetUnit( bdetid )->surface().toGlobal(bHit->localPosition());
00209 GlobalPoint fPosHit = theGeometry->idToDetUnit( fdetid )->surface().toGlobal(fHit->localPosition());
00210 GlobalPoint bPosState = measurements[0].updatedState().globalPosition();
00211 GlobalPoint fPosState = measurements[measurements.size() - 1].updatedState().globalPosition();
00212 bool trajReversedFlag = false;
00213 if (( (bPosHit - bPosState).mag() > (bPosHit - fPosState).mag() ) && ( (fPosHit - fPosState).mag() > (fPosHit - bPosState).mag() ) ){
00214 trajReversedFlag = true;
00215 }
00216 if (trajReversedFlag){ int temp = HITTOSPLITFROM; HITTOSPLITFROM = totalNumberOfHits - temp; }
00217 }
00218
00219 totalTracks_ = totalTracks_ + tracks->size();
00220
00221 for (std::vector<reco::Track>::const_iterator itt = tracks->begin(), edt = tracks->end(); itt != edt; ++itt) {
00222 hits.clear();
00223
00224 LogDebug("CosmicTrackSplitter") << "ntracks: " << tracks->size();
00225
00226
00227 math::XYZPoint refPoint = itt->referencePoint();
00228 GlobalPoint v( itt->vx(), itt->vy(), itt->vz() );
00229
00230
00231 bool continueWithTrack = true;
00232 if (fabs(v.z()) > dZcut_) continueWithTrack = false;
00233 if (v.perp() > dXYcut_) continueWithTrack = false;
00234 if (continueWithTrack == false) return;
00235
00236
00237 for (int i = 0; i < 2; ++i){
00238 hits.clear();
00239 LogDebug("CosmicTrackSplitter") << " loop on hits of track #" << (itt - tracks->begin());
00240 int usedHitCtr = 0;
00241 int hitCtr = 0;
00242 for (trackingRecHit_iterator ith = itt->recHitsBegin(), edh = itt->recHitsEnd(); ith != edh; ++ith) {
00243
00244 const TrackingRecHit * hit = ith->get();
00245 LogDebug("CosmicTrackSplitter") << " hit number " << (ith - itt->recHitsBegin());
00246
00247 if (hit->isValid()) {
00248 LogDebug("CosmicTrackSplitter") << " valid, detid = " << hit->geographicalId().rawId();
00249 DetId detid = hit->geographicalId();
00250
00251
00252 if (detid.det() == DetId::Tracker) {
00253 LogDebug("CosmicTrackSplitter") << " valid, tracker ";
00254 bool verdict = false;
00255
00256
00257
00258
00259 const GlobalPoint pos = theGeometry->idToDetUnit( detid )->surface().toGlobal(hit->localPosition());
00260 LogDebug("CosmicTrackSplitter") << "hit pos: " << pos << ", dca pos: " << v;
00261
00262
00263 if ((i == 0)&&(hitCtr < HITTOSPLITFROM)){
00264 verdict = true;
00265 LogDebug("CosmicTrackSplitter") << "tophalf";
00266 }
00267
00268 if ((i == 1)&&(hitCtr >= HITTOSPLITFROM)){
00269 verdict = true;
00270 LogDebug("CosmicTrackSplitter") << "bottomhalf";
00271 }
00272
00273
00274 if ( verdict && std::binary_search(detsToIgnore_.begin(), detsToIgnore_.end(), detid.rawId())) {
00275 verdict = false;
00276 }
00277
00278 LogDebug("CosmicTrackSplitter") << " verdict after module list: " << (verdict ? "ok" : "no");
00279 if (verdict == true) {
00280
00281 hits.push_back(hit->clone());
00282 usedHitCtr++;
00283 }
00284 else {
00285
00286 if (replaceWithInactiveHits_) {
00287 hits.push_back(new InvalidTrackingRecHit(detid, TrackingRecHit::inactive));
00288 }
00289 }
00290 }
00291 else {
00292 hits.push_back(hit->clone());
00293 }
00294 }
00295 else {
00296 if (!stripAllInvalidHits_) {
00297 hits.push_back(hit->clone());
00298 }
00299 }
00300 LogDebug("CosmicTrackSplitter") << " end of hit " << (ith - itt->recHitsBegin());
00301 hitCtr++;
00302 }
00303 LogDebug("CosmicTrackSplitter") << " end of loop on hits of track #" << (itt - tracks->begin());
00304
00305 std::vector<TrackingRecHit *>::iterator begin = hits.begin(), end = hits.end();
00306
00307 LogDebug("CosmicTrackSplitter") << " selected " << hits.size() << " hits ";
00308
00309
00310 if (stripFrontInvalidHits_) {
00311 while ( (begin != end) && ( (*begin)->isValid() == false ) ) ++begin;
00312 }
00313
00314 LogDebug("CosmicTrackSplitter") << " after front stripping we have " << (end - begin) << " hits ";
00315
00316
00317 if (stripBackInvalidHits_ && (begin != end)) {
00318 --end;
00319 while ( (begin != end) && ( (*end)->isValid() == false ) ) --end;
00320 ++end;
00321 }
00322
00323 LogDebug("CosmicTrackSplitter") << " after back stripping we have " << (end - begin) << " hits ";
00324
00325
00326
00327 if ( usedHitCtr >= int(minimumHits_)) {
00328 output->push_back( makeCandidate ( *itt, begin, end ) );
00329 LogDebug("CosmicTrackSplitter") << "we made a candidate of " << hits.size() << " hits!";
00330 }
00331
00332 for (begin = hits.begin(), end = hits.end(); begin != end; ++begin) {
00333 if (*begin) delete *begin;
00334 }
00335 LogDebug("CosmicTrackSplitter") << "loop: " << i << " has " << usedHitCtr << " active hits and " << hits.size() << " total hits...";
00336 hits.clear();
00337 }
00338 }
00339 LogDebug("CosmicTrackSplitter") << "totalTracks_ = " << totalTracks_;
00340 iEvent.put(output);
00341 }
00342
00343 TrackCandidate
00344 CosmicTrackSplitter::makeCandidate(const reco::Track &tk, std::vector<TrackingRecHit *>::iterator hitsBegin, std::vector<TrackingRecHit *>::iterator hitsEnd) {
00345
00346 LogDebug("CosmicTrackSplitter") << "Making a candidate!";
00347
00348 TrajectoryStateTransform transform;
00349 PropagationDirection pdir = tk.seedDirection();
00350 PTrajectoryStateOnDet *state;
00351 if ( pdir == anyDirection ) throw cms::Exception("UnimplementedFeature") << "Cannot work with tracks that have 'anyDirecton' \n";
00352 if ( (pdir == alongMomentum) == ( tk.p() >= tk.outerP() ) ) {
00353
00354 TrajectoryStateOnSurface originalTsosIn(transform.innerStateOnSurface(tk, *theGeometry, &*theMagField));
00355 state = transform.persistentState( originalTsosIn, DetId(tk.innerDetId()) );
00356 } else {
00357
00358 TrajectoryStateOnSurface originalTsosOut(transform.outerStateOnSurface(tk, *theGeometry, &*theMagField));
00359 state = transform.persistentState( originalTsosOut, DetId(tk.outerDetId()) );
00360 }
00361
00362 TrajectorySeed seed(*state, TrackCandidate::RecHitContainer(), pdir);
00363
00364 TrackCandidate::RecHitContainer ownHits;
00365 ownHits.reserve(hitsEnd - hitsBegin);
00366 for ( ; hitsBegin != hitsEnd; ++hitsBegin) { ownHits.push_back( *hitsBegin ); }
00367
00368 TrackCandidate cand(ownHits, seed, *state, tk.seedRef());
00369 delete state;
00370
00371 LogDebug("CosmicTrackSplitter") << " dumping the hits now: ";
00372 for (TrackCandidate::range hitR = cand.recHits(); hitR.first != hitR.second; ++hitR.first) {
00373 LogTrace("CosmicTrackSplitter") << " hit detid = " << hitR.first->geographicalId().rawId() <<
00374 ", type = " << typeid(*hitR.first).name();
00375 }
00376
00377 return cand;
00378 }
00379
00380 }}
00381
00382
00383
00384 #include "FWCore/PluginManager/interface/ModuleDef.h"
00385 #include "FWCore/Framework/interface/MakerMacros.h"
00386
00387 using reco::modules::CosmicTrackSplitter;
00388 DEFINE_FWK_MODULE(CosmicTrackSplitter);