Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:16:50

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 #ifndef DD4HEP_DDCORE_VISVOLNAMEPROCESSOR_H
0014 #define DD4HEP_DDCORE_VISVOLNAMEPROCESSOR_H
0015 
0016 // Framework include files
0017 #include <DD4hep/VolumeProcessor.h>
0018 
0019 // C/C++ include files
0020 #include <map>
0021 #include <regex>
0022 
0023 /// Namespace for the AIDA detector description toolkit
0024 namespace dd4hep  {
0025   
0026   /// DD4hep DetElement creator for the CMS geometry.
0027   /*  Set visualization attributes for sensitive volumes
0028    *
0029    *  \author  M.Frank
0030    *  \version 1.0
0031    *  \ingroup DD4HEP_CORE
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     /// Print properties
0043     void _show();
0044   public:
0045     /// Initializing constructor
0046     VisVolNameProcessor(Detector& desc);
0047     /// Default destructor
0048     virtual ~VisVolNameProcessor();
0049     /// Set volume matches
0050     void set_match(const std::vector<std::string>& matches);
0051     /// Callback to output PlacedVolume information of an single Placement
0052     virtual int operator()(PlacedVolume pv, int level);
0053   };
0054 }
0055 #endif //  DD4HEP_DDCORE_VISVOLNAMEPROCESSOR_H
0056 
0057 //==========================================================================
0058 //  AIDA Detector description implementation 
0059 //--------------------------------------------------------------------------
0060 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0061 // All rights reserved.
0062 //
0063 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0064 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0065 //
0066 // Author     : M.Frank
0067 //
0068 //==========================================================================
0069 
0070 //#include <DD4hep/VisVolNameProcessor.h>
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 /// Initializing constructor
0081 VisVolNameProcessor::VisVolNameProcessor(Detector& desc)
0082   : description(desc), name()
0083 {
0084 }
0085 
0086 /// Default destructor
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 /// Set volume matches
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 /// Callback to output PlacedVolume information of an single Placement
0101 int VisVolNameProcessor::operator()(PlacedVolume pv, int /* level */)   {
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       //printout(ALWAYS,m.first,"++       FAILED %s",vol_nam.c_str());
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 // first argument is the type from the xml file
0164 DECLARE_DD4HEP_CONSTRUCTOR(DD4hep_VisVolNameProcessor,create_object)
0165