15 #include "ROOT/RArrayView.hxx" 50 class TLoopManager :
public std::enable_shared_from_this<TLoopManager> {
59 std::shared_ptr<TTree> fTree{
nullptr};
62 const unsigned int fNSlots{0};
63 bool fHasRunAtLeastOnce{
false};
64 unsigned int fNChildren{0};
65 unsigned int fNStopsReceived{0};
67 void RunAndCheckFilters(
unsigned int slot,
Long64_t entry);
78 std::shared_ptr<TLoopManager>
GetSharedPtr() {
return shared_from_this(); }
79 const ColumnNames_t &GetDefaultBranches()
const;
81 TTree *GetTree()
const;
83 const std::map<std::string, TmpBranchBasePtr_t> &
GetBookedBranches()
const {
return fBookedBranches; }
89 void Book(
const std::shared_ptr<bool> &branchPtr);
91 bool CheckFilters(
int,
unsigned int);
129 template <
typename T>
135 std::unique_ptr<TTreeReaderValue<T>> fReaderValue{
nullptr};
137 std::unique_ptr<TTreeReaderArray<ProxyParam_t>> fReaderArray{
nullptr};
140 T *fValuePtr{
nullptr};
142 unsigned int fSlot{0};
152 bool useReaderValue = std::is_same<ProxyParam_t, T>::value;
159 template <
typename U =
T,
160 typename std::enable_if<std::is_same<typename TColumnValue<U>::ProxyParam_t, U>::value,
int>::type = 0>
163 template <typename U = T, typename std::enable_if<!std::is_same<ProxyParam_t, U>::value,
int>::type = 0>
166 auto &readerArray = *fReaderArray;
167 if (readerArray.GetSize() > 1 && 1 != (&readerArray[1] - &readerArray[0])) {
168 std::string exceptionText =
"Branch ";
169 exceptionText += fReaderArray->GetBranchName();
170 exceptionText +=
" hangs from a non-split branch. For this reason, it cannot be accessed via an array_view." 171 " Please read the top level branch instead.";
172 throw std::runtime_error(exceptionText.c_str());
175 return std::array_view<ProxyParam_t>(fReaderArray->begin(), fReaderArray->end());
180 fReaderValue =
nullptr;
181 fReaderArray =
nullptr;
183 fTmpColumn =
nullptr;
188 template <
typename T>
192 template <
typename... BranchTypes>
194 using type = std::tuple<TColumnValue<BranchTypes>...>;
197 template <
typename BranchType>
211 virtual void Run(
unsigned int slot,
Long64_t entry) = 0;
212 virtual void Init(
TTreeReader *
r,
unsigned int slot) = 0;
216 template <
typename Helper,
typename PrevDataFrame,
typename BranchTypes_t =
typename Helper::BranchTypes_t>
218 using TypeInd_t =
typename TGenStaticSeq<BranchTypes_t::fgSize>::Type_t;
223 std::vector<TDFValueTuple_t<BranchTypes_t>>
fValues;
226 TAction(Helper &&
h,
const ColumnNames_t &bl, PrevDataFrame &pd)
228 fPrevData(pd), fValues(fNSlots)
236 InitTDFValues(slot, fValues[slot],
r, fBranches, fTmpBranches, fImplPtr->GetBookedBranches(),
TypeInd_t());
237 fHelper.Init(
r, slot);
243 if (fPrevData.CheckFilters(slot, entry)) Exec(slot, entry,
TypeInd_t());
250 fHelper.Exec(slot, std::get<S>(fValues[slot]).Get(entry)...);
268 unsigned int fNChildren{0};
269 unsigned int fNStopsReceived{0};
275 virtual void Init(
TTreeReader *
r,
unsigned int slot) = 0;
276 virtual void *GetValuePtr(
unsigned int slot) = 0;
277 virtual const std::type_info &GetTypeId()
const = 0;
278 virtual bool CheckFilters(
unsigned int slot,
Long64_t entry) = 0;
280 virtual void Report()
const = 0;
281 virtual void PartialReport()
const = 0;
283 ColumnNames_t GetTmpBranches()
const;
284 virtual void Update(
unsigned int slot,
Long64_t entry) = 0;
286 virtual void StopProcessing() = 0;
290 template <
typename F,
typename PrevData>
293 using TypeInd_t =
typename TDFInternal::TGenStaticSeq<BranchTypes_t::fgSize>::Type_t;
294 using Ret_t =
typename TDFInternal::TFunctionTraits<F>::Ret_t;
300 std::vector<Long64_t> fLastCheckedEntry = {-1};
302 std::vector<TDFInternal::TDFValueTuple_t<BranchTypes_t>>
fValues;
307 fExpression(
std::move(expression)), fBranches(bl), fLastResultPtr(fNSlots), fPrevData(pd),
308 fLastCheckedEntry(fNSlots, -1), fValues(fNSlots)
311 []() {
return std::unique_ptr<Ret_t>(
new Ret_t()); });
312 fTmpBranches.emplace_back(name);
319 TDFInternal::InitTDFValues(slot, fValues[slot],
r, fBranches, fTmpBranches, fImplPtr->GetBookedBranches(),
323 void *
GetValuePtr(
unsigned int slot)
final {
return static_cast<void *
>(fLastResultPtr[slot].get()); }
327 if (entry != fLastCheckedEntry[slot]) {
330 fLastCheckedEntry[slot] = entry;
339 return fPrevData.CheckFilters(slot, entry);
342 template <
int...
S,
typename... BranchTypes>
344 TDFInternal::TTypeList<BranchTypes...>)
346 *fLastResultPtr[slot] = fExpression(std::get<S>(fValues[slot]).Get(entry)...);
351 void Report() const final { fPrevData.PartialReport(); }
358 if (fNStopsReceived == fNChildren) fPrevData.StopProcessing();
367 std::vector<Long64_t> fLastCheckedEntry = {-1};
368 std::vector<int> fLastResult = {
true};
369 std::vector<ULong64_t> fAccepted = {0};
370 std::vector<ULong64_t> fRejected = {0};
372 unsigned int fNChildren{0};
373 unsigned int fNStopsReceived{0};
379 virtual void Init(
TTreeReader *
r,
unsigned int slot) = 0;
380 virtual bool CheckFilters(
unsigned int slot,
Long64_t entry) = 0;
381 virtual void Report()
const = 0;
382 virtual void PartialReport()
const = 0;
384 ColumnNames_t GetTmpBranches()
const;
385 bool HasName()
const;
386 void PrintReport()
const;
388 virtual void StopProcessing() = 0;
390 virtual void ResetReportCount() = 0;
393 template <
typename FilterF,
typename PrevDataFrame>
395 using BranchTypes_t =
typename TDFInternal::TFunctionTraits<FilterF>::Args_t;
396 using TypeInd_t =
typename TDFInternal::TGenStaticSeq<BranchTypes_t::fgSize>::Type_t;
401 std::vector<TDFInternal::TDFValueTuple_t<BranchTypes_t>>
fValues;
404 TFilter(FilterF &&
f,
const ColumnNames_t &bl, PrevDataFrame &pd, std::string_view
name =
"")
406 fPrevData(pd), fValues(fNSlots)
414 if (entry != fLastCheckedEntry[slot]) {
415 if (!fPrevData.CheckFilters(slot, entry)) {
417 fLastResult[slot] =
false;
420 auto passed = CheckFilterHelper(slot, entry,
TypeInd_t());
421 passed ? ++fAccepted[slot] : ++fRejected[slot];
422 fLastResult[slot] = passed;
424 fLastCheckedEntry[slot] = entry;
426 return fLastResult[slot];
432 return fFilter(std::get<S>(fValues[slot]).Get(entry)...);
437 TDFInternal::InitTDFValues(slot, fValues[slot],
r, fBranches, fTmpBranches, fImplPtr->GetBookedBranches(),
442 void Report() const final { PartialReport(); }
446 fPrevData.PartialReport();
453 if (fNStopsReceived == fNChildren) fPrevData.StopProcessing();
458 assert(!fName.empty());
460 std::fill(fAccepted.begin(), fAccepted.end(), 0);
461 std::fill(fRejected.begin(), fRejected.end(), 0);
474 bool fLastResult{
true};
476 unsigned int fNChildren{0};
477 unsigned int fNStopsReceived{0};
478 bool fHasStopped{
false};
482 TRangeBase(
TLoopManager *implPtr,
const ColumnNames_t &tmpBranches,
unsigned int start,
unsigned int stop,
483 unsigned int stride,
unsigned int nSlots);
486 ColumnNames_t GetTmpBranches()
const;
487 virtual bool CheckFilters(
unsigned int slot,
Long64_t entry) = 0;
488 virtual void Report()
const = 0;
489 virtual void PartialReport()
const = 0;
491 virtual void StopProcessing() = 0;
495 template <
typename PrevData>
500 TRange(
unsigned int start,
unsigned int stop,
unsigned int stride, PrevData &pd)
501 :
TRangeBase(pd.GetImplPtr(), pd.GetTmpBranches(), start, stop, stride, pd.
GetNSlots()), fPrevData(pd)
512 }
else if (entry != fLastCheckedEntry) {
513 if (!fPrevData.CheckFilters(slot, entry)) {
518 ++fNProcessedEntries;
519 if (fNProcessedEntries <= fStart || (fStop > 0 && fNProcessedEntries > fStop) ||
520 (fStride != 1 && fNProcessedEntries % fStride != 0))
524 if (fNProcessedEntries == fStop) {
526 fPrevData.StopProcessing();
529 fLastCheckedEntry = entry;
536 void Report() const final { fPrevData.PartialReport(); }
543 if (fNStopsReceived == fNChildren && !fHasStopped) fPrevData.StopProcessing();
552 template <
typename T>
557 fTmpColumn = tmpColumn;
559 throw std::runtime_error(std::string(
"TColumnValue: type specified is ") +
typeid(
T).
name() +
560 " but temporary column has type " + tmpColumn->
GetTypeId().name());
561 fValuePtr =
static_cast<T *
>(tmpColumn->
GetValuePtr(slot));
569 template <
typename T>
570 template <
typename U,
571 typename std::enable_if<std::is_same<typename ROOT::Internal::TDF::TColumnValue<U>::ProxyParam_t, U>::value,
576 return *(fReaderValue->Get());
578 fTmpColumn->Update(fSlot, entry);
583 #endif // ROOT_TDFNODES typename TTDFValueTuple< BranchType >::type TDFValueTuple_t
std::string GetName(const std::string &scope_name)
RangeBaseVec_t fBookedRanges
virtual ~TCustomColumnBase()
FilterBaseVec_t fBookedFilters
bool CheckFilters(unsigned int slot, Long64_t entry) final
const std::type_info & GetTypeId() const
TTreeReader is a simple, robust and fast interface to read values from a TTree, TChain or TNtuple...
void Init(TTreeReader *r, unsigned int slot) final
Namespace for new ROOT classes and functions.
PrevDataFrame & fPrevData
std::array_view< ProxyParam_t > Get(Long64_t)
std::vector< TDFValueTuple_t< BranchTypes_t > > fValues
typename std::conditional< std::is_same< ReaderValueOrArray_t< T >, TTreeReaderValue< T >>::value, T, ExtractType_t< T >>::type ProxyParam_t
unsigned int GetNSlots() const
void PartialReport() const final
std::vector< std::unique_ptr< Ret_t > > fLastResultPtr
void Update(unsigned int slot, Long64_t entry) final
void PartialReport() const
End of recursive chain of calls, does nothing.
const ColumnNames_t GetTmpBranches() const
std::vector< TDFInternal::TDFValueTuple_t< BranchTypes_t > > fValues
std::map< std::string, TmpBranchBasePtr_t > fBookedBranches
void generate(R &r, TH1D *h)
void Init(TTreeReader *r, unsigned int slot) final
const ColumnNames_t fBranches
TAction(Helper &&h, const ColumnNames_t &bl, PrevDataFrame &pd)
const ColumnNames_t fTmpBranches
void Report() const final
void MakeProxy(TTreeReader *r, const std::string &bn)
const std::map< std::string, TmpBranchBasePtr_t > & GetBookedBranches() const
std::shared_ptr< TFilterBase > FilterBasePtr_t
bool HasRunAtLeastOnce() const
typename TDFInternal::TFunctionTraits< FilterF >::Args_t BranchTypes_t
Helper class that updates and returns TTree branches as well as TDataFrame temporary columns...
typename TDFInternal::TFunctionTraits< F >::Ret_t Ret_t
bool CheckFilters(unsigned int slot, Long64_t entry) final
typename TDFInternal::TGenStaticSeq< BranchTypes_t::fgSize >::Type_t TypeInd_t
std::shared_ptr< TLoopManager > GetSharedPtr()
void SetTmpColumn(unsigned int slot, TCustomColumnBase *tmpColumn)
const ColumnNames_t fTmpBranches
typename TGenStaticSeq< BranchTypes_t::fgSize >::Type_t TypeInd_t
Extracts data from a TTree.
const ColumnNames_t fBranches
ActionBaseVec_t fBookedActions
PrevDataFrame & fPrevData
std::vector< FilterBasePtr_t > FilterBaseVec_t
TCustomColumn(std::string_view name, F &&expression, const ColumnNames_t &bl, PrevData &pd)
void PartialReport() const final
unsigned int GetNSlots() const
RooArgSet S(const RooAbsArg &v1)
TRange(unsigned int start, unsigned int stop, unsigned int stride, PrevData &pd)
bool CheckFilterHelper(unsigned int slot, Long64_t entry, TDFInternal::TStaticSeq< S... >)
const unsigned int fNSlots
Number of thread slots used by this node, inherited from parent node.
unsigned int GetNSlots() const
std::vector< RangeBasePtr_t > RangeBaseVec_t
std::vector< ActionBasePtr_t > ActionBaseVec_t
std::shared_ptr< TCustomColumnBase > TmpBranchBasePtr_t
TLoopManager * fImplPtr
A raw pointer to the TLoopManager at the root of this functional graph.
void Exec(unsigned int slot, Long64_t entry, TStaticSeq< S... >)
void SetTree(std::shared_ptr< TTree > tree)
std::vector< TDFInternal::TDFValueTuple_t< BranchTypes_t > > fValues
std::tuple< TColumnValue< BranchTypes >... > type
unsigned int GetNSlots() const
unsigned int GetNSlots() const
typename TDFInternal::TFunctionTraits< F >::Args_t BranchTypes_t
void Reset(Detail::TBranchProxy *x)
FilterBaseVec_t fBookedNamedFilters
TFilter(FilterF &&f, const ColumnNames_t &bl, PrevDataFrame &pd, std::string_view name="")
void Report() const final
ColumnNames_t fTmpBranches
virtual const std::type_info & GetTypeId() const =0
const unsigned int fNSlots
Number of thread slots used by this node, inherited from parent node.
Describe directory structure in memory.
unsigned long long ULong64_t
const unsigned int fNSlots
Number of thread slots used by this node.
void Run(unsigned int slot, Long64_t entry) final
ColumnNames_t fTmpBranches
TLoopManager * fImplPtr
A raw pointer to the TLoopManager at the root of this functional graph.
const ColumnNames_t fDefaultBranches
void * GetValuePtr(unsigned int slot) final
std::shared_ptr< TDFInternal::TActionBase > ActionBasePtr_t
TLoopManager * fImplPtr
A raw pointer to the TLoopManager at the root of this functional graph.
typedef void((*Func_t)())
Long64_t GetNEmptyEntries() const
typename TDFInternal::TGenStaticSeq< BranchTypes_t::fgSize >::Type_t TypeInd_t
void Report() const final
const ColumnNames_t fBranches
void PartialReport() const final
A TTree object has a header with a name and a title.
std::vector< std::shared_ptr< bool > > fResProxyReadiness
void Init(TTreeReader *r, unsigned int slot) final
const unsigned int fNSlots
Number of thread slots used by this node, inherited from parent node.
void UpdateHelper(unsigned int slot, Long64_t entry, TDFInternal::TStaticSeq< S... >, TDFInternal::TTypeList< BranchTypes... >)
TLoopManager * fImplPtr
A raw pointer to the TLoopManager at the root of this functional graph.
virtual void * GetValuePtr(unsigned int slot)=0
std::shared_ptr< TRangeBase > RangeBasePtr_t
bool CheckFilters(unsigned int slot, Long64_t entry) final
Ranges act as filters when it comes to selecting entries that downstream nodes should process...