ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4HepRepFileXMLWriter.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4HepRepFileXMLWriter.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 // File and Version Information:
28 //
29 // Description:
30 // Create a HepRep XML File (HepRep version 1).
31 //
32 // Environment:
33 // Software developed for the general High Energy Physics community.
34 //
35 // Author :
36 // J. Perl Original Author
37 //
38 // Copyright Information:
39 // Copyright (C) 2001 Stanford Linear Accelerator Center
40 //------------------------------------------------------------------------
41 
42 #include "G4HepRepFileXMLWriter.hh"
43 
44 #include "G4HepRepMessenger.hh"
45 #include "G4ios.hh"
46 
48 {
49  isOpen = false;
50  init();
51 }
52 
54 {
55  typeDepth = -1;
56 
57  int i = -1;
58  while (i++<49) {
59  prevTypeName[i] = new char[1];
60  strcpy(prevTypeName[i],"");
61 
62  inType[i] = false;
63  inInstance[i] = false;
64  }
65 
66  inPrimitive = false;
67  inPoint = false;
68 }
69 
70 void G4HepRepFileXMLWriter::addType(const char* name,int newTypeDepth)
71 {
72  if (fout.good())
73  {
74  // Flatten structure if it exceeds maximum allowed typeDepth of 49.
75  if (newTypeDepth > 49)
76  newTypeDepth = 49;
77 
78  if (newTypeDepth < 0)
79  newTypeDepth = 0;
80 
81  // Insert any layers that are missing from the hierarchy (protects against
82  // callers that skip from, say, layer 1 to layer 3 with no layer 2).
83  while (typeDepth < (newTypeDepth-1)) {
84  addType("Layer Inserted by G4HepRepFileXMLWriter", typeDepth + 1);
85  addInstance();
86  }
87 
88  // If moving closer to the root, close previously open types.
89  while (newTypeDepth<typeDepth)
90  endType();
91 
92  // Close any remaining primitives of the current instance.
93  endPrimitive();
94 
95  // If this is a new type name for the current depth, declare the
96  // new Type. Otherwise, it is just another Instance of the current Type.
97  if (strcmp(name,prevTypeName[newTypeDepth])!=0)
98  {
99  if (inType[newTypeDepth])
100  endType();
101 
102  prevTypeName[newTypeDepth] = new char[strlen(name)+1];
103  strcpy(prevTypeName[newTypeDepth],name);
104 
105  inType[newTypeDepth] = true;
106  indent();
107  fout << "<heprep:type version=\"null\" name=\"" << name << "\">"
108  << G4endl;
109 
110  typeDepth = newTypeDepth;
111  }
112  } else {
113 #ifdef G4HEPREPFILEDEBUG
114  G4cout << "G4HepRepFileXMLWriter:addType No file is currently open." << G4endl;
115 #endif
116  }
117 }
118 
120 {
121  if (fout.good())
122  {
123  if (inType[typeDepth])
124  {
125  endInstance();
126  inInstance[typeDepth] = true;
127  indent();
128  fout << "<heprep:instance>" << G4endl;
129  } else {
130 #ifdef G4HEPREPFILEDEBUG
131  G4cout << "G4HepRepFileXMLWriter:addInstance No HepRep Type is currently open" << G4endl;
132 #endif
133  }
134  } else {
135 #ifdef G4HEPREPFILEDEBUG
136  G4cout << "G4HepRepFileXMLWriter:addInstance No file is currently open" << G4endl;
137 #endif
138  }
139 }
140 
142 {
143  if (fout.good())
144  {
145  if (inInstance[typeDepth])
146  {
147  endPrimitive();
148  inPrimitive = true;
149  indent();
150  fout << "<heprep:primitive>" << G4endl;
151  } else {
152 #ifdef G4HEPREPFILEDEBUG
153  G4cout << "G4HepRepFileXMLWriter:addPrimitive No HepRep Instance is currently open" << G4endl;
154 #endif
155  }
156  } else {
157 #ifdef G4HEPREPFILEDEBUG
158  G4cout << "G4HepRepFileXMLWriter:addPrimitive No file is currently open" << G4endl;
159 #endif
160  }
161 }
162 
163 void G4HepRepFileXMLWriter::addPoint(double x, double y, double z)
164 {
165  if (fout.good())
166  {
167  if (inPrimitive)
168  {
169  endPoint();
170  inPoint = true;
171  indent();
172 
173  // Include scale and center values
175  G4double scale = messenger->getScale();
176  G4ThreeVector center = messenger->getCenter();
177  G4double xNew = scale * ( x - center.x());
178  G4double yNew = scale * ( y - center.y());
179  G4double zNew = scale * ( z - center.z());
180 
181  fout << "<heprep:point x=\"" << xNew << "\" y=\"" << yNew << "\" z=\"" << zNew << "\">" << G4endl;
182  } else {
183 #ifdef G4HEPREPFILEDEBUG
184  G4cout << "G4HepRepFileXMLWriter:addPoint No HepRep Primitive is currently open" << G4endl;
185 #endif
186  }
187  } else {
188 #ifdef G4HEPREPFILEDEBUG
189  G4cout << "G4HepRepFileXMLWriter:addPoint No file is currently open" << G4endl;
190 #endif
191  }
192 }
193 
195  const char* desc,
196  const char* type,
197  const char* extra)
198 {
199  if (fout.good())
200  {
201  indent();
202  fout << " <heprep:attdef extra=\"" << extra << "\" name=\"" << name << "\" type=\"" << type << "\"" << G4endl;
203  indent();
204  fout << " desc=\"" << desc << "\"/>" << G4endl;
205  } else {
206 #ifdef G4HEPREPFILEDEBUG
207  G4cout << "G4HepRepFileXMLWriter:addAttDef No file is currently open" << G4endl;
208 #endif
209  }
210 }
211 
212 // Four methods to fill attValues
214  const char* value)
215 {
216  if (fout.good())
217  {
218  indent();
219  fout << " <heprep:attvalue showLabel=\"NONE\" name=\"" << name << "\"" << G4endl;
220  indent();
221  fout << " value=\"" << value << "\"/>" << G4endl;
222  } else {
223 #ifdef G4HEPREPFILEDEBUG
224  G4cout << "G4HepRepFileXMLWriter:addAttValue No file is currently open" << G4endl;
225 #endif
226  }
227 }
228 
230  double value)
231 {
232  if (fout.good())
233  {
234  indent();
235  fout << " <heprep:attvalue showLabel=\"NONE\" name=\"" << name << "\"" << G4endl;
236  indent();
237  fout << " value=\"" << value << "\"/>" << G4endl;
238  } else {
239 #ifdef G4HEPREPFILEDEBUG
240  G4cout << "G4HepRepFileXMLWriter:addAttValue No file is currently open" << G4endl;
241 #endif
242  }
243 }
244 
246  int value)
247 {
248  if (fout.good())
249  {
250  indent();
251  fout << " <heprep:attvalue showLabel=\"NONE\" name=\"" << name << "\"" << G4endl;
252  indent();
253  fout << " value=\"" << value << "\"/>" << G4endl;
254  } else {
255 #ifdef G4HEPREPFILEDEBUG
256  G4cout << "G4HepRepFileXMLWriter:addAttValue No file is currently open" << G4endl;
257 #endif
258  }
259 }
260 
262  bool value)
263 {
264  if (fout.good())
265  {
266  indent();
267  fout << " <heprep:attvalue showLabel=\"NONE\" name=\"" << name << "\"" << G4endl;
268  indent();
269  if (value)
270  fout << " value=\"True\"/>" << G4endl;
271  else
272  fout << " value=\"False\"/>" << G4endl;
273  } else {
274 #ifdef G4HEPREPFILEDEBUG
275  G4cout << "G4HepRepFileXMLWriter:addAttValue No file is currently open" << G4endl;
276 #endif
277  }
278 }
279 
281  double value1,
282  double value2,
283  double value3)
284 {
285  if (fout.good())
286  {
287  int redness = int(value1*255.);
288  int greenness = int(value2*255.);
289  int blueness = int(value3*255.);
290  indent();
291  fout << " <heprep:attvalue showLabel=\"NONE\" name=\"" << name << "\"" << G4endl;
292  indent();
293  fout << " value=\"" << redness << "," << greenness << "," << blueness << "\"/>" << G4endl;
294  } else {
295 #ifdef G4HEPREPFILEDEBUG
296  G4cout << "G4HepRepFileXMLWriter:addAttValue No file is currently open" << G4endl;
297 #endif
298  }
299 }
300 
301 void G4HepRepFileXMLWriter::open(const char* fileSpec)
302 {
303  if (isOpen)
304  close();
305 
306  fout.open(fileSpec);
307 
308  if (fout.good()) {
309  fout << "<?xml version=\"1.0\" ?>" << G4endl;
310  fout << "<heprep:heprep xmlns:heprep=\"http://www.slac.stanford.edu/~perl/heprep/\"" << G4endl;
311  fout << " xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\" xsi:schemaLocation=\"HepRep.xsd\">" << G4endl;
312 
313  isOpen = true;
314  init();
315  } else {
316  G4cout << "G4HepRepFileXMLWriter:open Unable to write to file " << fileSpec << G4endl;
317  }
318 }
319 
321 {
322  // Close any remaining open Types
323  endTypes();
324 
325  if (fout.good()) {
326  fout << "</heprep:heprep>" << G4endl;
327  fout.close( );
328  isOpen = false;
329  } else {
330  G4cout << "G4HepRepFileXMLWriter:close No file is currently open" << G4endl;
331  }
332 }
333 
335 {
336  // Close any remaining open Types
337  while(typeDepth>-1)
338  endType();
339 }
340 
342 {
343  endInstance();
344  indent();
345  fout << "</heprep:type>" << G4endl;
346  inType[typeDepth] = false;
347  delete [] prevTypeName[typeDepth];
348  prevTypeName[typeDepth] = new char[1];
349  strcpy(prevTypeName[typeDepth],"");
350  typeDepth--;
351 }
352 
354 {
355  if (inInstance[typeDepth])
356  {
357  endPrimitive();
358  indent();
359  fout << "</heprep:instance>" << G4endl;
360  inInstance[typeDepth] = false;
361  }
362 }
363 
365 {
366  if (inPrimitive)
367  {
368  endPoint();
369  indent();
370  fout << "</heprep:primitive>" << G4endl;
371  inPrimitive = false;
372  }
373 }
374 
376 {
377  if (inPoint)
378  {
379  indent();
380  fout << "</heprep:point>" << G4endl;
381  inPoint = false;
382  }
383 }
384 
386 {
387  if (fout.good())
388  {
389  int i = 0;
390  while (inType[i] && i<12) {
391  fout << " ";
392  if (inInstance[i])
393  fout << " ";
394  i++;
395  }
396 
397  if (inPrimitive)
398  fout << " ";
399  if (inPoint)
400  fout << " ";
401  }
402 }