Project
Loading...
Searching...
No Matches
nist_export_all.cxx
Go to the documentation of this file.
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.
13
14#include <fstream>
15#include <iostream>
16#include <iomanip>
17#include <string>
18#include <vector>
19#include <algorithm>
20
21// Geant4
22#include "G4NistManager.hh"
23#include "G4Material.hh"
24#include "G4Element.hh"
25#include "G4SystemOfUnits.hh"
26
27static std::string json_escape(const std::string& s)
28{
29 std::string out;
30 out.reserve(s.size() + 8);
31 for (char c : s) {
32 switch (c) {
33 case '\\':
34 out += "\\\\";
35 break;
36 case '"':
37 out += "\\\"";
38 break;
39 case '\n':
40 out += "\\n";
41 break;
42 case '\r':
43 out += "\\r";
44 break;
45 case '\t':
46 out += "\\t";
47 break;
48 default:
49 out += c;
50 break;
51 }
52 }
53 return out;
54}
55
56int main(int argc, char** argv)
57{
58 if (argc < 2) {
59 std::cerr << "Usage:\n " << argv[0] << " out.json\n";
60 return 2;
61 }
62
63 const std::string out_json = argv[1];
64
65 auto* nist = G4NistManager::Instance();
66
67 // This returns all known NIST material names.
68 std::vector<G4String> names = nist->GetNistMaterialNames();
69 std::sort(names.begin(), names.end());
70
71 std::ofstream out(out_json);
72 if (!out) {
73 std::cerr << "Cannot write: " << out_json << "\n";
74 return 2;
75 }
76
77 out << std::fixed << std::setprecision(10);
78 out << "{\n"
79 << " \"schema\": \"g4_nist_export_v1\",\n"
80 << " \"count_requested\": " << names.size() << ",\n"
81 << " \"materials\": {\n";
82
83 bool first_mat = true;
84 size_t built_ok = 0;
85 size_t built_fail = 0;
86
87 for (const auto& g4name : names) {
88 // Build the material (some may fail depending on Geant4 build/config).
89 G4Material* mat = nist->FindOrBuildMaterial(g4name, /*warning=*/false, /*isotopes=*/false);
90 if (!mat) {
91 ++built_fail;
92 continue;
93 }
94 ++built_ok;
95
96 const std::string name = g4name; // convert G4String -> std::string
97
98 // Export in convenient units
99 const double density_g_cm3 = mat->GetDensity() / (g / cm3);
100 const double radlen_cm = mat->GetRadlen() / cm;
101 const double intlen_cm = mat->GetNuclearInterLength() / cm;
102
103 const size_t ne = mat->GetNumberOfElements();
104 const auto* elems = mat->GetElementVector();
105 const auto* fracs = mat->GetFractionVector(); // mass fractions (nullptr for some edge cases)
106
107 if (!first_mat)
108 out << ",\n";
109 first_mat = false;
110
111 out << " \"" << json_escape(name) << "\": {\n";
112 out << " \"name\": \"" << json_escape(name) << "\",\n";
113 out << " \"density_g_cm3\": " << density_g_cm3 << ",\n";
114 out << " \"radlen_cm\": " << radlen_cm << ",\n";
115 out << " \"intlen_cm\": " << intlen_cm << ",\n";
116 out << " \"elements\": [\n";
117
118 for (size_t i = 0; i < ne; ++i) {
119 const G4Element* el = (*elems)[i];
120 const int Z = static_cast<int>(el->GetZ());
121 const double A_g_mol = el->GetA() / (g / mole);
122 const double w = fracs ? fracs[i] : 0.0;
123
124 out << " {"
125 << "\"symbol\": \"" << json_escape(el->GetSymbol()) << "\", "
126 << "\"Z\": " << Z << ", "
127 << "\"A_g_mol\": " << A_g_mol << ", "
128 << "\"mass_fraction\": " << w
129 << "}";
130
131 if (i + 1 != ne)
132 out << ",";
133 out << "\n";
134 }
135
136 out << " ]\n";
137 out << " }";
138 }
139
140 out << "\n },\n"
141 << " \"count_built_ok\": " << built_ok << ",\n"
142 << " \"count_built_fail\": " << built_fail << "\n"
143 << "}\n";
144
145 std::cerr << "Wrote: " << out_json << "\n"
146 << "NIST names: " << names.size() << ", built ok: " << built_ok
147 << ", failed: " << built_fail << "\n";
148 return 0;
149}
int32_t i
uint32_t c
Definition RawData.h:2
GLuint const GLchar * name
Definition glcorearb.h:781
GLboolean GLboolean g
Definition glcorearb.h:1233
GLubyte GLubyte GLubyte GLubyte w
Definition glcorearb.h:852
#define main