Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4BestUnit Class Reference

#include <G4UnitsTable.hh>

Public Member Functions

 G4BestUnit (G4double internalValue, const G4String &category)
 
 G4BestUnit (const G4ThreeVector &internalValue, const G4String &category)
 
 ~G4BestUnit ()=default
 
G4doubleGetValue ()
 
const G4StringGetCategory () const
 
std::size_t GetIndexOfCategory () const
 
 operator G4String () const
 

Friends

std::ostream & operator<< (std::ostream &, const G4BestUnit &VU)
 

Detailed Description

Definition at line 158 of file G4UnitsTable.hh.

Constructor & Destructor Documentation

◆ G4BestUnit() [1/2]

G4BestUnit::G4BestUnit ( G4double  internalValue,
const G4String category 
)

Definition at line 523 of file G4UnitsTable.cc.

524 : nbOfVals(1)
525{
526 // find the category
528 std::size_t nbCat = theUnitsTable.size();
529 std::size_t i = 0;
530 while((i < nbCat) && (theUnitsTable[i]->GetName() != category))
531 {
532 ++i;
533 }
534 if(i == nbCat)
535 {
536 G4cout << " G4BestUnit: the category " << category << " does not exist !!"
537 << G4endl;
538 G4Exception("G4BestUnit::G4BestUnit()", "InvalidCall", FatalException,
539 "Missing unit category !");
540 }
541
542 Value[0] = value;
543 Value[1] = 0.;
544 Value[2] = 0.;
545 Category = category;
546 IndexOfCategory = i;
547}
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:59
std::vector< G4UnitsCategory * > G4UnitsTable
Definition: G4UnitsTable.hh:68
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
static G4UnitsTable & GetUnitsTable()

◆ G4BestUnit() [2/2]

G4BestUnit::G4BestUnit ( const G4ThreeVector internalValue,
const G4String category 
)

Definition at line 551 of file G4UnitsTable.cc.

552 : nbOfVals(3)
553{
554 // find the category
556 std::size_t nbCat = theUnitsTable.size();
557 std::size_t i = 0;
558 while((i < nbCat) && (theUnitsTable[i]->GetName() != category))
559 {
560 ++i;
561 }
562 if(i == nbCat)
563 {
564 G4cerr << " G4BestUnit: the category " << category << " does not exist."
565 << G4endl;
566 G4Exception("G4BestUnit::G4BestUnit()", "InvalidCall", FatalException,
567 "Missing unit category !");
568 }
569
570 Value[0] = value.x();
571 Value[1] = value.y();
572 Value[2] = value.z();
573 Category = category;
574 IndexOfCategory = i;
575}
G4GLOB_DLL std::ostream G4cerr

◆ ~G4BestUnit()

G4BestUnit::~G4BestUnit ( )
default

Member Function Documentation

◆ GetCategory()

const G4String & G4BestUnit::GetCategory ( ) const
inline

◆ GetIndexOfCategory()

std::size_t G4BestUnit::GetIndexOfCategory ( ) const
inline

◆ GetValue()

G4double * G4BestUnit::GetValue ( )
inline

◆ operator G4String()

G4BestUnit::operator G4String ( ) const

Definition at line 579 of file G4UnitsTable.cc.

580{
581 std::ostringstream oss;
582 oss << *this;
583 return oss.str();
584}

Friends And Related Function Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  flux,
const G4BestUnit VU 
)
friend

Definition at line 588 of file G4UnitsTable.cc.

589{
591 G4UnitsContainer& List = theUnitsTable[a.IndexOfCategory]->GetUnitsList();
592 G4int len = theUnitsTable[a.IndexOfCategory]->GetSymbMxLen();
593
594 G4long ksup(-1), kinf(-1);
595 G4double umax(0.), umin(DBL_MAX);
596 G4double rsup(DBL_MAX), rinf(0.);
597
598 // for a ThreeVector, choose the best unit for the biggest value
599 G4double value =
600 std::max(std::max(std::fabs(a.Value[0]), std::fabs(a.Value[1])),
601 std::fabs(a.Value[2]));
602
603 //special treatement for Energy.
604 if ((a.Category == "Energy") && (value == 0.)) {
605 for (G4int j = 0; j < a.nbOfVals; ++j) {
606 flux << a.Value[j] << " ";
607 }
608 std::ios::fmtflags oldform = flux.flags();
609 flux.setf(std::ios::left, std::ios::adjustfield);
610 flux << std::setw(len) << "eV";
611 flux.flags(oldform);
612 return flux;
613 }
614
615 //here, value != 0.
616 for(std::size_t k = 0; k < List.size(); ++k)
617 {
618 G4double unit = List[k]->GetValue();
619 if(!(value != DBL_MAX))
620 {
621 if(unit > umax)
622 {
623 umax = unit;
624 ksup = k;
625 }
626 }
627 else if(value <= DBL_MIN)
628 {
629 if(unit < umin)
630 {
631 umin = unit;
632 kinf = k;
633 }
634 }
635 else
636 {
637 G4double ratio = value / unit;
638 if((ratio >= 1.) && (ratio < rsup))
639 {
640 rsup = ratio;
641 ksup = k;
642 }
643 if((ratio < 1.) && (ratio > rinf))
644 {
645 rinf = ratio;
646 kinf = k;
647 }
648 }
649 }
650
651 G4long index = ksup;
652 if(index == -1)
653 {
654 index = kinf;
655 }
656 if(index == -1)
657 {
658 index = 0;
659 }
660
661 for(G4int j = 0; j < a.nbOfVals; ++j)
662 {
663 flux << a.Value[j] / (List[index]->GetValue()) << " ";
664 }
665
666 std::ios::fmtflags oldform = flux.flags();
667
668 flux.setf(std::ios::left, std::ios::adjustfield);
669 flux << std::setw(len) << List[index]->GetSymbol();
670 flux.flags(oldform);
671
672 return flux;
673}
double G4double
Definition: G4Types.hh:83
long G4long
Definition: G4Types.hh:87
int G4int
Definition: G4Types.hh:85
std::vector< G4UnitDefinition * > G4UnitsContainer
#define DBL_MIN
Definition: templates.hh:54
#define DBL_MAX
Definition: templates.hh:62

The documentation for this class was generated from the following files: