Geant4-11
Public Member Functions | Private Attributes | Friends
G4BestUnit Class Reference

#include <G4UnitsTable.hh>

Public Member Functions

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

Private Attributes

G4String Category
 
std::size_t IndexOfCategory = 0
 
G4int nbOfVals = 0
 
G4double Value [3]
 

Friends

std::ostream & operator<< (std::ostream &, 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 532 of file G4UnitsTable.cc.

533 : nbOfVals(1)
534{
535 // find the category
537 std::size_t nbCat = theUnitsTable.size();
538 std::size_t i = 0;
539 while((i < nbCat) && (theUnitsTable[i]->GetName() != category))
540 {
541 ++i;
542 }
543 if(i == nbCat)
544 {
545 G4cout << " G4BestUnit: the category " << category << " does not exist !!"
546 << G4endl;
547 G4Exception("G4BestUnit::G4BestUnit()", "InvalidCall", FatalException,
548 "Missing unit category !");
549 }
550
551 Value[0] = value;
552 Value[1] = 0.;
553 Value[2] = 0.;
554 Category = category;
555 IndexOfCategory = i;
556}
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
std::vector< G4UnitsCategory * > G4UnitsTable
Definition: G4UnitsTable.hh:68
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
G4String Category
std::size_t IndexOfCategory
G4double Value[3]
G4int nbOfVals
static G4UnitsTable & GetUnitsTable()

References Category, FatalException, G4cout, G4endl, G4Exception(), G4UnitDefinition::GetUnitsTable(), IndexOfCategory, and Value.

◆ G4BestUnit() [2/2]

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

Definition at line 560 of file G4UnitsTable.cc.

561 : nbOfVals(3)
562{
563 // find the category
565 std::size_t nbCat = theUnitsTable.size();
566 std::size_t i = 0;
567 while((i < nbCat) && (theUnitsTable[i]->GetName() != category))
568 {
569 ++i;
570 }
571 if(i == nbCat)
572 {
573 G4cerr << " G4BestUnit: the category " << category << " does not exist."
574 << G4endl;
575 G4Exception("G4BestUnit::G4BestUnit()", "InvalidCall", FatalException,
576 "Missing unit category !");
577 }
578
579 Value[0] = value.x();
580 Value[1] = value.y();
581 Value[2] = value.z();
582 Category = category;
583 IndexOfCategory = i;
584}
G4GLOB_DLL std::ostream G4cerr

References Category, FatalException, G4cerr, G4endl, G4Exception(), G4UnitDefinition::GetUnitsTable(), IndexOfCategory, Value, CLHEP::Hep3Vector::x(), CLHEP::Hep3Vector::y(), and CLHEP::Hep3Vector::z().

◆ ~G4BestUnit()

G4BestUnit::~G4BestUnit ( )

Definition at line 588 of file G4UnitsTable.cc.

588{}

Member Function Documentation

◆ GetCategory()

const G4String & G4BestUnit::GetCategory ( ) const
inline

Referenced by export_G4UnitsTable().

◆ GetIndexOfCategory()

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

Referenced by export_G4UnitsTable().

◆ GetValue()

G4double * G4BestUnit::GetValue ( )
inline

◆ operator G4String()

G4BestUnit::operator G4String ( ) const

Definition at line 592 of file G4UnitsTable.cc.

593{
594 std::ostringstream oss;
595 oss << *this;
596 return oss.str();
597}

Friends And Related Function Documentation

◆ operator<<

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

Definition at line 601 of file G4UnitsTable.cc.

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

Field Documentation

◆ Category

G4String G4BestUnit::Category
private

Definition at line 180 of file G4UnitsTable.hh.

Referenced by G4BestUnit().

◆ IndexOfCategory

std::size_t G4BestUnit::IndexOfCategory = 0
private

Definition at line 181 of file G4UnitsTable.hh.

Referenced by G4BestUnit().

◆ nbOfVals

G4int G4BestUnit::nbOfVals = 0
private

Definition at line 179 of file G4UnitsTable.hh.

◆ Value

G4double G4BestUnit::Value[3]
private

Definition at line 178 of file G4UnitsTable.hh.

Referenced by G4BestUnit().


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