From dfc3737d696833cc0e8dffe95428244d3a2f5fb0 Mon Sep 17 00:00:00 2001 From: lobis Date: Mon, 20 Jun 2022 10:42:18 +0200 Subject: [PATCH 001/100] Adding constructor to TRestGeant4Event and adding to manager --- inc/TRestGeant4Event.h | 3 +++ inc/TRestGeant4Metadata.h | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index d45fad2..5801dbc 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -38,6 +38,8 @@ #include #include +class G4Event; + /// An event class to store geant4 generated event information class TRestGeant4Event : public TRestEvent { private: @@ -435,6 +437,7 @@ class TRestGeant4Event : public TRestEvent { // Constructor TRestGeant4Event(); + TRestGeant4Event(const G4Event*, const TRestGeant4Metadata&); // Implemented in restG4 // Destructor virtual ~TRestGeant4Event(); diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index fdc09ea..8828083 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -388,7 +388,7 @@ class TRestGeant4Metadata : public TRestMetadata { /// \brief Returns the probability per event to register (write to disk) hits in the /// storage volume with index n. - inline Double_t GetStorageChance(Int_t n) { return fChance[n]; } + inline Double_t GetStorageChance(Int_t n) const { return fChance[n]; } /// Returns the probability per event to register (write to disk) hits in a /// GDML volume given its geometry name. From 946cf2239b436f2f396b3249001a2dca2b4240db Mon Sep 17 00:00:00 2001 From: lobis Date: Mon, 20 Jun 2022 12:11:31 +0200 Subject: [PATCH 002/100] Added StackingAction, updated event insertion logic --- inc/TRestGeant4Event.h | 12 +++++++++++- inc/TRestGeant4Hits.h | 6 ++++++ inc/TRestGeant4Track.h | 37 ++++++++++++++++++++++++------------- src/TRestGeant4Track.cxx | 3 ++- 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index 5801dbc..726d676 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -39,6 +39,8 @@ #include class G4Event; +class G4Track; +class G4Step; /// An event class to store geant4 generated event information class TRestGeant4Event : public TRestEvent { @@ -437,10 +439,18 @@ class TRestGeant4Event : public TRestEvent { // Constructor TRestGeant4Event(); - TRestGeant4Event(const G4Event*, const TRestGeant4Metadata&); // Implemented in restG4 // Destructor virtual ~TRestGeant4Event(); ClassDef(TRestGeant4Event, 6); // REST event superclass + + // restG4 + public: + TRestGeant4Event(const G4Event*, const TRestGeant4Metadata&); //! // Implemented in restG4 + bool InsertTrack(const G4Track*); //! + void UpdateTrack(const G4Track*); //! + void InsertStep(const G4Step*); //! + + friend class OutputManager; }; #endif diff --git a/inc/TRestGeant4Hits.h b/inc/TRestGeant4Hits.h index eb0aea2..9e102b6 100644 --- a/inc/TRestGeant4Hits.h +++ b/inc/TRestGeant4Hits.h @@ -26,6 +26,8 @@ #include "TObject.h" +class G4Step; + class TRestGeant4Hits : public TRestHits { protected: TArrayI fVolumeID; @@ -64,5 +66,9 @@ class TRestGeant4Hits : public TRestHits { virtual ~TRestGeant4Hits(); ClassDef(TRestGeant4Hits, 6); // REST event superclass + + // restG4 + public: + void InsertStep(const G4Step*); }; #endif diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index 5030110..6d43892 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -28,18 +28,22 @@ #include "TObject.h" class TRestGeant4Metadata; +class G4Track; +class G4Step; + // Perhaps there might be need for a mother class TRestTrack (if there is future need) -class TRestGeant4Track : public TObject { +class TRestGeant4Track { protected: - Int_t fTrack_ID; - Int_t fParent_ID; - - Int_t fSubEventId; + Int_t fTrackID; + Int_t fParentID; + Int_t fSubEventID = 0; // We must change this to save space! (Might be not needed after all) // Int_t fParticle_ID; TString fParticleName; + TString fCreatorProcess; + Double_t fGlobalTimestamp; // in seconds precision Double_t fTrackTimestamp; // in ns precision (seconds have been removed) Double_t fKineticEnergy; @@ -48,10 +52,12 @@ class TRestGeant4Track : public TObject { TVector3 fTrackOrigin; + Double_t fWeight; //! // Used for biasing + public: inline void Initialize() { RemoveHits(); - fSubEventId = 0.; + fSubEventID = 0.; } inline const TRestGeant4Hits& GetHits() const { return fHits; } @@ -59,8 +65,8 @@ class TRestGeant4Track : public TObject { inline Double_t GetEnergy() const { return fHits.GetEnergy(); } Int_t GetNumberOfHits(Int_t volID = -1) const; - inline Int_t GetTrackID() const { return fTrack_ID; } - inline Int_t GetParentID() const { return fParent_ID; } + inline Int_t GetTrackID() const { return fTrackID; } + inline Int_t GetParentID() const { return fParentID; } inline TString GetParticleName() const { return fParticleName; } EColor GetParticleColor() const; @@ -70,7 +76,7 @@ class TRestGeant4Track : public TObject { inline Double_t GetKineticEnergy() const { return fKineticEnergy; } inline Double_t GetTotalDepositedEnergy() const { return fHits.GetTotalDepositedEnergy(); } inline TVector3 GetTrackOrigin() const { return fTrackOrigin; } - inline Int_t GetSubEventID() const { return fSubEventId; } + inline Int_t GetSubEventID() const { return fSubEventID; } inline Double_t GetEnergyInVolume(Int_t volID) const { return fHits.GetEnergyInVolume(volID); } inline TVector3 GetMeanPositionInVolume(Int_t volID) const { @@ -83,9 +89,9 @@ class TRestGeant4Track : public TObject { return fHits.GetLastPositionInVolume(volID); } - void SetSubEventID(Int_t id) { fSubEventId = id; } - void SetTrackID(Int_t id) { fTrack_ID = id; } - void SetParentID(Int_t id) { fParent_ID = id; } + void SetSubEventID(Int_t id) { fSubEventID = id; } + void SetTrackID(Int_t id) { fTrackID = id; } + void SetParentID(Int_t id) { fParentID = id; } // void SetParticleID( Int_t id ) { fParticle_ID = id; } void SetParticleName(const TString& particleName) { fParticleName = particleName; } void SetGlobalTrackTime(Double_t time) { fGlobalTimestamp = time; } @@ -289,10 +295,15 @@ class TRestGeant4Track : public TObject { // Constructor TRestGeant4Track(); + // Destructor virtual ~TRestGeant4Track(); - ClassDef(TRestGeant4Track, 3); // REST event superclass + ClassDef(TRestGeant4Track, 4); // REST event superclass + + // restG4 + public: + explicit TRestGeant4Track(const G4Track*); //! // Implemented in restG4 }; #endif diff --git a/src/TRestGeant4Track.cxx b/src/TRestGeant4Track.cxx index 258ba53..71d145a 100644 --- a/src/TRestGeant4Track.cxx +++ b/src/TRestGeant4Track.cxx @@ -25,6 +25,7 @@ ClassImp(TRestGeant4Track); TRestGeant4Track::TRestGeant4Track() { // TRestGeant4Track default constructor + Initialize(); } TRestGeant4Track::~TRestGeant4Track() { @@ -107,7 +108,7 @@ Double_t TRestGeant4Track::GetTrackLength() const { void TRestGeant4Track::PrintTrack(int maxHits, const TRestGeant4Metadata* geant4Metadata) const { cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl; cout.precision(10); - cout << " SubEvent ID : " << fSubEventId << " Global timestamp : " << GetGlobalTime() << " seconds" + cout << " SubEvent ID : " << fSubEventID << " Global timestamp : " << GetGlobalTime() << " seconds" << endl; cout.precision(5); cout << " Track ID : " << GetTrackID() << " Parent ID : " << GetParentID(); From 4e4a6c80cb73ea374cf033bdd9d077361a513fa9 Mon Sep 17 00:00:00 2001 From: lobis Date: Mon, 20 Jun 2022 16:40:53 +0200 Subject: [PATCH 003/100] Compilation works --- inc/TRestGeant4Event.h | 8 ++++++-- inc/TRestGeant4Hits.h | 3 ++- inc/TRestGeant4Metadata.h | 1 + inc/TRestGeant4Track.h | 6 +++++- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index 726d676..5929cb0 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -442,15 +442,19 @@ class TRestGeant4Event : public TRestEvent { // Destructor virtual ~TRestGeant4Event(); - ClassDef(TRestGeant4Event, 6); // REST event superclass + ClassDef(TRestGeant4Event, 7); // REST event superclass // restG4 public: TRestGeant4Event(const G4Event*, const TRestGeant4Metadata&); //! // Implemented in restG4 bool InsertTrack(const G4Track*); //! void UpdateTrack(const G4Track*); //! - void InsertStep(const G4Step*); //! + void InsertStep(const G4Step*, TRestGeant4Metadata&); //! friend class OutputManager; + + private: + std::map fTrackIDToTrackIndex = {}; //! + TRestGeant4Hits fInitialStep; //! }; #endif diff --git a/inc/TRestGeant4Hits.h b/inc/TRestGeant4Hits.h index 9e102b6..b915385 100644 --- a/inc/TRestGeant4Hits.h +++ b/inc/TRestGeant4Hits.h @@ -25,6 +25,7 @@ #include #include "TObject.h" +#include "TRestGeant4Metadata.h" class G4Step; @@ -69,6 +70,6 @@ class TRestGeant4Hits : public TRestHits { // restG4 public: - void InsertStep(const G4Step*); + void InsertStep(const G4Step*, TRestGeant4Metadata&); }; #endif diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 8828083..3157de8 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -430,5 +430,6 @@ class TRestGeant4Metadata : public TRestMetadata { // Allow modification of otherwise inaccessible / immutable members that shouldn't be modified by the user friend class SteppingAction; friend class DetectorConstruction; + friend class TRestGeant4Hits; }; #endif // RestCore_TRestGeant4Metadata diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index 6d43892..2dc3e73 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -47,6 +47,7 @@ class TRestGeant4Track { Double_t fGlobalTimestamp; // in seconds precision Double_t fTrackTimestamp; // in ns precision (seconds have been removed) Double_t fKineticEnergy; + Double_t fTrackLength; //! TRestGeant4Hits fHits; @@ -293,6 +294,7 @@ class TRestGeant4Track { /// Prints the track information. N number of hits to print, 0 = all void PrintTrack(int maxHits = 0, const TRestGeant4Metadata* geant4Metadata = nullptr) const; + inline void SetHits(const TRestGeant4Hits& hits) { fHits = hits; } // Constructor TRestGeant4Track(); @@ -303,7 +305,9 @@ class TRestGeant4Track { // restG4 public: - explicit TRestGeant4Track(const G4Track*); //! // Implemented in restG4 + explicit TRestGeant4Track(const G4Track*); //! + void UpdateTrack(const G4Track*); //! + void InsertStep(const G4Step*, TRestGeant4Metadata&); //! }; #endif From f555dc22e55de9a7539ea8c5e779a935678935f4 Mon Sep 17 00:00:00 2001 From: lobis Date: Mon, 20 Jun 2022 17:30:55 +0200 Subject: [PATCH 004/100] Fixed problem with stepping verbose --- inc/TRestGeant4Hits.h | 2 -- inc/TRestGeant4Track.h | 5 ----- src/TRestGeant4Hits.cxx | 33 ++------------------------------- 3 files changed, 2 insertions(+), 38 deletions(-) diff --git a/inc/TRestGeant4Hits.h b/inc/TRestGeant4Hits.h index b915385..9dd360c 100644 --- a/inc/TRestGeant4Hits.h +++ b/inc/TRestGeant4Hits.h @@ -46,8 +46,6 @@ class TRestGeant4Hits : public TRestHits { Int_t GetProcess(int n) const { return fProcessID[n]; } - void AddG4Hit(TVector3 pos, Double_t en, Double_t hit_global_time, Int_t process, Int_t volume, - Double_t eKin, TVector3 momentumDirection); void RemoveG4Hits(); Int_t GetHitProcess(int n) const { return fProcessID[n]; } diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index 2dc3e73..2e1f789 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -101,11 +101,6 @@ class TRestGeant4Track { void SetTrackOrigin(const TVector3& pos) { fTrackOrigin = pos; } void SetTrackOrigin(Double_t x, Double_t y, Double_t z) { fTrackOrigin.SetXYZ(x, y, z); } - void AddG4Hit(const TVector3& pos, Double_t en, Double_t hit_global_time, Int_t pcs, Int_t vol, - Double_t eKin, const TVector3& momentumDirection) { - fHits.AddG4Hit(pos, en, hit_global_time, pcs, vol, eKin, momentumDirection); - } - Double_t GetTrackLength() const; inline static Double_t GetDistance(const TVector3& v1, const TVector3& v2) { diff --git a/src/TRestGeant4Hits.cxx b/src/TRestGeant4Hits.cxx index 14c4293..a3434e2 100644 --- a/src/TRestGeant4Hits.cxx +++ b/src/TRestGeant4Hits.cxx @@ -29,35 +29,6 @@ TRestGeant4Hits::~TRestGeant4Hits() { // TRestGeant4Hits destructor } -void TRestGeant4Hits::AddG4Hit(TVector3 pos, Double_t en, Double_t hit_global_time, Int_t process, - Int_t volume, Double_t eKin, TVector3 momentumDirection) { - AddHit(pos, en, hit_global_time); - - fProcessID.Set(fNHits); - fProcessID[fNHits - 1] = process; - - fVolumeID.Set(fNHits); - fVolumeID[fNHits - 1] = volume; - - fKineticEnergy.Set(fNHits); - fKineticEnergy[fNHits - 1] = eKin; - - momentumDirection = momentumDirection.Unit(); - - Float_t x = momentumDirection.X(); - Float_t y = momentumDirection.Y(); - Float_t z = momentumDirection.Z(); - - fMomentumDirectionX.Set(fNHits); - fMomentumDirectionX[fNHits - 1] = x; - - fMomentumDirectionY.Set(fNHits); - fMomentumDirectionY[fNHits - 1] = y; - - fMomentumDirectionZ.Set(fNHits); - fMomentumDirectionZ[fNHits - 1] = z; -} - void TRestGeant4Hits::RemoveG4Hits() { RemoveHits(); @@ -71,9 +42,9 @@ void TRestGeant4Hits::RemoveG4Hits() { Double_t TRestGeant4Hits::GetEnergyInVolume(Int_t volID) const { Double_t en = 0; - for (int n = 0; n < fNHits; n++) + for (int n = 0; n < fNHits; n++) { if (fVolumeID[n] == volID) en += GetEnergy(n); - + } return en; } From f18a40bd6f8922c6e173ea2a8f726354706f29ad Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 21 Jun 2022 10:32:15 +0200 Subject: [PATCH 005/100] TRestGeant4Event - removed unecessary variable `fNTracks` --- inc/TRestGeant4Event.h | 7 +++---- src/TRestGeant4Event.cxx | 20 +++++++++----------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index 5929cb0..1c3eb58 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -125,7 +125,6 @@ class TRestGeant4Event : public TRestEvent { std::vector fVolumeStoredNames; std::vector fVolumeDepositedEnergy; - Int_t fNTracks; std::vector fTrack; Int_t fMaxSubEventID; @@ -147,7 +146,7 @@ class TRestGeant4Event : public TRestEvent { Double_t GetPrimaryEventEnergy(Int_t n = 0) const { return fPrimaryEventEnergy[n]; } Int_t GetNumberOfHits(Int_t volID = -1) const; - inline Int_t GetNumberOfTracks() const { return fNTracks; } + inline size_t GetNumberOfTracks() const { return fTrack.size(); } inline Int_t GetNumberOfPrimaries() const { return fPrimaryEventDirection.size(); } inline Int_t GetNumberOfActiveVolumes() const { return fNVolumes; } @@ -251,11 +250,11 @@ class TRestGeant4Event : public TRestEvent { inline Int_t GetLowestTrackID() const { Int_t lowestID = 0; - if (fNTracks > 0) { + if (GetNumberOfTracks() > 0) { lowestID = GetTrack(0).GetTrackID(); } - for (int i = 0; i < fNTracks; i++) { + for (int i = 0; i < GetNumberOfTracks(); i++) { auto tr = GetTrack(i); if (tr.GetTrackID() < lowestID) lowestID = tr.GetTrackID(); } diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index db9c387..7c5f806 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -47,7 +47,6 @@ void TRestGeant4Event::Initialize() { fPrimaryEventOrigin.SetXYZ(0, 0, 0); fTrack.clear(); - fNTracks = 0; // ClearVolumes(); fXZHitGraph = nullptr; @@ -112,12 +111,11 @@ void TRestGeant4Event::SetTrackSubEventID(Int_t n, Int_t id) { if (fMaxSubEventID < id) fMaxSubEventID = id; } -void TRestGeant4Event::AddTrack(TRestGeant4Track trk) { - fTrack.push_back(trk); - fNTracks = fTrack.size(); - fTotalDepositedEnergy += trk.GetTotalDepositedEnergy(); +void TRestGeant4Event::AddTrack(TRestGeant4Track track) { + fTrack.push_back(track); + fTotalDepositedEnergy += track.GetTotalDepositedEnergy(); for (int n = 0; n < GetNumberOfActiveVolumes(); n++) - fVolumeDepositedEnergy[n] += trk.GetEnergyInVolume(n); + fVolumeDepositedEnergy[n] += track.GetEnergyInVolume(n); } Double_t TRestGeant4Event::GetTotalDepositedEnergyFromTracks() const { @@ -215,7 +213,7 @@ TVector3 TRestGeant4Event::GetLastPositionInVolume(Int_t volID) const { } TRestGeant4Track* TRestGeant4Event::GetTrackByID(int id) { - for (int i = 0; i < fNTracks; i++) + for (int i = 0; i < GetNumberOfTracks(); i++) if (fTrack[i].GetTrackID() == id) return &fTrack[i]; return nullptr; } @@ -227,7 +225,7 @@ TRestGeant4Track* TRestGeant4Event::GetTrackByID(int id) { /// Int_t TRestGeant4Event::GetNumberOfHits(Int_t volID) const { Int_t hits = 0; - for (int i = 0; i < fNTracks; i++) { + for (int i = 0; i < GetNumberOfTracks(); i++) { hits += GetTrack(i).GetNumberOfHits(volID); } return hits; @@ -240,7 +238,7 @@ Int_t TRestGeant4Event::GetNumberOfHits(Int_t volID) const { /// TRestHits TRestGeant4Event::GetHits(Int_t volID) const { TRestHits hits; - for (int t = 0; t < fNTracks; t++) { + for (int t = 0; t < GetNumberOfTracks(); t++) { const auto& g4Hits = GetTrack(t).GetHits(); for (int n = 0; n < g4Hits.GetNumberOfHits(); n++) { if (volID != -1 && g4Hits.GetVolumeId(n) != volID) continue; @@ -1153,11 +1151,11 @@ void TRestGeant4Event::PrintEvent(int maxTracks, int maxHits) const { cout << "--------------------------------------------------------------------" "-------" << endl; - cout << "Total number of tracks : " << fNTracks << endl; + cout << "Total number of tracks : " << GetNumberOfTracks() << endl; int ntracks = GetNumberOfTracks(); if (maxTracks > 0) { - ntracks = min(maxTracks, GetNumberOfTracks()); + ntracks = min(maxTracks, int(GetNumberOfTracks())); cout << " Printing only the first " << ntracks << " tracks" << endl; } From c5beead1849dd2b537e5757e1b75ecadd4070b01 Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 21 Jun 2022 11:02:08 +0200 Subject: [PATCH 006/100] using `size_t` for numbers --- inc/TRestGeant4Event.h | 3 ++- inc/TRestGeant4Track.h | 2 +- src/TRestGeant4Event.cxx | 12 +++++++----- src/TRestGeant4Track.cxx | 14 ++++++++------ 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index 1c3eb58..b474a67 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -124,6 +124,7 @@ class TRestGeant4Event : public TRestEvent { std::vector fVolumeStored; std::vector fVolumeStoredNames; std::vector fVolumeDepositedEnergy; + std::map fEnergyInVolumeMap; std::vector fTrack; @@ -145,7 +146,7 @@ class TRestGeant4Event : public TRestEvent { TVector3 GetPrimaryEventOrigin() const { return fPrimaryEventOrigin; } Double_t GetPrimaryEventEnergy(Int_t n = 0) const { return fPrimaryEventEnergy[n]; } - Int_t GetNumberOfHits(Int_t volID = -1) const; + size_t GetNumberOfHits(Int_t volID = -1) const; inline size_t GetNumberOfTracks() const { return fTrack.size(); } inline Int_t GetNumberOfPrimaries() const { return fPrimaryEventDirection.size(); } inline Int_t GetNumberOfActiveVolumes() const { return fNVolumes; } diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index 2e1f789..0d6c1a6 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -65,7 +65,7 @@ class TRestGeant4Track { inline Double_t GetEnergy() const { return fHits.GetEnergy(); } - Int_t GetNumberOfHits(Int_t volID = -1) const; + size_t GetNumberOfHits(Int_t volID = -1) const; inline Int_t GetTrackID() const { return fTrackID; } inline Int_t GetParentID() const { return fParentID; } diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index 7c5f806..b053ef7 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -111,12 +111,14 @@ void TRestGeant4Event::SetTrackSubEventID(Int_t n, Int_t id) { if (fMaxSubEventID < id) fMaxSubEventID = id; } +/* void TRestGeant4Event::AddTrack(TRestGeant4Track track) { fTrack.push_back(track); fTotalDepositedEnergy += track.GetTotalDepositedEnergy(); for (int n = 0; n < GetNumberOfActiveVolumes(); n++) fVolumeDepositedEnergy[n] += track.GetEnergyInVolume(n); } +*/ Double_t TRestGeant4Event::GetTotalDepositedEnergyFromTracks() const { Double_t eDep = 0; @@ -223,12 +225,12 @@ TRestGeant4Track* TRestGeant4Event::GetTrackByID(int id) { /// a specific volume is given as argument only the hits of that specific volume /// will be counted. /// -Int_t TRestGeant4Event::GetNumberOfHits(Int_t volID) const { - Int_t hits = 0; - for (int i = 0; i < GetNumberOfTracks(); i++) { - hits += GetTrack(i).GetNumberOfHits(volID); +size_t TRestGeant4Event::GetNumberOfHits(Int_t volID) const { + size_t numberOfHits = 0; + for (const auto& track : fTrack) { + numberOfHits += track.GetNumberOfHits(volID); } - return hits; + return numberOfHits; } /////////////////////////////////////////////// diff --git a/src/TRestGeant4Track.cxx b/src/TRestGeant4Track.cxx index 71d145a..55a8fb7 100644 --- a/src/TRestGeant4Track.cxx +++ b/src/TRestGeant4Track.cxx @@ -83,13 +83,15 @@ EColor TRestGeant4Track::GetParticleColor() const { /// the TRestGeant4Track. If a specific volume id is given as argument only /// the hits of that specific volume will be counted. /// -Int_t TRestGeant4Track::GetNumberOfHits(Int_t volID) const { - Int_t hits = 0; +size_t TRestGeant4Track::GetNumberOfHits(Int_t volID) const { + size_t numberOfHits = 0; for (int n = 0; n < fHits.GetNumberOfHits(); n++) { - if (volID != -1 && fHits.GetVolumeId(n) != volID) continue; - hits++; + if (volID != -1 && fHits.GetVolumeId(n) != volID) { + continue; + } + numberOfHits++; } - return hits; + return numberOfHits; } Double_t TRestGeant4Track::GetTrackLength() const { @@ -122,7 +124,7 @@ void TRestGeant4Track::PrintTrack(int maxHits, const TRestGeant4Metadata* geant4 int nHits = GetNumberOfHits(); if (maxHits > 0) { - nHits = min(maxHits, GetNumberOfHits()); + nHits = min(maxHits, int(GetNumberOfHits())); cout << " Printing only the first " << nHits << " hits of the track" << endl; } From d8e0cf60e0a5368e26433cfdbaa2255e9f1c7580 Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 21 Jun 2022 11:38:48 +0200 Subject: [PATCH 007/100] Storing per process energy in TRestGeant4Event --- inc/TRestGeant4Event.h | 13 +++++++++++-- src/TRestGeant4Event.cxx | 16 ++++------------ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index b474a67..5541e6b 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -57,6 +57,8 @@ class TRestGeant4Event : public TRestEvent { Bool_t PerProcessEnergyInitFlag = false; std::map PerProcessEnergyInSensitive; + std::map> fEnergyInVolumePerProcess; + // TODO: review this method void InitializePerProcessEnergyInSensitive(); @@ -158,7 +160,6 @@ class TRestGeant4Event : public TRestEvent { inline Int_t GetNumberOfSubEventIDTracks() const { return fMaxSubEventID + 1; } inline Double_t GetTotalDepositedEnergy() const { return fTotalDepositedEnergy; } - Double_t GetTotalDepositedEnergyFromTracks() const; inline Double_t GetEnergyDepositedInVolume(Int_t volID) const { return fVolumeDepositedEnergy[volID]; } inline Double_t GetSensitiveVolumeEnergy() const { return fSensitiveVolumeEnergy; } TVector3 GetMeanPositionInVolume(Int_t volID) const; @@ -172,6 +173,10 @@ class TRestGeant4Event : public TRestEvent { Int_t GetNumberOfTracksForParticle(const TString& parName) const; Int_t GetEnergyDepositedByParticle(const TString& parName) const; + const std::map> GetEnergyInVolumePerProcess() const { + return fEnergyInVolumePerProcess; + } + inline Double_t GetEnergyInSensitiveFromProcessPhoto() { if (!PerProcessEnergyInitFlag) { InitializePerProcessEnergyInSensitive(); @@ -432,7 +437,11 @@ class TRestGeant4Event : public TRestEvent { /// maxTracks : number of tracks to print, 0 = all void PrintActiveVolumes() const; - void PrintEvent(int maxTracks = 0, int maxHits = 0) const; + void PrintEvent(int maxTracks = 0, int maxHits = 0, + const TRestGeant4Metadata* geant4Metadata = nullptr) const; + inline void PrintEvent(const TRestGeant4Metadata* geant4Metadata) const { + PrintEvent(0, 0, geant4Metadata); + } inline TPad* DrawEvent(const TString& option = "") { return DrawEvent(option, true); } TPad* DrawEvent(const TString& option, Bool_t autoBoundaries); diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index b053ef7..53d1ff8 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -120,16 +120,6 @@ void TRestGeant4Event::AddTrack(TRestGeant4Track track) { } */ -Double_t TRestGeant4Event::GetTotalDepositedEnergyFromTracks() const { - Double_t eDep = 0; - - for (int tk = 0; tk < GetNumberOfTracks(); tk++) { - eDep += GetTrack(tk).GetTotalDepositedEnergy(); - } - - return eDep; -} - TVector3 TRestGeant4Event::GetMeanPositionInVolume(Int_t volID) const { TVector3 pos; Double_t eDep = 0; @@ -1120,7 +1110,9 @@ void TRestGeant4Event::PrintActiveVolumes() const { cout << "Active volume " << i << ":" << fVolumeStoredNames[i] << " has not been stored" << endl; } } -void TRestGeant4Event::PrintEvent(int maxTracks, int maxHits) const { + +void TRestGeant4Event::PrintEvent(int maxTracks, int maxHits, + const TRestGeant4Metadata* geant4Metadata) const { TRestEvent::PrintEvent(); cout.precision(4); @@ -1161,7 +1153,7 @@ void TRestGeant4Event::PrintEvent(int maxTracks, int maxHits) const { cout << " Printing only the first " << ntracks << " tracks" << endl; } - for (int n = 0; n < ntracks; n++) GetTrack(n).PrintTrack(maxHits); + for (int n = 0; n < ntracks; n++) GetTrack(n).PrintTrack(maxHits, geant4Metadata); } void TRestGeant4Event::InitializePerProcessEnergyInSensitive() { From fa659e10056c62c2dd612dea5fed6c413540e786 Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 21 Jun 2022 13:09:33 +0200 Subject: [PATCH 008/100] Added method to get unique particle names --- inc/TRestGeant4Event.h | 3 ++- src/TRestGeant4Event.cxx | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index 5541e6b..1839b0d 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -238,7 +238,6 @@ class TRestGeant4Event : public TRestEvent { return PerProcessEnergyInSensitive["neutron_elastic"]; } - inline void SetPrimaryEventOrigin(const TVector3& pos) { fPrimaryEventOrigin = pos; } inline void SetPrimaryEventDirection(const TVector3& dir) { fPrimaryEventDirection.push_back(dir); } inline void SetPrimaryEventParticleName(const TString& pName) { fPrimaryParticleName.push_back(pName); } inline void SetPrimaryEventEnergy(Double_t en) { fPrimaryEventEnergy.push_back(en); } @@ -271,6 +270,8 @@ class TRestGeant4Event : public TRestEvent { void SetTrackSubEventID(Int_t n, Int_t id); void AddTrack(TRestGeant4Track trk); + std::set GetUniqueParticles() const; + inline Bool_t isRadiactiveDecay() const { for (int n = 0; n < GetNumberOfTracks(); n++) if (GetTrack(n).isRadiactiveDecay()) return true; diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index 53d1ff8..5e0857b 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -1222,4 +1222,12 @@ void TRestGeant4Event::InitializePerProcessEnergyInSensitive() { } } } -} \ No newline at end of file +} + +set TRestGeant4Event::GetUniqueParticles() const { + set result; + for (const auto& track : fTrack) { + result.insert(track.GetParticleName().Data()); + } + return result; +} From 625a5bf9c7a00b15d675587f8d51898eb9186f52 Mon Sep 17 00:00:00 2001 From: lobis Date: Wed, 22 Jun 2022 11:34:43 +0200 Subject: [PATCH 009/100] Inserted correctly volume ID and hit KE --- inc/TRestGeant4Hits.h | 9 ++++----- src/TRestGeant4GeometryInfo.cxx | 7 +++++++ src/TRestGeant4Hits.cxx | 8 +++----- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/inc/TRestGeant4Hits.h b/inc/TRestGeant4Hits.h index 9dd360c..8ef6c0d 100644 --- a/inc/TRestGeant4Hits.h +++ b/inc/TRestGeant4Hits.h @@ -24,16 +24,15 @@ #include -#include "TObject.h" #include "TRestGeant4Metadata.h" class G4Step; class TRestGeant4Hits : public TRestHits { protected: - TArrayI fVolumeID; - TArrayI fProcessID; // [fNHits] - TArrayF fKineticEnergy; // [fNHits] + std::vector fProcessID; + std::vector fVolumeID; + std::vector fKineticEnergy; public: TArrayF fMomentumDirectionX; @@ -64,7 +63,7 @@ class TRestGeant4Hits : public TRestHits { // Destructor virtual ~TRestGeant4Hits(); - ClassDef(TRestGeant4Hits, 6); // REST event superclass + ClassDef(TRestGeant4Hits, 7); // REST event superclass // restG4 public: diff --git a/src/TRestGeant4GeometryInfo.cxx b/src/TRestGeant4GeometryInfo.cxx index dda4260..fa21188 100644 --- a/src/TRestGeant4GeometryInfo.cxx +++ b/src/TRestGeant4GeometryInfo.cxx @@ -184,6 +184,12 @@ TString TRestGeant4GeometryInfo::GetVolumeFromID(Int_t id) const { } Int_t TRestGeant4GeometryInfo::GetIDFromVolume(const TString& volumeName) const { + if (fVolumeNameReverseMap.count(volumeName) == 0) { + // if we do not find the volume we return -1 instead of default (which is 0 and may be confusing) + cout << "TRestGeant4GeometryInfo::GetIDFromVolume - volume '" << volumeName << "' not found in store!" + << endl; + return -1; + } return GetOrDefaultMapValueFromKey(&fVolumeNameReverseMap, volumeName); } @@ -201,6 +207,7 @@ void TRestGeant4GeometryInfo::Print() const { auto newName = GetAlternativeNameFromGeant4PhysicalName(physical); const auto logical = fPhysicalToLogicalVolumeMap.at(physical); cout << "\t- " << (newName == physical ? physical : newName + " (" + physical + ")") + << " - ID: " << GetIDFromVolume(physical) << " - Logical: " << fPhysicalToLogicalVolumeMap.at(physical) << " - Material: " << fLogicalToMaterialMap.at(logical) << endl; } diff --git a/src/TRestGeant4Hits.cxx b/src/TRestGeant4Hits.cxx index a3434e2..1a029d3 100644 --- a/src/TRestGeant4Hits.cxx +++ b/src/TRestGeant4Hits.cxx @@ -32,11 +32,9 @@ TRestGeant4Hits::~TRestGeant4Hits() { void TRestGeant4Hits::RemoveG4Hits() { RemoveHits(); - fProcessID.Set(0); - - fVolumeID.Set(0); - - fKineticEnergy.Set(0); + fProcessID.clear(); + fVolumeID.clear(); + fKineticEnergy.clear(); } Double_t TRestGeant4Hits::GetEnergyInVolume(Int_t volID) const { From 2df809a2ea65a1b4060e9948d8b80b302357889d Mon Sep 17 00:00:00 2001 From: lobis Date: Wed, 22 Jun 2022 13:06:33 +0200 Subject: [PATCH 010/100] Added `GetNumberOfPhysicalHits` method to return number of htis with energy > 0 --- inc/TRestGeant4Event.h | 2 ++ inc/TRestGeant4Track.h | 2 ++ src/TRestGeant4Event.cxx | 13 +++++++++++++ src/TRestGeant4Track.cxx | 19 +++++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index 1839b0d..927c4ff 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -149,6 +149,8 @@ class TRestGeant4Event : public TRestEvent { Double_t GetPrimaryEventEnergy(Int_t n = 0) const { return fPrimaryEventEnergy[n]; } size_t GetNumberOfHits(Int_t volID = -1) const; + size_t GetNumberOfPhysicalHits(Int_t volID = -1) const; + inline size_t GetNumberOfTracks() const { return fTrack.size(); } inline Int_t GetNumberOfPrimaries() const { return fPrimaryEventDirection.size(); } inline Int_t GetNumberOfActiveVolumes() const { return fNVolumes; } diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index 0d6c1a6..0371151 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -66,6 +66,8 @@ class TRestGeant4Track { inline Double_t GetEnergy() const { return fHits.GetEnergy(); } size_t GetNumberOfHits(Int_t volID = -1) const; + size_t GetNumberOfPhysicalHits(Int_t volID = -1) const; + inline Int_t GetTrackID() const { return fTrackID; } inline Int_t GetParentID() const { return fParentID; } diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index 5e0857b..b65a47f 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -223,6 +223,19 @@ size_t TRestGeant4Event::GetNumberOfHits(Int_t volID) const { return numberOfHits; } +/////////////////////////////////////////////// +/// \brief Function that returns the total number of hits with energy > 0 in the Geant4 event. If +/// a specific volume is given as argument only the hits of that specific volume +/// will be counted. +/// +size_t TRestGeant4Event::GetNumberOfPhysicalHits(Int_t volID) const { + size_t numberOfHits = 0; + for (const auto& track : fTrack) { + numberOfHits += track.GetNumberOfPhysicalHits(volID); + } + return numberOfHits; +} + /////////////////////////////////////////////// /// \brief Function that returns all the hit depositions in the Geant4 event. If /// a specific volume is given as argument only the hits of that specific volume diff --git a/src/TRestGeant4Track.cxx b/src/TRestGeant4Track.cxx index 55a8fb7..2b2d488 100644 --- a/src/TRestGeant4Track.cxx +++ b/src/TRestGeant4Track.cxx @@ -94,6 +94,25 @@ size_t TRestGeant4Track::GetNumberOfHits(Int_t volID) const { return numberOfHits; } +/////////////////////////////////////////////// +/// \brief Function that returns the number of hit depositions found inside +/// the TRestGeant4Track with energy > 0. If a specific volume id is given as argument only +/// the hits of that specific volume will be counted. +/// +size_t TRestGeant4Track::GetNumberOfPhysicalHits(Int_t volID) const { + size_t numberOfHits = 0; + for (int n = 0; n < fHits.GetNumberOfHits(); n++) { + if (volID != -1 && fHits.GetVolumeId(n) != volID) { + continue; + } + if (fHits.GetEnergy(n) <= 0) { + continue; + } + numberOfHits++; + } + return numberOfHits; +} + Double_t TRestGeant4Track::GetTrackLength() const { Double_t length = 0; From 8462fb51c33f7e7e679319a6a1d9118a9bd838e3 Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 5 Jul 2022 11:02:42 +0200 Subject: [PATCH 011/100] sync with latest dev branch --- .github/workflows/validation.yml | 55 ++++--- inc/TRestGeant4Event.h | 242 +---------------------------- inc/TRestGeant4Hits.h | 8 +- inc/TRestGeant4Track.h | 185 ++-------------------- src/TRestGeant4AnalysisProcess.cxx | 67 ++------ src/TRestGeant4Event.cxx | 139 +++++++---------- src/TRestGeant4Hits.cxx | 16 +- src/TRestGeant4Track.cxx | 57 +++++-- 8 files changed, 168 insertions(+), 601 deletions(-) diff --git a/.github/workflows/validation.yml b/.github/workflows/validation.yml index ca763d2..453aa3f 100644 --- a/.github/workflows/validation.yml +++ b/.github/workflows/validation.yml @@ -1,17 +1,17 @@ name: Validation on: - workflow_dispatch: - push: - branches: [ master ] + branches: [ "master" ] pull_request: - branches: [ master ] + branches: [ "master" ] + release: + + workflow_dispatch: env: CMAKE_BUILD_TYPE: Release - REST_PATH: /rest/framework/install - REST_FRAMEWORK_SOURCE_DIR: /rest/framework/source + REST_PATH: /rest/geant4lib/install BRANCH_NAME: ${{ github.head_ref || github.ref_name }} defaults: @@ -19,32 +19,31 @@ defaults: shell: bash jobs: - framework-install: - name: Install framework with Geant4lib + framework-validation: + uses: rest-for-physics/framework/.github/workflows/validation.yml@master + + check-library: + name: Validate library runs-on: ubuntu-latest container: image: ghcr.io/lobis/root-geant4-garfield:rest-for-physics - steps: - - name: Print version of dependencies for image - run: version.sh + - uses: actions/checkout@v3 + - run: python3 pipeline/validateLibrary.py . - - name: Checkout framework - run: | - git clone https://github.com/rest-for-physics/framework.git ${{ env.REST_FRAMEWORK_SOURCE_DIR }} - cd ${{ env.REST_FRAMEWORK_SOURCE_DIR }} - ./scripts/checkoutRemoteBranch.sh ${{ env.BRANCH_NAME }} + build-geant4lib: + name: Build only geant4lib + runs-on: ubuntu-latest + container: + image: ghcr.io/lobis/root-geant4-garfield:rest-for-physics + steps: - uses: actions/checkout@v3 - - name: Setup, build and install - run: | - cd ${{ env.REST_FRAMEWORK_SOURCE_DIR }} - rm -rf source/libraries/geant4 && cp -r $GITHUB_WORKSPACE source/libraries/geant4 - mkdir -p ${{ env.REST_FRAMEWORK_SOURCE_DIR }}/build && cd ${{ env.REST_FRAMEWORK_SOURCE_DIR }}/build - cmake ../ -DCMAKE_BUILD_TYPE=${{ env.CMAKE_BUILD_TYPE }} -DREST_WELCOME=ON -DRESTLIB_GEANT4=ON -DCMAKE_INSTALL_PREFIX=${{ env.REST_PATH }} - make -j4 install - - name: Cache framework installation - id: framework-install-geant4lib-cache - uses: actions/cache@v2 + - name: Build and install + uses: rest-for-physics/framework/.github/actions/build@master with: - path: ${{ env.REST_PATH }} - key: ${{ github.sha }} + cmake-flags: "-DCMAKE_INSTALL_PREFIX=${{ env.REST_PATH }} -DCMAKE_BUILD_TYPE=${{ env.CMAKE_BUILD_TYPE }} -DREST_WELCOME=ON -DRESTLIB_GEANT4=ON" + branch: ${{ env.BRANCH_NAME }} + - name: Load REST libraries + run: | + source ${{ env.REST_PATH }}/thisREST.sh + restRoot -b -q diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index 927c4ff..1c424dd 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -54,14 +54,6 @@ class TRestGeant4Event : public TRestEvent { void AddEnergyDepositToVolume(Int_t volID, Double_t eDep); - Bool_t PerProcessEnergyInitFlag = false; - std::map PerProcessEnergyInSensitive; - - std::map> fEnergyInVolumePerProcess; - - // TODO: review this method - void InitializePerProcessEnergyInSensitive(); - protected: #ifndef __CINT__ @@ -175,71 +167,6 @@ class TRestGeant4Event : public TRestEvent { Int_t GetNumberOfTracksForParticle(const TString& parName) const; Int_t GetEnergyDepositedByParticle(const TString& parName) const; - const std::map> GetEnergyInVolumePerProcess() const { - return fEnergyInVolumePerProcess; - } - - inline Double_t GetEnergyInSensitiveFromProcessPhoto() { - if (!PerProcessEnergyInitFlag) { - InitializePerProcessEnergyInSensitive(); - } - return PerProcessEnergyInSensitive["photoelectric"]; - } - inline Double_t GetEnergyInSensitiveFromProcessCompton() { - if (!PerProcessEnergyInitFlag) { - InitializePerProcessEnergyInSensitive(); - } - return PerProcessEnergyInSensitive["compton"]; - } - inline Double_t GetEnergyInSensitiveFromProcessEIoni() { - if (!PerProcessEnergyInitFlag) { - InitializePerProcessEnergyInSensitive(); - } - return PerProcessEnergyInSensitive["electron_ionization"]; - } - inline Double_t GetEnergyInSensitiveFromProcessIonIoni() { - if (!PerProcessEnergyInitFlag) { - InitializePerProcessEnergyInSensitive(); - } - return PerProcessEnergyInSensitive["ion_ionization"]; - } - inline Double_t GetEnergyInSensitiveFromProcessAlphaIoni() { - if (!PerProcessEnergyInitFlag) { - InitializePerProcessEnergyInSensitive(); - } - return PerProcessEnergyInSensitive["alpha_ionization"]; - } - inline Double_t GetEnergyInSensitiveFromProcessMsc() { - if (!PerProcessEnergyInitFlag) { - InitializePerProcessEnergyInSensitive(); - } - return PerProcessEnergyInSensitive["msc"]; - } - inline Double_t GetEnergyInSensitiveFromProcessHadronIoni() { - if (!PerProcessEnergyInitFlag) { - InitializePerProcessEnergyInSensitive(); - } - return PerProcessEnergyInSensitive["hadronic_ionization"]; - } - inline Double_t GetEnergyInSensitiveFromProcessProtonIoni() { - if (!PerProcessEnergyInitFlag) { - InitializePerProcessEnergyInSensitive(); - } - return PerProcessEnergyInSensitive["proton_ionization"]; - } - inline Double_t GetEnergyInSensitiveFromProcessHadronElastic() { - if (!PerProcessEnergyInitFlag) { - InitializePerProcessEnergyInSensitive(); - } - return PerProcessEnergyInSensitive["hadronic_elastic"]; - } - inline Double_t GetEnergyInSensitiveFromProcessNeutronElastic() { - if (!PerProcessEnergyInitFlag) { - InitializePerProcessEnergyInSensitive(); - } - return PerProcessEnergyInSensitive["neutron_elastic"]; - } - inline void SetPrimaryEventDirection(const TVector3& dir) { fPrimaryEventDirection.push_back(dir); } inline void SetPrimaryEventParticleName(const TString& pName) { fPrimaryParticleName.push_back(pName); } inline void SetPrimaryEventEnergy(Double_t en) { fPrimaryEventEnergy.push_back(en); } @@ -270,181 +197,14 @@ class TRestGeant4Event : public TRestEvent { } void SetTrackSubEventID(Int_t n, Int_t id); - void AddTrack(TRestGeant4Track trk); std::set GetUniqueParticles() const; - inline Bool_t isRadiactiveDecay() const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isRadiactiveDecay()) return true; - return false; - } - - inline Bool_t isPhotoElectric() const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isPhotoElectric()) return true; - return false; - } - inline Bool_t isCompton() const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isCompton()) return true; - return false; - } - inline Bool_t isBremstralung() const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isBremstralung()) return true; - return false; - } - - inline Bool_t ishadElastic() const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).ishadElastic()) return true; - return false; - } - inline Bool_t isneutronInelastic() const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isneutronInelastic()) return true; - return false; - } - - inline Bool_t isnCapture() const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isnCapture()) return true; - return false; - } - - inline Bool_t ishIoni() const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).ishIoni()) return true; - return false; - } - - inline Bool_t isphotonNuclear() const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isphotonNuclear()) return true; - return false; - } - - inline Bool_t isAlpha() const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).GetParticleName() == "alpha") return true; - return false; - } - - inline Bool_t isNeutron() const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).GetParticleName() == "neutron") return true; - return false; - } - - inline Bool_t isArgon() const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if ((GetTrack(n).GetParticleName()).Contains("Ar")) return true; - return false; - } - - inline Bool_t isXenon() const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if ((GetTrack(n).GetParticleName()).Contains("Xe")) return true; - return false; - } - - inline Bool_t isNeon() const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if ((GetTrack(n).GetParticleName()).Contains("Ne")) return true; - return false; - } - /// Processes and particles in a given volume - - inline Bool_t isRadiactiveDecayInVolume(Int_t volID) const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isRadiactiveDecayInVolume(volID)) return true; - return false; - } - - inline Bool_t isPhotoElectricInVolume(Int_t volID) const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isPhotoElectricInVolume(volID)) return true; - return false; - } - inline Bool_t isPhotonNuclearInVolume(Int_t volID) const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isPhotonNuclearInVolume(volID)) return true; - return false; - } - inline Bool_t isComptonInVolume(Int_t volID) const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isComptonInVolume(volID)) return true; - return false; - } - inline Bool_t isBremstralungInVolume(Int_t volID) const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isBremstralungInVolume(volID)) return true; - return false; - } - - inline Bool_t isHadElasticInVolume(Int_t volID) const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isHadElasticInVolume(volID)) return true; - return false; - } - inline Bool_t isNeutronInelasticInVolume(Int_t volID) const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isNeutronInelasticInVolume(volID)) return true; - return false; - } - - inline Bool_t isNCaptureInVolume(Int_t volID) const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isNCaptureInVolume(volID)) return true; - return false; - } - - inline Bool_t ishIoniInVolume(Int_t volID) const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isHIoniInVolume(volID)) return true; - return false; - } - - inline Bool_t isAlphaInVolume(Int_t volID) const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isAlphaInVolume(volID)) return true; - return false; - } - - inline Bool_t isNeutronInVolume(Int_t volID) const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isNeutronInVolume(volID)) return true; - return false; - } - - inline Bool_t isArgonInVolume(Int_t volID) const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isArgonInVolume(volID)) return true; - return false; - } - - inline Bool_t isXenonInVolume(Int_t volID) const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isXenonInVolume(volID)) return true; - return false; - } - - inline Bool_t isNeonInVolume(Int_t volID) const { - for (int n = 0; n < GetNumberOfTracks(); n++) - if (GetTrack(n).isNeonInVolume(volID)) return true; - return false; - } - void Initialize(); /// maxTracks : number of tracks to print, 0 = all void PrintActiveVolumes() const; - void PrintEvent(int maxTracks = 0, int maxHits = 0, - const TRestGeant4Metadata* geant4Metadata = nullptr) const; - inline void PrintEvent(const TRestGeant4Metadata* geant4Metadata) const { - PrintEvent(0, 0, geant4Metadata); - } + void PrintEvent(int maxTracks = 0, int maxHits = 0) const; inline TPad* DrawEvent(const TString& option = "") { return DrawEvent(option, true); } TPad* DrawEvent(const TString& option, Bool_t autoBoundaries); diff --git a/inc/TRestGeant4Hits.h b/inc/TRestGeant4Hits.h index 8ef6c0d..abbe094 100644 --- a/inc/TRestGeant4Hits.h +++ b/inc/TRestGeant4Hits.h @@ -52,11 +52,11 @@ class TRestGeant4Hits : public TRestHits { Int_t GetVolumeId(int n) const { return fVolumeID[n]; } Double_t GetKineticEnergy(int n) const { return fKineticEnergy[n]; } - Double_t GetEnergyInVolume(Int_t volID) const; + Double_t GetEnergyInVolume(Int_t volumeID) const; - TVector3 GetMeanPositionInVolume(Int_t volID) const; - TVector3 GetFirstPositionInVolume(Int_t volID) const; - TVector3 GetLastPositionInVolume(Int_t volID) const; + TVector3 GetMeanPositionInVolume(Int_t volumeID) const; + TVector3 GetFirstPositionInVolume(Int_t volumeID) const; + TVector3 GetLastPositionInVolume(Int_t volumeID) const; // Constructor TRestGeant4Hits(); diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index 0371151..029272f 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -112,184 +112,21 @@ class TRestGeant4Track { inline void RemoveHits() { fHits.RemoveHits(); } - Int_t GetProcessID(const TString& processName, const TRestGeant4Metadata* geant4Metadata = nullptr); - TString GetProcessName(Int_t id, const TRestGeant4Metadata* geant4Metadata = nullptr) const; - - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isRadiactiveDecay() const { - // TODO: improve this - for (int n = 0; n < fHits.GetNumberOfHits(); n++) { - if (fHits.GetHitProcess(n) == 6210 // RadioactiveDecay, verified on v11.0.2 - ) { - return true; - } - } - return false; - } - // WARNING: All these methods do not work correctly since the processID depends on the Geant4 version - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isPhotoElectric() const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if (fHits.GetHitProcess(n) == 3) return true; - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isCompton() const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if (fHits.GetHitProcess(n) == 7) return true; - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isBremstralung() const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if (fHits.GetHitProcess(n) == 5) return true; - return false; - } + const TRestGeant4Metadata* GetGeant4Metadata() const; - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - ishadElastic() const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if (fHits.GetHitProcess(n) == 36) return true; - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isneutronInelastic() const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if (fHits.GetHitProcess(n) == 37) return true; - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isnCapture() const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if (fHits.GetHitProcess(n) == 38) return true; - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - ishIoni() const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if (fHits.GetHitProcess(n) == 33) return true; - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isphotonNuclear() const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if (fHits.GetHitProcess(n) == 42) return true; - return false; - } - // Processes in active volume - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isRadiactiveDecayInVolume(Int_t volID) const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if ((fHits.GetHitProcess(n) == 11) && (fHits.GetHitVolume(n)) == volID) return true; - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isPhotoElectricInVolume(Int_t volID) const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if ((fHits.GetHitProcess(n) == 3) && (fHits.GetHitVolume(n)) == volID) return true; - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isPhotonNuclearInVolume(Int_t volID) const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if ((fHits.GetHitProcess(n) == 42) && (fHits.GetHitVolume(n)) == volID) return true; - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isComptonInVolume(Int_t volID) const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if ((fHits.GetHitProcess(n) == 7) && (fHits.GetHitVolume(n)) == volID) return true; - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isBremstralungInVolume(Int_t volID) const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if ((fHits.GetHitProcess(n) == 5) && (fHits.GetHitVolume(n)) == volID) return true; - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isHadElasticInVolume(Int_t volID) const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if ((fHits.GetHitProcess(n) == 36) && (fHits.GetHitVolume(n)) == volID) return true; - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isNeutronInelasticInVolume(Int_t volID) const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if ((fHits.GetHitProcess(n) == 37) && (fHits.GetHitVolume(n)) == volID) return true; - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isNCaptureInVolume(Int_t volID) const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if ((fHits.GetHitProcess(n) == 38) && (fHits.GetHitVolume(n)) == volID) return true; - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isHIoniInVolume(Int_t volID) const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if ((fHits.GetHitProcess(n) == 33) && (fHits.GetHitVolume(n)) == volID) return true; - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isAlphaInVolume(Int_t volID) const { - if (GetParticleName() == "alpha") { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if ((fHits.GetHitVolume(n)) == volID) return true; - } - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isNeutronInVolume(Int_t volID) const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if ((fHits.GetHitVolume(n) == volID) && (GetParticleName() == "neutron")) return true; - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isArgonInVolume(Int_t volID) const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if ((fHits.GetHitVolume(n) == volID) && (GetParticleName().Contains("Ar"))) return true; - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isNeonInVolume(Int_t volID) const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if ((fHits.GetHitVolume(n) == volID) && (GetParticleName().Contains("Ne"))) return true; - return false; - } - [[gnu::warning( - "May not be accurate. Use `TRestGeant4Metadata::GetGeant4PhysicsInfo` instead")]] inline Bool_t - isXenonInVolume(Int_t volID) const { - for (int n = 0; n < fHits.GetNumberOfHits(); n++) - if ((fHits.GetHitVolume(n) == volID) && (GetParticleName().Contains("Xe"))) return true; - return false; + Int_t GetProcessID(const TString& processName) const; + TString GetProcessName(Int_t id) const; + + Bool_t ContainsProcessInVolume(Int_t processID, Int_t volumeID = -1) const; + inline Bool_t ContainsProcess(Int_t processID) const { return ContainsProcessInVolume(processID, -1); } + + Bool_t ContainsProcessInVolume(const TString& processName, Int_t volumeID = -1) const; + inline Bool_t ContainsProcess(const TString& processName) const { + return ContainsProcessInVolume(processName, -1); } - ///////////////////////////////// /// Prints the track information. N number of hits to print, 0 = all - void PrintTrack(int maxHits = 0, const TRestGeant4Metadata* geant4Metadata = nullptr) const; + void PrintTrack(size_t maxHits = 0) const; inline void SetHits(const TRestGeant4Hits& hits) { fHits = hits; } // Constructor diff --git a/src/TRestGeant4AnalysisProcess.cxx b/src/TRestGeant4AnalysisProcess.cxx index ae90074..3e3a5f8 100644 --- a/src/TRestGeant4AnalysisProcess.cxx +++ b/src/TRestGeant4AnalysisProcess.cxx @@ -454,67 +454,24 @@ TRestEvent* TRestGeant4AnalysisProcess::ProcessEvent(TRestEvent* inputEvent) { Double_t energyPrimary = fOutputG4Event->GetPrimaryEventEnergy(0); SetObservableValue((string) "energyPrimary", energyPrimary); - /* }}} */ Double_t energyTotal = fOutputG4Event->GetTotalDepositedEnergy(); obsName = this->GetName() + (TString) ".totalEdep"; SetObservableValue((string) "totalEdep", energyTotal); - Int_t photo = 0; - if (fOutputG4Event->isPhotoElectric()) photo = 1; - SetObservableValue((string) "photoelectric", photo); - - Int_t compton = 0; - if (fOutputG4Event->isCompton()) compton = 1; - SetObservableValue((string) "compton", compton); - - Int_t bremstralung = 0; - if (fOutputG4Event->isBremstralung()) bremstralung = 1; - SetObservableValue((string) "bremstralung", bremstralung); - - Int_t hadElastic = 0; - if (fOutputG4Event->ishadElastic()) hadElastic = 1; - SetObservableValue((string) "hadElastic", hadElastic); - - Int_t neutronInelastic = 0; - if (fOutputG4Event->isneutronInelastic()) neutronInelastic = 1; - SetObservableValue((string) "neutronInelastic", neutronInelastic); - - Int_t nCapture = 0; - if (fOutputG4Event->isnCapture()) nCapture = 1; - SetObservableValue((string) "nCapture", nCapture); - - Int_t hIoni = 0; - if (fOutputG4Event->ishIoni()) hIoni = 1; - SetObservableValue((string) "hIoni", hIoni); - - Int_t phoNucl = 0; - if (fOutputG4Event->isphotonNuclear()) phoNucl = 1; - SetObservableValue((string) "photonNuclear", phoNucl); - - // per process energy - if (fPerProcessSensitiveEnergy) { - SetObservableValue((string) "PerProcessPhotoelectric", - fOutputG4Event->GetEnergyInSensitiveFromProcessPhoto()); - SetObservableValue((string) "PerProcessCompton", - fOutputG4Event->GetEnergyInSensitiveFromProcessCompton()); - SetObservableValue((string) "PerProcessElectronicIoni", - fOutputG4Event->GetEnergyInSensitiveFromProcessEIoni()); - SetObservableValue((string) "PerProcessIonIoni", - fOutputG4Event->GetEnergyInSensitiveFromProcessIonIoni()); - SetObservableValue((string) "PerProcessAlphaIoni", - fOutputG4Event->GetEnergyInSensitiveFromProcessAlphaIoni()); - SetObservableValue((string) "PerProcessHadronicIoni", - fOutputG4Event->GetEnergyInSensitiveFromProcessHadronIoni()); - SetObservableValue((string) "PerProcessProtonIoni", - fOutputG4Event->GetEnergyInSensitiveFromProcessProtonIoni()); - SetObservableValue((string) "PerProcessMsc", fOutputG4Event->GetEnergyInSensitiveFromProcessMsc()); - SetObservableValue((string) "PerProcessHadronElastic", - fOutputG4Event->GetEnergyInSensitiveFromProcessHadronElastic()); - SetObservableValue((string) "PerProcessNeutronElastic", - fOutputG4Event->GetEnergyInSensitiveFromProcessNeutronElastic()); + // process names as named by Geant4 + // processes present here will be added to the list of observables which can be used to see if the event + // contains the process of interest. + vector processNames = {"phot", "compt"}; + for (const auto& processName : processNames) { + Int_t containsProcess = 0; + if (fOutputG4Event->ContainsProcess(fG4Metadata->GetGeant4PhysicsInfo().GetProcessID(processName))) { + containsProcess = 1; + } + SetObservableValue("ContainsProcess" + processName, containsProcess); } + /* for (unsigned int n = 0; n < fProcessObservables.size(); n++) { string obsName = fProcessObservables[n]; TString processName = fProcessName[n]; @@ -549,6 +506,8 @@ TRestEvent* TRestGeant4AnalysisProcess::ProcessEvent(TRestEvent* inputEvent) { else SetObservableValue(obsName, 0); } + */ + for (unsigned int n = 0; n < fParticleTrackCounter.size(); n++) { Int_t nT = fOutputG4Event->GetNumberOfTracksForParticle(fParticleTrackCounter[n]); string obsName = fTrackCounterObservables[n]; diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index b65a47f..3d640a3 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -111,15 +111,6 @@ void TRestGeant4Event::SetTrackSubEventID(Int_t n, Int_t id) { if (fMaxSubEventID < id) fMaxSubEventID = id; } -/* -void TRestGeant4Event::AddTrack(TRestGeant4Track track) { - fTrack.push_back(track); - fTotalDepositedEnergy += track.GetTotalDepositedEnergy(); - for (int n = 0; n < GetNumberOfActiveVolumes(); n++) - fVolumeDepositedEnergy[n] += track.GetEnergyInVolume(n); -} -*/ - TVector3 TRestGeant4Event::GetMeanPositionInVolume(Int_t volID) const { TVector3 pos; Double_t eDep = 0; @@ -1124,8 +1115,7 @@ void TRestGeant4Event::PrintActiveVolumes() const { } } -void TRestGeant4Event::PrintEvent(int maxTracks, int maxHits, - const TRestGeant4Metadata* geant4Metadata) const { +void TRestGeant4Event::PrintEvent(int maxTracks, int maxHits) const { TRestEvent::PrintEvent(); cout.precision(4); @@ -1155,86 +1145,77 @@ void TRestGeant4Event::PrintEvent(int maxTracks, int maxHits, << " has not been stored" << endl; } - cout << "--------------------------------------------------------------------" - "-------" - << endl; - cout << "Total number of tracks : " << GetNumberOfTracks() << endl; + cout << "---------------------------------------------------------------------------" << endl; + cout << "Total number of tracks : " << fNTracks << endl; - int ntracks = GetNumberOfTracks(); + int nTracks = GetNumberOfTracks(); if (maxTracks > 0) { - ntracks = min(maxTracks, int(GetNumberOfTracks())); - cout << " Printing only the first " << ntracks << " tracks" << endl; + nTracks = min(maxTracks, GetNumberOfTracks()); + cout << " Printing only the first " << nTracks << " tracks" << endl; } - for (int n = 0; n < ntracks; n++) GetTrack(n).PrintTrack(maxHits, geant4Metadata); + for (int n = 0; n < nTracks; n++) { + GetTrack(n).PrintTrack(maxHits); + } } -void TRestGeant4Event::InitializePerProcessEnergyInSensitive() { - PerProcessEnergyInitFlag = true; - PerProcessEnergyInSensitive["photoelectric"] = 0; - PerProcessEnergyInSensitive["compton"] = 0; - PerProcessEnergyInSensitive["electron_ionization"] = 0; - PerProcessEnergyInSensitive["ion_ionization"] = 0; - PerProcessEnergyInSensitive["alpha_ionization"] = 0; - PerProcessEnergyInSensitive["msc"] = 0; - PerProcessEnergyInSensitive["hadronic_ionization"] = 0; - PerProcessEnergyInSensitive["proton_ionization"] = 0; - PerProcessEnergyInSensitive["hadronic_elastic"] = 0; - PerProcessEnergyInSensitive["neutron_elastic"] = 0; - - std::string volume_name; - std::string process_name; - Double_t energy; - - for (Int_t track_id = 0; track_id < GetNumberOfTracks(); track_id++) { - const auto& track = GetTrack(track_id); - - if (track.GetEnergyInVolume(0) == 0) { - continue; +Bool_t TRestGeant4Event::ContainsProcessInVolume(Int_t processID, Int_t volumeID) const { + for (const auto& track : fTrack) { + if (track.ContainsProcessInVolume(processID, volumeID)) { + return true; } + } + return false; +} - const auto& hits = track.GetHits(); +Bool_t TRestGeant4Event::ContainsProcessInVolume(const TString& processName, Int_t volumeID) const { + const TRestGeant4Metadata* metadata = GetGeant4Metadata(); + if (metadata == nullptr) { + return false; + } + const auto& processID = metadata->GetGeant4PhysicsInfo().GetProcessID(processName); + for (const auto& track : fTrack) { + if (track.ContainsProcessInVolume(processID, volumeID)) { + return true; + } + } + return false; +} - for (Int_t hit_id = 0; hit_id < hits.GetNumberOfHits(); hit_id++) { - if (hits.GetVolumeId(hit_id) != 0) { - continue; - } +Bool_t TRestGeant4Event::ContainsParticle(const TString& particleName) const { + for (const auto& track : fTrack) { + if (track.GetParticleName() == particleName) { + return true; + } + } + return false; +} - process_name = (std::string)track.GetProcessName(hits.GetHitProcess(hit_id)); - energy = hits.GetEnergy(hit_id); - if (process_name == "phot") { - PerProcessEnergyInSensitive["photoelectric"] += energy; - } else if (process_name == "compt") { - PerProcessEnergyInSensitive["compton"] += energy; - } else if (process_name == "eIoni" || process_name == "e-Step" || process_name == "e+Step") { - PerProcessEnergyInSensitive["electron_ionization"] += energy; - } else if (process_name == "ionIoni") { - PerProcessEnergyInSensitive["ion_ionization"] += energy; - if (track.GetParticleName() == "alpha") { - PerProcessEnergyInSensitive["alpha_ionization"] += energy; - } - } else if (process_name == "msc") { - PerProcessEnergyInSensitive["msc"] += energy; - } else if (process_name == "hIoni") { - PerProcessEnergyInSensitive["hadronic_ionization"] += energy; - if (track.GetParticleName() == "proton") { - PerProcessEnergyInSensitive["proton_ionization"] += energy; - } - } else if (process_name == "hadElastic") { - PerProcessEnergyInSensitive["hadronic_elastic"] += energy; - if (track.GetParticleName() == "neutron") { - PerProcessEnergyInSensitive["neutron_elastic"] += energy; - } - } else if (process_name == "Transportation") { - if (track.GetParticleName() == "proton") { - PerProcessEnergyInSensitive["hadronic_ionization"] += energy; - PerProcessEnergyInSensitive["proton_ionization"] += energy; - } else if (track.GetParticleName() == "e-" || track.GetParticleName() == "e+") { - PerProcessEnergyInSensitive["electron_ionization"] += energy; - } - } +Bool_t TRestGeant4Event::ContainsParticleInVolume(const TString& particleName, Int_t volumeID) const { + for (const auto& track : fTrack) { + if (track.GetParticleName() != particleName) { + continue; + } + if (track.GetHits().GetNumberOfHitsInVolume(volumeID) > 0) { + return true; } } + return false; +} + +const TRestGeant4Metadata* TRestGeant4Event::GetGeant4Metadata(const char* name) const { + return dynamic_cast(fRun->GetMetadataClass(name)); +} + +void TRestGeant4Event::InitializeReferences(TRestRun* run) { + TRestEvent::InitializeReferences(run); + /* + This introduces overhead to event loading, but hopefully its small enough. + If this is a problem, we could rework this approach + */ + for (auto& track : fTrack) { + track.SetEvent(this); + } } set TRestGeant4Event::GetUniqueParticles() const { diff --git a/src/TRestGeant4Hits.cxx b/src/TRestGeant4Hits.cxx index 1a029d3..7addeb6 100644 --- a/src/TRestGeant4Hits.cxx +++ b/src/TRestGeant4Hits.cxx @@ -37,20 +37,20 @@ void TRestGeant4Hits::RemoveG4Hits() { fKineticEnergy.clear(); } -Double_t TRestGeant4Hits::GetEnergyInVolume(Int_t volID) const { +Double_t TRestGeant4Hits::GetEnergyInVolume(Int_t volumeID) const { Double_t en = 0; for (int n = 0; n < fNHits; n++) { - if (fVolumeID[n] == volID) en += GetEnergy(n); + if (fVolumeID[n] == volumeID) en += GetEnergy(n); } return en; } -TVector3 TRestGeant4Hits::GetMeanPositionInVolume(Int_t volID) const { +TVector3 TRestGeant4Hits::GetMeanPositionInVolume(Int_t volumeID) const { TVector3 pos; Double_t en = 0; for (int n = 0; n < fNHits; n++) - if (fVolumeID[n] == volID) { + if (fVolumeID[n] == volumeID) { pos += GetPosition(n) * GetEnergy(n); en += GetEnergy(n); } @@ -64,17 +64,17 @@ TVector3 TRestGeant4Hits::GetMeanPositionInVolume(Int_t volID) const { return pos; } -TVector3 TRestGeant4Hits::GetFirstPositionInVolume(Int_t volID) const { +TVector3 TRestGeant4Hits::GetFirstPositionInVolume(Int_t volumeID) const { for (int n = 0; n < fNHits; n++) - if (fVolumeID[n] == volID) return GetPosition(n); + if (fVolumeID[n] == volumeID) return GetPosition(n); Double_t nan = TMath::QuietNaN(); return {nan, nan, nan}; } -TVector3 TRestGeant4Hits::GetLastPositionInVolume(Int_t volID) const { +TVector3 TRestGeant4Hits::GetLastPositionInVolume(Int_t volumeID) const { for (int n = fNHits - 1; n >= 0; n--) - if (fVolumeID[n] == volID) return GetPosition(n); + if (fVolumeID[n] == volumeID) return GetPosition(n); Double_t nan = TMath::QuietNaN(); return {nan, nan, nan}; diff --git a/src/TRestGeant4Track.cxx b/src/TRestGeant4Track.cxx index 2b2d488..7d88ee2 100644 --- a/src/TRestGeant4Track.cxx +++ b/src/TRestGeant4Track.cxx @@ -32,9 +32,10 @@ TRestGeant4Track::~TRestGeant4Track() { // TRestGeant4Track destructor } -Int_t TRestGeant4Track::GetProcessID(const TString& processName, const TRestGeant4Metadata* geant4Metadata) { - if (geant4Metadata != nullptr) { - const auto processID = geant4Metadata->GetGeant4PhysicsInfo().GetProcessID(processName); +Int_t TRestGeant4Track::GetProcessID(const TString& processName) const { + const TRestGeant4Metadata* metadata = GetGeant4Metadata(); + if (metadata != nullptr) { + const auto processID = metadata->GetGeant4PhysicsInfo().GetProcessID(processName); if (processID != Int_t{}) { return processID; } @@ -44,15 +45,16 @@ Int_t TRestGeant4Track::GetProcessID(const TString& processName, const TRestGean return -1; } -TString TRestGeant4Track::GetProcessName(Int_t processID, const TRestGeant4Metadata* geant4Metadata) const { - if (geant4Metadata != nullptr) { - const auto& processName = geant4Metadata->GetGeant4PhysicsInfo().GetProcessName(processID); +TString TRestGeant4Track::GetProcessName(Int_t processID) const { + const TRestGeant4Metadata* metadata = GetGeant4Metadata(); + if (metadata != nullptr) { + const auto& processName = metadata->GetGeant4PhysicsInfo().GetProcessName(processID); if (processName != TString{}) { return processName; } } - // cout << "WARNING : The process " << processID << " was not found" << endl; + cout << "WARNING : The process " << processID << " was not found" << endl; return ""; } @@ -126,10 +128,10 @@ Double_t TRestGeant4Track::GetTrackLength() const { return length; } -void TRestGeant4Track::PrintTrack(int maxHits, const TRestGeant4Metadata* geant4Metadata) const { +void TRestGeant4Track::PrintTrack(size_t maxHits) const { cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl; cout.precision(10); - cout << " SubEvent ID : " << fSubEventID << " Global timestamp : " << GetGlobalTime() << " seconds" + cout << " SubEvent ID : " << fSubEventId << " Global timestamp : " << GetGlobalTime() << " seconds" << endl; cout.precision(5); cout << " Track ID : " << GetTrackID() << " Parent ID : " << GetParentID(); @@ -143,20 +145,21 @@ void TRestGeant4Track::PrintTrack(int maxHits, const TRestGeant4Metadata* geant4 int nHits = GetNumberOfHits(); if (maxHits > 0) { - nHits = min(maxHits, int(GetNumberOfHits())); + nHits = min(maxHits, GetNumberOfHits()); cout << " Printing only the first " << nHits << " hits of the track" << endl; } + const TRestGeant4Metadata* metadata = GetGeant4Metadata(); for (int i = 0; i < nHits; i++) { - TString processName = GetProcessName(fHits.GetHitProcess(i), geant4Metadata); + TString processName = GetProcessName(fHits.GetHitProcess(i)); if (processName.IsNull()) { // in case process name is not found, use ID processName = TString(std::to_string(fHits.GetHitProcess(i))); } TString volumeName = ""; - if (geant4Metadata != nullptr) { - volumeName = geant4Metadata->GetGeant4GeometryInfo().GetVolumeFromID(fHits.GetHitVolume(i)); + if (metadata != nullptr) { + volumeName = metadata->GetGeant4GeometryInfo().GetVolumeFromID(fHits.GetHitVolume(i)); } if (volumeName.IsNull()) { // in case process name is not found, use ID @@ -172,3 +175,31 @@ void TRestGeant4Track::PrintTrack(int maxHits, const TRestGeant4Metadata* geant4 cout << endl; cout.precision(2); } + +Bool_t TRestGeant4Track::ContainsProcessInVolume(Int_t processID, Int_t volumeID) const { + for (int i = 0; i < GetNumberOfHits(); i++) { + if (fHits.GetHitProcess(i) != processID) continue; + if (volumeID == -1 || fHits.GetVolumeId(i) == volumeID) return true; + } + return false; +} + +Bool_t TRestGeant4Track::ContainsProcessInVolume(const TString& processName, Int_t volumeID) const { + const TRestGeant4Metadata* metadata = GetGeant4Metadata(); + if (metadata == nullptr) { + return false; + } + const auto& processID = metadata->GetGeant4PhysicsInfo().GetProcessID(processName); + for (int i = 0; i < GetNumberOfHits(); i++) { + if (fHits.GetHitProcess(i) != processID) continue; + if (volumeID == -1 || fHits.GetVolumeId(i) == volumeID) return true; + } + return false; +} + +const TRestGeant4Metadata* TRestGeant4Track::GetGeant4Metadata() const { + if (GetEvent() == nullptr) { + return nullptr; + } + return GetEvent()->GetGeant4Metadata(); +} From 5b4164f00216a9884eb5db18c3b27e83d273c7de Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 5 Jul 2022 11:19:54 +0200 Subject: [PATCH 012/100] working compilation --- inc/TRestGeant4Event.h | 17 ++++++++++++++++- inc/TRestGeant4Hits.h | 2 ++ inc/TRestGeant4Track.h | 10 +++++++--- src/TRestGeant4Event.cxx | 10 ++++++---- src/TRestGeant4Hits.cxx | 10 ++++++++++ src/TRestGeant4Track.cxx | 3 ++- 6 files changed, 43 insertions(+), 9 deletions(-) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index 1c424dd..b603e5f 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -119,7 +119,7 @@ class TRestGeant4Event : public TRestEvent { std::vector fVolumeStoredNames; std::vector fVolumeDepositedEnergy; std::map fEnergyInVolumeMap; - + std::map> fEnergyInVolumePerProcess; std::vector fTrack; Int_t fMaxSubEventID; @@ -200,8 +200,23 @@ class TRestGeant4Event : public TRestEvent { std::set GetUniqueParticles() const; + Bool_t ContainsProcessInVolume(Int_t processID, Int_t volumeID = -1) const; + inline Bool_t ContainsProcess(Int_t processID) const { return ContainsProcessInVolume(processID, -1); } + + Bool_t ContainsProcessInVolume(const TString& processName, Int_t volumeID = -1) const; + inline Bool_t ContainsProcess(const TString& processName) const { + return ContainsProcessInVolume(processName, -1); + } + + Bool_t ContainsParticle(const TString& particleName) const; + Bool_t ContainsParticleInVolume(const TString& particleName, Int_t volumeID = -1) const; + void Initialize(); + void InitializeReferences(TRestRun* run) override; + + const TRestGeant4Metadata* GetGeant4Metadata(const char* name = "TRestGeant4Metadata") const; + /// maxTracks : number of tracks to print, 0 = all void PrintActiveVolumes() const; void PrintEvent(int maxTracks = 0, int maxHits = 0) const; diff --git a/inc/TRestGeant4Hits.h b/inc/TRestGeant4Hits.h index abbe094..87b61bf 100644 --- a/inc/TRestGeant4Hits.h +++ b/inc/TRestGeant4Hits.h @@ -58,6 +58,8 @@ class TRestGeant4Hits : public TRestHits { TVector3 GetFirstPositionInVolume(Int_t volumeID) const; TVector3 GetLastPositionInVolume(Int_t volumeID) const; + size_t GetNumberOfHitsInVolume(Int_t volumeID) const; + // Constructor TRestGeant4Hits(); // Destructor diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index 029272f..4dfa1be 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -25,8 +25,7 @@ #include #include -#include "TObject.h" - +class TRestGeant4Event; class TRestGeant4Metadata; class G4Track; class G4Step; @@ -53,7 +52,9 @@ class TRestGeant4Track { TVector3 fTrackOrigin; - Double_t fWeight; //! // Used for biasing + Double_t fWeight = 1; //! // Used for biasing + + TRestGeant4Event* fEvent = nullptr; //! public: inline void Initialize() { @@ -63,6 +64,9 @@ class TRestGeant4Track { inline const TRestGeant4Hits& GetHits() const { return fHits; } + inline const TRestGeant4Event* GetEvent() const { return fEvent; } + inline void SetEvent(TRestGeant4Event* event) { fEvent = event; } + inline Double_t GetEnergy() const { return fHits.GetEnergy(); } size_t GetNumberOfHits(Int_t volID = -1) const; diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index 3d640a3..c39f434 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -18,10 +18,12 @@ #include "TRestGeant4Event.h" #include +#include +#include +#include #include -#include "TRestStringHelper.h" -#include "TRestTools.h" +#include "TRestGeant4Metadata.h" using namespace std; @@ -1146,11 +1148,11 @@ void TRestGeant4Event::PrintEvent(int maxTracks, int maxHits) const { } cout << "---------------------------------------------------------------------------" << endl; - cout << "Total number of tracks : " << fNTracks << endl; + cout << "Total number of tracks : " << GetNumberOfTracks() << endl; int nTracks = GetNumberOfTracks(); if (maxTracks > 0) { - nTracks = min(maxTracks, GetNumberOfTracks()); + nTracks = min(maxTracks, int(GetNumberOfTracks())); cout << " Printing only the first " << nTracks << " tracks" << endl; } diff --git a/src/TRestGeant4Hits.cxx b/src/TRestGeant4Hits.cxx index 7addeb6..365e356 100644 --- a/src/TRestGeant4Hits.cxx +++ b/src/TRestGeant4Hits.cxx @@ -79,3 +79,13 @@ TVector3 TRestGeant4Hits::GetLastPositionInVolume(Int_t volumeID) const { Double_t nan = TMath::QuietNaN(); return {nan, nan, nan}; } + +size_t TRestGeant4Hits::GetNumberOfHitsInVolume(Int_t volumeID) const { + size_t result = 0; + for (int n = 0; n < fNHits; n++) { + if (fVolumeID[n] == volumeID) { + result++; + } + } + return result; +} diff --git a/src/TRestGeant4Track.cxx b/src/TRestGeant4Track.cxx index 7d88ee2..fe91957 100644 --- a/src/TRestGeant4Track.cxx +++ b/src/TRestGeant4Track.cxx @@ -17,6 +17,7 @@ #include "TRestGeant4Track.h" +#include "TRestGeant4Event.h" #include "TRestGeant4Metadata.h" using namespace std; @@ -131,7 +132,7 @@ Double_t TRestGeant4Track::GetTrackLength() const { void TRestGeant4Track::PrintTrack(size_t maxHits) const { cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl; cout.precision(10); - cout << " SubEvent ID : " << fSubEventId << " Global timestamp : " << GetGlobalTime() << " seconds" + cout << " SubEvent ID : " << fSubEventID << " Global timestamp : " << GetGlobalTime() << " seconds" << endl; cout.precision(5); cout << " Track ID : " << GetTrackID() << " Parent ID : " << GetParentID(); From 0db6558f6614b950fbcfa8e27e0cf518a3843bf9 Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Wed, 6 Jul 2022 13:32:20 +0200 Subject: [PATCH 013/100] TRestGeant4PhysicsInfo - more tests --- inc/TRestGeant4PhysicsInfo.h | 3 +++ src/TRestGeant4PhysicsInfo.cxx | 38 ++++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/inc/TRestGeant4PhysicsInfo.h b/inc/TRestGeant4PhysicsInfo.h index 50edc7d..ab08eec 100644 --- a/inc/TRestGeant4PhysicsInfo.h +++ b/inc/TRestGeant4PhysicsInfo.h @@ -4,6 +4,7 @@ #include #include +#include #include class G4VProcess; @@ -22,10 +23,12 @@ class TRestGeant4PhysicsInfo { TString GetProcessName(Int_t id) const; Int_t GetProcessID(const TString& processName) const; void InsertProcessName(Int_t id, const TString& processName); + std::set GetAllParticles() const; TString GetParticleName(Int_t id) const; Int_t GetParticleID(const TString& processName) const; void InsertParticleName(Int_t id, const TString& particleName); + std::set GetAllProcesses() const; public: inline TRestGeant4PhysicsInfo() = default; diff --git a/src/TRestGeant4PhysicsInfo.cxx b/src/TRestGeant4PhysicsInfo.cxx index 4f7e4da..665335e 100644 --- a/src/TRestGeant4PhysicsInfo.cxx +++ b/src/TRestGeant4PhysicsInfo.cxx @@ -7,29 +7,37 @@ using namespace std; ClassImp(TRestGeant4PhysicsInfo); -void TRestGeant4PhysicsInfo::PrintParticles() const { - vector ids = {}; - for (const auto& [id, _] : fParticleNamesMap) { - ids.push_back(id); +set TRestGeant4PhysicsInfo::GetAllParticles() const { + set particles = {}; + for (const auto& [_, name] : fParticleNamesMap) { + particles.insert(name); } - sort(ids.begin(), ids.end()); + return particles; +} - cout << "Particles:" << endl; - for (const auto& id : ids) { - cout << "\t" << id << " - " << GetParticleName(id) << endl; +std::set TRestGeant4PhysicsInfo::GetAllProcesses() const { + set processes = {}; + for (const auto& [_, name] : fProcessNamesMap) { + processes.insert(name); } + return processes; } -void TRestGeant4PhysicsInfo::PrintProcesses() const { - vector ids = {}; - for (const auto& [id, _] : fProcessNamesMap) { - ids.push_back(id); +void TRestGeant4PhysicsInfo::PrintParticles() const { + const auto particleNames = GetAllParticles(); + cout << "Particles:" << endl; + for (const auto& name : particleNames) { + const auto id = GetParticleID(name); + cout << "\t" << name << " - " << id << endl; } - sort(ids.begin(), ids.end()); +} +void TRestGeant4PhysicsInfo::PrintProcesses() const { + const auto processNames = GetAllProcesses(); cout << "Processes:" << endl; - for (const auto& id : ids) { - cout << "\t" << id << " - " << GetProcessName(id) << endl; + for (const auto& name : processNames) { + const auto id = GetProcessID(name); + cout << "\t" << name << " - " << id << endl; } } From 784db412d99cf90ca7b218184829a2cf3110c199 Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Wed, 6 Jul 2022 14:29:59 +0200 Subject: [PATCH 014/100] TRestGeant4Event - added new members to better track subevent info, updated validations --- inc/TRestGeant4Event.h | 50 +++++++++++++++++++++------------------- src/TRestGeant4Event.cxx | 41 +++++++++++++++----------------- 2 files changed, 45 insertions(+), 46 deletions(-) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index 1c9bd8e..67f393b 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -29,15 +29,15 @@ #include #include #include -#include #include -#include #include #include #include #include +#include "TRestGeant4Track.h" + class G4Event; class G4Track; class G4Step; @@ -106,11 +106,15 @@ class TRestGeant4Event : public TRestEvent { TH1D* GetZHistogram(Int_t gridElement, std::vector optList); #endif - TVector3 fPrimaryEventOrigin; + TVector3 fPrimaryPosition; + std::vector fPrimaryParticleNames; + std::vector fPrimaryEnergies; + std::vector fPrimaryDirections; - std::vector fPrimaryParticleName; - std::vector fPrimaryEventDirection; - std::vector fPrimaryEventEnergy; + TString fSubEventPrimaryParticleName; + Double_t fSubEventPrimaryEnergy; + TVector3 fSubEventPrimaryPosition; + TVector3 fSubEventPrimaryDirection; Double_t fTotalDepositedEnergy; Double_t fSensitiveVolumeEnergy; @@ -121,7 +125,7 @@ class TRestGeant4Event : public TRestEvent { std::vector fVolumeDepositedEnergy; std::map fEnergyInVolumeMap; std::map> fEnergyInVolumePerProcess; - std::vector fTrack; + std::vector fTracks; Int_t fMaxSubEventID; @@ -130,27 +134,28 @@ class TRestGeant4Event : public TRestEvent { void SetBoundaries(Double_t xMin, Double_t xMax, Double_t yMin, Double_t yMax, Double_t zMin, Double_t zMax); - inline TString GetPrimaryEventParticleName(int n) const { - if (fPrimaryParticleName.size() > n) { - return fPrimaryParticleName[n]; - } - return "Not defined"; - } + inline size_t GetNumberOfPrimaries() const { return fPrimaryParticleNames.size(); } + + inline TString GetPrimaryEventParticleName(size_t n = 0) const { return fPrimaryParticleNames[n]; } + inline TVector3 GetPrimaryEventDirection(size_t n = 0) const { return fPrimaryDirections[n]; } + inline TVector3 GetPrimaryEventOrigin() const { return fPrimaryPosition; } + inline Double_t GetPrimaryEventEnergy(size_t n = 0) const { return fPrimaryEnergies[n]; } - TVector3 GetPrimaryEventDirection(Int_t n = 0) const { return fPrimaryEventDirection[n]; } - TVector3 GetPrimaryEventOrigin() const { return fPrimaryEventOrigin; } - Double_t GetPrimaryEventEnergy(Int_t n = 0) const { return fPrimaryEventEnergy[n]; } + inline Bool_t IsSubEvent() const { return fSubEventID > 0; } + inline TString GetSubEventPrimaryEventParticleName() const { return fSubEventPrimaryParticleName; } + inline TVector3 GetSubEventPrimaryEventDirection() const { return fSubEventPrimaryDirection; } + inline TVector3 GetSubEventPrimaryEventOrigin() const { return fSubEventPrimaryPosition; } + inline Double_t GetSubEventPrimaryEventEnergy() const { return fSubEventPrimaryEnergy; } size_t GetNumberOfHits(Int_t volID = -1) const; size_t GetNumberOfPhysicalHits(Int_t volID = -1) const; - inline size_t GetNumberOfTracks() const { return fTrack.size(); } - inline Int_t GetNumberOfPrimaries() const { return fPrimaryEventDirection.size(); } + inline size_t GetNumberOfTracks() const { return fTracks.size(); } inline Int_t GetNumberOfActiveVolumes() const { return fNVolumes; } inline Int_t isVolumeStored(int n) const { return fVolumeStored[n]; } - inline const TRestGeant4Track& GetTrack(int n) const { return fTrack[n]; } - inline TRestGeant4Track* GetTrackPointer(int n) { return &fTrack[n]; } + inline const TRestGeant4Track& GetTrack(int n) const { return fTracks[n]; } + inline TRestGeant4Track* GetTrackPointer(int n) { return &fTracks[n]; } TRestGeant4Track* GetTrackByID(int id); inline Int_t GetNumberOfSubEventIDTracks() const { return fMaxSubEventID + 1; } @@ -168,9 +173,6 @@ class TRestGeant4Event : public TRestEvent { Int_t GetNumberOfTracksForParticle(const TString& parName) const; Int_t GetEnergyDepositedByParticle(const TString& parName) const; - inline void SetPrimaryEventDirection(const TVector3& dir) { fPrimaryEventDirection.push_back(dir); } - inline void SetPrimaryEventParticleName(const TString& pName) { fPrimaryParticleName.push_back(pName); } - inline void SetPrimaryEventEnergy(Double_t en) { fPrimaryEventEnergy.push_back(en); } inline void ActivateVolumeForStorage(Int_t n) { fVolumeStored[n] = 1; } inline void DisableVolumeForStorage(Int_t n) { fVolumeStored[n] = 0; } @@ -230,7 +232,7 @@ class TRestGeant4Event : public TRestEvent { // Destructor virtual ~TRestGeant4Event(); - ClassDef(TRestGeant4Event, 7); // REST event superclass + ClassDef(TRestGeant4Event, 8); // REST event superclass // restG4 public: diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index c39f434..9f0caab 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -43,12 +43,9 @@ TRestGeant4Event::~TRestGeant4Event() { void TRestGeant4Event::Initialize() { TRestEvent::Initialize(); - fPrimaryParticleName.clear(); - fPrimaryEventDirection.clear(); - fPrimaryEventEnergy.clear(); - fPrimaryEventOrigin.SetXYZ(0, 0, 0); + fPrimaryParticleNames.clear(); - fTrack.clear(); + fTracks.clear(); // ClearVolumes(); fXZHitGraph = nullptr; @@ -109,7 +106,7 @@ void TRestGeant4Event::AddEnergyDepositToVolume(Int_t volID, Double_t eDep) { } void TRestGeant4Event::SetTrackSubEventID(Int_t n, Int_t id) { - fTrack[n].SetSubEventID(id); + fTracks[n].SetSubEventID(id); if (fMaxSubEventID < id) fMaxSubEventID = id; } @@ -199,7 +196,7 @@ TVector3 TRestGeant4Event::GetLastPositionInVolume(Int_t volID) const { TRestGeant4Track* TRestGeant4Event::GetTrackByID(int id) { for (int i = 0; i < GetNumberOfTracks(); i++) - if (fTrack[i].GetTrackID() == id) return &fTrack[i]; + if (fTracks[i].GetTrackID() == id) return &fTracks[i]; return nullptr; } @@ -210,7 +207,7 @@ TRestGeant4Track* TRestGeant4Event::GetTrackByID(int id) { /// size_t TRestGeant4Event::GetNumberOfHits(Int_t volID) const { size_t numberOfHits = 0; - for (const auto& track : fTrack) { + for (const auto& track : fTracks) { numberOfHits += track.GetNumberOfHits(volID); } return numberOfHits; @@ -223,7 +220,7 @@ size_t TRestGeant4Event::GetNumberOfHits(Int_t volID) const { /// size_t TRestGeant4Event::GetNumberOfPhysicalHits(Int_t volID) const { size_t numberOfHits = 0; - for (const auto& track : fTrack) { + for (const auto& track : fTracks) { numberOfHits += track.GetNumberOfPhysicalHits(volID); } return numberOfHits; @@ -1124,15 +1121,15 @@ void TRestGeant4Event::PrintEvent(int maxTracks, int maxHits) const { cout << "Total energy : " << fTotalDepositedEnergy << " keV" << endl; cout << "Sensitive volume energy : " << fSensitiveVolumeEnergy << " keV" << endl; - cout << "Source origin : (" << fPrimaryEventOrigin.X() << "," << fPrimaryEventOrigin.Y() << "," - << fPrimaryEventOrigin.Z() << ") mm" << endl; + cout << "Source origin : (" << fPrimaryPosition.X() << "," << fPrimaryPosition.Y() << "," + << fPrimaryPosition.Z() << ") mm" << endl; for (int n = 0; n < GetNumberOfPrimaries(); n++) { - const auto dir = &fPrimaryEventDirection[n]; - cout << "Source " << n << " Particle name : " << GetPrimaryEventParticleName(n) << endl; - cout << "Source " << n << " direction : (" << dir->X() << "," << dir->Y() << "," << dir->Z() << ")" - << endl; - cout << "Source " << n << " energy : " << fPrimaryEventEnergy[n] << " keV" << endl; + const auto& direction = fPrimaryDirections[n]; + cout << "Source " << n << " Particle name : " << fPrimaryParticleNames[n] << endl; + cout << "Source " << n << " direction : (" << direction.X() << "," << direction.Y() << "," + << direction.Z() << ")" << endl; + cout << "Source " << n << " energy : " << fPrimaryEnergies[n] << " keV" << endl; } cout << "Number of active volumes : " << GetNumberOfActiveVolumes() << endl; @@ -1162,7 +1159,7 @@ void TRestGeant4Event::PrintEvent(int maxTracks, int maxHits) const { } Bool_t TRestGeant4Event::ContainsProcessInVolume(Int_t processID, Int_t volumeID) const { - for (const auto& track : fTrack) { + for (const auto& track : fTracks) { if (track.ContainsProcessInVolume(processID, volumeID)) { return true; } @@ -1176,7 +1173,7 @@ Bool_t TRestGeant4Event::ContainsProcessInVolume(const TString& processName, Int return false; } const auto& processID = metadata->GetGeant4PhysicsInfo().GetProcessID(processName); - for (const auto& track : fTrack) { + for (const auto& track : fTracks) { if (track.ContainsProcessInVolume(processID, volumeID)) { return true; } @@ -1185,7 +1182,7 @@ Bool_t TRestGeant4Event::ContainsProcessInVolume(const TString& processName, Int } Bool_t TRestGeant4Event::ContainsParticle(const TString& particleName) const { - for (const auto& track : fTrack) { + for (const auto& track : fTracks) { if (track.GetParticleName() == particleName) { return true; } @@ -1194,7 +1191,7 @@ Bool_t TRestGeant4Event::ContainsParticle(const TString& particleName) const { } Bool_t TRestGeant4Event::ContainsParticleInVolume(const TString& particleName, Int_t volumeID) const { - for (const auto& track : fTrack) { + for (const auto& track : fTracks) { if (track.GetParticleName() != particleName) { continue; } @@ -1215,14 +1212,14 @@ void TRestGeant4Event::InitializeReferences(TRestRun* run) { This introduces overhead to event loading, but hopefully its small enough. If this is a problem, we could rework this approach */ - for (auto& track : fTrack) { + for (auto& track : fTracks) { track.SetEvent(this); } } set TRestGeant4Event::GetUniqueParticles() const { set result; - for (const auto& track : fTrack) { + for (const auto& track : fTracks) { result.insert(track.GetParticleName().Data()); } return result; From f3af1ddd3865f4da4200486bd20b2e56229f04a8 Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Wed, 6 Jul 2022 16:59:12 +0200 Subject: [PATCH 015/100] removed metadata injection from constructors --- inc/TRestGeant4Event.h | 10 ++++------ inc/TRestGeant4Hits.h | 17 +++++++++++++++-- inc/TRestGeant4Track.h | 25 ++++++++++--------------- src/TRestGeant4Event.cxx | 5 ----- src/TRestGeant4Hits.cxx | 25 +++++++++++++++++++------ src/TRestGeant4Track.cxx | 28 +++++++++------------------- 6 files changed, 57 insertions(+), 53 deletions(-) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index 67f393b..7af0215 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -199,8 +199,6 @@ class TRestGeant4Event : public TRestEvent { return lowestID; } - void SetTrackSubEventID(Int_t n, Int_t id); - std::set GetUniqueParticles() const; Bool_t ContainsProcessInVolume(Int_t processID, Int_t volumeID = -1) const; @@ -236,10 +234,10 @@ class TRestGeant4Event : public TRestEvent { // restG4 public: - TRestGeant4Event(const G4Event*, const TRestGeant4Metadata&); //! // Implemented in restG4 - bool InsertTrack(const G4Track*); //! - void UpdateTrack(const G4Track*); //! - void InsertStep(const G4Step*, TRestGeant4Metadata&); //! + TRestGeant4Event(const G4Event*); //! // Implemented in restG4 + bool InsertTrack(const G4Track*); //! + void UpdateTrack(const G4Track*); //! + void InsertStep(const G4Step*); //! friend class OutputManager; diff --git a/inc/TRestGeant4Hits.h b/inc/TRestGeant4Hits.h index 87b61bf..ca1098d 100644 --- a/inc/TRestGeant4Hits.h +++ b/inc/TRestGeant4Hits.h @@ -27,6 +27,8 @@ #include "TRestGeant4Metadata.h" class G4Step; +class TRestGeant4Track; +class TRestGeant4Event; class TRestGeant4Hits : public TRestHits { protected: @@ -34,7 +36,18 @@ class TRestGeant4Hits : public TRestHits { std::vector fVolumeID; std::vector fKineticEnergy; + TRestGeant4Track* fTrack = nullptr; //! + TRestGeant4Event* fEvent = nullptr; //! + public: + TRestGeant4Metadata* GetGeant4Metadata() const; + + const TRestGeant4Track* GetTrack() const { return fTrack; } + void SetTrack(TRestGeant4Track* track) { fTrack = track; } + + const TRestGeant4Event* GetEvent() const { return fEvent; } + void SetEvent(TRestGeant4Event* event) { fEvent = event; } + TArrayF fMomentumDirectionX; TArrayF fMomentumDirectionY; TArrayF fMomentumDirectionZ; @@ -65,10 +78,10 @@ class TRestGeant4Hits : public TRestHits { // Destructor virtual ~TRestGeant4Hits(); - ClassDef(TRestGeant4Hits, 7); // REST event superclass + ClassDef(TRestGeant4Hits, 8); // REST event superclass // restG4 public: - void InsertStep(const G4Step*, TRestGeant4Metadata&); + void InsertStep(const G4Step*); }; #endif diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index d011880..e7e824b 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -35,10 +35,12 @@ class TRestGeant4Track { protected: Int_t fTrackID; Int_t fParentID; - Int_t fSubEventID = 0; // We must change this to save space! (Might be not needed after all) TString fParticleName; + + TRestGeant4Hits fHits; + TString fCreatorProcess; Double_t fGlobalTimestamp; // in seconds precision @@ -46,8 +48,6 @@ class TRestGeant4Track { Double_t fKineticEnergy; Double_t fTrackLength; //! - TRestGeant4Hits fHits; - TVector3 fTrackOrigin; Double_t fWeight = 1; // Used for biasing @@ -55,11 +55,6 @@ class TRestGeant4Track { TRestGeant4Event* fEvent = nullptr; //! public: - inline void Initialize() { - RemoveHits(); - fSubEventID = 0.; - } - inline const TRestGeant4Hits& GetHits() const { return fHits; } inline const TRestGeant4Event* GetEvent() const { return fEvent; } @@ -83,7 +78,6 @@ class TRestGeant4Track { inline Double_t GetKineticEnergy() const { return fKineticEnergy; } inline Double_t GetTotalDepositedEnergy() const { return fHits.GetTotalDepositedEnergy(); } inline TVector3 GetTrackOrigin() const { return fTrackOrigin; } - inline Int_t GetSubEventID() const { return fSubEventID; } inline Double_t GetEnergyInVolume(Int_t volID) const { return fHits.GetEnergyInVolume(volID); } inline TVector3 GetMeanPositionInVolume(Int_t volID) const { @@ -96,10 +90,8 @@ class TRestGeant4Track { return fHits.GetLastPositionInVolume(volID); } - void SetSubEventID(Int_t id) { fSubEventID = id; } void SetTrackID(Int_t id) { fTrackID = id; } void SetParentID(Int_t id) { fParentID = id; } - // void SetParticleID( Int_t id ) { fParticle_ID = id; } void SetParticleName(const TString& particleName) { fParticleName = particleName; } void SetGlobalTrackTime(Double_t time) { fGlobalTimestamp = time; } void SetTrackTimeLength(Double_t time) { fTrackTimestamp = time; } @@ -132,7 +124,10 @@ class TRestGeant4Track { /// Prints the track information. N number of hits to print, 0 = all void PrintTrack(size_t maxHits = 0) const; - inline void SetHits(const TRestGeant4Hits& hits) { fHits = hits; } + inline void SetHits(const TRestGeant4Hits& hits) { + fHits = hits; + fHits.SetTrack(this); + } // Constructor TRestGeant4Track(); @@ -143,9 +138,9 @@ class TRestGeant4Track { // restG4 public: - explicit TRestGeant4Track(const G4Track*); //! - void UpdateTrack(const G4Track*); //! - void InsertStep(const G4Step*, TRestGeant4Metadata&); //! + explicit TRestGeant4Track(const G4Track*); //! + void UpdateTrack(const G4Track*); //! + void InsertStep(const G4Step*); //! }; #endif diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index 9f0caab..f0ab59d 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -105,11 +105,6 @@ void TRestGeant4Event::AddEnergyDepositToVolume(Int_t volID, Double_t eDep) { fVolumeDepositedEnergy[volID] += eDep; } -void TRestGeant4Event::SetTrackSubEventID(Int_t n, Int_t id) { - fTracks[n].SetSubEventID(id); - if (fMaxSubEventID < id) fMaxSubEventID = id; -} - TVector3 TRestGeant4Event::GetMeanPositionInVolume(Int_t volID) const { TVector3 pos; Double_t eDep = 0; diff --git a/src/TRestGeant4Hits.cxx b/src/TRestGeant4Hits.cxx index e3fbb36..6178fa8 100644 --- a/src/TRestGeant4Hits.cxx +++ b/src/TRestGeant4Hits.cxx @@ -17,17 +17,16 @@ #include "TRestGeant4Hits.h" +#include "TRestGeant4Event.h" +#include "TRestGeant4Track.h" + using namespace std; ClassImp(TRestGeant4Hits); -TRestGeant4Hits::TRestGeant4Hits() : TRestHits() { - // TRestGeant4Hits default constructor -} +TRestGeant4Hits::TRestGeant4Hits() : TRestHits() {} -TRestGeant4Hits::~TRestGeant4Hits() { - // TRestGeant4Hits destructor -} +TRestGeant4Hits::~TRestGeant4Hits() {} void TRestGeant4Hits::RemoveG4Hits() { RemoveHits(); @@ -90,3 +89,17 @@ size_t TRestGeant4Hits::GetNumberOfHitsInVolume(Int_t volumeID) const { } return result; } + +TRestGeant4Metadata* TRestGeant4Hits::GetGeant4Metadata() const { + const TRestGeant4Event* event = nullptr; + if (fTrack != nullptr) { + event = fTrack->GetEvent(); + } else { + event = fEvent; + } + if (event == nullptr) { + cout << "null event" << endl; + return nullptr; + } + return const_cast(event->GetGeant4Metadata()); +} diff --git a/src/TRestGeant4Track.cxx b/src/TRestGeant4Track.cxx index 027aea6..50087cd 100644 --- a/src/TRestGeant4Track.cxx +++ b/src/TRestGeant4Track.cxx @@ -24,14 +24,9 @@ using namespace std; ClassImp(TRestGeant4Track); -TRestGeant4Track::TRestGeant4Track() { - // TRestGeant4Track default constructor - Initialize(); -} +TRestGeant4Track::TRestGeant4Track() {} -TRestGeant4Track::~TRestGeant4Track() { - // TRestGeant4Track destructor -} +TRestGeant4Track::~TRestGeant4Track() {} Int_t TRestGeant4Track::GetProcessID(const TString& processName) const { const TRestGeant4Metadata* metadata = GetGeant4Metadata(); @@ -131,19 +126,14 @@ Double_t TRestGeant4Track::GetTrackLength() const { void TRestGeant4Track::PrintTrack(size_t maxHits) const { cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl; - cout.precision(10); - cout << " SubEvent ID : " << fSubEventID << " Global timestamp : " << GetGlobalTime() << " seconds" - << endl; cout.precision(5); - cout << " Track ID : " << GetTrackID() << " Parent ID : " << GetParentID() - << " Created by process: " << fCreatorProcess; - cout << " Particle : " << GetParticleName() << " Time track length : " << GetTrackTimeLength() << " us" - << endl; - cout << " Origin : X = " << GetTrackOrigin().X() << "mm Y = " << GetTrackOrigin().Y() - << "mm Z = " << GetTrackOrigin().Z() << "mm Ekin : " << GetKineticEnergy() << " keV" << endl; - cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" - "++++++++++++" - << endl; + cout << " Particle : " << GetParticleName() << " Track ID: " << GetTrackID() + << " Parent ID: " << GetParentID() << " Created by process: " << fCreatorProcess + << " Global timestamp: " << fGlobalTimestamp << " Time track length: " << GetTrackTimeLength() + << " us" << endl; + cout << " Origin: (" << GetTrackOrigin().X() << ", " << GetTrackOrigin().Y() << ", " + << GetTrackOrigin().Z() << ") mm Initial KE: " << GetKineticEnergy() << " keV" << endl; + cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl; int nHits = GetNumberOfHits(); if (maxHits > 0) { From 8a106bfea4e41205005f1fd144efe5db64eb3b31 Mon Sep 17 00:00:00 2001 From: lobis Date: Thu, 7 Jul 2022 19:57:45 +0200 Subject: [PATCH 016/100] storing momentum as TVector3 --- inc/TRestGeant4Hits.h | 37 +++++++++++++++---------------------- inc/TRestGeant4Track.h | 24 ++++++++++-------------- 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/inc/TRestGeant4Hits.h b/inc/TRestGeant4Hits.h index ca1098d..6e429a9 100644 --- a/inc/TRestGeant4Hits.h +++ b/inc/TRestGeant4Hits.h @@ -18,8 +18,6 @@ #ifndef RestCore_TRestGeant4Hits #define RestCore_TRestGeant4Hits -#include -#include #include #include @@ -32,9 +30,10 @@ class TRestGeant4Event; class TRestGeant4Hits : public TRestHits { protected: - std::vector fProcessID; - std::vector fVolumeID; - std::vector fKineticEnergy; + std::vector fProcessID = {}; + std::vector fVolumeID = {}; + std::vector fKineticEnergy = {}; + std::vector fMomentumDirection = {}; TRestGeant4Track* fTrack = nullptr; //! TRestGeant4Event* fEvent = nullptr; //! @@ -42,28 +41,22 @@ class TRestGeant4Hits : public TRestHits { public: TRestGeant4Metadata* GetGeant4Metadata() const; - const TRestGeant4Track* GetTrack() const { return fTrack; } - void SetTrack(TRestGeant4Track* track) { fTrack = track; } + inline const TRestGeant4Track* GetTrack() const { return fTrack; } + inline void SetTrack(TRestGeant4Track* track) { fTrack = track; } - const TRestGeant4Event* GetEvent() const { return fEvent; } - void SetEvent(TRestGeant4Event* event) { fEvent = event; } + inline const TRestGeant4Event* GetEvent() const { return fEvent; } + inline void SetEvent(TRestGeant4Event* event) { fEvent = event; } - TArrayF fMomentumDirectionX; - TArrayF fMomentumDirectionY; - TArrayF fMomentumDirectionZ; + inline TVector3 GetMomentumDirection(size_t n) const { return fMomentumDirection[n]; } - TVector3 GetMomentumDirection(int n) const { - return {fMomentumDirectionX[n], fMomentumDirectionY[n], fMomentumDirectionZ[n]}; - } - - Int_t GetProcess(int n) const { return fProcessID[n]; } + inline Int_t GetProcess(size_t n) const { return fProcessID[n]; } void RemoveG4Hits(); - Int_t GetHitProcess(int n) const { return fProcessID[n]; } - Int_t GetHitVolume(int n) const { return fVolumeID[n]; } - Int_t GetVolumeId(int n) const { return fVolumeID[n]; } - Double_t GetKineticEnergy(int n) const { return fKineticEnergy[n]; } + inline Int_t GetHitProcess(size_t n) const { return fProcessID[n]; } + inline Int_t GetHitVolume(size_t n) const { return fVolumeID[n]; } + inline Int_t GetVolumeId(size_t n) const { return fVolumeID[n]; } + inline Double_t GetKineticEnergy(size_t n) const { return fKineticEnergy[n]; } Double_t GetEnergyInVolume(Int_t volumeID) const; @@ -78,7 +71,7 @@ class TRestGeant4Hits : public TRestHits { // Destructor virtual ~TRestGeant4Hits(); - ClassDef(TRestGeant4Hits, 8); // REST event superclass + ClassDef(TRestGeant4Hits, 9); // REST event superclass // restG4 public: diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index e7e824b..a6ff392 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -16,14 +16,11 @@ #ifndef RestCore_TRestGeant4Track #define RestCore_TRestGeant4Track -#include #include -#include #include #include -#include -#include +#include "TRestGeant4Hits.h" class TRestGeant4Event; class TRestGeant4Metadata; @@ -36,7 +33,6 @@ class TRestGeant4Track { Int_t fTrackID; Int_t fParentID; - // We must change this to save space! (Might be not needed after all) TString fParticleName; TRestGeant4Hits fHits; @@ -46,7 +42,7 @@ class TRestGeant4Track { Double_t fGlobalTimestamp; // in seconds precision Double_t fTrackTimestamp; // in ns precision (seconds have been removed) Double_t fKineticEnergy; - Double_t fTrackLength; //! + Double_t fTrackLength; TVector3 fTrackOrigin; @@ -90,14 +86,14 @@ class TRestGeant4Track { return fHits.GetLastPositionInVolume(volID); } - void SetTrackID(Int_t id) { fTrackID = id; } - void SetParentID(Int_t id) { fParentID = id; } - void SetParticleName(const TString& particleName) { fParticleName = particleName; } - void SetGlobalTrackTime(Double_t time) { fGlobalTimestamp = time; } - void SetTrackTimeLength(Double_t time) { fTrackTimestamp = time; } - void SetKineticEnergy(Double_t en) { fKineticEnergy = en; } - void SetTrackOrigin(const TVector3& pos) { fTrackOrigin = pos; } - void SetTrackOrigin(Double_t x, Double_t y, Double_t z) { fTrackOrigin.SetXYZ(x, y, z); } + inline void SetTrackID(Int_t id) { fTrackID = id; } + inline void SetParentID(Int_t id) { fParentID = id; } + inline void SetParticleName(const TString& particleName) { fParticleName = particleName; } + inline void SetGlobalTrackTime(Double_t time) { fGlobalTimestamp = time; } + inline void SetTrackTimeLength(Double_t time) { fTrackTimestamp = time; } + inline void SetKineticEnergy(Double_t en) { fKineticEnergy = en; } + inline void SetTrackOrigin(const TVector3& pos) { fTrackOrigin = pos; } + inline void SetTrackOrigin(Double_t x, Double_t y, Double_t z) { fTrackOrigin.SetXYZ(x, y, z); } Double_t GetTrackLength() const; From b75130b3826d153466fc012f8c590197d3b523c6 Mon Sep 17 00:00:00 2001 From: lobis Date: Thu, 7 Jul 2022 20:18:33 +0200 Subject: [PATCH 017/100] Added track length on stepping action --- inc/TRestGeant4Track.h | 48 ++++++++++++++-------------------------- src/TRestGeant4Track.cxx | 13 ----------- 2 files changed, 16 insertions(+), 45 deletions(-) diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index a6ff392..8d1621c 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -42,7 +42,7 @@ class TRestGeant4Track { Double_t fGlobalTimestamp; // in seconds precision Double_t fTrackTimestamp; // in ns precision (seconds have been removed) Double_t fKineticEnergy; - Double_t fTrackLength; + Double_t fTrackLength = 0; TVector3 fTrackOrigin; @@ -52,28 +52,32 @@ class TRestGeant4Track { public: inline const TRestGeant4Hits& GetHits() const { return fHits; } - inline const TRestGeant4Event* GetEvent() const { return fEvent; } - inline void SetEvent(TRestGeant4Event* event) { fEvent = event; } + const TRestGeant4Metadata* GetGeant4Metadata() const; - inline Double_t GetEnergy() const { return fHits.GetEnergy(); } + inline void SetEvent(TRestGeant4Event* event) { fEvent = event; } + inline void SetHits(const TRestGeant4Hits& hits) { + fHits = hits; + fHits.SetTrack(this); + } size_t GetNumberOfHits(Int_t volID = -1) const; size_t GetNumberOfPhysicalHits(Int_t volID = -1) const; inline Int_t GetTrackID() const { return fTrackID; } inline Int_t GetParentID() const { return fParentID; } - inline TString GetCreatorProcess() const { return fCreatorProcess; } - inline TString GetParticleName() const { return fParticleName; } - EColor GetParticleColor() const; - inline Double_t GetGlobalTime() const { return fGlobalTimestamp; } inline Double_t GetTrackTimeLength() const { return fTrackTimestamp; } inline Double_t GetKineticEnergy() const { return fKineticEnergy; } inline Double_t GetTotalDepositedEnergy() const { return fHits.GetTotalDepositedEnergy(); } inline TVector3 GetTrackOrigin() const { return fTrackOrigin; } + inline Double_t GetWeight() const { return fWeight; } + inline Double_t GetEnergy() const { return fHits.GetEnergy(); } + inline Double_t GetTrackLength() const { return fTrackLength; } + + EColor GetParticleColor() const; inline Double_t GetEnergyInVolume(Int_t volID) const { return fHits.GetEnergyInVolume(volID); } inline TVector3 GetMeanPositionInVolume(Int_t volID) const { @@ -86,26 +90,6 @@ class TRestGeant4Track { return fHits.GetLastPositionInVolume(volID); } - inline void SetTrackID(Int_t id) { fTrackID = id; } - inline void SetParentID(Int_t id) { fParentID = id; } - inline void SetParticleName(const TString& particleName) { fParticleName = particleName; } - inline void SetGlobalTrackTime(Double_t time) { fGlobalTimestamp = time; } - inline void SetTrackTimeLength(Double_t time) { fTrackTimestamp = time; } - inline void SetKineticEnergy(Double_t en) { fKineticEnergy = en; } - inline void SetTrackOrigin(const TVector3& pos) { fTrackOrigin = pos; } - inline void SetTrackOrigin(Double_t x, Double_t y, Double_t z) { fTrackOrigin.SetXYZ(x, y, z); } - - Double_t GetTrackLength() const; - - inline static Double_t GetDistance(const TVector3& v1, const TVector3& v2) { - return TMath::Sqrt((v1.X() - v2.X()) * (v1.X() - v2.X()) + (v1.Y() - v2.Y()) * (v1.Y() - v2.Y()) + - (v1.Z() - v2.Z()) * (v1.Z() - v2.Z())); - } - - inline void RemoveHits() { fHits.RemoveHits(); } - - const TRestGeant4Metadata* GetGeant4Metadata() const; - Int_t GetProcessID(const TString& processName) const; TString GetProcessName(Int_t id) const; @@ -120,10 +104,10 @@ class TRestGeant4Track { /// Prints the track information. N number of hits to print, 0 = all void PrintTrack(size_t maxHits = 0) const; - inline void SetHits(const TRestGeant4Hits& hits) { - fHits = hits; - fHits.SetTrack(this); - } + inline void RemoveHits() { fHits.RemoveHits(); } + + inline void IncreaseTrackLength(Double_t length) { fTrackLength += length; } + // Constructor TRestGeant4Track(); diff --git a/src/TRestGeant4Track.cxx b/src/TRestGeant4Track.cxx index 50087cd..6b8550b 100644 --- a/src/TRestGeant4Track.cxx +++ b/src/TRestGeant4Track.cxx @@ -111,19 +111,6 @@ size_t TRestGeant4Track::GetNumberOfPhysicalHits(Int_t volID) const { return numberOfHits; } -Double_t TRestGeant4Track::GetTrackLength() const { - Double_t length = 0; - - length = GetDistance(fHits.GetPosition(0), GetTrackOrigin()); - - for (int i = 1; i < GetNumberOfHits(); i++) { - TVector3 prevHit = fHits.GetPosition(i - 1); - TVector3 hit = fHits.GetPosition(i); - length += GetDistance(hit, prevHit); - } - return length; -} - void TRestGeant4Track::PrintTrack(size_t maxHits) const { cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl; cout.precision(5); From 5c252409b9f7e344b487725658ff82624ba388c3 Mon Sep 17 00:00:00 2001 From: lobis Date: Thu, 7 Jul 2022 20:19:42 +0200 Subject: [PATCH 018/100] TRestGeant4Hits.h - updated version --- inc/TRestGeant4Hits.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/TRestGeant4Hits.h b/inc/TRestGeant4Hits.h index 6e429a9..b65f206 100644 --- a/inc/TRestGeant4Hits.h +++ b/inc/TRestGeant4Hits.h @@ -71,7 +71,7 @@ class TRestGeant4Hits : public TRestHits { // Destructor virtual ~TRestGeant4Hits(); - ClassDef(TRestGeant4Hits, 9); // REST event superclass + ClassDef(TRestGeant4Hits, 7); // REST event superclass // restG4 public: From f08d340e5ef079924ca3af013287aa631decd16a Mon Sep 17 00:00:00 2001 From: lobis Date: Thu, 7 Jul 2022 20:35:40 +0200 Subject: [PATCH 019/100] TRestGeant4Track: added secondaries --- inc/TRestGeant4Track.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index 8d1621c..bb339dd 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -39,6 +39,8 @@ class TRestGeant4Track { TString fCreatorProcess; + std::vector fSecondaryTrackIDs; + Double_t fGlobalTimestamp; // in seconds precision Double_t fTrackTimestamp; // in ns precision (seconds have been removed) Double_t fKineticEnergy; @@ -61,6 +63,8 @@ class TRestGeant4Track { fHits.SetTrack(this); } + inline std::vector GetSecondaryTrackIDs() const { return fSecondaryTrackIDs; } + size_t GetNumberOfHits(Int_t volID = -1) const; size_t GetNumberOfPhysicalHits(Int_t volID = -1) const; From 651fca5942ba2c6859d9c5639da230341d05d900 Mon Sep 17 00:00:00 2001 From: lobis Date: Thu, 7 Jul 2022 21:34:32 +0200 Subject: [PATCH 020/100] TRestGeant4Track - removed redundant track length --- inc/TRestGeant4Track.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index bb339dd..291a486 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -44,7 +44,7 @@ class TRestGeant4Track { Double_t fGlobalTimestamp; // in seconds precision Double_t fTrackTimestamp; // in ns precision (seconds have been removed) Double_t fKineticEnergy; - Double_t fTrackLength = 0; + Double_t fTrackLength; TVector3 fTrackOrigin; @@ -110,8 +110,6 @@ class TRestGeant4Track { inline void RemoveHits() { fHits.RemoveHits(); } - inline void IncreaseTrackLength(Double_t length) { fTrackLength += length; } - // Constructor TRestGeant4Track(); From 331c50de431300a8ea935f0b1110dcec2d6f3ee9 Mon Sep 17 00:00:00 2001 From: lobis Date: Fri, 8 Jul 2022 11:00:13 +0200 Subject: [PATCH 021/100] Updated track secondary handling --- inc/TRestGeant4Event.h | 6 +++--- inc/TRestGeant4Track.h | 5 +++++ src/TRestGeant4Event.cxx | 16 +++++++++++++--- src/TRestGeant4Track.cxx | 18 ++++++++++++++++++ 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index 7af0215..abb7902 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -156,7 +156,7 @@ class TRestGeant4Event : public TRestEvent { inline Int_t isVolumeStored(int n) const { return fVolumeStored[n]; } inline const TRestGeant4Track& GetTrack(int n) const { return fTracks[n]; } inline TRestGeant4Track* GetTrackPointer(int n) { return &fTracks[n]; } - TRestGeant4Track* GetTrackByID(int id); + TRestGeant4Track* GetTrackByID(Int_t trackID) const; inline Int_t GetNumberOfSubEventIDTracks() const { return fMaxSubEventID + 1; } inline Double_t GetTotalDepositedEnergy() const { return fTotalDepositedEnergy; } @@ -242,7 +242,7 @@ class TRestGeant4Event : public TRestEvent { friend class OutputManager; private: - std::map fTrackIDToTrackIndex = {}; //! - TRestGeant4Hits fInitialStep; //! + std::map fTrackIDToTrackIndex = {}; + TRestGeant4Hits fInitialStep; //! }; #endif diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index 291a486..6a6b39d 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -57,6 +57,8 @@ class TRestGeant4Track { inline const TRestGeant4Event* GetEvent() const { return fEvent; } const TRestGeant4Metadata* GetGeant4Metadata() const; + TRestGeant4Track* GetParentTrack() const; + inline void SetEvent(TRestGeant4Event* event) { fEvent = event; } inline void SetHits(const TRestGeant4Hits& hits) { fHits = hits; @@ -64,6 +66,9 @@ class TRestGeant4Track { } inline std::vector GetSecondaryTrackIDs() const { return fSecondaryTrackIDs; } + inline void AddSecondaryTrackID(Int_t trackID) { fSecondaryTrackIDs.push_back(trackID); } + + std::vector GetSecondaryTracks() const; size_t GetNumberOfHits(Int_t volID = -1) const; size_t GetNumberOfPhysicalHits(Int_t volID = -1) const; diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index f0ab59d..907b349 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -189,10 +189,20 @@ TVector3 TRestGeant4Event::GetLastPositionInVolume(Int_t volID) const { return {nan, nan, nan}; } -TRestGeant4Track* TRestGeant4Event::GetTrackByID(int id) { - for (int i = 0; i < GetNumberOfTracks(); i++) - if (fTracks[i].GetTrackID() == id) return &fTracks[i]; +TRestGeant4Track* TRestGeant4Event::GetTrackByID(Int_t trackID) const { + if (fTrackIDToTrackIndex.count(trackID) > 0) { + return const_cast(&fTracks[fTrackIDToTrackIndex.at(trackID)]); + } + return nullptr; + + /* + for (const auto& track : fTracks) { + if (track.GetTrackID() == trackID) { + return const_cast(&track); + } + } return nullptr; + */ } /////////////////////////////////////////////// diff --git a/src/TRestGeant4Track.cxx b/src/TRestGeant4Track.cxx index 6b8550b..c6ba196 100644 --- a/src/TRestGeant4Track.cxx +++ b/src/TRestGeant4Track.cxx @@ -182,3 +182,21 @@ const TRestGeant4Metadata* TRestGeant4Track::GetGeant4Metadata() const { } return GetEvent()->GetGeant4Metadata(); } + +TRestGeant4Track* TRestGeant4Track::GetParentTrack() const { + if (fEvent == nullptr) { + return nullptr; + } + return fEvent->GetTrackByID(GetParentID()); +} + +vector TRestGeant4Track::GetSecondaryTracks() const { + vector secondaryTracks = {}; + for (const auto trackID : fSecondaryTrackIDs) { + const TRestGeant4Track* track = fEvent->GetTrackByID(trackID); + if (track != nullptr) { + secondaryTracks.push_back(track); + } + } + return secondaryTracks; +} From cb303f8a3c4dc828b878a90634f3a22acd943252 Mon Sep 17 00:00:00 2001 From: lobis Date: Fri, 8 Jul 2022 11:15:18 +0200 Subject: [PATCH 022/100] TRestGeant4Track - renamed some members --- inc/TRestGeant4Track.h | 31 ++++++++++++++---------- src/TRestGeant4EventViewer.cxx | 2 +- src/TRestGeant4NeutronTaggingProcess.cxx | 6 ++--- src/TRestGeant4Track.cxx | 6 ++--- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index 6a6b39d..ea07974 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -41,12 +41,14 @@ class TRestGeant4Track { std::vector fSecondaryTrackIDs; - Double_t fGlobalTimestamp; // in seconds precision - Double_t fTrackTimestamp; // in ns precision (seconds have been removed) - Double_t fKineticEnergy; - Double_t fTrackLength; + /* Time is stored in seconds */ + Double_t fGlobalTimestamp; + Double_t fTimeLength; - TVector3 fTrackOrigin; + Double_t fInitialKineticEnergy; + Double_t fLength; + + TVector3 fInitialPosition; Double_t fWeight = 1; // Used for biasing @@ -65,26 +67,29 @@ class TRestGeant4Track { fHits.SetTrack(this); } - inline std::vector GetSecondaryTrackIDs() const { return fSecondaryTrackIDs; } - inline void AddSecondaryTrackID(Int_t trackID) { fSecondaryTrackIDs.push_back(trackID); } + inline TString GetCreatorProcess() const { return fCreatorProcess; } - std::vector GetSecondaryTracks() const; + inline void AddSecondaryTrackID(Int_t trackID) { fSecondaryTrackIDs.push_back(trackID); } size_t GetNumberOfHits(Int_t volID = -1) const; size_t GetNumberOfPhysicalHits(Int_t volID = -1) const; inline Int_t GetTrackID() const { return fTrackID; } inline Int_t GetParentID() const { return fParentID; } - inline TString GetCreatorProcess() const { return fCreatorProcess; } inline TString GetParticleName() const { return fParticleName; } inline Double_t GetGlobalTime() const { return fGlobalTimestamp; } - inline Double_t GetTrackTimeLength() const { return fTrackTimestamp; } - inline Double_t GetKineticEnergy() const { return fKineticEnergy; } + inline Double_t GetTimeLength() const { return fTimeLength; } + inline Double_t GetInitialKineticEnergy() const { return fInitialKineticEnergy; } inline Double_t GetTotalDepositedEnergy() const { return fHits.GetTotalDepositedEnergy(); } - inline TVector3 GetTrackOrigin() const { return fTrackOrigin; } + inline TVector3 GetInitialPosition() const { return fInitialPosition; } inline Double_t GetWeight() const { return fWeight; } inline Double_t GetEnergy() const { return fHits.GetEnergy(); } - inline Double_t GetTrackLength() const { return fTrackLength; } + inline Double_t GetLength() const { return fLength; } + + inline std::vector GetSecondaryTrackIDs() const { return fSecondaryTrackIDs; } + std::vector GetSecondaryTracks() const; + + inline TVector3 GetTrackOrigin() const { return GetInitialPosition(); } EColor GetParticleColor() const; diff --git a/src/TRestGeant4EventViewer.cxx b/src/TRestGeant4EventViewer.cxx index 1d4e29f..95528d3 100644 --- a/src/TRestGeant4EventViewer.cxx +++ b/src/TRestGeant4EventViewer.cxx @@ -80,7 +80,7 @@ void TRestGeant4EventViewer::AddEvent(TRestEvent* ev) { TVector3 origin = g4Track->GetTrackOrigin(); // Building track name - Double_t eKin = g4Track->GetKineticEnergy(); + Double_t eKin = g4Track->GetInitialKineticEnergy(); // cout << "eKin: " << eKin << endl; TString ptlName = g4Track->GetParticleName(); // cout << "ptlName: " << ptlName << endl; diff --git a/src/TRestGeant4NeutronTaggingProcess.cxx b/src/TRestGeant4NeutronTaggingProcess.cxx index 33e518a..d27071f 100644 --- a/src/TRestGeant4NeutronTaggingProcess.cxx +++ b/src/TRestGeant4NeutronTaggingProcess.cxx @@ -352,7 +352,7 @@ TRestEvent* TRestGeant4NeutronTaggingProcess::ProcessEvent(TRestEvent* inputEven } } fNeutronsCapturedIsCaptureVolume.push_back(isCaptureVolume); - fNeutronsCapturedProductionE.push_back(track.GetKineticEnergy()); + fNeutronsCapturedProductionE.push_back(track.GetInitialKineticEnergy()); // get energy deposited by neutron that undergoes capture and children double neutronsCapturedEDepByNeutron = 0; @@ -445,7 +445,7 @@ TRestEvent* TRestGeant4NeutronTaggingProcess::ProcessEvent(TRestEvent* inputEven } } fGammasNeutronCaptureIsCaptureVolume.push_back(isCaptureVolume); - fGammasNeutronCaptureProductionE.push_back(track.GetKineticEnergy()); + fGammasNeutronCaptureProductionE.push_back(track.GetInitialKineticEnergy()); // cout << "gamma capture" << endl; @@ -505,7 +505,7 @@ TRestEvent* TRestGeant4NeutronTaggingProcess::ProcessEvent(TRestEvent* inputEven fSecondaryNeutronsShieldingIsCapturedInCaptureVolume.push_back(0); } - fSecondaryNeutronsShieldingProductionE.push_back(track.GetKineticEnergy()); + fSecondaryNeutronsShieldingProductionE.push_back(track.GetInitialKineticEnergy()); fSecondaryNeutronsShieldingExitE.push_back(hits.GetKineticEnergy(j)); } } diff --git a/src/TRestGeant4Track.cxx b/src/TRestGeant4Track.cxx index c6ba196..9fbdf2e 100644 --- a/src/TRestGeant4Track.cxx +++ b/src/TRestGeant4Track.cxx @@ -116,10 +116,10 @@ void TRestGeant4Track::PrintTrack(size_t maxHits) const { cout.precision(5); cout << " Particle : " << GetParticleName() << " Track ID: " << GetTrackID() << " Parent ID: " << GetParentID() << " Created by process: " << fCreatorProcess - << " Global timestamp: " << fGlobalTimestamp << " Time track length: " << GetTrackTimeLength() - << " us" << endl; + << " Global timestamp: " << fGlobalTimestamp << " s Time length: " << GetTimeLength() * 1E6 << " us" + << endl; cout << " Origin: (" << GetTrackOrigin().X() << ", " << GetTrackOrigin().Y() << ", " - << GetTrackOrigin().Z() << ") mm Initial KE: " << GetKineticEnergy() << " keV" << endl; + << GetTrackOrigin().Z() << ") mm Initial KE: " << GetInitialKineticEnergy() << " keV" << endl; cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl; int nHits = GetNumberOfHits(); From 8ec674c2b0c36d349ed646feb5cfb8b8e17f3974 Mon Sep 17 00:00:00 2001 From: lobis Date: Fri, 8 Jul 2022 13:39:25 +0200 Subject: [PATCH 023/100] Simplified user distribution initialization --- inc/TRestGeant4Metadata.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 3157de8..3ad7637 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -358,7 +358,7 @@ class TRestGeant4Metadata : public TRestMetadata { inline Int_t GetNumberOfSources() const { return fParticleSource.size(); } /// Returns the name of the particle source with index n (Geant4 based names). - inline TRestGeant4ParticleSource* GetParticleSource(int n) { return fParticleSource[n]; } + inline TRestGeant4ParticleSource* GetParticleSource(int n) const { return fParticleSource[n]; } /// Remove all the particle sources. void RemoveParticleSources(); From dfb0caf038c628510f59022ae5538c86360fe77e Mon Sep 17 00:00:00 2001 From: lobis Date: Mon, 11 Jul 2022 10:12:31 +0200 Subject: [PATCH 024/100] TRestGeant4Event - added missing override --- inc/TRestGeant4Event.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index abb7902..3f59c9d 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -212,7 +212,7 @@ class TRestGeant4Event : public TRestEvent { Bool_t ContainsParticle(const TString& particleName) const; Bool_t ContainsParticleInVolume(const TString& particleName, Int_t volumeID = -1) const; - void Initialize(); + void Initialize() override; void InitializeReferences(TRestRun* run) override; @@ -222,7 +222,7 @@ class TRestGeant4Event : public TRestEvent { void PrintActiveVolumes() const; void PrintEvent(int maxTracks = 0, int maxHits = 0) const; - inline TPad* DrawEvent(const TString& option = "") { return DrawEvent(option, true); } + inline TPad* DrawEvent(const TString& option = "") override { return DrawEvent(option, true); } TPad* DrawEvent(const TString& option, Bool_t autoBoundaries); // Constructor @@ -230,7 +230,7 @@ class TRestGeant4Event : public TRestEvent { // Destructor virtual ~TRestGeant4Event(); - ClassDef(TRestGeant4Event, 8); // REST event superclass + ClassDefOverride(TRestGeant4Event, 8); // REST event superclass // restG4 public: From 8476d88f36ea63b908c318bfe9a144f39e3b6add Mon Sep 17 00:00:00 2001 From: lobis Date: Wed, 13 Jul 2022 17:24:59 +0200 Subject: [PATCH 025/100] Attempt to fix segfaults on MT --- inc/TRestGeant4GeometryInfo.h | 2 -- inc/TRestGeant4PhysicsInfo.h | 2 ++ src/TRestGeant4PhysicsInfo.cxx | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/inc/TRestGeant4GeometryInfo.h b/inc/TRestGeant4GeometryInfo.h index 26673f8..71cdf50 100644 --- a/inc/TRestGeant4GeometryInfo.h +++ b/inc/TRestGeant4GeometryInfo.h @@ -44,8 +44,6 @@ class TRestGeant4GeometryInfo { TString GetAlternativeNameFromGeant4PhysicalName(const TString&) const; TString GetGeant4PhysicalNameFromAlternativeName(const TString&) const; - // Int_t GetIDFromVolumeName(const TString&) const; - void PopulateFromGeant4World(const G4VPhysicalVolume*); std::vector GetAllLogicalVolumes() const; diff --git a/inc/TRestGeant4PhysicsInfo.h b/inc/TRestGeant4PhysicsInfo.h index ab08eec..1089b00 100644 --- a/inc/TRestGeant4PhysicsInfo.h +++ b/inc/TRestGeant4PhysicsInfo.h @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -19,6 +20,7 @@ class TRestGeant4PhysicsInfo { std::map fParticleNamesMap = {}; std::map fParticleNamesReverseMap = {}; + std::mutex fMutex; //! public: TString GetProcessName(Int_t id) const; Int_t GetProcessID(const TString& processName) const; diff --git a/src/TRestGeant4PhysicsInfo.cxx b/src/TRestGeant4PhysicsInfo.cxx index 665335e..fc3d3c8 100644 --- a/src/TRestGeant4PhysicsInfo.cxx +++ b/src/TRestGeant4PhysicsInfo.cxx @@ -47,13 +47,23 @@ void TRestGeant4PhysicsInfo::Print() const { } void TRestGeant4PhysicsInfo::InsertProcessName(Int_t id, const TString& processName) { + if (fProcessNamesMap.count(id) > 0) { + return; + } + fMutex.lock(); fProcessNamesMap[id] = processName; fProcessNamesReverseMap[processName] = id; + fMutex.unlock(); } void TRestGeant4PhysicsInfo::InsertParticleName(Int_t id, const TString& particleName) { + if (fParticleNamesMap.count(id) > 0) { + return; + } + fMutex.lock(); fParticleNamesMap[id] = particleName; fParticleNamesReverseMap[particleName] = id; + fMutex.unlock(); } template From 6c8cbbca9dfce27137d8fe6f088847a366b38d28 Mon Sep 17 00:00:00 2001 From: lobis Date: Wed, 13 Jul 2022 17:55:35 +0200 Subject: [PATCH 026/100] updates locking --- src/TRestGeant4PhysicsInfo.cxx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/TRestGeant4PhysicsInfo.cxx b/src/TRestGeant4PhysicsInfo.cxx index fc3d3c8..afd3d52 100644 --- a/src/TRestGeant4PhysicsInfo.cxx +++ b/src/TRestGeant4PhysicsInfo.cxx @@ -50,20 +50,18 @@ void TRestGeant4PhysicsInfo::InsertProcessName(Int_t id, const TString& processN if (fProcessNamesMap.count(id) > 0) { return; } - fMutex.lock(); + std::lock_guard lock(fMutex); fProcessNamesMap[id] = processName; fProcessNamesReverseMap[processName] = id; - fMutex.unlock(); } void TRestGeant4PhysicsInfo::InsertParticleName(Int_t id, const TString& particleName) { if (fParticleNamesMap.count(id) > 0) { return; } - fMutex.lock(); + std::lock_guard lock(fMutex); fParticleNamesMap[id] = particleName; fParticleNamesReverseMap[particleName] = id; - fMutex.unlock(); } template From f89bdb839331ff8b279fd850263229f77b30e832 Mon Sep 17 00:00:00 2001 From: lobis Date: Thu, 14 Jul 2022 14:10:26 +0200 Subject: [PATCH 027/100] Added start of `TRestGeant4PrimaryGeneratorInfo` to store primary generator info --- inc/TRestGeant4Metadata.h | 10 +++++ inc/TRestGeant4PrimaryGeneratorInfo.h | 58 +++++++++++++++++++++++++ src/TRestGeant4PrimaryGeneratorInfo.cxx | 5 +++ 3 files changed, 73 insertions(+) create mode 100644 inc/TRestGeant4PrimaryGeneratorInfo.h create mode 100644 src/TRestGeant4PrimaryGeneratorInfo.cxx diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 3ad7637..d98fffe 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -41,6 +41,8 @@ #include "TRestGeant4GeometryInfo.h" #include "TRestGeant4ParticleSource.h" #include "TRestGeant4PhysicsInfo.h" +#include "TRestGeant4PrimaryGeneratorInfo.h" + //------------------------------------------------------------------------------------------------------------------------ // // * This section was added by Luis A. Obis (lobis@unizar.es) on 17/06/2019 @@ -107,6 +109,9 @@ class TRestGeant4Metadata : public TRestMetadata { /// Class used to store and retrieve physics info such as process names or particle names TRestGeant4PhysicsInfo fGeant4PhysicsInfo; + /// Class used to store and retrieve Geant4 primary generator info + TRestGeant4PrimaryGeneratorInfo fGeant4PrimaryGeneratorInfo; // TODO: Implement this + /// The version of Geant4 used to generate the data TString fGeant4Version; @@ -229,6 +234,11 @@ class TRestGeant4Metadata : public TRestMetadata { /// \brief Returns an immutable reference to the physics info inline const TRestGeant4PhysicsInfo& GetGeant4PhysicsInfo() const { return fGeant4PhysicsInfo; } + /// \brief Returns an immutable reference to the primary generator info + inline const TRestGeant4PrimaryGeneratorInfo& GetGeant4PrimaryGeneratorInfo() const { + return fGeant4PrimaryGeneratorInfo; + } + /// \brief Returns a std::string with the version of Geant4 used on the event data /// simulation inline TString GetGeant4Version() const { return fGeant4Version; } diff --git a/inc/TRestGeant4PrimaryGeneratorInfo.h b/inc/TRestGeant4PrimaryGeneratorInfo.h new file mode 100644 index 0000000..918df24 --- /dev/null +++ b/inc/TRestGeant4PrimaryGeneratorInfo.h @@ -0,0 +1,58 @@ + +#ifndef REST_TRESTGEANT4PRIMARYGENERATORINFO_H +#define REST_TRESTGEANT4PRIMARYGENERATORINFO_H + +namespace TRestGeant4PrimaryGeneratorInfo { + +enum class generator_types { + CUSTOM, + VOLUME, + SURFACE, + POINT, +}; +extern std::map generator_types_map; + +enum class generator_shapes { + GDML, + WALL, + CIRCLE, + BOX, + SPHERE, + CYLINDER, +}; +extern std::map generator_shapes_map; + +enum class energy_dist_types { + TH1D, + FORMULA, + MONO, + FLAT, + LOG, +}; +extern std::map energy_dist_types_map; + +enum class angular_dist_types { + TH1D, + FORMULA, + ISOTROPIC, + FLUX, + BACK_TO_BACK, +}; + +class TRestGeant4PrimaryGeneratorInfo { + ClassDef(TRestGeant4PrimaryGeneratorInfo, 1); + + private: + // Related to spatial generator + TString fSpatialGeneratorType; + TString fSpatialGeneratorShape; + TString fSpatialGeneratorGdmlFrom; + TString fSpatialGeneratorVolumeFrom; + TVector3 fSpatialGeneratorPosition; + TVector3 fSpatialGeneratorRotationAxis; + Double_t fSpatialGeneratorRotationValue; + TVector3 fSpatialGeneratorSize; + TString fSpatialGeneratorSpatialDensity; +}; + +#endif // REST_TRESTGEANT4PRIMARYGENERATORINFO_H diff --git a/src/TRestGeant4PrimaryGeneratorInfo.cxx b/src/TRestGeant4PrimaryGeneratorInfo.cxx new file mode 100644 index 0000000..f342f82 --- /dev/null +++ b/src/TRestGeant4PrimaryGeneratorInfo.cxx @@ -0,0 +1,5 @@ +// +// Created by lobis on 7/14/2022. +// + +#include "TRestGeant4PrimaryGeneratorInfo.h" From 8c5977b2f294d335cf49f3dcc3b239b7ca0d6384 Mon Sep 17 00:00:00 2001 From: lobis Date: Mon, 18 Jul 2022 12:05:58 +0200 Subject: [PATCH 028/100] TRestGeant4PrimaryGeneratorInfo - added enum to string --- inc/TRestGeant4PrimaryGeneratorInfo.h | 32 +++-- src/TRestGeant4PrimaryGeneratorInfo.cxx | 164 ++++++++++++++++++++++++ 2 files changed, 188 insertions(+), 8 deletions(-) diff --git a/inc/TRestGeant4PrimaryGeneratorInfo.h b/inc/TRestGeant4PrimaryGeneratorInfo.h index 918df24..de8637b 100644 --- a/inc/TRestGeant4PrimaryGeneratorInfo.h +++ b/inc/TRestGeant4PrimaryGeneratorInfo.h @@ -2,17 +2,24 @@ #ifndef REST_TRESTGEANT4PRIMARYGENERATORINFO_H #define REST_TRESTGEANT4PRIMARYGENERATORINFO_H -namespace TRestGeant4PrimaryGeneratorInfo { +#include +#include -enum class generator_types { +#include + +namespace TRestGeant4PrimaryGeneratorTypes { + +enum class SpatialGeneratorTypes { CUSTOM, VOLUME, SURFACE, POINT, }; -extern std::map generator_types_map; -enum class generator_shapes { +std::string SpatialGeneratorTypesToString(const SpatialGeneratorTypes&); +SpatialGeneratorTypes StringToSpatialGeneratorTypes(const std::string&); + +enum class SpatialGeneratorShapes { GDML, WALL, CIRCLE, @@ -20,18 +27,22 @@ enum class generator_shapes { SPHERE, CYLINDER, }; -extern std::map generator_shapes_map; -enum class energy_dist_types { +std::string SpatialGeneratorShapesToString(const SpatialGeneratorShapes&); +SpatialGeneratorShapes StringToSpatialGeneratorShapes(const std::string&); + +enum class EnergyDistributionTypes { TH1D, FORMULA, MONO, FLAT, LOG, }; -extern std::map energy_dist_types_map; -enum class angular_dist_types { +std::string EnergyDistributionTypesToString(const EnergyDistributionTypes&); +EnergyDistributionTypes StringToEnergyDistributionTypes(const std::string&); + +enum class AngularDistributionTypes { TH1D, FORMULA, ISOTROPIC, @@ -39,6 +50,11 @@ enum class angular_dist_types { BACK_TO_BACK, }; +std::string AngularDistributionTypesToString(const AngularDistributionTypes&); +AngularDistributionTypes StringToAngularDistributionTypes(const std::string&); + +} // namespace TRestGeant4PrimaryGeneratorTypes + class TRestGeant4PrimaryGeneratorInfo { ClassDef(TRestGeant4PrimaryGeneratorInfo, 1); diff --git a/src/TRestGeant4PrimaryGeneratorInfo.cxx b/src/TRestGeant4PrimaryGeneratorInfo.cxx index f342f82..66ce574 100644 --- a/src/TRestGeant4PrimaryGeneratorInfo.cxx +++ b/src/TRestGeant4PrimaryGeneratorInfo.cxx @@ -3,3 +3,167 @@ // #include "TRestGeant4PrimaryGeneratorInfo.h" + +#include + +using namespace std; +using namespace TRestGeant4PrimaryGeneratorTypes; + +string TRestGeant4PrimaryGeneratorTypes::SpatialGeneratorTypesToString(const SpatialGeneratorTypes& type) { + switch (type) { + case SpatialGeneratorTypes::CUSTOM: + return "Custom"; + case SpatialGeneratorTypes::VOLUME: + return "Volume"; + case SpatialGeneratorTypes::SURFACE: + return "Surface"; + case SpatialGeneratorTypes::POINT: + return "Point"; + } + cout << "Error: Unknown SpatialGeneratorTypes" << endl; + exit(1); +} + +SpatialGeneratorTypes StringToSpatialGeneratorTypes(const string& type) { + if (TString(type).EqualTo(SpatialGeneratorTypesToString(SpatialGeneratorTypes::CUSTOM), + TString::ECaseCompare::kIgnoreCase)) { + return SpatialGeneratorTypes::CUSTOM; + } else if (TString(type).EqualTo(SpatialGeneratorTypesToString(SpatialGeneratorTypes::VOLUME), + TString::ECaseCompare::kIgnoreCase)) { + return SpatialGeneratorTypes::VOLUME; + } else if (TString(type).EqualTo(SpatialGeneratorTypesToString(SpatialGeneratorTypes::SURFACE), + TString::ECaseCompare::kIgnoreCase)) { + return SpatialGeneratorTypes::SURFACE; + } else if (TString(type).EqualTo(SpatialGeneratorTypesToString(SpatialGeneratorTypes::POINT), + TString::ECaseCompare::kIgnoreCase)) { + return SpatialGeneratorTypes::POINT; + } else { + cout << "Error: Unknown SpatialGeneratorTypes: " << type << endl; + exit(1); + } +} + +string TRestGeant4PrimaryGeneratorTypes::SpatialGeneratorShapesToString(const SpatialGeneratorShapes& type) { + switch (type) { + case SpatialGeneratorShapes::GDML: + return "GDML"; + case SpatialGeneratorShapes::WALL: + return "Wall"; + case SpatialGeneratorShapes::CIRCLE: + return "Circle"; + case SpatialGeneratorShapes::BOX: + return "Box"; + case SpatialGeneratorShapes::SPHERE: + return "Sphere"; + case SpatialGeneratorShapes::CYLINDER: + return "Cylinder"; + } + cout << "Error: Unknown SpatialGeneratorShapes" << endl; + exit(1); +} + +SpatialGeneratorShapes StringToSpatialGeneratorShapes(const string& type) { + if (TString(type).EqualTo(SpatialGeneratorShapesToString(SpatialGeneratorShapes::GDML), + TString::ECaseCompare::kIgnoreCase)) { + return SpatialGeneratorShapes::GDML; + } else if (TString(type).EqualTo(SpatialGeneratorShapesToString(SpatialGeneratorShapes::WALL), + TString::ECaseCompare::kIgnoreCase)) { + return SpatialGeneratorShapes::WALL; + } else if (TString(type).EqualTo(SpatialGeneratorShapesToString(SpatialGeneratorShapes::CIRCLE), + TString::ECaseCompare::kIgnoreCase)) { + return SpatialGeneratorShapes::CIRCLE; + } else if (TString(type).EqualTo(SpatialGeneratorShapesToString(SpatialGeneratorShapes::BOX), + TString::ECaseCompare::kIgnoreCase)) { + return SpatialGeneratorShapes::BOX; + } else if (TString(type).EqualTo(SpatialGeneratorShapesToString(SpatialGeneratorShapes::SPHERE), + TString::ECaseCompare::kIgnoreCase)) { + return SpatialGeneratorShapes::SPHERE; + } else if (TString(type).EqualTo(SpatialGeneratorShapesToString(SpatialGeneratorShapes::CYLINDER), + TString::ECaseCompare::kIgnoreCase)) { + return SpatialGeneratorShapes::CYLINDER; + + } else { + cout << "Error: Unknown SpatialGeneratorShapes: " << type << endl; + exit(1); + } +} + +string TRestGeant4PrimaryGeneratorTypes::EnergyDistributionTypesToString( + const EnergyDistributionTypes& type) { + switch (type) { + case EnergyDistributionTypes::TH1D: + return "TH1D"; + case EnergyDistributionTypes::FORMULA: + return "Formula"; + case EnergyDistributionTypes::MONO: + return "Mono"; + case EnergyDistributionTypes::FLAT: + return "Flat"; + case EnergyDistributionTypes::LOG: + return "Log"; + } + cout << "Error: Unknown EnergyDistributionTypes" << endl; + exit(1); +} + +EnergyDistributionTypes StringToEnergyDistributionTypes(const string& type) { + if (TString(type).EqualTo(EnergyDistributionTypesToString(EnergyDistributionTypes::TH1D), + TString::ECaseCompare::kIgnoreCase)) { + return EnergyDistributionTypes::TH1D; + } else if (TString(type).EqualTo(EnergyDistributionTypesToString(EnergyDistributionTypes::FORMULA), + TString::ECaseCompare::kIgnoreCase)) { + return EnergyDistributionTypes::FORMULA; + } else if (TString(type).EqualTo(EnergyDistributionTypesToString(EnergyDistributionTypes::MONO), + TString::ECaseCompare::kIgnoreCase)) { + return EnergyDistributionTypes::MONO; + } else if (TString(type).EqualTo(EnergyDistributionTypesToString(EnergyDistributionTypes::FLAT), + TString::ECaseCompare::kIgnoreCase)) { + return EnergyDistributionTypes::FLAT; + } else if (TString(type).EqualTo(EnergyDistributionTypesToString(EnergyDistributionTypes::LOG), + TString::ECaseCompare::kIgnoreCase)) { + return EnergyDistributionTypes::LOG; + } else { + cout << "Error: Unknown EnergyDistributionTypes: " << type << endl; + exit(1); + } +} + +string TRestGeant4PrimaryGeneratorTypes::AngularDistributionTypesToString( + const AngularDistributionTypes& type) { + switch (type) { + case AngularDistributionTypes::TH1D: + return "TH1D"; + case AngularDistributionTypes::FORMULA: + return "Formula"; + case AngularDistributionTypes::ISOTROPIC: + return "Isotropic"; + case AngularDistributionTypes::FLUX: + return "Flux"; + case AngularDistributionTypes::BACK_TO_BACK: + return "Back to back"; + } + cout << "Error: Unknown AngularDistributionTypes" << endl; + exit(1); +} + +AngularDistributionTypes StringToAngularDistributionTypes(const string& type) { + if (TString(type).EqualTo(AngularDistributionTypesToString(AngularDistributionTypes::TH1D), + TString::ECaseCompare::kIgnoreCase)) { + return AngularDistributionTypes::TH1D; + } else if (TString(type).EqualTo(AngularDistributionTypesToString(AngularDistributionTypes::FORMULA), + TString::ECaseCompare::kIgnoreCase)) { + return AngularDistributionTypes::FORMULA; + } else if (TString(type).EqualTo(AngularDistributionTypesToString(AngularDistributionTypes::ISOTROPIC), + TString::ECaseCompare::kIgnoreCase)) { + return AngularDistributionTypes::ISOTROPIC; + } else if (TString(type).EqualTo(AngularDistributionTypesToString(AngularDistributionTypes::FLUX), + TString::ECaseCompare::kIgnoreCase)) { + return AngularDistributionTypes::FLUX; + } else if (TString(type).EqualTo(AngularDistributionTypesToString(AngularDistributionTypes::BACK_TO_BACK), + TString::ECaseCompare::kIgnoreCase)) { + return AngularDistributionTypes::BACK_TO_BACK; + } else { + cout << "Error: Unknown AngularDistributionTypes: " << type << endl; + exit(1); + } +} From 3f821fac6c472f94d57fbbaa67f2d3e3dce947f3 Mon Sep 17 00:00:00 2001 From: lobis Date: Mon, 18 Jul 2022 12:48:11 +0200 Subject: [PATCH 029/100] TRestGeant4PrimaryGeneratorInfo - updated initialization --- inc/TRestGeant4Metadata.h | 88 +------------------------ inc/TRestGeant4PrimaryGeneratorInfo.h | 72 ++++++++++++++++++-- src/TRestGeant4Metadata.cxx | 61 ++++++----------- src/TRestGeant4PrimaryGeneratorInfo.cxx | 51 ++++++++++++-- 4 files changed, 135 insertions(+), 137 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index d98fffe..197c861 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -127,38 +127,6 @@ class TRestGeant4Metadata : public TRestMetadata { /// A GDML materials reference introduced in the header of the GDML of materials definition TString fMaterialsReference; - /// Type of spatial generator (surface, volume, custom) - TString fGenType; - - /// Shape of spatial generator (point, wall, gdml, sphere, etc) - TString fGenShape; - - /// The volume name where the events are generated, in case of volume or - /// surface generator types. - TString fGenFrom; - - /// The position of the generator for virtual, and point, generator types. - TVector3 fGenPosition; - - /// \brief A 3d-std::vector with the angles, measured in degrees, of a XYZ rotation - /// applied to the virtual generator. This rotation is used by virtualWall, - /// virtualCircleWall and virtualCylinder generators. - TVector3 fGenRotationAxis; - - /// \brief degrees of rotation for generator virtual shape along the axis - Double_t fGenRotationDegree; - - /// \brief The size of the generator. Stores up to three deminsions according to the shape - /// box: length, width, height - /// sphere: radius - /// wall: length, width - /// circle: radius - /// cylinder: radius, length - TVector3 fGenSize; - - /// \brief Defines density distribution of the generator shape. rho=F(x,y,z), in range 0~1 - TString fGenDensityFunction; - /// \brief A 2d-std::vector storing the energy range, in keV, to decide if a particular /// event should be written to disk or not. TVector2 fEnergyRangeStored; @@ -209,7 +177,7 @@ class TRestGeant4Metadata : public TRestMetadata { Long_t fSeed = 0; /// \brief If this parameter is set to 'true' it will save all events even if they leave no energy in the - /// sensitive volume (used for debugging pourposes). It is set to 'false' by default. + /// sensitive volume (used for debugging purposes). It is set to 'false' by default. Bool_t fSaveAllEvents = false; /// If this parameter is set to 'true' it will print out on screen every time 10k events are reached. @@ -257,43 +225,6 @@ class TRestGeant4Metadata : public TRestMetadata { /// Returns the reference provided at the materials file header inline TString GetMaterialsReference() const { return fMaterialsReference; } - /// \brief Returns a std::string specifying the generator type (volume, surface, point, - /// virtualWall, etc ) - inline TString GetGeneratorType() const { return fGenType; } - - /// \brief Returns a std::string specifying the generator shape (point, wall, box, etc ) - inline TString GetGeneratorShape() const { return fGenShape; } - - /// \brief Returns the name of the GDML volume where primary events are - /// produced. This value has meaning only when using volume or surface - /// generator types. - inline TString GetGeneratedFrom() const { return fGenFrom; } - - /// \brief Returns the name of the GDML volume where primary events are - /// produced. This value has meaning only when using volume or surface - /// generator types. - inline TString GetGDMLGeneratorVolume() const { return fGenFrom; } - - /// \brief Returns a 3d-std::vector with the position of the primary event - /// generator. This value has meaning only when using point and virtual - /// generator types. - inline TVector3 GetGeneratorPosition() const { return fGenPosition; } - - /// \brief Returns a 3d-std::vector, fGenRotation, with the XYZ rotation angle - /// values in degrees. This value is used by virtualWall, virtualCircleWall - /// and virtualCylinder generator types. - inline TVector3 GetGeneratorRotationAxis() const { return fGenRotationAxis; } - - /// \brief Returns the degree of rotation - inline Double_t GetGeneratorRotationDegree() const { return fGenRotationDegree; } - - /// \brief Returns the main spatial dimension of virtual generator. - /// It is the size of a virtualBox. - inline TVector3 GetGeneratorSize() const { return fGenSize; } - - /// \brief Returns the density function of the generator - inline TString GetGeneratorSpatialDensity() const { return fGenDensityFunction; } - /// \brief Returns true in case full decay chain simulation is enabled. inline Bool_t isFullChainActivated() const { return fFullChain; } @@ -326,26 +257,9 @@ class TRestGeant4Metadata : public TRestMetadata { /// Sets the value of the Geant4 version inline void SetGeant4Version(const TString& versionString) { fGeant4Version = versionString; } - /// \brief Sets the generator type. I.e. volume, surface, point, virtualWall, - /// virtualCylinder, etc. - inline void SetGeneratorType(TString type) { fGenType = type; } - - /// \brief Sets the generator main spatial dimension. In a virtual generator is the - /// radius of cylinder, size of wall, etc. - inline void SetGeneratorSize(const TVector3& size) { fGenSize = size; } - /// Enables/disables the full chain decay generation. inline void SetFullChain(Bool_t fullChain) { fFullChain = fullChain; } - /// Sets the position of the virtual generator using a TVector3. - inline void SetGeneratorPosition(const TVector3& pos) { fGenPosition = pos; } - - /// Sets the position of the virtual generator using x,y,z coordinates. - inline void SetGeneratorPosition(double x, double y, double z) { fGenPosition = TVector3(x, y, z); } - - /// Sets the number of events to be simulated. - inline void SetNEvents(Int_t n) { fNEvents = n; } - /// It sets the location of the geometry files inline void SetGeometryPath(std::string path) { fGeometryPath = path; } diff --git a/inc/TRestGeant4PrimaryGeneratorInfo.h b/inc/TRestGeant4PrimaryGeneratorInfo.h index de8637b..f7dc450 100644 --- a/inc/TRestGeant4PrimaryGeneratorInfo.h +++ b/inc/TRestGeant4PrimaryGeneratorInfo.h @@ -55,20 +55,84 @@ AngularDistributionTypes StringToAngularDistributionTypes(const std::string&); } // namespace TRestGeant4PrimaryGeneratorTypes +class TiXmlElement; + class TRestGeant4PrimaryGeneratorInfo { ClassDef(TRestGeant4PrimaryGeneratorInfo, 1); + public: + void Print() const; + private: - // Related to spatial generator + /* Members related to spatial generator */ + /// Type of spatial generator (surface, volume, custom) TString fSpatialGeneratorType; + + /// Shape of spatial generator (point, wall, GDML, sphere, etc) TString fSpatialGeneratorShape; - TString fSpatialGeneratorGdmlFrom; - TString fSpatialGeneratorVolumeFrom; + + /// The volume name where the events are generated, in case of volume or surface generator types + TString fSpatialGeneratorFrom; + + /// The position of the generator for virtual, and point, generator types TVector3 fSpatialGeneratorPosition; + + /// \brief A 3d-std::vector with the angles, measured in degrees, of a XYZ rotation + /// applied to the virtual generator. This rotation is used by virtualWall, + /// virtualCircleWall and virtualCylinder generators. TVector3 fSpatialGeneratorRotationAxis; + + /// \brief degrees of rotation for generator virtual shape along the axis Double_t fSpatialGeneratorRotationValue; + + /// \brief The size of the generator. Stores up to three dimensions according to the shape + /// box: length, width, height + /// sphere: radius + /// wall: length, width + /// circle: radius + /// cylinder: radius, length TVector3 fSpatialGeneratorSize; - TString fSpatialGeneratorSpatialDensity; + + /// \brief Defines density distribution of the generator shape. rho=F(x,y,z), in range 0~1 + TString fSpatialGeneratorSpatialDensityFunction; + + public: + /// \brief Returns a std::string specifying the generator type (volume, surface, point, + /// virtualWall, etc ) + inline TString GetSpatialGeneratorType() const { return fSpatialGeneratorType; } + + /// \brief Returns a std::string specifying the generator shape (point, wall, box, etc ) + inline TString GetSpatialGeneratorShape() const { return fSpatialGeneratorShape; } + + /// \brief Returns the name of the GDML volume where primary events are + /// produced. This value has meaning only when using volume or surface + /// generator types. + inline TString GetSpatialGeneratorFrom() const { return fSpatialGeneratorFrom; } + + /// \brief Returns a 3d-std::vector with the position of the primary event + /// generator. This value has meaning only when using point and virtual + /// generator types. + inline TVector3 GetSpatialGeneratorPosition() const { return fSpatialGeneratorPosition; } + + /// \brief Returns a 3d-std::vector, fGenRotation, with the XYZ rotation angle + /// values in degrees. This value is used by virtualWall, virtualCircleWall + /// and virtualCylinder generator types. + inline TVector3 GetSpatialGeneratorRotationAxis() const { return fSpatialGeneratorRotationAxis; } + + /// \brief Returns the degree of rotation + inline Double_t GetSpatialGeneratorRotationValue() const { return fSpatialGeneratorRotationValue; } + + /// \brief Returns the main spatial dimension of virtual generator. + /// It is the size of a virtualBox. + inline TVector3 GetSpatialGeneratorSize() const { return fSpatialGeneratorSize; } + + /// \brief Returns the density function of the generator + inline TString GetSpatialGeneratorSpatialDensityFunction() const { + return fSpatialGeneratorSpatialDensityFunction; + } + + friend class TRestGeant4Metadata; + friend class DetectorConstruction; }; #endif // REST_TRESTGEANT4PRIMARYGENERATORINFO_H diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 8b431ec..7f56ccf 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -107,8 +107,8 @@ /// *restG4*, as important as the detector geometry, or the number of events to /// be simulated. /// -/// * **Nevents**: The number of primary particles to be generated. The number -/// of registered events might differ from *Nevents* due to storage definitions. +/// * **nEvents**: The number of primary particles to be generated. The number +/// of registered events might differ from *nEvents* due to storage definitions. /// The number of events registered could be even larger than the number of /// primaries, as for example when launching full decay chain simulations, where /// different isotope decays are stored in different events. @@ -940,17 +940,22 @@ void TRestGeant4Metadata::ReadGenerator() { TiXmlElement* generatorDefinition = GetElement("generator"); - fGenType = GetParameter("type", generatorDefinition, "volume"); - fGenShape = GetParameter("shape", generatorDefinition, "box"); - fGenFrom = GetParameter("from", generatorDefinition); - if (fGenFrom != PARAMETER_NOT_FOUND_STR) { - fGenShape = "gdml"; + fGeant4PrimaryGeneratorInfo.fSpatialGeneratorType = GetParameter("type", generatorDefinition, "volume"); + fGeant4PrimaryGeneratorInfo.fSpatialGeneratorShape = GetParameter("shape", generatorDefinition, "box"); + fGeant4PrimaryGeneratorInfo.fSpatialGeneratorFrom = GetParameter("from", generatorDefinition); + if (fGeant4PrimaryGeneratorInfo.fSpatialGeneratorFrom != PARAMETER_NOT_FOUND_STR) { + fGeant4PrimaryGeneratorInfo.fSpatialGeneratorShape = "gdml"; } - fGenSize = Get3DVectorParameterWithUnits("size", generatorDefinition); - fGenPosition = Get3DVectorParameterWithUnits("position", generatorDefinition); - fGenRotationAxis = StringTo3DVector(GetParameter("rotationAxis", generatorDefinition, "(0,0,1)")); - fGenRotationDegree = GetDblParameterWithUnits("rotationAngle", generatorDefinition); - fGenDensityFunction = GetParameter("densityFunc", generatorDefinition, "1"); + fGeant4PrimaryGeneratorInfo.fSpatialGeneratorSize = + Get3DVectorParameterWithUnits("size", generatorDefinition); + fGeant4PrimaryGeneratorInfo.fSpatialGeneratorPosition = + Get3DVectorParameterWithUnits("position", generatorDefinition); + fGeant4PrimaryGeneratorInfo.fSpatialGeneratorRotationAxis = + StringTo3DVector(GetParameter("rotationAxis", generatorDefinition, "(0,0,1)")); + fGeant4PrimaryGeneratorInfo.fSpatialGeneratorRotationValue = + GetDblParameterWithUnits("rotationAngle", generatorDefinition); + fGeant4PrimaryGeneratorInfo.fSpatialGeneratorSpatialDensityFunction = + GetParameter("densityFunc", generatorDefinition, "1"); TiXmlElement* sourceDefinition = GetElement("source", generatorDefinition); while (sourceDefinition) { @@ -965,7 +970,7 @@ void TRestGeant4Metadata::ReadGenerator() { // check if the generator is valid. if (GetNumberOfSources() == 0) { - RESTError << "No sources are added inside geneartor!" << RESTendl; + RESTError << "No sources are added inside generator!" << RESTendl; exit(1); } } @@ -1140,35 +1145,7 @@ void TRestGeant4Metadata::PrintMetadata() { RESTMetadata << "Register empty tracks was NOT enabled" << RESTendl; RESTMetadata << " ++++++++++ Generator +++++++++++ " << RESTendl; RESTMetadata << "Number of generated events : " << GetNumberOfEvents() << RESTendl; - RESTMetadata << "Generator type : " << fGenType << RESTendl; - RESTMetadata << "Generator shape : " << fGenShape; - if (fGenShape == "gdml") { - RESTMetadata << "::" << GetGDMLGeneratorVolume() << RESTendl; - } else { - if (fGenShape == "box") { - RESTMetadata << ", (length, width, height): "; - } else if (fGenShape == "sphere") { - RESTMetadata << ", (radius, , ): "; - } else if (fGenShape == "wall") { - RESTMetadata << ", (length, width, ): "; - } else if (fGenShape == "circle") { - RESTMetadata << ", (radius, , ): "; - } else if (fGenShape == "cylinder") { - RESTMetadata << ", (radius, length, ): "; - } - - if (fGenShape != "point") { - TVector3 s = GetGeneratorSize(); - RESTMetadata << s.X() << ", " << s.Y() << ", " << s.Z() << RESTendl; - } else { - RESTMetadata << RESTendl; - } - } - TVector3 a = GetGeneratorPosition(); - RESTMetadata << "Generator center : (" << a.X() << "," << a.Y() << "," << a.Z() << ") mm" << RESTendl; - TVector3 b = GetGeneratorRotationAxis(); - RESTMetadata << "Generator rotation : (" << b.X() << "," << b.Y() << "," << b.Z() - << "), angle: " << GetGeneratorRotationDegree() << " rads" << RESTendl; + fGeant4PrimaryGeneratorInfo.Print(); for (int i = 0; i < GetNumberOfSources(); i++) GetParticleSource(i)->PrintParticleSource(); diff --git a/src/TRestGeant4PrimaryGeneratorInfo.cxx b/src/TRestGeant4PrimaryGeneratorInfo.cxx index 66ce574..ea8e157 100644 --- a/src/TRestGeant4PrimaryGeneratorInfo.cxx +++ b/src/TRestGeant4PrimaryGeneratorInfo.cxx @@ -4,6 +4,10 @@ #include "TRestGeant4PrimaryGeneratorInfo.h" +#include +#include +#include + #include using namespace std; @@ -24,7 +28,7 @@ string TRestGeant4PrimaryGeneratorTypes::SpatialGeneratorTypesToString(const Spa exit(1); } -SpatialGeneratorTypes StringToSpatialGeneratorTypes(const string& type) { +SpatialGeneratorTypes TRestGeant4PrimaryGeneratorTypes::StringToSpatialGeneratorTypes(const string& type) { if (TString(type).EqualTo(SpatialGeneratorTypesToString(SpatialGeneratorTypes::CUSTOM), TString::ECaseCompare::kIgnoreCase)) { return SpatialGeneratorTypes::CUSTOM; @@ -62,7 +66,7 @@ string TRestGeant4PrimaryGeneratorTypes::SpatialGeneratorShapesToString(const Sp exit(1); } -SpatialGeneratorShapes StringToSpatialGeneratorShapes(const string& type) { +SpatialGeneratorShapes TRestGeant4PrimaryGeneratorTypes::StringToSpatialGeneratorShapes(const string& type) { if (TString(type).EqualTo(SpatialGeneratorShapesToString(SpatialGeneratorShapes::GDML), TString::ECaseCompare::kIgnoreCase)) { return SpatialGeneratorShapes::GDML; @@ -106,7 +110,8 @@ string TRestGeant4PrimaryGeneratorTypes::EnergyDistributionTypesToString( exit(1); } -EnergyDistributionTypes StringToEnergyDistributionTypes(const string& type) { +EnergyDistributionTypes TRestGeant4PrimaryGeneratorTypes::StringToEnergyDistributionTypes( + const string& type) { if (TString(type).EqualTo(EnergyDistributionTypesToString(EnergyDistributionTypes::TH1D), TString::ECaseCompare::kIgnoreCase)) { return EnergyDistributionTypes::TH1D; @@ -146,7 +151,8 @@ string TRestGeant4PrimaryGeneratorTypes::AngularDistributionTypesToString( exit(1); } -AngularDistributionTypes StringToAngularDistributionTypes(const string& type) { +AngularDistributionTypes TRestGeant4PrimaryGeneratorTypes::StringToAngularDistributionTypes( + const string& type) { if (TString(type).EqualTo(AngularDistributionTypesToString(AngularDistributionTypes::TH1D), TString::ECaseCompare::kIgnoreCase)) { return AngularDistributionTypes::TH1D; @@ -167,3 +173,40 @@ AngularDistributionTypes StringToAngularDistributionTypes(const string& type) { exit(1); } } + +void TRestGeant4PrimaryGeneratorInfo::Print() const { + const auto typeEnum = StringToSpatialGeneratorTypes(fSpatialGeneratorType.Data()); + const auto shapeEnum = StringToSpatialGeneratorShapes(fSpatialGeneratorShape.Data()); + + RESTMetadata << "Generator type: " << fSpatialGeneratorType << RESTendl; + RESTMetadata << "Generator shape: " << fSpatialGeneratorShape; + + if (shapeEnum == SpatialGeneratorShapes::GDML) { + RESTMetadata << "::" << fSpatialGeneratorFrom << RESTendl; + } else { + if (shapeEnum == SpatialGeneratorShapes::BOX) { + RESTMetadata << ", (length, width, height): "; + } else if (shapeEnum == SpatialGeneratorShapes::SPHERE) { + RESTMetadata << ", (radius, , ): "; + } else if (shapeEnum == SpatialGeneratorShapes::WALL) { + RESTMetadata << ", (length, width, ): "; + } else if (shapeEnum == SpatialGeneratorShapes::CIRCLE) { + RESTMetadata << ", (radius, , ): "; + } else if (shapeEnum == SpatialGeneratorShapes::CYLINDER) { + RESTMetadata << ", (radius, length, ): "; + } + + if (typeEnum != SpatialGeneratorTypes::POINT) { + RESTMetadata << fSpatialGeneratorSize.X() << ", " << fSpatialGeneratorSize.Y() << ", " + << fSpatialGeneratorSize.Z() << RESTendl; + } else { + RESTMetadata << RESTendl; + } + } + RESTMetadata << "Generator center : (" << fSpatialGeneratorPosition.X() << "," + << fSpatialGeneratorPosition.Y() << "," << fSpatialGeneratorPosition.Z() << ") mm" + << RESTendl; + RESTMetadata << "Generator rotation : (" << fSpatialGeneratorRotationAxis.X() << "," + << fSpatialGeneratorRotationAxis.Y() << "," << fSpatialGeneratorRotationAxis.Z() + << "), angle: " << fSpatialGeneratorRotationValue * TMath::RadToDeg() << "º" << RESTendl; +} From 94f12cc065cc3537ac00c5ae203c645f771a25b1 Mon Sep 17 00:00:00 2001 From: lobis Date: Mon, 18 Jul 2022 15:42:49 +0200 Subject: [PATCH 030/100] Simplified generator and TRestGeant4Metadata --- inc/TRestGeant4Metadata.h | 47 ------------------------------------- src/TRestGeant4Metadata.cxx | 42 +-------------------------------- 2 files changed, 1 insertion(+), 88 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 197c861..f45beef 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -43,53 +43,6 @@ #include "TRestGeant4PhysicsInfo.h" #include "TRestGeant4PrimaryGeneratorInfo.h" -//------------------------------------------------------------------------------------------------------------------------ -// -// * This section was added by Luis A. Obis (lobis@unizar.es) on 17/06/2019 -// -// Here we add all the possible options for different configurations such as all the types of generators, etc. -// We use a structure called 'enum' and a function to clean the strings so that we can easily implement case -// insensitivity or more options such as ignoring underscores. -// - -namespace g4_metadata_parameters { -std::string CleanString(std::string); - -enum class generator_types { - CUSTOM, - VOLUME, - SURFACE, - POINT, -}; -extern std::map generator_types_map; - -enum class generator_shapes { - GDML, - WALL, - CIRCLE, - BOX, - SPHERE, - CYLINDER, -}; -extern std::map generator_shapes_map; - -enum class energy_dist_types { - TH1D, - MONO, - FLAT, - LOG, -}; -extern std::map energy_dist_types_map; - -enum class angular_dist_types { - TH1D, - ISOTROPIC, - FLUX, - BACK_TO_BACK, -}; -extern std::map angular_dist_types_map; -} // namespace g4_metadata_parameters - /// The main class to store the *Geant4* simulation conditions that will be used by *restG4*. class TRestGeant4Metadata : public TRestMetadata { private: diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 7f56ccf..7a26326 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -650,51 +650,11 @@ #include #include +#include #include -#include "TRestGDMLParser.h" - using namespace std; -namespace g4_metadata_parameters { -string CleanString(string s) { - // transform the string to lowercase - transform(s.begin(), s.end(), s.begin(), ::tolower); - // this is a temporary fix, TH1D name comparison is being done elsewhere and giving error - if (s == "th1d") { - s = "TH1D"; - } - return s; -} - -map generator_types_map = { - {CleanString("custom"), generator_types::CUSTOM}, - {CleanString("volume"), generator_types::VOLUME}, - {CleanString("surface"), generator_types::SURFACE}, - {CleanString("point"), generator_types::POINT}, -}; - -std::map generator_shapes_map = { - {CleanString("gdml"), generator_shapes::GDML}, {CleanString("wall"), generator_shapes::WALL}, - {CleanString("circle"), generator_shapes::CIRCLE}, {CleanString("box"), generator_shapes::BOX}, - {CleanString("sphere"), generator_shapes::SPHERE}, {CleanString("cylinder"), generator_shapes::CYLINDER}, -}; - -map energy_dist_types_map = { - {CleanString("TH1D"), energy_dist_types::TH1D}, - {CleanString("mono"), energy_dist_types::MONO}, - {CleanString("flat"), energy_dist_types::FLAT}, - {CleanString("log"), energy_dist_types::LOG}, -}; - -map angular_dist_types_map = { - {CleanString("TH1D"), angular_dist_types::TH1D}, - {CleanString("isotropic"), angular_dist_types::ISOTROPIC}, - {CleanString("flux"), angular_dist_types::FLUX}, - {CleanString("backtoback"), angular_dist_types::BACK_TO_BACK}, -}; -} // namespace g4_metadata_parameters - ClassImp(TRestGeant4Metadata); /////////////////////////////////////////////// From d85c62a61c7fb167ed2818858210813bc1be2969 Mon Sep 17 00:00:00 2001 From: lobis Date: Mon, 18 Jul 2022 15:51:07 +0200 Subject: [PATCH 031/100] TRestGeant4Metadata - normalized name --- src/TRestGeant4Metadata.cxx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 7a26326..3094eda 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -653,7 +653,10 @@ #include #include +#include "TRestGeant4PrimaryGeneratorInfo.h" + using namespace std; +using namespace TRestGeant4PrimaryGeneratorTypes; ClassImp(TRestGeant4Metadata); @@ -900,8 +903,10 @@ void TRestGeant4Metadata::ReadGenerator() { TiXmlElement* generatorDefinition = GetElement("generator"); - fGeant4PrimaryGeneratorInfo.fSpatialGeneratorType = GetParameter("type", generatorDefinition, "volume"); - fGeant4PrimaryGeneratorInfo.fSpatialGeneratorShape = GetParameter("shape", generatorDefinition, "box"); + fGeant4PrimaryGeneratorInfo.fSpatialGeneratorType = SpatialGeneratorTypesToString( + StringToSpatialGeneratorTypes(GetParameter("type", generatorDefinition, "volume"))); + fGeant4PrimaryGeneratorInfo.fSpatialGeneratorShape = SpatialGeneratorShapesToString( + StringToSpatialGeneratorShapes(GetParameter("shape", generatorDefinition, "box"))); fGeant4PrimaryGeneratorInfo.fSpatialGeneratorFrom = GetParameter("from", generatorDefinition); if (fGeant4PrimaryGeneratorInfo.fSpatialGeneratorFrom != PARAMETER_NOT_FOUND_STR) { fGeant4PrimaryGeneratorInfo.fSpatialGeneratorShape = "gdml"; From 374100542030f24338fa59215b229c4f4a00ad16 Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 19 Jul 2022 10:40:32 +0200 Subject: [PATCH 032/100] TRestGeant4Metadata - fixed bug on print --- inc/TRestGeant4Metadata.h | 6 +-- inc/TRestGeant4PrimaryGeneratorInfo.h | 8 +-- src/TRestGeant4Metadata.cxx | 2 +- src/TRestGeant4PrimaryGeneratorInfo.cxx | 71 +++++++++++++++---------- 4 files changed, 50 insertions(+), 37 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index f45beef..6b90f08 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -112,18 +112,18 @@ class TRestGeant4Metadata : public TRestMetadata { /// \brief A time gap, in us, determining if an energy hit should be considered (and /// stored) as an independent event. - Double_t fSubEventTimeDelay; + Double_t fSubEventTimeDelay = 100; /// \brief Defines if a radioactive isotope decay is simulated in full chain /// (fFullChain=true), or just a single decay (fFullChain=false). - Bool_t fFullChain; + Bool_t fFullChain = true; /// \brief The volume that serves as trigger for data storage. Only events that /// deposit a non-zero energy on this volume will be registered. TString fSensitiveVolume; /// The number of events simulated, or to be simulated. - Int_t fNEvents; + Int_t fNEvents = 0; /// \brief The seed value used for Geant4 random event generator. If it is zero /// its value will be assigned using the system timestamp. diff --git a/inc/TRestGeant4PrimaryGeneratorInfo.h b/inc/TRestGeant4PrimaryGeneratorInfo.h index f7dc450..ba37263 100644 --- a/inc/TRestGeant4PrimaryGeneratorInfo.h +++ b/inc/TRestGeant4PrimaryGeneratorInfo.h @@ -65,10 +65,10 @@ class TRestGeant4PrimaryGeneratorInfo { private: /* Members related to spatial generator */ - /// Type of spatial generator (surface, volume, custom) - TString fSpatialGeneratorType; + /// Type of spatial generator (point, surface, volume, custom) + TString fSpatialGeneratorType = "point"; - /// Shape of spatial generator (point, wall, GDML, sphere, etc) + /// Shape of spatial generator (wall, GDML, sphere, etc) TString fSpatialGeneratorShape; /// The volume name where the events are generated, in case of volume or surface generator types @@ -83,7 +83,7 @@ class TRestGeant4PrimaryGeneratorInfo { TVector3 fSpatialGeneratorRotationAxis; /// \brief degrees of rotation for generator virtual shape along the axis - Double_t fSpatialGeneratorRotationValue; + Double_t fSpatialGeneratorRotationValue = 0; /// \brief The size of the generator. Stores up to three dimensions according to the shape /// box: length, width, height diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 3094eda..030a720 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1102,7 +1102,7 @@ void TRestGeant4Metadata::PrintMetadata() { Double_t mx = GetMagneticField().X(); Double_t my = GetMagneticField().Y(); Double_t mz = GetMagneticField().Z(); - RESTMetadata << "Magnetic field : ( " << mx << ", " << my << ", " << mz << ") T" << RESTendl; + RESTMetadata << "Magnetic field : (" << mx << ", " << my << ", " << mz << ") T" << RESTendl; if (fSaveAllEvents) RESTMetadata << "Save all events was enabled!" << RESTendl; if (fRegisterEmptyTracks) RESTMetadata << "Register empty tracks was enabled" << RESTendl; diff --git a/src/TRestGeant4PrimaryGeneratorInfo.cxx b/src/TRestGeant4PrimaryGeneratorInfo.cxx index ea8e157..74b99af 100644 --- a/src/TRestGeant4PrimaryGeneratorInfo.cxx +++ b/src/TRestGeant4PrimaryGeneratorInfo.cxx @@ -24,7 +24,9 @@ string TRestGeant4PrimaryGeneratorTypes::SpatialGeneratorTypesToString(const Spa case SpatialGeneratorTypes::POINT: return "Point"; } - cout << "Error: Unknown SpatialGeneratorTypes" << endl; + cout << "TRestGeant4PrimaryGeneratorTypes::SpatialGeneratorTypesToString - Error - Unknown " + "SpatialGeneratorTypes" + << endl; exit(1); } @@ -42,7 +44,9 @@ SpatialGeneratorTypes TRestGeant4PrimaryGeneratorTypes::StringToSpatialGenerator TString::ECaseCompare::kIgnoreCase)) { return SpatialGeneratorTypes::POINT; } else { - cout << "Error: Unknown SpatialGeneratorTypes: " << type << endl; + cout << "TRestGeant4PrimaryGeneratorTypes::StringToSpatialGeneratorTypes - Error - Unknown " + "SpatialGeneratorTypes: " + << type << endl; exit(1); } } @@ -62,7 +66,9 @@ string TRestGeant4PrimaryGeneratorTypes::SpatialGeneratorShapesToString(const Sp case SpatialGeneratorShapes::CYLINDER: return "Cylinder"; } - cout << "Error: Unknown SpatialGeneratorShapes" << endl; + cout << "TRestGeant4PrimaryGeneratorTypes::SpatialGeneratorShapesToString - Error - Unknown " + "SpatialGeneratorShapes" + << endl; exit(1); } @@ -87,7 +93,9 @@ SpatialGeneratorShapes TRestGeant4PrimaryGeneratorTypes::StringToSpatialGenerato return SpatialGeneratorShapes::CYLINDER; } else { - cout << "Error: Unknown SpatialGeneratorShapes: " << type << endl; + cout << "TRestGeant4PrimaryGeneratorTypes::StringToSpatialGeneratorShapes - Error - Unknown " + "SpatialGeneratorShapes: " + << type << endl; exit(1); } } @@ -106,7 +114,9 @@ string TRestGeant4PrimaryGeneratorTypes::EnergyDistributionTypesToString( case EnergyDistributionTypes::LOG: return "Log"; } - cout << "Error: Unknown EnergyDistributionTypes" << endl; + cout << "TRestGeant4PrimaryGeneratorTypes::EnergyDistributionTypesToString - Error - Unknown energy " + "distribution type" + << endl; exit(1); } @@ -128,7 +138,9 @@ EnergyDistributionTypes TRestGeant4PrimaryGeneratorTypes::StringToEnergyDistribu TString::ECaseCompare::kIgnoreCase)) { return EnergyDistributionTypes::LOG; } else { - cout << "Error: Unknown EnergyDistributionTypes: " << type << endl; + cout << "TRestGeant4PrimaryGeneratorTypes::StringToEnergyDistributionTypes - Error - Unknown " + "EnergyDistributionTypes: " + << type << endl; exit(1); } } @@ -147,7 +159,9 @@ string TRestGeant4PrimaryGeneratorTypes::AngularDistributionTypesToString( case AngularDistributionTypes::BACK_TO_BACK: return "Back to back"; } - cout << "Error: Unknown AngularDistributionTypes" << endl; + cout << "TRestGeant4PrimaryGeneratorTypes::AngularDistributionTypesToString - Error - Unknown angular " + "distribution type" + << endl; exit(1); } @@ -169,38 +183,37 @@ AngularDistributionTypes TRestGeant4PrimaryGeneratorTypes::StringToAngularDistri TString::ECaseCompare::kIgnoreCase)) { return AngularDistributionTypes::BACK_TO_BACK; } else { - cout << "Error: Unknown AngularDistributionTypes: " << type << endl; + cout << "TRestGeant4PrimaryGeneratorTypes::StringToAngularDistributionTypes - Error - Unknown " + "AngularDistributionTypes: " + << type << endl; exit(1); } } void TRestGeant4PrimaryGeneratorInfo::Print() const { const auto typeEnum = StringToSpatialGeneratorTypes(fSpatialGeneratorType.Data()); - const auto shapeEnum = StringToSpatialGeneratorShapes(fSpatialGeneratorShape.Data()); + RESTMetadata << "Generator type: " << SpatialGeneratorTypesToString(typeEnum) << RESTendl; - RESTMetadata << "Generator type: " << fSpatialGeneratorType << RESTendl; - RESTMetadata << "Generator shape: " << fSpatialGeneratorShape; - - if (shapeEnum == SpatialGeneratorShapes::GDML) { - RESTMetadata << "::" << fSpatialGeneratorFrom << RESTendl; - } else { - if (shapeEnum == SpatialGeneratorShapes::BOX) { - RESTMetadata << ", (length, width, height): "; - } else if (shapeEnum == SpatialGeneratorShapes::SPHERE) { - RESTMetadata << ", (radius, , ): "; - } else if (shapeEnum == SpatialGeneratorShapes::WALL) { - RESTMetadata << ", (length, width, ): "; - } else if (shapeEnum == SpatialGeneratorShapes::CIRCLE) { - RESTMetadata << ", (radius, , ): "; - } else if (shapeEnum == SpatialGeneratorShapes::CYLINDER) { - RESTMetadata << ", (radius, length, ): "; - } + if (typeEnum != SpatialGeneratorTypes::POINT) { + const auto shapeEnum = StringToSpatialGeneratorShapes(fSpatialGeneratorShape.Data()); + RESTMetadata << "Generator shape: " << SpatialGeneratorShapesToString(shapeEnum); + if (shapeEnum == SpatialGeneratorShapes::GDML) { + RESTMetadata << "::" << fSpatialGeneratorFrom << RESTendl; + } else { + if (shapeEnum == SpatialGeneratorShapes::BOX) { + RESTMetadata << ", (length, width, height): "; + } else if (shapeEnum == SpatialGeneratorShapes::SPHERE) { + RESTMetadata << ", (radius, , ): "; + } else if (shapeEnum == SpatialGeneratorShapes::WALL) { + RESTMetadata << ", (length, width, ): "; + } else if (shapeEnum == SpatialGeneratorShapes::CIRCLE) { + RESTMetadata << ", (radius, , ): "; + } else if (shapeEnum == SpatialGeneratorShapes::CYLINDER) { + RESTMetadata << ", (radius, length, ): "; + } - if (typeEnum != SpatialGeneratorTypes::POINT) { RESTMetadata << fSpatialGeneratorSize.X() << ", " << fSpatialGeneratorSize.Y() << ", " << fSpatialGeneratorSize.Z() << RESTendl; - } else { - RESTMetadata << RESTendl; } } RESTMetadata << "Generator center : (" << fSpatialGeneratorPosition.X() << "," From 1bcf1b38261e6f2462272824cb1ebf256a6cdd1f Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 19 Jul 2022 10:41:35 +0200 Subject: [PATCH 033/100] TRestGeant4Metadata - removed TODO --- inc/TRestGeant4Metadata.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 6b90f08..7c47cec 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -63,7 +63,7 @@ class TRestGeant4Metadata : public TRestMetadata { TRestGeant4PhysicsInfo fGeant4PhysicsInfo; /// Class used to store and retrieve Geant4 primary generator info - TRestGeant4PrimaryGeneratorInfo fGeant4PrimaryGeneratorInfo; // TODO: Implement this + TRestGeant4PrimaryGeneratorInfo fGeant4PrimaryGeneratorInfo; /// The version of Geant4 used to generate the data TString fGeant4Version; From d4c8a2026e637e33907267fca9863edcb72fc038 Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 19 Jul 2022 11:28:23 +0200 Subject: [PATCH 034/100] Starting to add formula support for angular/energy dist --- inc/TRestGeant4ParticleSource.h | 62 +++++++++++++------------ inc/TRestGeant4PrimaryGeneratorInfo.h | 9 ++++ src/TRestGeant4ParticleSource.cxx | 34 +++++++------- src/TRestGeant4PrimaryGeneratorInfo.cxx | 47 +++++++++++++++++++ 4 files changed, 105 insertions(+), 47 deletions(-) diff --git a/inc/TRestGeant4ParticleSource.h b/inc/TRestGeant4ParticleSource.h index 57dc2c5..5d5e62b 100644 --- a/inc/TRestGeant4ParticleSource.h +++ b/inc/TRestGeant4ParticleSource.h @@ -35,23 +35,24 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada bool ReadOldDecay0File(TString fileName); protected: - TString fAngularDistType; - TString fEnergyDistType; - TVector2 fEnergyRange; + TString fAngularDistributionType; + TString fAngularDistributionFilename; + TString fAngularDistributionNameInFile; + TString fAngularDistributionFormulaString; - TString fSpectrumFilename; - TString fSpectrumName; - - TString fAngularFilename; - TString fAngularName; + TString fEnergyDistributionType; + TString fEnergyDistributionFilename; + TString fEnergyDistributionNameInFile; + TString fEnergyDistributionFormulaString; + TVector2 fEnergyDistributionRange; TString fGenFilename; // store a set of generated particles - std::vector fParticles; // + std::vector fParticles; // store a list of particle set templates that could be used to generate fParticles std::vector> fParticlesTemplate; //! // the random method to generate 0~1 equally distributed numbers - double (*fRndmMethod)(); //! + double (*fRandomMethod)(); //! public: virtual void Update(); @@ -59,37 +60,40 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada static TRestGeant4ParticleSource* instantiate(std::string model = ""); inline TString GetParticleName() const { return fParticleName; } - inline TString GetAngularDistType() const { return fAngularDistType; } + inline TString GetAngularDistType() const { return fAngularDistributionType; } inline TVector3 GetDirection() const { return fDirection; } - inline TString GetEnergyDistType() const { return fEnergyDistType; } - inline TVector2 GetEnergyRange() const { return fEnergyRange; } - inline Double_t GetMinEnergy() const { return fEnergyRange.X(); } - inline Double_t GetMaxEnergy() const { return fEnergyRange.Y(); } - inline TString GetSpectrumFilename() const { return fSpectrumFilename; } - inline TString GetSpectrumName() const { return fSpectrumName; } - inline TString GetAngularFilename() const { return fAngularFilename; } - inline TString GetAngularName() const { return fAngularName; } + inline TString GetEnergyDistType() const { return fEnergyDistributionType; } + inline TVector2 GetEnergyRange() const { return fEnergyDistributionRange; } + inline Double_t GetMinEnergy() const { return fEnergyDistributionRange.X(); } + inline Double_t GetMaxEnergy() const { return fEnergyDistributionRange.Y(); } + inline TString GetSpectrumFilename() const { return fEnergyDistributionFilename; } + inline TString GetSpectrumName() const { return fEnergyDistributionNameInFile; } + inline TString GetAngularFilename() const { return fAngularDistributionFilename; } + inline TString GetAngularName() const { return fAngularDistributionNameInFile; } inline TString GetGenFilename() const { return fGenFilename; } inline std::vector GetParticles() const { return fParticles; } - inline void SetAngularDistType(const TString& type) { fAngularDistType = type; } - inline void SetEnergyDistType(const TString& type) { fEnergyDistType = type; } - inline void SetEnergyRange(const TVector2& range) { fEnergyRange = range; } - inline void SetSpectrumFilename(const TString& spectrumFilename) { fSpectrumFilename = spectrumFilename; } - inline void SetSpectrumName(const TString& spectrumName) { fSpectrumName = spectrumName; } - inline void SetAngularFilename(const TString& angFilename) { fAngularFilename = angFilename; } - inline void SetAngularName(const TString& angularName) { fAngularName = angularName; } + inline void SetAngularDistType(const TString& type) { fAngularDistributionType = type; } + inline void SetEnergyDistType(const TString& type) { fEnergyDistributionType = type; } + inline void SetEnergyRange(const TVector2& range) { fEnergyDistributionRange = range; } + inline void SetSpectrumFilename(const TString& spectrumFilename) { + fEnergyDistributionFilename = spectrumFilename; + } + inline void SetSpectrumName(const TString& spectrumName) { fEnergyDistributionNameInFile = spectrumName; } + inline void SetAngularFilename(const TString& angFilename) { fAngularDistributionFilename = angFilename; } + inline void SetAngularName(const TString& angularName) { fAngularDistributionNameInFile = angularName; } inline void SetGenFilename(const TString& name) { fGenFilename = name; } - inline void SetRndmMethod(double (*method)()) { fRndmMethod = method; } + inline void setRandomMethod(double (*method)()) { fRandomMethod = method; } inline void AddParticle(const TRestGeant4Particle& particle) { fParticles.push_back(particle); } + inline void RemoveParticles() { fParticles.clear(); } + inline void RemoveTemplates() { fParticlesTemplate.clear(); } inline void FlushParticlesTemplate() { fParticlesTemplate.push_back(fParticles); fParticles.clear(); } - inline void RemoveTemplates() { fParticlesTemplate.clear(); } virtual void PrintParticleSource(); @@ -98,6 +102,6 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada // Destructor virtual ~TRestGeant4ParticleSource(); - ClassDef(TRestGeant4ParticleSource, 3); + ClassDef(TRestGeant4ParticleSource, 4); }; #endif diff --git a/inc/TRestGeant4PrimaryGeneratorInfo.h b/inc/TRestGeant4PrimaryGeneratorInfo.h index ba37263..f400929 100644 --- a/inc/TRestGeant4PrimaryGeneratorInfo.h +++ b/inc/TRestGeant4PrimaryGeneratorInfo.h @@ -2,6 +2,7 @@ #ifndef REST_TRESTGEANT4PRIMARYGENERATORINFO_H #define REST_TRESTGEANT4PRIMARYGENERATORINFO_H +#include #include #include @@ -53,6 +54,14 @@ enum class AngularDistributionTypes { std::string AngularDistributionTypesToString(const AngularDistributionTypes&); AngularDistributionTypes StringToAngularDistributionTypes(const std::string&); +enum class AngularDistributionFormulas { + COS2, +}; + +std::string AngularDistributionFormulasToString(const AngularDistributionFormulas&); +AngularDistributionFormulas StringToAngularDistributionFormulas(const std::string&); +TF1 AngularDistributionFormulasToRootFormula(const AngularDistributionFormulas&); + } // namespace TRestGeant4PrimaryGeneratorTypes class TiXmlElement; diff --git a/src/TRestGeant4ParticleSource.cxx b/src/TRestGeant4ParticleSource.cxx index 16b6c45..8c4da89 100644 --- a/src/TRestGeant4ParticleSource.cxx +++ b/src/TRestGeant4ParticleSource.cxx @@ -36,8 +36,8 @@ ClassImp(TRestGeant4ParticleSource); TRestGeant4ParticleSource::TRestGeant4ParticleSource() { // TRestGeant4ParticleSource default constructor - fAngularDistType = "flux"; - fEnergyDistType = "mono"; + fAngularDistributionType = "flux"; + fEnergyDistributionType = "mono"; } TRestGeant4ParticleSource::~TRestGeant4ParticleSource() { @@ -51,38 +51,38 @@ void TRestGeant4ParticleSource::PrintParticleSource() { RESTMetadata << "Generator file: " << GetGenFilename() << RESTendl; RESTMetadata << "Stored templates: " << fParticlesTemplate.size() << RESTendl; RESTMetadata << "Particles: "; - for (auto p : fParticles) RESTMetadata << p.GetParticleName() << ", "; + for (const auto& particle : fParticles) RESTMetadata << particle.GetParticleName() << ", "; RESTMetadata << RESTendl; } else { RESTMetadata << "Charge : " << GetParticleCharge() << RESTendl; RESTMetadata << "Angular distribution type : " << GetAngularDistType() << RESTendl; if (GetAngularDistType() == "TH1D") { RESTMetadata << "Angular distribution filename : " - << TRestTools::GetPureFileName((string)GetAngularFilename()) << RESTendl; + << TRestTools::GetPureFileName((string)GetAngularFilename()) << RESTendl; RESTMetadata << "Angular histogram name : " << GetAngularName() << RESTendl; } RESTMetadata << "Direction : (" << GetDirection().X() << "," << GetDirection().Y() << "," - << GetDirection().Z() << ")" << RESTendl; + << GetDirection().Z() << ")" << RESTendl; RESTMetadata << "Energy distribution : " << GetEnergyDistType() << RESTendl; if (GetEnergyDistType() == "TH1D") { RESTMetadata << "Energy distribution filename : " - << TRestTools::GetPureFileName((string)GetSpectrumFilename()) << RESTendl; + << TRestTools::GetPureFileName((string)GetSpectrumFilename()) << RESTendl; RESTMetadata << "Energy histogram name : " << GetSpectrumName() << RESTendl; } else if (GetEnergyRange().X() == GetEnergyRange().Y()) RESTMetadata << "Energy : " << GetEnergy() << " keV" << RESTendl; else - RESTMetadata << "Energy range : (" << GetEnergyRange().X() << "," << GetEnergyRange().Y() << ") keV" - << RESTendl; + RESTMetadata << "Energy range : (" << GetEnergyRange().X() << "," << GetEnergyRange().Y() + << ") keV" << RESTendl; } } TRestGeant4ParticleSource* TRestGeant4ParticleSource::instantiate(std::string model) { - if (model == "" || model == "geant4" || model.find(".dat") != -1) { + if (model.empty() || model == "geant4" || model.find(".dat") != -1) { // use default generator return new TRestGeant4ParticleSource(); } else { // use specific generator - // naming convension: TRestGeant4ParticleSourceXXX + // naming convention: TRestGeant4ParticleSourceXXX // currently supported generator: decay0 // in future we may add wrapper of generators: cry(for muon), pythia(for HEP), etc. model[0] = *REST_StringHelper::ToUpper(std::string(&model[0], 1)).c_str(); @@ -120,17 +120,14 @@ void TRestGeant4ParticleSource::InitFromConfigFile() { // base class's generator action: randomize the particle's energy/direction with distribution file void TRestGeant4ParticleSource::Update() { - if (fParticlesTemplate.size() > 0) { + if (!fParticlesTemplate.empty()) { // we use particle template to generate particles - Int_t rndCollection = (Int_t)(fRndmMethod() * fParticlesTemplate.size()); + Int_t rndCollection = (Int_t)(fRandomMethod() * fParticlesTemplate.size()); Int_t pCollectionID = rndCollection % fParticlesTemplate.size(); fParticles = fParticlesTemplate[pCollectionID]; } else { TRestGeant4Particle p(*this); - // Future: implement particle generation for toy simulation - // - // - + // TODO: implement particle generation for toy simulation fParticles = {p}; } } @@ -281,8 +278,9 @@ bool TRestGeant4ParticleSource::ReadOldDecay0File(TString fileName) { } } if (!headerFound) { - RESTError << "TRestG4Metadata::ReadOldDecay0File. Problem reading generator file: no \"First event and " - "full number of events:\" header.\n"; + RESTError + << "TRestG4Metadata::ReadOldDecay0File. Problem reading generator file: no \"First event and " + "full number of events:\" header.\n"; abort(); } int tmpInt; diff --git a/src/TRestGeant4PrimaryGeneratorInfo.cxx b/src/TRestGeant4PrimaryGeneratorInfo.cxx index 74b99af..c0f096b 100644 --- a/src/TRestGeant4PrimaryGeneratorInfo.cxx +++ b/src/TRestGeant4PrimaryGeneratorInfo.cxx @@ -190,6 +190,53 @@ AngularDistributionTypes TRestGeant4PrimaryGeneratorTypes::StringToAngularDistri } } +std::string AngularDistributionFormulasToString(const AngularDistributionFormulas&); +AngularDistributionTypes StringToAngularDistributionFormulas(const std::string&); +TFormula AngularDistributionFormulasToRootFormula(const AngularDistributionFormulas&); + +string TRestGeant4PrimaryGeneratorTypes::AngularDistributionFormulasToString( + const AngularDistributionFormulas& type) { + switch (type) { + case AngularDistributionFormulas::COS2: + return "Cos2"; + } + cout << "TRestGeant4PrimaryGeneratorTypes::AngularDistributionFormulasToString - Error - Unknown angular " + "distribution formula" + << endl; + exit(1); +} + +AngularDistributionFormulas TRestGeant4PrimaryGeneratorTypes::StringToAngularDistributionFormulas( + const string& type) { + if (TString(type).EqualTo(AngularDistributionFormulasToString(AngularDistributionFormulas::COS2), + TString::ECaseCompare::kIgnoreCase)) { + return AngularDistributionFormulas::COS2; + } else { + cout << "TRestGeant4PrimaryGeneratorTypes::StringToAngularDistributionFormulas - Error - Unknown " + "AngularDistributionFormulas: " + << type << endl; + exit(1); + } +} + +TF1 TRestGeant4PrimaryGeneratorTypes::AngularDistributionFormulasToRootFormula( + const AngularDistributionFormulas& type) { + switch (type) { + case AngularDistributionFormulas::COS2: + auto cos2 = [](double* xs, double* ps) { + if (xs[0] >= 0 && xs[0] <= TMath::Pi() / 2) { + return TMath::Power(TMath::Cos(xs[0]), 2); + } + return 0.0; + }; + return TF1("AngularDistribution: Cos2", cos2, 0.0, TMath::Pi()); + } + cout << "TRestGeant4PrimaryGeneratorTypes::AngularDistributionFormulasToRootFormula - Error - Unknown " + "angular distribution formula" + << endl; + exit(1); +} + void TRestGeant4PrimaryGeneratorInfo::Print() const { const auto typeEnum = StringToSpatialGeneratorTypes(fSpatialGeneratorType.Data()); RESTMetadata << "Generator type: " << SpatialGeneratorTypesToString(typeEnum) << RESTendl; From 2ba9e4222524e6ae261080a8a2dd0824461f9199 Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 19 Jul 2022 11:47:34 +0200 Subject: [PATCH 035/100] TRestGeant4PrimaryGeneratorInfo - added energy formula --- inc/TRestGeant4PrimaryGeneratorInfo.h | 10 ++++ src/TRestGeant4PrimaryGeneratorInfo.cxx | 68 +++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/inc/TRestGeant4PrimaryGeneratorInfo.h b/inc/TRestGeant4PrimaryGeneratorInfo.h index f400929..4f9f7d1 100644 --- a/inc/TRestGeant4PrimaryGeneratorInfo.h +++ b/inc/TRestGeant4PrimaryGeneratorInfo.h @@ -43,6 +43,16 @@ enum class EnergyDistributionTypes { std::string EnergyDistributionTypesToString(const EnergyDistributionTypes&); EnergyDistributionTypes StringToEnergyDistributionTypes(const std::string&); +enum class EnergyDistributionFormulas { + COSMIC_MUONS, + COSMIC_NEUTRONS, + COSMIC_GAMMAS, +}; + +std::string EnergyDistributionFormulasToString(const EnergyDistributionFormulas&); +EnergyDistributionFormulas StringToEnergyDistributionFormulas(const std::string&); +TF1 EnergyDistributionFormulasToRootFormula(const EnergyDistributionFormulas&); + enum class AngularDistributionTypes { TH1D, FORMULA, diff --git a/src/TRestGeant4PrimaryGeneratorInfo.cxx b/src/TRestGeant4PrimaryGeneratorInfo.cxx index c0f096b..c5da40c 100644 --- a/src/TRestGeant4PrimaryGeneratorInfo.cxx +++ b/src/TRestGeant4PrimaryGeneratorInfo.cxx @@ -145,6 +145,67 @@ EnergyDistributionTypes TRestGeant4PrimaryGeneratorTypes::StringToEnergyDistribu } } +string TRestGeant4PrimaryGeneratorTypes::EnergyDistributionFormulasToString( + const EnergyDistributionFormulas& type) { + switch (type) { + case EnergyDistributionFormulas::COSMIC_MUONS: + return "CosmicMuons"; + case EnergyDistributionFormulas::COSMIC_NEUTRONS: + return "CosmicNeutrons"; + case EnergyDistributionFormulas::COSMIC_GAMMAS: + return "CosmicGammas"; + } + cout << "TRestGeant4PrimaryGeneratorTypes::EnergyDistributionFormulasToString - Error - Unknown energy " + "distribution formula" + << endl; + exit(1); +} + +EnergyDistributionFormulas TRestGeant4PrimaryGeneratorTypes::StringToEnergyDistributionFormulas( + const string& type) { + if (TString(type).EqualTo(EnergyDistributionFormulasToString(EnergyDistributionFormulas::COSMIC_MUONS), + TString::ECaseCompare::kIgnoreCase)) { + return EnergyDistributionFormulas::COSMIC_MUONS; + } else if (TString(type).EqualTo( + EnergyDistributionFormulasToString(EnergyDistributionFormulas::COSMIC_NEUTRONS), + TString::ECaseCompare::kIgnoreCase)) { + return EnergyDistributionFormulas::COSMIC_NEUTRONS; + } else if (TString(type).EqualTo( + EnergyDistributionFormulasToString(EnergyDistributionFormulas::COSMIC_GAMMAS), + TString::ECaseCompare::kIgnoreCase)) { + return EnergyDistributionFormulas::COSMIC_GAMMAS; + } else { + cout << "TRestGeant4PrimaryGeneratorTypes::StringToEnergyDistributionFormulas - Error - Unknown " + "energyDistributionFormulas: " + << type << endl; + exit(1); + } +} + +TF1 TRestGeant4PrimaryGeneratorTypes::EnergyDistributionFormulasToRootFormula( + const EnergyDistributionFormulas& type) { + switch (type) { + case EnergyDistributionFormulas::COSMIC_MUONS: + exit(1); + case EnergyDistributionFormulas::COSMIC_NEUTRONS: { + const char* title = "EnergyDistribution: Cosmic Neutrons Sea Level"; + auto f = TF1( + title, + "1.006E-6 * TMath::Exp(-0.3500 * TMath::Power(TMath::Log(x), 2) + 2.1451 * TMath::Log(x)) + " + "1.011E-3 * TMath::Exp(-0.4106 * TMath::Power(TMath::Log(x), 2) -0.6670 * TMath::Log(x))", + 0.1E0, 10E3); + f.SetTitle(title); + return f; + } + case EnergyDistributionFormulas::COSMIC_GAMMAS: + exit(1); + } + cout << "TRestGeant4PrimaryGeneratorTypes::EnergyDistributionFormulasToRootFormula - Error - Unknown " + "energy distribution formula" + << endl; + exit(1); +} + string TRestGeant4PrimaryGeneratorTypes::AngularDistributionTypesToString( const AngularDistributionTypes& type) { switch (type) { @@ -190,10 +251,6 @@ AngularDistributionTypes TRestGeant4PrimaryGeneratorTypes::StringToAngularDistri } } -std::string AngularDistributionFormulasToString(const AngularDistributionFormulas&); -AngularDistributionTypes StringToAngularDistributionFormulas(const std::string&); -TFormula AngularDistributionFormulasToRootFormula(const AngularDistributionFormulas&); - string TRestGeant4PrimaryGeneratorTypes::AngularDistributionFormulasToString( const AngularDistributionFormulas& type) { switch (type) { @@ -222,7 +279,7 @@ AngularDistributionFormulas TRestGeant4PrimaryGeneratorTypes::StringToAngularDis TF1 TRestGeant4PrimaryGeneratorTypes::AngularDistributionFormulasToRootFormula( const AngularDistributionFormulas& type) { switch (type) { - case AngularDistributionFormulas::COS2: + case AngularDistributionFormulas::COS2: { auto cos2 = [](double* xs, double* ps) { if (xs[0] >= 0 && xs[0] <= TMath::Pi() / 2) { return TMath::Power(TMath::Cos(xs[0]), 2); @@ -230,6 +287,7 @@ TF1 TRestGeant4PrimaryGeneratorTypes::AngularDistributionFormulasToRootFormula( return 0.0; }; return TF1("AngularDistribution: Cos2", cos2, 0.0, TMath::Pi()); + } } cout << "TRestGeant4PrimaryGeneratorTypes::AngularDistributionFormulasToRootFormula - Error - Unknown " "angular distribution formula" From f9726c7724db2a2430bb4b4951bdab0c0069df05 Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 19 Jul 2022 12:50:59 +0200 Subject: [PATCH 036/100] TRestGeant4Metadata - updated some member names --- inc/TRestGeant4ParticleSource.h | 54 ++++++++++++---------- inc/TRestGeant4PrimaryGeneratorInfo.h | 1 + src/TRestGeant4Metadata.cxx | 60 ++++++++++++------------- src/TRestGeant4ParticleSource.cxx | 56 +++++++++-------------- src/TRestGeant4PrimaryGeneratorInfo.cxx | 22 ++++++++- test/src/TRestGeant4Metadata.cxx | 2 +- 6 files changed, 106 insertions(+), 89 deletions(-) diff --git a/inc/TRestGeant4ParticleSource.h b/inc/TRestGeant4ParticleSource.h index 5d5e62b..e847072 100644 --- a/inc/TRestGeant4ParticleSource.h +++ b/inc/TRestGeant4ParticleSource.h @@ -17,16 +17,14 @@ #ifndef RestCore_TRestGeant4ParticleSource #define RestCore_TRestGeant4ParticleSource +#include #include #include #include #include -#include "TObject.h" -// #include "TRestGeant4Particle.h" -#include "TRestMetadata.h" class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetadata { private: @@ -35,12 +33,12 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada bool ReadOldDecay0File(TString fileName); protected: - TString fAngularDistributionType; + TString fAngularDistributionType = "Flux"; TString fAngularDistributionFilename; TString fAngularDistributionNameInFile; TString fAngularDistributionFormulaString; - TString fEnergyDistributionType; + TString fEnergyDistributionType = "Mono"; TString fEnergyDistributionFilename; TString fEnergyDistributionNameInFile; TString fEnergyDistributionFormulaString; @@ -60,31 +58,41 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada static TRestGeant4ParticleSource* instantiate(std::string model = ""); inline TString GetParticleName() const { return fParticleName; } - inline TString GetAngularDistType() const { return fAngularDistributionType; } inline TVector3 GetDirection() const { return fDirection; } - inline TString GetEnergyDistType() const { return fEnergyDistributionType; } - inline TVector2 GetEnergyRange() const { return fEnergyDistributionRange; } - inline Double_t GetMinEnergy() const { return fEnergyDistributionRange.X(); } - inline Double_t GetMaxEnergy() const { return fEnergyDistributionRange.Y(); } - inline TString GetSpectrumFilename() const { return fEnergyDistributionFilename; } - inline TString GetSpectrumName() const { return fEnergyDistributionNameInFile; } - inline TString GetAngularFilename() const { return fAngularDistributionFilename; } - inline TString GetAngularName() const { return fAngularDistributionNameInFile; } + + inline TString GetEnergyDistributionType() const { return fEnergyDistributionType; } + inline TVector2 GetEnergyDistributionRange() const { return fEnergyDistributionRange; } + inline Double_t GetEnergyDistributionRangeMin() const { return fEnergyDistributionRange.X(); } + inline Double_t GetEnergyDistributionRangeMax() const { return fEnergyDistributionRange.Y(); } + inline TString GetEnergyDistributionFilename() const { return fEnergyDistributionFilename; } + inline TString GetEnergyDistributionNameInFile() const { return fEnergyDistributionNameInFile; } + + inline TString GetAngularDistributionType() const { return fAngularDistributionType; } + inline TString GetAngularDistributionFilename() const { return fAngularDistributionFilename; } + inline TString GetAngularDistributionNameInFile() const { return fAngularDistributionNameInFile; } + inline TString GetGenFilename() const { return fGenFilename; } inline std::vector GetParticles() const { return fParticles; } - inline void SetAngularDistType(const TString& type) { fAngularDistributionType = type; } - inline void SetEnergyDistType(const TString& type) { fEnergyDistributionType = type; } - inline void SetEnergyRange(const TVector2& range) { fEnergyDistributionRange = range; } - inline void SetSpectrumFilename(const TString& spectrumFilename) { - fEnergyDistributionFilename = spectrumFilename; + inline void SetAngularDistributionType(const TString& type) { fAngularDistributionType = type; } + inline void SetAngularDistributionFilename(const TString& filename) { + fAngularDistributionFilename = filename; + } + inline void SetAngularDistributionNameInFile(const TString& name) { + fAngularDistributionNameInFile = name; + } + + inline void SetEnergyDistributionType(const TString& type) { fEnergyDistributionType = type; } + inline void SetEnergyDistributionRange(const TVector2& range) { fEnergyDistributionRange = range; } + inline void SetEnergyDistributionFilename(const TString& filename) { + fEnergyDistributionFilename = filename; } - inline void SetSpectrumName(const TString& spectrumName) { fEnergyDistributionNameInFile = spectrumName; } - inline void SetAngularFilename(const TString& angFilename) { fAngularDistributionFilename = angFilename; } - inline void SetAngularName(const TString& angularName) { fAngularDistributionNameInFile = angularName; } + inline void SetEnergyDistributionNameInFile(const TString& name) { fEnergyDistributionNameInFile = name; } + inline void SetGenFilename(const TString& name) { fGenFilename = name; } - inline void setRandomMethod(double (*method)()) { fRandomMethod = method; } + + inline void SetRandomMethod(double (*method)()) { fRandomMethod = method; } inline void AddParticle(const TRestGeant4Particle& particle) { fParticles.push_back(particle); } diff --git a/inc/TRestGeant4PrimaryGeneratorInfo.h b/inc/TRestGeant4PrimaryGeneratorInfo.h index 4f9f7d1..06e24ff 100644 --- a/inc/TRestGeant4PrimaryGeneratorInfo.h +++ b/inc/TRestGeant4PrimaryGeneratorInfo.h @@ -66,6 +66,7 @@ AngularDistributionTypes StringToAngularDistributionTypes(const std::string&); enum class AngularDistributionFormulas { COS2, + COS3, }; std::string AngularDistributionFormulasToString(const AngularDistributionFormulas&); diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 030a720..39daf56 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -968,29 +968,29 @@ void TRestGeant4Metadata::ReadParticleSource(TRestGeant4ParticleSource* source, // Angular distribution parameters TiXmlElement* angularDefinition = GetElement("angularDist", sourceDefinition); - source->SetAngularDistType(GetParameter("type", angularDefinition, "flux")); - if (source->GetAngularDistType() == "TH1D") { - source->SetAngularFilename(SearchFile(GetParameter("file", angularDefinition))); - source->SetAngularName(GetParameter("spctName", angularDefinition)); + source->SetAngularDistributionType(GetParameter("type", angularDefinition, "flux")); + if (source->GetAngularDistributionType() == "TH1D") { + source->SetAngularDistributionFilename(SearchFile(GetParameter("file", angularDefinition))); + source->SetAngularDistributionNameInFile(GetParameter("spctName", angularDefinition)); } - if (GetNumberOfSources() == 0 && source->GetAngularDistType() == "backtoback") { + if (GetNumberOfSources() == 0 && source->GetAngularDistributionType() == "backtoback") { cout << "WARNING: First source cannot be backtoback. Setting it to isotropic" << endl; - source->SetAngularDistType("isotropic"); + source->SetAngularDistributionType("isotropic"); } source->SetDirection(StringTo3DVector(GetParameter("direction", angularDefinition, "(1,0,0)"))); // Energy distribution parameters TiXmlElement* energyDefinition = GetElement("energyDist", sourceDefinition); - source->SetEnergyDistType(GetParameter("type", energyDefinition, "mono")); - if (source->GetEnergyDistType() == "TH1D") { - source->SetSpectrumFilename(SearchFile(GetParameter("file", energyDefinition))); - source->SetSpectrumName(GetParameter("spctName", energyDefinition)); + source->SetEnergyDistributionType(GetParameter("type", energyDefinition, "mono")); + if (source->GetEnergyDistributionType() == "TH1D") { + source->SetEnergyDistributionFilename(SearchFile(GetParameter("file", energyDefinition))); + source->SetEnergyDistributionNameInFile(GetParameter("spctName", energyDefinition)); } - source->SetEnergyRange(Get2DVectorParameterWithUnits("range", energyDefinition)); - if (source->GetEnergyDistType() == "mono") { + source->SetEnergyDistributionRange(Get2DVectorParameterWithUnits("range", energyDefinition)); + if (source->GetEnergyDistributionType() == "mono") { Double_t en; en = GetDblParameterWithUnits("energy", energyDefinition, 0); - source->SetEnergyRange(TVector2(en, en)); + source->SetEnergyDistributionRange(TVector2(en, en)); source->SetEnergy(en); } @@ -1190,8 +1190,8 @@ void TRestGeant4Metadata::PrintMetadata() { // // TRestGeant4ParticleSource* src = TRestGeant4ParticleSource::instantiate(); // src->SetGenFilename(fileName); -// // src->SetAngularDistType("flux"); -// // src->SetEnergyDistType("mono"); +// // src->SetAngularDistributionType("flux"); +// // src->SetEnergyDistributionType("mono"); // // TRestGeant4Particle particle; // @@ -1303,8 +1303,8 @@ void TRestGeant4Metadata::PrintMetadata() { // // cout << "i : " << tmpInt << " fN : " << fGeneratorEvents << endl; // TRestGeant4ParticleSource* src = TRestGeant4ParticleSource::instantiate(); // src->SetGenFilename(fileName); -// // src->SetAngularDistType("flux"); -// // src->SetEnergyDistType("mono"); +// // src->SetAngularDistributionType("flux"); +// // src->SetEnergyDistributionType("mono"); // // TRestGeant4Particle particle; // string type = (string)GetGeneratorType(); @@ -1406,16 +1406,16 @@ void TRestGeant4Metadata::PrintMetadata() { // // Angular distribution parameters // TiXmlElement* angularDefinition = GetElement("angularDist", sourceDefinition); // -// source->SetAngularDistType(GetFieldValue("type", angularDefinition)); +// source->SetAngularDistributionType(GetFieldValue("type", angularDefinition)); // -// if (source->GetAngularDistType() == "TH1D") { -// source->SetAngularFilename(SearchFile(GetFieldValue("file", angularDefinition))); -// source->SetAngularName(GetFieldValue("spctName", angularDefinition)); +// if (source->GetAngularDistributionType() == "TH1D") { +// source->SetAngularDistributionFilename(SearchFile(GetFieldValue("file", angularDefinition))); +// source->SetAngularDistributionNameInFile(GetFieldValue("spctName", angularDefinition)); // } // -// if (GetNumberOfSources() == 0 && source->GetAngularDistType() == "backtoback") { +// if (GetNumberOfSources() == 0 && source->GetAngularDistributionType() == "backtoback") { // cout << "WARNING: First source cannot be backtoback. Setting it to isotropic" << endl; -// source->SetAngularDistType("isotropic"); +// source->SetAngularDistributionType("isotropic"); // } // // source->SetDirection(StringTo3DVector(GetFieldValue("direction", angularDefinition))); @@ -1423,19 +1423,19 @@ void TRestGeant4Metadata::PrintMetadata() { // // Energy distribution parameters // TiXmlElement* energyDefinition = GetElement("energyDist", sourceDefinition); // -// source->SetEnergyDistType(GetFieldValue("type", energyDefinition)); +// source->SetEnergyDistributionType(GetFieldValue("type", energyDefinition)); // -// if (source->GetEnergyDistType() == "TH1D") { -// source->SetSpectrumFilename(SearchFile(GetFieldValue("file", energyDefinition))); -// source->SetSpectrumName(GetFieldValue("spctName", energyDefinition)); +// if (source->GetEnergyDistributionType() == "TH1D") { +// source->SetEnergyDistributionFilename(SearchFile(GetFieldValue("file", energyDefinition))); +// source->SetEnergyDistributionNameInFile(GetFieldValue("spctName", energyDefinition)); // } // -// source->SetEnergyRange(Get2DVectorParameterWithUnits("range", energyDefinition)); +// source->SetEnergyDistributionRange(Get2DVectorParameterWithUnits("range", energyDefinition)); // -// if (source->GetEnergyDistType() == "mono") { +// if (source->GetEnergyDistributionType() == "mono") { // Double_t en; // en = GetDblParameterWithUnits("energy", energyDefinition); -// source->SetEnergyRange(TVector2(en, en)); +// source->SetEnergyDistributionRange(TVector2(en, en)); // source->SetEnergy(en); // } // diff --git a/src/TRestGeant4ParticleSource.cxx b/src/TRestGeant4ParticleSource.cxx index 8c4da89..67a0184 100644 --- a/src/TRestGeant4ParticleSource.cxx +++ b/src/TRestGeant4ParticleSource.cxx @@ -17,32 +17,20 @@ #include "TRestGeant4ParticleSource.h" +#include +#include +#include + #include "TRestGeant4Metadata.h" -#include "TRestReflector.h" -#include "TRestStringHelper.h" -#include "TRestStringOutput.h" using namespace std; - -// REST_Verbose_Level fLevel = REST_Essential; //! -//// TRestLeveledOutput(REST_Verbose_Level& vref, string _color = -//// COLOR_RESET, string BorderOrHeader = "", REST_Display_Format style = -//// kBorderedLeft) -// TRestLeveledOutput metadata = -// TRestLeveledOutput(fLevel, COLOR_BOLDGREEN, "||", -// (REST_Display_Format)kBorderedMiddle); //! +using namespace TRestGeant4PrimaryGeneratorTypes; ClassImp(TRestGeant4ParticleSource); -TRestGeant4ParticleSource::TRestGeant4ParticleSource() { - // TRestGeant4ParticleSource default constructor - fAngularDistributionType = "flux"; - fEnergyDistributionType = "mono"; -} +TRestGeant4ParticleSource::TRestGeant4ParticleSource() = default; -TRestGeant4ParticleSource::~TRestGeant4ParticleSource() { - // TRestGeant4ParticleSource destructor -} +TRestGeant4ParticleSource::~TRestGeant4ParticleSource() = default; void TRestGeant4ParticleSource::PrintParticleSource() { RESTMetadata << "---------------------------------------" << RESTendl; @@ -55,24 +43,24 @@ void TRestGeant4ParticleSource::PrintParticleSource() { RESTMetadata << RESTendl; } else { RESTMetadata << "Charge : " << GetParticleCharge() << RESTendl; - RESTMetadata << "Angular distribution type : " << GetAngularDistType() << RESTendl; - if (GetAngularDistType() == "TH1D") { + RESTMetadata << "Angular distribution type : " << GetAngularDistributionType() << RESTendl; + if (GetAngularDistributionType() == "TH1D") { RESTMetadata << "Angular distribution filename : " - << TRestTools::GetPureFileName((string)GetAngularFilename()) << RESTendl; - RESTMetadata << "Angular histogram name : " << GetAngularName() << RESTendl; + << TRestTools::GetPureFileName((string)GetAngularDistributionFilename()) << RESTendl; + RESTMetadata << "Angular histogram name : " << GetAngularDistributionNameInFile() << RESTendl; } RESTMetadata << "Direction : (" << GetDirection().X() << "," << GetDirection().Y() << "," << GetDirection().Z() << ")" << RESTendl; - RESTMetadata << "Energy distribution : " << GetEnergyDistType() << RESTendl; - if (GetEnergyDistType() == "TH1D") { + RESTMetadata << "Energy distribution : " << GetEnergyDistributionType() << RESTendl; + if (GetEnergyDistributionType() == "TH1D") { RESTMetadata << "Energy distribution filename : " - << TRestTools::GetPureFileName((string)GetSpectrumFilename()) << RESTendl; - RESTMetadata << "Energy histogram name : " << GetSpectrumName() << RESTendl; - } else if (GetEnergyRange().X() == GetEnergyRange().Y()) + << TRestTools::GetPureFileName((string)GetEnergyDistributionFilename()) << RESTendl; + RESTMetadata << "Energy histogram name : " << GetEnergyDistributionNameInFile() << RESTendl; + } else if (GetEnergyDistributionRange().X() == GetEnergyDistributionRange().Y()) RESTMetadata << "Energy : " << GetEnergy() << " keV" << RESTendl; else - RESTMetadata << "Energy range : (" << GetEnergyRange().X() << "," << GetEnergyRange().Y() - << ") keV" << RESTendl; + RESTMetadata << "Energy range : (" << GetEnergyDistributionRange().X() << "," + << GetEnergyDistributionRange().Y() << ") keV" << RESTendl; } } @@ -181,8 +169,8 @@ bool TRestGeant4ParticleSource::ReadNewDecay0File(TString fileName) { // TRestGeant4ParticleSource* src = TRestGeant4ParticleSource::instantiate(); // this->SetGenFilename(fileName); - // this->SetAngularDistType("flux"); - // this->SetEnergyDistType("mono"); + // this->SetAngularDistributionType("flux"); + // this->SetEnergyDistributionType("mono"); TRestGeant4Particle particle; @@ -290,8 +278,8 @@ bool TRestGeant4ParticleSource::ReadOldDecay0File(TString fileName) { // cout << "i : " << tmpInt << " fN : " << fGeneratorEvents << std::endl; // TRestGeant4ParticleSource* src = TRestGeant4ParticleSource::instantiate(); // this->SetGenFilename(fileName); - // this->SetAngularDistType("flux"); - // this->SetEnergyDistType("mono"); + // this->SetAngularDistributionType("flux"); + // this->SetEnergyDistributionType("mono"); TRestGeant4Particle particle; diff --git a/src/TRestGeant4PrimaryGeneratorInfo.cxx b/src/TRestGeant4PrimaryGeneratorInfo.cxx index c5da40c..5d0c47b 100644 --- a/src/TRestGeant4PrimaryGeneratorInfo.cxx +++ b/src/TRestGeant4PrimaryGeneratorInfo.cxx @@ -256,6 +256,8 @@ string TRestGeant4PrimaryGeneratorTypes::AngularDistributionFormulasToString( switch (type) { case AngularDistributionFormulas::COS2: return "Cos2"; + case AngularDistributionFormulas::COS3: + return "Cos3"; } cout << "TRestGeant4PrimaryGeneratorTypes::AngularDistributionFormulasToString - Error - Unknown angular " "distribution formula" @@ -268,6 +270,9 @@ AngularDistributionFormulas TRestGeant4PrimaryGeneratorTypes::StringToAngularDis if (TString(type).EqualTo(AngularDistributionFormulasToString(AngularDistributionFormulas::COS2), TString::ECaseCompare::kIgnoreCase)) { return AngularDistributionFormulas::COS2; + } else if (TString(type).EqualTo(AngularDistributionFormulasToString(AngularDistributionFormulas::COS3), + TString::ECaseCompare::kIgnoreCase)) { + return AngularDistributionFormulas::COS3; } else { cout << "TRestGeant4PrimaryGeneratorTypes::StringToAngularDistributionFormulas - Error - Unknown " "AngularDistributionFormulas: " @@ -286,7 +291,22 @@ TF1 TRestGeant4PrimaryGeneratorTypes::AngularDistributionFormulasToRootFormula( } return 0.0; }; - return TF1("AngularDistribution: Cos2", cos2, 0.0, TMath::Pi()); + const char* title = "AngularDistribution: Cos2"; + auto f = TF1(title, cos2, 0.0, TMath::Pi()); + f.SetTitle(title); + return f; + } + case AngularDistributionFormulas::COS3: { + auto cos3 = [](double* xs, double* ps) { + if (xs[0] >= 0 && xs[0] <= TMath::Pi() / 2) { + return TMath::Power(TMath::Cos(xs[0]), 3); + } + return 0.0; + }; + const char* title = "AngularDistribution: Cos3"; + auto f = TF1(title, cos3, 0.0, TMath::Pi()); + f.SetTitle(title); + return f; } } cout << "TRestGeant4PrimaryGeneratorTypes::AngularDistributionFormulasToRootFormula - Error - Unknown " diff --git a/test/src/TRestGeant4Metadata.cxx b/test/src/TRestGeant4Metadata.cxx index 1bf09df..bb45723 100644 --- a/test/src/TRestGeant4Metadata.cxx +++ b/test/src/TRestGeant4Metadata.cxx @@ -46,5 +46,5 @@ TEST(TRestGeant4Metadata, FromRml) { EXPECT_TRUE(restGeant4Metadata.GetNumberOfSources() == 1); const auto particleSource = restGeant4Metadata.GetParticleSource(0); EXPECT_TRUE(particleSource->GetParticleName() == "geantino"); - EXPECT_TRUE(particleSource->GetEnergyDistType() == "mono"); + EXPECT_TRUE(particleSource->GetEnergyDistributionType() == "mono"); } From 91b093737db180b6e4d39893695925d35854d216 Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 19 Jul 2022 12:56:53 +0200 Subject: [PATCH 037/100] TRestGeant4ParticleSource - updated getters / setters --- inc/TRestGeant4ParticleSource.h | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/inc/TRestGeant4ParticleSource.h b/inc/TRestGeant4ParticleSource.h index e847072..d0a9a0f 100644 --- a/inc/TRestGeant4ParticleSource.h +++ b/inc/TRestGeant4ParticleSource.h @@ -17,6 +17,7 @@ #ifndef RestCore_TRestGeant4ParticleSource #define RestCore_TRestGeant4ParticleSource +#include #include #include #include @@ -66,29 +67,47 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada inline Double_t GetEnergyDistributionRangeMax() const { return fEnergyDistributionRange.Y(); } inline TString GetEnergyDistributionFilename() const { return fEnergyDistributionFilename; } inline TString GetEnergyDistributionNameInFile() const { return fEnergyDistributionNameInFile; } + inline TString GetEnergyDistributionFormulaString() const { return fEnergyDistributionFormulaString; } inline TString GetAngularDistributionType() const { return fAngularDistributionType; } inline TString GetAngularDistributionFilename() const { return fAngularDistributionFilename; } inline TString GetAngularDistributionNameInFile() const { return fAngularDistributionNameInFile; } + inline TString GetAngularDistributionFormulaString() const { return fAngularDistributionFormulaString; } inline TString GetGenFilename() const { return fGenFilename; } inline std::vector GetParticles() const { return fParticles; } - inline void SetAngularDistributionType(const TString& type) { fAngularDistributionType = type; } + inline void SetAngularDistributionType(const TString& type) { + fAngularDistributionType = TRestGeant4PrimaryGeneratorTypes::AngularDistributionTypesToString( + TRestGeant4PrimaryGeneratorTypes::StringToAngularDistributionTypes(type.Data())); + } inline void SetAngularDistributionFilename(const TString& filename) { fAngularDistributionFilename = filename; } inline void SetAngularDistributionNameInFile(const TString& name) { fAngularDistributionNameInFile = name; } + inline void SetAngularDistributionFormulaString(const TString& formula) { + fAngularDistributionFormulaString = + TRestGeant4PrimaryGeneratorTypes::AngularDistributionFormulasToString( + TRestGeant4PrimaryGeneratorTypes::StringToAngularDistributionFormulas(formula.Data())); + } - inline void SetEnergyDistributionType(const TString& type) { fEnergyDistributionType = type; } + inline void SetEnergyDistributionType(const TString& type) { + fEnergyDistributionType = TRestGeant4PrimaryGeneratorTypes::EnergyDistributionTypesToString( + TRestGeant4PrimaryGeneratorTypes::StringToEnergyDistributionTypes(type.Data())); + } inline void SetEnergyDistributionRange(const TVector2& range) { fEnergyDistributionRange = range; } inline void SetEnergyDistributionFilename(const TString& filename) { fEnergyDistributionFilename = filename; } inline void SetEnergyDistributionNameInFile(const TString& name) { fEnergyDistributionNameInFile = name; } + inline void SetEnergyDistributionFormulaString(const TString& formula) { + fEnergyDistributionFormulaString = + TRestGeant4PrimaryGeneratorTypes::EnergyDistributionFormulasToString( + TRestGeant4PrimaryGeneratorTypes::StringToEnergyDistributionFormulas(formula.Data())); + } inline void SetGenFilename(const TString& name) { fGenFilename = name; } From 84981bc3b67a294d06b8c714950832ba74e02496 Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 19 Jul 2022 13:03:37 +0200 Subject: [PATCH 038/100] TRestGeant4Metadata - protected initialization of generators --- src/TRestGeant4Metadata.cxx | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 39daf56..04bbd58 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -969,11 +969,18 @@ void TRestGeant4Metadata::ReadParticleSource(TRestGeant4ParticleSource* source, // Angular distribution parameters TiXmlElement* angularDefinition = GetElement("angularDist", sourceDefinition); source->SetAngularDistributionType(GetParameter("type", angularDefinition, "flux")); - if (source->GetAngularDistributionType() == "TH1D") { + if (StringToAngularDistributionTypes(source->GetAngularDistributionType().Data()) == + AngularDistributionTypes::TH1D) { source->SetAngularDistributionFilename(SearchFile(GetParameter("file", angularDefinition))); source->SetAngularDistributionNameInFile(GetParameter("spctName", angularDefinition)); } - if (GetNumberOfSources() == 0 && source->GetAngularDistributionType() == "backtoback") { + if (StringToAngularDistributionTypes(source->GetAngularDistributionType().Data()) == + AngularDistributionTypes::FORMULA) { + source->SetAngularDistributionFormulaString(GetParameter("formula", angularDefinition)); + } + if (GetNumberOfSources() == 0 && + StringToAngularDistributionTypes(source->GetAngularDistributionType().Data()) == + AngularDistributionTypes::BACK_TO_BACK) { cout << "WARNING: First source cannot be backtoback. Setting it to isotropic" << endl; source->SetAngularDistributionType("isotropic"); } @@ -982,18 +989,23 @@ void TRestGeant4Metadata::ReadParticleSource(TRestGeant4ParticleSource* source, // Energy distribution parameters TiXmlElement* energyDefinition = GetElement("energyDist", sourceDefinition); source->SetEnergyDistributionType(GetParameter("type", energyDefinition, "mono")); - if (source->GetEnergyDistributionType() == "TH1D") { + if (StringToEnergyDistributionTypes(source->GetEnergyDistributionType().Data()) == + EnergyDistributionTypes::TH1D) { source->SetEnergyDistributionFilename(SearchFile(GetParameter("file", energyDefinition))); source->SetEnergyDistributionNameInFile(GetParameter("spctName", energyDefinition)); } source->SetEnergyDistributionRange(Get2DVectorParameterWithUnits("range", energyDefinition)); - if (source->GetEnergyDistributionType() == "mono") { - Double_t en; - en = GetDblParameterWithUnits("energy", energyDefinition, 0); - source->SetEnergyDistributionRange(TVector2(en, en)); - source->SetEnergy(en); + if (StringToEnergyDistributionTypes(source->GetEnergyDistributionType().Data()) == + EnergyDistributionTypes::MONO) { + Double_t energy; + energy = GetDblParameterWithUnits("energy", energyDefinition, 0); + source->SetEnergyDistributionRange({energy, energy}); + source->SetEnergy(energy); + } + if (StringToEnergyDistributionTypes(source->GetEnergyDistributionType().Data()) == + EnergyDistributionTypes::FORMULA) { + source->SetEnergyDistributionFormulaString(GetParameter("formula", energyDefinition)); } - // allow custom configuration from the class source->LoadConfigFromElement(sourceDefinition, fElementGlobal, fVariables); // AddParticleSource(source); From 20049e211f3dbefa17a596b2cbdcdc6e893275fe Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 19 Jul 2022 13:07:05 +0200 Subject: [PATCH 039/100] TRestGeant4Metadata - small refactor --- src/TRestGeant4Metadata.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 04bbd58..f2f3a11 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1012,8 +1012,8 @@ void TRestGeant4Metadata::ReadParticleSource(TRestGeant4ParticleSource* source, } void TRestGeant4Metadata::RemoveParticleSources() { - for (auto c : fParticleSource) { - delete c; + for (auto source : fParticleSource) { + delete source; } fParticleSource.clear(); } From 7fb2c7e225a25bec378db7197a943dcc93da00af Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 19 Jul 2022 13:50:59 +0200 Subject: [PATCH 040/100] Using random cos2 angle from formula instead of th1d --- inc/TRestGeant4Metadata.h | 4 +- inc/TRestGeant4Particle.h | 2 +- inc/TRestGeant4ParticleSource.h | 9 +- src/TRestGeant4Metadata.cxx | 314 +------------------------------- 4 files changed, 13 insertions(+), 316 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 7c47cec..fc8db60 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -125,8 +125,8 @@ class TRestGeant4Metadata : public TRestMetadata { /// The number of events simulated, or to be simulated. Int_t fNEvents = 0; - /// \brief The seed value used for Geant4 random event generator. If it is zero - /// its value will be assigned using the system timestamp. + /// \brief The seed value used for Geant4 random event generator. + /// If it is zero its value will be assigned using the system timestamp. Long_t fSeed = 0; /// \brief If this parameter is set to 'true' it will save all events even if they leave no energy in the diff --git a/inc/TRestGeant4Particle.h b/inc/TRestGeant4Particle.h index 2734d3d..96d98da 100644 --- a/inc/TRestGeant4Particle.h +++ b/inc/TRestGeant4Particle.h @@ -27,7 +27,7 @@ class TRestGeant4Particle : public TObject { protected: TString fParticleName; Double_t fExcitationLevel = 0; - TVector3 fDirection; + TVector3 fDirection = {1, 0, 0}; Double_t fEnergy; Int_t fCharge = 0; TVector3 fOrigin; diff --git a/inc/TRestGeant4ParticleSource.h b/inc/TRestGeant4ParticleSource.h index d0a9a0f..74602e0 100644 --- a/inc/TRestGeant4ParticleSource.h +++ b/inc/TRestGeant4ParticleSource.h @@ -59,7 +59,14 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada static TRestGeant4ParticleSource* instantiate(std::string model = ""); inline TString GetParticleName() const { return fParticleName; } - inline TVector3 GetDirection() const { return fDirection; } + inline TVector3 GetDirection() const { + if (fDirection.Mag() <= 0) { + std::cout << "TRestGeant4ParticleSource::GetDirection: " + << "Direction cannot be the zero vector" << std::endl; + exit(1); + } + return fDirection; + } inline TString GetEnergyDistributionType() const { return fEnergyDistributionType; } inline TVector2 GetEnergyDistributionRange() const { return fEnergyDistributionRange; } diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index f2f3a11..37c92e9 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -976,7 +976,7 @@ void TRestGeant4Metadata::ReadParticleSource(TRestGeant4ParticleSource* source, } if (StringToAngularDistributionTypes(source->GetAngularDistributionType().Data()) == AngularDistributionTypes::FORMULA) { - source->SetAngularDistributionFormulaString(GetParameter("formula", angularDefinition)); + source->SetAngularDistributionFormulaString(GetParameter("name", angularDefinition)); } if (GetNumberOfSources() == 0 && StringToAngularDistributionTypes(source->GetAngularDistributionType().Data()) == @@ -1004,7 +1004,7 @@ void TRestGeant4Metadata::ReadParticleSource(TRestGeant4ParticleSource* source, } if (StringToEnergyDistributionTypes(source->GetEnergyDistributionType().Data()) == EnergyDistributionTypes::FORMULA) { - source->SetEnergyDistributionFormulaString(GetParameter("formula", energyDefinition)); + source->SetEnergyDistributionFormulaString(GetParameter("name", energyDefinition)); } // allow custom configuration from the class source->LoadConfigFromElement(sourceDefinition, fElementGlobal, fVariables); @@ -1144,316 +1144,6 @@ void TRestGeant4Metadata::PrintMetadata() { RESTMetadata << "+++++" << RESTendl; } -///////////////////////////////////////////////// -///// \brief Reads an input file produced by Decay0. -///// -///// The input file should contain the description of several -///// pre-generated events, providing the names (or ids) of -///// particles to be produced, their energy, and momentum. -///// The particles and their properties are stored in a -///// TRestG4ParticleCollection which will be randomly accessed -///// by the restG4 package. -///// -///// \param fName The Decay0 filename located at -///// REST_PATH/data/generator/ -///// -// void TRestGeant4Metadata::ReadEventDataFile(TString fName) { -// string fullPathName = SearchFile((string)fName); -// if (fullPathName == "") { -// ferr << "File not found : " << fName << endl; -// ferr << "Decay0 generator file could not be found!!" << endl; -// exit(1); -// } -// -// debug << "TRestGeant4Metadata::ReadGeneratorFile" << endl; -// debug << "Full path generator file : " << fullPathName << endl; -// -// if (!ReadOldDecay0File(fullPathName)) ReadNewDecay0File(fullPathName); -// } -// -///////////////////////////////////////////////// -///// \brief Reads particle information using the file format from newer Decay0 versions. -///// -///// This is an auxiliar method used in TRestGeant4Metadata::ReadEventDataFile that will read the Decay0 -/// files -///// produced with the newer Decay0 versions. -///// -// Int_t TRestGeant4Metadata::ReadNewDecay0File(TString fileName) { -// ifstream infile; -// infile.open(fileName); -// if (!infile.is_open()) { -// printf("Error when opening file %s\n", fileName.Data()); -// return 0; -// } -// -// int generatorEvents = 0; -// string s; -// // First lines are discarded. -// for (int i = 0; i < 24; i++) { -// getline(infile, s); -// int pos = s.find("@nevents="); -// if (pos != -1) generatorEvents = StringToInteger(s.substr(10, s.length() - 10)); -// } -// -// if (generatorEvents == 0) { -// ferr << "TRestGeant4Metadata::ReadNewDecay0File. Problem reading generator file" << endl; -// exit(1); -// } -// -// TRestGeant4ParticleSource* src = TRestGeant4ParticleSource::instantiate(); -// src->SetGenFilename(fileName); -// // src->SetAngularDistributionType("flux"); -// // src->SetEnergyDistributionType("mono"); -// -// TRestGeant4Particle particle; -// -// debug << "Reading generator file : " << fileName << endl; -// debug << "Total number of events : " << generatorEvents << endl; -// for (int n = 0; n < generatorEvents && !infile.eof(); n++) { -// int pos = -1; -// while (!infile.eof() && pos == -1) { -// getline(infile, s); -// pos = s.find("@event_start"); -// } -// -// // Time - nuclide is skipped -// getline(infile, s); -// -// Int_t nParticles; -// infile >> nParticles; -// debug << "Number of particles : " << nParticles << endl; -// -// // cout << evID <<" "<< time <<" "<< nParticles <<" "<< endl; -// for (int i = 0; i < nParticles && !infile.eof(); i++) { -// Int_t pID; -// Double_t momx, momy, momz, mass; -// Double_t energy = -1, momentum2; -// TString pName; -// -// Double_t time; -// infile >> pID >> time >> momx >> momy >> momz >> pName; -// -// debug << " ---- New particle found --- " << endl; -// debug << " Particle name : " << pName << endl; -// debug << " - pId : " << pID << endl; -// debug << " - Relative time : " << time << endl; -// debug << " - px: " << momx << " py: " << momy << " pz: " << momz << " " << endl; -// -// if (pID == 3) { -// momentum2 = (momx * momx) + (momy * momy) + (momz * momz); -// mass = 0.511; -// -// energy = TMath::Sqrt(momentum2 + mass * mass) - mass; -// particle.SetParticleName("e-"); -// particle.SetParticleCharge(-1); -// particle.SetExcitationLevel(0); -// -// } else if (pID == 1) { -// momentum2 = (momx * momx) + (momy * momy) + (momz * momz); -// -// energy = TMath::Sqrt(momentum2); -// particle.SetParticleName("gamma"); -// particle.SetParticleCharge(0); -// particle.SetExcitationLevel(0); -// } else { -// cout << "Particle id " << pID << " not recognized" << endl; -// } -// -// TVector3 momDirection(momx, momy, momz); -// momDirection = momDirection.Unit(); -// -// particle.SetEnergy(1000. * energy); -// particle.SetDirection(momDirection); -// -// src->AddParticle(particle); -// } -// src->FlushParticlesTemplate(); -// } -// -// AddParticleSource(src); -// -// return 1; -// } -// -///////////////////////////////////////////////// -///// \brief Reads particle information using the file format from older Decay0 versions. -///// -///// This is an auxiliar method used in TRestGeant4Metadata::ReadEventDataFile that will read the Decay0 -/// files -///// produced with the newer Decay0 versions. -///// -// Int_t TRestGeant4Metadata::ReadOldDecay0File(TString fileName) { -// ifstream infile; -// infile.open(fileName); -// if (!infile.is_open()) { -// printf("Error when opening file %s\n", fileName.Data()); -// return 0; -// } -// -// string s; -// // First lines are discarded. -// int headerFound = 0; -// for (int i = 0; i < 30; i++) { -// getline(infile, s); -// if (s.find("#!bxdecay0 1.0.0") != -1) return 0; -// if (s.find("First event and full number of events:") != -1) { -// headerFound = 1; -// break; -// } -// } -// if (!headerFound) { -// ferr -// << "TRestGeant4Metadata::ReadOldDecay0File. Problem reading generator file: no \"First event and -// " -// "full number of events:\" header.\n"; -// abort(); -// } -// int tmpInt; -// int fGeneratorEvents; -// infile >> tmpInt >> fGeneratorEvents; -// -// // cout << "i : " << tmpInt << " fN : " << fGeneratorEvents << endl; -// TRestGeant4ParticleSource* src = TRestGeant4ParticleSource::instantiate(); -// src->SetGenFilename(fileName); -// // src->SetAngularDistributionType("flux"); -// // src->SetEnergyDistributionType("mono"); -// -// TRestGeant4Particle particle; -// string type = (string)GetGeneratorType(); -// -// cout << "Reading generator file : " << fileName << endl; -// cout << "Total number of events : " << fGeneratorEvents << endl; -// for (int n = 0; n < fGeneratorEvents && !infile.eof(); n++) { -// Int_t nParticles; -// Int_t evID; -// Double_t time; -// -// infile >> evID >> time >> nParticles; -// -// // cout << evID <<" "<< time <<" "<< nParticles <<" "<< endl; -// for (int i = 0; i < nParticles && !infile.eof(); i++) { -// Int_t pID; -// Double_t momx, momy, momz, mass; -// Double_t energy = -1, momentum2; -// Double_t x, y, z; -// -// infile >> pID >> momx >> momy >> momz >> time; -// if (type == "file") infile >> x >> y >> z; -// -// // cout << momx << " " << momy << " " << momz << " " << endl; -// -// bool ise = 2 <= pID && pID <= 3, ismu = 5 <= pID && pID <= 6, isp = pID == 14, isg = pID == 1; -// if (ise || ismu || isp || isg) { -// momentum2 = (momx * momx) + (momy * momy) + (momz * momz); -// if (ise) { -// mass = 0.511; -// particle.SetParticleName(pID == 2 ? "e+" : "e-"); -// particle.SetParticleCharge(pID == 2 ? 1 : -1); -// } else if (ismu) { -// mass = 105.7; -// particle.SetParticleName(pID == 5 ? "mu+" : "mu-"); -// particle.SetParticleCharge(pID == 5 ? 1 : -1); -// } else if (isp) { -// mass = 938.3; -// particle.SetParticleName("proton"); -// particle.SetParticleCharge(1); -// } else { -// mass = 0; -// particle.SetParticleName("gamma"); -// particle.SetParticleCharge(0); -// } -// -// energy = TMath::Sqrt(momentum2 + mass * mass) - mass; -// particle.SetExcitationLevel(0); -// } else { -// cout << "Particle id " << pID << " not recognized" << endl; -// } -// -// TVector3 momDirection(momx, momy, momz); -// momDirection = momDirection.Unit(); -// -// particle.SetEnergy(1000. * energy); -// particle.SetDirection(momDirection); -// particle.SetOrigin(TVector3(x, y, z)); -// -// src->AddParticle(particle); -// } -// -// src->FlushParticlesTemplate(); -// } -// -// AddParticleSource(src); -// -// return 1; -// } -// -///////////////////////////////////////////////// -///// \brief It reads the SetParticleName(GetFieldValue("particle", sourceDefinition)); -// -// source->SetExcitationLevel(StringToDouble(GetFieldValue("excitedLevel", sourceDefinition))); -// -// Int_t charge = 0; -// if (GetFieldValue("charge", sourceDefinition) == "Not defined") -// charge = 0; -// else -// charge = StringToInteger(GetFieldValue("charge", sourceDefinition)); -// -// source->SetParticleCharge(charge); -// -// TString fullChain = GetFieldValue("fullChain", sourceDefinition); -// -// if (fullChain == "on" || fullChain == "ON" || fullChain == "On" || fullChain == "oN") { -// SetFullChain(true); -// } else { -// SetFullChain(false); -// } -// -// // Angular distribution parameters -// TiXmlElement* angularDefinition = GetElement("angularDist", sourceDefinition); -// -// source->SetAngularDistributionType(GetFieldValue("type", angularDefinition)); -// -// if (source->GetAngularDistributionType() == "TH1D") { -// source->SetAngularDistributionFilename(SearchFile(GetFieldValue("file", angularDefinition))); -// source->SetAngularDistributionNameInFile(GetFieldValue("spctName", angularDefinition)); -// } -// -// if (GetNumberOfSources() == 0 && source->GetAngularDistributionType() == "backtoback") { -// cout << "WARNING: First source cannot be backtoback. Setting it to isotropic" << endl; -// source->SetAngularDistributionType("isotropic"); -// } -// -// source->SetDirection(StringTo3DVector(GetFieldValue("direction", angularDefinition))); -// -// // Energy distribution parameters -// TiXmlElement* energyDefinition = GetElement("energyDist", sourceDefinition); -// -// source->SetEnergyDistributionType(GetFieldValue("type", energyDefinition)); -// -// if (source->GetEnergyDistributionType() == "TH1D") { -// source->SetEnergyDistributionFilename(SearchFile(GetFieldValue("file", energyDefinition))); -// source->SetEnergyDistributionNameInFile(GetFieldValue("spctName", energyDefinition)); -// } -// -// source->SetEnergyDistributionRange(Get2DVectorParameterWithUnits("range", energyDefinition)); -// -// if (source->GetEnergyDistributionType() == "mono") { -// Double_t en; -// en = GetDblParameterWithUnits("energy", energyDefinition); -// source->SetEnergyDistributionRange(TVector2(en, en)); -// source->SetEnergy(en); -// } -// -// AddParticleSource(source); -// } - /////////////////////////////////////////////// /// \brief Returns the id of an active volume giving as parameter its name. Int_t TRestGeant4Metadata::GetActiveVolumeID(TString name) { From b130926efdb8777181944c4d7d5253ef42e702a9 Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 19 Jul 2022 14:12:41 +0200 Subject: [PATCH 041/100] PrimaryGeneratorAction - implemented generator energy from formula --- inc/TRestGeant4ParticleSource.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/inc/TRestGeant4ParticleSource.h b/inc/TRestGeant4ParticleSource.h index 74602e0..d735e0f 100644 --- a/inc/TRestGeant4ParticleSource.h +++ b/inc/TRestGeant4ParticleSource.h @@ -105,7 +105,18 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada fEnergyDistributionType = TRestGeant4PrimaryGeneratorTypes::EnergyDistributionTypesToString( TRestGeant4PrimaryGeneratorTypes::StringToEnergyDistributionTypes(type.Data())); } - inline void SetEnergyDistributionRange(const TVector2& range) { fEnergyDistributionRange = range; } + inline void SetEnergyDistributionRange(const TVector2& range) { + fEnergyDistributionRange = range; + if (fEnergyDistributionRange.x() < 0) { + fEnergyDistributionRange.Set(0, fEnergyDistributionRange.y()); + } + if (fEnergyDistributionRange.y() < 0) { + fEnergyDistributionRange.Set(fEnergyDistributionRange.x(), 0); + } + if (fEnergyDistributionRange.x() > fEnergyDistributionRange.y()) { + fEnergyDistributionRange.Set(fEnergyDistributionRange.y(), fEnergyDistributionRange.x()); + } + } inline void SetEnergyDistributionFilename(const TString& filename) { fEnergyDistributionFilename = filename; } From e321fbf98f0518fa923f1a5f06a0fe979a770862 Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 19 Jul 2022 14:20:28 +0200 Subject: [PATCH 042/100] TRestGeant4ParticleSource - fixed bug --- inc/TRestGeant4ParticleSource.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/inc/TRestGeant4ParticleSource.h b/inc/TRestGeant4ParticleSource.h index d735e0f..1aa37c8 100644 --- a/inc/TRestGeant4ParticleSource.h +++ b/inc/TRestGeant4ParticleSource.h @@ -107,14 +107,14 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada } inline void SetEnergyDistributionRange(const TVector2& range) { fEnergyDistributionRange = range; - if (fEnergyDistributionRange.x() < 0) { - fEnergyDistributionRange.Set(0, fEnergyDistributionRange.y()); + if (fEnergyDistributionRange.X() < 0) { + fEnergyDistributionRange.Set(0, fEnergyDistributionRange.Y()); } - if (fEnergyDistributionRange.y() < 0) { - fEnergyDistributionRange.Set(fEnergyDistributionRange.x(), 0); + if (fEnergyDistributionRange.Y() < 0) { + fEnergyDistributionRange.Set(fEnergyDistributionRange.X(), 0); } - if (fEnergyDistributionRange.x() > fEnergyDistributionRange.y()) { - fEnergyDistributionRange.Set(fEnergyDistributionRange.y(), fEnergyDistributionRange.x()); + if (fEnergyDistributionRange.Y() > fEnergyDistributionRange.Y()) { + fEnergyDistributionRange.Set(fEnergyDistributionRange.Y(), fEnergyDistributionRange.X()); } } inline void SetEnergyDistributionFilename(const TString& filename) { From b264355589a5fba73e7c16c6c8e4e40f19d5fc4b Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 19 Jul 2022 20:56:19 +0200 Subject: [PATCH 043/100] Updated parameter names preserving backwards comp --- src/TRestGeant4Metadata.cxx | 69 ++++++++++++++++++------------- test/files/TRestGeant4Example.rml | 4 +- 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 37c92e9..5544bf7 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -159,8 +159,8 @@ /// /// /// -/// -/// +/// +/// /// /// /// @@ -283,8 +283,8 @@ /// \code /// // 3. geant4 internal /// -/// -/// +/// +/// /// /// \endcode /// @@ -331,7 +331,7 @@ /// \code /// // We launch pre-generated 136Xe NLDBD events /// -/// // The energyDist and angularDist +/// // The energy and angular /// // definitions here will be just /// // ignored with this type of source /// @@ -341,7 +341,7 @@ /// /// A source, or particle, associated kinetic energy is defined by using. /// \code -/// +/// /// \endcode /// /// There are different energy distribution types we can use to define the @@ -354,7 +354,7 @@ /// `energy="E"`, where `E` is the kinetic energy of the particle in keV. /// \code /// // A mono energetic particle with 10keV. -/// +/// /// \endcode /// /// * **flat**: All the particles from this source will be launched in a @@ -362,7 +362,7 @@ /// define the parameter `range="(Ei,Ef)"`, where `Ei` is the minimum energy and /// `Ef` is the maximum energy (in keV). \code /// // A random uniform generation between 1 and 10 keV -/// +/// /// \endcode /// /// * **log**: All the particles from this source will be launched in a @@ -370,12 +370,12 @@ /// define the parameter `range="(Ei,Ef)"`, where `Ei` is the minimum energy and /// `Ef` is the maximum energy (in keV). `Ei` needs to be > 0. \code /// // A random logarithmic generation between 1 and 10 keV -/// +/// /// \endcode /// /// * **TH1D**: It will use a TH1D histogram from a ROOT file with a user /// defined spectrum. It requires to define the parameters -/// `file="mySpectrum.root"` `spctName="histName"` and `range="(Ei,Ef)"`. The +/// `file="mySpectrum.root"` `name="histName"` and `range="(Ei,Ef)"`. The /// ROOT file should contain a TH1D histogram with name `histName`. Only the /// region of the spectrum inside the range `Ei-Ef` will be considered. If `range` /// is not specified inside the RML, the full TH1D range definition will be used. @@ -385,7 +385,7 @@ /// \code /// // A TH1D input spectrum to produce underground muons in the range /// // between 150 and 400 GeV -/// /// \endcode /// @@ -393,7 +393,7 @@ /// /// The momentum direction of a particle is specified by using. /// \code -/// +/// /// \endcode /// /// We can use different types of angular distributions to define the @@ -406,7 +406,7 @@ /// normalized). /// \code /// // Particles will be launched in the positive y-axis direction. -/// +/// /// \endcode /// /// * **isotropic**: The momentum direction of each particle will be @@ -415,7 +415,7 @@ /// volume will be considered. /// \code /// // Particles will be launched without preferred direction. -/// +/// /// \endcode /// /// * **backtoback**: The source momentum direction will be opposite to @@ -423,18 +423,18 @@ /// angular distribution type will be re-defined to isotropic. /// \code /// // Particles will be launched without preferred direction. -/// +/// /// \endcode /// /// * **TH1D** : It will use a TH1D histogram from a ROOT file with a /// user defined angular distribution. It requires to define the /// additional parameters as `file="mySpectrum.root"` and -/// `spctName="histName"`. The file we give should be stored in +/// `name="histName"`. The file we give should be stored in /// `"data/distributions/"` and contain a TH1D histogram with /// name "histName". /// \code /// // A TH1D input angular distribution used for cosmic rays -/// +/// /// \endcode /// /// ## 3. The storage section definition @@ -885,8 +885,8 @@ void TRestGeant4Metadata::ReadBiasing() { /// /// ///3. geant4 internal /// /// /// \endcode @@ -959,20 +959,25 @@ void TRestGeant4Metadata::ReadParticleSource(TRestGeant4ParticleSource* source, source->SetParticleCharge(charge); TString fullChain = GetParameter("fullChain", sourceDefinition); - - if (fullChain == "on" || fullChain == "ON" || fullChain == "On" || fullChain == "oN") { + SetFullChain(false); + if (fullChain.EqualTo("on"), TString::ECaseCompare::kIgnoreCase) { SetFullChain(true); - } else { - SetFullChain(false); } // Angular distribution parameters - TiXmlElement* angularDefinition = GetElement("angularDist", sourceDefinition); + TiXmlElement* angularDefinition = GetElement("angular", sourceDefinition); + if (angularDefinition == nullptr) { + angularDefinition = GetElement("angularDist", sourceDefinition); // old name + } source->SetAngularDistributionType(GetParameter("type", angularDefinition, "flux")); if (StringToAngularDistributionTypes(source->GetAngularDistributionType().Data()) == AngularDistributionTypes::TH1D) { source->SetAngularDistributionFilename(SearchFile(GetParameter("file", angularDefinition))); - source->SetAngularDistributionNameInFile(GetParameter("spctName", angularDefinition)); + auto name = GetParameter("name", angularDefinition); + if (name == "NO_SUCH_PARA") { + name = GetParameter("spctName", angularDefinition); // old name + } + source->SetAngularDistributionNameInFile(name); } if (StringToAngularDistributionTypes(source->GetAngularDistributionType().Data()) == AngularDistributionTypes::FORMULA) { @@ -982,17 +987,25 @@ void TRestGeant4Metadata::ReadParticleSource(TRestGeant4ParticleSource* source, StringToAngularDistributionTypes(source->GetAngularDistributionType().Data()) == AngularDistributionTypes::BACK_TO_BACK) { cout << "WARNING: First source cannot be backtoback. Setting it to isotropic" << endl; - source->SetAngularDistributionType("isotropic"); + source->SetAngularDistributionType( + AngularDistributionTypesToString(AngularDistributionTypes::ISOTROPIC)); } source->SetDirection(StringTo3DVector(GetParameter("direction", angularDefinition, "(1,0,0)"))); // Energy distribution parameters - TiXmlElement* energyDefinition = GetElement("energyDist", sourceDefinition); + TiXmlElement* energyDefinition = GetElement("energy", sourceDefinition); + if (energyDefinition == nullptr) { + energyDefinition = GetElement("energyDist", sourceDefinition); // old name + } source->SetEnergyDistributionType(GetParameter("type", energyDefinition, "mono")); if (StringToEnergyDistributionTypes(source->GetEnergyDistributionType().Data()) == EnergyDistributionTypes::TH1D) { source->SetEnergyDistributionFilename(SearchFile(GetParameter("file", energyDefinition))); - source->SetEnergyDistributionNameInFile(GetParameter("spctName", energyDefinition)); + auto name = GetParameter("name", energyDefinition); + if (name == "NO_SUCH_PARA") { + name = GetParameter("spctName", energyDefinition); // old name + } + source->SetEnergyDistributionNameInFile(name); } source->SetEnergyDistributionRange(Get2DVectorParameterWithUnits("range", energyDefinition)); if (StringToEnergyDistributionTypes(source->GetEnergyDistributionType().Data()) == diff --git a/test/files/TRestGeant4Example.rml b/test/files/TRestGeant4Example.rml index 705c01d..bf2e58b 100644 --- a/test/files/TRestGeant4Example.rml +++ b/test/files/TRestGeant4Example.rml @@ -11,8 +11,8 @@ - - + + From 20544e484bf367ca4e0b253c5f15a9bf734d5074 Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 19 Jul 2022 21:29:53 +0200 Subject: [PATCH 044/100] TRestGeant4Metadata - fixed bug in "fullChain" --- src/TRestGeant4Metadata.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 5544bf7..c430e25 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -960,7 +960,7 @@ void TRestGeant4Metadata::ReadParticleSource(TRestGeant4ParticleSource* source, TString fullChain = GetParameter("fullChain", sourceDefinition); SetFullChain(false); - if (fullChain.EqualTo("on"), TString::ECaseCompare::kIgnoreCase) { + if (fullChain.EqualTo("on", TString::ECaseCompare::kIgnoreCase)) { SetFullChain(true); } From 1f9dd2cc425d5aec86e18d772138c866b4157403 Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 19 Jul 2022 21:40:44 +0200 Subject: [PATCH 045/100] TRestGeant4Metadata - simplified charge initialization --- src/TRestGeant4Metadata.cxx | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index c430e25..bac9853 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -947,16 +947,8 @@ void TRestGeant4Metadata::ReadParticleSource(TRestGeant4ParticleSource* source, TiXmlElement* sourceDefinition = definition; source->SetParticleName(GetParameter("particle", sourceDefinition)); - source->SetExcitationLevel(StringToDouble(GetParameter("excitedLevel", sourceDefinition))); - - Int_t charge = 0; - if (GetParameter("charge", sourceDefinition) == PARAMETER_NOT_FOUND_STR) - charge = 0; - else - charge = StringToInteger(GetParameter("charge", sourceDefinition)); - - source->SetParticleCharge(charge); + source->SetParticleCharge(StringToInteger(GetParameter("charge", sourceDefinition, "0"))); TString fullChain = GetParameter("fullChain", sourceDefinition); SetFullChain(false); From 4140662117c8311eae744fa544c6c8f9429b386b Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Wed, 20 Jul 2022 11:56:03 +0200 Subject: [PATCH 046/100] Fixed example 09 validation bug --- src/TRestGeant4Metadata.cxx | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index bac9853..49bb20d 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1053,6 +1053,16 @@ void TRestGeant4Metadata::ReadStorage() { RESTInfo << "Sensitive volume: " << fSensitiveVolume << RESTendl; fEnergyRangeStored = Get2DVectorParameterWithUnits("energyRange", storageDefinition); + // TODO: Place this as a generic method + if (fEnergyRangeStored.X() < 0) { + fEnergyRangeStored.Set(0, fEnergyRangeStored.Y()); + } + if (fEnergyRangeStored.Y() < 0) { + fEnergyRangeStored.Set(fEnergyRangeStored.X(), 0); + } + if (fEnergyRangeStored.Y() > fEnergyRangeStored.Y()) { + fEnergyRangeStored.Set(fEnergyRangeStored.Y(), fEnergyRangeStored.X()); + } auto gdml = new TRestGDMLParser(); gdml->Load(GetGdmlFilename().Data()); From bac1ac9e12c131bcaebee5d059a68b9cdcee6101 Mon Sep 17 00:00:00 2001 From: lobis Date: Wed, 20 Jul 2022 17:33:14 +0200 Subject: [PATCH 047/100] TRestGeant4Metadata - safer parameter initialization --- inc/TRestGeant4Metadata.h | 6 +++--- src/TRestGeant4Metadata.cxx | 35 ++++++++++++++++++++--------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index fc8db60..79ca0fa 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -269,9 +269,9 @@ class TRestGeant4Metadata : public TRestMetadata { /// Returns the probability per event to register (write to disk) hits in a /// GDML volume given its geometry name. - Double_t GetStorageChance(TString vol); + Double_t GetStorageChance(TString volume); - Double_t GetMaxStepSize(TString vol); + Double_t GetMaxStepSize(TString volume); /// Returns the minimum event energy required for an event to be stored. inline Double_t GetMinimumEnergyStored() const { return fEnergyRangeStored.X(); } @@ -291,7 +291,7 @@ class TRestGeant4Metadata : public TRestMetadata { Int_t GetActiveVolumeID(TString name); - Bool_t isVolumeStored(TString volName); + Bool_t isVolumeStored(TString volume); void SetActiveVolume(const TString& name, Double_t chance, Double_t maxStep = 0); diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 49bb20d..4539910 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -903,13 +903,16 @@ void TRestGeant4Metadata::ReadGenerator() { TiXmlElement* generatorDefinition = GetElement("generator"); - fGeant4PrimaryGeneratorInfo.fSpatialGeneratorType = SpatialGeneratorTypesToString( - StringToSpatialGeneratorTypes(GetParameter("type", generatorDefinition, "volume"))); - fGeant4PrimaryGeneratorInfo.fSpatialGeneratorShape = SpatialGeneratorShapesToString( - StringToSpatialGeneratorShapes(GetParameter("shape", generatorDefinition, "box"))); + fGeant4PrimaryGeneratorInfo.fSpatialGeneratorType = + SpatialGeneratorTypesToString(StringToSpatialGeneratorTypes(GetParameter( + "type", generatorDefinition, SpatialGeneratorTypesToString(SpatialGeneratorTypes::VOLUME)))); + fGeant4PrimaryGeneratorInfo.fSpatialGeneratorShape = + SpatialGeneratorShapesToString(StringToSpatialGeneratorShapes(GetParameter( + "shape", generatorDefinition, SpatialGeneratorShapesToString(SpatialGeneratorShapes::BOX)))); fGeant4PrimaryGeneratorInfo.fSpatialGeneratorFrom = GetParameter("from", generatorDefinition); if (fGeant4PrimaryGeneratorInfo.fSpatialGeneratorFrom != PARAMETER_NOT_FOUND_STR) { - fGeant4PrimaryGeneratorInfo.fSpatialGeneratorShape = "gdml"; + fGeant4PrimaryGeneratorInfo.fSpatialGeneratorShape = + SpatialGeneratorShapesToString(SpatialGeneratorShapes::GDML); } fGeant4PrimaryGeneratorInfo.fSpatialGeneratorSize = Get3DVectorParameterWithUnits("size", generatorDefinition); @@ -961,7 +964,8 @@ void TRestGeant4Metadata::ReadParticleSource(TRestGeant4ParticleSource* source, if (angularDefinition == nullptr) { angularDefinition = GetElement("angularDist", sourceDefinition); // old name } - source->SetAngularDistributionType(GetParameter("type", angularDefinition, "flux")); + source->SetAngularDistributionType(GetParameter( + "type", angularDefinition, AngularDistributionTypesToString(AngularDistributionTypes::FLUX))); if (StringToAngularDistributionTypes(source->GetAngularDistributionType().Data()) == AngularDistributionTypes::TH1D) { source->SetAngularDistributionFilename(SearchFile(GetParameter("file", angularDefinition))); @@ -989,7 +993,8 @@ void TRestGeant4Metadata::ReadParticleSource(TRestGeant4ParticleSource* source, if (energyDefinition == nullptr) { energyDefinition = GetElement("energyDist", sourceDefinition); // old name } - source->SetEnergyDistributionType(GetParameter("type", energyDefinition, "mono")); + source->SetEnergyDistributionType(GetParameter( + "type", energyDefinition, EnergyDistributionTypesToString(EnergyDistributionTypes::MONO))); if (StringToEnergyDistributionTypes(source->GetEnergyDistributionType().Data()) == EnergyDistributionTypes::TH1D) { source->SetEnergyDistributionFilename(SearchFile(GetParameter("file", energyDefinition))); @@ -1201,9 +1206,9 @@ void TRestGeant4Metadata::SetActiveVolume(const TString& name, Double_t chance, /// \brief Returns true if the volume named *volName* has been registered for /// data storage. /// -Bool_t TRestGeant4Metadata::isVolumeStored(TString volName) { +Bool_t TRestGeant4Metadata::isVolumeStored(TString volume) { for (int n = 0; n < GetNumberOfActiveVolumes(); n++) - if (GetActiveVolumeName(n) == volName) return true; + if (GetActiveVolumeName(n) == volume) return true; return false; } @@ -1211,12 +1216,12 @@ Bool_t TRestGeant4Metadata::isVolumeStored(TString volName) { /////////////////////////////////////////////// /// \brief Returns the probability of an active volume being stored /// -Double_t TRestGeant4Metadata::GetStorageChance(TString vol) { +Double_t TRestGeant4Metadata::GetStorageChance(TString volume) { Int_t id; for (id = 0; id < (Int_t)fActiveVolumes.size(); id++) { - if (fActiveVolumes[id] == vol) return fChance[id]; + if (fActiveVolumes[id] == volume) return fChance[id]; } - RESTWarning << "TRestGeant4Metadata::GetStorageChance. Volume " << vol << " not found" << RESTendl; + RESTWarning << "TRestGeant4Metadata::GetStorageChance. Volume " << volume << " not found" << RESTendl; return 0; } @@ -1224,11 +1229,11 @@ Double_t TRestGeant4Metadata::GetStorageChance(TString vol) { /////////////////////////////////////////////// /// \brief Returns the maximum step at a particular active volume /// -Double_t TRestGeant4Metadata::GetMaxStepSize(TString vol) { +Double_t TRestGeant4Metadata::GetMaxStepSize(TString volume) { for (Int_t id = 0; id < (Int_t)fActiveVolumes.size(); id++) { - if (fActiveVolumes[id] == vol) return fMaxStepSize[id]; + if (fActiveVolumes[id] == volume) return fMaxStepSize[id]; } - RESTWarning << "TRestGeant4Metadata::GetMaxStepSize. Volume " << vol << " not found" << RESTendl; + RESTWarning << "TRestGeant4Metadata::GetMaxStepSize. Volume " << volume << " not found" << RESTendl; return 0; } From 44207bc4943f7d1f4e7c14f6e5ed5d5efcac1358 Mon Sep 17 00:00:00 2001 From: lobis Date: Wed, 20 Jul 2022 19:06:05 +0200 Subject: [PATCH 048/100] TRestGeant4Metadata - use more default values --- inc/TRestGeant4Metadata.h | 2 +- inc/TRestGeant4ParticleSource.h | 1 - src/TRestGeant4Metadata.cxx | 8 ++++---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 79ca0fa..5151a62 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -235,7 +235,7 @@ class TRestGeant4Metadata : public TRestMetadata { inline Int_t GetNumberOfSources() const { return fParticleSource.size(); } /// Returns the name of the particle source with index n (Geant4 based names). - inline TRestGeant4ParticleSource* GetParticleSource(int n) const { return fParticleSource[n]; } + inline TRestGeant4ParticleSource* GetParticleSource(size_t n = 0) const { return fParticleSource[n]; } /// Remove all the particle sources. void RemoveParticleSources(); diff --git a/inc/TRestGeant4ParticleSource.h b/inc/TRestGeant4ParticleSource.h index 1aa37c8..d3b4dad 100644 --- a/inc/TRestGeant4ParticleSource.h +++ b/inc/TRestGeant4ParticleSource.h @@ -58,7 +58,6 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada virtual void InitFromConfigFile(); static TRestGeant4ParticleSource* instantiate(std::string model = ""); - inline TString GetParticleName() const { return fParticleName; } inline TVector3 GetDirection() const { if (fDirection.Mag() <= 0) { std::cout << "TRestGeant4ParticleSource::GetDirection: " diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 4539910..db179c1 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -915,13 +915,13 @@ void TRestGeant4Metadata::ReadGenerator() { SpatialGeneratorShapesToString(SpatialGeneratorShapes::GDML); } fGeant4PrimaryGeneratorInfo.fSpatialGeneratorSize = - Get3DVectorParameterWithUnits("size", generatorDefinition); + Get3DVectorParameterWithUnits("size", generatorDefinition, {0, 0, 0}); fGeant4PrimaryGeneratorInfo.fSpatialGeneratorPosition = - Get3DVectorParameterWithUnits("position", generatorDefinition); + Get3DVectorParameterWithUnits("position", generatorDefinition, {0, 0, 0}); fGeant4PrimaryGeneratorInfo.fSpatialGeneratorRotationAxis = - StringTo3DVector(GetParameter("rotationAxis", generatorDefinition, "(0,0,1)")); + Get3DVectorParameterWithUnits("rotationAxis", generatorDefinition, {0, 0, 1}); fGeant4PrimaryGeneratorInfo.fSpatialGeneratorRotationValue = - GetDblParameterWithUnits("rotationAngle", generatorDefinition); + GetDblParameterWithUnits("rotationAngle", generatorDefinition, 0); fGeant4PrimaryGeneratorInfo.fSpatialGeneratorSpatialDensityFunction = GetParameter("densityFunc", generatorDefinition, "1"); From d824de68d094f268031e4257108f8aa2c485ce2d Mon Sep 17 00:00:00 2001 From: lobis Date: Thu, 21 Jul 2022 11:46:18 +0200 Subject: [PATCH 049/100] Cleaned some print statements --- inc/TRestGeant4Particle.h | 26 ++++++------- src/TRestGeant4Metadata.cxx | 41 ++++++++++---------- src/TRestGeant4ParticleSource.cxx | 63 ++++++++++++++++--------------- 3 files changed, 65 insertions(+), 65 deletions(-) diff --git a/inc/TRestGeant4Particle.h b/inc/TRestGeant4Particle.h index 96d98da..3722d57 100644 --- a/inc/TRestGeant4Particle.h +++ b/inc/TRestGeant4Particle.h @@ -21,14 +21,12 @@ #include -#include "TObject.h" - -class TRestGeant4Particle : public TObject { +class TRestGeant4Particle { protected: TString fParticleName; Double_t fExcitationLevel = 0; TVector3 fDirection = {1, 0, 0}; - Double_t fEnergy; + Double_t fEnergy = 0; Int_t fCharge = 0; TVector3 fOrigin; @@ -40,17 +38,17 @@ class TRestGeant4Particle : public TObject { inline Int_t GetParticleCharge() const { return fCharge; } inline TVector3 GetOrigin() const { return fOrigin; } - void SetParticle(TRestGeant4Particle ptcle) { - fExcitationLevel = ptcle.GetExcitationLevel(); - fParticleName = ptcle.GetParticleName(); - fEnergy = ptcle.GetEnergy(); - fDirection = ptcle.GetMomentumDirection(); - fOrigin = ptcle.fOrigin; + void SetParticle(TRestGeant4Particle particle) { + fExcitationLevel = particle.GetExcitationLevel(); + fParticleName = particle.GetParticleName(); + fEnergy = particle.GetEnergy(); + fDirection = particle.GetMomentumDirection(); + fOrigin = particle.fOrigin; } - void SetParticleName(TString ptcle) { fParticleName = ptcle; } - void SetExcitationLevel(Double_t eenergy) { - fExcitationLevel = eenergy; + void SetParticleName(TString particle) { fParticleName = particle; } + void SetExcitationLevel(Double_t excitationEnergy) { + fExcitationLevel = excitationEnergy; if (fExcitationLevel < 0) fExcitationLevel = 0; } @@ -65,6 +63,6 @@ class TRestGeant4Particle : public TObject { // Destructor virtual ~TRestGeant4Particle(); - ClassDef(TRestGeant4Particle, 3); + ClassDef(TRestGeant4Particle, 4); }; #endif diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index db179c1..188a8b8 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -426,7 +426,7 @@ /// /// \endcode /// -/// * **TH1D** : It will use a TH1D histogram from a ROOT file with a +/// * **TH1D**: It will use a TH1D histogram from a ROOT file with a /// user defined angular distribution. It requires to define the /// additional parameters as `file="mySpectrum.root"` and /// `name="histName"`. The file we give should be stored in @@ -837,7 +837,7 @@ void TRestGeant4Metadata::ReadBiasing() { TString biasEnabled = GetFieldValue("value", biasingDefinition); TString biasType = GetFieldValue("type", biasingDefinition); - RESTDebug << "Bias : " << biasEnabled << RESTendl; + RESTDebug << "Bias: " << biasEnabled << RESTendl; if (biasEnabled == "on" || biasEnabled == "ON" || biasEnabled == "On" || biasEnabled == "oN") { RESTInfo << "Biasing is enabled" << RESTendl; @@ -846,7 +846,7 @@ void TRestGeant4Metadata::ReadBiasing() { Int_t n = 0; while (biasVolumeDefinition) { TRestGeant4BiasingVolume biasVolume; - RESTDebug << "Def : " << biasVolumeDefinition << RESTendl; + RESTDebug << "Def: " << biasVolumeDefinition << RESTendl; biasVolume.SetBiasingVolumePosition( Get3DVectorParameterWithUnits("position", biasVolumeDefinition)); @@ -1126,37 +1126,38 @@ void TRestGeant4Metadata::ReadStorage() { void TRestGeant4Metadata::PrintMetadata() { TRestMetadata::PrintMetadata(); - RESTMetadata << "Geant 4 version : " << GetGeant4Version() << RESTendl; - RESTMetadata << "Random seed : " << GetSeed() << RESTendl; - RESTMetadata << "GDML geometry : " << GetGdmlReference() << RESTendl; - RESTMetadata << "GDML materials reference : " << GetMaterialsReference() << RESTendl; - RESTMetadata << "Sub-event time delay : " << GetSubEventTimeDelay() << " us" << RESTendl; + RESTMetadata << "Geant4 version: " << GetGeant4Version() << RESTendl; + RESTMetadata << "Random seed: " << GetSeed() << RESTendl; + RESTMetadata << "GDML geometry: " << GetGdmlReference() << RESTendl; + RESTMetadata << "GDML materials reference: " << GetMaterialsReference() << RESTendl; + RESTMetadata << "Sub-event time delay: " << GetSubEventTimeDelay() << " us" << RESTendl; Double_t mx = GetMagneticField().X(); Double_t my = GetMagneticField().Y(); Double_t mz = GetMagneticField().Z(); - RESTMetadata << "Magnetic field : (" << mx << ", " << my << ", " << mz << ") T" << RESTendl; + RESTMetadata << "Magnetic field: (" << mx << ", " << my << ", " << mz << ") T" << RESTendl; if (fSaveAllEvents) RESTMetadata << "Save all events was enabled!" << RESTendl; - if (fRegisterEmptyTracks) + if (fRegisterEmptyTracks) { RESTMetadata << "Register empty tracks was enabled" << RESTendl; - else + } else { RESTMetadata << "Register empty tracks was NOT enabled" << RESTendl; + } RESTMetadata << " ++++++++++ Generator +++++++++++ " << RESTendl; - RESTMetadata << "Number of generated events : " << GetNumberOfEvents() << RESTendl; + RESTMetadata << "Number of generated events: " << GetNumberOfEvents() << RESTendl; fGeant4PrimaryGeneratorInfo.Print(); for (int i = 0; i < GetNumberOfSources(); i++) GetParticleSource(i)->PrintParticleSource(); RESTMetadata << " ++++++++++ Storage volumes +++++++++++ " << RESTendl; - RESTMetadata << "Energy range : Emin = " << GetMinimumEnergyStored() - << ", Emax : " << GetMaximumEnergyStored() << RESTendl; - RESTMetadata << "Sensitive volume : " << GetSensitiveVolume() << RESTendl; - RESTMetadata << "Active volumes : " << GetNumberOfActiveVolumes() << RESTendl; + RESTMetadata << "Energy range (keV): (" << GetMinimumEnergyStored() << ", " << GetMaximumEnergyStored() + << ")" << RESTendl; + RESTMetadata << "Sensitive volume: " << GetSensitiveVolume() << RESTendl; + RESTMetadata << "Active volumes: " << GetNumberOfActiveVolumes() << RESTendl; RESTMetadata << "---------------------------------------" << RESTendl; for (int n = 0; n < GetNumberOfActiveVolumes(); n++) { - RESTMetadata << "Name : " << GetActiveVolumeName(n) - << ", ID : " << GetActiveVolumeID(GetActiveVolumeName(n)) - << ", maxStep : " << GetMaxStepSize(GetActiveVolumeName(n)) << "mm " - << ", chance : " << GetStorageChance(GetActiveVolumeName(n)) << RESTendl; + RESTMetadata << "Name: " << GetActiveVolumeName(n) + << ", ID: " << GetActiveVolumeID(GetActiveVolumeName(n)) + << ", maxStep: " << GetMaxStepSize(GetActiveVolumeName(n)) << "mm " + << ", chance: " << GetStorageChance(GetActiveVolumeName(n)) << RESTendl; } for (int n = 0; n < GetNumberOfBiasingVolumes(); n++) { GetBiasingVolume(n).PrintBiasingVolume(); diff --git a/src/TRestGeant4ParticleSource.cxx b/src/TRestGeant4ParticleSource.cxx index 67a0184..77f545c 100644 --- a/src/TRestGeant4ParticleSource.cxx +++ b/src/TRestGeant4ParticleSource.cxx @@ -3,7 +3,7 @@ ///_______________________________________________________________________________ /// /// -/// RESTSoft : Software for Rare Event Searches with TPCs +/// RESTSoft: Software for Rare Event Searches with TPCs /// /// TRestGeant4ParticleSource.cxx /// @@ -42,25 +42,31 @@ void TRestGeant4ParticleSource::PrintParticleSource() { for (const auto& particle : fParticles) RESTMetadata << particle.GetParticleName() << ", "; RESTMetadata << RESTendl; } else { - RESTMetadata << "Charge : " << GetParticleCharge() << RESTendl; - RESTMetadata << "Angular distribution type : " << GetAngularDistributionType() << RESTendl; - if (GetAngularDistributionType() == "TH1D") { - RESTMetadata << "Angular distribution filename : " + if (GetParticleCharge() != 0) { + RESTMetadata << "Particle charge: " << GetParticleCharge() << RESTendl; + } + RESTMetadata << "Angular distribution type: " << GetAngularDistributionType() << RESTendl; + if (StringToAngularDistributionTypes(GetAngularDistributionType().Data()) == + AngularDistributionTypes::TH1D) { + RESTMetadata << "Angular distribution filename: " << TRestTools::GetPureFileName((string)GetAngularDistributionFilename()) << RESTendl; - RESTMetadata << "Angular histogram name : " << GetAngularDistributionNameInFile() << RESTendl; + RESTMetadata << "Angular histogram name: " << GetAngularDistributionNameInFile() << RESTendl; } - RESTMetadata << "Direction : (" << GetDirection().X() << "," << GetDirection().Y() << "," + RESTMetadata << "Direction: (" << GetDirection().X() << "," << GetDirection().Y() << "," << GetDirection().Z() << ")" << RESTendl; - RESTMetadata << "Energy distribution : " << GetEnergyDistributionType() << RESTendl; - if (GetEnergyDistributionType() == "TH1D") { - RESTMetadata << "Energy distribution filename : " + RESTMetadata << "Energy distribution type: " << GetEnergyDistributionType() << RESTendl; + if (StringToEnergyDistributionTypes(GetEnergyDistributionType().Data()) == + EnergyDistributionTypes::TH1D) { + RESTMetadata << "Energy distribution filename: " << TRestTools::GetPureFileName((string)GetEnergyDistributionFilename()) << RESTendl; - RESTMetadata << "Energy histogram name : " << GetEnergyDistributionNameInFile() << RESTendl; - } else if (GetEnergyDistributionRange().X() == GetEnergyDistributionRange().Y()) - RESTMetadata << "Energy : " << GetEnergy() << " keV" << RESTendl; - else - RESTMetadata << "Energy range : (" << GetEnergyDistributionRange().X() << "," - << GetEnergyDistributionRange().Y() << ") keV" << RESTendl; + RESTMetadata << "Energy histogram name: " << GetEnergyDistributionNameInFile() << RESTendl; + } + if (GetEnergyDistributionRangeMin() == GetEnergyDistributionRangeMax()) { + RESTMetadata << "Energy: " << GetEnergy() << " keV" << RESTendl; + } else { + RESTMetadata << "Energy range (keV): (" << GetEnergyDistributionRangeMin() << ", " + << GetEnergyDistributionRangeMax() << ")" << RESTendl; + } } } @@ -91,7 +97,7 @@ void TRestGeant4ParticleSource::InitFromConfigFile() { if (((string)modelUse).find(".dat") != -1) { string fullPathName = SearchFile((string)modelUse); if (fullPathName == "") { - RESTError << "File not found : " << modelUse << RESTendl; + RESTError << "File not found: " << modelUse << RESTendl; RESTError << "Decay0 generator file could not be found!!" << RESTendl; exit(1); } @@ -167,15 +173,10 @@ bool TRestGeant4ParticleSource::ReadNewDecay0File(TString fileName) { exit(1); } - // TRestGeant4ParticleSource* src = TRestGeant4ParticleSource::instantiate(); - // this->SetGenFilename(fileName); - // this->SetAngularDistributionType("flux"); - // this->SetEnergyDistributionType("mono"); - TRestGeant4Particle particle; - RESTDebug << "Reading generator file : " << fileName << RESTendl; - RESTDebug << "Total number of events : " << generatorEvents << RESTendl; + RESTDebug << "Reading generator file: " << fileName << RESTendl; + RESTDebug << "Total number of events: " << generatorEvents << RESTendl; for (int n = 0; n < generatorEvents && !infile.eof(); n++) { int pos = -1; while (!infile.eof() && pos == -1) { @@ -188,7 +189,7 @@ bool TRestGeant4ParticleSource::ReadNewDecay0File(TString fileName) { Int_t nParticles; infile >> nParticles; - RESTDebug << "Number of particles : " << nParticles << RESTendl; + RESTDebug << "Number of particles: " << nParticles << RESTendl; // cout << evID <<" "<< time <<" "<< nParticles <<" "<< std::endl; for (int i = 0; i < nParticles && !infile.eof(); i++) { @@ -201,9 +202,9 @@ bool TRestGeant4ParticleSource::ReadNewDecay0File(TString fileName) { infile >> pID >> time >> momx >> momy >> momz >> pName; RESTDebug << " ---- New particle found --- " << RESTendl; - RESTDebug << " Particle name : " << pName << RESTendl; - RESTDebug << " - pId : " << pID << RESTendl; - RESTDebug << " - Relative time : " << time << RESTendl; + RESTDebug << " Particle name: " << pName << RESTendl; + RESTDebug << " - pId: " << pID << RESTendl; + RESTDebug << " - Relative time: " << time << RESTendl; RESTDebug << " - px: " << momx << " py: " << momy << " pz: " << momz << " " << RESTendl; if (pID == 3) { @@ -275,7 +276,7 @@ bool TRestGeant4ParticleSource::ReadOldDecay0File(TString fileName) { int fGeneratorEvents; infile >> tmpInt >> fGeneratorEvents; - // cout << "i : " << tmpInt << " fN : " << fGeneratorEvents << std::endl; + // cout << "i: " << tmpInt << " fN: " << fGeneratorEvents << std::endl; // TRestGeant4ParticleSource* src = TRestGeant4ParticleSource::instantiate(); // this->SetGenFilename(fileName); // this->SetAngularDistributionType("flux"); @@ -283,8 +284,8 @@ bool TRestGeant4ParticleSource::ReadOldDecay0File(TString fileName) { TRestGeant4Particle particle; - cout << "Reading generator file : " << fileName << std::endl; - cout << "Total number of events : " << fGeneratorEvents << std::endl; + cout << "Reading generator file: " << fileName << std::endl; + cout << "Total number of events: " << fGeneratorEvents << std::endl; for (int n = 0; n < fGeneratorEvents && !infile.eof(); n++) { Int_t nParticles; Int_t evID; From 5b4e01e5350a97ce1a6c4be8b9333b4681fa0c42 Mon Sep 17 00:00:00 2001 From: lobis Date: Thu, 21 Jul 2022 12:26:28 +0200 Subject: [PATCH 050/100] Updated formula generator logic --- inc/TRestGeant4ParticleSource.h | 27 ++++++++++++++----------- src/TRestGeant4Metadata.cxx | 17 +++++++++++++--- src/TRestGeant4PrimaryGeneratorInfo.cxx | 11 +++++----- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/inc/TRestGeant4ParticleSource.h b/inc/TRestGeant4ParticleSource.h index d3b4dad..2d35252 100644 --- a/inc/TRestGeant4ParticleSource.h +++ b/inc/TRestGeant4ParticleSource.h @@ -17,6 +17,7 @@ #ifndef RestCore_TRestGeant4ParticleSource #define RestCore_TRestGeant4ParticleSource +#include #include #include #include @@ -37,12 +38,12 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada TString fAngularDistributionType = "Flux"; TString fAngularDistributionFilename; TString fAngularDistributionNameInFile; - TString fAngularDistributionFormulaString; + TF1* fAngularDistributionFunction = nullptr; TString fEnergyDistributionType = "Mono"; TString fEnergyDistributionFilename; TString fEnergyDistributionNameInFile; - TString fEnergyDistributionFormulaString; + TF1* fEnergyDistributionFunction = nullptr; TVector2 fEnergyDistributionRange; TString fGenFilename; @@ -73,12 +74,12 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada inline Double_t GetEnergyDistributionRangeMax() const { return fEnergyDistributionRange.Y(); } inline TString GetEnergyDistributionFilename() const { return fEnergyDistributionFilename; } inline TString GetEnergyDistributionNameInFile() const { return fEnergyDistributionNameInFile; } - inline TString GetEnergyDistributionFormulaString() const { return fEnergyDistributionFormulaString; } + inline const TF1* GetEnergyDistributionFunction() const { return fEnergyDistributionFunction; } inline TString GetAngularDistributionType() const { return fAngularDistributionType; } inline TString GetAngularDistributionFilename() const { return fAngularDistributionFilename; } inline TString GetAngularDistributionNameInFile() const { return fAngularDistributionNameInFile; } - inline TString GetAngularDistributionFormulaString() const { return fAngularDistributionFormulaString; } + inline const TF1* GetAngularDistributionFunction() const { return fAngularDistributionFunction; } inline TString GetGenFilename() const { return fGenFilename; } @@ -94,10 +95,11 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada inline void SetAngularDistributionNameInFile(const TString& name) { fAngularDistributionNameInFile = name; } - inline void SetAngularDistributionFormulaString(const TString& formula) { - fAngularDistributionFormulaString = - TRestGeant4PrimaryGeneratorTypes::AngularDistributionFormulasToString( - TRestGeant4PrimaryGeneratorTypes::StringToAngularDistributionFormulas(formula.Data())); + inline void SetAngularDistributionFormula(const TString& formula) { + fAngularDistributionFunction = + (TF1*)TRestGeant4PrimaryGeneratorTypes::AngularDistributionFormulasToRootFormula( + TRestGeant4PrimaryGeneratorTypes::StringToAngularDistributionFormulas(formula.Data())) + .Clone(); } inline void SetEnergyDistributionType(const TString& type) { @@ -120,10 +122,11 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada fEnergyDistributionFilename = filename; } inline void SetEnergyDistributionNameInFile(const TString& name) { fEnergyDistributionNameInFile = name; } - inline void SetEnergyDistributionFormulaString(const TString& formula) { - fEnergyDistributionFormulaString = - TRestGeant4PrimaryGeneratorTypes::EnergyDistributionFormulasToString( - TRestGeant4PrimaryGeneratorTypes::StringToEnergyDistributionFormulas(formula.Data())); + inline void SetEnergyDistributionFormula(const TString& formula) { + fEnergyDistributionFunction = + (TF1*)TRestGeant4PrimaryGeneratorTypes::EnergyDistributionFormulasToRootFormula( + TRestGeant4PrimaryGeneratorTypes::StringToEnergyDistributionFormulas(formula.Data())) + .Clone(); } inline void SetGenFilename(const TString& name) { fGenFilename = name; } diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 188a8b8..46ad8c5 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -648,6 +648,7 @@ #include "TRestGeant4Metadata.h" +#include #include #include #include @@ -977,7 +978,7 @@ void TRestGeant4Metadata::ReadParticleSource(TRestGeant4ParticleSource* source, } if (StringToAngularDistributionTypes(source->GetAngularDistributionType().Data()) == AngularDistributionTypes::FORMULA) { - source->SetAngularDistributionFormulaString(GetParameter("name", angularDefinition)); + source->SetAngularDistributionFormula(GetParameter("name", angularDefinition)); } if (GetNumberOfSources() == 0 && StringToAngularDistributionTypes(source->GetAngularDistributionType().Data()) == @@ -1004,7 +1005,7 @@ void TRestGeant4Metadata::ReadParticleSource(TRestGeant4ParticleSource* source, } source->SetEnergyDistributionNameInFile(name); } - source->SetEnergyDistributionRange(Get2DVectorParameterWithUnits("range", energyDefinition)); + source->SetEnergyDistributionRange(Get2DVectorParameterWithUnits("range", energyDefinition, {0, 1E20})); if (StringToEnergyDistributionTypes(source->GetEnergyDistributionType().Data()) == EnergyDistributionTypes::MONO) { Double_t energy; @@ -1014,7 +1015,17 @@ void TRestGeant4Metadata::ReadParticleSource(TRestGeant4ParticleSource* source, } if (StringToEnergyDistributionTypes(source->GetEnergyDistributionType().Data()) == EnergyDistributionTypes::FORMULA) { - source->SetEnergyDistributionFormulaString(GetParameter("name", energyDefinition)); + source->SetEnergyDistributionFormula(GetParameter("name", energyDefinition)); + // We cannot use an energy range bigger than the range of the formula + const auto function = source->GetEnergyDistributionFunction(); + if (source->GetEnergyDistributionRangeMin() < function->GetXaxis()->GetXmin()) { + source->SetEnergyDistributionRange( + {function->GetXaxis()->GetXmin(), source->GetEnergyDistributionRangeMax()}); + } + if (source->GetEnergyDistributionRangeMax() > function->GetXaxis()->GetXmax()) { + source->SetEnergyDistributionRange( + {source->GetEnergyDistributionRangeMin(), function->GetXaxis()->GetXmax()}); + } } // allow custom configuration from the class source->LoadConfigFromElement(sourceDefinition, fElementGlobal, fVariables); diff --git a/src/TRestGeant4PrimaryGeneratorInfo.cxx b/src/TRestGeant4PrimaryGeneratorInfo.cxx index 5d0c47b..f23378a 100644 --- a/src/TRestGeant4PrimaryGeneratorInfo.cxx +++ b/src/TRestGeant4PrimaryGeneratorInfo.cxx @@ -189,11 +189,12 @@ TF1 TRestGeant4PrimaryGeneratorTypes::EnergyDistributionFormulasToRootFormula( exit(1); case EnergyDistributionFormulas::COSMIC_NEUTRONS: { const char* title = "EnergyDistribution: Cosmic Neutrons Sea Level"; - auto f = TF1( - title, - "1.006E-6 * TMath::Exp(-0.3500 * TMath::Power(TMath::Log(x), 2) + 2.1451 * TMath::Log(x)) + " - "1.011E-3 * TMath::Exp(-0.4106 * TMath::Power(TMath::Log(x), 2) -0.6670 * TMath::Log(x))", - 0.1E0, 10E3); + auto f = TF1(title, + "1.006E-6 * TMath::Exp(-0.3500 * TMath::Power(TMath::Log(x * 1E-3), 2) + 2.1451 * " + "TMath::Log(x * 1E-3)) + " + "1.011E-3 * TMath::Exp(-0.4106 * TMath::Power(TMath::Log(x * 1E-3), 2) - 0.6670 * " + "TMath::Log(x * 1E-3))", + 1E2, 1E7); f.SetTitle(title); return f; } From 48e7ada9ad0d3afd108d6bee971096dfc2916ec9 Mon Sep 17 00:00:00 2001 From: lobis Date: Thu, 21 Jul 2022 12:40:09 +0200 Subject: [PATCH 051/100] TRestGeant4ParticleSource - fixed small bug --- inc/TRestGeant4ParticleSource.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/TRestGeant4ParticleSource.h b/inc/TRestGeant4ParticleSource.h index 2d35252..ca18fe9 100644 --- a/inc/TRestGeant4ParticleSource.h +++ b/inc/TRestGeant4ParticleSource.h @@ -114,7 +114,7 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada if (fEnergyDistributionRange.Y() < 0) { fEnergyDistributionRange.Set(fEnergyDistributionRange.X(), 0); } - if (fEnergyDistributionRange.Y() > fEnergyDistributionRange.Y()) { + if (fEnergyDistributionRange.X() > fEnergyDistributionRange.Y()) { fEnergyDistributionRange.Set(fEnergyDistributionRange.Y(), fEnergyDistributionRange.X()); } } From 528ae0e223e3d45d43c0a7e678d9a8446a5f4dff Mon Sep 17 00:00:00 2001 From: lobis Date: Thu, 21 Jul 2022 12:49:21 +0200 Subject: [PATCH 052/100] TRestGeant4ParticleSource - made print a bit more clear --- src/TRestGeant4ParticleSource.cxx | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/TRestGeant4ParticleSource.cxx b/src/TRestGeant4ParticleSource.cxx index 77f545c..1dc80a4 100644 --- a/src/TRestGeant4ParticleSource.cxx +++ b/src/TRestGeant4ParticleSource.cxx @@ -50,21 +50,23 @@ void TRestGeant4ParticleSource::PrintParticleSource() { AngularDistributionTypes::TH1D) { RESTMetadata << "Angular distribution filename: " << TRestTools::GetPureFileName((string)GetAngularDistributionFilename()) << RESTendl; - RESTMetadata << "Angular histogram name: " << GetAngularDistributionNameInFile() << RESTendl; + RESTMetadata << "Angular distribution histogram name: " << GetAngularDistributionNameInFile() + << RESTendl; } - RESTMetadata << "Direction: (" << GetDirection().X() << "," << GetDirection().Y() << "," - << GetDirection().Z() << ")" << RESTendl; + RESTMetadata << "Angular distribution direction: (" << GetDirection().X() << "," << GetDirection().Y() + << "," << GetDirection().Z() << ")" << RESTendl; RESTMetadata << "Energy distribution type: " << GetEnergyDistributionType() << RESTendl; if (StringToEnergyDistributionTypes(GetEnergyDistributionType().Data()) == EnergyDistributionTypes::TH1D) { RESTMetadata << "Energy distribution filename: " << TRestTools::GetPureFileName((string)GetEnergyDistributionFilename()) << RESTendl; - RESTMetadata << "Energy histogram name: " << GetEnergyDistributionNameInFile() << RESTendl; + RESTMetadata << "Energy distribution histogram name: " << GetEnergyDistributionNameInFile() + << RESTendl; } if (GetEnergyDistributionRangeMin() == GetEnergyDistributionRangeMax()) { - RESTMetadata << "Energy: " << GetEnergy() << " keV" << RESTendl; + RESTMetadata << "Energy distribution energy: " << GetEnergy() << " keV" << RESTendl; } else { - RESTMetadata << "Energy range (keV): (" << GetEnergyDistributionRangeMin() << ", " + RESTMetadata << "Energy distribution range (keV): (" << GetEnergyDistributionRangeMin() << ", " << GetEnergyDistributionRangeMax() << ")" << RESTendl; } } From ff0c798a49f8d66217f39cb590c7cbf5c5612d45 Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Wed, 27 Jul 2022 11:52:01 +0200 Subject: [PATCH 053/100] Updated TRestGeant4Metadata class version --- inc/TRestGeant4Metadata.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 5151a62..459ae14 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -302,7 +302,7 @@ class TRestGeant4Metadata : public TRestMetadata { ~TRestGeant4Metadata(); - ClassDefOverride(TRestGeant4Metadata, 10); + ClassDefOverride(TRestGeant4Metadata, 11); // Allow modification of otherwise inaccessible / immutable members that shouldn't be modified by the user friend class SteppingAction; From 09b9429edc6a29e0c7e3151f35f3fb1053ea993d Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Wed, 27 Jul 2022 14:33:33 +0200 Subject: [PATCH 054/100] Added -n option to restG4 to set nEvents from cli --- inc/TRestGeant4Metadata.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 459ae14..c5e821c 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -260,6 +260,9 @@ class TRestGeant4Metadata : public TRestMetadata { /// Returns a std::string with the name of the sensitive volume. inline TString GetSensitiveVolume() const { return fSensitiveVolume; } + /// Sets the name of the sensitive volume + inline void SetNumberOfEvents(Int_t n) { fNEvents = n; } + /// Sets the name of the sensitive volume inline void SetSensitiveVolume(const TString& sensitiveVolume) { fSensitiveVolume = sensitiveVolume; } From 5ff82d17dea97830a2a17d704ce0048115f3ecb0 Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Wed, 27 Jul 2022 18:40:45 +0200 Subject: [PATCH 055/100] Fixed bug in MT --- inc/TRestGeant4Metadata.h | 9 ++++++++- src/TRestGeant4Metadata.cxx | 2 -- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index c5e821c..6ec68ba 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -125,6 +125,9 @@ class TRestGeant4Metadata : public TRestMetadata { /// The number of events simulated, or to be simulated. Int_t fNEvents = 0; + /// The number of events the user desired to be on the file + Int_t fNDesiredEntries = 0; + /// \brief The seed value used for Geant4 random event generator. /// If it is zero its value will be assigned using the system timestamp. Long_t fSeed = 0; @@ -227,10 +230,12 @@ class TRestGeant4Metadata : public TRestMetadata { /// Returns the number of events to be simulated. inline Int_t GetNumberOfEvents() const { return fNEvents; } + + inline Int_t GetNumberOfDesiredEntries() const { return fNDesiredEntries; } + /////////////////////////////////////////////////////////// // Direct access to sources definition in primary generator - /////////////////////////////////////////////////////////// /// Returns the number of primary event sources defined in the generator. inline Int_t GetNumberOfSources() const { return fParticleSource.size(); } @@ -263,6 +268,8 @@ class TRestGeant4Metadata : public TRestMetadata { /// Sets the name of the sensitive volume inline void SetNumberOfEvents(Int_t n) { fNEvents = n; } + inline void SetNumberOfDesiredEntries(Int_t n) { fNDesiredEntries = n; } + /// Sets the name of the sensitive volume inline void SetSensitiveVolume(const TString& sensitiveVolume) { fSensitiveVolume = sensitiveVolume; } diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 46ad8c5..d020f07 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -685,8 +685,6 @@ TRestGeant4Metadata::TRestGeant4Metadata(const char* configFilename, const strin Initialize(); LoadConfigFromFile(fConfigFileName, name); - - PrintMetadata(); } /////////////////////////////////////////////// From c7a3070a138680a41b2bc7ba58fb289c9ce71ddf Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Wed, 27 Jul 2022 21:01:12 +0200 Subject: [PATCH 056/100] Added mising update to TRestGeant4Metadata.h related to -S in restG4 --- inc/TRestGeant4Metadata.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 6ec68ba..7f473a5 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -128,6 +128,9 @@ class TRestGeant4Metadata : public TRestMetadata { /// The number of events the user desired to be on the file Int_t fNDesiredEntries = 0; + /// Time before simulation is ended and saved + Int_t fSimulationMaxTimeSeconds = 0; + /// \brief The seed value used for Geant4 random event generator. /// If it is zero its value will be assigned using the system timestamp. Long_t fSeed = 0; @@ -233,6 +236,8 @@ class TRestGeant4Metadata : public TRestMetadata { inline Int_t GetNumberOfDesiredEntries() const { return fNDesiredEntries; } + inline Int_t GetSimulationMaxTimeSeconds() const { return fSimulationMaxTimeSeconds; } + /////////////////////////////////////////////////////////// // Direct access to sources definition in primary generator @@ -270,6 +275,8 @@ class TRestGeant4Metadata : public TRestMetadata { inline void SetNumberOfDesiredEntries(Int_t n) { fNDesiredEntries = n; } + inline void SetSimulationMaxTimeSeconds(Int_t seconds) { fSimulationMaxTimeSeconds = seconds; } + /// Sets the name of the sensitive volume inline void SetSensitiveVolume(const TString& sensitiveVolume) { fSensitiveVolume = sensitiveVolume; } From 30a95b9178507554a1f804c7eeca5970e0272841 Mon Sep 17 00:00:00 2001 From: lobis Date: Thu, 28 Jul 2022 13:20:54 +0200 Subject: [PATCH 057/100] Added reference for cosmic neutrons dist --- src/TRestGeant4PrimaryGeneratorInfo.cxx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/TRestGeant4PrimaryGeneratorInfo.cxx b/src/TRestGeant4PrimaryGeneratorInfo.cxx index f23378a..2d2b2cd 100644 --- a/src/TRestGeant4PrimaryGeneratorInfo.cxx +++ b/src/TRestGeant4PrimaryGeneratorInfo.cxx @@ -188,6 +188,7 @@ TF1 TRestGeant4PrimaryGeneratorTypes::EnergyDistributionFormulasToRootFormula( case EnergyDistributionFormulas::COSMIC_MUONS: exit(1); case EnergyDistributionFormulas::COSMIC_NEUTRONS: { + // Formula from https://ieeexplore.ieee.org/document/1369506 const char* title = "EnergyDistribution: Cosmic Neutrons Sea Level"; auto f = TF1(title, "1.006E-6 * TMath::Exp(-0.3500 * TMath::Power(TMath::Log(x * 1E-3), 2) + 2.1451 * " From 7de33821ea7582127813a1f01abe5132cc54e97e Mon Sep 17 00:00:00 2001 From: lobis Date: Thu, 28 Jul 2022 14:01:22 +0200 Subject: [PATCH 058/100] Added information about distributions --- src/TRestGeant4PrimaryGeneratorInfo.cxx | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/TRestGeant4PrimaryGeneratorInfo.cxx b/src/TRestGeant4PrimaryGeneratorInfo.cxx index 2d2b2cd..1433051 100644 --- a/src/TRestGeant4PrimaryGeneratorInfo.cxx +++ b/src/TRestGeant4PrimaryGeneratorInfo.cxx @@ -4,6 +4,7 @@ #include "TRestGeant4PrimaryGeneratorInfo.h" +#include #include #include #include @@ -189,15 +190,18 @@ TF1 TRestGeant4PrimaryGeneratorTypes::EnergyDistributionFormulasToRootFormula( exit(1); case EnergyDistributionFormulas::COSMIC_NEUTRONS: { // Formula from https://ieeexplore.ieee.org/document/1369506 - const char* title = "EnergyDistribution: Cosmic Neutrons Sea Level"; - auto f = TF1(title, - "1.006E-6 * TMath::Exp(-0.3500 * TMath::Power(TMath::Log(x * 1E-3), 2) + 2.1451 * " - "TMath::Log(x * 1E-3)) + " - "1.011E-3 * TMath::Exp(-0.4106 * TMath::Power(TMath::Log(x * 1E-3), 2) - 0.6670 * " - "TMath::Log(x * 1E-3))", - 1E2, 1E7); - f.SetTitle(title); - return f; + const char* title = "Cosmic Neutrons at Sea Level"; + auto distribution = + TF1(title, + "1.006E-6 * TMath::Exp(-0.3500 * TMath::Power(TMath::Log(x * 1E-3), 2) + 2.1451 * " + "TMath::Log(x * 1E-3)) + " + "1.011E-3 * TMath::Exp(-0.4106 * TMath::Power(TMath::Log(x * 1E-3), 2) - 0.6670 * " + "TMath::Log(x * 1E-3))", + 1E2, 1E7); + distribution.SetNormalized(true); // Normalized, not really necessary + distribution.SetTitle(title); + distribution.GetXaxis()->SetTitle("Energy (keV)"); + return distribution; } case EnergyDistributionFormulas::COSMIC_GAMMAS: exit(1); From dafe6e45f8fb52a489e10d0d79b9bf1a6ba0132b Mon Sep 17 00:00:00 2001 From: lobis Date: Thu, 28 Jul 2022 18:59:33 +0200 Subject: [PATCH 059/100] cleaned up TRestGeant4GeometryInfo --- inc/TRestGeant4GeometryInfo.h | 5 ++++ src/TRestGeant4GeometryInfo.cxx | 43 ++++++++------------------------- 2 files changed, 15 insertions(+), 33 deletions(-) diff --git a/inc/TRestGeant4GeometryInfo.h b/inc/TRestGeant4GeometryInfo.h index 71cdf50..992e29d 100644 --- a/inc/TRestGeant4GeometryInfo.h +++ b/inc/TRestGeant4GeometryInfo.h @@ -46,6 +46,11 @@ class TRestGeant4GeometryInfo { void PopulateFromGeant4World(const G4VPhysicalVolume*); + inline void InitializeOnDetectorConstruction(const G4VPhysicalVolume* world, const TString& gdmlFilename) { + PopulateFromGdml(gdmlFilename); + PopulateFromGeant4World(world); + } + std::vector GetAllLogicalVolumes() const; std::vector GetAllPhysicalVolumes() const; std::vector GetAllAlternativePhysicalVolumes() const; diff --git a/src/TRestGeant4GeometryInfo.cxx b/src/TRestGeant4GeometryInfo.cxx index fa21188..2acbbe0 100644 --- a/src/TRestGeant4GeometryInfo.cxx +++ b/src/TRestGeant4GeometryInfo.cxx @@ -37,12 +37,6 @@ TString GetNodeAttribute(TXMLEngine xml, XMLNodePointer_t node, const TString& a void AddVolumesRecursively(vector* physicalNames, vector* logicalNames, const vector& children, map& nameTable, map>& childrenTable, const TString& name = "") { - /* - cout << "called AddVolumesRecursively with name: " << name << endl; - for (const auto& child : children) { - cout << "\t" << child << endl; - } - */ if (children.empty()) { physicalNames->push_back(name); const auto logicalVolumeName = nameTable[name]; @@ -117,16 +111,21 @@ void TRestGeant4GeometryInfo::PopulateFromGdml(const TString& gdmlFilename) { xml.FreeDoc(xmldoc); + // Checks + if (fGdmlNewPhysicalNames.empty()) { + cout << "TRestGeant4GeometryInfo::PopulateFromGdml - ERROR - No physical volumes have been added!" + << endl; + exit(1); + } // Find duplicates - size_t n = fGdmlNewPhysicalNames.size(); set s; for (const auto& name : fGdmlNewPhysicalNames) { s.insert(name); } - if (s.size() != n) { - cout - << "TRestGeant4GeometryInfo::PopulateFromGdml - Generated a duplicate name, please check assembly" - << endl; + if (s.size() != fGdmlNewPhysicalNames.size()) { + cout << "TRestGeant4GeometryInfo::PopulateFromGdml - ERROR - Generated a duplicate name, please " + "check assembly" + << endl; exit(1); } } @@ -149,28 +148,6 @@ TString TRestGeant4GeometryInfo::GetGeant4PhysicalNameFromAlternativeName( return ""; } -/* - * DO NOT REMOVE! -Int_t TRestGeant4GeometryInfo::GetIDFromVolumeName(const TString& volumeName) const { - for (int i = 0; i < fGdmlNewPhysicalNames.size(); i++) { - if (volumeName.EqualTo(fGdmlNewPhysicalNames[i])) { - return i; - } - } - - int i = 0; - for (const auto& physical : GetAllPhysicalVolumes()) { - if (volumeName.EqualTo(physical)) { - return i; - } - i++; - } - - cout << "TRestGeant4GeometryInfo::GetIDFromPhysicalName - ID not found for " << volumeName << endl; - exit(1); -} -*/ - template U GetOrDefaultMapValueFromKey(const map* pMap, const T& key) { if (pMap->count(key) > 0) { From 1e90a1aa794c1e0346c18f02036ecfc6c2e89cbc Mon Sep 17 00:00:00 2001 From: lobis Date: Thu, 28 Jul 2022 19:00:08 +0200 Subject: [PATCH 060/100] no longer throwing error when `nEvents` is not specified on TRestGeant4Metadata (error is on restG4) --- src/TRestGeant4Metadata.cxx | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index d020f07..1daf835 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -761,11 +761,7 @@ void TRestGeant4Metadata::InitFromConfigFile() { if (nEventsString == PARAMETER_NOT_FOUND_STR) { nEventsString = GetParameter("Nevents"); // old name } - if (nEventsString == PARAMETER_NOT_FOUND_STR) { - cout << "\"nEvents\" parameter is not defined!" << endl; - exit(1); - } - fNEvents = StringToInteger(nEventsString); + fNEvents = nEventsString == PARAMETER_NOT_FOUND_STR ? 0 : StringToInteger(nEventsString); fSaveAllEvents = ToUpper(GetParameter("saveAllEvents", "false")) == "TRUE" || ToUpper(GetParameter("saveAllEvents", "off")) == "ON"; From d916e7ef9f1a94dd0738d3a4b8d9d787798916c2 Mon Sep 17 00:00:00 2001 From: lobis Date: Thu, 28 Jul 2022 19:02:11 +0200 Subject: [PATCH 061/100] TRestGeant4GeometryInfo - fixed small bug --- inc/TRestGeant4GeometryInfo.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/inc/TRestGeant4GeometryInfo.h b/inc/TRestGeant4GeometryInfo.h index 992e29d..cbc7d3c 100644 --- a/inc/TRestGeant4GeometryInfo.h +++ b/inc/TRestGeant4GeometryInfo.h @@ -46,7 +46,8 @@ class TRestGeant4GeometryInfo { void PopulateFromGeant4World(const G4VPhysicalVolume*); - inline void InitializeOnDetectorConstruction(const G4VPhysicalVolume* world, const TString& gdmlFilename) { + inline void InitializeOnDetectorConstruction(const TString& gdmlFilename, + const G4VPhysicalVolume* world) { PopulateFromGdml(gdmlFilename); PopulateFromGeant4World(world); } From 2b3101d06848215797a5b2cf5803ee79b6319086 Mon Sep 17 00:00:00 2001 From: lobis Date: Thu, 28 Jul 2022 19:12:13 +0200 Subject: [PATCH 062/100] Fixed bug in assembly geometry --- inc/TRestGeant4GeometryInfo.h | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/inc/TRestGeant4GeometryInfo.h b/inc/TRestGeant4GeometryInfo.h index cbc7d3c..3208219 100644 --- a/inc/TRestGeant4GeometryInfo.h +++ b/inc/TRestGeant4GeometryInfo.h @@ -20,6 +20,14 @@ class TRestGeant4GeometryInfo { std::map fVolumeNameMap = {}; std::map fVolumeNameReverseMap = {}; + void PopulateFromGeant4World(const G4VPhysicalVolume*); + + inline void InitializeOnDetectorConstruction(const TString& gdmlFilename, + const G4VPhysicalVolume* world) { + PopulateFromGdml(gdmlFilename); + PopulateFromGeant4World(world); + } + public: // Insertion order is important for GDML containers. These containers are filled from GDML only not Geant4 std::vector fGdmlNewPhysicalNames; @@ -44,14 +52,6 @@ class TRestGeant4GeometryInfo { TString GetAlternativeNameFromGeant4PhysicalName(const TString&) const; TString GetGeant4PhysicalNameFromAlternativeName(const TString&) const; - void PopulateFromGeant4World(const G4VPhysicalVolume*); - - inline void InitializeOnDetectorConstruction(const TString& gdmlFilename, - const G4VPhysicalVolume* world) { - PopulateFromGdml(gdmlFilename); - PopulateFromGeant4World(world); - } - std::vector GetAllLogicalVolumes() const; std::vector GetAllPhysicalVolumes() const; std::vector GetAllAlternativePhysicalVolumes() const; @@ -89,6 +89,8 @@ class TRestGeant4GeometryInfo { Int_t GetIDFromVolume(const TString& volumeName) const; void Print() const; + + friend class DetectorConstruction; }; #endif // REST_TRESTGEANT4GEOMETRYINFO_H From 0d1c4be1c009156904a4c7477284fdecb88b000a Mon Sep 17 00:00:00 2001 From: lobis Date: Fri, 29 Jul 2022 00:19:40 +0200 Subject: [PATCH 063/100] Fixed bug introduced for assembly geometry --- src/TRestGeant4GeometryInfo.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TRestGeant4GeometryInfo.cxx b/src/TRestGeant4GeometryInfo.cxx index 2acbbe0..1b0f406 100644 --- a/src/TRestGeant4GeometryInfo.cxx +++ b/src/TRestGeant4GeometryInfo.cxx @@ -181,9 +181,9 @@ void TRestGeant4GeometryInfo::Print() const { const auto physicalVolumes = GetAllPhysicalVolumes(); cout << "Physical volumes (" << physicalVolumes.size() << "):" << endl; for (const auto& physical : GetAllPhysicalVolumes()) { - auto newName = GetAlternativeNameFromGeant4PhysicalName(physical); + auto geant4Name = GetGeant4PhysicalNameFromAlternativeName(physical); const auto logical = fPhysicalToLogicalVolumeMap.at(physical); - cout << "\t- " << (newName == physical ? physical : newName + " (" + physical + ")") + cout << "\t- " << (geant4Name == physical ? physical : physical + " (" + geant4Name + ")") << " - ID: " << GetIDFromVolume(physical) << " - Logical: " << fPhysicalToLogicalVolumeMap.at(physical) << " - Material: " << fLogicalToMaterialMap.at(logical) << endl; From bacdea4435308489adc0b7bd75b3e731d5f7d3db Mon Sep 17 00:00:00 2001 From: lobis Date: Fri, 29 Jul 2022 16:32:41 +0200 Subject: [PATCH 064/100] Starting to support multiple sensitive volumes --- inc/TRestGeant4Metadata.h | 22 +++++++++++++++++----- src/TRestGeant4Metadata.cxx | 18 ++++++++---------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 7f473a5..6b11d24 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -53,7 +53,7 @@ class TRestGeant4Metadata : public TRestMetadata { void ReadGenerator(); void ReadParticleSource(TRestGeant4ParticleSource* src, TiXmlElement* sourceDefinition); - void ReadStorage(); + void ReadDetector(); void ReadBiasing(); /// Class used to store and retrieve geometry info @@ -100,7 +100,7 @@ class TRestGeant4Metadata : public TRestMetadata { /// \brief The number of biasing volumes used in the simulation. If zero, no biasing /// technique is used. - Int_t fNBiasingVolumes; + Int_t fNBiasingVolumes = 0; /// A std::vector containing the biasing volume properties. std::vector fBiasingVolumes; @@ -120,7 +120,7 @@ class TRestGeant4Metadata : public TRestMetadata { /// \brief The volume that serves as trigger for data storage. Only events that /// deposit a non-zero energy on this volume will be registered. - TString fSensitiveVolume; + std::vector fSensitiveVolumes; /// The number of events simulated, or to be simulated. Int_t fNEvents = 0; @@ -268,7 +268,11 @@ class TRestGeant4Metadata : public TRestMetadata { inline Int_t isBiasingActive() const { return fBiasingVolumes.size(); } /// Returns a std::string with the name of the sensitive volume. - inline TString GetSensitiveVolume() const { return fSensitiveVolume; } + inline TString GetSensitiveVolume(int n = 0) const { return fSensitiveVolumes[n]; } + + inline size_t GetNumberOfSensitiveVolumes() const { return fSensitiveVolumes.size(); } + + inline const std::vector& GetSensitiveVolumes() const { return fSensitiveVolumes; } /// Sets the name of the sensitive volume inline void SetNumberOfEvents(Int_t n) { fNEvents = n; } @@ -278,7 +282,15 @@ class TRestGeant4Metadata : public TRestMetadata { inline void SetSimulationMaxTimeSeconds(Int_t seconds) { fSimulationMaxTimeSeconds = seconds; } /// Sets the name of the sensitive volume - inline void SetSensitiveVolume(const TString& sensitiveVolume) { fSensitiveVolume = sensitiveVolume; } + inline void InsertSensitiveVolume(const TString& sensitiveVolume) { + for (const auto& sensitiveVolume : fSensitiveVolumes) { + // Do not add duplicate volumes + if (sensitiveVolume == sensitiveVolume) { + return; + } + } + fSensitiveVolumes.push_back(sensitiveVolume); + } /// \brief Returns the probability per event to register (write to disk) hits in the /// storage volume with index n. diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 1daf835..fc11d04 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -703,12 +703,8 @@ void TRestGeant4Metadata::Initialize() { fActiveVolumes.clear(); fBiasingVolumes.clear(); - fNBiasingVolumes = 0; - RemoveParticleSources(); - fSensitiveVolume = "gas"; - fEnergyRangeStored.Set(0, 1.E20); } @@ -774,7 +770,7 @@ void TRestGeant4Metadata::InitFromConfigFile() { ReadGenerator(); - ReadStorage(); + ReadDetector(); ReadBiasing(); @@ -1050,17 +1046,19 @@ void TRestGeant4Metadata::AddParticleSource(TRestGeant4ParticleSource* src) { /// /// \endcode /// -void TRestGeant4Metadata::ReadStorage() { +void TRestGeant4Metadata::ReadDetector() { TiXmlElement* storageDefinition = GetElement("storage"); - fSensitiveVolume = GetFieldValue("sensitiveVolume", storageDefinition); - if (fSensitiveVolume == "Not defined") { + string sensitiveVolume = GetFieldValue("sensitiveVolume", storageDefinition); + if (sensitiveVolume == "Not defined") { RESTWarning << "Sensitive volume not defined. Setting it to 'gas'!" << RESTendl; - fSensitiveVolume = "gas"; + sensitiveVolume = "gas"; } + InsertSensitiveVolume(sensitiveVolume); + Double_t defaultStep = GetDblParameterWithUnits("maxStepSize", storageDefinition); if (defaultStep < 0) defaultStep = 0; - RESTInfo << "Sensitive volume: " << fSensitiveVolume << RESTendl; + RESTInfo << "Sensitive volume: " << sensitiveVolume << RESTendl; fEnergyRangeStored = Get2DVectorParameterWithUnits("energyRange", storageDefinition); // TODO: Place this as a generic method From 3be74e9de4bd471c8c1447dc428ae92d876daa91 Mon Sep 17 00:00:00 2001 From: lobis Date: Fri, 29 Jul 2022 19:01:28 +0200 Subject: [PATCH 065/100] Updated storage section tu support multiple sensitive volumes (work in progress) --- inc/TRestGeant4Metadata.h | 11 ++- src/TRestGeant4Metadata.cxx | 174 +++++++++++++++++++++++------------- 2 files changed, 118 insertions(+), 67 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 6b11d24..ef20a47 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -56,6 +56,8 @@ class TRestGeant4Metadata : public TRestMetadata { void ReadDetector(); void ReadBiasing(); + bool fDetectorSectionInitialized = false; //! + /// Class used to store and retrieve geometry info TRestGeant4GeometryInfo fGeant4GeometryInfo; @@ -139,6 +141,9 @@ class TRestGeant4Metadata : public TRestMetadata { /// sensitive volume (used for debugging purposes). It is set to 'false' by default. Bool_t fSaveAllEvents = false; + /// \brief Sets all volume as active without having to explicitly list them. + Bool_t fActivateAllVolumes = true; //! + /// If this parameter is set to 'true' it will print out on screen every time 10k events are reached. Bool_t fPrintProgress = false; //! @@ -282,14 +287,14 @@ class TRestGeant4Metadata : public TRestMetadata { inline void SetSimulationMaxTimeSeconds(Int_t seconds) { fSimulationMaxTimeSeconds = seconds; } /// Sets the name of the sensitive volume - inline void InsertSensitiveVolume(const TString& sensitiveVolume) { + inline void InsertSensitiveVolume(const TString& volume) { for (const auto& sensitiveVolume : fSensitiveVolumes) { // Do not add duplicate volumes - if (sensitiveVolume == sensitiveVolume) { + if (volume == sensitiveVolume) { return; } } - fSensitiveVolumes.push_back(sensitiveVolume); + fSensitiveVolumes.push_back(volume); } /// \brief Returns the probability per event to register (write to disk) hits in the diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index fc11d04..ea54857 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -769,9 +769,8 @@ void TRestGeant4Metadata::InitFromConfigFile() { ToUpper(GetParameter("registerEmptyTracks", "off")) == "ON"; ReadGenerator(); - - ReadDetector(); - + // Detector (old storage) section is processed after initializing geometry info in Detector Construction + // This allows to use regular expression to match logical or physical volumes etc. ReadBiasing(); fMaxTargetStepSize = GetDblParameterWithUnits("maxTargetStepSize", -1); @@ -1038,29 +1037,105 @@ void TRestGeant4Metadata::AddParticleSource(TRestGeant4ParticleSource* src) { /// /// This section allows to define which hits will be stored to disk. /// Different volumes in the geometry can be tagged for hit storage. +/// At least one volume must be tagged as sensitive /// /// \code -/// +/// /// -/// -/// +/// +/// /// \endcode /// void TRestGeant4Metadata::ReadDetector() { - TiXmlElement* storageDefinition = GetElement("storage"); - string sensitiveVolume = GetFieldValue("sensitiveVolume", storageDefinition); - if (sensitiveVolume == "Not defined") { - RESTWarning << "Sensitive volume not defined. Setting it to 'gas'!" << RESTendl; - sensitiveVolume = "gas"; + RESTInfo << "TRestGeant4Metadata: Processing detector section" << RESTendl; + TiXmlElement* detectorDefinition = GetElement("detector"); + if (detectorDefinition == nullptr) { + RESTError << "Detector section () is not present in metadata!" << RESTendl; + exit(1); } - InsertSensitiveVolume(sensitiveVolume); - Double_t defaultStep = GetDblParameterWithUnits("maxStepSize", storageDefinition); - if (defaultStep < 0) defaultStep = 0; + const bool activateAllVolumes = + StringToBool(GetParameter("activateAllVolumes", detectorDefinition, "false")); + RESTInfo << "TRestGeant4Metadata: Setting 'fActivateAllVolumes' to " << fActivateAllVolumes << RESTendl; + fActivateAllVolumes = activateAllVolumes; - RESTInfo << "Sensitive volume: " << sensitiveVolume << RESTendl; + Double_t defaultStep = GetDblParameterWithUnits("maxStepSize", detectorDefinition); + if (defaultStep < 0) { + defaultStep = 0; + } - fEnergyRangeStored = Get2DVectorParameterWithUnits("energyRange", storageDefinition); + TiXmlElement* volumeDefinition = GetElement("volume", detectorDefinition); + while (volumeDefinition != nullptr) { + string name = GetFieldValue("name", volumeDefinition); + if (name.empty() || name == "Not defined") { + RESTError << "volume inside detector section defined without name" << RESTendl; + exit(1); + } + vector physicalVolumes; + if (!fGeant4GeometryInfo.IsValidGdmlName(name)) { + if (fGeant4GeometryInfo.IsValidLogicalVolume(name)) { + for (const auto& physical : fGeant4GeometryInfo.GetAllPhysicalVolumesFromLogical(name)) { + physicalVolumes.emplace_back( + fGeant4GeometryInfo.GetAlternativeNameFromGeant4PhysicalName(physical)); + } + } + // does not match a explicit (gdml) physical name or a logical name, perhaps its a regular exp + if (physicalVolumes.empty()) { + for (const auto& physical : + fGeant4GeometryInfo.GetAllPhysicalVolumesMatchingExpression(name)) { + physicalVolumes.emplace_back( + fGeant4GeometryInfo.GetAlternativeNameFromGeant4PhysicalName(physical)); + } + } + if (physicalVolumes.empty()) { + for (const auto& logical : fGeant4GeometryInfo.GetAllLogicalVolumesMatchingExpression(name)) { + for (const auto& physical : + fGeant4GeometryInfo.GetAllPhysicalVolumesFromLogical(logical)) { + physicalVolumes.emplace_back( + fGeant4GeometryInfo.GetAlternativeNameFromGeant4PhysicalName(physical)); + } + } + } + } else { + physicalVolumes.push_back(name); + } + + if (physicalVolumes.empty()) { + RESTError + << "volume '" << name + << "' is not defined in the geometry. Could not match to a physical, logical volume or regex" + << RESTendl; + exit(1); + } + + bool isSensitive = StringToBool(GetFieldValue("sensitive", volumeDefinition)); + + Double_t chance = StringToDouble(GetFieldValue("chance", volumeDefinition)); + if (chance == -1 || chance < 0) { + chance = 1.0; + } + Double_t maxStep = GetDblParameterWithUnits("maxStepSize", volumeDefinition); + if (maxStep < 0) { + maxStep = defaultStep; + } + + for (const auto& physical : physicalVolumes) { + RESTInfo << "Adding " << (isSensitive ? "sensitive" : "active") << " volume from RML: '" + << physical << (chance != 1 ? " with change: " + to_string(chance) : " ") << RESTendl; + SetActiveVolume(physical, chance, maxStep); + if (isSensitive) { + InsertSensitiveVolume(physical); + } + } + volumeDefinition = GetNextElement(detectorDefinition); + } + + if (fSensitiveVolumes.empty()) { + cout << "No sensitive volumes defined in detector section. Adding 'gas' as sensitive volume" << endl; + InsertSensitiveVolume("gas"); + } + + fEnergyRangeStored = Get2DVectorParameterWithUnits("energyRange", detectorDefinition); // TODO: Place this as a generic method if (fEnergyRangeStored.X() < 0) { fEnergyRangeStored.Set(0, fEnergyRangeStored.Y()); @@ -1077,49 +1152,16 @@ void TRestGeant4Metadata::ReadDetector() { fGeant4GeometryInfo.PopulateFromGdml(gdml->GetOutputGDMLFile()); - const auto& physicalVolumes = fGeant4GeometryInfo.fGdmlNewPhysicalNames; - TiXmlElement* volumeDefinition = GetElement("activeVolume", storageDefinition); - while (volumeDefinition) { - Double_t chance = StringToDouble(GetFieldValue("chance", volumeDefinition)); - if (chance == -1) chance = 1; - - Double_t maxStep = GetDblParameterWithUnits("maxStepSize", volumeDefinition); - if (maxStep < 0) maxStep = defaultStep; - - const TString& name = GetFieldValue("name", volumeDefinition); - // first we verify it's in the list of valid volumes - if (!fGeant4GeometryInfo.IsValidGdmlName(name)) { - bool isValidLogical = false; - for (size_t i = 0; i < fGeant4GeometryInfo.fGdmlNewPhysicalNames.size(); i++) { - if (fGeant4GeometryInfo.fGdmlLogicalNames[i] == name) { - isValidLogical = true; - const auto& gdmlName = fGeant4GeometryInfo.fGdmlNewPhysicalNames[i]; - RESTInfo << "Adding active volume from RML: '" << gdmlName << "' from logical volume: '" - << name << "' with chance: " << chance << RESTendl; - SetActiveVolume(gdmlName, chance, maxStep); - } - } - if (!isValidLogical) { - RESTError << "TRestGeant4Metadata: Problem reading storage section." << RESTendl; - RESTError << " - The volume '" << name << "' was not found in the GDML geometry." - << RESTendl; - exit(1); - } - } else { - RESTInfo << "Adding active volume from RML: '" << name << "' with chance: " << chance << RESTendl; - SetActiveVolume(name, chance, maxStep); - } - volumeDefinition = GetNextElement(volumeDefinition); - } - // If the user did not add explicitly any volume to the storage section we understand // the user wants to register all the volumes - if (GetNumberOfActiveVolumes() == 0) { - for (auto& name : physicalVolumes) { + if (fActivateAllVolumes || GetNumberOfActiveVolumes() == 0) { + for (auto& name : fGeant4GeometryInfo.fGdmlNewPhysicalNames) { SetActiveVolume(name, 1, defaultStep); RESTInfo << "Automatically adding active volume: '" << name << "' with chance: " << 1 << RESTendl; } } + + fDetectorSectionInitialized = true; } /////////////////////////////////////////////// @@ -1150,17 +1192,21 @@ void TRestGeant4Metadata::PrintMetadata() { for (int i = 0; i < GetNumberOfSources(); i++) GetParticleSource(i)->PrintParticleSource(); - RESTMetadata << " ++++++++++ Storage volumes +++++++++++ " << RESTendl; - RESTMetadata << "Energy range (keV): (" << GetMinimumEnergyStored() << ", " << GetMaximumEnergyStored() - << ")" << RESTendl; - RESTMetadata << "Sensitive volume: " << GetSensitiveVolume() << RESTendl; - RESTMetadata << "Active volumes: " << GetNumberOfActiveVolumes() << RESTendl; - RESTMetadata << "---------------------------------------" << RESTendl; - for (int n = 0; n < GetNumberOfActiveVolumes(); n++) { - RESTMetadata << "Name: " << GetActiveVolumeName(n) - << ", ID: " << GetActiveVolumeID(GetActiveVolumeName(n)) - << ", maxStep: " << GetMaxStepSize(GetActiveVolumeName(n)) << "mm " - << ", chance: " << GetStorageChance(GetActiveVolumeName(n)) << RESTendl; + if (fDetectorSectionInitialized) { + RESTMetadata << " ++++++++++ Detector +++++++++++ " << RESTendl; + RESTMetadata << "Energy range (keV): (" << GetMinimumEnergyStored() << ", " + << GetMaximumEnergyStored() << ")" << RESTendl; + RESTMetadata << "Number of sensitive volumes: " << GetNumberOfSensitiveVolumes() << RESTendl; + for (const auto& sensitiveVolume : fSensitiveVolumes) { + RESTMetadata << "Sensitive volume: " << sensitiveVolume << RESTendl; + } + RESTMetadata << "Number of active volumes: " << GetNumberOfActiveVolumes() << RESTendl; + for (int n = 0; n < GetNumberOfActiveVolumes(); n++) { + RESTMetadata << "Name: " << GetActiveVolumeName(n) + << ", ID: " << GetActiveVolumeID(GetActiveVolumeName(n)) + << ", maxStep: " << GetMaxStepSize(GetActiveVolumeName(n)) << "mm " + << ", chance: " << GetStorageChance(GetActiveVolumeName(n)) << RESTendl; + } } for (int n = 0; n < GetNumberOfBiasingVolumes(); n++) { GetBiasingVolume(n).PrintBiasingVolume(); From 4ecfc4c9770018a2cc4eff3f840e00c2465d6d63 Mon Sep 17 00:00:00 2001 From: lobis Date: Fri, 29 Jul 2022 20:14:26 +0200 Subject: [PATCH 066/100] Fixed bug with strings in active volume check --- inc/TRestGeant4Metadata.h | 14 +++++++++++--- src/TRestGeant4Metadata.cxx | 8 +++++--- test/src/TRestGeant4Metadata.cxx | 1 - 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index ef20a47..af8a8f6 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -142,7 +142,7 @@ class TRestGeant4Metadata : public TRestMetadata { Bool_t fSaveAllEvents = false; /// \brief Sets all volume as active without having to explicitly list them. - Bool_t fActivateAllVolumes = true; //! + Bool_t fActivateAllVolumes = false; //! /// If this parameter is set to 'true' it will print out on screen every time 10k events are reached. Bool_t fPrintProgress = false; //! @@ -156,6 +156,8 @@ class TRestGeant4Metadata : public TRestMetadata { TVector3 fMagneticField = TVector3(0, 0, 0); public: + std::set fActiveVolumesSet = {}; //! // Used for faster lookup + /// \brief Returns the random seed that was used to generate the corresponding /// geant4 dataset. inline Long_t GetSeed() const { return fSeed; } @@ -317,15 +319,21 @@ class TRestGeant4Metadata : public TRestMetadata { /// selected for data storage. inline Int_t GetNumberOfActiveVolumes() const { return fActiveVolumes.size(); } + inline bool IsActiveVolume(const char* volumeName) const { + return fActiveVolumesSet.count(volumeName) > 0; + } //! + /// Returns a std::string with the name of the active volume with index n - inline TString GetActiveVolumeName(Int_t n) { return fActiveVolumes[n]; } + inline TString GetActiveVolumeName(Int_t n) const { return fActiveVolumes[n]; } + + inline std::vector GetActiveVolumes() const { return fActiveVolumes; } /// Returns the world magnetic field in Tesla inline TVector3 GetMagneticField() const { return fMagneticField; } Int_t GetActiveVolumeID(TString name); - Bool_t isVolumeStored(TString volume); + Bool_t isVolumeStored(const TString& volume) const; void SetActiveVolume(const TString& name, Double_t chance, Double_t maxStep = 0); diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index ea54857..eddb15c 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1120,8 +1120,8 @@ void TRestGeant4Metadata::ReadDetector() { } for (const auto& physical : physicalVolumes) { - RESTInfo << "Adding " << (isSensitive ? "sensitive" : "active") << " volume from RML: '" - << physical << (chance != 1 ? " with change: " + to_string(chance) : " ") << RESTendl; + RESTInfo << "Adding " << (isSensitive ? "sensitive" : "active") << " volume from RML: '" << physical + << (chance != 1 ? " with change: " + to_string(chance) : " ") << RESTendl; SetActiveVolume(physical, chance, maxStep); if (isSensitive) { InsertSensitiveVolume(physical); @@ -1250,13 +1250,15 @@ void TRestGeant4Metadata::SetActiveVolume(const TString& name, Double_t chance, fActiveVolumes.push_back(name); fChance.push_back(chance); fMaxStepSize.push_back(maxStep); + + fActiveVolumesSet.insert(nameC); } /////////////////////////////////////////////// /// \brief Returns true if the volume named *volName* has been registered for /// data storage. /// -Bool_t TRestGeant4Metadata::isVolumeStored(TString volume) { +Bool_t TRestGeant4Metadata::isVolumeStored(const TString& volume) const { for (int n = 0; n < GetNumberOfActiveVolumes(); n++) if (GetActiveVolumeName(n) == volume) return true; diff --git a/test/src/TRestGeant4Metadata.cxx b/test/src/TRestGeant4Metadata.cxx index bb45723..ddb10a1 100644 --- a/test/src/TRestGeant4Metadata.cxx +++ b/test/src/TRestGeant4Metadata.cxx @@ -29,7 +29,6 @@ TEST(TRestGeant4Metadata, Default) { restGeant4Metadata.PrintMetadata(); EXPECT_TRUE(restGeant4Metadata.GetSeed() == 0); - EXPECT_TRUE(restGeant4Metadata.GetSensitiveVolume() == "gas"); } TEST(TRestGeant4Metadata, FromRml) { From f8a32ea44996fc090a91c316e7a9715e052feeec Mon Sep 17 00:00:00 2001 From: lobis Date: Fri, 29 Jul 2022 20:22:09 +0200 Subject: [PATCH 067/100] Fixed compilation error --- src/TRestGeant4Metadata.cxx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index eddb15c..92083d1 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1120,8 +1120,8 @@ void TRestGeant4Metadata::ReadDetector() { } for (const auto& physical : physicalVolumes) { - RESTInfo << "Adding " << (isSensitive ? "sensitive" : "active") << " volume from RML: '" << physical - << (chance != 1 ? " with change: " + to_string(chance) : " ") << RESTendl; + RESTInfo << "Adding " << (isSensitive ? "sensitive" : "active") << " volume from RML: '" + << physical << (chance != 1 ? " with change: " + to_string(chance) : " ") << RESTendl; SetActiveVolume(physical, chance, maxStep); if (isSensitive) { InsertSensitiveVolume(physical); @@ -1250,8 +1250,7 @@ void TRestGeant4Metadata::SetActiveVolume(const TString& name, Double_t chance, fActiveVolumes.push_back(name); fChance.push_back(chance); fMaxStepSize.push_back(maxStep); - - fActiveVolumesSet.insert(nameC); + fActiveVolumesSet.insert(name.Data()); } /////////////////////////////////////////////// From 9a4aa7356cea808a603abd905e24d60c44d53292 Mon Sep 17 00:00:00 2001 From: lobis Date: Fri, 29 Jul 2022 20:59:03 +0200 Subject: [PATCH 068/100] Fixed bug causing not to process multiple detector volumes --- src/TRestGeant4Metadata.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 92083d1..eaf7f8b 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1127,7 +1127,7 @@ void TRestGeant4Metadata::ReadDetector() { InsertSensitiveVolume(physical); } } - volumeDefinition = GetNextElement(detectorDefinition); + volumeDefinition = GetNextElement(volumeDefinition); } if (fSensitiveVolumes.empty()) { From e51183311d6ce344d1cc8204d40091984d1a97bf Mon Sep 17 00:00:00 2001 From: lobis Date: Fri, 29 Jul 2022 22:01:12 +0200 Subject: [PATCH 069/100] TRestGeant4Metadata - Added flags for remove unwanted tracks --- inc/TRestGeant4Metadata.h | 5 +++++ src/TRestGeant4Metadata.cxx | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index af8a8f6..217037f 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -144,6 +144,9 @@ class TRestGeant4Metadata : public TRestMetadata { /// \brief Sets all volume as active without having to explicitly list them. Bool_t fActivateAllVolumes = false; //! + /// \brief If activated will remove tracks not present in volumes marked as "keep" or "sensitive". + Bool_t fRemoveUnwantedTracks = false; + /// If this parameter is set to 'true' it will print out on screen every time 10k events are reached. Bool_t fPrintProgress = false; //! @@ -328,6 +331,8 @@ class TRestGeant4Metadata : public TRestMetadata { inline std::vector GetActiveVolumes() const { return fActiveVolumes; } + inline bool GetRemoveUnwantedTracks() const { return fRemoveUnwantedTracks; } + /// Returns the world magnetic field in Tesla inline TVector3 GetMagneticField() const { return fMagneticField; } diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index eaf7f8b..36962ba 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1059,6 +1059,11 @@ void TRestGeant4Metadata::ReadDetector() { RESTInfo << "TRestGeant4Metadata: Setting 'fActivateAllVolumes' to " << fActivateAllVolumes << RESTendl; fActivateAllVolumes = activateAllVolumes; + const bool removeUnwantedTracks = + StringToBool(GetParameter("removeUnwantedTracks", detectorDefinition, "false")); + RESTInfo << "TRestGeant4Metadata: Setting 'removeUnwantedTracks' to " << removeUnwantedTracks << RESTendl; + fRemoveUnwantedTracks = removeUnwantedTracks; + Double_t defaultStep = GetDblParameterWithUnits("maxStepSize", detectorDefinition); if (defaultStep < 0) { defaultStep = 0; @@ -1108,7 +1113,17 @@ void TRestGeant4Metadata::ReadDetector() { exit(1); } - bool isSensitive = StringToBool(GetFieldValue("sensitive", volumeDefinition)); + bool isSensitive = false; + const string isSensitiveValue = GetFieldValue("sensitive", volumeDefinition); + if (isSensitiveValue != "Not defined") { + isSensitive = StringToBool(isSensitiveValue); + } + + bool isKeepTracks = isSensitive; + const string isKeepTracksValue = GetFieldValue("keepTracks", volumeDefinition); + if (isKeepTracksValue != "Not defined") { + isKeepTracks = StringToBool(isKeepTracksValue); + } Double_t chance = StringToDouble(GetFieldValue("chance", volumeDefinition)); if (chance == -1 || chance < 0) { From a625a5691a6594cf66f63bb651bd74f3a058576b Mon Sep 17 00:00:00 2001 From: lobis Date: Fri, 29 Jul 2022 22:06:53 +0200 Subject: [PATCH 070/100] Initialization of containers for RemoveUnwantedTracks --- inc/TRestGeant4Metadata.h | 7 +++++++ src/TRestGeant4Metadata.cxx | 3 +++ 2 files changed, 10 insertions(+) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 217037f..fb89275 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -89,6 +89,9 @@ class TRestGeant4Metadata : public TRestMetadata { /// \brief A std::vector to store the names of the active volumes. std::vector fActiveVolumes; + /// \brief A container related to fRemoveUnwantedTracks. + std::set fKeepTracksVolumesSet; + /// \brief A std::vector to store the probability value to write to disk the hits in a /// particular event. std::vector fChance; @@ -326,6 +329,10 @@ class TRestGeant4Metadata : public TRestMetadata { return fActiveVolumesSet.count(volumeName) > 0; } //! + inline bool IsKeepTracksVolume(const char* volumeName) const { + return fKeepTracksVolumesSet.count(volumeName) > 0; + } + /// Returns a std::string with the name of the active volume with index n inline TString GetActiveVolumeName(Int_t n) const { return fActiveVolumes[n]; } diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 36962ba..db51ae9 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1141,6 +1141,9 @@ void TRestGeant4Metadata::ReadDetector() { if (isSensitive) { InsertSensitiveVolume(physical); } + if (fRemoveUnwantedTracks && isKeepTracks) { + fKeepTracksVolumesSet.insert(physical); + } } volumeDefinition = GetNextElement(volumeDefinition); } From ff9ec9f63b324620f34efc96def0639193fe3e82 Mon Sep 17 00:00:00 2001 From: lobis Date: Sat, 30 Jul 2022 21:01:13 +0200 Subject: [PATCH 071/100] Added extra option for RemoveUnwantedTracks, fixed possible issue in activating default volumes --- inc/TRestGeant4Metadata.h | 18 +++++++++++++----- src/TRestGeant4Metadata.cxx | 30 ++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index fb89275..99633b6 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -89,9 +89,6 @@ class TRestGeant4Metadata : public TRestMetadata { /// \brief A std::vector to store the names of the active volumes. std::vector fActiveVolumes; - /// \brief A container related to fRemoveUnwantedTracks. - std::set fKeepTracksVolumesSet; - /// \brief A std::vector to store the probability value to write to disk the hits in a /// particular event. std::vector fChance; @@ -150,6 +147,13 @@ class TRestGeant4Metadata : public TRestMetadata { /// \brief If activated will remove tracks not present in volumes marked as "keep" or "sensitive". Bool_t fRemoveUnwantedTracks = false; + /// \brief Option for 'removeUnwantedTracks', if enabled tracks with hits in volumes will be kept event if + /// they deposit zero energy (such as neutron captures) + Bool_t fRemoveUnwantedTracksKeepZeroEnergyTracks = false; + + /// \brief A container related to fRemoveUnwantedTracks. + std::set fRemoveUnwantedTracksVolumesToKeep; + /// If this parameter is set to 'true' it will print out on screen every time 10k events are reached. Bool_t fPrintProgress = false; //! @@ -313,7 +317,7 @@ class TRestGeant4Metadata : public TRestMetadata { /// GDML volume given its geometry name. Double_t GetStorageChance(TString volume); - Double_t GetMaxStepSize(TString volume); + Double_t GetMaxStepSize(const TString& volume); /// Returns the minimum event energy required for an event to be stored. inline Double_t GetMinimumEnergyStored() const { return fEnergyRangeStored.X(); } @@ -330,7 +334,7 @@ class TRestGeant4Metadata : public TRestMetadata { } //! inline bool IsKeepTracksVolume(const char* volumeName) const { - return fKeepTracksVolumesSet.count(volumeName) > 0; + return fRemoveUnwantedTracksVolumesToKeep.count(volumeName) > 0; } /// Returns a std::string with the name of the active volume with index n @@ -340,6 +344,10 @@ class TRestGeant4Metadata : public TRestMetadata { inline bool GetRemoveUnwantedTracks() const { return fRemoveUnwantedTracks; } + inline bool GetRemoveUnwantedTracksKeepZeroEnergyTracks() const { + return fRemoveUnwantedTracksKeepZeroEnergyTracks; + } + /// Returns the world magnetic field in Tesla inline TVector3 GetMagneticField() const { return fMagneticField; } diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index db51ae9..09829c9 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1059,10 +1059,15 @@ void TRestGeant4Metadata::ReadDetector() { RESTInfo << "TRestGeant4Metadata: Setting 'fActivateAllVolumes' to " << fActivateAllVolumes << RESTendl; fActivateAllVolumes = activateAllVolumes; - const bool removeUnwantedTracks = - StringToBool(GetParameter("removeUnwantedTracks", detectorDefinition, "false")); - RESTInfo << "TRestGeant4Metadata: Setting 'removeUnwantedTracks' to " << removeUnwantedTracks << RESTendl; - fRemoveUnwantedTracks = removeUnwantedTracks; + TiXmlElement* removeUnwantedTracksDefinition = GetElement("removeUnwantedTracks", detectorDefinition); + if (removeUnwantedTracksDefinition != nullptr) { + fRemoveUnwantedTracks = StringToBool(GetParameter("enabled", removeUnwantedTracksDefinition, "true")); + fRemoveUnwantedTracksKeepZeroEnergyTracks = + StringToBool(GetParameter("keepZeroEnergyTracks", removeUnwantedTracksDefinition, "false")); + RESTInfo << "TRestGeant4Metadata: Setting 'fRemoveUnwantedTracks' to " << fRemoveUnwantedTracks + << " with 'keepZeroEnergyTracks' option set to " << fRemoveUnwantedTracksKeepZeroEnergyTracks + << RESTendl; + } Double_t defaultStep = GetDblParameterWithUnits("maxStepSize", detectorDefinition); if (defaultStep < 0) { @@ -1142,7 +1147,7 @@ void TRestGeant4Metadata::ReadDetector() { InsertSensitiveVolume(physical); } if (fRemoveUnwantedTracks && isKeepTracks) { - fKeepTracksVolumesSet.insert(physical); + fRemoveUnwantedTracksVolumesToKeep.insert(physical); } } volumeDefinition = GetNextElement(volumeDefinition); @@ -1174,8 +1179,11 @@ void TRestGeant4Metadata::ReadDetector() { // the user wants to register all the volumes if (fActivateAllVolumes || GetNumberOfActiveVolumes() == 0) { for (auto& name : fGeant4GeometryInfo.fGdmlNewPhysicalNames) { - SetActiveVolume(name, 1, defaultStep); - RESTInfo << "Automatically adding active volume: '" << name << "' with chance: " << 1 << RESTendl; + if (!IsActiveVolume(name)) { + SetActiveVolume(name, 1, defaultStep); + RESTInfo << "Automatically adding active volume: '" << name << "' with chance: " << 1 + << RESTendl; + } } } @@ -1298,9 +1306,11 @@ Double_t TRestGeant4Metadata::GetStorageChance(TString volume) { /////////////////////////////////////////////// /// \brief Returns the maximum step at a particular active volume /// -Double_t TRestGeant4Metadata::GetMaxStepSize(TString volume) { - for (Int_t id = 0; id < (Int_t)fActiveVolumes.size(); id++) { - if (fActiveVolumes[id] == volume) return fMaxStepSize[id]; +Double_t TRestGeant4Metadata::GetMaxStepSize(const TString& volume) { + for (Int_t i = 0; i < (Int_t)fActiveVolumes.size(); i++) { + if (volume.EqualTo(fActiveVolumes[i], TString::kIgnoreCase)) { + return fMaxStepSize[i]; + } } RESTWarning << "TRestGeant4Metadata::GetMaxStepSize. Volume " << volume << " not found" << RESTendl; From 013609eaa19ca9c23030e04266b9e72f973e26b1 Mon Sep 17 00:00:00 2001 From: lobis Date: Sat, 30 Jul 2022 22:44:10 +0200 Subject: [PATCH 072/100] Fixed bug with default energy range --- inc/TRestGeant4Metadata.h | 2 +- src/TRestGeant4Metadata.cxx | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 99633b6..90c0589 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -84,7 +84,7 @@ class TRestGeant4Metadata : public TRestMetadata { /// \brief A 2d-std::vector storing the energy range, in keV, to decide if a particular /// event should be written to disk or not. - TVector2 fEnergyRangeStored; + TVector2 fEnergyRangeStored = {0, 1E20}; /// \brief A std::vector to store the names of the active volumes. std::vector fActiveVolumes; diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 09829c9..31e90d6 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -704,8 +704,6 @@ void TRestGeant4Metadata::Initialize() { fBiasingVolumes.clear(); RemoveParticleSources(); - - fEnergyRangeStored.Set(0, 1.E20); } /////////////////////////////////////////////// @@ -1158,7 +1156,7 @@ void TRestGeant4Metadata::ReadDetector() { InsertSensitiveVolume("gas"); } - fEnergyRangeStored = Get2DVectorParameterWithUnits("energyRange", detectorDefinition); + fEnergyRangeStored = Get2DVectorParameterWithUnits("energyRange", detectorDefinition, fEnergyRangeStored); // TODO: Place this as a generic method if (fEnergyRangeStored.X() < 0) { fEnergyRangeStored.Set(0, fEnergyRangeStored.Y()); @@ -1169,6 +1167,11 @@ void TRestGeant4Metadata::ReadDetector() { if (fEnergyRangeStored.Y() > fEnergyRangeStored.Y()) { fEnergyRangeStored.Set(fEnergyRangeStored.Y(), fEnergyRangeStored.X()); } + if (fEnergyRangeStored.Y() <= 0) { + RESTError << "Energy range is not valid: (" << fEnergyRangeStored.X() << ", " + << fEnergyRangeStored.Y() << ") keV" << RESTendl; + exit(1); + } auto gdml = new TRestGDMLParser(); gdml->Load(GetGdmlFilename().Data()); From 9e3649745fa9a9f9c17c9bf8fdfb78160adc4844 Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Mon, 1 Aug 2022 03:06:05 +0200 Subject: [PATCH 073/100] Updated some Event and Track print using fmt --- src/TRestGeant4Event.cxx | 56 ++++++++++++++++++-------------------- src/TRestGeant4Track.cxx | 59 ++++++++++++++++++++++++---------------- 2 files changed, 63 insertions(+), 52 deletions(-) diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index 907b349..abb69f5 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -22,10 +22,13 @@ #include #include #include +#include +#include #include "TRestGeant4Metadata.h" using namespace std; +using namespace fmt; ClassImp(TRestGeant4Event); @@ -1119,47 +1122,42 @@ void TRestGeant4Event::PrintActiveVolumes() const { } } +template <> +struct fmt::formatter : formatter { + auto format(TVector3 c, format_context& ctx) { + string s = fmt::format("({:0.3f}, {:0.3f}, {:0.3f})", c.X(), c.Y(), c.Z()); + return formatter::format(s, ctx); + } +}; + void TRestGeant4Event::PrintEvent(int maxTracks, int maxHits) const { TRestEvent::PrintEvent(); - cout.precision(4); + print("- Total deposited energy: {}\n", ToEnergyString(fTotalDepositedEnergy)); + print(fg(color::medium_sea_green), "- Sensitive detectors total energy: {}\n", + ToEnergyString(fSensitiveVolumeEnergy)); - cout << "Total energy : " << fTotalDepositedEnergy << " keV" << endl; - cout << "Sensitive volume energy : " << fSensitiveVolumeEnergy << " keV" << endl; - cout << "Source origin : (" << fPrimaryPosition.X() << "," << fPrimaryPosition.Y() << "," - << fPrimaryPosition.Z() << ") mm" << endl; - - for (int n = 0; n < GetNumberOfPrimaries(); n++) { - const auto& direction = fPrimaryDirections[n]; - cout << "Source " << n << " Particle name : " << fPrimaryParticleNames[n] << endl; - cout << "Source " << n << " direction : (" << direction.X() << "," << direction.Y() << "," - << direction.Z() << ")" << endl; - cout << "Source " << n << " energy : " << fPrimaryEnergies[n] << " keV" << endl; - } + print("- Primary source position: {} mm\n", fPrimaryPosition); - cout << "Number of active volumes : " << GetNumberOfActiveVolumes() << endl; - for (int i = 0; i < GetNumberOfActiveVolumes(); i++) { - if (isVolumeStored(i)) { - cout << "Active volume " << i << ":" - << " has been stored." << endl; - cout << "Total energy deposit in volume " << i << ":" - << " : " << fVolumeDepositedEnergy[i] << " keV" << endl; - } else - cout << "Active volume " << i << ":" - << " has not been stored" << endl; + for (int i = 0; i < GetNumberOfPrimaries(); i++) { + const auto sourceNumberString = + GetNumberOfPrimaries() <= 1 ? "" : format(" {} of {}", i + 1, GetNumberOfPrimaries()); + print(" - Source{} primary particle: {}\n", sourceNumberString, fPrimaryParticleNames[i]); + print(" Direction: {}\n", fPrimaryDirections[i]); + print(fg(color::light_golden_rod_yellow), " Energy: {}\n", ToEnergyString(fPrimaryEnergies[i])); } - cout << "---------------------------------------------------------------------------" << endl; - cout << "Total number of tracks : " << GetNumberOfTracks() << endl; + print("\n"); + print("Total number of tracks: {}\n", GetNumberOfTracks()); int nTracks = GetNumberOfTracks(); - if (maxTracks > 0) { + if (maxTracks > 0 && maxTracks < GetNumberOfTracks()) { nTracks = min(maxTracks, int(GetNumberOfTracks())); - cout << " Printing only the first " << nTracks << " tracks" << endl; + print(fg(color::light_slate_gray), "Printing only the first {} tracks\n", nTracks); } - for (int n = 0; n < nTracks; n++) { - GetTrack(n).PrintTrack(maxHits); + for (int i = 0; i < nTracks; i++) { + GetTrack(i).PrintTrack(maxHits); } } diff --git a/src/TRestGeant4Track.cxx b/src/TRestGeant4Track.cxx index 9fbdf2e..247955b 100644 --- a/src/TRestGeant4Track.cxx +++ b/src/TRestGeant4Track.cxx @@ -17,10 +17,14 @@ #include "TRestGeant4Track.h" +#include +#include + #include "TRestGeant4Event.h" #include "TRestGeant4Metadata.h" using namespace std; +using namespace fmt; ClassImp(TRestGeant4Track); @@ -111,29 +115,39 @@ size_t TRestGeant4Track::GetNumberOfPhysicalHits(Int_t volID) const { return numberOfHits; } +template <> +struct fmt::formatter : formatter { + auto format(TVector3 c, format_context& ctx) { + string s = fmt::format("({:0.3f}, {:0.3f}, {:0.3f})", c.X(), c.Y(), c.Z()); + return formatter::format(s, ctx); + } +}; + void TRestGeant4Track::PrintTrack(size_t maxHits) const { - cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl; - cout.precision(5); - cout << " Particle : " << GetParticleName() << " Track ID: " << GetTrackID() - << " Parent ID: " << GetParentID() << " Created by process: " << fCreatorProcess - << " Global timestamp: " << fGlobalTimestamp << " s Time length: " << GetTimeLength() * 1E6 << " us" - << endl; - cout << " Origin: (" << GetTrackOrigin().X() << ", " << GetTrackOrigin().Y() << ", " - << GetTrackOrigin().Z() << ") mm Initial KE: " << GetInitialKineticEnergy() << " keV" << endl; - cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl; - - int nHits = GetNumberOfHits(); - if (maxHits > 0) { - nHits = min(maxHits, GetNumberOfHits()); - cout << " Printing only the first " << nHits << " hits of the track" << endl; + print(fg(color::green_yellow), + " * TrackID: {} - Particle: {} - ParentID: {}{} - Created by '{}' with initial " + "KE of {}\n", + fTrackID, fParticleName, fParentID, + (GetParentTrack() != nullptr ? format(" - Parent particle: {}", GetParentTrack()->GetParticleName()) + : ""), + fCreatorProcess, ToEnergyString(fInitialKineticEnergy)); + print(fg(color::green_yellow), + " Initial position {} mm at time {} - Time length of {} and spatial length of {}\n", + fInitialPosition, ToTimeString(fGlobalTimestamp), ToTimeString(fTimeLength), + ToLengthString(fLength)); + + size_t nHits = GetNumberOfHits(); + if (maxHits > 0 && maxHits < nHits) { + nHits = min(maxHits, nHits); + print(fg(color::light_slate_gray), "Printing only the first {} hits of the track\n", nHits); } const TRestGeant4Metadata* metadata = GetGeant4Metadata(); for (int i = 0; i < nHits; i++) { TString processName = GetProcessName(fHits.GetHitProcess(i)); if (processName.IsNull()) { - // in case process name is not found, use ID - processName = TString(std::to_string(fHits.GetHitProcess(i))); + processName = + TString(std::to_string(fHits.GetHitProcess(i))); // in case process name is not found, use ID } TString volumeName = ""; @@ -145,14 +159,13 @@ void TRestGeant4Track::PrintTrack(size_t maxHits) const { volumeName = TString(std::to_string(fHits.GetHitVolume(i))); } - cout << " # Hit " << i << " # process : " << processName << " volume : " << volumeName - << " X : " << fHits.GetX(i) << " Y : " << fHits.GetY(i) << " Z : " << fHits.GetZ(i) << " mm" - << endl; - cout << " Edep : " << fHits.GetEnergy(i) << " keV Ekin : " << fHits.GetKineticEnergy(i) << " keV" - << " Global time : " << fHits.GetTime(i) << " us" << endl; + print( + (fHits.GetEnergy(i) > 0 ? fg(color::light_salmon) : fg(color::antique_white)), + " - Hit {} - Energy: {} - Process: {} - Volume: {} - Position: {} mm - Time: {} - KE: {}\n", + i, ToEnergyString(fHits.GetEnergy(i)), processName, volumeName, + TVector3(fHits.GetX(i), fHits.GetY(i), fHits.GetZ(i)), ToTimeString(fHits.GetTime(i)), + ToEnergyString(fHits.GetKineticEnergy(i))); } - cout << endl; - cout.precision(2); } Bool_t TRestGeant4Track::ContainsProcessInVolume(Int_t processID, Int_t volumeID) const { From 27ea629afff1144ec8abae1c74f704652d1b1706 Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Mon, 1 Aug 2022 03:08:17 +0200 Subject: [PATCH 074/100] TRestGeant4Event - added comment --- src/TRestGeant4Event.cxx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index abb69f5..8575427 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -1122,6 +1122,7 @@ void TRestGeant4Event::PrintActiveVolumes() const { } } +// TODO: Find how to place this so that we don't need to copy it in every source file template <> struct fmt::formatter : formatter { auto format(TVector3 c, format_context& ctx) { From 5236ee174d3786016829f080379bdcd6905da680 Mon Sep 17 00:00:00 2001 From: lobis Date: Wed, 3 Aug 2022 22:28:19 +0200 Subject: [PATCH 075/100] Updated tests --- inc/TRestGeant4GeometryInfo.h | 4 +++ test/files/TRestGeant4Example.rml | 21 +++++++---- .../TRestGeant4VetoAnalysisProcessExample.rml | 11 ++++++ test/src/Geant4AnalysisProcesses.cxx | 36 +++++++++++++++++++ test/src/TRestGeant4Metadata.cxx | 2 +- 5 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 test/files/TRestGeant4VetoAnalysisProcessExample.rml create mode 100644 test/src/Geant4AnalysisProcesses.cxx diff --git a/inc/TRestGeant4GeometryInfo.h b/inc/TRestGeant4GeometryInfo.h index 3208219..56b617a 100644 --- a/inc/TRestGeant4GeometryInfo.h +++ b/inc/TRestGeant4GeometryInfo.h @@ -81,6 +81,10 @@ class TRestGeant4GeometryInfo { return {}; } + inline TVector3 GetPosition(const TString& volume) const { + return fPhysicalToPositionInWorldMap.at(volume); + } + inline bool IsAssembly() const { return fIsAssembly; } void InsertVolumeName(Int_t id, const TString& volumeName); diff --git a/test/files/TRestGeant4Example.rml b/test/files/TRestGeant4Example.rml index bf2e58b..175b79f 100644 --- a/test/files/TRestGeant4Example.rml +++ b/test/files/TRestGeant4Example.rml @@ -1,4 +1,4 @@ - + @@ -9,18 +9,25 @@ - + - - - - - + + + + + + + + + + + + diff --git a/test/files/TRestGeant4VetoAnalysisProcessExample.rml b/test/files/TRestGeant4VetoAnalysisProcessExample.rml new file mode 100644 index 0000000..fb769bc --- /dev/null +++ b/test/files/TRestGeant4VetoAnalysisProcessExample.rml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/test/src/Geant4AnalysisProcesses.cxx b/test/src/Geant4AnalysisProcesses.cxx new file mode 100644 index 0000000..8286270 --- /dev/null +++ b/test/src/Geant4AnalysisProcesses.cxx @@ -0,0 +1,36 @@ + +#include +#include + +#include + +namespace fs = std::filesystem; + +using namespace std; + +const auto filesPath = fs::path(__FILE__).parent_path().parent_path() / "files"; +const auto vetoAnalysisRml = filesPath / "TRestGeant4VetoAnalysisProcessExample.rml"; + +TEST(TRestGeant4VetoAnalysisProcess, TestFiles) { + cout << "Test files path: " << filesPath << endl; + + // Check dir exists and is a directory + EXPECT_TRUE(fs::is_directory(filesPath)); + // Check it's not empty + EXPECT_TRUE(!fs::is_empty(filesPath)); + + // All used files in this tests + EXPECT_TRUE(fs::exists(vetoAnalysisRml)); +} + +TEST(TRestGeant4VetoAnalysisProcess, Default) { + TRestGeant4VetoAnalysisProcess process; + + process.PrintMetadata(); + + EXPECT_TRUE(process.GetVetoVolumesExpression().IsNull()); + EXPECT_TRUE(process.GetVetoDetectorExpression().IsNull()); + EXPECT_TRUE(process.GetVetoDetectorOffsetSize() == 0); + EXPECT_TRUE(process.GetVetoLightAttenuation() == 0); + EXPECT_TRUE(process.GetVetoQuenchingFactor() == 1.0); +} diff --git a/test/src/TRestGeant4Metadata.cxx b/test/src/TRestGeant4Metadata.cxx index ddb10a1..cae0a62 100644 --- a/test/src/TRestGeant4Metadata.cxx +++ b/test/src/TRestGeant4Metadata.cxx @@ -12,7 +12,7 @@ const auto filesPath = fs::path(__FILE__).parent_path().parent_path() / "files"; const auto geant4MetadataRml = filesPath / "TRestGeant4Example.rml"; TEST(TRestGeant4Metadata, TestFiles) { - cout << "FrameworkCore test files path: " << filesPath << endl; + cout << "Test files path: " << filesPath << endl; // Check dir exists and is a directory EXPECT_TRUE(fs::is_directory(filesPath)); From 4a5758026b9e57bdb7fe8851315004e4e9f4ccd3 Mon Sep 17 00:00:00 2001 From: lobis Date: Thu, 4 Aug 2022 20:54:00 +0200 Subject: [PATCH 076/100] Updated tests and added more prints --- src/TRestGeant4GeometryInfo.cxx | 7 +++- .../TRestGeant4VetoAnalysisProcessExample.rml | 5 +++ test/src/Geant4AnalysisProcesses.cxx | 37 +++++++++++++++++++ test/src/TRestGeant4Metadata.cxx | 4 +- 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/TRestGeant4GeometryInfo.cxx b/src/TRestGeant4GeometryInfo.cxx index 1b0f406..48dfca9 100644 --- a/src/TRestGeant4GeometryInfo.cxx +++ b/src/TRestGeant4GeometryInfo.cxx @@ -182,11 +182,14 @@ void TRestGeant4GeometryInfo::Print() const { cout << "Physical volumes (" << physicalVolumes.size() << "):" << endl; for (const auto& physical : GetAllPhysicalVolumes()) { auto geant4Name = GetGeant4PhysicalNameFromAlternativeName(physical); - const auto logical = fPhysicalToLogicalVolumeMap.at(physical); + const auto& logical = fPhysicalToLogicalVolumeMap.at(physical); + const auto& position = GetPosition(physical); cout << "\t- " << (geant4Name == physical ? physical : physical + " (" + geant4Name + ")") << " - ID: " << GetIDFromVolume(physical) << " - Logical: " << fPhysicalToLogicalVolumeMap.at(physical) - << " - Material: " << fLogicalToMaterialMap.at(logical) << endl; + << " - Material: " << fLogicalToMaterialMap.at(logical) // + << " - Position: (" << position.X() << ", " << position.Y() << ", " << position.Z() << ") mm" // + << endl; } const auto logicalVolumes = GetAllLogicalVolumes(); diff --git a/test/files/TRestGeant4VetoAnalysisProcessExample.rml b/test/files/TRestGeant4VetoAnalysisProcessExample.rml index fb769bc..c7754ce 100644 --- a/test/files/TRestGeant4VetoAnalysisProcessExample.rml +++ b/test/files/TRestGeant4VetoAnalysisProcessExample.rml @@ -5,6 +5,11 @@ + + + + + diff --git a/test/src/Geant4AnalysisProcesses.cxx b/test/src/Geant4AnalysisProcesses.cxx index 8286270..88aac9a 100644 --- a/test/src/Geant4AnalysisProcesses.cxx +++ b/test/src/Geant4AnalysisProcesses.cxx @@ -10,6 +10,7 @@ using namespace std; const auto filesPath = fs::path(__FILE__).parent_path().parent_path() / "files"; const auto vetoAnalysisRml = filesPath / "TRestGeant4VetoAnalysisProcessExample.rml"; +const auto vetoAnalysisRestG4Run = filesPath / "VetoAnalysisGeant4Run.root"; TEST(TRestGeant4VetoAnalysisProcess, TestFiles) { cout << "Test files path: " << filesPath << endl; @@ -21,6 +22,7 @@ TEST(TRestGeant4VetoAnalysisProcess, TestFiles) { // All used files in this tests EXPECT_TRUE(fs::exists(vetoAnalysisRml)); + EXPECT_TRUE(fs::exists(vetoAnalysisRestG4Run)); } TEST(TRestGeant4VetoAnalysisProcess, Default) { @@ -34,3 +36,38 @@ TEST(TRestGeant4VetoAnalysisProcess, Default) { EXPECT_TRUE(process.GetVetoLightAttenuation() == 0); EXPECT_TRUE(process.GetVetoQuenchingFactor() == 1.0); } + +TEST(TRestGeant4VetoAnalysisProcess, FromRml) { + TRestGeant4VetoAnalysisProcess process(vetoAnalysisRml.c_str()); + + process.PrintMetadata(); + + EXPECT_TRUE(process.GetVetoVolumesExpression() == "^scintillatorVolume"); + EXPECT_TRUE(process.GetVetoDetectorExpression() == "^scintillatorLightGuideVolume"); + EXPECT_TRUE(process.GetVetoDetectorOffsetSize() == 0); + EXPECT_TRUE(process.GetVetoLightAttenuation() == 0); + EXPECT_TRUE(process.GetVetoQuenchingFactor() == 0); +} + +TEST(TRestGeant4VetoAnalysisProcess, Simulation) { + TRestGeant4VetoAnalysisProcess process(vetoAnalysisRml.c_str()); + + EXPECT_TRUE(process.GetVetoVolumesExpression() == "^scintillatorVolume"); + EXPECT_TRUE(process.GetVetoDetectorExpression() == "^scintillatorLightGuideVolume"); + EXPECT_TRUE(process.GetVetoDetectorOffsetSize() == 0); + EXPECT_TRUE(process.GetVetoLightAttenuation() == 0); + EXPECT_TRUE(process.GetVetoQuenchingFactor() == 0); + + TRestRun run(vetoAnalysisRestG4Run.c_str()); + run.GetInputFile()->ls(); + + const auto metadata = + dynamic_cast(run.GetMetadataClass("TRestGeant4Metadata")); + EXPECT_TRUE(metadata != nullptr); + + process.SetGeant4Metadata(metadata); + + process.InitProcess(); + + process.PrintMetadata(); +} diff --git a/test/src/TRestGeant4Metadata.cxx b/test/src/TRestGeant4Metadata.cxx index cae0a62..005b8f5 100644 --- a/test/src/TRestGeant4Metadata.cxx +++ b/test/src/TRestGeant4Metadata.cxx @@ -32,10 +32,12 @@ TEST(TRestGeant4Metadata, Default) { } TEST(TRestGeant4Metadata, FromRml) { - GTEST_SKIP_("Problem with paths..."); + cout << "Path: " << geant4MetadataRml << endl; TRestGeant4Metadata restGeant4Metadata(geant4MetadataRml.c_str()); + GTEST_SKIP_("TODO: fix this"); + restGeant4Metadata.PrintMetadata(); EXPECT_TRUE(restGeant4Metadata.GetSensitiveVolume() == "sensitiveVolume"); From 5af7b0932d8b1c0461abb515ec6c7d540d0773e3 Mon Sep 17 00:00:00 2001 From: lobis Date: Fri, 5 Aug 2022 01:38:57 +0200 Subject: [PATCH 077/100] Added process to analyze veto system events (work in progress) --- inc/TRestGeant4Event.h | 1 + 1 file changed, 1 insertion(+) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index 3f59c9d..9f71de0 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -150,6 +150,7 @@ class TRestGeant4Event : public TRestEvent { size_t GetNumberOfHits(Int_t volID = -1) const; size_t GetNumberOfPhysicalHits(Int_t volID = -1) const; + inline const std::vector& GetTracks() const { return fTracks; } inline size_t GetNumberOfTracks() const { return fTracks.size(); } inline Int_t GetNumberOfActiveVolumes() const { return fNVolumes; } From 1292758fc1f048f910961df1c4331b1bcd271459 Mon Sep 17 00:00:00 2001 From: lobis Date: Sat, 6 Aug 2022 15:10:34 +0200 Subject: [PATCH 078/100] Added missing getters --- inc/TRestGeant4Event.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index 9f71de0..20d64d7 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -168,6 +168,20 @@ class TRestGeant4Event : public TRestEvent { TVector3 GetLastPositionInVolume(Int_t volID) const; TVector3 GetPositionDeviationInVolume(Int_t volID) const; + inline const std::map GetEnergyInVolumeMap() const { + std::map result; + for (const auto& [volume, processMap] : fEnergyInVolumePerProcess) { + for (const auto& [process, energy] : processMap) { + result[volume] += energy; + } + } + return result; + } + inline const std::map>& GetEnergyInVolumePerProcessMap() + const { + return fEnergyInVolumePerProcess; + } + TRestHits GetHits(Int_t volID = -1) const; inline TRestHits GetHitsInVolume(Int_t volID) const { return GetHits(volID); } From 00625ebd7ce915f394ab4af7dd43b514631d514a Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 30 Aug 2022 22:44:54 +0200 Subject: [PATCH 079/100] work in progress on alternative shaping process --- inc/TRestGeant4Event.h | 24 +++++------- src/TRestGeant4Event.cxx | 80 +++++++++++++++++++++++++++++++++++----- 2 files changed, 79 insertions(+), 25 deletions(-) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index 20d64d7..8a8bc30 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -123,8 +123,8 @@ class TRestGeant4Event : public TRestEvent { std::vector fVolumeStored; std::vector fVolumeStoredNames; std::vector fVolumeDepositedEnergy; - std::map fEnergyInVolumeMap; - std::map> fEnergyInVolumePerProcess; + std::map>> + fEnergyInVolumePerParticlePerProcess; std::vector fTracks; Int_t fMaxSubEventID; @@ -168,19 +168,13 @@ class TRestGeant4Event : public TRestEvent { TVector3 GetLastPositionInVolume(Int_t volID) const; TVector3 GetPositionDeviationInVolume(Int_t volID) const; - inline const std::map GetEnergyInVolumeMap() const { - std::map result; - for (const auto& [volume, processMap] : fEnergyInVolumePerProcess) { - for (const auto& [process, energy] : processMap) { - result[volume] += energy; - } - } - return result; - } - inline const std::map>& GetEnergyInVolumePerProcessMap() - const { - return fEnergyInVolumePerProcess; - } + std::map>> + GetEnergyInVolumePerParticlePerProcessMap() const; + std::map> GetEnergyInVolumePerProcessMap() const; + std::map> GetEnergyInVolumePerParticleMap() const; + std::map GetEnergyPerProcessMap() const; + std::map GetEnergyPerParticleMap() const; + std::map GetEnergyInVolumeMap() const; TRestHits GetHits(Int_t volID = -1) const; inline TRestHits GetHitsInVolume(Int_t volID) const { return GetHits(volID); } diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index 8575427..5fbe2bb 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -335,7 +335,7 @@ void TRestGeant4Event::SetBoundaries() { } /* {{{ Get{XY,YZ,XZ}MultiGraph methods */ -TMultiGraph* TRestGeant4Event::GetXYMultiGraph(Int_t gridElement, std::vector pcsList, +TMultiGraph* TRestGeant4Event::GetXYMultiGraph(Int_t gridElement, vector pcsList, Double_t minPointSize, Double_t maxPointSize) { if (fXYHitGraph) { delete[] fXYHitGraph; @@ -427,7 +427,7 @@ TMultiGraph* TRestGeant4Event::GetXYMultiGraph(Int_t gridElement, std::vector pcsList, +TMultiGraph* TRestGeant4Event::GetYZMultiGraph(Int_t gridElement, vector pcsList, Double_t minPointSize, Double_t maxPointSize) { if (fYZHitGraph) { delete[] fYZHitGraph; @@ -519,7 +519,7 @@ TMultiGraph* TRestGeant4Event::GetYZMultiGraph(Int_t gridElement, std::vector pcsList, +TMultiGraph* TRestGeant4Event::GetXZMultiGraph(Int_t gridElement, vector pcsList, Double_t minPointSize, Double_t maxPointSize) { if (fXZHitGraph) { delete[] fXZHitGraph; @@ -613,7 +613,7 @@ TMultiGraph* TRestGeant4Event::GetXZMultiGraph(Int_t gridElement, std::vector optList) { +TH2F* TRestGeant4Event::GetXYHistogram(Int_t gridElement, vector optList) { if (fXYHisto) { delete fXYHisto; fXYHisto = nullptr; @@ -672,7 +672,7 @@ TH2F* TRestGeant4Event::GetXYHistogram(Int_t gridElement, std::vector o return fXYHisto; } -TH2F* TRestGeant4Event::GetXZHistogram(Int_t gridElement, std::vector optList) { +TH2F* TRestGeant4Event::GetXZHistogram(Int_t gridElement, vector optList) { if (fXZHisto) { delete fXZHisto; fXZHisto = nullptr; @@ -731,7 +731,7 @@ TH2F* TRestGeant4Event::GetXZHistogram(Int_t gridElement, std::vector o return fXZHisto; } -TH2F* TRestGeant4Event::GetYZHistogram(Int_t gridElement, std::vector optList) { +TH2F* TRestGeant4Event::GetYZHistogram(Int_t gridElement, vector optList) { if (fYZHisto) { delete fYZHisto; fYZHisto = nullptr; @@ -791,7 +791,7 @@ TH2F* TRestGeant4Event::GetYZHistogram(Int_t gridElement, std::vector o } /* }}} */ -TH1D* TRestGeant4Event::GetXHistogram(Int_t gridElement, std::vector optList) { +TH1D* TRestGeant4Event::GetXHistogram(Int_t gridElement, vector optList) { if (fXHisto) { delete fXHisto; fXHisto = nullptr; @@ -842,7 +842,7 @@ TH1D* TRestGeant4Event::GetXHistogram(Int_t gridElement, std::vector op return fXHisto; } -TH1D* TRestGeant4Event::GetZHistogram(Int_t gridElement, std::vector optList) { +TH1D* TRestGeant4Event::GetZHistogram(Int_t gridElement, vector optList) { if (fZHisto) { delete fZHisto; fZHisto = nullptr; @@ -893,7 +893,7 @@ TH1D* TRestGeant4Event::GetZHistogram(Int_t gridElement, std::vector op return fZHisto; } -TH1D* TRestGeant4Event::GetYHistogram(Int_t gridElement, std::vector optList) { +TH1D* TRestGeant4Event::GetYHistogram(Int_t gridElement, vector optList) { if (fYHisto) { delete fYHisto; fYHisto = nullptr; @@ -1024,7 +1024,7 @@ TPad* TRestGeant4Event::DrawEvent(const TString& option, Bool_t autoBoundaries) if (optList[n] == "print") this->PrintEvent(); } - optList.erase(std::remove(optList.begin(), optList.end(), "print"), optList.end()); + optList.erase(remove(optList.begin(), optList.end(), "print"), optList.end()); unsigned int nPlots = optList.size(); @@ -1228,3 +1228,63 @@ set TRestGeant4Event::GetUniqueParticles() const { } return result; } + +map> TRestGeant4Event::GetEnergyInVolumePerProcessMap() const { + map> result; + for (const auto& [volume, particleProcessMap] : fEnergyInVolumePerParticlePerProcess) { + for (const auto& [particle, processMap] : particleProcessMap) { + for (const auto& [process, energy] : processMap) { + result[volume][process] += energy; + } + } + } + return result; +} + +map> TRestGeant4Event::GetEnergyInVolumePerParticleMap() const { + map> result; + for (const auto& [volume, particleProcessMap] : fEnergyInVolumePerParticlePerProcess) { + for (const auto& [particle, processMap] : particleProcessMap) { + for (const auto& [process, energy] : processMap) { + result[volume][particle] += energy; + } + } + } + return result; +} + +map TRestGeant4Event::GetEnergyPerProcessMap() const { + map result; + for (const auto& [volume, particleProcessMap] : fEnergyInVolumePerParticlePerProcess) { + for (const auto& [particle, processMap] : particleProcessMap) { + for (const auto& [process, energy] : processMap) { + result[process] += energy; + } + } + } + return result; +} + +map TRestGeant4Event::GetEnergyPerParticleMap() const { + map result; + for (const auto& [volume, particleProcessMap] : fEnergyInVolumePerParticlePerProcess) { + for (const auto& [particle, processMap] : particleProcessMap) { + for (const auto& [process, energy] : processMap) { + result[particle] += energy; + } + } + } + return result; +} + +map TRestGeant4Event::GetEnergyInVolumeMap() const { + map result; + for (const auto& [volume, particleProcessMap] : fEnergyInVolumePerParticlePerProcess) { + for (const auto& [particle, processMap] : particleProcessMap) { + for (const auto& [process, energy] : processMap) { + result[volume] += energy; + } + } + } + return result; +} \ No newline at end of file From 9cc814b0068d6ee1829f62b9639136cb782559cd Mon Sep 17 00:00:00 2001 From: lobis Date: Thu, 1 Sep 2022 18:12:58 +0200 Subject: [PATCH 080/100] TRestGeant4VetoAnalysisProcess - update --- inc/TRestGeant4VetoAnalysisProcess.h | 124 ++++++---------- src/TRestGeant4VetoAnalysisProcess.cxx | 189 +++++++++++++++++-------- 2 files changed, 175 insertions(+), 138 deletions(-) diff --git a/inc/TRestGeant4VetoAnalysisProcess.h b/inc/TRestGeant4VetoAnalysisProcess.h index 0771be6..359f254 100644 --- a/inc/TRestGeant4VetoAnalysisProcess.h +++ b/inc/TRestGeant4VetoAnalysisProcess.h @@ -23,58 +23,56 @@ #ifndef RestCore_TRestGeant4VetoAnalysisProcess #define RestCore_TRestGeant4VetoAnalysisProcess -#include -#include +#include -#include "TRestEventProcess.h" - -using namespace std; +#include "TRestGeant4Event.h" +#include "TRestGeant4Metadata.h" class TRestGeant4VetoAnalysisProcess : public TRestEventProcess { private: - /// A pointer to the specific TRestGeant4Event input - TRestGeant4Event* fInputG4Event; //! - /// A pointer to the specific TRestGeant4Event output - TRestGeant4Event* fOutputG4Event; //! - /// A pointer to the simulation metadata information accessible to TRestRun - TRestGeant4Metadata* fG4Metadata; //! - - std::vector fVetoVolumeIds; //! - std::string fVetoKeyword = ""; //! - std::vector fVetoGroupKeywords; //! - std::map> fVetoGroupVolumeNames; //! - std::vector fQuenchingFactors; //! + TString fVetoVolumesExpression; + TString fVetoDetectorsExpression; + double fVetoDetectorOffsetSize = 0; + double fVetoLightAttenuation = 0; + double fVetoQuenchingFactor = 1.0; + + public: + inline TString GetVetoVolumesExpression() const { return fVetoVolumesExpression; } + inline TString GetVetoDetectorExpression() const { return fVetoDetectorsExpression; } + inline double GetVetoDetectorOffsetSize() const { return fVetoDetectorOffsetSize; } + inline double GetVetoLightAttenuation() const { return fVetoLightAttenuation; } + inline double GetVetoQuenchingFactor() const { return fVetoQuenchingFactor; } + + inline void SetVetoVolumesExpression(const TString& expression) { fVetoVolumesExpression = expression; } + inline void SetVetoDetectorsExpression(const TString& expression) { + fVetoDetectorsExpression = expression; + } + inline void SetVetoDetectorOffsetSize(double offset) { fVetoDetectorOffsetSize = offset; } + inline void SetVetoLightAttenuation(double attenuation) { fVetoLightAttenuation = attenuation; } + inline void SetVetoQuenchingFactor(double quenchingFactor) { fVetoQuenchingFactor = quenchingFactor; } + + private: + TRestGeant4Event* fInputEvent = nullptr; //! + TRestGeant4Event* fOutputEvent = nullptr; //! + const TRestGeant4Metadata* fGeant4Metadata = nullptr; //! + + std::vector fVetoVolumes; + std::vector fVetoDetectorVolumes; + std::map fVetoDetectorBoundaryPosition; + std::map fVetoDetectorBoundaryDirection; void InitFromConfigFile() override; void Initialize() override; void LoadDefaultConfig(); - // clean std::string (https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring) - inline std::string& rtrim(std::string& s, const char* t = " \t\n\r\f\v") { - s.erase(s.find_last_not_of(t) + 1); - return s; - } - // trim from beginning of std::string (left) - inline std::string& ltrim(std::string& s, const char* t = " \t\n\r\f\v") { - s.erase(0, s.find_first_not_of(t)); - return s; - } - // trim from both ends of std::string (right then left) - inline std::string& trim(std::string& s, const char* t = " \t\n\r\f\v") { return ltrim(rtrim(s, t), t); } - - // final clean std::string: trim and UPPER - inline std::string& clean_string(std::string& s) { - s = trim(s); - std::transform(s.begin(), s.end(), s.begin(), ::tolower); - return s; - } - - protected: - // add here the members of your event process - public: - any GetInputEvent() const override { return fInputG4Event; } - any GetOutputEvent() const override { return fOutputG4Event; } + any GetInputEvent() const override { return fInputEvent; } + any GetOutputEvent() const override { return fOutputEvent; } + + inline void SetGeant4Metadata(const TRestGeant4Metadata* metadata) { + fGeant4Metadata = metadata; + } // TODO: We should not need this! but `GetMetadata()` is not working early in the + // processing (look at the tests for more details) void InitProcess() override; TRestEvent* ProcessEvent(TRestEvent* inputEvent) override; @@ -82,52 +80,14 @@ class TRestGeant4VetoAnalysisProcess : public TRestEventProcess { void LoadConfig(const std::string& configFilename, const std::string& name = ""); - /// It prints out the process parameters stored in the metadata structure - void PrintMetadata() override { - BeginPrintProcess(); - - RESTDebug << "VETO KEYWORD: " << fVetoKeyword << RESTendl; - RESTDebug << RESTendl; - - RESTDebug << "VETO GROUP KEYWORDS:" << RESTendl; - for (unsigned int i = 0; i < fVetoGroupKeywords.size(); i++) { - RESTDebug << "\t" << fVetoGroupKeywords[i] << RESTendl; - } - RESTDebug << RESTendl; - - RESTDebug << "Found " << fVetoVolumeIds.size() << " veto volumes:" << RESTendl; - for (unsigned int i = 0; i < fVetoVolumeIds.size(); i++) { - RESTDebug << "\t" << fG4Metadata->GetActiveVolumeName(fVetoVolumeIds[i]) << RESTendl; - } - RESTDebug << RESTendl; - - RESTDebug << "GROUPS:" << RESTendl; - for (const auto& pair : fVetoGroupVolumeNames) { - RESTDebug << "GROUP " << pair.first << " (" << pair.second.size() << " volumes):\n"; - for (unsigned int i = 0; i < pair.second.size(); i++) { - RESTDebug << "\t" << pair.second[i] << RESTendl; - } - } - RESTDebug << RESTendl; - - RESTDebug << "QUENCHING FACTORS (" << fQuenchingFactors.size() << " Total)" << RESTendl; - for (unsigned int i = 0; i < fQuenchingFactors.size(); i++) { - RESTDebug << "\t" << fQuenchingFactors[i] << RESTendl; - } - RESTDebug << RESTendl; - - EndPrintProcess(); - } + void PrintMetadata() override; - /// Returns a new instance of this class - TRestEventProcess* Maker() { return new TRestGeant4VetoAnalysisProcess; } - /// Returns the name of this process const char* GetProcessName() const override { return "geant4VetoAnalysis"; } TRestGeant4VetoAnalysisProcess(); TRestGeant4VetoAnalysisProcess(const char* configFilename); ~TRestGeant4VetoAnalysisProcess(); - ClassDefOverride(TRestGeant4VetoAnalysisProcess, 1); + ClassDefOverride(TRestGeant4VetoAnalysisProcess, 2); }; #endif // RestCore_TRestGeant4VetoAnalysisProcess diff --git a/src/TRestGeant4VetoAnalysisProcess.cxx b/src/TRestGeant4VetoAnalysisProcess.cxx index a112743..bea483a 100644 --- a/src/TRestGeant4VetoAnalysisProcess.cxx +++ b/src/TRestGeant4VetoAnalysisProcess.cxx @@ -65,6 +65,10 @@ #include "TRestGeant4VetoAnalysisProcess.h" +#include +#include + +using namespace fmt; using namespace std; ClassImp(TRestGeant4VetoAnalysisProcess); @@ -72,14 +76,13 @@ ClassImp(TRestGeant4VetoAnalysisProcess); TRestGeant4VetoAnalysisProcess::TRestGeant4VetoAnalysisProcess() { Initialize(); } TRestGeant4VetoAnalysisProcess::TRestGeant4VetoAnalysisProcess(const char* configFilename) { - Initialize(); - if (LoadConfigFromFile(configFilename)) LoadDefaultConfig(); + TRestGeant4VetoAnalysisProcess(); + if (LoadConfigFromFile(configFilename)) { + LoadDefaultConfig(); + } } -/////////////////////////////////////////////// -/// \brief Default destructor -/// -TRestGeant4VetoAnalysisProcess::~TRestGeant4VetoAnalysisProcess() { delete fOutputG4Event; } +TRestGeant4VetoAnalysisProcess::~TRestGeant4VetoAnalysisProcess() { delete fOutputEvent; } /////////////////////////////////////////////// /// \brief Function to load the default config in absence of RML input @@ -91,13 +94,10 @@ void TRestGeant4VetoAnalysisProcess::LoadDefaultConfig() { SetTitle("Default con /// section name /// void TRestGeant4VetoAnalysisProcess::Initialize() { - fG4Metadata = nullptr; - SetSectionName(this->ClassName()); SetLibraryVersion(LIBRARY_VERSION); - fInputG4Event = nullptr; - fOutputG4Event = new TRestGeant4Event(); + fOutputEvent = new TRestGeant4Event(); } /////////////////////////////////////////////// @@ -120,34 +120,75 @@ void TRestGeant4VetoAnalysisProcess::LoadConfig(const string& configFilename, co /// \brief Process initialization. /// void TRestGeant4VetoAnalysisProcess::InitProcess() { - fG4Metadata = GetMetadata(); - // CAREFUL THIS METHOD IS CALLED TWICE! - // we need to reset these variables to zero - fVetoVolumeIds.clear(); - fVetoGroupVolumeNames.clear(); - - // get "veto" volumes - for (unsigned int i = 0; i < fG4Metadata->GetNumberOfActiveVolumes(); i++) { - string volume_name = (string)fG4Metadata->GetActiveVolumeName(i); - volume_name = clean_string(volume_name); - if (volume_name.find(clean_string(fVetoKeyword)) != string::npos) { - fVetoVolumeIds.push_back(i); + fVetoVolumes.clear(); + fVetoDetectorVolumes.clear(); + fVetoDetectorBoundaryDirection.clear(); + fVetoDetectorBoundaryPosition.clear(); + + if (fGeant4Metadata == nullptr) { + // maybe it was manually initialized + fGeant4Metadata = GetMetadata(); + } + if (fGeant4Metadata == nullptr) { + cerr << "TRestGeant4VetoAnalysisProcess::InitProcess: Geant4 metadata not found" << endl; + exit(1); + } + + const auto& geometryInfo = fGeant4Metadata->GetGeant4GeometryInfo(); + + fVetoVolumes = geometryInfo.GetAllPhysicalVolumesMatchingExpression(fVetoVolumesExpression); + if (fVetoVolumes.empty()) { + const auto logicalVolumes = + geometryInfo.GetAllLogicalVolumesMatchingExpression(fVetoVolumesExpression); + for (const auto& logicalVolume : logicalVolumes) { + for (const auto& physicalVolume : geometryInfo.GetAllPhysicalVolumesFromLogical(logicalVolume)) { + fVetoVolumes.push_back(geometryInfo.GetAlternativeNameFromGeant4PhysicalName(physicalVolume)); + } } } + if (fVetoVolumes.empty()) { + cerr << "TRestGeant4VetoAnalysisProcess::InitProcess: No veto volumes found" << endl; + exit(1); + } - // veto groups (fill fVetoGroupVolumeNames) - for (unsigned int i = 0; i < fVetoGroupKeywords.size(); i++) { - string veto_group_keyword = clean_string(fVetoGroupKeywords[i]); - fVetoGroupVolumeNames[veto_group_keyword] = std::vector{}; - for (int& id : fVetoVolumeIds) { - string volume_name = (string)fG4Metadata->GetActiveVolumeName(id); - volume_name = clean_string(volume_name); - if (volume_name.find(veto_group_keyword) != string::npos) { - fVetoGroupVolumeNames[veto_group_keyword].push_back( - (string)fG4Metadata->GetActiveVolumeName(id)); + // get detector volumes if requested + if (!fVetoDetectorsExpression.IsNull()) { + fVetoDetectorVolumes = geometryInfo.GetAllPhysicalVolumesMatchingExpression(fVetoDetectorsExpression); + if (fVetoDetectorVolumes.empty()) { + const auto logicalVolumes = + geometryInfo.GetAllLogicalVolumesMatchingExpression(fVetoDetectorsExpression); + for (const auto& logicalVolume : logicalVolumes) { + for (const auto& physicalVolume : + geometryInfo.GetAllPhysicalVolumesFromLogical(logicalVolume)) { + fVetoDetectorVolumes.push_back( + geometryInfo.GetAlternativeNameFromGeant4PhysicalName(physicalVolume)); + } } } + if (fVetoDetectorVolumes.empty()) { + cerr << "TRestGeant4VetoAnalysisProcess::InitProcess: No detector volumes found" << endl; + exit(1); + } + if (fVetoDetectorVolumes.size() != fVetoVolumes.size()) { + cerr << "TRestGeant4VetoAnalysisProcess::InitProcess: Number of detector volumes " + << "does not match number of veto volumes" << endl; + exit(1); + } + } + + for (int i = 0; i < fVetoDetectorVolumes.size(); i++) { + const auto& vetoName = fVetoVolumes[i]; + const auto& vetoPosition = geometryInfo.GetPosition(vetoName); + + const auto& vetoDetectorName = fVetoDetectorVolumes[i]; + const auto& vetoDetectorPosition = geometryInfo.GetPosition(vetoDetectorName); + + const auto distance = vetoDetectorPosition - vetoPosition; + const auto direction = distance.Unit(); + + fVetoDetectorBoundaryDirection[vetoName] = direction; + fVetoDetectorBoundaryPosition[vetoName] = vetoDetectorPosition - direction * fVetoDetectorOffsetSize; } PrintMetadata(); @@ -157,14 +198,15 @@ void TRestGeant4VetoAnalysisProcess::InitProcess() { /// \brief The main processing event function /// TRestEvent* TRestGeant4VetoAnalysisProcess::ProcessEvent(TRestEvent* inputEvent) { - fInputG4Event = (TRestGeant4Event*)inputEvent; - *fOutputG4Event = *((TRestGeant4Event*)inputEvent); + /* + fInputEvent = (TRestGeant4Event*)inputEvent; + *fOutputEvent = *((TRestGeant4Event*)inputEvent); std::map volume_energy_map; for (unsigned int i = 0; i < fVetoVolumeIds.size(); i++) { int id = fVetoVolumeIds[i]; - string volume_name = (string)fG4Metadata->GetActiveVolumeName(id); + string volume_name = (string)fGeant4Metadata->GetActiveVolumeName(id); Double_t energy = fOutputG4Event->GetEnergyDepositedInVolume(id); volume_energy_map[volume_name] = energy; @@ -214,7 +256,7 @@ TRestEvent* TRestGeant4VetoAnalysisProcess::ProcessEvent(TRestEvent* inputEvent) auto track = fOutputG4Event->GetTrack(i); string particle_name = (string)track.GetParticleName(); for (const auto& id : fVetoVolumeIds) { - string volume_name = (string)fG4Metadata->GetActiveVolumeName(id); + string volume_name = (string)fGeant4Metadata->GetActiveVolumeName(id); if (particle_name == "e-" || particle_name == "e+" || particle_name == "gamma") { // no quenching factor @@ -261,6 +303,7 @@ TRestEvent* TRestGeant4VetoAnalysisProcess::ProcessEvent(TRestEvent* inputEvent) } return fOutputG4Event; + */ } /////////////////////////////////////////////// @@ -282,29 +325,63 @@ void TRestGeant4VetoAnalysisProcess::EndProcess() { /// void TRestGeant4VetoAnalysisProcess::InitFromConfigFile() { // word to identify active volume as veto (default = "veto" e.g. "vetoTop") - string veto_keyword = GetParameter("vetoKeyword", "veto"); - fVetoKeyword = clean_string(veto_keyword); - // comma separated tags: "top, bottom, ..." - string veto_group_keywords = GetParameter("vetoGroupKeywords", ""); - stringstream ss(veto_group_keywords); - while (ss.good()) { - string substr; - getline(ss, substr, ','); - fVetoGroupKeywords.push_back(clean_string(substr)); + fVetoVolumesExpression = GetParameter("vetoVolumesExpression", fVetoVolumesExpression); + fVetoDetectorsExpression = GetParameter("vetoDetectorsExpression", fVetoDetectorsExpression); + + fVetoDetectorOffsetSize = GetDblParameterWithUnits("vetoDetectorOffset", fVetoDetectorOffsetSize); + fVetoLightAttenuation = GetDblParameterWithUnits("vetoLightAttenuation", fVetoLightAttenuation); + fVetoQuenchingFactor = GetDblParameterWithUnits("quenchingFactor", fVetoQuenchingFactor); +} + +// TODO: Find how to place this so that we don't need to copy it in every source file +template <> +struct fmt::formatter : formatter { + auto format(const TVector3& c, format_context& ctx) { + string s = fmt::format("({:0.3f}, {:0.3f}, {:0.3f})", c.X(), c.Y(), c.Z()); + return formatter::format(s, ctx); + } +}; + +void TRestGeant4VetoAnalysisProcess::PrintMetadata() { + BeginPrintProcess(); + + cout << "Veto volume expression: " << fVetoVolumesExpression << endl; + if (!fVetoDetectorsExpression.IsNull()) { + cout << "Veto detector expression: " << fVetoDetectorsExpression << endl; + cout << "Veto detector offset: " << fVetoDetectorOffsetSize << endl; + cout << "Veto light attenuation: " << fVetoLightAttenuation << endl; + } else { + cout << "Veto detector expression: not set" << endl; + } + cout << "Veto quenching factor: " << fVetoQuenchingFactor << endl; + + RESTDebug << RESTendl; + + if (fVetoVolumes.empty()) { + cout << "Process not initialized yet" << endl; + return; } - // comma separated quenching factors: "0.15, 1.00, ..." - string quenching_factors = GetParameter("vetoQuenchingFactors", "-1"); - stringstream ss_qf(quenching_factors); - while (ss_qf.good()) { - string substr; - getline(ss_qf, substr, ','); - substr = clean_string(substr); - Float_t quenching_factor = (Float_t)std::atof(substr.c_str()); - if (quenching_factor > 1 || quenching_factor < 0) { - cout << "ERROR: quenching factor must be between 0 and 1" << endl; + cout << "Number of veto volumes: " << fVetoVolumes.size() << endl; + cout << "Number of veto detector volumes: " << fVetoDetectorVolumes.size() << endl; + + const auto& geometryInfo = fGeant4Metadata->GetGeant4GeometryInfo(); + for (int i = 0; i < fVetoVolumes.size(); i++) { + const auto& vetoName = fVetoVolumes[i]; + const auto& vetoPosition = geometryInfo.GetPosition(vetoName); + + print(" - Veto volume: {} - name: '{}' - position: {} mm\n", i, vetoName, vetoPosition); + + if (fVetoDetectorVolumes.empty()) { continue; } - fQuenchingFactors.push_back(quenching_factor); + + const auto& vetoDetectorName = fVetoDetectorVolumes[i]; + const auto& vetoDetectorPosition = geometryInfo.GetPosition(vetoDetectorName); + + print(" Veto detector name: '{}' - position: {} mm\n", vetoDetectorName, vetoDetectorPosition); + + print(" Boundary position: {} mm - direction: {}\n", fVetoDetectorBoundaryPosition.at(vetoName), + fVetoDetectorBoundaryDirection.at(vetoName)); } } From 2a98625332ac475097c303cbd555d2730a1292af Mon Sep 17 00:00:00 2001 From: lobis Date: Mon, 5 Sep 2022 18:18:25 +0200 Subject: [PATCH 081/100] fixed bug with total energy not computed --- inc/TRestGeant4Event.h | 22 +++++++++++++--------- src/TRestGeant4Event.cxx | 29 ++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index 8a8bc30..971db25 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -116,8 +116,8 @@ class TRestGeant4Event : public TRestEvent { TVector3 fSubEventPrimaryPosition; TVector3 fSubEventPrimaryDirection; - Double_t fTotalDepositedEnergy; - Double_t fSensitiveVolumeEnergy; + Double_t fTotalDepositedEnergy = 0; + Double_t fSensitiveVolumeEnergy = 0; Int_t fNVolumes; std::vector fVolumeStored; @@ -168,13 +168,13 @@ class TRestGeant4Event : public TRestEvent { TVector3 GetLastPositionInVolume(Int_t volID) const; TVector3 GetPositionDeviationInVolume(Int_t volID) const; - std::map>> + const std::map>>& GetEnergyInVolumePerParticlePerProcessMap() const; - std::map> GetEnergyInVolumePerProcessMap() const; - std::map> GetEnergyInVolumePerParticleMap() const; - std::map GetEnergyPerProcessMap() const; - std::map GetEnergyPerParticleMap() const; - std::map GetEnergyInVolumeMap() const; + const std::map> GetEnergyInVolumePerProcessMap() const; + const std::map> GetEnergyInVolumePerParticleMap() const; + const std::map GetEnergyPerProcessMap() const; + const std::map GetEnergyPerParticleMap() const; + const std::map GetEnergyInVolumeMap() const; TRestHits GetHits(Int_t volID = -1) const; inline TRestHits GetHitsInVolume(Int_t volID) const { return GetHits(volID); } @@ -208,7 +208,7 @@ class TRestGeant4Event : public TRestEvent { return lowestID; } - std::set GetUniqueParticles() const; + const std::set GetUniqueParticles() const; Bool_t ContainsProcessInVolume(Int_t processID, Int_t volumeID = -1) const; inline Bool_t ContainsProcess(Int_t processID) const { return ContainsProcessInVolume(processID, -1); } @@ -253,5 +253,9 @@ class TRestGeant4Event : public TRestEvent { private: std::map fTrackIDToTrackIndex = {}; TRestGeant4Hits fInitialStep; //! + + void AddEnergyInVolumeForParticleForProcess(Double_t energy, const std::string& volumeName, + const std::string& particleName, + const std::string& processName); }; #endif diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index 5fbe2bb..b27e34c 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -1221,7 +1221,7 @@ void TRestGeant4Event::InitializeReferences(TRestRun* run) { } } -set TRestGeant4Event::GetUniqueParticles() const { +const set TRestGeant4Event::GetUniqueParticles() const { set result; for (const auto& track : fTracks) { result.insert(track.GetParticleName().Data()); @@ -1229,7 +1229,7 @@ set TRestGeant4Event::GetUniqueParticles() const { return result; } -map> TRestGeant4Event::GetEnergyInVolumePerProcessMap() const { +const map> TRestGeant4Event::GetEnergyInVolumePerProcessMap() const { map> result; for (const auto& [volume, particleProcessMap] : fEnergyInVolumePerParticlePerProcess) { for (const auto& [particle, processMap] : particleProcessMap) { @@ -1241,7 +1241,7 @@ map> TRestGeant4Event::GetEnergyInVolumePerProcessMa return result; } -map> TRestGeant4Event::GetEnergyInVolumePerParticleMap() const { +const map> TRestGeant4Event::GetEnergyInVolumePerParticleMap() const { map> result; for (const auto& [volume, particleProcessMap] : fEnergyInVolumePerParticlePerProcess) { for (const auto& [particle, processMap] : particleProcessMap) { @@ -1253,7 +1253,7 @@ map> TRestGeant4Event::GetEnergyInVolumePerParticleM return result; } -map TRestGeant4Event::GetEnergyPerProcessMap() const { +const map TRestGeant4Event::GetEnergyPerProcessMap() const { map result; for (const auto& [volume, particleProcessMap] : fEnergyInVolumePerParticlePerProcess) { for (const auto& [particle, processMap] : particleProcessMap) { @@ -1265,7 +1265,7 @@ map TRestGeant4Event::GetEnergyPerProcessMap() const { return result; } -map TRestGeant4Event::GetEnergyPerParticleMap() const { +const map TRestGeant4Event::GetEnergyPerParticleMap() const { map result; for (const auto& [volume, particleProcessMap] : fEnergyInVolumePerParticlePerProcess) { for (const auto& [particle, processMap] : particleProcessMap) { @@ -1277,7 +1277,7 @@ map TRestGeant4Event::GetEnergyPerParticleMap() const { return result; } -map TRestGeant4Event::GetEnergyInVolumeMap() const { +const map TRestGeant4Event::GetEnergyInVolumeMap() const { map result; for (const auto& [volume, particleProcessMap] : fEnergyInVolumePerParticlePerProcess) { for (const auto& [particle, processMap] : particleProcessMap) { @@ -1287,4 +1287,19 @@ map TRestGeant4Event::GetEnergyInVolumeMap() const { } } return result; -} \ No newline at end of file +} + +const map>>& +TRestGeant4Event::GetEnergyInVolumePerParticlePerProcessMap() const { + return fEnergyInVolumePerParticlePerProcess; +} + +void TRestGeant4Event::AddEnergyInVolumeForParticleForProcess(Double_t energy, const string& volumeName, + const string& particleName, + const string& processName) { + if (energy <= 0) { + return; + } + fEnergyInVolumePerParticlePerProcess[volumeName][particleName][processName] += energy; + fTotalDepositedEnergy += energy; +} From cfc22007e18a60a971209e2b9bd9370308b88633 Mon Sep 17 00:00:00 2001 From: lobis Date: Mon, 5 Sep 2022 18:25:46 +0200 Subject: [PATCH 082/100] TRestGeant4Event - removed `fMaxSubEventID` field and methods --- inc/TRestGeant4Event.h | 3 --- src/TRestGeant4Event.cxx | 1 - 2 files changed, 4 deletions(-) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index 971db25..6cd3e4a 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -127,8 +127,6 @@ class TRestGeant4Event : public TRestEvent { fEnergyInVolumePerParticlePerProcess; std::vector fTracks; - Int_t fMaxSubEventID; - public: void SetBoundaries(); void SetBoundaries(Double_t xMin, Double_t xMax, Double_t yMin, Double_t yMax, Double_t zMin, @@ -158,7 +156,6 @@ class TRestGeant4Event : public TRestEvent { inline const TRestGeant4Track& GetTrack(int n) const { return fTracks[n]; } inline TRestGeant4Track* GetTrackPointer(int n) { return &fTracks[n]; } TRestGeant4Track* GetTrackByID(Int_t trackID) const; - inline Int_t GetNumberOfSubEventIDTracks() const { return fMaxSubEventID + 1; } inline Double_t GetTotalDepositedEnergy() const { return fTotalDepositedEnergy; } inline Double_t GetEnergyDepositedInVolume(Int_t volID) const { return fVolumeDepositedEnergy[volID]; } diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index b27e34c..a8d28ce 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -75,7 +75,6 @@ void TRestGeant4Event::Initialize() { fTotalDepositedEnergy = 0; fSensitiveVolumeEnergy = 0; - fMaxSubEventID = 0; fMinX = 1e20; fMaxX = -1e20; From f3944cf724799255544f9c4405bb34303bacdedc Mon Sep 17 00:00:00 2001 From: lobis Date: Mon, 5 Sep 2022 18:29:44 +0200 Subject: [PATCH 083/100] removed const from maps ([] operator is not const) --- inc/TRestGeant4Event.h | 17 +++++++++++------ src/TRestGeant4Event.cxx | 14 +++++++------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/inc/TRestGeant4Event.h b/inc/TRestGeant4Event.h index 6cd3e4a..1da6782 100644 --- a/inc/TRestGeant4Event.h +++ b/inc/TRestGeant4Event.h @@ -165,13 +165,18 @@ class TRestGeant4Event : public TRestEvent { TVector3 GetLastPositionInVolume(Int_t volID) const; TVector3 GetPositionDeviationInVolume(Int_t volID) const; - const std::map>>& + std::map>> GetEnergyInVolumePerParticlePerProcessMap() const; - const std::map> GetEnergyInVolumePerProcessMap() const; - const std::map> GetEnergyInVolumePerParticleMap() const; - const std::map GetEnergyPerProcessMap() const; - const std::map GetEnergyPerParticleMap() const; - const std::map GetEnergyInVolumeMap() const; + std::map> GetEnergyInVolumePerProcessMap() const; + std::map> GetEnergyInVolumePerParticleMap() const; + std::map GetEnergyPerProcessMap() const; + std::map GetEnergyPerParticleMap() const; + std::map GetEnergyInVolumeMap() const; + + inline Double_t GetEnergyInVolume(const std::string& volumeName) const { + auto energyMap = GetEnergyInVolumeMap(); + return energyMap[volumeName]; + } TRestHits GetHits(Int_t volID = -1) const; inline TRestHits GetHitsInVolume(Int_t volID) const { return GetHits(volID); } diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index a8d28ce..2a140a9 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -1228,7 +1228,7 @@ const set TRestGeant4Event::GetUniqueParticles() const { return result; } -const map> TRestGeant4Event::GetEnergyInVolumePerProcessMap() const { +map> TRestGeant4Event::GetEnergyInVolumePerProcessMap() const { map> result; for (const auto& [volume, particleProcessMap] : fEnergyInVolumePerParticlePerProcess) { for (const auto& [particle, processMap] : particleProcessMap) { @@ -1240,7 +1240,7 @@ const map> TRestGeant4Event::GetEnergyInVolumePerPro return result; } -const map> TRestGeant4Event::GetEnergyInVolumePerParticleMap() const { +map> TRestGeant4Event::GetEnergyInVolumePerParticleMap() const { map> result; for (const auto& [volume, particleProcessMap] : fEnergyInVolumePerParticlePerProcess) { for (const auto& [particle, processMap] : particleProcessMap) { @@ -1252,7 +1252,7 @@ const map> TRestGeant4Event::GetEnergyInVolumePerPar return result; } -const map TRestGeant4Event::GetEnergyPerProcessMap() const { +map TRestGeant4Event::GetEnergyPerProcessMap() const { map result; for (const auto& [volume, particleProcessMap] : fEnergyInVolumePerParticlePerProcess) { for (const auto& [particle, processMap] : particleProcessMap) { @@ -1264,7 +1264,7 @@ const map TRestGeant4Event::GetEnergyPerProcessMap() const { return result; } -const map TRestGeant4Event::GetEnergyPerParticleMap() const { +map TRestGeant4Event::GetEnergyPerParticleMap() const { map result; for (const auto& [volume, particleProcessMap] : fEnergyInVolumePerParticlePerProcess) { for (const auto& [particle, processMap] : particleProcessMap) { @@ -1276,7 +1276,7 @@ const map TRestGeant4Event::GetEnergyPerParticleMap() const { return result; } -const map TRestGeant4Event::GetEnergyInVolumeMap() const { +map TRestGeant4Event::GetEnergyInVolumeMap() const { map result; for (const auto& [volume, particleProcessMap] : fEnergyInVolumePerParticlePerProcess) { for (const auto& [particle, processMap] : particleProcessMap) { @@ -1288,8 +1288,8 @@ const map TRestGeant4Event::GetEnergyInVolumeMap() const { return result; } -const map>>& -TRestGeant4Event::GetEnergyInVolumePerParticlePerProcessMap() const { +map>> TRestGeant4Event::GetEnergyInVolumePerParticlePerProcessMap() + const { return fEnergyInVolumePerParticlePerProcess; } From e5c58ad2ca41b2b256088f2cec0fa024a8beb76b Mon Sep 17 00:00:00 2001 From: lobis Date: Mon, 5 Sep 2022 18:34:44 +0200 Subject: [PATCH 084/100] skipping some tests for now --- test/src/Geant4AnalysisProcesses.cxx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/src/Geant4AnalysisProcesses.cxx b/test/src/Geant4AnalysisProcesses.cxx index 88aac9a..ab68718 100644 --- a/test/src/Geant4AnalysisProcesses.cxx +++ b/test/src/Geant4AnalysisProcesses.cxx @@ -13,6 +13,8 @@ const auto vetoAnalysisRml = filesPath / "TRestGeant4VetoAnalysisProcessExample. const auto vetoAnalysisRestG4Run = filesPath / "VetoAnalysisGeant4Run.root"; TEST(TRestGeant4VetoAnalysisProcess, TestFiles) { + GTEST_SKIP(); + cout << "Test files path: " << filesPath << endl; // Check dir exists and is a directory @@ -50,6 +52,8 @@ TEST(TRestGeant4VetoAnalysisProcess, FromRml) { } TEST(TRestGeant4VetoAnalysisProcess, Simulation) { + GTEST_SKIP(); + TRestGeant4VetoAnalysisProcess process(vetoAnalysisRml.c_str()); EXPECT_TRUE(process.GetVetoVolumesExpression() == "^scintillatorVolume"); From 8352ad0598eecb925ad400af41748cc7d904f086 Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 6 Sep 2022 11:49:14 +0200 Subject: [PATCH 085/100] TRestGeant4PhysicsInfo - added process type info --- inc/TRestGeant4PhysicsInfo.h | 9 +++++++-- src/TRestGeant4PhysicsInfo.cxx | 17 ++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/inc/TRestGeant4PhysicsInfo.h b/inc/TRestGeant4PhysicsInfo.h index 1089b00..d190497 100644 --- a/inc/TRestGeant4PhysicsInfo.h +++ b/inc/TRestGeant4PhysicsInfo.h @@ -11,7 +11,7 @@ class G4VProcess; class TRestGeant4PhysicsInfo { - ClassDef(TRestGeant4PhysicsInfo, 1); + ClassDef(TRestGeant4PhysicsInfo, 2); private: std::map fProcessNamesMap = {}; @@ -20,11 +20,13 @@ class TRestGeant4PhysicsInfo { std::map fParticleNamesMap = {}; std::map fParticleNamesReverseMap = {}; + std::map fProcessTypesMap = {}; // process name -> process type + std::mutex fMutex; //! public: TString GetProcessName(Int_t id) const; Int_t GetProcessID(const TString& processName) const; - void InsertProcessName(Int_t id, const TString& processName); + void InsertProcessName(Int_t id, const TString& processName, const TString& processType); std::set GetAllParticles() const; TString GetParticleName(Int_t id) const; @@ -32,6 +34,9 @@ class TRestGeant4PhysicsInfo { void InsertParticleName(Int_t id, const TString& particleName); std::set GetAllProcesses() const; + TString GetProcessType(const TString& processName) const; + std::set GetAllProcessTypes() const; + public: inline TRestGeant4PhysicsInfo() = default; inline ~TRestGeant4PhysicsInfo() = default; diff --git a/src/TRestGeant4PhysicsInfo.cxx b/src/TRestGeant4PhysicsInfo.cxx index afd3d52..0f833ae 100644 --- a/src/TRestGeant4PhysicsInfo.cxx +++ b/src/TRestGeant4PhysicsInfo.cxx @@ -23,6 +23,14 @@ std::set TRestGeant4PhysicsInfo::GetAllProcesses() const { return processes; } +std::set TRestGeant4PhysicsInfo::GetAllProcessTypes() const { + set types = {}; + for (const auto& [_, type] : fProcessTypesMap) { + types.insert(type); + } + return types; +} + void TRestGeant4PhysicsInfo::PrintParticles() const { const auto particleNames = GetAllParticles(); cout << "Particles:" << endl; @@ -46,13 +54,16 @@ void TRestGeant4PhysicsInfo::Print() const { PrintProcesses(); } -void TRestGeant4PhysicsInfo::InsertProcessName(Int_t id, const TString& processName) { +void TRestGeant4PhysicsInfo::InsertProcessName(Int_t id, const TString& processName, + const TString& processType) { if (fProcessNamesMap.count(id) > 0) { return; } std::lock_guard lock(fMutex); fProcessNamesMap[id] = processName; fProcessNamesReverseMap[processName] = id; + + fProcessTypesMap[processName] = processType; } void TRestGeant4PhysicsInfo::InsertParticleName(Int_t id, const TString& particleName) { @@ -87,3 +98,7 @@ TString TRestGeant4PhysicsInfo::GetParticleName(Int_t id) const { Int_t TRestGeant4PhysicsInfo::GetParticleID(const TString& processName) const { return GetOrDefaultMapValueFromKey(&fParticleNamesReverseMap, processName); } + +TString TRestGeant4PhysicsInfo::GetProcessType(const TString& processName) const { + return GetOrDefaultMapValueFromKey(&fProcessTypesMap, processName); +} From f49eb4645383acaecbff98222e728ebd822b2f37 Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 6 Sep 2022 14:11:46 +0200 Subject: [PATCH 086/100] updated visualization --- inc/TRestGeant4EventViewer.h | 7 +- src/TRestGeant4EventViewer.cxx | 253 +++++++++++++++++++++++---------- 2 files changed, 178 insertions(+), 82 deletions(-) diff --git a/inc/TRestGeant4EventViewer.h b/inc/TRestGeant4EventViewer.h index 18f4fcb..e9d40a5 100644 --- a/inc/TRestGeant4EventViewer.h +++ b/inc/TRestGeant4EventViewer.h @@ -22,12 +22,13 @@ class TRestGeant4EventViewer : public TRestEveEventViewer { private: std::vector fHitConnectors; - TRestGeant4Event* fG4Event; + TRestGeant4Event* fG4Event = nullptr; + const TRestGeant4Metadata* fG4Metadata = nullptr; public: void Initialize(); void DeleteCurrentEvent(); - void AddEvent(TRestEvent* ev); + void AddEvent(TRestEvent* event); void NextTrackVertex(Int_t trkID, TVector3 to); void AddTrack(Int_t trkID, Int_t parentID, TVector3 from, TString name); @@ -41,6 +42,6 @@ class TRestGeant4EventViewer : public TRestEveEventViewer { // Destructor ~TRestGeant4EventViewer(); - ClassDef(TRestGeant4EventViewer, 1); // class inherited from TRestEventViewer + ClassDef(TRestGeant4EventViewer, 2); // class inherited from TRestEventViewer }; #endif diff --git a/src/TRestGeant4EventViewer.cxx b/src/TRestGeant4EventViewer.cxx index 95528d3..fd2c742 100644 --- a/src/TRestGeant4EventViewer.cxx +++ b/src/TRestGeant4EventViewer.cxx @@ -14,7 +14,8 @@ #include "TRestGeant4EventViewer.h" -#include "TRestStringOutput.h" +#include +#include using namespace std; @@ -35,105 +36,198 @@ void TRestGeant4EventViewer::Initialize() { } void TRestGeant4EventViewer::DeleteCurrentEvent() { - // cout<<"Removing event"< 10 ? 10 : width); + width = (width < 1 ? 1 : width); + config.fLineWidth = TMath::Log10(track.GetInitialKineticEnergy() / 10); - for (int i = 0; i < fG4Event->GetNumberOfTracks(); i++) { - const auto& g4Track = fG4Event->GetTrack(i); - const auto& g4Hits = g4Track.GetHits(); - Int_t nHits = g4Track.GetNumberOfHits(); - for (int j = 0; j < nHits; j++) { - Double_t eDep = g4Hits.GetEnergy(j); - if (eDep > eDepMax) eDepMax = eDep; - if (eDep < eDepMin) eDepMin = eDep; + // line style - // cout<<" track "<AddLine({static_cast(GEOM_SCALE * hits.GetPosition(i).x()), + static_cast(GEOM_SCALE * hits.GetPosition(i).y()), + static_cast(GEOM_SCALE * hits.GetPosition(i).z())}, // + {static_cast(GEOM_SCALE * hits.GetPosition(i + 1).x()), + static_cast(GEOM_SCALE * hits.GetPosition(i + 1).y()), + static_cast(GEOM_SCALE * hits.GetPosition(i + 1).z())}); + + const auto config = GetTrackVisualConfiguration(track); + lineSet->SetMainColor(config.fColor); + lineSet->SetLineColor(config.fColor); + lineSet->SetLineStyle(config.fLineStyle); + lineSet->SetLineWidth(config.fLineWidth); } - cout << "TRestGeant4EventViewer::AddEvent. Total EDep " << totalEDep << endl; - - Double_t slope = (fMaxRadius - fMinRadius) / (eDepMax - eDepMin); - Double_t bias = fMinRadius - slope * eDepMin; - - Int_t textAdded = 0; - for (int trkID = 1; trkID < fG4Event->GetNumberOfTracks() + 1; trkID++) { - const auto& g4Track = fG4Event->GetTrackByID(trkID); - - Int_t parentID = g4Track->GetParentID(); - TVector3 origin = g4Track->GetTrackOrigin(); - - // Building track name - Double_t eKin = g4Track->GetInitialKineticEnergy(); - // cout << "eKin: " << eKin << endl; - TString ptlName = g4Track->GetParticleName(); - // cout << "ptlName: " << ptlName << endl; - char pcleStr[64]; - sprintf(pcleStr, "Track ID : %d %s (%6.2lf keV)", trkID, ptlName.Data(), eKin); - - char markerStr[256]; - sprintf(markerStr, " %s (%6.2lf keV). Position (%4.1lf,%4.1lf,%4.1lf)", ptlName.Data(), eKin, - origin.X(), origin.Y(), origin.Z()); - - if (parentID == 0) { - // cout << " Parent ID : 0" << endl; - char evInfoStr[256]; - sprintf(evInfoStr, "%s. EventID = %d at position (%4.2lf, %4.2lf, %4.2lf) mm", ptlName.Data(), - fG4Event->GetID(), origin.X(), origin.Y(), origin.Z()); - this->AddParentTrack(trkID, origin, pcleStr); - if (fG4Event->GetSubID() == 0) - this->AddText(evInfoStr, origin); - else if (!textAdded) { - textAdded = 1; - sprintf(evInfoStr, "%s. EventID = %d at position (%4.2lf, %4.2lf, %4.2lf) mm", - fG4Event->GetSubEventTag().Data(), fG4Event->GetID(), origin.X(), origin.Y(), - origin.Z()); - this->AddText(evInfoStr, origin); - } - } else { - // cout << "Adding track : " << trkID << " parent : " << parentID << " pt: " << pcleStr << endl; - this->AddTrack(trkID, parentID, origin, pcleStr); - } + return lineSet; +} - // cout << "Adding marker" << endl; - this->AddMarker(trkID, origin, markerStr); +struct HitsVisualConfiguration { + Color_t fColor = kBlack; + Style_t fMarkerStyle = 1; + Size_t fMarkerSize = 1.0; +}; + +HitsVisualConfiguration GetHitsVisualConfiguration(const TString& processNameOrType) { + auto config = HitsVisualConfiguration(); + // based on particle type + if (processNameOrType.EqualTo("Electromagnetic")) { + config.fColor = kRed; + } else if (processNameOrType.EqualTo("Init")) { + // custom process (not Geant4) + config.fColor = kWhite; + } - const auto& g4Hits = g4Track->GetHits(); - Int_t nHits = g4Track->GetNumberOfHits(); + // based on particle name + if (processNameOrType.EqualTo("Transportation")) { + config.fColor = kWhite; + } else if (processNameOrType.EqualTo("Init")) { + // custom process (not Geant4) + config.fColor = kWhite; + } else if (processNameOrType.EqualTo("hadElastic")) { + config.fColor = kOrange; + } else if (processNameOrType.EqualTo("neutronInelastic")) { + config.fColor = kGreen; + config.fMarkerStyle = 4; + } else if (processNameOrType.EqualTo("nCapture")) { + config.fColor = kBlue; + config.fMarkerStyle = 2; + config.fMarkerSize = 4.0; + } + return config; +} + +void TRestGeant4EventViewer::AddEvent(TRestEvent* event) { + DeleteCurrentEvent(); + + fG4Event = (TRestGeant4Event*)event; + fG4Metadata = fG4Event->GetGeant4Metadata(); + if (fG4Metadata == nullptr) { + cerr << "TRestGeant4EventViewer::Initialize. No TRestGeant4Metadata found in TRestGeant4Event" + << endl; + exit(1); + } - // cout << "Adding hits" << endl; - for (int i = 0; i < nHits; i++) { - Double_t x = g4Hits.GetX(i); - Double_t y = g4Hits.GetY(i); - Double_t z = g4Hits.GetZ(i); + size_t trackCounter = 0; + size_t hitsCounter = 0; - this->NextTrackVertex(trkID, TVector3(x, y, z)); + map linesSet; + map hitsPoints; + map hitsType; - Double_t eDep = g4Hits.GetEnergy(i); + auto trackList = new TEveElementList("Tracks"); + gEve->AddElement(trackList); + auto hitsList = new TEveElementList("Hits"); + gEve->AddElement(hitsList); - if (eDep > 0) { - Double_t radius = slope * eDep + bias; - AddSphericalHit(x, y, z, radius, eDep); + const auto& physicsInfo = fG4Metadata->GetGeant4PhysicsInfo(); + + for (const auto& track : fG4Event->GetTracks()) { + if (track.GetInitialKineticEnergy() < 1.0 || track.GetLength() < 0.1) { + continue; + } + + auto line = GetTrackEveDrawable(track); + linesSet[track.GetTrackID()] = line; + TEveElement* parentLine = trackList; + if (linesSet.count(track.GetParentID())) { + parentLine = linesSet.at(track.GetParentID()); + } + gEve->AddElement(line, parentLine); + trackCounter++; + + const auto& hits = track.GetHits(); + for (int i = 0; i < hits.GetNumberOfHits(); i++) { + const auto& processName = physicsInfo.GetProcessName(hits.GetProcess(i)); + const auto& processType = physicsInfo.GetProcessType(processName); + const auto& position = hits.GetPosition(i); + + if (hitsType.count(processType) == 0) { + hitsType[processType] = new TEveElementList(processType); + gEve->AddElement(hitsType[processType], hitsList); + } + if (hitsPoints.count(processName) == 0) { + hitsPoints[processName] = new TEvePointSet(processName); + auto hitPoints = hitsPoints.at(processName); + auto hitsVisualConfig = GetHitsVisualConfiguration(processName); + hitPoints->SetMarkerColor(hitsVisualConfig.fColor); + hitPoints->SetMarkerStyle(hitsVisualConfig.fMarkerStyle); + hitPoints->SetMarkerSize(hitsVisualConfig.fMarkerSize); + + gEve->AddElement(hitPoints, hitsType[processType]); } + hitsPoints.at(processName) + ->SetNextPoint(GEOM_SCALE * position.X(), GEOM_SCALE * position.Y(), + GEOM_SCALE * position.Z()); + hitsCounter++; } } - // cout << "Updating" << endl; + + // Add event text + const auto& firstTrack = fG4Event->GetTracks().front(); + TVector3 position = {firstTrack.GetInitialPosition().X(), firstTrack.GetInitialPosition().Y(), + firstTrack.GetInitialPosition().Z()}; + AddText( + TString::Format("Event ID: %d (SubID: %d) | Position: (%4.2lf, %4.2lf, %4.2lf) mm", fG4Event->GetID(), + fG4Event->GetSubID(), position.X(), position.Y(), position.Z()), + position); Update(); } @@ -182,7 +276,8 @@ void TRestGeant4EventViewer::AddTrack(Int_t trkID, Int_t parentID, TVector3 from fHitConnectors[parentID]->AddElement(fHitConnectors[trkID]); else { RESTWarning << "Parent ID: " << parentID << " of track " << trkID << " was not found!" << RESTendl; - RESTWarning << "This might be solved by enabling TRestGeant4Metadata::fRegisterEmptyTracks" << RESTendl; + RESTWarning << "This might be solved by enabling TRestGeant4Metadata::fRegisterEmptyTracks" + << RESTendl; } } From 46f262351e4618373d01d6c060e68c48e2040266 Mon Sep 17 00:00:00 2001 From: Luis Obis Date: Tue, 6 Sep 2022 21:17:09 +0200 Subject: [PATCH 087/100] working on remove unwanted tracks bug --- src/TRestGeant4Metadata.cxx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/TRestGeant4Metadata.cxx b/src/TRestGeant4Metadata.cxx index 31e90d6..1c4f185 100644 --- a/src/TRestGeant4Metadata.cxx +++ b/src/TRestGeant4Metadata.cxx @@ -1240,6 +1240,20 @@ void TRestGeant4Metadata::PrintMetadata() { for (int n = 0; n < GetNumberOfBiasingVolumes(); n++) { GetBiasingVolume(n).PrintBiasingVolume(); } + + if (GetRemoveUnwantedTracks()) { + RESTMetadata << "removeUnwantedTracks is enabled " + << (fRemoveUnwantedTracksKeepZeroEnergyTracks ? "with" : "without") + << " keeping zero energy tracks" << RESTendl; + + /* + RESTMetadata << "keep volumes for removeUnwantedTracks:" << RESTendl; + for (const auto& volume : fRemoveUnwantedTracksVolumesToKeep) { + RESTMetadata << "\t" << volume << RESTendl; + } + */ + } + RESTMetadata << "+++++" << RESTendl; } From 7821cd41d903d2f58de5f5d78b897d4e7e9f3e5a Mon Sep 17 00:00:00 2001 From: lobis Date: Fri, 9 Sep 2022 13:04:44 +0200 Subject: [PATCH 088/100] TRestGeant4Event - added check on GetTrackByID --- src/TRestGeant4Event.cxx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index 2a140a9..dfcd444 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -193,7 +193,11 @@ TVector3 TRestGeant4Event::GetLastPositionInVolume(Int_t volID) const { TRestGeant4Track* TRestGeant4Event::GetTrackByID(Int_t trackID) const { if (fTrackIDToTrackIndex.count(trackID) > 0) { - return const_cast(&fTracks[fTrackIDToTrackIndex.at(trackID)]); + TRestGeant4Track* result = const_cast(&fTracks[fTrackIDToTrackIndex.at(trackID)]); + if (result->GetTrackID() != trackID) { + cerr << "TRestGeant4Event::GetTrackByID - ERROR: trackIDToTrackIndex map is corrupted" << endl; + exit(1); + } } return nullptr; From 5770e60347fbe7c45d5c3339a2786ca18c3d8b9c Mon Sep 17 00:00:00 2001 From: lobis Date: Fri, 9 Sep 2022 13:37:46 +0200 Subject: [PATCH 089/100] updated GetTrackByID --- src/TRestGeant4Event.cxx | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index dfcd444..5d2ca87 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -192,23 +192,15 @@ TVector3 TRestGeant4Event::GetLastPositionInVolume(Int_t volID) const { } TRestGeant4Track* TRestGeant4Event::GetTrackByID(Int_t trackID) const { + TRestGeant4Track* result = nullptr; if (fTrackIDToTrackIndex.count(trackID) > 0) { - TRestGeant4Track* result = const_cast(&fTracks[fTrackIDToTrackIndex.at(trackID)]); + result = const_cast(&fTracks[fTrackIDToTrackIndex.at(trackID)]); if (result->GetTrackID() != trackID) { cerr << "TRestGeant4Event::GetTrackByID - ERROR: trackIDToTrackIndex map is corrupted" << endl; exit(1); } } - return nullptr; - - /* - for (const auto& track : fTracks) { - if (track.GetTrackID() == trackID) { - return const_cast(&track); - } - } - return nullptr; - */ + return result; } /////////////////////////////////////////////// From 8170dab78ee9fa3adb4df65939b5808e6a2facf4 Mon Sep 17 00:00:00 2001 From: lobis Date: Fri, 9 Sep 2022 13:38:41 +0200 Subject: [PATCH 090/100] added new check to TRestGeant4Event::GetTrackByID --- src/TRestGeant4Event.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index 5d2ca87..20e8be3 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -195,7 +195,7 @@ TRestGeant4Track* TRestGeant4Event::GetTrackByID(Int_t trackID) const { TRestGeant4Track* result = nullptr; if (fTrackIDToTrackIndex.count(trackID) > 0) { result = const_cast(&fTracks[fTrackIDToTrackIndex.at(trackID)]); - if (result->GetTrackID() != trackID) { + if (result == nullptr || result->GetTrackID() != trackID) { cerr << "TRestGeant4Event::GetTrackByID - ERROR: trackIDToTrackIndex map is corrupted" << endl; exit(1); } From 8598b5e919458fa83e5eb4aae800ce9d7d0d32ce Mon Sep 17 00:00:00 2001 From: lobis Date: Sat, 10 Sep 2022 17:46:13 +0200 Subject: [PATCH 091/100] removed bad comment --- inc/TRestGeant4Track.h | 1 - 1 file changed, 1 deletion(-) diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index ea07974..3c3d993 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -41,7 +41,6 @@ class TRestGeant4Track { std::vector fSecondaryTrackIDs; - /* Time is stored in seconds */ Double_t fGlobalTimestamp; Double_t fTimeLength; From 9646c613e7b598270adec18924708496fb9245ef Mon Sep 17 00:00:00 2001 From: lobis Date: Sat, 10 Sep 2022 20:46:54 +0200 Subject: [PATCH 092/100] removed fmt dependency and updated prints --- src/TRestGeant4Event.cxx | 37 ++++++----------- src/TRestGeant4Track.cxx | 55 +++++++++++--------------- src/TRestGeant4VetoAnalysisProcess.cxx | 26 +++++------- 3 files changed, 44 insertions(+), 74 deletions(-) diff --git a/src/TRestGeant4Event.cxx b/src/TRestGeant4Event.cxx index 20e8be3..80a1cb0 100644 --- a/src/TRestGeant4Event.cxx +++ b/src/TRestGeant4Event.cxx @@ -22,13 +22,10 @@ #include #include #include -#include -#include #include "TRestGeant4Metadata.h" using namespace std; -using namespace fmt; ClassImp(TRestGeant4Event); @@ -1117,39 +1114,31 @@ void TRestGeant4Event::PrintActiveVolumes() const { } } -// TODO: Find how to place this so that we don't need to copy it in every source file -template <> -struct fmt::formatter : formatter { - auto format(TVector3 c, format_context& ctx) { - string s = fmt::format("({:0.3f}, {:0.3f}, {:0.3f})", c.X(), c.Y(), c.Z()); - return formatter::format(s, ctx); - } -}; - void TRestGeant4Event::PrintEvent(int maxTracks, int maxHits) const { TRestEvent::PrintEvent(); - print("- Total deposited energy: {}\n", ToEnergyString(fTotalDepositedEnergy)); - print(fg(color::medium_sea_green), "- Sensitive detectors total energy: {}\n", - ToEnergyString(fSensitiveVolumeEnergy)); + cout << "- Total deposited energy: " << ToEnergyString(fTotalDepositedEnergy) << endl; + cout << "- Sensitive detectors total energy: " << ToEnergyString(fSensitiveVolumeEnergy) << endl; - print("- Primary source position: {} mm\n", fPrimaryPosition); + cout << "- Primary source position: " << VectorToString(fPrimaryPosition) << "{} mm" << endl; for (int i = 0; i < GetNumberOfPrimaries(); i++) { - const auto sourceNumberString = - GetNumberOfPrimaries() <= 1 ? "" : format(" {} of {}", i + 1, GetNumberOfPrimaries()); - print(" - Source{} primary particle: {}\n", sourceNumberString, fPrimaryParticleNames[i]); - print(" Direction: {}\n", fPrimaryDirections[i]); - print(fg(color::light_golden_rod_yellow), " Energy: {}\n", ToEnergyString(fPrimaryEnergies[i])); + const char* sourceNumberString = + GetNumberOfPrimaries() <= 1 ? "" + : TString::Format(" %d of %zu", i + 1, GetNumberOfPrimaries()).Data(); + cout << " - Source" << sourceNumberString << " primary particle: " << fPrimaryParticleNames[i] + << endl; + cout << " Direction: " << VectorToString(fPrimaryDirections[i]) << endl; + cout << " Energy: " << ToEnergyString(fPrimaryEnergies[i]) << endl; } - print("\n"); - print("Total number of tracks: {}\n", GetNumberOfTracks()); + cout << endl; + cout << "Total number of tracks: " << GetNumberOfTracks() << endl; int nTracks = GetNumberOfTracks(); if (maxTracks > 0 && maxTracks < GetNumberOfTracks()) { nTracks = min(maxTracks, int(GetNumberOfTracks())); - print(fg(color::light_slate_gray), "Printing only the first {} tracks\n", nTracks); + cout << "Printing only the first " << nTracks << " tracks" << endl; } for (int i = 0; i < nTracks; i++) { diff --git a/src/TRestGeant4Track.cxx b/src/TRestGeant4Track.cxx index 247955b..b34299a 100644 --- a/src/TRestGeant4Track.cxx +++ b/src/TRestGeant4Track.cxx @@ -17,20 +17,16 @@ #include "TRestGeant4Track.h" -#include -#include - #include "TRestGeant4Event.h" #include "TRestGeant4Metadata.h" using namespace std; -using namespace fmt; ClassImp(TRestGeant4Track); -TRestGeant4Track::TRestGeant4Track() {} +TRestGeant4Track::TRestGeant4Track() = default; -TRestGeant4Track::~TRestGeant4Track() {} +TRestGeant4Track::~TRestGeant4Track() = default; Int_t TRestGeant4Track::GetProcessID(const TString& processName) const { const TRestGeant4Metadata* metadata = GetGeant4Metadata(); @@ -115,31 +111,26 @@ size_t TRestGeant4Track::GetNumberOfPhysicalHits(Int_t volID) const { return numberOfHits; } -template <> -struct fmt::formatter : formatter { - auto format(TVector3 c, format_context& ctx) { - string s = fmt::format("({:0.3f}, {:0.3f}, {:0.3f})", c.X(), c.Y(), c.Z()); - return formatter::format(s, ctx); - } -}; - void TRestGeant4Track::PrintTrack(size_t maxHits) const { - print(fg(color::green_yellow), - " * TrackID: {} - Particle: {} - ParentID: {}{} - Created by '{}' with initial " - "KE of {}\n", - fTrackID, fParticleName, fParentID, - (GetParentTrack() != nullptr ? format(" - Parent particle: {}", GetParentTrack()->GetParticleName()) - : ""), - fCreatorProcess, ToEnergyString(fInitialKineticEnergy)); - print(fg(color::green_yellow), - " Initial position {} mm at time {} - Time length of {} and spatial length of {}\n", - fInitialPosition, ToTimeString(fGlobalTimestamp), ToTimeString(fTimeLength), - ToLengthString(fLength)); + cout + << " * TrackID: " << fTrackID << " - Particle: " << fParticleName << " - ParentID: " << fParentID + << "" + << (GetParentTrack() != nullptr + ? TString::Format(" - Parent particle: {}", GetParentTrack()->GetParticleName().Data()).Data() + : "") + << " - Created by '" << fCreatorProcess + << "' with initial " + "KE of " + << ToEnergyString(fInitialKineticEnergy) << "" << endl; + + cout << " Initial position " << VectorToString(fInitialPosition) << " mm at time " + << ToTimeString(fGlobalTimestamp) << " - Time length of " << ToTimeString(fTimeLength) + << " and spatial length of " << ToLengthString(fLength) << endl; size_t nHits = GetNumberOfHits(); if (maxHits > 0 && maxHits < nHits) { nHits = min(maxHits, nHits); - print(fg(color::light_slate_gray), "Printing only the first {} hits of the track\n", nHits); + cout << "Printing only the first " << nHits << " hits of the track" << endl; } const TRestGeant4Metadata* metadata = GetGeant4Metadata(); @@ -158,13 +149,11 @@ void TRestGeant4Track::PrintTrack(size_t maxHits) const { // in case process name is not found, use ID volumeName = TString(std::to_string(fHits.GetHitVolume(i))); } - - print( - (fHits.GetEnergy(i) > 0 ? fg(color::light_salmon) : fg(color::antique_white)), - " - Hit {} - Energy: {} - Process: {} - Volume: {} - Position: {} mm - Time: {} - KE: {}\n", - i, ToEnergyString(fHits.GetEnergy(i)), processName, volumeName, - TVector3(fHits.GetX(i), fHits.GetY(i), fHits.GetZ(i)), ToTimeString(fHits.GetTime(i)), - ToEnergyString(fHits.GetKineticEnergy(i))); + cout << " - Hit " << i << " - Energy: " << ToEnergyString(fHits.GetEnergy(i)) + << " - Process: " << processName << " - Volume: " << volumeName + << " - Position: " << VectorToString(TVector3(fHits.GetX(i), fHits.GetY(i), fHits.GetZ(i))) + << " mm - Time: " << ToTimeString(fHits.GetTime(i)) + << " - KE: " << ToEnergyString(fHits.GetKineticEnergy(i)) << endl; } } diff --git a/src/TRestGeant4VetoAnalysisProcess.cxx b/src/TRestGeant4VetoAnalysisProcess.cxx index bea483a..f8e29db 100644 --- a/src/TRestGeant4VetoAnalysisProcess.cxx +++ b/src/TRestGeant4VetoAnalysisProcess.cxx @@ -65,10 +65,6 @@ #include "TRestGeant4VetoAnalysisProcess.h" -#include -#include - -using namespace fmt; using namespace std; ClassImp(TRestGeant4VetoAnalysisProcess); @@ -333,15 +329,6 @@ void TRestGeant4VetoAnalysisProcess::InitFromConfigFile() { fVetoQuenchingFactor = GetDblParameterWithUnits("quenchingFactor", fVetoQuenchingFactor); } -// TODO: Find how to place this so that we don't need to copy it in every source file -template <> -struct fmt::formatter : formatter { - auto format(const TVector3& c, format_context& ctx) { - string s = fmt::format("({:0.3f}, {:0.3f}, {:0.3f})", c.X(), c.Y(), c.Z()); - return formatter::format(s, ctx); - } -}; - void TRestGeant4VetoAnalysisProcess::PrintMetadata() { BeginPrintProcess(); @@ -370,7 +357,9 @@ void TRestGeant4VetoAnalysisProcess::PrintMetadata() { const auto& vetoName = fVetoVolumes[i]; const auto& vetoPosition = geometryInfo.GetPosition(vetoName); - print(" - Veto volume: {} - name: '{}' - position: {} mm\n", i, vetoName, vetoPosition); + cout << TString::Format(" - Veto volume: %d - name: '%s' - position: %s mm", i, vetoName.Data(), + VectorToString(vetoPosition).c_str()) + << endl; if (fVetoDetectorVolumes.empty()) { continue; @@ -379,9 +368,12 @@ void TRestGeant4VetoAnalysisProcess::PrintMetadata() { const auto& vetoDetectorName = fVetoDetectorVolumes[i]; const auto& vetoDetectorPosition = geometryInfo.GetPosition(vetoDetectorName); - print(" Veto detector name: '{}' - position: {} mm\n", vetoDetectorName, vetoDetectorPosition); + cout << TString::Format(" Veto detector name: '%s' - position: %s mm", vetoDetectorName.Data(), + VectorToString(vetoDetectorPosition).c_str()) + << endl; - print(" Boundary position: {} mm - direction: {}\n", fVetoDetectorBoundaryPosition.at(vetoName), - fVetoDetectorBoundaryDirection.at(vetoName)); + cout << TString::Format(" Boundary position: %s mm - direction: %s", + VectorToString(fVetoDetectorBoundaryPosition.at(vetoName)).c_str(), + VectorToString(fVetoDetectorBoundaryDirection.at(vetoName)).c_str()); } } From cf762711372963330b66166e87aa1738d2ee96e5 Mon Sep 17 00:00:00 2001 From: lobis Date: Sun, 11 Sep 2022 00:11:20 +0200 Subject: [PATCH 093/100] TRestGeant4Track - added methods to quickly retrieve first and last volume --- inc/TRestGeant4Track.h | 3 +++ src/TRestGeant4Track.cxx | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index 3c3d993..9d4d63e 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -85,6 +85,9 @@ class TRestGeant4Track { inline Double_t GetEnergy() const { return fHits.GetEnergy(); } inline Double_t GetLength() const { return fLength; } + TString GetInitialVolume() const; + TString GetFinalVolume() const; + inline std::vector GetSecondaryTrackIDs() const { return fSecondaryTrackIDs; } std::vector GetSecondaryTracks() const; diff --git a/src/TRestGeant4Track.cxx b/src/TRestGeant4Track.cxx index b34299a..a03d74f 100644 --- a/src/TRestGeant4Track.cxx +++ b/src/TRestGeant4Track.cxx @@ -202,3 +202,22 @@ vector TRestGeant4Track::GetSecondaryTracks() const { } return secondaryTracks; } + +TString TRestGeant4Track::GetInitialVolume() const { + const auto metadata = GetGeant4Metadata(); + if (metadata == nullptr) { + return ""; + } + const auto& hits = GetHits(); + return GetGeant4Metadata()->GetGeant4GeometryInfo().GetVolumeFromID(hits.GetVolumeId(0)); +} + +TString TRestGeant4Track::GetFinalVolume() const { + const auto metadata = GetGeant4Metadata(); + if (metadata == nullptr) { + return ""; + } + const auto& hits = GetHits(); + return GetGeant4Metadata()->GetGeant4GeometryInfo().GetVolumeFromID( + hits.GetVolumeId(hits.GetNumberOfHits() - 1)); +} From a9ceb7443d010a9bf1027685663652917fb4a238 Mon Sep 17 00:00:00 2001 From: lobis Date: Sun, 11 Sep 2022 16:08:10 +0200 Subject: [PATCH 094/100] Added alternative name `GetChildrenTracks` to `GetSecondaryTracks` (same method) --- inc/TRestGeant4Track.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index 9d4d63e..6601392 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -58,8 +58,6 @@ class TRestGeant4Track { inline const TRestGeant4Event* GetEvent() const { return fEvent; } const TRestGeant4Metadata* GetGeant4Metadata() const; - TRestGeant4Track* GetParentTrack() const; - inline void SetEvent(TRestGeant4Event* event) { fEvent = event; } inline void SetHits(const TRestGeant4Hits& hits) { fHits = hits; @@ -90,6 +88,9 @@ class TRestGeant4Track { inline std::vector GetSecondaryTrackIDs() const { return fSecondaryTrackIDs; } std::vector GetSecondaryTracks() const; + inline std::vector GetChildrenTracks() const { return GetSecondaryTracks(); } + + TRestGeant4Track* GetParentTrack() const; inline TVector3 GetTrackOrigin() const { return GetInitialPosition(); } From bfa94029c43531821d2a6af2039157fb8a987acd Mon Sep 17 00:00:00 2001 From: lobis Date: Sun, 11 Sep 2022 16:35:03 +0200 Subject: [PATCH 095/100] TRestGeant4Track - added method to get energy in volume recursively for all children --- inc/TRestGeant4Track.h | 2 ++ src/TRestGeant4Track.cxx | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index 6601392..1888f62 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -118,6 +118,8 @@ class TRestGeant4Track { return ContainsProcessInVolume(processName, -1); } + Double_t GetEnergyInVolume(const TString& volumeName, bool children = false) const; + /// Prints the track information. N number of hits to print, 0 = all void PrintTrack(size_t maxHits = 0) const; diff --git a/src/TRestGeant4Track.cxx b/src/TRestGeant4Track.cxx index a03d74f..263c653 100644 --- a/src/TRestGeant4Track.cxx +++ b/src/TRestGeant4Track.cxx @@ -221,3 +221,31 @@ TString TRestGeant4Track::GetFinalVolume() const { return GetGeant4Metadata()->GetGeant4GeometryInfo().GetVolumeFromID( hits.GetVolumeId(hits.GetNumberOfHits() - 1)); } + +Double_t TRestGeant4Track::GetEnergyInVolume(const TString& volumeName, bool children) const { + const auto metadata = GetGeant4Metadata(); + if (metadata == nullptr) { + return 0; + } + + const auto volumeId = metadata->GetGeant4GeometryInfo().GetIDFromVolume(volumeName); + + if (!children) { + return GetEnergyInVolume(volumeId); + } + + Double_t energy = 0; + vector tracks = {this}; + while (!tracks.empty()) { + const TRestGeant4Track* track = tracks.back(); + tracks.pop_back(); + if (track == nullptr) { + continue; + } + energy += track->GetEnergyInVolume(volumeId); + for (const TRestGeant4Track* secondaryTrack : track->GetSecondaryTracks()) { + tracks.push_back(secondaryTrack); + } + } + return energy; +} From c7f9999a3fbd9d21ad62022d0ef9ed1c138af407 Mon Sep 17 00:00:00 2001 From: lobis Date: Sun, 11 Sep 2022 20:14:02 +0200 Subject: [PATCH 096/100] TRestGeant4Track - added method to get last process name --- inc/TRestGeant4Track.h | 2 ++ src/TRestGeant4Track.cxx | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/inc/TRestGeant4Track.h b/inc/TRestGeant4Track.h index 1888f62..0024bd5 100644 --- a/inc/TRestGeant4Track.h +++ b/inc/TRestGeant4Track.h @@ -120,6 +120,8 @@ class TRestGeant4Track { Double_t GetEnergyInVolume(const TString& volumeName, bool children = false) const; + TString GetLastProcessName() const; + /// Prints the track information. N number of hits to print, 0 = all void PrintTrack(size_t maxHits = 0) const; diff --git a/src/TRestGeant4Track.cxx b/src/TRestGeant4Track.cxx index 263c653..8fef250 100644 --- a/src/TRestGeant4Track.cxx +++ b/src/TRestGeant4Track.cxx @@ -249,3 +249,14 @@ Double_t TRestGeant4Track::GetEnergyInVolume(const TString& volumeName, bool chi } return energy; } + +TString TRestGeant4Track::GetLastProcessName() const { + const auto metadata = GetGeant4Metadata(); + if (metadata == nullptr) { + return ""; + } + + const auto& hits = GetHits(); + return GetGeant4Metadata()->GetGeant4PhysicsInfo().GetProcessName( + hits.GetProcess(hits.GetNumberOfHits() - 1)); +} From e9149acc77c23002d18b4dad47a91ea8876ff4a0 Mon Sep 17 00:00:00 2001 From: lobis Date: Mon, 12 Sep 2022 17:10:43 +0200 Subject: [PATCH 097/100] renamed "Desired" to "Requested" --- inc/TRestGeant4Metadata.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inc/TRestGeant4Metadata.h b/inc/TRestGeant4Metadata.h index 90c0589..0d1037d 100644 --- a/inc/TRestGeant4Metadata.h +++ b/inc/TRestGeant4Metadata.h @@ -127,8 +127,8 @@ class TRestGeant4Metadata : public TRestMetadata { /// The number of events simulated, or to be simulated. Int_t fNEvents = 0; - /// The number of events the user desired to be on the file - Int_t fNDesiredEntries = 0; + /// The number of events the user requested to be on the file + Int_t fNRequestedEntries = 0; /// Time before simulation is ended and saved Int_t fSimulationMaxTimeSeconds = 0; @@ -251,7 +251,7 @@ class TRestGeant4Metadata : public TRestMetadata { /// Returns the number of events to be simulated. inline Int_t GetNumberOfEvents() const { return fNEvents; } - inline Int_t GetNumberOfDesiredEntries() const { return fNDesiredEntries; } + inline Int_t GetNumberOfRequestedEntries() const { return fNRequestedEntries; } inline Int_t GetSimulationMaxTimeSeconds() const { return fSimulationMaxTimeSeconds; } @@ -294,7 +294,7 @@ class TRestGeant4Metadata : public TRestMetadata { /// Sets the name of the sensitive volume inline void SetNumberOfEvents(Int_t n) { fNEvents = n; } - inline void SetNumberOfDesiredEntries(Int_t n) { fNDesiredEntries = n; } + inline void SetNumberOfRequestedEntries(Int_t n) { fNRequestedEntries = n; } inline void SetSimulationMaxTimeSeconds(Int_t seconds) { fSimulationMaxTimeSeconds = seconds; } From 53e42a750eeefa9be85e01028612d17f121117be Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 13 Sep 2022 12:07:44 +0200 Subject: [PATCH 098/100] fix small mistake in print --- src/TRestGeant4Track.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TRestGeant4Track.cxx b/src/TRestGeant4Track.cxx index 8fef250..8f5a322 100644 --- a/src/TRestGeant4Track.cxx +++ b/src/TRestGeant4Track.cxx @@ -116,7 +116,7 @@ void TRestGeant4Track::PrintTrack(size_t maxHits) const { << " * TrackID: " << fTrackID << " - Particle: " << fParticleName << " - ParentID: " << fParentID << "" << (GetParentTrack() != nullptr - ? TString::Format(" - Parent particle: {}", GetParentTrack()->GetParticleName().Data()).Data() + ? TString::Format(" - Parent particle: %s", GetParentTrack()->GetParticleName().Data()).Data() : "") << " - Created by '" << fCreatorProcess << "' with initial " From 9310ceef2e4f42d1d45f358085c0be43aee671d3 Mon Sep 17 00:00:00 2001 From: lobis Date: Tue, 13 Sep 2022 20:05:49 +0200 Subject: [PATCH 099/100] updated description strings --- src/TRestGeant4EventViewer.cxx | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/TRestGeant4EventViewer.cxx b/src/TRestGeant4EventViewer.cxx index fd2c742..095f0c0 100644 --- a/src/TRestGeant4EventViewer.cxx +++ b/src/TRestGeant4EventViewer.cxx @@ -79,7 +79,6 @@ TrackVisualConfiguration GetTrackVisualConfiguration(const TRestGeant4Track& tra } // width - Width_t width = TMath::Log10(track.GetInitialKineticEnergy() / 100); width = (width > 10 ? 10 : width); width = (width < 1 ? 1 : width); @@ -96,9 +95,9 @@ TrackVisualConfiguration GetTrackVisualConfiguration(const TRestGeant4Track& tra TEveStraightLineSet* GetTrackEveDrawable(const TRestGeant4Track& track) { auto lineSet = new TEveStraightLineSet( - TString::Format("ID%d | %s | Created by %s | Init KE: %0.2f keV", // + TString::Format("ID %d | %s | Created by %s | KE: %s", // track.GetTrackID(), track.GetParticleName().Data(), track.GetCreatorProcess().Data(), - track.GetInitialKineticEnergy())); + ToEnergyString(track.GetInitialKineticEnergy()).c_str())); const auto& hits = track.GetHits(); for (int i = 0; i < hits.GetNumberOfHits() - 1; i++) { @@ -225,8 +224,10 @@ void TRestGeant4EventViewer::AddEvent(TRestEvent* event) { TVector3 position = {firstTrack.GetInitialPosition().X(), firstTrack.GetInitialPosition().Y(), firstTrack.GetInitialPosition().Z()}; AddText( - TString::Format("Event ID: %d (SubID: %d) | Position: (%4.2lf, %4.2lf, %4.2lf) mm", fG4Event->GetID(), - fG4Event->GetSubID(), position.X(), position.Y(), position.Z()), + TString::Format( + "Event ID: %d%s | Primary origin: (%4.2lf, %4.2lf, %4.2lf) mm", fG4Event->GetID(), + (fG4Event->GetSubID() > 0 ? TString::Format(" (SubID: %d)", fG4Event->GetSubID()) : "").Data(), + position.X(), position.Y(), position.Z()), position); Update(); From fa3edc2faf7d3db3cb51a8584712c55dd78f8dcd Mon Sep 17 00:00:00 2001 From: lobis Date: Wed, 14 Sep 2022 18:47:22 +0200 Subject: [PATCH 100/100] updated validation workflow back to master --- .github/workflows/validation.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/validation.yml b/.github/workflows/validation.yml index ffa71d7..453aa3f 100644 --- a/.github/workflows/validation.yml +++ b/.github/workflows/validation.yml @@ -20,7 +20,7 @@ defaults: jobs: framework-validation: - uses: rest-for-physics/framework/.github/workflows/validation.yml@lobis-prepare-for-multithreading # TODO: master + uses: rest-for-physics/framework/.github/workflows/validation.yml@master check-library: name: Validate library