Crombie Tools
Corrector.h
Go to the documentation of this file.
1 /**
2  @file Corrector.h
3 
4  Header file for the Corrector class.
5 
6  @author Daniel Abercrombie <dabercro@mit.edu>
7 */
8 
9 #ifndef CROMBIETOOLS_SKIMMINGTOOLS_CORRECTOR_H
10 #define CROMBIETOOLS_SKIMMINGTOOLS_CORRECTOR_H
11 
12 #include <utility>
13 #include <vector>
14 #include "TString.h"
15 #include "TFile.h"
16 #include "TTree.h"
17 #include "TTreeFormula.h"
18 #include "TH1.h"
19 
20 #include "Debug.h"
21 
22 /**
23  @ingroup skimminggroup
24  @class Corrector
25  @brief Can be created using the CrombieTools.SkimmingTools.Corrector module.
26 
27  A Corrector reads from a tree and histogram and returns a correction factor.
28  The CorrectorApplicator facilitates this application by providing the trees
29  and writes the result to a branch.
30 */
31 
32 class Corrector : virtual public Debug
33 {
34  public:
35  /// Constructor setting the name of a branch it would like to write to.
36  Corrector( TString name = "correction" );
37  virtual ~Corrector();
38 
39  /// Copy this Corrector for parallelization
40  virtual Corrector* Copy ();
41 
42  /// Set the file containing the correction histogram by name.
43  void SetCorrectionFile ( TString fileName );
44  /// Set the correction histogram name within the correction file.
45  void SetCorrectionHist ( TString histName );
46  /// Set two histograms to divide in order to obtain the correction histogram.
47  void SetCorrectionHist ( TString hist1, TString hist2 );
48 
49  /// Evaluate the TTree pointer fInTree at its current entry and return a status bit corresponding to cut.
50  std::pair<bool, Float_t> EvaluateWithFlag ();
51  /// Evaluate the TTree pointer fInTree at its current entry.
52  Float_t Evaluate ();
53 
54  /// Compares the file name to the Regex and returns true if corrections will be applied
55  Bool_t CompareFileName ( TString fileName );
56 
57  /// Get the name of the branch that this Corrector would like to write to.
58  TString GetName () const { return fName; }
59 
60  /**
61  Add an expression to read from the histogram.
62  In most normal uses (reading from a TH1) this function needs to only be called once.
63  If reading from a 2D histogram, call it twice. This gets the bin contents in the order
64  that the expressions were added. Up to 3D histograms is supported.
65  */
66  void AddInExpression ( TString expres ) { fInExpressions.push_back(expres); ++fNumDims; }
67  /// Set a cut for the corrector.
68  void SetInCut ( TString cut ) { fInCut = cut; }
69  /// Set the pointer to the TTree this Corrector is reading.
70  virtual void SetInTree ( TTree* tree ) { fInTree = tree; InitializeTree(); }
71 
72  /// Gets the list of formulas that this corrector is using
73  virtual std::vector<TString> GetFormulas ();
74 
75  /**
76  Used to set the way to read the correction histogram.
77  Note that the uncertainty is added in quadrature, so the unity-centered uncertainty
78  can safely be used for both up and down histograms.
79  */
80  enum HistReader {
81  eValue = 0, ///< Simply returns the value of the histogram
82  eZeroCenteredUnc, ///< The fractional uncertainty is just the value of the histogram
83  eUnityCenteredUnc ///< For when reading an uncertainty from a histogram where the perfect precision would be at unity
84  };
85 
86  /// Set the type of histogram reader
87  void SetHistReader ( HistReader reader ) { fHistReader = reader; }
88 
89  /// Set a RegEx for the Corrector to check against the filename. If no match, corrections are not applied
90  void SetMatchFileName ( TString regexpr ) { fMatchFileName = regexpr; }
91 
92  bool Merge = true; ///< Flag determining whether or not to merge into the CorrectorApplicator branch
93 
94  /// Set the binning when the histograms do not store the correct x values
95  void SetBinning ( UInt_t num, Double_t min, Double_t max ) { bin_num = num; bin_min = min; bin_max = max; }
96 
97  protected:
98 
99  TString fName; ///< Name of branch to write to
100  void InitializeTree (); ///< Function to initialize TTreeFormula on the tree
101  virtual Float_t DoEval (); ///< Function that actually does the evaluation of the correction factor
102 
103  TTree* fInTree = NULL; ///< Pointer to tree being read
104  TString fInCut = "1"; ///< Corrector cut
105  TTreeFormula* fCutFormula = NULL; ///< Formula for cut
106  Bool_t fIsCopy = false; ///< Track if instance is a copy
107  Bool_t fMatchedFileName; ///< A flag telling this corrector if the file has been matched
108 
109  /// Evaluate one of the formulae. If use_lims, then don't give a result that would go off the relevant axis
110  Double_t GetFormulaResult ( Int_t index, Bool_t use_mins = true );
111  /// Get the number of dimensions that the formulas hold
112  Int_t GetNumDims () { return fNumDims; }
113 
114  private:
115  TFile* fCorrectionFile = NULL; ///< Name of file containing correction histogram
116  TH1* fCorrectionHist = NULL; ///< Name of correction histogram
117  void SetMinMax (); ///< Set the mininum and maximum values for each histogram axis
118 
119  Int_t fNumDims = 0; ///< Number of dimensions in the correction histogram
120  std::vector<TString> fInExpressions; ///< Expressions to read from tree
121  std::vector<TTreeFormula*> fFormulas; ///< Formulae of fInExpressions
122  std::vector<Double_t> fMins; ///< Minimum possible values of each axis
123  std::vector<Double_t> fMaxs; ///< Maximum possible values of each axis
124  HistReader fHistReader = eValue; ///< The method to reading histograms
125 
126  UInt_t bin_num = 0; ///< Used if the binning of the histograms is off
127  Double_t bin_min = 0;
128  Double_t bin_max = 0;
129 
130  TString fMatchFileName = "";
131 
132  ClassDef(Corrector,1)
133 };
134 
135 #endif