00001 #ifdef USE_XML
00002 #include "BDSGlobalConstants.hh"
00003 #include "BDSGeometryGDML.hh"
00004 #include "G4Box.hh"
00005 #include "G4Tubs.hh"
00006 #include "G4Cons.hh"
00007 #include "G4Torus.hh"
00008 #include "G4Polycone.hh"
00009 #include "G4SubtractionSolid.hh"
00010 #include "G4VisAttributes.hh"
00011 #include "G4LogicalVolume.hh"
00012 #include "G4VPhysicalVolume.hh"
00013 #include "G4PVPlacement.hh"
00014 #include "G4UserLimits.hh"
00015 #include "BDSMySQLWrapper.hh"
00016 #include "BDSMaterials.hh"
00017 #include "BDSSamplerSD.hh"
00018 #include "G4SDManager.hh"
00019 #include "BDSSamplerSD.hh"
00020 #include "BDSOutput.hh"
00021 #include <vector>
00022 #include <map>
00023 #include <cstdlib>
00024 #include "G4ClassicalRK4.hh"
00025 #include <cstring>
00026
00027 using namespace std;
00028
00029 extern BDSSamplerSD* BDSSamplerSensDet;
00030
00031 extern BDSMaterials* theMaterials;
00032 extern G4RotationMatrix* RotY90;
00033 extern BDSOutput bdsOutput;
00034 extern BDSGlobalConstants* BDSGlobals;
00035
00036 BDSGeometryGDML::BDSGeometryGDML(G4String GDMLfile)
00037 {
00038 itsGDMLfile = GDMLfile;
00039 visRed = visGreen = visBlue = 0.0;
00040
00041
00042 CONST_REF aconst;
00043 aconst.name="pi";
00044 aconst.value=pi;
00045 CONST_LIST.push_back(aconst);
00046
00047 aconst.name="TWOPI";
00048 aconst.value=twopi;
00049 CONST_LIST.push_back(aconst);
00050
00051
00052 aconst.name="HALFPI";
00053 aconst.value=halfpi;
00054 CONST_LIST.push_back(aconst);
00055
00056
00057
00058
00059 aconst.name="radian";
00060 aconst.value=radian;
00061 CONST_LIST.push_back(aconst);
00062
00063 aconst.name="degree";
00064 aconst.value=degree;
00065 CONST_LIST.push_back(aconst);
00066
00067
00068 aconst.name="mm";
00069 aconst.value=mm;
00070 CONST_LIST.push_back(aconst);
00071
00072
00073 aconst.name="cm";
00074 aconst.value=cm;
00075 CONST_LIST.push_back(aconst);
00076
00077
00078 aconst.name="m";
00079 aconst.value=m;
00080 CONST_LIST.push_back(aconst);
00081
00082 }
00083
00084 BDSGeometryGDML::~BDSGeometryGDML()
00085 {;}
00086
00087 void BDSGeometryGDML::Construct(G4LogicalVolume *marker)
00088 {
00089 itsMarkerVol = marker;
00090 parseDoc();
00091
00092 }
00093
00094 void BDSGeometryGDML::parseDoc()
00095 {
00096 const char* docname = itsGDMLfile.c_str();
00097 xmlDocPtr doc;
00098 xmlNodePtr cur;
00099
00100 doc = xmlParseFile(docname);
00101
00102 if (doc == NULL )
00103 {
00104 G4Exception("Document not parsed successfully");
00105 }
00106
00107 cur = xmlDocGetRootElement(doc);
00108
00109 if (cur == NULL)
00110 {
00111 xmlFreeDoc(doc);
00112 G4Exception("empty document");
00113 }
00114
00115
00116 if (xmlStrcmp(cur->name, (const xmlChar *) "lcdd"))
00117 {
00118 xmlFreeDoc(doc);
00119 G4Exception("XML document of the wrong type, root node != lcdd\nCheck your XML file\n");
00120 }
00121
00122 cur = cur->xmlChildrenNode;
00123
00124 while (cur != NULL)
00125 {
00126 if ((!xmlStrcmp(cur->name, (const xmlChar *)"header")))
00127 {
00128
00129 }
00130 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"iddict")))
00131 {
00132
00133 }
00134 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"sensitive_detectors")))
00135 {
00136
00137 }
00138 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"limits")))
00139 {
00140
00141 }
00142 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"regions")))
00143 {
00144
00145 }
00146 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"display")))
00147 {
00148 parseDISPLAY(cur);
00149 }
00150 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"gdml")))
00151 {
00152 parseGDML(cur);
00153 }
00154 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"fields")))
00155 {
00156
00157 }
00158 cur = cur->next;
00159 }
00160
00161 xmlFreeDoc(doc);
00162 return;
00163 }
00164
00165 void BDSGeometryGDML::parseDISPLAY(xmlNodePtr cur)
00166 {
00167 cur = cur->xmlChildrenNode;
00168
00169 while (cur != NULL)
00170 {
00171 if ((!xmlStrcmp(cur->name, (const xmlChar *)"vis")))
00172 {
00173 parseVIS(cur);
00174 }
00175 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"VisType")))
00176 {
00177 }
00178 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"DisplayType")))
00179 {
00180
00181 }
00182 cur = cur->next;
00183 }
00184 return;
00185 }
00186
00187 void BDSGeometryGDML::parseVIS(xmlNodePtr cur)
00188 {
00189 G4String name = parseStrChar(xmlGetProp(cur,(const xmlChar*)"name"));
00190 G4bool visible = parseBoolChar(xmlGetProp(cur,(const xmlChar*)"visible"));
00191 G4bool show_daughters = parseBoolChar(xmlGetProp(cur,(const xmlChar*)"show_daughters"));
00192 G4String line_style = parseStrChar(xmlGetProp(cur,(const xmlChar*)"line_style"));
00193 G4String drawing_style = parseStrChar(xmlGetProp(cur,(const xmlChar*)"drawing_style"));
00194
00195 G4double R = 1.0, G = 1.0, B = 1.0, alpha = 1.0;
00196
00197 cur = cur->xmlChildrenNode;
00198
00199 while(cur!=NULL)
00200 {
00201 if ((!xmlStrcmp(cur->name, (const xmlChar *)"color")))
00202 {
00203 R = parseDblChar(xmlGetProp(cur,(const xmlChar*)"R"));
00204 G = parseDblChar(xmlGetProp(cur,(const xmlChar*)"G"));
00205 B = parseDblChar(xmlGetProp(cur,(const xmlChar*)"R"));
00206 alpha = parseDblChar(xmlGetProp(cur,(const xmlChar*)"alpha"));
00207
00208 }
00209 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"color")))
00210 {
00211
00212 }
00213 cur = cur->next;
00214 }
00215
00216 G4VisAttributes* VisAtt =
00217 new G4VisAttributes(G4Colour(R,G,B,alpha));
00218
00219 if(drawing_style=="solid" )VisAtt->SetForceSolid(true);
00220 else if(drawing_style=="wireframe") VisAtt->SetForceWireframe(true);
00221
00222 if(line_style=="unbroken" )VisAtt->SetLineStyle(G4VisAttributes::unbroken);
00223 else if(line_style=="dashed") VisAtt->SetLineStyle(G4VisAttributes::dashed);
00224 else if(line_style=="dotted") VisAtt->SetLineStyle(G4VisAttributes::dotted);
00225
00226 VisAtt->SetVisibility(visible);
00227 if(show_daughters) VisAtt->SetDaughtersInvisible(false);
00228 else VisAtt->SetDaughtersInvisible(true);
00229
00230 VIS_REF avis;
00231 avis.name = name;
00232 avis.value = VisAtt;
00233 VIS_LIST.push_back(avis);
00234 return;
00235 }
00236 void BDSGeometryGDML::parseGDML(xmlNodePtr cur)
00237 {
00238 cur = cur->xmlChildrenNode;
00239
00240 while (cur != NULL)
00241 {
00242 if ((!xmlStrcmp(cur->name, (const xmlChar *)"define")))
00243 {
00244 G4cout << "Importing GDML Definitions" << G4endl;
00245 parseDEFINE(cur);
00246 }
00247 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"materials")))
00248 {
00249 G4cout << "Importing GDML Materials" << G4endl;
00250 parseMATERIALS(cur);
00251 }
00252 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"solids")))
00253 {
00254 G4cout << "Importing GDML Solids" << G4endl;
00255 parseSOLID(cur);
00256 }
00257 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"structure")))
00258 {
00259 G4cout << "Importing GDML Structures" << G4endl;
00260 parseSTRUCTURE(cur);
00261 }
00262 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"setup")))
00263 {
00264 G4cout << "Importing GDML Setup" << G4endl;
00265 G4String name = parseStrChar(xmlGetProp(cur,(const xmlChar*)"name"));
00266 G4String version = parseStrChar(xmlGetProp(cur,(const xmlChar*)"version"));
00267 xmlNodePtr tempcur = cur->xmlChildrenNode;
00268 G4String worldref;
00269
00270 while(tempcur!=NULL)
00271 {
00272 if ((!xmlStrcmp(tempcur->name, (const xmlChar *)"world")))
00273 worldref = parseStrChar(xmlGetProp(tempcur,(const xmlChar*)"ref"));
00274 tempcur = tempcur->next;
00275 }
00276
00277 G4LogicalVolume* topvol = GetLogVolByName(worldref);
00278 new G4PVPlacement(NULL,
00279 G4ThreeVector(0.,0.,0.),
00280 topvol,
00281 topvol->GetName()+"_PhysiComp",
00282 itsMarkerVol,
00283 false,
00284 0);
00285
00286
00287 }
00288 cur = cur->next;
00289 }
00290 return;
00291 }
00292
00293 void BDSGeometryGDML::parseSTRUCTURE(xmlNodePtr cur)
00294 {
00295 cur = cur->xmlChildrenNode;
00296
00297 while (cur != NULL)
00298 {
00299
00300 if ((!xmlStrcmp(cur->name, (const xmlChar *)"volume")))
00301 parseVOLUME(cur);
00302 cur = cur->next;
00303 }
00304
00305 return;
00306 }
00307
00308
00309 void BDSGeometryGDML::parseDEFINE(xmlNodePtr cur)
00310 {
00311 cur = cur->xmlChildrenNode;
00312
00313 while (cur != NULL)
00314 {
00315
00316 if ((!xmlStrcmp(cur->name, (const xmlChar *)"rotation")))
00317 {
00318 ROT_REF arot;
00319 arot.name=(parseStrChar(xmlGetProp(cur,(const xmlChar*)"name")));
00320 arot.value=G4ThreeVector(parseDblChar(xmlGetProp(cur,(const xmlChar*)"x")),
00321 parseDblChar(xmlGetProp(cur,(const xmlChar*)"y")),
00322 parseDblChar(xmlGetProp(cur,(const xmlChar*)"z")));
00323
00324 ROT_LIST.push_back(arot);
00325 }
00326 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"position")))
00327 {
00328 POS_REF apos;
00329 apos.name=(parseStrChar(xmlGetProp(cur,(const xmlChar*)"name")));
00330 apos.value=G4ThreeVector(parseDblChar(xmlGetProp(cur,(const xmlChar*)"x")),
00331 parseDblChar(xmlGetProp(cur,(const xmlChar*)"y")),
00332 parseDblChar(xmlGetProp(cur,(const xmlChar*)"z")));
00333
00334 POS_LIST.push_back(apos);
00335 }
00336 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"constant")))
00337 {
00338 CONST_REF aconst;
00339 aconst.name=(parseStrChar(xmlGetProp(cur,(const xmlChar*)"name")));
00340 aconst.value=parseDblChar(xmlGetProp(cur,(const xmlChar*)"value"));
00341 CONST_LIST.push_back(aconst);
00342 }
00343 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"expression")))
00344 {
00345
00346 }
00347 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"quantity")))
00348 {
00349
00350 }
00351 cur = cur->next;
00352 }
00353
00354 return;
00355 }
00356
00357 void BDSGeometryGDML::parseMATERIALS(xmlNodePtr cur)
00358 {
00359 cur = cur->xmlChildrenNode;
00360
00361 while (cur != NULL)
00362 {
00363
00364 if ((!xmlStrcmp(cur->name, (const xmlChar *)"define")))
00365 {
00366
00367 }
00368 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"isotope")))
00369 {
00370
00371 }
00372 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"element")))
00373 {
00374
00375 }
00376 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"material")))
00377 {
00378
00379 }
00380 cur = cur->next;
00381 }
00382
00383 return;
00384
00385 }
00386
00387 void BDSGeometryGDML::parseSOLID(xmlNodePtr cur)
00388 {
00389 cur = cur->xmlChildrenNode;
00390
00391 while (cur != NULL)
00392 {
00393
00394 if ((!xmlStrcmp(cur->name, (const xmlChar *)"tube")))
00395 BuildTube(cur);
00396
00397 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"box")))
00398 BuildBox(cur);
00399
00400 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"cone")))
00401 {
00402 G4Exception("GDML cone not currently implemented in BDSIM");
00403 }
00404 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"eltube")))
00405 {
00406 G4Exception("GDML eltube not currently implemented in BDSIM");
00407 }
00408 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"hype")))
00409 {
00410 G4Exception("GDML hype not currently implemented in BDSIM");
00411 }
00412 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"intersection")))
00413 {
00414 G4Exception("GDML intersection not currently implemented in BDSIM");
00415 }
00416 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"orb")))
00417 {
00418 G4Exception("GDML orb not currently implemented in BDSIM");
00419 }
00420 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"polyhedra")))
00421 {
00422 G4Exception("GDML polyhedra not currently implemented in BDSIM");
00423 }
00424 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"sphere")))
00425 {
00426 G4Exception("GDML sphere not currently implemented in BDSIM");
00427 }
00428 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"trap")))
00429 {
00430 G4Exception("GDML trap not currently implemented in BDSIM");
00431 }
00432 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"subtraction")))
00433 BuildSubtraction(cur);
00434
00435 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"torus")))
00436 {
00437 G4Exception("GDML torus not currently implemented in BDSIM");
00438 }
00439 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"trd")))
00440 {
00441 G4Exception("GDML trd not currently implemented in BDSIM");
00442 }
00443 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"union")))
00444 {
00445 G4Exception("GDML union not currently implemented in BDSIM");
00446 }
00447
00448 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"polycone")))
00449 BuildPolycone(cur);
00450
00451 cur = cur->next;
00452 }
00453
00454 return;
00455 }
00456
00457 void BDSGeometryGDML::parseVOLUME(xmlNodePtr cur)
00458 {
00459 G4String volume_name = parseStrChar(xmlGetProp(cur,(const xmlChar*)"name"));
00460 cur = cur->xmlChildrenNode;
00461
00462 G4String materialref;
00463 G4String solidref;
00464 G4String visref;
00465 xmlNodePtr origcur = cur;
00466 while (cur != NULL)
00467 {
00468 if ((!xmlStrcmp(cur->name, (const xmlChar *)"materialref")))
00469 {
00470 materialref = parseStrChar(xmlGetProp(cur,(const xmlChar*)"ref"));
00471 }
00472 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"solidref")))
00473 {
00474 solidref = parseStrChar(xmlGetProp(cur,(const xmlChar*)"ref"));
00475 }
00476 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"visref")))
00477 {
00478 visref = parseStrChar(xmlGetProp(cur,(const xmlChar*)"ref"));
00479 }
00480
00481 cur = cur->next;
00482 }
00483
00484 if(materialref!="" && solidref!="")
00485 {
00486
00487 materialref="Iron";
00488 G4LogicalVolume* alogvol = new G4LogicalVolume(GetSolidByName(solidref),
00489 theMaterials->GetMaterial(materialref),
00490 volume_name);
00491
00492 LOGVOL_LIST.push_back(alogvol);
00493
00494 alogvol->SetVisAttributes(GetVisByName(visref));
00495
00496 while(origcur!=NULL)
00497 {
00498 if ((!xmlStrcmp(origcur->name, (const xmlChar *)"physvol")))
00499 {
00500 parsePHYSVOL(origcur, volume_name);
00501 }
00502 origcur = origcur->next;
00503 }
00504 }
00505
00506 else
00507 {
00508 G4cout << "Can't build" << volume_name << " : " << solidref << G4endl;
00509 G4Exception();
00510 }
00511
00512
00513
00514 return;
00515 }
00516
00517 void BDSGeometryGDML::parsePHYSVOL(xmlNodePtr cur, G4String volume_name)
00518 {
00519 cur = cur->xmlChildrenNode;
00520
00521 G4String volumeref;
00522
00523 G4RotationMatrix* componentRotation = NULL;
00524
00525 G4ThreeVector PlacementPoint;
00526
00527
00528 while (cur != NULL)
00529 {
00530 if ((!xmlStrcmp(cur->name, (const xmlChar *)"volumeref")))
00531 volumeref = parseStrChar(xmlGetProp(cur,(const xmlChar*)"ref"));
00532
00533 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"positionref")))
00534 PlacementPoint = GetPosition(parseStrChar(xmlGetProp(cur,(const xmlChar*)"ref")));
00535
00536 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"position")))
00537 PlacementPoint = GetPosition(cur);
00538
00539 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"rotationref")))
00540 componentRotation = GetRotation(parseStrChar(xmlGetProp(cur,(const xmlChar*)"ref")));
00541
00542 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"rotation")))
00543 componentRotation = GetRotation(cur);
00544
00545 cur = cur->next;
00546 }
00547
00548 G4LogicalVolume* currentVol = GetLogVolByName(volumeref);
00549 G4LogicalVolume* parentVol = GetLogVolByName(volume_name);
00550
00551 new G4PVPlacement(componentRotation,
00552 PlacementPoint,
00553 currentVol,
00554 currentVol->GetName()+"_PhysiComp",
00555 parentVol,
00556 false,
00557 0);
00558
00559
00560 return;
00561 }
00562
00563 G4RotationMatrix* BDSGeometryGDML::RotateComponent(G4ThreeVector rotvalues)
00564 {
00565 G4RotationMatrix *rotateComponent = new G4RotationMatrix;
00566
00567
00568
00569 rotateComponent->rotateX(rotvalues.x());
00570 rotateComponent->rotateY(rotvalues.y());
00571 rotateComponent->rotateZ(rotvalues.z());
00572
00573
00574 return rotateComponent;
00575 }
00576
00577 #endif