00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "G4HepRepFileXMLWriter.hh"
00044
00045 #include "G4HepRepMessenger.hh"
00046 #include "G4ios.hh"
00047
00048 G4HepRepFileXMLWriter::G4HepRepFileXMLWriter()
00049 {
00050 isOpen = false;
00051 init();
00052 }
00053
00054 void G4HepRepFileXMLWriter::init()
00055 {
00056 typeDepth = -1;
00057
00058 int i = -1;
00059 while (i++<49) {
00060 prevTypeName[i] = new char[1];
00061 strcpy(prevTypeName[i],"");
00062
00063 inType[i] = false;
00064 inInstance[i] = false;
00065 }
00066
00067 inPrimitive = false;
00068 inPoint = false;
00069 }
00070
00071 void G4HepRepFileXMLWriter::addType(const char* name,int newTypeDepth)
00072 {
00073 if (fout.good())
00074 {
00075
00076 if (newTypeDepth > 49)
00077 newTypeDepth = 49;
00078
00079 if (newTypeDepth < 0)
00080 newTypeDepth = 0;
00081
00082
00083
00084 while (typeDepth < (newTypeDepth-1)) {
00085 addType("Layer Inserted by G4HepRepFileXMLWriter", typeDepth + 1);
00086 addInstance();
00087 }
00088
00089
00090 while (newTypeDepth<typeDepth)
00091 endType();
00092
00093
00094 endPrimitive();
00095
00096
00097
00098 if (strcmp(name,prevTypeName[newTypeDepth])!=0)
00099 {
00100 if (inType[newTypeDepth])
00101 endType();
00102
00103 prevTypeName[newTypeDepth] = new char[strlen(name)+1];
00104 strcpy(prevTypeName[newTypeDepth],name);
00105
00106 inType[newTypeDepth] = true;
00107 indent();
00108 fout << "<heprep:type version=\"null\" name=\"" << name << "\">"
00109 << G4endl;
00110
00111 typeDepth = newTypeDepth;
00112 }
00113 } else {
00114 #ifdef G4HEPREPFILEDEBUG
00115 G4cout << "G4HepRepFileXMLWriter:addType No file is currently open." << G4endl;
00116 #endif
00117 }
00118 }
00119
00120 void G4HepRepFileXMLWriter::addInstance()
00121 {
00122 if (fout.good())
00123 {
00124 if (inType[typeDepth])
00125 {
00126 endInstance();
00127 inInstance[typeDepth] = true;
00128 indent();
00129 fout << "<heprep:instance>" << G4endl;
00130 } else {
00131 #ifdef G4HEPREPFILEDEBUG
00132 G4cout << "G4HepRepFileXMLWriter:addInstance No HepRep Type is currently open" << G4endl;
00133 #endif
00134 }
00135 } else {
00136 #ifdef G4HEPREPFILEDEBUG
00137 G4cout << "G4HepRepFileXMLWriter:addInstance No file is currently open" << G4endl;
00138 #endif
00139 }
00140 }
00141
00142 void G4HepRepFileXMLWriter::addPrimitive()
00143 {
00144 if (fout.good())
00145 {
00146 if (inInstance[typeDepth])
00147 {
00148 endPrimitive();
00149 inPrimitive = true;
00150 indent();
00151 fout << "<heprep:primitive>" << G4endl;
00152 } else {
00153 #ifdef G4HEPREPFILEDEBUG
00154 G4cout << "G4HepRepFileXMLWriter:addPrimitive No HepRep Instance is currently open" << G4endl;
00155 #endif
00156 }
00157 } else {
00158 #ifdef G4HEPREPFILEDEBUG
00159 G4cout << "G4HepRepFileXMLWriter:addPrimitive No file is currently open" << G4endl;
00160 #endif
00161 }
00162 }
00163
00164 void G4HepRepFileXMLWriter::addPoint(double x, double y, double z)
00165 {
00166 if (fout.good())
00167 {
00168 if (inPrimitive)
00169 {
00170 endPoint();
00171 inPoint = true;
00172 indent();
00173
00174
00175 G4HepRepMessenger* messenger = G4HepRepMessenger::GetInstance();
00176 G4double scale = messenger->getScale();
00177 G4ThreeVector center = messenger->getCenter();
00178 G4double xNew = scale * ( x - center.x());
00179 G4double yNew = scale * ( y - center.y());
00180 G4double zNew = scale * ( z - center.z());
00181
00182 fout << "<heprep:point x=\"" << xNew << "\" y=\"" << yNew << "\" z=\"" << zNew << "\">" << G4endl;
00183 } else {
00184 #ifdef G4HEPREPFILEDEBUG
00185 G4cout << "G4HepRepFileXMLWriter:addPoint No HepRep Primitive is currently open" << G4endl;
00186 #endif
00187 }
00188 } else {
00189 #ifdef G4HEPREPFILEDEBUG
00190 G4cout << "G4HepRepFileXMLWriter:addPoint No file is currently open" << G4endl;
00191 #endif
00192 }
00193 }
00194
00195 void G4HepRepFileXMLWriter::addAttDef(const char* name,
00196 const char* desc,
00197 const char* type,
00198 const char* extra)
00199 {
00200 if (fout.good())
00201 {
00202 indent();
00203 fout << " <heprep:attdef extra=\"" << extra << "\" name=\"" << name << "\" type=\"" << type << "\"" << G4endl;
00204 indent();
00205 fout << " desc=\"" << desc << "\"/>" << G4endl;
00206 } else {
00207 #ifdef G4HEPREPFILEDEBUG
00208 G4cout << "G4HepRepFileXMLWriter:addAttDef No file is currently open" << G4endl;
00209 #endif
00210 }
00211 }
00212
00213
00214 void G4HepRepFileXMLWriter::addAttValue (const char* name,
00215 const char* value)
00216 {
00217 if (fout.good())
00218 {
00219 indent();
00220 fout << " <heprep:attvalue showLabel=\"NONE\" name=\"" << name << "\"" << G4endl;
00221 indent();
00222 fout << " value=\"" << value << "\"/>" << G4endl;
00223 } else {
00224 #ifdef G4HEPREPFILEDEBUG
00225 G4cout << "G4HepRepFileXMLWriter:addAttValue No file is currently open" << G4endl;
00226 #endif
00227 }
00228 }
00229
00230 void G4HepRepFileXMLWriter::addAttValue (const char* name,
00231 double value)
00232 {
00233 if (fout.good())
00234 {
00235 indent();
00236 fout << " <heprep:attvalue showLabel=\"NONE\" name=\"" << name << "\"" << G4endl;
00237 indent();
00238 fout << " value=\"" << value << "\"/>" << G4endl;
00239 } else {
00240 #ifdef G4HEPREPFILEDEBUG
00241 G4cout << "G4HepRepFileXMLWriter:addAttValue No file is currently open" << G4endl;
00242 #endif
00243 }
00244 }
00245
00246 void G4HepRepFileXMLWriter::addAttValue (const char* name,
00247 int value)
00248 {
00249 if (fout.good())
00250 {
00251 indent();
00252 fout << " <heprep:attvalue showLabel=\"NONE\" name=\"" << name << "\"" << G4endl;
00253 indent();
00254 fout << " value=\"" << value << "\"/>" << G4endl;
00255 } else {
00256 #ifdef G4HEPREPFILEDEBUG
00257 G4cout << "G4HepRepFileXMLWriter:addAttValue No file is currently open" << G4endl;
00258 #endif
00259 }
00260 }
00261
00262 void G4HepRepFileXMLWriter::addAttValue (const char* name,
00263 bool value)
00264 {
00265 if (fout.good())
00266 {
00267 indent();
00268 fout << " <heprep:attvalue showLabel=\"NONE\" name=\"" << name << "\"" << G4endl;
00269 indent();
00270 if (value)
00271 fout << " value=\"True\"/>" << G4endl;
00272 else
00273 fout << " value=\"False\"/>" << G4endl;
00274 } else {
00275 #ifdef G4HEPREPFILEDEBUG
00276 G4cout << "G4HepRepFileXMLWriter:addAttValue No file is currently open" << G4endl;
00277 #endif
00278 }
00279 }
00280
00281 void G4HepRepFileXMLWriter::addAttValue (const char* name,
00282 double value1,
00283 double value2,
00284 double value3)
00285 {
00286 if (fout.good())
00287 {
00288 int redness = int(value1*255.);
00289 int greenness = int(value2*255.);
00290 int blueness = int(value3*255.);
00291 indent();
00292 fout << " <heprep:attvalue showLabel=\"NONE\" name=\"" << name << "\"" << G4endl;
00293 indent();
00294 fout << " value=\"" << redness << "," << greenness << "," << blueness << "\"/>" << G4endl;
00295 } else {
00296 #ifdef G4HEPREPFILEDEBUG
00297 G4cout << "G4HepRepFileXMLWriter:addAttValue No file is currently open" << G4endl;
00298 #endif
00299 }
00300 }
00301
00302 void G4HepRepFileXMLWriter::open(const char* fileSpec)
00303 {
00304 if (isOpen)
00305 close();
00306
00307 fout.open(fileSpec);
00308
00309 if (fout.good()) {
00310 fout << "<?xml version=\"1.0\" ?>" << G4endl;
00311 fout << "<heprep:heprep xmlns:heprep=\"http://www.slac.stanford.edu/~perl/heprep/\"" << G4endl;
00312 fout << " xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\" xsi:schemaLocation=\"HepRep.xsd\">" << G4endl;
00313
00314 isOpen = true;
00315 init();
00316 } else {
00317 G4cout << "G4HepRepFileXMLWriter:open Unable to write to file " << fileSpec << G4endl;
00318 }
00319 }
00320
00321 void G4HepRepFileXMLWriter::close()
00322 {
00323
00324 endTypes();
00325
00326 if (fout.good()) {
00327 fout << "</heprep:heprep>" << G4endl;
00328 fout.close( );
00329 isOpen = false;
00330 } else {
00331 G4cout << "G4HepRepFileXMLWriter:close No file is currently open" << G4endl;
00332 }
00333 }
00334
00335 void G4HepRepFileXMLWriter::endTypes()
00336 {
00337
00338 while(typeDepth>-1)
00339 endType();
00340 }
00341
00342 void G4HepRepFileXMLWriter::endType()
00343 {
00344 endInstance();
00345 indent();
00346 fout << "</heprep:type>" << G4endl;
00347 inType[typeDepth] = false;
00348 delete [] prevTypeName[typeDepth];
00349 prevTypeName[typeDepth] = new char[1];
00350 strcpy(prevTypeName[typeDepth],"");
00351 typeDepth--;
00352 }
00353
00354 void G4HepRepFileXMLWriter::endInstance()
00355 {
00356 if (inInstance[typeDepth])
00357 {
00358 endPrimitive();
00359 indent();
00360 fout << "</heprep:instance>" << G4endl;
00361 inInstance[typeDepth] = false;
00362 }
00363 }
00364
00365 void G4HepRepFileXMLWriter::endPrimitive()
00366 {
00367 if (inPrimitive)
00368 {
00369 endPoint();
00370 indent();
00371 fout << "</heprep:primitive>" << G4endl;
00372 inPrimitive = false;
00373 }
00374 }
00375
00376 void G4HepRepFileXMLWriter::endPoint()
00377 {
00378 if (inPoint)
00379 {
00380 indent();
00381 fout << "</heprep:point>" << G4endl;
00382 inPoint = false;
00383 }
00384 }
00385
00386 void G4HepRepFileXMLWriter::indent()
00387 {
00388 if (fout.good())
00389 {
00390 int i = 0;
00391 while (inType[i] && i<12) {
00392 fout << " ";
00393 if (inInstance[i])
00394 fout << " ";
00395 i++;
00396 }
00397
00398 if (inPrimitive)
00399 fout << " ";
00400 if (inPoint)
00401 fout << " ";
00402 }
00403 }