G4UImanager.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 //
00030 // ---------------------------------------------------------------------
00031 
00032 #include "G4UImanager.hh"
00033 #include "G4UIcommandTree.hh"
00034 #include "G4UIcommand.hh"
00035 #include "G4UIsession.hh"
00036 #include "G4UIbatch.hh"
00037 #include "G4UIcontrolMessenger.hh"
00038 #include "G4UnitsMessenger.hh"
00039 #include "G4ios.hh"
00040 #include "G4strstreambuf.hh"
00041 #include "G4StateManager.hh"
00042 #include "G4UIaliasList.hh"
00043 #include "G4Tokenizer.hh"
00044 
00045 #include <sstream>
00046 #include <fstream>
00047 
00048 G4UImanager * G4UImanager::fUImanager = 0;
00049 G4bool G4UImanager::fUImanagerHasBeenKilled = false;
00050 
00051 G4UImanager * G4UImanager::GetUIpointer()
00052 {
00053   if(!fUImanager)
00054   {
00055     if(!fUImanagerHasBeenKilled)
00056     {
00057       fUImanager = new G4UImanager;
00058       fUImanager->CreateMessenger();
00059     }
00060   }
00061   return fUImanager;
00062 }
00063 
00064 G4UImanager::G4UImanager()
00065   : G4VStateDependent(true),
00066     UImessenger(0), UnitsMessenger(0)
00067 {
00068   savedCommand = 0;
00069   treeTop = new G4UIcommandTree("/");
00070   aliasList = new G4UIaliasList;
00071   G4String nullString;
00072   savedParameters = nullString;
00073   verboseLevel = 0;
00074   saveHistory = false;
00075   session = NULL;
00076   g4UIWindow = NULL;
00077   SetCoutDestination(session);
00078   pauseAtBeginOfEvent = false;
00079   pauseAtEndOfEvent = false;
00080   maxHistSize = 20;
00081   searchPath="";
00082 }
00083 
00084 void G4UImanager::CreateMessenger()
00085 {
00086   UImessenger = new G4UIcontrolMessenger;
00087   UnitsMessenger = new G4UnitsMessenger;
00088 }
00089 
00090 G4UImanager::~G4UImanager()
00091 {
00092   SetCoutDestination(NULL);
00093   histVec.clear();
00094   if(saveHistory) historyFile.close();
00095   delete UImessenger;
00096   delete UnitsMessenger;
00097   delete treeTop;
00098   delete aliasList;
00099   fUImanagerHasBeenKilled = true;
00100   fUImanager = NULL;
00101 }
00102 
00103 G4UImanager::G4UImanager(const G4UImanager& ui)
00104   : G4VStateDependent(true)
00105 {
00106   UImessenger = ui.UImessenger;
00107   UnitsMessenger = ui.UnitsMessenger;
00108   aliasList = ui.aliasList;
00109   g4UIWindow = ui.g4UIWindow;
00110   savedCommand= ui.savedCommand;
00111   session = ui.session;
00112   treeTop = ui.treeTop;
00113 }
00114 
00115 const G4UImanager & G4UImanager::operator=(const G4UImanager &right)
00116 { return right; }
00117 G4int G4UImanager::operator==(const G4UImanager &right) const
00118 { return (this==&right); }
00119 G4int G4UImanager::operator!=(const G4UImanager &right) const
00120 { return (this!=&right); }
00121 
00122 G4String G4UImanager::GetCurrentValues(const char * aCommand)
00123 {
00124   G4String theCommand = aCommand;
00125   savedCommand = treeTop->FindPath( theCommand );
00126   if( savedCommand == NULL )
00127   {
00128     G4cerr << "command not found" << G4endl;
00129     return G4String();
00130   }
00131   return savedCommand->GetCurrentValue();
00132 }
00133 
00134 G4String G4UImanager::GetCurrentStringValue(const char * aCommand,
00135 G4int parameterNumber, G4bool reGet)
00136 {
00137   if(reGet || savedCommand == NULL)
00138   {
00139     savedParameters = GetCurrentValues( aCommand );
00140   }
00141   G4Tokenizer savedToken( savedParameters );
00142   G4String token;
00143   for(G4int i_thParameter=0;i_thParameter<parameterNumber;i_thParameter++)
00144   {
00145     token = savedToken();
00146     if( token.isNull() ) return G4String();
00147     if( token[(size_t)0] == '"' )
00148     {
00149       token.append(" ");
00150       token.append(savedToken("\""));
00151     }
00152   }
00153   return token;
00154 }
00155 
00156 G4String G4UImanager::GetCurrentStringValue(const char * aCommand,
00157 const char * aParameterName, G4bool reGet)
00158 {
00159   if(reGet || savedCommand == NULL)
00160   {
00161     G4String parameterValues = GetCurrentValues( aCommand );
00162   }
00163   for(G4int i=0;i<savedCommand->GetParameterEntries();i++)
00164   {
00165     if( aParameterName ==
00166       savedCommand->GetParameter(i)->GetParameterName() )
00167       return GetCurrentStringValue(aCommand,i+1,false);
00168   }
00169   return G4String();
00170 }
00171 
00172 G4int G4UImanager::GetCurrentIntValue(const char * aCommand,
00173 const char * aParameterName, G4bool reGet)
00174 {
00175   G4String targetParameter =
00176      GetCurrentStringValue( aCommand, aParameterName, reGet );
00177   G4int value;
00178   const char* t = targetParameter;
00179   std::istringstream is(t);
00180   is >> value;
00181   return value;
00182 }
00183 
00184 G4int G4UImanager::GetCurrentIntValue(const char * aCommand,
00185 G4int parameterNumber, G4bool reGet)
00186 {
00187   G4String targetParameter =
00188      GetCurrentStringValue( aCommand, parameterNumber, reGet );
00189   G4int value;
00190   const char* t = targetParameter;
00191   std::istringstream is(t);
00192   is >> value;
00193   return value;
00194 }
00195 
00196 G4double G4UImanager::GetCurrentDoubleValue(const char * aCommand,
00197 const char * aParameterName, G4bool reGet)
00198 {
00199   G4String targetParameter =
00200      GetCurrentStringValue( aCommand, aParameterName, reGet );
00201   G4double value;
00202   const char* t = targetParameter;
00203   std::istringstream is(t);
00204   is >> value;
00205   return value;
00206 }
00207 
00208 G4double G4UImanager::GetCurrentDoubleValue(const char * aCommand,
00209 G4int parameterNumber, G4bool reGet)
00210 {
00211   G4String targetParameter =
00212      GetCurrentStringValue( aCommand, parameterNumber, reGet );
00213   G4double value;
00214   const char* t = targetParameter;
00215   std::istringstream is(t);
00216   is >> value;
00217   return value;
00218 }
00219 
00220 void G4UImanager::AddNewCommand(G4UIcommand * newCommand)
00221 {
00222   treeTop->AddNewCommand( newCommand );
00223 }
00224 
00225 void G4UImanager::RemoveCommand(G4UIcommand * aCommand)
00226 {
00227   treeTop->RemoveCommand( aCommand );
00228 }
00229 
00230 void G4UImanager::ExecuteMacroFile(const char * fileName)
00231 {
00232   G4UIsession* batchSession = new G4UIbatch(fileName,session);
00233   session = batchSession;
00234   G4UIsession* previousSession = session->SessionStart();
00235   delete session;
00236   session = previousSession;
00237 }
00238 
00239 void G4UImanager::LoopS(const char* valueList)
00240 {
00241   G4String vl = valueList;
00242   G4Tokenizer parameterToken(vl);
00243   G4String mf = parameterToken();
00244   G4String vn = parameterToken();
00245   G4String c1 = parameterToken();
00246   c1 += " ";
00247   c1 += parameterToken();
00248   c1 += " ";
00249   c1 += parameterToken();
00250   const char* t1 = c1;
00251   std::istringstream is(t1);
00252   G4double d1;
00253   G4double d2;
00254   G4double d3;
00255   is >> d1 >> d2 >> d3;
00256   Loop(mf,vn,d1,d2,d3);
00257 }
00258 
00259 void G4UImanager::Loop(const char * macroFile,const char * variableName,
00260                    G4double initialValue,G4double finalValue,G4double stepSize)
00261 {
00262   G4String cd;
00263   if (stepSize > 0) {
00264     for(G4double d=initialValue;d<=finalValue;d+=stepSize)
00265       {
00266   std::ostringstream os;
00267   os << d;
00268   cd += os.str();
00269   cd += " ";
00270       }
00271   } else {
00272     for(G4double d=initialValue;d>=finalValue;d+=stepSize)
00273       {
00274   std::ostringstream os;
00275   os << d;
00276   cd += os.str();
00277   cd += " ";
00278       }
00279   }
00280   Foreach(macroFile,variableName,cd);
00281 }
00282 
00283 void G4UImanager::ForeachS(const char* valueList)
00284 {
00285   G4String vl = valueList;
00286   G4Tokenizer parameterToken(vl);
00287   G4String mf = parameterToken();
00288   G4String vn = parameterToken();
00289   G4String c1 = parameterToken();
00290   G4String ca;
00291   while(!((ca=parameterToken()).isNull()))
00292   {
00293     c1 += " ";
00294     c1 += ca;
00295   }
00296   Foreach(mf,vn,c1);
00297 }
00298 
00299 void G4UImanager::Foreach(const char * macroFile,const char * variableName,
00300                    const char * candidates)
00301 {
00302   G4String candidatesString = candidates;
00303   G4Tokenizer parameterToken( candidatesString );
00304   G4String cd;
00305   while(!((cd=parameterToken()).isNull()))
00306   {
00307     G4String vl = variableName;
00308     vl += " ";
00309     vl += cd;
00310     SetAlias(vl);
00311     ExecuteMacroFile(macroFile);
00312   }
00313 }
00314 
00315 
00316 G4String G4UImanager::SolveAlias(const char* aCmd)
00317 {
00318   G4String aCommand = aCmd;
00319   G4int ia = aCommand.index("{");
00320   G4int iz = aCommand.index("#");
00321   while((ia != G4int(std::string::npos))&&((iz==G4int(std::string::npos))||(ia<iz)))
00322   {
00323     G4int ibx = -1;
00324     while(ibx<0)
00325     {
00326       G4int ib = aCommand.index("}");
00327       if( ib == G4int(std::string::npos) )
00328       {
00329         G4cerr << aCommand << G4endl;
00330         for(G4int i=0;i<ia;i++) G4cerr << " ";
00331         G4cerr << "^" << G4endl;
00332         G4cerr << "Unmatched alias parenthis -- command ignored" << G4endl;
00333         G4String nullStr;
00334         return nullStr;
00335       }
00336       G4String ps = aCommand(ia+1,aCommand.length()-(ia+1));
00337       G4int ic = ps.index("{");
00338       G4int id = ps.index("}");
00339       if(ic!=G4int(std::string::npos) && ic < id)
00340       { ia+=ic+1; }
00341       else
00342       { ibx = ib; }
00343     }
00344     //--- Here ia represents the position of innermost "{"
00345     //--- and ibx represents corresponding "}"
00346     G4String subs;
00347     if(ia>0) subs = aCommand(0,ia);
00348     G4String alis = aCommand(ia+1,ibx-ia-1);
00349     G4String rems = aCommand(ibx+1,aCommand.length()-ibx);
00350     // G4cout << "<" << subs << "> <" << alis << "> <" << rems << ">" << G4endl;
00351     G4String* alVal = aliasList->FindAlias(alis);
00352     if(!alVal)
00353     {
00354       G4cerr << "Alias <" << alis << "> not found -- command ignored" << G4endl;
00355       G4String nullStr;
00356       return nullStr;
00357     }
00358     aCommand = subs+(*alVal)+rems;
00359     ia = aCommand.index("{");
00360   }
00361   return aCommand;
00362 }
00363 
00364 G4int G4UImanager::ApplyCommand(const G4String& aCmd)
00365 {
00366   return ApplyCommand(aCmd.data());
00367 }
00368 
00369 G4int G4UImanager::ApplyCommand(const char * aCmd)
00370 {
00371   G4String aCommand = SolveAlias(aCmd);
00372   if(aCommand.isNull()) return fAliasNotFound;
00373   if(verboseLevel) G4cout << aCommand << G4endl;
00374   G4String commandString;
00375   G4String commandParameter;
00376 
00377   G4int i = aCommand.index(" ");
00378   if( i != G4int(std::string::npos) )
00379   {
00380     commandString = aCommand(0,i);
00381     commandParameter = aCommand(i+1,aCommand.length()-(i+1));
00382   }
00383   else
00384   {
00385     commandString = aCommand;
00386   }
00387 
00388   // remove doubled slash
00389   G4int len = commandString.length();
00390   G4int ll = 0;
00391   G4String a1;
00392   G4String a2;
00393   while(ll<len-1)
00394   {
00395     if(commandString(ll,2)=="//")
00396     {
00397       if(ll==0)
00398       { commandString.remove(ll,1); }
00399       else
00400       {
00401         a1 = commandString(0,ll);
00402         a2 = commandString(ll+1,len-ll-1);
00403         commandString = a1+a2;
00404       }
00405       len--;
00406     }
00407     else
00408     { ll++; }
00409   }
00410 
00411   G4UIcommand * targetCommand = treeTop->FindPath( commandString );
00412   if( targetCommand == NULL )
00413   { return fCommandNotFound; }
00414 
00415   if(!(targetCommand->IsAvailable()))
00416   { return fIllegalApplicationState; }
00417 
00418   if(saveHistory) historyFile << aCommand << G4endl;
00419   if( G4int(histVec.size()) >= maxHistSize )
00420   { histVec.erase(histVec.begin()); }
00421   histVec.push_back(aCommand);
00422   return targetCommand->DoIt( commandParameter );
00423 }
00424 
00425 void G4UImanager::StoreHistory(const char* fileName)
00426 { StoreHistory(true,fileName); }
00427 
00428 void G4UImanager::StoreHistory(G4bool historySwitch,const char* fileName)
00429 {
00430   if(historySwitch)
00431   {
00432     if(saveHistory)
00433     { historyFile.close(); }
00434     historyFile.open((char*)fileName);
00435     saveHistory = true;
00436   }
00437   else
00438   {
00439     historyFile.close();
00440     saveHistory = false;
00441   }
00442   saveHistory = historySwitch;
00443 }
00444 
00445 void G4UImanager::PauseSession(const char* msg)
00446 {
00447   if(session) session->PauseSessionStart(msg);
00448 }
00449 
00450 void G4UImanager::ListCommands(const char* direct)
00451 {
00452   G4UIcommandTree* comTree = FindDirectory(direct);
00453   if(comTree)
00454   { comTree->List(); }
00455   else
00456   { G4cout << direct << " is not found." << G4endl; }
00457 }
00458 
00459 G4UIcommandTree* G4UImanager::FindDirectory(const char* dirName)
00460 {
00461   G4String aDirName = dirName;
00462   G4String targetDir = aDirName.strip(G4String::both);
00463   if( targetDir( targetDir.length()-1 ) != '/' )
00464   { targetDir += "/"; }
00465   G4UIcommandTree* comTree = treeTop;
00466   if( targetDir == "/" )
00467   { return comTree; }
00468   G4int idx = 1;
00469   while( idx < G4int(targetDir.length())-1 )
00470   {
00471     G4int i = targetDir.index("/",idx);
00472     G4String targetDirString = targetDir(0,i+1);
00473     comTree = comTree->GetTree(targetDirString);
00474     if( comTree == NULL )
00475     { return NULL; }
00476     idx = i+1;
00477   }
00478   return comTree;
00479 }
00480 
00481 G4bool G4UImanager::Notify(G4ApplicationState requestedState)
00482 {
00483   //G4cout << G4StateManager::GetStateManager()->GetStateString(requestedState) << " <--- " << G4StateManager::GetStateManager()->GetStateString(G4StateManager::GetStateManager()->GetPreviousState()) << G4endl;
00484   if(pauseAtBeginOfEvent)
00485   {
00486     if(requestedState==G4State_EventProc &&
00487        G4StateManager::GetStateManager()->GetPreviousState()==G4State_GeomClosed)
00488     { PauseSession("BeginOfEvent"); }
00489   }
00490   if(pauseAtEndOfEvent)
00491   {
00492     if(requestedState==G4State_GeomClosed &&
00493        G4StateManager::GetStateManager()->GetPreviousState()==G4State_EventProc)
00494     { PauseSession("EndOfEvent"); }
00495   }
00496   return true;
00497 }
00498 
00499 //void G4UImanager::Interact()
00500 //{
00501 //  Interact(G4String("G4> "));
00502 //}
00503 
00504 //void G4UImanager::Interact(const char * pC)
00505 //{
00506 //  G4cerr << "G4UImanager::Interact() is out of date and is not used anymore." << G4endl;
00507 //  G4cerr << "This method will be removed shortly!!!" << G4endl;
00508 //  G4cerr << "In case of main() use" << G4endl;
00509 //  G4cerr << "    G4UIsession * session = new G4UIterminal;" << G4endl;
00510 //  G4cerr << "    session->SessionStart();" << G4endl;
00511 //  G4cerr << "In other cases use" << G4endl;
00512 //  G4cerr << "    G4StateManager::GetStateManager()->Pause();" << G4endl;
00513 //}
00514 
00515 
00516 
00517 void G4UImanager::SetCoutDestination(G4UIsession *const value)
00518 {
00519     G4coutbuf.SetDestination(value);
00520     G4cerrbuf.SetDestination(value);
00521 }
00522 
00523 void G4UImanager::SetAlias(const char * aliasLine)
00524 {
00525   G4String aLine = aliasLine;
00526   G4int i = aLine.index(" ");
00527   G4String aliasName = aLine(0,i);
00528   G4String aliasValue = aLine(i+1,aLine.length()-(i+1));
00529   if(aliasValue(0)=='"')
00530   {
00531     G4String strippedValue;
00532     if(aliasValue(aliasValue.length()-1)=='"')
00533     { strippedValue = aliasValue(1,aliasValue.length()-2); }
00534     else
00535     { strippedValue = aliasValue(1,aliasValue.length()-1); }
00536     aliasValue = strippedValue;
00537   }
00538 
00539   aliasList->ChangeAlias(aliasName,aliasValue);
00540 }
00541 
00542 void G4UImanager::RemoveAlias(const char * aliasName)
00543 {
00544   G4String aL = aliasName;
00545   G4String targetAlias = aL.strip(G4String::both);
00546   aliasList->RemoveAlias(targetAlias);
00547 }
00548 
00549 void G4UImanager::ListAlias()
00550 {
00551   aliasList->List();
00552 }
00553 
00554 void G4UImanager::CreateHTML(const char* dir)
00555 {
00556   G4UIcommandTree* tr = FindDirectory(dir);
00557   if(tr!=0)
00558   { tr->CreateHTML(); }
00559   else
00560   { G4cerr << "Directory <" << dir << "> is not found." << G4endl; }
00561 }
00562 
00563 void G4UImanager::ParseMacroSearchPath()
00564 {
00565   searchDirs.clear();
00566 
00567   size_t idxfirst = 0;
00568   size_t idxend = 0;
00569   G4String pathstring = "";
00570   while( (idxend = searchPath.index(':', idxfirst)) != G4String::npos) {
00571     pathstring = searchPath.substr(idxfirst, idxend-idxfirst);
00572     if(pathstring.size() != 0) searchDirs.push_back(pathstring);
00573     idxfirst = idxend + 1;
00574   }
00575 
00576   pathstring = searchPath.substr(idxfirst, searchPath.size()-idxfirst);
00577   if(pathstring.size() != 0) searchDirs.push_back(pathstring);
00578 }
00579 
00580 
00581 static G4bool FileFound(const G4String& fname)
00582 {
00583   G4bool qopen = false;
00584   std::ifstream fs;
00585   fs.open(fname.c_str(), std::ios::in);
00586   if(fs.good()) {
00587     fs.close();
00588     qopen = true;
00589   }
00590   return qopen;
00591 }
00592 
00593 G4String G4UImanager::FindMacroPath(const G4String& fname) const
00594 {
00595   G4String macrofile = fname;
00596 
00597   for (size_t i = 0; i < searchDirs.size(); i++) {
00598     G4String fullpath = searchDirs[i] + "/" + fname;
00599     if ( FileFound(fullpath) ) {
00600       macrofile = fullpath;
00601       break;
00602     }
00603   }
00604 
00605   return macrofile;
00606 }

Generated on Mon May 27 17:50:06 2013 for Geant4 by  doxygen 1.4.7