ATLAS Offline Software
Loading...
Searching...
No Matches
StreamToFileTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7#include <fstream>
8#include <iomanip>
9
10namespace JiveXML {
11
17 StreamToFileTool::StreamToFileTool( const std::string& type , const std::string& name, const IInterface* p):
18 AthAlgTool(type,name,p){
19
20 //Provide interface
21 declareInterface<IStreamTool>(this);
22
24 declareProperty("FileNamePrefix", m_FileNamePrefix = "JiveXML");
26 declareProperty("FileNameSuffix", m_FileNameSuffix = ".xml");
27
28 }
29
34 if(m_isOnline){
35 if(m_onlineEDsvc.retrieve().isFailure()){
36 ATH_MSG_ERROR("Could not locate the online event displays service");
37 return StatusCode::FAILURE;
38 }
39 }
40 return StatusCode::SUCCESS;
41 }
42
47 return StatusCode::SUCCESS;
48 }
49
56 StatusCode StreamToFileTool::StreamEvent( const unsigned long EventNumber, const unsigned int RunNumber, const std::ostringstream* EventBuffer ) {
57 if(m_isOnline){
58 m_FileNamePrefix = m_onlineEDsvc->getFileNamePrefix();
59 ATH_MSG_DEBUG("m_FileNamePrefix: " << m_FileNamePrefix << " EventNumber: " << EventNumber);
60 }
62 std::ofstream* outFile;
63 StatusCode sc = NewFile(EventNumber,RunNumber,outFile);
64 if (sc.isFailure()){
65 ATH_MSG_WARNING("Could not open file for event " << EventNumber << " from run " << RunNumber);
66 return sc;
67 }
69 (*outFile) << EventBuffer->str();
70 outFile->flush();
72 if (!outFile->good()){
73 ATH_MSG_WARNING("Could not open file for event " << EventNumber << " from run " << RunNumber);
74 return StatusCode::FAILURE;
75 }
76
78 sc = CloseFile(outFile);
79 return sc;
80
81 }
82
88 std::string StreamToFileTool::MakeFileName(const unsigned long EventNumber, const unsigned int RunNumber) const {
89
90 //Generate a the return string with file prefix
91 std::ostringstream name;
92
93 //Assemble file name
94 name << m_FileNamePrefix << std::setfill('0');
95 name << "_" << std::setw(5) << RunNumber;
96 name << "_" << std::setw(5) << EventNumber;
97 name << m_FileNameSuffix;
98 return name.str();
99 }
100
104 StatusCode StreamToFileTool::NewFile( const unsigned long EventNumber, const unsigned int RunNumber, std::ofstream *& outputFile) const {
105
106 // Generate the file name
107 std::string filename = MakeFileName(EventNumber,RunNumber);
108
109 // create a new output stream
110 outputFile = new std::ofstream(filename.c_str());
111 ATH_MSG_DEBUG("outputFile "<< filename);
112 // check if it worked
113 if ( !(outputFile->good()) ){
114 ATH_MSG_WARNING("Unable to create output file with name " << filename);
115 return StatusCode::FAILURE;
116 }
117
118 return StatusCode::SUCCESS;
119 }
120
124 StatusCode StreamToFileTool::CloseFile( std::ofstream *& outputFile ) const {
125
126 //Try to close the file
127 outputFile->close();
128
129 //See if it worked
130 if (!outputFile->good()){
131 ATH_MSG_WARNING("Unable to close file");
132 }
133
134 //In any case delete object
135 delete outputFile;
136
137 return StatusCode::SUCCESS;
138 }
139
140} //namespace
#define ATH_MSG_ERROR(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
static Double_t sc
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
std::string MakeFileName(const unsigned long EventNumber, const unsigned int RunNumber) const
Generate a file name.
virtual StatusCode finalize()
Finalize.
StreamToFileTool(const std::string &, const std::string &, const IInterface *)
Constructor.
virtual StatusCode StreamEvent(const unsigned long EventNumber, const unsigned int RunNumber, const std::ostringstream *EventBuffer)
Stream one event.
StatusCode CloseFile(std::ofstream *&outFile) const
Closes output stream.
Gaudi::Property< bool > m_isOnline
ServiceHandle< IOnlineEventDisplaysSvc > m_onlineEDsvc
StatusCode NewFile(const unsigned long EventNumber, const unsigned int RunNumber, std::ofstream *&outFile) const
Creates a new output stream to write XML to.
virtual StatusCode initialize()
Initialize.
std::string m_FileNamePrefix
Prefix put in front of JiveXML file name.
std::string m_FileNameSuffix
Suffix put at the end of the file name (including type)
This header is shared inbetween the C-style server thread and the C++ Athena ServerSvc.