G4AttCheck.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 
00029 #include "G4AttCheck.hh"
00030 
00031 #include "globals.hh"
00032 
00033 #include "G4AttDef.hh"
00034 #include "G4AttDefStore.hh"
00035 #include "G4AttValue.hh"
00036 #include "G4UnitsTable.hh"
00037 #include "G4UIcommand.hh"
00038 
00039 G4AttCheck::G4AttCheck
00040 (const std::vector<G4AttValue>* values,
00041  const std::map<G4String,G4AttDef>* definitions):
00042   fpValues(values),
00043   fpDefinitions(definitions)
00044 {
00045   if (fFirst) {  // Initialise static containers.
00046     fFirst = false;
00047 
00048     // Legal Unit Category Types...
00049     fUnitCategories.insert("Length");
00050     fUnitCategories.insert("Energy");
00051     fUnitCategories.insert("Time");
00052     fUnitCategories.insert("Electric charge");
00053     fUnitCategories.insert("Volumic Mass");  // (Density)
00054 
00055     // Corresponding Standard Units...
00056     fStandardUnits["Length"] = "m";
00057     fStandardUnits["Energy"] = "MeV";
00058     fStandardUnits["Time"] = "ns";
00059     fStandardUnits["Electric charge"] = "e+";
00060     fStandardUnits["Volumic Mass"] = "kg/m3";
00061 
00062     // Legal Categories...
00063     fCategories.insert("Bookkeeping");
00064     fCategories.insert("Draw");
00065     fCategories.insert("Physics");
00066     fCategories.insert("PickAction");
00067     fCategories.insert("Association");
00068 
00069     // Legal units...
00070     fUnits.insert("");
00071     fUnits.insert("G4BestUnit");
00072     // ...plus any legal unit symbol ("MeV", "km", etc.)...
00073     G4UnitsTable& units = G4UnitDefinition::GetUnitsTable();
00074     for (size_t i = 0; i < units.size(); ++i) {
00075       if (fUnitCategories.find(units[i]->GetName()) !=
00076           fUnitCategories.end()) {
00077         //G4cout << units[i]->GetName() << G4endl;
00078         G4UnitsContainer& container = units[i]->GetUnitsList();
00079         for (size_t j = 0; j < container.size(); ++j) {
00080           //G4cout << container[j]->GetName() << ' '
00081           //       << container[j]->GetSymbol() << G4endl;
00082           fUnits.insert(container[j]->GetSymbol());
00083         }
00084       }
00085     }
00086 
00087     // Legal Value Types...
00088     fValueTypes.insert("G4String");
00089     fValueTypes.insert("G4int");
00090     fValueTypes.insert("G4double");
00091     fValueTypes.insert("G4ThreeVector");
00092     fValueTypes.insert("G4bool");
00093   }
00094 }
00095 
00096 G4AttCheck::~G4AttCheck() {}
00097 
00098 G4bool G4AttCheck::fFirst = true;
00099 
00100 std::set<G4String> G4AttCheck::fUnitCategories;
00101 
00102 std::map<G4String,G4String> G4AttCheck::fStandardUnits;
00103 
00104 std::set<G4String> G4AttCheck::fCategories;
00105 
00106 std::set<G4String> G4AttCheck::fUnits;
00107 
00108 std::set<G4String> G4AttCheck::fValueTypes;
00109 
00110 G4bool G4AttCheck::Check(const G4String& leader) const {
00111   // Check only.  Silent unless error - then G4cerr.  Returns error.
00112   G4bool error = false;
00113   static G4int iError = 0;
00114   G4bool print = false;
00115   if (iError < 10 || iError%100 == 0) {
00116     print = true;
00117   }
00118   using namespace std;
00119   if (!fpValues) return error;  // A null values vector is a valid situation.
00120   if (!fpDefinitions) {
00121     ++iError;
00122     error = true;
00123     if (print) {
00124       G4cerr <<
00125         "\n*******************************************************";
00126       if (leader != "") {
00127         G4cerr << '\n' << leader;
00128       }
00129       G4cerr <<
00130         "\nG4AttCheck: ERROR " << iError << ": Null definitions pointer"
00131         "\n*******************************************************"
00132              << G4endl;
00133     }
00134     return error;
00135   }
00136   vector<G4AttValue>::const_iterator iValue;
00137   for (iValue = fpValues->begin(); iValue != fpValues->end(); ++iValue) {
00138     const G4String& valueName = iValue->GetName();
00139     const G4String& value = iValue->GetValue();
00140     map<G4String,G4AttDef>::const_iterator iDef =
00141       fpDefinitions->find(valueName);
00142     if (iDef == fpDefinitions->end()) {
00143       ++iError;
00144       error = true;
00145       if (print) {
00146         G4cerr <<
00147           "\n*******************************************************";
00148         if (leader != "") {
00149           G4cerr << '\n' << leader;
00150         }
00151         G4cerr <<
00152           "\nG4AttCheck: ERROR " << iError << ": No G4AttDef for G4AttValue \""
00153                <<  valueName << "\": " << value <<
00154           "\n*******************************************************"
00155                << G4endl;
00156       }
00157     } else {
00158       const G4String& category = iDef->second.GetCategory();
00159       const G4String& extra = iDef->second.GetExtra();
00160       const G4String& valueType = iDef->second.GetValueType();
00161       if (fCategories.find(category) == fCategories.end()) {
00162         ++iError;
00163         error = true;
00164         if (print) {
00165           G4cerr <<
00166             "\n*******************************************************";
00167           if (leader != "") {
00168             G4cerr << '\n' << leader;
00169           }
00170           G4cerr <<
00171             "\nG4AttCheck: ERROR " << iError << ": Illegal Category Field \""
00172                  << category << "\" for G4AttValue \""
00173                  << valueName << "\": " << value <<
00174             "\n  Possible Categories:";
00175           set<G4String>::iterator i;
00176           for (i = fCategories.begin(); i != fCategories.end(); ++i) {
00177             G4cerr << ' ' << *i;
00178           }
00179           G4cerr <<
00180             "\n*******************************************************"
00181                  << G4endl;
00182         }
00183       }
00184       if(category == "Physics" && fUnits.find(extra) == fUnits.end()) {
00185         ++iError;
00186         error = true;
00187         if (print) {
00188           G4cerr <<
00189             "\n*******************************************************";
00190           if (leader != "") {
00191             G4cerr << '\n' << leader;
00192           }
00193           G4cerr <<
00194             "\nG4AttCheck: ERROR " << iError << ": Illegal Extra field \""
00195                  << extra << "\" for G4AttValue \""
00196                  << valueName << "\": " << value <<
00197             "\n  Possible Extra fields if Category==\"Physics\":\n    ";
00198           set<G4String>::iterator i;
00199           for (i = fUnits.begin(); i != fUnits.end(); ++i) {
00200             G4cerr << ' ' << *i;
00201           }
00202           G4cerr <<
00203             "\n*******************************************************"
00204                  << G4endl;
00205         }
00206       }
00207       if (fValueTypes.find(valueType) == fValueTypes.end()) {
00208         ++iError;
00209         error = true;
00210         if (print) {
00211           G4cerr <<
00212             "\n*******************************************************";
00213           if (leader != "") {
00214             G4cerr << '\n' << leader;
00215           }
00216           G4cerr <<
00217             "\nG4AttCheck: ERROR " << iError << ": Illegal Value Type field \""
00218                  << valueType << "\" for G4AttValue \""
00219                  << valueName << "\": " << value <<
00220             "\n  Possible Value Types:";
00221           set<G4String>::iterator i;
00222           for (i = fValueTypes.begin(); i != fValueTypes.end(); ++i) {
00223             G4cerr << ' ' << *i;
00224           }
00225           G4cerr <<
00226             "\n*******************************************************"
00227                << G4endl;
00228         }
00229       }
00230     }
00231   }
00232   return error;
00233 }
00234 
00235 std::ostream& operator<< (std::ostream& os, const G4AttCheck& ac)
00236 {
00237   using namespace std;
00238   if (!ac.fpDefinitions) {
00239     os << "G4AttCheck: ERROR: zero definitions pointer." << endl;
00240     return os;
00241   }
00242   G4String storeKey;
00243   if (G4AttDefStore::GetStoreKey(ac.fpDefinitions, storeKey)) {
00244     os << storeKey << ':' << endl;
00245   }
00246   if (!ac.fpValues) {
00247     // A null values vector is a valid situation.
00248     os << "G4AttCheck: zero values pointer." << endl;
00249     return os;
00250   }
00251   vector<G4AttValue>::const_iterator iValue;
00252   for (iValue = ac.fpValues->begin(); iValue != ac.fpValues->end(); ++iValue) {
00253     const G4String& valueName = iValue->GetName();
00254     const G4String& value = iValue->GetValue();
00255     map<G4String,G4AttDef>::const_iterator iDef =
00256       ac.fpDefinitions->find(valueName);
00257     G4bool error = false;
00258     if (iDef == ac.fpDefinitions->end()) {
00259       error = true;
00260       os << "G4AttCheck: ERROR: No G4AttDef for G4AttValue \""
00261          << valueName << "\": " << value << endl;
00262     } else {
00263       const G4String& category = iDef->second.GetCategory();
00264       const G4String& extra = iDef->second.GetExtra();
00265       const G4String& valueType = iDef->second.GetValueType();
00266       if (ac.fCategories.find(category) == ac.fCategories.end()) {
00267         error = true;
00268         os <<
00269           "G4AttCheck: ERROR: Illegal Category Field \"" << category
00270            << "\" for G4AttValue \"" << valueName << "\": " << value <<
00271           "\n  Possible Categories:";
00272         set<G4String>::iterator i;
00273         for (i = ac.fCategories.begin(); i != ac.fCategories.end(); ++i) {
00274           os << ' ' << *i;
00275         }
00276         os << endl;
00277       }
00278       if(category == "Physics" && ac.fUnits.find(extra) == ac.fUnits.end()) {
00279         error = true;
00280         os <<
00281           "G4AttCheck: ERROR: Illegal Extra field \""<< extra
00282            << "\" for G4AttValue \"" << valueName << "\": " << value <<
00283           "\n  Possible Extra fields if Category==\"Physics\":\n    ";
00284         set<G4String>::iterator i;
00285         for (i = ac.fUnits.begin(); i != ac.fUnits.end(); ++i) {
00286           os << ' ' << *i;
00287         }
00288         os << endl;
00289       }
00290       if (ac.fValueTypes.find(valueType) == ac.fValueTypes.end()) {
00291         error = true;
00292         os <<
00293           "G4AttCheck: ERROR: Illegal Value Type field \"" << valueType
00294            << "\" for G4AttValue \"" << valueName << "\": " << value <<
00295           "\n  Possible Value Types:";
00296         set<G4String>::iterator i;
00297         for (i = ac.fValueTypes.begin(); i != ac.fValueTypes.end(); ++i) {
00298           os << ' ' << *i;
00299         }
00300         os << endl;
00301       }
00302     }
00303     if (!error) {
00304       os << iDef->second.GetDesc()
00305          << " (" << valueName
00306          << "): " << value;
00307       if (iDef->second.GetCategory() == "Physics" &&
00308           !iDef->second.GetExtra().empty()) {
00309         os << " (" << iDef->second.GetExtra() << ")";
00310       }
00311       os << endl;
00312     }
00313   }
00314   return os;
00315 }
00316 
00317 void G4AttCheck::AddValuesAndDefs
00318 (std::vector<G4AttValue>* standardValues,
00319  std::map<G4String,G4AttDef>* standardDefinitions,
00320  const G4String& oldName,
00321  const G4String& name,
00322  const G4String& value,
00323  const G4String& extra,
00324  const G4String& description) const {
00325   // Add new G4AttDeff...
00326   standardValues->push_back(G4AttValue(name,value,""));
00327   // Copy original G4AttDef...
00328   (*standardDefinitions)[name] = fpDefinitions->find(oldName)->second;
00329   // ...and make appropriate changes...
00330   (*standardDefinitions)[name].SetName(name);
00331   (*standardDefinitions)[name].SetExtra(extra);
00332   if (description != "") (*standardDefinitions)[name].SetDesc(description);
00333 }
00334 
00335 G4bool G4AttCheck::Standard
00336 (std::vector<G4AttValue>* standardValues,
00337  std::map<G4String,G4AttDef>* standardDefinitions) const {
00338   // Places standard versions in provided vector and map and returns error.
00339   // Assumes valid input.  Use Check to check.
00340   using namespace std;
00341   G4bool error = false;
00342   vector<G4AttValue>::const_iterator iValue;
00343   for (iValue = fpValues->begin(); iValue != fpValues->end(); ++iValue) {
00344     const G4String& valueName = iValue->GetName();
00345     const G4String& value = iValue->GetValue();
00346     map<G4String,G4AttDef>::const_iterator iDef =
00347       fpDefinitions->find(valueName);
00348     if (iDef == fpDefinitions->end()) {
00349       error = true;
00350     } else {
00351       const G4String& category = iDef->second.GetCategory();
00352       const G4String& extra = iDef->second.GetExtra();
00353       const G4String& valueType = iDef->second.GetValueType();
00354       if (fCategories.find(category) == fCategories.end() ||
00355           (category == "Physics" && fUnits.find(extra) == fUnits.end()) ||
00356           fValueTypes.find(valueType) == fValueTypes.end()) {
00357         error = true;
00358       } else {
00359         if (category != "Physics") {  // Simply copy...
00360           standardValues->push_back(*iValue);
00361           (*standardDefinitions)[valueName] =
00362             fpDefinitions->find(valueName)->second;
00363         } else {  // "Physics"...
00364           if (extra.empty()) {  // Dimensionless...
00365             if (valueType == "G4ThreeVector") {  // Split vector into 3...
00366               G4ThreeVector internalValue =
00367                 G4UIcommand::ConvertTo3Vector(value);
00368               AddValuesAndDefs
00369                 (standardValues,standardDefinitions,
00370                  valueName,valueName+"-X",
00371                  G4UIcommand::ConvertToString(internalValue.x()),"",
00372                  fpDefinitions->find(valueName)->second.GetDesc()+"-X");
00373               AddValuesAndDefs
00374                 (standardValues,standardDefinitions,
00375                  valueName,valueName+"-Y",
00376                  G4UIcommand::ConvertToString(internalValue.y()),"",
00377                  fpDefinitions->find(valueName)->second.GetDesc()+"-Y");
00378               AddValuesAndDefs
00379                 (standardValues,standardDefinitions,
00380                  valueName,valueName+"-Z",
00381                  G4UIcommand::ConvertToString(internalValue.z()),"",
00382                  fpDefinitions->find(valueName)->second.GetDesc()+"-Z");
00383             } else {  // Simply copy...
00384               standardValues->push_back(*iValue);
00385               (*standardDefinitions)[valueName] =
00386                 fpDefinitions->find(valueName)->second;
00387             }
00388           } else {  // Dimensioned...
00389             G4String valueAndUnit;
00390             G4String unit;
00391             if (extra == "G4BestUnit") {
00392               valueAndUnit = value;
00393               valueAndUnit = valueAndUnit.strip();
00394               unit = valueAndUnit.substr(valueAndUnit.rfind(' ')+1);
00395             } else {
00396               valueAndUnit = value + ' ' + extra;
00397               valueAndUnit = valueAndUnit.strip();
00398               unit = extra;
00399             }
00400             G4String unitCategory = G4UnitDefinition::GetCategory(unit);
00401             if (fUnitCategories.find(unitCategory) != fUnitCategories.end()) {
00402               G4String standardUnit = fStandardUnits[unitCategory];
00403               G4double valueOfStandardUnit =
00404                 G4UnitDefinition::GetValueOf(standardUnit);
00405 //            G4String exstr = iDef->second.GetExtra();
00406               if (valueType == "G4ThreeVector") {  // Split vector into 3...
00407                 G4ThreeVector internalValue =
00408                   G4UIcommand::ConvertToDimensioned3Vector(valueAndUnit);
00409                 AddValuesAndDefs
00410                   (standardValues,standardDefinitions,
00411                    valueName,valueName+"-X",
00412                    G4UIcommand::ConvertToString
00413                    (internalValue.x()/valueOfStandardUnit),
00414                    standardUnit,
00415                    fpDefinitions->find(valueName)->second.GetDesc()+"-X");
00416                 AddValuesAndDefs
00417                   (standardValues,standardDefinitions,
00418                    valueName,valueName+"-Y",
00419                    G4UIcommand::ConvertToString
00420                    (internalValue.y()/valueOfStandardUnit),
00421                    standardUnit,
00422                    fpDefinitions->find(valueName)->second.GetDesc()+"-Y");
00423                 AddValuesAndDefs
00424                   (standardValues,standardDefinitions,
00425                    valueName,valueName+"-Z",
00426                    G4UIcommand::ConvertToString
00427                    (internalValue.z()/valueOfStandardUnit),
00428                    standardUnit,
00429                    fpDefinitions->find(valueName)->second.GetDesc()+"-Z");
00430               } else {
00431                 G4double internalValue =
00432                   G4UIcommand::ConvertToDimensionedDouble(valueAndUnit);
00433                 AddValuesAndDefs
00434                   (standardValues,standardDefinitions,
00435                    valueName,valueName,
00436                    G4UIcommand::ConvertToString
00437                    (internalValue/valueOfStandardUnit),
00438                    standardUnit);
00439               }
00440             }
00441           }
00442         }
00443       }
00444     }
00445   }
00446   if (error) {
00447     G4cerr << "G4AttCheck::Standard: Conversion error." << G4endl;
00448   }
00449   return error;
00450 }

Generated on Mon May 27 17:47:41 2013 for Geant4 by  doxygen 1.4.7