The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
SequentialOutputStream.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2024 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
26
27// Define the algorithm factory for the standard output data writer
29
30using namespace std;
31namespace bf = boost::filesystem;
32
33//=============================================================================
35 try {
37 } catch ( const GaudiException& except ) {
38 error() << except.message() << endmsg;
40 }
42}
43
44//=============================================================================
46 // Clear any previously existing item list
48 // Test whether this event should be output
49 if ( isEventAccepted() ) {
52 ++m_events;
53 return sc;
54 }
56}
57
58//=============================================================================
60 if ( m_events % m_eventsPerFile != 0 ) return;
61
62 bf::path outputPath( m_outputName.value() );
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
90 stringstream name;
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.resize( 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
106 stringstream name;
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}
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
const std::string & name() const override
The identifying name of the algorithm object.
Define general base for Gaudi exception.
virtual const std::string & message() const
error message to be printed
void clearSelection()
Clear list of selected objects.
Gaudi::Property< std::string > m_outputName
virtual StatusCode writeObjects()
Select the different objects and write them to file.
bool isEventAccepted() const
Test whether this event should be output.
Extension of OutputStream to write run records after last event.
StatusCode execute() override
Working entry point.
Gaudi::Property< unsigned int > m_eventsPerFile
Gaudi::Property< unsigned int > m_nNumbersAdded
StatusCode writeObjects() override
OutputStream override: Select the different objects and write them to file.
Gaudi::Property< bool > m_numericFilename
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
STL namespace.