All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SequentialOutputStream.cpp
Go to the documentation of this file.
1 // boost
2 #include <boost/numeric/conversion/bounds.hpp>
3 #include <boost/filesystem.hpp>
4 #include <boost/foreach.hpp>
5 #include <boost/lexical_cast.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 using boost::lexical_cast;
23 using boost::bad_lexical_cast;
24 
25 //=============================================================================
27  ISvcLocator* svc )
28 : OutputStream( name, svc ), m_events( 0 ), m_iFile( 1 )
29 {
30  declareProperty( "EventsPerFile", m_eventsPerFile
31  = boost::numeric::bounds< unsigned int>::highest() );
32  declareProperty( "NumericFilename", m_numericFilename = false );
33  declareProperty( "NumbersAdded", m_nNumbersAdded = 6 );
34 }
35 
36 //=============================================================================
38 {
39  try {
40  makeFilename();
41  } catch ( const GaudiException& except ) {
42  MsgStream log(msgSvc(), name());
43  log << MSG::ERROR << except.message() << endmsg;
44  return StatusCode::FAILURE;
45  }
47 }
48 
49 //=============================================================================
51 {
52  // Clear any previously existing item list
54  // Test whether this event should be output
55  if ( isEventAccepted() ) {
58  m_events++;
59  return sc;
60  }
61  return StatusCode::SUCCESS;
62 }
63 
64 //=============================================================================
66 {
67  if ( m_events % m_eventsPerFile != 0 ) return;
68 
69  bf::path outputPath( m_outputName );
70  string filename = outputPath.filename().string();
71  bf::path dir = outputPath.parent_path();
72  string stem = outputPath.stem().string();
73  string extension = outputPath.extension().string();
74 
75  if ( !dir.empty() ) {
76  if ( !bf::exists( dir ) ) {
77  stringstream stream;
78  stream << "Directory " << dir << " does not exist.";
79  throw GaudiException( stream.str(), "error", StatusCode::FAILURE );
80  }
81  }
82 
83  if ( m_numericFilename ) {
84  if ( m_events == 0 ) {
85  try {
86  m_iFile = lexical_cast< unsigned int >( stem );
87  } catch( const bad_lexical_cast& /* cast */ ) {
88  stringstream stream;
89  stream << "Filename " << filename
90  << " is not a number, which was needed.";
91  throw GaudiException( stream.str(), "error", StatusCode::FAILURE );
92  }
93  }
94  stringstream iFileStream;
95  iFileStream << m_iFile;
96  string iFile( iFileStream.str() );
97  unsigned int length = 0;
98 
99  if ( stem.length() > iFile.length() ) {
100  length = stem.length() - iFile.length();
101  }
102 
103  stringstream name;
104  if ( !dir.empty() ) {
105  name << dir << "/";
106  }
107  for ( unsigned int i = 0; i < length; ++i ) {
108  name << "0";
109  }
110  name << iFile << extension;
111  m_outputName = name.str();
112  } else {
113  if ( m_iFile != 1 ) {
114  size_t pos = stem.rfind( "_" );
115  stem = stem.substr( 0, pos );
116  }
117 
118  stringstream iFileStream;
119  iFileStream << m_iFile;
120  string iFile( iFileStream.str() );
121 
122  unsigned int length = 0;
123  if ( m_nNumbersAdded > iFile.length() ) {
124  length = m_nNumbersAdded - iFile.length();
125  }
126 
127  stringstream name;
128  name << dir << "/" << stem;
129  for ( unsigned int i = 0; i < length; ++i ) {
130  if ( i == 0 ) name << "_";
131  name << "0";
132  }
133  name << iFile << extension;
134  m_outputName = name.str();
135  }
136  ++m_iFile;
137 }
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
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:26
virtual StatusCode writeObjects()
OutputStream override: Select the different objects and write them to file.
std::string m_outputName
Name of the output file.
Definition: OutputStream.h:49
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
A small to stream Data I/O.
Definition: OutputStream.h:27
virtual StatusCode writeObjects()
Select the different objects and write them to file.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
virtual const std::string & name() const
The identifying name of the algorithm object.
Definition: Algorithm.cpp:837
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
Definition: Algorithm.cpp:896
void clearSelection()
Clear list of selected objects.
GAUDI_API std::string path(const AIDA::IBaseHistogram *aida)
get the path in THS for AIDA histogram
Extension of OutputStream to write run records after last event.
virtual StatusCode execute()
Working entry point.
list i
Definition: ana.py:128
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244