Geant4 10.7.0
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 ()
 
G4doubleGetValue ()
 
const G4StringGetCategory () const
 
std::size_t GetIndexOfCategory () const
 
 operator G4String () const
 

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 510 of file G4UnitsTable.cc.

511 : nbOfVals(1)
512{
513 // find the category
515 std::size_t nbCat = theUnitsTable.size();
516 std::size_t i = 0;
517 while((i < nbCat) && (theUnitsTable[i]->GetName() != category))
518 {
519 ++i;
520 }
521 if(i == nbCat)
522 {
523 G4cout << " G4BestUnit: the category " << category << " does not exist !!"
524 << G4endl;
525 G4Exception("G4BestUnit::G4BestUnit()", "InvalidCall", FatalException,
526 "Missing unit category !");
527 }
528
529 Value[0] = value;
530 Value[1] = 0.;
531 Value[2] = 0.;
532 IndexOfCategory = i;
533}
@ 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
static G4UnitsTable & GetUnitsTable()

◆ G4BestUnit() [2/2]

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

Definition at line 537 of file G4UnitsTable.cc.

538 : nbOfVals(3)
539{
540 // find the category
542 std::size_t nbCat = theUnitsTable.size();
543 std::size_t i = 0;
544 while((i < nbCat) && (theUnitsTable[i]->GetName() != category))
545 {
546 ++i;
547 }
548 if(i == nbCat)
549 {
550 G4cerr << " G4BestUnit: the category " << category << " does not exist."
551 << G4endl;
552 G4Exception("G4BestUnit::G4BestUnit()", "InvalidCall", FatalException,
553 "Missing unit category !");
554 }
555
556 Value[0] = value.x();
557 Value[1] = value.y();
558 Value[2] = value.z();
559 IndexOfCategory = i;
560}
G4GLOB_DLL std::ostream G4cerr

◆ ~G4BestUnit()

G4BestUnit::~G4BestUnit ( )

Definition at line 564 of file G4UnitsTable.cc.

564{}

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 568 of file G4UnitsTable.cc.

569{
570 std::ostringstream oss;
571 oss << *this;
572 return oss.str();
573}

Friends And Related Function Documentation

◆ operator<<

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

Definition at line 577 of file G4UnitsTable.cc.

578{
580 G4UnitsContainer& List = theUnitsTable[a.IndexOfCategory]->GetUnitsList();
581 G4int len = theUnitsTable[a.IndexOfCategory]->GetSymbMxLen();
582
583 G4int ksup(-1), kinf(-1);
584 G4double umax(0.), umin(DBL_MAX);
585 G4double rsup(DBL_MAX), rinf(0.);
586
587 // for a ThreeVector, choose the best unit for the biggest value
588 G4double value =
589 std::max(std::max(std::fabs(a.Value[0]), std::fabs(a.Value[1])),
590 std::fabs(a.Value[2]));
591
592 for(std::size_t k = 0; k < List.size(); ++k)
593 {
594 G4double unit = List[k]->GetValue();
595 if(!(value != DBL_MAX))
596 {
597 if(unit > umax)
598 {
599 umax = unit;
600 ksup = k;
601 }
602 }
603 else if(value <= DBL_MIN)
604 {
605 if(unit < umin)
606 {
607 umin = unit;
608 kinf = k;
609 }
610 }
611 else
612 {
613 G4double ratio = value / unit;
614 if((ratio >= 1.) && (ratio < rsup))
615 {
616 rsup = ratio;
617 ksup = k;
618 }
619 if((ratio < 1.) && (ratio > rinf))
620 {
621 rinf = ratio;
622 kinf = k;
623 }
624 }
625 }
626
627 G4int index = ksup;
628 if(index == -1)
629 {
630 index = kinf;
631 }
632 if(index == -1)
633 {
634 index = 0;
635 }
636
637 for(G4int j = 0; j < a.nbOfVals; ++j)
638 {
639 flux << a.Value[j] / (List[index]->GetValue()) << " ";
640 }
641
642 std::ios::fmtflags oldform = flux.flags();
643
644 flux.setf(std::ios::left, std::ios::adjustfield);
645 flux << std::setw(len) << List[index]->GetSymbol();
646 flux.flags(oldform);
647
648 return flux;
649}
double G4double
Definition: G4Types.hh:83
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: