DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
VisProcessor.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 #ifndef DD4HEP_DDCORE_VISMATERIALPROCESSOR_H
14 #define DD4HEP_DDCORE_VISMATERIALPROCESSOR_H
15 
17 #include <DD4hep/VolumeProcessor.h>
18 
20 namespace dd4hep {
21 
22 
24  /* Set visualization attributes for sensitive volumes
25  *
26  * \author M.Frank
27  * \version 1.0
28  * \ingroup DD4HEP_CORE
29  */
31  public:
33  std::string name;
34  std::vector<Atom> activeElements;
35  std::vector<Material> activeMaterials;
36  std::vector<Material> inactiveMaterials;
39  double fraction = 100e-2;
40  size_t numActive = 0;
41  size_t numInactive = 0;
42  bool setAllInactive = false;
43  bool show = false;
45  void _show();
46  public:
50  virtual ~VisMaterialProcessor();
52  virtual int operator()(PlacedVolume pv, int level);
53  };
54 }
55 #endif // DD4HEP_DDCORE_VISMATERIALPROCESSOR_H
56 
57 //==========================================================================
58 // AIDA Detector description implementation
59 //--------------------------------------------------------------------------
60 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
61 // All rights reserved.
62 //
63 // For the licensing terms see $DD4hepINSTALL/LICENSE.
64 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
65 //
66 // Author : M.Frank
67 //
68 //==========================================================================
69 
71 //#include <DD4hep/VisMaterialProcessor.h>
72 #include <DD4hep/Printout.h>
73 #include <DD4hep/DetectorTools.h>
74 #include <DD4hep/DetectorHelper.h>
76 
78 #include <sstream>
79 
80 using namespace dd4hep;
81 
82 namespace {
83  void set_attr(Volume vol, VisAttr attr) {
84  if ( vol.visAttributes().ptr() != attr.ptr() ) {
85  vol.setVisAttributes(attr);
86  }
87  }
88 }
89 
90 
92 VisMaterialProcessor::VisMaterialProcessor(Detector& desc) : description(desc), name("VisMaterialProcessor")
93 {
94 }
95 
98  if ( show ) {
99  if ( activeVis.isValid() )
100  printout(ALWAYS,name,"++ %8ld active vis-attrs applied: %s", numActive, activeVis.name());
101  if ( inactiveVis.isValid() )
102  printout(ALWAYS,name,"++ %8ld inactive vis-attrs applied: %s", numInactive, inactiveVis.name());
103  }
104 }
105 
108  if ( show ) {
109  if ( activeVis.isValid() ) {
110  for ( Atom atom : activeElements ) {
111  printout(ALWAYS,name,"++ SETUP Active element: %-11s Vis: %s",atom.name(),activeVis.name());
112  }
113  for ( Material mat : activeMaterials ) {
114  printout(ALWAYS,name,"++ SETUP Active material: %-11s Vis: %s", mat.name(),activeVis.name());
115  }
116  }
117  if ( inactiveVis.isValid() ) {
118  if ( setAllInactive || !activeElements.empty() ) {
119  printout(ALWAYS,name,"++ SETUP Inactive material: %-11s Vis: %s", "All-non-active",inactiveVis.name());
120  }
121  for ( Material mat : inactiveMaterials ) {
122  printout(ALWAYS,name,"++ SETUP Inactive material: %-11s Vis: %s", mat.name(),activeVis.name());
123  }
124  }
125  }
126 }
127 
130  Volume vol = pv.volume();
131  double frac_active = 0.0;
132  VisAttr attr;
133 
134  for ( Atom atom : activeElements ) {
135  frac_active += vol.material().fraction(atom);
136  }
137  //if ( frac_active >= fraction )
138  printout(DEBUG,name,
139  "++ Volume:%s [%s] active:%s fraction:%.3f active-vis:%s inactive-vis:%s",
140  pv.name(), vol.name(), yes_no(frac_active >= fraction), frac_active,
141  yes_no(activeVis.isValid()), yes_no(inactiveVis.isValid()));
142  if ( activeVis.isValid() ) {
143  if ( frac_active >= fraction ) {
144  attr = activeVis;
145  ++numActive;
146  }
147  if ( !attr.isValid() ) {
148  for ( Material mat : activeMaterials ) {
149  if ( mat.ptr() == vol.material().ptr() ) {
150  attr = activeVis;
151  ++numActive;
152  break;
153  }
154  }
155  }
156  }
157  // If we get here, the material is definitely inactive
158  if ( inactiveVis.isValid() ) {
159  if ( !attr.isValid() && setAllInactive ) {
160  attr = inactiveVis;
161  ++numInactive;
162  }
163  else if ( frac_active<fraction ) {
164  attr = inactiveVis;
165  ++numInactive;
166  }
167  if ( !attr.isValid() && inactiveVis.isValid() ) {
168  for ( Material imat : inactiveMaterials ) {
169  if ( imat.ptr() == vol.material().ptr() ) {
170  attr = inactiveVis;
171  ++numInactive;
172  break;
173  }
174  }
175  }
176  }
177  if ( attr.isValid() ) {
178  set_attr(vol,attr);
179  }
180  return 1;
181 }
182 
183 static void* create_object(Detector& description, int argc, char** argv) {
184  DetectorHelper helper(description);
185  VisMaterialProcessor* proc = new VisMaterialProcessor(description);
186  for ( int i=0; i<argc; ++i ) {
187  if ( argv[i] ) {
188  if ( ::strncmp(argv[i],"-vis-active",6) == 0 ) {
189  VisAttr vis = description.visAttributes(argv[++i]);
190  if ( vis.isValid() ) proc->activeVis = vis;
191  continue;
192  }
193  else if ( ::strncmp(argv[i],"-vis-inactive",6) == 0 ) {
194  VisAttr vis = description.visAttributes(argv[++i]);
195  if ( vis.isValid() ) proc->inactiveVis = vis;
196  continue;
197  }
198  else if ( ::strncmp(argv[i],"-elt-active",6) == 0 ) {
199  Atom atom = helper.element(argv[++i]);
200  if ( atom.isValid() ) proc->activeElements.emplace_back(atom);
201  continue;
202  }
203  else if ( ::strncmp(argv[i],"-mat-active",6) == 0 ) {
204  Material mat = helper.material(argv[++i]);
205  if ( mat.isValid() ) proc->activeMaterials.emplace_back(mat);
206  continue;
207  }
208  else if ( ::strncmp(argv[i],"-mat-inactive",6) == 0 ) {
209  Material mat = helper.material(argv[++i]);
210  if ( mat.isValid() ) proc->inactiveMaterials.emplace_back(mat);
211  continue;
212  }
213  else if ( ::strncmp(argv[i],"-all-inactive",6) == 0 ) {
214  proc->setAllInactive = true;
215  continue;
216  }
217  else if ( ::strncmp(argv[i],"-fraction",3) == 0 ) {
218  std::stringstream str(argv[++i]);
219  if ( str.good() ) {
220  str >> proc->fraction;
221  if ( !str.fail() ) continue;
222  }
223  }
224  else if ( ::strncmp(argv[i],"-path",4) == 0 ) {
225  std::string path = argv[++i];
226  DetElement de = detail::tools::findElement(description,path);
227  if ( de.isValid() ) continue;
228  printout(ERROR,"VisMaterialProcessor","++ Invalid DetElement path: %s",path.c_str());
229  }
230  else if ( ::strncmp(argv[i],"-name",4) == 0 ) {
231  proc->name = argv[++i];
232  continue;
233  }
234  else if ( ::strncmp(argv[i],"-show",4) == 0 ) {
235  proc->show = true;
236  continue;
237  }
238  std::cout <<
239  "Usage: DD4hep_VisMaterialProcessor -arg [-arg] \n"
240  " -vis-active <name> Set the visualization attribute for active materials\n"
241  " -vis-inactive <name> Set the visualization attribute for inactive materials\n"
242  " -elt-active <name> Add active element by name. If the fractional sum of \n"
243  " all active elements in a volume exceeds <fraction> \n"
244  " the volume is considered active \n"
245  " -mat-active <name> Add material by name to the list of active materials\n"
246  " -mat-inactive <name> Add material by name to the list of inactive materials\n"
247  " -all-inactive Auto set all volumes inactive, which are NOT active \n"
248  " -fraction <double> Set the fraction above which the active elment content\n"
249  " defines an active volume. \n"
250  " -show Print setup to output device (stdout) \n"
251  "\tArguments given: " << arguments(argc,argv) << std::endl << std::flush;
252  ::exit(EINVAL);
253  }
254  }
255  proc->_show();
256  PlacedVolumeProcessor* placement_proc = proc;
257  return (void*)placement_proc;
258 }
259 
260 // first argument is the type from the xml file
261 DECLARE_DD4HEP_CONSTRUCTOR(DD4hep_VisMaterialProcessor,create_object)
dd4hep::PlacedVolumeProcessor
Generic PlacedVolume processor.
Definition: VolumeProcessor.h:37
dd4hep::VisMaterialProcessor::inactiveMaterials
std::vector< Material > inactiveMaterials
Definition: VisProcessor.cpp:36
dd4hep::VisMaterialProcessor::numActive
size_t numActive
Definition: VisProcessor.cpp:40
dd4hep::Atom
Handle class describing an element in the periodic table.
Definition: Objects.h:242
dd4hep::VisMaterialProcessor::~VisMaterialProcessor
virtual ~VisMaterialProcessor()
Default destructor.
Definition: VisProcessor.cpp:97
dd4hep::VisMaterialProcessor::setAllInactive
bool setAllInactive
Definition: VisProcessor.cpp:42
dd4hep::PlacedVolume
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:173
dd4hep::VisAttr
Handle class describing visualization attributes.
Definition: Objects.h:324
dd4hep::VisMaterialProcessor::show
bool show
Definition: VisProcessor.cpp:43
dd4hep::VisMaterialProcessor::activeVis
VisAttr activeVis
Definition: VisProcessor.cpp:37
dd4hep::Handle::isValid
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:128
DetectorHelper.h
dd4hep::Handle::name
const char * name() const
Access the object name (or "" if not supported by the object)
dd4hep::VisMaterialProcessor::activeElements
std::vector< Atom > activeElements
Definition: VisProcessor.cpp:34
dd4hep::VisMaterialProcessor::numInactive
size_t numInactive
Definition: VisProcessor.cpp:41
dd4hep::Material::fraction
double fraction(Atom atom) const
Access the fraction of an element within the material.
Definition: Objects.cpp:232
dd4hep::Volume::material
Material material() const
Access to the Volume material.
Definition: Volumes.cpp:1122
dd4hep::detail::tools::findElement
DetElement findElement(const Detector &description, const std::string &path)
Find DetElement as child of the top level volume by its absolute path.
Definition: DetectorTools.cpp:214
dd4hep::Material
Handle class describing a material.
Definition: Objects.h:272
dd4hep::DetElement
Handle class describing a detector element.
Definition: DetElement.h:188
dd4hep::Volume
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:378
dd4hep::VisMaterialProcessor::inactiveVis
VisAttr inactiveVis
Definition: VisProcessor.cpp:38
VolumeProcessor.h
DetectorTools.h
dd4hep::VisMaterialProcessor::_show
void _show()
Print properties.
Definition: VisProcessor.cpp:107
DECLARE_DD4HEP_CONSTRUCTOR
#define DECLARE_DD4HEP_CONSTRUCTOR(name, func)
Definition: Factories.h:291
dd4hep::VisMaterialProcessor::activeMaterials
std::vector< Material > activeMaterials
Definition: VisProcessor.cpp:35
dd4hep::VisMaterialProcessor
DD4hep DetElement creator for the CMS geometry.
Definition: VisProcessor.cpp:30
dd4hep::DetectorHelper
DetectorHelper: class to shortcut certain questions to the dd4hep detector description interface.
Definition: DetectorHelper.h:32
dd4hep::Detector::visAttributes
virtual const HandleMap & visAttributes() const =0
Accessor to the map of visualisation attributes.
dd4hep::VisMaterialProcessor::description
Detector & description
Definition: VisProcessor.cpp:32
dd4hep::VisMaterialProcessor::fraction
double fraction
Definition: VisProcessor.cpp:39
DetFactoryHelper.h
dd4hep::VisMaterialProcessor::operator()
virtual int operator()(PlacedVolume pv, int level)
Callback to output PlacedVolume information of an single Placement.
Definition: VisProcessor.cpp:129
dd4hep::Handle::ptr
T * ptr() const
Access to the held object.
Definition: Handle.h:153
dd4hep::Volume::setVisAttributes
const Volume & setVisAttributes(const VisAttr &obj) const
Set Visualization attributes to the volume.
Definition: Volumes.cpp:1127
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::PlacedVolume::volume
Volume volume() const
Logical volume of this placement.
Definition: Volumes.cpp:452
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
dd4hep::Volume::visAttributes
VisAttr visAttributes() const
Access the visualisation attributes.
Definition: Volumes.cpp:1210
dd4hep::VisMaterialProcessor::VisMaterialProcessor
VisMaterialProcessor(Detector &desc)
Initializing constructor.
Definition: VisProcessor.cpp:92
dd4hep::VisMaterialProcessor::name
std::string name
Definition: VisProcessor.cpp:33
Printout.h