The Gaudi Framework  master (2e57474e)
Loading...
Searching...
No Matches
GenericNTupleReader.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 2026 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#include <Gaudi/Algorithm.h>
14#include <TFile.h>
15#include <TTree.h>
16#include <format>
17#include <mutex>
18#include <string>
19#include <unordered_map>
20
21namespace {
22 // Extract the name from a path in the TES string by returning the last part after a slash
23 auto getNameFromLoc( std::string_view loc ) {
24 auto lastSlashPos = loc.find_last_of( '/' );
25 return std::string{ lastSlashPos != loc.npos ? loc.substr( lastSlashPos + 1 ) : loc };
26 }
27} // namespace
28
29namespace Gaudi::NTuple {
30
32 public:
34
36 const auto& extraOutputs = extraOutputDeps();
37 if ( extraOutputs.empty() ) {
38 error() << "No extra output locations specified. Please define extra output for the NTuple reader" << endmsg;
40 }
41
42 auto fileSvc = service<Gaudi::Interfaces::IFileSvc>( "FileSvc" );
43 if ( !fileSvc ) {
44 error() << "Failed to retrieve FileSvc" << endmsg;
46 }
47
48 auto file = fileSvc->getFile( m_fileId );
49 if ( !file ) {
50 error() << "Failed to retrieve TFile with identifier " << m_fileId.value() << endmsg;
52 }
53
54 m_tree = file->Get<TTree>( m_ntupleTname.value().c_str() );
55 if ( !m_tree ) {
56 error() << "Failed to retrieve TTree " << m_ntupleTname.value() << endmsg;
58 }
59
60 return connectBranches( extraOutputs );
61 }
62
63 StatusCode execute( const EventContext& ctx ) const override {
64 std::scoped_lock lock( m_mutex );
65 m_tree->GetEntry( ctx.evt() );
66 for ( auto& [address, helper] : m_branchReadHelpers ) {
67 eventSvc()
68 ->registerObject( address, helper.adoptValue().release() )
69 .orThrow( std::format( "Failed to register object to location '{}'", address ), name() );
70 }
72 }
73
75
76 private:
77 Gaudi::Property<std::string> m_fileId{ this, "InputFile", "NTuple",
78 "Identifier for the TFile to read from" }; // Property to hold the the
79 // identifier of where the
80 // TTree will be saved
81 Gaudi::Property<std::string> m_ntupleTname{ this, "NTupleName", "", "Name of the TTree" };
82 TTree* m_tree{ nullptr };
83 mutable std::unordered_map<std::string, Gaudi::details::BranchReadHelper> m_branchReadHelpers;
84 mutable std::mutex m_mutex;
85
86 StatusCode connectBranches( const DataObjIDColl& extraOutputs ) {
87 m_branchReadHelpers.clear();
88 for ( const auto& dep : extraOutputs ) {
89 auto branchName = getNameFromLoc( dep.key() );
90 auto branch = m_tree->GetBranch( branchName.c_str() );
91 if ( !branch ) {
92 error() << "Failed to retrieve TBranch " << branchName << endmsg;
94 }
95 m_branchReadHelpers.emplace( dep.key(), branch );
96 }
97
99 }
100 };
101
102 DECLARE_COMPONENT( GenericReader )
103
104} // namespace Gaudi::NTuple
std::unordered_set< DataObjID, DataObjID_Hasher > DataObjIDColl
Definition DataObjID.h:121
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)
This class represents an entry point to all the event specific data.
Base class from which all concrete algorithm classes should be derived.
Definition Algorithm.h:87
SmartIF< IDataProviderSvc > & eventSvc() const
The standard event data service.
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition Algorithm.h:98
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition Algorithm.h:181
const std::string & name() const override
The identifying name of the algorithm object.
SmartIF< IService > service(std::string_view name, const bool createIf=true, const bool quiet=false) const
Return a pointer to the service identified by name (or "type/name").
StatusCode connectBranches(const DataObjIDColl &extraOutputs)
StatusCode execute(const EventContext &ctx) const override
std::unordered_map< std::string, Gaudi::details::BranchReadHelper > m_branchReadHelpers
Gaudi::Property< std::string > m_ntupleTname
Gaudi::Property< std::string > m_fileId
Implementation of property with value of concrete type.
Definition Property.h:35
StatusCode registerObject(std::string_view fullPath, DataObject *pObject)
Register object with the data store.
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
const StatusCode & orThrow(std::string_view message, std::string_view tag) const
Throw a GaudiException in case of failures.
Definition StatusCode.h:194
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100