All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sbnana/sbnana/CAFAna/Core/Utilities.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <fenv.h>
4 #include <map>
5 #include <set>
6 #include <string>
7 #include <vector>
8 #include <iostream>
9 #include <memory>
10 
11 // these are templated types.
12 // can't forward-declare them here
13 // because compiler errors result
14 // when the templates are introduced
15 #include "TMatrixD.h"
16 #include "TVectorD.h"
17 
18 class TArrayD;
19 class TDirectory;
20 class TH1;
21 class TH2;
22 class TH3;
23 class TF1;
24 class TH1D;
25 class TH2F;
26 class TH2D;
27 class TH3D;
28 class TVector3;
29 
30 namespace ana
31 {
32  class Binning;
33  class Spectrum;
34  class Ratio;
35 
36  enum EBinType
37  {
38  kBinContent, ///< Regular histogram
39  kBinDensity ///< Divide bin contents by bin widths
40  };
41 
42 
43  /// For use as an argument to \ref Spectrum::ToTH1
47  };
48 
49 
50  /// Return a different string each time, for creating histograms
51  std::string UniqueName();
52 
53  /// \brief Prevent histograms being added to the current directory
54  ///
55  /// Upon going out of scope, restores the previous setting
57  {
58  public:
61  protected:
62  bool fBackup;
63  };
64 
65  /// \brief ifdh calls between construction and destruction produce no output
66  ///
67  /// Upon going out of scope, restores the previous setting
68  class IFDHSilent
69  {
70  public:
71  IFDHSilent();
72  ~IFDHSilent();
73  protected:
74  bool fSet;
75  };
76 
77  /// \brief Alter floating-point exception flag
78  ///
79  /// Upon going out of scope, restores the previous setting
81  {
82  public:
83  FloatingExceptionOnNaN(bool enable = true);
85  protected:
86  fexcept_t fBackup;
87  };
88 
89  /// $EXPERIMENT or a nice error message and abort
90  std::string Experiment();
91 
92  /// $SAM_EXPERIMENT or a nice error message and abort
93  std::string SAMExperiment();
94 
95  /** \brief Compute bin-to-bin covariance matrix from a collection of sets of bin contents.
96 
97  \param binSets Collection of sets of bins from which covariances should be calculated
98  \param firstBin The first bin that should be considered (inclusive)
99  \param lastBin The last bin that should be considered (inclusive). -1 means "last in set"
100 
101  \returns unique_ptr to TMatrixD containing computed covariance matrix unless binSets.size() < 2,
102  in which case the unique_ptr's target is nullptr.
103 
104  Note TH1D is a child class of TArrayD -- so you can pass a vector
105  of TH1D* to this method.
106  **/
107  std::unique_ptr<TMatrixD> CalcCovMx(const std::vector<TArrayD*> & binSets, int firstBin=0, int lastBin=-1);
108 
110  {
111  public:
112  static void SetError(double e) {fgErr = e;}
113  static double GetError() {return fgErr;}
114  protected:
115  static double fgErr;
116  };
117 
118  /** \brief The log-likelihood formula from the PDG.
119 
120  \param exp The expected spectrum
121  \param obs The corresponding observed spectrum
122 
123  \returns The log-likelihood formula from the PDG
124  \f[ \chi^2=2\sum_i^{\rm bins}\left(e_i-o_i+o_i\ln\left({o_i\over e_i}\right)\right) \f]
125 
126  Includes underflow bin and an option for
127  overflow bin (off by default) and handles
128  zero observed or expected events correctly.
129  **/
130  double LogLikelihood(const TH1* exp, const TH1* obs, bool useOverflow = false);
131 
132  /** \brief The log-likelihood formula for a single bin
133 
134  \param exp Expected count
135  \param obs Observed count
136 
137  \returns The log-likelihood formula from the PDG
138  \f[ \chi^2=2\left(e-o+o\ln\left({o\over e}\right)\right) \f]
139 
140  Handles zero observed or expected events correctly.
141  **/
142  double LogLikelihood(double exp, double obs);
143 
144  /** \brief Chi-squared calculation using a covariance matrix.
145 
146  \param exp Expected bin counts
147  \param obs Observed bin counts
148  \param covmxinv Inverse of covariance matrix. Must have same dimensions as exp and obs
149 
150  \returns The chi^2 calculated according to the formula from the PDG:
151  \f[ \chi^2 = (\vec{o}-\vec{e})^{T} V^{-1} (\vec{o} - \vec{e}) \]
152 
153  Note that this implicitly assumes Gaussian statistics for the bin counts!
154  **/
155  double Chi2CovMx(const TVectorD* exp, const TVectorD* obs, const TMatrixD* covmxinv);
156 
157  /// Chi-squared calculation using covariance matrix (calls the TVectorD version internally).
158  double Chi2CovMx(const TH1* exp, const TH1* obs, const TMatrixD* covmxinv);
159 
160  /// \brief Internal helper for \ref Surface and \ref FCSurface
161  ///
162  /// Creates a histogram having bins \em centred at the min and max
163  /// coordinates
164  TH2F* ExpandedHistogram(const std::string& title,
165  int nbinsx, double xmin, double xmax, bool xlog,
166  int nbinsy, double ymin, double ymax, bool ylog);
167 
168  /// \brief Invert a symmetric matrix with possibly empty rows/columns.
169  ///
170  /// Invert a symmetric matrix that may have empty rows/columns,
171  /// which (strictly speaking) make it impossible to invert the matrix.
172  /// (This often arises when computing covariance matrices for predictions
173  /// which have empty bins; the covariance is 0 for the entire row/column
174  /// in that case.)
175  /// Since those rows/cols are not useful, we can sidestep the problem
176  /// by removing them (and the corresponding columns)
177  /// from the matrix, inverting that, then re-inserting
178  /// the null rows/columns.
179  std::unique_ptr<TMatrixD> SymmMxInverse(const TMatrixD& mx);
180 
181  /// Utility function to avoid need to switch on bins.IsSimple()
182  TH1D* MakeTH1D(const char* name, const char* title, const Binning& bins);
183  /// Utility function to avoid 4-way combinatorial explosion on the bin types
184  TH2D* MakeTH2D(const char* name, const char* title,
185  const Binning& binsx,
186  const Binning& binsy);
187 
188  /// \brief For use with \ref Var2D
189  ///
190  /// Re-expand a histogram flattened by \ref Var2D into a 2D histogram for
191  /// plotting purposes. The binning scheme must match that used in the
192  /// original Var.
193  TH2* ToTH2(const Spectrum& s, double exposure, ana::EExposureType expotype,
194  const Binning& binsx, const Binning& binsy,
196 
197  /// Same as ToTH2, but with 3 dimensions
198  TH3* ToTH3(const Spectrum& s, double exposure, ana::EExposureType expotype,
199  const Binning& binsx, const Binning& binsy, const Binning& binsz,
201 
202  /// \brief For use with \ref Var2D
203  ///
204  /// Re-expand a flatenned histogram into a 2D histogram for
205  /// plotting purposes. The binning scheme must match that used in the
206  /// original Var.
207  TH2* ToTH2(const Ratio& r, const Binning& binsx, const Binning& binsy);
208 
209  /// Same as ToTH2, but with 3 dimensions
210  TH3* ToTH3(const Ratio& r, const Binning& binsx,
211  const Binning& binsy, const Binning& binsz);
212 
213  /// Helper for ana::ToTH2
214  TH2* ToTH2Helper(std::unique_ptr<TH1> h1,
215  const Binning& binsx,
216  const Binning& binsy,
218 
219  /// Helper for ana::ToTH3
220  TH3* ToTH3Helper(std::unique_ptr<TH1> h1,
221  const Binning& binsx,
222  const Binning& binsy,
223  const Binning& binsz,
225 
226  /// Find files matching a UNIX glob, plus expand environment variables
227  std::vector<std::string> Wildcard(const std::string& wildcardString);
228 
229  /// This is $SRT_PRIVATE_CONTEXT if a CAFAna directory exists there,
230  /// otherwise $SRT_PUBLIC_CONTEXT
231  std::string FindCAFAnaDir();
232 
233  /// Read list of input files from a text file, one per line
234  std::vector<std::string> LoadFileList(const std::string& listfile);
235 
236  /// \brief Extract map of metadata parameters from a CAF file
237  ///
238  /// \param dir The "meta" directory from the CAF file
239  /// \return A map from metadata field name to metadata value
240  std::map<std::string, std::string> GetCAFMetadata(TDirectory* dir);
241 
242  /// \brief \a base += \a add
243  ///
244  /// \param base The original source of strings, will be altered
245  /// \param add Strings to add to \a base if missing
246  /// \param mask Fields for which there was a conflict, will be altered
247  void CombineMetadata(std::map<std::string, std::string>& base,
248  const std::map<std::string, std::string>& add,
249  std::set<std::string>& mask);
250 
251  /// \brief Write map of metadata parameters into a CAF file
252  ///
253  /// \param dir The "meta" directory of the CAF file
254  /// \param meta Map from metadata field name to metadata value
255  void WriteCAFMetadata(TDirectory* dir,
256  const std::map<std::string, std::string>& meta);
257 
258  /// Is this a grid (condor) job?
259  bool RunningOnGrid();
260 
261  /// Value passed to --stride, or 1 if not specified
262  size_t Stride(bool allow_default = true);
263  /// Value passed to --offset, or 0 if not specified
264  size_t Offset(bool allow_default = true);
265  /// Value passed to --limit, or -1 if not specified
266  int Limit();
267 
268  /// What's the process number for a grid job?
269  size_t JobNumber();
270  size_t NumJobs();
271 
272  bool AlmostEqual(double a, double b);
273 
274  std::string pnfs2xrootd(std::string loc, bool unauth = false);
275 
276  // Calling this function will return a Fourier series, fit to the input
277  // histogram. Assumes x-axis covers one period
278  class FitToFourier
279  {
280  public:
281  FitToFourier(TH1* h, double xlo, double xhi, int NOsc);
282  ~FitToFourier();
283  TF1* Fit() const;
284  double operator()(double *x, double *par) const;
285  private:
286 
287  const TH1* fHist; // Histogram to fit
288  const double fxlo; // Lower bound
289  const double fxhi; // Upper bound - assumed to be 1 osc from the low end
290  const int fNOsc; // Highest harmonic to include
291 
292  };
293 
294  void EnsurePositiveDefinite(TH2* mat);
295 
296  /// Returns a masking histogram based on axis limits
297  TH1* GetMaskHist(const Spectrum& s,
298  double xmin=0, double xmax=-1,
299  double ymin=0, double ymax=-1);
300 
301  /// /param frac Quantile to find, eg 0.9
302  /// /param xs Values to search in -- this will be sorted
303  double FindQuantile(double frac, std::vector<double>& xs);
304 }
TH2D * MakeTH2D(const char *name, const char *title, const Binning &binsx, const Binning &binsy)
Represent the binning of a Spectrum&#39;s x-axis.
Definition: Binning.h:18
process_name opflash particleana ie x
size_t Offset(bool allow_default)
TH3 * ToTH3Helper(std::unique_ptr< TH1 > h1, const Binning &binsx, const Binning &binsy, const Binning &binsz, ana::EBinType bintype)
double LogLikelihood(double e, double o)
The log-likelihood formula for a single bin.
TH2 * ToTH2Helper(std::unique_ptr< TH1 > h1, const Binning &binsx, const Binning &binsy, ana::EBinType bintype)
void CombineMetadata(std::map< std::string, std::string > &base, const std::map< std::string, std::string > &add, std::set< std::string > &mask)
Divide bin contents by bin widths.
std::vector< std::string > Wildcard(const std::string &wildcardString)
double FindQuantile(double frac, std::vector< double > &xs)
std::unique_ptr< TMatrixD > SymmMxInverse(const TMatrixD &mx)
process_name opflashCryoW ana
std::string pnfs2xrootd(std::string loc, bool unauth)
ifdh calls between construction and destruction produce no output
void EnsurePositiveDefinite(TH2 *mat)
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:30
process_name gaushit a
process_name pandoraGausCryo1 vertexChargeCryo1 vertexStubCryo1 xmin
while getopts h
std::map< std::string, std::string > GetCAFMetadata(TDirectory *dir)
TH2F * ExpandedHistogram(const std::string &title, int nbinsx, double xmin, double xmax, bool xlog, int nbinsy, double ymin, double ymax, bool ylog)
constexpr mask_t< EnumType > mask(EnumType bit, OtherBits...otherBits)
Returns a mask with all specified bits set.
std::string Experiment()
$EXPERIMENT or a nice error message and abort
std::unique_ptr< TMatrixD > CalcCovMx(const std::vector< TArrayD * > &binSets, int firstBin, int lastBin)
Compute bin-to-bin covariance matrix from a collection of sets of bin contents.
EExposureType
For use as an argument to Spectrum::ToTH1.
void WriteCAFMetadata(TDirectory *dir, const std::map< std::string, std::string > &meta)
bool AlmostEqual(double a, double b)
TH2 * ToTH2(const Spectrum &s, double exposure, ana::EExposureType expotype, const Binning &binsx, const Binning &binsy, ana::EBinType bintype)
Represent the ratio between two spectra.
Definition: Ratio.h:8
TH1D * MakeTH1D(const char *name, const char *title, const Binning &bins)
tuple dir
Definition: dropbox.py:28
size_t Stride(bool allow_default)
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
std::string SAMExperiment()
$SAM_EXPERIMENT or a nice error message and abort
do i e
Alter floating-point exception flag.
then echo fcl name
std::vector< std::string > LoadFileList(const std::string &listfile)
Prevent histograms being added to the current directory.
TH3 * ToTH3(const Spectrum &s, double exposure, ana::EExposureType expotype, const Binning &binsx, const Binning &binsy, const Binning &binsz, ana::EBinType bintype)
esac echo uname r
TH1 * GetMaskHist(const Spectrum &s, double xmin, double xmax, double ymin, double ymax)
std::string UniqueName()
Return a different string each time, for creating histograms.
double Chi2CovMx(const TVectorD *e, const TVectorD *o, const TMatrixD *covmxinv)