The Gaudi Framework  v29r0 (ff2e7097)
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 {
26  try {
27  makeFilename();
28  } catch ( const GaudiException& except ) {
29  error() << except.message() << endmsg;
30  return StatusCode::FAILURE;
31  }
33 }
34 
35 //=============================================================================
37 {
38  // Clear any previously existing item list
40  // Test whether this event should be output
41  if ( isEventAccepted() ) {
42  StatusCode sc = writeObjects();
44  ++m_events;
45  return sc;
46  }
47  return StatusCode::SUCCESS;
48 }
49 
50 //=============================================================================
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 
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 
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 }
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.
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:731
The namespace threadpool contains a thread pool and related utility classes.
Definition: iter_pos.hpp:13
StatusCode writeObjects() override
OutputStream override: Select the different objects and write them to file.
T rfind(T...args)
Gaudi::Property< std::string > m_outputName
Definition: OutputStream.h:52
T to_string(T...args)
STL namespace.
Gaudi::Property< bool > m_numericFilename
T stoul(T...args)
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:33
T string(T...args)
virtual StatusCode writeObjects()
Select the different objects and write them to file.
StatusCode execute() override
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
T str(T...args)
void clearSelection()
Clear list of selected objects.
T length(T...args)
Gaudi::Property< unsigned int > m_eventsPerFile
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:209
Gaudi::Property< unsigned int > m_nNumbersAdded