SpectrumComponents.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <string>
5 
6 #include "CAFAna/Core/Cut.h"
7 #include "CAFAna/Core/Spectrum.h"
9 #include "CAFAna/Core/Var.h"
10 
11 class TLegend;
12 
13 namespace ana
14 {
15 
17  {
18  public:
19  struct Component
20  {
21  Component(const Cut& cut_, const std::string & blurb_="", int color_=kBlue)
22  : cut(cut_), blurb(blurb_), color(color_)
23  {}
24 
27  int color;
28  };
29 
30  /// \brief \ref SpectrumComponents makes a collection of Spectra differing by a single (often truth) Cut.
31  ///
32  /// \param baseCut the selection cut shared between all components
33  /// \param axis \ref HistAxis shared between all components
34  /// \param loaderData \ref SpectrumLoader for data
35  /// \param loaderMC \ref SpectrumLoader for MC
36  /// \param wei Weight to apply to events
37  /// \param systs std::vector of ISyst* for drawing plots with syst bands
38  /// \param bkg \ref Cut object to define background
39  /// \param mcCategories the various \ref Cut s defining each category
40  SpectrumComponents(const ana::Cut& baseCut,
41  const ana::HistAxis & axis,
43  const Var & wei = kUnweighted,
44  const std::map<std::string, Component>& categories = {});
45 
46  SpectrumComponents(const ana::Cut& baseCut,
47  const ana::MultiVarHistAxis & axis,
48  ana::SpectrumLoaderBase & loader,
49  const Var & wei = kUnweighted,
50  const std::map<std::string, Component>& categories = {});
51 
52  /// Constructor for use in LoadFrom()
54  std::map<std::string, Component>&& compDescrs,
55  std::map<std::string, Spectrum>&& compSpecs)
56  : fSel(kNoCut),
57  fTot(tot),
58  fComponentDescr(compDescrs),
59  fComponentSpecs(compSpecs)
60  {}
61 
62  /// Constructor from copies...
64  const std::map<std::string, Component>& compDescrs,
65  const std::map<std::string, Spectrum>& compSpecs)
66  : fSel(kNoCut),
67  fTot(tot),
68  fComponentDescr(compDescrs),
69  fComponentSpecs(compSpecs)
70  {}
71 
72  /// \brief Draw legend on plots
73  /// \param drawThreshold Only create entries for histograms whose integral is greater than this fraction of the total
74  TLegend* DrawLegend(double drawThreshold = 0, TLegend * leg = nullptr) const;
75 
76  /// \brief Draw MC components distribution
77  ///
78  /// \param stack By default components will be shown stacked. Pass false to show them unstacked instead.
79  /// \param bintype Raw bin contents or bin-width normalized?
80  /// \param POT Normalize to specific POT? Otherwise use internal Spectrum POT
81  /// \param sameAll Draw all hists with "same" option?
82  void DrawComponents(bool stacked=true, EBinType bintype = kBinContent, double POT=0, bool sameAll=true) const;
83 
84  /// \brief Purity of the MC selection based on the MC subcategories.
85  ///
86  /// \param signalCatNames If any of your subcategories are "signal", pass them here to avoid counting them as background
87  float Purity(const std::set<std::string>& signalCatNames={}) const;
88 
89  void SaveTo(TDirectory* dir, const std::string& name) const;
90  static std::unique_ptr<SpectrumComponents> LoadFrom(TDirectory* dir, const std::string& name);
91 
92  const ana::Cut & Sel() const { return fSel; };
93 
94  Spectrum & Tot() { return fTot; };
95  const Spectrum & Tot() const { return fTot; };
96 
97  unsigned int NDimensions() const { return fTot.NDimensions(); }
98  std::vector<std::string> GetLabels() const { return fTot.GetLabels(); }
99  std::vector<Binning> GetBinnings() const { return fTot.GetBinnings(); }
100 
101  const std::map<std::string, Component> & ComponentDescrs() const { return fComponentDescr; };
102  std::map<std::string, Component> & ComponentDescrs() { return fComponentDescr; };
103  const std::map<std::string, Spectrum> & ComponentSpecs() const { return fComponentSpecs; };
104 
105  private:
106  ana::Cut fSel;
108  std::map<std::string, Component> fComponentDescr;
109  std::map<std::string, Spectrum> fComponentSpecs;
110 
111  };
112 }
const std::vector< Binning > & GetBinnings() const
Definition: Spectrum.h:264
const XML_Char * name
Definition: expat.h:151
const Spectrum & Tot() const
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
static std::unique_ptr< SpectrumComponents > LoadFrom(TDirectory *dir, const std::string &name)
std::map< std::string, Component > & ComponentDescrs()
std::map< std::string, Component > fComponentDescr
std::vector< Binning > GetBinnings() const
float Purity(const std::set< std::string > &signalCatNames={}) const
Purity of the MC selection based on the MC subcategories.
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:40
EBinType
Definition: UtilsExt.h:28
Component(const Cut &cut_, const std::string &blurb_="", int color_=kBlue)
TLegend * DrawLegend(double drawThreshold=0, TLegend *leg=nullptr) const
Draw legend on plots.
SpectrumComponents(Spectrum &&tot, std::map< std::string, Component > &&compDescrs, std::map< std::string, Spectrum > &&compSpecs)
Constructor for use in LoadFrom()
void DrawComponents(bool stacked=true, EBinType bintype=kBinContent, double POT=0, bool sameAll=true) const
Draw MC components distribution.
unsigned int NDimensions() const
Definition: Spectrum.h:262
loader
Definition: demo0.py:10
const ana::Cut & Sel() const
dictionary categories
Naming schema histograms should follow a logical naming convention: <category>-<observable>-<reconstr...
Definition: naming_schema.py:9
Regular histogram.
Definition: UtilsExt.h:30
SpectrumComponents(const Spectrum &tot, const std::map< std::string, Component > &compDescrs, const std::map< std::string, Spectrum > &compSpecs)
Constructor from copies...
std::vector< std::string > GetLabels() const
std::vector< double > POT
Base class for the various types of spectrum loader.
TDirectory * dir
Definition: macro.C:5
SpectrumComponents(const ana::Cut &baseCut, const ana::HistAxis &axis, ana::SpectrumLoaderBase &loader, const Var &wei=kUnweighted, const std::map< std::string, Component > &categories={})
SpectrumComponents makes a collection of Spectra differing by a single (often truth) Cut...
std::map< std::string, Spectrum > fComponentSpecs
unsigned int NDimensions() const
const std::vector< std::string > & GetLabels() const
Definition: Spectrum.h:263
enum BeamMode kBlue
const Var kUnweighted
The simplest possible Var, always 1. Used as a default weight.
Definition: Var.h:96
const std::map< std::string, Component > & ComponentDescrs() const
const std::map< std::string, Spectrum > & ComponentSpecs() const
const Cut kNoCut
The simplest possible cut: pass everything, used as a default.
Definition: Cut.h:109
void SaveTo(TDirectory *dir, const std::string &name) const
enum BeamMode string