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