Index: snovis/trunk/source/G4Lab/cxx/PhysicsTableAccessor.cxx
===================================================================
--- snovis/trunk/source/G4Lab/cxx/PhysicsTableAccessor.cxx	(revision 233)
+++ snovis/trunk/source/G4Lab/cxx/PhysicsTableAccessor.cxx	(revision 288)
@@ -24,8 +24,10 @@
 #include <Slash/Data/IIterator.h>
 #include <Lib/Out.h>
+#include <Lib/sout.h>
 #include <Lib/Value.h>
 #include <Lib/smanip.h>
 #include <Lib/fmanip.h>
 #include <Lib/dirmanip.h>
+#include <Lib/Cast.h>
 #include <Lib/Debug.h>
 
@@ -39,4 +41,7 @@
 #include <AIDA/IFunctionFactory.h>
 #include <AIDA/IFunction.h>
+
+// G4Lab :
+#include <G4Lab/Interfaces/IGeant4Manager.h>
 
 namespace G4Lab {
@@ -72,8 +77,10 @@
 G4Lab::PhysicsTableAccessor::PhysicsTableAccessor(
  Slash::Core::ISession& aSession
+,IGeant4Manager& aManager
 ,AIDA::IAnalysisFactory* aAIDA
 )
 :Lib::BaseAccessor(aSession.printer())
 ,fSession(aSession)
+,fManager(aManager)
 ,fType("PhysicsTable")
 ,fAIDA(aAIDA)
@@ -94,4 +101,14 @@
 {
   for(unsigned int index=0;index<fTables.size();index++) delete fTables[index];
+}
+//////////////////////////////////////////////////////////////////////////////
+void* G4Lab::PhysicsTableAccessor::cast(
+ const std::string& aClass
+) const 
+//////////////////////////////////////////////////////////////////////////////
+//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
+{
+  if_Lib_SCast(Slash::Data::IVisualizer)
+  else return Lib::BaseAccessor::cast(aClass);
 }
 //////////////////////////////////////////////////////////////////////////////
@@ -129,53 +146,7 @@
 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
 {
-  if(!fTables.size() ) {
-    G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
-    if(particleTable) {
-      G4ParticleTable::G4PTblDicIterator* particleTableIterator =
-        particleTable->GetIterator();
-      if(particleTableIterator) {
-        particleTableIterator->reset();
-        while( (*particleTableIterator)() ){
-          G4ParticleDefinition* particle = particleTableIterator->value();
-          if(particle) {
-            G4String particleName = particle->GetParticleName();
-            //Lib::Out out(fSession.printer());
-            //out << "debug : Particle : " << particleName << Lib::endl;
-            // Processes for this particle :
-            G4ProcessManager* processManager = particle->GetProcessManager();
-            if(processManager) {
-              G4ProcessVector* processList = processManager->GetProcessList();
-              if(processList) {
-                int number = processList->entries();
-                for (int index=0;index<number;index++){
-                  G4VProcess* process = (*processList)(index);
-                  std::vector<std::string> names;
-                  std::vector<G4PhysicsTable*> tables;
-                  if(findProcessTables(particle,
-                                       process,
-                                       names,
-                                       tables)) {
-                    for(unsigned int i=0;i<tables.size();i++) {
-                      std::vector<std::string> words;
-                      Lib::smanip::words(names[i],".",words);
-                      if(words.size()==3) {
-                        fTables.push_back(new PhysicsTable(words[0],
-                                                           words[1],
-                                                           words[2],
-                                                           tables[i],
-                                                           process,
-                                                           particle));
-                      }
-                    }
-                  }
-                }
-              }
-            }
-          }
-        }
-      }
-    }
-  }
-
+  if(!fTables.size()) {
+    if(!buildProcessTables()) return 0;
+  }
   return new PhysicsTableIterator(fTables);
 }
@@ -277,47 +248,51 @@
     G4PhysicsVector* physicsVector = (*(obj->fTable))[count];
     size_t n = physicsVector->GetVectorLength();
-    if(n>=2) {
-      std::string name;
-      Lib::smanip::printf(name,1024,"%s.%s.%s.%d",
-                    obj->fTableName.c_str(),
-                    obj->fProcessName.c_str(),
-                    obj->fParticleName.c_str(),
-                    count);
-      std::vector<double> params;
-      params.push_back(1); // Dimension of the grid (here one).
-      params.push_back(n); // Number of entries for first grid axis.
-      size_t i;
-      for (i=0;i<n;i++) params.push_back(i); // Xs.
-      for (i=0;i<n;i++) params.push_back((*physicsVector)[i]); //Values.
-      AIDA::IFunction* function = 
-        functionFactory->createFunctionByName(name,"Grid1D");
-      if(!function) {
-        Lib::Out out(fSession.printer());
-        out << "Can't create \"" << name << "\" function." << Lib::endl;
-        break;
-      }
-      function->setParameters(params);
+    if(n<2) {
+      Lib::Out out(fSession.printer());
+      out << "PhysicsVector with " << int(n) << " entries. Can't plot." 
+          << Lib::endl;
+      continue;
+    }
+
+    std::string name;
+    Lib::smanip::printf(name,1024,"%s.%s.%s.%d",
+                  obj->fTableName.c_str(),
+                  obj->fProcessName.c_str(),
+                  obj->fParticleName.c_str(),
+                  count);
+    std::vector<double> params;
+    params.push_back(1); // Dimension of the grid (here one).
+    params.push_back(n); // Number of entries for first grid axis.
+    size_t i;
+    for (i=0;i<n;i++) params.push_back(i); // Xs.
+    for (i=0;i<n;i++) params.push_back((*physicsVector)[i]); //Values.
+    AIDA::IFunction* function = 
+      functionFactory->createFunctionByName(name,"Grid1D");
+    if(!function) {
+      Lib::Out out(fSession.printer());
+      out << "Can't create \"" << name << "\" function." << Lib::endl;
+      break;
+    }
+    function->setParameters(params);
       
-      AIDA::IPlotterRegion& region = plotter->currentRegion();
-
-      region.plot(*function);
-      region.setParameter("plotter.xAxisAutomated","FALSE");
-      std::string value1;
-      Lib::smanip::printf(value1,32,"%g",0.);
-      region.setParameter("plotter.xAxisMinimum",value1);
-      std::string value2;
-      Lib::smanip::printf(value2,32,"%g",(double)(n-1));
-      region.setParameter("plotter.xAxisMaximum",value2);
-      std::string value3;
-      Lib::smanip::printf(value3,32,"%d",4 * n);
-      region.setParameter("plotter.xNumberOfPoints",value3);
-      /*
-      fSession.out().println("debug : min : %s, max : %s, steps : %s\n.",
-                             value1.c_str(),value2.c_str(),value3.c_str());
-      */
-
-      if(!superpose) plotter->next();
+    AIDA::IPlotterRegion& region = plotter->currentRegion();
+
+    region.plot(*function);
+    region.setParameter("plotter.xAxisAutomated","FALSE");
+    std::string value1;
+    Lib::smanip::printf(value1,32,"%g",0.);
+    region.setParameter("plotter.xAxisMinimum",value1);
+    std::string value2;
+    Lib::smanip::printf(value2,32,"%g",(double)(n-1));
+    region.setParameter("plotter.xAxisMaximum",value2);
+    //std::string value3;
+    //Lib::smanip::printf(value3,32,"%d",4 * n);
+    //region.setParameter("plotter.xNumberOfPoints",value3);
+    /*
+    fSession.out().println("debug : min : %s, max : %s, steps : %s\n.",
+                           value1.c_str(),value2.c_str(),value3.c_str());
+    */
+    if(!superpose) plotter->next();
       
-    }
   }
   
@@ -335,4 +310,106 @@
 }
 //////////////////////////////////////////////////////////////////////////////
+bool G4Lab::PhysicsTableAccessor::buildProcessTables(
+) 
+//////////////////////////////////////////////////////////////////////////////
+//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
+{
+  if(fTables.size()) return true; //done.
+
+  if(!fManager.isRunning()) {
+    Lib::Out out(fSession.printer());
+    out << "G4Lab::PhysicsTableAccessor::buildProcessTables :"
+        << " It is needed to have started a run to get physics tables." 
+        << Lib::endl;
+    return false;
+  }
+
+  G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
+  if(!particleTable) {
+    Lib::Out out(fSession.printer());
+    out << "G4Lab::PhysicsTableAccessor::buildProcessTables :"
+        << " Can't get particle table." 
+        << Lib::endl;
+    return false;
+  }
+
+  G4ParticleTable::G4PTblDicIterator* particleTableIterator = 
+    particleTable->GetIterator();
+  if(!particleTableIterator) {
+    Lib::Out out(fSession.printer());
+    out << "G4Lab::PhysicsTableAccessor::buildProcessTables :"
+        << " Can't get particle table iterator." 
+        << Lib::endl;
+    return false;
+  }
+
+  particleTableIterator->reset();
+  while( (*particleTableIterator)() ){
+    G4ParticleDefinition* particle = particleTableIterator->value();
+    if(!particle) {
+      Lib::Out out(fSession.printer());
+      out << "G4Lab::PhysicsTableAccessor::buildProcessTables :"
+          << " Can't get particle table particle." 
+          << Lib::endl;
+      return false;
+    }
+    G4String particleName = particle->GetParticleName();
+    //Lib::Out out(fSession.printer());
+    //out << "debug : Particle : " << particleName << Lib::endl;
+
+    // Processes for this particle :
+    G4ProcessManager* processManager = particle->GetProcessManager();
+    if(!processManager) {
+      Lib::Out out(fSession.printer());
+      out << "G4Lab::PhysicsTableAccessor::buildProcessTables :"
+          << " Can't get process manager of particle " 
+          << particleName << "."
+          << Lib::endl;
+      return false;
+    }
+
+    G4ProcessVector* processList = processManager->GetProcessList();
+    if(!processList) {
+      Lib::Out out(fSession.printer());
+      out << "G4Lab::PhysicsTableAccessor::buildProcessTables :"
+          << " Can't get process list for process manager of particle " 
+          << particleName << "."
+          << Lib::endl;
+      return false;
+    }
+
+    int number = processList->entries();
+    for (int index=0;index<number;index++){
+      G4VProcess* process = (*processList)(index);
+      std::vector<std::string> names;
+      std::vector<G4PhysicsTable*> tables;
+      if(!findProcessTables(particle,process,names,tables)) {
+        return false;
+      }
+
+      for(unsigned int i=0;i<tables.size();i++) {
+        std::vector<std::string> words;
+        Lib::smanip::words(names[i],".",words);
+        if(words.size()!=3) {
+          Lib::Out out(fSession.printer());
+          out << "G4Lab::PhysicsTableAccessor::buildProcessTables :"
+              << " Three words separted with a dot expected in " 
+              << names[i] << " for particle " << particleName << "."
+              << Lib::endl;
+          return false;
+        }
+        fTables.push_back(new PhysicsTable(words[0],
+                                           words[1],
+                                           words[2],
+                                           tables[i],
+                                           process,
+                                           particle));
+      }
+    }
+  }
+
+  return true;
+}
+//////////////////////////////////////////////////////////////////////////////
 bool G4Lab::PhysicsTableAccessor::findProcessTables(
  G4ParticleDefinition* aParticle
@@ -364,22 +441,52 @@
 #else
   G4bool ascii = false;
+  //G4bool ascii = true;
 #endif
-  aProcess->StorePhysicsTable(aParticle,tmpName,ascii);
-
-  // Analyse the content of the directory :
-  std::vector<std::string> files;
-  if(!Lib::dirmanip::entries(tmpName,files,false)) {
-    Lib::Out out(fSession.printer());
-    out << "Can't get files in \"" << tmpName << "\" directory." << Lib::endl;
+
+  if(!aProcess->StorePhysicsTable(aParticle,tmpName,ascii)) {
+    Lib::Out out(fSession.printer());
+    out << "Can't store physics table for particle "
+        << aParticle->GetParticleName()
+        << " and process " << aProcess->GetProcessName() << "."
+        << Lib::endl;
+    //return false;
   } else {
-    // Analyse files :
-    Lib::smanip::remove(files,".");
-    Lib::smanip::remove(files,"..");
-    for(unsigned int index=0;index<files.size();index++) {
-      std::vector<std::string> words;
-      Lib::smanip::words(files[index],".",words);
-      //Lib::Out out(fSession.printer());
-      //out << "debug : Analyse \"" << files[index] << "\" ." << Lib::endl;
-      if(words.size()==4) {
+
+    // Analyse the content of the directory :
+    std::vector<std::string> files;
+    if(!Lib::dirmanip::entries(tmpName,files,false)) {
+      Lib::Out out(fSession.printer());
+      out << "Can't get files in \"" << tmpName << "\" directory." 
+          << Lib::endl;
+    } else {
+      // Analyse files :
+      Lib::smanip::remove(files,".");
+      Lib::smanip::remove(files,"..");
+/*
+      if(!files.size()) {
+        Lib::Out out(fSession.printer());
+        out << "No files in \"" << tmpName << "\" directory for particle " 
+            << aParticle->GetParticleName() 
+            << " and process " << aProcess->GetProcessName() << "."
+            << Lib::endl;
+        //return false;
+      }
+*/
+      for(unsigned int index=0;index<files.size();index++) {
+        std::vector<std::string> words;
+        Lib::smanip::words(files[index],".",words);
+        //Lib::Out out(fSession.printer());
+        //out << "debug : Analyse \"" << files[index] << "\" ." << Lib::endl;
+        if(words.size()!=4) {
+          Lib::Out out(fSession.printer());
+          out << "Bad syntax in file name " << Lib::sout(files[index])
+              << " for particle " << aParticle->GetParticleName() 
+              << " and process " << aProcess->GetProcessName() << "."
+              << Lib::endl;
+          //aTables.clear();
+          //aPhysicsTables.clear();
+          //return false;
+          continue;
+        }
         std::string table = words[0];
         std::string process = words[1];
@@ -393,12 +500,10 @@
         G4PhysicsTable* physicsTable = 
           new G4PhysicsTable(G4Element::GetNumberOfElements());
-        if(physicsTable) {
-          G4bool status = physicsTable->RetrievePhysicsTable(filename,ascii);
-          if(!status){
-            Lib::Out out(fSession.printer());
-            out << "Unable to read \"" << filename << "\" file." << Lib::endl;
-            delete physicsTable;
-            physicsTable = 0;
-          }
+        G4bool status = physicsTable->RetrievePhysicsTable(filename,ascii);
+        if(!status){
+          Lib::Out out(fSession.printer());
+          out << "Unable to read \"" << filename << "\" file." << Lib::endl;
+          delete physicsTable;
+          physicsTable = 0;
         }
         if(physicsTable) {
@@ -408,4 +513,5 @@
       }
     }
+
   }
 
