Crombie Tools
TreeContainer.h
Go to the documentation of this file.
1 /**
2  @file TreeContainer.h
3  Defines the TreeContainer class.
4  @author Daniel Abercrombie <dabercro@mit.edu>
5 */
6 
7 #ifndef CROMBIETOOLS_COMMONTOOLS_TREECONTAINER_H
8 #define CROMBIETOOLS_COMMONTOOLS_TREECONTAINER_H
9 
10 #include <vector>
11 
12 #include "TString.h"
13 #include "TCut.h"
14 #include "TFile.h"
15 #include "TTree.h"
16 
17 /**
18  @ingroup commongroup
19  @class TreeContainer
20  @brief Can be called from the CrombieTools.CommonTools.TreeContainer module.
21 
22  Capable of holding many trees and skimming them. Probably just a poor reimplementation
23  of TChain, but I have code that depends on this now.
24 */
25 
27 {
28  public:
29 
30  /**
31  TreeContainer Constructor.
32  @param fileName can either be the name of a single file or a directory.
33  If it's a file, that file is opened. If it's a directory, all the
34  .root files in that directory are opened
35  */
36  TreeContainer(TString fileName = "");
37  virtual ~TreeContainer();
38 
39  /// Adds another file to the list of files contained in TreeContainer.
40  void AddFile ( TString fileName );
41  /// Adds all of the files located in a particular directory.
42  void AddDirectory ( TString directoryName, TString searchFor = ".root" );
43  /// Return all of the trees contained as a single TTree pointer.
44  TTree* ReturnTree ( TString Name = "", TFile *inFile = NULL );
45  /// Return all of the trees contained as a vector of TTree pointers.
46  std::vector<TTree*> ReturnTreeList ( TString Name = "" );
47 
48  /// Write a single TTree into a TFile.
49  void MakeFile ( TString fileName = "", TString treeName = "");
50 
51  /// Sets the name of the tree searched for in the input files.
52  inline void SetTreeName ( TString TreeName ) { fTreeName = TreeName; }
53  /// Add a friend to look for in each file.
54  inline void AddFriendName ( TString name ) { fFriendNames.push_back(name); }
55  /// Set the printing level for debugging.
56  inline void SetPrinting ( Bool_t printing ) { fPrinting = printing; }
57 
58  /// Use this to keep only certain branches when trees are returned.
59  inline void AddKeepBranch ( TString name ) { fKeepBranches.push_back(name); }
60  /// Use this to set an output file to place a single tree.
61  inline void SetOutputFileName ( TString name ) { fOutputFileName = name; }
62  /// Set a cut to apply to returned trees.
63  inline void SetSkimmingCut ( TCut cut ) { fSkimmingCut = cut; }
64 
65  /// Return vector of file names read by the TreeContainer.
66  inline std::vector<TString> ReturnFileNames () { return fFileNames; }
67  /// Return vector of TFile pointers read by the TreeContainer.
68  inline std::vector<TFile*> ReturnFileList () { return fFileList; }
69 
70  private:
71 
72  Bool_t fPrinting = false; ///< Printer for debugging
73  TFile* tempFile = NULL; ///< Pointer to File
74  TTree* tempTree = NULL; ///< Pointer to Tree
75  TTree* tempFriend = NULL;
76  TString fTreeName = "events"; ///< Name of Trees looking for
77  std::vector<TString> fFriendNames;
78  std::vector<TFile*> fFileList; ///< List of files
79  std::vector<TTree*> fTreeList; ///< List of trees
80  TTree* fTree = NULL; ///< Keep pointer to be deleted at end
81 
82  std::vector<TString> fKeepBranches; ///< Branches kept in the event of skimming
83  TString fOutputFileName = "output.root"; ///< Potential output file name of skim
84  TCut fSkimmingCut = ""; ///< Cut to return only branches meeting given conditions
85 
86  std::vector<TString> fFileNames; ///< Used to track names of files where tree list come from
87 
88  TTree* SkimTree ( TTree *tree, TFile *inFile );
89 
90  ClassDef(TreeContainer,1)
91 };
92 
93 #endif