ATLAS Offline Software
TBXMLWriter.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 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/TypeNameString.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 {
42  // loop all writers
43  for (const std::string& toolName : m_writerToolNames)
44  {
45  IAlgTool* algToolPtr;
46  Gaudi::Utils::TypeNameString writerAlgoTool(toolName);
47 
48  // pick up tool
49  StatusCode checkOut = toolSvc()->retrieveTool(writerAlgoTool.type(),
50  writerAlgoTool.name(),
51  algToolPtr,
52  this);
53  // not found
54  if ( checkOut.isFailure() )
55  {
57  ( "failed to pick up tool of type \042"
58  << writerAlgoTool.type()
59  << "\042 with name <"
60  << writerAlgoTool.name()
61  << ">"
62  );
63  return StatusCode::FAILURE;
64  }
65 
66  // store pointer to tool if right type
67  TBXMLWriterToolBase* thisTool =
68  dynamic_cast< TBXMLWriterToolBase* >(algToolPtr);
69  if ( thisTool != 0 )
70  {
71  m_writerTools.push_back(thisTool);
72  }
73 
74  }
75 
76  // check tools
77  if ( m_writerTools.size() == 0 )
78  {
79  ATH_MSG_ERROR ( "no tools found!" );
80  return StatusCode::FAILURE;
81  }
82 
84  {
85  // reset statistics
86  m_toolInvoke[tool->name()] = 0;
87  m_toolReject[tool->name()] = 0;
88  m_toolAccept[tool->name()] = 0;
89  }
90 
91  return StatusCode::SUCCESS;
92 }
93 
95 // Execute //
97 
99 {
100  const EventContext& ctx = Gaudi::Hive::currentContext();
101 
103  // Check Tools //
105 
107  // Output Frequency Control //
109 
110  m_eventCounter++;
111  if ( m_eventCounter > 1 && m_eventCounter % m_outputFrequency != 0 )
112  {
113  return StatusCode::SUCCESS;
114  }
115 
117  // XML Files //
119 
120  // run info
121  // if ( m_geomWriter > 0 && thisRun != m_oldRun )
122  // {
123  // m_oldRun = thisRun;
124  // this->writeRunInfo();
125  // }
126 
127  // construct directory and file name
128  std::ostringstream thisFileName;
129  thisFileName << m_topDirectory << "/evnt."
130  << std::setw(6) << std::setfill('0')
131  << ctx.eventID().run_number() << "." << std::setfill('0')
132  << std::setw(6)
133  << ctx.eventID().event_number() << ".xml" << std::ends;
134 
135  std::ofstream thisFileStream((thisFileName.str()).c_str());
136 
138  // Loop Tools //
140 
142  {
143  StatusCode checkOut = tool->writeOut(thisFileStream);
144  m_toolInvoke[tool->name()]++;
145  if ( checkOut.isFailure() )
146  {
147  m_toolReject[tool->name()]++;
148  }
149  else
150  {
151  m_toolAccept[tool->name()]++;
152  }
153  }
154 
155  // finalize the stream
156  ATH_CHECK((*(m_writerTools.begin()))->finalize(thisFileStream));
157  thisFileStream.close();
158 
159  return StatusCode::SUCCESS;
160 }
161 
162 
164 {
165  return StatusCode::SUCCESS;
166 }
167 
168 
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:98
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:228
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:163
TBXMLWriter::m_eventCounter
unsigned int m_eventCounter
Definition: TBXMLWriter.h:54
TBXMLWriter::m_outputFrequency
unsigned int m_outputFrequency
Definition: TBXMLWriter.h:53