Skip to content

Commit

Permalink
[tree] Apply clang-format to TTreeReader[,Value,Array].[h,cxx]
Browse files Browse the repository at this point in the history
vepadulano committed Jan 29, 2025
1 parent 67d8e55 commit 1e681c6
Showing 6 changed files with 889 additions and 861 deletions.
85 changes: 38 additions & 47 deletions tree/treeplayer/inc/TTreeReader.h
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@
#ifndef ROOT_TTreeReader
#define ROOT_TTreeReader


////////////////////////////////////////////////////////////////////////////
// //
// TTreeReader //
@@ -38,14 +37,13 @@ class TFileCollection;

namespace ROOT {
namespace Internal {
class TBranchProxyDirector;
class TFriendProxy;
}
}
class TBranchProxyDirector;
class TFriendProxy;
} // namespace Internal
} // namespace ROOT

class TTreeReader: public TObject {
class TTreeReader : public TObject {
public:

///\class TTreeReader::Iterator_t
/// Iterate through the entries of a TTree.
///
@@ -57,8 +55,8 @@ class TTreeReader: public TObject {
/// is reached).
class Iterator_t {
private:
Long64_t fEntry; ///< Entry number of the tree referenced by this iterator; -1 is invalid.
TTreeReader* fReader; ///< The reader we select the entries on.
Long64_t fEntry; ///< Entry number of the tree referenced by this iterator; -1 is invalid.
TTreeReader *fReader; ///< The reader we select the entries on.

/// Whether the iterator points to a valid entry.
bool IsValid() const { return fEntry >= 0; }
@@ -72,12 +70,11 @@ class TTreeReader: public TObject {
using reference = const Long64_t &;

/// Default-initialize the iterator as "past the end".
Iterator_t(): fEntry(-1), fReader(nullptr) {}
Iterator_t() : fEntry(-1), fReader(nullptr) {}

/// Initialize the iterator with the reader it steers and a
/// tree entry number; -1 is invalid.
Iterator_t(TTreeReader& reader, Long64_t entry):
fEntry(entry), fReader(&reader) {}
Iterator_t(TTreeReader &reader, Long64_t entry) : fEntry(entry), fReader(&reader) {}

/// Compare two iterators for equality.
bool operator==(const Iterator_t &lhs) const
@@ -106,19 +103,19 @@ class TTreeReader: public TObject {
}

/// Compare two iterators for inequality.
bool operator!=(const Iterator_t& lhs) const {
return !(*this == lhs);
}
bool operator!=(const Iterator_t &lhs) const { return !(*this == lhs); }

/// Increment the iterator (postfix i++).
Iterator_t operator++(int) {
Iterator_t operator++(int)
{
Iterator_t ret = *this;
this->operator++();
return ret;
}

/// Increment the iterator (prefix ++i).
Iterator_t& operator++() {
Iterator_t &operator++()
{
if (IsValid()) {
++fEntry;
// Force validity check of new fEntry.
@@ -131,7 +128,8 @@ class TTreeReader: public TObject {
}

/// Set the entry number in the reader and return it.
const Long64_t& operator*() {
const Long64_t &operator*()
{
if (IsValid()) {
// If we cannot access that entry, mark the iterator invalid.
if (fReader->SetEntry(fEntry) != kEntryValid) {
@@ -142,9 +140,7 @@ class TTreeReader: public TObject {
return fEntry;
}

const Long64_t& operator*() const {
return **const_cast<Iterator_t*>(this);
}
const Long64_t &operator*() const { return **const_cast<Iterator_t *>(this); }
};

typedef Iterator_t iterator;
@@ -189,33 +185,29 @@ class TTreeReader: public TObject {

TTreeReader(TTree *tree, TEntryList *entryList = nullptr, bool warnAboutLongerFriends = true,
const std::vector<std::string> &suppressErrorsForMissingBranches = {});
TTreeReader(const char* keyname, TDirectory* dir, TEntryList* entryList = nullptr);
TTreeReader(const char *keyname, TDirectory *dir, TEntryList *entryList = nullptr);
TTreeReader(const char *keyname, TEntryList *entryList = nullptr) : TTreeReader(keyname, nullptr, entryList) {}

~TTreeReader() override;

void SetTree(TTree* tree, TEntryList* entryList = nullptr);
void SetTree(const char* keyname, TEntryList* entryList = nullptr) {
SetTree(keyname, nullptr, entryList);
}
void SetTree(const char* keyname, TDirectory* dir, TEntryList* entryList = nullptr);
void SetTree(TTree *tree, TEntryList *entryList = nullptr);
void SetTree(const char *keyname, TEntryList *entryList = nullptr) { SetTree(keyname, nullptr, entryList); }
void SetTree(const char *keyname, TDirectory *dir, TEntryList *entryList = nullptr);

bool IsChain() const { return TestBit(kBitIsChain); }

bool IsInvalid() const { return fLoadTreeStatus == kNoTree; }

TTree* GetTree() const { return fTree; }
TEntryList* GetEntryList() const { return fEntryList; }
TTree *GetTree() const { return fTree; }
TEntryList *GetEntryList() const { return fEntryList; }

///\{ \name Entry setters

/// Move to the next entry (or index of the TEntryList if that is set).
///
/// \return false if the previous entry was already the last entry. This allows
/// the function to be used in `while (reader.Next()) { ... }`
bool Next() {
return SetEntry(GetCurrentEntry() + 1) == kEntryValid;
}
bool Next() { return SetEntry(GetCurrentEntry() + 1) == kEntryValid; }

/// Set the next entry (or index of the TEntryList if that is set).
///
@@ -263,16 +255,14 @@ class TTreeReader: public TObject {
bool Notify() override;

/// Return an iterator to the 0th TTree entry.
Iterator_t begin() {
return Iterator_t(*this, 0);
}
Iterator_t begin() { return Iterator_t(*this, 0); }
/// Return an iterator beyond the last TTree entry.
Iterator_t end() { return Iterator_t(*this, -1); }

protected:
using NamedProxies_t = std::unordered_map<std::string, std::unique_ptr<ROOT::Internal::TNamedBranchProxy>>;
void Initialize();
ROOT::Internal::TNamedBranchProxy* FindProxy(const char* branchname) const
ROOT::Internal::TNamedBranchProxy *FindProxy(const char *branchname) const
{
const auto proxyIt = fProxies.find(branchname);
return fProxies.end() != proxyIt ? proxyIt->second.get() : nullptr;
@@ -293,8 +283,8 @@ class TTreeReader: public TObject {

ROOT::Internal::TFriendProxy &AddFriendProxy(std::size_t friendIdx);

bool RegisterValueReader(ROOT::Internal::TTreeReaderValueBase* reader);
void DeregisterValueReader(ROOT::Internal::TTreeReaderValueBase* reader);
bool RegisterValueReader(ROOT::Internal::TTreeReaderValueBase *reader);
void DeregisterValueReader(ROOT::Internal::TTreeReaderValueBase *reader);

EEntryStatus SetEntryBase(Long64_t entry, bool local);

@@ -304,37 +294,38 @@ class TTreeReader: public TObject {
std::string GetProxyKey(const char *branchname)
{
std::string key(branchname);
//key += reinterpret_cast<std::uintptr_t>(fTree);
// key += reinterpret_cast<std::uintptr_t>(fTree);
return key;
}

enum EStatusBits {
kBitIsChain = BIT(14), ///< our tree is a chain
kBitHaveWarnedAboutEntryListAttachedToTTree = BIT(15), ///< the tree had a TEntryList and we have warned about that
kBitHaveWarnedAboutEntryListAttachedToTTree =
BIT(15), ///< the tree had a TEntryList and we have warned about that
kBitSetEntryBaseCallingLoadTree = BIT(16), ///< SetEntryBase is in the process of calling TChain/TTree::%LoadTree.
kBitIsExternalTree = BIT(17) ///< we do not own the tree
kBitIsExternalTree = BIT(17) ///< we do not own the tree
};

TTree* fTree = nullptr; ///< tree that's read
TEntryList* fEntryList = nullptr; ///< entry list to be used
TTree *fTree = nullptr; ///< tree that's read
TEntryList *fEntryList = nullptr; ///< entry list to be used
EEntryStatus fEntryStatus = kEntryNotLoaded; ///< status of most recent read request
ELoadTreeStatus fLoadTreeStatus = kNoTree; ///< Indicator on how LoadTree was called 'last' time.
/// TTree and TChain will notify this object upon LoadTree, leading to a call to TTreeReader::Notify().
TNotifyLink<TTreeReader> fNotify;
std::unique_ptr<ROOT::Internal::TBranchProxyDirector> fDirector{nullptr}; ///< proxying director
/// Proxies to friend trees, created in TTreeReader[Value,Array]::CreateProxy
std::vector<std::unique_ptr<ROOT::Internal::TFriendProxy>> fFriendProxies;
std::deque<ROOT::Internal::TTreeReaderValueBase*> fValues; ///< readers that use our director
NamedProxies_t fProxies; ///< attached ROOT::TNamedBranchProxies; owned
std::deque<ROOT::Internal::TTreeReaderValueBase *> fValues; ///< readers that use our director
NamedProxies_t fProxies; ///< attached ROOT::TNamedBranchProxies; owned

Long64_t fEntry = -1; ///< Current (non-local) entry of fTree or of fEntryList if set.

/// The end of the entry loop. When set (i.e. >= 0), it provides a way
/// to stop looping over the TTree when we reach a certain entry: Next()
/// returns false when GetCurrentEntry() reaches fEndEntry.
Long64_t fEndEntry = -1LL;
Long64_t fBeginEntry = 0LL; ///< This allows us to propagate the range to the TTreeCache
bool fProxiesSet = false; ///< True if the proxies have been set, false otherwise
Long64_t fBeginEntry = 0LL; ///< This allows us to propagate the range to the TTreeCache
bool fProxiesSet = false; ///< True if the proxies have been set, false otherwise
bool fSetEntryBaseCallingLoadTree = false; ///< True if during the LoadTree execution triggered by SetEntryBase.

// Flag to activate or deactivate warnings in case the friend trees have
54 changes: 27 additions & 27 deletions tree/treeplayer/inc/TTreeReaderArray.h
Original file line number Diff line number Diff line change
@@ -12,9 +12,6 @@
#ifndef ROOT_TTreeReaderArray
#define ROOT_TTreeReaderArray




#include "TTreeReaderValue.h"
#include "TTreeReaderUtils.h"
#include <type_traits>
@@ -26,32 +23,31 @@ namespace Internal {
Base class of TTreeReaderArray.
*/

class TTreeReaderArrayBase: public TTreeReaderValueBase {
public:
TTreeReaderArrayBase(TTreeReader* reader, const char* branchname,
TDictionary* dict):
TTreeReaderValueBase(reader, branchname, dict) {}
class TTreeReaderArrayBase : public TTreeReaderValueBase {
public:
TTreeReaderArrayBase(TTreeReader *reader, const char *branchname, TDictionary *dict)
: TTreeReaderValueBase(reader, branchname, dict)
{
}

std::size_t GetSize() const { return fImpl ? fImpl->GetSize(GetProxy()) : 0; }
bool IsEmpty() const { return !GetSize(); }
std::size_t GetSize() const { return fImpl ? fImpl->GetSize(GetProxy()) : 0; }
bool IsEmpty() const { return !GetSize(); }

EReadStatus GetReadStatus() const override { return fImpl ? fImpl->fReadStatus : kReadError; }
EReadStatus GetReadStatus() const override { return fImpl ? fImpl->fReadStatus : kReadError; }

protected:
void *UntypedAt(std::size_t idx) const { return fImpl->At(GetProxy(), idx); }
void CreateProxy() override;
bool GetBranchAndLeaf(TBranch *&branch, TLeaf *&myLeaf, TDictionary *&branchActualType,
bool suppressErrorsForMissingBranch = false);
void SetImpl(TBranch* branch, TLeaf* myLeaf);
const char* GetBranchContentDataType(TBranch* branch,
TString& contentTypeName,
TDictionary* &dict);
protected:
void *UntypedAt(std::size_t idx) const { return fImpl->At(GetProxy(), idx); }
void CreateProxy() override;
bool GetBranchAndLeaf(TBranch *&branch, TLeaf *&myLeaf, TDictionary *&branchActualType,
bool suppressErrorsForMissingBranch = false);
void SetImpl(TBranch *branch, TLeaf *myLeaf);
const char *GetBranchContentDataType(TBranch *branch, TString &contentTypeName, TDictionary *&dict);

std::unique_ptr<TVirtualCollectionReader> fImpl; // Common interface to collections
std::unique_ptr<TVirtualCollectionReader> fImpl; // Common interface to collections

// FIXME: re-introduce once we have ClassDefInline!
//ClassDefOverride(TTreeReaderArrayBase, 0);//Accessor to member of an object stored in a collection
};
// FIXME: re-introduce once we have ClassDefInline!
// ClassDefOverride(TTreeReaderArrayBase, 0);//Accessor to member of an object stored in a collection
};

} // namespace Internal
} // namespace ROOT
@@ -73,7 +69,7 @@ Base class of TTreeReaderArray.

template <typename T>
class R__CLING_PTRCHECK(off) TTreeReaderArray final : public ROOT::Internal::TTreeReaderArrayBase {
// R__CLING_PTRCHECK is disabled because pointer / types are checked by CreateProxy().
// R__CLING_PTRCHECK is disabled because pointer / types are checked by CreateProxy().

public:
/// Random access iterator to the elements of a TTreeReaderArray.
@@ -106,7 +102,9 @@ class R__CLING_PTRCHECK(off) TTreeReaderArray final : public ROOT::Internal::TTr

/// Construct iterator from a const TTreeReaderArray
Iterator_t(std::size_t index, const TTreeReaderArray *array)
: Iterator_t(index, const_cast<TTreeReaderArray *>(array)) {}
: Iterator_t(index, const_cast<TTreeReaderArray *>(array))
{
}

Iterator_t(Iterator_t &&) = default;
Iterator_t(const Iterator_t &) = default;
@@ -200,7 +198,9 @@ class R__CLING_PTRCHECK(off) TTreeReaderArray final : public ROOT::Internal::TTr

/// Create an array reader of branch "branchname" for TTreeReader "tr".
TTreeReaderArray(TTreeReader &tr, const char *branchname)
: TTreeReaderArrayBase(&tr, branchname, TDictionary::GetDictionary(typeid(T))) {}
: TTreeReaderArrayBase(&tr, branchname, TDictionary::GetDictionary(typeid(T)))
{
}

T &At(std::size_t idx) { return *static_cast<T *>(UntypedAt(idx)); }
const T &At(std::size_t idx) const { return *static_cast<T *>(UntypedAt(idx)); }
262 changes: 134 additions & 128 deletions tree/treeplayer/inc/TTreeReaderValue.h
Original file line number Diff line number Diff line change
@@ -42,144 +42,149 @@ namespace Internal {
Base class of TTreeReaderValue.
*/

class TTreeReaderValueBase {
public:

/// Status flags, 0 is good
enum ESetupStatus {
kSetupNotSetup = -7, ///< No initialization has happened yet.
kSetupTreeDestructed = -8, ///< The TTreeReader has been destructed / not set.
kSetupMakeClassModeMismatch = -9, ///< readers disagree on whether TTree::SetMakeBranch() should be on
kSetupMissingCounterBranch = -6, ///< The array cannot find its counter branch: Array[CounterBranch]
kSetupMissingBranch = -5, ///< The specified branch cannot be found.
kSetupInternalError = -4, ///< Some other error - hopefully the error message helps.
kSetupMissingDictionary = -3, ///< To read this branch, we need a dictionary.
kSetupMismatch = -2, ///< Mismatch of branch type and reader template type.
kSetupNotACollection = -1, ///< The branch class type is not a collection.
kSetupMatch = 0, ///< This branch has been set up, branch data type and reader template type match, reading should succeed.
kSetupMatchBranch = 7, ///< This branch has been set up, branch data type and reader template type match, reading should succeed.
//kSetupMatchConversion = 1, /// This branch has been set up, the branch data type can be converted to the reader template type, reading should succeed.
//kSetupMatchConversionCollection = 2, /// This branch has been set up, the data type of the branch's collection elements can be converted to the reader template type, reading should succeed.
//kSetupMakeClass = 3, /// This branch has been set up, enabling MakeClass mode for it, reading should succeed.
// kSetupVoidPtr = 4,
kSetupNoCheck = 5,
kSetupMatchLeaf = 6 ///< This branch (or TLeaf, really) has been set up, reading should succeed.
};
enum EReadStatus {
kReadSuccess = 0, ///< Data read okay
kReadNothingYet, ///< Data now yet accessed
kReadError ///< Problem reading data
};

EReadStatus ProxyRead() { return (this->*fProxyReadFunc)(); }

EReadStatus ProxyReadDefaultImpl();

typedef bool (ROOT::Detail::TBranchProxy::*BranchProxyRead_t)();
template <BranchProxyRead_t Func>
ROOT::Internal::TTreeReaderValueBase::EReadStatus ProxyReadTemplate();

/// Return true if the branch was setup \em and \em read correctly.
/// Use GetSetupStatus() to only check the setup status.
bool IsValid() const { return fProxy && 0 == (int)fSetupStatus && 0 == (int)fReadStatus; }
/// Return this TTreeReaderValue's setup status.
/// Use this method to check e.g. whether the TTreeReaderValue is correctly setup and ready for reading.
ESetupStatus GetSetupStatus() const { return fSetupStatus; }
virtual EReadStatus GetReadStatus() const { return fReadStatus; }

/// If we are reading a leaf, return the corresponding TLeaf.
TLeaf* GetLeaf() { return fLeaf; }

void* GetAddress();

const char* GetBranchName() const { return fBranchName; }

virtual ~TTreeReaderValueBase();

protected:
TTreeReaderValueBase(TTreeReader *reader, const char *branchname, TDictionary *dict, bool opaqueRead = false);
TTreeReaderValueBase(const TTreeReaderValueBase&);
TTreeReaderValueBase& operator=(const TTreeReaderValueBase&);

void RegisterWithTreeReader();
void NotifyNewTree(TTree* newTree);

TBranch* SearchBranchWithCompositeName(TLeaf *&myleaf, TDictionary *&branchActualType, std::string &err);
virtual void CreateProxy();
static const char* GetBranchDataType(TBranch* branch,
TDictionary* &dict,
TDictionary const *curDict);

virtual const char* GetDerivedTypeName() const = 0;

Detail::TBranchProxy* GetProxy() const { return fProxy; }

void MarkTreeReaderUnavailable() { fTreeReader = nullptr; fSetupStatus = kSetupTreeDestructed; }

/// Stringify the template argument.
static std::string GetElementTypeName(const std::type_info& ti);

void ErrorAboutMissingProxyIfNeeded();

bool fHaveLeaf : 1; ///< Whether the data is in a leaf
bool fHaveStaticClassOffsets : 1; ///< Whether !fStaticClassOffsets.empty()
EReadStatus fReadStatus : 2; ///< Read status of this data access
ESetupStatus fSetupStatus = kSetupNotSetup; ///< Setup status of this data access
TString fBranchName; ///< Name of the branch to read data from.
TString fLeafName;
TTreeReader* fTreeReader; ///< Tree reader we belong to
TDictionary* fDict; ///< Type that the branch should contain
Detail::TBranchProxy* fProxy = nullptr; ///< Proxy for this branch, owned by TTreeReader
TLeaf* fLeaf = nullptr;
std::vector<Long64_t> fStaticClassOffsets;
typedef EReadStatus (TTreeReaderValueBase::*Read_t)();
Read_t fProxyReadFunc = &TTreeReaderValueBase::ProxyReadDefaultImpl; ///<! Pointer to the Read implementation to use.
/**
* If true, the reader will not do any type-checking against the actual
* type held by the branch. Useful to just check if the current entry can
* be read or not without caring about its value.
* \note Only used by TTreeReaderOpaqueValue.
*/
bool fOpaqueRead{false};

// FIXME: re-introduce once we have ClassDefInline!
//ClassDefOverride(TTreeReaderValueBase, 0);//Base class for accessors to data via TTreeReader

friend class ::TTreeReader;
class TTreeReaderValueBase {
public:
/// Status flags, 0 is good
enum ESetupStatus {
kSetupNotSetup = -7, ///< No initialization has happened yet.
kSetupTreeDestructed = -8, ///< The TTreeReader has been destructed / not set.
kSetupMakeClassModeMismatch = -9, ///< readers disagree on whether TTree::SetMakeBranch() should be on
kSetupMissingCounterBranch = -6, ///< The array cannot find its counter branch: Array[CounterBranch]
kSetupMissingBranch = -5, ///< The specified branch cannot be found.
kSetupInternalError = -4, ///< Some other error - hopefully the error message helps.
kSetupMissingDictionary = -3, ///< To read this branch, we need a dictionary.
kSetupMismatch = -2, ///< Mismatch of branch type and reader template type.
kSetupNotACollection = -1, ///< The branch class type is not a collection.
kSetupMatch =
0, ///< This branch has been set up, branch data type and reader template type match, reading should succeed.
kSetupMatchBranch =
7, ///< This branch has been set up, branch data type and reader template type match, reading should succeed.
// kSetupMatchConversion = 1, /// This branch has been set up, the branch data type can be converted to the reader
// template type, reading should succeed. kSetupMatchConversionCollection = 2, /// This branch has been set up,
// the data type of the branch's collection elements can be converted to the reader template type, reading should
// succeed. kSetupMakeClass = 3, /// This branch has been set up, enabling MakeClass mode for it, reading should
// succeed.
// kSetupVoidPtr = 4,
kSetupNoCheck = 5,
kSetupMatchLeaf = 6 ///< This branch (or TLeaf, really) has been set up, reading should succeed.
};
enum EReadStatus {
kReadSuccess = 0, ///< Data read okay
kReadNothingYet, ///< Data now yet accessed
kReadError ///< Problem reading data
};

EReadStatus ProxyRead() { return (this->*fProxyReadFunc)(); }

EReadStatus ProxyReadDefaultImpl();

typedef bool (ROOT::Detail::TBranchProxy::*BranchProxyRead_t)();
template <BranchProxyRead_t Func>
ROOT::Internal::TTreeReaderValueBase::EReadStatus ProxyReadTemplate();

/// Return true if the branch was setup \em and \em read correctly.
/// Use GetSetupStatus() to only check the setup status.
bool IsValid() const { return fProxy && 0 == (int)fSetupStatus && 0 == (int)fReadStatus; }
/// Return this TTreeReaderValue's setup status.
/// Use this method to check e.g. whether the TTreeReaderValue is correctly setup and ready for reading.
ESetupStatus GetSetupStatus() const { return fSetupStatus; }
virtual EReadStatus GetReadStatus() const { return fReadStatus; }

/// If we are reading a leaf, return the corresponding TLeaf.
TLeaf *GetLeaf() { return fLeaf; }

void *GetAddress();

const char *GetBranchName() const { return fBranchName; }

virtual ~TTreeReaderValueBase();

protected:
TTreeReaderValueBase(TTreeReader *reader, const char *branchname, TDictionary *dict, bool opaqueRead = false);
TTreeReaderValueBase(const TTreeReaderValueBase &);
TTreeReaderValueBase &operator=(const TTreeReaderValueBase &);

void RegisterWithTreeReader();
void NotifyNewTree(TTree *newTree);

TBranch *SearchBranchWithCompositeName(TLeaf *&myleaf, TDictionary *&branchActualType, std::string &err);
virtual void CreateProxy();
static const char *GetBranchDataType(TBranch *branch, TDictionary *&dict, TDictionary const *curDict);

virtual const char *GetDerivedTypeName() const = 0;

Detail::TBranchProxy *GetProxy() const { return fProxy; }

void MarkTreeReaderUnavailable()
{
fTreeReader = nullptr;
fSetupStatus = kSetupTreeDestructed;
}

/// Stringify the template argument.
static std::string GetElementTypeName(const std::type_info &ti);

void ErrorAboutMissingProxyIfNeeded();

bool fHaveLeaf : 1; ///< Whether the data is in a leaf
bool fHaveStaticClassOffsets : 1; ///< Whether !fStaticClassOffsets.empty()
EReadStatus fReadStatus : 2; ///< Read status of this data access
ESetupStatus fSetupStatus = kSetupNotSetup; ///< Setup status of this data access
TString fBranchName; ///< Name of the branch to read data from.
TString fLeafName;
TTreeReader *fTreeReader; ///< Tree reader we belong to
TDictionary *fDict; ///< Type that the branch should contain
Detail::TBranchProxy *fProxy = nullptr; ///< Proxy for this branch, owned by TTreeReader
TLeaf *fLeaf = nullptr;
std::vector<Long64_t> fStaticClassOffsets;
typedef EReadStatus (TTreeReaderValueBase::*Read_t)();
Read_t fProxyReadFunc = &TTreeReaderValueBase::ProxyReadDefaultImpl; ///<! Pointer to the Read implementation to use.
/**
* \brief Read a value in a branch without knowledge of its type
*
* This class is helpful in situations where the actual contents of the branch
* at the current entry are not relevant and one only wants to know whether
* the entry can be read.
* If true, the reader will not do any type-checking against the actual
* type held by the branch. Useful to just check if the current entry can
* be read or not without caring about its value.
* \note Only used by TTreeReaderOpaqueValue.
*/
class R__CLING_PTRCHECK(off) TTreeReaderOpaqueValue final : public ROOT::Internal::TTreeReaderValueBase {
public:
TTreeReaderOpaqueValue(TTreeReader &tr, const char *branchname)
: TTreeReaderValueBase(&tr, branchname, /*dict*/ nullptr, /*opaqueRead*/ true)
{
}
bool fOpaqueRead{false};

protected:
const char *GetDerivedTypeName() const { return ""; }
};
// FIXME: re-introduce once we have ClassDefInline!
// ClassDefOverride(TTreeReaderValueBase, 0);//Base class for accessors to data via TTreeReader

friend class ::TTreeReader;
};

/**
* \brief Read a value in a branch without knowledge of its type
*
* This class is helpful in situations where the actual contents of the branch
* at the current entry are not relevant and one only wants to know whether
* the entry can be read.
*/
class R__CLING_PTRCHECK(off) TTreeReaderOpaqueValue final : public ROOT::Internal::TTreeReaderValueBase {
public:
TTreeReaderOpaqueValue(TTreeReader &tr, const char *branchname)
: TTreeReaderValueBase(&tr, branchname, /*dict*/ nullptr, /*opaqueRead*/ true)
{
}

protected:
const char *GetDerivedTypeName() const { return ""; }
};

} // namespace Internal
} // namespace ROOT


template <typename T>
class R__CLING_PTRCHECK(off) TTreeReaderValue final: public ROOT::Internal::TTreeReaderValueBase {
// R__CLING_PTRCHECK is disabled because pointer / types are checked by CreateProxy().
class R__CLING_PTRCHECK(off) TTreeReaderValue final : public ROOT::Internal::TTreeReaderValueBase {
// R__CLING_PTRCHECK is disabled because pointer / types are checked by CreateProxy().

public:
using NonConstT_t = typename std::remove_const<T>::type;
TTreeReaderValue() = delete;
TTreeReaderValue(TTreeReader& tr, const char* branchname):
TTreeReaderValueBase(&tr, branchname,
TDictionary::GetDictionary(typeid(NonConstT_t))) {}
TTreeReaderValue(TTreeReader &tr, const char *branchname)
: TTreeReaderValueBase(&tr, branchname, TDictionary::GetDictionary(typeid(NonConstT_t)))
{
}

/// Return a pointer to the value of the current entry.
/// Return a nullptr and print an error if no entry has been loaded yet.
@@ -198,23 +203,24 @@ class R__CLING_PTRCHECK(off) TTreeReaderValue final: public ROOT::Internal::TTre

/// Return a pointer to the value of the current entry.
/// Equivalent to Get().
T* operator->() { return Get(); }
T *operator->() { return Get(); }

/// Return a reference to the value of the current entry.
/// Equivalent to dereferencing the pointer returned by Get(). Behavior is undefined if no entry has been loaded yet.
/// Most likely a crash will occur.
T& operator*() { return *Get(); }
T &operator*() { return *Get(); }

protected:
// FIXME: use IsA() instead once we have ClassDefTInline
/// Get the template argument as a string.
const char* GetDerivedTypeName() const override {
const char *GetDerivedTypeName() const override
{
static const std::string sElementTypeName = GetElementTypeName(typeid(T));
return sElementTypeName.data();
}

// FIXME: re-introduce once we have ClassDefTInline!
//ClassDefT(TTreeReaderValue, 0);//Accessor to data via TTreeReader
// ClassDefT(TTreeReaderValue, 0);//Accessor to data via TTreeReader
};

namespace cling {
74 changes: 36 additions & 38 deletions tree/treeplayer/src/TTreeReader.cxx
Original file line number Diff line number Diff line change
@@ -183,7 +183,7 @@ ClassImp(TTreeReader);
using namespace ROOT::Internal;

// Provide some storage for the poor little symbol.
constexpr const char * const TTreeReader::fgEntryStatusText[TTreeReader::kEntryUnknownError + 1];
constexpr const char *const TTreeReader::fgEntryStatusText[TTreeReader::kEntryUnknownError + 1];

////////////////////////////////////////////////////////////////////////////////
/// Default constructor. Call SetTree to connect to a TTree.
@@ -234,7 +234,8 @@ TTreeReader::TTreeReader(TTree *tree, TEntryList *entryList /*= nullptr*/, bool
TTreeReader::TTreeReader(const char *keyname, TDirectory *dir, TEntryList *entryList /*= nullptr*/)
: fEntryList(entryList), fNotify(this), fFriendProxies()
{
if (!dir) dir = gDirectory;
if (!dir)
dir = gDirectory;
dir->GetObject(keyname, fTree);
if (!fTree) {
std::string msg = "No TTree called ";
@@ -250,8 +251,8 @@ TTreeReader::TTreeReader(const char *keyname, TDirectory *dir, TEntryList *entry

TTreeReader::~TTreeReader()
{
for (std::deque<ROOT::Internal::TTreeReaderValueBase*>::const_iterator
i = fValues.begin(), e = fValues.end(); i != e; ++i) {
for (std::deque<ROOT::Internal::TTreeReaderValueBase *>::const_iterator i = fValues.begin(), e = fValues.end();
i != e; ++i) {
(*i)->MarkTreeReaderUnavailable();
}
if (fTree && fNotify.IsLinked())
@@ -335,10 +336,10 @@ bool TTreeReader::Notify()
// This means that "local" should be set!
// There are two entities switching trees which is bad.
Warning("SetEntryBase()",
"The current tree in the TChain %s has changed (e.g. by TTree::Process) "
"even though TTreeReader::SetEntry() was called, which switched the tree "
"again. Did you mean to call TTreeReader::SetLocalEntry()?",
fTree->GetName());
"The current tree in the TChain %s has changed (e.g. by TTree::Process) "
"even though TTreeReader::SetEntry() was called, which switched the tree "
"again. Did you mean to call TTreeReader::SetLocalEntry()?",
fTree->GetName());
}
fLoadTreeStatus = kInternalLoadTree;
} else {
@@ -360,7 +361,7 @@ bool TTreeReader::Notify()
}

if (fProxiesSet) {
for (auto value: fValues) {
for (auto value : fValues) {
value->NotifyNewTree(fTree->GetTree());
}
}
@@ -559,16 +560,16 @@ TTreeReader::EEntryStatus TTreeReader::SetEntriesRange(Long64_t beginEntry, Long
// thus adding all the branches to the cache and triggering the learning phase.
EEntryStatus es = SetEntry(beginEntry - 1);
if (es != kEntryValid) {
Error("SetEntriesRange()", "Error setting first entry %lld: %s",
beginEntry, fgEntryStatusText[(int)es]);
Error("SetEntriesRange()", "Error setting first entry %lld: %s", beginEntry, fgEntryStatusText[(int)es]);
return es;
}
}

return kEntryValid;
}

void TTreeReader::Restart() {
void TTreeReader::Restart()
{
fDirector->SetReadEntry(-1);
fProxiesSet = false; // we might get more value readers, meaning new proxies.
fEntry = -1;
@@ -585,16 +586,15 @@ void TTreeReader::Restart() {
/// of the TTree / TChain, independent of a range set by SetEntriesRange()
/// by calling TTree/TChain::%GetEntriesFast.


Long64_t TTreeReader::GetEntries() const {
Long64_t TTreeReader::GetEntries() const
{
if (fEntryList)
return fEntryList->GetN();
if (!fTree)
return -1;
return fTree->GetEntriesFast();
}


////////////////////////////////////////////////////////////////////////////////
/// Returns the number of entries of the TEntryList if one is provided, else
/// of the TTree / TChain, independent of a range set by SetEntriesRange().
@@ -603,7 +603,8 @@ Long64_t TTreeReader::GetEntries() const {
/// this TChain should be opened to determine the exact number of entries
/// of the TChain. If `!IsChain()`, `force` is ignored.

Long64_t TTreeReader::GetEntries(bool force) {
Long64_t TTreeReader::GetEntries(bool force)
{
if (fEntryList)
return fEntryList->GetN();
if (!fTree)
@@ -619,8 +620,6 @@ Long64_t TTreeReader::GetEntries(bool force) {
return fTree->GetEntriesFast();
}



////////////////////////////////////////////////////////////////////////////////
/// Load an entry into the tree, return the status of the read.
/// For chains, entry is the global (i.e. not tree-local) entry number, unless
@@ -663,7 +662,7 @@ TTreeReader::EEntryStatus TTreeReader::SetEntryBase(Long64_t entry, bool local)
}
}

TTree* treeToCallLoadOn = local ? fTree->GetTree() : fTree;
TTree *treeToCallLoadOn = local ? fTree->GetTree() : fTree;

fSetEntryBaseCallingLoadTree = true;
const Long64_t loadResult = treeToCallLoadOn->LoadTree(entryAfterList);
@@ -679,21 +678,20 @@ TTreeReader::EEntryStatus TTreeReader::SetEntryBase(Long64_t entry, bool local)
if (loadResult == -3 && TestBit(kBitIsChain) && !fTree->GetTree()) {
fDirector->Notify();
if (fProxiesSet) {
for (auto value: fValues) {
for (auto value : fValues) {
value->NotifyNewTree(fTree->GetTree());
}
}
Warning("SetEntryBase()",
"There was an issue opening the last file associated to the TChain "
"being processed.");
Warning("SetEntryBase()", "There was an issue opening the last file associated to the TChain "
"being processed.");
fEntryStatus = kEntryChainFileError;
return fEntryStatus;
}

if (loadResult == -2) {
fDirector->Notify();
if (fProxiesSet) {
for (auto value: fValues) {
for (auto value : fValues) {
value->NotifyNewTree(fTree->GetTree());
}
}
@@ -723,7 +721,7 @@ TTreeReader::EEntryStatus TTreeReader::SetEntryBase(Long64_t entry, bool local)
// the TTree is missing from the file.
fDirector->Notify();
if (fProxiesSet) {
for (auto value: fValues) {
for (auto value : fValues) {
value->NotifyNewTree(fTree->GetTree());
}
}
@@ -749,8 +747,7 @@ TTreeReader::EEntryStatus TTreeReader::SetEntryBase(Long64_t entry, bool local)
return fEntryStatus;
}

Warning("SetEntryBase()",
"Unexpected error '%lld' in %s::LoadTree", loadResult,
Warning("SetEntryBase()", "Unexpected error '%lld' in %s::LoadTree", loadResult,
treeToCallLoadOn->IsA()->GetName());

fEntryStatus = kEntryUnknownError;
@@ -813,7 +810,7 @@ TTreeReader::EEntryStatus TTreeReader::SetEntryBase(Long64_t entry, bool local)
/// Set (or update) the which tree to read from. `tree` can be
/// a TTree or a TChain.

void TTreeReader::SetTree(TTree* tree, TEntryList* entryList /*= nullptr*/)
void TTreeReader::SetTree(TTree *tree, TEntryList *entryList /*= nullptr*/)
{
if (fEntryStatus != kEntryNoTree && !TestBit(kBitIsExternalTree)) {
// a plain TTree is automatically added to the current directory,
@@ -838,8 +835,7 @@ void TTreeReader::SetTree(TTree* tree, TEntryList* entryList /*= nullptr*/)

if (!fDirector) {
Initialize();
}
else {
} else {
fDirector->SetTree(fTree);
fDirector->SetReadEntry(-1);
}
@@ -853,9 +849,9 @@ void TTreeReader::SetTree(TTree* tree, TEntryList* entryList /*= nullptr*/)
/// \param dir - the `TDirectory` to load `keyname` from (or gDirectory if `nullptr`)
/// \param entryList - the `TEntryList` to attach to the `TTreeReader`.

void TTreeReader::SetTree(const char* keyname, TDirectory* dir, TEntryList* entryList /*= nullptr*/)
void TTreeReader::SetTree(const char *keyname, TDirectory *dir, TEntryList *entryList /*= nullptr*/)
{
TTree* tree = nullptr;
TTree *tree = nullptr;
if (!dir)
dir = gDirectory;
dir->GetObject(keyname, tree);
@@ -865,11 +861,12 @@ void TTreeReader::SetTree(const char* keyname, TDirectory* dir, TEntryList* entr
////////////////////////////////////////////////////////////////////////////////
/// Add a value reader for this tree.

bool TTreeReader::RegisterValueReader(ROOT::Internal::TTreeReaderValueBase* reader)
bool TTreeReader::RegisterValueReader(ROOT::Internal::TTreeReaderValueBase *reader)
{
if (fProxiesSet) {
Error("RegisterValueReader",
"Error registering reader for %s: TTreeReaderValue/Array objects must be created before the call to Next() / SetEntry() / SetLocalEntry(), or after TTreeReader::Restart()!",
"Error registering reader for %s: TTreeReaderValue/Array objects must be created before the call to Next() "
"/ SetEntry() / SetLocalEntry(), or after TTreeReader::Restart()!",
reader->GetBranchName());
return false;
}
@@ -880,12 +877,13 @@ bool TTreeReader::RegisterValueReader(ROOT::Internal::TTreeReaderValueBase* read
////////////////////////////////////////////////////////////////////////////////
/// Remove a value reader for this tree.

void TTreeReader::DeregisterValueReader(ROOT::Internal::TTreeReaderValueBase* reader)
void TTreeReader::DeregisterValueReader(ROOT::Internal::TTreeReaderValueBase *reader)
{
std::deque<ROOT::Internal::TTreeReaderValueBase*>::iterator iReader
= std::find(fValues.begin(), fValues.end(), reader);
std::deque<ROOT::Internal::TTreeReaderValueBase *>::iterator iReader =
std::find(fValues.begin(), fValues.end(), reader);
if (iReader == fValues.end()) {
Error("DeregisterValueReader", "Cannot find reader of type %s for branch %s", reader->GetDerivedTypeName(), reader->fBranchName.Data());
Error("DeregisterValueReader", "Cannot find reader of type %s for branch %s", reader->GetDerivedTypeName(),
reader->fBranchName.Data());
return;
}
fValues.erase(iReader);
933 changes: 477 additions & 456 deletions tree/treeplayer/src/TTreeReaderArray.cxx

Large diffs are not rendered by default.

342 changes: 177 additions & 165 deletions tree/treeplayer/src/TTreeReaderValue.cxx

Large diffs are not rendered by default.

0 comments on commit 1e681c6

Please sign in to comment.