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/DataObject.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  ISvcLocator* svc )
26 : OutputStream( name, svc )
27 {
28  declareProperty( "EventsPerFile", m_eventsPerFile
30  declareProperty( "NumericFilename", m_numericFilename = false );
31  declareProperty( "NumbersAdded", m_nNumbersAdded = 6 );
32 }
33 
34 //=============================================================================
36 {
37  try {
38  makeFilename();
39  } catch ( const GaudiException& except ) {
40  error() << except.message() << endmsg;
41  return StatusCode::FAILURE;
42  }
44 }
45 
46 //=============================================================================
48 {
49  // Clear any previously existing item list
51  // Test whether this event should be output
52  if ( isEventAccepted() ) {
55  ++m_events;
56  return sc;
57  }
58  return StatusCode::SUCCESS;
59 }
60 
61 //=============================================================================
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 
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 
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 }
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.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
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)
T to_string(T...args)
STL namespace.
std::string m_outputName
Name of the output file.
Definition: OutputStream.h:51
T stoul(T...args)
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
A small to stream Data I/O.
Definition: OutputStream.h:29
T string(T...args)
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:820
virtual StatusCode writeObjects()
Select the different objects and write them to file.
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
StatusCode execute() override
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
T str(T...args)
void clearSelection()
Clear list of selected objects.
GAUDI_API std::string path(const AIDA::IBaseHistogram *aida)
get the path in THS for AIDA histogram
T length(T...args)
Extension of OutputStream to write run records after last event.
T substr(T...args)
list i
Definition: ana.py:128
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244