Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-06-20 01:36:40

0001 #include "SimG4Core/Application/interface/TrackingAction.h"
0002 
0003 #include "SimG4Core/Notification/interface/CurrentG4Track.h"
0004 #include "SimG4Core/Notification/interface/BeginOfTrack.h"
0005 #include "SimG4Core/Notification/interface/EndOfTrack.h"
0006 #include "SimG4Core/Notification/interface/TrackInformation.h"
0007 #include "SimG4Core/Notification/interface/TrackWithHistory.h"
0008 #include "SimG4Core/Notification/interface/SimTrackManager.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(SimTrackManager* stm, CMSSteppingVerbose* sv, const edm::ParameterSet& p)
0020     : trackManager_(stm),
0021       steppingVerbose_(sv),
0022       endPrintTrackID_(p.getParameter<int>("EndPrintTrackID")),
0023       checkTrack_(p.getUntrackedParameter<bool>("CheckTrack", false)),
0024       doFineCalo_(p.getParameter<bool>("DoFineCalo")),
0025       saveCaloBoundaryInformation_(p.getParameter<bool>("SaveCaloBoundaryInformation")),
0026       eMinFine_(p.getParameter<double>("EminFineTrack") * CLHEP::MeV) {
0027   if (!doFineCalo_) {
0028     eMinFine_ = DBL_MAX;
0029   }
0030   edm::LogVerbatim("SimG4CoreApplication") << "TrackingAction: boundary: " << saveCaloBoundaryInformation_
0031                                            << "; DoFineCalo: " << doFineCalo_ << "; EminFineTrack(MeV)=" << eMinFine_;
0032 }
0033 
0034 void TrackingAction::PreUserTrackingAction(const G4Track* aTrack) {
0035   g4Track_ = aTrack;
0036   currentTrack_ = new TrackWithHistory(aTrack, aTrack->GetParentID());
0037 
0038   BeginOfTrack bt(aTrack);
0039   m_beginOfTrackSignal(&bt);
0040 
0041   trkInfo_ = static_cast<TrackInformation*>(aTrack->GetUserInformation());
0042 
0043   // Always save primaries
0044   // Decays from primaries are marked as primaries, but are not saved by
0045   // default. The primary is the earliest ancestor, and it must be saved.
0046   if (trkInfo_->isPrimary()) {
0047     trackManager_->cleanTracksWithHistory();
0048     currentTrack_->setToBeSaved();
0049   }
0050   if (nullptr != steppingVerbose_) {
0051     steppingVerbose_->trackStarted(aTrack, false);
0052     if (aTrack->GetTrackID() == endPrintTrackID_) {
0053       steppingVerbose_->stopEventPrint();
0054     }
0055   }
0056   double ekin = aTrack->GetKineticEnergy();
0057 
0058 #ifdef EDM_ML_DEBUG
0059   edm::LogVerbatim("DoFineCalo") << "PreUserTrackingAction: Start processing track " << aTrack->GetTrackID()
0060                                  << " pdgid=" << aTrack->GetDefinition()->GetPDGEncoding()
0061                                  << " ekin[GeV]=" << ekin / CLHEP::GeV << " vertex[cm]=("
0062                                  << aTrack->GetVertexPosition().x() / CLHEP::cm << ","
0063                                  << aTrack->GetVertexPosition().y() / CLHEP::cm << ","
0064                                  << aTrack->GetVertexPosition().z() / CLHEP::cm << ")"
0065                                  << " parentid=" << aTrack->GetParentID();
0066 #endif
0067   if (ekin > eMinFine_) {
0068     // It is impossible to tell whether daughter tracks if this track may need to be saved at
0069     // this point; Therefore, every track above the threshold is put in history,
0070     // so that it can potentially be saved later.
0071     trkInfo_->putInHistory();
0072   }
0073 }
0074 
0075 void TrackingAction::PostUserTrackingAction(const G4Track* aTrack) {
0076   // Add the post-step position for every track in history to the TrackManager.
0077   // Tracks in history may be upgraded to stored tracks, at which point
0078   // the post-step position is needed again.
0079   int id = aTrack->GetTrackID();
0080   bool ok = (trkInfo_->storeTrack() || currentTrack_->saved());
0081   if (trkInfo_->crossedBoundary()) {
0082     currentTrack_->setCrossedBoundaryPosMom(id, trkInfo_->getPositionAtBoundary(), trkInfo_->getMomentumAtBoundary());
0083     ok = (ok || saveCaloBoundaryInformation_ || doFineCalo_);
0084   }
0085   if (ok) {
0086     currentTrack_->setToBeSaved();
0087   }
0088 
0089   bool withAncestor = (trkInfo_->getIDonCaloSurface() == id || trkInfo_->isAncestor());
0090   bool isInHistory = trkInfo_->isInHistory();
0091 
0092   trackManager_->addTrack(currentTrack_, aTrack, isInHistory, withAncestor);
0093 
0094 #ifdef EDM_ML_DEBUG
0095   edm::LogVerbatim("TrackingAction") << "TrackingAction end track=" << id << "  "
0096                                      << aTrack->GetDefinition()->GetParticleName() << " ansestor= " << withAncestor
0097                                      << " saved= " << currentTrack_->saved() << " end point " << aTrack->GetPosition();
0098 #endif
0099   if (!isInHistory) {
0100     delete currentTrack_;
0101   }
0102 
0103   EndOfTrack et(aTrack);
0104   m_endOfTrackSignal(&et);
0105 }