ATLAS Offline Software
TBXMLWriter.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
6 #include "TBXMLWriterToolBase.h"
7 #include "TBXMLWriter.h"
8 
9 
10 // event
11 #include "GaudiKernel/ListItem.h"
12 
13 #include <string>
14 #include <vector>
15 #include <map>
16 #include <iostream>
17 #include <fstream>
18 #include <sstream>
19 #include <ios>
20 #include <iomanip>
21 
22 TBXMLWriter::TBXMLWriter(const std::string& name,
23  ISvcLocator* pSvcLocator)
24  : AthAlgorithm(name,pSvcLocator)
25  , m_outputFrequency(1)
26  , m_eventCounter(0)
27 {
28  declareProperty("OutputFrequency",m_outputFrequency);
29  declareProperty("WriteTools",m_writerToolNames);
30  declareProperty("FileTopDirectory",m_topDirectory);
31 }
32 
34 { }
35 
37 // Initialize //
39 
41 {
43  // Services //
45 
46  // tool service
47  IToolSvc* toolSvcPtr = 0;
48  ATH_CHECK( service("ToolSvc",toolSvcPtr) );
49 
51  // Current Job Options //
53 
55  // Algorithm Tools //
57 
58  // loop all writers
59 
60  for (const std::string& toolName : m_writerToolNames)
61  {
62  IAlgTool* algToolPtr;
63  ListItem writerAlgoTool(toolName);
64 
65  // pick up tool
66  StatusCode checkOut = toolSvcPtr->retrieveTool(writerAlgoTool.type(),
67  writerAlgoTool.name(),
68  algToolPtr,
69  this);
70  // not found
71  if ( checkOut.isFailure() )
72  {
74  ( "failed to pick up tool of type \042"
75  << writerAlgoTool.type()
76  << "\042 with name <"
77  << writerAlgoTool.name()
78  << ">"
79  );
80  return StatusCode::FAILURE;
81  }
82 
83  // store pointer to tool if right type
84  TBXMLWriterToolBase* thisTool =
85  dynamic_cast< TBXMLWriterToolBase* >(algToolPtr);
86  if ( thisTool != 0 )
87  {
88  m_writerTools.push_back(thisTool);
89  }
90 
91  }
92 
93  // check tools
94  if ( m_writerTools.size() == 0 )
95  {
96  ATH_MSG_ERROR ( "no tools found!" );
97  return StatusCode::FAILURE;
98  }
99 
101  {
102  // reset statistics
103  m_toolInvoke[tool->name()] = 0;
104  m_toolReject[tool->name()] = 0;
105  m_toolAccept[tool->name()] = 0;
106  }
107 
108  return StatusCode::SUCCESS;
109 }
110 
112 // Execute //
114 
116 {
117  const EventContext& ctx = Gaudi::Hive::currentContext();
118 
120  // Check Tools //
122 
124  // Output Frequency Control //
126 
127  m_eventCounter++;
128  if ( m_eventCounter > 1 && m_eventCounter % m_outputFrequency != 0 )
129  {
130  return StatusCode::SUCCESS;
131  }
132 
134  // XML Files //
136 
137  // run info
138  // if ( m_geomWriter > 0 && thisRun != m_oldRun )
139  // {
140  // m_oldRun = thisRun;
141  // this->writeRunInfo();
142  // }
143 
144  // construct directory and file name
145  std::ostringstream thisFileName;
146  thisFileName << m_topDirectory << "/evnt."
147  << std::setw(6) << std::setfill('0')
148  << ctx.eventID().run_number() << "." << std::setfill('0')
149  << std::setw(6)
150  << ctx.eventID().event_number() << ".xml" << std::ends;
151 
152  std::ofstream thisFileStream((thisFileName.str()).c_str());
153 
155  // Loop Tools //
157 
159  {
160  StatusCode checkOut = tool->writeOut(thisFileStream);
161  m_toolInvoke[tool->name()]++;
162  if ( checkOut.isFailure() )
163  {
164  m_toolReject[tool->name()]++;
165  }
166  else
167  {
168  m_toolAccept[tool->name()]++;
169  }
170  }
171 
172  // finalize the stream
173  ATH_CHECK((*(m_writerTools.begin()))->finalize(thisFileStream));
174  thisFileStream.close();
175 
176  return StatusCode::SUCCESS;
177 }
178 
179 
181 {
182  return StatusCode::SUCCESS;
183 }
184 
185 
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
TBXMLWriter::m_toolReject
std::map< std::string, unsigned int > m_toolReject
Definition: TBXMLWriter.h:67
TBXMLWriter::m_writerTools
std::vector< TBXMLWriterToolBase * > m_writerTools
Definition: TBXMLWriter.h:60
TBXMLWriter::m_toolAccept
std::map< std::string, unsigned int > m_toolAccept
Definition: TBXMLWriter.h:66
TBXMLWriterToolBase.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
TBXMLWriter::m_topDirectory
std::string m_topDirectory
Definition: TBXMLWriter.h:58
TBXMLWriter::execute
virtual StatusCode execute() override
Definition: TBXMLWriter.cxx:115
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
TBXMLWriter::TBXMLWriter
TBXMLWriter(const std::string &name, ISvcLocator *pSvcLocator)
Algorithm constructor.
Definition: TBXMLWriter.cxx:22
AthAlgorithm
Definition: AthAlgorithm.h:47
TBXMLWriter::m_writerToolNames
std::vector< std::string > m_writerToolNames
Definition: TBXMLWriter.h:56
TBXMLWriter::~TBXMLWriter
virtual ~TBXMLWriter()
Definition: TBXMLWriter.cxx:33
TBXMLWriter::initialize
virtual StatusCode initialize() override
Definition: TBXMLWriter.cxx:40
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
TBXMLWriter.h
AtlCoolConsole.tool
tool
Definition: AtlCoolConsole.py:453
TBXMLWriter::m_toolInvoke
std::map< std::string, unsigned int > m_toolInvoke
Definition: TBXMLWriter.h:68
TBXMLWriterToolBase
base class for XML writer tools for 2004 event display
Definition: TBXMLWriterToolBase.h:17
TBXMLWriter::finalize
virtual StatusCode finalize() override
Definition: TBXMLWriter.cxx:180
TBXMLWriter::m_eventCounter
unsigned int m_eventCounter
Definition: TBXMLWriter.h:54
TBXMLWriter::m_outputFrequency
unsigned int m_outputFrequency
Definition: TBXMLWriter.h:53