~mbogomilov/maus/devel3

« back to all changes in this revision

Viewing changes to src/legacy/Config/ModuleTextFileIO.cc

  • Committer: Durga Rajaram
  • Date: 2014-01-14 07:07:02 UTC
  • mfrom: (659.1.80 relcand)
  • Revision ID: durga@fnal.gov-20140114070702-2l1fuj1w6rraw7xe
Tags: MAUS-v0.7.6
MAUS-v0.7.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
 
15
15
#include "Config/MiceModule.hh"
16
16
#include "Config/ModuleTextFileIO.hh"
17
 
#include "Interface/Squeal.hh"
 
17
#include "Utils/Exception.hh"
18
18
 
19
19
#include "CLHEP/Units/SystemOfUnits.h"
20
20
 
47
47
ModuleTextFileIO::ModuleTextFileIO( std::string fname ) : _this(NULL), _hasFile("")
48
48
{
49
49
  if (fname == "")
50
 
                throw(Squeal(Squeal::recoverable,
 
50
                throw(MAUS::Exception(MAUS::Exception::recoverable,
51
51
                 "Attempting to open MiceModule with no filename",
52
52
                 "ModuleTextFileIO::ModuleTextFileIO"));
53
53
        _this = new MiceModule(NULL, stripDirs(fname));
54
54
        if(getenv( "MICEFILES" ) == NULL) 
55
 
                throw(Squeal(Squeal::recoverable, "MICEFILES environment variable was not defined", "ModuleTextFileIO::ModuleTextFileIO"));
 
55
                throw(MAUS::Exception(MAUS::Exception::recoverable, "MICEFILES environment variable was not defined", "ModuleTextFileIO::ModuleTextFileIO"));
56
56
        std::string fnam = std::string(getenv( "MICEFILES" )) + "/Models/Configurations/" + fname;
57
57
        std::ifstream fin(fnam.c_str());
58
58
        if(!fin) 
61
61
                if(!fin) 
62
62
                {
63
63
                        fin.close(); 
64
 
                        throw(Squeal(Squeal::recoverable, "Failed to open root module file at "+fnam+" or "+fname, "MiceModule::MiceModule(std::string)"));
 
64
                        throw(MAUS::Exception(MAUS::Exception::recoverable, "Failed to open root module file at "+fnam+" or "+fname, "MiceModule::MiceModule(std::string)"));
65
65
                }
66
66
        }
67
67
        std::stringstream inOut1, inOut2, inOut3;
91
91
                        else if(key.find("Module")       != std::string::npos) newModule(line, in);
92
92
                        else if(key.find("Substitution") != std::string::npos); //do nothing, should be handled by preprocessor
93
93
                        else if(key=="" || key =="//" || key=="!"); //ignore white space and comments  
94
 
                        else throw(Squeal(Squeal::recoverable, "Failed to parse module line "+line+" in module "+name+_hasFile, "MiceModule::setModule(std::istream*)"));
 
94
                        else throw(MAUS::Exception(MAUS::Exception::recoverable, "Failed to parse module line "+line+" in module "+name+_hasFile, "MiceModule::setModule(std::istream*)"));
95
95
                }
96
 
                catch(Squeal squee) { throw squee;}
97
 
                catch(...) {throw Squeal(Squeal::recoverable, "Failed to parse module line "+line+" in module "+name+_hasFile, "MiceModule::setModule(std::istream*)");}
 
96
                catch(MAUS::Exception exc) { throw exc;}
 
97
                catch(...) {throw MAUS::Exception(MAUS::Exception::recoverable, "Failed to parse module line "+line+" in module "+name+_hasFile, "MiceModule::setModule(std::istream*)");}
98
98
        }
99
99
        checkRepeats();
100
100
}
158
158
 
159
159
        modLine       = "";
160
160
        if(getenv( "MICEFILES" ) == NULL) 
161
 
                throw(Squeal(Squeal::recoverable, "Error - MICEFILES environment variable was not defined", "ModuleTextFileIO::ModuleTextFileIO"));
 
161
                throw(MAUS::Exception(MAUS::Exception::recoverable, "Error - MICEFILES environment variable was not defined", "ModuleTextFileIO::ModuleTextFileIO"));
162
162
        std::string file = std::string(getenv( "MICEFILES" )) + "/Models/Modules/" + modName;
163
163
        std::ifstream fin(file.c_str());
164
164
        if(!fin) 
180
180
void ModuleTextFileIO::checkRepeats()
181
181
{
182
182
        if     (_this->propertyExistsThis("RepeatModule",  "bool") && _this->propertyExistsThis("RepeatModule2",  "bool"))
183
 
                throw(Squeal(Squeal::recoverable, "Multiple repeat module types defined in MiceModule "+_this->fullName()+_hasFile, "ModuleTextFileIO::checkRepeats"));
 
183
                throw(MAUS::Exception(MAUS::Exception::recoverable, "Multiple repeat module types defined in MiceModule "+_this->fullName()+_hasFile, "ModuleTextFileIO::checkRepeats"));
184
184
        if     (_this->propertyExistsThis("RepeatModule",  "bool")) {if(!_this->propertyBool("RepeatModule"))  return;}
185
185
        else if(_this->propertyExistsThis("RepeatModule2", "bool")) {if(!_this->propertyBool("RepeatModule2")) return;}
186
186
        else return;
240
240
      std::string       dimensions;  
241
241
      ist >> dimensions;
242
242
      if(dimensions != "Dimensions") 
243
 
        throw(Squeal(Squeal::recoverable, "Did not recognise dimensions input in module "+_this->fullName()+_hasFile, "MiceModule::setDimensions(string, string)"));
 
243
        throw(MAUS::Exception(MAUS::Exception::recoverable, "Did not recognise dimensions input in module "+_this->fullName()+_hasFile, "MiceModule::setDimensions(string, string)"));
244
244
      if( volumeType == "Cylinder" || volumeType == "Sphere" || volumeType == "Polycone")
245
245
      {
246
246
        std::stringstream dimStream;
276
276
      else if( volumeType == "Multipole" || volumeType == "Quadrupole" 
277
277
               || volumeType == "None" || volumeType == "Boolean")
278
278
        _this->addPropertyHep3Vector("Dimensions", CLHEP::Hep3Vector(0,0,0));
279
 
      else throw(Squeal(Squeal::recoverable, "Did not recognise volume type "+volumeType+" in module "+_this->fullName()+_hasFile, "MiceModule::setDimensions(std::string)"));
 
279
      else throw(MAUS::Exception(MAUS::Exception::recoverable, "Did not recognise volume type "+volumeType+" in module "+_this->fullName()+_hasFile, "MiceModule::setDimensions(std::string)"));
280
280
}
281
281
 
282
282
template <class Temp> Temp        ModuleTextFileIO::fromString(const std::string& source)
296
296
  try {
297
297
      out = static_cast<int>(_evaluator->evaluate(value));
298
298
  }
299
 
  catch (Squeal squee) {
300
 
      throw(Squeal(Squeal::recoverable, "Could not convert "+source+" to an int", "ModuleTextFileIO::parseString(const std::string&, int&)"));
 
299
  catch (MAUS::Exception exc) {
 
300
      throw(MAUS::Exception(MAUS::Exception::recoverable, "Could not convert "+source+" to an int", "ModuleTextFileIO::parseString(const std::string&, int&)"));
301
301
  }
302
302
}
303
303
 
312
312
  try {
313
313
      out = _evaluator->evaluate(value);
314
314
  }
315
 
  catch (Squeal squee) {
316
 
      squee.Print();
317
 
      throw(Squeal(Squeal::recoverable, "Could not convert "+source+" to a double", "ModuleTextFileIO::parseString(const std::string&, double&)"));
 
315
  catch (MAUS::Exception exc) {
 
316
      exc.Print();
 
317
      throw(MAUS::Exception(MAUS::Exception::recoverable, "Could not convert "+source+" to a double", "ModuleTextFileIO::parseString(const std::string&, double&)"));
318
318
  }
319
319
  if (_units == NULL) _units = new MAUS::MAUSEvaluator();
320
320
  out *= _units->evaluate(units);
328
328
    out = true;
329
329
  else if(source_lower == "0" || source_lower == "false" || source_lower == ".false.")
330
330
    out = false;
331
 
  else throw(Squeal(Squeal::recoverable, "Could not convert "+source+" to a boolean", "ModuleTextFileIO::parseString"));
 
331
  else throw(MAUS::Exception(MAUS::Exception::recoverable, "Could not convert "+source+" to a boolean", "ModuleTextFileIO::parseString"));
332
332
}
333
333
 
334
334
void ModuleTextFileIO::parseString(const std::string& source, std::string&        out) 
359
359
    ss >> eval;
360
360
    parseString(eval, out[i]); //will call evaluator on each in turn
361
361
    if(!ss)
362
 
      throw(Squeal(Squeal::recoverable, "Failed to parse "+source+" as Hep3Vector", "ModuleTextFileIO::parseString(string, Hep3Vector)"));
 
362
      throw(MAUS::Exception(MAUS::Exception::recoverable, "Failed to parse "+source+" as Hep3Vector", "ModuleTextFileIO::parseString(string, Hep3Vector)"));
363
363
  }
364
364
  ss >> units;
365
365
  if (_units == NULL) _units = new MAUS::MAUSEvaluator();
383
383
      _this->addPropertyString( propName, prop );
384
384
    else if( key == "PropertyDouble" )
385
385
      _this->addPropertyDouble(propName, prop);
386
 
    else throw(Squeal(Squeal::recoverable, "Did not recognise property type "+key+" in module "+_this->fullName()+_hasFile, "ModuleTextFileIO::readProperty(std::string)"));
 
386
    else throw(MAUS::Exception(MAUS::Exception::recoverable, "Did not recognise property type "+key+" in module "+_this->fullName()+_hasFile, "ModuleTextFileIO::readProperty(std::string)"));
387
387
}
388
388
 
389
389
std::string ModuleTextFileIO::stripDirs(std::string name)
407
407
      std::string  name,value;
408
408
      linestream >> name >> value;
409
409
      if(!linestream)
410
 
        throw(Squeal(Squeal::recoverable, "Failed to parse substitution "+line, "ModuleTextFileIO::findSubs"));
 
410
        throw(MAUS::Exception(MAUS::Exception::recoverable, "Failed to parse substitution "+line, "ModuleTextFileIO::findSubs"));
411
411
      if(value.find("$$") != std::string::npos)
412
 
        throw(Squeal(Squeal::recoverable, "Value "+value+" for Substitution "+name+" contains reserved character $", "ModuleTextFileIO::findSubs"));
 
412
        throw(MAUS::Exception(MAUS::Exception::recoverable, "Value "+value+" for Substitution "+name+" contains reserved character $", "ModuleTextFileIO::findSubs"));
413
413
      if(name[0] != '$')
414
 
        throw(Squeal(Squeal::recoverable, "Substitution name "+name+" must start with $", "ModuleTextFileIO::findSubs"));
 
414
        throw(MAUS::Exception(MAUS::Exception::recoverable, "Substitution name "+name+" must start with $", "ModuleTextFileIO::findSubs"));
415
415
      if(name[1] == '$')
416
 
        throw(Squeal(Squeal::recoverable, "Substitution name "+name+" must not start with $$ - this is reserved for internal parameters", "ModuleTextFileIO::findSubs"));
 
416
        throw(MAUS::Exception(MAUS::Exception::recoverable, "Substitution name "+name+" must not start with $$ - this is reserved for internal parameters", "ModuleTextFileIO::findSubs"));
417
417
      _substitutions[name] = value;
418
418
      Squeak::mout(Squeak::debug) << "Substitution "+name+" "+value << std::endl;
419
419
    }
470
470
  while (mother != NULL) {
471
471
      if (mother->propertyExistsThis("RepeatModule2", "bool") &&
472
472
          mother->propertyBoolThis("RepeatModule2")) {
473
 
          throw(Squeal(Squeal::recoverable,
 
473
          throw(MAUS::Exception(MAUS::Exception::recoverable,
474
474
                       "Nested RepeatModule2 is not allowed - found in module "+
475
475
                       first->name()+" and ancestor "+mother->name(),
476
476
                       "ModuleTextFileIO::repeatModule2"));