From 2abee0ce1dc8829e23484f1195c72e6f931eb904 Mon Sep 17 00:00:00 2001
From: Chengdong Fu <fucd@ihep.ac.cn>
Date: Mon, 27 Jun 2022 23:19:25 +0800
Subject: [PATCH] add protection

---
 .../Tracking/src/Clupatra/clupatra_new.cpp    | 21 ++++++++++++-------
 .../Tracking/src/Clupatra/clupatra_new.h      | 11 +++++-----
 2 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/Reconstruction/Tracking/src/Clupatra/clupatra_new.cpp b/Reconstruction/Tracking/src/Clupatra/clupatra_new.cpp
index c4ed1fed..9ecee564 100644
--- a/Reconstruction/Tracking/src/Clupatra/clupatra_new.cpp
+++ b/Reconstruction/Tracking/src/Clupatra/clupatra_new.cpp
@@ -1238,31 +1238,35 @@ start:
 		bool reverse_order =   ( std::abs( hf->first->pos.z() ) > std::abs( hb->first->pos.z()) + 3. ) ;
 
 		unsigned nHit = 0 ;
-
+		int code = 0;
 		if( reverse_order ){
 		  //std::cout << "It is true order" << std::endl;
 		  for( CluTrack::reverse_iterator it=clu->rbegin() ; it != clu->rend() ; ++it){
 		    edm4hep::TrackerHit ph = (*it)->first->edm4hepHit;
 		    trk->addHit(ph) ;
 		    ++nHit ;
-		    //std::cout  <<  "   hit  added  " <<  (*it)->first->edm4hepHit   << std::endl ;
+		    //std::cout  <<  "   hit  added  " <<  (*it)->first->edm4hepHit.id() << std::endl ;
 		  }
 		  
-		  trk->initialise( MarlinTrk::IMarlinTrack::forward ) ;
+		  code = trk->initialise( MarlinTrk::IMarlinTrack::forward ) ;
 
 		} else {
 		  //std::cout << "It is reverse order" << std::endl;
 		  for( CluTrack::iterator it=clu->begin() ; it != clu->end() ; ++it){
 		    edm4hep::TrackerHit ph = (*it)->first->edm4hepHit;
-		    trk->addHit(ph) ;
+		    if( trk->addHit(ph) == MarlinTrk::IMarlinTrack::success ){
+		      //std::cout << "   hit added  " <<  (*it)->first->edm4hepHit.id() << std::endl;
+		    }
+		    else{
+		      //std::cout << "   hit not added  " <<  (*it)->first->edm4hepHit.id() << std::endl;
+		    }
 		    ++nHit ;
-		    //std::cout <<  "   hit  added  "<<  (*it)->first->edm4hepHit   << std::endl ;
 		  }
 
-		  trk->initialise( MarlinTrk::IMarlinTrack::backward ) ;
+		  code = trk->initialise( MarlinTrk::IMarlinTrack::backward ) ;
 		}
 
-		int code = trk->fit(  maxChi2  ) ;
+		if( code != MarlinTrk::IMarlinTrack::error ) code = trk->fit(  maxChi2  ) ;
                 // for (auto hit : hitsInFit) std::cout << hit.first << std::endl;
 
 		if( code != MarlinTrk::IMarlinTrack::success ){
@@ -1270,7 +1274,8 @@ start:
 			std::cout << "  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> IMarlinTrkFitter :  problem fitting track "
 		          << " error code : " << MarlinTrk::errorCode( code )
 		          << std::endl ;
-
+			delete trk;
+			return 0;
 		}
 
 
diff --git a/Reconstruction/Tracking/src/Clupatra/clupatra_new.h b/Reconstruction/Tracking/src/Clupatra/clupatra_new.h
index 46247d0b..6ace630a 100644
--- a/Reconstruction/Tracking/src/Clupatra/clupatra_new.h
+++ b/Reconstruction/Tracking/src/Clupatra/clupatra_new.h
@@ -467,11 +467,12 @@ namespace clupatra_new{
 
 				// FIXME Mingrui debug
 				// streamlog_out( DEBUG3  )  << "               -- extrapolate TrackState : " << lcshort( ts )    << std::endl ;
-
-				edm4hep::TrackerHit ht = trk.getTrackerHits(0); 
-				//need to add a dummy hit to the track
-				mTrk->addHit( ht ) ;  // is this the right hit ??????????
-
+				// fucd: getTrackerHits(0) is possible to miss ILDVTrackHit
+				for(int ih=0;ih<nHit;ih++){
+				  edm4hep::TrackerHit ht = trk.getTrackerHits(ih);
+				  //need to add a dummy hit to the track
+				  if(mTrk->addHit( ht ) == MarlinTrk::IMarlinTrack::success) break;  // is this the right hit ??????????
+				}
 				mTrk->initialise( ts ,  _b ,  MarlinTrk::IMarlinTrack::backward ) ;
 
 				double deltaChi ;
-- 
GitLab