82 using namespace HEPREP;
83 using namespace cheprep;
95 geometryLayer (
"Geometry"),
97 calHitLayer (
"CalHit"),
98 trajectoryLayer (
"Trajectory"),
100 rootVolumeName (
"Geometry"),
102 eventNumberPrefix (
""),
103 eventNumberSuffix (
""),
105 eventNumberWidth (-1),
110 writeMultipleFiles (
false),
114 _heprepGeometry (NULL)
118 cout <<
"G4HepRepSceneHandler::G4HepRepSceneHandler: " << system << endl;
136 cout <<
"G4HepRepSceneHandler::~G4HepRepSceneHandler() " << endl;
149 if (
writer != NULL)
return;
151 if (name ==
"stdout") {
153 cout <<
"G4HepRepSceneHandler::Open() stdout" << endl;
167 }
else if (name ==
"stderr") {
169 cout <<
"G4HepRepSceneHandler::Open() stderr" << endl;
185 cout <<
"G4HepRepSceneHandler::Open() " << name << endl;
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"};
194 while (i < numberOfExtensions) {
195 int dot = name.size() - ext[i].size();
197 (name.substr(dot, ext[i].size()) == ext[i]))
break;
201 if (i != numberOfExtensions) {
205 writeGZ = (i == 3) || (i == 7);
207 int dot = name.length() -
extension.length();
208 baseName = (dot >= 0) ? name.substr(0, dot) :
"";
220 int startDigit = -1;
int endDigit = -1;
227 endDigit = suffix.length()-1;
228 while (endDigit >= 0) {
229 if (isdigit(suffix.at(endDigit)))
break;
233 cerr <<
"/vis/heprep/appendEventNumberSuffix contains no digits" << endl;
236 startDigit = endDigit;
237 while (startDigit >= 0) {
238 if (!isdigit(suffix.at(startDigit)))
break;
247 eventNumber = atoi(suffix.substr(startDigit, endDigit).c_str());
266 cout <<
"G4HepRepSceneHandler::OpenHepRep() " << endl;
295 if (
_heprep == NULL)
return true;
298 cout <<
"G4HepRepSceneHandler::CloseHepRep() start" << endl;
304 cerr <<
"WARNING: you probably used '/vis/viewer/endOfEventAction accumulate' and "
305 <<
"forgot to call /vis/viewer/update before exit. No event written." << endl;
326 sprintf(name,
"%s%s%s#%s",
baseName.c_str(),
"-geometry",
extension.c_str(),
"G4GeometryData");
328 sprintf(name,
"%s%s#%s",
"geometry", (
writeBinary ?
".bheprep" :
".heprep"),
"G4GeometryData");
362 sprintf(name,
"%s%s",
"geometry", (
writeBinary ?
".bheprep" :
".heprep"));
382 char fileFormat[128];
395 char eventFormat[128];
415 cout <<
"G4HepRepSceneHandler::Close() " << endl;
418 if (
writer == NULL)
return;
430 out =
new ofstream(name.c_str(), std::ios::out | std::ios::binary );
444 if (heprep == NULL)
return;
454 cout <<
"G4HepRepSceneHandler::BeginModeling() " << endl;
462 cout <<
"G4HepRepSceneHandler::EndModeling() " << endl;
469 cout <<
"G4HepRepSceneHandler::AddSolid(const G4Box& box)" << endl;
525 cout <<
"G4HepRepSceneHandler::AddSolid(const G4Cons& cons)" << endl;
566 outer->addAttValue(
"showParentAttributes",
true);
589 cout <<
"G4HepRepSceneHandler::AddSolid(const G4Tubs& tubs)" << endl;
630 outer->addAttValue(
"pickParent",
true);
631 outer->addAttValue(
"showParentAttributes",
true);
649 cout <<
"G4HepRepSceneHandler::AddSolid(const G4Trd& trd)" << endl;
758 cout <<
"G4HepRepSceneHandler::AddPrimitive(G4Polyline&) " << line.size() << endl;
763 static G4bool warned =
false;
767 (
"G4HepRepSceneHandler::AddPrimitive (const G4Polyline&)",
769 "2D polylines not implemented. Ignored.");
785 for (
size_t i=0; i < line.size(); i++) {
795 cout <<
"G4HepRepSceneHandler::AddPrimitive(G4Polymarker&) " << line.size() << endl;
800 static G4bool warned =
false;
804 (
"G4HepRepSceneHandler::AddPrimitive (const G4Polymarker&)",
806 "2D polymarkers not implemented. Ignored.");
826 if (mtype == line.
dots) {
829 }
else if (mtype == line.
circles) {
830 }
else if (mtype == line.
squares) {
837 for (
size_t i=0; i < line.size(); i++) {
846 cout <<
"G4HepRepSceneHandler::AddPrimitive(G4Circle&) " << endl;
851 static G4bool warned =
false;
855 (
"G4HepRepSceneHandler::AddPrimitive (const G4Circle&)",
857 "2D circles not implemented. Ignored.");
882 cout <<
"G4HepRepSceneHandler::AddPrimitive(G4Polyhedron&) " << endl;
887 static G4bool warned =
false;
891 (
"G4HepRepSceneHandler::AddPrimitive (const G4Polyhedron&)",
893 "2D polyhedra not implemented. Ignored.");
909 G4int currentDepth = 0;
938 }
while (notLastEdge);
939 }
while (notLastFace);
945 cout <<
"G4HepRepSceneHandler::AddPrimitive(G4Text&) " << endl;
963 cout <<
"G4HepRepSceneHandler::AddPrimitive G4Text : not yet implemented. " << endl;
969 cout <<
"G4HepRepSceneHandler::AddPrimitive(G4Square&) " << endl;
974 static G4bool warned =
false;
978 (
"G4HepRepSceneHandler::AddPrimitive (const G4Square&)",
980 "2D squares not implemented. Ignored.");
1007 cout <<
"G4HepRepSceneHandler::AddCompound(G4VTrajectory&) " << endl;
1019 cout <<
"G4HepRepSceneHandler::AddCompound(G4VHit&) " << endl;
1035 cout <<
"G4HepRepSceneHandler::PreAddSolid(G4Transform3D&, G4VisAttributes&)" << endl;
1042 cout <<
"G4HepRepSceneHandler::PostAddSolid()" << endl;
1050 cout <<
"G4HepRepSceneHandler::BeginPrimitives(G4Transform3D&)" << endl;
1060 cout <<
"G4HepRepSceneHandler::EndPrimitives" << endl;
1075 cout <<
"G4HepRepSceneHandler::setColor : red : " << color.
GetRed () <<
1076 " green : " << color.
GetGreen () <<
1077 " blue : " << color.
GetBlue () << endl;
1158 addAttVals(instance, hitAttDefs, hitAttValues);
1160 delete hitAttValues;
1178 addAttVals(instance, trajectoryAttDefs, trajectoryAttValues);
1180 delete trajectoryAttValues;
1189 if (point != NULL) {
1196 if (instance != NULL) {
1199 attribute = instance->
getType();
1211 if (point != NULL) {
1218 if (instance != NULL) {
1221 attribute = instance->
getType();
1233 if (point != NULL) {
1240 if (instance != NULL) {
1243 attribute = instance->
getType();
1255 if (point != NULL) {
1262 if (instance != NULL) {
1265 attribute = instance->
getType();
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))) {
1284 if (point != NULL) {
1291 if (instance != NULL) {
1294 attribute = instance->
getType();
1298 attribute->
addAttValue(name, red, green, blue, alpha);
1303 if (attDefs == NULL)
return;
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());
1315 if (attValues == NULL)
return;
1318 for (vector<G4AttValue>::iterator attValIterator = attValues->begin(); attValIterator != attValues->end(); attValIterator++) {
1322 if ((name ==
"Pos") && (point != NULL)) {
1329 for (
unsigned int i=0; i<pos.length(); i++) {
1330 if (pos[i] ==
' ') {
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));
1336 }
else if (in == 3) {
1338 unit = pos.substr(is, i-is);
1341 }
else if (unit ==
G4String(
"mm")) {
1343 }
else if (unit ==
G4String(
"cm")) {
1345 }
else if (unit ==
G4String(
"m")) {
1347 }
else if (unit ==
G4String(
"km")) {
1350 cerr <<
"HepRepSceneHandler: Unrecognized Unit: '" << unit <<
"'" << endl;
1374 cerr <<
"HepRepSceneHandler: No valid unit found for im: " << im << endl;
1384 if (name ==
"NTP")
continue;
1387 const map<G4String,G4AttDef>::const_iterator attDefIterator = attDefs->find(name);
1388 G4String type = attDefIterator->second.GetValueType();
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()));
1396 setAttribute(attribute, attValIterator->GetName(), attValIterator->GetValue());
1411 type->
addAttDef(
"Generator",
"Generator of the file",
"General",
"");
1414 type->
addAttDef(
"GeneratorVersion",
"Version of the Generator",
"General",
"");
1416 versionString = versionString.substr(1,versionString.size()-2);
1417 versionString =
" Geant4 version " + versionString +
" " +
G4Date;
1418 type->
addAttValue(
"GeneratorVersion", versionString);
1422 type->
addAttDef(
"ViewTheta",
"Theta of initial suggested viewpoint",
"Draw",
"rad");
1425 type->
addAttDef(
"ViewPhi",
"Phi of initial suggested viewpoint",
"Draw",
"rad");
1428 type->
addAttDef(
"ViewScale",
"Scale of initial suggested viewpoint",
"Draw",
"");
1432 type->
addAttDef(
"ViewTranslateX",
"Translate in X of initial suggested viewpoint",
"Draw",
"");
1435 type->
addAttDef(
"ViewTranslateY",
"Translate in Y of initial suggested viewpoint",
"Draw",
"");
1438 type->
addAttDef(
"ViewTranslateZ",
"Translate in Z of initial suggested viewpoint",
"Draw",
"");
1441 type->
addAttDef(
"PointUnit",
"Length",
"Physics",
"");
1446 type->
addAttDef(
"UseSolids",
"Use HepRep Solids rather than Geant4 Primitives",
"Draw",
"");
1449 type->
addAttDef(
"WriteInvisibles",
"Write Invisible Objects",
"Draw",
"");
1636 G4String problem =
"HierarchyProblem";
1642 return "/" + problem +
"/" + volumeName;
1652 name = name +
"/" + volumeName;