Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-06 07:38:53

0001 #include "SimG4Core/Application/interface/TrackingAction.h"
0002 #include "SimG4Core/Application/interface/EventAction.h"
0003 #include "SimG4Core/Notification/interface/NewTrackAction.h"
0004 #include "SimG4Core/Notification/interface/CurrentG4Track.h"
0005 #include "SimG4Core/Notification/interface/BeginOfTrack.h"
0006 #include "SimG4Core/Notification/interface/EndOfTrack.h"
0007 #include "SimG4Core/Notification/interface/TrackInformation.h"
0008 #include "SimG4Core/Notification/interface/TrackWithHistory.h"
0009 #include "SimG4Core/Notification/interface/CMSSteppingVerbose.h"
0010 
0011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0012 
0013 #include "G4UImanager.hh"
0014 #include "G4TrackingManager.hh"
0015 #include "G4SystemOfUnits.hh"
0016 
0017 //#define EDM_ML_DEBUG
0018 
0019 TrackingAction::TrackingAction(EventAction* e, const edm::ParameterSet& p, CMSSteppingVerbose* sv)
0020     : eventAction_(e),
0021       steppingVerbose_(sv),
0022       checkTrack_(p.getUntrackedParameter<bool>("CheckTrack", false)),
0023       doFineCalo_(p.getParameter<bool>("DoFineCalo")),
0024       saveCaloBoundaryInformation_(p.getParameter<bool>("SaveCaloBoundaryInformation")),
0025       eMinFine_(p.getParameter<double>("EminFineTrack") * CLHEP::MeV) {
0026   if (!doFineCalo_) {
0027     eMinFine_ = DBL_MAX;
0028   }
0029   edm::LogVerbatim("SimG4CoreApplication") << "TrackingAction: boundary: " << saveCaloBoundaryInformation_
0030                                            << "; DoFineCalo: " << doFineCalo_ << "; EminFineTrack(MeV)=" << eMinFine_;
0031 }
0032 
0033 TrackingAction::~TrackingAction() {}
0034 
0035 void TrackingAction::PreUserTrackingAction(const G4Track* aTrack) {
0036   g4Track_ = aTrack;
0037   currentTrack_ = new TrackWithHistory(aTrack);
0038 
0039   BeginOfTrack bt(aTrack);
0040   m_beginOfTrackSignal(&bt);
0041 
0042   trkInfo_ = static_cast<TrackInformation*>(aTrack->GetUserInformation());
0043 
0044   // Always save primaries
0045   // Decays from primaries are marked as primaries (see NewTrackAction), but are not saved by
0046   // default. The primary is the earliest ancestor, and it must be saved.
0047   if (trkInfo_->isPrimary()) {
0048     eventAction_->prepareForNewPrimary();
0049     currentTrack_->save();
0050   }
0051   if (nullptr != steppingVerbose_) {
0052     steppingVerbose_->TrackStarted(aTrack, false);
0053   }
0054   double ekin = aTrack->GetKineticEnergy();
0055 
0056 #ifdef EDM_ML_DEBUG
0057   edm::LogVerbatim("DoFineCalo") << "PreUserTrackingAction: Start processing track " << aTrack->GetTrackID()
0058                                  << " pdgid=" << aTrack->GetDefinition()->GetPDGEncoding()
0059                                  << " ekin[GeV]=" << ekin / CLHEP::GeV << " vertex[cm]=("
0060                                  << aTrack->GetVertexPosition().x() / CLHEP::cm << ","
0061                                  << aTrack->GetVertexPosition().y() / CLHEP::cm << ","
0062                                  << aTrack->GetVertexPosition().z() / CLHEP::cm << ")"
0063                                  << " parentid=" << aTrack->GetParentID();
0064 #endif
0065   if (ekin > eMinFine_) {
0066     // It is impossible to tell whether daughter tracks if this track may need to be saved at
0067     // this point; Therefore, every track above the threshold is put in history,
0068     // so that it can potentially be saved later.
0069     trkInfo_->putInHistory();
0070   }
0071 }
0072 
0073 void TrackingAction::PostUserTrackingAction(const G4Track* aTrack) {
0074   int id = aTrack->GetTrackID();
0075   const auto& ppos = aTrack->GetStep()->GetPostStepPoint()->GetPosition();
0076   math::XYZVectorD pos(ppos.x(), ppos.y(), ppos.z());
0077   math::XYZTLorentzVectorD mom;
0078   std::pair<math::XYZVectorD, math::XYZTLorentzVectorD> p(pos, mom);
0079 
0080 #ifdef EDM_ML_DEBUG
0081   edm::LogVerbatim("DoFineCalo") << "PostUserTrackingAction:"
0082                                  << " aTrack->GetTrackID()=" << id
0083                                  << " currentTrack_->saved()=" << currentTrack_->saved();
0084 #endif
0085 
0086   bool boundary = false;
0087   if (doFineCalo_) {
0088     // Add the post-step position for _every_ track
0089     // in history to the TrackManager. Tracks in history _may_ be upgraded to stored
0090     // tracks, at which point the post-step position is needed again.
0091     eventAction_->addTkCaloStateInfo(id, p);
0092     boundary = true;
0093   } else if (trkInfo_->storeTrack() || currentTrack_->saved() ||
0094              (saveCaloBoundaryInformation_ && trkInfo_->crossedBoundary())) {
0095     currentTrack_->save();
0096     eventAction_->addTkCaloStateInfo(id, p);
0097     boundary = true;
0098   }
0099   if (boundary && trkInfo_->crossedBoundary()) {
0100     currentTrack_->setCrossedBoundaryPosMom(id, trkInfo_->getPositionAtBoundary(), trkInfo_->getMomentumAtBoundary());
0101     currentTrack_->save();
0102 
0103 #ifdef EDM_ML_DEBUG
0104     edm::LogVerbatim("DoFineCalo") << "PostUserTrackingAction:"
0105                                    << " Track " << id << " crossed boundary; pos=("
0106                                    << trkInfo_->getPositionAtBoundary().x() << ","
0107                                    << trkInfo_->getPositionAtBoundary().y() << ","
0108                                    << trkInfo_->getPositionAtBoundary().z() << ")"
0109                                    << " mom[GeV]=(" << trkInfo_->getMomentumAtBoundary().x() << ","
0110                                    << trkInfo_->getMomentumAtBoundary().y() << ","
0111                                    << trkInfo_->getMomentumAtBoundary().z() << ","
0112                                    << trkInfo_->getMomentumAtBoundary().e() << ")";
0113 #endif
0114   }
0115 
0116   bool withAncestor = (trkInfo_->getIDonCaloSurface() == id || trkInfo_->isAncestor());
0117 
0118   if (trkInfo_->isInHistory()) {
0119     // check with end-of-track information
0120     if (checkTrack_) {
0121       currentTrack_->checkAtEnd(aTrack);
0122     }
0123 
0124     eventAction_->addTrack(currentTrack_, true, withAncestor);
0125 
0126 #ifdef EDM_ML_DEBUG
0127     edm::LogVerbatim("SimTrackManager") << "TrackingAction addTrack " << id << "  "
0128                                         << aTrack->GetDefinition()->GetParticleName() << " added= " << withAncestor
0129                                         << " at " << aTrack->GetPosition();
0130 #endif
0131 
0132   } else {
0133     eventAction_->addTrack(currentTrack_, false, false);
0134 
0135 #ifdef EDM_ML_DEBUG
0136     edm::LogVerbatim("SimTrackManager") << "TrackingAction addTrack " << id << " added with " << false << " and "
0137                                         << false;
0138 #endif
0139     delete currentTrack_;
0140   }
0141   if (nullptr != steppingVerbose_) {
0142     steppingVerbose_->TrackEnded(aTrack);
0143   }
0144 
0145   EndOfTrack et(aTrack);
0146   m_endOfTrackSignal(&et);
0147 }