The Gaudi Framework  master (60ee365e)
Loading...
Searching...
No Matches
FileSvc Class Reference

Implementation of the IFileSvc interface, allowing algorithms to access ROOT files via a centralized service, and ensuring that files are shared appropriately and not unintentionally overwritten or duplicated. More...

Inheritance diagram for FileSvc:
Collaboration diagram for FileSvc:

Public Member Functions

 FileSvc (const std::string &name, ISvcLocator *svc)
StatusCode initialize () override
StatusCode finalize () override
std::shared_ptr< TFile > getFile (const std::string &identifier) override
 Get a TFile pointer based on an identifier.
bool hasIdentifier (const std::string &identifier) const override
 Check if a given identifier is known to the service.
Public Member Functions inherited from extends< Service, Gaudi::Interfaces::IFileSvc >
void const * i_cast (const InterfaceID &tid) const override
 Implementation of IInterface::i_cast.
StatusCode queryInterface (const InterfaceID &ti, void **pp) override
 Implementation of IInterface::queryInterface.
std::vector< std::string > getInterfaceNames () const override
 Implementation of IInterface::getInterfaceNames.
Public Member Functions inherited from Service
const std::string & name () const override
 Retrieve name of the service.
StatusCode configure () override
StatusCode initialize () override
StatusCode start () override
StatusCode stop () override
StatusCode finalize () override
StatusCode terminate () override
Gaudi::StateMachine::State FSMState () const override
Gaudi::StateMachine::State targetFSMState () const override
StatusCode reinitialize () override
StatusCode restart () override
StatusCode sysInitialize () override
 Initialize Service.
StatusCode sysStart () override
 Initialize Service.
StatusCode sysStop () override
 Initialize Service.
StatusCode sysFinalize () override
 Finalize Service.
StatusCode sysReinitialize () override
 Re-initialize the Service.
StatusCode sysRestart () override
 Re-initialize the Service.
 Service (std::string name, ISvcLocator *svcloc)
 Standard Constructor.
SmartIF< ISvcLocator > & serviceLocator () const override
 Retrieve pointer to service locator.
template<typename IFace = IService>
SmartIF< IFace > service (const std::string &name, bool createIf=true) const
template<class T>
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
template<class T>
StatusCode declareTool (ToolHandle< T > &handle, bool createIf=true)
template<class T>
StatusCode declareTool (ToolHandle< T > &handle, const std::string &toolTypeAndName, bool createIf=true)
 Declare used tool.
template<class T>
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
template<class T>
void addToolsArray (ToolHandleArray< T > &hndlArr)
const std::vector< IAlgTool * > & tools () const
SmartIF< IAuditorSvc > & auditorSvc () const
 The standard auditor service.May not be invoked before sysInitialize() has been invoked.
Public Member Functions inherited from PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
StatusCode setProperty (const Gaudi::Details::PropertyBase &p)
 Set the property from a property.
StatusCode setProperty (const std::string &name, const char *v)
 Special case for string literals.
StatusCode setProperty (const std::string &name, const std::string &v)
 Special case for std::string.
StatusCode setProperty (const std::string &name, const TYPE &value)
 set the property form the value
 PropertyHolder ()=default
Gaudi::Details::PropertyBasedeclareProperty (Gaudi::Details::PropertyBase &prop)
 Declare a property.
Gaudi::Details::PropertyBasedeclareRemoteProperty (const std::string &name, IProperty *rsvc, const std::string &rname="")
 Declare a remote property.
StatusCode setPropertyRepr (const std::string &n, const std::string &r) override
 set the property from name and value string representation
StatusCode getProperty (Gaudi::Details::PropertyBase *p) const override
 get the property
const std::vector< Gaudi::Details::PropertyBase * > & getProperties () const override
 get all properties
bool hasProperty (std::string_view name) const override
 Return true if we have a property with the given name.
Gaudi::Details::PropertyBaseproperty (std::string_view name) const
 \fixme property and bindPropertiesTo should be protected
void bindPropertiesTo (Gaudi::Interfaces::IOptionsSvc &optsSvc)
PropertyHolderoperator= (const PropertyHolder &)=delete
Public Member Functions inherited from CommonMessagingBase
virtual ~CommonMessagingBase ()=default
 Virtual destructor.
const SmartIF< IMessageSvc > & msgSvc () const
 The standard message service.
MsgStreammsgStream () const
 Return an uninitialized MsgStream.
MsgStreammsgStream (const MSG::Level level) const
 Predefined configurable message stream for the efficient printouts.
MsgStreamalways () const
 shortcut for the method msgStream(MSG::ALWAYS)
MsgStreamfatal () const
 shortcut for the method msgStream(MSG::FATAL)
MsgStreamerr () const
 shortcut for the method msgStream(MSG::ERROR)
MsgStreamerror () const
 shortcut for the method msgStream(MSG::ERROR)
MsgStreamwarning () const
 shortcut for the method msgStream(MSG::WARNING)
MsgStreaminfo () const
 shortcut for the method msgStream(MSG::INFO)
MsgStreamdebug () const
 shortcut for the method msgStream(MSG::DEBUG)
MsgStreamverbose () const
 shortcut for the method msgStream(MSG::VERBOSE)
MsgStreammsg () const
 shortcut for the method msgStream(MSG::INFO)
MSG::Level msgLevel () const
 get the cached level (originally extracted from the embedded MsgStream)
bool msgLevel (MSG::Level lvl) const
 get the output level from the embedded MsgStream

Public Attributes

Gaudi::Property< std::map< std::string, std::string > > m_config

Private Member Functions

std::shared_ptr< TFile > openFile (const std::string &filePath, const std::string &option, int compress)
 Open a file based on a specified path and opening mode.
StatusCode closeFiles ()
 Close all files.

Private Attributes

std::unordered_map< std::string, size_t > m_identifiers
std::vector< std::shared_ptr< TFile > > m_files

Additional Inherited Members

Public Types inherited from extends< Service, Gaudi::Interfaces::IFileSvc >
using base_class
 Typedef to this class.
using extend_interfaces_base
 Typedef to the base of this class.
Public Types inherited from Service
using Factory = Gaudi::PluginService::Factory<IService*( const std::string&, ISvcLocator* )>
Public Types inherited from PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
using PropertyHolderImpl
 Typedef used to refer to this class from derived classes, as in.
Public Types inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
using base_class
Public Types inherited from extend_interfaces< Interfaces... >
using ext_iids
 take union of the ext_iids of all Interfaces...
Protected Member Functions inherited from Service
std::vector< IAlgTool * > & tools ()
 ~Service () override
int outputLevel () const
 get the Service's output level
Protected Member Functions inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
MSG::Level setUpMessaging () const override final
 Set up local caches.
MSG::Level resetMessaging ()
 Reinitialize internal states.
void updateMsgStreamOutputLevel (int level)
 Update the output level of the cached MsgStream.
Protected Attributes inherited from Service
Gaudi::StateMachine::State m_state = Gaudi::StateMachine::OFFLINE
 Service state.
Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::OFFLINE
 Service state.
Gaudi::Property< int > m_outputLevel { this, "OutputLevel", MSG::NIL, "output level" }
 flag indicating whether ToolHandle tools have been added to m_tools
Gaudi::Property< bool > m_auditorInitialize { this, "AuditInitialize", false, "trigger auditor on initialize()" }
Gaudi::Property< bool > m_auditorStart { this, "AuditStart", false, "trigger auditor on start()" }
Gaudi::Property< bool > m_auditorStop { this, "AuditStop", false, "trigger auditor on stop()" }
Gaudi::Property< bool > m_auditorFinalize { this, "AuditFinalize", false, "trigger auditor on finalize()" }
Gaudi::Property< bool > m_auditorReinitialize { this, "AuditReinitialize", false, "trigger auditor on reinitialize()" }
Gaudi::Property< bool > m_auditorRestart { this, "AuditRestart", false, "trigger auditor on restart()" }
Gaudi::Property< bool > m_autoRetrieveTools
Gaudi::Property< bool > m_checkToolDeps
SmartIF< IAuditorSvcm_pAuditorSvc
 Auditor Service.

Detailed Description

Implementation of the IFileSvc interface, allowing algorithms to access ROOT files via a centralized service, and ensuring that files are shared appropriately and not unintentionally overwritten or duplicated.

Files can be accessed via an identifier and should be listed at configuration time through the Config property. Opening a distinct physical file emits a CONNECTED_OUTPUT ContextIncident<TFile*> when IncidentSvc is available.

Note that the paths given to Config property are URL syntax, that is they support extra paremeters in key=vlaue form after a '?' and seprated by '&' Supported parameters are : mode : one of 'CREATE' (default), 'RECREATE', 'UPDATE' compress : integer describing compression level to be used (see ROOT::RCompressionSetting::EDefaults)

Definition at line 42 of file FileSvc.cpp.

Constructor & Destructor Documentation

◆ FileSvc()

FileSvc::FileSvc ( const std::string & name,
ISvcLocator * svc )

Definition at line 161 of file FileSvc.cpp.

161: base_class( name, svc ) {}
const std::string & name() const override
Retrieve name of the service.
Definition Service.cpp:333

Member Function Documentation

◆ closeFiles()

StatusCode FileSvc::closeFiles ( )
private

Close all files.

Returns
StatusCode indicating the success or failure of the operation.

Definition at line 219 of file FileSvc.cpp.

219 {
220 for ( const auto& file : m_files ) {
221 if ( file ) { file->Close(); }
222 }
223 m_files.clear();
224 m_identifiers.clear();
225 return StatusCode::SUCCESS;
226}
std::vector< std::shared_ptr< TFile > > m_files
Definition FileSvc.cpp:89
std::unordered_map< std::string, size_t > m_identifiers
Definition FileSvc.cpp:86
constexpr static const auto SUCCESS
Definition StatusCode.h:99

◆ finalize()

StatusCode FileSvc::finalize ( )
override

Definition at line 200 of file FileSvc.cpp.

200{ return closeFiles(); }
StatusCode closeFiles()
Close all files.
Definition FileSvc.cpp:219

◆ getFile()

std::shared_ptr< TFile > FileSvc::getFile ( const std::string & identifier)
override

Get a TFile pointer based on an identifier.

Parameters
identifierA string identifying the file (2 identifiers can point to the same TFile).
Returns
An shared pointer to a TFile object corresponding to the specified identifier.

Definition at line 202 of file FileSvc.cpp.

202 {
203 auto it = m_identifiers.find( boost::to_lower_copy( identifier ) );
204 if ( it != m_identifiers.end() && it->second < m_files.size() ) { return m_files[it->second]; }
205 error() << "No file associated with identifier: " << identifier << endmsg;
206 return nullptr;
207}
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)

◆ hasIdentifier()

bool FileSvc::hasIdentifier ( const std::string & identifier) const
override

Check if a given identifier is known to the service.

Definition at line 209 of file FileSvc.cpp.

209 {
210 return m_identifiers.find( boost::to_lower_copy( identifier ) ) != m_identifiers.end();
211}

◆ initialize()

StatusCode FileSvc::initialize ( )
override

Definition at line 163 of file FileSvc.cpp.

163 {
164 return Service::initialize().andThen( [this]() {
165 auto incidentSvc = service<IIncidentSvc>( "IncidentSvc", false );
166
167 if ( checkConfig( m_config, *this ).isFailure() ) { return StatusCode::FAILURE; }
168
169 for ( const auto& [identifier, path] : m_config ) {
170 auto params = parseFilePath( path );
171
172 // Check if this file is already opened and mapped
173 if ( auto fileIndex = findFileIndex( m_files, params["path"] ) ) {
174 // File already opened, just map the identifier to the existing index
175 m_identifiers[boost::to_lower_copy( identifier )] = *fileIndex;
176 } else {
177 // File not found, open a new one
178 int compress = ( params.find( "compress" ) != params.end() )
179 ? std::stoi( params["compress"] )
180 : ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault;
181 if ( auto file = openFile( path, params["mode"], compress ) ) {
182 m_files.push_back( std::move( file ) );
183 m_identifiers[boost::to_lower_copy( identifier )] = m_files.size() - 1;
184 auto* outputFile = m_files.back().get();
185 if ( incidentSvc ) {
186 incidentSvc->fireIncident(
187 ContextIncident<TFile*>( outputFile->GetName(), "CONNECTED_OUTPUT", outputFile ) );
188 }
189 } else {
190 error() << "Failed to open file: " << params["path"] << endmsg;
191 return StatusCode::FAILURE;
192 }
193 }
194 }
195
196 return StatusCode::SUCCESS;
197 } );
198}
Gaudi::Property< std::map< std::string, std::string > > m_config
Definition FileSvc.cpp:64
std::shared_ptr< TFile > openFile(const std::string &filePath, const std::string &option, int compress)
Open a file based on a specified path and opening mode.
Definition FileSvc.cpp:213
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
Definition Service.h:79
StatusCode initialize() override
Definition Service.cpp:118
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
Definition StatusCode.h:151
constexpr static const auto FAILURE
Definition StatusCode.h:100
@ ROOT
Definition IFileMgr.h:147

◆ openFile()

std::shared_ptr< TFile > FileSvc::openFile ( const std::string & filePath,
const std::string & option,
int compress )
private

Open a file based on a specified path and opening mode.

Parameters
filePathThe file path.
optionThe file opening options to pass to root ("NEW/CREATE", "RECREATE", "UPDATE").
compressThe compression level and algorithm to use for the file.
Returns
A shared_ptr to a newly opened TFile object, or null if the operation fails.

Definition at line 213 of file FileSvc.cpp.

213 {
214 auto file = std::make_shared<TFile>( filePath.c_str(), option.c_str(), "", compress );
215 if ( !file || file->IsZombie() ) { return nullptr; }
216 return file;
217}

Member Data Documentation

◆ m_config

Gaudi::Property<std::map<std::string, std::string> > FileSvc::m_config
Initial value:
{
this, "Config", {}, "Map of keywords to file paths for file access" }

Definition at line 64 of file FileSvc.cpp.

64 {
65 this, "Config", {}, "Map of keywords to file paths for file access" };

◆ m_files

std::vector<std::shared_ptr<TFile> > FileSvc::m_files
private

Definition at line 89 of file FileSvc.cpp.

◆ m_identifiers

std::unordered_map<std::string, size_t> FileSvc::m_identifiers
private

Definition at line 86 of file FileSvc.cpp.


The documentation for this class was generated from the following file: