Gaudi Framework, version v23r9

Home   Generated: Thu Jul 18 2013
 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
14 #include "GaudiKernel/DataObject.h"
15 #include "GaudiKernel/MsgStream.h"
16 #include "SequentialOutputStream.h"
17 
18 // Define the algorithm factory for the standard output data writer
20 
21 using namespace std;
22 namespace bf = boost::filesystem;
23 using boost::lexical_cast;
24 using boost::bad_lexical_cast;
25 
26 //=============================================================================
28  ISvcLocator* svc )
29 : OutputStream( name, svc ), m_events( 0 ), m_iFile( 1 )
30 {
31  declareProperty( "EventsPerFile", m_eventsPerFile
32  = boost::numeric::bounds< unsigned int>::highest() );
33  declareProperty( "NumericFilename", m_numericFilename = false );
34  declareProperty( "NumbersAdded", m_nNumbersAdded = 6 );
35 }
36 
37 //=============================================================================
39 {
40  try {
41  makeFilename();
42  } catch ( const GaudiException& except ) {
43  MsgStream log(msgSvc(), name());
44  log << MSG::ERROR << except.message() << endmsg;
45  return StatusCode::FAILURE;
46  }
48 }
49 
50 //=============================================================================
52 {
53  // Clear any previously existing item list
55  // Test whether this event should be output
56  if ( isEventAccepted() ) {
59  m_events++;
60  return sc;
61  }
62  return StatusCode::SUCCESS;
63 }
64 
65 //=============================================================================
67 {
68  if ( m_events % m_eventsPerFile != 0 ) return;
69 
70  bf::path outputPath( m_outputName );
71  string filename = outputPath.filename().string();
72  bf::path dir = outputPath.parent_path();
73  string stem = outputPath.stem().string();
74  string extension = outputPath.extension().string();
75 
76  if ( !dir.empty() ) {
77  if ( !bf::exists( dir ) ) {
78  stringstream stream;
79  stream << "Directory " << dir << " does not exist.";
80  throw GaudiException( stream.str(), "error", StatusCode::FAILURE );
81  }
82  }
83 
84  if ( m_numericFilename ) {
85  if ( m_events == 0 ) {
86  try {
87  m_iFile = lexical_cast< unsigned int >( stem );
88  } catch( const bad_lexical_cast& /* cast */ ) {
89  stringstream stream;
90  stream << "Filename " << filename
91  << " is not a number, which was needed.";
92  throw GaudiException( stream.str(), "error", StatusCode::FAILURE );
93  }
94  }
95  stringstream iFileStream;
96  iFileStream << m_iFile;
97  string iFile( iFileStream.str() );
98  unsigned int length = 0;
99 
100  if ( stem.length() > iFile.length() ) {
101  length = stem.length() - iFile.length();
102  }
103 
105  if ( !dir.empty() ) {
106  name << dir << "/";
107  }
108  for ( unsigned int i = 0; i < length; ++i ) {
109  name << "0";
110  }
111  name << iFile << extension;
112  m_outputName = name.str();
113  } else {
114  if ( m_iFile != 1 ) {
115  size_t pos = stem.rfind( "_" );
116  stem = stem.substr( 0, pos );
117  }
118 
119  stringstream iFileStream;
120  iFileStream << m_iFile;
121  string iFile( iFileStream.str() );
122 
123  unsigned int length = 0;
124  if ( m_nNumbersAdded > iFile.length() ) {
125  length = m_nNumbersAdded - iFile.length();
126  }
127 
129  name << dir << "/" << stem;
130  for ( unsigned int i = 0; i < length; ++i ) {
131  if ( i == 0 ) name << "_";
132  name << "0";
133  }
134  name << iFile << extension;
135  m_outputName = name.str();
136  }
137  ++m_iFile;
138 }

Generated at Thu Jul 18 2013 12:18:01 for Gaudi Framework, version v23r9 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004