CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_4/src/RecoVertex/NuclearInteractionProducer/src/NuclearVertexBuilder.cc

Go to the documentation of this file.
00001 #include "RecoVertex/NuclearInteractionProducer/interface/NuclearVertexBuilder.h"
00002 #include "TrackingTools/TrajectoryState/interface/FreeTrajectoryState.h"
00003 #include "DataFormats/GeometryVector/interface/GlobalPoint.h"
00004 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
00005 #include "RecoVertex/VertexPrimitives/interface/TransientVertex.h"
00006 #include "RecoVertex/AdaptiveVertexFit/interface/AdaptiveVertexFitter.h"
00007 
00008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00009 
00010 void NuclearVertexBuilder::build( const reco::TrackRef& primTrack, std::vector<reco::TrackRef>& secTracks) {
00011 
00012      cleanTrackCollection(primTrack, secTracks); 
00013      std::sort(secTracks.begin(),secTracks.end(),cmpTracks());
00014      checkEnergy(primTrack, secTracks);
00015 
00016      if( secTracks.size() != 0) {
00017          if( FillVertexWithAdaptVtxFitter(primTrack, secTracks) ) return;
00018          else if( FillVertexWithCrossingPoint(primTrack, secTracks) ) return;
00019          else FillVertexWithLastPrimHit( primTrack, secTracks);
00020      }
00021      else {
00022        // if no secondary tracks : vertex position = position of last rechit of the primary track 
00023        FillVertexWithLastPrimHit( primTrack, secTracks);
00024      }
00025 }
00026           
00027 FreeTrajectoryState NuclearVertexBuilder::getTrajectory(const reco::TrackRef& track) const
00028 { 
00029   GlobalPoint position(track->vertex().x(),
00030                        track->vertex().y(),
00031                        track->vertex().z());
00032 
00033   GlobalVector momentum(track->momentum().x(),
00034                         track->momentum().y(),
00035                         track->momentum().z());
00036 
00037   GlobalTrajectoryParameters gtp(position,momentum,
00038                                  track->charge(),theMagField);
00039   
00040   FreeTrajectoryState fts(gtp);
00041 
00042   return fts; 
00043 } 
00044 
00045 void NuclearVertexBuilder::FillVertexWithLastPrimHit(const reco::TrackRef& primTrack, const std::vector<reco::TrackRef>& secTracks) {
00046    LogDebug("NuclearInteractionMaker") << "Vertex build from the end point of the primary track";
00047        the_vertex = reco::Vertex(reco::Vertex::Point(primTrack->outerPosition().x(),
00048                                          primTrack->outerPosition().y(),
00049                                          primTrack->outerPosition().z()),
00050                                          reco::Vertex::Error(), 0.,0.,0);
00051        the_vertex.add(reco::TrackBaseRef( primTrack ), 1.0);
00052        for( unsigned short i=0; i != secTracks.size(); i++) {
00053              the_vertex.add(reco::TrackBaseRef( secTracks[i] ), 0.0);
00054        }
00055 }
00056 
00057 bool NuclearVertexBuilder::FillVertexWithAdaptVtxFitter(const reco::TrackRef& primTrack, const std::vector<reco::TrackRef>& secTracks) {
00058          std::vector<reco::TransientTrack> transientTracks;
00059          transientTracks.push_back( theTransientTrackBuilder->build(primTrack));
00060          // get the secondary track with the max number of hits
00061          for( unsigned short i=0; i != secTracks.size(); i++ ) {
00062               transientTracks.push_back( theTransientTrackBuilder->build( secTracks[i]) );
00063          }
00064          if( transientTracks.size() == 1 ) return  false;
00065          AdaptiveVertexFitter AVF;
00066          try {
00067             TransientVertex tv = AVF.vertex(transientTracks);
00068             the_vertex = reco::Vertex(tv);
00069          }
00070          catch(VertexException& exception){
00071             // AdaptivevertexFitter does not work
00072             LogDebug("NuclearInteractionMaker") << exception.what() << "\n";
00073             return false;
00074          }
00075          catch( cms::Exception& exception){
00076             // AdaptivevertexFitter does not work
00077             LogDebug("NuclearInteractionMaker") << exception.what() << "\n";
00078             return false;
00079          }
00080          if( the_vertex.isValid() ) {
00081               LogDebug("NuclearInteractionMaker") << "Try to build from AdaptiveVertexFitter with " << the_vertex.tracksSize() << " tracks";
00082               return true;
00083            }
00084          else return false;
00085 }
00086 
00087 
00088 bool NuclearVertexBuilder::FillVertexWithCrossingPoint(const reco::TrackRef& primTrack, const std::vector<reco::TrackRef>& secTracks) {
00089             // get the secondary track with the max number of hits
00090             unsigned short maxHits = 0; int indice=-1;
00091             for( unsigned short i=0; i < secTracks.size(); i++) {
00092                    // Add references to daughters
00093                    unsigned short nhits = secTracks[i]->numberOfValidHits();
00094                    if( nhits > maxHits ) { maxHits = nhits; indice=i; }
00095             }
00096 
00097             // Closest points
00098             if(indice == -1) return false;
00099 
00100             ClosestApproachInRPhi* theApproach = closestApproach( primTrack, secTracks[indice]);
00101             GlobalPoint crossing = theApproach->crossingPoint();
00102             delete theApproach;
00103 
00104             // Create vertex (creation point)
00105             // TODO: add error
00106             the_vertex = reco::Vertex(reco::Vertex::Point(crossing.x(),
00107                                                           crossing.y(),
00108                                                           crossing.z()),
00109                                       reco::Vertex::Error(), 0.,0.,0);
00110 
00111             the_vertex.add(reco::TrackBaseRef( primTrack ), 1.0);
00112             for( unsigned short i=0; i < secTracks.size(); i++) {
00113                  if( i==indice ) the_vertex.add(reco::TrackBaseRef( secTracks[i] ), 1.0);
00114                  else the_vertex.add(reco::TrackBaseRef( secTracks[i] ), 0.0);
00115             }
00116             return true;
00117 }
00118 
00119 ClosestApproachInRPhi* NuclearVertexBuilder::closestApproach( const reco::TrackRef& primTrack, const reco::TrackRef& secTrack )  const{
00120             FreeTrajectoryState primTraj = getTrajectory(primTrack);
00121             ClosestApproachInRPhi *theApproach = new ClosestApproachInRPhi();
00122             FreeTrajectoryState secTraj = getTrajectory(secTrack);
00123             bool status = theApproach->calculate(primTraj,secTraj);
00124             if( status ) { return theApproach; }
00125             else { 
00126                    return NULL;
00127             }
00128 }
00129 
00130 bool NuclearVertexBuilder::isGoodSecondaryTrack( const reco::TrackRef& primTrack, const reco::TrackRef& secTrack ) const {
00131            ClosestApproachInRPhi* theApproach = closestApproach(primTrack, secTrack);
00132            bool result = false;
00133            if(theApproach)
00134              result = isGoodSecondaryTrack(secTrack, primTrack, theApproach->distance() , theApproach->crossingPoint());
00135            delete theApproach;
00136            return result;
00137 } 
00138 
00139 bool NuclearVertexBuilder::isGoodSecondaryTrack( const reco::TrackRef& secTrack, 
00140                                                  const reco::TrackRef& primTrack, 
00141                                                  const double& distOfClosestApp, 
00142                                                  const GlobalPoint& crossPoint ) const {
00143           float TRACKER_RADIUS=129;
00144           double pt2 = secTrack->pt();
00145           double Dpt2 = secTrack->ptError();
00146           double p1 = primTrack->p();
00147           double Dp1 = primTrack->qoverpError()*p1*p1;
00148           double p2 = secTrack->p();
00149           double Dp2 = secTrack->qoverpError()*p2*p2;
00150           std::cout << "1)" << distOfClosestApp << " < " << minDistFromPrim_ << " " << (distOfClosestApp < minDistFromPrim_)  << "\n";
00151           std::cout << "2)" << secTrack->normalizedChi2() << " < " << chi2Cut_ << " " << (secTrack->normalizedChi2() < chi2Cut_) << "\n";
00152           std::cout << "3)" << crossPoint.perp() << " < " << TRACKER_RADIUS << " " << (crossPoint.perp() < TRACKER_RADIUS) << "\n";
00153           std::cout << "4)" << (Dpt2/pt2) << " < " << DPtovPtCut_ << " " << ((Dpt2/pt2) < DPtovPtCut_)  << "\n";
00154           std::cout << "5)" << (p2-2*Dp2) << " < " << (p1+2*Dp1) << " " << ((p2-2*Dp2) < (p1+2*Dp1))<< "\n";
00155           if( distOfClosestApp < minDistFromPrim_ &&
00156               secTrack->normalizedChi2() < chi2Cut_ && 
00157               crossPoint.perp() < TRACKER_RADIUS && 
00158               (Dpt2/pt2) < DPtovPtCut_ &&
00159               (p2-2*Dp2) < (p1+2*Dp1)) return true;
00160           else return false;
00161 }
00162 
00163 
00164 bool NuclearVertexBuilder::isCompatible( const reco::TrackRef& secTrack ) const{
00165          
00166          reco::TrackRef primTrack = (*(the_vertex.tracks_begin())).castTo<reco::TrackRef>();
00167          ClosestApproachInRPhi *theApproach = closestApproach(primTrack, secTrack);
00168          bool result = false;
00169          if( theApproach ) {
00170            GlobalPoint crp = theApproach->crossingPoint();
00171            math::XYZPoint vtx = the_vertex.position();
00172            float dist = sqrt((crp.x()-vtx.x())*(crp.x()-vtx.x()) +
00173                         (crp.y()-vtx.y())*(crp.y()-vtx.y()) +
00174                         (crp.z()-vtx.z())*(crp.z()-vtx.z()));
00175 
00176            float distError = sqrt(the_vertex.xError()*the_vertex.xError() +
00177                                   the_vertex.yError()*the_vertex.yError() +
00178                                   the_vertex.zError()*the_vertex.zError());
00179  
00180            //std::cout << "Distance between Additional track and vertex =" << dist << " +/- " << distError << std::endl;
00181            // TODO : add condition on distance between last rechit of the primary and the first rec hit of the secondary
00182  
00183            result = (isGoodSecondaryTrack(secTrack, primTrack, theApproach->distance(), crp) 
00184                      && dist-distError<minDistFromVtx_);
00185          }
00186          delete theApproach;
00187          return result;
00188 }
00189 
00190 void NuclearVertexBuilder::addSecondaryTrack( const reco::TrackRef& secTrack ) {
00191         std::vector<reco::TrackRef> allSecondary;
00192         for( reco::Vertex::trackRef_iterator it=the_vertex.tracks_begin()+1; it != the_vertex.tracks_end(); it++) {
00193             allSecondary.push_back( (*it).castTo<reco::TrackRef>() );
00194         } 
00195         allSecondary.push_back( secTrack );
00196         build( (*the_vertex.tracks_begin()).castTo<reco::TrackRef>(), allSecondary );
00197 }
00198 
00199 void NuclearVertexBuilder::cleanTrackCollection( const reco::TrackRef& primTrack, 
00200                                                  std::vector<reco::TrackRef>& tC) const {
00201 
00202     // inspired from FinalTrackSelector (S. Wagner) modified by P. Janot
00203     LogDebug("NuclearInteractionMaker") << "cleanTrackCollection number of input tracks : " << tC.size();
00204    std::map<std::vector<reco::TrackRef>::const_iterator, std::vector<const TrackingRecHit*> > rh;
00205 
00206     // first remove bad quality tracks and create map
00207     std::vector<bool> selected(tC.size(), false);
00208     int i=0;
00209     for (std::vector<reco::TrackRef>::const_iterator track=tC.begin(); track!=tC.end(); track++){
00210        if( isGoodSecondaryTrack(primTrack, *track)) { 
00211             selected[i]=true;
00212             trackingRecHit_iterator itB = (*track)->recHitsBegin();
00213             trackingRecHit_iterator itE = (*track)->recHitsEnd();
00214             for (trackingRecHit_iterator it = itB;  it != itE; ++it) {
00215                const TrackingRecHit* hit = &(**it);
00216                rh[track].push_back(hit);
00217             }
00218         }
00219        i++;
00220     }
00221 
00222     // then remove duplicated tracks
00223     i=-1;
00224     for (std::vector<reco::TrackRef>::const_iterator track=tC.begin(); track!=tC.end(); track++){
00225       i++;
00226       int j=-1;
00227       for (std::vector<reco::TrackRef>::const_iterator track2=tC.begin(); track2!=tC.end(); track2++){
00228         j++;
00229         if ((!selected[j])||(!selected[i]))continue;
00230         if ((j<=i))continue;
00231         int noverlap=0;
00232         std::vector<const TrackingRecHit*>& iHits = rh[track];
00233         for ( unsigned ih=0; ih<iHits.size(); ++ih ) {
00234           const TrackingRecHit* it = iHits[ih];
00235           if (it->isValid()){
00236             std::vector<const TrackingRecHit*>& jHits = rh[track2];
00237             for ( unsigned ih2=0; ih2<jHits.size(); ++ih2 ) {
00238             const TrackingRecHit* jt = jHits[ih2];
00239               if (jt->isValid()){
00240                 const TrackingRecHit* kt = jt;
00241                 if ( it->sharesInput(kt,TrackingRecHit::some) )noverlap++;
00242                }
00243              }
00244           }
00245         }
00246         float fi=float(noverlap)/float((*track)->recHitsSize()); 
00247         float fj=float(noverlap)/float((*track2)->recHitsSize());
00248         if ((fi>shareFrac_)||(fj>shareFrac_)){
00249           if (fi<fj){
00250             selected[j]=false;
00251           }else{
00252             if (fi>fj){
00253               selected[i]=false;
00254             }else{
00255               if ((*track)->normalizedChi2() > (*track2)->normalizedChi2()){selected[i]=false;}else{selected[j]=false;}
00256             }//end fi > or = fj
00257           }//end fi < fj
00258         }//end got a duplicate
00259       }//end track2 loop
00260     }//end track loop
00261 
00262    std::vector< reco::TrackRef > newTrackColl;
00263    i=0;
00264    for (std::vector<reco::TrackRef>::const_iterator track=tC.begin(); track!=tC.end(); track++){
00265          if( selected[i] ) newTrackColl.push_back( *track );
00266          ++i;
00267    }
00268    tC = newTrackColl;
00269 }
00270 
00271 void NuclearVertexBuilder::checkEnergy( const reco::TrackRef& primTrack,
00272                                         std::vector<reco::TrackRef>& tC) const {
00273    float totalEnergy=0;
00274    for(size_t i=0; i< tC.size(); ++i) {
00275      totalEnergy += tC[i]->p();
00276    }
00277    if( totalEnergy > primTrack->p()+0.1*primTrack->p() ) {
00278            tC.pop_back();
00279            checkEnergy(primTrack,tC);
00280    }
00281 }