Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 09:21:47

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // ********************************************************************
0027 //
0028 //  CaTS (Calorimetry and Tracking Simulation)
0029 //
0030 //  Authors : Hans Wenzel
0031 //            Soon Yung Jun
0032 //            (Fermi National Accelerator Laboratory)
0033 //
0034 // History
0035 //   October 18th, 2021 : first implementation
0036 //
0037 // ********************************************************************
0038 //
0039 /// \file ColorReader.cc
0040 /// \brief extending gdml reader to deal with colors and visualization attributes
0041 //
0042 
0043 // Geant4 headers
0044 #include "G4LogicalVolume.hh"
0045 #include "G4VisAttributes.hh"
0046 // project headers
0047 #include "ColorReader.hh"
0048 #include "ConfigurationManager.hh"
0049 ColorReader::ColorReader()
0050   : G4GDMLReadStructure()
0051 {}
0052 
0053 ColorReader::~ColorReader()
0054 {
0055 #if(G4VERSION_NUMBER > 1072)
0056   for(auto [name, attribute] : fAttribs)
0057   {
0058     delete attribute;
0059   }
0060 #else
0061   std::map<G4String, G4VisAttributes*>::iterator pos;
0062   for(pos = fAttribs.begin(); pos != fAttribs.end(); pos++)
0063   {
0064     delete pos->second;
0065   }
0066 #endif
0067 }
0068 
0069 void ColorReader::ExtensionRead(const xercesc::DOMElement* const extElement)
0070 {
0071   G4bool verbose = ConfigurationManager::getInstance()->isEnable_verbose();
0072   if(verbose)
0073   {
0074     G4cout << "ColorReaderReading GDML extension..." << G4endl;
0075     G4cout << "ColorReader: Reading new GDML extension..." << G4endl;
0076   }
0077   for(xercesc::DOMNode* iter = extElement->getFirstChild(); iter != 0;
0078       iter                   = iter->getNextSibling())
0079   {
0080     if(iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE)
0081     {
0082       continue;
0083     }
0084     const xercesc::DOMElement* const child =
0085       dynamic_cast<xercesc::DOMElement*>(iter);
0086     const G4String tag = Transcode(child->getTagName());
0087     if(verbose)
0088     {
0089       G4cout << "G4GDML:" << tag << G4endl;
0090     }
0091     if(tag == "color")
0092     {
0093       ColorRead(child);
0094     }
0095     else
0096     {
0097       G4String error_msg = "Unknown tag in structure: " + tag;
0098       G4Exception("ColorReader::ExtensionRead()", "ReadError", FatalException,
0099                   error_msg);
0100     }
0101   }
0102 }
0103 
0104 void ColorReader::VolumeRead(const xercesc::DOMElement* const volumeElement)
0105 {
0106   G4bool verbose = ConfigurationManager::getInstance()->isEnable_verbose();
0107   if(verbose)
0108   {
0109     G4cout << "G4GDML: VolumeRead" << G4endl;
0110   }
0111   G4VSolid* solidPtr       = 0;
0112   G4Material* materialPtr  = 0;
0113   G4VisAttributes* attrPtr = 0;
0114   G4GDMLAuxListType auxList;
0115   XMLCh* name_attr    = xercesc::XMLString::transcode("name");
0116   const G4String name = Transcode(volumeElement->getAttribute(name_attr));
0117   xercesc::XMLString::release(&name_attr);
0118   for(xercesc::DOMNode* iter = volumeElement->getFirstChild(); iter != 0;
0119       iter                   = iter->getNextSibling())
0120   {
0121     if(iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE)
0122     {
0123       continue;
0124     }
0125     const xercesc::DOMElement* const child =
0126       dynamic_cast<xercesc::DOMElement*>(iter);
0127     const G4String tag = Transcode(child->getTagName());
0128     if(tag == "auxiliary")
0129     {
0130       auxList.push_back(AuxiliaryRead(child));
0131     }
0132     else if(tag == "materialref")
0133     {
0134       materialPtr = GetMaterial(GenerateName(RefRead(child), true));
0135     }
0136     else if(tag == "solidref")
0137     {
0138       solidPtr = GetSolid(GenerateName(RefRead(child)));
0139     }
0140     else if(tag == "colorref")
0141     {
0142       if(verbose)
0143       {
0144         G4cout << "G4GDML: found visual attribute ..." << G4endl;
0145       }
0146       attrPtr = GetVisAttribute(GenerateName(RefRead(child)));
0147     }
0148   }
0149   pMotherLogical =
0150     new G4LogicalVolume(solidPtr, materialPtr, GenerateName(name), 0, 0, 0);
0151   if(verbose)
0152   {
0153     G4cout << "G4GDML: attaching visual attribute ..." << G4endl;
0154   }
0155   pMotherLogical->SetVisAttributes(attrPtr);
0156   if(!auxList.empty())
0157   {
0158     auxMap[pMotherLogical] = auxList;
0159   }
0160   Volume_contentRead(volumeElement);
0161 }
0162 
0163 void ColorReader::ColorRead(const xercesc::DOMElement* const colorElement)
0164 {
0165   G4bool verbose = ConfigurationManager::getInstance()->isEnable_verbose();
0166   if(verbose)
0167   {
0168     G4cout << "G4GDML: ColorRead" << G4endl;
0169   }
0170   G4String name;
0171   G4VisAttributes* color = 0;
0172   G4double r = 0., g = 0., b = 0., a = 0.;
0173   const xercesc::DOMNamedNodeMap* const attributes =
0174     colorElement->getAttributes();
0175   XMLSize_t attributeCount = attributes->getLength();
0176   for(XMLSize_t attribute_index = 0; attribute_index < attributeCount;
0177       attribute_index++)
0178   {
0179     xercesc::DOMNode* attribute_node = attributes->item(attribute_index);
0180     if(attribute_node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE)
0181     {
0182       continue;
0183     }
0184     const xercesc::DOMAttr* const attribute =
0185       dynamic_cast<xercesc::DOMAttr*>(attribute_node);
0186     const G4String attName  = Transcode(attribute->getName());
0187     const G4String attValue = Transcode(attribute->getValue());
0188     if(attName == "name")
0189     {
0190       name = GenerateName(attValue);
0191     }
0192     else if(attName == "R")
0193     {
0194       r = eval.Evaluate(attValue);
0195     }
0196     else if(attName == "G")
0197     {
0198       g = eval.Evaluate(attValue);
0199     }
0200     else if(attName == "B")
0201     {
0202       b = eval.Evaluate(attValue);
0203     }
0204     else if(attName == "A")
0205     {
0206       a = eval.Evaluate(attValue);
0207     }
0208   }
0209   if(verbose)
0210   {
0211     G4cout << "Color attribute (R,G,B,A) is: " << r << ", " << g << ", " << b
0212            << ", " << a << " !" << G4endl;
0213   }
0214   color = new G4VisAttributes(G4Color(r, g, b, a));
0215   fAttribs.insert(std::make_pair(name, color));
0216 }
0217 
0218 G4VisAttributes* ColorReader::GetVisAttribute(const G4String& ref)
0219 {
0220   G4bool verbose = ConfigurationManager::getInstance()->isEnable_verbose();
0221   if(verbose)
0222   {
0223     G4cout << "G4GDML: GetVisAttribute" << G4endl;
0224   }
0225   G4VisAttributes* col                               = 0;
0226   std::map<G4String, G4VisAttributes*>::iterator pos = fAttribs.find(ref);
0227   if(pos != fAttribs.end())
0228   {
0229     col = pos->second;
0230   }
0231   else
0232   {
0233     G4String err_mess = "Attribute: " + ref + " NOT found !";
0234     G4Exception("ColorReader::GetVisAttribute()", "ReadError", FatalException,
0235                 err_mess);
0236   }
0237   return col;
0238 }