Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
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
19 
20 using namespace std;
21 namespace bf = boost::filesystem;
22 
23 //=============================================================================
25  try {
26  makeFilename();
27  } catch ( const GaudiException& except ) {
28  error() << except.message() << endmsg;
29  return StatusCode::FAILURE;
30  }
32 }
33 
34 //=============================================================================
36  // Clear any previously existing item list
38  // Test whether this event should be output
39  if ( isEventAccepted() ) {
40  StatusCode sc = writeObjects();
42  ++m_events;
43  return sc;
44  }
45  return StatusCode::SUCCESS;
46 }
47 
48 //=============================================================================
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 
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 
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 }
bool isEventAccepted() const
Test whether this event should be output.
virtual const std::string & message() const
error message to be printed
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
Define general base for Gaudi exception.
StatusCode writeObjects() override
OutputStream override: Select the different objects and write them to file.
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:635
T rfind(T...args)
Gaudi::Property< std::string > m_outputName
Definition: OutputStream.h:54
T to_string(T...args)
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
STL namespace.
Gaudi::Property< bool > m_numericFilename
T stoul(T...args)
T string(T...args)
#define DECLARE_COMPONENT(type)
virtual StatusCode writeObjects()
Select the different objects and write them to file.
StatusCode execute() override
Working entry point.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
T str(T...args)
void clearSelection()
Clear list of selected objects.
T length(T...args)
Gaudi::Property< unsigned int > m_eventsPerFile
constexpr static const auto FAILURE
Definition: StatusCode.h:86
Extension of OutputStream to write run records after last event.
T substr(T...args)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
Gaudi::Property< unsigned int > m_nNumbersAdded