00001
00002
00003
00004 #include "GaudiKernel/DeclareFactoryEntries.h"
00005 #include "GaudiKernel/MsgStream.h"
00006
00007 #include <xercesc/dom/DOMElement.hpp>
00008 #include <xercesc/dom/DOMNodeList.hpp>
00009
00010 #include "XmlTools/IXmlSvc.h"
00011 #include <DetDesc/ILVolume.h>
00012
00013 #include "SimulationSvc.h"
00014
00015
00016
00017
00018
00019
00020
00021
00022 DECLARE_SERVICE_FACTORY( SimulationSvc );
00023
00024
00025
00026
00027
00028
00029 const std::string dom2Std (const XMLCh* domString) {
00030 char *cString = xercesc::XMLString::transcode(domString);
00031 std::string stdString;
00032 if (cString) {
00033 stdString = cString;
00034 xercesc::XMLString::release(&cString);
00035 }
00036 return stdString;
00037 }
00038
00039
00040
00041
00042
00043 SimulationSvc::SimulationSvc (const std::string& name, ISvcLocator* svc) :
00044 Service (name, svc) {
00045 declareProperty ("SimulationDbLocation", m_simDbLocation="empty" );
00046 }
00047
00048 SimulationSvc::~SimulationSvc()
00049 {
00050 for (AttributeSet::iterator it=m_attributeSet.begin();
00051 it!= m_attributeSet.end();it++) delete (*it).second;
00052 m_attributeSet.clear();
00053
00054 for (Dict::iterator itt=m_logvol2Sim.begin();
00055 itt!= m_logvol2Sim.end();itt++) delete (*itt).second;
00056 m_logvol2Sim.clear();
00057
00058 m_regionsDefs.clear();
00059 }
00060
00061
00062
00063
00064 StatusCode SimulationSvc::initialize() {
00065
00066 StatusCode status = Service::initialize();
00067 if (!status.isSuccess()) {
00068 return status;
00069 }
00070 if( m_simDbLocation.empty() || "empty" == m_simDbLocation ) {
00071 if ( 0 != getenv("XMLDDDBROOT") ) {
00072 m_simDbLocation = getenv("XMLDDDBROOT");
00073 m_simDbLocation += "/DDDB/Simulation/simulation.xml";
00074 } else {
00075 m_simDbLocation = "";
00076 }
00077 }
00078
00079 reload();
00080
00081 return StatusCode::SUCCESS;
00082 }
00083
00084
00085
00086
00087 void SimulationSvc::clear () {
00088 m_attributeSet.clear();
00089 m_logvol2Sim.clear();
00090 m_regionsDefs.clear();
00091 }
00092
00093
00094
00095
00096 void SimulationSvc::reload () {
00097 MsgStream msg (msgSvc(), "SimulationSvc");
00098
00099
00100 clear();
00101
00102
00103 IXmlSvc* xmlSvc;
00104 StatusCode status = serviceLocator()->service("XmlCnvSvc", xmlSvc, true);
00105 if (status.isFailure()) {
00106 msg << MSG::ERROR << "Unable to get XmlCnvSvc. The simulation "
00107 << "attributes will not be loaded." << endmsg;
00108 return;
00109 }
00110
00111 msg << MSG::INFO << "Loading simulation attributes file \""
00112 << m_simDbLocation << "\" ..." << endmsg;
00113
00114
00115 IOVDOMDocument* iovDoc = xmlSvc->parse(m_simDbLocation.c_str());
00116 if (!iovDoc) {
00117 msg << MSG::ERROR << "Unable to parse file " << m_simDbLocation
00118 << ". The simulation attributes will not be loaded." << endmsg;
00119 return;
00120 }
00121 xercesc::DOMDocument* document = iovDoc->getDOM();
00122 if (0 == document) {
00123 msg << MSG::ERROR << "Unable to parse file " << m_simDbLocation
00124 << ". The simulation attributes will not be loaded." << endmsg;
00125 xmlSvc->releaseDoc(iovDoc);
00126 return;
00127 }
00128
00129
00130 const XMLCh* SimStr = xercesc::XMLString::transcode("SimAtt");
00131 xercesc::DOMNodeList* domAttrList = document->getElementsByTagName(SimStr);
00132 unsigned int i;
00133 const XMLCh* nameStr = xercesc::XMLString::transcode("name");
00134 const XMLCh* mASStr = xercesc::XMLString::transcode("maxAllowedStep");
00135 const XMLCh* mTRStr = xercesc::XMLString::transcode("maxTrackLength");
00136 const XMLCh* mTStr = xercesc::XMLString::transcode("maxTime");
00137 const XMLCh* mEkStr = xercesc::XMLString::transcode("minEkine");
00138 const XMLCh* mRStr = xercesc::XMLString::transcode("minRange");
00139
00140 for (i = 0; i < domAttrList->getLength(); i++) {
00141 xercesc::DOMNode* attrNode = domAttrList->item(i);
00142 xercesc::DOMElement* attr = (xercesc::DOMElement *) attrNode;
00143
00144 std::string name = dom2Std (attr->getAttribute (nameStr));
00145 std::string mASAttribute = dom2Std (attr->getAttribute (mASStr));
00146 std::string mTRAttribute = dom2Std (attr->getAttribute (mTRStr));
00147 std::string mTAttribute = dom2Std (attr->getAttribute (mTStr));
00148 std::string mEkAttribute = dom2Std (attr->getAttribute (mEkStr));
00149 std::string mRAttribute = dom2Std (attr->getAttribute (mRStr));
00150
00151
00152 double maxAllowedStep = -1.0;
00153 double maxTrackLength = -1.0;
00154 double maxTime = -1.0;
00155 double minEkine = -1.0;
00156 double minRange = -1.0;
00157 if (!mASAttribute.empty()) {
00158 maxAllowedStep = xmlSvc->eval(mASAttribute, false);
00159 }
00160 if (!mTRAttribute.empty()) {
00161 maxTrackLength = xmlSvc->eval(mTRAttribute, true);
00162 }
00163 if (!mTAttribute.empty()) {
00164 maxTime = xmlSvc->eval(mTAttribute, true);
00165 }
00166 if (!mEkAttribute.empty()) {
00167 minEkine = xmlSvc->eval(mEkAttribute, true);
00168 }
00169 if (!mRAttribute.empty()) {
00170 minRange = xmlSvc->eval(mRAttribute, true);
00171 }
00172
00173
00174 m_attributeSet[name] =
00175 new SimAttribute
00176 (maxAllowedStep, maxTrackLength, maxTime, minEkine, minRange);
00177 }
00178 xercesc::XMLString::release((XMLCh**) &mASStr);
00179 xercesc::XMLString::release((XMLCh**) &mTRStr);
00180 xercesc::XMLString::release((XMLCh**) &mTStr);
00181 xercesc::XMLString::release((XMLCh**) &mEkStr);
00182 xercesc::XMLString::release((XMLCh**) &mRStr);
00183 xercesc::XMLString::release((XMLCh**) &SimStr);
00184
00185
00186
00187 const XMLCh* LVStr = xercesc::XMLString::transcode("LogVols");
00188 const XMLCh* ItemStr = xercesc::XMLString::transcode("Item");
00189
00190 xercesc::DOMNodeList* domLogvolsList = document->getElementsByTagName(LVStr);
00191 if (domLogvolsList->getLength() > 0) {
00192 xercesc::DOMNode* logvolsNode = domLogvolsList->item(0);
00193 xercesc::DOMElement* logvolsElement = (xercesc::DOMElement *) logvolsNode;
00194
00195 xercesc::DOMNodeList* domLogvolList = logvolsElement->getElementsByTagName(ItemStr);
00196 unsigned int i;
00197 const XMLCh* partStr = xercesc::XMLString::transcode("particle");
00198 const XMLCh* attrStr = xercesc::XMLString::transcode("attr");
00199 const XMLCh* CutStr = xercesc::XMLString::transcode("Cut");
00200
00201 for (i = 0; i < domLogvolList->getLength(); i++) {
00202 xercesc::DOMNode* logvolNode = domLogvolList->item(i);
00203 xercesc::DOMElement* logvol = (xercesc::DOMElement *) logvolNode;
00204
00205 std::string name = dom2Std (logvol->getAttribute (nameStr));
00206
00207 PartAttr* partattr = new PartAttr();
00208 m_logvol2Sim[name]=partattr;
00209
00210 xercesc::DOMNodeList* domLogvolNode = logvol->getElementsByTagName(CutStr);
00211 unsigned int j;
00212 for (j = 0; j < domLogvolNode->getLength(); j++) {
00213 xercesc::DOMNode* cutNode = domLogvolNode->item(j);
00214 xercesc::DOMElement* cut = (xercesc::DOMElement *) cutNode;
00215 std::string dompart = dom2Std (cut->getAttribute(partStr));
00216 int particle = (int) xmlSvc->eval(dompart, false);
00217 std::string attr = dom2Std (cut->getAttribute (attrStr));
00218
00219 partattr->operator[](particle) = m_attributeSet[attr];
00220 }
00221 }
00222 xercesc::XMLString::release((XMLCh**) &partStr);
00223 xercesc::XMLString::release((XMLCh**) &attrStr);
00224 xercesc::XMLString::release((XMLCh**) &CutStr);
00225 }
00226
00227 xercesc::XMLString::release((XMLCh**) &LVStr);
00228 xercesc::XMLString::release((XMLCh**) &ItemStr);
00229
00231
00232
00233
00234 std::map<std::string, Prcuts> regcut;
00235
00236
00237 const XMLCh* ProdCutStr = xercesc::XMLString::transcode("ProductionCut");
00238
00239 xercesc::DOMNodeList* domPrCutList = document->getElementsByTagName(ProdCutStr);
00240 const XMLCh* GaStr = xercesc::XMLString::transcode("gammaCut");
00241 const XMLCh* ElStr = xercesc::XMLString::transcode("electronCut");
00242 const XMLCh* PoStr = xercesc::XMLString::transcode("positronCut");
00243 const XMLCh* PrStr = xercesc::XMLString::transcode("ProtonCut");
00244 const XMLCh* APStr = xercesc::XMLString::transcode("antiProtonCut");
00245 const XMLCh* NeStr = xercesc::XMLString::transcode("neutronCut");
00246 const XMLCh* ANStr = xercesc::XMLString::transcode("antiNeutronCut");
00247
00248 unsigned int ii;
00249 for (ii = 0; ii < domPrCutList->getLength(); ii++) {
00250 xercesc::DOMNode* prcutNode = domPrCutList->item(ii);
00251 xercesc::DOMElement* prcut = (xercesc::DOMElement *) prcutNode;
00252 std::string name = dom2Std (prcut->getAttribute (nameStr));
00253 std::string atrgammacut = dom2Std (prcut->getAttribute (GaStr));
00254 std::string atrelectroncut = dom2Std (prcut->getAttribute (ElStr));
00255 std::string atrpositroncut = dom2Std (prcut->getAttribute (PoStr));
00256 std::string atrprotoncut = dom2Std (prcut->getAttribute (PrStr));
00257 std::string atraprotoncut = dom2Std (prcut->getAttribute (APStr));
00258 std::string atrneutroncut = dom2Std (prcut->getAttribute (NeStr));
00259 std::string atraneutroncut = dom2Std (prcut->getAttribute (ANStr));
00260
00261 Prcuts tempcuts;
00262
00263
00264 tempcuts.gammacut = -1.0;
00265 tempcuts.electroncut = -1.0;
00266 tempcuts.positroncut = -1.0;
00267 tempcuts.protoncut = -1.0;
00268 tempcuts.aprotoncut = -1.0;
00269 tempcuts.neutroncut = -1.0;
00270 tempcuts.aneutroncut = -1.0;
00271
00272
00273 if (!atrgammacut.empty()) {
00274 tempcuts.gammacut = xmlSvc->eval(atrgammacut, false);
00275 }
00276 if (!atrelectroncut.empty()) {
00277 tempcuts.electroncut = xmlSvc->eval(atrelectroncut, true);
00278 }
00279 if (!atrpositroncut.empty()) {
00280 tempcuts.positroncut = xmlSvc->eval(atrpositroncut, true);
00281 }
00282 if (!atrprotoncut.empty()) {
00283 tempcuts.protoncut = xmlSvc->eval(atrprotoncut, true);
00284 }
00285 if (!atraprotoncut.empty()) {
00286 tempcuts.aprotoncut = xmlSvc->eval(atraprotoncut, true);
00287 }
00288 if (!atrneutroncut.empty()) {
00289 tempcuts.neutroncut = xmlSvc->eval(atrneutroncut, true);
00290 }
00291 if (!atraneutroncut.empty()) {
00292 tempcuts.aneutroncut = xmlSvc->eval(atraneutroncut, true);
00293 }
00294
00295
00296 regcut[name] = tempcuts;
00297 }
00298 xercesc::XMLString::release((XMLCh**) &GaStr);
00299 xercesc::XMLString::release((XMLCh**) &ElStr);
00300 xercesc::XMLString::release((XMLCh**) &PoStr);
00301 xercesc::XMLString::release((XMLCh**) &PrStr);
00302 xercesc::XMLString::release((XMLCh**) &APStr);
00303 xercesc::XMLString::release((XMLCh**) &NeStr);
00304 xercesc::XMLString::release((XMLCh**) &ANStr);
00305 xercesc::XMLString::release((XMLCh**) &ProdCutStr);
00306
00307
00308
00309
00310
00311 const XMLCh* RegnsStr = xercesc::XMLString::transcode("Regions");
00312 const XMLCh* RegStr = xercesc::XMLString::transcode("Region");
00313 const XMLCh* VolStr = xercesc::XMLString::transcode("Volume");
00314 const XMLCh* ProdStr = xercesc::XMLString::transcode("prodcut");
00315
00316 xercesc::DOMNodeList* domRegionsList = document->getElementsByTagName(RegnsStr);
00317
00318 msg << MSG::DEBUG << "SimSvc Production regions list length \""
00319 << (int) domRegionsList ->getLength() << "\" ..." << endmsg;
00320
00321
00322
00323 if (domRegionsList->getLength() > 0) {
00324 xercesc::DOMNode* regionsNode = domRegionsList->item(0);
00325 xercesc::DOMElement* regionsElement = (xercesc::DOMElement*) regionsNode;
00326 xercesc::DOMNodeList* domRegionList = regionsElement->getElementsByTagName(RegStr);
00327 msg << MSG::DEBUG << "SimSvc Production region list length \""
00328 << (int) domRegionList ->getLength() << "\" ..." << endmsg;
00329
00330 unsigned int i;
00331 for (i = 0; i < domRegionList->getLength(); i++) {
00332 xercesc::DOMNode* regionNode = domRegionList->item(i);
00333 xercesc::DOMElement* region = (xercesc::DOMElement*) regionNode;
00334 std::string regname = dom2Std (region->getAttribute (nameStr));
00335 std::string prcut = dom2Std (region->getAttribute (ProdStr));
00336
00337 msg << MSG::DEBUG << "SimSvc region name prcut \""
00338 << regname<<" "<<prcut << "\" ..." << endmsg;
00339
00340 xercesc::DOMNodeList* domRegionNode = region->getElementsByTagName(VolStr);
00341 unsigned int j;
00342 std::vector<std::string> volvect;
00343
00344 for (j = 0; j < domRegionNode->getLength(); j++)
00345 {
00346 xercesc::DOMNode* volNode = domRegionNode->item(j);
00347 xercesc::DOMElement* vol = (xercesc::DOMElement*) volNode;
00348
00349 std::string volname = dom2Std (vol->getAttribute (nameStr));
00350 msg << MSG::DEBUG << "SimSvc vol name \""
00351 << volname <<" "<< "\" ..." << endmsg;
00352
00353 volvect.push_back(volname);
00354 }
00355
00356 RegionCuts rcut(regname,
00357 volvect,
00358 regcut[prcut].gammacut,
00359 regcut[prcut].electroncut,
00360 regcut[prcut].positroncut,
00361 regcut[prcut].protoncut,
00362 regcut[prcut].aprotoncut,
00363 regcut[prcut].neutroncut,
00364 regcut[prcut].aneutroncut);
00365 m_regionsDefs.push_back(rcut);
00366 }
00367 }
00368 xercesc::XMLString::release((XMLCh**) &nameStr);
00369 xercesc::XMLString::release((XMLCh**) &RegStr);
00370 xercesc::XMLString::release((XMLCh**) &VolStr);
00371 xercesc::XMLString::release((XMLCh**) &ProdStr);
00372
00373 xmlSvc->releaseDoc(iovDoc);
00374
00375 }
00376
00377
00378
00379
00380
00381 const bool SimulationSvc::hasSimAttribute (const ILVolume* vol) const {
00382 if (0 != vol) {
00383
00384 std::string bnn = vol->name();
00385 Dict::const_iterator it = m_logvol2Sim.find (bnn);
00386 if (it != m_logvol2Sim.end()) {
00387 return true;
00388 }
00389 }
00390 return false;
00391 }
00392
00393
00394
00395
00396 const bool SimulationSvc::hasSimAttribute (const std::string volname) const {
00397
00398
00399 Dict::const_iterator it = m_logvol2Sim.find (volname);
00400 if (it != m_logvol2Sim.end()) {
00401 return true;
00402 }
00403 return false;
00404 }
00405
00406
00407
00408
00409 const SimulationSvc::PartAttr*
00410 SimulationSvc::simAttribute (const ILVolume* vol) const {
00411 PartAttr* part = 0;
00412
00413 if (0 != vol) {
00414
00415 std::string bnn = vol->name();
00416 Dict::const_iterator it = m_logvol2Sim.find (bnn);
00417 if (it != m_logvol2Sim.end())
00418 {
00419 part=it->second;
00420 }
00421 else
00422 {
00423 MsgStream msg (msgSvc(), name());
00424 msg << MSG::WARNING << "No SimAttribute for "
00425 << vol->name() << endmsg;
00426 }
00427 }
00428 return part;
00429 }
00430
00431
00432
00433
00434 const SimulationSvc::PartAttr*
00435 SimulationSvc::simAttribute (std::string volname) const {
00436 PartAttr* part = 0;
00437
00438
00439 Dict::const_iterator it = m_logvol2Sim.find (volname);
00440 if (it != m_logvol2Sim.end())
00441 {
00442 part=it->second;
00443 }
00444 else
00445 {
00446 MsgStream msg (msgSvc(), name());
00447 msg << MSG::WARNING << "No SimAttribute for "
00448 << volname << endmsg;
00449 }
00450 return part;
00451 }
00452
00453
00454
00455
00456
00457 const std::vector<RegionCuts>* SimulationSvc::regionsDefs () const
00458 {
00459 return &m_regionsDefs;
00460 }
00461
00462
00463
00464
00465
00466 StatusCode
00467 SimulationSvc::queryInterface(const InterfaceID& riid, void** ppvInterface) {
00468 if (IID_ISimulationSvc.versionMatch(riid)) {
00469 *ppvInterface = (ISimulationSvc*)this;
00470 } else {
00471
00472 return Service::queryInterface(riid, ppvInterface);
00473 }
00474 addRef();
00475 return StatusCode::SUCCESS;
00476 }