00001 #ifndef TreeHandler_HPP_
00002 #define TreeHandler_HPP_
00003
00004 #include <map>
00005 #include <string>
00006 #include <stdexcept>
00007
00008 #include "cafe/Event.hpp"
00009 #include "cafe/Collection.hpp"
00010
00011 #include "TFile.h"
00012 #include "TTree.h"
00013 #include "TBranch.h"
00014 #include "TBranchElement.h"
00015 #include "TDirectory.h"
00016 #include "TClonesArray.h"
00017
00018 namespace caf_util {
00019
00059 class TreeHandler : public TObject {
00060
00061 private:
00062
00063 TTree *_tree;
00064 TTree *_tmb_tree;
00065 std::map<std::string,TClonesArray*> _BranchContent;
00066 std::map<std::string,TBranch*> _Branches;
00067 std::string _toTree;
00068
00069 public:
00070
00071 TreeHandler();
00072
00073 inline TTree* GetTree() { return _tree; }
00074 inline TBranch* GetBranch(std::string BranchName) { return _Branches[BranchName.c_str()]; }
00075 inline TClonesArray* GetBranchArray(std::string BranchName) { return _BranchContent[BranchName.c_str()]; }
00076
00077 void InitTree(std::string TreeName, TFile *input_file);
00078
00079 void AddBranch(std::string BranchName, std::string BranchObjType);
00080 void AddBranch(std::string BranchName, int &intref, std::string BranchObjType);
00081 void AddBranch(std::string BranchName, float &floatref, std::string BranchObjType);
00082 void AddBranch(std::string BranchName, double &dbleref, std::string BranchObjType);
00083
00084
00085 template <class T>
00086 void AddBranch(std::string BranchName, std::string BranchObjType, T** obj) {
00087 if ( _tree )
00088 _Branches[BranchName.c_str()] =
00089 _tree->Branch(BranchName.c_str(), BranchObjType.c_str(), obj);
00090 else
00091 std::cerr << " In TreeHandler::AddBranch[" << BranchName.c_str()
00092 << "] -- No output tree found. _tree needs to be initialized.\n";
00093 }
00094
00095 template<class T>
00096 void FillBranch(std::string BranchName, cafe::Collection<T> result )
00097 {
00098 if ( TClonesArray *array = GetBranchArray(BranchName.c_str()) ) {
00099 array->Delete();
00100
00101 int next = 0;
00102 for( typename cafe::Collection<T>::iterator it = result.begin(); it != result.end(); ++it )
00103 new ((*array)[next++]) T(*it);
00104 }
00105 }
00106
00107
00108 void ClearIt();
00109
00110 ClassDef(TreeHandler,1);
00111
00112 };
00113
00114 }
00115
00116 #endif
00117