ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4HepRepSceneHandler.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4HepRepSceneHandler.cc
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 //
27 
32 #include <stdio.h>
33 
34 #include "globals.hh"
35 #include <vector>
36 #include <iostream>
37 // NOTE not available on Solaris 5.2 and Linux g++ 2.95.2
38 // #include <sstream>
39 #include <iomanip>
40 #include <fstream>
41 #include <cmath>
42 #include <cassert>
43 
44 //HepRep
45 #include "HEPREP/HepRep.h"
46 #include "G4HepRepMessenger.hh"
47 
48 //G4
49 #include "G4PhysicalConstants.hh"
50 #include "G4Vector3D.hh"
51 #include "G4Version.hh"
52 #include "G4Types.hh"
53 #include "G4Point3D.hh"
54 #include "G4Normal3D.hh"
55 #include "G4Polyline.hh"
56 #include "G4Polymarker.hh"
57 #include "G4Polyhedron.hh"
58 #include "G4Circle.hh"
59 #include "G4Square.hh"
60 #include "G4Text.hh"
61 #include "G4VPhysicalVolume.hh"
62 #include "G4VisAttributes.hh"
63 #include "G4VSolid.hh"
64 #include "G4VTrajectory.hh"
65 #include "G4VTrajectoryPoint.hh"
66 #include "G4VHit.hh"
67 #include "G4Scene.hh"
68 #include "G4Material.hh"
69 #include "G4AttDef.hh"
70 #include "G4AttValue.hh"
71 #include "G4AttCheck.hh"
72 
73 // CHepRep
75 
76 // This
77 #include "G4HepRep.hh"
78 #include "G4HepRepSceneHandler.hh"
79 #include "G4HepRepViewer.hh"
80 
81 
82 using namespace HEPREP;
83 using namespace cheprep;
84 using namespace std;
85 
87 
88 //#define LDEBUG 1
89 //#define SDEBUG 1
90 //#define PDEBUG 1
91 
93  : G4VSceneHandler (system, sceneIdCount++, name),
94  out (0),
95  geometryLayer ("Geometry"),
96  eventLayer ("Event"),
97  calHitLayer ("CalHit"),
98  trajectoryLayer ("Trajectory"),
99  hitLayer ("Hit"),
100  rootVolumeName ("Geometry"),
101  baseName (""),
102  eventNumberPrefix (""),
103  eventNumberSuffix (""),
104  eventNumber (1),
105  eventNumberWidth (-1),
106  extension (""),
107  writeBinary (false),
108  writeZip (false),
109  writeGZ (false),
110  writeMultipleFiles (false),
111  currentHit (NULL),
112  currentTrack (NULL),
113  _heprep (NULL),
114  _heprepGeometry (NULL)
115 {
116 
117 #ifdef LDEBUG
118  cout << "G4HepRepSceneHandler::G4HepRepSceneHandler: " << system << endl;
119 #endif
120 
121  materialState[kStateSolid] = G4String("Solid");
122  materialState[kStateLiquid] = G4String("Liquid");
123  materialState[kStateGas] = G4String("Gas");
124  materialState[kStateUndefined] = G4String("Undefined");
125 
126  factory = new XMLHepRepFactory();
127  writer = NULL;
128 
129  // opening of file deferred to closeHepRep();
130  openHepRep();
131 }
132 
133 
135 #ifdef LDEBUG
136  cout << "G4HepRepSceneHandler::~G4HepRepSceneHandler() " << endl;
137 #endif
138  close();
139 
140  delete factory;
141  factory = NULL;
142 
143  G4HepRep* pHepRepSystem = dynamic_cast<G4HepRep*>(GetGraphicsSystem());
144  if (pHepRepSystem) pHepRepSystem->removeSceneHandler();
145 }
146 
147 
149  if (writer != NULL) return;
150 
151  if (name == "stdout") {
152 #ifdef LDEBUG
153  cout << "G4HepRepSceneHandler::Open() stdout" << endl;
154 #endif
155  writer = factory->createHepRepWriter(&cout, false, false);
156  out = NULL;
157  baseName = name;
158  eventNumberPrefix = "";
159  eventNumberSuffix = "";
160  extension = "";
161  writeBinary = false;
162  writeZip = false;
163  writeGZ = false;
164  writeMultipleFiles = false;
165  eventNumber = 0;
166  eventNumberWidth = 0;
167  } else if (name == "stderr") {
168 #ifdef LDEBUG
169  cout << "G4HepRepSceneHandler::Open() stderr" << endl;
170 #endif
171  writer = factory->createHepRepWriter(&cerr, false, false);
172  out = NULL;
173  baseName = name;
174  eventNumberPrefix = "";
175  eventNumberSuffix = "";
176  extension = "";
177  writeBinary = false;
178  writeZip = false;
179  writeGZ = false;
180  writeMultipleFiles = false;
181  eventNumber = 0;
182  eventNumberWidth = 0;
183  } else {
184 #ifdef LDEBUG
185  cout << "G4HepRepSceneHandler::Open() " << name << endl;
186 #endif
187  if (eventNumberWidth < 0) {
188  // derive filename(s)
189  // check for extensions
190  const unsigned int numberOfExtensions = 8;
191  string ext[numberOfExtensions] = {".heprep", ".heprep.xml", ".heprep.zip", ".heprep.gz",
192  ".bheprep", ".bheprep.xml", ".bheprep.zip", ".bheprep.gz"};
193  unsigned int i=0;
194  while (i < numberOfExtensions) {
195  int dot = name.size() - ext[i].size();
196  if ((dot >= 0) &&
197  (name.substr(dot, ext[i].size()) == ext[i])) break;
198  i++;
199  }
200 
201  if (i != numberOfExtensions) {
202  extension = ext[i];
203  writeBinary = i >= (numberOfExtensions/2);
204  writeZip = (i == 2) || (i == 6);
205  writeGZ = (i == 3) || (i == 7);
206 
207  int dot = name.length() - extension.length();
208  baseName = (dot >= 0) ? name.substr(0, dot) : "";
209 
210  } else {
211  // Default for no extension
212  extension = ".heprep.zip";
213  writeBinary = false;
214  writeZip = true;
215  writeGZ = false;
216  baseName = name;
217  }
218 
219  writeMultipleFiles = false;
220  int startDigit = -1; int endDigit = -1;
221 
223 
224  string suffix = messenger->getEventNumberSuffix();
225  if (suffix != "") {
226  // look for 0000 pattern in suffix
227  endDigit = suffix.length()-1;
228  while (endDigit >= 0) {
229  if (isdigit(suffix.at(endDigit))) break;
230  endDigit--;
231  }
232  if (endDigit < 0) {
233  cerr << "/vis/heprep/appendEventNumberSuffix contains no digits" << endl;
234  } else {
235  writeMultipleFiles = true;
236  startDigit = endDigit;
237  while (startDigit >= 0) {
238  if (!isdigit(suffix.at(startDigit))) break;
239  startDigit--;
240  }
241  startDigit++;
242  }
243  }
244 
245  if (writeMultipleFiles) {
246  eventNumberPrefix = suffix.substr(0, startDigit);
247  eventNumber = atoi(suffix.substr(startDigit, endDigit).c_str());
248  eventNumberWidth = endDigit +1 - startDigit;
249  eventNumberSuffix = suffix.substr(endDigit+1);
250  } else {
251  // open single file here
253 
254  eventNumber = 1;
255  eventNumberWidth = 10;
256  eventNumberPrefix = "";
257  eventNumberSuffix = "";
258  }
259  }
260  }
261 }
262 
263 
265 #ifdef LDEBUG
266  cout << "G4HepRepSceneHandler::OpenHepRep() " << endl;
267 #endif
268 
269  if (_heprep != NULL) return;
270 
271  // all done on demand, once pointers are set to NULL
272  _heprepGeometry = NULL;
273  _geometryInstanceTree = NULL;
274  _geometryRootInstance = NULL;
275  _geometryInstance.clear();
276  _geometryTypeTree = NULL;
277  _geometryRootType = NULL;
278  _geometryTypeName.clear();
279  _geometryType.clear();
280  _eventInstanceTree = NULL;
281  _eventInstance = NULL;
282  _eventTypeTree = NULL;
283  _eventType = NULL;
284  _trajectoryType = NULL;
285  _hitType = NULL;
286  _calHitType = NULL;
287  _calHitFaceType = NULL;
288 }
289 
290 
295  if (_heprep == NULL) return true;
296 
297 #ifdef LDEBUG
298  cout << "G4HepRepSceneHandler::CloseHepRep() start" << endl;
299 #endif
300 
301  // if this is the final close, then there should not be any event pending to be written.
302  if (final) {
303  if (_eventInstanceTree != NULL) {
304  cerr << "WARNING: you probably used '/vis/viewer/endOfEventAction accumulate' and "
305  << "forgot to call /vis/viewer/update before exit. No event written." << endl;
306  }
307  } else {
308 
310 
311  // add geometry to the heprep if there is an event (separate geometries are written
312  // using DrawView() called from /vis/viewer/flush)
313  if (_eventInstanceTree != NULL) {
315 
316  // couple geometry
317 
318  if ( messenger->appendGeometry()) {
319  // couple geometry to event if geometry was written
320  if ((_geometryInstanceTree != NULL)) {
322  }
323  } else {
324  char name[128];
325  if (writeMultipleFiles) {
326  sprintf(name, "%s%s%s#%s", baseName.c_str(), "-geometry", extension.c_str(), "G4GeometryData");
327  } else {
328  sprintf(name, "%s%s#%s", "geometry", (writeBinary ? ".bheprep" : ".heprep"), "G4GeometryData");
329  }
331  }
332  }
333 
334  // force inclusion of all subtypes of event
335  if (_eventInstanceTree != NULL) {
336  getEventType();
338  getHitType();
339  getCalHitType();
341  }
342 
343  // Give this HepRep all of the layer order info for both geometry and event,
344  // since these will both end up in a single HepRep.
347 
348  // open heprep file
349  if (writer == NULL) {
350  open((GetScene() == NULL) ? G4String("G4HepRepOutput.heprep.zip") : GetScene()->GetName());
351  }
352 
353  // write out separate geometry
354  if (! messenger->appendGeometry() && (_heprepGeometry != NULL)) {
355  if (writeMultipleFiles) {
356  char fileName[128];
357  sprintf(fileName, "%s%s%s", baseName.c_str(), "-geometry", extension.c_str());
358  openFile(G4String(fileName));
359  }
360 
361  char name[128];
362  sprintf(name, "%s%s", "geometry", (writeBinary ? ".bheprep" : ".heprep"));
363  if (!writeMultipleFiles) {
364  writer->addProperty("RecordLoop.ignore", name);
365  }
366 
368 
369  delete _heprepGeometry;
370  _heprepGeometry = NULL;
371 
373  }
374 
375  if (writeMultipleFiles) {
376 // NOTE: does not work on Solaris 5.2 and Linux 2.95.2
377 // stringstream fileName;
378 // fileName << baseName << eventNumberPrefix << setw(eventNumberWidth) << setfill('0') << eventNumber << eventNumberSuffix << extension;
379 // openFile(fileName.str());
380 // Use instead:
381  char fileName[128];
382  char fileFormat[128];
383  sprintf(fileFormat, "%s%d%s", "%s%s%0", eventNumberWidth, "d%s%s");
384  sprintf(fileName, fileFormat, baseName.c_str(), eventNumberPrefix.c_str(), eventNumber, eventNumberSuffix.c_str(), extension.c_str());
385  openFile(G4String(fileName));
386  }
387 
388  // write out the heprep
389 // NOTE: does not work on Solaris 5.2 and Linux 2.95.2
390 // stringstream eventName;
391 // eventName << "event-" << setw(eventNumberWidth) << setfill('0') << eventNumber << (writeBinary ? ".bheprep" : ".heprep");
392 // writer->write(_heprep, eventName.str());
393 // Use instead:
394  char eventName[128];
395  char eventFormat[128];
396  sprintf(eventFormat, "%s%d%s%s", "event-%0", eventNumberWidth, "d", (writeBinary ? ".bheprep" : ".heprep"));
397  sprintf(eventName, eventFormat, eventNumber);
398  if (writer) writer->write(_heprep, G4String(eventName));
399 
400  eventNumber++;
401  }
402 
403  delete _heprep;
404  _heprep = NULL;
405 
407 
408  return true;
409 }
410 
411 
413 
414 #ifdef LDEBUG
415  cout << "G4HepRepSceneHandler::Close() " << endl;
416 #endif
417 
418  if (writer == NULL) return;
419 
420  if (!writeMultipleFiles) {
421  closeHepRep(true);
422  closeFile();
423  }
424 
425  G4HepRepViewer* viewer = dynamic_cast<G4HepRepViewer*>(GetCurrentViewer());
426  viewer->reset();
427 }
428 
430  out = new ofstream(name.c_str(), std::ios::out | std::ios::binary );
432 }
433 
435  writer->close();
436  delete writer;
437  writer = NULL;
438 
439  delete out;
440  out = NULL;
441 }
442 
444  if (heprep == NULL) return;
445  heprep->addLayer(geometryLayer);
446  heprep->addLayer(eventLayer);
447  heprep->addLayer(calHitLayer);
448  heprep->addLayer(trajectoryLayer);
449  heprep->addLayer(hitLayer);
450 }
451 
453 #ifdef SDEBUG
454  cout << "G4HepRepSceneHandler::BeginModeling() " << endl;
455 #endif
457 }
458 
459 
461 #ifdef SDEBUG
462  cout << "G4HepRepSceneHandler::EndModeling() " << endl;
463 #endif
465 }
466 
468 #ifdef SDEBUG
469  cout << "G4HepRepSceneHandler::AddSolid(const G4Box& box)" << endl;
470 #endif
471 
472  if (dontWrite()) return;
473 
475 
476  if (! messenger->useSolids()) {
478  return;
479  }
480 
481  G4double dx = box.GetXHalfLength();
482  G4double dy = box.GetYHalfLength();
483  G4double dz = box.GetZHalfLength();
484 
485  G4Point3D vertex1(G4Point3D( dx, dy,-dz));
486  G4Point3D vertex2(G4Point3D( dx,-dy,-dz));
487  G4Point3D vertex3(G4Point3D(-dx,-dy,-dz));
488  G4Point3D vertex4(G4Point3D(-dx, dy,-dz));
489  G4Point3D vertex5(G4Point3D( dx, dy, dz));
490  G4Point3D vertex6(G4Point3D( dx,-dy, dz));
491  G4Point3D vertex7(G4Point3D(-dx,-dy, dz));
492  G4Point3D vertex8(G4Point3D(-dx, dy, dz));
493 
494  vertex1 = (transform) * vertex1;
495  vertex2 = (transform) * vertex2;
496  vertex3 = (transform) * vertex3;
497  vertex4 = (transform) * vertex4;
498  vertex5 = (transform) * vertex5;
499  vertex6 = (transform) * vertex6;
500  vertex7 = (transform) * vertex7;
501  vertex8 = (transform) * vertex8;
502 
504  addAttributes(instance, getCalHitType());
505 
506  setAttribute(instance, "DrawAs", G4String("Prism"));
507 
508  setVisibility(instance, box);
509  setLine(instance, box);
510  setColor(instance, getColorFor(box));
511 
512  factory->createHepRepPoint(instance, vertex1.x(), vertex1.y(), vertex1.z());
513  factory->createHepRepPoint(instance, vertex2.x(), vertex2.y(), vertex2.z());
514  factory->createHepRepPoint(instance, vertex3.x(), vertex3.y(), vertex3.z());
515  factory->createHepRepPoint(instance, vertex4.x(), vertex4.y(), vertex4.z());
516  factory->createHepRepPoint(instance, vertex5.x(), vertex5.y(), vertex5.z());
517  factory->createHepRepPoint(instance, vertex6.x(), vertex6.y(), vertex6.z());
518  factory->createHepRepPoint(instance, vertex7.x(), vertex7.y(), vertex7.z());
519  factory->createHepRepPoint(instance, vertex8.x(), vertex8.y(), vertex8.z());
520 }
521 
522 
524 #ifdef SDEBUG
525  cout << "G4HepRepSceneHandler::AddSolid(const G4Cons& cons)" << endl;
526 #endif
527 
528  if (dontWrite()) return;
529 
531 
532  if (! messenger->useSolids() || (cons.GetDeltaPhiAngle() < twopi)) {
534  return;
535  }
536 
537  G4PhysicalVolumeModel* pPVModel =
538  dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
539  if (!pPVModel) {
541  return;
542  }
543 
544  G4LogicalVolume* pCurrentLV = pPVModel->GetCurrentLV();
545  G4int currentDepth = pPVModel->GetCurrentDepth();
546  G4Material* pCurrentMaterial = pPVModel->GetCurrentMaterial();
547 
548  G4Point3D vertex1(G4Point3D( 0., 0., cons.GetZHalfLength()));
549  G4Point3D vertex2(G4Point3D( 0., 0.,-cons.GetZHalfLength()));
550 
551  vertex1 = (transform) * vertex1;
552  vertex2 = (transform) * vertex2;
553 
554  HepRepInstance* instance = getGeometryInstance(pCurrentLV, pCurrentMaterial, currentDepth);
555  setAttribute(instance, "DrawAs", G4String("Cylinder"));
556 
557  setVisibility(instance, cons);
558  setLine(instance, cons);
559  setColor(instance, getColorFor(cons));
560 
561  HepRepType* type = getGeometryType(pCurrentLV->GetName(), currentDepth);
562 
563  // Outer cylinder.
564  HepRepInstance* outer = factory->createHepRepInstance(instance, type);
565  outer->addAttValue("pickParent",true);
566  outer->addAttValue("showParentAttributes",true);
567 
568  HepRepPoint* op1 = factory->createHepRepPoint(outer, vertex1.x(), vertex1.y(), vertex1.z());
569  op1->addAttValue("Radius",cons.GetOuterRadiusPlusZ());
570 
571  HepRepPoint* op2 = factory->createHepRepPoint(outer, vertex2.x(), vertex2.y(), vertex2.z());
572  op2->addAttValue("Radius",cons.GetOuterRadiusMinusZ());
573 
574  // Inner cylinder.
575  HepRepInstance* inner = factory->createHepRepInstance(instance, type);
576  inner->addAttValue("pickParent",true);
577  inner->addAttValue("showParentAttributes",true);
578 
579  HepRepPoint* ip1 = factory->createHepRepPoint(inner, vertex1.x(), vertex1.y(), vertex1.z());
580  ip1->addAttValue("Radius",cons.GetInnerRadiusPlusZ());
581 
582  HepRepPoint* ip2 = factory->createHepRepPoint(inner, vertex2.x(), vertex2.y(), vertex2.z());
583  ip2->addAttValue("Radius",cons.GetInnerRadiusMinusZ());
584 }
585 
586 
588 #ifdef SDEBUG
589  cout << "G4HepRepSceneHandler::AddSolid(const G4Tubs& tubs)" << endl;
590 #endif
591 
592  if (dontWrite()) return;
593 
595 
596  if (! messenger->useSolids() || (tubs.GetDeltaPhiAngle() < twopi)) {
598  return;
599  }
600 
601  G4PhysicalVolumeModel* pPVModel =
602  dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
603  if (!pPVModel) {
605  return;
606  }
607 
608  G4LogicalVolume* pCurrentLV = pPVModel->GetCurrentLV();
609  G4int currentDepth = pPVModel->GetCurrentDepth();
610  G4Material* pCurrentMaterial = pPVModel->GetCurrentMaterial();
611 
612  G4Point3D vertex1(G4Point3D( 0., 0., tubs.GetZHalfLength()));
613  G4Point3D vertex2(G4Point3D( 0., 0.,-tubs.GetZHalfLength()));
614 
615  vertex1 = (transform) * vertex1;
616  vertex2 = (transform) * vertex2;
617 
618  HepRepInstance* instance = getGeometryInstance(pCurrentLV, pCurrentMaterial, currentDepth);
619  setAttribute(instance, "DrawAs", G4String("Cylinder"));
620 
621  setVisibility(instance, tubs);
622  setLine(instance, tubs);
623  setColor(instance, getColorFor(tubs));
624 
625  HepRepType* type = getGeometryType(pCurrentLV->GetName(), currentDepth);
626 
627  // Outer cylinder.
628  HepRepInstance* outer = factory->createHepRepInstance(instance, type);
629  outer->addAttValue("Radius",tubs.GetOuterRadius());
630  outer->addAttValue("pickParent",true);
631  outer->addAttValue("showParentAttributes",true);
632  factory->createHepRepPoint(outer, vertex1.x(), vertex1.y(), vertex1.z());
633  factory->createHepRepPoint(outer, vertex2.x(), vertex2.y(), vertex2.z());
634 
635  // Inner cylinder.
636  if (tubs.GetInnerRadius() > 0.) {
637  HepRepInstance* inner = factory->createHepRepInstance(instance, type);
638  inner->addAttValue("Radius",tubs.GetInnerRadius());
639  inner->addAttValue("pickParent",true);
640  inner->addAttValue("showParentAttributes",true);
641  factory->createHepRepPoint(inner, vertex1.x(), vertex1.y(), vertex1.z());
642  factory->createHepRepPoint(inner, vertex2.x(), vertex2.y(), vertex2.z());
643  }
644 }
645 
646 
648 #ifdef SDEBUG
649  cout << "G4HepRepSceneHandler::AddSolid(const G4Trd& trd)" << endl;
650 #endif
651  if (dontWrite()) return;
652 
654 
655  if (! messenger->useSolids()) {
657  return;
658  }
659 
660  G4double dx1 = trd.GetXHalfLength1();
661  G4double dy1 = trd.GetYHalfLength1();
662  G4double dx2 = trd.GetXHalfLength2();
663  G4double dy2 = trd.GetYHalfLength2();
664  G4double dz = trd.GetZHalfLength();
665 
666  G4Point3D vertex1(G4Point3D( dx1, dy1,-dz));
667  G4Point3D vertex2(G4Point3D( dx1,-dy1,-dz));
668  G4Point3D vertex3(G4Point3D(-dx1,-dy1,-dz));
669  G4Point3D vertex4(G4Point3D(-dx1, dy1,-dz));
670  G4Point3D vertex5(G4Point3D( dx2, dy2, dz));
671  G4Point3D vertex6(G4Point3D( dx2,-dy2, dz));
672  G4Point3D vertex7(G4Point3D(-dx2,-dy2, dz));
673  G4Point3D vertex8(G4Point3D(-dx2, dy2, dz));
674 
675  vertex1 = (transform) * vertex1;
676  vertex2 = (transform) * vertex2;
677  vertex3 = (transform) * vertex3;
678  vertex4 = (transform) * vertex4;
679  vertex5 = (transform) * vertex5;
680  vertex6 = (transform) * vertex6;
681  vertex7 = (transform) * vertex7;
682  vertex8 = (transform) * vertex8;
683 
685 
686  addAttributes(instance, getCalHitType());
687 
688  setAttribute(instance, "DrawAs", G4String("Prism"));
689 
690  setVisibility(instance, trd);
691  setLine(instance, trd);
692  setColor(instance, getColorFor(trd));
693 
694  factory->createHepRepPoint(instance, vertex1.x(), vertex1.y(), vertex1.z());
695  factory->createHepRepPoint(instance, vertex2.x(), vertex2.y(), vertex2.z());
696  factory->createHepRepPoint(instance, vertex3.x(), vertex3.y(), vertex3.z());
697  factory->createHepRepPoint(instance, vertex4.x(), vertex4.y(), vertex4.z());
698  factory->createHepRepPoint(instance, vertex5.x(), vertex5.y(), vertex5.z());
699  factory->createHepRepPoint(instance, vertex6.x(), vertex6.y(), vertex6.z());
700  factory->createHepRepPoint(instance, vertex7.x(), vertex7.y(), vertex7.z());
701  factory->createHepRepPoint(instance, vertex8.x(), vertex8.y(), vertex8.z());
702 }
703 
705  if (dontWrite()) return;
707 }
708 
710  if (dontWrite()) return;
711  G4VSceneHandler::AddSolid (sphere);
712 }
713 
715  if (dontWrite()) return;
717 }
718 
720  if (dontWrite()) return;
721  G4VSceneHandler::AddSolid (torus);
722 }
723 
725  if (dontWrite()) return;
726  G4VSceneHandler::AddSolid (polycone);
727 }
728 
730  if (dontWrite()) return;
731  G4VSceneHandler::AddSolid (polyhedra);
732 }
733 
735  if (dontWrite()) return;
737 }
738 
740  if (dontWrite()) return;
741  G4VSceneHandler::AddSolid (ellipsoid);
742 }
743 
745  if (dontWrite()) return;
747 }
748 
750  if (dontWrite()) return;
752 }
753 
754 
756 
757 #ifdef PDEBUG
758  cout << "G4HepRepSceneHandler::AddPrimitive(G4Polyline&) " << line.size() << endl;
759 #endif
760  if (dontWrite()) return;
761 
762  if (fProcessing2D) {
763  static G4bool warned = false;
764  if (!warned) {
765  warned = true;
767  ("G4HepRepSceneHandler::AddPrimitive (const G4Polyline&)",
768  "vis-HepRep1001", JustWarning,
769  "2D polylines not implemented. Ignored.");
770  }
771  return;
772  }
773 
775 
776  addAttributes(instance, getTrajectoryType());
777 
779  setColor(instance, GetColor());
780 
781  setVisibility(instance, line);
782 
783  setLine(instance, line);
784 
785  for (size_t i=0; i < line.size(); i++) {
786  G4Point3D vertex = transform * line[i];
787  factory->createHepRepPoint(instance, vertex.x(), vertex.y(), vertex.z());
788  }
789 }
790 
791 
793 
794 #ifdef PDEBUG
795  cout << "G4HepRepSceneHandler::AddPrimitive(G4Polymarker&) " << line.size() << endl;
796 #endif
797  if (dontWrite()) return;
798 
799  if (fProcessing2D) {
800  static G4bool warned = false;
801  if (!warned) {
802  warned = true;
804  ("G4HepRepSceneHandler::AddPrimitive (const G4Polymarker&)",
805  "vis-HepRep1002", JustWarning,
806  "2D polymarkers not implemented. Ignored.");
807  }
808  return;
809  }
810 
812 
813  addAttributes(instance, getHitType());
814 
816  setColor(instance, GetColor());
817 
818  setVisibility(instance, line);
819 
820  setMarker(instance, line);
821 
822  // Default MarkName is set to Circle for this Type.
823  int mtype = line.GetMarkerType();
824 
825  // Cannot be case statement since line.xxx is not a constant
826  if (mtype == line.dots) {
827  setAttribute(instance, "Fill", true);
828  setColor(instance, GetColor(), G4String("FillColor"));
829  } else if (mtype == line.circles) {
830  } else if (mtype == line.squares) {
831  setAttribute(instance, "MarkName", G4String("Box"));
832  } else {
833  // line.line + default
834  setAttribute(instance, "MarkName", G4String("Plus"));
835  }
836 
837  for (size_t i=0; i < line.size(); i++) {
838  G4Point3D vertex = transform * line[i];
839  factory->createHepRepPoint(instance, vertex.x(), vertex.y(), vertex.z());
840  }
841 }
842 
843 
845 #ifdef PDEBUG
846  cout << "G4HepRepSceneHandler::AddPrimitive(G4Circle&) " << endl;
847 #endif
848  if (dontWrite()) return;
849 
850  if (fProcessing2D) {
851  static G4bool warned = false;
852  if (!warned) {
853  warned = true;
855  ("G4HepRepSceneHandler::AddPrimitive (const G4Circle&)",
856  "vis-HepRep1003", JustWarning,
857  "2D circles not implemented. Ignored.");
858  }
859  return;
860  }
861 
863 
864  addAttributes(instance, getHitType());
865 
866  G4Point3D center = transform * circle.GetPosition();
867 
868  fpVisAttribs = circle.GetVisAttributes();
869  setColor (instance, GetColor());
870 
871  setVisibility(instance, circle);
872 
873  setMarker(instance, circle);
874 
875  factory->createHepRepPoint(instance, center.x(), center.y(), center.z());
876 }
877 
878 
880 
881 #ifdef PDEBUG
882  cout << "G4HepRepSceneHandler::AddPrimitive(G4Polyhedron&) " << endl;
883 #endif
884  if (dontWrite()) return;
885 
886  if (fProcessing2D) {
887  static G4bool warned = false;
888  if (!warned) {
889  warned = true;
891  ("G4HepRepSceneHandler::AddPrimitive (const G4Polyhedron&)",
892  "vis-HepRep1004", JustWarning,
893  "2D polyhedra not implemented. Ignored.");
894  }
895  return;
896  }
897 
898  G4Normal3D surfaceNormal;
899  G4Point3D vertex;
900 
901  if (polyhedron.GetNoFacets()==0) return;
902 
904 
905  addAttributes(instance, getCalHitType());
906 
907  setVisibility(instance, polyhedron);
908 
909  G4int currentDepth = 0;
910  G4PhysicalVolumeModel* pPVModel =
911  dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
912  if (pPVModel) currentDepth = pPVModel->GetCurrentDepth();
913 
914  G4bool notLastFace;
915  do {
916  HepRepInstance* face;
917  if (isEventData()) {
918  face = factory->createHepRepInstance(instance, getCalHitFaceType());
919  } else {
920  face = getGeometryInstance("*Face", currentDepth+1);
921  setAttribute(face, "PickParent", true);
922  setAttribute(face, "DrawAs", G4String("Polygon"));
923  }
924 
925  setLine(face, polyhedron);
926  fpVisAttribs = polyhedron.GetVisAttributes();
927  setColor(face, GetColor());
928  if (isEventData()) setColor(face, GetColor(), G4String("FillColor"));
929 
930  notLastFace = polyhedron.GetNextNormal (surfaceNormal);
931 
932  G4int edgeFlag = 1;
933  G4bool notLastEdge;
934  do {
935  notLastEdge = polyhedron.GetNextVertex (vertex, edgeFlag);
936  vertex = transform * vertex;
937  factory->createHepRepPoint(face, vertex.x(), vertex.y(), vertex.z());
938  } while (notLastEdge);
939  } while (notLastFace);
940 }
941 
942 
944 #ifdef PDEBUG
945  cout << "G4HepRepSceneHandler::AddPrimitive(G4Text&) " << endl;
946 #endif
947  if (dontWrite()) return;
948 
949  /*** You may need this
950  if (fProcessing2D) {
951  static G4bool warned = false;
952  if (!warned) {
953  warned = true;
954  G4Exception
955  ("G4HepRepSceneHandler::AddPrimitive (const G4Text&)",
956  "vis-HepRep1005", JustWarning,
957  "2D text not implemented. Ignored.");
958  }
959  return;
960  }
961  ***/
962 
963  cout << "G4HepRepSceneHandler::AddPrimitive G4Text : not yet implemented. " << endl;
964 }
965 
966 
968 #ifdef PDEBUG
969  cout << "G4HepRepSceneHandler::AddPrimitive(G4Square&) " << endl;
970 #endif
971  if (dontWrite()) return;
972 
973  if (fProcessing2D) {
974  static G4bool warned = false;
975  if (!warned) {
976  warned = true;
978  ("G4HepRepSceneHandler::AddPrimitive (const G4Square&)",
979  "vis-HepRep1006", JustWarning,
980  "2D squares not implemented. Ignored.");
981  }
982  return;
983  }
984 
986 
987  addAttributes(instance, getHitType());
988 
989  G4Point3D center = transform * square.GetPosition();
990 
991  setColor (instance, getColorFor(square));
992 
993  setVisibility(instance, square);
994 
995  setMarker(instance, square);
996 
997  factory->createHepRepPoint(instance, center.x(), center.y(), center.z());
998 }
999 
1001  if (dontWrite()) return;
1003 }
1004 
1006 #ifdef PDEBUG
1007  cout << "G4HepRepSceneHandler::AddCompound(G4VTrajectory&) " << endl;
1008 #endif
1009  if (dontWrite()) return;
1010 
1011  currentTrack = &trajectory;
1012  G4VSceneHandler::AddCompound(trajectory);
1013  currentTrack = NULL;
1014 }
1015 
1016 
1018 #ifdef PDEBUG
1019  cout << "G4HepRepSceneHandler::AddCompound(G4VHit&) " << endl;
1020 #endif
1021  if (dontWrite()) return;
1022 
1023  currentHit = &hit;
1025  currentHit = NULL;
1026 }
1027 
1028 void G4HepRepSceneHandler::PreAddSolid (const G4Transform3D& objectTransformation,
1029  const G4VisAttributes& visAttribs) {
1030 
1031  G4VSceneHandler::PreAddSolid (objectTransformation, visAttribs);
1032 
1033  transform = objectTransformation;
1034 #ifdef SDEBUG
1035  cout << "G4HepRepSceneHandler::PreAddSolid(G4Transform3D&, G4VisAttributes&)" << endl;
1036 #endif
1037 }
1038 
1039 
1041 #ifdef SDEBUG
1042  cout << "G4HepRepSceneHandler::PostAddSolid()" << endl;
1043 #endif
1045 }
1046 
1047 
1048 void G4HepRepSceneHandler::BeginPrimitives (const G4Transform3D& objectTransformation) {
1049 #ifdef SDEBUG
1050  cout << "G4HepRepSceneHandler::BeginPrimitives(G4Transform3D&)" << endl;
1051 #endif
1052 
1053  G4VSceneHandler::BeginPrimitives (objectTransformation);
1054  transform = objectTransformation;
1055 }
1056 
1057 
1059 #ifdef SDEBUG
1060  cout << "G4HepRepSceneHandler::EndPrimitives" << endl;
1061 #endif
1063 }
1064 
1065 
1068  return !( messenger->writeInvisibles() || (fpVisAttribs ? (bool)fpVisAttribs->IsVisible() : true));
1069 }
1070 
1072  const G4Color& color,
1073  const G4String& key) {
1074 #ifdef CDEBUG
1075  cout << "G4HepRepSceneHandler::setColor : red : " << color.GetRed () <<
1076  " green : " << color.GetGreen () <<
1077  " blue : " << color.GetBlue () << endl;
1078 #endif
1079 
1080  setAttribute(attribute, key, color.GetRed(), color.GetGreen(), color.GetBlue(), color.GetAlpha());
1081 }
1082 
1084  // fpVisAttribs has been set for solids.
1085  return GetColor();
1086 }
1087 
1089  fpVisAttribs = visible.GetVisAttributes();
1090  return GetColor();
1091 }
1092 
1093 void G4HepRepSceneHandler::setVisibility (HepRepAttribute *attribute, const G4VSolid& /* solid */) {
1094  setAttribute(attribute, "Visibility", (fpVisAttribs ? (bool)fpVisAttribs->IsVisible() : true));
1095 }
1096 
1098  const G4VisAttributes* atts = visible.GetVisAttributes();
1099 
1100  setAttribute(attribute, "Visibility", (atts && (atts->IsVisible()==0)) ? false : true);
1101 }
1102 
1103 void G4HepRepSceneHandler::setLine (HepRepAttribute *attribute, const G4VSolid& /* solid*/) {
1104  setAttribute(attribute, "LineWidth", 1.0);
1105 }
1106 
1108  const G4VisAttributes* atts = visible.GetVisAttributes();
1109 
1110  setAttribute(attribute, "LineWidth", (atts != NULL) ? atts->GetLineWidth() : 1.0);
1111 
1112  if (atts != NULL) {
1113  switch (atts->GetLineStyle()) {
1115  setAttribute(attribute, "LineStyle", G4String("Dotted"));
1116  break;
1118  setAttribute(attribute, "LineStyle", G4String("Dashed"));
1119  break;
1121  default:
1122  break;
1123  }
1124  }
1125 }
1126 
1128  MarkerSizeType markerType;
1129  G4double size = GetMarkerRadius( marker , markerType );
1130 
1131  setAttribute(attribute, "MarkSize", size);
1132 
1133  if (markerType == screen) setAttribute(attribute, "MarkType", G4String("Symbol"));
1134  if (marker.GetFillStyle() == G4VMarker::noFill) {
1135  setAttribute(attribute, "Fill", false);
1136  } else {
1137  fpVisAttribs = marker.GetVisAttributes();
1138  setColor(attribute, GetColor(), G4String("FillColor"));
1139  }
1140 }
1141 
1143  if (currentHit) {
1144  vector<G4AttValue>* hitAttValues = currentHit->CreateAttValues();
1145  const map<G4String,G4AttDef>* hitAttDefs = currentHit->GetAttDefs();
1146 
1147  addAttDefs(getHitType(), hitAttDefs);
1148 
1149  // these attValues are non-standard, so can only be added when we have the attDef.
1150  type->addAttValue("LVol", G4String(""));
1151  type->addAttValue("HitType", G4String(""));
1152  type->addAttValue("ID", -1);
1153  type->addAttValue("Column", -1);
1154  type->addAttValue("Row", -1);
1155  type->addAttValue("Energy", 0.0);
1156  type->addAttValue("Pos", G4String(""));
1157 
1158  addAttVals(instance, hitAttDefs, hitAttValues);
1159 
1160  delete hitAttValues;
1161 
1162  } else if (currentTrack) {
1163  vector<G4AttValue>* trajectoryAttValues = currentTrack->CreateAttValues();
1164  const map<G4String,G4AttDef>* trajectoryAttDefs = currentTrack->GetAttDefs();
1165 
1166  addAttDefs(type, trajectoryAttDefs);
1167 
1168  // these attValues are non-standard, so can only be added when we have the attDef.
1169  type->addAttValue("Ch", 0.0);
1170  type->addAttValue("Color", 1.0, 1.0, 1.0, 1.0);
1171  type->addAttValue("ID", -1);
1172  type->addAttValue("IMom", G4String(""));
1173  type->addAttValue("IMag", 0.0);
1174  type->addAttValue("PDG", -1);
1175  type->addAttValue("PN", G4String(""));
1176  type->addAttValue("PID", -1);
1177 
1178  addAttVals(instance, trajectoryAttDefs, trajectoryAttValues);
1179 
1180  delete trajectoryAttValues;
1181 
1182  }
1183 }
1184 
1186  HepRepAttValue* attValue = attribute->getAttValue(name);
1187  if ((attValue == NULL) || (attValue->getString() != value)) {
1188  HepRepPoint* point = dynamic_cast<HepRepPoint*>(attribute);
1189  if (point != NULL) {
1190  if (point->getInstance()->getAttValueFromNode(name) == NULL) {
1191  attribute = point->getInstance();
1192  }
1193  }
1194 
1195  HepRepInstance* instance = dynamic_cast<HepRepInstance*>(attribute);
1196  if (instance != NULL) {
1197  // look for definition on type (node only)
1198  if (instance->getType()->getAttValueFromNode(name) == NULL) {
1199  attribute = instance->getType();
1200  }
1201  }
1202 
1203  attribute->addAttValue(name, value);
1204  }
1205 }
1206 
1208  HepRepAttValue* attValue = attribute->getAttValue(name);
1209  if ((attValue == NULL) || (attValue->getBoolean() != value)) {
1210  HepRepPoint* point = dynamic_cast<HepRepPoint*>(attribute);
1211  if (point != NULL) {
1212  if (point->getInstance()->getAttValueFromNode(name) == NULL) {
1213  attribute = point->getInstance();
1214  }
1215  }
1216 
1217  HepRepInstance* instance = dynamic_cast<HepRepInstance*>(attribute);
1218  if (instance != NULL) {
1219  // look for definition on type (node only)
1220  if (instance->getType()->getAttValueFromNode(name) == NULL) {
1221  attribute = instance->getType();
1222  }
1223  }
1224 
1225  attribute->addAttValue(name, value);
1226  }
1227 }
1228 
1230  HepRepAttValue* attValue = attribute->getAttValue(name);
1231  if ((attValue == NULL) || (attValue->getDouble() != value)) {
1232  HepRepPoint* point = dynamic_cast<HepRepPoint*>(attribute);
1233  if (point != NULL) {
1234  if (point->getInstance()->getAttValueFromNode(name) == NULL) {
1235  attribute = point->getInstance();
1236  }
1237  }
1238 
1239  HepRepInstance* instance = dynamic_cast<HepRepInstance*>(attribute);
1240  if (instance != NULL) {
1241  // look for definition on type (node only)
1242  if (instance->getType()->getAttValueFromNode(name) == NULL) {
1243  attribute = instance->getType();
1244  }
1245  }
1246 
1247  attribute->addAttValue(name, value);
1248  }
1249 }
1250 
1252  HepRepAttValue* attValue = attribute->getAttValue(name);
1253  if ((attValue == NULL) || (attValue->getInteger() != value)) {
1254  HepRepPoint* point = dynamic_cast<HepRepPoint*>(attribute);
1255  if (point != NULL) {
1256  if (point->getInstance()->getAttValueFromNode(name) == NULL) {
1257  attribute = point->getInstance();
1258  }
1259  }
1260 
1261  HepRepInstance* instance = dynamic_cast<HepRepInstance*>(attribute);
1262  if (instance != NULL) {
1263  // look for definition on type (node only)
1264  if (instance->getType()->getAttValueFromNode(name) == NULL) {
1265  attribute = instance->getType();
1266  }
1267  }
1268 
1269  attribute->addAttValue(name, value);
1270  }
1271 }
1272 
1273 void G4HepRepSceneHandler::setAttribute(HepRepAttribute* attribute, G4String name, double red, double green, double blue, double alpha) {
1274  HepRepAttValue* attValue = attribute->getAttValue(name);
1275  vector<double> color;
1276  if (attValue != NULL) color = attValue->getColor();
1277  if ((color.size() == 0) ||
1278  (color[0] != red) ||
1279  (color[1] != green) ||
1280  (color[2] != blue) ||
1281  ((color.size() > 3) && (color[3] != alpha))) {
1282 
1283  HepRepPoint* point = dynamic_cast<HepRepPoint*>(attribute);
1284  if (point != NULL) {
1285  if (point->getInstance()->getAttValueFromNode(name) == NULL) {
1286  attribute = point->getInstance();
1287  }
1288  }
1289 
1290  HepRepInstance* instance = dynamic_cast<HepRepInstance*>(attribute);
1291  if (instance != NULL) {
1292  // look for definition on type (node only)
1293  if (instance->getType()->getAttValueFromNode(name) == NULL) {
1294  attribute = instance->getType();
1295  }
1296  }
1297 
1298  attribute->addAttValue(name, red, green, blue, alpha);
1299  }
1300 }
1301 
1302 void G4HepRepSceneHandler::addAttDefs(HepRepDefinition* definition, const map<G4String,G4AttDef>* attDefs) {
1303  if (attDefs == NULL) return;
1304 
1305  // Specify additional attribute definitions.
1306  map<G4String,G4AttDef>::const_iterator attDefIterator = attDefs->begin();
1307  while (attDefIterator != attDefs->end()) {
1308  definition->addAttDef(attDefIterator->first, attDefIterator->second.GetDesc(),
1309  attDefIterator->second.GetCategory(), attDefIterator->second.GetExtra());
1310  attDefIterator++;
1311  }
1312 }
1313 
1314 void G4HepRepSceneHandler::addAttVals(HepRepAttribute* attribute, const map<G4String,G4AttDef>* attDefs, vector<G4AttValue>* attValues) {
1315  if (attValues == NULL) return;
1316 
1317  // Copy the instance's G4AttValues to HepRepAttValues.
1318  for (vector<G4AttValue>::iterator attValIterator = attValues->begin(); attValIterator != attValues->end(); attValIterator++) {
1319  G4String name = attValIterator->GetName();
1320 
1321  HepRepPoint* point = dynamic_cast<HepRepPoint*>(attribute);
1322  if ((name == "Pos") && (point != NULL)) {
1323  G4String pos = attValIterator->GetValue();
1324 // cout << "Pos* " << pos << endl;
1325  int is = 0;
1326  int in = 0;
1327  int im = 0;
1328  G4String unit;
1329  for (unsigned int i=0; i<pos.length(); i++) {
1330  if (pos[i] == ' ') {
1331  if (in == 0) {
1332  // first coordinate
1333  double factor = atof(pos.substr(is, i-is).c_str())/point->getX();
1334  im = (int)(std::log10(factor)+((factor < 1) ? -0.5 : 0.5));
1335 // cout << factor << ", " << im << endl;
1336  } else if (in == 3) {
1337  // unit
1338  unit = pos.substr(is, i-is);
1339  if (unit == G4String("mum")) {
1340  im += -6;
1341  } else if (unit == G4String("mm")) {
1342  im += -3;
1343  } else if (unit == G4String("cm")) {
1344  im += -2;
1345  } else if (unit == G4String("m")) {
1346  im += 0;
1347  } else if (unit == G4String("km")) {
1348  im += 3;
1349  } else {
1350  cerr << "HepRepSceneHandler: Unrecognized Unit: '" << unit << "'" << endl;
1351  }
1352  }
1353  is = i+1;
1354  in++;
1355  }
1356  }
1357  switch(im) {
1358  case -6:
1359  unit = G4String("mum");
1360  break;
1361  case -3:
1362  unit = G4String("mm");
1363  break;
1364  case -2:
1365  unit = G4String("cm");
1366  break;
1367  case 0:
1368  unit = G4String("m");
1369  break;
1370  case 3:
1371  unit = G4String("km");
1372  break;
1373  default:
1374  cerr << "HepRepSceneHandler: No valid unit found for im: " << im << endl;
1375  unit = G4String("*im");
1376  break;
1377  }
1378 // cout << "U: " << unit << endl;
1379  setAttribute(attribute, G4String("PointUnit"), unit);
1380  continue;
1381  }
1382 
1383  // NTP already in points being written
1384  if (name == "NTP") continue;
1385 
1386  // find type of attribute using def
1387  const map<G4String,G4AttDef>::const_iterator attDefIterator = attDefs->find(name);
1388  G4String type = attDefIterator->second.GetValueType();
1389 
1390  // set based on type
1391  if ((type == "G4double") || (type == "double")) {
1392  setAttribute(attribute, attValIterator->GetName(), atof(attValIterator->GetValue()));
1393  } else if ((type == "G4int") || (type == "int")) {
1394  setAttribute(attribute, attValIterator->GetName(), atoi(attValIterator->GetValue()));
1395  } else { // G4String, string and others
1396  setAttribute(attribute, attValIterator->GetName(), attValIterator->GetValue());
1397  }
1398  }
1399 }
1400 
1401 
1403  G4PhysicalVolumeModel* pPVModel =
1404  dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
1405  return !pPVModel || fReadyForTransients || currentHit || currentTrack;
1406 }
1407 
1409 
1410  // Some non-standard attributes
1411  type->addAttDef( "Generator", "Generator of the file", "General", "");
1412  type->addAttValue("Generator", G4String("Geant4"));
1413 
1414  type->addAttDef( "GeneratorVersion", "Version of the Generator", "General", "");
1415  G4String versionString = G4Version;
1416  versionString = versionString.substr(1,versionString.size()-2);
1417  versionString = " Geant4 version " + versionString + " " + G4Date;
1418  type->addAttValue("GeneratorVersion", versionString);
1419 
1421  const G4Vector3D& viewPointDirection = parameters.GetViewpointDirection();
1422  type->addAttDef( "ViewTheta", "Theta of initial suggested viewpoint", "Draw", "rad");
1423  type->addAttValue("ViewTheta", viewPointDirection.theta());
1424 
1425  type->addAttDef( "ViewPhi", "Phi of initial suggested viewpoint", "Draw", "rad");
1426  type->addAttValue("ViewPhi", viewPointDirection.phi());
1427 
1428  type->addAttDef( "ViewScale", "Scale of initial suggested viewpoint", "Draw", "");
1429  type->addAttValue("ViewScale", parameters.GetZoomFactor());
1430 
1431 // FIXME, no way to set these
1432  type->addAttDef( "ViewTranslateX", "Translate in X of initial suggested viewpoint", "Draw", "");
1433  type->addAttValue("ViewTranslateX", 0.0);
1434 
1435  type->addAttDef( "ViewTranslateY", "Translate in Y of initial suggested viewpoint", "Draw", "");
1436  type->addAttValue("ViewTranslateY", 0.0);
1437 
1438  type->addAttDef( "ViewTranslateZ", "Translate in Z of initial suggested viewpoint", "Draw", "");
1439  type->addAttValue("ViewTranslateZ", 0.0);
1440 
1441  type->addAttDef( "PointUnit", "Length", "Physics", "");
1442  type->addAttValue("PointUnit", G4String("m"));
1443 
1445 
1446  type->addAttDef( "UseSolids", "Use HepRep Solids rather than Geant4 Primitives", "Draw", "");
1447  type->addAttValue("UseSolids", messenger->useSolids());
1448 
1449  type->addAttDef( "WriteInvisibles", "Write Invisible Objects", "Draw", "");
1450  type->addAttValue("WriteInvisibles", messenger->writeInvisibles());
1451 }
1452 
1453 
1455  if (isEventData()) {
1457  } else {
1458  G4PhysicalVolumeModel* pPVModel =
1459  dynamic_cast<G4PhysicalVolumeModel*>(fpModel);
1460  assert(pPVModel); // To keep Coverity happy.
1461  G4LogicalVolume* pCurrentLV = pPVModel->GetCurrentLV();
1462  G4int currentDepth = pPVModel->GetCurrentDepth();
1463  G4Material* pCurrentMaterial = pPVModel->GetCurrentMaterial();
1464  return getGeometryInstance(pCurrentLV, pCurrentMaterial, currentDepth);
1465  }
1466 }
1467 
1469  if (_heprep == NULL) {
1470  // Create the HepRep that holds the Trees.
1472  }
1473  return _heprep;
1474 }
1475 
1477  if (_heprepGeometry == NULL) {
1478  // Create the HepRep that holds the Trees.
1480  }
1481  return _heprepGeometry;
1482 }
1483 
1485  if (_geometryInstanceTree == NULL) {
1486  // Create the Geometry InstanceTree.
1488 
1490  if ( messenger->appendGeometry()) {
1492  } else {
1494  }
1495  }
1496  return _geometryInstanceTree;
1497 }
1498 
1500  if (_geometryRootInstance == NULL) {
1501  // Create the top level Geometry Instance.
1503  }
1504  return _geometryRootInstance;
1505 }
1506 
1508  HepRepInstance* instance = getGeometryInstance(volume->GetName(), depth);
1509 
1510  setAttribute(instance, "LVol", volume->GetName());
1511  G4Region* region = volume->GetRegion();
1512  G4String regionName = region? region->GetName(): G4String("No region");
1513  setAttribute(instance, "Region", regionName);
1514  setAttribute(instance, "RootRegion", volume->IsRootRegion());
1515  setAttribute(instance, "Solid", volume->GetSolid()->GetName());
1516  setAttribute(instance, "EType", volume->GetSolid()->GetEntityType());
1517  G4String matName = material? material->GetName(): G4String("No material");
1518  setAttribute(instance, "Material", matName );
1519  G4double matDensity = material? material->GetDensity(): 0.;
1520  setAttribute(instance, "Density", matDensity);
1521  G4double matRadlen = material? material->GetRadlen(): 0.;
1522  setAttribute(instance, "Radlen", matRadlen);
1523 
1524  G4State matState = material? material->GetState(): kStateUndefined;
1525  G4String state = materialState[matState];
1526  setAttribute(instance, "State", state);
1527 
1528  return instance;
1529 }
1530 
1532  // no extra checks since these are done in the geometryType already
1533 
1534  // adjust depth, also pop the current instance
1535  while ((int)_geometryInstance.size() > depth) {
1536  _geometryInstance.pop_back();
1537  }
1538 
1539  // get parent
1541 
1542  // get type
1543  HepRepType* type = getGeometryType(volumeName, depth);
1544 
1545  // create instance
1547  _geometryInstance.push_back(instance);
1548 
1549  return instance;
1550 }
1551 
1553  if (_geometryTypeTree == NULL) {
1554  // Create the Geometry TypeTree.
1555  HepRepTreeID* geometryTreeID = factory->createHepRepTreeID("G4GeometryTypes", "1.0");
1556  _geometryTypeTree = factory->createHepRepTypeTree(geometryTreeID);
1557 
1559  if ( messenger->appendGeometry()) {
1561  } else {
1563  }
1564  }
1565  return _geometryTypeTree;
1566 }
1567 
1569  if (_geometryRootType == NULL) {
1570  // Create the top level Geometry Type.
1573 
1574  // Add attdefs used by all geometry types.
1575  _geometryRootType->addAttDef ("LVol", "Logical Volume", "Physics","");
1576  _geometryRootType->addAttValue("LVol", G4String(""));
1577  _geometryRootType->addAttDef ("Region", "Cuts Region", "Physics","");
1578  _geometryRootType->addAttValue("Region", G4String(""));
1579  _geometryRootType->addAttDef ("RootRegion", "Root Region", "Physics","");
1580  _geometryRootType->addAttValue("RootRegion", false);
1581  _geometryRootType->addAttDef ("Solid", "Solid Name", "Physics","");
1582  _geometryRootType->addAttValue("Solid", G4String(""));
1583  _geometryRootType->addAttDef ("EType", "Entity Type", "Physics","");
1584  _geometryRootType->addAttValue("EType", G4String("G4Box"));
1585  _geometryRootType->addAttDef ("Material", "Material Name", "Physics","");
1586  _geometryRootType->addAttValue("Material", G4String("Air"));
1587  _geometryRootType->addAttDef ("Density", "Material Density", "Physics","");
1588  _geometryRootType->addAttValue("Density", 0.0);
1589  _geometryRootType->addAttDef ("State", "Material State", "Physics","");
1590  _geometryRootType->addAttValue("State", G4String("Gas"));
1591  _geometryRootType->addAttDef ("Radlen", "Material Radiation Length", "Physics","");
1592  _geometryRootType->addAttValue("Radlen", 0.0);
1593 
1594  // add defaults for Geometry
1595  _geometryRootType->addAttValue("Color", 0.8, 0.8, 0.8, 1.0);
1596  _geometryRootType->addAttValue("Visibility", true);
1597  _geometryRootType->addAttValue("FillColor", 0.8, 0.8, 0.8, 1.0);
1598  _geometryRootType->addAttValue("LineWidth", 1.0);
1599  _geometryRootType->addAttValue("DrawAs", G4String("Polygon"));
1600  _geometryRootType->addAttValue("PickParent", false);
1601  _geometryRootType->addAttValue("ShowParentAttributes", true);
1602 
1603  _geometryRootType->addAttValue("MarkSizeMultiplier", 4.0);
1604  _geometryRootType->addAttValue("LineWidthMultiplier", 1.0);
1605 
1607 
1609  }
1610  return _geometryRootType;
1611 }
1612 
1614  // make sure we have a root
1616 
1617  // construct the full name for this volume
1618  G4String name = getFullTypeName(volumeName, depth);
1619 
1620  // lookup type and create if necessary
1621  HepRepType* type = _geometryType[name];
1622  if (type == NULL) {
1623  G4String parentName = getParentTypeName(depth);
1624  HepRepType* parentType = _geometryType[parentName];
1625  // HepRep uses hierarchical names
1626  type = factory->createHepRepType(parentType, volumeName);
1627  _geometryType[name] = type;
1628  }
1629  return type;
1630 }
1631 
1633  // check for name depth
1634  if (depth > (int)_geometryTypeName.size()) {
1635  // there is a problem, book this type under problems
1636  G4String problem = "HierarchyProblem";
1637  if (_geometryType["/"+problem] == NULL) {
1638  // HepRep uses hierarchical names
1640  _geometryType["/"+problem] = type;
1641  }
1642  return "/" + problem + "/" + volumeName;
1643  }
1644 
1645  // adjust name depth, also pop the current volumeName
1646  while ((int)_geometryTypeName.size() > depth) {
1647  _geometryTypeName.pop_back();
1648  }
1649 
1650  // construct full name and push it
1652  name = name + "/" + volumeName;
1653  _geometryTypeName.push_back(name);
1654  return name;
1655 }
1656 
1658  return (depth >= 1) ? _geometryTypeName[depth-1] : G4String("/"+rootVolumeName);
1659 }
1660 
1662  if (_eventInstanceTree == NULL) {
1663  // Create the Event InstanceTree.
1666  }
1667  return _eventInstanceTree;
1668 }
1669 
1671  if (_eventInstance == NULL) {
1672  // Create the top level Event Instance.
1674  }
1675  return _eventInstance;
1676 }
1677 
1679  if (_eventTypeTree == NULL) {
1680  // Create the Event TypeTree.
1681  HepRepTreeID* eventTreeID = factory->createHepRepTreeID("G4EventTypes", "1.0");
1684  }
1685 
1686  return _eventTypeTree;
1687 }
1688 
1690  if (_eventType == NULL) {
1691  // Create the top level Event Type.
1693  _eventType->addAttValue("Layer", eventLayer);
1694 
1695  // add defaults for Events
1696  _eventType->addAttValue("Visibility", true);
1697  _eventType->addAttValue("Color", 1.0, 1.0, 1.0, 1.0);
1698  _eventType->addAttValue("FillColor", 1.0, 1.0, 1.0, 1.0);
1699  _eventType->addAttValue("LineWidth", 1.0);
1700  _eventType->addAttValue("HasFrame", true);
1701  _eventType->addAttValue("PickParent", false);
1702  _eventType->addAttValue("ShowParentAttributes", false);
1703 
1704  _eventType->addAttValue("MarkSizeMultiplier", 4.0);
1705  _eventType->addAttValue("LineWidthMultiplier", 1.0);
1706 
1708  }
1709 
1710  return _eventType;
1711 }
1712 
1714  if (_trajectoryType == NULL) {
1716 
1718  _trajectoryType->addAttValue("DrawAs", G4String("Line"));
1719 
1720  _trajectoryType->addAttValue("LineWidthMultiplier", 2.0);
1721 
1722  // attributes to draw the points of a track as markers.
1723  _trajectoryType->addAttValue("MarkName", G4String("Box"));
1724  _trajectoryType->addAttValue("MarkSize", 4);
1725  _trajectoryType->addAttValue("MarkType", G4String("Symbol"));
1726  _trajectoryType->addAttValue("Fill", true);
1727  }
1728  return _trajectoryType;
1729 }
1730 
1732  if (_hitType == NULL) {
1734  _hitType->addAttValue("Layer", hitLayer);
1735  _hitType->addAttValue("DrawAs", G4String("Point"));
1736  _hitType->addAttValue("MarkName", G4String("Box"));
1737  _hitType->addAttValue("MarkSize", 4.0);
1738  _hitType->addAttValue("MarkType", G4String("Symbol"));
1739  _hitType->addAttValue("Fill", true);
1740  }
1741  return _hitType;
1742 }
1743 
1745  if (_calHitType == NULL) {
1747  _calHitType->addAttValue("Layer", calHitLayer);
1748  _calHitType->addAttValue("Fill", true);
1749  _calHitType->addAttValue("DrawAs", G4String("Polygon"));
1750  }
1751  return _calHitType;
1752 }
1753 
1755  if (_calHitFaceType == NULL) {
1757  _calHitFaceType->addAttValue("PickParent", true);
1758  }
1759  return _calHitFaceType;
1760 }
1761