BLCMDtotalenergy Class Reference

Inheritance diagram for BLCMDtotalenergy:

BLCommand BLCallback BLManager::SteppingAction

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 UserSteppingAction (const G4Step *step)
 UserSteppingAction() from BLManager::SteppingAction.

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.

00072                                    : volumes("*"), total(), printOrder(), 
00073                                                         patterns()
00074 {
00075         registerCommand(BLCMDTYPE_DATA);
00076         setSynopsis("Print total energy deposited in selected volumes.");
00077         setDescription("At end of run, prints the total energy deposited in "
00078                 "the selected volumes.\n\n"
00079                 "Volume-name patterns are like UNIX filename patterns: "
00080                 "e.g. '*[AB]*' matches any name containing an A or a B.");
00081 
00082         ancestors = 0;
00083         filename = "";
00084         alreadyRegistered = false;
00085         world = 0; // initialized in callback()
00086 }


Member Function Documentation

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

commandName() returns "totalenergy".

Implements BLCommand.

00049 { 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(), BLCommand::splitString(), and volumes.

00089 {
00090         int retval = handleNamedArgs(namedArgs);
00091 
00092         if(alreadyRegistered) {
00093                 printError("Duplicate totalenergy command");
00094                 return -1;
00095         }
00096 
00097         BLManager::getObject()->registerCallback(this,0);
00098         BLManager::getObject()->registerCallback(this,2);
00099         BLManager::getObject()->registerBeamStep(0,this);
00100         alreadyRegistered = true;
00101 
00102         print("");
00103 
00104         patterns = splitString(volumes, ",", true);
00105 
00106         return retval;
00107 }

void BLCMDtotalenergy::defineNamedArgs (  )  [virtual]

defineNamedArgs() defines the named arguments for this command.

Reimplemented from BLCommand.

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

00110 {
00111         argString(volumes,"volumes","Comma-separated list of Volume-Name patterns (*)");
00112         argInt(ancestors,"ancestors","Set nonzero to sum energy into all ancestor (enclosing) volumess (0).");
00113         argInt(ancestors,"enclosing","Synonym for ancestors.");
00114         argString(filename,"filename","Filename to write summary (stdout).");
00115         argString(filename,"file","Synonym for filename.");
00116 }

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

callback() from BLCallback.

Reimplemented from BLCallback.

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

00119 {
00120         if(type == 0) { 
00121                 // Walk the tree of physical volumes
00122                 printf("\nPhysical volumes for totalenergy (* = totaled):\n");
00123                 world = BLManager::getObject()->getWorldPhysicalVolume();
00124                 walkPVTree(world);
00125         } else if(type == 2) {
00126                 FILE *out=stdout;
00127                 if(filename.size() > 0) {
00128                         out = fopen(filename.c_str(),"w");
00129                         if(!out) {
00130                                 printError("Cannot write to '%s' - stdout used\n",
00131                                         filename.c_str());
00132                                 out = stdout;
00133                         }
00134                 }
00135                 fprintf(out,"# Total energy deposited in selected volumes (MeV):\n");
00136                 for(unsigned i=0; i<printOrder.size(); ++i) {
00137                         G4VPhysicalVolume *pv = printOrder[i];
00138                         double t=total[pv].total/MeV;
00139                         if(t < 10.0)
00140                             fprintf(out,"%s\t%.6f\n",pv->GetName().c_str(),t);
00141                         else if(t < 10000.0)
00142                             fprintf(out,"%s\t%.3f\n",pv->GetName().c_str(),t);
00143                         else if(t < 100000.0)
00144                             fprintf(out,"%s\t%.1f\n",pv->GetName().c_str(),t);
00145                         else
00146                             fprintf(out,"%s\t%.4g\n",pv->GetName().c_str(),t);
00147                 }
00148                 if(out != stdout) fclose(out);
00149         }
00150 }

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

walk the tree of physical volumes

References isMatch(), printOrder, and total.

Referenced by callback().

00153 {
00154         printf("%s%s",indent.c_str(),pv->GetName().c_str());
00155         if(isMatch(pv)) {
00156                 printf(" *");
00157                 if(total.count(pv) != 0)
00158                         printf(" ***WARNING: Duplicate PhysicalVolume!");
00159                 total[pv] = Totalizer();
00160                 printOrder.push_back(pv);
00161         }
00162         printf("\n");
00163 
00164         G4LogicalVolume *lv = pv->GetLogicalVolume();
00165         int n=lv->GetNoDaughters();
00166         for(int i=0; i<n; ++i)
00167                 walkPVTree(lv->GetDaughter(i),indent+"    ");
00168 }

bool BLCMDtotalenergy::isMatch ( G4VPhysicalVolume *  pv  ) 

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

References fnmatch(), and patterns.

Referenced by walkPVTree().

00171 {
00172         const char *name = pv->GetName().c_str();
00173         for(unsigned i=0; i<patterns.size(); ++i) {
00174                 if(patterns[i].size() == 0) continue;
00175                 if(fnmatch(patterns[i].c_str(),name,0) == 0)
00176                         return true;
00177         }
00178         return false;
00179 }

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

UserSteppingAction() from BLManager::SteppingAction.

Implements BLManager::SteppingAction.

References ancestors, total, and world.

00182 {
00183         G4StepPoint *prePoint = step->GetPreStepPoint();
00184         G4VPhysicalVolume *preVol=0;
00185         if(prePoint) preVol = prePoint->GetPhysicalVolume();
00186         if(!preVol) return;
00187 
00188         G4double energy = step->GetTotalEnergyDeposit();
00189         if(total.count(preVol) > 0) {
00190                 total[preVol].total += energy; 
00191                 if(ancestors && preVol != world) {
00192                         G4TouchableHandle th = prePoint->GetTouchableHandle();
00193                         for(int depth=1; true; ++depth) {
00194                                 G4VPhysicalVolume *pv = th->GetVolume(depth);
00195                                 if(!pv) break;
00196                                 total[pv].total += energy; // create if needed
00197                                 if(pv == world) break;
00198                         }
00199                 }
00200         }
00201 }


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]

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]


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