Skip to content

Commit 1a18e76

Browse files
authored
optional use of Geant4 fluence weighting added (#15417)
* optional use of Geant4 fluence weighting added * Update copyright notice in FluenceWeightCalculator.cxx * Add copyright and license information to header Added copyright notice and license information to the header. * Update copyright and license information Updated copyright information and license details in g4Config.C. * Remove blank line before G4 VMC creation Removed unnecessary blank line before G4 VMC creation. * Refactor Eval calls to use nullptr instead of 0 Updated function calls to use nullptr instead of 0 for better clarity and safety. Removed unnecessary blank line before G4 VMC creation.
1 parent ab643bf commit 1a18e76

6 files changed

Lines changed: 198 additions & 9 deletions

File tree

Common/SimConfig/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ o2_add_library(SimConfig
2020
src/MatMapParams.cxx
2121
src/InteractionDiamondParam.cxx
2222
src/GlobalProcessCutSimParam.cxx
23+
src/FluenceWeightCalculator.cxx
2324
PUBLIC_LINK_LIBRARIES O2::CommonUtils
2425
O2::DetectorsCommonDataFormats O2::SimulationDataFormat
2526
FairRoot::Base Boost::program_options)
@@ -35,7 +36,8 @@ o2_target_root_dictionary(SimConfig
3536
include/SimConfig/G4Params.h
3637
include/SimConfig/DetectorLists.h
3738
include/SimConfig/GlobalProcessCutSimParam.h
38-
include/SimConfig/MatMapParams.h)
39+
include/SimConfig/MatMapParams.h
40+
include/SimConfig/FluenceWeightCalculator.h)
3941

4042
o2_add_test(Config
4143
SOURCES test/TestConfig.cxx
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2019-2026 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
12+
#ifndef FluenceWeightCalculator_h
13+
#define FluenceWeightCalculator_h
14+
#include <vector>
15+
#include <string>
16+
#include <memory>
17+
#include "TGraph.h"
18+
//
19+
// Static container class for damage weight funnctions in form of TGraphs
20+
// The weights can be read from a csv file and stored in the graphs.
21+
//
22+
class FluenceWeightCalculator
23+
{
24+
public:
25+
FluenceWeightCalculator() = delete;
26+
static void InitWeights(const std::string& filename);
27+
static void InitWeightsFromCSV(const std::string& filename);
28+
static double GetWeight(const int pdg, const double ekin);
29+
30+
private:
31+
static std::unique_ptr<TGraph> neutronG;
32+
static std::unique_ptr<TGraph> protonG;
33+
static std::unique_ptr<TGraph> pionG;
34+
};
35+
#endif

Common/SimConfig/include/SimConfig/G4Params.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ struct G4Params : public o2::conf::ConfigurableParamHelper<G4Params> {
4949

5050
EG4Nav navmode = EG4Nav::kTGeo; // geometry navigation mode (default TGeo)
5151

52+
std::string fluenceWeightFile = ""; // file containing the scoring weights (pdg, ekin, weight)
5253
std::string const& getPhysicsConfigString() const;
5354

55+
bool g4scoring = false;
56+
bool g4fluenceweight = false;
5457
O2ParamDef(G4Params, "G4");
5558
};
5659

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
12+
#include "SimConfig/FluenceWeightCalculator.h"
13+
#include <TFile.h>
14+
#include <fstream>
15+
#include <sstream>
16+
#include <iostream>
17+
18+
std::unique_ptr<TGraph> FluenceWeightCalculator::neutronG;
19+
std::unique_ptr<TGraph> FluenceWeightCalculator::protonG;
20+
std::unique_ptr<TGraph> FluenceWeightCalculator::pionG;
21+
22+
double FluenceWeightCalculator::GetWeight(const int pdg, const double kineticEnergy)
23+
{
24+
//
25+
// Obtain weight for given particle and kinetic energy
26+
if (!neutronG || !protonG || !pionG) {
27+
std::cerr << "FluenceWeightCalculator not initialized\n";
28+
return 0.;
29+
}
30+
switch (std::abs(pdg)) {
31+
case 2112: {
32+
return neutronG->Eval(kineticEnergy, nullptr, "S");
33+
}
34+
case 2212: {
35+
return ((kineticEnergy > 1e-3) ? protonG->Eval(kineticEnergy, nullptr, "S") : 0.);
36+
}
37+
case 211: {
38+
return ((kineticEnergy > 10.) ? pionG->Eval(kineticEnergy, nullptr, "S") : 0.);
39+
}
40+
default:
41+
return 0.0;
42+
}
43+
}
44+
45+
void FluenceWeightCalculator::InitWeights(const std::string& filename)
46+
{
47+
//
48+
// Read graphs from file
49+
TFile inFile(filename.c_str(), "READ");
50+
if (inFile.IsZombie()) {
51+
std::cerr << "Cannot open " << filename << "\n";
52+
return;
53+
}
54+
//
55+
TGraph* tmp = nullptr;
56+
inFile.GetObject("neutronDW", tmp);
57+
neutronG.reset(tmp ? static_cast<TGraph*>(tmp->Clone()) : nullptr);
58+
if (!neutronG) {
59+
std::cerr << "Missing graph neutronDW\n";
60+
return;
61+
}
62+
neutronG->SetBit(TGraph::kIsSortedX);
63+
inFile.GetObject("protonDW", tmp);
64+
protonG.reset(tmp ? static_cast<TGraph*>(tmp->Clone()) : nullptr);
65+
if (!protonG) {
66+
std::cerr << "Missing graph protonDW\n";
67+
return;
68+
}
69+
protonG->SetBit(TGraph::kIsSortedX);
70+
inFile.GetObject("pionDW", tmp);
71+
pionG.reset(tmp ? static_cast<TGraph*>(tmp->Clone()) : nullptr);
72+
if (!pionG) {
73+
std::cerr << "Missing graph pionDW\n";
74+
return;
75+
}
76+
pionG->SetBit(TGraph::kIsSortedX);
77+
}
78+
79+
void FluenceWeightCalculator::InitWeightsFromCSV(const std::string& filename)
80+
{
81+
//
82+
// read the NIEL weights from input file and store them as TGraphs
83+
neutronG = std::make_unique<TGraph>();
84+
neutronG->SetName("neutronDW");
85+
auto neuN = 0;
86+
protonG = std::make_unique<TGraph>();
87+
protonG->SetName("protonDW");
88+
auto proN = 0;
89+
pionG = std::make_unique<TGraph>();
90+
pionG->SetName("pionDW");
91+
auto pioN = 0;
92+
93+
std::ifstream in(filename);
94+
if (!in.is_open()) {
95+
std::cerr << "Error: cannot open file with damage weights.\n";
96+
return;
97+
}
98+
std::string line;
99+
while (std::getline(in, line)) {
100+
if (line.empty() || line[0] == '#') {
101+
continue;
102+
}
103+
std::istringstream ss(line);
104+
std::string particle, e_str, w_str;
105+
if (!std::getline(ss, particle, ',')) {
106+
continue;
107+
}
108+
if (!std::getline(ss, e_str, ',')) {
109+
continue;
110+
}
111+
if (!std::getline(ss, w_str, ',')) {
112+
continue;
113+
}
114+
auto e = std::stod(e_str);
115+
auto w = std::stod(w_str);
116+
auto pdg = std::stoi(particle);
117+
switch (pdg) {
118+
case 2112: {
119+
neutronG->SetPoint(neuN++, e, w);
120+
break;
121+
}
122+
case 2212: {
123+
protonG->SetPoint(proN++, e, w);
124+
break;
125+
}
126+
case 211: {
127+
pionG->SetPoint(pioN++, e, w);
128+
break;
129+
}
130+
default:;
131+
}
132+
}
133+
auto fout = new TFile("rd50_niel.root", "recreate");
134+
neutronG->Write();
135+
protonG->Write();
136+
pionG->Write();
137+
fout->Close();
138+
}
23.2 KB
Binary file not shown.

Detectors/gconfig/g4Config.C

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
/********************************************************************************
2-
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3-
* *
4-
* This software is distributed under the terms of the *
5-
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
6-
* copied verbatim in the file "LICENSE" *
7-
********************************************************************************/
1+
// Copyright 2020-2022 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
811
R__LOAD_LIBRARY(libG4ptl)
912
R__LOAD_LIBRARY(libG4zlib)
1013
R__LOAD_LIBRARY(libG4expat)
@@ -50,12 +53,14 @@ R__LOAD_LIBRARY(libgeant4vmc)
5053

5154
#if !defined(__CLING__) || defined(__ROOTCLING__)
5255
#include <iostream>
56+
#include <functional>
5357
#include "TGeant4.h"
5458
#include "TString.h"
5559
#include "FairRunSim.h"
5660
#include "TSystem.h"
5761
#include "TG4RunConfiguration.h"
5862
#include "SimConfig/G4Params.h"
63+
#include "SimConfig/FluenceWeightCalculator.h"
5964
#endif
6065
#include "commonConfig.C"
6166

@@ -111,9 +116,15 @@ void Config()
111116

112117
auto runConfiguration = new TG4RunConfiguration(geomNavStr, physicsSetup, "stepLimiter+specialCuts",
113118
specialStacking, mtMode);
119+
if (g4Params.g4scoring) {
120+
runConfiguration->SetUseOfG4Scoring();
121+
if (g4Params.g4fluenceweight) {
122+
FluenceWeightCalculator::InitWeightsFromCSV(g4Params.fluenceWeightFile);
123+
runConfiguration->SetScoreWeightCalculator(&FluenceWeightCalculator::GetWeight);
124+
}
125+
}
114126
/// avoid the use of G4BACKTRACE (it seems to inferfere with process logic in o2-sim)
115127
setenv("G4BACKTRACE", "none", 1);
116-
117128
/// Create the G4 VMC
118129
TGeant4* geant4 = new TGeant4("TGeant4", "The Geant4 Monte Carlo", runConfiguration);
119130
std::cout << "Geant4 has been created." << std::endl;

0 commit comments

Comments
 (0)