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
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
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
29  = std::numeric_limits< unsigned int>::max() );
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  MsgStream log(msgSvc(), name());
41  log << MSG::ERROR << except.message() << endmsg;
42  return StatusCode::FAILURE;
43  }
45 }
46 
47 //=============================================================================
49 {
50  // Clear any previously existing item list
52  // Test whether this event should be output
53  if ( isEventAccepted() ) {
56  ++m_events;
57  return sc;
58  }
59  return StatusCode::SUCCESS;
60 }
61 
62 //=============================================================================
64 {
65  if ( m_events % m_eventsPerFile != 0 ) return;
66 
67  bf::path outputPath( m_outputName );
68  string filename = outputPath.filename().string();
69  bf::path dir = outputPath.parent_path();
70  string stem = outputPath.stem().string();
71  string extension = outputPath.extension().string();
72 
73  if ( !dir.empty() ) {
74  if ( !bf::exists( dir ) ) {
75  stringstream stream;
76  stream << "Directory " << dir << " does not exist.";
77  throw GaudiException( stream.str(), "error", StatusCode::FAILURE );
78  }
79  }
80 
81  if ( m_numericFilename ) {
82  if ( m_events == 0 ) {
83  try {
84  m_iFile = std::stoul( stem );
85  } catch( const std::invalid_argument& /* cast */ ) {
86  string msg = "Filename " + filename
87  + " is not a number, which was needed.";
88  throw GaudiException( msg, "error", StatusCode::FAILURE );
89  }
90  }
91  string iFile = std::to_string(m_iFile);
92  unsigned int length = 0;
93 
94  if ( stem.length() > iFile.length() ) {
95  length = stem.length() - iFile.length();
96  }
97 
98  stringstream name;
99  if ( !dir.empty() ) name << dir << "/";
100  for ( unsigned int i = 0; i < length; ++i ) {
101  name << "0";
102  }
103  name << iFile << extension;
104  m_outputName = name.str();
105  } else {
106  if ( m_iFile != 1 ) {
107  size_t pos = stem.rfind( "_" );
108  stem = stem.substr( 0, pos );
109  }
110 
111  string iFile = std::to_string(m_iFile);
112 
113  unsigned int length = 0;
114  if ( m_nNumbersAdded > iFile.length() ) {
115  length = m_nNumbersAdded - iFile.length();
116  }
117 
118  stringstream name;
119  name << dir << "/" << stem;
120  for ( unsigned int i = 0; i < length; ++i ) {
121  if ( i == 0 ) name << "_";
122  name << "0";
123  }
124  name << iFile << extension;
125  m_outputName = name.str();
126  }
127  ++m_iFile;
128 }
bool isEventAccepted() const
Test whether this event should be output.
virtual const std::string & message() const
error message to be printed
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
string to_string(const T &value)
Definition: mergesort.cpp:40
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
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
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.
list path
Definition: __init__.py:15
STL namespace.
std::string m_outputName
Name of the output file.
Definition: OutputStream.h:51
A small to stream Data I/O.
Definition: OutputStream.h:29
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:919
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:26
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
void clearSelection()
Clear list of selected objects.
Extension of OutputStream to write run records after last event.
list i
Definition: ana.py:128
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
Definition: Algorithm.cpp:1001