Index: trunk/source/visualization/management/src/G4VSceneHandler.cc
===================================================================
--- trunk/source/visualization/management/src/G4VSceneHandler.cc	(revision 1140)
+++ trunk/source/visualization/management/src/G4VSceneHandler.cc	(revision 1168)
@@ -25,5 +25,5 @@
 //
 //
-// $Id: G4VSceneHandler.cc,v 1.86 2009/10/23 07:58:36 allison Exp $
+// $Id: G4VSceneHandler.cc,v 1.91 2009/11/16 13:51:08 lgarnier Exp $
 // GEANT4 tag $Name:  $
 //
@@ -74,4 +74,6 @@
 #include "G4HitsModel.hh"
 #include "G4VHit.hh"
+#include "G4ScoringManager.hh"
+#include "G4DefaultLinearColorMap.hh"
 #include "Randomize.hh"
 #include "G4StateManager.hh"
@@ -116,7 +118,13 @@
 
 G4VSceneHandler::~G4VSceneHandler () {
-  G4ViewerListIterator i;
-  for (i = fViewerList.begin(); i != fViewerList.end(); ++i) {
-    delete *i;
+//   G4ViewerListIterator i;
+//   for (i = fViewerList.begin(); i != fViewerList.end(); ++i) {
+//      delete *i;
+//    }
+  G4VViewer* last;
+  while( ! fViewerList.empty() ) {
+    last = fViewerList.back();
+    fViewerList.pop_back();
+    delete last;
   }
 }
@@ -262,10 +270,48 @@
 
 void G4VSceneHandler::AddCompound (const G4VHit& hit) {
-  static_cast<G4VHit>(hit).Draw(); // Cast because Draw is non-const!!!!
+  // Cast away const because Draw is non-const!!!!
+  const_cast<G4VHit&>(hit).Draw();
 }
 
 void G4VSceneHandler::AddCompound (const G4THitsMap<G4double>& hits) {
-  // Cast because DrawAllHits is non-const!!!!
-  static_cast<G4THitsMap<G4double> >(hits).DrawAllHits();
+  //G4cout << "AddCompound: hits: " << &hits << G4endl;
+  G4bool scoreMapHits = false;
+  G4ScoringManager* scoringManager = G4ScoringManager::GetScoringManagerIfExist();
+  if (scoringManager) {
+    size_t nMeshes = scoringManager->GetNumberOfMesh();
+    for (size_t i = 0; i < nMeshes; ++i) {
+      G4VScoringMesh* mesh = scoringManager->GetMesh(i);
+      if (mesh && mesh->IsActive()) {
+	MeshScoreMap scoreMap = mesh->GetScoreMap();
+	for(MeshScoreMap::const_iterator i = scoreMap.begin();
+	    i != scoreMap.end(); ++i) {
+	  const G4String& scoreMapName = i->first;
+	  const G4THitsMap<G4double>* foundHits = i->second;
+	  if (foundHits == &hits) {
+	    G4DefaultLinearColorMap colorMap("G4VSceneHandlerColorMap");
+	    scoreMapHits = true;
+	    mesh->DrawMesh(scoreMapName, &colorMap);
+	  }
+	}
+      }
+    }
+  }
+  if (scoreMapHits) {
+    static G4bool first = true;
+    if (first) {
+      first = false;
+      G4cout <<
+	"Scoring map drawn with default parameters."
+	"\n  To get gMocren file for gMocren browser:"
+	"\n    /vis/open gMocrenFile"
+	"\n    /vis/viewer/flush"
+	"\n  Many other options available with /score/draw... commands."
+	"\n  You might want to \"/vis/viewer/set/autoRefresh false\"."
+	     << G4endl;
+    }
+  } else {  // Not score map hits.  Just call DrawAllHits.
+    // Cast away const because DrawAllHits is non-const!!!!
+    const_cast<G4THitsMap<G4double>&>(hits).DrawAllHits();
+  }
 }
 
@@ -456,9 +502,4 @@
     if (pPolyhedron) {
       pPolyhedron -> SetVisAttributes (fpVisAttribs);
-#ifdef G4DEBUG_VIS_MANAGEMENT
-      printf("G4VSceneHandler::RequestPrimitives VSolid: %d\n",pPolyhedron);
-#endif
-      G4cout <<
-        "G4VSceneHandler::RequestPrimitives VSolid " << solid.GetName () <<G4endl;
       AddPrimitive (*pPolyhedron);
     }
@@ -481,36 +522,17 @@
 void G4VSceneHandler::ProcessScene (G4VViewer&) {
 
-#ifdef G4DEBUG_VIS_MANAGEMENT
-  printf("G4VSceneHandler::ProcessScene : BEGIN\n");
-#endif
-
   if (!fpScene) return;
 
-#ifdef G4DEBUG_VIS_MANAGEMENT
-  printf("G4VSceneHandler::ProcessScene : 2\n");
-#endif
   G4VisManager* visManager = G4VisManager::GetInstance();
 
-#ifdef G4DEBUG_VIS_MANAGEMENT
-  printf("G4VSceneHandler::ProcessScene : 3\n");
-#endif
   if (!visManager->GetConcreteInstance()) return;
 
-#ifdef G4DEBUG_VIS_MANAGEMENT
-  printf("G4VSceneHandler::ProcessScene : 4\n");
-#endif
   G4VisManager::Verbosity verbosity = visManager->GetVerbosity();
 
   fReadyForTransients = false;
 
-#ifdef G4DEBUG_VIS_MANAGEMENT
-  printf("G4VSceneHandler::ProcessScene : Before clear store \n");
-#endif
   // Clear stored scene, if any, i.e., display lists, scene graphs.
   ClearStore ();
 
-#ifdef G4DEBUG_VIS_MANAGEMENT
-  printf("G4VSceneHandler::ProcessScene : 5\n");
-#endif
   // Reset fMarkForClearingTransientStore.  No need to clear transient
   // store since it has just been cleared above.  (Leaving
@@ -526,14 +548,8 @@
 
   if (runDurationModelList.size ()) {
-#ifdef G4DEBUG_VIS_MANAGEMENT
-  printf("G4VSceneHandler::ProcessScene : 6\n");
-#endif
     if (verbosity >= G4VisManager::confirmations) {
       G4cout << "Traversing scene data..." << G4endl;
     }
 
-#ifdef G4DEBUG_VIS_MANAGEMENT
-    printf("G4VSceneHandler::ProcessScene : begin model\n");
-#endif
     BeginModeling ();
 
@@ -542,7 +558,4 @@
 
     for (size_t i = 0; i < runDurationModelList.size (); i++) {
-#ifdef G4DEBUG_VIS_MANAGEMENT
-  printf("G4VSceneHandler::ProcessScene : 7\n");
-#endif
       G4VModel* pModel = runDurationModelList[i];
       // Note: this is not the place to take action on
@@ -558,12 +571,6 @@
     // Repeat if required...
     if (fSecondPassRequested) {
-#ifdef G4DEBUG_VIS_MANAGEMENT
-  printf("G4VSceneHandler::ProcessScene : 8\n");
-#endif
       fSecondPass = true;
       for (size_t i = 0; i < runDurationModelList.size (); i++) {
-#ifdef G4DEBUG_VIS_MANAGEMENT
-  printf("G4VSceneHandler::ProcessScene : 9\n");
-#endif
 	G4VModel* pModel = runDurationModelList[i];
 	pModel -> SetModelingParameters (pMP);
@@ -585,7 +592,4 @@
   fReadyForTransients = true;
 
-#ifdef G4DEBUG_VIS_MANAGEMENT
-  printf("G4VSceneHandler::ProcessScene : Idle ?\n");
-#endif
   // Refresh event from end-of-event model list.
   // Allow only in Idle or GeomClosed state...
@@ -593,26 +597,14 @@
   G4ApplicationState state = stateManager->GetCurrentState();
   if (state == G4State_Idle || state == G4State_GeomClosed) {
-#ifdef G4DEBUG_VIS_MANAGEMENT
-    printf("G4VSceneHandler::ProcessScene : IDLE\n");
-#endif
 
     visManager->SetEventRefreshing(true);
 
     if (visManager->GetRequestedEvent()) {
-#ifdef G4DEBUG_VIS_MANAGEMENT
-      printf("G4VSceneHandler::ProcessScene : DrawEvent\n");
-#endif
       DrawEvent(visManager->GetRequestedEvent());
 
     } else {
 
-#ifdef G4DEBUG_VIS_MANAGEMENT
-      printf("G4VSceneHandler::ProcessScene : no event\n");
-#endif
       G4RunManager* runManager = G4RunManager::GetRunManager();
       if (runManager) {
-#ifdef G4DEBUG_VIS_MANAGEMENT
-        printf("G4VSceneHandler::ProcessScene : runManager\n");
-#endif
 	const G4Run* run = runManager->GetCurrentRun();
 	const std::vector<const G4Event*>* events =
@@ -622,11 +614,5 @@
 	if (nKeptEvents) {
 
-#ifdef G4DEBUG_VIS_MANAGEMENT
-        printf("G4VSceneHandler::ProcessScene : 1\n");
-#endif
 	  if (fpScene->GetRefreshAtEndOfEvent()) {
-#ifdef G4DEBUG_VIS_MANAGEMENT
-        printf("G4VSceneHandler::ProcessScene : 2\n");
-#endif
 
 	    if (verbosity >= G4VisManager::confirmations) {
@@ -638,7 +624,4 @@
 
 	  } else {  // Accumulating events.
-#ifdef G4DEBUG_VIS_MANAGEMENT
-        printf("G4VSceneHandler::ProcessScene : 3\n");
-#endif
 
 	    if (verbosity >= G4VisManager::confirmations) {
@@ -646,7 +629,4 @@
 	    }
 	    for (size_t i = 0; i < nKeptEvents; ++i) {
-#ifdef G4DEBUG_VIS_MANAGEMENT
-        printf("G4VSceneHandler::ProcessScene : 4\n");
-#endif
 	      const G4Event* event = (*events)[i];
 	      if (event) DrawEvent(event);
@@ -654,7 +634,4 @@
 
 	    if (!fpScene->GetRefreshAtEndOfRun()) {
-#ifdef G4DEBUG_VIS_MANAGEMENT
-        printf("G4VSceneHandler::ProcessScene : 5\n");
-#endif
 	      if (verbosity >= G4VisManager::warnings) {
 		G4cout <<
@@ -671,4 +648,10 @@
   }
 
+  // Refresh end-of-run model list.
+  // Allow only in Idle or GeomClosed state...
+  if (state == G4State_Idle || state == G4State_GeomClosed) {
+    DrawEndOfRunModels();
+  }
+
   fMarkForClearingTransientStore = tmpMarkForClearingTransientStore;
 }
@@ -684,4 +667,24 @@
     for (size_t i = 0; i < nModels; i++) {
       G4VModel* pModel = EOEModelList [i];
+      pModel -> SetModelingParameters(pMP);
+      SetModel (pModel);
+      pModel -> DescribeYourselfTo (*this);
+      pModel -> SetModelingParameters(0);
+    }
+    delete pMP;
+    SetModel (0);
+  }
+}
+
+void G4VSceneHandler::DrawEndOfRunModels()
+{
+  const std::vector<G4VModel*>& EORModelList =
+    fpScene -> GetEndOfRunModelList ();
+  size_t nModels = EORModelList.size();
+  if (nModels) {
+    G4ModelingParameters* pMP = CreateModelingParameters();
+    pMP->SetEvent(0);
+    for (size_t i = 0; i < nModels; i++) {
+      G4VModel* pModel = EORModelList [i];
       pModel -> SetModelingParameters(pMP);
       SetModel (pModel);
