source: trunk/source/graphics_reps/src/G4AttCheck.cc @ 1346

Last change on this file since 1346 was 1337, checked in by garnier, 14 years ago

tag geant4.9.4 beta 1 + modifs locales

File size: 15.1 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
5// * The  Geant4 software  is  copyright of the Copyright Holders  of *
6// * the Geant4 Collaboration.  It is provided  under  the terms  and *
7// * conditions of the Geant4 Software License,  included in the file *
8// * LICENSE and available at  http://cern.ch/geant4/license .  These *
9// * include a list of copyright holders.                             *
10// *                                                                  *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work  make  any representation or  warranty, express or implied, *
14// * regarding  this  software system or assume any liability for its *
15// * use.  Please see the license in the file  LICENSE  and URL above *
16// * for the full disclaimer and the limitation of liability.         *
17// *                                                                  *
18// * This  code  implementation is the result of  the  scientific and *
19// * technical work of the GEANT4 collaboration.                      *
20// * By using,  copying,  modifying or  distributing the software (or *
21// * any work based  on the software)  you  agree  to acknowledge its *
22// * use  in  resulting  scientific  publications,  and indicate your *
23// * acceptance of all terms of the Geant4 Software license.          *
24// ********************************************************************
25//
26//
27// $Id: G4AttCheck.cc,v 1.15 2006/11/07 11:50:22 allison Exp $
28// GEANT4 tag $Name: geant4-09-04-beta-01 $
29
30#include "G4AttCheck.hh"
31
32#include "globals.hh"
33
34#include "G4AttDef.hh"
35#include "G4AttDefStore.hh"
36#include "G4AttValue.hh"
37#include "G4UnitsTable.hh"
38#include "G4UIcommand.hh"
39
40G4AttCheck::G4AttCheck
41(const std::vector<G4AttValue>* values,
42 const std::map<G4String,G4AttDef>* definitions):
43  fpValues(values),
44  fpDefinitions(definitions)
45{
46  if (fFirst) {  // Initialise static containers.
47    fFirst = false;
48
49    // Legal Unit Category Types...
50    fUnitCategories.insert("Length");
51    fUnitCategories.insert("Energy");
52    fUnitCategories.insert("Time");
53    fUnitCategories.insert("Electric charge");
54    fUnitCategories.insert("Volumic Mass");  // (Density)
55
56    // Corresponding Standard Units...
57    fStandardUnits["Length"] = "m";
58    fStandardUnits["Energy"] = "MeV";
59    fStandardUnits["Time"] = "ns";
60    fStandardUnits["Electric charge"] = "e+";
61    fStandardUnits["Volumic Mass"] = "kg/m3";
62
63    // Legal Categories...
64    fCategories.insert("Bookkeeping");
65    fCategories.insert("Draw");
66    fCategories.insert("Physics");
67    fCategories.insert("PickAction");
68    fCategories.insert("Association");
69
70    // Legal units...
71    fUnits.insert("");
72    fUnits.insert("G4BestUnit");
73    // ...plus any legal unit symbol ("MeV", "km", etc.)...
74    G4UnitsTable& units = G4UnitDefinition::GetUnitsTable();
75    for (size_t i = 0; i < units.size(); ++i) {
76      if (fUnitCategories.find(units[i]->GetName()) !=
77          fUnitCategories.end()) {
78        //G4cout << units[i]->GetName() << G4endl;
79        G4UnitsContainer& container = units[i]->GetUnitsList();
80        for (size_t j = 0; j < container.size(); ++j) {
81          //G4cout << container[j]->GetName() << ' '
82          //       << container[j]->GetSymbol() << G4endl;
83          fUnits.insert(container[j]->GetSymbol());
84        }
85      }
86    }
87
88    // Legal Value Types...
89    fValueTypes.insert("G4String");
90    fValueTypes.insert("G4int");
91    fValueTypes.insert("G4double");
92    fValueTypes.insert("G4ThreeVector");
93    fValueTypes.insert("G4bool");
94  }
95}
96
97G4AttCheck::~G4AttCheck() {}
98
99G4bool G4AttCheck::fFirst = true;
100
101std::set<G4String> G4AttCheck::fUnitCategories;
102
103std::map<G4String,G4String> G4AttCheck::fStandardUnits;
104
105std::set<G4String> G4AttCheck::fCategories;
106
107std::set<G4String> G4AttCheck::fUnits;
108
109std::set<G4String> G4AttCheck::fValueTypes;
110
111G4bool G4AttCheck::Check(const G4String& leader) const {
112  // Check only.  Silent unless error - then G4cerr.  Returns error.
113  G4bool error = false;
114  static G4int iError = 0;
115  G4bool print = false;
116  if (iError < 10 || iError%100 == 0) {
117    print = true;
118  }
119  using namespace std;
120  if (!fpValues) return error;  // A null values vector is a valid situation.
121  if (!fpDefinitions) {
122    ++iError;
123    error = true;
124    if (print) {
125      G4cerr <<
126        "\n*******************************************************";
127      if (leader != "") {
128        G4cerr << '\n' << leader;
129      }
130      G4cerr <<
131        "\nG4AttCheck: ERROR " << iError << ": Null definitions pointer"
132        "\n*******************************************************"
133             << G4endl;
134      return error;
135    }
136  }
137  vector<G4AttValue>::const_iterator iValue;
138  for (iValue = fpValues->begin(); iValue != fpValues->end(); ++iValue) {
139    const G4String& valueName = iValue->GetName();
140    const G4String& value = iValue->GetValue();
141    map<G4String,G4AttDef>::const_iterator iDef =
142      fpDefinitions->find(valueName);
143    if (iDef == fpDefinitions->end()) {
144      ++iError;
145      error = true;
146      if (print) {
147        G4cerr <<
148          "\n*******************************************************";
149        if (leader != "") {
150          G4cerr << '\n' << leader;
151        }
152        G4cerr <<
153          "\nG4AttCheck: ERROR " << iError << ": No G4AttDef for G4AttValue \""
154               <<  valueName << "\": " << value <<
155          "\n*******************************************************"
156               << G4endl;
157      }
158    } else {
159      const G4String& category = iDef->second.GetCategory();
160      const G4String& extra = iDef->second.GetExtra();
161      const G4String& valueType = iDef->second.GetValueType();
162      if (fCategories.find(category) == fCategories.end()) {
163        ++iError;
164        error = true;
165        if (print) {
166          G4cerr <<
167            "\n*******************************************************";
168          if (leader != "") {
169            G4cerr << '\n' << leader;
170          }
171          G4cerr <<
172            "\nG4AttCheck: ERROR " << iError << ": Illegal Category Field \""
173                 << category << "\" for G4AttValue \""
174                 << valueName << "\": " << value <<
175            "\n  Possible Categories:";
176          set<G4String>::iterator i;
177          for (i = fCategories.begin(); i != fCategories.end(); ++i) {
178            G4cerr << ' ' << *i;
179          }
180          G4cerr <<
181            "\n*******************************************************"
182                 << G4endl;
183        }
184      }
185      if(category == "Physics" && fUnits.find(extra) == fUnits.end()) {
186        ++iError;
187        error = true;
188        if (print) {
189          G4cerr <<
190            "\n*******************************************************";
191          if (leader != "") {
192            G4cerr << '\n' << leader;
193          }
194          G4cerr <<
195            "\nG4AttCheck: ERROR " << iError << ": Illegal Extra field \""
196                 << extra << "\" for G4AttValue \""
197                 << valueName << "\": " << value <<
198            "\n  Possible Extra fields if Category==\"Physics\":\n    ";
199          set<G4String>::iterator i;
200          for (i = fUnits.begin(); i != fUnits.end(); ++i) {
201            G4cerr << ' ' << *i;
202          }
203          G4cerr <<
204            "\n*******************************************************"
205                 << G4endl;
206        }
207      }
208      if (fValueTypes.find(valueType) == fValueTypes.end()) {
209        ++iError;
210        error = true;
211        if (print) {
212          G4cerr <<
213            "\n*******************************************************";
214          if (leader != "") {
215            G4cerr << '\n' << leader;
216          }
217          G4cerr <<
218            "\nG4AttCheck: ERROR " << iError << ": Illegal Value Type field \""
219                 << valueType << "\" for G4AttValue \""
220                 << valueName << "\": " << value <<
221            "\n  Possible Value Types:";
222          set<G4String>::iterator i;
223          for (i = fValueTypes.begin(); i != fValueTypes.end(); ++i) {
224            G4cerr << ' ' << *i;
225          }
226          G4cerr <<
227            "\n*******************************************************"
228               << G4endl;
229        }
230      }
231    }
232  }
233  return error;
234}
235
236std::ostream& operator<< (std::ostream& os, const G4AttCheck& ac)
237{
238  using namespace std;
239  if (!ac.fpDefinitions) {
240    os << "G4AttCheck: ERROR: zero definitions pointer." << endl;
241    return os;
242  }
243  G4String storeKey;
244  if (G4AttDefStore::GetStoreKey(ac.fpDefinitions, storeKey)) {
245    os << storeKey << ':' << endl;
246  }
247  if (!ac.fpValues) {
248    // A null values vector is a valid situation.
249    os << "G4AttCheck: zero values pointer." << endl;
250    return os;
251  }
252  vector<G4AttValue>::const_iterator iValue;
253  for (iValue = ac.fpValues->begin(); iValue != ac.fpValues->end(); ++iValue) {
254    const G4String& valueName = iValue->GetName();
255    const G4String& value = iValue->GetValue();
256    map<G4String,G4AttDef>::const_iterator iDef =
257      ac.fpDefinitions->find(valueName);
258    G4bool error = false;
259    if (iDef == ac.fpDefinitions->end()) {
260      error = true;
261      os << "G4AttCheck: ERROR: No G4AttDef for G4AttValue \""
262         << valueName << "\": " << value << endl;
263    } else {
264      const G4String& category = iDef->second.GetCategory();
265      const G4String& extra = iDef->second.GetExtra();
266      const G4String& valueType = iDef->second.GetValueType();
267      if (ac.fCategories.find(category) == ac.fCategories.end()) {
268        error = true;
269        os <<
270          "G4AttCheck: ERROR: Illegal Category Field \"" << category
271           << "\" for G4AttValue \"" << valueName << "\": " << value <<
272          "\n  Possible Categories:";
273        set<G4String>::iterator i;
274        for (i = ac.fCategories.begin(); i != ac.fCategories.end(); ++i) {
275          os << ' ' << *i;
276        }
277        os << endl;
278      }
279      if(category == "Physics" && ac.fUnits.find(extra) == ac.fUnits.end()) {
280        error = true;
281        os <<
282          "G4AttCheck: ERROR: Illegal Extra field \""<< extra
283           << "\" for G4AttValue \"" << valueName << "\": " << value <<
284          "\n  Possible Extra fields if Category==\"Physics\":\n    ";
285        set<G4String>::iterator i;
286        for (i = ac.fUnits.begin(); i != ac.fUnits.end(); ++i) {
287          os << ' ' << *i;
288        }
289        os << endl;
290      }
291      if (ac.fValueTypes.find(valueType) == ac.fValueTypes.end()) {
292        error = true;
293        os <<
294          "G4AttCheck: ERROR: Illegal Value Type field \"" << valueType
295           << "\" for G4AttValue \"" << valueName << "\": " << value <<
296          "\n  Possible Value Types:";
297        set<G4String>::iterator i;
298        for (i = ac.fValueTypes.begin(); i != ac.fValueTypes.end(); ++i) {
299          os << ' ' << *i;
300        }
301        os << endl;
302      }
303    }
304    if (!error) {
305      os << iDef->second.GetDesc()
306         << " (" << valueName
307         << "): " << value;
308      if (iDef->second.GetCategory() == "Physics" &&
309          !iDef->second.GetExtra().empty()) {
310        os << " (" << iDef->second.GetExtra() << ")";
311      }
312      os << endl;
313    }
314  }
315  return os;
316}
317
318void G4AttCheck::AddValuesAndDefs
319(std::vector<G4AttValue>* standardValues,
320 std::map<G4String,G4AttDef>* standardDefinitions,
321 const G4String& oldName,
322 const G4String& name,
323 const G4String& value,
324 const G4String& extra,
325 const G4String& description) const {
326  // Add new G4AttDeff...
327  standardValues->push_back(G4AttValue(name,value,""));
328  // Copy original G4AttDef...
329  (*standardDefinitions)[name] = fpDefinitions->find(oldName)->second;
330  // ...and make appropriate changes...
331  (*standardDefinitions)[name].SetName(name);
332  (*standardDefinitions)[name].SetExtra(extra);
333  if (description != "") (*standardDefinitions)[name].SetDesc(description);
334}
335
336G4bool G4AttCheck::Standard
337(std::vector<G4AttValue>* standardValues,
338 std::map<G4String,G4AttDef>* standardDefinitions) const {
339  // Places standard versions in provided vector and map and returns error.
340  // Assumes valid input.  Use Check to check.
341  using namespace std;
342  G4bool error = false;
343  vector<G4AttValue>::const_iterator iValue;
344  for (iValue = fpValues->begin(); iValue != fpValues->end(); ++iValue) {
345    const G4String& valueName = iValue->GetName();
346    const G4String& value = iValue->GetValue();
347    map<G4String,G4AttDef>::const_iterator iDef =
348      fpDefinitions->find(valueName);
349    if (iDef == fpDefinitions->end()) {
350      error = true;
351    } else {
352      const G4String& category = iDef->second.GetCategory();
353      const G4String& extra = iDef->second.GetExtra();
354      const G4String& valueType = iDef->second.GetValueType();
355      if (fCategories.find(category) == fCategories.end() ||
356          (category == "Physics" && fUnits.find(extra) == fUnits.end()) ||
357          fValueTypes.find(valueType) == fValueTypes.end()) {
358        error = true;
359      } else {
360        if (category != "Physics") {  // Simply copy...
361          standardValues->push_back(*iValue);
362          (*standardDefinitions)[valueName] =
363            fpDefinitions->find(valueName)->second;
364        } else {  // "Physics"...
365          if (extra.empty()) {  // Dimensionless...
366            if (valueType == "G4ThreeVector") {  // Split vector into 3...
367              G4ThreeVector internalValue =
368                G4UIcommand::ConvertTo3Vector(value);
369              AddValuesAndDefs
370                (standardValues,standardDefinitions,
371                 valueName,valueName+"-X",
372                 G4UIcommand::ConvertToString(internalValue.x()),"",
373                 fpDefinitions->find(valueName)->second.GetDesc()+"-X");
374              AddValuesAndDefs
375                (standardValues,standardDefinitions,
376                 valueName,valueName+"-Y",
377                 G4UIcommand::ConvertToString(internalValue.y()),"",
378                 fpDefinitions->find(valueName)->second.GetDesc()+"-Y");
379              AddValuesAndDefs
380                (standardValues,standardDefinitions,
381                 valueName,valueName+"-Z",
382                 G4UIcommand::ConvertToString(internalValue.z()),"",
383                 fpDefinitions->find(valueName)->second.GetDesc()+"-Z");
384            } else {  // Simply copy...
385              standardValues->push_back(*iValue);
386              (*standardDefinitions)[valueName] =
387                fpDefinitions->find(valueName)->second;
388            }
389          } else {  // Dimensioned...
390            G4String valueAndUnit;
391            G4String unit;
392            if (extra == "G4BestUnit") {
393              valueAndUnit = value;
394              valueAndUnit = valueAndUnit.strip();
395              unit = valueAndUnit.substr(valueAndUnit.rfind(' ')+1);
396            } else {
397              valueAndUnit = value + ' ' + extra;
398              valueAndUnit = valueAndUnit.strip();
399              unit = extra;
400            }
401            G4String unitCategory = G4UnitDefinition::GetCategory(unit);
402            if (fUnitCategories.find(unitCategory) != fUnitCategories.end()) {
403              G4String standardUnit = fStandardUnits[unitCategory];
404              G4double valueOfStandardUnit =
405                G4UnitDefinition::GetValueOf(standardUnit);
406              G4String extra = iDef->second.GetExtra();
407              if (valueType == "G4ThreeVector") {  // Split vector into 3...
408                G4ThreeVector internalValue =
409                  G4UIcommand::ConvertToDimensioned3Vector(valueAndUnit);
410                AddValuesAndDefs
411                  (standardValues,standardDefinitions,
412                   valueName,valueName+"-X",
413                   G4UIcommand::ConvertToString
414                   (internalValue.x()/valueOfStandardUnit),
415                   standardUnit,
416                   fpDefinitions->find(valueName)->second.GetDesc()+"-X");
417                AddValuesAndDefs
418                  (standardValues,standardDefinitions,
419                   valueName,valueName+"-Y",
420                   G4UIcommand::ConvertToString
421                   (internalValue.y()/valueOfStandardUnit),
422                   standardUnit,
423                   fpDefinitions->find(valueName)->second.GetDesc()+"-Y");
424                AddValuesAndDefs
425                  (standardValues,standardDefinitions,
426                   valueName,valueName+"-Z",
427                   G4UIcommand::ConvertToString
428                   (internalValue.z()/valueOfStandardUnit),
429                   standardUnit,
430                   fpDefinitions->find(valueName)->second.GetDesc()+"-Z");
431              } else {
432                G4double internalValue =
433                  G4UIcommand::ConvertToDimensionedDouble(valueAndUnit);
434                AddValuesAndDefs
435                  (standardValues,standardDefinitions,
436                   valueName,valueName,
437                   G4UIcommand::ConvertToString
438                   (internalValue/valueOfStandardUnit),
439                   standardUnit);
440              }
441            }
442          }
443        }
444      }
445    }
446  }
447  if (error) {
448    G4cerr << "G4AttCheck::Standard: Conversion error." << G4endl;
449  }
450  return error;
451}
Note: See TracBrowser for help on using the repository browser.