ATLAS Offline Software
Loading...
Searching...
No Matches
MuonAGDDTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "MuonAGDDTool.h"
6
11
12
13#include <fstream>
14
15MuonAGDDTool::MuonAGDDTool(const std::string& type, const std::string& name, const IInterface* parent) :
16 AGDDToolBase(type,name,parent),
19{}
20
21StatusCode MuonAGDDTool::initialize ATLAS_NOT_THREAD_SAFE ()
22{
23 ATH_CHECK(AGDDToolBase::initialize());
24 ATH_MSG_INFO("MuonAGDDTool::initialize");
25 m_outFileName = "Out.AmdcOracle.AM." + m_outFileType + "temp.data";
26 m_outPREsqlName = "Out.AmdcOracle.AM." + m_outFileType + ".PREsql";
27
28 if (m_DBFileName.empty()) {
29 m_DBFileName = "Generated_" + m_outFileType + "_pool.txt";
30 }
31
32 if (m_buildNSW)
33 {
34 MuonAGDDToolHelper theHelper;
35 theHelper.setAGDDtoGeoSvcName(m_agdd2GeoSvcName);
36 theHelper.SetNSWComponents();
37 }
38
39 ATH_CHECK(construct());
40 return StatusCode::SUCCESS;
41}
42
43// Base class method is also marked not thread-safe.
44// Uses unsafe function UseGeoModelDetector
45StatusCode MuonAGDDTool::construct ATLAS_NOT_THREAD_SAFE ()
46{
47 ATH_MSG_INFO(name()<<"::construct()");
48 MuonAGDDToolHelper theHelper;
49 theHelper.setAGDDtoGeoSvcName(m_agdd2GeoSvcName);
50
51 IAGDDtoGeoSvc::LockedController controller = m_svc->getController();
52 controller->UseGeoModelDetector("Muon");
53
54 if (!m_locked)
55 {
56 ATH_MSG_INFO(" Reading AGDD2GeoSwitches flags ");
57 m_structuresFromFlags=theHelper.ReadAGDDFlags();
58 for (const auto &structure: m_structuresFromFlags) {
59 ATH_MSG_INFO(" ----> "<<structure);
60 }
61 }
62
63 // reading from a local AGDD xml file
64 if (!m_readAGDD) {
65 ATH_MSG_INFO(" Parsing local xml file ");
66 controller->ParseFiles();
67 // reading the AGDD xml blob from the ATLAS geometry database
68 } else {
69 ATH_MSG_INFO(" now reading AGDD blob ");
70
71 std::string AGDDfile=theHelper.GetAGDD(m_dumpAGDD, m_outFileType, m_DBFileName);
72 if( AGDDfile.empty() ) {
73 ATH_MSG_ERROR("\t-- empty AGDDfile - this cannot be correct " );
74 return StatusCode::FAILURE;
75 }
76 controller->ParseString(AGDDfile);
77 }
78
79 if (m_printSections) {
80 ATH_MSG_INFO("\t Printing all sections");
81 controller->PrintSections();
82 }
83
84 // when reading from a local AGDD xml file and not creating a layout (i.e. running simulation from a local xml file),
85 // only build those volumes that are specified at the 'Volumes' property (analogously to the AGDD2GeoSwitches when reading the blob)
86 if (!m_readAGDD && !m_writeDBfile) {
87 for (const auto &vol:m_volumesToBuild) {
88 controller->GetBuilder()->BuildFromVolume(vol);
89 }
90 } else {
91 // when reading the AGDD xml blob, only build the volumes specified via the AGDD2GeoSwitches
92 for (const auto &structure: m_structuresFromFlags) {
93 if (!m_buildNSW && structure=="NewSmallWheel") continue;
94 controller->GetBuilder()->BuildFromVolume(structure);
95 }
96 }
97
98 if(m_writeDBfile)
99 {
100 // build model before writing blob - if Athena crashes the XML is not good and should not become a blob
101 ((AGDD2GeoModelBuilder*)controller->GetBuilder())->BuildAllVolumes();
102 ATH_MSG_INFO("\t-- attempting to write output to "<< m_outFileName );
103 if( !m_outFileName.empty() )
104 {
105 if(!controller->WriteAGDDtoDBFile( m_outFileName ))
106 {
107 ATH_MSG_ERROR("\t-- something went wrong during writing AGDD file - crashing" );
108 return StatusCode::FAILURE;
109 }
110 else {
111 ATH_MSG_INFO("\t-- AGDD successfully dumped to "<< m_outFileName);
112 }
113 if( !WritePREsqlFile() )
114 {
115 ATH_MSG_ERROR("\t-- something went wrong during writing PREsql file - crashing" );
116 return StatusCode::FAILURE;
117 }
118 else {
119 ATH_MSG_INFO("\t-- AGDD successfully wrote PREsql file "<< m_outPREsqlName);
120 }
121 }
122 else {
123 ATH_MSG_ERROR("\t-- no output file name provided - crashing " );
124 return StatusCode::FAILURE;
125 }
126 }
127
128 controller->Clean();
129
130 return StatusCode::SUCCESS;
131}
132
134{
135
136 std::ifstream outfile(m_outFileName.value().c_str(), std::ifstream::in | std::ifstream::binary);
137
138 std::vector<std::string> newoutfilelines;
139 std::string outfileline;
140 while( getline(outfile, outfileline) )
141 if( outfileline != "\n" && outfileline != "\r" && !outfileline.empty() )
142 {
143 const auto strBegin = outfileline.find_first_not_of(" \t");
144 const auto strEnd = outfileline.find_last_not_of(" \t");
145 const auto strRange = strEnd - strBegin + 1;
146 if (strBegin != std::string::npos) outfileline = outfileline.substr(strBegin, strRange);
147 newoutfilelines.push_back(outfileline);
148 }
149 outfile.close();
150
151 std::ofstream newoutfile(m_outFileName.value().c_str(), std::ofstream::out | std::ofstream::trunc);
152 for(auto it = newoutfilelines.begin(); it != newoutfilelines.end(); ++it)
153 {
154 if(it != newoutfilelines.begin()) newoutfile << "\n";
155 newoutfile << *it;
156 }
157 newoutfile.close();
158 outfile.open(m_outFileName.value().c_str(), std::ifstream::in | std::ifstream::binary);
159
160 int fileSize = 0;
161 if(outfile.is_open())
162 {
163 outfile.seekg(0, std::ios::end);
164 fileSize = outfile.tellg();
165 outfile.close();
166 }
167 else {
168 ATH_MSG_ERROR("\t-- cannot get size of file " << m_outFileName );
169 return false;
170 }
171
172 std::string TheAmdcName = m_amdcName;
173
174 std::ofstream prefile;
175 prefile.open (m_outPREsqlName.c_str());
176 prefile << "insert into AGDD_data (\n";
177 prefile << "AGDD_data_id,\n";
178 prefile << "VERS,\n";
179 prefile << "VNAME,\n";
180 prefile << "LENAGDD,\n";
181 prefile << "NLINE,\n";
182 prefile << "data\n";
183 prefile << ") values (XXX_DATA_ID_KOUNTER,\n";
184 // see constructor AmdcDbSvcMakerFromAmdc::AmdcDbSvcMakerFromAmdc
185 prefile << " 7,'";
186 // see method AmdcDbSvcMakerFromAmdc::AGDD (no idea why "-1", but it's needed to be consistent)
187 prefile << TheAmdcName.substr(0,4) <<"'," << fileSize-1 << ","<< int( (fileSize + 2) / 4 )<<",\n";
188 prefile << "empty_clob()\n";
189 prefile << ");\n";
190 prefile << "insert into AGDD_data2tag values (XXX_DATA2TAG_KOUNTER,XXX_DATA_ID_KOUNTER);\n";
191 prefile << "DECLARE\n";
192 prefile << " lobloc CLOB;\n";
193 prefile << " req utl_http.req;\n";
194 prefile << " resp utl_http.resp;\n";
195 prefile << " text VARCHAR2(32767);\n";
196 prefile << " amount INTEGER(10) := 0;\n";
197 prefile << " offset INTEGER(10) := 0;\n";
198 prefile << " TRUE BOOLEAN;\n";
199 prefile << "BEGIN\n";
200 prefile << " SELECT data INTO lobloc\n";
201 prefile << " FROM AGDD_data\n";
202 prefile << " WHERE AGDD_data_id = XXX_DATA_ID_KOUNTER FOR UPDATE;\n";
203 prefile << " offset := DBMS_LOB.GETLENGTH(lobloc)+2;\n";
204 prefile << " req := utl_http.begin_request(\n";
205 prefile << " 'WEB_ADDRESS_FOR_TEMP_DATA_FILEAGDDtemp.data');\n";
206 prefile << " resp := utl_http.get_response(req);\n";
207 prefile << " LOOP\n";
208 prefile << " text := ' ';\n";
209 prefile << " UTL_HTTP.READ_TEXT(resp, text, NULL);\n";
210 prefile << " /* DBMS_OUTPUT.PUT_LINE(text); */\n";
211 prefile << " amount := length(text);\n";
212 prefile << " DBMS_LOB.WRITEAPPEND(lobloc,amount,text);\n";
213 prefile << " END LOOP;\n";
214 prefile << " utl_http.end_response(resp);\n";
215 prefile << " EXCEPTION\n";
216 prefile << " WHEN utl_http.end_of_body\n";
217 prefile << " THEN utl_http.end_response(resp);\n";
218 prefile << "END;\n";
219 prefile << "/\n";
220 prefile.close();
221
222 return true;
223
224}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
StatusCode MuonAGDDTool::initialize ATLAS_NOT_THREAD_SAFE()
Install fatal handler with default options.
virtual void BuildFromVolume(const std::string &)=0
bool WriteAGDDtoDBFile(const std::string &)
void PrintSections() const
void ParseString(const std::string &)
AGDDBuilder * GetBuilder()
Gaudi::Property< std::string > m_outFileName
AGDDToolBase(const std::string &type, const std::string &name, const IInterface *parent)
CxxUtils::LockedPointer< AGDDController > LockedController
std::string GetAGDD(const bool dumpIt, const std::string &tableName, const std::string &outFileName)
std::vector< std::string > ReadAGDDFlags()
void setAGDDtoGeoSvcName(const std::string &name)
Gaudi::Property< std::string > m_amdcName
MuonAGDDTool(const std::string &type, const std::string &name, const IInterface *parent)
std::vector< std::string > m_structuresFromFlags
bool WritePREsqlFile() const
std::string m_outPREsqlName