00001 #ifndef CAFE_SELECTUSEROBJECTS_HPP__
00002 #define CAFE_SELECTUSEROBJECTS_HPP__
00003
00004 #include "cafe/Event.hpp"
00005 #include "cafe/Processor.hpp"
00006 #include "cafe/Config.hpp"
00007 #include "cafe/Variables.hpp"
00008
00009 #include "TFile.h"
00010 #include "TTree.h"
00011 #include "TBranch.h"
00012 #include "TBranchElement.h"
00013 #include "TDirectory.h"
00014 #include "TClonesArray.h"
00015
00016 #include <string>
00017 #include <stdexcept>
00018
00019 namespace cafe {
00020
00050 template<class T>
00051 class SelectUserObjects : public Processor {
00052 public:
00053 SelectUserObjects(const char *name);
00054 void inputFileOpened(TFile *file);
00055 void inputFileClosing(TFile *file);
00056 bool processEvent(cafe::Event& event);
00057 protected:
00062 virtual bool selectObject(const T& obj) = 0;
00063
00066 virtual void before(Collection<T>& from);
00067
00069 virtual void after(Collection<T>& accepted, Collection<T>& rejected);
00070
00072 void addVariable(const std::string& name) {_vars.add(name);}
00073
00074 private:
00075 std::string _fromBranch;
00076 std::string _toBranch;
00077 std::string _toRejectedBranch;
00078 std::string _treeName;
00079 TTree *_tree;
00080 TClonesArray *_result;
00081 TClonesArray *_resultRejected;
00082 TBranch *_branch;
00083 TBranch *_branchRejected;
00084 Variables _vars;
00085 public:
00086 ClassDef(SelectUserObjects, 0);
00087 };
00088
00089
00090
00091
00092
00093
00094 template<class T>
00095 SelectUserObjects<T>::SelectUserObjects(const char *name)
00096 : Processor(name),
00097 _tree(0),
00098 _result(0),
00099 _resultRejected(0),
00100 _branch(0),
00101 _branchRejected(0)
00102 {
00103 using namespace cafe;
00104
00105
00106 Config config(name);
00107
00108 _fromBranch = config.get("From", "");
00109 _toBranch = config.get("To", "");
00110 _toRejectedBranch = config.get("RejectedBranch", "");
00111 _treeName = config.get("Tree", "TMBTree");
00112 _vars = config.getVString("Variables", " ,");
00113
00114 if(_fromBranch.empty()) {
00115 throw std::runtime_error(std::string("SelectUserObject[") + name
00116 + "] : From: branch in config file not set");
00117 }
00118
00119 if(_toBranch.empty()) {
00120 throw std::runtime_error(std::string("SelectUserObject[") + name
00121 + "] : To: branch in config file not set");
00122 }
00123
00124 out() << "SelectUserObjects[" << name << "] From: " << _fromBranch
00125 << " To: " << _toBranch << std::endl ;
00126 if (_toRejectedBranch != "") {
00127 out () << " Rejected objects go to branch: " << _toRejectedBranch << std::endl;
00128 }
00129 out() << "SelectUserObjects[" << name << "] Output Tree: " << _treeName << std::endl;
00130 }
00131
00132 template<class T>
00133 void SelectUserObjects<T>::inputFileOpened(TFile *file)
00134 {
00135 if(TTree *tmb_tree = (TTree *)file->Get("TMBTree")) {
00136
00137 if ( _treeName=="TMBTree" ) _tree = dynamic_cast<TTree*>(file->Get("TMBTree"));
00138 else _tree = dynamic_cast<TTree *>(gROOT->Get(_treeName.c_str()));
00139
00140 if (_tree == 0) {
00141 gROOT->cd();
00142 _tree = new TTree(_treeName.c_str(), fullName().c_str());
00143 tmb_tree->AddFriend(_tree);
00144 }
00145
00146 _branch = _tree->GetBranch(_toBranch.c_str()) ;
00147 if (_branch == 0) {
00148 if(TBranchElement *br = (TBranchElement *)tmb_tree->GetBranch(_fromBranch.c_str())) {
00149 _result = new TClonesArray(br->GetClonesName());
00150 _branch = _tree->Branch(_toBranch.c_str(), &_result, 4096);
00151 if (_toRejectedBranch != "" && !_branchRejected) {
00152 _resultRejected = new TClonesArray(br->GetClonesName());
00153 _branchRejected = _tree->Branch(_toRejectedBranch.c_str(), &_resultRejected, 4096); }
00154 } else {
00155 err() << "SelectUserObjects[" << name() << "] No such branch: "
00156 << _fromBranch << std::endl;
00157 }
00158 }
00159 } else {
00160 err() << "SelectUserObjects[" << name() << "] No TMBTree" << std::endl;
00161 }
00162 }
00163
00164 template<class T>
00165 void SelectUserObjects<T>::inputFileClosing(TFile *file)
00166 {
00167
00168 if(TTree* oldtree =
00169 dynamic_cast<TTree *>(gROOT->Get(_treeName.c_str()))) {
00170 oldtree->Delete();
00171 }
00172 _tree = 0;
00173 _branch = 0;
00174 _branchRejected = 0;
00175 }
00176
00177 template<class T>
00178 void SelectUserObjects<T>::before(Collection<T>& from)
00179 {
00180 }
00181
00182 template<class T>
00183 void SelectUserObjects<T>::after(Collection<T>& accepted, Collection<T>& rejected)
00184 {
00185 }
00186
00187 template<class T>
00188 bool SelectUserObjects<T>::processEvent(Event& event)
00189 {
00190 using namespace cafe;
00191
00192 if(!_result) return true;
00193 _result->Delete() ;
00194 if(_resultRejected) _resultRejected->Delete() ;
00195
00196 Collection<T> from(event.getCollection<T>(_fromBranch.c_str(), _vars));
00197
00198 before(from);
00199
00200 Int_t next = 0;
00201 Int_t nextr = 0;
00202
00203 for(typename Collection<T>::const_iterator it = from.begin();
00204 it != from.end();
00205 ++it) {
00206 if(selectObject(*it)) {
00207 new ((*_result)[next++]) T(*it);
00208 } else if (_resultRejected) {
00209 new ((*_resultRejected)[nextr++]) T(*it);
00210 }
00211 }
00212
00213 Collection<T> accepted(_result);
00214 Collection<T> rejected(_resultRejected);
00215
00216 after(accepted, rejected);
00217
00218 return true;
00219 }
00220
00221 }
00222
00223 #endif // CAFE_SELECTUSEROBJECTS_HPP__