-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add performance profiler as stepping action #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wdconinc
wants to merge
3
commits into
main
Choose a base branch
from
performance-profiling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+124
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/plugins/include/npdet/PerformanceProfileSteppingAction.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| //========================================================================== | ||
| // AIDA Detector description implementation | ||
| //-------------------------------------------------------------------------- | ||
| // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN) | ||
| // All rights reserved. | ||
| // | ||
| // For the licensing terms see $DD4hepINSTALL/LICENSE. | ||
| // For the list of contributors see $DD4hepINSTALL/doc/CREDITS. | ||
| // | ||
| // Author : M.Frank | ||
| // | ||
| //========================================================================== | ||
| #ifndef PERFORMANCEPROFILESTEPPINGACTION_H | ||
| #define PERFORMANCEPROFILESTEPPINGACTION_H | ||
|
|
||
| #include "DDG4/Geant4SteppingAction.h" | ||
| #include "G4Step.hh" | ||
| #include "TFile.h" | ||
| #include "TH2F.h" | ||
|
|
||
| #include <chrono> | ||
|
|
||
| /// Namespace for the AIDA detector description toolkit | ||
| namespace dd4hep { | ||
|
|
||
| /// Namespace for the Geant4 based simulation part of the AIDA detector description toolkit | ||
| namespace sim { | ||
|
|
||
| using namespace std::chrono_literals; | ||
|
|
||
| class PerformanceProfileSteppingAction : public Geant4SteppingAction { | ||
| public: | ||
| /// Standard constructor with initializing arguments | ||
| PerformanceProfileSteppingAction(Geant4Context* c, const std::string& n) : Geant4SteppingAction(c, n) {}; | ||
| /// Default destructor | ||
| virtual ~PerformanceProfileSteppingAction() { | ||
| std::chrono::milliseconds total_duration = std::chrono::milliseconds::zero(); | ||
| for (auto& [track_id, duration] : m_duration) { | ||
| if (std::chrono::abs(duration) > std::chrono::nanoseconds(10ms)) { | ||
| auto p0 = m_prestep_position[track_id] / CLHEP::mm; | ||
| auto p1 = m_poststep_position[track_id] / CLHEP::mm; | ||
| auto E1 = m_pdg[track_id] == -22 ? m_poststep_energy[track_id] / CLHEP::eV | ||
| : m_poststep_energy[track_id] / CLHEP::MeV; | ||
| printout(INFO, name(), "track %d (%d): %ld ms (%f %f %f mm -> %f %f %f mm, %f (M?)eV)", track_id, | ||
| m_pdg[track_id], std::chrono::duration_cast<std::chrono::milliseconds>(duration).count(), p0.x(), | ||
| p0.y(), p0.z(), p1.x(), p1.y(), p1.z(), E1); | ||
| } | ||
| total_duration += std::chrono::duration_cast<std::chrono::milliseconds>(duration); | ||
| } | ||
| printout(INFO, name(), "total duration: %ld ms", total_duration); | ||
| TFile f("histos.root", "recreate"); | ||
| m_xy.Write(); | ||
| m_zr.Write(); | ||
| f.Close(); | ||
| }; | ||
| /// User stepping callback | ||
| void operator()(const G4Step* step, G4SteppingManager*) override { | ||
| std::chrono::nanoseconds step_duration = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - m_previous_timepoint); | ||
|
|
||
| // Get step info | ||
| auto* track = step->GetTrack(); | ||
| auto* particle = track->GetParticleDefinition(); | ||
| auto pdg = particle->GetPDGEncoding(); | ||
| auto track_id = track->GetTrackID(); | ||
| auto* prestep [[maybe_unused]] = step->GetPreStepPoint(); | ||
| auto prestep_position = prestep->GetPosition(); | ||
| auto* poststep [[maybe_unused]] = step->GetPostStepPoint(); | ||
| auto poststep_position = poststep->GetPosition(); | ||
| auto poststep_energy = poststep->GetTotalEnergy(); | ||
| auto firststep [[maybe_unused]] = step->IsFirstStepInVolume(); | ||
| auto laststep [[maybe_unused]] = step->IsLastStepInVolume(); | ||
|
|
||
| if (track_id == m_previous_track_id) { | ||
| // Current track | ||
| m_duration[track_id] += step_duration; | ||
| m_poststep_position[track_id] = poststep_position; | ||
| m_poststep_energy[track_id] = poststep_energy; | ||
| m_xy.Fill(poststep_position.x(), poststep_position.y(), step_duration.count()); | ||
| m_zr.Fill(poststep_position.z(), std::hypot(poststep_position.x(), poststep_position.y()), step_duration.count()); | ||
| } else { | ||
| // New track | ||
| if (track_id == 0) { | ||
| m_duration.clear(); | ||
| m_pdg.clear(); | ||
| m_prestep_position.clear(); | ||
| m_poststep_position.clear(); | ||
| m_poststep_energy.clear(); | ||
| printout(INFO, name(), "new event"); | ||
| } | ||
| m_previous_track_id = track_id; | ||
| m_pdg[track_id] = pdg; | ||
| m_duration[track_id] = std::chrono::nanoseconds::zero(); | ||
| m_prestep_position[track_id] = prestep_position; | ||
| } | ||
|
|
||
| m_previous_timepoint = std::chrono::steady_clock::now(); | ||
| }; | ||
|
|
||
| private: | ||
| std::map<G4int, std::chrono::nanoseconds> m_duration; | ||
| std::map<G4int, G4int> m_pdg; | ||
| std::map<G4int, G4ThreeVector> m_prestep_position; | ||
| std::map<G4int, G4ThreeVector> m_poststep_position; | ||
| std::map<G4int, G4double> m_poststep_energy; | ||
| G4int m_previous_track_id{0}; | ||
| std::chrono::time_point<std::chrono::steady_clock> m_previous_timepoint; | ||
| TH2F m_xy{"m_xy", "xy", 100, -3.*CLHEP::m, +3.*CLHEP::m, 100, -3.*CLHEP::m, +3.*CLHEP::m}; | ||
| TH2F m_zr{"m_zr", "zr", 100, -4.5*CLHEP::m, +5.5*CLHEP::m, 100, 0., +3.*CLHEP::m}; | ||
| }; | ||
| } // End namespace sim | ||
| } // End namespace dd4hep | ||
|
|
||
| #endif // PERFORMANCEPROFILESTEPPINGACTION_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #include "DDG4/Factories.h" | ||
|
|
||
| #include "npdet/PerformanceProfileSteppingAction.h" | ||
|
|
||
| DECLARE_GEANT4ACTION(PerformanceProfileSteppingAction) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this to run unconditionally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to add an argparse after https://github.com/eic/npsim/pull/32/files#diff-ec66169d33f9f45a183f1276f7d09c786eca03516f48b1ff756e3c5ed439cff8R24