| The Gaudi Framework v27r1

- 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/IRegistry.h"
9 #include "GaudiKernel/IDataManagerSvc.h"
10 #include "GaudiKernel/IDataProviderSvc.h"
11 #include "GaudiKernel/IOpaqueAddress.h"
12 #include "GaudiKernel/DataStoreItem.h"
13 #include "GaudiKernel/DataObject.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 SequentialOutputStream::SequentialOutputStream( const string& name,
25 ISvcLocator* svc )
26 : OutputStream( name, svc )
27 {
28 declareProperty( "EventsPerFile", m_eventsPerFile
29 = std::numeric_limits< unsigned int>::max() );
30 declareProperty( "NumericFilename", m_numericFilename = false );
31 declareProperty( "NumbersAdded", m_nNumbersAdded = 6 );
32 }
33
34 //=============================================================================
35 StatusCode SequentialOutputStream::writeObjects()
36 {
37 try {
38 makeFilename();
39 } catch ( const GaudiException& except ) {
40 error() << except.message() << endmsg;
41 return StatusCode::FAILURE;
42 }
43 return OutputStream::writeObjects();
44 }
45
46 //=============================================================================
47 StatusCode SequentialOutputStream::execute()
48 {
49 // Clear any previously existing item list
50 clearSelection();
51 // Test whether this event should be output
52 if ( isEventAccepted() ) {
53 StatusCode sc = writeObjects();
54 clearSelection();
55 ++m_events;
56 return sc;
57 }
58 return StatusCode::SUCCESS;
59 }
60
61 //=============================================================================
62 void SequentialOutputStream::makeFilename()
63 {
64 if ( m_events % m_eventsPerFile != 0 ) return;
65
66 bf::path outputPath( m_outputName );
67 string filename = outputPath.filename().string();
68 bf::path dir = outputPath.parent_path();
69 string stem = outputPath.stem().string();
70 string extension = outputPath.extension().string();
71
72 if ( !dir.empty() ) {
73 if ( !bf::exists( dir ) ) {
74 stringstream stream;
75 stream << "Directory " << dir << " does not exist.";
76 throw GaudiException( stream.str(), "error", StatusCode::FAILURE );
77 }
78 }
79
80 if ( m_numericFilename ) {
81 if ( m_events == 0 ) {
82 try {
83 m_iFile = std::stoul( stem );
84 } catch( const std::invalid_argument& /* cast */ ) {
85 string msg = "Filename " + filename
86 + " is not a number, which was needed.";
87 throw GaudiException( msg, "error", StatusCode::FAILURE );
88 }
89 }
90 string iFile = std::to_string(m_iFile);
91 unsigned int length = 0;
92
93 if ( stem.length() > iFile.length() ) {
94 length = stem.length() - iFile.length();
95 }
96
97 stringstream name;
98 if ( !dir.empty() ) name << dir << "/";
99 for ( unsigned int i = 0; i < length; ++i ) {
100 name << "0";
101 }
102 name << iFile << extension;
103 m_outputName = name.str();
104 } else {
105 if ( m_iFile != 1 ) {
106 size_t pos = stem.rfind( "_" );
107 stem = stem.substr( 0, pos );
108 }
109
110 string iFile = std::to_string(m_iFile);
111
112 unsigned int length = 0;
113 if ( m_nNumbersAdded > iFile.length() ) {
114 length = m_nNumbersAdded - iFile.length();
115 }
116
117 stringstream name;
118 name << dir << "/" << stem;
119 for ( unsigned int i = 0; i < length; ++i ) {
120 if ( i == 0 ) name << "_";
121 name << "0";
122 }
123 name << iFile << extension;
124 m_outputName = name.str();
125 }
126 ++m_iFile;
127 }
OutputStream::isEventAccepted
bool isEventAccepted() const
Test whether this event should be output.
Definition: OutputStream.cpp:609
GaudiException::message
virtual const std::string & message() const
error message to be printed
Definition: GaudiException.h:111
CommonMessagingBase::msg
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:133
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:23
SequentialOutputStream::makeFilename
void makeFilename()
Definition: SequentialOutputStream.cpp:62
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
SequentialOutputStream::m_events
unsigned int m_events
Definition: SequentialOutputStream.h:38
boost
The namespace threadpool contains a thread pool and related utility classes.
Definition: iter_pos.hpp:13
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:28
SequentialOutputStream::writeObjects
StatusCode writeObjects() override
OutputStream override: Select the different objects and write them to file.
Definition: SequentialOutputStream.cpp:35
MsgStream.h
std::string::rfind
T rfind(T...args)
std::to_string
T to_string(T...args)
std
STL namespace.
OutputStream::m_outputName
std::string m_outputName
Name of the output file.
Definition: OutputStream.h:51
graphanalysis.filename
string filename
Definition: graphanalysis.py:109
merge_files.sc
int sc
Definition: merge_files.py:167
SequentialOutputStream.h
std::stringstream
STL class.
std::stoul
T stoul(T...args)
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
OutputStream
A small to stream Data I/O.
Definition: OutputStream.h:29
std::string::string
T string(T...args)
Algorithm::name
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:820
OutputStream::writeObjects
virtual StatusCode writeObjects()
Select the different objects and write them to file.
Definition: OutputStream.cpp:199
CommonMessagingBase::error
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:118
DataStoreItem.h
IDataManagerSvc.h
IDataProviderSvc.h
StatusCode::FAILURE
Definition: StatusCode.h:29
SequentialOutputStream::execute
StatusCode execute() override
Definition: SequentialOutputStream.cpp:47
StatusCode::SUCCESS
Definition: StatusCode.h:30
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:375
SequentialOutputStream::m_iFile
unsigned int m_iFile
Definition: SequentialOutputStream.h:39
Gaudi::Utils::Histos::path
GAUDI_API std::string path(const AIDA::IBaseHistogram *aida)
get the path in THS for AIDA histogram
Definition: HistoTableFormat.cpp:191
IOpaqueAddress.h
std::string::length
T length(T...args)
SequentialOutputStream
Extension of OutputStream to write run records after last event.
Definition: SequentialOutputStream.h:14
std::string::substr
T substr(T...args)
SequentialOutputStream::m_numericFilename
bool m_numericFilename
Definition: SequentialOutputStream.h:35
SequentialOutputStream::m_nNumbersAdded
unsigned int m_nNumbersAdded
Definition: SequentialOutputStream.h:34
ana.i
list i
Definition: ana.py:128
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
std::invalid_argument
STL class.
DataObject.h
SequentialOutputStream::m_eventsPerFile
unsigned int m_eventsPerFile
Definition: SequentialOutputStream.h:33
std::numeric_limits