Skip to content
Snippets Groups Projects
Fitter.cc 22.7 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
#include "Tools/Fitter.h"

#include <algorithm>

//#include "marlin/Global.h"
//#include "UTIL/LCTrackerConf.h"
#include <UTIL/ILDConf.h>

#include <DD4hep/DD4hepUnits.h>
#include <DD4hep/Detector.h>

#include "TrackSystemSvc/HelixTrack.h"
#include "DataHelper/Navigation.h"
#include "Tools/KiTrackMarlinTools.h"

typedef std::vector<edm4hep::ConstTrackerHit> TrackerHitVec;
using namespace MarlinTrk;

// by fucd: 3.5->3.0 default, will be read from GeoSvc
// if compare to Marlin, should change to 3.5
float Fitter::_bField = 3.5;//later on overwritten with the value read by geo file

void Fitter::init_BField(){

  // B field from DD4hep
  /*
  dd4hep::Detector & lcdd = dd4hep::Detector::getInstance();
  const double pos[3]={0,0,0}; 
  double bFieldVec[3]={0,0,0}; 
  lcdd.field().magneticField(pos,bFieldVec); // get the magnetic field vector from DD4hep
  _bField = bFieldVec[2]/dd4hep::tesla; // z component at (0,0,0)    
  */

}

bool compare_TrackerHit_z( edm4hep::TrackerHit* a, edm4hep::TrackerHit* b ){
  return ( fabs(a->getPosition()[2]) < fabs( b->getPosition()[2]) ); //compare their z values
}

bool compare_TrackerHit_R( edm4hep::TrackerHit* a, edm4hep::TrackerHit* b ){
  double Rad_a2 = (a->getPosition()[0]*a->getPosition()[0]) + (a->getPosition()[1]*a->getPosition()[1]) ;
  double Rad_b2 = (b->getPosition()[0]*b->getPosition()[0]) + (b->getPosition()[1]*b->getPosition()[1]) ;
  
  return ( Rad_a2 < Rad_b2 ); //compare their radii
}

Fitter::Fitter( edm4hep::Track* track , MarlinTrk::IMarlinTrkSystem* trkSystem ): _trkSystem( trkSystem ){
  _trackerHits.clear();

  std::copy(track->trackerHits_begin(), track->trackerHits_end(), std::back_inserter(_trackerHits));
  //_trackerHits = track->getTrackerHits();

  fit();
}

Fitter::Fitter( edm4hep::Track* track , MarlinTrk::IMarlinTrkSystem* trkSystem, int VXDFlag ): _trkSystem( trkSystem ){
  _trackerHits.clear();
  std::copy(track->trackerHits_begin(), track->trackerHits_end(), std::back_inserter(_trackerHits));
  //_trackerHits = track->getTrackerHits();
  fitVXD();
}

Fitter::Fitter( std::vector<edm4hep::ConstTrackerHit> trackerHits , MarlinTrk::IMarlinTrkSystem* trkSystem ): _trkSystem( trkSystem ){
  _trackerHits = trackerHits;
  fit();
}

void Fitter::fitVXD(){
  //create the MarlinTrk
  _marlinTrk = _trkSystem->createTrack();

  /**********************************************************************************************/
  /*       Add the hits to the MarlinTrack                                                      */
  /**********************************************************************************************/
   
  // hits are in reverse order 
  std::sort( _trackerHits.begin(), _trackerHits.end(), KiTrackMarlin::compare_TrackerHit_R );
  // now at [0] is the hit with the smallest |z| and at [1] is the one with a bigger |z| and so on
  // So the direction of the hits when following the index from 0 on is:
  // from inside out: from the IP into the distance. 
  // (It is important to keep in mind, in which direction we fit, when using MarlinTrk)
  
  TrackerHitVec::iterator it;
     
  unsigned number_of_added_hits = 0;
  unsigned ndof_added = 0;
  std::vector< edm4hep::TrackerHit* > added_hits;
  std::vector< edm4hep::TrackerHit* > added_hits_2D;
  
  for( it = _trackerHits.begin() ; it != _trackerHits.end() ; ++it ) {
    edm4hep::TrackerHit* trkHit = Navigation::Instance()->GetTrackerHit((*it).getObjectID());
    bool isSuccessful = false; 
    
    if( UTIL::BitSet32( trkHit->getType() )[ UTIL::ILDTrkHitTypeBit::COMPOSITE_SPACEPOINT ]   ){ //it is a composite spacepoint
      //Split it up and hits to the MarlinTrk
      std::vector< edm4hep::TrackerHit* > rawHits;
      //const LCObjectVec rawObjects = trkHit->getRawHits();
      //for( unsigned k=0; k<rawObjects.size(); k++ ) rawHits.push_back( dynamic_cast< TrackerHit* >( rawObjects[k] ) );
      int nRawHit = trkHit->rawHits_size();
      for( unsigned k=0; k< nRawHit; k++ ){
	edm4hep::TrackerHit* rawHit = Navigation::Instance()->GetTrackerHit(trkHit->getRawHits(k));
	rawHits.push_back(rawHit);
      }
      std::sort( rawHits.begin(), rawHits.end(), compare_TrackerHit_R );
      
      for( unsigned k=0; k< rawHits.size(); k++ ){
	if( _marlinTrk->addHit( rawHits[k] ) == IMarlinTrack::success ){
	  isSuccessful = true; //if at least one hit from the spacepoint gets added
	  ++ndof_added; // 1 degree of freedom for each strip hit
	}
	else{
	  //std::cout << "Cannot addHit " << rawHits[k]->id() << " to MarlinTrk" << std::endl; 
	}
      }
    }
    else { // normal non composite hit
      if (_marlinTrk->addHit( trkHit ) == 0) {
	isSuccessful = true;
	ndof_added += 2;
      }
    }
    
    if (isSuccessful) {
      added_hits.push_back(trkHit);
      ++number_of_added_hits;
    }
    else{
      //std::cout << "DEBUG Fitter::fit(): Hit " << it - _trackerHits.begin() << " Dropped " << std::endl;          
    }
  }
  
  if( ndof_added < 6 ) {
    std::stringstream s;
    s << "Fitter::fit(): Cannot fit less with less than 6 degrees of freedom. Number of hits =  " << number_of_added_hits << " ndof = " << ndof_added << "\n";
    
    throw FitterException( s.str() );
  }
   
  /**********************************************************************************************/
  /*       Create a helix from the first, last and middle hit                                   */
  /**********************************************************************************************/
  
  for (unsigned ihit=0; ihit <added_hits.size(); ++ihit) {
    // check if this a space point or 2D hit 
    if(UTIL::BitSet32( added_hits[ihit]->getType() )[ UTIL::ILDTrkHitTypeBit::ONE_DIMENSIONAL ] == false ){
      // then add to the list 
      added_hits_2D.push_back(added_hits[ihit]);
      
    }
  }
  
  // initialise with space-points not strips 
  // make a helix from 3 hits to get a trackstate
  const edm4hep::Vector3d x1 = added_hits_2D[0]->getPosition();
  const edm4hep::Vector3d x2 = added_hits_2D[ added_hits_2D.size()/2 ]->getPosition();
  const edm4hep::Vector3d x3 = added_hits_2D.back()->getPosition();   
  
  init_BField();
  HelixTrack helixTrack( x1, x2, x3, _bField, HelixTrack::forwards );
  
  helixTrack.moveRefPoint(0.0, 0.0, 0.0);
  
  //const float referencePoint[3] = { float(helixTrack.getRefPointX()) , float(helixTrack.getRefPointY() ), float(helixTrack.getRefPointZ() )};
  edm4hep::Vector3f referencePoint = { float(helixTrack.getRefPointX()) , float(helixTrack.getRefPointY() ), float(helixTrack.getRefPointZ() )};
  
  /**********************************************************************************************/
  /*       Create a TrackStateImpl from the helix values and use it to initalise the fit        */
  /**********************************************************************************************/
  std::array<float,15> covMatrix;
  
  for (unsigned icov = 0; icov<covMatrix.size(); ++icov) {
    covMatrix[icov] = 0;
  }
  
  covMatrix[0]  = ( 1.e6 ); //sigma_d0^2
  covMatrix[2]  = ( 1.e2 ); //sigma_phi0^2
  covMatrix[5]  = ( 1.e-4 ); //sigma_omega^2
  covMatrix[9]  = ( 1.e6 ); //sigma_z0^2
  covMatrix[14] = ( 1.e2 ); //sigma_tanl^2
  
  
  edm4hep::TrackState trackState = {0/*TrackState::AtOther*/, 
				    helixTrack.getD0(), 
				    helixTrack.getPhi0(), 
				    helixTrack.getOmega(), 
				    helixTrack.getZ0(), 
				    helixTrack.getTanLambda(),
				    referencePoint,
				    covMatrix};
  
  //init_BField();
  _marlinTrk->initialise( trackState, _bField, IMarlinTrack::backward ) ;
  
  //     _marlinTrk->initialise( IMarlinTrack::backward ) ;
   
  /**********************************************************************************************/
  /*       Do the fit                                                                           */
  /**********************************************************************************************/
  
  int fit_status = 0;
  
  try{
    
    fit_status = _marlinTrk->fit() ; 
    
  }
  catch( MarlinTrk::Exception& e ){
    
    std::stringstream s;
    s << "Fitter::fit(): Couldn't fit, MarlinTrk->fit() gave: " << e.what() << "\n";
    throw FitterException( s.str() );
    
  }
  
  if( fit_status != IMarlinTrack::success ){ 
    
    std::stringstream s;
    s << "Fitter::fit(): MarlinTrk->fit() wasn't successful, fit_status = " << fit_status << "\n";
    throw FitterException( s.str() );
    
  }
  
  
  // fitting finished get hits in the fit for safety checks:
  
  std::vector<std::pair<edm4hep::TrackerHit*, double> > hits_in_fit;
  
  // remember the hits are ordered in the order in which they were fitted
  // here we are fitting inwards so the first is the last and vice verse
  
  _marlinTrk->getHitsInFit(hits_in_fit);
  
  if( hits_in_fit.size() < 3 ) {
    
    
    std::stringstream s;
    s << "Fitter::fit() Less than 3 hits in fit: Only " << hits_in_fit.size() << 
      " of " << _trackerHits.size() << " hits\n";
    
    throw FitterException( s.str() );
    
  }
  edm4hep::TrackerHit* first_hit_in_fit = hits_in_fit.back().first;
  if (!first_hit_in_fit) {
    throw FitterException( std::string("Fitter::fit(): TrackerHit pointer to first hit == NULL ")  ) ;
  }
  
  
  edm4hep::TrackerHit* last_hit_in_fit = hits_in_fit.front().first;
  if (!last_hit_in_fit) {
    throw FitterException( std::string("Fitter::fit(): TrackerHit pointer to last hit == NULL ")  ) ;
  }
  
  return;
}



void Fitter::fit(){
  //create the MarlinTrk
  _marlinTrk = _trkSystem->createTrack();
  
  /**********************************************************************************************/
  /*       Add the hits to the MarlinTrack                                                      */
  /**********************************************************************************************/
  
  // hits are in reverse order 
  std::sort( _trackerHits.begin(), _trackerHits.end(), KiTrackMarlin::compare_TrackerHit_z );
  // now at [0] is the hit with the smallest |z| and at [1] is the one with a bigger |z| and so on
  // So the direction of the hits when following the index from 0 on is:
  // from inside out: from the IP into the distance. 
  // (It is important to keep in mind, in which direction we fit, when using MarlinTrk)
  
  TrackerHitVec::iterator it;
  
  unsigned number_of_added_hits = 0;
  unsigned ndof_added = 0;
  std::vector<edm4hep::TrackerHit*> added_hits;
  
  for( it = _trackerHits.begin() ; it != _trackerHits.end() ; ++it ) {
    edm4hep::TrackerHit* trkHit = Navigation::Instance()->GetTrackerHit((*it).getObjectID());
    bool isSuccessful = false; 
    //std::cout << "Hit " << trkHit->id() << " " << trkHit->getPosition() << std::endl;
    if( UTIL::BitSet32( trkHit->getType() )[ UTIL::ILDTrkHitTypeBit::COMPOSITE_SPACEPOINT ]   ){ //it is a composite spacepoint
      //Split it up and hits to the MarlinTrk
      std::vector<edm4hep::TrackerHit*> rawHits;
      //const LCObjectVec rawObjects = trkHit->getRawHits();                    
      //for( unsigned k=0; k<rawObjects.size(); k++ ) rawHits.push_back( dynamic_cast< TrackerHit* >( rawObjects[k] ) );
      int nRawHit = trkHit->rawHits_size();
      for( unsigned k=0; k< nRawHit; k++ ){
	edm4hep::TrackerHit* rawHit = Navigation::Instance()->GetTrackerHit(trkHit->getRawHits(k));
	//std::cout << "Raw Hit " << rawHit->id() << " " << rawHit->getPosition() << std::endl;
	rawHits.push_back(rawHit);
      }
      std::sort( rawHits.begin(), rawHits.end(), compare_TrackerHit_z );
      
      for( unsigned k=0; k< rawHits.size(); k++ ){
	if( _marlinTrk->addHit( rawHits[k] ) == IMarlinTrack::success ){
	   isSuccessful = true; //if at least one hit from the spacepoint gets added
	   ++ndof_added; // 1 degree of freedom for each strip hit
	}
	else{
	  //std::cout << "Cannot addHit " << rawHits[k]->id() << " to MarlinTrk" << std::endl;
	}
      }
    }
    else { // normal non composite hit
      
      if (_marlinTrk->addHit( trkHit ) == 0) {
	isSuccessful = true;
	ndof_added += 2;
      }
    }
    
    if (isSuccessful) {
      added_hits.push_back(trkHit);
      ++number_of_added_hits;
    }
    else{
      //std::cout << "DEBUG: Fitter::fit(): Hit " << it - _trackerHits.begin() << " Dropped " << std::endl;          
    }
  }
  
  if( ndof_added < 6 ) {
    std::stringstream s;
    s << "Fitter::fit(): Cannot fit less with less than 6 degrees of freedom. Number of hits =  " << number_of_added_hits << " ndof = " << ndof_added << "\n";
    
    throw FitterException( s.str() );
  }
   
  /**********************************************************************************************/
  /*       Create a helix from the first, last and middle hit                                   */
  /**********************************************************************************************/
    
  // initialise with space-points not strips 
  // make a helix from 3 hits to get a trackstate
  const edm4hep::Vector3d x1 = added_hits[0]->getPosition();
  const edm4hep::Vector3d x2 = added_hits[ added_hits.size()/2 ]->getPosition();
  const edm4hep::Vector3d x3 = added_hits.back()->getPosition();
  
  init_BField();
  HelixTrack helixTrack( x1, x2, x3, _bField, HelixTrack::forwards );
  
  helixTrack.moveRefPoint(0.0, 0.0, 0.0);
  
  //const float referencePoint[3] = { float(helixTrack.getRefPointX()) , float(helixTrack.getRefPointY()) , float(helixTrack.getRefPointZ()) };
  edm4hep::Vector3f referencePoint = { float(helixTrack.getRefPointX()) , float(helixTrack.getRefPointY()) , float(helixTrack.getRefPointZ()) };
  
  /**********************************************************************************************/
  /*       Create a TrackStateImpl from the helix values and use it to initalise the fit        */
  /**********************************************************************************************/
  std::array<float,15> covMatrix;
  for (unsigned icov = 0; icov<covMatrix.size(); ++icov) {
    covMatrix[icov] = 0;
  }
  
  covMatrix[0]  = ( 1.e6 ); //sigma_d0^2
  covMatrix[2]  = ( 1.e2 ); //sigma_phi0^2
  covMatrix[5]  = ( 1.e-4 ); //sigma_omega^2
  covMatrix[9]  = ( 1.e6 ); //sigma_z0^2
  covMatrix[14] = ( 1.e2 ); //sigma_tanl^2
  
  edm4hep::TrackState trackState = {0/*TrackState::AtOther*/, 
				    helixTrack.getD0(), 
				    helixTrack.getPhi0(), 
				    helixTrack.getOmega(), 
				    helixTrack.getZ0(), 
				    helixTrack.getTanLambda(), 
				    referencePoint,
				    covMatrix};
  
  //init_BField();
  _marlinTrk->initialise( trackState, _bField, IMarlinTrack::backward ) ;
  
  //     _marlinTrk->initialise( IMarlinTrack::backward ) ;
  
  /**********************************************************************************************/
  /*       Do the fit                                                                           */
  /**********************************************************************************************/
  int fit_status = 0;
  
  try{
    fit_status = _marlinTrk->fit() ; 
  }
  catch( MarlinTrk::Exception& e ){
    std::stringstream s;
    s << "Fitter::fit(): Couldn't fit, MarlinTrk->fit() gave: " << e.what() << "\n";
    throw FitterException( s.str() );
  }
   
  if( fit_status != IMarlinTrack::success ){ 
    std::stringstream s;
    s << "Fitter::fit(): MarlinTrk->fit() wasn't successful, fit_status = " << fit_status << "\n";
    throw FitterException( s.str() );
  }
  
  // fitting finished get hits in the fit for safety checks:
  
  std::vector<std::pair<edm4hep::TrackerHit*, double> > hits_in_fit;
  
  // remember the hits are ordered in the order in which they were fitted
  // here we are fitting inwards so the first is the last and vice verse
  
  _marlinTrk->getHitsInFit(hits_in_fit);
  
  if( hits_in_fit.size() < 3 ) {
    std::stringstream s;
    s << "Fitter::fit() Less than 3 hits in fit: Only " << hits_in_fit.size()
      << " of " << _trackerHits.size() << " hits\n";
    
    throw FitterException( s.str() );
  }
  edm4hep::TrackerHit* first_hit_in_fit = hits_in_fit.back().first;
  if (!first_hit_in_fit) {
    throw FitterException( std::string("Fitter::fit(): TrackerHit pointer to first hit == NULL ")  ) ;
  }
  
  edm4hep::TrackerHit* last_hit_in_fit = hits_in_fit.front().first;
  if (!last_hit_in_fit) {
    throw FitterException( std::string("Fitter::fit(): TrackerHit pointer to last hit == NULL ")  ) ;
  }
  
  return;
}
   
   
const edm4hep::TrackState* Fitter::getTrackState( int trackStateLocation ){
  return getTrackStatePlus( trackStateLocation )->getTrackState();
}

double Fitter::getChi2Prob( int trackStateLocation ){
  return ROOT::Math::chisquared_cdf_c( getChi2( trackStateLocation ) , getNdf( trackStateLocation ) );   
}

double Fitter::getChi2( int trackStateLocation ){
  return getTrackStatePlus( trackStateLocation )->getChi2();
}
   
int Fitter::getNdf( int trackStateLocation ){
   return getTrackStatePlus( trackStateLocation )->getNdf();
}
   
   
const TrackStatePlus* Fitter::getTrackStatePlus( int trackStateLocation ){
   // check if there is already an entry with this trackState location
   for( unsigned i=0; i<_trackStatesPlus.size(); i++ ){
     if( _trackStatesPlus[i]->getTrackState()->location == trackStateLocation ){
       return _trackStatesPlus[i];
     }
   }
   
   // If we reach this point, obviously no trackState with the given location has been created so far
   // Thus we create it now
   edm4hep::TrackState* trackState = new edm4hep::TrackState;
   int return_code = 0;
   double chi2;
   int ndf;
   switch( trackStateLocation ){
   case 1/*lcio::TrackState::AtIP*/:{
     const edm4hep::Vector3d point(0.,0.,0.); // nominal IP
              
     return_code = _marlinTrk->propagate(point, *trackState, chi2, ndf ) ;
         
     if (return_code != MarlinTrk::IMarlinTrack::success ) {
       delete trackState;
            
       std::stringstream s;
       s << "Fitter::getTrackStatePlus(): Couldn't create TrackState at IP, return code from propagation = " << return_code << "\n";
       throw FitterException( s.str() );
       
       break;
     }
     else{
       trackState->location = trackStateLocation;
       TrackStatePlus* trackStatePlus = new TrackStatePlus( trackState, chi2, ndf );
       _trackStatesPlus.push_back( trackStatePlus );
       return trackStatePlus;
     }
   }
   case 2/*lcio::TrackState::AtFirstHit*/:{
     std::vector<std::pair<edm4hep::TrackerHit*, double> > hits_in_fit;
         
     // remember the hits are ordered in the order in which they were fitted
     // here we are fitting inwards so the first is the last and vice verse
     _marlinTrk->getHitsInFit(hits_in_fit);
     
     edm4hep::TrackerHit* first_hit_in_fit = hits_in_fit.back().first;
          
     return_code = _marlinTrk->getTrackState(first_hit_in_fit, *trackState, chi2, ndf ) ;
     
     if(return_code !=MarlinTrk::IMarlinTrack::success){
       
       delete trackState;
       
       std::stringstream s;
       s << "Fitter::getTrackStatePlus(): Couldn't create TrackState at first hit, return code from propagation = " << return_code << "\n";
       throw FitterException( s.str() );
	    
       break;
     }
     else{
       trackState->location = trackStateLocation;
       TrackStatePlus* trackStatePlus = new TrackStatePlus( trackState, chi2, ndf );
       _trackStatesPlus.push_back( trackStatePlus );
       return trackStatePlus;
     }
   }
   case 3/*lcio::TrackState::AtLastHit*/:{
     std::vector<std::pair<edm4hep::TrackerHit*, double> > hits_in_fit;
     _marlinTrk->getHitsInFit(hits_in_fit);
     
     edm4hep::TrackerHit* last_hit_in_fit = hits_in_fit.front().first;
          
     return_code = _marlinTrk->getTrackState(last_hit_in_fit, *trackState, chi2, ndf ) ;
         
     if(return_code !=MarlinTrk::IMarlinTrack::success){
       delete trackState;
            
       std::stringstream s;
       s << "Fitter::getTrackStatePlus(): Couldn't create TrackState at last hit, return code from propagation = " << return_code << "\n";
       throw FitterException( s.str() );
       
       break;
     }
     else{
       trackState->location = trackStateLocation;
       TrackStatePlus* trackStatePlus = new TrackStatePlus( trackState, chi2, ndf );
       _trackStatesPlus.push_back( trackStatePlus );
       return trackStatePlus;
     }
     break;
   }
   case 4/*lcio::TrackState::AtCalorimeter*/:{
     std::vector<std::pair<edm4hep::TrackerHit*, double> > hits_in_fit;
     _marlinTrk->getHitsInFit(hits_in_fit);
     
     edm4hep::TrackerHit* last_hit_in_fit = hits_in_fit.front().first;
          
     UTIL::BitField64 encoder( UTIL::ILDCellID0::encoder_string ) ; 
     encoder.reset() ;  // reset to 0
         
     // ================== need to get the correct ID(s) for the calorimeter face  ============================
     unsigned ecal_barrel_face_ID = UTIL::ILDDetID::ECAL ;
     //unsigned ecal_endcap_face_ID = UTIL::ILDDetID::ECAL_ENDCAP ;
     //=========================================================================================================

     encoder[UTIL::ILDCellID0::subdet] =  ecal_barrel_face_ID ;
     encoder[UTIL::ILDCellID0::side]   = UTIL::ILDDetID::barrel;
     encoder[UTIL::ILDCellID0::layer]  = 0 ;
     
     int detElementID = 0;
     return_code = _marlinTrk->propagateToLayer(encoder.lowWord(), last_hit_in_fit, *trackState, chi2, ndf, detElementID, IMarlinTrack::modeForward ) ;
     
     if (return_code == MarlinTrk::IMarlinTrack::no_intersection ) { // try forward or backward
       //encoder[UTIL::ILDCellID0::subdet] = ecal_endcap_face_ID ;
       
       const edm4hep::TrackState* trkStateLastHit = getTrackStatePlus( 3/*lcio::TrackState::AtLastHit*/ )->getTrackState();
       
       if (trkStateLastHit->tanLambda>0) {
	 encoder[UTIL::ILDCellID0::side] = UTIL::ILDDetID::fwd;
       }
       else{
	 encoder[UTIL::ILDCellID0::side] = UTIL::ILDDetID::bwd;
       }
       return_code = _marlinTrk->propagateToLayer(encoder.lowWord(), last_hit_in_fit, *trackState, chi2, ndf, detElementID, IMarlinTrack::modeForward ) ;
     }
                 
     if(return_code !=MarlinTrk::IMarlinTrack::success){
       delete trackState;
            
       std::stringstream s;
       s << "Fitter::getTrackStatePlus(): Couldn't create TrackState at Calorimeter, return code from propagation = " << return_code << "\n";
       throw FitterException( s.str() );
       break;
     }
     else{
       trackState->location = trackStateLocation;
       TrackStatePlus* trackStatePlus = new TrackStatePlus( trackState, chi2, ndf );
       _trackStatesPlus.push_back( trackStatePlus );
       return trackStatePlus;
     }
   }
   default:{
     std::stringstream s;
     s << "Creation of a trackState for the given location " << trackStateLocation 
       << " is not yet implemented for the class Fitter. \nImplemented are: AtIP, AtFirstHit, AtLastHit, AtCalorimeter.\n"
       << "If another location is desired, it must be implemented in the method Fitter::getTrackStatePlus.\n";
     
     throw FitterException( s.str() );
     return NULL;
   }
   }
      
   return NULL; // if we haven't returned so far, there was no success, so we return NULL
}