ATLAS Offline Software
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 
7 #include "MuonAGDDToolHelper.h"
10 #include "AGDDKernel/AliasStore.h"
11 
12 
13 #include <fstream>
14 
15 MuonAGDDTool::MuonAGDDTool(const std::string& type, const std::string& name, const IInterface* parent) :
17  m_structuresFromFlags(),
18  m_outPREsqlName("")
19 {}
20 
22 {
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
45 StatusCode 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 }
AGDDController.h
MuonAGDDTool::m_outPREsqlName
std::string m_outPREsqlName
Definition: MuonAGDDTool.h:29
AGDDToolBase
Definition: AGDDToolBase.h:17
AGDDToolBase::m_outFileName
Gaudi::Property< std::string > m_outFileName
Definition: AGDDToolBase.h:40
MuonAGDDTool::WritePREsqlFile
bool WritePREsqlFile() const
Definition: MuonAGDDTool.cxx:133
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
CxxUtils::LockedPointer
A pointer together with a movable lock.
Definition: LockedPointer.h:35
initialize
void initialize()
Definition: run_EoverP.cxx:894
ATLAS_NOT_THREAD_SAFE
StatusCode MuonAGDDTool::initialize ATLAS_NOT_THREAD_SAFE()
Install fatal handler with default options.
Definition: MuonAGDDTool.cxx:21
skel.it
it
Definition: skel.GENtoEVGEN.py:423
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
AliasStore.h
MuonAGDDToolHelper::GetAGDD
std::string GetAGDD(const bool dumpIt, const std::string &tableName, const std::string &outFileName)
Definition: MuonAGDDToolHelper.cxx:93
MuonAGDDToolHelper.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
MuonAGDDTool.h
MuonAGDDToolHelper::SetNSWComponents
void SetNSWComponents()
Definition: MuonAGDDToolHelper.cxx:146
MuonAGDDTool::m_amdcName
Gaudi::Property< std::string > m_amdcName
Definition: MuonAGDDTool.h:26
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
test_pyathena.parent
parent
Definition: test_pyathena.py:15
AGDD2GeoModelBuilder
Definition: AGDD2GeoModelBuilder.h:42
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
MuonAGDDTool::MuonAGDDTool
MuonAGDDTool(const std::string &type, const std::string &name, const IInterface *parent)
Definition: MuonAGDDTool.cxx:15
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
MuonAGDDToolHelper
Definition: MuonAGDDToolHelper.h:20
AGDD2GeoModelBuilder.h
MuonAGDDToolHelper::setAGDDtoGeoSvcName
void setAGDDtoGeoSvcName(const std::string &name)
Definition: MuonAGDDToolHelper.cxx:166
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
MuonAGDDToolHelper::ReadAGDDFlags
std::vector< std::string > ReadAGDDFlags()
Definition: MuonAGDDToolHelper.cxx:64
PrepareReferenceFile.outfile
outfile
Definition: PrepareReferenceFile.py:42