Gaudi Framework, version v25r0

Home   Generated: Mon Feb 17 2014
 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 
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 
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 }

Generated at Mon Feb 17 2014 14:37:40 for Gaudi Framework, version v25r0 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004