Geant4 VMC Version 6.6
Loading...
Searching...
No Matches
TG4ParticlesChecker.cxx
Go to the documentation of this file.
1//------------------------------------------------
2// The Geant4 Virtual Monte Carlo package
3// Copyright (C) 2007 - 2014 Ivana Hrivnacova
4// All rights reserved.
5//
6// For the licensing terms see geant4_vmc/LICENSE.
7// Contact: root-vmc@cern.ch
8//-------------------------------------------------
9
14
15#include "TG4ParticlesChecker.h"
16#include "TG4Globals.h"
17
18#include <G4ParticleDefinition.hh>
19#include <G4ParticleTable.hh>
20
21#include <TClonesArray.h>
22#include <TDatabasePDG.h>
23#include <THashList.h>
24#include <TParticle.h>
25
26// Moved after Root includes to avoid shadowed variables
27// generated from short units names
28#include <G4SystemOfUnits.hh>
29
30#include <iomanip>
31
32const G4double TG4ParticlesChecker::fgkDefaultPrecision = 1.e-06;
33
34//
35// static methods
36//
37
38//_____________________________________________________________________________
40{
42
43 switch (property) {
44 case kName:
45 return "name";
46 case kMass:
47 return "mass";
48 case kCharge:
49 return "charge";
50 case kLifetime:
51 return "lifetime";
52 case kWidth:
53 return "width";
54 case kParity:
55 return "parity";
56 case kSpin:
57 return "spin";
58 case kIsospin:
59 return "isospin";
60 case kIsospin3:
61 return "isospin3";
62 case kNone:
63 default:
64 return "";
65 }
66}
67
68//_____________________________________________________________________________
70 const G4String& propertyName)
71{
73
74 if (propertyName == GetParticlePropertyName(kMass))
75 return kMass;
76 else if (propertyName == GetParticlePropertyName(kName))
77 return kName;
78 else if (propertyName == GetParticlePropertyName(kCharge))
79 return kCharge;
80 else if (propertyName == GetParticlePropertyName(kLifetime))
81 return kLifetime;
82 else if (propertyName == GetParticlePropertyName(kWidth))
83 return kWidth;
84 else if (propertyName == GetParticlePropertyName(kParity))
85 return kParity;
86 else if (propertyName == GetParticlePropertyName(kSpin))
87 return kSpin;
88 else if (propertyName == GetParticlePropertyName(kIsospin))
89 return kIsospin;
90 else if (propertyName == GetParticlePropertyName(kIsospin3))
91 return kIsospin3;
92 else
93 return kNone;
94}
95
96//
97// ctor, dtor
98//
99
100//_____________________________________________________________________________
102 : TG4Verbose("particlesChecker"),
103 fMessenger(this),
104 fAvailableProperties(),
105 fCheckedProperties(),
106 fPrecision(fgkDefaultPrecision)
107{
109
119
125
127}
128
129//_____________________________________________________________________________
134
135//
136// private methods
137//
138
139//_____________________________________________________________________________
141 G4double dx, G4double dy, G4double epsilon) const
142{
146
147 return fabs(dx - dy) <= epsilon * fabs(dx);
148}
149
150//_____________________________________________________________________________
152 ParticleProperty property, G4ParticleDefinition* g4Particle) const
153{
155
156 switch (property) {
157 case kMass:
158 return g4Particle->GetPDGMass() / GeV;
159 case kCharge:
160 return g4Particle->GetPDGCharge();
161 case kLifetime:
162 return g4Particle->GetPDGLifeTime() / second;
163 case kWidth:
164 return g4Particle->GetPDGWidth() / GeV;
165 case kParity:
166 return g4Particle->GetPDGiParity();
167 case kSpin:
168 return g4Particle->GetPDGSpin();
169 case kIsospin:
170 return g4Particle->GetPDGIsospin();
171 case kIsospin3:
172 return g4Particle->GetPDGIsospin3();
173 case kNone:
174 default:
175 return 0.;
176 }
177}
178
179//_____________________________________________________________________________
181 ParticleProperty property, TParticlePDG* rtParticle) const
182{
184
185 switch (property) {
186 case kMass:
187 return rtParticle->Mass();
188 case kCharge:
189 return rtParticle->Charge() / 3.;
190 case kLifetime:
191 return rtParticle->Lifetime();
192 case kWidth:
193 return rtParticle->Width();
194 case kParity:
195 return rtParticle->Parity();
196 case kSpin:
197 return rtParticle->Spin();
198 case kIsospin:
199 return rtParticle->Isospin();
200 case kIsospin3:
201 return rtParticle->I3();
202 case kNone:
203 default:
204 return 0.;
205 }
206}
207
208//_____________________________________________________________________________
210{
213
214 std::set<ParticleProperty>::const_iterator it;
215 G4cout << "Checking properties: " << G4endl;
216 for (it = fCheckedProperties.begin(); it != fCheckedProperties.end(); it++) {
217 G4cout << GetParticlePropertyName(*it) << " ";
218 }
219
220 if (!(fCheckedProperties.size() == 1 &&
222 G4cout << " ... within precision " << fPrecision;
223 }
224 G4cout << G4endl << G4endl;
225}
226
227//_____________________________________________________________________________
229 G4ParticleDefinition* g4Particle, TParticlePDG* rtParticle) const
230{
234
235 G4String propertyName = GetParticlePropertyName(kName);
236 G4String g4Value = g4Particle->GetParticleName();
237 G4String rtValue = rtParticle->GetName();
238
239 G4bool printPdg = fCheckedProperties.size() == 1 &&
241
242 std::set<G4String> candidates;
243 std::string rtValueStd = rtValue;
244 char firstChar = rtValueStd[0];
245 // explicitly declared std::string needed for compiling
246 // on Debian Lenny (reported by Mohhamad)
247 if (firstChar >= 'A' && firstChar <= 'Z') {
248 firstChar = tolower(firstChar); // Xx -> xx
249 }
250
251 candidates.insert(rtValue);
252 std::string::size_type pos = rtValue.find("_bar");
253 if (pos == std::string::npos) {
254 // particles wo _bar extension
255 G4String candidate = rtValue;
256 candidate.replace(0, 1, std::string(1, firstChar));
257 candidates.insert(candidate);
258 }
259 else {
260 // particles with _bar extension
261 G4String candidate = rtValue;
262 candidate.erase(pos, 4);
263 candidate.insert(0, "anti");
264 candidates.insert(candidate); // Xx_bar -> antiXx
265
266 candidate.insert(4, "_");
267 candidates.insert(candidate); // Xx_bar -> anti_Xx
268
269 candidate = rtValue;
270 candidate.replace(0, 1, std::string(1, firstChar));
271 candidate.erase(pos, 4);
272 candidate.insert(0, "anti");
273 candidates.insert(candidate); // Xx_bar -> antixx
274
275 candidate.insert(4, "_");
276 candidates.insert(candidate); // Xx_bar -> anti_xx
277 }
278
279 G4bool match = false;
280 std::set<G4String>::iterator it;
281 for (it = candidates.begin(); it != candidates.end(); it++) {
282 if (VerboseLevel() > 2) {
283 if (it == candidates.begin()) G4cout << " ... comparing ";
284 G4cout << *it << " ";
285 }
286 if (g4Value == *it) {
287 match = true;
288 break;
289 }
290 }
291 if (VerboseLevel() > 2) {
292 G4cout << G4endl;
293 }
294
295 if (!match) {
296 if (!printPdg) {
297 // printing with other properties
298 G4cout << " " << std::setw(10) << propertyName << std::setw(8)
299 << " Root: " << std::setw(12) << rtValue << std::setw(6)
300 << " G4: " << std::setw(12) << g4Value;
301 }
302 else {
303 // printing only names, release format and add PDG code
304 G4cout << " " << std::setw(10) << propertyName << std::setw(8)
305 << " Root: " << std::setw(12) << rtValue << std::setw(6)
306 << " G4: " << std::setw(20) << g4Value << std::setw(6)
307 << " pdg: " << std::setw(12) << g4Particle->GetPDGEncoding();
308 }
309 G4cout << G4endl;
310 return false;
311 }
312 else {
313 if (VerboseLevel() > 2) {
314 G4cout << " " << propertyName << " equivalent: " << rtValue << " "
315 << g4Value;
316 if (printPdg) {
317 G4cout << std::setw(6) << " pdg: " << std::setw(10)
318 << g4Particle->GetPDGEncoding() << G4endl;
319 }
320 G4cout << G4endl;
321 }
322 return true;
323 }
324}
325
326//_____________________________________________________________________________
328 G4ParticleDefinition* g4Particle, TParticlePDG* rtParticle) const
329{
333
334 if (property == kName) return CheckName(g4Particle, rtParticle);
335
336 G4String propertyName = GetParticlePropertyName(property);
337 G4double g4Value = GetPropertyValue(property, g4Particle);
338 G4double rtValue = GetPropertyValue(property, rtParticle);
339
340 if (!IsEqualRel(g4Value, rtValue, fPrecision) &&
341 !(property == kLifetime && g4Value < 0 && rtValue == 0)) {
342 G4cout << " " << std::setw(10) << propertyName << std::setw(8)
343 << " Root: " << std::setw(12) << rtValue << std::setw(6)
344 << " G4: " << std::setw(12) << g4Value;
345 if (g4Value != 0) {
346 G4cout << std::setw(6) << " eps: " << std::setw(12)
347 << fabs(g4Value - rtValue) / g4Value;
348 }
349 G4cout << G4endl;
350 return false;
351 }
352 else {
353 if (VerboseLevel() > 2)
354 G4cout << " " << propertyName << " equal" << G4endl;
355 return true;
356 }
357}
358
359//_____________________________________________________________________________
361 G4ParticleDefinition* g4Particle, TParticlePDG* rtParticle) const
362{
364
365 G4bool resultAll = true;
366 std::set<ParticleProperty>::const_iterator it;
367 for (it = fCheckedProperties.begin(); it != fCheckedProperties.end(); it++) {
368 G4bool result = CheckProperty(*it, g4Particle, rtParticle);
369 resultAll = resultAll && result;
370 }
371
372 if (resultAll) {
373 if (fCheckedProperties.size() > 1)
374 G4cout << " all properties match" << G4endl;
375 }
376
377 return resultAll;
378}
379
380//
381// public methods
382//
383
384//_____________________________________________________________________________
386{
390
391 G4bool resultAll = true;
392
393 G4ParticleTable* g4ParticleTable = G4ParticleTable::GetParticleTable();
394
395 const THashList* rootParticles = TDatabasePDG::Instance()->ParticleList();
396
398
399 G4bool printBanner =
400 !(fCheckedProperties.size() == 1 &&
402
403 std::set<G4String> names;
404 std::set<G4String> duplicateNames;
405
406 TIter next(rootParticles);
407 TObject* obj;
408 while ((obj = next())) {
409 TParticlePDG* rootParticle = (TParticlePDG*)obj;
410 G4int pdgCode = rootParticle->PdgCode();
411 G4ParticleDefinition* g4Particle = g4ParticleTable->FindParticle(pdgCode);
412
413 if (g4Particle && printBanner) {
414 // print particle info
415 G4cout << "=============================================================="
416 "======="
417 << G4endl << "Particle: " << std::setw(16)
418 << rootParticle->GetName() << " pdg: " << std::setw(10)
419 << pdgCode << G4endl
420 << "=============================================================="
421 "======="
422 << G4endl;
423 }
424
425 // check if name is unique in Root database
426 G4String name = rootParticle->GetName();
427 if (names.find(name) != names.end()) {
428 G4cout << "!!! duplicate name: " << name << G4endl;
429 duplicateNames.insert(name);
430 }
431 names.insert(name);
432
433 if (!g4Particle) {
434 if (VerboseLevel() > 1)
435 G4cout << " no G4 particle equivalent to Root: "
436 << rootParticle->GetName() << " pdg = " << pdgCode << G4endl
437 << G4endl;
438 continue;
439 }
440
441 G4bool result = CheckParticle(g4Particle, rootParticle);
442 resultAll = resultAll && result;
443
444 if (printBanner) {
445 G4cout << G4endl;
446 }
447 }
448
449 if (duplicateNames.size() > 0) {
450 G4cout << "!!! Found duplicate particle names: ";
451 std::set<G4String>::iterator it;
452 for (it = duplicateNames.begin(); it != duplicateNames.end(); it++) {
453 G4cout << *it << " ";
454 }
455 G4cout << G4endl;
456 }
457
458 return resultAll;
459}
460
461//_____________________________________________________________________________
462G4bool TG4ParticlesChecker::CheckParticle(G4int pdgEncoding) const
463{
466
467 TParticlePDG* rootParticle =
468 TDatabasePDG::Instance()->GetParticle(pdgEncoding);
469 if (!rootParticle) {
470 TString text = "Particle with PDG encoding ";
471 text += pdgEncoding;
472 text += " not found in TDatabasePDG.";
473 TG4Globals::Warning("TG4ParticlesChecker", "CheckParticle", text);
474 return false;
475 }
476
477 G4ParticleDefinition* g4Particle =
478 G4ParticleTable::GetParticleTable()->FindParticle(pdgEncoding);
479 if (!g4Particle) {
480 TString text = "Particle with PDG encoding ";
481 text += pdgEncoding;
482 text += " not found in G4ParticleTable.";
483 TG4Globals::Warning("TG4ParticlesChecker", "CheckParticle", text);
484 return false;
485 }
486
487 G4int pdgCode = rootParticle->PdgCode();
488 G4cout << "Particle: " << std::setw(16) << rootParticle->GetName()
489 << " pdg: " << std::setw(10) << pdgCode << G4endl << " ";
490
492
493 return CheckParticle(g4Particle, rootParticle);
494}
495
496//_____________________________________________________________________________
498{
500
501 std::set<ParticleProperty>::const_iterator it =
502 fCheckedProperties.find(property);
503
504 if ((check && it != fCheckedProperties.end()) ||
505 (!check && it == fCheckedProperties.end())) {
506 // no change of checking property
507 return;
508 }
509
510 if (check) {
511 fCheckedProperties.insert(property);
512 }
513 else {
514 fCheckedProperties.erase(it);
515 }
516}
Definition of the TG4Globals class and basic container types.
Definition of the TG4ParticlesChecker class.
static void Warning(const TString &className, const TString &methodName, const TString &text)
G4double fPrecision
precision for checking
G4bool CheckParticle(G4int pdgEncoding) const
ParticleProperty
The enumeration of "checkable" particle properties.
void SetChecking(ParticleProperty property, G4bool check)
std::set< ParticleProperty > fCheckedProperties
set of properties selected for checking
static G4String GetParticlePropertyName(ParticleProperty property)
static ParticleProperty GetParticleProperty(const G4String &propertyName)
static const G4double fgkDefaultPrecision
default precision
TG4ParticlesCheckerMessenger fMessenger
messenger for this class
G4bool CheckName(G4ParticleDefinition *g4Particle, TParticlePDG *rtParticle) const
G4bool IsEqualRel(G4double dx, G4double dy, G4double epsilon) const
std::set< ParticleProperty > fAvailableProperties
set of available properties
G4double GetPropertyValue(ParticleProperty property, G4ParticleDefinition *g4Particle) const
G4bool CheckProperty(ParticleProperty property, G4ParticleDefinition *g4Particle, TParticlePDG *rtParticle) const
Base class for defining the verbose level and a common messenger.
Definition TG4Verbose.h:36
virtual G4int VerboseLevel() const
Definition TG4Verbose.h:78