Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:52

0001 // @(#)root/tree:$Id$
0002 // Author: Brian Bockelman, 2017-06-13
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2017, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_TTreeReaderValueFast
0013 #define ROOT_TTreeReaderValueFast
0014 
0015 
0016 ////////////////////////////////////////////////////////////////////////////
0017 //                                                                        //
0018 // TTreeReaderValueFast                                                   //
0019 //                                                                        //
0020 // A simple interface for reading data from trees or chains.              //
0021 //                                                                        //
0022 //                                                                        //
0023 ////////////////////////////////////////////////////////////////////////////
0024 
0025 #include "ROOT/TTreeReaderFast.hxx"
0026 
0027 #include "TBranch.h"
0028 #include "TBufferFile.h"
0029 
0030 #include <type_traits>
0031 
0032 class TBranch;
0033 
0034 namespace ROOT {
0035 namespace Experimental {
0036 namespace Internal {
0037 
0038 /* All the common code shared by the fast reader templates.
0039  */
0040 class TTreeReaderValueFastBase {
0041    public:
0042       TTreeReaderValueFastBase(const TTreeReaderValueFastBase&) = delete;
0043 
0044       ROOT::Internal::TTreeReaderValueBase::ESetupStatus GetSetupStatus() const { return fSetupStatus; }
0045       virtual ROOT::Internal::TTreeReaderValueBase::EReadStatus GetReadStatus() const { return fReadStatus; }
0046 
0047       //////////////////////////////////////////////////////////////////////////////
0048       /// Construct a tree value reader and register it with the reader object.
0049       TTreeReaderValueFastBase(TTreeReaderFast* reader, const std::string &branchName) :
0050          fBranchName(branchName),
0051          fLeafName(branchName), // TODO: only support single-leaf branches for now.
0052          fTreeReader(reader),
0053          fBuffer(TBuffer::kWrite, 32*1024),
0054          fEvtIndex(reader->GetIndexRef())
0055       {
0056          if (fTreeReader) fTreeReader->RegisterValueReader(this);
0057       }
0058 
0059       Int_t GetEvents(Long64_t eventNum) {
0060           //printf("Getting events starting at %lld.  Current remaining is %d events with base %lld.\n", eventNum, fRemaining, fEventBase);
0061           if (fEventBase >= 0 && (fRemaining + fEventBase > eventNum)) {
0062              Int_t adjust = (eventNum - fEventBase);
0063              if (R__unlikely(Adjust(adjust) < 0)) {
0064                 //printf("Failed to adjust offset to mid-buffer.\n");
0065                 return -1;
0066              }
0067              fRemaining -= adjust;
0068           } else {
0069              fRemaining = fBranch->GetBulkRead().GetEntriesSerialized(eventNum, fBuffer);
0070              if (R__unlikely(fRemaining < 0)) {
0071                 fReadStatus = ROOT::Internal::TTreeReaderValueBase::kReadError;
0072                 //printf("Failed to retrieve entries from the branch.\n");
0073                 return -1;
0074              }
0075           }
0076           fEventBase = eventNum;
0077           //printf("After getting events, the base is %lld with %d remaining.\n", fEventBase, fRemaining);
0078           fReadStatus = ROOT::Internal::TTreeReaderValueBase::kReadSuccess;
0079           return fRemaining;
0080       }
0081 
0082       virtual const char *GetTypeName() {return "{UNDETERMINED}";}
0083 
0084 
0085    protected:
0086 
0087       // Adjust the current buffer offset forward N events.
0088       virtual Int_t Adjust(Int_t eventCount) {
0089          Int_t bufOffset = fBuffer.Length();
0090          fBuffer.SetBufferOffset(bufOffset + eventCount*GetSize());
0091          return 0;
0092       }
0093       virtual UInt_t GetSize() = 0;
0094 
0095       void MarkTreeReaderUnavailable() {
0096          fTreeReader = nullptr;
0097       }
0098 
0099       // Create the linkage between the TTreeReader's current tree and this ReaderValue
0100       // object.  After CreateProxy() is invoked, if fSetupStatus doesn't indicate an
0101       // error, then we are pointing toward a valid TLeaf in the current tree
0102       void CreateProxy();
0103 
0104       virtual ~TTreeReaderValueFastBase();
0105 
0106       // Returns the name of the branch type; will be used when the TBranch version to
0107       // detect between the compile-time and runtime type names.
0108       virtual const char *BranchTypeName() = 0;
0109 
0110       std::string  fBranchName;          // Name of the branch we should read from.
0111       std::string  fLeafName;            // The branch's leaf we should read from.  NOTE: currently only support single-leaf branches.
0112       TTreeReaderFast *fTreeReader{nullptr}; // Reader we belong to
0113       TBranch *    fBranch{nullptr};     // Actual branch object we are reading.
0114       TLeaf *      fLeaf{nullptr};       // Actual leaf we are reading.
0115       TBufferFile  fBuffer;              // Buffer object holding the current events.
0116       Int_t        fRemaining{0};        // Number of events remaining in the buffer.
0117       Int_t       &fEvtIndex;            // Current event index.
0118       Long64_t     fLastChainOffset{-1}; // Current chain in the TTree we are pointed at.
0119       Long64_t     fEventBase{-1};       // Event number of the current buffer position.
0120 
0121       ROOT::Internal::TTreeReaderValueBase::ESetupStatus fSetupStatus{ROOT::Internal::TTreeReaderValueBase::kSetupNotSetup}; // setup status of this data access
0122       ROOT::Internal::TTreeReaderValueBase::EReadStatus  fReadStatus{ROOT::Internal::TTreeReaderValueBase::kReadNothingYet}; // read status of this data access
0123 
0124       friend class ROOT::Experimental::TTreeReaderFast;
0125 };
0126 
0127 }  // Internal
0128 
0129 template <typename T>
0130 class TTreeReaderValueFast final : public ROOT::Experimental::Internal::TTreeReaderValueFastBase {
0131 
0132    public:
0133        TTreeReaderValueFast(TTreeReaderFast* reader, const std::string &branchname) : ROOT::Experimental::Internal::TTreeReaderValueFastBase(reader, branchname) {}
0134 
0135       T* Get() {
0136          return Deserialize(reinterpret_cast<char *>(reinterpret_cast<T*>(fBuffer.GetCurrent()) + fEvtIndex));
0137       }
0138       T* operator->() { return Get(); }
0139       T& operator*() { return *Get(); }
0140 
0141    protected:
0142       T* Deserialize(char *) {return nullptr;}
0143 
0144       const char *GetTypeName() override {return "{INCOMPLETE}";}
0145       UInt_t GetSize() override {return sizeof(T);}
0146 };
0147 
0148 template<>
0149 class TTreeReaderValueFast<float> final : public ROOT::Experimental::Internal::TTreeReaderValueFastBase {
0150 
0151    public:
0152 
0153       TTreeReaderValueFast(TTreeReaderFast& tr, const std::string &branchname) :
0154             TTreeReaderValueFastBase(&tr, branchname) {}
0155 
0156       float* Get() {
0157          return Deserialize(reinterpret_cast<char *>(reinterpret_cast<float*>(fBuffer.GetCurrent()) + fEvtIndex));
0158       }
0159       float* operator->() { return Get(); }
0160       float& operator*() { return *Get(); }
0161 
0162    protected:
0163       const char *GetTypeName() override {return "float";}
0164       const char *BranchTypeName() override {return "float";}
0165       UInt_t GetSize() override {return sizeof(float);}
0166       float * Deserialize(char *input) {frombuf(input, &fTmp); return &fTmp;}
0167 
0168       float fTmp;
0169 };
0170 
0171 template <>
0172 class TTreeReaderValueFast<double> final : public ROOT::Experimental::Internal::TTreeReaderValueFastBase {
0173 
0174    public:
0175 
0176       TTreeReaderValueFast(TTreeReaderFast& tr, const std::string &branchname) :
0177             TTreeReaderValueFastBase(&tr, branchname) {}
0178 
0179       // TODO: why isn't template specialization working here?
0180       double* Get() {
0181          //printf("Double: Attempting to deserialize buffer %p from index %d.\n", fBuffer.GetCurrent(), fEvtIndex);
0182          return Deserialize(reinterpret_cast<char *>(reinterpret_cast<double*>(fBuffer.GetCurrent()) + fEvtIndex));
0183       }
0184       double* operator->() { return Get(); }
0185       double& operator*() { return *Get(); }
0186 
0187    protected:
0188       const char *GetTypeName() override {return "double";}
0189       const char *BranchTypeName() override {return "double";}
0190       UInt_t GetSize() override {return sizeof(double);}
0191       double* Deserialize(char *input) {frombuf(input, &fTmp); return &fTmp;}
0192 
0193       double fTmp;
0194 };
0195 
0196 template <>
0197 class TTreeReaderValueFast<Int_t> final : public ROOT::Experimental::Internal::TTreeReaderValueFastBase {
0198 
0199    public:
0200 
0201       TTreeReaderValueFast(TTreeReaderFast& tr, const std::string &branchname) :
0202             TTreeReaderValueFastBase(&tr, branchname) {}
0203 
0204       Int_t* Get() {
0205          return Deserialize(reinterpret_cast<char *>(reinterpret_cast<Int_t*>(fBuffer.GetCurrent()) + fEvtIndex));
0206       }
0207       Int_t* operator->() { return Get(); }
0208       Int_t& operator*() { return *Get(); }
0209 
0210    protected:
0211       const char *GetTypeName() override {return "integer";}
0212       const char *BranchTypeName() override {return "integer";}
0213       UInt_t GetSize() override {return sizeof(Int_t);}
0214       Int_t* Deserialize(char *input) {frombuf(input, &fTmp); return &fTmp;}
0215 
0216       Int_t fTmp;
0217 };
0218 
0219 template <>
0220 class TTreeReaderValueFast<UInt_t> final : public ROOT::Experimental::Internal::TTreeReaderValueFastBase {
0221 
0222    public:
0223 
0224       TTreeReaderValueFast(TTreeReaderFast& tr, const std::string &branchname) :
0225             TTreeReaderValueFastBase(&tr, branchname) {}
0226 
0227       UInt_t* Get() {
0228          return Deserialize(reinterpret_cast<char *>(reinterpret_cast<UInt_t*>(fBuffer.GetCurrent()) + fEvtIndex));
0229       }
0230       UInt_t* operator->() { return Get(); }
0231       UInt_t& operator*() { return *Get(); }
0232 
0233    protected:
0234       const char *GetTypeName() override {return "unsigned integer";}
0235       const char *BranchTypeName() override {return "unsigned integer";}
0236       UInt_t GetSize() override {return sizeof(UInt_t);}
0237       UInt_t* Deserialize(char *input) {frombuf(input, &fTmp); return &fTmp;}
0238 
0239       UInt_t fTmp;
0240 };
0241 
0242 template <>
0243 class TTreeReaderValueFast<bool> final : public ROOT::Experimental::Internal::TTreeReaderValueFastBase {
0244 
0245    public:
0246 
0247       TTreeReaderValueFast(TTreeReaderFast& tr, const std::string &branchname) :
0248             TTreeReaderValueFastBase(&tr, branchname) {}
0249 
0250       bool* Get() {
0251          return Deserialize(reinterpret_cast<char *>(reinterpret_cast<bool*>(fBuffer.GetCurrent()) + fEvtIndex));
0252       }
0253       bool* operator->() { return Get(); }
0254       bool& operator*() { return *Get(); }
0255 
0256    protected:
0257       const char *GetTypeName() override {return "unsigned integer";}
0258       const char *BranchTypeName() override {return "unsigned integer";}
0259       UInt_t GetSize() override {return sizeof(bool);}
0260       bool* Deserialize(char *input) {frombuf(input, &fTmp); return &fTmp;}
0261 
0262       bool fTmp;
0263 };
0264 
0265 }  // Experimental
0266 }  // ROOT
0267 
0268 #endif // ROOT_TTreeReaderValueFast