CMS 3D CMS Logo

TrackingAction.cc
Go to the documentation of this file.
10 
12 
13 #include "G4UImanager.hh"
14 #include "G4TrackingManager.hh"
15 #include "G4SystemOfUnits.hh"
16 
17 //#define EDM_ML_DEBUG
18 
20  : eventAction_(e),
21  steppingVerbose_(sv),
22  checkTrack_(p.getUntrackedParameter<bool>("CheckTrack", false)),
23  doFineCalo_(p.getParameter<bool>("DoFineCalo")),
24  saveCaloBoundaryInformation_(p.getParameter<bool>("SaveCaloBoundaryInformation")),
25  eMinFine_(p.getParameter<double>("EminFineTrack") * CLHEP::MeV) {
26  if (!doFineCalo_) {
27  eMinFine_ = DBL_MAX;
28  }
29  edm::LogVerbatim("SimG4CoreApplication") << "TrackingAction: boundary: " << saveCaloBoundaryInformation_
30  << "; DoFineCalo: " << doFineCalo_ << "; EminFineTrack(MeV)=" << eMinFine_;
31 }
32 
34 
35 void TrackingAction::PreUserTrackingAction(const G4Track* aTrack) {
36  g4Track_ = aTrack;
37  currentTrack_ = new TrackWithHistory(aTrack);
38 
39  BeginOfTrack bt(aTrack);
41 
42  trkInfo_ = static_cast<TrackInformation*>(aTrack->GetUserInformation());
43 
44  // Always save primaries
45  // Decays from primaries are marked as primaries (see NewTrackAction), but are not saved by
46  // default. The primary is the earliest ancestor, and it must be saved.
47  if (trkInfo_->isPrimary()) {
50  }
51  if (nullptr != steppingVerbose_) {
52  steppingVerbose_->TrackStarted(aTrack, false);
53  }
54  double ekin = aTrack->GetKineticEnergy();
55 
56 #ifdef EDM_ML_DEBUG
57  edm::LogVerbatim("DoFineCalo") << "PreUserTrackingAction: Start processing track " << aTrack->GetTrackID()
58  << " pdgid=" << aTrack->GetDefinition()->GetPDGEncoding()
59  << " ekin[GeV]=" << ekin / CLHEP::GeV << " vertex[cm]=("
60  << aTrack->GetVertexPosition().x() / CLHEP::cm << ","
61  << aTrack->GetVertexPosition().y() / CLHEP::cm << ","
62  << aTrack->GetVertexPosition().z() / CLHEP::cm << ")"
63  << " parentid=" << aTrack->GetParentID();
64 #endif
65  if (ekin > eMinFine_) {
66  // It is impossible to tell whether daughter tracks if this track may need to be saved at
67  // this point; Therefore, every track above the threshold is put in history,
68  // so that it can potentially be saved later.
70  }
71 }
72 
73 void TrackingAction::PostUserTrackingAction(const G4Track* aTrack) {
74  int id = aTrack->GetTrackID();
75  const auto& ppos = aTrack->GetStep()->GetPostStepPoint()->GetPosition();
76  math::XYZVectorD pos(ppos.x(), ppos.y(), ppos.z());
78  std::pair<math::XYZVectorD, math::XYZTLorentzVectorD> p(pos, mom);
79 
80 #ifdef EDM_ML_DEBUG
81  edm::LogVerbatim("DoFineCalo") << "PostUserTrackingAction:"
82  << " aTrack->GetTrackID()=" << id
83  << " currentTrack_->saved()=" << currentTrack_->saved();
84 #endif
85 
86  bool boundary = false;
87  if (doFineCalo_) {
88  // Add the post-step position for _every_ track
89  // in history to the TrackManager. Tracks in history _may_ be upgraded to stored
90  // tracks, at which point the post-step position is needed again.
92  boundary = true;
93  } else if (trkInfo_->storeTrack() || currentTrack_->saved() ||
97  boundary = true;
98  }
99  if (boundary && trkInfo_->crossedBoundary()) {
101  currentTrack_->save();
102 
103 #ifdef EDM_ML_DEBUG
104  edm::LogVerbatim("DoFineCalo") << "PostUserTrackingAction:"
105  << " Track " << id << " crossed boundary; pos=("
106  << trkInfo_->getPositionAtBoundary().x() << ","
107  << trkInfo_->getPositionAtBoundary().y() << ","
108  << trkInfo_->getPositionAtBoundary().z() << ")"
109  << " mom[GeV]=(" << trkInfo_->getMomentumAtBoundary().x() << ","
110  << trkInfo_->getMomentumAtBoundary().y() << ","
111  << trkInfo_->getMomentumAtBoundary().z() << ","
112  << trkInfo_->getMomentumAtBoundary().e() << ")";
113 #endif
114  }
115 
116  bool withAncestor = (trkInfo_->getIDonCaloSurface() == id || trkInfo_->isAncestor());
117 
118  if (trkInfo_->isInHistory()) {
119  // check with end-of-track information
120  if (checkTrack_) {
121  currentTrack_->checkAtEnd(aTrack);
122  }
123 
124  eventAction_->addTrack(currentTrack_, true, withAncestor);
125 
126 #ifdef EDM_ML_DEBUG
127  edm::LogVerbatim("SimTrackManager") << "TrackingAction addTrack " << id << " "
128  << aTrack->GetDefinition()->GetParticleName() << " added= " << withAncestor
129  << " at " << aTrack->GetPosition();
130 #endif
131 
132  } else {
133  eventAction_->addTrack(currentTrack_, false, false);
134 
135 #ifdef EDM_ML_DEBUG
136  edm::LogVerbatim("SimTrackManager") << "TrackingAction addTrack " << id << " added with " << false << " and "
137  << false;
138 #endif
139  delete currentTrack_;
140  }
141  if (nullptr != steppingVerbose_) {
142  steppingVerbose_->TrackEnded(aTrack);
143  }
144 
145  EndOfTrack et(aTrack);
147 }
bool storeTrack() const
Log< level::Info, true > LogVerbatim
bool isPrimary() const
bool crossedBoundary() const
ROOT::Math::LorentzVector< ROOT::Math::PxPyPzE4D< double > > XYZTLorentzVectorD
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:14
bool isAncestor() const
const math::XYZTLorentzVectorF & getPositionAtBoundary() const
const G4Track * g4Track_
void setCrossedBoundaryPosMom(int id, const math::XYZTLorentzVectorF position, const math::XYZTLorentzVectorF momentum)
CMSSteppingVerbose * steppingVerbose_
TrackWithHistory * currentTrack_
bool isInHistory() const
TrackInformation * trkInfo_
void prepareForNewPrimary()
Definition: EventAction.h:44
~TrackingAction() override
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double > > XYZVectorD
spatial vector with cartesian internal representation
Definition: Vector3D.h:8
EventAction * eventAction_
TrackingAction(EventAction *ea, const edm::ParameterSet &ps, CMSSteppingVerbose *)
void checkAtEnd(const G4Track *)
bool saveCaloBoundaryInformation_
void PreUserTrackingAction(const G4Track *aTrack) override
SimActivityRegistry::EndOfTrackSignal m_endOfTrackSignal
const math::XYZTLorentzVectorF & getMomentumAtBoundary() const
int getIDonCaloSurface() const
bool saved() const
void addTkCaloStateInfo(uint32_t t, const std::pair< math::XYZVectorD, math::XYZTLorentzVectorD > &p)
Definition: EventAction.cc:71
void TrackEnded(const G4Track *) const
void PostUserTrackingAction(const G4Track *aTrack) override
void TrackStarted(const G4Track *, bool isKilled)
void addTrack(TrackWithHistory *iTrack, bool inHistory, bool withAncestor)
Definition: EventAction.h:35
SimActivityRegistry::BeginOfTrackSignal m_beginOfTrackSignal