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] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  1 //
  2 // ********************************************************************
  3 // * License and Disclaimer                                           *
  4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.                             *
 10 // *                                                                  *
 11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                                                  *
 18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // ********************************************************************
 25 //
 26 // ********************************************************************
 27 //
 28 //  CaTS (Calorimetry and Tracking Simulation)
 29 //
 30 //  Authors : Hans Wenzel
 31 //            Soon Yung Jun
 32 //            (Fermi National Accelerator Laboratory)
 33 //
 34 // History
 35 //   October 18th, 2021 : first implementation
 36 //
 37 // ********************************************************************
 38 //
 39 /// \file ColorReader.cc
 40 /// \brief extending gdml reader to deal with colors and visualization attributes
 41 //
 42 
 43 // Geant4 headers
 44 #include "G4LogicalVolume.hh"
 45 #include "G4VisAttributes.hh"
 46 // project headers
 47 #include "ColorReader.hh"
 48 #include "ConfigurationManager.hh"
 49 ColorReader::ColorReader()
 50   : G4GDMLReadStructure()
 51 {}
 52 
 53 ColorReader::~ColorReader()
 54 {
 55 #if(G4VERSION_NUMBER > 1072)
 56   for(auto [name, attribute] : fAttribs)
 57   {
 58     delete attribute;
 59   }
 60 #else
 61   std::map<G4String, G4VisAttributes*>::iterator pos;
 62   for(pos = fAttribs.begin(); pos != fAttribs.end(); pos++)
 63   {
 64     delete pos->second;
 65   }
 66 #endif
 67 }
 68 
 69 void ColorReader::ExtensionRead(const xercesc::DOMElement* const extElement)
 70 {
 71   G4bool verbose = ConfigurationManager::getInstance()->isEnable_verbose();
 72   if(verbose)
 73   {
 74     G4cout << "ColorReaderReading GDML extension..." << G4endl;
 75     G4cout << "ColorReader: Reading new GDML extension..." << G4endl;
 76   }
 77   for(xercesc::DOMNode* iter = extElement->getFirstChild(); iter != 0;
 78       iter                   = iter->getNextSibling())
 79   {
 80     if(iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE)
 81     {
 82       continue;
 83     }
 84     const xercesc::DOMElement* const child =
 85       dynamic_cast<xercesc::DOMElement*>(iter);
 86     const G4String tag = Transcode(child->getTagName());
 87     if(verbose)
 88     {
 89       G4cout << "G4GDML:" << tag << G4endl;
 90     }
 91     if(tag == "color")
 92     {
 93       ColorRead(child);
 94     }
 95     else
 96     {
 97       G4String error_msg = "Unknown tag in structure: " + tag;
 98       G4Exception("ColorReader::ExtensionRead()", "ReadError", FatalException,
 99                   error_msg);
100     }
101   }
102 }
103 
104 void ColorReader::VolumeRead(const xercesc::DOMElement* const volumeElement)
105 {
106   G4bool verbose = ConfigurationManager::getInstance()->isEnable_verbose();
107   if(verbose)
108   {
109     G4cout << "G4GDML: VolumeRead" << G4endl;
110   }
111   G4VSolid* solidPtr       = 0;
112   G4Material* materialPtr  = 0;
113   G4VisAttributes* attrPtr = 0;
114   G4GDMLAuxListType auxList;
115   XMLCh* name_attr    = xercesc::XMLString::transcode("name");
116   const G4String name = Transcode(volumeElement->getAttribute(name_attr));
117   xercesc::XMLString::release(&name_attr);
118   for(xercesc::DOMNode* iter = volumeElement->getFirstChild(); iter != 0;
119       iter                   = iter->getNextSibling())
120   {
121     if(iter->getNodeType() != xercesc::DOMNode::ELEMENT_NODE)
122     {
123       continue;
124     }
125     const xercesc::DOMElement* const child =
126       dynamic_cast<xercesc::DOMElement*>(iter);
127     const G4String tag = Transcode(child->getTagName());
128     if(tag == "auxiliary")
129     {
130       auxList.push_back(AuxiliaryRead(child));
131     }
132     else if(tag == "materialref")
133     {
134       materialPtr = GetMaterial(GenerateName(RefRead(child), true));
135     }
136     else if(tag == "solidref")
137     {
138       solidPtr = GetSolid(GenerateName(RefRead(child)));
139     }
140     else if(tag == "colorref")
141     {
142       if(verbose)
143       {
144         G4cout << "G4GDML: found visual attribute ..." << G4endl;
145       }
146       attrPtr = GetVisAttribute(GenerateName(RefRead(child)));
147     }
148   }
149   pMotherLogical =
150     new G4LogicalVolume(solidPtr, materialPtr, GenerateName(name), 0, 0, 0);
151   if(verbose)
152   {
153     G4cout << "G4GDML: attaching visual attribute ..." << G4endl;
154   }
155   pMotherLogical->SetVisAttributes(attrPtr);
156   if(!auxList.empty())
157   {
158     auxMap[pMotherLogical] = auxList;
159   }
160   Volume_contentRead(volumeElement);
161 }
162 
163 void ColorReader::ColorRead(const xercesc::DOMElement* const colorElement)
164 {
165   G4bool verbose = ConfigurationManager::getInstance()->isEnable_verbose();
166   if(verbose)
167   {
168     G4cout << "G4GDML: ColorRead" << G4endl;
169   }
170   G4String name;
171   G4VisAttributes* color = 0;
172   G4double r = 0., g = 0., b = 0., a = 0.;
173   const xercesc::DOMNamedNodeMap* const attributes =
174     colorElement->getAttributes();
175   XMLSize_t attributeCount = attributes->getLength();
176   for(XMLSize_t attribute_index = 0; attribute_index < attributeCount;
177       attribute_index++)
178   {
179     xercesc::DOMNode* attribute_node = attributes->item(attribute_index);
180     if(attribute_node->getNodeType() != xercesc::DOMNode::ATTRIBUTE_NODE)
181     {
182       continue;
183     }
184     const xercesc::DOMAttr* const attribute =
185       dynamic_cast<xercesc::DOMAttr*>(attribute_node);
186     const G4String attName  = Transcode(attribute->getName());
187     const G4String attValue = Transcode(attribute->getValue());
188     if(attName == "name")
189     {
190       name = GenerateName(attValue);
191     }
192     else if(attName == "R")
193     {
194       r = eval.Evaluate(attValue);
195     }
196     else if(attName == "G")
197     {
198       g = eval.Evaluate(attValue);
199     }
200     else if(attName == "B")
201     {
202       b = eval.Evaluate(attValue);
203     }
204     else if(attName == "A")
205     {
206       a = eval.Evaluate(attValue);
207     }
208   }
209   if(verbose)
210   {
211     G4cout << "Color attribute (R,G,B,A) is: " << r << ", " << g << ", " << b
212            << ", " << a << " !" << G4endl;
213   }
214   color = new G4VisAttributes(G4Color(r, g, b, a));
215   fAttribs.insert(std::make_pair(name, color));
216 }
217 
218 G4VisAttributes* ColorReader::GetVisAttribute(const G4String& ref)
219 {
220   G4bool verbose = ConfigurationManager::getInstance()->isEnable_verbose();
221   if(verbose)
222   {
223     G4cout << "G4GDML: GetVisAttribute" << G4endl;
224   }
225   G4VisAttributes* col                               = 0;
226   std::map<G4String, G4VisAttributes*>::iterator pos = fAttribs.find(ref);
227   if(pos != fAttribs.end())
228   {
229     col = pos->second;
230   }
231   else
232   {
233     G4String err_mess = "Attribute: " + ref + " NOT found !";
234     G4Exception("ColorReader::GetVisAttribute()", "ReadError", FatalException,
235                 err_mess);
236   }
237   return col;
238 }
239