Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

TreeHandler.cpp

Go to the documentation of this file.
00001 #include "caf_util/TreeHandler.hpp"
00002 #include <iostream>
00003 
00004 namespace caf_util {
00005   
00006   TreeHandler::TreeHandler()
00007     : _tree(0),
00008       _tmb_tree(0)
00009   {
00010   }
00011   
00012   
00013   void TreeHandler::InitTree(std::string TreeName, TFile *input_file)
00014   {
00015     _toTree = TreeName;
00016     _tmb_tree = 0; _tree = 0;
00017     
00018     if( _tmb_tree = (TTree *)input_file->Get("TMBTree") ) {
00019       
00020       if ( TreeName=="TMBTree" ) _tree = _tmb_tree;
00021       else _tree = dynamic_cast<TTree *>(gROOT->Get(TreeName.c_str()));
00022       
00023       if ( _tree == 0 ) {
00024         gROOT->cd();
00025         _tree = new TTree(_toTree.c_str(), _toTree.c_str());
00026         _tmb_tree->AddFriend(_tree);
00027       }
00028     }
00029     else
00030       std::cerr << " In TreeHandler::InitTree[" << TreeName.c_str() 
00031                 << "] -- No TMBTree in input file!\n";
00032     
00033   }
00034   
00035   void TreeHandler::AddBranch(std::string BranchName, std::string BranchObjType)
00036   {
00037     if ( _tree ) {
00038       _BranchContent[BranchName.c_str()] = new TClonesArray(BranchObjType.c_str());
00039       _Branches[BranchName.c_str()] = 
00040         _tree->Branch(BranchName.c_str(), &_BranchContent[BranchName.c_str()]);
00041     }
00042     else
00043       std::cerr << " In TreeHandler::AddBranch[" << BranchName.c_str() 
00044                 << "] -- No output tree found. _tree needs to be initialized.\n";
00045   }
00046 
00047   void TreeHandler::AddBranch(std::string BranchName, int &intref, std::string BranchObjType)
00048   {
00049     if ( _tree ) {
00050       _Branches[BranchName.c_str()] =
00051         _tree->Branch(BranchName.c_str(), &intref, BranchObjType.c_str());
00052     }
00053     else
00054       std::cerr << " In TreeHandler::AddBranch[" << BranchName.c_str() 
00055                 << "] -- No output tree found. _tree needs to be initialized.\n";
00056   }
00057 
00058   void TreeHandler::AddBranch(std::string BranchName, float &floatref, std::string BranchObjType)
00059   {
00060     if ( _tree ) {
00061       _Branches[BranchName.c_str()] =
00062         _tree->Branch(BranchName.c_str(), &floatref, BranchObjType.c_str());
00063     }
00064     else
00065       std::cerr << " In TreeHandler::AddBranch[" << BranchName.c_str() 
00066                 << "] -- No output tree found. _tree needs to be initialized.\n";
00067   }
00068 
00069   void TreeHandler::AddBranch(std::string BranchName, double &dbleref, std::string BranchObjType)
00070   {
00071     if ( _tree ) {
00072       _Branches[BranchName.c_str()] =
00073         _tree->Branch(BranchName.c_str(), &dbleref, BranchObjType.c_str());
00074     }
00075     else
00076       std::cerr << " In TreeHandler::AddBranch[" << BranchName.c_str() 
00077                 << "] -- No output tree found. _tree needs to be initialized.\n";
00078   }
00079 
00080   void TreeHandler::ClearIt() 
00081   {
00082 
00083     // Look for our tree in the memory
00084     // If it exist -  delete it!
00085 
00086     if ( _tree == (TTree *)gROOT->Get(_toTree.c_str()) ) {
00087 
00088       if ( _tmb_tree )
00089         _tmb_tree->RemoveFriend(_tree);
00090       _tree->Delete();
00091       _tree=0;
00092     }
00093 
00094     // Clear and remove all TClonesArrays :
00095     std::map<std::string,TClonesArray*>::iterator it;
00096     for( it = _BranchContent.begin(); it != _BranchContent.end(); it++ ) {
00097       if ( TClonesArray *array = it->second )
00098         array->Delete();
00099       else
00100         std::printf("No Array!\n");
00101     }
00102 
00103     _BranchContent.clear();
00104     _Branches.clear();
00105   }
00106 
00107 }

Generated on Tue Mar 28 10:13:05 2006 for CAF by doxygen 1.3.4