Geant4 Cross Reference |
1 // 1 2 // ******************************************* 3 // * License and Disclaimer 4 // * 5 // * The Geant4 software is copyright of th 6 // * the Geant4 Collaboration. It is provided 7 // * conditions of the Geant4 Software License 8 // * LICENSE and available at http://cern.ch/ 9 // * include a list of copyright holders. 10 // * 11 // * Neither the authors of this software syst 12 // * institutes,nor the agencies providing fin 13 // * work make any representation or warran 14 // * regarding this software system or assum 15 // * use. Please see the license in the file 16 // * for the full disclaimer and the limitatio 17 // * 18 // * This code implementation is the result 19 // * technical work of the GEANT4 collaboratio 20 // * By using, copying, modifying or distri 21 // * any work based on the software) you ag 22 // * use in resulting scientific publicati 23 // * acceptance of all terms of the Geant4 Sof 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 En 34 // 35 // Author : 36 // J. Perl Original A 37 // 38 // Copyright Information: 39 // Copyright (C) 2001 Stanford L 40 //-------------------------------------------- 41 42 #include "G4HepRepFileXMLWriter.hh" 43 44 #include "G4HepRepMessenger.hh" 45 #include "G4ios.hh" 46 47 G4HepRepFileXMLWriter::G4HepRepFileXMLWriter() 48 { 49 isOpen = false; 50 init(); 51 } 52 53 void G4HepRepFileXMLWriter::init() 54 { 55 typeDepth = -1; 56 57 int i = -1; 58 while(i++ < 49) 59 { 60 prevTypeName[i] = new char[1]; 61 strcpy(prevTypeName[i], ""); 62 63 inType[i] = false; 64 inInstance[i] = false; 65 } 66 67 inPrimitive = false; 68 inPoint = false; 69 } 70 71 void G4HepRepFileXMLWriter::addType(const char 72 { 73 if(fout.good()) 74 { 75 // Flatten structure if it exceeds maximum 76 if(newTypeDepth > 49) 77 newTypeDepth = 49; 78 79 if(newTypeDepth < 0) 80 newTypeDepth = 0; 81 82 // Insert any layers that are missing from 83 // callers that skip from, say, layer 1 to 84 while(typeDepth < (newTypeDepth - 1)) 85 { 86 addType("Layer Inserted by G4HepRepFileX 87 addInstance(); 88 } 89 90 // If moving closer to the root, close pre 91 while(newTypeDepth < typeDepth) 92 endType(); 93 94 // Close any remaining primitives of the c 95 endPrimitive(); 96 97 // If this is a new type name for the curr 98 // new Type. Otherwise, it is just anothe 99 if(strcmp(name, prevTypeName[newTypeDepth] 100 { 101 if(inType[newTypeDepth]) 102 endType(); 103 104 prevTypeName[newTypeDepth] = new char[st 105 strcpy(prevTypeName[newTypeDepth], name) 106 107 inType[newTypeDepth] = true; 108 indent(); 109 fout << "<heprep:type version=\"null\" n 110 << G4endl; 111 112 typeDepth = newTypeDepth; 113 } 114 } 115 else 116 { 117 #ifdef G4HEPREPFILEDEBUG 118 G4cout << "G4HepRepFileXMLWriter:addType N 119 << G4endl; 120 #endif 121 } 122 } 123 124 void G4HepRepFileXMLWriter::addInstance() 125 { 126 if(fout.good()) 127 { 128 if(inType[typeDepth]) 129 { 130 endInstance(); 131 inInstance[typeDepth] = true; 132 indent(); 133 fout << "<heprep:instance>" << G4endl; 134 } 135 else 136 { 137 #ifdef G4HEPREPFILEDEBUG 138 G4cout 139 << "G4HepRepFileXMLWriter:addInstance 140 << G4endl; 141 #endif 142 } 143 } 144 else 145 { 146 #ifdef G4HEPREPFILEDEBUG 147 G4cout << "G4HepRepFileXMLWriter:addInstan 148 << G4endl; 149 #endif 150 } 151 } 152 153 void G4HepRepFileXMLWriter::addPrimitive() 154 { 155 if(fout.good()) 156 { 157 if(inInstance[typeDepth]) 158 { 159 endPrimitive(); 160 inPrimitive = true; 161 indent(); 162 fout << "<heprep:primitive>" << G4endl; 163 } 164 else 165 { 166 #ifdef G4HEPREPFILEDEBUG 167 G4cout << "G4HepRepFileXMLWriter:addPrim 168 "currently open" 169 << G4endl; 170 #endif 171 } 172 } 173 else 174 { 175 #ifdef G4HEPREPFILEDEBUG 176 G4cout << "G4HepRepFileXMLWriter:addPrimit 177 << G4endl; 178 #endif 179 } 180 } 181 182 void G4HepRepFileXMLWriter::addPoint(double x, 183 { 184 if(fout.good()) 185 { 186 if(inPrimitive) 187 { 188 endPoint(); 189 inPoint = true; 190 indent(); 191 192 // Include scale and center values 193 G4HepRepMessenger* messenger = G4HepRepM 194 G4double scale = messenger 195 G4ThreeVector center = messenger 196 G4double xNew = scale * ( 197 G4double yNew = scale * ( 198 G4double zNew = scale * ( 199 200 fout << "<heprep:point x=\"" << xNew << 201 << zNew << "\">" << G4endl; 202 } 203 else 204 { 205 #ifdef G4HEPREPFILEDEBUG 206 G4cout << "G4HepRepFileXMLWriter:addPoin 207 "currently open" 208 << G4endl; 209 #endif 210 } 211 } 212 else 213 { 214 #ifdef G4HEPREPFILEDEBUG 215 G4cout << "G4HepRepFileXMLWriter:addPoint 216 << G4endl; 217 #endif 218 } 219 } 220 221 void G4HepRepFileXMLWriter::addAttDef(const ch 222 const ch 223 { 224 if(fout.good()) 225 { 226 indent(); 227 fout << " <heprep:attdef extra=\"" << ext 228 << "\" type=\"" << type << "\"" << G4 229 indent(); 230 fout << " desc=\"" << desc << "\"/>" << G 231 } 232 else 233 { 234 #ifdef G4HEPREPFILEDEBUG 235 G4cout << "G4HepRepFileXMLWriter:addAttDef 236 << G4endl; 237 #endif 238 } 239 } 240 241 // Four methods to fill attValues 242 void G4HepRepFileXMLWriter::addAttValue(const 243 { 244 if(fout.good()) 245 { 246 indent(); 247 fout << " <heprep:attvalue showLabel=\"NO 248 << G4endl; 249 indent(); 250 fout << " value=\"" << value << "\"/>" 251 } 252 else 253 { 254 #ifdef G4HEPREPFILEDEBUG 255 G4cout << "G4HepRepFileXMLWriter:addAttVal 256 << G4endl; 257 #endif 258 } 259 } 260 261 void G4HepRepFileXMLWriter::addAttValue(const 262 { 263 if(fout.good()) 264 { 265 indent(); 266 fout << " <heprep:attvalue showLabel=\"NO 267 << G4endl; 268 indent(); 269 fout << " value=\"" << value << "\"/>" 270 } 271 else 272 { 273 #ifdef G4HEPREPFILEDEBUG 274 G4cout << "G4HepRepFileXMLWriter:addAttVal 275 << G4endl; 276 #endif 277 } 278 } 279 280 void G4HepRepFileXMLWriter::addAttValue(const 281 { 282 if(fout.good()) 283 { 284 indent(); 285 fout << " <heprep:attvalue showLabel=\"NO 286 << G4endl; 287 indent(); 288 fout << " value=\"" << value << "\"/>" 289 } 290 else 291 { 292 #ifdef G4HEPREPFILEDEBUG 293 G4cout << "G4HepRepFileXMLWriter:addAttVal 294 << G4endl; 295 #endif 296 } 297 } 298 299 void G4HepRepFileXMLWriter::addAttValue(const 300 { 301 if(fout.good()) 302 { 303 indent(); 304 fout << " <heprep:attvalue showLabel=\"NO 305 << G4endl; 306 indent(); 307 if(value) 308 fout << " value=\"True\"/>" << G4endl 309 else 310 fout << " value=\"False\"/>" << G4end 311 } 312 else 313 { 314 #ifdef G4HEPREPFILEDEBUG 315 G4cout << "G4HepRepFileXMLWriter:addAttVal 316 << G4endl; 317 #endif 318 } 319 } 320 321 void G4HepRepFileXMLWriter::addAttValue(const 322 double 323 { 324 if(fout.good()) 325 { 326 int redness = int(value1 * 255.); 327 int greenness = int(value2 * 255.); 328 int blueness = int(value3 * 255.); 329 indent(); 330 fout << " <heprep:attvalue showLabel=\"NO 331 << G4endl; 332 indent(); 333 fout << " value=\"" << redness << "," < 334 << "\"/>" << G4endl; 335 } 336 else 337 { 338 #ifdef G4HEPREPFILEDEBUG 339 G4cout << "G4HepRepFileXMLWriter:addAttVal 340 << G4endl; 341 #endif 342 } 343 } 344 345 void G4HepRepFileXMLWriter::open(const char* f 346 { 347 if(isOpen) 348 close(); 349 350 fout.open(fileSpec); 351 352 if(fout.good()) 353 { 354 fout << "<?xml version=\"1.0\" ?>" << G4en 355 fout << "<heprep:heprep " 356 "xmlns:heprep=\"http://www.slac.st 357 << G4endl; 358 fout << " xmlns:xsi=\"http://www.w3.org/1 359 "xsi:schemaLocation=\"HepRep.xsd\" 360 << G4endl; 361 362 isOpen = true; 363 init(); 364 } 365 else 366 { 367 G4cout << "G4HepRepFileXMLWriter:open Unab 368 << G4endl; 369 } 370 } 371 372 void G4HepRepFileXMLWriter::close() 373 { 374 // Close any remaining open Types 375 endTypes(); 376 377 if(fout.good()) 378 { 379 fout << "</heprep:heprep>" << G4endl; 380 fout.close(); 381 isOpen = false; 382 } 383 else 384 { 385 G4cout << "G4HepRepFileXMLWriter:close No 386 } 387 } 388 389 void G4HepRepFileXMLWriter::endTypes() 390 { 391 // Close any remaining open Types 392 while(typeDepth > -1) 393 endType(); 394 } 395 396 void G4HepRepFileXMLWriter::endType() 397 { 398 endInstance(); 399 indent(); 400 fout << "</heprep:type>" << G4endl; 401 inType[typeDepth] = false; 402 delete[] prevTypeName[typeDepth]; 403 prevTypeName[typeDepth] = new char[1]; 404 strcpy(prevTypeName[typeDepth], ""); 405 typeDepth--; 406 } 407 408 void G4HepRepFileXMLWriter::endInstance() 409 { 410 if(inInstance[typeDepth]) 411 { 412 endPrimitive(); 413 indent(); 414 fout << "</heprep:instance>" << G4endl; 415 inInstance[typeDepth] = false; 416 } 417 } 418 419 void G4HepRepFileXMLWriter::endPrimitive() 420 { 421 if(inPrimitive) 422 { 423 endPoint(); 424 indent(); 425 fout << "</heprep:primitive>" << G4endl; 426 inPrimitive = false; 427 } 428 } 429 430 void G4HepRepFileXMLWriter::endPoint() 431 { 432 if(inPoint) 433 { 434 indent(); 435 fout << "</heprep:point>" << G4endl; 436 inPoint = false; 437 } 438 } 439 440 void G4HepRepFileXMLWriter::indent() 441 { 442 if(fout.good()) 443 { 444 int i = 0; 445 while(inType[i] && i < 12) 446 { 447 fout << " "; 448 if(inInstance[i]) 449 fout << " "; 450 i++; 451 } 452 453 if(inPrimitive) 454 fout << " "; 455 if(inPoint) 456 fout << " "; 457 } 458 } 459