Skip to content

Commit 9f3e333

Browse files
authored
Added interface to user score weighting: (#81)
Added TG4ScoreWeightCalculator type for user scoreweight function that takes (pdg, ekin) as arguments and its setter to the TG4RunConfiguration class: void SetScoreWeightCalculator(TG4ScoreWeightCalculator swc); The user function is then, in TG4SDManager::LateInitialize(), wrapped to G4ScoreWeightCalculator function that takes (const G4Step*) as argument and applied to all Geant4 scorers with activated score weighting.
1 parent 85052c5 commit 9f3e333

6 files changed

Lines changed: 110 additions & 7 deletions

File tree

source/digits+hits/include/TG4SDManager.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
///
1616
/// \author I. Hrivnacova; IPN, Orsay
1717

18-
#include <globals.hh>
18+
#include "TG4ScoreWeightCalculator.h"
1919

20+
#include <globals.hh>
2021
#include <Rtypes.h>
2122

2223
class TG4SDServices;
@@ -41,6 +42,7 @@ class TG4SDManager
4142

4243
// methods
4344
void Initialize();
45+
void LateInitialize(TG4ScoreWeightCalculator swc);
4446

4547
// TVirtualMC methods
4648
Int_t VolId(const Text_t* volName) const;
@@ -78,6 +80,9 @@ class TG4SDManager
7880
/// services related with sensitive detectors
7981
TG4SDServices* fSDServices;
8082

83+
/// score weight calculator
84+
TG4ScoreWeightCalculator fScoreWeightCalculator;
85+
8186
/// buffer for volume name
8287
mutable G4String fNameBuffer;
8388
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef TG4_SCORE_WEIGHT_CALCULATOR_H
2+
#define TG4_SCORE_WEIGHT_CALCULATOR_H
3+
4+
//------------------------------------------------
5+
// The Geant4 Virtual Monte Carlo package
6+
// Copyright (C) 2014 - 2018 Ivana Hrivnacova
7+
// All rights reserved.
8+
//
9+
// For the licensing terms see geant4_vmc/LICENSE.
10+
// Contact: root-vmc@cern.ch
11+
//-------------------------------------------------
12+
13+
/// \file TG4ScoreWeightCalculator.h
14+
/// \brief Definition of the score weight calculator type
15+
///
16+
/// \author I. Hrivnacova; IPN, Orsay
17+
18+
#include <Rtypes.h>
19+
20+
#include <functional>
21+
22+
using TG4ScoreWeightCalculator = std::function<Double_t(Int_t pdg, Double_t ekin)>;
23+
24+
#endif // TG4_SCORE_WEIGHT_CALCULATOR_H

source/digits+hits/src/TG4SDManager.cxx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
#include "TG4SDConstruction.h"
2121
#include "TG4SDServices.h"
2222

23+
#include "G4MultiFunctionalDetector.hh"
24+
#include "G4ScoringManager.hh"
25+
#include "G4VPrimitiveScorer.hh"
26+
#include "G4VScoringMesh.hh"
27+
2328
#include <TVirtualMC.h>
2429

2530
TG4SDManager* TG4SDManager::fgInstance = 0;
@@ -67,6 +72,48 @@ void TG4SDManager::Initialize()
6772
// G4cout << "TG4SDManager::Initialize done" << G4endl;
6873
}
6974

75+
//_____________________________________________________________________________
76+
void TG4SDManager::LateInitialize(TG4ScoreWeightCalculator swc)
77+
{
78+
// Apply score weight if use of Geant4 scoring is activated
79+
80+
auto g4ScoringManager = G4ScoringManager::GetScoringManagerIfExist();
81+
if (g4ScoringManager == nullptr ) return;
82+
83+
fScoreWeightCalculator = swc;
84+
85+
G4ScoreWeightCalculator g4Swc = [&calculator = fScoreWeightCalculator](const G4Step* step) -> G4double {
86+
Double_t ekin = step->GetTrack()->GetKineticEnergy();
87+
Int_t pdg = step->GetTrack()->GetParticleDefinition()->GetPDGEncoding();
88+
return calculator(pdg, ekin);
89+
};
90+
91+
// Loop over existing scoring meshes
92+
auto nofMesh = g4ScoringManager->GetNumberOfMesh();
93+
if (nofMesh < 1) return;
94+
// G4cout << "Processing scorers for " << nofMesh << " meshes" << G4endl;
95+
std::size_t counter = 0;
96+
for (std::size_t i = 0; i < nofMesh; ++i) {
97+
auto mesh = g4ScoringManager->GetMesh((G4int)i);
98+
const auto mfd = mesh->GetMFD();
99+
G4int nps = mfd->GetNumberOfPrimitives();
100+
for (G4int i = 0; i < nps; ++i) {
101+
auto scorer = mfd->GetPrimitive(i);
102+
// G4cout << "Looping over " << scorer->GetName() << G4endl;
103+
if (scorer->IsScoreWeighted()) {
104+
// G4cout << " is weightScoring activated " << scorer->IsScoreWeighted() << G4endl;
105+
scorer->SetScoreWeightCalculator(g4Swc);
106+
++counter;
107+
}
108+
}
109+
}
110+
111+
if (counter > 0) {
112+
G4cout << "### User score weight calculator set to " << counter << " scorers."
113+
<< G4endl;
114+
}
115+
}
116+
70117
//_____________________________________________________________________________
71118
Int_t TG4SDManager::VolId(const Text_t* volName) const
72119
{

source/run/include/TG4RunConfiguration.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
///
1616
/// \author I. Hrivnacova; IPN Orsay
1717

18+
#include "TG4ScoreWeightCalculator.h"
19+
1820
#include <Rtypes.h>
1921
#include <TString.h>
2022

@@ -103,6 +105,7 @@ class TG4RunConfiguration
103105
void SetParameter(const TString& name, Double_t value);
104106
void SetSpecialCutsOld();
105107
void SetUseOfG4Scoring();
108+
void SetScoreWeightCalculator(TG4ScoreWeightCalculator swc);
106109

107110
// get methods
108111
TString GetUserGeometry() const;
@@ -112,7 +115,9 @@ class TG4RunConfiguration
112115
Bool_t IsSpecialCuts() const;
113116
Bool_t IsSpecialCutsOld() const;
114117
Bool_t IsUseOfG4Scoring() const;
118+
Bool_t IsUseOfScoreWeighting() const;
115119
Bool_t IsMTApplication() const;
120+
TG4ScoreWeightCalculator GetScoreWeightCalculator() const;
116121

117122
protected:
118123
// data members
@@ -125,9 +130,10 @@ class TG4RunConfiguration
125130
Bool_t fSpecialCuts; ///< option for special cuts
126131
Bool_t fSpecialCutsOld; ///< option for special cuts old
127132
Bool_t fUseOfG4Scoring; ///< option to activate G4 commmand-line scoring
133+
Bool_t fUseOfScoreWeighting; ///< option to activate score weighting
128134
G4UImessenger* fAGDDMessenger; //!< XML messenger
129135
G4UImessenger* fGDMLMessenger; //!< XML messenger
130-
136+
TG4ScoreWeightCalculator fScoreWeightCalculator; //!< User Scoring Weight Calculator
131137
/// The map of special parameters which need to be set before creating TGeant4
132138
/// Actually used for monopole properties:
133139
/// monopoleMass, monopoleElCharge, monopoleMagCharge
@@ -150,6 +156,13 @@ inline void TG4RunConfiguration::SetUseOfG4Scoring()
150156
fUseOfG4Scoring = true;
151157
}
152158

159+
/// Set User Scoring Weight Calculator
160+
inline void TG4RunConfiguration::SetScoreWeightCalculator(TG4ScoreWeightCalculator swc)
161+
{
162+
fUseOfScoreWeighting = true;
163+
fScoreWeightCalculator = swc;
164+
}
165+
153166
/// Return physics list selection
154167
inline TString TG4RunConfiguration::GetPhysicsListSelection() const
155168
{
@@ -161,4 +174,14 @@ inline Bool_t TG4RunConfiguration::IsUseOfG4Scoring() const
161174
return fUseOfG4Scoring;
162175
}
163176

177+
inline Bool_t TG4RunConfiguration::IsUseOfScoreWeighting() const
178+
{
179+
return fUseOfScoreWeighting;
180+
}
181+
182+
inline TG4ScoreWeightCalculator TG4RunConfiguration::GetScoreWeightCalculator() const
183+
{
184+
return fScoreWeightCalculator;
185+
}
186+
164187
#endif // TG4V_RUN_CONFIGURATION_H

source/run/src/TG4RunConfiguration.cxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ TG4RunConfiguration::TG4RunConfiguration(const TString& userGeometry,
5050
fSpecialCuts(false),
5151
fSpecialCutsOld(false),
5252
fUseOfG4Scoring(false),
53+
fUseOfScoreWeighting(false),
5354
fAGDDMessenger(0),
5455
fGDMLMessenger(0),
5556
fParameters()

source/run/src/TG4RunManager.cxx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
#include <G4Version.hh>
5555
#include <Randomize.hh>
5656

57-
5857
#ifdef USE_G4ROOT
5958
#include <TG4RootNavMgr.h>
6059
#endif
@@ -132,14 +131,12 @@ TG4RunManager::TG4RunManager(
132131

133132
if (isMaster) {
134133
fgMasterInstance = this;
135-
136-
// create and configure G4 run manager
137-
ConfigureRunManager();
138-
139134
if (runConfiguration->IsUseOfG4Scoring()) {
140135
// activate G4 command-line scoring
141136
G4ScoringManager::GetScoringManager();
142137
}
138+
// create and configure G4 run manager
139+
ConfigureRunManager();
143140
}
144141
else {
145142
// Get G4 worker run manager
@@ -477,6 +474,12 @@ void TG4RunManager::LateInitialize()
477474
TG4PhysicsManager::Instance()->SetProcessActivation();
478475
TG4PhysicsManager::Instance()->RetrieveOpBoundaryProcess();
479476

477+
// late initialize SD manager
478+
// (needed only if user sets score weight calculator)
479+
if (fRunConfiguration->IsUseOfScoreWeighting()) {
480+
TG4SDManager::Instance()->LateInitialize(fRunConfiguration->GetScoreWeightCalculator());
481+
}
482+
480483
// late initialize step manager
481484
TG4StepManager::Instance()->LateInitialize();
482485

0 commit comments

Comments
 (0)