Belle II Software development
Materials.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <geometry/Materials.h>
9#include <gtest/gtest.h>
10#include <iostream>
11#include <string>
12
13#include <framework/gearbox/Gearbox.h>
14#include <framework/gearbox/GearDir.h>
15#include <framework/utilities/TestHelpers.h>
16
17#include <G4Element.hh>
18#include <G4Material.hh>
19#include <G4OpticalSurface.hh>
20#include <G4MaterialPropertiesTable.hh>
21
22using namespace std;
23
24namespace Belle2 {
29 namespace geometry {
30
33 TEST(Materials, Element)
34 {
36 G4Element* e1 = m.getElement("H");
37 ASSERT_TRUE(e1);
38 EXPECT_EQ("H", e1->GetName());
39 EXPECT_EQ("H", e1->GetSymbol());
40 EXPECT_EQ(1., e1->GetZ());
41 }
42
46 TEST(Materials, Material)
47 {
48 G4Material* m1 = Materials::get("Air");
49 G4Material* m2 = Materials::get("G4_AIR");
50 EXPECT_EQ(m1, m2);
51 ASSERT_TRUE(m1);
52 EXPECT_EQ("G4_AIR", m1->GetName());
53 EXPECT_TRUE(Materials::get("Si"));
54 }
55
59 TEST(Materials, Create)
60 {
62 vector<string> backends;
63 backends.emplace_back("string:<Material name='Test'><state>Gas</state><Components>"
64 "<Material fraction='1.5'>Si</Material>"
65 "<Material fraction='2.5612'>Au</Material>"
66 "</Components></Material>");
67 gb.setBackends(backends);
68 gb.open();
70 ASSERT_EQ(0, gb.getNumberNodes("/Material/density"));
71 ASSERT_EQ(1, gb.getNumberNodes("/Material/@name"));
72
73 G4Material* au = Materials::get("Au");
74 G4Material* si = Materials::get("Si");
75 G4Material* mat = m.createMaterial(GearDir("/Material"));
76 ASSERT_TRUE(mat);
77 EXPECT_EQ("Test", mat->GetName());
78 EXPECT_DOUBLE_EQ((2.5612 * au->GetDensity() + 1.5 * si->GetDensity()) / 4.0612, mat->GetDensity());
79 gb.close();
80 }
81
84 TEST(Materials, CreateDensityError)
85 {
87 vector<string> backends;
88 backends.emplace_back("string:<Material name='Test1'><state>Solid</state><Components>"
89 "<Material fraction='0.5'>Si</Material>"
90 "<Element fraction='0.5'>Au</Element>"
91 "</Components></Material>");
92 gb.setBackends(backends);
93 gb.open();
94
96 G4Material* mat = m.createMaterial(GearDir("/Material"));
97 ASSERT_FALSE(mat);
98 gb.close();
99 }
100
103 TEST(Materials, CreateDensity)
104 {
106 vector<string> backends;
107 backends.emplace_back("string:<Material name='Test2'><state>Liquid</state><density>1</density><Components>"
108 "<Material>Si</Material>"
109 "<Element>Au</Element>"
110 "</Components></Material>");
111 gb.setBackends(backends);
112 gb.open();
113
115 G4Material* mat = m.createMaterial(GearDir("/Material"));
116 ASSERT_TRUE(mat);
117 gb.close();
118 }
119
122 TEST(Materials, CreateMaterialError)
123 {
125 vector<string> backends;
126 backends.emplace_back("string:<Material name='Test3'><Components>"
127 "<Material>Foo</Material>"
128 "</Components></Material>");
129 gb.setBackends(backends);
130 gb.open();
131
133 G4Material* mat = m.createMaterial(GearDir("/Material"));
134 ASSERT_FALSE(mat);
135 gb.close();
136 }
137
140 TEST(Materials, CreateElementError)
141 {
143 vector<string> backends;
144 backends.emplace_back("string:<Material name='Test4'><density>1</density><Components>"
145 "<Element>Foo</Element>"
146 "</Components></Material>");
147 gb.setBackends(backends);
148 gb.open();
149
151 G4Material* mat = m.createMaterial(GearDir("/Material"));
152 ASSERT_FALSE(mat);
153 gb.close();
154 }
155
158 TEST(Materials, OpticalSurface)
159 {
161 vector<string> backends;
162 backends.emplace_back("string:<test><Surface/>"
163 "<Surface name='test'><Model>unified</Model><Finish>Ground</Finish>"
164 "<Type>x_ray</Type><Value>2.0</Value></Surface>"
165 "<Surface><Model>not existing</Model></Surface>"
166 "<Surface><Finish>not existing</Finish></Surface>"
167 "<Surface><Type>not existing</Type></Surface></test>"
168 );
169 gb.setBackends(backends);
170 gb.open();
171
173
174 G4OpticalSurface* surf1 = m.createOpticalSurface(GearDir("/test/Surface[1]"));
175 ASSERT_TRUE(surf1);
176 EXPECT_EQ("OpticalSurface", surf1->GetName());
177 EXPECT_EQ(glisur, surf1->GetModel());
178 EXPECT_EQ(polished, surf1->GetFinish());
179 EXPECT_EQ(dielectric_dielectric, surf1->GetType());
180 EXPECT_EQ(1.0, surf1->GetPolish());
181
182 G4OpticalSurface* surf2 = m.createOpticalSurface(GearDir("/test/Surface[2]"));
183 ASSERT_TRUE(surf2);
184 EXPECT_EQ("test", surf2->GetName());
185 EXPECT_EQ(unified, surf2->GetModel());
186 EXPECT_EQ(ground, surf2->GetFinish());
187 EXPECT_EQ(x_ray, surf2->GetType());
188 EXPECT_EQ(2.0, surf2->GetSigmaAlpha());
189
190 EXPECT_B2FATAL(m.createOpticalSurface(GearDir("/test/Surface[3]")));
191 EXPECT_B2FATAL(m.createOpticalSurface(GearDir("/test/Surface[4]")));
192 EXPECT_B2FATAL(m.createOpticalSurface(GearDir("/test/Surface[5]")));
193 }
194
197 TEST(Materials, Properties)
198 {
200 vector<string> backends;
201 backends.emplace_back("string:<Material name='TestProperties'>"
202 "<Components><Material>Si</Material></Components>"
203 "<Property name='RINDEX' unit='eV'>"
204 "<value energy='1.0'>1.40</value>"
205 "<value energy='1.5'>1.41</value>"
206 "<value energy='2.0'>1.42</value>"
207 "<value energy='3.5'>1.43</value>"
208 "</Property>"
209 "</Material>");
210 gb.setBackends(backends);
211 gb.open();
213
214 G4Material* mat = m.createMaterial(GearDir("/Material"));
215 ASSERT_TRUE(mat);
216 G4MaterialPropertiesTable* properties = mat->GetMaterialPropertiesTable();
217 ASSERT_TRUE(properties);
218 G4MaterialPropertyVector* property = properties->GetProperty("RINDEX");
219 ASSERT_TRUE(property);
220 EXPECT_EQ(4u, property->GetVectorLength());
221 EXPECT_DOUBLE_EQ(1.40, property->GetMinValue());
222 EXPECT_DOUBLE_EQ(1.43, property->GetMaxValue());
223 EXPECT_DOUBLE_EQ(1 * CLHEP::eV, property->GetMinEnergy());
224 EXPECT_DOUBLE_EQ(3.5 * CLHEP::eV, property->GetMaxEnergy());
225 EXPECT_DOUBLE_EQ(1.0 * CLHEP::eV, property->Energy(0));
226 EXPECT_DOUBLE_EQ(1.5 * CLHEP::eV, property->Energy(1));
227 EXPECT_DOUBLE_EQ(2.0 * CLHEP::eV, property->Energy(2));
228 EXPECT_DOUBLE_EQ(3.5 * CLHEP::eV, property->Energy(3));
229 EXPECT_DOUBLE_EQ(1.40, (*property)[0]);
230 EXPECT_DOUBLE_EQ(1.41, (*property)[1]);
231 EXPECT_DOUBLE_EQ(1.42, (*property)[2]);
232 EXPECT_DOUBLE_EQ(1.43, (*property)[3]);
233 gb.close();
234 }
235 } // namespace geometry
237} // namespace Belle2
GearDir is the basic class used for accessing the parameter store.
Definition: GearDir.h:31
Singleton class responsible for loading detector parameters from an XML file.
Definition: Gearbox.h:34
virtual int getNumberNodes(const std::string &path="") const override
Return the number of nodes a given path will expand to.
Definition: Gearbox.h:112
Thin wrapper around the Geant4 Material system.
Definition: Materials.h:48
static G4Material * get(const std::string &name)
Find given material.
Definition: Materials.h:63
static Materials & getInstance()
Get a reference to the singleton instance.
Definition: Materials.cc:85
static Gearbox & getInstance()
Return reference to the Gearbox instance.
Definition: Gearbox.cc:81
void close()
Free internal structures of previously parsed tree and clear cache.
Definition: Gearbox.cc:169
void setBackends(const std::vector< std::string > &backends)
Select the backends to use to find resources.
Definition: Gearbox.cc:99
void open(const std::string &name="Belle2.xml", size_t cacheSize=c_DefaultCacheSize)
Open connection to backend and parse tree.
Definition: Gearbox.cc:133
Abstract base class for different kinds of events.
STL namespace.