Skip to content

Commit 99bba46

Browse files
agheatadpiparo
authored andcommitted
[gdml] Resolve material fractions by exact reference
1 parent 9f4a1ea commit 99bba46

4 files changed

Lines changed: 76 additions & 12 deletions

File tree

geom/gdml/src/TGDMLParse.cxx

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,6 @@ XMLNodePointer_t TGDMLParse::MatProcess(TXMLEngine *gdml, XMLNodePointer_t node,
13101310
TGeoMixture *mix = nullptr;
13111311
TGeoMaterial *mat = nullptr;
13121312
TString tempconst = "";
1313-
TString matname;
13141313
Bool_t composite = kFALSE;
13151314

13161315
if (z == 1) {
@@ -1546,25 +1545,29 @@ XMLNodePointer_t TGDMLParse::MatProcess(TXMLEngine *gdml, XMLNodePointer_t node,
15461545
Double_t weight;
15471546

15481547
for (fractions f = fracmap.begin(); f != fracmap.end(); ++f) {
1549-
matname = f->first;
1550-
matname = NameShort(matname);
1551-
1552-
TGeoMaterial *mattmp = (TGeoMaterial *)gGeoManager->GetListOfMaterials()->FindObject(matname);
1548+
TGeoMaterial *mattmp = nullptr;
1549+
auto material = fmatmap.find(f->first);
1550+
if (material != fmatmap.end())
1551+
mattmp = (TGeoMaterial *)material->second;
1552+
else {
1553+
auto mixture = fmixmap.find(f->first);
1554+
if (mixture != fmixmap.end())
1555+
mattmp = (TGeoMixture *)mixture->second;
1556+
}
1557+
auto element = felemap.find(f->first);
15531558

1554-
if (mattmp || (felemap.find(f->first) != felemap.end())) {
1559+
if ((composite && element != felemap.end()) || (!composite && (mattmp || element != felemap.end()))) {
15551560
if (composite) {
15561561
natoms = (Int_t)f->second;
1557-
1558-
mix->AddElement(felemap[f->first], natoms);
1559-
1562+
mix->AddElement((TGeoElement *)element->second, natoms);
15601563
}
15611564

15621565
else {
15631566
weight = f->second;
15641567
if (mattmp) {
15651568
mix->AddElement(mattmp, weight);
15661569
} else {
1567-
mix->AddElement(felemap[f->first], weight);
1570+
mix->AddElement((TGeoElement *)element->second, weight);
15681571
}
15691572
}
15701573
}
@@ -1573,13 +1576,16 @@ XMLNodePointer_t TGDMLParse::MatProcess(TXMLEngine *gdml, XMLNodePointer_t node,
15731576

15741577
medid = medid + 1;
15751578

1579+
if (mixflag == 1)
1580+
fmixmap[local_name.Data()] = mix;
1581+
else if (mixflag == 0)
1582+
fmatmap[local_name.Data()] = mat;
1583+
15761584
TGeoMedium *med = mgr->GetMedium(NameShort(name));
15771585
if (!med) {
15781586
if (mixflag == 1) {
1579-
fmixmap[local_name.Data()] = mix;
15801587
med = new TGeoMedium(NameShort(name), medid, mix);
15811588
} else if (mixflag == 0) {
1582-
fmatmap[local_name.Data()] = mat;
15831589
med = new TGeoMedium(NameShort(name), medid, mat);
15841590
}
15851591
} else if (gDebug >= 2) {

geom/test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66

77
if(gdml)
88
configure_file(no_extrusion.gdml no_extrusion.gdml COPYONLY)
9+
configure_file(material_name_collision.gdml material_name_collision.gdml COPYONLY)
910
ROOT_ADD_GTEST(material_units
1011
test_material_units.cxx
1112
LIBRARIES Geom)
13+
ROOT_ADD_GTEST(gdml_material_name_collision
14+
test_gdml_material_name_collision.cxx
15+
LIBRARIES Geom)
1216
ROOT_ADD_GTEST(boolean_extrusion
1317
test_boolean_extrusion.cxx
1418
LIBRARIES Geom GeomChecker)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0"?>
2+
<gdml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3+
<define/>
4+
<materials>
5+
<element Z="26" formula="FE" name="Iron0xaaa"><atom unit="g/mole" value="55.845"/></element>
6+
<element Z="28" formula="NI" name="Nickel0xbbb"><atom unit="g/mole" value="58.693"/></element>
7+
<material name="Iron0xccc" state="solid">
8+
<D unit="g/cm3" value="7.87"/><fraction n="1" ref="Iron0xaaa"/>
9+
</material>
10+
<material name="Alloy0xddd" state="solid">
11+
<D unit="g/cm3" value="8.2"/>
12+
<fraction n="0.5" ref="Iron0xaaa"/><fraction n="0.5" ref="Nickel0xbbb"/>
13+
</material>
14+
</materials>
15+
<solids>
16+
<box lunit="mm" name="world0x1a" x="1000" y="1000" z="1000"/>
17+
<box lunit="mm" name="cube0x1b" x="100" y="100" z="100"/>
18+
</solids>
19+
<structure>
20+
<volume name="ironVol0x2a"><materialref ref="Iron0xccc"/><solidref ref="cube0x1b"/></volume>
21+
<volume name="World0x2c"><materialref ref="Alloy0xddd"/><solidref ref="world0x1a"/>
22+
<physvol name="ironPV"><volumeref ref="ironVol0x2a"/></physvol></volume>
23+
</structure>
24+
<setup name="Default" version="1.0"><world ref="World0x2c"/></setup>
25+
</gdml>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <TGeoElement.h>
4+
#include <TGeoManager.h>
5+
#include <TGeoMaterial.h>
6+
7+
TEST(Geometry, GDMLMaterialElementNameCollision)
8+
{
9+
auto geom = TGeoManager::Import("material_name_collision.gdml");
10+
ASSERT_NE(geom, nullptr);
11+
12+
auto iron = geom->GetMaterial("Iron");
13+
ASSERT_NE(iron, nullptr);
14+
ASSERT_TRUE(iron->IsMixture());
15+
auto ironMixture = static_cast<TGeoMixture *>(iron);
16+
ASSERT_EQ(iron->GetNelements(), 1);
17+
EXPECT_STREQ(iron->GetElement(0)->GetName(), "FE");
18+
EXPECT_DOUBLE_EQ(ironMixture->GetWmixt()[0], 1.0);
19+
20+
auto alloy = geom->GetMaterial("Alloy");
21+
ASSERT_NE(alloy, nullptr);
22+
ASSERT_TRUE(alloy->IsMixture());
23+
auto alloyMixture = static_cast<TGeoMixture *>(alloy);
24+
ASSERT_EQ(alloy->GetNelements(), 2);
25+
EXPECT_STREQ(alloy->GetElement(0)->GetName(), "FE");
26+
EXPECT_STREQ(alloy->GetElement(1)->GetName(), "NI");
27+
EXPECT_DOUBLE_EQ(alloyMixture->GetWmixt()[0], 0.5);
28+
EXPECT_DOUBLE_EQ(alloyMixture->GetWmixt()[1], 0.5);
29+
}

0 commit comments

Comments
 (0)