CMS 3D CMS Logo

TrajectorySeedProducer.cc

Go to the documentation of this file.
00001 #include <memory>
00002 
00003 #include "FWCore/Framework/interface/Event.h"
00004 #include "FWCore/Framework/interface/EventSetup.h"
00005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00006 #include "FWCore/Framework/interface/ESHandle.h"
00007 
00008 #include "DataFormats/Common/interface/Handle.h"
00009 #include "DataFormats/Common/interface/OwnVector.h"
00010 #include "DataFormats/TrackerRecHit2D/interface/SiTrackerGSRecHit2DCollection.h" 
00011 #include "DataFormats/TrackerRecHit2D/interface/SiTrackerGSMatchedRecHit2DCollection.h" 
00012 #include "DataFormats/TrajectorySeed/interface/TrajectorySeedCollection.h"
00013 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
00014 #include "DataFormats/VertexReco/interface/Vertex.h"
00015 
00016 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00017 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00018 
00019 #include "FastSimulation/ParticlePropagator/interface/MagneticFieldMapRecord.h"
00020 
00021 #include "FastSimulation/Tracking/plugins/TrajectorySeedProducer.h"
00022 #include "FastSimulation/Tracking/interface/TrackerRecHit.h"
00023 
00024 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
00025 #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
00026 
00027 #include "TrackingTools/TrajectoryParametrization/interface/CurvilinearTrajectoryError.h"
00028 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00029 #include "TrackingTools/TrajectoryState/interface/FreeTrajectoryState.h"
00030 
00031 #include "Geometry/CommonDetUnit/interface/GeomDetUnit.h"
00032 #include "DataFormats/DetId/interface/DetId.h"
00033 
00034 #include "FastSimulation/BaseParticlePropagator/interface/BaseParticlePropagator.h"
00035 #include "FastSimulation/ParticlePropagator/interface/ParticlePropagator.h"
00036 
00037 //Propagator withMaterial
00038 #include "TrackingTools/MaterialEffects/interface/PropagatorWithMaterial.h"
00039 //analyticalpropagator
00040 //#include "TrackingTools/GeomPropagators/interface/AnalyticalPropagator.h"
00041 
00042 
00043 //
00044 
00045 //for debug only 
00046 //#define FAMOS_DEBUG
00047 
00048 TrajectorySeedProducer::TrajectorySeedProducer(const edm::ParameterSet& conf)  :thePropagator(0)
00049 {  
00050 
00051   // The input tag for the beam spot
00052   theBeamSpot = conf.getParameter<edm::InputTag>("beamSpot");
00053 
00054   // The name of the TrajectorySeed Collections
00055   seedingAlgo = conf.getParameter<std::vector<std::string> >("seedingAlgo");
00056   for ( unsigned i=0; i<seedingAlgo.size(); ++i )
00057     produces<TrajectorySeedCollection>(seedingAlgo[i]);
00058 
00059   // The smallest true pT for a track candidate
00060   pTMin = conf.getParameter<std::vector<double> >("pTMin");
00061   if ( pTMin.size() != seedingAlgo.size() ) 
00062     throw cms::Exception("FastSimulation/TrajectorySeedProducer ") 
00063       << " WARNING : pTMin does not have the proper size "
00064       << std::endl;
00065 
00066   for ( unsigned i=0; i<pTMin.size(); ++i )
00067     pTMin[i] *= pTMin[i];  // Cut is done of perp2() - CPU saver
00068   
00069   // The smallest number of Rec Hits for a track candidate
00070   minRecHits = conf.getParameter<std::vector<unsigned int> >("minRecHits");
00071   if ( minRecHits.size() != seedingAlgo.size() ) 
00072     throw cms::Exception("FastSimulation/TrajectorySeedProducer ") 
00073       << " WARNING : minRecHits does not have the proper size "
00074       << std::endl;
00075   // Set the overall number hits to be checked
00076   absMinRecHits = 0;
00077   for ( unsigned ialgo=0; ialgo<minRecHits.size(); ++ialgo ) 
00078     if ( minRecHits[ialgo] > absMinRecHits ) absMinRecHits = minRecHits[ialgo];
00079 
00080   // The smallest true impact parameters (d0 and z0) for a track candidate
00081   maxD0 = conf.getParameter<std::vector<double> >("maxD0");
00082   if ( maxD0.size() != seedingAlgo.size() ) 
00083     throw cms::Exception("FastSimulation/TrajectorySeedProducer ") 
00084       << " WARNING : maxD0 does not have the proper size "
00085       << std::endl;
00086 
00087   maxZ0 = conf.getParameter<std::vector<double> >("maxZ0");
00088   if ( maxZ0.size() != seedingAlgo.size() ) 
00089     throw cms::Exception("FastSimulation/TrajectorySeedProducer ") 
00090       << " WARNING : maxZ0 does not have the proper size "
00091       << std::endl;
00092 
00093   // The name of the hit producer
00094   hitProducer = conf.getParameter<edm::InputTag>("HitProducer");
00095 
00096   // The cuts for seed cleaning
00097   seedCleaning = conf.getParameter<bool>("seedCleaning");
00098 
00099   // Number of hits needed for a seed
00100   numberOfHits = conf.getParameter<std::vector<unsigned int> >("numberOfHits");
00101   if ( numberOfHits.size() != seedingAlgo.size() ) 
00102     throw cms::Exception("FastSimulation/TrajectorySeedProducer ") 
00103       << " WARNING : numberOfHits does not have the proper size "
00104       << std::endl;
00105 
00106   //
00107   firstHitSubDetectorNumber = 
00108     conf.getParameter<std::vector<unsigned int> >("firstHitSubDetectorNumber");
00109   if ( firstHitSubDetectorNumber.size() != seedingAlgo.size() ) 
00110     throw cms::Exception("FastSimulation/TrajectorySeedProducer ") 
00111       << " WARNING : firstHitSubDetectorNumber does not have the proper size "
00112       << std::endl;
00113 
00114   std::vector<unsigned int> firstSubDets = 
00115     conf.getParameter<std::vector<unsigned int> >("firstHitSubDetectors");
00116   unsigned isub1 = 0;
00117   unsigned check1 = 0;
00118   firstHitSubDetectors.resize(seedingAlgo.size());
00119   for ( unsigned ialgo=0; ialgo<firstHitSubDetectorNumber.size(); ++ialgo ) { 
00120     check1 += firstHitSubDetectorNumber[ialgo];
00121     for ( unsigned idet=0; idet<firstHitSubDetectorNumber[ialgo]; ++idet ) { 
00122       firstHitSubDetectors[ialgo].push_back(firstSubDets[isub1++]);
00123     }
00124   }
00125   if ( firstSubDets.size() != check1 ) 
00126     throw cms::Exception("FastSimulation/TrajectorySeedProducer ") 
00127       << " WARNING : firstHitSubDetectors does not have the proper size (should be " << check1 << ")"
00128       << std::endl;
00129 
00130 
00131   secondHitSubDetectorNumber = 
00132     conf.getParameter<std::vector<unsigned int> >("secondHitSubDetectorNumber");
00133   if ( secondHitSubDetectorNumber.size() != seedingAlgo.size() ) 
00134     throw cms::Exception("FastSimulation/TrajectorySeedProducer ") 
00135       << " WARNING : secondHitSubDetectorNumber does not have the proper size "
00136       << std::endl;
00137 
00138   std::vector<unsigned int> secondSubDets = 
00139     conf.getParameter<std::vector<unsigned int> >("secondHitSubDetectors");
00140   unsigned isub2 = 0;
00141   unsigned check2 = 0;
00142   secondHitSubDetectors.resize(seedingAlgo.size());
00143   for ( unsigned ialgo=0; ialgo<secondHitSubDetectorNumber.size(); ++ialgo ) { 
00144     check2 += secondHitSubDetectorNumber[ialgo];
00145     for ( unsigned idet=0; idet<secondHitSubDetectorNumber[ialgo]; ++idet ) { 
00146       secondHitSubDetectors[ialgo].push_back(secondSubDets[isub2++]);
00147     }
00148   }
00149   if ( secondSubDets.size() != check2 ) 
00150     throw cms::Exception("FastSimulation/TrajectorySeedProducer ") 
00151       << " WARNING : secondHitSubDetectors does not have the proper size (should be " << check2 << ")"
00152       << std::endl;
00153 
00154   thirdHitSubDetectorNumber = 
00155     conf.getParameter<std::vector<unsigned int> >("thirdHitSubDetectorNumber");
00156   if ( thirdHitSubDetectorNumber.size() != seedingAlgo.size() ) 
00157     throw cms::Exception("FastSimulation/TrajectorySeedProducer ") 
00158       << " WARNING : thirdHitSubDetectorNumber does not have the proper size "
00159       << std::endl;
00160 
00161   std::vector<unsigned int> thirdSubDets = 
00162     conf.getParameter<std::vector<unsigned int> >("thirdHitSubDetectors");
00163   unsigned isub3 = 0;
00164   unsigned check3 = 0;
00165   thirdHitSubDetectors.resize(seedingAlgo.size());
00166   for ( unsigned ialgo=0; ialgo<thirdHitSubDetectorNumber.size(); ++ialgo ) { 
00167     check3 += thirdHitSubDetectorNumber[ialgo];
00168     for ( unsigned idet=0; idet<thirdHitSubDetectorNumber[ialgo]; ++idet ) { 
00169       thirdHitSubDetectors[ialgo].push_back(thirdSubDets[isub3++]);
00170     }
00171   }
00172   if ( thirdSubDets.size() != check3 ) 
00173     throw cms::Exception("FastSimulation/TrajectorySeedProducer ") 
00174       << " WARNING : thirdHitSubDetectors does not have the proper size (should be " << check3 << ")"
00175       << std::endl;
00176 
00177   originRadius = conf.getParameter<std::vector<double> >("originRadius");
00178   if ( originRadius.size() != seedingAlgo.size() ) 
00179     throw cms::Exception("FastSimulation/TrajectorySeedProducer ") 
00180       << " WARNING : originRadius does not have the proper size "
00181       << std::endl;
00182 
00183   originHalfLength = conf.getParameter<std::vector<double> >("originHalfLength");
00184   if ( originHalfLength.size() != seedingAlgo.size() ) 
00185     throw cms::Exception("FastSimulation/TrajectorySeedProducer ") 
00186       << " WARNING : originHalfLength does not have the proper size "
00187       << std::endl;
00188 
00189   originpTMin = conf.getParameter<std::vector<double> >("originpTMin");
00190   if ( originpTMin.size() != seedingAlgo.size() ) 
00191     throw cms::Exception("FastSimulation/TrajectorySeedProducer ") 
00192       << " WARNING : originpTMin does not have the proper size "
00193       << std::endl;
00194 
00195   primaryVertices = conf.getParameter<std::vector<edm::InputTag> >("primaryVertices");
00196   if ( primaryVertices.size() != seedingAlgo.size() ) 
00197     throw cms::Exception("FastSimulation/TrajectorySeedProducer ") 
00198       << " WARNING : primaryVertices does not have the proper size "
00199       << std::endl;
00200 
00201   zVertexConstraint = conf.getParameter<std::vector<double> >("zVertexConstraint");
00202   if ( zVertexConstraint.size() != seedingAlgo.size() ) 
00203     throw cms::Exception("FastSimulation/TrajectorySeedProducer ") 
00204       << " WARNING : zVertexConstraint does not have the proper size "
00205       << std::endl;
00206 
00207 }
00208 
00209   
00210 // Virtual destructor needed.
00211 TrajectorySeedProducer::~TrajectorySeedProducer() {
00212   
00213   if(thePropagator) delete thePropagator;
00214 
00215   // do nothing
00216 #ifdef FAMOS_DEBUG
00217   std::cout << "TrajectorySeedProducer destructed" << std::endl;
00218 #endif
00219 
00220 } 
00221  
00222 void 
00223 TrajectorySeedProducer::beginRun(edm::Run & run, const edm::EventSetup & es) {
00224 
00225   //services
00226   //  es.get<TrackerRecoGeometryRecord>().get(theGeomSearchTracker);
00227 
00228   edm::ESHandle<MagneticField>          magField;
00229   edm::ESHandle<TrackerGeometry>        geometry;
00230   edm::ESHandle<MagneticFieldMap>       magFieldMap;
00231 
00232 
00233   es.get<IdealMagneticFieldRecord>().get(magField);
00234   es.get<TrackerDigiGeometryRecord>().get(geometry);
00235   es.get<MagneticFieldMapRecord>().get(magFieldMap);
00236 
00237   theMagField = &(*magField);
00238   theGeometry = &(*geometry);
00239   theFieldMap = &(*magFieldMap);
00240 
00241   thePropagator = new PropagatorWithMaterial(alongMomentum,0.105,&(*theMagField)); 
00242 
00243   const GlobalPoint g(0.,0.,0.);
00244 
00245 }
00246   
00247   // Functions that gets called by framework every event
00248 void 
00249 TrajectorySeedProducer::produce(edm::Event& e, const edm::EventSetup& es) {        
00250 
00251 #ifdef FAMOS_DEBUG
00252   std::cout << "################################################################" << std::endl;
00253   std::cout << " TrajectorySeedProducer produce init " << std::endl;
00254 #endif
00255 
00256   unsigned nSimTracks = 0;
00257   unsigned nTracksWithHits = 0;
00258   unsigned nTracksWithPT = 0;
00259   unsigned nTracksWithD0Z0 = 0;
00260   //  unsigned nTrackCandidates = 0;
00261   PTrajectoryStateOnDet initialState;
00262   
00263   // Output
00264   std::vector<TrajectorySeedCollection*>
00265     output(seedingAlgo.size(),static_cast<TrajectorySeedCollection*>(0));
00266   for ( unsigned ialgo=0; ialgo<seedingAlgo.size(); ++ialgo ) { 
00267     //    std::auto_ptr<TrajectorySeedCollection> p(new TrajectorySeedCollection );
00268     output[ialgo] = new TrajectorySeedCollection;
00269   }
00270   
00271   // Beam spot
00272   edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
00273   e.getByLabel(theBeamSpot,recoBeamSpotHandle); 
00274   math::XYZPoint BSPosition_ = recoBeamSpotHandle->position();
00275   double sigmaZ=recoBeamSpotHandle->sigmaZ();
00276   double sigmaZ0Error=recoBeamSpotHandle->sigmaZ0Error();
00277   double sigmaz0=sqrt(sigmaZ*sigmaZ+sigmaZ0Error*sigmaZ0Error);
00278   x0 = BSPosition_.X();
00279   y0 = BSPosition_.Y();
00280   z0 = BSPosition_.Z();
00281 
00282   // SimTracks and SimVertices
00283   edm::Handle<edm::SimTrackContainer> theSimTracks;
00284   e.getByLabel("famosSimHits",theSimTracks);
00285   
00286   edm::Handle<edm::SimVertexContainer> theSimVtx;
00287   e.getByLabel("famosSimHits",theSimVtx);
00288 
00289 #ifdef FAMOS_DEBUG
00290   std::cout << " Step A: SimTracks found " << theSimTracks->size() << std::endl;
00291 #endif
00292   
00293   //  edm::Handle<SiTrackerGSRecHit2DCollection> theGSRecHits;
00294   edm::Handle<SiTrackerGSMatchedRecHit2DCollection> theGSRecHits;
00295   e.getByLabel(hitProducer, theGSRecHits);
00296   
00297   // No tracking attempted if no hits (but put an empty collection in the event)!
00298 #ifdef FAMOS_DEBUG
00299   std::cout << " Step B: Full GS RecHits found " << theGSRecHits->size() << std::endl;
00300 #endif
00301   if(theGSRecHits->size() == 0) {
00302     for ( unsigned ialgo=0; ialgo<seedingAlgo.size(); ++ialgo ) {
00303       std::auto_ptr<TrajectorySeedCollection> p(output[ialgo]);
00304       e.put(p,seedingAlgo[ialgo]);
00305     }
00306     return;
00307   }
00308   
00309   // Primary vertices
00310   vertices = std::vector<const reco::VertexCollection*>
00311     (seedingAlgo.size(),static_cast<const reco::VertexCollection*>(0));
00312   for ( unsigned ialgo=0; ialgo<seedingAlgo.size(); ++ialgo ) { 
00313     originHalfLength[ialgo] = 3.*sigmaz0; // Overrides the configuration
00314     edm::Handle<reco::VertexCollection> aHandle;
00315     bool isVertexCollection = e.getByLabel(primaryVertices[ialgo],aHandle);
00316     if (!isVertexCollection ) continue;
00317     vertices[ialgo] = &(*aHandle);
00318   }
00319   
00320 #ifdef FAMOS_DEBUG
00321   std::cout << " Step C: Loop over the RecHits, track by track " << std::endl;
00322 #endif
00323 
00324   // The vector of simTrack Id's carrying GSRecHits
00325   const std::vector<unsigned> theSimTrackIds = theGSRecHits->ids();
00326 
00327   // loop over SimTrack Id's
00328   for ( unsigned tkId=0;  tkId != theSimTrackIds.size(); ++tkId ) {
00329 
00330 #ifdef FAMOS_DEBUG
00331     std::cout << "Track number " << tkId << std::endl;
00332 #endif
00333 
00334     ++nSimTracks;
00335     unsigned simTrackId = theSimTrackIds[tkId];
00336     const SimTrack& theSimTrack = (*theSimTracks)[simTrackId]; 
00337 #ifdef FAMOS_DEBUG
00338     std::cout << "Pt = " << std::sqrt(theSimTrack.momentum().Perp2()) 
00339               << " eta " << theSimTrack.momentum().Eta()
00340               << std::endl;
00341 #endif
00342     
00343     // Check that the sim track comes from the main vertex (loose cut)
00344     int vertexIndex = theSimTrack.vertIndex();
00345     const SimVertex& theSimVertex = (*theSimVtx)[vertexIndex]; 
00346 #ifdef FAMOS_DEBUG
00347     std::cout << " o SimTrack " << theSimTrack << std::endl;
00348     std::cout << " o SimVertex " << theSimVertex << std::endl;
00349 #endif
00350     
00351     BaseParticlePropagator theParticle = 
00352       BaseParticlePropagator( 
00353          RawParticle(XYZTLorentzVector(theSimTrack.momentum().px(),
00354                                        theSimTrack.momentum().py(),
00355                                        theSimTrack.momentum().pz(),
00356                                        theSimTrack.momentum().e()),
00357                      XYZTLorentzVector(theSimVertex.position().x(),
00358                                        theSimVertex.position().y(),
00359                                        theSimVertex.position().z(),
00360                                        theSimVertex.position().t())),
00361                      0.,0.,4.);
00362     theParticle.setCharge((*theSimTracks)[simTrackId].charge());
00363 
00364     SiTrackerGSMatchedRecHit2DCollection::range theRecHitRange = theGSRecHits->get(simTrackId);
00365     SiTrackerGSMatchedRecHit2DCollection::const_iterator theRecHitRangeIteratorBegin = theRecHitRange.first;
00366     SiTrackerGSMatchedRecHit2DCollection::const_iterator theRecHitRangeIteratorEnd   = theRecHitRange.second;
00367     SiTrackerGSMatchedRecHit2DCollection::const_iterator iterRecHit;
00368     SiTrackerGSMatchedRecHit2DCollection::const_iterator iterRecHit1;
00369     SiTrackerGSMatchedRecHit2DCollection::const_iterator iterRecHit2;
00370     SiTrackerGSMatchedRecHit2DCollection::const_iterator iterRecHit3;
00371 
00372     // Check the number of layers crossed
00373     unsigned numberOfRecHits = 0;
00374     TrackerRecHit previousHit, currentHit;
00375     for ( iterRecHit = theRecHitRangeIteratorBegin; 
00376           iterRecHit != theRecHitRangeIteratorEnd; 
00377           ++iterRecHit) { 
00378       previousHit = currentHit;
00379       currentHit = TrackerRecHit(&(*iterRecHit),theGeometry);
00380       if ( currentHit.isOnTheSameLayer(previousHit) ) continue;
00381       ++numberOfRecHits;
00382       if ( numberOfRecHits == absMinRecHits ) break;
00383     }
00384 
00385     // Loop on the successive seedings
00386     for ( unsigned int ialgo = 0; ialgo < seedingAlgo.size(); ++ialgo ) { 
00387 
00388 #ifdef FAMOS_DEBUG
00389       std::cout << "Algo " << seedingAlgo[ialgo] << std::endl;
00390 #endif
00391 
00392       // Request a minimum number of RecHits for the track to give a seed.
00393 #ifdef FAMOS_DEBUG
00394       std::cout << "The number of RecHits = " << numberOfRecHits << std::endl;
00395 #endif
00396       if ( numberOfRecHits < minRecHits[ialgo] ) continue;
00397       ++nTracksWithHits;
00398 
00399       // Request a minimum pT for the sim track
00400       if ( theSimTrack.momentum().Perp2() < pTMin[ialgo] ) continue;
00401       ++nTracksWithPT;
00402       
00403       // Cut on sim track impact parameters
00404       if ( theParticle.xyImpactParameter(x0,y0) > maxD0[ialgo] ) continue;
00405       if ( fabs( theParticle.zImpactParameter(x0,y0) - z0 ) > maxZ0[ialgo] ) continue;
00406       ++nTracksWithD0Z0;
00407       
00408       std::vector<TrackerRecHit > 
00409         theSeedHits(numberOfHits[ialgo],
00410                     static_cast<TrackerRecHit >(TrackerRecHit()));
00411       TrackerRecHit& theSeedHits0 = theSeedHits[0];
00412       TrackerRecHit& theSeedHits1 = theSeedHits[1];
00413       TrackerRecHit& theSeedHits2 = theSeedHits[2];
00414       bool compatible = false;
00415       for ( iterRecHit1 = theRecHitRangeIteratorBegin; iterRecHit1 != theRecHitRangeIteratorEnd; ++iterRecHit1) {
00416         theSeedHits[0] = TrackerRecHit(&(*iterRecHit1),theGeometry);
00417 #ifdef FAMOS_DEBUG
00418         std::cout << "The first hit position = " << theSeedHits0.globalPosition() << std::endl;
00419         std::cout << "The first hit subDetId = " << theSeedHits0.subDetId() << std::endl;
00420         std::cout << "The first hit layer    = " << theSeedHits0.layerNumber() << std::endl;
00421 #endif
00422 
00423         // Check if inside the requested detectors
00424         bool isInside = theSeedHits0.subDetId() < firstHitSubDetectors[ialgo][0];
00425         if ( isInside ) continue;
00426 
00427         // Check if on requested detectors
00428         bool isOndet =  theSeedHits0.isOnRequestedDet(firstHitSubDetectors[ialgo]);
00429         if ( !isOndet ) break;
00430 
00431 #ifdef FAMOS_DEBUG
00432         std::cout << "Apparently the first hit is on the requested detector! " << std::endl;
00433 #endif
00434         for ( iterRecHit2 = iterRecHit1+1; iterRecHit2 != theRecHitRangeIteratorEnd; ++iterRecHit2) {
00435           theSeedHits[1] = TrackerRecHit(&(*iterRecHit2),theGeometry);
00436 #ifdef FAMOS_DEBUG
00437           std::cout << "The second hit position = " << theSeedHits1.globalPosition() << std::endl;
00438           std::cout << "The second hit subDetId = " << theSeedHits1.subDetId() << std::endl;
00439           std::cout << "The second hit layer    = " << theSeedHits1.layerNumber() << std::endl;
00440 #endif
00441 
00442           // Check if inside the requested detectors
00443           isInside = theSeedHits1.subDetId() < secondHitSubDetectors[ialgo][0];
00444           if ( isInside ) continue;
00445 
00446           // Check if on requested detectors
00447           isOndet =  theSeedHits1.isOnRequestedDet(secondHitSubDetectors[ialgo]);
00448           if ( !isOndet ) break;
00449 
00450           // Check if on the same layer as previous hit
00451           if ( theSeedHits1.isOnTheSameLayer(theSeedHits0) ) continue;
00452 
00453 #ifdef FAMOS_DEBUG
00454           std::cout << "Apparently the second hit is on the requested detector! " << std::endl;
00455 #endif
00456           GlobalPoint gpos1 = theSeedHits0.globalPosition();
00457           GlobalPoint gpos2 = theSeedHits1.globalPosition();
00458           bool forward = theSeedHits0.isForward();
00459           double error = std::sqrt(theSeedHits0.largerError()+theSeedHits1.largerError());
00460           //      compatible = compatibleWithVertex(gpos1,gpos2,ialgo);
00461           compatible = compatibleWithBeamAxis(gpos1,gpos2,error,forward,ialgo);
00462 #ifdef FAMOS_DEBUG
00463           std::cout << "Are the two hits compatible with the PV? " << compatible << std::endl;
00464 #endif
00465 
00466           // Check if the pair is on the requested dets
00467           if ( numberOfHits[ialgo] == 2 ) compatible = compatible && theSeedHits[0].makesAPairWith(theSeedHits[1]);
00468 
00469           // Reject non suited pairs
00470           if ( !compatible ) continue;
00471 
00472 #ifdef FAMOS_DEBUG
00473           std::cout << "Pair kept! " << std::endl;
00474 #endif
00475 
00476           // Leave here if only two hits are required.
00477           if ( numberOfHits[ialgo] == 2 ) break; 
00478           
00479           compatible = false;
00480           // Check if there is a third satisfying hit otherwise
00481           for ( iterRecHit3 = iterRecHit2+1; iterRecHit3 != theRecHitRangeIteratorEnd; ++iterRecHit3) {
00482             theSeedHits[2] = TrackerRecHit(&(*iterRecHit3),theGeometry);
00483 #ifdef FAMOS_DEBUG
00484             std::cout << "The third hit position = " << theSeedHits2.globalPosition() << std::endl;
00485             std::cout << "The third hit subDetId = " << theSeedHits2.subDetId() << std::endl;
00486             std::cout << "The third hit layer    = " << theSeedHits2.layerNumber() << std::endl;
00487 #endif
00488 
00489             // Check if inside the requested detectors
00490             isInside = theSeedHits2.subDetId() < thirdHitSubDetectors[ialgo][0];
00491             if ( isInside ) continue;
00492             
00493             // Check if on requested detectors
00494             isOndet =  theSeedHits2.isOnRequestedDet(thirdHitSubDetectors[ialgo]);
00495             if ( !isOndet ) break;
00496 
00497             // Check if on the same layer as previous hit
00498             compatible = !(theSeedHits2.isOnTheSameLayer(theSeedHits1));
00499 
00500             // Check if the triplet is on the requested det combination
00501             compatible = compatible && theSeedHits[0].makesATripletWith(theSeedHits[1],theSeedHits[2]);
00502 
00503 #ifdef FAMOS_DEBUG
00504             if ( compatible ) 
00505               std::cout << "Apparently the third hit is on the requested detector! " << std::endl;
00506 #endif
00507 
00508             if ( compatible ) break;      
00509 
00510           }
00511 
00512           if ( compatible ) break;
00513 
00514         }
00515 
00516         if ( compatible ) break;
00517 
00518       }
00519 
00520       // There is no compatible seed for this track with this seeding algorithm 
00521       // Go to next algo
00522       if ( !compatible ) continue;
00523 
00524 #ifdef FAMOS_DEBUG
00525       std::cout << "Preparing to create the TrajectorySeed" << std::endl;
00526 #endif
00527       // The seed is validated -> include in the collection
00528       // 1) Create the vector of RecHits
00529       edm::OwnVector<TrackingRecHit> recHits;
00530       for ( unsigned ih=0; ih<theSeedHits.size(); ++ih ) {
00531         TrackingRecHit* aTrackingRecHit = theSeedHits[ih].hit()->clone();
00532         recHits.push_back(aTrackingRecHit);
00533       }
00534 #ifdef FAMOS_DEBUG
00535       std::cout << "with " << recHits.size() << " hits." << std::endl;
00536 #endif
00537 
00538       // 2) Create the initial state
00539       //   a) origin vertex
00540       GlobalPoint  position((*theSimVtx)[vertexIndex].position().x(),
00541                             (*theSimVtx)[vertexIndex].position().y(),
00542                             (*theSimVtx)[vertexIndex].position().z());
00543       
00544       //   b) initial momentum
00545       GlobalVector momentum( (*theSimTracks)[simTrackId].momentum().x() , 
00546                              (*theSimTracks)[simTrackId].momentum().y() , 
00547                              (*theSimTracks)[simTrackId].momentum().z() );
00548       //   c) electric charge
00549       float        charge   = (*theSimTracks)[simTrackId].charge();
00550       //  -> inital parameters
00551       GlobalTrajectoryParameters initialParams(position,momentum,(int)charge,theMagField);
00552       //  -> large initial errors
00553       AlgebraicSymMatrix errorMatrix(5,1);      
00554       // errorMatrix = errorMatrix * 10;
00555 #ifdef FAMOS_DEBUG
00556       std::cout << "TrajectorySeedProducer: SimTrack parameters " << std::endl;
00557       std::cout << "\t\t pT  = " << (*theSimTracks)[simTrackId].momentum().Pt() << std::endl;
00558       std::cout << "\t\t eta = " << (*theSimTracks)[simTrackId].momentum().Eta()  << std::endl;
00559       std::cout << "\t\t phi = " << (*theSimTracks)[simTrackId].momentum().Phi()  << std::endl;
00560       std::cout << "TrajectorySeedProducer: AlgebraicSymMatrix " << errorMatrix << std::endl;
00561 #endif
00562       CurvilinearTrajectoryError initialError(errorMatrix);
00563       // -> initial state
00564       FreeTrajectoryState initialFTS(initialParams, initialError);      
00565 #ifdef FAMOS_DEBUG
00566       std::cout << "TrajectorySeedProducer: FTS momentum " << initialFTS.momentum() << std::endl;
00567 #endif
00568       // const GeomDetUnit* initialLayer = theGeometry->idToDetUnit( recHits.front().geographicalId() );
00569       const GeomDet* initialLayer = theGeometry->idToDet( recHits.front().geographicalId() );
00570 
00571       //this is wrong because the FTS is defined at vertex, and it need to be properly propagated.
00572       //      const TrajectoryStateOnSurface initialTSOS(initialFTS, initialLayer->surface());      
00573 
00574       const TrajectoryStateOnSurface initialTSOS = thePropagator->propagate(initialFTS,initialLayer->surface()) ;
00575       if (!initialTSOS.isValid()) continue; 
00576 
00577 #ifdef FAMOS_DEBUG
00578       std::cout << "TrajectorySeedProducer: TSOS global momentum "    << initialTSOS.globalMomentum() << std::endl;
00579       std::cout << "\t\t\tpT = "                                     << initialTSOS.globalMomentum().perp() << std::endl;
00580       std::cout << "\t\t\teta = "                                    << initialTSOS.globalMomentum().eta() << std::endl;
00581       std::cout << "\t\t\tphi = "                                    << initialTSOS.globalMomentum().phi() << std::endl;
00582       std::cout << "TrajectorySeedProducer: TSOS local momentum "     << initialTSOS.localMomentum()  << std::endl;
00583       std::cout << "TrajectorySeedProducer: TSOS local error "        << initialTSOS.localError().positionError() << std::endl;
00584       std::cout << "TrajectorySeedProducer: TSOS local error matrix " << initialTSOS.localError().matrix() << std::endl;
00585       std::cout << "TrajectorySeedProducer: TSOS surface side "       << initialTSOS.surfaceSide()    << std::endl;
00586 #endif
00587       stateOnDet(initialTSOS, 
00588                  recHits.front().geographicalId().rawId(),
00589                  initialState);
00590       // Create a new Trajectory Seed    
00591       output[ialgo]->push_back(TrajectorySeed(initialState, recHits, alongMomentum));
00592 #ifdef FAMOS_DEBUG
00593       std::cout << "Trajectory seed created ! " << std::endl;
00594 #endif
00595       break;
00596       // End of the loop over seeding algorithms
00597     }
00598     // End on the loop over simtracks
00599   }
00600 
00601   for ( unsigned ialgo=0; ialgo<seedingAlgo.size(); ++ialgo ) { 
00602     std::auto_ptr<TrajectorySeedCollection> p(output[ialgo]);
00603     e.put(p,seedingAlgo[ialgo]);
00604   }
00605 
00606 }
00607 
00608 // This is a copy of a method in 
00609 // TrackingTools/TrajectoryState/src/TrajectoryStateTransform.cc
00610 // but it does not return a pointer (thus avoiding a memory leak)
00611 // In addition, it's also CPU more efficient, because 
00612 // ts.localError().matrix() is not copied
00613 void 
00614 TrajectorySeedProducer::stateOnDet(const TrajectoryStateOnSurface& ts,
00615                                    unsigned int detid,
00616                                    PTrajectoryStateOnDet& pts) const
00617 {
00618 
00619   const AlgebraicSymMatrix55& m = ts.localError().matrix();
00620   
00621   int dim = 5; 
00622 
00623   float localErrors[15];
00624   int k = 0;
00625   for (int i=0; i<dim; ++i) {
00626     for (int j=0; j<=i; ++j) {
00627       localErrors[k++] = m(i,j);
00628     }
00629   }
00630   int surfaceSide = static_cast<int>(ts.surfaceSide());
00631 
00632   pts = PTrajectoryStateOnDet( ts.localParameters(),
00633                                localErrors, detid,
00634                                surfaceSide);
00635 }
00636 
00637 bool
00638 TrajectorySeedProducer::compatibleWithBeamAxis(GlobalPoint& gpos1, 
00639                                                GlobalPoint& gpos2,
00640                                                double error,
00641                                                bool forward,
00642                                                unsigned algo) const {
00643 
00644   if ( !seedCleaning ) return true;
00645 
00646   // The hits 1 and 2 positions, in HepLorentzVector's
00647   XYZTLorentzVector thePos1(gpos1.x(),gpos1.y(),gpos1.z(),0.);
00648   XYZTLorentzVector thePos2(gpos2.x(),gpos2.y(),gpos2.z(),0.);
00649 #ifdef FAMOS_DEBUG
00650   std::cout << "ThePos1 = " << thePos1 << std::endl;
00651   std::cout << "ThePos2 = " << thePos2 << std::endl;
00652 #endif
00653 
00654 
00655   // Create new particles that pass through the second hit with pT = ptMin 
00656   // and charge = +/-1
00657   
00658   // The momentum direction is by default joining the two hits 
00659   XYZTLorentzVector theMom2 = (thePos2-thePos1);
00660 
00661   // The corresponding RawParticle, with an (irrelevant) electric charge
00662   // (The charge is determined in the next step)
00663   ParticlePropagator myPart(theMom2,thePos2,1.,theFieldMap);
00664 
00668   bool intersect = myPart.propagateToBeamCylinder(thePos1,originRadius[algo]*1.);
00669   if ( !intersect ) return false;
00670 
00671 #ifdef FAMOS_DEBUG
00672   std::cout << "MyPart R = " << myPart.R() << "\t Z = " << myPart.Z() 
00673             << "\t pT = " << myPart.Pt() << std::endl;
00674 #endif
00675 
00676   // Check if the constraints are satisfied
00677   // 1. pT at cylinder with radius originRadius
00678   if ( myPart.Pt() < originpTMin[algo] ) return false;
00679 
00680   // 2. Z compatible with beam spot size
00681   if ( fabs(myPart.Z()-z0) > originHalfLength[algo] ) return false;
00682 
00683   // 3. Z compatible with one of the primary vertices (always the case if no primary vertex)
00684   const reco::VertexCollection* theVertices = vertices[algo];
00685   if (!theVertices) return true;
00686   unsigned nVertices = theVertices->size();
00687   if ( !nVertices || zVertexConstraint[algo] < 0. ) return true;
00688   // Radii of the two hits with respect to the beam spot position
00689   double R1 = std::sqrt ( (thePos1.X()-x0)*(thePos1.X()-x0) 
00690                         + (thePos1.Y()-y0)*(thePos1.Y()-y0) );
00691   double R2 = std::sqrt ( (thePos2.X()-x0)*(thePos2.X()-x0) 
00692                         + (thePos2.Y()-y0)*(thePos2.Y()-y0) );
00693   // Loop on primary vertices
00694   for ( unsigned iv=0; iv<nVertices; ++iv ) { 
00695     // Z position of the primary vertex
00696     double zV = (*theVertices)[iv].z();
00697     // Constraints on the inner hit
00698     double checkRZ1 = forward ?
00699       (thePos1.Z()-zV+zVertexConstraint[algo]) / (thePos2.Z()-zV+zVertexConstraint[algo]) * R2 : 
00700       -zVertexConstraint[algo] + R1/R2*(thePos2.Z()-zV+zVertexConstraint[algo]);
00701     double checkRZ2 = forward ?
00702       (thePos1.Z()-zV-zVertexConstraint[algo])/(thePos2.Z()-zV-zVertexConstraint[algo]) * R2 :
00703       +zVertexConstraint[algo] + R1/R2*(thePos2.Z()-zV-zVertexConstraint[algo]);
00704     double checkRZmin = std::min(checkRZ1,checkRZ2)-3.*error;
00705     double checkRZmax = std::max(checkRZ1,checkRZ2)+3.*error;
00706     // Check if the innerhit is within bounds
00707     bool compat = forward ?
00708       checkRZmin < R1 && R1 < checkRZmax : 
00709       checkRZmin < thePos1.Z()-zV && thePos1.Z()-zV < checkRZmax; 
00710     // If it is, just return ok
00711     if ( compat ) return compat;
00712   }
00713   // Otherwise, return not ok
00714   return false;
00715 
00716 }  
00717 

Generated on Tue Jun 9 17:35:16 2009 for CMSSW by  doxygen 1.5.4