Skip to content
Snippets Groups Projects

scan geometry for non standard top volume names

Closed Eoin Clerkin requested to merge e.clerkin/cbmroot:scan_geometry into master
1 file
+ 61
51
Compare changes
  • Side-by-side
  • Inline
+ 61
51
@@ -10,74 +10,84 @@
#include "TCanvas.h"
#include "TStopwatch.h"
#endif // !defined(__CLING__)
#endif
#include <cstdlib>
#include <fstream>
#include <iostream>
void scan_geometry(std::string fileName = "test.geo.root", uint32_t uMaxNodeDepth = 25)
#define NODE_DEPTH 25
TGeoNode* node[NODE_DEPTH];
TGeoMedium* med;
TGeoManager* gman;
TGeoVolume* top;
//const char* seekstr1 = "cable";
const char* seekstr2 = "cable";
void scan_geometry(const char* fileName = "test.geo.root")
{
TString myName = "scan_and_graph";
std::vector<TGeoNode*> node(uMaxNodeDepth, nullptr);
TGeoMedium* med = nullptr;
TGeoVolume* top = nullptr;
TString myName = "scan_and_graph";
TFile file(fileName.c_str(), "READ");
TFile file(fileName, "READ");
if (!file.IsOpen()) {
std::cerr << "Was a root geo file supplied?" << std::endl;
std::cerr << "Was a root geo file supplied?" << endl;
exit(1);
};
gROOT->cd();
file.GetListOfKeys()->Print();
file.GetSize();
file.GetObject("FAIRGeom", gGeoManager);
char geo_tag[100];
strcpy(geo_tag, basename(fileName));
char* ptr = strstr(geo_tag, ".geo.root");
*(ptr) = '\0';
if (gGeoManager != nullptr) { top = gGeoManager->GetTopVolume(); };
gROOT->cd();
if (top == nullptr) {
std::string geo_tag = fileName.substr(fileName.find_last_of('/') + 1);
geo_tag = geo_tag.substr(0, geo_tag.find_first_of('.'));
std::cout << "geo tag extracted from file name is: " << geo_tag << std::endl;
file.GetObject(geo_tag.c_str(), top);
}
if (top == nullptr) file.GetObject("FAIRGeom", top);
if (top == nullptr) file.GetObject("top", top);
if (top == nullptr) file.GetObject("TOP", top);
if (top == nullptr) file.GetObject("Top", top);
if (top == nullptr) file.GetObject("geometryFromGDML", top);
gman = (TGeoManager*) file.Get("FAIRGeom");
if (gman != NULL) { top = gman->GetTopVolume(); };
if (top == NULL) top = (TGeoVolume*) file.Get(geo_tag);
if (top == NULL) top = (TGeoVolume*) file.Get("FAIRGeom");
if (top == NULL) top = (TGeoVolume*) file.Get("top");
if (top == NULL) top = (TGeoVolume*) file.Get("TOP");
if (top == NULL) top = (TGeoVolume*) file.Get("Top");
if (top == NULL) top = (TGeoVolume*) file.Get("geometryFromGDML");
gROOT->cd();
if (top == NULL) top = (TGeoVolume *) file.Get(file.GetListOfKeys()->First()->GetName());
if (top == nullptr) {
std::cerr << "No Top Volume found. Is the TGeoManager or Top Volume unusally named" << std::endl;
if (top == NULL) {
std::cerr << "No Top Volume found. Is the TGeoManager or Top Volume unusally named" << endl;
exit(1);
}
FILE* graph = fopen("geo.gv", "w+");
fprintf(graph, "digraph D {\n");
TObjArray* nodes = top->GetNodes();
if (nodes == nullptr) {
std::cerr << "Could not get the Nodes." << std::endl;
exit(1);
}
nodes->Print();
gROOT->cd();
std::vector<int32_t> i_array(uMaxNodeDepth, 0);
std::vector<int32_t> num_array(uMaxNodeDepth + 2, 0);
int32_t j = 0;
int i_array[NODE_DEPTH], num_array[NODE_DEPTH], j;
for (int i = 0; i < NODE_DEPTH; i++)
i_array[i] = 0;
for (int i = 0; i < NODE_DEPTH; i++)
num_array[i] = 0;
//TGeoNode* node[NODE_DEPTH];
//TGeoMedium* med;
j = 0;
i_array[0] = 0;
num_array[0] = nodes->GetEntries();
std::cout << "\n" << endl;
FILE* graph = fopen("geo.gv", "w+");
fprintf(graph, "digraph D {\n");
int k = 0;
while (num_array[0] != 0 && 0 <= j) {
while (num_array[0] != 0) {
k++; // Only used for stop.
//if(k==3)break;
@@ -89,12 +99,10 @@ void scan_geometry(std::string fileName = "test.geo.root", uint32_t uMaxNodeDept
i_array[j]++; // Update number.
std::cout << "Node: \t";
for (int i = 0; i < j + 1 && i < uMaxNodeDepth; i++)
for (int i = 0; i < j + 1; i++)
printf("%d/%d \t", i_array[i], num_array[i]);
printf("\n");
fprintf(graph, "%s -> %s \n", node[j]->GetMotherVolume()->GetName(), node[j]->GetVolume()->GetName());
std::cout << "Name: ";
node[j]->Print();
//std::cout << "Mother: ";
@@ -111,32 +119,34 @@ void scan_geometry(std::string fileName = "test.geo.root", uint32_t uMaxNodeDept
std::cout << "Number of daughters: " << num_array[j + 1];
std::cout << endl << endl << endl;
if (j + 1 < uMaxNodeDepth && num_array[j + 1] > 0) {
fprintf(graph, "%s (%s) -> %s (%s) \n",
node[j]->GetMotherVolume()->GetName(),
node[j]->GetMotherVolume()->GetName(),
node[j]->GetVolume()->GetName(),
node[j]->GetVolume()->GetName()
);
if (num_array[j + 1] > 0) {
j++;
num_array[j + 2] = 0;
};
while (0 <= j && i_array[j] == num_array[j]) {
while (i_array[j] == num_array[j]) {
num_array[j] = 0;
i_array[j] = 0; // Reset the counter before falling back.
j--;
};
if (0 <= j && j >= uMaxNodeDepth) {
std::cerr << "Maximum depth of " << uMaxNodeDepth << " exceeded (" << j
<< "). Increase uMaxNodeDepth which is the second parameter of the macro" << std::endl;
exit(uMaxNodeDepth);
if (j >= NODE_DEPTH) {
std::cerr << "Maximum depth of " << NODE_DEPTH << " exceeded. Increase NODE_DEPTH in header of macro," << endl;
exit(NODE_DEPTH);
};
};
fprintf(graph, "}\n");
fclose(graph);
gROOT->cd();
file.Close();
RemoveGeoManager();
printf("Processed %d nodes.\n", k);
std::cout << "Program Ends" << std::endl;
std::cout << "Program Ends" << endl;
} // End of macro
Loading