EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Extract_ATHENA_gdml.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Extract_ATHENA_gdml.C
1 #include <TFile.h>
2 #include <TGeoManager.h>
3 #include <TGeoNode.h>
4 #include <TGDMLWrite.h>
5 #include <RVersion.h>
6 
7 #include <string>
8 
9 int Extract_ATHENA_gdml( string subsys="all", const string outbase="athena_")
10 {
11  TFile * file = new TFile("detector_geometry.root","READ");
12  gGeoManager = (TGeoManager*) file->Get("default");
13 
14  // need to switch to root units
15  // This flexibility was added in 6.22
16  // And seems to have changed the default
17 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,22,00)
18  gGeoManager->LockDefaultUnits(false);
19  gGeoManager->SetDefaultUnits(TGeoManager::kRootUnits);
20 #endif
21 
22  TGDMLWrite *writer = new TGDMLWrite;
23  writer->SetG4Compatibility(true);
24  writer->WriteGDMLfile ( gGeoManager, "athena_all.gdml","vg");
25 
26  vector<string> outnames;
27 
28  TGeoNodeMatrix* m = (TGeoNodeMatrix*) gGeoManager->GetListOfNodes()->At(0);
29  for (int i = 0; i<m->GetNdaughters() ; ++i ){
30  TGeoNode* n = (TGeoNode*) m->GetNodes()->At(i);
31  string outname = outbase+n->GetName()+".gdml";
32  if ( subsys !="all" && n->GetName()!=subsys ) continue;
33  writer->WriteGDMLfile ( gGeoManager, n, outname.c_str(),"vg");
34  outnames.push_back(outname);
35  }
36 
37  // Now need to reimport these and add a world_volume
38  for ( auto fname : outnames ){
39 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,22,00)
40  gGeoManager->LockDefaultUnits(false);
41  gGeoManager->SetDefaultUnits(TGeoManager::kRootUnits);
42 #endif
43  gGeoManager->Import(fname.c_str());
44 
45  TGeoMedium *Air = gGeoManager->GetMedium("Air");
46  if ( !Air ){
47  TGeoElementTable *table = gGeoManager->GetElementTable();
48  TGeoMixture *air = new TGeoMixture("air",4, 0.00120479);
49  air->AddElement(table->GetElement(6),0.00012);
50  air->AddElement(table->GetElement(7),0.754);
51  air->AddElement(table->GetElement(8),0.234);
52  air->AddElement(table->GetElement(18),0.012827);
53  Air = new TGeoMedium("Air",0, air);
54  }
55 
56  TGeoVolume *top = gGeoManager->MakeBox("world_volume", Air, 3000., 3000., 10000.);
57  top->AddNode( gGeoManager->GetTopVolume(), 1 );
58  gGeoManager->SetTopVolume(top);
59  gGeoManager->Export(fname.c_str(), "vg");
60  }
61 
62  return 0;
63 }