Skip to content

Commit

Permalink
Merge pull request #499 from wrzof/message_optical_g4v11
Browse files Browse the repository at this point in the history
Add nicer error message for unknown property when xml file is read.
  • Loading branch information
tbaudier authored Mar 21, 2022
2 parents 98658e0 + d0d3f7f commit c16c5d4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
4 changes: 4 additions & 0 deletions source/general/include/GateXMLDocument.hh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class GateXMLDocument
GateXMLDocument(const G4String& filename);
~GateXMLDocument();

const G4String &GetFilename() const;

G4bool Ok() const;

G4String GetName();
Expand All @@ -59,6 +61,8 @@ class GateXMLDocument
xmlDocPtr m_doc;
xmlNodePtr m_cur;
G4bool m_reset;
const G4String m_filename;

};

//! Reads a properties table from the xml-file given by doc
Expand Down
58 changes: 54 additions & 4 deletions source/general/src/GateXMLDocument.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ See LICENSE.md for further details
* Use the Ok() method to check if the opening went ok.
* */
GateXMLDocument::GateXMLDocument(const G4String& filename) :
m_ok(false), m_reset(true)
m_ok(false), m_reset(true), m_filename(filename)

//
// SJ COMMENTS## : read the file by using a messenger mechanism
Expand Down Expand Up @@ -305,7 +305,28 @@ void GateXMLDocument::SetState(GateXMLDocumentState state)
m_cur = state.cur;
m_reset = state.reset;
}


// geant4 v11 replaces some properties name by new one
// We propose suggestion for replacements in error message.
const G4String suggest_values_for_keys(const G4String& key)
{
if(key == "FASTTIMECONSTANT")
return "SCINTILLATIONTIMECONSTANT1";
else if( key == "YIELDRATIO")
return "SCINTILLATIONYIELD1";
else if( key == "FASTCOMPONENT")
return "SCINTILLATIONCOMPONENT1";
return "?";

}



const G4String &GateXMLDocument::GetFilename() const
{
return m_filename;
}

G4MaterialPropertiesTable* ReadMaterialPropertiesTable(GateXMLDocument* doc)
{
G4MaterialPropertiesTable* table = 0;
Expand All @@ -328,7 +349,21 @@ G4MaterialPropertiesTable* ReadMaterialPropertiesTable(GateXMLDocument* doc)
G4String unitstr = "1 " + doc->GetProperty("unit");
value *= G4UIcmdWithADoubleAndUnit::GetNewDoubleValue(unitstr.c_str());
}
table->AddConstProperty(property.c_str(), value);

auto list_of_known_properties_key = &table->GetMaterialConstPropertyNames();
if(std::count(list_of_known_properties_key->begin(), list_of_known_properties_key->end(), property))
{
table->AddConstProperty(property.c_str(), value);
}
else
{
G4cout << "Unknown property '" << property << "' in xml file '" << doc->GetFilename() << "'. Abort simulation." << G4endl;
G4cout << "Suggestion: property '"<< property << "' can be replaced by '" << suggest_values_for_keys(property) << "'" << G4endl;
exit(-1);

}


}
else if (doc->GetName() == "propertyvector")
{
Expand Down Expand Up @@ -358,7 +393,22 @@ G4MaterialPropertiesTable* ReadMaterialPropertiesTable(GateXMLDocument* doc)
G4double value = G4UIcmdWithADouble::GetNewDoubleValue(valuestr.c_str());
vector->InsertValues(energy*energyunit, value*unit);
}
table->AddProperty(property.c_str(), vector);


auto list_of_known_properties_key = &table->GetMaterialPropertyNames();
if(std::count(list_of_known_properties_key->begin(), list_of_known_properties_key->end(), property))
{
table->AddProperty(property.c_str(), vector);
}
else
{
G4cout << "Unknown propertyvector '" << property << "' in xml file '" << doc->GetFilename() << "'. Abort simulation." << G4endl;
G4cout << "Suggestion: propertyvector '"<< property << "' can be replaced by '" << suggest_values_for_keys(property) << "'" << G4endl;
exit(-1);
}



doc->Leave();
}
}
Expand Down

0 comments on commit c16c5d4

Please sign in to comment.