File indexing completed on 2025-01-30 09:16:50
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef DD4HEP_DDCORE_VISVOLNAMEPROCESSOR_H
0014 #define DD4HEP_DDCORE_VISVOLNAMEPROCESSOR_H
0015
0016
0017 #include <DD4hep/VolumeProcessor.h>
0018
0019
0020 #include <map>
0021 #include <regex>
0022
0023
0024 namespace dd4hep {
0025
0026
0027
0028
0029
0030
0031
0032
0033 class VisVolNameProcessor : public PlacedVolumeProcessor {
0034 public:
0035 typedef std::map<std::string,std::regex> Matches;
0036 Detector& description;
0037 Matches matches;
0038 std::string name;
0039 size_t numApplied = 0;
0040 bool show = false;
0041 VisAttr visattr;
0042
0043 void _show();
0044 public:
0045
0046 VisVolNameProcessor(Detector& desc);
0047
0048 virtual ~VisVolNameProcessor();
0049
0050 void set_match(const std::vector<std::string>& matches);
0051
0052 virtual int operator()(PlacedVolume pv, int level);
0053 };
0054 }
0055 #endif
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071 #include <DD4hep/Printout.h>
0072 #include <DD4hep/DetectorTools.h>
0073 #include <DD4hep/DetectorHelper.h>
0074 #include <DD4hep/DetFactoryHelper.h>
0075 #include <sstream>
0076
0077 using namespace std;
0078 using namespace dd4hep;
0079
0080
0081 VisVolNameProcessor::VisVolNameProcessor(Detector& desc)
0082 : description(desc), name()
0083 {
0084 }
0085
0086
0087 VisVolNameProcessor::~VisVolNameProcessor() {
0088 if ( show ) {
0089 printout(ALWAYS,name,"++ %8ld vis-attrs '%s' applied.",
0090 numApplied, visattr.isValid() ? visattr.name() : "");
0091 }
0092 }
0093
0094
0095 void VisVolNameProcessor::set_match(const std::vector<std::string>& vals) {
0096 for( const auto& v : vals )
0097 matches[v] = regex(v);
0098 }
0099
0100
0101 int VisVolNameProcessor::operator()(PlacedVolume pv, int ) {
0102 Volume vol = pv.volume();
0103 if ( vol.visAttributes().ptr() != visattr.ptr() ) {
0104 string vol_nam = vol.name();
0105 for ( const auto& match : matches ) {
0106 if ( std::regex_match(vol_nam, match.second) ) {
0107 printout(ALWAYS,match.first,"++ Set visattr %s to %s",
0108 visattr.isValid() ? visattr.name() : "", vol_nam.c_str());
0109 vol.setVisAttributes(visattr);
0110 ++numApplied;
0111 return 1;
0112 }
0113
0114 }
0115 }
0116 return 1;
0117 }
0118
0119 static void* create_object(Detector& description, int argc, char** argv) {
0120 string vis_name;
0121 vector<string> vol_names;
0122 DetectorHelper helper(description);
0123 VisVolNameProcessor* proc = new VisVolNameProcessor(description);
0124 for ( int i=0; i<argc; ++i ) {
0125 if ( argv[i] ) {
0126 if ( ::strncmp(argv[i],"-name",4) == 0 ) {
0127 proc->name = argv[++i];
0128 vol_names.push_back(proc->name);
0129 if ( vis_name.empty() ) vis_name = proc->name;
0130 continue;
0131 }
0132 else if ( ::strncmp(argv[i],"-match",4) == 0 ) {
0133 vol_names.push_back(argv[++i]);
0134 if ( vis_name.empty() ) vis_name = vol_names.back();
0135 if ( proc->name.empty() ) proc->name = vol_names.back();
0136 continue;
0137 }
0138 else if ( ::strncmp(argv[i],"-vis",4) == 0 ) {
0139 vis_name = argv[++i];
0140 continue;
0141 }
0142 else if ( ::strncmp(argv[i],"-show",4) == 0 ) {
0143 proc->show = true;
0144 continue;
0145 }
0146 cout <<
0147 "Usage: DD4hep_VisVolNameProcessor -arg [-arg] \n"
0148 " -match <regex> Regular expression matching volume name \n"
0149 " -show Print setup to output device (stdout) \n"
0150 "\tArguments given: " << arguments(argc,argv) << endl << flush;
0151 ::exit(EINVAL);
0152 }
0153 }
0154 proc->set_match(vol_names);
0155 proc->visattr = description.visAttributes(vis_name);
0156 if ( !proc->visattr.ptr() ) {
0157 except(proc->name,"+++ Unknown visual attribute: %s",vis_name.c_str());
0158 }
0159 PlacedVolumeProcessor* placement_proc = proc;
0160 return (void*)placement_proc;
0161 }
0162
0163
0164 DECLARE_DD4HEP_CONSTRUCTOR(DD4hep_VisVolNameProcessor,create_object)
0165