00001 #include "cafe/Variables.hpp"
00002
00003 #include "TBranch.h"
00004 #include "TTree.h"
00005
00006 #include <cassert>
00007 #include <stdexcept>
00008
00009 namespace cafe {
00010
00011 Variables::Variables()
00012 : _cookie(-1),
00013 _branch(0)
00014 {
00015 }
00016
00017 Variables::Variables(const std::vector<std::string>& names)
00018 : _names(names),
00019 _cookie(-1),
00020 _branch(0)
00021 {
00022 }
00023
00024 Variables::Variables(const std::string& name1)
00025 : _names(1, name1),
00026 _cookie(-1),
00027 _branch(0)
00028 {
00029 }
00030
00031 Variables::Variables(const std::string& name1, const std::string& name2)
00032 : _cookie(-1),
00033 _branch(0)
00034 {
00035 add(name1);
00036 add(name2);
00037 }
00038
00039 Variables::Variables(const std::string& name1, const std::string& name2, const std::string& name3)
00040 : _cookie(-1),
00041 _branch(0)
00042 {
00043 add(name1);
00044 add(name2);
00045 add(name3);
00046 }
00047
00048 Variables::Variables(const std::string& name1, const std::string& name2, const std::string& name3, const std::string& name4)
00049 : _cookie(-1),
00050 _branch(0)
00051 {
00052 add(name1);
00053 add(name2);
00054 add(name3);
00055 add(name4);
00056 }
00057
00058 Variables::Variables(const Variables& other)
00059 : _names(other._names),
00060 _subbranches(other._subbranches),
00061 _cookie(other._cookie),
00062 _branch(other._branch)
00063 {
00064 }
00065
00066 Variables& Variables::operator=(const Variables& other)
00067 {
00068 if(this != &other) {
00069 _names = other._names;
00070 _subbranches = other._subbranches;
00071 _cookie = other._cookie;
00072 _branch = other._branch;
00073 }
00074 return *this;
00075 }
00076
00077 Variables& Variables::add(const std::string& name)
00078 {
00079 _names.push_back(name);
00080 _cookie = -1;
00081 _branch = 0;
00082 return *this;
00083 }
00084
00085 Variables& Variables::add(const std::vector<std::string>& names)
00086 {
00087 _names.insert(_names.end(), names.begin(), names.end());
00088 _cookie = -1;
00089 _branch = 0;
00090 return *this;
00091 }
00092
00093 Variables::~Variables()
00094 {
00095 }
00096
00097 bool Variables::empty() const
00098 {
00099 return _names.empty();
00100 }
00101
00102 void Variables::get(TTree *tree, TBranch *br, int cookie) const
00103 {
00104 using namespace std;
00105
00106 assert(tree != 0);
00107 assert(br != 0);
00108
00109 if(cookie != _cookie) {
00110
00111
00112
00113 _subbranches.clear();
00114 for(vector<string>::const_iterator it = _names.begin();
00115 it != _names.end();
00116 ++it) {
00117 if(TBranch *subbr = br->FindBranch((*it).c_str())) {
00118 _subbranches.push_back(subbr);
00119 } else {
00120 throw runtime_error("Variables: non-existing branch name: " + *it);
00121 }
00122 }
00123
00124 _cookie = cookie;
00125 _branch = br;
00126 }
00127
00128 if(_branch != br) {
00129 throw std::logic_error(std::string("Variables: using same object for two different branches: ") +
00130 _branch->GetName() + " and " + br->GetName());
00131 }
00132
00133
00134 Long64_t current = tree->GetReadEntry();
00135 for(vector<TBranch*>::iterator it = _subbranches.begin();
00136 it != _subbranches.end();
00137 ++it) {
00138 if((*it)->GetReadEntry() != current) {
00139 (*it)->GetEntry(current);
00140 }
00141 }
00142 }
00143
00144 }
00145