| The Gaudi Framework v30r0 (c919700c)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 {
26 try {
27 makeFilename();
28 } catch ( const GaudiException& except ) {
29 error() << except.message() << endmsg;
30 return StatusCode::FAILURE;
31 }
32 return OutputStream::writeObjects();
33 }
34
35 //=============================================================================
36 StatusCode SequentialOutputStream::execute()
37 {
38 // Clear any previously existing item list
39 clearSelection();
40 // Test whether this event should be output
41 if ( isEventAccepted() ) {
42 StatusCode sc = writeObjects();
43 clearSelection();
44 ++m_events;
45 return sc;
46 }
47 return StatusCode::SUCCESS;
48 }
49
50 //=============================================================================
51 void SequentialOutputStream::makeFilename()
52 {
53 if ( m_events % m_eventsPerFile != 0 ) return;
54
55 bf::path outputPath( m_outputName );
56 string filename = outputPath.filename().string();
57 bf::path dir = outputPath.parent_path();
58 string stem = outputPath.stem().string();
59 string extension = outputPath.extension().string();
60
61 if ( !dir.empty() ) {
62 if ( !bf::exists( dir ) ) {
63 stringstream stream;
64 stream << "Directory " << dir << " does not exist.";
65 throw GaudiException( stream.str(), "error", StatusCode::FAILURE );
66 }
67 }
68
69 if ( m_numericFilename ) {
70 if ( m_events == 0 ) {
71 try {
72 m_iFile = std::stoul( stem );
73 } catch ( const std::invalid_argument& /* cast */ ) {
74 string msg = "Filename " + filename + " is not a number, which was needed.";
75 throw GaudiException( msg, "error", StatusCode::FAILURE );
76 }
77 }
78 string iFile = std::to_string( m_iFile );
79 unsigned int length = 0;
80
81 if ( stem.length() > iFile.length() ) {
82 length = stem.length() - iFile.length();
83 }
84
85 stringstream name;
86 if ( !dir.empty() ) name << dir << "/";
87 for ( unsigned int i = 0; i < length; ++i ) {
88 name << "0";
89 }
90 name << iFile << extension;
91 m_outputName = name.str();
92 } else {
93 if ( m_iFile != 1 ) {
94 size_t pos = stem.rfind( "_" );
95 stem = stem.substr( 0, pos );
96 }
97
98 string iFile = std::to_string( m_iFile );
99
100 unsigned int length = 0;
101 if ( m_nNumbersAdded > iFile.length() ) {
102 length = m_nNumbersAdded - iFile.length();
103 }
104
105 stringstream name;
106 name << dir << "/" << stem;
107 for ( unsigned int i = 0; i < length; ++i ) {
108 if ( i == 0 ) name << "_";
109 name << "0";
110 }
111 name << iFile << extension;
112 m_outputName = name.str();
113 }
114 ++m_iFile;
115 }
OutputStream::isEventAccepted
bool isEventAccepted() const
Test whether this event should be output.
Definition: OutputStream.cpp:533
GaudiException::message
virtual const std::string & message() const
error message to be printed
Definition: GaudiException.h:99
StatusCode::SUCCESS
Definition: StatusCode.h:29
CommonMessagingBase::msg
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:121
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:22
SequentialOutputStream::makeFilename
void makeFilename()
Definition: SequentialOutputStream.cpp:51
SequentialOutputStream::m_events
unsigned int m_events
Definition: SequentialOutputStream.h:35
Algorithm::name
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:737
boost
The namespace threadpool contains a thread pool and related utility classes.
Definition: iter_pos.hpp:13
GaudiPython.HistoUtils.path
path
Definition: HistoUtils.py:895
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)
OutputStream::m_outputName
Gaudi::Property< std::string > m_outputName
Definition: OutputStream.h:52
std::to_string
T to_string(T...args)
std
STL namespace.
SequentialOutputStream::m_numericFilename
Gaudi::Property< bool > m_numericFilename
Definition: SequentialOutputStream.h:31
graphanalysis.filename
string filename
Definition: graphanalysis.py:117
SequentialOutputStream.h
std::stringstream
STL class.
std::stoul
T stoul(T...args)
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:33
std::string::string
T string(T...args)
OutputStream::writeObjects
virtual StatusCode writeObjects()
Select the different objects and write them to file.
Definition: OutputStream.cpp:162
DataStoreItem.h
IDataManagerSvc.h
IDataProviderSvc.h
SequentialOutputStream::execute
StatusCode execute() override
Definition: SequentialOutputStream.cpp:36
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
std::stringstream::str
T str(T...args)
IRegistry.h
OutputStream::clearSelection
void clearSelection()
Clear list of selected objects.
Definition: OutputStream.cpp:309
SequentialOutputStream::m_iFile
unsigned int m_iFile
Definition: SequentialOutputStream.h:36
IOpaqueAddress.h
std::string::length
T length(T...args)
SequentialOutputStream::m_eventsPerFile
Gaudi::Property< unsigned int > m_eventsPerFile
Definition: SequentialOutputStream.h:30
SequentialOutputStream
Extension of OutputStream to write run records after last event.
Definition: SequentialOutputStream.h:14
std::string::substr
T substr(T...args)
StatusCode::FAILURE
Definition: StatusCode.h:29
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
SequentialOutputStream::m_nNumbersAdded
Gaudi::Property< unsigned int > m_nNumbersAdded
Definition: SequentialOutputStream.h:32
std::invalid_argument
STL class.
DataObject.h