DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
VisVolNameProcessor.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_VISVOLNAMEPROCESSOR_H
14 #define DD4HEP_DDCORE_VISVOLNAMEPROCESSOR_H
15 
16 // Framework include files
17 #include <DD4hep/VolumeProcessor.h>
18 
19 // C/C++ include files
20 #include <map>
21 #include <regex>
22 
24 namespace dd4hep {
25 
27  /* Set visualization attributes for sensitive volumes
28  *
29  * \author M.Frank
30  * \version 1.0
31  * \ingroup DD4HEP_CORE
32  */
34  public:
35  typedef std::map<std::string,std::regex> Matches;
38  std::string name;
39  size_t numApplied = 0;
40  bool show = false;
43  void _show();
44  public:
48  virtual ~VisVolNameProcessor();
50  void set_match(const std::vector<std::string>& matches);
52  virtual int operator()(PlacedVolume pv, int level);
53  };
54 }
55 #endif // DD4HEP_DDCORE_VISVOLNAMEPROCESSOR_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 
70 //#include <DD4hep/VisVolNameProcessor.h>
71 #include <DD4hep/Printout.h>
72 #include <DD4hep/DetectorTools.h>
73 #include <DD4hep/DetectorHelper.h>
75 #include <sstream>
76 
77 using namespace std;
78 using namespace dd4hep;
79 
81 VisVolNameProcessor::VisVolNameProcessor(Detector& desc)
82  : description(desc), name()
83 {
84 }
85 
88  if ( show ) {
89  printout(ALWAYS,name,"++ %8ld vis-attrs '%s' applied.",
90  numApplied, visattr.isValid() ? visattr.name() : "");
91  }
92 }
93 
95 void VisVolNameProcessor::set_match(const std::vector<std::string>& vals) {
96  for( const auto& v : vals )
97  matches[v] = regex(v);
98 }
99 
102  Volume vol = pv.volume();
103  if ( vol.visAttributes().ptr() != visattr.ptr() ) {
104  string vol_nam = vol.name();
105  for ( const auto& match : matches ) {
106  if ( std::regex_match(vol_nam, match.second) ) {
107  printout(ALWAYS,match.first,"++ Set visattr %s to %s",
108  visattr.isValid() ? visattr.name() : "", vol_nam.c_str());
110  ++numApplied;
111  return 1;
112  }
113  //printout(ALWAYS,m.first,"++ FAILED %s",vol_nam.c_str());
114  }
115  }
116  return 1;
117 }
118 
119 static void* create_object(Detector& description, int argc, char** argv) {
120  string vis_name;
121  vector<string> vol_names;
122  DetectorHelper helper(description);
123  VisVolNameProcessor* proc = new VisVolNameProcessor(description);
124  for ( int i=0; i<argc; ++i ) {
125  if ( argv[i] ) {
126  if ( ::strncmp(argv[i],"-name",4) == 0 ) {
127  proc->name = argv[++i];
128  vol_names.push_back(proc->name);
129  if ( vis_name.empty() ) vis_name = proc->name;
130  continue;
131  }
132  else if ( ::strncmp(argv[i],"-match",4) == 0 ) {
133  vol_names.push_back(argv[++i]);
134  if ( vis_name.empty() ) vis_name = vol_names.back();
135  if ( proc->name.empty() ) proc->name = vol_names.back();
136  continue;
137  }
138  else if ( ::strncmp(argv[i],"-vis",4) == 0 ) {
139  vis_name = argv[++i];
140  continue;
141  }
142  else if ( ::strncmp(argv[i],"-show",4) == 0 ) {
143  proc->show = true;
144  continue;
145  }
146  cout <<
147  "Usage: DD4hep_VisVolNameProcessor -arg [-arg] \n"
148  " -match <regex> Regular expression matching volume name \n"
149  " -show Print setup to output device (stdout) \n"
150  "\tArguments given: " << arguments(argc,argv) << endl << flush;
151  ::exit(EINVAL);
152  }
153  }
154  proc->set_match(vol_names);
155  proc->visattr = description.visAttributes(vis_name);
156  if ( !proc->visattr.ptr() ) {
157  except(proc->name,"+++ Unknown visual attribute: %s",vis_name.c_str());
158  }
159  PlacedVolumeProcessor* placement_proc = proc;
160  return (void*)placement_proc;
161 }
162 
163 // first argument is the type from the xml file
164 DECLARE_DD4HEP_CONSTRUCTOR(DD4hep_VisVolNameProcessor,create_object)
165 
dd4hep::PlacedVolumeProcessor
Generic PlacedVolume processor.
Definition: VolumeProcessor.h:37
v
View * v
Definition: MultiView.cpp:28
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::VisVolNameProcessor::numApplied
size_t numApplied
Definition: VisVolNameProcessor.cpp:39
dd4hep::VisVolNameProcessor
DD4hep DetElement creator for the CMS geometry.
Definition: VisVolNameProcessor.cpp:33
dd4hep::Handle::isValid
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:128
dd4hep::VisVolNameProcessor::matches
Matches matches
Definition: VisVolNameProcessor.cpp:37
DetectorHelper.h
dd4hep::Handle::name
const char * name() const
Access the object name (or "" if not supported by the object)
dd4hep::VisVolNameProcessor::Matches
std::map< std::string, std::regex > Matches
Definition: VisVolNameProcessor.cpp:35
dd4hep::Volume
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:378
dd4hep::VisVolNameProcessor::VisVolNameProcessor
VisVolNameProcessor(Detector &desc)
Initializing constructor.
Definition: VisVolNameProcessor.cpp:81
VolumeProcessor.h
DetectorTools.h
DECLARE_DD4HEP_CONSTRUCTOR
#define DECLARE_DD4HEP_CONSTRUCTOR(name, func)
Definition: Factories.h:291
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.
DetFactoryHelper.h
std
Definition: Plugins.h:30
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::VisVolNameProcessor::visattr
VisAttr visattr
Definition: VisVolNameProcessor.cpp:41
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
dd4hep::VisVolNameProcessor::show
bool show
Definition: VisVolNameProcessor.cpp:40
dd4hep::VisVolNameProcessor::set_match
void set_match(const std::vector< std::string > &matches)
Set volume matches.
Definition: VisVolNameProcessor.cpp:95
dd4hep::VisVolNameProcessor::~VisVolNameProcessor
virtual ~VisVolNameProcessor()
Default destructor.
Definition: VisVolNameProcessor.cpp:87
dd4hep::VisVolNameProcessor::name
std::string name
Definition: VisVolNameProcessor.cpp:38
dd4hep::Volume::visAttributes
VisAttr visAttributes() const
Access the visualisation attributes.
Definition: Volumes.cpp:1210
Printout.h
dd4hep::VisVolNameProcessor::operator()
virtual int operator()(PlacedVolume pv, int level)
Callback to output PlacedVolume information of an single Placement.
Definition: VisVolNameProcessor.cpp:101
dd4hep::VisVolNameProcessor::_show
void _show()
Print properties.
dd4hep::VisVolNameProcessor::description
Detector & description
Definition: VisVolNameProcessor.cpp:36