Skip to content

Commit

Permalink
Merge branch 'cjbackhouse_gdml_consts'
Browse files Browse the repository at this point in the history
  • Loading branch information
agheata committed Sep 29, 2016
2 parents 99c55ea + 1aea6f2 commit 5a8c67d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
7 changes: 2 additions & 5 deletions geom/gdml/inc/TGDMLParse.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,6 @@ class TGDMLParse : public TObject {
}

virtual ~TGDMLParse() { //destructor

for (size_t i = 0; i < fformvec.size(); i++)
if (fformvec[i] != NULL) delete fformvec[i];
}

static TGeoVolume* StartGDML(const char* filename) {
Expand Down Expand Up @@ -205,7 +202,7 @@ class TGDMLParse : public TObject {
typedef std::map<std::string, std::string> ReflectionsMap;
typedef std::map<std::string, std::string> ReflVolMap;
typedef std::map<std::string, double> FracMap;
typedef std::vector<TFormula*> FormVec;
typedef std::map<std::string, double> ConstMap;

PosMap fposmap; //!Map containing position names and the TGeoTranslation for it
RotMap frotmap; //!Map containing rotation names and the TGeoRotation for it
Expand All @@ -221,7 +218,7 @@ class TGDMLParse : public TObject {
ReflSolidMap freflsolidmap; //!Map containing reflection names and the TGDMLRefl for it - containing refl matrix
ReflVolMap freflvolmap; //!Map containing reflected volume names and the solid ref for it
FileMap ffilemap; //!Map containing files parsed during entire parsing, with their world volume name
FormVec fformvec; //!Vector containing constant functions for GDML constant definitions
ConstMap fconsts; //!Map containing values of constants declared in the file

ClassDef(TGDMLParse, 0) //imports GDML using DOM and binds it to ROOT
};
Expand Down
64 changes: 60 additions & 4 deletions geom/gdml/src/TGDMLParse.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ XMLNodePointer_t TGDMLParse::ConProcess(TXMLEngine* gdml, XMLNodePointer_t node,
// name = TString::Format("%s_%s", name.Data(), fCurrentFile);
//}

fformvec.push_back(new TFormula(name, value));
fconsts[name.Data()] = Value(value);

return node;
}
Expand Down Expand Up @@ -521,9 +521,65 @@ Double_t TGDMLParse::GetScaleVal(const char* sunit)

Double_t TGDMLParse::Value(const char* svalue) const
{
static TString s;
s = svalue;
return s.Atof();
char* end;
double val = strtod(svalue, &end);

// Succesfully parsed all the characters up to the ending NULL, so svalue
// was a simple number.
if(*end == 0) return val;

// Otherwise we'll use TFormula to evaluate the string, having first found
// all the GDML variable names in it and marked them with [] so that
// TFormula will recognize them as parameters.

std::string expanded;
expanded.reserve(strlen(svalue)*2);

// Be careful about locale so we always mean the same thing by
// "alphanumeric"
const std::locale& loc = std::locale::classic(); // "C" locale

// Walk through the string inserting '[' and ']' where necessary
const char* p = svalue;
while(*p){
// Find a site for a '['. Just before the first alphabetic character
for(; *p != 0; ++p){
if(std::isalpha(*p, loc) || *p == '_'){
expanded += '[';
break;
}
expanded += *p;
}
// If we reached the end of the string while looking for the start of a
// token then we're done
if(*p == 0) break;

// Now look for the position of the following ']'. Straight before the
// first non-alphanumeric character
for(; *p != 0; ++p){
if(!isalnum(*p, loc) && *p != '_'){
expanded += ']';
break;
}
expanded += *p;
}
// If we reached the end of the string while looking for a position for a
// ']' then it goes here
if(*p == 0) expanded += ']';
} // end loop over svalue

TFormula f("TFormula", expanded.c_str());

// Tell the TFormula about every parameter we know about
for(auto it: fconsts) f.SetParameter(it.first.c_str(), it.second);

val = f.Eval(0);

if(std::isnan(val) || std::isinf(val)){
Fatal("Value", "Got bad value %lf from string '%s'", val, svalue);
}

return val;
}

////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 5a8c67d

Please sign in to comment.