The Gaudi Framework  master (37c0b60a)
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
18 #include "SequentialOutputStream.h"
19 #include <GaudiKernel/DataObject.h>
24 #include <GaudiKernel/IRegistry.h>
25 #include <GaudiKernel/MsgStream.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.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 ) ) {
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.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 
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 }
std::string::resize
T resize(T... args)
Write.stream
stream
Definition: Write.py:32
DataStoreItem.h
std::stoul
T stoul(T... args)
AtlasMCRecoFullPrecedenceDump.path
path
Definition: AtlasMCRecoFullPrecedenceDump.py:49
std::string::length
T length(T... args)
SequentialOutputStream::execute
StatusCode execute() override
Working entry point.
Definition: SequentialOutputStream.cpp:45
GaudiException
Definition: GaudiException.h:31
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
std::stringstream
STL class.
SequentialOutputStream
Definition: SequentialOutputStream.h:24
SequentialOutputStream::writeObjects
StatusCode writeObjects() override
OutputStream override: Select the different objects and write them to file.
Definition: SequentialOutputStream.cpp:34
SequentialOutputStream::makeFilename
void makeFilename()
Definition: SequentialOutputStream.cpp:59
IDataProviderSvc.h
GaudiException::message
virtual const std::string & message() const
error message to be printed
Definition: GaudiException.h:68
StatusCode
Definition: StatusCode.h:65
IOpaqueAddress.h
std::to_string
T to_string(T... args)
OutputStream::writeObjects
virtual StatusCode writeObjects()
Select the different objects and write them to file.
Definition: OutputStream.cpp:180
std::invalid_argument
STL class.
SequentialOutputStream.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
IRegistry.h
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
DataObject.h
std
STL namespace.
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
graphanalysis.filename
filename
Definition: graphanalysis.py:130
IDataManagerSvc.h
std::string::rfind
T rfind(T... args)
MsgStream.h