EventBase.cpp

Go to the documentation of this file.
00001 
00002 #include "TObject.h"
00003 
00004 #include "cafe/EventBase.hpp"
00005 #include "cafe/Stat.hpp"
00006 
00007 #include "TProcessID.h"
00008 
00009 #include <stdexcept>
00010 
00011 extern cafe::Stat* STAT ; 
00012 
00013 namespace cafe {
00014 
00015     namespace detail {
00016         const Variables empty;
00017     }
00018 
00019     EventBase::EventBase()
00020         : _tree (0),
00021           _cookie(0),
00022           _partial_read(true),
00023           _objectCount(TProcessID::GetObjectCount())
00024     {
00025     }
00026     
00027     EventBase::~EventBase()
00028     {
00029     }
00030 
00031     // Called when the tree changes.
00032     // Set addresses for reading Branches.
00033     void EventBase::setBranchAddresses(TTree *tree)
00034     {
00035         _tree = tree;
00036         for(BranchMap::iterator it = _branches.begin();
00037             it != _branches.end();
00038             ++it) {
00039             (*it).second->setBranchAddress(tree);
00040         }
00041         _cookie++;
00042     }
00043 
00044     bool EventBase::readBranch(const std::string& branchName) 
00045     {
00046         if(_branches.find(branchName) != _branches.end()) {
00047             BranchHolderBase *branch = _branches[branchName];
00048             branch->readBranch(_tree);
00049             return true;
00050         } else if(TBranch *br = _tree->FindBranch(branchName.c_str())) {
00051             return br->GetEntry(_tree->GetReadEntry()) >= 0;
00052         } else {
00053             return false;
00054         }
00055     }
00056 
00057     bool EventBase::setPartialRead(bool allow)
00058     {
00059         bool ret = _partial_read;
00060         _partial_read = allow;
00061         return ret;
00062     }
00063 
00064     bool EventBase::getPartialRead() const
00065     {
00066         return _partial_read;
00067     }
00068 
00069     void EventBase::clear()
00070     {
00071         for(Map::iterator it = _map.begin();
00072             it != _map.end();
00073             ++it) {
00074             (*it).second->clear();
00075             delete (*it).second;
00076         }
00077         _map.clear();
00078         _tags.clear();
00079         if (STAT) STAT->eventEnd() ;
00080 
00081         //
00082         // The real limit is > 83000000 million objects, but there
00083         // is something seriously wrong if you created more than
00084         // 100000 objects per event.
00085         //
00086         
00087         if(TProcessID::GetObjectCount() - _objectCount > 100000) {
00088             throw std::runtime_error("EventBase::clear(): TProcessID object count too large");
00089         }
00090 
00091         TProcessID::SetObjectCount(_objectCount);
00092     }
00093 
00094     void EventBase::clear(const std::string& key)
00095     {
00096         Map::iterator it = _map.find(key);
00097         if(it != _map.end()) {
00098             (*it).second->clear();
00099             delete (*it).second;
00100             _map.erase (it);
00101         }
00102     }
00103 
00104     std::vector<std::string> *EventBase::varnames() 
00105     {
00106       Map::iterator it = _map.begin();
00107       std::vector<std::string> *varnamelist = new std::vector<std::string>;
00108       for(; it!=_map.end(); it++) varnamelist->push_back(it->first);
00109       return varnamelist;
00110     };
00111   
00112     std::vector<std::string> *EventBase::vartypes()
00113     {
00114       Map::iterator it = _map.begin();
00115       std::vector<std::string> *vartypelist = new std::vector<std::string>;
00116       for(; it!=_map.end(); it++) vartypelist->push_back(std::string( (typeid(it->second)).name()) );
00117       return vartypelist;
00118     };
00119 
00120     void EventBase::tag(const std::string& tag)
00121     {
00122         _tags.insert(tag);
00123     }
00124 
00125     bool EventBase::hasTag(const std::string& tag) const
00126     {
00127         return _tags.find(tag) != _tags.end();
00128     }
00129 
00130     bool EventBase::hasTag(const char *tag) const
00131     {
00132         return _tags.find(std::string(tag)) != _tags.end();
00133     }
00134 
00135     void EventBase::untag(const std::string& tag)
00136     {
00137       _tags.erase(tag);
00138     }    
00139   
00140    ostream& EventBase::printTags(ostream& os) const {
00141      for(std::set<std::string>::const_iterator it = _tags.begin() ;
00142          it != _tags.end() ; it++) 
00143        os << "[" << *it  << "] " ;
00144      return os ;
00145    }
00146 
00147 
00148     TTree *EventBase::getTree() const
00149     {
00150         return _tree;
00151     }
00152 
00153     int   EventBase::getCookie() const
00154     {
00155         return _cookie;
00156     }
00157 
00158     const TClonesArray *EventBase::getClonesArray(const std::string& branchName, const Variables& vars) const
00159     {
00160         BranchHolder<TClonesArray> *branch = dynamic_cast<BranchHolder<TClonesArray>*>(_branches[branchName]);
00161         if(branch == 0) {
00162             _branches[branchName] = branch = new BranchHolder<TClonesArray>(branchName);
00163             branch->setBranchAddress(_tree);
00164         }
00165         return branch->getObject(_tree, _partial_read ? vars : cafe::detail::empty, _cookie);
00166     }
00167 
00168 
00169 
00170 }
00171 
00172 ClassImp(cafe::EventBase);
00173 

Generated on Thu Apr 3 04:14:23 2008 for CAF by doxygen 1.3.4