00001
00005 #ifndef __NTFILLITERATOR_H
00006 #define __NTFILLITERATOR_H
00007
00008 #include "jbltools/sfh/FillIterator.h"
00009
00010 #include <Rtypes.h>
00011 #include <TTree.h>
00012 #include <TBranch.h>
00013 #include <cassert>
00014 #include <cstring>
00015 #include <stdio.h>
00016 #include <iostream>
00017
00018
00020
00042 template<class Ntuple, class T = Int_t>
00043 class NTFillIterator: public FillIterator {
00044 public:
00046 NTFillIterator (const Ntuple& nt_,
00047 const char *branchname
00048 )
00049 : FillIterator (branchname ? branchname : "?"),
00050 nt(nt_),
00051 index (0)
00052 {
00053 TTree *t = nt.fChain;
00054 assert (t);
00055 TBranch *b = t->GetBranch (branchname);
00056 assert (b);
00057 branchAddress = reinterpret_cast<T *>(b->GetAddress());
00058 assert (branchAddress);
00059
00060 #if (DEBUG>1)
00061 std::cout << "NTFillIterator: created '" << getName() << "'\n";
00062 #endif
00063 }
00064
00066 NTFillIterator (const Ntuple& nt_,
00067 T Ntuple::* p_mem,
00068 const char *objectname = "?"
00069 )
00070 : FillIterator (objectname ? objectname : "?"),
00071 nt(nt_),
00072 index (0)
00073 {
00074 branchAddress = &(nt.*p_mem);
00075 assert (branchAddress);
00076 #if (DEBUG>1)
00077 std::cout << "NTFillIterator: created '" << getName() << "'\n";
00078 #endif
00079 }
00080
00082 virtual int operator() () const {
00083 return (index < *branchAddress) ? index : -1;
00084 }
00085
00087 virtual bool next() {
00088 return (++index < *branchAddress);
00089 }
00090
00092 virtual bool reset() {
00093 index = 0;
00094 return (*branchAddress > 0);
00095 }
00096
00098 virtual bool isValid() const {
00099 return (index >= 0) && (index < *branchAddress);
00100 }
00101
00102 protected:
00104 virtual ~NTFillIterator() {}
00105
00106
00107 const Ntuple& nt;
00108 const T *branchAddress;
00109 int index;
00110 };
00111
00112
00117 template<class Ntuple, class T>
00118 NTFillIterator<Ntuple, T>& ntfilliterator (const Ntuple& nt,
00119 T Ntuple::* p_mem,
00120 const char *objectname = "?"
00121 ) {
00122 return *new NTFillIterator<Ntuple, T> (nt, p_mem, objectname);
00123 }
00124
00125 #endif