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

NTIntFun.h

Go to the documentation of this file.
00001 
00005 #ifndef __NTINTFUN_H
00006 #define __NTINTFUN_H
00007 
00008 #include "jbltools/sfh/IntFun.h"
00009 #include "jbltools/sfh/IntFun.h"
00010 #include "jbltools/sfh/IntFunPoR.h"
00011  
00012 #include <Rtypes.h>     // declares Int_t
00013 #include <TTree.h>    
00014 #include <TBranch.h>    
00015 #include <cassert>   
00016 #include <cstring>   
00017 #include <stdio.h>  
00018 #include <iostream>
00019  
00020  
00021 // Class NTIntFun
00023 
00032 class NTIntFun: public IntFun {
00033   public:
00035     NTIntFun (const std::string& name_       
00036              );
00037     
00039     NTIntFun (const std::string& name_,      
00040               const IntFun *iter_            
00041              );
00043     NTIntFun (const std::string& name_,      
00044               int offset_                    
00045              );
00046     
00047     
00049     virtual NTIntFun& operator[] (const IntFunPoR& iter_  
00050                                   ) const = 0;
00051     
00053     virtual NTIntFun& operator[] (int offset_  
00054                                  ) const = 0;
00055     
00056 
00058     virtual const FillIterator *getIterator() const { 
00059       return iter ? iter->getIterator() : 0; 
00060     }
00061     
00062   protected:  
00064     virtual ~NTIntFun() {}
00065     
00067     std::string getBaseName() const;
00068     
00069     // Data members
00070     const IntFun *iter;               
00071     int offset;                       
00072 };
00073  
00074 // Class NTIntFunT
00076 
00101 template<class Ntuple, class T = Int_t> 
00102 class NTIntFunT: public NTIntFun {
00103   public:
00105     NTIntFunT (const Ntuple& nt,            
00106                const char *branchname,      
00107                const IntFunPoR& iter_ = 0   
00108               ) 
00109     : NTIntFun (branchname, iter_.pif)
00110     {        
00111       TTree *t = nt.fChain;
00112       assert (t);
00113       TBranch *b = t->GetBranch (branchname);
00114       assert (b);
00115       branchAddress = reinterpret_cast<T *>(b->GetAddress());
00116       assert (branchAddress);
00117     }
00118     
00120     NTIntFunT (const Ntuple& nt,            
00121                T Ntuple::* p_mem,           
00122                const char *name_ = "?",     
00123                const IntFunPoR& iter_ = 0   
00124               ) 
00125     : NTIntFun (name_, iter_.pif)       
00126     {
00127       branchAddress = &(nt.*p_mem);
00128       assert (branchAddress);
00129     }
00130   
00132     NTIntFunT (const Ntuple& nt,            
00133                const char *branchname,      
00134                int offset_                  
00135               ) 
00136     : NTIntFun (branchname, offset_)
00137     {        
00138       TTree *t = nt.fChain;
00139       assert (t);
00140       TBranch *b = t->GetBranch (branchname);
00141       assert (b);
00142       branchAddress = reinterpret_cast<T *>(b->GetAddress());
00143       assert (branchAddress);
00144     }
00145     
00147     NTIntFunT (const Ntuple& nt,            
00148               T Ntuple::* p_mem,            
00149               const char *name_,            
00150               int offset_                   
00151              ) 
00152     : NTIntFun (name_, offset_)   
00153     {
00154       branchAddress = &(nt.*p_mem);
00155       assert (branchAddress);
00156     }
00157     
00159     NTIntFunT (const NTIntFunT<Ntuple, T>& source, 
00160                const IntFunPoR& iter_                  
00161               )
00162       : NTIntFun (source.getBaseName(), iter_.pif),
00163         branchAddress (source.branchAddress)
00164         {}
00166     NTIntFunT (const NTIntFunT<Ntuple, T>& source, 
00167                const int offset_                        
00168               )
00169       : NTIntFun (source.getBaseName(), offset_),
00170         branchAddress (source.branchAddress)
00171         {}
00172   
00174     virtual int operator() () const {
00175       int i = (iter) ? (*iter)() : offset;
00176       return branchAddress[i];
00177     } 
00178     
00180     NTIntFunT<Ntuple, T>& operator[] (const IntFunPoR& iter_  
00181                                     ) const {
00182       return *new NTIntFunT<Ntuple, T> (*this, iter_.pif);
00183     }
00184     
00186     NTIntFunT<Ntuple, T>& operator[] (int offset_  
00187                                     ) const {
00188       return *new NTIntFunT<Ntuple, T> (*this, offset_);
00189     }
00190     
00191     
00192   protected:  
00194     virtual ~NTIntFunT() {}
00195     
00196     // Data members
00197     const T *branchAddress;           
00198 };
00199 
00200 
00205 template<class Ntuple, class T> 
00206 NTIntFunT<Ntuple, T>& ntintfun (const Ntuple& nt,            
00207                                 T Ntuple::* p_mem,           
00208                                 const char *name_ = "?",     
00209                                 const IntFunPoR& iter_ = 0   
00210                                ) {
00211   return *new NTIntFunT<Ntuple, T> (nt, p_mem, name_, iter_);
00212 }   
00213 
00218 template<class Ntuple, class T> 
00219 NTIntFunT<Ntuple, T>& ntintfun (const Ntuple& nt,            
00220                                 T Ntuple::* p_mem,           
00221                                 const char *name_ = "?",     
00222                                 int offset_                  
00223                                ) {
00224   return *new NTIntFunT<Ntuple, T> (nt, p_mem, name_, offset_);
00225 }   
00226 /*
00227  * A very special solution to the problem
00228  * that a member pointer to an ARRAY of T (i.e., a T[n]) is not equivalent
00229  * to a pointer to T or anything. Unfortunately, we also cannot
00230  * use a syntax like "p_mem[0]", because that is forbidden.
00231  * Therefore we have to use n as template argument explicitely,
00232  * and use the ugly reinterpret_cast to make a pointer to T out of a 
00233  * pointer to a T[n].
00234  */                               
00235 template<class Ntuple, class T, unsigned int n> 
00236 NTIntFunT<Ntuple, T>& ntintfun (const Ntuple& nt,            
00237                                 T (Ntuple::* p_mem)[n],           
00238                                 const char *name_ = "?",
00239                                 const IntFunPoR& iter_ = 0   
00240                                ) {
00241   // p_mem is of type "T[n] Ntuple::*", which is correctly written as "T (Ntuple::*)[n]",
00242   // now we convert it to type "T Ntuple::*"                                
00243   T (Ntuple::* p_mem0) = reinterpret_cast<T Ntuple::*>(p_mem);
00244   return *new NTIntFunT<Ntuple, T> (nt, p_mem0, name_, iter_);
00245 }                                  
00246 template<class Ntuple, class T, unsigned int n> 
00247 NTIntFunT<Ntuple, T>& ntintfun (const Ntuple& nt,             
00248                                 T (Ntuple::* p_mem)[n],       
00249                                 const char *name_ = "?", 
00250                                 int offset_                   
00251                                ) {
00252   // p_mem is of type "T[n] Ntuple::*", which is correctly written as "T (Ntuple::*)[n]",
00253   // now we convert it to type "T Ntuple::*"                                
00254   T (Ntuple::* p_mem0) = reinterpret_cast<T Ntuple::*>(p_mem);
00255   return *new NTIntFunT<Ntuple, T> (nt, p_mem0, name_, offset_);
00256 }                                  
00257 
00258 
00259 #endif /* #ifndef __NTINTFUN_H */

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