BLCMDtotalenergy Class Reference

Inheritance diagram for BLCMDtotalenergy:

BLCommand BLCallback BLManager::SteppingAction BLManager::TrackingAction

List of all members.


Detailed Description

class BLCMDtotalenergy prints the total energy deposited into each selected volume, at the end of the run.

Public Member Functions

 BLCMDtotalenergy ()
 Constructor.
virtual G4String commandName ()
 commandName() returns "totalenergy".
virtual int command (BLArgumentVector &argv, BLArgumentMap &namedArgs)
 command() implements the totalenergy command.
virtual void defineNamedArgs ()
 defineNamedArgs() defines the named arguments for this command.
void callback (int type)
 callback() from BLCallback.
void walkPVTree (G4VPhysicalVolume *pv, G4String indent="")
 walk the tree of physical volumes
bool isMatch (G4VPhysicalVolume *pv)
 isMatch() returns true if the name of this PV matches some pattern
void sum (const G4TouchableHandle &th, double energy)
 sum() will sum the energy into the relevant volumes.
void UserSteppingAction (const G4Step *step)
 UserSteppingAction() from BLManager::SteppingAction.
void PreUserTrackingAction (const G4Track *track)
 PreUserTrackingAction() from BLManager::TrackingAction.
void PostUserTrackingAction (const G4Track *track)
 PostUserTrackingAction() from BLManager::TrackingAction.

Private Attributes

G4String volumes
G4int ancestors
G4String filename
bool alreadyRegistered
std::map< G4VPhysicalVolume
*, Totalizer
total
std::vector< G4VPhysicalVolume * > printOrder
std::vector< G4String > patterns
G4VPhysicalVolume * world

Classes

struct  Totalizer

Constructor & Destructor Documentation

BLCMDtotalenergy::BLCMDtotalenergy (  ) 

Constructor.

References alreadyRegistered, ancestors, BLCMDTYPE_DATA, filename, BLCommand::registerCommand(), BLCommand::setDescription(), BLCommand::setSynopsis(), and world.

00083                                    : volumes("*"), total(), printOrder(), 
00084                                                         patterns()
00085 {
00086         registerCommand(BLCMDTYPE_DATA);
00087         setSynopsis("Print total energy deposited in selected volumes.");
00088         setDescription("At end of run, prints the total energy deposited in "
00089                 "the selected volumes.\n\n"
00090                 "Volume-name patterns are like UNIX filename patterns: "
00091                 "e.g. '*[AB]*' matches any name containing an A or a B.\n\n"
00092                 "Tracks that are killed have their kinetic energy summed into "
00093                 "the volume where they were killed, unless they are killed "
00094                 "because they leave the World.\n\n"
00095                 "With ancestors=1, energy deposited in matching volumes is "
00096                 "added into their ancestors; energy deposited directly into "
00097                 "those ancestors is not summed into them unless their names "
00098                 "also match. "
00099                 "That is, if A is a child of B, but only A matches the list "
00100                 "of volume-names, energy "
00101                 "deposited into A will be reported in both A and B, but energy "
00102                 "deposited directly into B is ignored.");
00103 
00104         ancestors = 0;
00105         filename = "";
00106         alreadyRegistered = false;
00107         world = 0; // initialized in callback()
00108 }


Member Function Documentation

virtual G4String BLCMDtotalenergy::commandName (  )  [inline, virtual]

commandName() returns "totalenergy".

Implements BLCommand.

00051 { return "totalenergy"; }

int BLCMDtotalenergy::command ( BLArgumentVector argv,
BLArgumentMap namedArgs 
) [virtual]

command() implements the totalenergy command.

Implements BLCommand.

References alreadyRegistered, BLManager::getObject(), BLCommand::handleNamedArgs(), patterns, BLCommand::print(), BLCommand::printError(), BLManager::registerBeamStep(), BLManager::registerCallback(), BLManager::registerTrackingAction(), BLCommand::splitString(), and volumes.

00111 {
00112         int retval = handleNamedArgs(namedArgs);
00113 
00114         if(alreadyRegistered) {
00115                 printError("Duplicate totalenergy command");
00116                 return -1;
00117         }
00118 
00119         BLManager::getObject()->registerCallback(this,0);
00120         BLManager::getObject()->registerCallback(this,2);
00121         BLManager::getObject()->registerBeamStep(0,this);
00122         BLManager::getObject()->registerTrackingAction(this);
00123         alreadyRegistered = true;
00124 
00125         print("");
00126 
00127         patterns = splitString(volumes, ",", true);
00128 
00129         return retval;
00130 }

void BLCMDtotalenergy::defineNamedArgs (  )  [virtual]

defineNamedArgs() defines the named arguments for this command.

Reimplemented from BLCommand.

References ancestors, BLCommand::argInt(), BLCommand::argString(), filename, and volumes.

00133 {
00134         argString(volumes,"volumes","Comma-separated list of Volume-Name patterns (*)");
00135         argInt(ancestors,"ancestors","Set nonzero to sum energy into all ancestor (enclosing) volumess (0).");
00136         argInt(ancestors,"enclosing","Synonym for ancestors.");
00137         argString(filename,"filename","Filename to write summary (stdout).");
00138         argString(filename,"file","Synonym for filename.");
00139 }

void BLCMDtotalenergy::callback ( int  type  )  [virtual]

callback() from BLCallback.

Reimplemented from BLCallback.

References BLAsciiFile::fclose(), filename, BLAsciiFile::fopen(), BLManager::getObject(), BLManager::getWorldPhysicalVolume(), BLCommand::printError(), printOrder, total, walkPVTree(), and world.

00142 {
00143         if(type == 0) { 
00144                 // Walk the tree of physical volumes
00145                 printf("\nPhysical volumes for totalenergy (* = totaled):\n");
00146                 world = BLManager::getObject()->getWorldPhysicalVolume();
00147                 walkPVTree(world);
00148         } else if(type == 2) {
00149                 FILE *out=stdout;
00150                 if(filename.size() > 0) {
00151                         out = BLAsciiFile::fopen(filename);
00152                         if(!out) {
00153                                 printError("Cannot write to '%s' - stdout used\n",
00154                                         filename.c_str());
00155                                 out = stdout;
00156                         }
00157                 }
00158                 fprintf(out,"# Total energy deposited in selected volumes (MeV):\n");
00159                 for(unsigned i=0; i<printOrder.size(); ++i) {
00160                         G4VPhysicalVolume *pv = printOrder[i];
00161                         double t=total[pv].total/MeV;
00162                         if(t < 10.0)
00163                             fprintf(out,"%s\t%.6f\n",pv->GetName().c_str(),t);
00164                         else if(t < 10000.0)
00165                             fprintf(out,"%s\t%.3f\n",pv->GetName().c_str(),t);
00166                         else if(t < 100000.0)
00167                             fprintf(out,"%s\t%.1f\n",pv->GetName().c_str(),t);
00168                         else
00169                             fprintf(out,"%s\t%.4g\n",pv->GetName().c_str(),t);
00170                 }
00171                 if(out != stdout) BLAsciiFile::fclose(out);
00172         }
00173 }

void BLCMDtotalenergy::walkPVTree ( G4VPhysicalVolume *  pv,
G4String  indent = "" 
)

walk the tree of physical volumes

References isMatch(), printOrder, and total.

Referenced by callback().

00176 {
00177         printf("%s%s",indent.c_str(),pv->GetName().c_str());
00178         if(isMatch(pv)) {
00179                 printf(" *");
00180                 if(total.count(pv) != 0)
00181                         G4Exception("totalenergy command",
00182                                 "Duplicate PhysicalVolume cut",JustWarning, "");
00183                 total[pv] = Totalizer();
00184                 printOrder.push_back(pv);
00185         }
00186         printf("\n");
00187 
00188         G4LogicalVolume *lv = pv->GetLogicalVolume();
00189         int n=lv->GetNoDaughters();
00190         for(int i=0; i<n; ++i)
00191                 walkPVTree(lv->GetDaughter(i),indent+"    ");
00192 }

bool BLCMDtotalenergy::isMatch ( G4VPhysicalVolume *  pv  ) 

isMatch() returns true if the name of this PV matches some pattern

References fnmatch(), and patterns.

Referenced by walkPVTree().

00195 {
00196         const char *name = pv->GetName().c_str();
00197         for(unsigned i=0; i<patterns.size(); ++i) {
00198                 if(patterns[i].size() == 0) continue;
00199                 if(fnmatch(patterns[i].c_str(),name,0) == 0)
00200                         return true;
00201         }
00202         return false;
00203 }

void BLCMDtotalenergy::sum ( const G4TouchableHandle &  th,
double  energy 
)

sum() will sum the energy into the relevant volumes.

References ancestors, total, and world.

Referenced by PostUserTrackingAction(), and UserSteppingAction().

00206 {
00207         if(!th) return;
00208 
00209         for(int depth=0; depth<=th->GetHistoryDepth(); ++depth) {
00210                 G4VPhysicalVolume *pv = th->GetVolume(depth);
00211                 if(!pv) break;
00212                 if(depth == 0 && total.count(pv) == 0) break;
00213                 total[pv].total += energy; // create if needed
00214                 if(pv == world) break;
00215                 if(!ancestors) break;
00216         }
00217 }

void BLCMDtotalenergy::UserSteppingAction ( const G4Step *  step  )  [virtual]

UserSteppingAction() from BLManager::SteppingAction.

Implements BLManager::SteppingAction.

References sum().

00220 {
00221         G4StepPoint *prePoint = step->GetPreStepPoint();
00222         if(!prePoint) return;
00223         G4double energy = step->GetTotalEnergyDeposit();
00224         sum(prePoint->GetTouchableHandle(),energy);
00225 }

void BLCMDtotalenergy::PreUserTrackingAction ( const G4Track *  track  )  [virtual]

void BLCMDtotalenergy::PostUserTrackingAction ( const G4Track *  track  )  [virtual]

PostUserTrackingAction() from BLManager::TrackingAction.

Implements BLManager::TrackingAction.

References sum().

00232 {
00233         if(track->GetTrackStatus() == fSuspend) return;
00234         if(track->GetNextVolume() == 0) return; // don't sum if leaving World
00235         sum(track->GetTouchableHandle(),track->GetKineticEnergy());
00236 }


Member Data Documentation

G4String BLCMDtotalenergy::volumes [private]

Referenced by command(), and defineNamedArgs().

G4int BLCMDtotalenergy::ancestors [private]

G4String BLCMDtotalenergy::filename [private]

Referenced by BLCMDtotalenergy(), and command().

std::map<G4VPhysicalVolume*,Totalizer> BLCMDtotalenergy::total [private]

Referenced by callback(), sum(), and walkPVTree().

std::vector<G4VPhysicalVolume*> BLCMDtotalenergy::printOrder [private]

Referenced by callback(), and walkPVTree().

std::vector<G4String> BLCMDtotalenergy::patterns [private]

Referenced by command(), and isMatch().

G4VPhysicalVolume* BLCMDtotalenergy::world [private]

Referenced by BLCMDtotalenergy(), callback(), and sum().


The documentation for this class was generated from the following file:
g4beamline