CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/RecoEgamma/EgammaPhotonAlgos/src/InOutConversionSeedFinder.cc

Go to the documentation of this file.
00001 #include "RecoEgamma/EgammaPhotonAlgos/interface/InOutConversionSeedFinder.h"
00002 #include "RecoEgamma/EgammaPhotonAlgos/interface/ConversionBarrelEstimator.h"
00003 #include "RecoEgamma/EgammaPhotonAlgos/interface/ConversionForwardEstimator.h"
00004 
00005 #include "RecoEgamma/EgammaPhotonAlgos/interface/ConversionFastHelix.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 
00008 // Field
00009 #include "MagneticField/Engine/interface/MagneticField.h"
00010 //
00011 #include "DataFormats/CLHEP/interface/AlgebraicObjects.h"
00012 // Geometry
00013 #include "Geometry/Records/interface/IdealGeometryRecord.h"
00014 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
00015 //
00016 #include "TrackingTools/TrajectoryParametrization/interface/GlobalTrajectoryParameters.h"
00017 #include "TrackingTools/DetLayers/interface/DetLayer.h"
00018 #include "TrackingTools/PatternTools/interface/TrajectoryMeasurement.h"
00019 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00020 #include "TrackingTools/TransientTrackingRecHit/interface/TransientTrackingRecHit.h"
00021 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
00022 //
00023 //
00024 #include "FWCore/Framework/interface/EventSetup.h"
00025 //
00026 #include "CLHEP/Units/GlobalPhysicalConstants.h"
00027 #include "CLHEP/Geometry/Point3D.h"
00028 
00029 InOutConversionSeedFinder::InOutConversionSeedFinder( const edm::ParameterSet& conf ):
00030   ConversionSeedFinder( conf ), conf_(conf)  
00031 {
00032   
00033   
00034   // std::cout << " InOutConversionSeedFinder CTOR " << "\n";      
00035     
00036   
00037   //the2ndHitdphi_ = 0.008; 
00038   the2ndHitdphi_ = 0.01; 
00039   the2ndHitdzConst_ = 5.;
00040   the2ndHitdznSigma_ = 2.;
00041   maxNumberOfInOutSeedsPerInputTrack_=50; 
00042   
00043 }
00044 
00045 
00046 
00047 InOutConversionSeedFinder::~InOutConversionSeedFinder() {
00048  //std::cout << " InOutConversionSeedFinder DTOR " << "\n";
00049 }
00050 
00051 
00052 
00053 void InOutConversionSeedFinder::makeSeeds( const edm::Handle<edm::View<reco::CaloCluster> > &  allBC )  const  {
00054   
00055 
00056  //std::cout << "  InOutConversionSeedFinder::makeSeeds() " << "\n";
00057   theSeeds_.clear();
00058  //std::cout << " Check Calo cluster collection size " << allBC->size() << "\n";  
00059   bcCollection_= allBC;
00060   
00061   
00062   findLayers();
00063   
00064   
00065   fillClusterSeeds();
00066   //std::cout << "Built vector of seeds of size  " << theSeeds_.size() <<  "\n" ;
00067   
00068   
00069   
00070   
00071   
00072 }
00073 
00074 
00075 void InOutConversionSeedFinder::fillClusterSeeds() const {
00076   
00077   std::vector<Trajectory>::const_iterator outInTrackItr;
00078   
00079  //std::cout << "  InOutConversionSeedFinder::fillClusterSeeds outInTracks_.size " << theOutInTracks_.size() << "\n";
00080   //Start looking for seeds for both of the 2 best tracks from the inward tracking
00081   
00083   /*
00084   for(outInTrackItr = theOutInTracks_.begin(); outInTrackItr != theOutInTracks_.end();  ++outInTrackItr) {
00085 
00086 
00087    //std::cout << " InOutConversionSeedFinder::fillClusterSeeds out in input track hits " << (*outInTrackItr).foundHits() << "\n";
00088     DetId tmpId = DetId( (*outInTrackItr).seed().startingState().detId());
00089     const GeomDet* tmpDet  = this->getMeasurementTracker()->geomTracker()->idToDet( tmpId );
00090     GlobalVector gv = tmpDet->surface().toGlobal( (*outInTrackItr).seed().startingState().parameters().momentum() );
00091 
00092 
00093    //std::cout << " InOutConversionSeedFinder::fillClusterSeed was built from seed position " <<gv   <<  " charge " << (*outInTrackItr).seed().startingState().parameters().charge() << "\n";
00094 
00095     Trajectory::DataContainer m=  outInTrackItr->measurements();
00096     int nHit=0;
00097     for (Trajectory::DataContainer::iterator itm = m.begin(); itm != m.end(); ++itm) {
00098       if ( itm->recHit()->isValid()  ) {
00099         nHit++;
00100         //std::cout << nHit << ")  Valid RecHit global position " << itm->recHit()->globalPosition() << " R " <<  itm->recHit()->globalPosition().perp() << " phi " << itm->recHit()->globalPosition().phi() << " eta " << itm->recHit()->globalPosition().eta() << "\n";
00101       } 
00102 
00103     }
00104 
00105   }
00106 
00107   */
00108 
00109 
00110   //Start looking for seeds for both of the 2 best tracks from the inward tracking
00111   for(outInTrackItr = theOutInTracks_.begin(); outInTrackItr != theOutInTracks_.end();  ++outInTrackItr) {
00112    //std::cout << " InOutConversionSeedFinder::fillClusterSeeds out in input track hits " << (*outInTrackItr).foundHits() << "\n";
00113     nSeedsPerInputTrack_=0;    
00114     
00115     //Find the first valid hit of the track
00116     // Measurements are ordered according to the direction in which the trajectories were built
00117     std::vector<TrajectoryMeasurement> measurements = (*outInTrackItr).measurements();
00118     
00119     std::vector<const DetLayer*> allLayers=layerList();
00120     
00121     //std::cout << "  InOutConversionSeedFinder::fill clusterSeed allLayers.size " <<  allLayers.size() << "\n";
00122     for(unsigned int i = 0; i < allLayers.size(); ++i) {
00123       //std::cout <<  " allLayers " << allLayers[i] << "\n"; 
00124       printLayer(i);
00125      }
00126     
00127     
00128     
00129     std::vector<const DetLayer*> myLayers;
00130     myLayers.clear();    
00131     std::vector<TrajectoryMeasurement>::reverse_iterator measurementItr;    
00132     std::vector<TrajectoryMeasurement*> myItr;
00133     // TrajectoryMeasurement* myPointer=0;
00134     myPointer=0;
00135     //std::cout << "  InOutConversionSeedFinder::fillClusterSeeds measurements.size " << measurements.size() <<"\n";
00136     
00137     for(measurementItr = measurements.rbegin() ; measurementItr != measurements.rend();  ++measurementItr) {
00138     
00139 
00140       if( (*measurementItr).recHit()->isValid()) {
00141         
00142         //std::cout << "  InOutConversionSeedFinder::fillClusterSeeds measurement on  layer  " << measurementItr->layer() <<   " " <<&(*measurementItr) <<  " position " << measurementItr->recHit()->globalPosition() <<   " R " << sqrt( measurementItr->recHit()->globalPosition().x()*measurementItr->recHit()->globalPosition().x() + measurementItr->recHit()->globalPosition().y()*measurementItr->recHit()->globalPosition().y() ) << " Z " << measurementItr->recHit()->globalPosition().z() << " phi " <<  measurementItr->recHit()->globalPosition().phi() << "\n";
00143         
00144         
00145         myLayers.push_back( measurementItr->layer() ) ; 
00146         myItr.push_back( &(*measurementItr) );
00147         
00148         
00149       }
00150     }
00151     
00152     
00153     
00154    //std::cout << " InOutConversionSeedFinder::fillClusterSeed myLayers.size " <<  myLayers.size() << "\n";
00155     //    for( unsigned int i = 0; i < myLayers.size(); ++i) {
00157     // }
00158     
00159     
00160     if ( myItr.size()==0 ) {
00161       //std::cout << "HORRENDOUS ERROR!  No meas on track!" << "\n";
00162     }      
00163     unsigned int ilayer;
00164     for(ilayer = 0; ilayer < allLayers.size(); ++ilayer) {
00165       //std::cout <<  " allLayers in the search loop  " << allLayers[ilayer] <<  " " << myLayers[0] <<  "\n"; 
00166       if ( allLayers[ilayer] == myLayers[0]) {
00167         
00168         myPointer=myItr[0];
00169         
00170         //std::cout <<  " allLayers in the search loop   allLayers[ilayer] == myLayers[0])  " << allLayers[ilayer] <<  " " << myLayers[0] <<  " myPointer " << myPointer << "\n"; 
00171         
00172         //std::cout  << "Layer " << ilayer << "  contains the first valid measurement " << "\n";        
00173         printLayer(ilayer);     
00174         
00175         if ( (myLayers[0])->location() == GeomDetEnumerators::barrel ) {
00176           //      const BarrelDetLayer * barrelLayer = dynamic_cast<const BarrelDetLayer*>(myLayers[0]);
00177          //std::cout << " InOutConversionSeedFinder::fillClusterSeeds  **** firstHit found in Barrel on layer " << ilayer  << " R= " << barrelLayer->specificSurface().radius() <<   "\n";
00178         } else {
00179           //const ForwardDetLayer * forwardLayer = dynamic_cast<const ForwardDetLayer*>(myLayers[0]);
00180          //std::cout << " InOutwardConversionSeedFinder::fillClusterSeeds  **** firstHit found in Forw on layer " << ilayer  << " Z= " << forwardLayer->specificSurface().position().z() <<  "\n";
00181         }
00182         
00183         
00184         break;
00185         
00186       } else if ( allLayers[ilayer] == myLayers[1] )  {
00187         myPointer=myItr[1];
00188         
00189         //std::cout <<  " allLayers in the search loop   allLayers[ilayer] == myLayers[1])  " << allLayers[ilayer] <<  " " << myLayers[1] <<  " myPointer " << myPointer << "\n"; 
00190         
00191         //std::cout << "Layer " << ilayer << "  contains the first innermost  valid measurement " << "\n";      
00192         if ( (myLayers[1])->location() == GeomDetEnumerators::barrel ) {
00193           //      const BarrelDetLayer * barrelLayer = dynamic_cast<const BarrelDetLayer*>(myLayers[1]);
00194          //std::cout << " InOutConversionSeedFinder::fillClusterSeeds  **** 2ndHit found in Barrel on layer " << ilayer  << " R= " << barrelLayer->specificSurface().radius() <<   "\n"; 
00195         } else {
00196           //const ForwardDetLayer * forwardLayer = dynamic_cast<const ForwardDetLayer*>(myLayers[1]);
00197          //std::cout << " InOutwardConversionSeedFinder::fillClusterSeeds  ****  2ndHitfound on forw layer " << ilayer  << " Z= " << forwardLayer->specificSurface().position().z() <<  "\n";
00198         }
00199         
00200         
00201         
00202         break;
00203         
00204       }
00205     }
00206     
00207     
00208     
00209     if(ilayer == allLayers.size()) {
00210      //std::cout << "InOutConversionSeedFinder::fillClusterSeeds ERROR could not find layer on list" <<  "\n"; 
00211       return;
00212     }
00213     
00214     //PropagatorWithMaterial reversePropagator(oppositeToMomentum, 0.000511, &(*theMF_) );
00215     FreeTrajectoryState * fts = myPointer->updatedState().freeTrajectoryState();
00216     
00217    //std::cout << " InOutConversionSeedFinder::fillClusterSeeds First FTS charge " << fts->charge() << " Position " << fts->position() << " momentum " << fts->momentum() << " R " << sqrt(fts->position().x()*fts->position().x() + fts->position().y()* fts->position().y() ) << " Z " << fts->position().z() << " phi " << fts->position().phi() << " fts parameters " << fts->parameters() << "\n";
00218     
00219     
00220     while (ilayer > 0) {
00221       
00222       //std::cout << " InOutConversionSeedFinder::fillClusterSeeds looking for 2nd seed from layer " << ilayer << "\n";
00223       
00224       //   if ( (allLayers[ilayer])->location() == GeomDetEnumerators::barrel ) {const BarrelDetLayer * barrelLayer = dynamic_cast<const BarrelDetLayer*>(allLayers[ilayer]);
00225       //std::cout <<  " InOutConversionSeedFinder::fillClusterSeeds  ****  Barrel on layer " << ilayer  << " R= " << barrelLayer->specificSurface().radius() <<  "\n";     
00226       // } else {
00227         //const ForwardDetLayer * forwardLayer = dynamic_cast<const ForwardDetLayer*>(allLayers[ilayer]);
00228         //std::cout <<  " InOutConversionSeedFinder::fillClusterSeeds  ****  Forw on layer " << ilayer  << " Z= " << forwardLayer->specificSurface().position().z() << "\n";
00229       //      }
00230       
00231      
00232      const DetLayer * previousLayer = allLayers[ilayer];
00233      TrajectoryStateOnSurface  stateAtPreviousLayer;
00234      //std::cout << " InOutConversionSeedFinder::fillClusterSeeds previousLayer->surface() position before  " <<allLayers[ilayer] << " " <<  previousLayer->surface().position() << " layer location " << previousLayer->location() << "\n";   
00235      // Propagate to the previous layer
00236      // The present layer is actually included in the loop so that a partner can be searched for
00237      // Applying the propagator to the same layer does not do any harm. It simply does nothing
00238      
00239      //     const Propagator& newProp=thePropagatorOppositeToMomentum_;  
00240      //std::cout << " InOutConversionSeedFinder::fillClusterSeeds reversepropagator direction " << thePropagatorOppositeToMomentum_->propagationDirection()  << "\n";
00241      if (ilayer-1>0) { 
00242        
00243        if ( allLayers[ilayer] == myLayers[0] ) {
00244          //std::cout << " innermost hit R " << myPointer->recHit()->globalPosition().perp() << " Z " << myPointer->recHit()->globalPosition().z() << " phi " <<myPointer->recHit()->globalPosition().phi() << "\n";
00245          //std::cout << " surface R " << theTrackerGeom_->idToDet(  myPointer->recHit() ->geographicalId())->surface().position().perp() <<  " Z " <<  theTrackerGeom_->idToDet(  myPointer->recHit() ->geographicalId())->surface().position().z() << " phi " << theTrackerGeom_->idToDet(  myPointer->recHit() ->geographicalId())->surface().position().phi() << "\n";
00246          
00247          stateAtPreviousLayer= thePropagatorOppositeToMomentum_->propagate(*fts,   theTrackerGeom_->idToDet(  myPointer->recHit() ->geographicalId())->surface()   );
00248          
00249        } else {
00250          
00251          stateAtPreviousLayer= thePropagatorOppositeToMomentum_->propagate(*fts, previousLayer->surface() );
00252          //std::cout << " InOutConversionSeedFinder::fillClusterSeeds previousLayer->surface() position after " << previousLayer->surface().position() << " layer location " << previousLayer->location() <<   "\n";
00253          
00254        }
00255        
00256      } else if ( ilayer-1==0) {
00257        
00258        
00261        
00262        //stateAtPreviousLayer= thePropagatorOppositeToMomentum_->propagate(*fts,   theTrackerGeom_->idToDet(  myPointer->recHit() ->geographicalId())->surface()   );
00263        stateAtPreviousLayer= thePropagatorOppositeToMomentum_->propagate(*fts, previousLayer->surface() );      
00264        
00265      }
00266      
00267      if(!stateAtPreviousLayer.isValid()) {
00268        //std::cout << "InOutConversionSeedFinder::fillClusterSeeds ERROR:could not propagate back to layer "  << ilayer << "\n";
00270      } else {
00271        //std::cout << "InOutConversionSeedFinder::fillClusterSeeds  stateAtPreviousLayer is valid.  Propagating back to layer "  << ilayer << "\n";
00272        //std::cout << "InOutConversionSeedFinder::fillClusterSeeds stateAtPreviousLayer R  " << stateAtPreviousLayer.globalPosition().perp()  << " Z " << stateAtPreviousLayer.globalPosition().z() << " phi " <<  stateAtPreviousLayer.globalPosition().phi() << "\n";
00273        
00274        startSeed(fts,  stateAtPreviousLayer, -1, ilayer ); 
00275        
00276        
00277      }      
00278      
00279      --ilayer;
00280      
00281     }
00282     
00283     if ( ilayer == 0) {
00284       
00285 
00286       //     if ( (allLayers[ilayer])->location() == GeomDetEnumerators::barrel ) {const BarrelDetLayer * barrelLayer = dynamic_cast<const BarrelDetLayer*>(allLayers[ilayer]);
00287       // //std::cout <<  " InOutConversionSeedFinder::fillClusterSeeds  ****  Barrel on layer " << ilayer  << " R= " << barrelLayer->specificSurface().radius() <<  "\n";     
00288       // } else {
00289       //const ForwardDetLayer * forwardLayer = dynamic_cast<const ForwardDetLayer*>(allLayers[ilayer]);
00290        //std::cout <<  " InOutConversionSeedFinder::fillClusterSeeds  ****  Forw on layer " << ilayer  << " Z= " << forwardLayer->specificSurface().position().z() << "\n";
00291       // }
00292      const DetLayer * previousLayer = allLayers[ilayer];
00293      TrajectoryStateOnSurface  stateAtPreviousLayer;
00294      stateAtPreviousLayer= thePropagatorOppositeToMomentum_->propagate(*fts, previousLayer->surface() );
00295 
00296      if(!stateAtPreviousLayer.isValid()) {
00297        //std::cout << "InOutConversionSeedFinder::fillClusterSeeds ERROR:could not propagate back to layer "  << ilayer << "\n";
00299      } else {
00300        //std::cout << "InOutConversionSeedFinder::fillClusterSeeds  stateAtPreviousLayer is valid.  Propagating back to layer "  << ilayer << "\n";
00301        //std::cout << "InOutConversionSeedFinder::fillClusterSeeds stateAtPreviousLayer R  " << stateAtPreviousLayer.globalPosition().perp()  << " Z " << stateAtPreviousLayer.globalPosition().z() << " phi " <<  stateAtPreviousLayer.globalPosition().phi() << "\n";
00302        
00303        startSeed(fts,  stateAtPreviousLayer, -1, ilayer ); 
00304      }     
00305        
00306      
00307    }
00308 
00309     
00310     
00311     
00312   }  // End loop over Out In tracks
00313   
00314   
00315   
00316 }
00317 
00318 
00319 
00320 void InOutConversionSeedFinder::startSeed( FreeTrajectoryState * fts, const TrajectoryStateOnSurface & stateAtPreviousLayer, int charge, int ilayer  )  const {
00321   
00322   //std::cout << "InOutConversionSeedFinder::startSeed ilayer " << ilayer <<  "\n";
00323   // Get a list of basic clusters that are consistent with a track 
00324   // starting at the assumed conversion point with opp. charge to the 
00325   // inward track.  Loop over these basic clusters.
00326   track2Charge_ = charge*fts->charge();
00327   std::vector<const reco::CaloCluster*> bcVec;
00328  //std::cout << "InOutConversionSeedFinder::startSeed charge assumed for the in-out track  " << track2Charge_ <<  "\n";
00329   
00330   Geom::Phi<float> theConvPhi( stateAtPreviousLayer.globalPosition().phi());
00331  //std::cout << "InOutConversionSeedFinder::startSeed  stateAtPreviousLayer phi " << stateAtPreviousLayer.globalPosition().phi() << " R " <<  stateAtPreviousLayer.globalPosition().perp() << " Z " << stateAtPreviousLayer.globalPosition().z() << "\n";
00332   
00333   bcVec = getSecondCaloClusters(stateAtPreviousLayer.globalPosition(),track2Charge_);
00334   
00335   std::vector<const reco::CaloCluster*>::iterator bcItr;
00336  //std::cout << "InOutConversionSeedFinder::startSeed bcVec.size " << bcVec.size() << "\n";
00337   
00338   // debug
00339   //  for(bcItr = bcVec.begin(); bcItr != bcVec.end(); ++bcItr) {
00340   //  //std::cout << "InOutConversionSeedFinder::startSeed list of  bc eta " << (*bcItr)->position().eta() << " phi " << (*bcItr)->position().phi() << " x " << (*bcItr)->position().x() << " y " << (*bcItr)->position().y() << " z " << (*bcItr)->position().z() << "\n";
00341   // }
00342   
00343 
00344   for(bcItr = bcVec.begin(); bcItr != bcVec.end(); ++bcItr) {
00345     
00346     theSecondBC_ = **bcItr;
00347     GlobalPoint bcPos((theSecondBC_.position()).x(),
00348                       (theSecondBC_.position()).y(),
00349                       (theSecondBC_.position()).z());
00350     
00351    //std::cout << "InOutConversionSeedFinder::startSeed for  bc position x " << bcPos.x() << " y " <<  bcPos.y() << " z  " <<  bcPos.z() << " eta " <<  bcPos.eta() << " phi " <<  bcPos.phi() << "\n";
00352     GlobalVector dir = stateAtPreviousLayer.globalDirection();
00353     GlobalPoint back1mm = stateAtPreviousLayer.globalPosition();
00354    //std::cout << "InOutConversionSeedFinder::startSeed   stateAtPreviousLayer.globalPosition() " << back1mm << "\n";
00355     
00356     back1mm -= dir.unit()*0.1;
00357     //std::cout << " InOutConversionSeedFinder:::startSeed going to make the helix using back1mm " << back1mm <<"\n";
00358     ConversionFastHelix helix(bcPos, stateAtPreviousLayer.globalPosition(), back1mm, &(*theMF_));
00359     helix.stateAtVertex();    
00360     
00361     //std::cout << " InOutConversionSeedFinder:::startSeed helix status " <<helix.isValid() << std::endl; 
00362     if ( !helix.isValid()  ) continue;
00363     findSeeds(stateAtPreviousLayer, helix.stateAtVertex().transverseCurvature(), ilayer);
00364     
00365     
00366   }
00367   
00368   
00369   
00370 }
00371 
00372 
00373 
00374 std::vector<const reco::CaloCluster*> InOutConversionSeedFinder::getSecondCaloClusters(const GlobalPoint & conversionPosition, float charge) const {
00375   
00376   
00377   std::vector<const reco::CaloCluster*> result;
00378   
00379  //std::cout << "InOutConversionSeedFinder::getSecondCaloClusters" <<  "\n"; 
00380   
00381   Geom::Phi<float> theConvPhi(conversionPosition.phi() );
00382 
00383   for (unsigned i = 0; i < bcCollection_->size(); ++i ) {
00384     
00385     Geom::Phi<float> theBcPhi( bcCollection_->ptrAt(i)->position().phi()   );
00386    //std::cout<< "InOutConversionSeedFinder::getSecondCaloClusters  BC energy " <<  bcCollection_->ptrAt(i)->energy() << " Calo cluster phi " << theBcPhi << " " <<  bcCollection_->ptrAt(i)->position().phi()<<  " theConvPhi " << theConvPhi << "\n";
00387     
00388     // Require phi of cluster to be consistent with the conversion 
00389     // position and the track charge
00390     
00391     
00392     if (fabs(theBcPhi-theConvPhi ) < .5 &&
00393         ((charge<0 && theBcPhi-theConvPhi >-.5) || 
00394          (charge>0 && theBcPhi-theConvPhi <.5))){
00396       
00397       //result.push_back(&(*bcItr));
00398 
00399       result.push_back(&(*(bcCollection_->ptrAt(i))  ));
00400 
00401     }
00402     
00403     
00404     
00405     
00406   }
00407   
00408   
00409   
00410   return result;
00411   
00412   
00413 }
00414 
00415 
00416 
00417 void InOutConversionSeedFinder::findSeeds(const TrajectoryStateOnSurface & startingState,
00418                                           float transverseCurvature, 
00419                                           unsigned int startingLayer) const {
00420   
00421   
00422   std::vector<const DetLayer*> allLayers=layerList();
00423  //std::cout << "InOutConversionSeedFinder::findSeeds starting forward propagation from  startingLayer " << startingLayer <<  "\n"; 
00424   
00425   
00426   // create error matrix
00427   AlgebraicSymMatrix55 m = AlgebraicMatrixID();
00428   m(0,0) = 0.1; m(1,1) = 0.0001 ; m(2,2) = 0.0001 ;
00429   m(3,3) = 0.0001 ; m(4,4) = 0.001;
00430 
00431   // Make an FTS consistent with the start point, start direction and curvature
00432   FreeTrajectoryState fts(GlobalTrajectoryParameters(startingState.globalPosition(), 
00433                                                      startingState.globalDirection(),
00434                                                      double(transverseCurvature), 0, &(*theMF_) ),
00435                           CurvilinearTrajectoryError(m));
00436  //std::cout << " InOutConversionSeedFinder::findSeeds startingState R "<< startingState.globalPosition().perp() << " Z " << startingState.globalPosition().z() << " phi " <<  startingState.globalPosition().phi() <<  " position " << startingState.globalPosition() << "\n";
00437  //std::cout << " InOutConversionSeedFinder::findSeeds Initial FTS charge " << fts.charge() << " curvature " <<  transverseCurvature << "\n";  
00438  //std::cout << " InOutConversionSeedFinder::findSeeds Initial FTS parameters " << fts <<  "\n"; 
00439     
00440   
00441   //float dphi = 0.01;
00442   float dphi = 0.03;
00443   float zrange = 5.;
00444   for( unsigned int ilayer = startingLayer; ilayer <= startingLayer+1 && (ilayer < allLayers.size()-2); ++ilayer) {
00445     const DetLayer * layer = allLayers[ilayer];
00446     
00447     
00448     
00450     //    if ( layer->location() == GeomDetEnumerators::barrel ) {const BarrelDetLayer * barrelLayer = dynamic_cast<const BarrelDetLayer*>(layer);
00451     // //std::cout << "InOutConversionSeedFinder::findSeeds  ****  Barrel on layer " << ilayer  << " R= " << barrelLayer->specificSurface().radius() <<  "\n";     
00452     // } else {
00453     // const ForwardDetLayer * forwardLayer = dynamic_cast<const ForwardDetLayer*>(layer);
00455     // }
00456     // // end debug
00457     
00458     
00459 
00460     MeasurementEstimator * newEstimator=0;
00461     if (layer->location() == GeomDetEnumerators::barrel ) {
00462      //std::cout << "InOutConversionSeedFinder::findSeeds Barrel ilayer " << ilayer <<  "\n"; 
00463       newEstimator = new ConversionBarrelEstimator(-dphi, dphi, -zrange, zrange);
00464     }
00465     else {
00466      //std::cout << "InOutConversionSeedFinder::findSeeds Forward  ilayer " << ilayer <<  "\n"; 
00467       newEstimator = new ConversionForwardEstimator(-dphi, dphi, 15.);
00468     }
00469     
00470     
00471     theFirstMeasurements_.clear();
00472     // Get measurements compatible with the FTS and Estimator
00473     TSOS tsos(fts, layer->surface() );
00474     
00475    //std::cout << "InOutConversionSeedFinder::findSeed propagationDirection " << int(thePropagatorAlongMomentum_->propagationDirection() ) << "\n";               
00477     LayerMeasurements theLayerMeasurements_(this->getMeasurementTracker() );
00478 
00479     theFirstMeasurements_ = theLayerMeasurements_.measurements( *layer, tsos, *thePropagatorAlongMomentum_, *newEstimator);
00480     
00481     delete newEstimator;
00482     //std::cout <<  "InOutConversionSeedFinder::findSeeds  Found " << theFirstMeasurements_.size() << " first hits" << "\n";
00483     
00484     if ( theFirstMeasurements_.size() == 1 ) {      // only dummy hit found: start finding the seed from the innermost hit of the OutIn track
00485 
00486       
00487       GlobalPoint bcPos((theSecondBC_.position()).x(),(theSecondBC_.position()).y(),(theSecondBC_.position()).z());
00488       GlobalVector dir = startingState.globalDirection();
00489       GlobalPoint back1mm = myPointer->recHit()->globalPosition();
00490       
00491       back1mm -= dir.unit()*0.1;
00492       //std::cout << " InOutConversionSeedFinder:::findSeeds going to make the helix using back1mm " << back1mm << "\n";
00493       ConversionFastHelix helix(bcPos,  myPointer->recHit()->globalPosition(), back1mm, &(*theMF_));
00494       
00495       helix.stateAtVertex();
00496       //std::cout << " InOutConversionSeedFinder:::findSeeds helix status " <<helix.isValid() << std::endl; 
00497       if ( !helix.isValid()  ) continue;
00498       
00499       track2InitialMomentum_= helix.stateAtVertex().momentum();
00500                   
00501       // Make a new FTS
00502       FreeTrajectoryState newfts(GlobalTrajectoryParameters(
00503                                                             myPointer->recHit()->globalPosition(), startingState.globalDirection(),
00504                                                             helix.stateAtVertex().transverseCurvature(), 0, &(*theMF_)), 
00505                                  CurvilinearTrajectoryError(m));
00506       
00507       
00508       completeSeed(*myPointer, newfts,  thePropagatorAlongMomentum_, ilayer+1);
00509       completeSeed(*myPointer, newfts,  thePropagatorAlongMomentum_, ilayer+2);
00510       
00511       
00512     } else { 
00513       
00514       
00515       
00516       //Loop over compatible hits
00517       int mea=0;
00518       for(std::vector<TrajectoryMeasurement>::iterator tmItr = theFirstMeasurements_.begin(); tmItr !=theFirstMeasurements_.end();  ++tmItr) {
00519         
00520         mea++;
00521         
00522         
00523         if (tmItr->recHit()->isValid() ) {
00524           // Make a new helix as in fillClusterSeeds() but using the hit position
00525           //std::cout << "InOutConversionSeedFinder::findSeeds hit  R " << tmItr->recHit()->globalPosition().perp() << " Z " <<  tmItr->recHit()->globalPosition().z() << " " <<  tmItr->recHit()->globalPosition() << "\n";
00526           GlobalPoint bcPos((theSecondBC_.position()).x(),(theSecondBC_.position()).y(),(theSecondBC_.position()).z());
00527           GlobalVector dir = startingState.globalDirection();
00528           GlobalPoint back1mm = tmItr->recHit()->globalPosition();
00529           
00530           back1mm -= dir.unit()*0.1;
00531           //std::cout << " InOutConversionSeedFinder:::findSeeds going to make the helix using back1mm " << back1mm << "\n";
00532           ConversionFastHelix helix(bcPos,  tmItr->recHit()->globalPosition(), back1mm, &(*theMF_));
00533           
00534           helix.stateAtVertex();
00535           //std::cout << " InOutConversionSeedFinder:::findSeeds helix status " <<helix.isValid() << std::endl; 
00536           if ( !helix.isValid()  ) continue;
00537           
00538           track2InitialMomentum_= helix.stateAtVertex().momentum();
00539           
00540           //std::cout << "InOutConversionSeedFinder::findSeeds Updated estimatedPt = " << helix.stateAtVertex().momentum().perp()  << " curvature "  << helix.stateAtVertex().transverseCurvature() << "\n";
00541           //     << ", bcet = " << theBc->Et() 
00542           //     << ", estimatedPt/bcet = " << estimatedPt/theBc->Et() << endl;
00543           
00544           
00545           // Make a new FTS
00546           FreeTrajectoryState newfts(GlobalTrajectoryParameters(
00547                                                                 tmItr->recHit()->globalPosition(), startingState.globalDirection(),
00548                                                                 helix.stateAtVertex().transverseCurvature(), 0, &(*theMF_)), 
00549                                      CurvilinearTrajectoryError(m));
00550           
00551           //std::cout <<  "InOutConversionSeedFinder::findSeeds  new FTS charge " << newfts.charge() << "\n";
00552           
00553           
00554           /*
00555           // Soome diagnostic output
00556           // may be useful - comparission of the basic cluster position 
00557           // with the ecal impact position of the track
00558           TrajectoryStateOnSurface stateAtECAL
00559           = forwardPropagator.propagate(newfts, ECALSurfaces::barrel());
00560           if (!stateAtECAL.isValid() || abs(stateAtECAL.globalPosition().eta())>1.479) {
00561           if (startingState.globalDirection().eta() > 0.) {
00562           stateAtECAL = forwardPropagator.propagate(newfts, 
00563           ECALSurfaces::positiveEtaEndcap());
00564           } else {
00565           stateAtECAL = forwardPropagator.propagate(newfts, 
00566           ECALSurfaces::negativeEtaEndcap());
00567           }
00568           }
00569           GlobalPoint ecalImpactPosition = stateAtECAL.isValid() ? stateAtECAL.globalPosition() : GlobalPoint(0.,0.,0.);
00570           cout << "Projected fts positon at ECAL surface: " << 
00571           ecalImpactPosition << " bc position: " << theBc->Position() << endl;
00572           */
00573           
00574           
00575           completeSeed(*tmItr, newfts,  thePropagatorAlongMomentum_, ilayer+1);
00576           completeSeed(*tmItr, newfts,  thePropagatorAlongMomentum_, ilayer+2);
00577           
00578           
00579         }
00580         
00581       }
00582       
00583     }    
00584     
00585     
00586   }
00587   
00588   
00589   
00590 }
00591 
00592 
00593 
00594 
00595 
00596 void InOutConversionSeedFinder::completeSeed(const TrajectoryMeasurement & m1,
00597                                              FreeTrajectoryState & fts, const Propagator* propagator, int ilayer) const {
00598   
00599  //std::cout<<  "InOutConversionSeedFinder::completeSeed ilayer " << ilayer <<  "\n";
00600   // A seed is made from 2 Trajectory Measuremennts.  The 1st is the input
00601   // argument m1.  This routine looks for the 2nd measurement in layer ilayer
00602   // Begin by making a new much stricter MeasurementEstimator based on the
00603   // position errors of the 1st hit.
00604   printLayer(ilayer);
00605   
00606   MeasurementEstimator * newEstimator;
00607   std::vector<const DetLayer*> allLayers=layerList();
00608   const DetLayer * layer = allLayers[ilayer];
00609   
00611   //  if ( layer->location() == GeomDetEnumerators::barrel ) {const BarrelDetLayer * barrelLayer = dynamic_cast<const BarrelDetLayer*>(layer);
00612   // //std::cout << "InOutConversionSeedFinder::completeSeed  ****  Barrel on layer " << ilayer  << " R= " << barrelLayer->specificSurface().radius() <<  "\n";     
00613   // } else {
00614   // const ForwardDetLayer * forwardLayer = dynamic_cast<const ForwardDetLayer*>(layer);
00615   //  //std::cout << "InOutConversionSeedFinder::completeSeed ****  Forw on layer " << ilayer  << " Z= " << forwardLayer->specificSurface().position().z() <<  "\n"; 
00618   
00619   
00620 
00621 
00622   if (layer->location() == GeomDetEnumerators::barrel ) {
00623     
00624     float dz = sqrt(the2ndHitdznSigma_*the2ndHitdznSigma_*m1.recHit()->globalPositionError().czz()
00625                     + the2ndHitdzConst_*the2ndHitdzConst_);
00626     newEstimator = new ConversionBarrelEstimator(-the2ndHitdphi_, the2ndHitdphi_, -dz, dz);
00627     
00628   }
00629   else {
00630     float m1dr = sqrt(m1.recHit()->localPositionError().yy());
00631     float dr = sqrt(the2ndHitdznSigma_*the2ndHitdznSigma_*m1dr*m1dr
00632                     + the2ndHitdzConst_*the2ndHitdznSigma_);
00633     
00634     newEstimator =  new ConversionForwardEstimator(-the2ndHitdphi_, the2ndHitdphi_, dr);
00635   }
00636   
00637 
00638  //std::cout << "InOutConversionSeedFinder::completeSeed fts For the TSOS " << fts << "\n";   
00639   
00640   TSOS tsos(fts, layer->surface() );
00641 
00642   if ( !tsos.isValid() ) {
00643    //std::cout  << "InOutConversionSeedFinder::completeSeed TSOS is not valid " <<  "\n"; 
00644   } 
00645 
00646  //std::cout << "InOutConversionSeedFinder::completeSeed TSOS " << tsos << "\n";   
00647  //std::cout << "InOutConversionSeedFinder::completeSeed propagationDirection  " << int(propagator->propagationDirection() ) << "\n";               
00648  //std::cout << "InOutConversionSeedFinder::completeSeed pointer to estimator " << newEstimator << "\n";
00649   LayerMeasurements theLayerMeasurements_(this->getMeasurementTracker() );
00650   std::vector<TrajectoryMeasurement> measurements = theLayerMeasurements_.measurements( *layer, tsos, *propagator, *newEstimator);
00651  //std::cout << "InOutConversionSeedFinder::completeSeed Found " << measurements.size() << " second hits " <<  "\n"; 
00652   delete newEstimator;
00653   
00654   for(unsigned int i = 0; i < measurements.size(); ++i) {
00655     if( measurements[i].recHit()->isValid()  ) {
00656       createSeed(m1, measurements[i]);
00657     }
00658   }
00659   
00660   
00661   
00662   
00663   
00664   
00665 }
00666 
00667 
00668 
00669 void InOutConversionSeedFinder::createSeed(const TrajectoryMeasurement & m1,  const TrajectoryMeasurement & m2) const {
00670   
00671  //std::cout << "InOutConversionSeedFinder::createSeed " << "\n";
00672 
00673   if (  m1.predictedState().isValid() ) {  
00674   GlobalTrajectoryParameters newgtp(  m1.recHit()->globalPosition(), track2InitialMomentum_, track2Charge_, &(*theMF_) );
00675   CurvilinearTrajectoryError errors = m1.predictedState().curvilinearError();
00676   FreeTrajectoryState fts(newgtp, errors);
00677 
00678   TrajectoryStateOnSurface state1 = thePropagatorAlongMomentum_->propagate(fts,  m1.recHit()->det()->surface());
00679   
00680   /*
00681    //std::cout << "hit surface " <<  m1.recHit()->det()->surface().position() << "\n";
00682    //std::cout << "prop to " << typeid( m1.recHit()->det()->surface() ).name() <<"\n";
00683    //std::cout << "prop to first hit " << state1 << "\n"; 
00684    //std::cout << "update to " <<  m1.recHit()->globalPosition() << "\n";
00685   */
00686   
00687   
00688   
00689   
00690   if ( state1.isValid() ) {
00691     
00692     TrajectoryStateOnSurface updatedState1 = theUpdator_.update(state1,  *m1.recHit() );
00693     
00694     if ( updatedState1.isValid() ) {
00695       
00696       TrajectoryStateOnSurface state2 = thePropagatorAlongMomentum_->propagate(*updatedState1.freeTrajectoryState(),  m2.recHit()->det()->surface());
00697       
00698       if ( state2.isValid() ) {
00699         
00700         TrajectoryStateOnSurface updatedState2 = theUpdator_.update(state2, *m2.recHit() );
00701         TrajectoryMeasurement meas1(state1, updatedState1,  m1.recHit()  , m1.estimate(), m1.layer());
00702         TrajectoryMeasurement meas2(state2, updatedState2,  m2.recHit()  , m2.estimate(), m2.layer());
00703         
00704         edm::OwnVector<TrackingRecHit> myHits;
00705         myHits.push_back(meas1.recHit()->hit()->clone());
00706         myHits.push_back(meas2.recHit()->hit()->clone());
00707         
00708         //std::cout << "InOutConversionSeedFinder::createSeed new seed " << "\n";
00709         if ( nSeedsPerInputTrack_ >= maxNumberOfInOutSeedsPerInputTrack_ ) return;
00710         
00711 
00712         TrajectoryStateTransform tsTransform;
00713         PTrajectoryStateOnDet* ptsod= tsTransform.persistentState(state2, meas2.recHit()->hit()->geographicalId().rawId()  );
00714         //std::cout << "  InOutConversionSeedFinder::createSeed New seed parameters " << state2 << "\n";
00715        
00716         
00717        
00718         theSeeds_.push_back(TrajectorySeed( *ptsod, myHits, alongMomentum ));
00719         nSeedsPerInputTrack_++;
00720 
00721         delete ptsod;
00722        
00723         //std::cout << "InOutConversionSeedFinder::createSeed New seed hit 1 R " << m1.recHit()->globalPosition().perp() << "\n";
00724         //std::cout << "InOutConversionSeedFinder::createSeed New seed hit 2 R " << m2.recHit()->globalPosition().perp() << "\n";
00725         
00726         
00727         
00728         
00729       }
00730     }
00731   }
00732   }
00733 
00734   
00735 }