| The Gaudi Framework v32r1 (f65d50dc)

SequentialOutputStream.cpp
Go to the documentation of this file.
1 // standard headers
2 #include <limits>
3
4 // boost
5 #include <boost/filesystem.hpp>
6
7 // Framework include files
8 #include "GaudiKernel/DataObject.h"
9 #include "GaudiKernel/DataStoreItem.h"
10 #include "GaudiKernel/IDataManagerSvc.h"
11 #include "GaudiKernel/IDataProviderSvc.h"
12 #include "GaudiKernel/IOpaqueAddress.h"
13 #include "GaudiKernel/IRegistry.h"
14 #include "GaudiKernel/MsgStream.h"
15 #include "SequentialOutputStream.h"
16
17 // Define the algorithm factory for the standard output data writer
18 DECLARE_COMPONENT( SequentialOutputStream )
19
20 using namespace std;
21 namespace bf = boost::filesystem;
22
23 //=============================================================================
24 StatusCode SequentialOutputStream::writeObjects() {
25 try {
26 makeFilename();
27 } catch ( const GaudiException& except ) {
28 error() << except.message() << endmsg;
29 return StatusCode::FAILURE;
30 }
31 return OutputStream::writeObjects();
32 }
33
34 //=============================================================================
35 StatusCode SequentialOutputStream::execute() {
36 // Clear any previously existing item list
37 clearSelection();
38 // Test whether this event should be output
39 if ( isEventAccepted() ) {
40 StatusCode sc = writeObjects();
41 clearSelection();
42 ++m_events;
43 return sc;
44 }
45 return StatusCode::SUCCESS;
46 }
47
48 //=============================================================================
49 void SequentialOutputStream::makeFilename() {
50 if ( m_events % m_eventsPerFile != 0 ) return;
51
52 bf::path outputPath( m_outputName );
53 string filename = outputPath.filename().string();
54 bf::path dir = outputPath.parent_path();
55 string stem = outputPath.stem().string();
56 string extension = outputPath.extension().string();
57
58 if ( !dir.empty() ) {
59 if ( !bf::exists( dir ) ) {
60 stringstream stream;
61 stream << "Directory " << dir << " does not exist.";
62 throw GaudiException( stream.str(), "error", StatusCode::FAILURE );
63 }
64 }
65
66 if ( m_numericFilename ) {
67 if ( m_events == 0 ) {
68 try {
69 m_iFile = std::stoul( stem );
70 } catch ( const std::invalid_argument& /* cast */ ) {
71 string msg = "Filename " + filename + " is not a number, which was needed.";
72 throw GaudiException( msg, "error", StatusCode::FAILURE );
73 }
74 }
75 string iFile = std::to_string( m_iFile );
76 unsigned int length = 0;
77
78 if ( stem.length() > iFile.length() ) { length = stem.length() - iFile.length(); }
79
80 stringstream name;
81 if ( !dir.empty() ) name << dir << "/";
82 for ( unsigned int i = 0; i < length; ++i ) { name << "0"; }
83 name << iFile << extension;
84 m_outputName = name.str();
85 } else {
86 if ( m_iFile != 1 ) {
87 size_t pos = stem.rfind( "_" );
88 stem = stem.substr( 0, pos );
89 }
90
91 string iFile = std::to_string( m_iFile );
92
93 unsigned int length = 0;
94 if ( m_nNumbersAdded > iFile.length() ) { length = m_nNumbersAdded - iFile.length(); }
95
96 stringstream name;
97 name << dir << "/" << stem;
98 for ( unsigned int i = 0; i < length; ++i ) {
99 if ( i == 0 ) name << "_";
100 name << "0";
101 }
102 name << iFile << extension;
103 m_outputName = name.str();
104 }
105 ++m_iFile;
106 }
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:21
SequentialOutputStream::makeFilename
void makeFilename()
Definition: SequentialOutputStream.cpp:49
GaudiPython.HistoUtils.path
path
Definition: HistoUtils.py:932
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
SequentialOutputStream::writeObjects
StatusCode writeObjects() override
OutputStream override: Select the different objects and write them to file.
Definition: SequentialOutputStream.cpp:24
MsgStream.h
std::string::rfind
T rfind(T... args)
std::to_string
T to_string(T... args)
GaudiException::message
virtual const std::string & message() const
error message to be printed
Definition: GaudiException.h:58
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
std
STL namespace.
graphanalysis.filename
string filename
Definition: graphanalysis.py:119
SequentialOutputStream.h
std::stringstream
STL class.
std::stoul
T stoul(T... args)
std::string::string
T string(T... args)
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
OutputStream::writeObjects
virtual StatusCode writeObjects()
Select the different objects and write them to file.
Definition: OutputStream.cpp:155
DataStoreItem.h
IDataManagerSvc.h
IDataProviderSvc.h
SequentialOutputStream::execute
StatusCode execute() override
Working entry point.
Definition: SequentialOutputStream.cpp:35
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
std::stringstream::str
T str(T... args)
IRegistry.h
IOpaqueAddress.h
std::string::length
T length(T... args)
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:8
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
SequentialOutputStream
Extension of OutputStream to write run records after last event.
Definition: SequentialOutputStream.h:14
std::string::substr
T substr(T... args)
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
std::invalid_argument
STL class.
DataObject.h