Commit f30c12fe authored by Whitney Armstrong's avatar Whitney Armstrong
Browse files

Cleaning up interface

parent b40919d1
......@@ -32,6 +32,7 @@ namespace fs = std::experimental::filesystem;
#include "DDRec/SurfaceManager.h"
#include "DDRec/Surface.h"
#include "DD4hep/DD4hepUnits.h"
#include "DD4hep/Printout.h"
#include "TApplication.h"
#include "TMultiGraph.h"
......@@ -48,31 +49,33 @@ namespace fs = std::experimental::filesystem;
#include "clipp.h"
using namespace clipp;
//______________________________________________________________________________
enum class mode { none, help, list, part };
struct settings {
bool help = false;
bool success = false;
std::string infile = "";
std::string outfile = "detector_geometry";
std::string p_name = "";
int p_level = -1;
std::map<std::string,int> part_name_levels;
bool level_set = false;
int geo_level = -1;
bool list_all = false;
mode selected = mode::part;
using string = std::string;
using part_map = std::map<string,int>;
//bool help = false;
bool success = false;
string infile = "";
string outfile = "detector_geometry";
string part_name = "";
int part_level = -1;
mode selected = mode::list;
bool level_set = false;
int global_level = 1;
bool list_all = false;
part_map part_name_levels ;
};
//______________________________________________________________________________
void run_list_mode(const settings& s);
void run_part_mode(const settings& s);
//______________________________________________________________________________
template<typename T>
void print_usage(T cli, const char* argv0 ){
//used default formatting
std::cout << "Usage:\n" << usage_lines(cli, argv0)
std::cout << "Usage:\n" << usage_lines(cli, argv0)
<< "\nOptions:\n" << documentation(cli) << '\n';
}
//______________________________________________________________________________
......@@ -116,21 +119,34 @@ settings cmdline_settings(int argc, char* argv[])
{
settings s;
auto listMode = "list mode:" % (
command("list").set(s.selected,mode::list) |
command("info").set(s.selected,mode::list) % "list detectors and print info about geometry ",
option("-v","--verbose")
//auto listMode = "list mode:" % (
// command("list").set(s.selected,mode::list) % "list detectors and print info about geometry ",
// option("-v","--verbose")
// );
auto listMode = "list mode:" % repeatable(
command("list").set(s.selected,mode::list) % "list detectors and print info about geometry ",
repeatable(
option("-l","--level").set(s.level_set)
& value("level",s.part_level)
& value("name")([&](const std::string& p)
{
s.part_name = p;
if(!s.level_set) { s.part_level = -1; }
s.part_name_levels[p] = s.part_level;
s.level_set = false;
}) % "Part/Node name (must be child of top node)"
)
);
auto partMode = "part mode:" % repeatable(
command("part").set(s.selected,mode::part) % "Select only the first level nodes by name",
repeatable(
option("-l","--level").set(s.level_set) & value("level",s.p_level) % "Maximum level navigated to for part",
option("-l","--level").set(s.level_set) & value("level",s.part_level) % "Maximum level navigated to for part",
value("name")([&](const std::string& p)
{
s.p_name = p;
if(!s.level_set) { s.p_level = -1; }
s.part_name_levels[p] = s.p_level;
s.part_name = p;
if(!s.level_set) { s.part_level = -1; }
s.part_name_levels[p] = s.part_level;
s.level_set = false;
}) % "Part/Node name (must be child of top node)"
)
......@@ -138,20 +154,64 @@ settings cmdline_settings(int argc, char* argv[])
auto lastOpt = " options:" % (
option("-h", "--help").set(s.selected, mode::help) % "show help",
option("-g","--global_level") & integer("level",s.geo_level),
option("-g","--global_level") & integer("level",s.global_level),
option("-o","--output") & value("out",s.outfile),
value("file",s.infile).if_missing([]{ std::cout << "You need to provide an input xml filename as the last argument!\n"; } )
% "input xml file"
);
auto helpMode = command("help").set(s.selected, mode::help);
std::string wrong;
auto cli = (
command("help").set(s.selected, mode::help) | (partMode | listMode , lastOpt)
helpMode | (partMode | listMode , lastOpt)
);
assert( cli.flags_are_prefix_free() );
auto res = parse(argc, argv, cli);
//auto doc_label = [](const parameter& p) {
// if(!p.flags().empty()) return p.flags().front();
// if(!p.label().empty()) return p.label();
// return doc_string{"<?>"};
//};
//auto& os = std::cout;
//std::cout << "args -> parameter mapping:\n";
//for(const auto& m : res) {
// os << "#" << m.index() << " " << m.arg() << " -> ";
// auto p = m.param();
// if(p) {
// os << doc_label(*p) << " \t";
// if(m.repeat() > 0) {
// os << (m.bad_repeat() ? "[bad repeat " : "[repeat ")
// << m.repeat() << "]";
// }
// if(m.blocked()) os << " [blocked]";
// if(m.conflict()) os << " [conflict]";
// os << '\n';
// }
// else {
// os << " [unmapped]\n";
// }
//}
//std::cout << "missing parameters:\n";
//for(const auto& m : res.missing()) {
// auto p = m.param();
// if(p) {
// os << doc_label(*p) << " \t";
// os << " [missing after " << m.after_index() << "]\n";
// }
//}
//std::cout << "wrong " << wrong << std::endl;
//if(res.unmapped_args_count()) { std::cout << "a\n"; }
//if(res.any_bad_repeat()) { std::cout << "b\n"; }
//if(res.any_blocked()) { std::cout << "c\n"; }
//if(res.any_conflict()) { std::cout << "d\n"; }
if( res.any_error() ) {
s.success = false;
std::cout << make_man_page(cli, argv[0]).prepend_section("error: ",
......@@ -180,7 +240,7 @@ int main (int argc, char *argv[]) {
return 0;
}
// ------------------------
// --------------------------------------
// CLI Checks
if( !fs::exists(fs::path(s.infile)) ) {
std::cerr << "file, " << s.infile << ", does not exist\n";
......@@ -193,21 +253,95 @@ int main (int argc, char *argv[]) {
if( !has_suffix(s.outfile,".stp") ) {
s.outfile += ".stp";
}
for(const auto& [part_name, part_level] : s.part_name_levels ) {
std::cout << " SOME Part : " << part_name << ", level = " << part_level <<"\n";
// Make things quite
gErrorIgnoreLevel = kWarning;// kPrint, kInfo, kWarning,
dd4hep::setPrintLevel(dd4hep::WARNING);
// ---------------------------------------------
// Run modes
//
switch(s.selected) {
case mode::list:
run_list_mode(s);
break;
case mode::part:
run_part_mode(s);
break;
default:
break;
}
return 0;
}
//______________________________________________________________________________
void print_daughter_nodes(TGeoNode* node, int print_depth) {
TGeoIterator nextNode( node->GetVolume() );
int path_index = 0;
TGeoNode* currentNode = nullptr;
nextNode.SetType(0);
while( (currentNode = nextNode()) ) {
// Not iterator level starts at 1
auto nlevel = nextNode.GetLevel();
if( nlevel > print_depth ) {
continue;
}
for(int i=0;i<nlevel; i++){
std::cout << " \t";
}
std::cout << "/" <<currentNode->GetName() << " \t(vol: " << currentNode->GetVolume()->GetName() << ")" << "\n";
}
}
//______________________________________________________________________________
void run_list_mode(const settings& s)
{
dd4hep::setPrintLevel(dd4hep::WARNING);
gErrorIgnoreLevel = kWarning;// kPrint, kInfo, kWarning,
// -------------------------
// Get the DD4hep instance
// Load the compact XML file
dd4hep::Detector& detector = dd4hep::Detector::getInstance();
detector.fromCompact(s.infile);
if(s.part_name_levels.size() == 0 ){
std::cout << gGeoManager->GetPath() << "\n";
print_daughter_nodes(gGeoManager->GetTopNode(), 1);
}
for(const auto& [p,l] : s.part_name_levels){
bool dir = gGeoManager->cd(p.c_str());
if (!dir) {
std::cerr << p << " not found!\n";
continue;
}
std::cout << p << std::endl;
TGeoNode *node = gGeoManager->GetCurrentNode();
print_daughter_nodes(node, l);
}
//detector.manager().GetTopVolume()->GetNodes()->Print();
//detector.manager().GetTopVolume()->GetNode(0)->Dump();
}
//______________________________________________________________________________
void run_part_mode(const settings& s)
{
dd4hep::setPrintLevel(dd4hep::WARNING);
gErrorIgnoreLevel = kWarning;// kPrint, kInfo, kWarning,
// -------------------------
// Get the DD4hep instance
// Load the compact XML file
dd4hep::Detector& detector = dd4hep::Detector::getInstance();
detector.fromCompact(s.infile);
detector.manager().GetTopVolume()->GetNodes()->Print();
//detector.manager().GetTopVolume()->GetNodes()->Print();
//detector.manager().GetTopVolume()->GetNode(0)->Dump();
if(s.selected == mode::list) {
return 0;
}
TGeoToStep * mygeom= new TGeoToStep( &(detector.manager()) );
if( s.part_name_levels.size() > 1 ) {
......@@ -218,11 +352,8 @@ int main (int argc, char *argv[]) {
mygeom->CreatePartialGeometry( n.c_str(), l, s.outfile.c_str() );
}
} else {
mygeom->CreateGeometry(s.outfile.c_str(), s.geo_level);
mygeom->CreateGeometry(s.outfile.c_str(), s.global_level);
}
//detector.manager().Export("geometry.gdml");
return 0;
}
//______________________________________________________________________________
}
......@@ -68,15 +68,16 @@ struct settings {
std::string infile = "";
std::string outfile = "detector_geometry";
std::string p_name = "";
int p_level = -1;
bool level_set = false;
int geo_level = -1;
bool list_all = false;
mode selected = mode::part;
int color = 1;
bool color_set = false;
std::map<std::string,int> part_name_colors;
int part_level = -1;
bool level_set = false;
int geo_level = -1;
bool list_all = false;
mode selected = mode::list;
int color = 1;
double alpha = 1;
std::map<std::string,int> part_name_levels;
std::map<std::string,int> part_name_colors;
std::map<std::string,double> part_name_alphas;
};
//______________________________________________________________________________
......@@ -135,12 +136,12 @@ settings cmdline_settings(int argc, char* argv[])
command("list").set(s.selected,mode::list) % "list detectors and print info about geometry ",
repeatable(
option("-l","--level").set(s.level_set)
& value("level",s.p_level)
& value("level",s.part_level)
& value("name")([&](const std::string& p)
{
s.p_name = p;
if(!s.level_set) { s.p_level = -1; }
s.part_name_levels[p] = s.p_level;
if(!s.level_set) { s.part_level = -1; }
s.part_name_levels[p] = s.part_level;
s.level_set = false;
}) % "Part/Node name (must be child of top node)"
)
......@@ -148,32 +149,65 @@ settings cmdline_settings(int argc, char* argv[])
auto partMode = "part mode:" % repeatable(
command("part").set(s.selected,mode::part) % "Select only the first level nodes by name",
(
repeatable(
option("-l","--level").set(s.level_set) & value("level",s.p_level) % "Maximum level navigated to for part",
value("name")([&](const std::string& p)
{
s.color++;
s.p_name = p;
if(!s.level_set) { s.p_level = -1; }
s.part_name_levels[p] = s.p_level;
s.part_name_colors[p] = s.color;
s.level_set = false;
}) % "Part/Node name (must be child of top node)"
) |
//(
//repeatable(
// required("-l","--level").set(s.level_set) & integer("level",s.part_level) % "Maximum level navigated to for part",
// value("name")([&](const std::string& p)
// {
// s.color++;
// s.p_name = p;
// if(!s.level_set) { s.p_level = -1; }
// s.level_set = false;
// s.part_name_levels[p] = s.part_level;
// s.part_name_colors[p] = s.color;
// s.part_name_alphas[p] = s.alpha;
// }) % "Part/Node name (must be child of top node)"
// ) |
//repeatable(
// required("-l","--level").set(s.level_set) & integer("level",s.part_level) % "Maximum level navigated to for part",
// required("-c","--color") & integer("color",s.color),
// value("name")([&](const std::string& p)
// {
// s.p_name = p;
// if(!s.level_set) { s.part_level = -1; }
// s.level_set = false;
// s.part_name_levels[p] = s.part_level;
// s.part_name_colors[p] = s.color;
// s.part_name_alphas[p] = s.alpha;
// }) % "Part/Node name (must be child of top node)"
// )|
repeatable(
option("-l","--level").set(s.level_set) & value("level",s.p_level) % "Maximum level navigated to for part",
required("-c","--color") & value("color",s.color),
required("-l","--level").set(s.level_set) & integer("level",s.part_level) % "Maximum level navigated to for part",
option("-c","--color") & integer("color",s.color),
option("-a","--alpha") & number("alpha",s.alpha),
value("name")([&](const std::string& p)
{
s.p_name = p;
if(!s.level_set) { s.p_level = -1; }
s.part_name_levels[p] = s.p_level;
if(!s.level_set) { s.part_level = -1; }
s.level_set = false;
std::cout << "s.color " << s.color << "\n";
std::cout << "s.alpha " << s.alpha << "\n";
s.part_name_levels[p] = s.part_level;
s.part_name_colors[p] = s.color;
s.part_name_alphas[p] = s.alpha;
}) % "Part/Node name (must be child of top node)"
)
)
//|
//repeatable(
// required("-l","--level").set(s.level_set) & integer("level",s.part_level) % "Maximum level navigated to for part",
// required("-c","--color") & integer("color",s.color),
// required("-t","--transparency") & number("transparency",s.alpha),
// value("name")([&](const std::string& p)
// {
// s.p_name = p;
// if(!s.level_set) { s.part_level = -1; }
// s.level_set = false;
// s.part_name_levels[p] = s.part_level;
// s.part_name_colors[p] = s.color;
// s.part_name_alphas[p] = 1.0 - s.alpha;
// }) % "Part/Node name (must be child of top node)"
// )
//)
);
auto lastOpt = " options:" % (
......@@ -184,20 +218,25 @@ settings cmdline_settings(int argc, char* argv[])
% "input xml file"
);
std::string wrong;
auto cli = (
command("help").set(s.selected, mode::help) | (partMode | listMode , lastOpt)
command("help").set(s.selected, mode::help) | (partMode | listMode , lastOpt),
any_other(wrong)
);
assert( cli.flags_are_prefix_free() );
auto res = parse(argc, argv, cli);
if( res.any_error() ) {
s.success = false;
std::cout << make_man_page(cli, argv[0]).prepend_section("error: ",
" The best thing since sliced bread.");
return s;
}
std::cout << "wrong " << wrong << std::endl;
//if( res.any_error() ) {
// s.success = false;
// std::cout << make_man_page(cli, argv[0]).prepend_section("error: ",
// " The best thing since sliced bread.");
// return s;
//}
s.success = true;
if(s.selected == mode::help) {
......@@ -254,12 +293,14 @@ int main (int argc, char *argv[]) {
}
//______________________________________________________________________________
void run_list_mode(const settings& s)
{
// -------------------------
// Get the DD4hep instance
dd4hep::setPrintLevel(dd4hep::WARNING);
gErrorIgnoreLevel = kWarning;// kPrint, kInfo, kWarning,
// -------------------------
// Get the DD4hep instance
dd4hep::Detector& detector = dd4hep::Detector::getInstance();
detector.fromCompact(s.infile);
......@@ -310,6 +351,7 @@ void run_part_mode(const settings& s)
int root_argc = 0;
char *root_argv[1] = {"npdet_to_teve"};
//argv[0] = "npdet_fields";
gErrorIgnoreLevel = kWarning;// kPrint, kInfo, kWarning,
TApplication app("tapp", &root_argc, root_argv);
......@@ -339,12 +381,24 @@ void run_part_mode(const settings& s)
int ilevel = gGeoManager->GetLevel();
if( ilevel > l ){
std::cout << p << " found at level " << ilevel << " but above selected level of " << l << "\n";
return;
//return;
}
int istate = gGeoManager->GetCurrentNodeId();
std::string ipath = gGeoManager->GetPath();
auto pcolor = s.part_name_colors.find(p)->second;
auto ptransparency = 100.0*(1.0 - s.part_name_alphas.find(p)->second);
std::cout << " color " << pcolor << "\n";
std::cout << " ptransparency " << ptransparency << "\n";
node->GetVolume()->SetLineColor(pcolor);
node->GetVolume()->SetFillColor(pcolor);
node->GetVolume()->SetTransparency(ptransparency);
node->GetVolume()->SetTransparency(ptransparency);
TEveGeoNode* res = new TEveGeoNode(node);
std::map<int,TEveGeoNode*> eve_nodes;
eve_nodes[0] = res;
......@@ -361,11 +415,12 @@ void run_part_mode(const settings& s)
continue;
}
//if( path_index == nlevel) {
auto pcolor = s.part_name_colors.find(p);
if(pcolor != s.part_name_colors.end() ){
currentNode->GetVolume()->SetLineColor(pcolor->second);
currentNode->GetVolume()->SetFillColor(pcolor->second);
}
// if(pcolor != s.part_name_colors.end() ){
currentNode->GetVolume()->SetLineColor(pcolor);
currentNode->GetVolume()->SetFillColor(pcolor);
currentNode->GetVolume()->SetTransparency(ptransparency);
currentNode->GetVolume()->SetTransparency(ptransparency);
// }
TEveGeoNode* daughter = new TEveGeoNode( currentNode );
eve_nodes[nlevel] = daughter;
eve_nodes[nlevel-1]->AddElement(daughter);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment