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