1 #include "StFwdTrackMaker/StFwdTrackMaker.h"
2 #include "StFwdTrackMaker/include/Tracker/FwdHit.h"
3 #include "StFwdTrackMaker/include/Tracker/FwdTracker.h"
4 #include "StFwdTrackMaker/include/Tracker/TrackFitter.h"
5 #include "StFwdTrackMaker/include/Tracker/FwdGeomUtils.h"
6 #include "StFwdTrackMaker/include/Tracker/ObjExporter.h"
8 #include "KiTrack/IHit.h"
9 #include "GenFit/Track.h"
19 #include "StBFChain/StBFChain.h"
21 #include "StEvent/StEvent.h"
22 #include "StEvent/StGlobalTrack.h"
23 #include "StEvent/StHelixModel.h"
24 #include "StEvent/StPrimaryTrack.h"
25 #include "StEvent/StRnDHit.h"
26 #include "StEvent/StRnDHitCollection.h"
27 #include "StEvent/StTrack.h"
28 #include "StEvent/StBTofCollection.h"
29 #include "StEvent/StBTofHeader.h"
30 #include "StEvent/StTrackGeometry.h"
31 #include "StEvent/StTrackNode.h"
32 #include "StEvent/StPrimaryVertex.h"
34 #include "StEvent/StTrackDetectorInfo.h"
35 #include "StEvent/StFttPoint.h"
36 #include "StEvent/StFcsHit.h"
37 #include "StEvent/StFcsCluster.h"
38 #include "StEvent/StFttCollection.h"
39 #include "StEvent/StFcsCollection.h"
40 #include "StEvent/StTriggerData.h"
41 #include "StEvent/StFstHitCollection.h"
42 #include "StEvent/StFstHit.h"
43 #include "StEvent/StFwdTrackCollection.h"
44 #include "StChain/StChainOpt.h"
46 #include "StEventUtilities/StEventHelper.h"
48 #include "StMcEvent/StMcEvent.hh"
49 #include "StMcEvent/StMcVertex.hh"
51 #include "tables/St_g2t_fts_hit_Table.h"
52 #include "tables/St_g2t_track_Table.h"
53 #include "tables/St_g2t_vertex_Table.h"
54 #include "tables/St_g2t_event_Table.h"
56 #include "StarMagField/StarMagField.h"
58 #include "St_base/StMessMgr.h"
59 #include "StarClassLibrary/StPhysicalHelix.hh"
60 #include "StarClassLibrary/SystemOfUnits.h"
62 #include <SystemOfUnits.h>
66 #include "TLorentzVector.h"
68 #include "StRoot/StEpdUtil/StEpdGeom.h"
69 #include "StFcsDbMaker/StFcsDb.h"
70 #include "StFstUtil/StFstCollection.h"
72 #include "StEvent/StFwdTrack.h"
73 #include "GenFit/AbsMeasurement.h"
75 #include "StMuDSTMaker/COMMON/StMuDstMaker.h"
76 #include "StMuDSTMaker/COMMON/StMuDst.h"
77 #include "StMuDSTMaker/COMMON/StMuFstCollection.h"
78 #include "StMuDSTMaker/COMMON/StMuFstHit.h"
79 #include "StMuDSTMaker/COMMON/StMuPrimaryVertex.h"
81 #include "sys/types.h"
82 #include "sys/sysinfo.h"
84 FwdSystem* FwdSystem::sInstance =
nullptr;
91 template<
typename T>
static bool accept( T ) {
return true; }
95 template<>
bool GenfitUtils::accept( genfit::Track *
track )
99 if (track->getNumPoints() <= 0 )
return false;
101 auto cardinal = track->getCardinalRep();
104 auto status = track->getFitStatus( cardinal );
105 if ( !status->isFitConverged() ) {
112 for (
auto point : track->getPoints() ) {
113 if ( !point->hasFitterInfo(cardinal) ) {
131 genfit::TrackPoint* first =
nullptr;
132 unsigned int ipoint = 0;
133 for ( ipoint = 0; ipoint < track->getNumPoints(); ipoint++ ) {
134 first = track->getPointWithFitterInfo( ipoint );
140 LOG_WARN <<
"No fit information on fwd genfit track" << endm;
144 auto& fittedState= track->getFittedState(ipoint);
146 TVector3 momentum = fittedState.getMom();
147 double pt = momentum.Perp();
149 if (pt < 0.10 )
return false;
163 mRasterR = cfg.
get<
double>(
"SiRasterizer:r", 3.0);
164 mRasterPhi = cfg.
get<
double>(
"SiRasterizer:phi", 0.004);
168 return cfg.
get<
bool>(
"SiRasterizer:active",
true);
171 TVector3 raster(TVector3 p0) {
174 double phi = p.Phi();
175 const double minR = 5.0;
177 p.SetPerp(minR + (std::floor((r - minR) / mRasterR) * mRasterR + mRasterR / 2.0));
178 p.SetPhi(-TMath::Pi() + (std::floor((phi + TMath::Pi()) / mRasterPhi) * mRasterPhi + mRasterPhi / 2.0));
183 double mRasterR, mRasterPhi;
199 mTrackFitter->
setup();
206 if (FwdSystem::sInstance){
207 delete FwdSystem::sInstance;
208 FwdSystem::sInstance = 0;
218 StFwdTrackMaker::StFwdTrackMaker() :
StMaker(
"fwdTrack"), mEventVertex(0,0,0), mForwardTracker(nullptr), mForwardData(nullptr), mGeoCache(
""){
221 mEventVertexCov.ResizeTo(3, 3);
222 mEventVertexCov.Zero();
227 SetAttr(
"config",
"config.xml");
228 SetAttr(
"fillEvent",1);
231 configLoaded =
false;
235 setOutputFilename(
"stfwdtrackmaker_data.root" );
241 mForwardTracker->finish();
245 void StFwdTrackMaker::LoadConfiguration() {
246 if (mConfigFile.length() < 5){
249 mFwdConfig.
load( defaultConfig,
true );
251 LOG_INFO <<
"Forward Tracker is using config from file : " << mConfigFile << endm;
252 mFwdConfig.
load( mConfigFile );
260 if ( mGeoCache ==
"" ){
262 GetDataBase(
"VmcGeometry");
264 mGeoCache = GetChainOpt()->GetFileOut();
266 mGeoCache = GetChainOpt()->GetFileIn();
269 mGeoCache = mGeoCache.ReplaceAll(
"@",
"");
271 mGeoCache = mGeoCache( 0, mGeoCache.Last(
'.') );
273 mGeoCache+=
".geom.root";
276 mSiRasterizer = std::shared_ptr<SiRasterizer>(
new SiRasterizer(mFwdConfig));
277 mForwardTracker = std::shared_ptr<ForwardTracker>(
new ForwardTracker( ));
278 mForwardTracker->setConfig(mFwdConfig);
281 mForwardTracker->setSaveCriteriaValues(
false);
283 mForwardData = std::shared_ptr<FwdDataSource>(
new FwdDataSource());
284 mForwardTracker->setData(mForwardData);
285 mForwardTracker->initialize( mGeoCache,
false );
291 auto fstZ = fwdGeoUtils.fstZ( {151.750000, 165.248001, 178.781006} );
292 mFstZFromGeom.assign( fstZ.begin(), fstZ.end() );
293 auto fttZ = fwdGeoUtils.fttZ( {280.904999, 303.704987, 326.605011, 349.404999} );
294 mFttZFromGeom.assign( fttZ.begin(), fttZ.end() );
302 float rSize = xfg.
get<
float>(
"SiRasterizer:r", 3.0);
303 float phiSize = xfg.get<
float>(
"SiRasterizer:phi", 0.004);
310 const float x =
hit.X();
311 const float y =
hit.Y();
312 const float R = sqrt(x * x + y * y);
313 const float cosphi = x / R;
314 const float sinphi = y / R;
315 const float sqrt12 = sqrt(12.);
317 const float dr = rSize / sqrt12;
318 const float dphi = (phiSize) / sqrt12;
324 T(0, 1) = -R * sinphi;
326 T(1, 1) = R * cosphi;
330 J(1, 0) = -R * sinphi;
331 J(1, 1) = R * cosphi;
333 TMatrixD cmcyl(2, 2);
334 cmcyl(0, 0) = dr * dr;
335 cmcyl(1, 1) = dphi * dphi;
337 TMatrixD r = T * cmcyl * J;
347 TMatrixDSym tamvoc(3);
348 tamvoc( 0, 0 ) = cm(0, 0); tamvoc( 0, 1 ) = cm(0, 1); tamvoc( 0, 2 ) = 0.0;
349 tamvoc( 1, 0 ) = cm(1, 0); tamvoc( 1, 1 ) = cm(1, 1); tamvoc( 1, 2 ) = 0.0;
350 tamvoc( 2, 0 ) = 0.0; tamvoc( 2, 1 ) = 0.0; tamvoc( 2, 2 ) = 0.01*0.01;
356 void StFwdTrackMaker::loadFttHits( FwdDataSource::McTrackMap_t &mcTrackMap, FwdDataSource::HitMap_t &hitMap,
int count ){
357 LOG_DEBUG <<
"Loading FTT Hits" << endm;
360 string fttFromSource = mFwdConfig.
get<
string>(
"Source:ftt",
"DATA" );
363 LOG_ERROR <<
"No StEvent, cannot load Ftt Data" << endm;
369 if ( col ||
"DATA" == fttFromSource ) {
370 loadFttHitsFromStEvent( mcTrackMap, hitMap, count );
376 LOG_DEBUG <<
"Try loading sTGC hits directly from GEANT hits" << endm;
377 loadFttHitsFromGEANT( mcTrackMap, hitMap, count );
382 void StFwdTrackMaker::loadFttHitsFromStEvent( FwdDataSource::McTrackMap_t &mcTrackMap, FwdDataSource::HitMap_t &hitMap,
int count ){
383 LOG_DEBUG <<
"Loading FTT Hits from Data" << endm;
386 size_t numFwdHitsPrior = mFwdHitsFtt.size();
388 if ( col && col->numberOfPoints() > 0 ){
389 LOG_DEBUG <<
"The Ftt Collection has " << col->numberOfPoints() <<
" points" << endm;
390 TMatrixDSym hitCov3(3);
391 const double sigXY = 0.2;
392 hitCov3(0, 0) = sigXY * sigXY;
393 hitCov3(1, 1) = sigXY * sigXY;
395 static const double mm_to_cm = 0.1;
396 for (
auto point : col->points() ){
398 float xcm = point->xyz().x()*mm_to_cm;
399 float ycm = point->xyz().y()*mm_to_cm;
400 float zcm = point->xyz().z();
403 int track_id = point->idTruth();
404 shared_ptr<McTrack> mcTrack =
nullptr;
405 if ( mcTrackMap.count(track_id) ) {
406 mcTrack = mcTrackMap[track_id];
407 LOG_DEBUG <<
"Adding McTrack to FTT hit: " << track_id << endm;
410 mFwdHitsFtt.push_back(
FwdHit(count++,
418 mFttHits.push_back( TVector3( xcm, ycm, zcm) );
421 LOG_DEBUG <<
"The Ftt Collection is EMPTY points" << endm;
425 size_t numFwdHitsPost = mFwdHitsFtt.size();
426 for (
size_t iFwdHit = numFwdHitsPrior; iFwdHit < numFwdHitsPost; iFwdHit++){
429 if ( hit->getLayer() >= 0 )
430 hitMap[hit->getSector()].push_back(hit);
432 if ( hit->getMcTrack() ){
433 hit->getMcTrack()->addFttHit(hit);
437 if ( numFwdHitsPost != numFwdHitsPrior ){
438 LOG_INFO <<
"Loaded " << numFwdHitsPost - numFwdHitsPrior <<
" FTT hits from StEvent" << endm;
442 void StFwdTrackMaker::loadFttHitsFromGEANT( FwdDataSource::McTrackMap_t &mcTrackMap, FwdDataSource::HitMap_t &hitMap,
int count ){
445 St_g2t_fts_hit *g2t_stg_hits = (St_g2t_fts_hit *)GetDataSet(
"geant/g2t_stg_hit");
447 size_t numFwdHitsPrior = mFwdHitsFtt.size();
449 LOG_WARN <<
"geant/g2t_stg_hit is empty" << endm;
454 TMatrixDSym hitCov3(3);
455 const double sigXY = 0.02;
456 hitCov3(0, 0) = sigXY * sigXY;
457 hitCov3(1, 1) = sigXY * sigXY;
460 int nstg = g2t_stg_hits->GetNRows();
462 LOG_DEBUG <<
"This event has " << nstg <<
" stg hits in geant/g2t_stg_hit " << endm;
464 bool filterGEANT = mFwdConfig.
get<
bool>(
"Source:fttFilter", false );
465 for (
int i = 0; i < nstg; i++) {
467 g2t_fts_hit_st *git = (g2t_fts_hit_st *)g2t_stg_hits->At(i);
470 int track_id = git->track_p;
471 int volume_id = git->volume_id;
472 int plane_id = (volume_id - 1) / 100;
475 if ( volume_id % 2 ==0 )
482 if (plane_id < 0 || plane_id >= 4) {
485 mFwdHitsFtt.push_back(
496 mFttHits.push_back( TVector3( x, y, z ) );
500 size_t numFwdHitsPost = mFwdHitsFtt.size();
501 for (
size_t iFwdHit = numFwdHitsPrior; iFwdHit < numFwdHitsPost; iFwdHit++){
502 FwdHit *hit = &(mFwdHitsFtt[ iFwdHit ]);
504 if ( hit->getLayer() >= 0 )
505 hitMap[hit->getSector()].push_back(hit);
507 if ( dynamic_cast<FwdHit*>(hit)->_mcTrack ){
508 dynamic_cast<FwdHit*
>(hit)->_mcTrack->addFttHit(hit);
512 if ( numFwdHitsPost != numFwdHitsPrior ){
513 LOG_INFO <<
"Loaded " << numFwdHitsPost - numFwdHitsPrior <<
" FST hits from MuDst" << endm;
533 int count = loadFstHitsFromMuDst(mcTrackMap, hitMap);
534 if ( count > 0 )
return count;
536 count += loadFstHitsFromStEvent(mcTrackMap, hitMap);
537 if ( count > 0 )
return count;
539 bool siRasterizer = mFwdConfig.
get<
bool>(
"SiRasterizer:active", false );
541 if ( !siRasterizer ) count += loadFstHitsFromStRnDHits( mcTrackMap, hitMap );
542 if ( count > 0 )
return count;
544 return loadFstHitsFromGEANT( mcTrackMap, hitMap );
547 int StFwdTrackMaker::loadFstHitsFromMuDst( FwdDataSource::McTrackMap_t &mcTrackMap, FwdDataSource::HitMap_t &hitMap){
551 LOG_WARN <<
" No MuDstMaker ... bye-bye" << endm;
556 LOG_WARN <<
" No MuDst ... bye-bye" << endm;
562 LOG_WARN <<
"No StMuFstCollection ... bye-bye" << endm;
566 size_t numFwdHitsPrior = mFwdHitsFst.size();
567 LOG_INFO <<
"Loading " << fst->numberOfHits() <<
" StMuFstHits" << endm;
568 TMatrixDSym hitCov3(3);
569 for (
unsigned int index = 0; index < fst->numberOfHits(); index++){
572 float vR = muFstHit->localPosition(0);
573 float vPhi = muFstHit->localPosition(1);
574 float vZ = muFstHit->localPosition(2);
576 const float dz0 = fabs( vZ - mFstZFromGeom[0] );
577 const float dz1 = fabs( vZ - mFstZFromGeom[1] );
578 const float dz2 = fabs( vZ - mFstZFromGeom[2] );
579 static const float fstThickness = 2.0;
582 int d = 0 * ( dz0 < fstThickness ) + 1 * ( dz1 < fstThickness ) + 2 * ( dz2 < fstThickness );
584 float x0 = vR * cos( vPhi );
585 float y0 = vR * sin( vPhi );
586 hitCov3 = makeSiCovMat( TVector3( x0, y0, vZ ), mFwdConfig );
588 LOG_DEBUG <<
"FST HIT: d = " << d <<
", x=" << x0 <<
", y=" << y0 <<
", z=" << vZ << endm;
589 mFstHits.push_back( TVector3( x0, y0, vZ) );
592 mFwdHitsFst.push_back(
606 size_t numFwdHitsPost = mFwdHitsFst.size();
607 for (
size_t iFwdHit = numFwdHitsPrior; iFwdHit < numFwdHitsPost; iFwdHit++){
608 FwdHit *hit = &(mFwdHitsFst[ iFwdHit ]);
610 if ( hit->getLayer() >= 0 )
611 hitMap[hit->getSector()].push_back(hit);
614 if ( numFwdHitsPost != numFwdHitsPrior ){
615 LOG_INFO <<
"Loaded " << numFwdHitsPost - numFwdHitsPrior <<
" FST hits from MuDst" << endm;
622 int StFwdTrackMaker::loadFstHitsFromStEvent( FwdDataSource::McTrackMap_t &mcTrackMap, FwdDataSource::HitMap_t &hitMap){
626 LOG_WARN <<
"No StEvent, cannot load FST hits from StEvent StFstHitCollection" << endm;
629 LOG_DEBUG <<
"Got StEvent, loading Fst Hits" << endm;
631 size_t numFwdHitsPrior = mFwdHitsFst.size();
633 if ( fstHitCollection && fstHitCollection->numberOfHits() > 0){
635 TMatrixDSym hitCov3(3);
636 LOG_DEBUG <<
"StFstHitCollection is NOT NULL, loading hits" << endm;
637 for (
unsigned int iw = 0; iw < kFstNumWedges; iw++ ){
640 for (
unsigned int is = 0; is < kFstNumSensorsPerWedge; is++ ){
643 StSPtrVecFstHit fsthits = sc->hits();
644 for (
unsigned int ih = 0; ih < fsthits.size(); ih++ ){
645 float vR = fsthits[ih]->localPosition(0);
646 float vPhi = fsthits[ih]->localPosition(1);
647 float vZ = fsthits[ih]->localPosition(2);
649 const float dz0 = fabs( vZ - mFstZFromGeom[0] );
650 const float dz1 = fabs( vZ - mFstZFromGeom[1] );
651 const float dz2 = fabs( vZ - mFstZFromGeom[2] );
652 static const float fstThickness = 3.0;
655 int d = 0 * ( dz0 < fstThickness ) + 1 * ( dz1 < fstThickness ) + 2 * ( dz2 < fstThickness );
657 float x0 = vR * cos( vPhi );
658 float y0 = vR * sin( vPhi );
659 hitCov3 = makeSiCovMat( TVector3( x0, y0, vZ ), mFwdConfig );
661 LOG_DEBUG <<
"FST HIT: d = " << d <<
", x=" << x0 <<
", y=" << y0 <<
", z=" << vZ << endm;
662 mFstHits.push_back( TVector3( x0, y0, vZ) );
663 int track_id = fsthits[ih]->idTruth();
664 LOG_DEBUG <<
"FST Hit: idTruth = " << track_id << endm;
665 shared_ptr<McTrack> mcTrack =
nullptr;
666 if ( mcTrackMap.count(track_id) ) {
667 mcTrack = mcTrackMap[track_id];
668 LOG_DEBUG <<
"Adding McTrack to FST hit" << endm;
672 mFwdHitsFst.push_back(
684 mFwdHitsFst.back()._hit = fsthits[ih];
688 LOG_DEBUG <<
" FOUND " << mFstHits.size() <<
" FST HITS in StFstHitCollection" << endm;
691 size_t numFwdHitsPost = mFwdHitsFst.size();
692 for (
size_t iFwdHit = numFwdHitsPrior; iFwdHit < numFwdHitsPost; iFwdHit++){
693 FwdHit *hit = &(mFwdHitsFst[ iFwdHit ]);
695 if ( hit->getLayer() >= 0 )
696 hitMap[hit->getSector()].push_back(hit);
698 if ( hit->getMcTrack() ){
699 hit->getMcTrack()->addFstHit(hit);
702 if ( numFwdHitsPost != numFwdHitsPrior ){
703 LOG_INFO <<
"Loaded " << numFwdHitsPost - numFwdHitsPrior <<
" FST hits from StEvent" << endm;
708 int StFwdTrackMaker::loadFstHitsFromStRnDHits( FwdDataSource::McTrackMap_t &mcTrackMap, FwdDataSource::HitMap_t &hitMap){
709 LOG_DEBUG <<
"Looking for FST hits in StEvent StRnDHit Collection" << endm;
714 LOG_DEBUG <<
"No StEvent, cannot load FST FastSim hits from StEvent StRndHitCollection" << endm;
718 size_t numFwdHitsPrior = mFwdHitsFst.size();
720 if (!rndCollection)
return 0;
722 const StSPtrVecRnDHit &hits = rndCollection->hits();
725 TMatrixDSym hitCov3(3);
727 for (
unsigned int fsthit_index = 0; fsthit_index < hits.size(); fsthit_index++) {
730 if ( hit->layer() > 6 ){
737 StMatrixF covmat = hit->covariantMatrix();
740 hitCov3(0,0) = covmat[0][0]; hitCov3(0,1) = covmat[0][1]; hitCov3(0,2) = covmat[0][2];
741 hitCov3(1,0) = covmat[1][0]; hitCov3(1,1) = covmat[1][1]; hitCov3(1,2) = covmat[1][2];
742 hitCov3(2,0) = covmat[2][0]; hitCov3(2,1) = covmat[2][1]; hitCov3(2,2) = covmat[2][2];
744 mFwdHitsFst.push_back(
747 hit->position().x(), hit->position().y(), hit->position().z(),
752 mcTrackMap[hit->idTruth()]
755 mFstHits.push_back( TVector3( hit->position().x(), hit->position().y(), hit->position().z()) );
759 size_t numFwdHitsPost = mFwdHitsFst.size();
760 for (
size_t iFwdHit = numFwdHitsPrior; iFwdHit < numFwdHitsPost; iFwdHit++){
761 FwdHit *hit = &(mFwdHitsFst[ iFwdHit ]);
763 if ( hit->getLayer() >= 0 )
764 hitMap[hit->getSector()].push_back(hit);
766 if ( numFwdHitsPost != numFwdHitsPrior ){
767 LOG_INFO <<
"Loaded " << numFwdHitsPost - numFwdHitsPrior <<
" FST hits from StEvent FastSim" << endm;
773 int StFwdTrackMaker::loadFstHitsFromGEANT( FwdDataSource::McTrackMap_t &mcTrackMap, FwdDataSource::HitMap_t &hitMap ){
775 LOG_DEBUG <<
"Looking for FST hits in geant struct" << endm;
778 St_g2t_fts_hit *g2t_fsi_hits = (St_g2t_fts_hit *)GetDataSet(
"geant/g2t_fsi_hit");
780 if ( !g2t_fsi_hits ){
781 LOG_DEBUG <<
"No g2t_fts_hits, cannot load FST hits from GEANT" << endm;
785 int nfsi = g2t_fsi_hits->GetNRows();
786 size_t numFwdHitsPrior = mFwdHitsFst.size();
789 TMatrixDSym hitCov3(3);
791 for (
int i = 0; i < nfsi; i++) {
793 g2t_fts_hit_st *git = (g2t_fts_hit_st *)g2t_fsi_hits->At(i);
798 int track_id = git->track_p;
799 int volume_id = git->volume_id;
800 int d = volume_id / 1000;
802 int plane_id = d - 4;
807 if (mSiRasterizer->active()) {
808 TVector3 rastered = mSiRasterizer->raster(TVector3(git->x[0], git->x[1], git->x[2]));
809 LOG_INFO << TString::Format(
"Rastered: %f %f %f -> %f %f %f", git->x[0], git->x[1], git->x[2], rastered.X(), rastered.Y(), rastered.Z()) << endm;
813 LOG_INFO <<
"Using GEANT FST hit positions without rasterization" << endm;
816 if (plane_id > 3 || plane_id < 0) {
820 hitCov3 = makeSiCovMat( TVector3( x, y, z ), mFwdConfig );
821 mFwdHitsFst.push_back(
832 mFstHits.push_back( TVector3( x, y, z ) );
836 size_t numFwdHitsPost = mFwdHitsFst.size();
837 for (
size_t iFwdHit = numFwdHitsPrior; iFwdHit < numFwdHitsPost; iFwdHit++){
838 FwdHit *hit = &(mFwdHitsFst[ iFwdHit ]);
840 if ( hit->getLayer() >= 0 )
841 hitMap[hit->getSector()].push_back(hit);
844 if ( hit->getMcTrack() )
845 hit->getMcTrack()->addFstHit(hit);
847 if ( numFwdHitsPost != numFwdHitsPrior ){
848 LOG_INFO <<
"Loaded " << numFwdHitsPost - numFwdHitsPrior <<
" FST hits from GEANT" << endm;
865 LOG_DEBUG <<
"Looking for GEANT sim vertex info" << endm;
866 St_g2t_vertex *g2t_vertex = (St_g2t_vertex *)GetDataSet(
"geant/g2t_vertex");
868 if ( g2t_vertex !=
nullptr ) {
870 g2t_vertex_st *vert = (g2t_vertex_st*)g2t_vertex->At(0);
876 mForwardTracker->setEventVertex( TVector3( vert->ge_x[0], vert->ge_x[1], vert->ge_x[2] ), cov );
881 St_g2t_track *g2t_track = (St_g2t_track *)GetDataSet(
"geant/g2t_track");
887 LOG_DEBUG << g2t_track->GetNRows() <<
" mc tracks in geant/g2t_track " << endm;
889 for (
int irow = 0; irow < g2t_track->GetNRows(); irow++) {
890 g2t_track_st *track = (g2t_track_st *)g2t_track->At(irow);
895 int track_id = track->id;
896 float pt2 = track->p[0] * track->p[0] + track->p[1] * track->p[1];
897 float pt = std::sqrt(pt2);
898 float eta = track->eta;
899 TVector3 pp( track->p[0], track->p[1], track->p[2] );
900 float phi = std::atan2(track->p[1], track->p[0]);
901 int q = track->charge;
903 if (!mcTrackMap[track_id] )
904 mcTrackMap[track_id] = shared_ptr<McTrack>(
new McTrack(pp.Pt(), pp.Eta(), pp.Phi(), q, track->start_vertex_p));
909 size_t nForwardTracks = 0;
910 size_t nForwardTracksNoThreshold = 0;
911 for (
auto mctm : mcTrackMap ){
912 if ( mctm.second ==
nullptr )
continue;
913 if ( mctm.second->mEta > 2.5 && mctm.second->mEta < 4.0 ){
914 nForwardTracksNoThreshold++;
915 if ( mctm.second->mPt > 0.05 )
919 return nForwardTracks;
933 mFcsDb =
static_cast<StFcsDb*
>(GetDataSet(
"fcsDb"));
934 if ( !stEvent || !mFcsDb ){
945 for (
int idet = 0; idet < 4; idet++ ){
946 StSPtrVecFcsCluster& clusters = fcsCol->clusters(idet);
947 int nc=fcsCol->numberOfClusters(idet);
948 for (
int i = 0; i < nc; i++ ){
951 mFcsClusters.push_back( TVector3( xyz.x(), xyz.y(), xyz.z() - 2 ) );
952 mFcsClusterEnergy.push_back( clu->energy() );
957 for (
int det = 4; det < 6; det ++ ) {
959 StSPtrVecFcsHit& hits = stEvent->fcsCollection()->hits(det);
960 int nh=fcsCol->numberOfHits(det);
961 for (
int i = 0; i < nh; i++ ){
964 if(det==kFcsPresNorthDetId || det==kFcsPresSouthDetId){
969 if ( hit->energy() < 0.2 )
continue;
971 epdgeo.GetCorners(100*pp+tt,&n,x,y);
972 double x0 = (x[0] + x[1] + x[2] + x[3]) / 4.0;
973 double y0 = (y[0] + y[1] + y[2] + y[3]) / 4.0;
974 mFcsPreHits.push_back( TVector3( x0, y0, zepd ) );
980 TVector3 StFwdTrackMaker::GetEventPrimaryVertex(){
981 if ( mFwdVertexSource != kFwdVertexSourceUnknown ){
986 mEventVertexCov.ResizeTo(3, 3);
987 mEventVertexCov.Zero();
989 mEventVertexCov(0, 0) = sig2;
990 mEventVertexCov(1, 1) = sig2;
991 mEventVertexCov(2, 2) = sig2;
994 mFwdVertexSource = kFwdVertexSourceNone;
999 St_g2t_vertex *g2t_vertex = (St_g2t_vertex *)GetDataSet(
"geant/g2t_vertex");
1000 LOG_DEBUG <<
"Searching for Event Vertex from geant/g2t_vertex: " << g2t_vertex << endm;
1001 if ( g2t_vertex !=
nullptr ) {
1003 g2t_vertex_st *vert = (g2t_vertex_st*)g2t_vertex->At(0);
1004 LOG_INFO <<
"Setting Event Vertex from geant/g2t_vertex[0]: " << vert << endm;
1006 mEventVertexCov.ResizeTo(3, 3);
1007 const double sigXY = 0.1;
1008 const double sigZ = 0.1;
1009 mEventVertexCov(0, 0) = pow(sigXY,2);
1010 mEventVertexCov(1, 1) = pow(sigXY,2);
1011 mEventVertexCov(2, 2) = pow(sigZ, 2);
1012 auto rhc = TVectorD( 3 );
1013 rhc[0] = vert->ge_x[0];
1014 rhc[1] = vert->ge_x[1];
1015 rhc[2] = vert->ge_x[2];
1016 mEventVertex.SetXYZ( vert->ge_x[0], vert->ge_x[1], vert->ge_x[2] );
1017 mFwdVertexSource = kFwdVertexSourceMc;
1018 return mEventVertex;
1024 LOG_DEBUG <<
"Searching for Event Vertex from StMcEvent: " << stMcEvent << endm;
1025 if (stMcEvent && stMcEvent->primaryVertex() ) {
1027 mEventVertex.SetXYZ( vertex.x(), vertex.y(), vertex.z() );
1028 mFwdVertexSource = kFwdVertexSourceMc;
1029 LOG_INFO <<
"FWD Tracking on event with MC Primary Vertex: " << mEventVertex.X() <<
", " << mEventVertex.Y() <<
", " << mEventVertex.Z() << endm;
1031 const double sigXY = 0.1;
1032 const double sigZ = 0.1;
1033 mEventVertexCov(0, 0) = pow(sigXY,2);
1034 mEventVertexCov(1, 1) = pow(sigXY,2);
1035 mEventVertexCov(2, 2) = pow(sigZ, 2);
1036 return mEventVertex;
1040 if(mMuDstMaker && mMuDstMaker->
muDst() && mMuDstMaker->
muDst()->numberOfPrimaryVertices() > 0 && mMuDstMaker->
muDst()->
primaryVertex() ) {
1044 mFwdVertexSource = kFwdVertexSourceTpc;
1045 return mEventVertex;
1049 LOG_DEBUG <<
"FWD Tracking on event without available Mu Primary Vertex" << endm;
1050 StEvent *stEvent =
static_cast<StEvent *
>(GetInputDS(
"StEvent"));
1051 if (!stEvent)
return mEventVertex;
1055 LOG_WARN <<
"Cannot get BTOF collections, Cannot use VPD vertex" << endm;
1056 return mEventVertex;
1061 LOG_WARN <<
"Cannot get BTOF Header, Cannot use VPD vertex" << endm;
1062 return mEventVertex;
1065 int nEast = btofHeader->numberOfVpdHits( east );
1066 int nWest = btofHeader->numberOfVpdHits( west );
1067 int nTof = btofC->tofHits().size();
1069 if ( btofHeader->vpdVz() && fabs(btofHeader->vpdVz()) < 100 ){
1071 LOG_DEBUG <<
"FWD Tracking on event using VPD z vertex: (, 0, 0, " << btofHeader->vpdVz() <<
" )" << endm;
1072 mFwdVertexSource = kFwdVertexSourceVpd;
1073 const double sigXY = 1;
1074 const double sigZ = 6;
1075 mEventVertexCov(0, 0) = pow(sigXY,2);
1076 mEventVertexCov(1, 1) = pow(sigXY,2);
1077 mEventVertexCov(2, 2) = pow(sigZ, 2);
1078 mEventVertex.SetXYZ( 0, 0, btofHeader->vpdVz() );
1079 return mEventVertex;
1083 return mEventVertex;
1089 long long itStart = FwdTrackerUtils::nowNanoSecond();
1091 StEvent *stEvent =
static_cast<StEvent *
>(GetInputDS(
"StEvent"));
1092 if (!stEvent)
return kStOk;
1096 FwdDataSource::McTrackMap_t &mcTrackMap = mForwardData->getMcTracks();
1097 FwdDataSource::HitMap_t &hitMap = mForwardData->getFttHits();
1098 FwdDataSource::HitMap_t &fsiHitMap = mForwardData->getFstHits();
1102 mFwdVertexSource = StFwdTrackMaker::kFwdVertexSourceUnknown;
1103 GetEventPrimaryVertex();
1104 LOG_DEBUG <<
"FWD Vertex Source: " << mFwdVertexSource << endm;
1105 LOG_DEBUG <<
"Setting FWD event vertex to: " << TString::Format(
"mEventVertex=(%0.3f+/-%0.3f, %0.3f+/-%0.3f, %0.3f+/-%0.3f)", mEventVertex.X(), sqrt(mEventVertexCov(0, 0)), mEventVertex.Y(), sqrt(mEventVertexCov(1, 1)), mEventVertex.Z(), sqrt(mEventVertexCov(2, 2)) ) << endm;
1106 mForwardTracker->setEventVertex( mEventVertex, mEventVertexCov );
1112 size_t maxForwardTracks = mFwdConfig.
get<
size_t>(
"McEvent.Mult:max", 10000 );
1113 if ( nForwardTracks > maxForwardTracks ){
1114 LOG_WARN <<
"Skipping event with more than " << maxForwardTracks <<
" forward tracks" << endm;
1117 LOG_DEBUG <<
"We have " << nForwardTracks <<
" forward MC tracks" << endm;
1121 LOG_DEBUG <<
">>StFwdTrackMaker::loadFttHits" << endm;
1122 if ( IAttr(
"useFtt") ) {
1123 loadFttHits( mcTrackMap, hitMap );
1128 if ( IAttr(
"useFst") ) {
1129 LOG_DEBUG <<
">>StFwdTrackMaker::loadFstHits" << endm;
1130 int fstCount =
loadFstHits( mcTrackMap, fsiHitMap );
1131 LOG_DEBUG <<
"Loaded " << fstCount <<
" FST hits" << endm;
1136 LOG_DEBUG <<
">>StFwdTrackMaker::loadFcsHits" << endm;
1137 if ( IAttr(
"useFcs") ) {
1143 for (
auto kv : mcTrackMap ){
1144 if ( kv.second ==
nullptr )
continue;
1145 LOG_DEBUG <<
"MC Track: id=" << kv.first <<
", nFTT=" << kv.second->mFttHits.size() <<
", nFST=" << kv.second->mFstHits.size() << endm;
1150 LOG_DEBUG <<
">>START Event Forward Tracking" << endm;
1151 LOG_INFO <<
"\tFinding FWD Track Seeds" << endm;
1152 mForwardTracker->findTrackSeeds();
1153 LOG_INFO <<
"\tFitting FWD Track Seeds" << endm;
1155 mForwardTracker->doTrackFitting( mForwardTracker->getTrackSeeds() );
1156 LOG_DEBUG <<
"<<FINISH Event Forward Tracking" << endm;
1157 LOG_INFO <<
"<<Fwd Tracking Found : " << mForwardTracker -> getTrackSeeds().size() <<
" Track Seeds" << endm;
1158 LOG_INFO <<
"<<Fwd Tracking Fit :" << mForwardTracker -> getTrackResults().size() <<
" GenFit Tracks" << endm;
1165 std::vector<genfit::Track *> genfitTracks;
1166 for (
auto gtr : mForwardTracker->getTrackResults() ) {
1167 if ( gtr.mIsFitConvergedFully ==
false )
continue;
1168 genfitTracks.push_back( gtr.mTrack.get() );
1171 if ( mVisualize && genfitTracks.size() > 0 && genfitTracks.size() < 400 && eventIndex < 50 ) {
1172 const auto &seed_tracks = mForwardTracker -> getTrackSeeds();
1176 TString::Format(
"ev%lu", eventIndex ).Data(),
1178 seed_tracks, genfitTracks, mRaveVertices,
1179 mFttHits, mFstHits, mFcsPreHits, mFcsClusters, mFcsClusterEnergy );
1181 LOG_DEBUG <<
"Done Writing OBJ " << endm;
1182 }
else if (mVisualize && genfitTracks.size() == 0) {
1183 LOG_DEBUG <<
"Skipping visualization, no FWD tracks" << endm;
1184 }
else if (mVisualize && genfitTracks.size() >= 400) {
1185 LOG_DEBUG <<
"Skipping visualization, too many FWD tracks" << endm;
1189 LOG_DEBUG <<
"Forward tracking on this event took " << (FwdTrackerUtils::nowNanoSecond() - itStart) * 1e-6 <<
" ms" << endm;
1190 if ( IAttr(
"fillEvent") ) {
1192 LOG_WARN <<
"No StEvent found. Forward tracks will not be saved" << endm;
1212 LOG_DEBUG <<
"StFwdTrackMaker::makeStFwdTrack()" << endm;
1217 int nSeedPoints = 0;
1218 for (
auto s : gtr.mSeed ){
1222 cov[0] = fh->_covmat(0,0); cov[3] = fh->_covmat(1,0); cov[6] = fh->_covmat(2,0);
1223 cov[1] = fh->_covmat(0,1); cov[4] = fh->_covmat(1,1); cov[7] = fh->_covmat(2,1);
1224 cov[2] = fh->_covmat(0,2); cov[5] = fh->_covmat(1,2); cov[8] = fh->_covmat(2,2);
1228 fh->_detid * 10 + fh->getSector(),
1233 fwdTrack->mFSTPoints.push_back( p );
1234 else if ( fh->isFtt() )
1235 fwdTrack->mFTTPoints.push_back( p );
1241 fwdTrack->setNumberOfSeedPoints( nSeedPoints );
1244 idt = MCTruthUtils::dominantContribution(gtr.mSeed, qual);
1245 fwdTrack->setMc( idt, qual*100 );
1246 LOG_DEBUG <<
"Dominant contribution: " << idt <<
" with quality " << qual << endm;
1249 if ( gtr.mTrack ==
nullptr ){
1251 LOG_DEBUG <<
"GenfitTrack is nullptr, making StFwdTrack with seed info only" << endm;
1255 fwdTrack->setDidFitConverge( gtr.mStatus->isFitConverged() );
1256 fwdTrack->setDidFitConvergeFully( gtr.mStatus->isFitConvergedFully() );
1257 fwdTrack->setNumberOfFailedPoints( gtr.mStatus->getNFailedPoints() );
1259 fwdTrack->setNumberOfFitPoints( gtr.mTrack->getNumPoints() );
1260 fwdTrack->setChi2( gtr.mStatus->getChi2() );
1261 fwdTrack->setNDF( gtr.mStatus->getNdf() );
1262 fwdTrack->setPval( gtr.mStatus->getPVal() );
1265 fwdTrack->setCharge( gtr.mCharge );
1266 fwdTrack->setDCA( gtr.mDCA.X(), gtr.mDCA.Y(), gtr.mDCA.Z() );
1268 TVector3 p = gtr.mMomentum;
1269 fwdTrack->setPrimaryMomentum(
StThreeVectorD( gtr.mMomentum.X(), gtr.mMomentum.Y(), gtr.mMomentum.Z() ) );
1271 if ( gtr.isPrimary ){
1272 fwdTrack->setVtxIndex( 0 );
1274 fwdTrack->setVtxIndex( UCHAR_MAX );
1279 if ( !gtr.mStatus->isFitConvergedPartially() ){
1287 map<int, float> mapDetectorToZPlane = {
1289 { kFstId, mFstZFromGeom[0] },
1290 { kFstId, mFstZFromGeom[1] },
1291 { kFstId, mFstZFromGeom[2] },
1292 { kFttId, mFttZFromGeom[0] },
1293 { kFttId, mFttZFromGeom[1] },
1294 { kFttId, mFttZFromGeom[2] },
1295 { kFttId, mFttZFromGeom[3] },
1296 { kFcsPresId, 375.0 },
1297 { kFcsWcalId, 715.0 },
1298 { kFcsHcalId, 807.0 }
1301 if ( gtr.mStatus->isFitConverged() ){
1303 TVector3 mom(0, 0, 0);
1305 TVector3 tv3(0, 0, 0);
1306 for (
auto zp : mapDetectorToZPlane ){
1307 int detIndex = zp.first;
1308 float z = zp.second;
1309 tv3.SetXYZ(0, 0, 0);
1310 if ( detIndex != kFcsHcalId && detIndex != kFcsWcalId ){
1311 float detpos[3] = {0,0,z};
1312 float detnorm[3] = {0,0,1};
1313 if( detIndex==kFcsPresId ){
1315 detpos[0] = (float)xyzoff.x();
1316 detpos[1] = (float)xyzoff.y();
1317 detpos[2] = (float)xyzoff.z();
1319 tv3 = ObjExporter::trackPosition( gtr.mTrack.get(), detpos, detnorm, cov, mom );
1323 if( detIndex==kFcsWcalId ){
1326 if( p[2]>=0 && p[0]>=0 ){ det=1; }
1327 if( p[2]<0 && p[0]<0 ){ det=1; }
1330 if( detIndex==kFcsHcalId ){
1333 if( p[2]>=0 && p[0]>=0 ){ det=3; }
1334 if( p[2]<0 && p[0]<0 ){ det=3; }
1338 float xyz0[3] = { 0, 0, 575.0 };
1339 float xyz1[3] = { 0, 0, 625.0 };
1340 float xyzdet[3] = { (float)xyzoff.x(), (float)xyzoff.y(), (float)xyzoff.z() };
1341 float detnorm[3] = { (float)planenormal.x(), (float)planenormal.y(), (float)planenormal.z() };
1342 LOG_DEBUG <<
"Projecting to: " << detIndex << endm;
1343 tv3 = ObjExporter::projectAsStraightLine( gtr.mTrack.get(), xyz0, xyz1, xyzdet, detnorm, cov, mom );
1359 void StFwdTrackMaker::FillEvent() {
1360 StEvent *stEvent =
static_cast<StEvent *
>(GetInputDS(
"StEvent"));
1365 LOG_INFO <<
"Creating the StFwdTrackCollection" << endm;
1367 stEvent->setFwdTrackCollection( ftc );
1370 size_t indexTrack = 0;
1371 for (
auto gtr : mForwardTracker->getTrackResults() ) {
1374 if (
nullptr == fwdTrack)
1376 ftc->addTrack( fwdTrack );
1379 LOG_INFO <<
"StFwdTrackCollection has " << ftc->numberOfTracks() <<
" tracks now" << endm;
1384 auto mk = GetMaker(
"PicoDst");
1385 if ( mk && stEvent->numberOfPrimaryVertices() == 0 ){
1386 LOG_INFO <<
"Adding a primary vertex to StEvent since PicoDst maker was found in chain, but no vertices found" << endm;
1388 LOG_INFO <<
"StPrimaryVertex::numberOfPrimaryVertices = " << stEvent->numberOfPrimaryVertices() << endm;
1395 vector<genfit::Track *> genfitTracks;
1397 const auto &trackResults = mForwardTracker -> getTrackResults();
1398 if ( genfitTracks.size() >= 2 ){
1399 genfit::GFRaveVertexFactory gfrvf;
1401 TMatrixDSym bscm(3);
1402 const double bssXY = 2.0;
1403 bscm(0, 0) = bssXY*bssXY;
1404 bscm(1, 1) = bssXY*bssXY;
1405 bscm(2, 2) = 50.5 * 50.5;
1406 gfrvf.setBeamspot( TVector3( 0, 0, 0 ), bscm );
1408 mRaveVertices.clear();
1409 gfrvf.findVertices( &mRaveVertices, genfitTracks,
false );
1411 LOG_DEBUG <<
"mRaveVertices.size() = " << mRaveVertices.size() << endm;
1412 for (
auto vert : mRaveVertices ){
1413 LOG_DEBUG << TString::Format(
"RAVE vertex @(%f, %f, %f)\n\n", vert->getPos().X(), vert->getPos().Y(), vert->getPos().Z() ) << endm;
1419 void StFwdTrackMaker::Clear(
const Option_t *opts) {
1420 LOG_DEBUG <<
"StFwdTrackMaker::CLEAR" << endm;
1421 mForwardData->clear();
1422 mForwardTracker->Clear();
1425 mFwdHitsFst.clear();
1426 mFwdHitsFtt.clear();
1431 mFcsPreHits.clear();
1432 mFcsClusters.clear();
1437 void StFwdTrackMaker::ProcessFwdTracks( ){
1439 LOG_DEBUG <<
"StFwdTrackMaker::ProcessFwdTracks" << endm;
1440 StEvent *stEvent =
static_cast<StEvent *
>(GetInputDS(
"StEvent"));
1442 for (
auto fwdTrack : ftc->tracks() ){
1443 LOG_DEBUG << TString::Format(
"StFwdTrack[ nProjections=%lu, nFTTSeeds=%lu, nFSTSeeds=%lu, mPt=%f ]", fwdTrack->mProjections.size(), fwdTrack->mFTTPoints.size(), fwdTrack->mFSTPoints.size(), fwdTrack->momentum().perp()) << endm;
1444 for (
auto proj : fwdTrack->mProjections ) {
1445 LOG_DEBUG << TString::Format(
"Proj[ %d, %f, %f, %f ]", proj.mDetId, proj.mXYZ.x(), proj.mXYZ.y(), proj.mXYZ.z() ) << endm;
1451 std::string StFwdTrackMaker::defaultConfig = R
"(
<?xml version="1.0" encoding="UTF-8"?>
<config>
<TrackFinder nIterations="1">
<Iteration nPhiSlices="1" > <!-- Options for first iteration -->
<SegmentBuilder>
<!-- <Criteria name="Crit2_RZRatio" min="0" max="1.20" /> -->
<!-- <Criteria name="Crit2_DeltaRho" min="-50" max="50.9"/> -->
<Criteria name="Crit2_DeltaPhi" min="0" max="10.0" />
<!-- <Criteria name="Crit2_StraightTrackRatio" min="0.01" max="5.85"/> -->
</SegmentBuilder>
<ThreeHitSegments>
<!-- <Criteria name="Crit3_3DAngle" min="0" max="60" />
<Criteria name="Crit3_PT" min="0" max="100" />
<Criteria name="Crit3_ChangeRZRatio" min="0.8" max="1.21" />
<Criteria name="Crit3_2DAngle" min="0" max="30" /> -->
</ThreeHitSegments>
</Iteration>
<Connector distance="2"/>
<SubsetNN active="true" min-hits-on-track="2" >
<!-- <InitialTemp>2.1</InitialTemp> -->
<!-- <InfTemp>0.1</InfTemp> -->
<Omega>0.99</Omega>
<StableThreshold>0.001</StableThreshold>
</SubsetNN>
<HitRemover active="false" />
</TrackFinder>
<TrackFitter refit="true" zeroB="false" active="true">
<Vertex sigmaXY="1" sigmaZ="10" includeInFit="true" smearMcVertex="false" />
</TrackFitter>
</config>
)";
1454 const std::vector<Seed_t> &StFwdTrackMaker::getTrackSeeds()
const{
1455 return mForwardTracker->getTrackSeeds();
1458 const std::vector<GenfitTrackResult> &StFwdTrackMaker::getFitResults()
const{
1459 return mForwardTracker->getTrackResults();
1461
static StMuPrimaryVertex * primaryVertex()
return pointer to current primary vertex
int loadFstHits(std::map< int, std::shared_ptr< McTrack >> &mcTrackMap, std::map< int, std::vector< KiTrack::IHit * >> &hitMap)
Loads FST hits from various sources into the hitmap and McTrackMap (if availabale) ...
void FitVertex()
Fit the primary vertex using FWD tracks.
void initialize(TString geoCache, bool genHistograms)
Initialize FwdTracker.
StThreeVectorD getDetectorOffset(int det, double zdepth=-1) const
Utility functions related to DetectorPosition.
StFwdTrack * makeStFwdTrack(GenfitTrackResult >r, size_t indexTrack)
StThreeVectorD getStarXYZfromColumnRow(int det, float col, float row, float FcsZ=-1.0) const
get the STAR frame cooridnates from other way
void load(std::string filename, bool asString=false)
Main setup routine Loads the given XML file (or string) and maps it.
void getEPDfromId(int det, int id, int &pp, int &tt)
Get FCS's EPD map foom EPD mapping.
static StMuFstCollection * muFstCollection()
returns pointer to current StMuFstCollection
T get(std::string path, T dv) const
template function for getting any type that can be converted from string via stringstream ...
size_t loadMcTracks(std::map< int, std::shared_ptr< McTrack >> &mcTrackMap)
virtual void initialize(TString geoCache, bool genHistograms)
Initialize FwdTracker.
void setup()
Setup the tracker object Load geometry Setup Material Effects Setup the magnetic field Setup the fitt...
Event data structure to hold all information from a Monte Carlo simulation. This class is the interfa...