Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members | Related Pages

NTFloatFun.h

Go to the documentation of this file.
00001 
00005 #ifndef __NTFLOATFUN_H
00006 #define __NTFLOATFUN_H
00007 
00008 #include "jbltools/sfh/FloatFun.h"
00009 #include "jbltools/sfh/IntFun.h"
00010 #include "jbltools/sfh/IntFunPoR.h"
00011  
00012 #include <Rtypes.h>     // declares Float_t
00013 #include <TTree.h>    
00014 #include <TBranch.h>    
00015 #include <cstring>   
00016  
00017  
00018 // Class NTFloatFun
00020 
00035 class NTFloatFun: public FloatFun {
00036   public:
00038     NTFloatFun (const std::string& name_       
00039                );
00040     
00042     NTFloatFun (const std::string& name_,      
00043                 const IntFun *iter_     
00044                );
00046     NTFloatFun (const std::string& name_,      
00047                 int offset_             
00048                );
00049     
00051     virtual NTFloatFun& operator[] (const IntFunPoR& iter_  
00052                                   ) const = 0;
00053     
00055     virtual NTFloatFun& operator[] (int offset_  
00056                                  ) const = 0;
00057     
00059     virtual const FillIterator *getIterator() const { 
00060       return iter ? iter->getIterator() : 0; 
00061     }
00062     
00063   protected:  
00065     virtual ~NTFloatFun() {}
00066     
00068     std::string getBaseName() const;
00069 
00070     // Data members
00071     const IntFun *iter;               
00072     int offset;                       
00073 };
00074  
00075 // Class NTFloatFunT
00077 
00100 template<class Ntuple, class T = Float_t> 
00101 class NTFloatFunT: public NTFloatFun {
00102   public:
00104     NTFloatFunT (const Ntuple& nt,            
00105                  const char *branchname,      
00106                  const IntFunPoR& iter_ = 0   
00107                 ) 
00108     : NTFloatFun (branchname, iter_.pif)          
00109     {
00110       TTree *t = nt.fChain;
00111       assert (t);
00112       TBranch *b = t->GetBranch (branchname);
00113       assert (b);
00114       branchAddress = reinterpret_cast<T *>(b->GetAddress());
00115       assert (branchAddress);
00116     }
00117     
00119     NTFloatFunT (const Ntuple& nt,            
00120                  T Ntuple::* p_mem,           
00121                  const char *name_ = "?",     
00122                  const IntFunPoR& iter_ = 0   
00123                 ) 
00124     : NTFloatFun (name_, iter_.pif)            
00125     {
00126       branchAddress = &(nt.*p_mem);
00127       assert (branchAddress);
00128     }
00129   
00131     NTFloatFunT (const Ntuple& nt,            
00132                  const char *branchname,      
00133                  int offset_                  
00134                 ) 
00135     : NTFloatFun (branchname, offset_)
00136     {        
00137       TTree *t = nt.fChain;
00138       assert (t);
00139       TBranch *b = t->GetBranch (branchname);
00140       assert (b);
00141       branchAddress = reinterpret_cast<T *>(b->GetAddress());
00142       assert (branchAddress);
00143     }
00144     
00146     NTFloatFunT (const Ntuple& nt,           
00147                 T Ntuple::* p_mem,           
00148                 const char *name_,           
00149                 int offset_                  
00150                ) 
00151     : NTFloatFun (name_, offset_)       
00152     {
00153       branchAddress = &(nt.*p_mem);
00154       assert (branchAddress);
00155     }
00156     
00158     NTFloatFunT (const NTFloatFunT<Ntuple, T>& source, 
00159                  const IntFunPoR& iter_                  
00160                )
00161       : NTFloatFun (source.getBaseName(), iter_.pif),
00162         branchAddress (source.branchAddress)
00163         {}
00165     NTFloatFunT (const NTFloatFunT<Ntuple, T>& source, 
00166                  const int offset_                        
00167                )
00168       : NTFloatFun (source.getBaseName(), offset_),
00169         branchAddress (source.branchAddress)
00170         {}
00171     
00172   
00174     virtual Float_FF operator() () const {
00175       int i = (iter) ? (*iter)() : offset;
00176       return branchAddress[i];
00177     } 
00178     
00180     NTFloatFunT<Ntuple, T>& operator[] (const IntFunPoR& iter_  
00181                                       ) const {
00182       return *new NTFloatFunT<Ntuple, T> (*this, iter_);
00183     }
00184     
00186     NTFloatFunT<Ntuple, T>& operator[] (int offset_  
00187                                       ) const {
00188       return *new NTFloatFunT<Ntuple, T> (*this, offset_);
00189     }
00190  
00191   protected:  
00193     virtual ~NTFloatFunT() {}
00194     
00195     // Data members
00196     const T       *branchAddress;     
00197 };
00198  
00199 // Class NTFloatFunG
00201 
00215 template<class Ntuple, class T = Float_t> 
00216 class NTFloatFunG: public NTFloatFun {
00217   public:
00218     typedef T (Ntuple::*Getter)(); // a getter function, returning T
00219     typedef T (Ntuple::*ConstGetter)() const; // a getter function, returning T
00220     
00222     NTFloatFunG (const Ntuple& nt_,            
00223                  Getter p_mem,                 
00224                  const char *name_ = "?"       
00225                 ) 
00226     : NTFloatFun (name_), nt (const_cast<Ntuple&>(nt_)), getMethod (p_mem)           
00227     {
00228     }
00229     NTFloatFunG (const Ntuple& nt_,            
00230                  ConstGetter p_mem,            
00231                  const char *name_ = "?"       
00232                 ) 
00233     : NTFloatFun (name_), nt (const_cast<Ntuple&>(nt_)), getMethod (reinterpret_cast<Getter>(p_mem))           
00234     {
00235     }
00236   
00237   
00239     virtual Float_FF operator() () const {
00240       return (nt.*getMethod)();
00241     } 
00242     
00244     virtual NTFloatFun& operator[] (const IntFunPoR& iter_  
00245                                   ) const 
00246     {assert (0); return (*(NTFloatFun*)(0)); }
00247     
00249     virtual NTFloatFun& operator[] (int offset_  
00250                                  ) const
00251     {assert (0); return (*(NTFloatFun*)(0)); }
00252     
00253  
00254   protected:  
00256     virtual ~NTFloatFunG() {}
00257     
00258     // Data members
00259     Ntuple& nt;
00260     Getter getMethod;
00261 };
00262  
00263 // Class NTFloatFunGA
00265 
00279 template<class Ntuple, class T = Float_t, class A = Float_t> 
00280 class NTFloatFunGA: public NTFloatFun {
00281   public:
00282     typedef T (Ntuple::*Getter)(A); // a getter function, returning T
00283     typedef T (Ntuple::*ConstGetter)(A) const; // a getter function, returning T
00284     
00286     NTFloatFunGA (const Ntuple& nt_,            
00287                  Getter p_mem,                 
00288                  A arg_,                       
00289                  const char *name_ = "?"       
00290                 ) 
00291     : NTFloatFun (name_), nt (const_cast<Ntuple&>(nt_)), getMethod (p_mem), arg (arg_)
00292     {
00293     }
00294     NTFloatFunGA (const Ntuple& nt_,            
00295                  ConstGetter p_mem,            
00296                  A arg_,                       
00297                  const char *name_ = "?"       
00298                 ) 
00299     : NTFloatFun (name_), nt (const_cast<Ntuple&>(nt_)), 
00300       getMethod (reinterpret_cast<Getter>(p_mem)), arg (arg_)           
00301     {
00302     }
00303   
00304   
00306     virtual Float_FF operator() () const {
00307       return (nt.*getMethod)(arg);
00308     } 
00309     
00311     virtual NTFloatFun& operator[] (const IntFunPoR& iter_  
00312                                   ) const 
00313     {assert (0); return (*(NTFloatFun*)(0)); }
00314     
00316     virtual NTFloatFun& operator[] (int offset_  
00317                                  ) const
00318     {assert (0); return (*(NTFloatFun*)(0)); }
00319     
00320  
00321   protected:  
00323     virtual ~NTFloatFunGA() {}
00324     
00325     // Data members
00326     Ntuple& nt;
00327     Getter getMethod;
00328     A arg;
00329 };
00330  
00331 // Class NTFloatFunGF
00333 
00347 template<class T = Float_t> 
00348 class NTFloatFunGF: public NTFloatFun {
00349   public:
00350     typedef T (*Getter)(); // a getter function, returning T
00351     
00353     NTFloatFunGF (Getter p_mem,                 
00354                   const char *name_ = "?"       
00355                  ) 
00356     : NTFloatFun (name_), getMethod (p_mem)           
00357     {
00358     }
00359   
00360   
00362     virtual Float_FF operator() () const {
00363       return (*getMethod)();
00364     } 
00365     
00367     virtual NTFloatFun& operator[] (const IntFunPoR& iter_  
00368                                   ) const 
00369     {assert (0); return (*(NTFloatFun*)(0)); }
00370     
00372     virtual NTFloatFun& operator[] (int offset_  
00373                                  ) const
00374     {assert (0); return (*(NTFloatFun*)(0)); }
00375     
00376  
00377   protected:  
00379     virtual ~NTFloatFunGF() {}
00380     
00381     // Data members
00382     Getter getMethod;
00383 };
00384 
00389 template<class Ntuple, class T> 
00390 NTFloatFunT<Ntuple, T>& ntfloatfun (const Ntuple& nt,           
00391                                     T Ntuple::* p_mem,           
00392                                     const char *name_ = "?",     
00393                                     const IntFunPoR& iter_ = 0   
00394                                    ) {
00395   return *new NTFloatFunT<Ntuple, T> (nt, p_mem, name_, iter_);
00396 }  
00397  
00402 template<class Ntuple, class T> 
00403 NTFloatFunT<Ntuple, T>& ntfloatfun (const Ntuple& nt,           
00404                                     T Ntuple::* p_mem,          
00405                                     const char *name_ = "?",    
00406                                     int offset_                 
00407                                    ) {
00408   return *new NTFloatFunT<Ntuple, T> (nt, p_mem, name_, offset_);
00409 } 
00410  
00415 template<class Ntuple, class T> 
00416 NTFloatFunG<Ntuple, T>& ntfloatfun (const Ntuple& nt,           
00417                                     T (Ntuple::* p_mem)(),      
00418                                     const char *name_ = "?"     
00419                                    ) {
00420   return *new NTFloatFunG<Ntuple, T> (nt, p_mem, name_);
00421 } 
00426 template<class Ntuple, class T> 
00427 NTFloatFunG<Ntuple, T>& ntfloatfun (const Ntuple& nt,            
00428                                     T (Ntuple::* p_mem)() const, 
00429                                     const char *name_ = "?"      
00430                                    ) {
00431   return *new NTFloatFunG<Ntuple, T> (nt, p_mem, name_);
00432 } 
00433  
00438 template<class Ntuple, class T, class A> 
00439 NTFloatFunGA<Ntuple, T, A>& ntfloatfun (const Ntuple& nt,           
00440                                         T (Ntuple::* p_mem)(A),     
00441                                         A arg_,                     
00442                                         const char *name_ = "?"     
00443                                        ) {
00444   return *new NTFloatFunGA<Ntuple, T, A> (nt, p_mem, arg_, name_);
00445 } 
00450 template<class Ntuple, class T, class A> 
00451 NTFloatFunGA<Ntuple, T, A>& ntfloatfun (const Ntuple& nt,             
00452                                         T (Ntuple::* p_mem)(A) const, 
00453                                         A arg_,                       
00454                                         const char *name_ = "?"       
00455                                        ) {
00456   return *new NTFloatFunGA<Ntuple, T, A> (nt, p_mem, arg_, name_);
00457 } 
00458  
00463 template<class T> 
00464 NTFloatFunGF<T>& ntfloatfun (T (* p_mem)(),     
00465                              const char *name_ = "?"     
00466                              ) {
00467   return *new NTFloatFunGF<T> (p_mem, name_);
00468 } 
00469   
00470 /*
00471  * A very special solution to the problem
00472  * that a member pointer to an ARRAY of T (i.e., a T[n]) is not equivalent
00473  * to a pointer to T or anything. Unfortunately, we also cannot
00474  * use a syntax like "p_mem[0]", because that is forbidden.
00475  * Therefore we have to use n as template argument explicitely,
00476  * and use the ugly reinterpret_cast to make a pointer to T out of a 
00477  * pointer to a T[n].
00478  */                               
00479 template<class Ntuple, class T, unsigned int n> 
00480 NTFloatFunT<Ntuple, T>& ntfloatfun (const Ntuple& nt,           
00481                                    T (Ntuple::* p_mem)[n],      
00482                                    const char *name_ = "?",     
00483                                    const IntFunPoR& iter_ = 0   
00484                                   ) {
00485   // p_mem is of type "T[n] Ntuple::*", which is correctly written as "T (Ntuple::*)[n]",
00486   // now we convert it to type "T Ntuple::*"                                
00487   T (Ntuple::* p_mem0) = reinterpret_cast<T Ntuple::*>(p_mem);
00488   return *new NTFloatFunT<Ntuple, T> (nt, p_mem0, name_, iter_);
00489 }                                  
00490 
00491 template<class Ntuple, class T, unsigned int n> 
00492 NTFloatFunT<Ntuple, T>& ntfloatfun (const Ntuple& nt,            
00493                                    T (Ntuple::* p_mem)[n],       
00494                                    const char *name_,            
00495                                    int offset_                   
00496                                   ) {
00497   // p_mem is of type "T[n] Ntuple::*", which is correctly written as "T (Ntuple::*)[n]",
00498   // now we convert it to type "T Ntuple::*"                                
00499   T (Ntuple::* p_mem0) = reinterpret_cast<T Ntuple::*>(p_mem);
00500   return *new NTFloatFunT<Ntuple, T> (nt, p_mem0, name_, offset_);
00501 }                                  
00502 
00503 
00504 #endif /* #ifndef __NTFLOATFUN_H */

Generated on Thu Oct 26 12:52:58 2006 for SFH by doxygen 1.3.2