source: trunk/source/visualization/management/src/G4VisManager.cc @ 894

Last change on this file since 894 was 894, checked in by garnier, 16 years ago

only debug msg

  • Property svn:mime-type set to text/cpp
File size: 52.0 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
5// * The  Geant4 software  is  copyright of the Copyright Holders  of *
6// * the Geant4 Collaboration.  It is provided  under  the terms  and *
7// * conditions of the Geant4 Software License,  included in the file *
8// * LICENSE and available at  http://cern.ch/geant4/license .  These *
9// * include a list of copyright holders.                             *
10// *                                                                  *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work  make  any representation or  warranty, express or implied, *
14// * regarding  this  software system or assume any liability for its *
15// * use.  Please see the license in the file  LICENSE  and URL above *
16// * for the full disclaimer and the limitation of liability.         *
17// *                                                                  *
18// * This  code  implementation is the result of  the  scientific and *
19// * technical work of the GEANT4 collaboration.                      *
20// * By using,  copying,  modifying or  distributing the software (or *
21// * any work based  on the software)  you  agree  to acknowledge its *
22// * use  in  resulting  scientific  publications,  and indicate your *
23// * acceptance of all terms of the Geant4 Software license.          *
24// ********************************************************************
25//
26// $Id: G4VisManager.cc,v 1.115 2008/01/04 22:03:46 allison Exp $
27// GEANT4 tag $Name: HEAD $
28//
29//
30// GEANT4 Visualization Manager - John Allison 02/Jan/1996.
31
32#include "G4VisManager.hh"
33
34#include "G4VisCommands.hh"
35#include "G4VisCommandsCompound.hh"
36#include "G4VisCommandsGeometry.hh"
37#include "G4VisCommandsGeometrySet.hh"
38#include "G4VisCommandsScene.hh"
39#include "G4VisCommandsSceneAdd.hh"
40#include "G4VisCommandsSceneHandler.hh"
41#include "G4VisCommandsViewer.hh"
42#include "G4VisCommandsViewerSet.hh"
43#include "G4UImanager.hh"
44#include "G4VisStateDependent.hh"
45#include "G4UIdirectory.hh"
46#include "G4VisFeaturesOfFukuiRenderer.hh"
47#include "G4VisFeaturesOfDAWNFILE.hh"
48#include "G4VisFeaturesOfOpenGL.hh"
49#include "G4VisFeaturesOfOpenInventor.hh"
50#include "G4VGraphicsSystem.hh"
51#include "G4VSceneHandler.hh"
52#include "G4VViewer.hh"
53#include "G4VPhysicalVolume.hh"
54#include "G4LogicalVolume.hh"
55#include "G4VSolid.hh"
56#include "G4Vector3D.hh"
57#include "G4Point3D.hh"
58#include "G4RotationMatrix.hh"
59#include "G4Polyline.hh"
60#include "G4Polyhedron.hh"
61#include "G4NURBS.hh"
62#include "G4NullModel.hh"
63#include "G4ModelingParameters.hh"
64#include "G4TransportationManager.hh"
65#include "G4VisCommandModelCreate.hh"
66#include "G4VisCommandsListManager.hh"
67#include "G4VisModelManager.hh"
68#include "G4VModelFactory.hh"
69#include "G4VisFilterManager.hh"
70#include "G4VTrajectoryModel.hh"
71#include "G4TrajectoryDrawByCharge.hh"
72#include "Randomize.hh"
73#include "G4RunManager.hh"
74#include "G4EventManager.hh"
75#include "G4Run.hh"
76#include "G4Event.hh"
77#include <sstream>
78
79G4VisManager* G4VisManager::fpInstance = 0;
80
81G4VisManager::G4VisManager ():
82  fVerbose         (1),
83  fInitialised     (false),
84  fpUserVisAction  (0),
85  fpGraphicsSystem (0),
86  fpScene          (0),
87  fpSceneHandler   (0),
88  fpViewer         (0),
89  fVerbosity       (warnings),
90  fpStateDependent (0),
91  fEventRefreshing          (false),
92  fTransientsDrawnThisRun   (false),
93  fTransientsDrawnThisEvent (false),
94  fEventKeepingSuspended    (false),
95  fKeptLastEvent            (false),
96  fpRequestedEvent (0),
97  fAbortReviewKeptEvents    (false)
98  // All other objects use default constructors.
99{
100  fpTrajDrawModelMgr = new G4VisModelManager<G4VTrajectoryModel>("/vis/modeling/trajectories");
101  fpTrajFilterMgr = new G4VisFilterManager<G4VTrajectory>("/vis/filtering/trajectories");
102  fpHitFilterMgr = new G4VisFilterManager<G4VHit>("/vis/filtering/hits");
103
104  VerbosityGuidanceStrings.push_back
105    ("Simple graded message scheme - digit or string (1st character defines):");
106  VerbosityGuidanceStrings.push_back
107    ("  0) quiet,         // Nothing is printed.");
108  VerbosityGuidanceStrings.push_back
109    ("  1) startup,       // Startup and endup messages are printed...");
110  VerbosityGuidanceStrings.push_back
111    ("  2) errors,        // ...and errors...");
112  VerbosityGuidanceStrings.push_back
113    ("  3) warnings,      // ...and warnings...");
114  VerbosityGuidanceStrings.push_back
115    ("  4) confirmations, // ...and confirming messages...");
116  VerbosityGuidanceStrings.push_back
117    ("  5) parameters,    // ...and parameters of scenes and views...");
118  VerbosityGuidanceStrings.push_back
119    ("  6) all            // ...and everything available.");
120
121  if (fpInstance) {
122    G4Exception
123      ("G4VisManager: attempt to Construct more than one VisManager.");
124  }
125  else {
126
127    fpInstance = this;
128    SetConcreteInstance(this);
129
130    fpStateDependent = new G4VisStateDependent (this);
131    // No need to delete this; G4StateManager does this.
132
133    if (fVerbosity >= startup) {
134      G4cout << "Visualization Manager instantiating..." << G4endl;
135    }
136
137    // Note: The specific graphics systems must be instantiated in a
138    // higher level library to avoid circular dependencies.  Also,
139    // some specifically need additional external libararies that the
140    // user must supply.  Therefore we ask the user to implement
141    // RegisterGraphicsSystems() and RegisterModelFactories()
142    // in a subclass.  We have to wait for the subclass to instantiate
143    // so RegisterGraphicsSystems() cannot be called from this
144    // constructor; it is called from Initialise().  So we ask the
145    // user:
146    //   (a) to write a subclass and implement  RegisterGraphicsSystems()
147    //       and RegisterModelFactories().  See
148    //       visualization/include/G4VisExecutive.hh/icc as an example.
149    //   (b) instantiate the subclass.
150    //   (c) invoke the Initialise() method of the subclass.
151    // For example:
152    //   ...
153    // #ifdef G4VIS_USE
154    //   // Instantiate and initialise Visualization Manager.
155    //   G4VisManager* visManager = new G4VisExecutive;
156    //   visManager -> SetVerboseLevel (Verbose);
157    //   visManager -> Initialise ();
158    // #endif
159    //   // (Don't forget to delete visManager;)
160    //   ...
161  }
162}
163
164G4VisManager::~G4VisManager () {
165  fpInstance = 0;
166  size_t i;
167  for (i = 0; i < fSceneList.size (); ++i) {
168    delete fSceneList[i];
169  }
170  for (i = 0; i < fAvailableSceneHandlers.size (); ++i) {
171    delete fAvailableSceneHandlers[i];
172  }
173  for (i = 0; i < fAvailableGraphicsSystems.size (); ++i) {
174    delete fAvailableGraphicsSystems[i];
175  }
176  if (fVerbosity >= startup) {
177    G4cout << "Graphics systems deleted." << G4endl;
178    G4cout << "Visualization Manager deleting..." << G4endl;
179  }
180  for (i = 0; i < fMessengerList.size (); ++i) {
181    delete fMessengerList[i];
182  }
183  for (i = 0; i < fDirectoryList.size (); ++i) {
184    delete fDirectoryList[i];
185  }
186
187  delete fpTrajDrawModelMgr;
188  delete fpTrajFilterMgr;
189  delete fpHitFilterMgr;
190}
191
192G4VisManager* G4VisManager::GetInstance () {
193  if (!fpInstance) {
194    G4Exception
195      ("G4VisManager::GetInstance: VisManager not yet instantiated!");
196  }
197  return fpInstance;
198}
199
200void G4VisManager::Initialise () {
201#ifdef G4DEBUG
202  printf("G4VisManager::Initialise\n");
203#endif
204
205  if (fVerbosity >= startup) {
206    G4cout << "Visualization Manager initialising..." << G4endl;
207  }
208
209  if (fVerbosity >= parameters) {
210    G4cout <<
211      "\nYou have instantiated your own Visualization Manager, inheriting"
212      "\n  G4VisManager and implementing RegisterGraphicsSystems(), in which"
213      "\n  you should, normally, instantiate drivers which do not need"
214      "\n  external packages or libraries, and, optionally, drivers under"
215      "\n  control of environment variables."
216      "\n  Also you should implement RegisterModelFactories()."
217      "\n  See visualization/include/G4VisExecutive.hh/icc, for example."
218      "\n  In your main() you will have something like:"
219      "\n  #ifdef G4VIS_USE"
220      "\n    G4VisManager* visManager = new G4VisExecutive;"
221      "\n    visManager -> SetVerboseLevel (Verbose);"
222      "\n    visManager -> Initialize ();"
223      "\n  #endif"
224      "\n  (Don't forget to delete visManager;)"
225      "\n"
226         << G4endl;
227  }
228
229  if (fVerbosity >= startup) {
230    G4cout << "Registering graphics systems..." << G4endl;
231  }
232
233  RegisterGraphicsSystems ();
234
235  if (fVerbosity >= startup) {
236    G4cout <<
237      "\nYou have successfully registered the following graphics systems."
238         << G4endl;
239    PrintAvailableGraphicsSystems ();
240    G4cout << G4endl;
241  }
242
243  // Make top level directory...
244  G4UIcommand* directory;
245  directory = new G4UIdirectory ("/vis/");
246  directory -> SetGuidance ("Visualization commands.");
247  fDirectoryList.push_back (directory);
248
249  // ... and make command directory for commands instantiated in the
250  // modeling subcategory...
251  directory = new G4UIdirectory ("/vis/modeling/");
252  directory -> SetGuidance ("Modeling commands.");
253  fDirectoryList.push_back (directory);
254  directory = new G4UIdirectory ("/vis/modeling/trajectories/");
255  directory -> SetGuidance ("Trajectory model commands.");
256  fDirectoryList.push_back (directory);
257  directory = new G4UIdirectory ("/vis/modeling/trajectories/create/");
258  directory -> SetGuidance ("Create trajectory models and messengers.");
259  fDirectoryList.push_back (directory);
260
261  // Filtering command directory
262  directory = new G4UIdirectory ("/vis/filtering/");
263  directory -> SetGuidance ("Filtering commands.");
264  fDirectoryList.push_back (directory);
265  directory = new G4UIdirectory ("/vis/filtering/trajectories/");
266  directory -> SetGuidance ("Trajectory filtering commands.");
267  fDirectoryList.push_back (directory);
268  directory = new G4UIdirectory ("/vis/filtering/trajectories/create/");
269  directory -> SetGuidance ("Create trajectory filters and messengers.");
270  fDirectoryList.push_back (directory);
271  directory = new G4UIdirectory ("/vis/filtering/hits/");
272  directory -> SetGuidance ("Hit filtering commands.");
273  fDirectoryList.push_back (directory);
274  directory = new G4UIdirectory ("/vis/filtering/hits/create/");
275  directory -> SetGuidance ("Create hit filters and messengers.");
276  fDirectoryList.push_back (directory);
277
278  RegisterMessengers ();
279
280  if (fVerbosity >= startup) {
281    G4cout << "Registering model factories..." << G4endl;
282  }
283
284  RegisterModelFactories();
285
286  if (fVerbosity >= startup) {
287    G4cout <<
288      "\nYou have successfully registered the following model factories."
289         << G4endl;
290    PrintAvailableModels (fVerbosity);
291    G4cout << G4endl;
292  }
293
294  fInitialised = true;
295}
296
297void G4VisManager::Enable() {
298  if (IsValidView ()) {
299    if (fVerbosity >= confirmations) {
300      G4cout << "G4VisManager::Enable: visualization enabled." << G4endl;
301    }
302  }
303  else {
304    if (fVerbosity >= warnings) {
305      G4cout <<
306        "G4VisManager::Enable: WARNING: visualization remains disabled for"
307        "\n  above reasons.  Rectifying with valid vis commands will"
308        "\n  automatically enable."
309             << G4endl;
310    }
311  }
312}
313
314void G4VisManager::Disable() {
315  SetConcreteInstance(0);
316  if (fVerbosity >= confirmations) {
317    G4cout <<
318      "G4VisManager::Disable: visualization disabled."
319      "\n  The pointer returned by GetConcreteInstance will be zero."
320      "\n  Note that it will become enabled after some valid vis commands."
321           << G4endl;
322  }
323}
324
325const G4GraphicsSystemList& G4VisManager::GetAvailableGraphicsSystems () {
326  G4int nSystems = fAvailableGraphicsSystems.size ();
327  if (nSystems == 0) {
328    if (fVerbosity >= warnings) {
329      G4cout << "G4VisManager::GetAvailableGraphicsSystems: WARNING: no"
330        "\n graphics system available!"
331        "\n  1) Did you have environment variables G4VIS_BUILD_xxxx_DRIVER set"
332        "\n     when you compiled/built the visualization code?"
333        "\n  2) Did you instantiate your own Visualization Manager and forget"
334        "\n     to implement RegisterGraphicsSystems correctly?"
335        "\n  3) You can register your own graphics system, e.g.,"
336        "\n     visManager->RegisterGraphicsSystem(new MyGraphicsSystem);)"
337        "\n     after instantiating your vis manager and before"
338        "\n     visManager->Initialize()."
339             << G4endl;
340    }
341  }
342  return fAvailableGraphicsSystems;
343}
344
345G4bool G4VisManager::RegisterGraphicsSystem (G4VGraphicsSystem* pSystem) {
346  G4bool happy = true;
347  if (pSystem) {
348    fAvailableGraphicsSystems.push_back (pSystem);
349    if (fVerbosity >= confirmations) {
350      G4cout << "G4VisManager::RegisterGraphicsSystem: "
351             << pSystem -> GetName ();
352      if (pSystem -> GetNickname () != "") {
353        G4cout << " (" << pSystem -> GetNickname () << ")";
354      }
355      G4cout << " registered." << G4endl;
356    }
357  }
358  else {
359    if (fVerbosity >= errors) {
360      G4cout << "G4VisManager::RegisterGraphicsSystem: null pointer!"
361             << G4endl;
362    }
363    happy=false;
364  }
365  return happy;
366}
367
368const G4VTrajectoryModel*
369G4VisManager::CurrentTrajDrawModel() const
370{
371  assert (0 != fpTrajDrawModelMgr);
372
373  const G4VTrajectoryModel* model = fpTrajDrawModelMgr->Current();
374
375  if (0 == model) {
376    // No model was registered with the trajectory model manager.
377    // Use G4TrajectoryDrawByCharge as a default.
378    fpTrajDrawModelMgr->Register(new G4TrajectoryDrawByCharge("AutoGenerated"));
379
380    if (fVerbosity >= warnings) {
381      G4cout<<"G4VisManager: Using G4TrajectoryDrawByCharge as default trajectory model."<<G4endl;
382      G4cout<<"See commands in /vis/modeling/trajectories/ for other options."<<G4endl;
383    }
384  }
385
386  model = fpTrajDrawModelMgr->Current();
387  assert (0 != model); // Should definitely exist now
388
389  return model;
390}
391
392void G4VisManager::RegisterModel(G4VTrajectoryModel* model)
393{
394  fpTrajDrawModelMgr->Register(model);
395}
396
397void
398G4VisManager::RegisterModelFactory(G4TrajDrawModelFactory* factory)
399{
400  fpTrajDrawModelMgr->Register(factory);
401}
402
403void G4VisManager::RegisterModel(G4VFilter<G4VTrajectory>* model)
404{
405  fpTrajFilterMgr->Register(model);
406}
407
408void
409G4VisManager::RegisterModelFactory(G4TrajFilterFactory* factory)
410{
411  fpTrajFilterMgr->Register(factory);
412}
413
414void G4VisManager::RegisterModel(G4VFilter<G4VHit>* model)
415{
416  fpHitFilterMgr->Register(model);
417}
418
419void
420G4VisManager::RegisterModelFactory(G4HitFilterFactory* factory)
421{
422  fpHitFilterMgr->Register(factory);
423}
424
425void G4VisManager::SelectTrajectoryModel(const G4String& model)
426{
427   fpTrajDrawModelMgr->SetCurrent(model);
428}
429
430void G4VisManager::Draw (const G4Circle& circle,
431                         const G4Transform3D& objectTransform) {
432  if (IsValidView ()) {
433    ClearTransientStoreIfMarked();
434    fpSceneHandler -> BeginPrimitives (objectTransform);
435    fpSceneHandler -> AddPrimitive (circle);
436    fpSceneHandler -> EndPrimitives ();
437  }
438}
439
440void G4VisManager::Draw (const G4NURBS& nurbs,
441                         const G4Transform3D& objectTransform) {
442  if (IsValidView ()) {
443    ClearTransientStoreIfMarked();
444    fpSceneHandler -> BeginPrimitives (objectTransform);
445    fpSceneHandler -> AddPrimitive (nurbs);
446    fpSceneHandler -> EndPrimitives ();
447  }
448}
449
450void G4VisManager::Draw (const G4Polyhedron& polyhedron,
451                         const G4Transform3D& objectTransform) {
452  if (IsValidView ()) {
453    ClearTransientStoreIfMarked();
454    fpSceneHandler -> BeginPrimitives (objectTransform);
455    fpSceneHandler -> AddPrimitive (polyhedron);
456    fpSceneHandler -> EndPrimitives ();
457  }
458}
459
460void G4VisManager::Draw (const G4Polyline& line,
461                         const G4Transform3D& objectTransform) {
462  if (IsValidView ()) {
463    ClearTransientStoreIfMarked();
464    fpSceneHandler -> BeginPrimitives (objectTransform);
465    fpSceneHandler -> AddPrimitive (line);
466    fpSceneHandler -> EndPrimitives ();
467  }
468}
469
470void G4VisManager::Draw (const G4Polymarker& polymarker,
471                         const G4Transform3D& objectTransform) {
472  if (IsValidView ()) {
473    ClearTransientStoreIfMarked();
474    fpSceneHandler -> BeginPrimitives (objectTransform);
475    fpSceneHandler -> AddPrimitive (polymarker);
476    fpSceneHandler -> EndPrimitives ();
477  }
478}
479
480void G4VisManager::Draw (const G4Scale& scale,
481                         const G4Transform3D& objectTransform) {
482  if (IsValidView ()) {
483    ClearTransientStoreIfMarked();
484    fpSceneHandler -> BeginPrimitives (objectTransform);
485    fpSceneHandler -> AddPrimitive (scale);
486    fpSceneHandler -> EndPrimitives ();
487  }
488}
489
490void G4VisManager::Draw (const G4Square& square,
491                         const G4Transform3D& objectTransform) {
492  if (IsValidView ()) {
493    ClearTransientStoreIfMarked();
494    fpSceneHandler -> BeginPrimitives (objectTransform);
495    fpSceneHandler -> AddPrimitive (square);
496    fpSceneHandler -> EndPrimitives ();
497  }
498}
499
500void G4VisManager::Draw (const G4Text& text,
501                         const G4Transform3D& objectTransform) {
502  if (IsValidView ()) {
503    ClearTransientStoreIfMarked();
504    fpSceneHandler -> BeginPrimitives (objectTransform);
505    fpSceneHandler -> AddPrimitive (text);
506    fpSceneHandler -> EndPrimitives ();
507  }
508}
509
510void G4VisManager::Draw2D (const G4Circle& circle,
511                           const G4Transform3D& objectTransform)
512{
513  if (IsValidView()) {
514    ClearTransientStoreIfMarked();
515    fpSceneHandler -> BeginPrimitives2D(objectTransform);
516    fpSceneHandler -> AddPrimitive(circle);
517    fpSceneHandler -> EndPrimitives2D();
518  }
519}
520
521void G4VisManager::Draw2D (const G4NURBS& nurbs,
522                           const G4Transform3D& objectTransform)
523{
524  if (IsValidView()) {
525    ClearTransientStoreIfMarked();
526    fpSceneHandler -> BeginPrimitives2D(objectTransform);
527    fpSceneHandler -> AddPrimitive(nurbs);
528    fpSceneHandler -> EndPrimitives2D();
529  }
530}
531
532void G4VisManager::Draw2D (const G4Polyhedron& polyhedron,
533                           const G4Transform3D& objectTransform)
534{
535  if (IsValidView()) {
536    ClearTransientStoreIfMarked();
537    fpSceneHandler -> BeginPrimitives2D(objectTransform);
538    fpSceneHandler -> AddPrimitive(polyhedron);
539    fpSceneHandler -> EndPrimitives2D();
540  }
541}
542
543void G4VisManager::Draw2D (const G4Polyline& line,
544                           const G4Transform3D& objectTransform)
545{
546  if (IsValidView()) {
547    ClearTransientStoreIfMarked();
548    fpSceneHandler -> BeginPrimitives2D(objectTransform);
549    fpSceneHandler -> AddPrimitive(line);
550    fpSceneHandler -> EndPrimitives2D();
551  }
552}
553
554void G4VisManager::Draw2D (const G4Polymarker& polymarker,
555                           const G4Transform3D& objectTransform)
556{
557  if (IsValidView()) {
558    ClearTransientStoreIfMarked();
559    fpSceneHandler -> BeginPrimitives2D(objectTransform);
560    fpSceneHandler -> AddPrimitive(polymarker);
561    fpSceneHandler -> EndPrimitives2D();
562  }
563}
564
565void G4VisManager::Draw2D (const G4Square& square,
566                           const G4Transform3D& objectTransform)
567{
568  if (IsValidView()) {
569    ClearTransientStoreIfMarked();
570    fpSceneHandler -> BeginPrimitives2D(objectTransform);
571    fpSceneHandler -> AddPrimitive(square);
572    fpSceneHandler -> EndPrimitives2D();
573  }
574}
575
576void G4VisManager::Draw2D (const G4Text& text,
577                           const G4Transform3D& objectTransform)
578{
579  if (IsValidView()) {
580    ClearTransientStoreIfMarked();
581    fpSceneHandler -> BeginPrimitives2D(objectTransform);
582    fpSceneHandler -> AddPrimitive(text);
583    fpSceneHandler -> EndPrimitives2D();
584  }
585}
586
587void G4VisManager::Draw (const G4VHit& hit) {
588  if (IsValidView ()) {
589    ClearTransientStoreIfMarked();
590    fpSceneHandler -> AddCompound (hit);
591  }
592}
593
594void G4VisManager::Draw (const G4VTrajectory& traj,
595                         G4int i_mode) {
596  if (IsValidView ()) {
597    ClearTransientStoreIfMarked();
598    fpSceneHandler -> SetModel (&dummyTrajectoriesModel);
599    dummyTrajectoriesModel.SetDrawingMode(i_mode);
600    fpSceneHandler -> AddCompound (traj);
601  }
602}
603
604void G4VisManager::Draw (const G4LogicalVolume& logicalVol,
605                         const G4VisAttributes& attribs,
606                         const G4Transform3D& objectTransform) {
607  // Find corresponding solid.
608  G4VSolid* pSol = logicalVol.GetSolid ();
609  Draw (*pSol, attribs, objectTransform);
610}
611
612void G4VisManager::Draw (const G4VSolid& solid,
613                         const G4VisAttributes& attribs,
614                         const G4Transform3D& objectTransform) {
615  if (IsValidView ()) {
616    ClearTransientStoreIfMarked();
617    fpSceneHandler -> PreAddSolid (objectTransform, attribs);
618    solid.DescribeYourselfTo (*fpSceneHandler);
619    fpSceneHandler -> PostAddSolid ();
620  }
621}
622
623void G4VisManager::Draw (const G4VPhysicalVolume& physicalVol,
624                         const G4VisAttributes& attribs,
625                         const G4Transform3D& objectTransform) {
626  // Find corresponding logical volume and solid.
627  G4LogicalVolume* pLV  = physicalVol.GetLogicalVolume ();
628  G4VSolid*        pSol = pLV -> GetSolid ();
629  Draw (*pSol, attribs, objectTransform);
630}
631
632void G4VisManager::CreateSceneHandler (G4String name) {
633  if (!fInitialised) Initialise ();
634  if (fpGraphicsSystem) {
635    G4VSceneHandler* pSceneHandler =
636      fpGraphicsSystem -> CreateSceneHandler (name);
637    if (pSceneHandler) {
638      fAvailableSceneHandlers.push_back (pSceneHandler);
639      fpSceneHandler = pSceneHandler;                         // Make current.
640    }
641    else {
642      if(fVerbosity >= errors) {
643        G4cout << "ERROR in G4VisManager::CreateSceneHandler during "
644               << fpGraphicsSystem -> GetName ()
645               << " scene creation.\n  No action taken."
646               << G4endl;
647      }
648    }
649  }
650  else PrintInvalidPointers ();
651}
652
653void G4VisManager::CreateViewer (G4String name) {
654#ifdef G4DEBUG
655  printf("G4VisManager::CreateViewer TOP\n");
656#endif
657
658  if (!fInitialised) Initialise ();
659#ifdef G4DEBUG
660  printf("G4VisManager::CreateViewer 2\n");
661#endif
662
663  if (fpSceneHandler) {
664#ifdef G4DEBUG
665    printf("G4VisManager::CreateViewer 3\n");
666#endif
667    G4VViewer* p = fpGraphicsSystem -> CreateViewer (*fpSceneHandler, name);
668#ifdef G4DEBUG
669    printf("G4VisManager::CreateViewer 4\n");
670#endif
671    if (!p) {
672#ifdef G4DEBUG
673      printf("G4VisManager::CreateViewer !p\n");
674#endif
675      if (fVerbosity >= errors) {
676        G4cout << "ERROR in G4VisManager::CreateViewer during "
677               << fpGraphicsSystem -> GetName ()
678               << " viewer creation.\n  No action taken."
679               << G4endl;
680      }
681    } else {
682#ifdef G4DEBUG
683    printf("G4VisManager::CreateViewer 5\n");
684#endif
685      p -> Initialise ();
686#ifdef G4DEBUG
687    printf("G4VisManager::CreateViewer 6\n");
688#endif
689      if (p -> GetViewId() < 0) {
690#ifdef G4DEBUG
691        printf("G4VisManager::CreateViewer Id <0\n");
692#endif
693        if (fVerbosity >= errors) {
694        G4cout << "ERROR in G4VisManager::CreateViewer during "
695               << fpGraphicsSystem -> GetName ()
696               << " viewer initialisation.\n  No action taken."
697               << G4endl;
698        }
699      } else {
700#ifdef G4DEBUG
701    printf("G4VisManager::CreateViewer 7\n");
702#endif
703        fpViewer = p;                             // Make current.
704#ifdef G4DEBUG
705    printf("G4VisManager::CreateViewer 8\n");
706#endif
707        fpSceneHandler -> AddViewerToList (fpViewer);
708#ifdef G4DEBUG
709  printf("G4VisManager::CreateViewer 9\n");
710#endif
711        fpSceneHandler -> SetCurrentViewer (fpViewer);
712
713        if (fVerbosity >= confirmations) {
714          G4cout << "G4VisManager::CreateViewer: new viewer created."
715                 << G4endl;
716        }
717
718#ifdef G4DEBUG
719  printf("G4VisManager::CreateViewer 10 Read parameters\n");
720#endif
721        const G4ViewParameters& vp = fpViewer->GetViewParameters();
722#ifdef G4DEBUG
723  printf("G4VisManager::CreateViewer 11 END of read Parameters\n");
724#endif
725        if (fVerbosity >= parameters) {
726          G4cout << " view parameters are:\n  " << vp << G4endl;
727        }
728
729        if (vp.IsCulling () && vp.IsCullingInvisible ()) {
730          static G4bool warned = false;
731          if (fVerbosity >= confirmations) {
732            if (!warned) {
733              G4cout <<
734  "NOTE: objects with visibility flag set to \"false\""
735  " will not be drawn!"
736  "\n  \"/vis/viewer/set/culling global false\" to Draw such objects."
737  "\n  Also see other \"/vis/viewer/set\" commands."
738                     << G4endl;
739              warned = true;
740            }
741          }
742        }
743        if (vp.IsCullingCovered ()) {
744          static G4bool warned = false;
745          if (fVerbosity >= warnings) {
746            if (!warned) {
747              G4cout <<
748  "WARNING: covered objects in solid mode will not be rendered!"
749  "\n  \"/vis/viewer/set/culling coveredDaughters false\" to reverse this."
750  "\n  Also see other \"/vis/viewer/set\" commands."
751                     << G4endl;
752              warned = true;
753            }
754          }
755        }
756      }
757    }
758  }
759  else PrintInvalidPointers ();
760#ifdef G4DEBUG
761  printf("G4VisManager::END OF CreateViewer\n");
762#endif
763}
764
765void G4VisManager::GeometryHasChanged () {
766  if (fVerbosity >= confirmations) {
767    G4cout << "G4VisManager::GeometryHasChanged() called." << G4endl;
768  }
769
770  // Change the world...
771  G4VPhysicalVolume* pWorld =
772    G4TransportationManager::GetTransportationManager ()
773    -> GetNavigatorForTracking () -> GetWorldVolume ();
774  if (!pWorld) {
775    if (fVerbosity >= warnings) {
776      G4cout << "WARNING: There is no world volume!" << G4endl;
777    }
778  }
779
780  // Check scenes.
781  G4SceneList& sceneList = fSceneList;
782  G4int iScene, nScenes = sceneList.size ();
783  for (iScene = 0; iScene < nScenes; iScene++) {
784    G4Scene* pScene = sceneList [iScene];
785    std::vector<G4VModel*>& modelList = pScene -> SetRunDurationModelList ();
786
787    if (modelList.size ()) {
788      G4bool modelInvalid;
789      do {  // Remove, if required, one at a time.
790        modelInvalid = false;
791        std::vector<G4VModel*>::iterator iterModel;
792        for (iterModel = modelList.begin();
793             iterModel != modelList.end();
794             ++iterModel) {
795          modelInvalid = !((*iterModel) -> Validate (fVerbosity >= warnings));
796          if (modelInvalid) {
797            // Model invalid - remove and break.
798            if (fVerbosity >= warnings) {
799              G4cout << "WARNING: Model \""
800                     << (*iterModel) -> GetGlobalDescription ()
801                     <<
802                "\" is no longer valid - being removed\n  from scene \""
803                     << pScene -> GetName () << "\""
804                     << G4endl;
805            }
806            modelList.erase (iterModel);
807            break;
808          }
809        }
810      } while (modelInvalid);
811
812      if (modelList.size () == 0) {
813        if (fVerbosity >= warnings) {
814          G4cout << "WARNING: No models left in this scene \""
815                 << pScene -> GetName ()
816                 << "\"."
817                 << G4endl;
818        }
819      }
820      else {
821        pScene->CalculateExtent();
822        G4UImanager::GetUIpointer () ->
823          ApplyCommand (G4String("/vis/scene/notifyHandlers " + pScene->GetName()));
824      }
825    }
826  }
827
828  // Check the manager's current scene...
829  if (fpScene && fpScene -> GetRunDurationModelList ().size () == 0) {
830    if (fVerbosity >= warnings) {
831      G4cout << "WARNING: The current scene \""
832             << fpScene -> GetName ()
833             << "\" has no models."
834             << G4endl;
835    }
836  }
837
838}
839
840G4bool G4VisManager::FilterTrajectory(const G4VTrajectory& trajectory)
841{
842  return fpTrajFilterMgr->Accept(trajectory);
843}   
844
845G4bool G4VisManager::FilterHit(const G4VHit& hit)
846{
847  return fpHitFilterMgr->Accept(hit);
848}   
849
850void G4VisManager::DispatchToModel(const G4VTrajectory& trajectory, G4int i_mode)
851{
852  G4bool visible(true);
853
854  // See if trajectory passes filter
855  G4bool passed = FilterTrajectory(trajectory);
856
857  if (!passed) {
858    // Draw invisible trajectory if trajectory failed filter and
859    // are filtering in soft mode
860    if (fpTrajFilterMgr->GetMode() == FilterMode::Soft) visible = false;
861    else {return;}
862  }
863
864  // Go on to draw trajectory
865  assert (0 != fpTrajDrawModelMgr);
866
867  const G4VTrajectoryModel* model = CurrentTrajDrawModel();
868
869  assert (0 != model); // Should exist
870
871  model->Draw(trajectory, i_mode, visible);
872}
873
874void G4VisManager::SetUserAction
875(G4VUserVisAction* pVisAction,
876 const G4VisExtent& extent) {
877  fpUserVisAction = pVisAction;
878  fUserVisActionExtent = extent;
879  if (extent.GetExtentRadius() <= 0.) {
880    if (fVerbosity >= warnings) {
881      G4cout <<
882        "WARNING: No extent set for user vis action.  (You may"
883        "\n  set it later when adding with /vis/scene/add/userAction.)"
884             << G4endl;
885    }
886  }
887}
888
889void G4VisManager::SetCurrentScene (G4Scene* pScene) {
890  if (pScene != fpScene) {
891    // A change of scene.  Therefore reset transients drawn flags.  All
892    // memory of previous transient proceessing thereby erased...
893    ResetTransientsDrawnFlags();
894  }
895  fpScene = pScene;
896}
897
898void G4VisManager::SetCurrentGraphicsSystem (G4VGraphicsSystem* pSystem) {
899  fpGraphicsSystem = pSystem;
900  if (fVerbosity >= confirmations) {
901    G4cout << "G4VisManager::SetCurrentGraphicsSystem: system now "
902           << pSystem -> GetName () << G4endl;
903  }
904  // If current scene handler is of same graphics system, leave unchanged.
905  // Else find the most recent scene handler of same graphics system.
906  // Or clear pointers.
907  if (!(fpSceneHandler && fpSceneHandler -> GetGraphicsSystem () == pSystem)) {
908    const G4SceneHandlerList& sceneHandlerList = fAvailableSceneHandlers;
909    G4int nSH = sceneHandlerList.size ();  // No. of scene handlers.
910    G4int iSH;
911    for (iSH = nSH - 1; iSH >= 0; iSH--) {
912      if (sceneHandlerList [iSH] -> GetGraphicsSystem () == pSystem) break;
913    }
914    if (iSH >= 0) {
915      fpSceneHandler = sceneHandlerList [iSH];
916      if (fVerbosity >= confirmations) {
917        G4cout << "  Scene Handler now "
918               << fpSceneHandler -> GetName () << G4endl;
919      }
920      if (fpScene != fpSceneHandler -> GetScene ()) {
921        fpScene = fpSceneHandler -> GetScene ();
922        if (fVerbosity >= confirmations) {
923          G4cout << "  Scene now \""
924                 << fpScene -> GetName () << "\"" << G4endl;
925        }
926      }
927      const G4ViewerList& viewerList = fpSceneHandler -> GetViewerList ();
928      if (viewerList.size ()) {
929        fpViewer = viewerList [0];
930        if (fVerbosity >= confirmations) {
931          G4cout << "  Viewer now " << fpViewer -> GetName () << G4endl;
932        }
933      }
934      else {
935        fpViewer = 0;
936      }
937    }
938    else {
939      fpSceneHandler = 0;
940      fpViewer = 0;
941    }
942  }
943}
944
945void G4VisManager::SetCurrentSceneHandler (G4VSceneHandler* pSceneHandler) {
946  fpSceneHandler = pSceneHandler;
947  if (fVerbosity >= confirmations) {
948    G4cout << "G4VisManager::SetCurrentSceneHandler: scene handler now \""
949           << pSceneHandler -> GetName () << "\"" << G4endl;
950  }
951  if (fpScene != fpSceneHandler -> GetScene ()) {
952    fpScene = fpSceneHandler -> GetScene ();
953    if (fVerbosity >= confirmations) {
954      G4cout << "  Scene now \""
955             << fpScene -> GetName () << "\"" << G4endl;
956    }
957  }
958  if (fpGraphicsSystem != pSceneHandler -> GetGraphicsSystem ()) {
959    fpGraphicsSystem = pSceneHandler -> GetGraphicsSystem ();
960    if (fVerbosity >= confirmations) {
961      G4cout << "  Graphics system now \""
962             << fpGraphicsSystem -> GetName () << "\"" << G4endl;
963    }
964  }
965  const G4ViewerList& viewerList = fpSceneHandler -> GetViewerList ();
966  G4int nViewers = viewerList.size ();
967  if (nViewers) {
968    G4int iViewer;
969    for (iViewer = 0; iViewer < nViewers; iViewer++) {
970      if (fpViewer == viewerList [iViewer]) break;
971    }
972    if (iViewer >= nViewers) {
973      fpViewer = viewerList [0];
974      if (fVerbosity >= confirmations) {
975        G4cout << "  Viewer now \"" << fpViewer -> GetName () << "\""
976               << G4endl;
977      }
978    }
979    IsValidView ();  // Checks.
980  }
981  else {
982    fpViewer = 0;
983    if (fVerbosity >= warnings) {
984      G4cout <<
985        "WARNING: No viewers for this scene handler - please create one."
986             << G4endl;
987    }
988  }
989}
990
991void G4VisManager::SetCurrentViewer (G4VViewer* pViewer) {
992  fpViewer  = pViewer;
993  if (fVerbosity >= confirmations) {
994    G4cout << "G4VisManager::SetCurrentViewer: viewer now "
995           << pViewer -> GetName ()
996           << G4endl;
997  }
998  fpSceneHandler = fpViewer -> GetSceneHandler ();
999  fpSceneHandler -> SetCurrentViewer (pViewer);
1000  fpScene = fpSceneHandler -> GetScene ();
1001  fpGraphicsSystem = fpSceneHandler -> GetGraphicsSystem ();
1002  IsValidView ();  // Checks.
1003}
1004
1005void G4VisManager::RegisterMessengers () {
1006
1007  // Instantiate individual messengers/commands (often - but not
1008  // always - one command per messenger).
1009
1010  G4VVisCommand::SetVisManager (this);  // Sets shared pointer to vis manager.
1011
1012  G4UIcommand* directory;
1013
1014  // Top level basic commands...
1015  RegisterMessenger(new G4VisCommandAbortReviewKeptEvents);
1016  RegisterMessenger(new G4VisCommandEnable);
1017  RegisterMessenger(new G4VisCommandList);
1018  RegisterMessenger(new G4VisCommandVerbose);
1019
1020  // Other top level commands...
1021  RegisterMessenger(new G4VisCommandReviewKeptEvents);
1022
1023  // Compound commands...
1024  RegisterMessenger(new G4VisCommandDrawTree);
1025  RegisterMessenger(new G4VisCommandDrawView);
1026  RegisterMessenger(new G4VisCommandDrawVolume);
1027  RegisterMessenger(new G4VisCommandOpen);
1028  RegisterMessenger(new G4VisCommandSpecify);
1029
1030  directory = new G4UIdirectory ("/vis/geometry/");
1031  directory -> SetGuidance("Operations on vis attributes of Geant4 geometry.");
1032  fDirectoryList.push_back (directory);
1033  RegisterMessenger(new G4VisCommandGeometryList);
1034  RegisterMessenger(new G4VisCommandGeometryRestore);
1035
1036  directory = new G4UIdirectory ("/vis/geometry/set/");
1037  directory -> SetGuidance("Set vis attributes of Geant4 geometry.");
1038  fDirectoryList.push_back (directory);
1039  RegisterMessenger(new G4VisCommandGeometrySetColour);
1040  RegisterMessenger(new G4VisCommandGeometrySetDaughtersInvisible);
1041  RegisterMessenger(new G4VisCommandGeometrySetLineStyle);
1042  RegisterMessenger(new G4VisCommandGeometrySetLineWidth);
1043  RegisterMessenger(new G4VisCommandGeometrySetForceAuxEdgeVisible);
1044  RegisterMessenger(new G4VisCommandGeometrySetForceLineSegmentsPerCircle);
1045  RegisterMessenger(new G4VisCommandGeometrySetForceSolid);
1046  RegisterMessenger(new G4VisCommandGeometrySetForceWireframe);
1047  RegisterMessenger(new G4VisCommandGeometrySetVisibility);
1048
1049  directory = new G4UIdirectory ("/vis/scene/");
1050  directory -> SetGuidance ("Operations on Geant4 scenes.");
1051  fDirectoryList.push_back (directory);
1052  RegisterMessenger(new G4VisCommandSceneCreate);
1053  RegisterMessenger(new G4VisCommandSceneEndOfEventAction);
1054  RegisterMessenger(new G4VisCommandSceneEndOfRunAction);
1055  RegisterMessenger(new G4VisCommandSceneList);
1056  RegisterMessenger(new G4VisCommandSceneNotifyHandlers);
1057  RegisterMessenger(new G4VisCommandSceneSelect);
1058
1059  directory = new G4UIdirectory ("/vis/scene/add/");
1060  directory -> SetGuidance ("Add model to current scene.");
1061  fDirectoryList.push_back (directory);
1062  RegisterMessenger(new G4VisCommandSceneAddAxes);
1063  RegisterMessenger(new G4VisCommandSceneAddEventID);
1064  RegisterMessenger(new G4VisCommandSceneAddGhosts);
1065  RegisterMessenger(new G4VisCommandSceneAddHits);
1066  RegisterMessenger(new G4VisCommandSceneAddLogicalVolume);
1067  RegisterMessenger(new G4VisCommandSceneAddLogo);
1068  RegisterMessenger(new G4VisCommandSceneAddScale);
1069  RegisterMessenger(new G4VisCommandSceneAddText);
1070  RegisterMessenger(new G4VisCommandSceneAddTrajectories);
1071  RegisterMessenger(new G4VisCommandSceneAddUserAction);
1072  RegisterMessenger(new G4VisCommandSceneAddVolume);
1073
1074  directory = new G4UIdirectory ("/vis/sceneHandler/");
1075  directory -> SetGuidance ("Operations on Geant4 scene handlers.");
1076  fDirectoryList.push_back (directory);
1077  RegisterMessenger(new G4VisCommandSceneHandlerAttach);
1078  RegisterMessenger(new G4VisCommandSceneHandlerCreate);
1079  RegisterMessenger(new G4VisCommandSceneHandlerList);
1080  RegisterMessenger(new G4VisCommandSceneHandlerSelect);
1081
1082  directory = new G4UIdirectory ("/vis/viewer/");
1083  directory -> SetGuidance ("Operations on Geant4 viewers.");
1084  fDirectoryList.push_back (directory);
1085  RegisterMessenger(new G4VisCommandViewerAddCutawayPlane);
1086  RegisterMessenger(new G4VisCommandViewerChangeCutawayPlane);
1087  RegisterMessenger(new G4VisCommandViewerClear);
1088  RegisterMessenger(new G4VisCommandViewerClearCutawayPlanes);
1089  RegisterMessenger(new G4VisCommandViewerClearTransients);
1090  RegisterMessenger(new G4VisCommandViewerClone);
1091  RegisterMessenger(new G4VisCommandViewerCreate);
1092  RegisterMessenger(new G4VisCommandViewerDolly);
1093  RegisterMessenger(new G4VisCommandViewerFlush);
1094  RegisterMessenger(new G4VisCommandViewerList);
1095  RegisterMessenger(new G4VisCommandViewerPan);
1096  RegisterMessenger(new G4VisCommandViewerRebuild);
1097  RegisterMessenger(new G4VisCommandViewerRefresh);
1098  RegisterMessenger(new G4VisCommandViewerReset);
1099  RegisterMessenger(new G4VisCommandViewerScale);
1100  RegisterMessenger(new G4VisCommandViewerSelect);
1101  RegisterMessenger(new G4VisCommandViewerUpdate);
1102  RegisterMessenger(new G4VisCommandViewerZoom);
1103
1104  directory = new G4UIdirectory ("/vis/viewer/set/");
1105  directory -> SetGuidance ("Set view parameters of current viewer.");
1106  fDirectoryList.push_back (directory);
1107  RegisterMessenger(new G4VisCommandsViewerSet);
1108
1109  // List manager commands
1110  RegisterMessenger(new G4VisCommandListManagerList< G4VisModelManager<G4VTrajectoryModel> >
1111                    (fpTrajDrawModelMgr, fpTrajDrawModelMgr->Placement()));
1112  RegisterMessenger(new G4VisCommandListManagerSelect< G4VisModelManager<G4VTrajectoryModel> >
1113                    (fpTrajDrawModelMgr, fpTrajDrawModelMgr->Placement())); 
1114
1115  // Trajectory filter manager commands
1116  RegisterMessenger(new G4VisCommandListManagerList< G4VisFilterManager<G4VTrajectory> >
1117                    (fpTrajFilterMgr, fpTrajFilterMgr->Placement()));
1118  RegisterMessenger(new G4VisCommandManagerMode< G4VisFilterManager<G4VTrajectory> >
1119                    (fpTrajFilterMgr, fpTrajFilterMgr->Placement()));
1120
1121  // Hit filter manager commands
1122  RegisterMessenger(new G4VisCommandListManagerList< G4VisFilterManager<G4VHit> >
1123                    (fpHitFilterMgr, fpHitFilterMgr->Placement()));
1124  RegisterMessenger(new G4VisCommandManagerMode< G4VisFilterManager<G4VHit> >
1125                    (fpHitFilterMgr, fpHitFilterMgr->Placement()));
1126}
1127
1128void G4VisManager::PrintAvailableGraphicsSystems () const {
1129  G4int nSystems = fAvailableGraphicsSystems.size ();
1130  G4cout << "Current available graphics systems are:";
1131  if (nSystems) {
1132    for (int i = 0; i < nSystems; i++) {
1133      const G4VGraphicsSystem* pSystem = fAvailableGraphicsSystems [i];
1134      G4cout << "\n  " << pSystem -> GetName ();
1135      if (pSystem -> GetNickname () != "") {
1136        G4cout << " (" << pSystem -> GetNickname () << ")";
1137      }
1138    }
1139  }
1140  else {
1141    G4cout << "\n  NONE!!!  None registered - yet!  Mmmmm!";
1142  }
1143  G4cout << G4endl;
1144}
1145
1146void G4VisManager::PrintAvailableModels (Verbosity verbosity) const
1147{
1148  {
1149    //fpTrajDrawModelMgr->Print(G4cout);
1150    G4cout << "Registered model factories:" << G4endl;
1151    const std::vector<G4VModelFactory<G4VTrajectoryModel>*>& factoryList =
1152      fpTrajDrawModelMgr->FactoryList();
1153    if (factoryList.empty()) G4cout << "  None" << G4endl;
1154    else {
1155      std::vector<G4VModelFactory<G4VTrajectoryModel>*>::const_iterator i;
1156      for (i = factoryList.begin(); i != factoryList.end(); ++i)
1157        (*i)->Print(G4cout);
1158    }
1159    const G4VisListManager<G4VTrajectoryModel>* listManager =
1160      fpTrajDrawModelMgr->ListManager();
1161    const std::map<G4String, G4VTrajectoryModel*>& modelMap =
1162      listManager->Map();
1163    if (!modelMap.empty()) {
1164      G4cout << "\nRegistered models:" << G4endl;
1165      std::map<G4String, G4VTrajectoryModel*>::const_iterator i;
1166      for (i = modelMap.begin(); i != modelMap.end(); ++i) {
1167        G4cout << "  " << i->second->Name();
1168        if (i->second == listManager->Current()) G4cout << " (Current)";
1169        G4cout << G4endl;
1170        if (verbosity >= parameters) i->second->Print(G4cout);
1171      }
1172    }
1173  }
1174
1175  G4cout << G4endl;
1176
1177  {
1178    //fpTrajFilterMgr->Print(G4cout);
1179    G4cout << "Registered filter factories:" << G4endl;
1180    const std::vector<G4VModelFactory<G4VFilter<G4VTrajectory> >*>&
1181      factoryList = fpTrajFilterMgr->FactoryList();
1182    if (factoryList.empty()) G4cout << "  None" << G4endl;
1183    else {
1184      std::vector<G4VModelFactory<G4VFilter<G4VTrajectory> >*>::const_iterator i;
1185      for (i = factoryList.begin(); i != factoryList.end(); ++i)
1186        (*i)->Print(G4cout);
1187    }
1188    const std::vector<G4VFilter<G4VTrajectory>*>&
1189      filterList = fpTrajFilterMgr->FilterList();
1190    if (!filterList.empty()) {
1191      G4cout << "\nRegistered filters:" << G4endl;
1192      std::vector<G4VFilter<G4VTrajectory>*>::const_iterator i;
1193      for (i = filterList.begin(); i != filterList.end(); ++i) {
1194        G4cout << "  " << (*i)->GetName() << G4endl;
1195        if (verbosity >= parameters) (*i)->PrintAll(G4cout);
1196      }
1197    }
1198  }
1199}
1200
1201void G4VisManager::PrintInvalidPointers () const {
1202  if (fVerbosity >= errors) {
1203    G4cout << "ERROR: G4VisManager::PrintInvalidPointers:";
1204    if (!fpGraphicsSystem) {
1205      G4cout << "\n null graphics system pointer.";
1206    }
1207    else {
1208      G4cout << "\n  Graphics system is " << fpGraphicsSystem -> GetName ()
1209             << " but:";
1210      if (!fpScene)
1211        G4cout <<
1212          "\n  Null scene pointer. Use \"/vis/drawVolume\" or"
1213          " \"/vis/scene/create\".";
1214      if (!fpSceneHandler)
1215        G4cout <<
1216          "\n  Null scene handler pointer. Use \"/vis/open\" or"
1217          " \"/vis/sceneHandler/create\".";
1218      if (!fpViewer )
1219        G4cout <<
1220          "\n  Null viewer pointer. Use \"/vis/viewer/create\".";
1221    }
1222    G4cout << G4endl;
1223  }
1224}
1225
1226void G4VisManager::BeginOfRun ()
1227{
1228  //G4cout << "G4VisManager::BeginOfRun" << G4endl;
1229  fKeptLastEvent = false;
1230  fEventKeepingSuspended = false;
1231  fTransientsDrawnThisRun = false;
1232  if (fpSceneHandler) fpSceneHandler->SetTransientsDrawnThisRun(false);
1233}
1234
1235void G4VisManager::BeginOfEvent ()
1236{
1237  //G4cout << "G4VisManager::BeginOfEvent" << G4endl;
1238  fTransientsDrawnThisEvent = false;
1239  if (fpSceneHandler) fpSceneHandler->SetTransientsDrawnThisEvent(false);
1240}
1241
1242void G4VisManager::EndOfEvent ()
1243{
1244  //G4cout << "G4VisManager::EndOfEvent" << G4endl;
1245
1246  // Don't call IsValidView unless there is a scene handler.  This
1247  // avoids WARNING message at end of event and run when the user has
1248  // not instantiated a scene handler, e.g., in batch mode.
1249  G4bool valid = GetConcreteInstance() && fpSceneHandler && IsValidView();
1250  if (!valid) return;
1251
1252  G4RunManager* runManager = G4RunManager::GetRunManager();
1253  const G4Run* currentRun = runManager->GetCurrentRun();
1254
1255  G4EventManager* eventManager = G4EventManager::GetEventManager();
1256  const G4Event* currentEvent = eventManager->GetConstCurrentEvent();
1257  if (!currentEvent) return;
1258
1259  ClearTransientStoreIfMarked();
1260  fpSceneHandler->DrawEvent(currentEvent);
1261
1262  G4int nEventsToBeProcessed = 0;
1263  G4int nKeptEvents = 0;
1264  G4int eventID = -2;  // (If no run manager, triggers ShowView as normal.)
1265  if (currentRun) {
1266    nEventsToBeProcessed = currentRun->GetNumberOfEventToBeProcessed();
1267    eventID = currentEvent->GetEventID();
1268    const std::vector<const G4Event*>* events =
1269      currentRun->GetEventVector();
1270    if (events) nKeptEvents = events->size();
1271  }
1272
1273  if (fpScene->GetRefreshAtEndOfEvent()) {
1274
1275    // Unless last event (in which case wait end of run)...
1276    if (eventID < nEventsToBeProcessed - 1) {
1277      fpViewer->ShowView();
1278      fpSceneHandler->SetMarkForClearingTransientStore(true);
1279    } else {  // Last event...
1280      // Keep, but only if user has not kept any...
1281      if (!nKeptEvents) {
1282        eventManager->KeepTheCurrentEvent();
1283        fKeptLastEvent = true;
1284      }
1285    }
1286
1287  } else {  //  Accumulating events...
1288
1289    G4int maxNumberOfKeptEvents = fpScene->GetMaxNumberOfKeptEvents();
1290    if (maxNumberOfKeptEvents > 0 && nKeptEvents >= maxNumberOfKeptEvents) {
1291      fEventKeepingSuspended = true;
1292      static G4bool warned = false;
1293      if (!warned) {
1294        if (fVerbosity >= warnings) {
1295          G4cout <<
1296            "WARNING: G4VisManager::EndOfEvent: Event keeping suspended."
1297            "\n  The number of events exceeds the maximum that may be kept, "
1298                 << maxNumberOfKeptEvents << '.'
1299                 << G4endl;
1300        }
1301        warned = true;
1302      }
1303    } else if (maxNumberOfKeptEvents != 0) {
1304      eventManager->KeepTheCurrentEvent();
1305    }
1306  }
1307}
1308
1309void G4VisManager::EndOfRun ()
1310{
1311  //G4cout << "G4VisManager::EndOfRun" << G4endl;
1312
1313  // Don't call IsValidView unless there is a scene handler.  This
1314  // avoids WARNING message at end of event and run when the user has
1315  // not instantiated a scene handler, e.g., in batch mode.
1316  G4bool valid = GetConcreteInstance() && fpSceneHandler && IsValidView();
1317  if (valid) {
1318    if (!fpSceneHandler->GetMarkForClearingTransientStore()) {
1319      if (fpScene->GetRefreshAtEndOfRun()) {
1320        fpViewer->ShowView();
1321        fpSceneHandler->SetMarkForClearingTransientStore(true);
1322      }
1323    }
1324
1325    if (fEventKeepingSuspended && fVerbosity >= warnings) {
1326      G4cout <<
1327        "WARNING: G4VisManager::EndOfRun: Event keeping was suspended."
1328        "\n  The number of events in the run exceeded the maximum to be kept, "
1329             << fpScene->GetMaxNumberOfKeptEvents() << '.' <<
1330        "\n  The number of events to be kept can be changed with"
1331        "\n  \"/vis/scene/endOfEventAction accumulate <N>\", where N is the"
1332        "\n  maximum number you wish to allow.  N < 0 means \"unlimited\"."
1333             << G4endl;
1334    }
1335  }
1336  fEventRefreshing = false;
1337
1338  G4RunManager* runManager = G4RunManager::GetRunManager();
1339  const G4Run* currentRun = runManager->GetCurrentRun();
1340 
1341  G4int nKeptEvents = 0;
1342  const std::vector<const G4Event*>* events =
1343    currentRun? currentRun->GetEventVector(): 0;
1344  if (events) nKeptEvents = events->size();
1345
1346  if (nKeptEvents && !fKeptLastEvent) {
1347    if (!valid && fVerbosity >= warnings) G4cout << "WARNING: ";
1348    if (fVerbosity >= warnings) {
1349      G4cout << nKeptEvents;
1350      if (nKeptEvents == 1) G4cout << " event has";
1351      else G4cout << " events have";
1352      G4cout << " been kept for refreshing and/or reviewing." << G4endl;
1353    }
1354    static G4bool warned = false;
1355    if (!valid && fVerbosity >= warnings && !warned) {
1356      G4cout <<
1357        "  Only useful if before starting the run:"
1358        "\n    a) trajectories are stored (\"/vis/scene/add/trajectories [smooth|rich]\"), or"
1359        "\n    b) the Draw method of any hits is implemented."
1360        "\n  To view trajectories and hits:"
1361        "\n    open a viewer, draw a volume, \"/vis/scene/add/trajectories\""
1362        "\n    \"/vis/scene/add/hits\" and, possibly, \"/vis/viewer/flush\"."
1363        "\n  To see all events: \"/vis/scene/endOfEventAction accumulate\"."
1364        "\n  To see events individually: \"/vis/reviewKeptEvents\"."
1365             << G4endl;
1366      warned = true;
1367    }
1368  }
1369}
1370
1371void G4VisManager::ClearTransientStoreIfMarked(){
1372  // Assumes valid view.
1373  if (fpSceneHandler->GetMarkForClearingTransientStore()) {
1374    fpSceneHandler->SetMarkForClearingTransientStore(false);
1375    fpSceneHandler->ClearTransientStore();
1376  }
1377  // Record if transients drawn.  These local flags are only set
1378  // *after* ClearTransientStore.  In the code in G4VSceneHandler
1379  // triggered by ClearTransientStore, use these flags so that
1380  // event refreshing is not done too early.
1381  fTransientsDrawnThisEvent = fpSceneHandler->GetTransientsDrawnThisEvent();
1382  fTransientsDrawnThisRun = fpSceneHandler->GetTransientsDrawnThisRun();
1383}
1384
1385void G4VisManager::ResetTransientsDrawnFlags()
1386{
1387  fTransientsDrawnThisRun = false;
1388  fTransientsDrawnThisEvent = false;
1389  G4SceneHandlerListConstIterator i;
1390  for (i = fAvailableSceneHandlers.begin();
1391       i != fAvailableSceneHandlers.end(); ++i) {
1392    (*i)->SetTransientsDrawnThisEvent(false);
1393    (*i)->SetTransientsDrawnThisRun(false);
1394  }
1395}
1396
1397G4String G4VisManager::ViewerShortName (const G4String& viewerName) const {
1398  G4String viewerShortName (viewerName);
1399  viewerShortName = viewerShortName (0, viewerShortName.find (' '));
1400  return viewerShortName.strip ();
1401}
1402
1403G4VViewer* G4VisManager::GetViewer (const G4String& viewerName) const {
1404  G4String viewerShortName = ViewerShortName (viewerName);
1405  size_t nHandlers = fAvailableSceneHandlers.size ();
1406  size_t iHandler, iViewer;
1407  G4VViewer* viewer = 0;
1408  G4bool found = false;
1409  for (iHandler = 0; iHandler < nHandlers; iHandler++) {
1410    G4VSceneHandler* sceneHandler = fAvailableSceneHandlers [iHandler];
1411    const G4ViewerList& viewerList = sceneHandler -> GetViewerList ();
1412    for (iViewer = 0; iViewer < viewerList.size (); iViewer++) {
1413      viewer = viewerList [iViewer];
1414      if (viewerShortName == viewer -> GetShortName ()) {
1415        found = true;
1416        break;
1417      }
1418    }
1419    if (found) break;
1420  }
1421  if (found) return viewer;
1422  else return 0;
1423}
1424
1425std::vector<G4String> G4VisManager::VerbosityGuidanceStrings;
1426
1427G4String G4VisManager::VerbosityString(Verbosity verbosity) {
1428  G4String s;
1429  switch (verbosity) {
1430  case         quiet: s = "quiet (0)"; break;
1431  case       startup: s = "startup (1)"; break;
1432  case        errors: s = "errors (2)"; break;
1433  case      warnings: s = "warnings (3)"; break;
1434  case confirmations: s = "confirmations (4)"; break;
1435  case    parameters: s = "parameters (5)"; break;
1436  case           all: s = "all (6)"; break;
1437  }
1438  return s;
1439}
1440
1441G4VisManager::Verbosity
1442G4VisManager::GetVerbosityValue(const G4String& verbosityString) {
1443  G4String s(verbosityString); s.toLower();
1444  Verbosity verbosity;
1445  if      (s(0) == 'q') verbosity = quiet;
1446  else if (s(0) == 's') verbosity = startup;
1447  else if (s(0) == 'e') verbosity = errors;
1448  else if (s(0) == 'w') verbosity = warnings;
1449  else if (s(0) == 'c') verbosity = confirmations;
1450  else if (s(0) == 'p') verbosity = parameters;
1451  else if (s(0) == 'a') verbosity = all;
1452  else {
1453    G4int intVerbosity;
1454    std::istringstream is(s);
1455    is >> intVerbosity;
1456    if (!is) {
1457      G4cout << "ERROR: G4VisManager::GetVerbosityValue: invalid verbosity \""
1458             << verbosityString << "\"";
1459      for (size_t i = 0; i < VerbosityGuidanceStrings.size(); ++i) {
1460        G4cout << '\n' << VerbosityGuidanceStrings[i];
1461      }
1462      verbosity = warnings;
1463      G4cout << "\n  Returning " << VerbosityString(verbosity)
1464             << G4endl;
1465    }
1466    else {
1467      verbosity = GetVerbosityValue(intVerbosity);
1468    }
1469  }
1470  return verbosity;
1471}
1472
1473G4VisManager::Verbosity G4VisManager::GetVerbosityValue(G4int intVerbosity) {
1474  Verbosity verbosity;
1475  if      (intVerbosity < quiet) verbosity = quiet;
1476  else if (intVerbosity > all)   verbosity = all;
1477  else                           verbosity = Verbosity(intVerbosity);
1478  return verbosity;
1479}
1480
1481void G4VisManager::SetVerboseLevel (G4int intVerbosity) {
1482  fVerbosity = GetVerbosityValue(intVerbosity);
1483}
1484
1485void G4VisManager::SetVerboseLevel (const G4String& verbosityString) {
1486  fVerbosity = GetVerbosityValue(verbosityString);
1487}
1488
1489G4bool G4VisManager::IsValidView () {
1490
1491  if (!fInitialised) Initialise ();
1492
1493  static G4bool noGSPrinting = true;
1494  if (!fpGraphicsSystem) {
1495    // Limit printing - we do not want printing if the user simply does
1496    // not want to use graphics, e.g., in batch mode.
1497    if (noGSPrinting) {
1498      noGSPrinting = false;
1499      if (fVerbosity >= warnings) {
1500        G4cout <<
1501  "WARNING: G4VisManager::IsValidView(): Attempt to draw when no graphics system"
1502  "\n  has been instantiated.  Use \"/vis/open\" or \"/vis/sceneHandler/create\"."
1503  "\n  Alternatively, to avoid this message, suppress instantiation of vis"
1504  "\n  manager (G4VisExecutive), possibly by setting G4VIS_NONE, and ensure"
1505  "\n  drawing code is executed only if G4VVisManager::GetConcreteInstance()"
1506  "\n  is non-zero."
1507               << G4endl;
1508      }
1509    }
1510    return false;
1511  }
1512
1513  if ((!fpScene) || (!fpSceneHandler) || (!fpViewer)) {
1514    if (fVerbosity >= errors) {
1515      G4cout <<
1516        "ERROR: G4VisManager::IsValidView(): Current view is not valid."
1517             << G4endl;
1518      PrintInvalidPointers ();
1519    }
1520    return false;
1521  }
1522
1523  if (fpScene != fpSceneHandler -> GetScene ()) {
1524    if (fVerbosity >= errors) {
1525      G4cout << "ERROR: G4VisManager::IsValidView ():";
1526      if (fpSceneHandler -> GetScene ()) {
1527        G4cout <<
1528          "\n  The current scene \""
1529               << fpScene -> GetName ()
1530               << "\" is not handled by"
1531          "\n  the current scene handler \""
1532               << fpSceneHandler -> GetName ()
1533               << "\""
1534          "\n  (it currently handles scene \""
1535               << fpSceneHandler -> GetScene () -> GetName ()
1536               << "\")."
1537          "\n  Either:"
1538          "\n  (a) attach it to the scene handler with"
1539          "\n      /vis/sceneHandler/attach "
1540               << fpScene -> GetName ()
1541               <<       ", or"
1542          "\n  (b) create a new scene handler with "
1543          "\n      /vis/sceneHandler/create <graphics-system>,"
1544          "\n      in which case it should pick up the the new scene."
1545               << G4endl;
1546      }
1547      else {
1548        G4cout << "\n  Scene handler \""
1549               << fpSceneHandler -> GetName ()
1550               << "\" has null scene pointer."
1551          "\n  Attach a scene with /vis/sceneHandler/attach [<scene-name>]"
1552               << G4endl;
1553      }
1554    }
1555    return false;
1556  }
1557
1558  const G4ViewerList& viewerList = fpSceneHandler -> GetViewerList ();
1559  if (viewerList.size () == 0) {
1560    if (fVerbosity >= errors) {
1561      G4cout <<
1562        "ERROR: G4VisManager::IsValidView (): the current scene handler\n  \""
1563             << fpSceneHandler -> GetName ()
1564             << "\" has no viewers.  Do /vis/viewer/create."
1565             << G4endl;
1566    }
1567    return false;
1568  }
1569
1570  G4bool isValid = true;
1571  if (fpScene -> IsEmpty ()) {  // Add world by default if possible...
1572    G4bool warn(fVerbosity >= warnings);
1573    G4bool successful = fpScene -> AddWorldIfEmpty (warn);
1574    if (!successful || fpScene -> IsEmpty ()) {        // If still empty...
1575      if (fVerbosity >= errors) {
1576        G4cout << "ERROR: G4VisManager::IsViewValid ():";
1577        G4cout <<
1578          "\n  Attempt at some drawing operation when scene is empty."
1579          "\n  Maybe the geometry has not yet been defined."
1580          "  Try /run/initialize."
1581               << G4endl;
1582      }
1583      isValid = false;
1584    }
1585    else {
1586      G4UImanager::GetUIpointer()->ApplyCommand ("/vis/scene/notifyHandlers");
1587      if (fVerbosity >= warnings) {
1588        G4cout <<
1589          "WARNING: G4VisManager: the scene was empty, \"world\" has been"
1590          "\n  added and the scene handlers notified.";
1591        G4cout << G4endl;
1592      }
1593    }
1594  }
1595  if (isValid) SetConcreteInstance(this);
1596  return isValid;
1597}
1598
1599void
1600G4VisManager::RegisterModelFactories()
1601{
1602  if (fVerbosity >= warnings) {
1603    G4cout<<"G4VisManager: No model factories registered with G4VisManager."<<G4endl;
1604    G4cout<<"G4VisManager::RegisterModelFactories() should be overridden in derived"<<G4endl;
1605    G4cout<<"class. See G4VisExecutive for an example."<<G4endl;
1606  }
1607}
Note: See TracBrowser for help on using the repository browser.