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