DataMCPair.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include <iostream>
5 #include <string>
6 #include <vector>
7 #include <fstream>
8 #include <sstream>
9 
10 #include "CAFAna/Analysis/Style.h"
11 #include "TLegend.h"
12 
13 
15 #include "CAFAna/Core/Spectrum.h"
16 #include "CAFAna/Core/Var.h"
17 #include "CAFAna/Core/Cut.h"
19 #include "CAFAna/Vars/Vars.h" // sigh
20 
21 
22 namespace ana
23 {
24 
25  /// Format numbers for tables, with two nice digits after decimal
26  TString TableNum(float num);
27 
28 
29  // Replace all instances of from with to in str
31  const std::string& from,
32  const std::string& to);
33 
34  template<class T> class Tangible
35  {
36 
37  public:
38  Tangible(const T& obj, const std::string& shortName,
39  const std::string& blurb ):
40  fObj(obj),
41  fBlurb(blurb),
42  fShortName(shortName)
43  {};
44 
45  T fObj;
48 
49  };
50 
51 
54 
55  class DataMCPair
56  {
57  public:
58 
59  /// \brief \ref DataMCPair wraps up spectrum creation for data/MC comparison
60  ///
61  /// \param sel \ref Selection object (cut, blurb, name) for cuts
62  /// \param tanAxis \ref TangibleAxis object (\ref HistAxis, blurb, name)
63  /// \param loaderData \ref SpectrumLoader for data
64  /// \param loaderMC \ref SpectrumLoader for MC
65  /// \param wei Weight to apply to events
66  /// \param systs std::vector of ISyst* for drawing plots with syst bands
67  /// \param bkg \ref Cut object to define background
68  DataMCPair(Selection sel, TangibleAxis tanAxis,
69  SpectrumLoaderBase& loaderData, SpectrumLoaderBase& loaderMC,
70  const Var & wei = kUnweighted,
71  const std::vector<const ISyst*>& systs = {},
72  const std::map<std::string, SpectrumComponents::Component>& mcCategories = {});
73 
74  /// \brief \ref DataMCPair wraps up spectrum creation for data/MC comparison
75  /// This version simply takes a \ref Cut and \ref Var, leaving the
76  /// ShortName and Blurb blank for both.
77  ///
78  /// \param cut \ref Cut object to define selection
79  /// \param axis \ref HistAxis object to set up histograms
80  /// \param loaderData \ref SpectrumLoader for data
81  /// \param loaderMC \ref SpectrumLoader for MC
82  /// \param wei Weight to apply to events
83  /// \param systs std::vector of ISyst* for drawing plots with syst bands
84  /// \param bkg \ref Cut object to define background
86  SpectrumLoaderBase& loaderData, SpectrumLoaderBase& loaderMC,
87  const ana::Var& wei = kUnweighted,
88  const std::vector<const ISyst*>& systs = {},
89  const std::map<std::string, SpectrumComponents::Component>& mcCategories = {});
90 
91  /// Constructor from copies
93  const SpectrumComponents& MC,
94  const std::vector<Spectrum>& ups,
95  const std::vector<Spectrum>& downs,
96  const std::string& shortName)
97  : fSel(kNoCut, "", ""),
98  fData(data), fMC(MC),
99  fUps(ups), fDowns(downs),
100  fShortName(shortName)
101  {}
102 
103  /// Constructor for use in LoadFrom()
105  SpectrumComponents&& MC,
106  std::vector<Spectrum>&& ups,
107  std::vector<Spectrum>&& downs,
109  : fSel(kNoCut, "", ""),
110  fData(data), fMC(MC),
111  fUps(ups), fDowns(downs),
112  fShortName(shortName)
113  {}
114 
116  {return fShortName;};
117  const char* CName() const
118  {return fShortName.c_str();};
119 
120  /// \brief Draw MC with error band
121  ///
122  /// \param iSyst index of syst in the syst vector
123  void DrawMCSyst(const int iSyst = -1, EBinType bintype = kBinContent) const;
124 
125  /// \brief Draw data and MC with error band
126  ///
127  /// \param iSyst index of syst in the syst vector
128  void OverlayDataMCSyst(const int iSyst = -1, bool drawMCBreakdown=true, EBinType bintype = kBinContent, bool drawLegend = true) const;
129 
130  /// \brief Draw MC with error band, but with each syst normalized
131  /// to total area of MC to highlight shape differences.
132  ///
133  /// \param iSyst index of syst in the syst vector
134  void DrawMCNormSyst(const int iSyst = -1, EBinType bintype = kBinContent) const;
135 
136  /// \brief Draw data and MC with error band, but with each syst normalized
137  /// to total area of MC to highlight shape differences.
138  ///
139  /// \param iSyst index of syst in the syst vector
140  void OverlayDataMCSystNorm(const int iSyst = -1, bool drawMCBreakdown=true, EBinType bintype = kBinContent, bool drawLegend = true) const;
141 
142  /// \brief Draw legend on plots, mostly for internal use
143  ///
144  /// \param includeComponents Add legend entries for MC subcomponents?
145  /// \param drawThreshold Only draw MC components that are larger than this fraction of the total MC
146  TLegend* DrawLegend(const int dataColor=kBlack, bool includeComponents=true, double drawThreshold=-1) const;
147 
148  /// \brief Write exposure on plots, used internally
149  void AddExposure() const;
150 
151  /// \brief Draw data on plots, mostly for internal use
152  ///
153  /// \param color color of data for legend points
154  void DrawData(const int color=kBlack, EBinType bintype = kBinContent)const;
155 
156  /// \brief Draw MC components distribution
157  ///
158  /// \param stack By default components will be shown stacked. Pass false to show them unstacked instead.
159  void DrawMCComponents(bool stacked=true, EBinType bintype = kBinContent) const;
160 
161  /// \brief Purity of the MC selection based on the MC subcategories.
162  ///
163  /// \param signalCatNames If any of your subcategories are "signal", pass them here to avoid counting them as background
164  float Purity(const std::set<std::string>& signalCatNames={}) const;
165 
166  /// \brief Write LaTeX table of syst means: |par|mean up(%)|mean down(%)|
167  ///
168  /// /param fname Path where output file will be written
169  void CreateSystTable(const std::string& fname)const;
170 
171  void SaveTo(TDirectory* dir, const std::string& name) const;
172  static std::unique_ptr<DataMCPair> LoadFrom(TDirectory* dir, const std::string& name);
173 
174 
175  unsigned int NDimensions() const { return fMC.NDimensions(); }
176  std::vector<std::string> GetLabels() const { return fMC.GetLabels(); }
177  std::vector<Binning> GetBinnings() const { return fMC.GetBinnings(); }
178 
179  void SetComponentColor(const std::string& name, int color);
180  void SetComponentBlurb(const std::string & name, const std::string & blurb);
181 
182  Selection fSel;
185  std::vector<Spectrum> fUps;
186  std::vector<Spectrum> fDowns;
188  const std::vector<const ISyst*> fSysts;
189 
190  };
191 
192 
193 }
TString TableNum(float num)
Format numbers for tables, with two nice digits after decimal.
Definition: DataMCPair.cxx:42
const XML_Char * name
Definition: expat.h:151
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
std::string fShortName
Definition: DataMCPair.h:187
Spectrum fData
Definition: DataMCPair.h:183
std::vector< SystGroupDef > systs
Definition: syst_header.h:385
void SaveTo(const osc::IOscCalc &x, TDirectory *dir, const std::string &name)
std::string fShortName
Definition: DataMCPair.h:47
Tangible< Cut > Selection
Definition: DataMCPair.h:52
std::vector< Spectrum > fDowns
Definition: DataMCPair.h:186
DataMCPair(const Spectrum &data, const SpectrumComponents &MC, const std::vector< Spectrum > &ups, const std::vector< Spectrum > &downs, const std::string &shortName)
Constructor from copies.
Definition: DataMCPair.h:92
unsigned int NDimensions() const
Definition: DataMCPair.h:175
std::unique_ptr< T > LoadFrom(TDirectory *dir, const std::string &label)
Definition: LoadFromFile.h:17
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:40
const std::vector< const ISyst * > fSysts
Definition: DataMCPair.h:188
const XML_Char const XML_Char * data
Definition: expat.h:268
EBinType
Definition: UtilsExt.h:28
std::vector< std::string > GetLabels() const
Definition: DataMCPair.h:176
void DrawLegend(LegendMap &legobj, int textCol=kBlack)
DataMCPair(Spectrum &&data, SpectrumComponents &&MC, std::vector< Spectrum > &&ups, std::vector< Spectrum > &&downs, std::string shortName)
Constructor for use in LoadFrom()
Definition: DataMCPair.h:104
Tangible< HistAxis > TangibleAxis
Definition: DataMCPair.h:53
const char * CName() const
Definition: DataMCPair.h:117
Regular histogram.
Definition: UtilsExt.h:30
std::string ShortName() const
Definition: DataMCPair.h:115
std::string fBlurb
Definition: DataMCPair.h:46
Base class for the various types of spectrum loader.
const Cut cut
Definition: exporter_fd.C:30
std::vector< Spectrum > fUps
Definition: DataMCPair.h:185
Selection fSel
Definition: DataMCPair.h:182
TDirectory * dir
Definition: macro.C:5
int num
Definition: f2_nu.C:119
Tangible(const T &obj, const std::string &shortName, const std::string &blurb)
Definition: DataMCPair.h:38
std::string replaceAll(const std::string &str, const std::string &from, const std::string &to)
Definition: DataMCPair.cxx:27
string shortName
THUMBNAIL BLOCK: We need to make a thumbnail for each.
std::vector< Binning > GetBinnings() const
Definition: DataMCPair.h:177
double T
Definition: Xdiff_gwt.C:5
SpectrumComponents fMC
Definition: DataMCPair.h:184
const Var kUnweighted
The simplest possible Var, always 1. Used as a default weight.
Definition: Var.h:96
const Cut kNoCut
The simplest possible cut: pass everything, used as a default.
Definition: Cut.h:109
enum BeamMode string