The Gaudi Framework  v33r1 (b1225454)
SequentialOutputStream.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // standard headers
12 #include <limits>
13 
14 // boost
15 #include <boost/filesystem.hpp>
16 
17 // Framework include files
18 #include "GaudiKernel/DataObject.h"
23 #include "GaudiKernel/IRegistry.h"
24 #include "GaudiKernel/MsgStream.h"
25 #include "SequentialOutputStream.h"
26 
27 // Define the algorithm factory for the standard output data writer
29 
30 using namespace std;
31 namespace bf = boost::filesystem;
32 
33 //=============================================================================
35  try {
36  makeFilename();
37  } catch ( const GaudiException& except ) {
38  error() << except.message() << endmsg;
39  return StatusCode::FAILURE;
40  }
42 }
43 
44 //=============================================================================
46  // Clear any previously existing item list
47  clearSelection();
48  // Test whether this event should be output
49  if ( isEventAccepted() ) {
50  StatusCode sc = writeObjects();
51  clearSelection();
52  ++m_events;
53  return sc;
54  }
55  return StatusCode::SUCCESS;
56 }
57 
58 //=============================================================================
60  if ( m_events % m_eventsPerFile != 0 ) return;
61 
62  bf::path outputPath( m_outputName );
63  string filename = outputPath.filename().string();
64  bf::path dir = outputPath.parent_path();
65  string stem = outputPath.stem().string();
66  string extension = outputPath.extension().string();
67 
68  if ( !dir.empty() ) {
69  if ( !bf::exists( dir ) ) {
70  stringstream stream;
71  stream << "Directory " << dir << " does not exist.";
72  throw GaudiException( stream.str(), "error", StatusCode::FAILURE );
73  }
74  }
75 
76  if ( m_numericFilename ) {
77  if ( m_events == 0 ) {
78  try {
79  m_iFile = std::stoul( stem );
80  } catch ( const std::invalid_argument& /* cast */ ) {
81  string msg = "Filename " + filename + " is not a number, which was needed.";
82  throw GaudiException( msg, "error", StatusCode::FAILURE );
83  }
84  }
85  string iFile = std::to_string( m_iFile );
86  unsigned int length = 0;
87 
88  if ( stem.length() > iFile.length() ) { length = stem.length() - iFile.length(); }
89 
91  if ( !dir.empty() ) name << dir << "/";
92  for ( unsigned int i = 0; i < length; ++i ) { name << "0"; }
93  name << iFile << extension;
94  m_outputName = name.str();
95  } else {
96  if ( m_iFile != 1 ) {
97  size_t pos = stem.rfind( "_" );
98  stem = stem.substr( 0, pos );
99  }
100 
101  string iFile = std::to_string( m_iFile );
102 
103  unsigned int length = 0;
104  if ( m_nNumbersAdded > iFile.length() ) { length = m_nNumbersAdded - iFile.length(); }
105 
107  name << dir << "/" << stem;
108  for ( unsigned int i = 0; i < length; ++i ) {
109  if ( i == 0 ) name << "_";
110  name << "0";
111  }
112  name << iFile << extension;
113  m_outputName = name.str();
114  }
115  ++m_iFile;
116 }
Define general base for Gaudi exception.
StatusCode writeObjects() override
OutputStream override: Select the different objects and write them to file.
T rfind(T... args)
T to_string(T... args)
virtual const std::string & message() const
error message to be printed
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
STL namespace.
T stoul(T... args)
T string(T... args)
#define DECLARE_COMPONENT(type)
virtual StatusCode writeObjects()
Select the different objects and write them to file.
StatusCode execute() override
Working entry point.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
T str(T... args)
T length(T... args)
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Extension of OutputStream to write run records after last event.
T substr(T... args)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202