File indexing completed on 2026-04-09 07:49:04
0001 #include <iostream>
0002 #include <iomanip>
0003 #include <cstring>
0004 #include <csignal>
0005 #include <sstream>
0006 #include <xercesc/util/PlatformUtils.hpp>
0007
0008 #include "GDXMLRead.hh"
0009 #include "GDXMLWrite.hh"
0010 #include "GDXML.hh"
0011
0012 #include "SStr.hh"
0013 #include "SLOG.hh"
0014
0015 const plog::Severity GDXML::LEVEL = SLOG::EnvLevel("GDXML", "DEBUG" );
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 void GDXML::Fix(const char* dstpath, const char* srcpath)
0036 {
0037 xercesc::XMLPlatformUtils::Initialize();
0038
0039 bool same = strcmp(dstpath, srcpath) == 0 ;
0040 bool expect = same == false ;
0041 assert(expect);
0042 if(!expect) std::raise(SIGINT);
0043
0044 GDXML gd(srcpath);
0045 gd.write(dstpath);
0046 }
0047
0048
0049 GDXML::GDXML(const char* srcpath_)
0050 :
0051 srcpath(strdup(srcpath_)),
0052 kludge_truncated_matrix(true),
0053 reader(new GDXMLRead(srcpath, kludge_truncated_matrix)),
0054 doc(const_cast<xercesc::DOMDocument*>(reader->doc)),
0055 defineElement(reader->the_defineElement),
0056 num_duplicated_matrixElement(reader->checkDuplicatedMatrix()),
0057 num_pruned_matrixElement(reader->pruneDuplicatedMatrix()),
0058 num_truncated_matrixElement(reader->truncated_matrixElement.size()),
0059 num_constants(reader->constants.size()),
0060 writer(new GDXMLWrite(doc)),
0061 issues(false)
0062 {
0063 if(num_constants > 0 ) replaceAllConstantWithMatrix();
0064 issues = (num_truncated_matrixElement > 0 || num_constants > 0 ) ;
0065 }
0066
0067 std::string GDXML::desc() const
0068 {
0069 std::stringstream ss ;
0070 ss << "GDXML::desc" << std::endl
0071 << " srcpath " << srcpath << std::endl
0072 << " num_duplicated_matrixElement " << num_duplicated_matrixElement << std::endl
0073 << " num_pruned_matrixElement " << num_pruned_matrixElement << std::endl
0074 << " num_truncated_matrixElement " << num_truncated_matrixElement << std::endl
0075 << " num_constants " << num_constants << std::endl
0076 << " issues " << ( issues ? "YES" : "NO" ) << std::endl
0077 ;
0078 std::string s = ss.str();
0079 return s ;
0080 }
0081
0082 void GDXML::write(const char* dstpath)
0083 {
0084 const char* txtpath = SStr::ReplaceEnd(dstpath, ".gdml", "_gdxml_report.txt" );
0085 LOG(LEVEL) << "writing .gdml dstpath " << dstpath ;
0086 LOG(LEVEL) << "writing .txt report txtpath " << txtpath ;
0087 writer->write(dstpath);
0088 std::string rep = desc();
0089 LOG(LEVEL) << " rep " << std::endl << rep ;
0090 SStr::Save(txtpath, rep.c_str() );
0091 }
0092
0093 GDXML::~GDXML()
0094 {
0095 }
0096
0097 void GDXML::replaceAllConstantWithMatrix()
0098 {
0099 assert( defineElement );
0100 for(unsigned i=0 ; i < num_constants ; i++)
0101 {
0102 const Constant& c = reader->constants[i] ;
0103 LOG(LEVEL)
0104 << " c.name " << std::setw(20) << c.name
0105 << " c.value " << std::setw(10) << c.value
0106 ;
0107
0108 double nm_lo = 80. ;
0109 double nm_hi = 800. ;
0110 xercesc::DOMElement* matrixElement = writer->ConstantToMatrixElement(c.name.c_str(), c.value, nm_lo, nm_hi );
0111 defineElement->appendChild(matrixElement);
0112 }
0113 }
0114
0115