Warning, file /DD4hep/DDCore/src/plugins/VisProcessor.cpp was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef DD4HEP_DDCORE_VISMATERIALPROCESSOR_H
0014 #define DD4HEP_DDCORE_VISMATERIALPROCESSOR_H
0015
0016
0017 #include <DD4hep/VolumeProcessor.h>
0018
0019
0020 namespace dd4hep {
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 class VisMaterialProcessor : public PlacedVolumeProcessor {
0031 public:
0032 Detector& description;
0033 std::string name;
0034 std::vector<Atom> activeElements;
0035 std::vector<Material> activeMaterials;
0036 std::vector<Material> inactiveMaterials;
0037 VisAttr activeVis;
0038 VisAttr inactiveVis;
0039 double fraction = 100e-2;
0040 size_t numActive = 0;
0041 size_t numInactive = 0;
0042 bool setAllInactive = false;
0043 bool show = false;
0044
0045 void _show();
0046 public:
0047
0048 VisMaterialProcessor(Detector& desc);
0049
0050 virtual ~VisMaterialProcessor();
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
0072 #include <DD4hep/Printout.h>
0073 #include <DD4hep/DetectorTools.h>
0074 #include <DD4hep/DetectorHelper.h>
0075 #include <DD4hep/DetFactoryHelper.h>
0076
0077
0078 #include <sstream>
0079
0080 using namespace dd4hep;
0081
0082 namespace {
0083 void set_attr(Volume vol, VisAttr attr) {
0084 if ( vol.visAttributes().ptr() != attr.ptr() ) {
0085 vol.setVisAttributes(attr);
0086 }
0087 }
0088 }
0089
0090
0091
0092 VisMaterialProcessor::VisMaterialProcessor(Detector& desc) : description(desc), name("VisMaterialProcessor")
0093 {
0094 }
0095
0096
0097 VisMaterialProcessor::~VisMaterialProcessor() {
0098 if ( show ) {
0099 if ( activeVis.isValid() )
0100 printout(ALWAYS,name,"++ %8ld active vis-attrs applied: %s", numActive, activeVis.name());
0101 if ( inactiveVis.isValid() )
0102 printout(ALWAYS,name,"++ %8ld inactive vis-attrs applied: %s", numInactive, inactiveVis.name());
0103 }
0104 }
0105
0106
0107 void VisMaterialProcessor::_show() {
0108 if ( show ) {
0109 if ( activeVis.isValid() ) {
0110 for ( Atom atom : activeElements ) {
0111 printout(ALWAYS,name,"++ SETUP Active element: %-11s Vis: %s",atom.name(),activeVis.name());
0112 }
0113 for ( Material mat : activeMaterials ) {
0114 printout(ALWAYS,name,"++ SETUP Active material: %-11s Vis: %s", mat.name(),activeVis.name());
0115 }
0116 }
0117 if ( inactiveVis.isValid() ) {
0118 if ( setAllInactive || !activeElements.empty() ) {
0119 printout(ALWAYS,name,"++ SETUP Inactive material: %-11s Vis: %s", "All-non-active",inactiveVis.name());
0120 }
0121 for ( Material mat : inactiveMaterials ) {
0122 printout(ALWAYS,name,"++ SETUP Inactive material: %-11s Vis: %s", mat.name(),activeVis.name());
0123 }
0124 }
0125 }
0126 }
0127
0128
0129 int VisMaterialProcessor::operator()(PlacedVolume pv, int ) {
0130 Volume vol = pv.volume();
0131 double frac_active = 0.0;
0132 VisAttr attr;
0133
0134 for ( Atom atom : activeElements ) {
0135 frac_active += vol.material().fraction(atom);
0136 }
0137
0138 printout(DEBUG,name,
0139 "++ Volume:%s [%s] active:%s fraction:%.3f active-vis:%s inactive-vis:%s",
0140 pv.name(), vol.name(), yes_no(frac_active >= fraction), frac_active,
0141 yes_no(activeVis.isValid()), yes_no(inactiveVis.isValid()));
0142 if ( activeVis.isValid() ) {
0143 if ( frac_active >= fraction ) {
0144 attr = activeVis;
0145 ++numActive;
0146 }
0147 if ( !attr.isValid() ) {
0148 for ( Material mat : activeMaterials ) {
0149 if ( mat.ptr() == vol.material().ptr() ) {
0150 attr = activeVis;
0151 ++numActive;
0152 break;
0153 }
0154 }
0155 }
0156 }
0157
0158 if ( inactiveVis.isValid() ) {
0159 if ( !attr.isValid() && setAllInactive ) {
0160 attr = inactiveVis;
0161 ++numInactive;
0162 }
0163 else if ( frac_active<fraction ) {
0164 attr = inactiveVis;
0165 ++numInactive;
0166 }
0167 if ( !attr.isValid() && inactiveVis.isValid() ) {
0168 for ( Material imat : inactiveMaterials ) {
0169 if ( imat.ptr() == vol.material().ptr() ) {
0170 attr = inactiveVis;
0171 ++numInactive;
0172 break;
0173 }
0174 }
0175 }
0176 }
0177 if ( attr.isValid() ) {
0178 set_attr(vol,attr);
0179 }
0180 return 1;
0181 }
0182
0183 static void* create_object(Detector& description, int argc, char** argv) {
0184 DetectorHelper helper(description);
0185 VisMaterialProcessor* proc = new VisMaterialProcessor(description);
0186 for ( int i=0; i<argc; ++i ) {
0187 if ( argv[i] ) {
0188 if ( ::strncmp(argv[i],"-vis-active",6) == 0 ) {
0189 VisAttr vis = description.visAttributes(argv[++i]);
0190 if ( vis.isValid() ) proc->activeVis = vis;
0191 continue;
0192 }
0193 else if ( ::strncmp(argv[i],"-vis-inactive",6) == 0 ) {
0194 VisAttr vis = description.visAttributes(argv[++i]);
0195 if ( vis.isValid() ) proc->inactiveVis = vis;
0196 continue;
0197 }
0198 else if ( ::strncmp(argv[i],"-elt-active",6) == 0 ) {
0199 Atom atom = helper.element(argv[++i]);
0200 if ( atom.isValid() ) proc->activeElements.emplace_back(atom);
0201 continue;
0202 }
0203 else if ( ::strncmp(argv[i],"-mat-active",6) == 0 ) {
0204 Material mat = helper.material(argv[++i]);
0205 if ( mat.isValid() ) proc->activeMaterials.emplace_back(mat);
0206 continue;
0207 }
0208 else if ( ::strncmp(argv[i],"-mat-inactive",6) == 0 ) {
0209 Material mat = helper.material(argv[++i]);
0210 if ( mat.isValid() ) proc->inactiveMaterials.emplace_back(mat);
0211 continue;
0212 }
0213 else if ( ::strncmp(argv[i],"-all-inactive",6) == 0 ) {
0214 proc->setAllInactive = true;
0215 continue;
0216 }
0217 else if ( ::strncmp(argv[i],"-fraction",3) == 0 ) {
0218 std::stringstream str(argv[++i]);
0219 if ( str.good() ) {
0220 str >> proc->fraction;
0221 if ( !str.fail() ) continue;
0222 }
0223 }
0224 else if ( ::strncmp(argv[i],"-path",4) == 0 ) {
0225 std::string path = argv[++i];
0226 DetElement de = detail::tools::findElement(description,path);
0227 if ( de.isValid() ) continue;
0228 printout(ERROR,"VisMaterialProcessor","++ Invalid DetElement path: %s",path.c_str());
0229 }
0230 else if ( ::strncmp(argv[i],"-name",4) == 0 ) {
0231 proc->name = argv[++i];
0232 continue;
0233 }
0234 else if ( ::strncmp(argv[i],"-show",4) == 0 ) {
0235 proc->show = true;
0236 continue;
0237 }
0238 std::cout <<
0239 "Usage: DD4hep_VisMaterialProcessor -arg [-arg] \n"
0240 " -vis-active <name> Set the visualization attribute for active materials\n"
0241 " -vis-inactive <name> Set the visualization attribute for inactive materials\n"
0242 " -elt-active <name> Add active element by name. If the fractional sum of \n"
0243 " all active elements in a volume exceeds <fraction> \n"
0244 " the volume is considered active \n"
0245 " -mat-active <name> Add material by name to the list of active materials\n"
0246 " -mat-inactive <name> Add material by name to the list of inactive materials\n"
0247 " -all-inactive Auto set all volumes inactive, which are NOT active \n"
0248 " -fraction <double> Set the fraction above which the active elment content\n"
0249 " defines an active volume. \n"
0250 " -show Print setup to output device (stdout) \n"
0251 "\tArguments given: " << arguments(argc,argv) << std::endl << std::flush;
0252 ::exit(EINVAL);
0253 }
0254 }
0255 proc->_show();
0256 PlacedVolumeProcessor* placement_proc = proc;
0257 return (void*)placement_proc;
0258 }
0259
0260
0261 DECLARE_DD4HEP_CONSTRUCTOR(DD4hep_VisMaterialProcessor,create_object)