CutOptimization.h
Go to the documentation of this file.
1 #pragma once
2 #include <map>
3 
5 #include "CAFAna/Core/Cut.h"
6 #include "CAFAna/Core/HistAxis.h"
8 #include "CAFAna/Core/Spectrum.h"
10 #include "CAFAna/XSec/Multiverse.h"
11 
12 #include "TString.h"
13 #include "TDirectory.h"
14 #include "TObjString.h"
15 #include "TCanvas.h"
16 #include "TH1.h"
17 #include "TLegend.h"
18 
19 namespace ana
20 {
22  {
23  public:
24 
25  // static functions for plotting histograms that get saved to the
26  // debug file
27  static void PlotDebug(TH1 * hist,
31 
32  static void PlotDebug(std::vector<TH1 *> hists,
33  std::vector<std::string> labels,
34  std::string draw_option,
35  std::string title,
36  std::string name,
37  double ymax = -1);
38 
39  static void PlotDebug(const std::vector<TH1 *> universes,
40  Spectrum * nominal,
41  std::string title,
42  std::string name);
43 
44  static void PlotDebug(const std::vector<TH1 *> universes,
45  TH1 * h_nominal,
46  std::string title,
47  std::string name);
48 
50  kBinByBin, // bin by bin
51  kIntegratedUp, // integrate bin and above
52  kIntegratedDown, // integrate bin and below
53  kIntegratedInsideBounds, // integrate between upper and lower bounds
54  };
55  enum FOM_t {
57  };
58 
59  CutOptimization(const HistAxis * hist_axis,
60  const Cut * signal_cut,
61  const Cut * selection_cut);
62 
63  void DefineNominal(SystematicDef * nominal);
64  void DefineSystematic(SystematicDef * syst);
65  void DefineMultiverseSystematic(SystematicDef * syst, std::vector<Var> mv_weights);
66  void DefineMultiverseSystematic(SystematicDef * syst, std::vector<SystShifts> mv_shifts);
67 
68  void SetSpillCuts(const SpillCut &);
69 
70  void Go();
71  void SaveTo(TDirectory * dir, const std::string& name) const;
72 
73  static std::unique_ptr<CutOptimization> LoadFrom(TDirectory * dir, const std::string& name);
74  CutOptimization(const CutOptimization & rhs);
76 
79  ~CutOptimization() = default;
80 
81  void Optimize(OptimizeMethod_t method, FOM_t fom, double data_pot,
82  TDirectory * save_dir, std::string plot_dump = ".",
83  bool debug = false);
84 
85 
86  CutOptimization(Spectrum * nominal_signal,
87  Spectrum * nominal_bkgd,
88  Spectrum * nominal_selected_signal,
89  Spectrum * nominal_selected_bkgd,
90  Spectrum * nominal_selected,
91  std::vector<SystematicDef*> syst_defs,
92  std::map<SystematicDef*, UpDownPair<Spectrum > > syst_signal,
93  std::map<SystematicDef*, UpDownPair<Spectrum > > syst_bkgd,
94  std::map<SystematicDef*, UpDownPair<Spectrum > > syst_selected_signal,
95  std::map<SystematicDef*, UpDownPair<Spectrum > > syst_selected_bkgd,
96  std::map<SystematicDef*, UpDownPair<Spectrum > > syst_selected,
97  std::vector<SystematicDef*> mv_defs,
98  std::map<SystematicDef*, Multiverse * > mv_signal,
99  std::map<SystematicDef*, Multiverse * > mv_bkgd,
100  std::map<SystematicDef*, Multiverse * > mv_selected_signal,
101  std::map<SystematicDef*, Multiverse * > mv_selected_bkgd,
102  std::map<SystematicDef*, Multiverse * > mv_selected);
103  static void Integrate(TH1 *& result, const TH1 * tmp, OptimizeMethod_t method);
104  static void Integrate(Multiverse * mv, OptimizeMethod_t method);
105 
106  std::vector<SystematicDef*> GetSystDefs() { return fSystDefs; }
107  std::vector<SystematicDef*> GetMVSystDefs() { return fMVSystDefs; }
108  std::vector<SystematicDef*> GetAllSystDefs() {
109  std::vector<SystematicDef*> ret = fSystDefs;
110  ret.insert(fSystDefs.end(), fMVSystDefs.begin(), fMVSystDefs.end());
111  return ret;
112  }
113 
114  private:
115  TH1 * AbsUncertainty (const UpDownPair<TH1> syst, const TH1 * nominal) const;
116  TH1 * AbsUncertaintySquared(const UpDownPair<TH1> syst, const TH1 * nominal) const;
117 
118  static TH1 * ToHist(const Spectrum * spec, OptimizeMethod_t method, double POT = -1);
119  UpDownPair<TH1> ToUpDownHist(Multiverse * mv , const TH1 * h_nominal);
120 
121  void OptimizedSigmaOverSigma(OptimizeMethod_t method, double data_pot,
122  TDirectory * save_dir, std::string plot_dump = ".",
123  bool debug = false);
124 
125  static TH1 * Efficiency(TH1 * num, TH1 * denom, OptimizeMethod_t method);
127  Multiverse * Efficiency(Multiverse * num, Multiverse * denom, OptimizeMethod_t method);
128 
135 
136  std::vector<SystematicDef*> fSystDefs; // todo change to set
137  std::map< SystematicDef*, UpDownPair<Spectrum> > fSignal;
138  std::map< SystematicDef*, UpDownPair<Spectrum> > fBkgd;
139  std::map< SystematicDef*, UpDownPair<Spectrum> > fSelectedSignal;
140  std::map< SystematicDef*, UpDownPair<Spectrum> > fSelectedBkgd;
141  std::map< SystematicDef*, UpDownPair<Spectrum> > fSelected;
142 
143  std::vector<SystematicDef*> fMVSystDefs;
144  std::map< SystematicDef*, Multiverse *> fMVSignal;
145  std::map< SystematicDef*, Multiverse *> fMVBkgd;
146  std::map< SystematicDef*, Multiverse *> fMVSelectedSignal;
147  std::map< SystematicDef*, Multiverse *> fMVSelectedBkgd;
148  std::map< SystematicDef*, Multiverse *> fMVSelected;
149 
150 
151  const Cut * fSignalCut = NULL;
152  const Cut * fSelectionCut = NULL;
153  const HistAxis * fOptiHistAxis = NULL;
154  };
155 
157  {
159  if(copy.up) ret.up = new Spectrum(*copy.up);
160  if(copy.down) ret.down = new Spectrum(*copy.down);
161  return ret;
162  }
163 
165  {
167  if(copy.up) ret.up = std::move(copy.up);
168  if(copy.down) ret.down = std::move(copy.down);
169  return ret;
170  }
171 
173  TDirectory * dir)
174  {
175  TDirectory * tmp = gDirectory;
176  dir->cd();
177  TObjString("UpDownPair").Write("type");
178 
179  if(save.IsTwoSided()) {
180  save.up->SaveTo(dir, "up");
181  save.down->SaveTo(dir, "down");
182  }
183  else {
184  save.up->SaveTo(dir, "onesided");
185  }
186  tmp->cd();
187  }
188 
190  {
191  TObjString * ptag = (TObjString*) dir->Get("type");
192  assert(ptag->GetString() == "UpDownPair");
193 
194  if(dir->GetDirectory("onesided")) {
195  return UpDownPair<Spectrum>{Spectrum::LoadFrom(dir, "onesided").release()};
196  }
197  else {
198  return UpDownPair<Spectrum>
199  {Spectrum::LoadFrom(dir, "up").release(),
200  Spectrum::LoadFrom(dir, "down").release()};
201  }
202  }
203 
204 }
std::vector< SystematicDef * > GetMVSystDefs()
const XML_Char * name
Definition: expat.h:151
UpDownPair< TH1 > ToUpDownHist(Multiverse *mv, const TH1 *h_nominal)
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
bool IsTwoSided() const
Definition: SystematicDef.h:18
std::map< SystematicDef *, UpDownPair< Spectrum > > fSignal
std::map< SystematicDef *, UpDownPair< Spectrum > > fSelectedBkgd
CutOptimization(const HistAxis *hist_axis, const Cut *signal_cut, const Cut *selection_cut)
const HistAxis * fOptiHistAxis
static std::unique_ptr< CutOptimization > LoadFrom(TDirectory *dir, const std::string &name)
std::map< SystematicDef *, Multiverse * > fMVBkgd
static TH1 * ToHist(const Spectrum *spec, OptimizeMethod_t method, double POT=-1)
std::map< SystematicDef *, UpDownPair< Spectrum > > fBkgd
Float_t tmp
Definition: plot.C:36
TString hists[nhists]
Definition: bdt_com.C:3
void DefineNominal(SystematicDef *nominal)
UpDownPair< Spectrum > CopyUpDownSpectrum(const UpDownPair< Spectrum > &copy)
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:40
CutOptimization & operator=(const CutOptimization &rhs)
static void PlotDebug(TH1 *hist, std::string draw_option, std::string title, std::string name)
Double_t ymax
Definition: plot.C:25
static std::unique_ptr< Spectrum > LoadFrom(TDirectory *dir, const std::string &name)
Definition: Spectrum.cxx:546
std::map< SystematicDef *, Multiverse * > fMVSelectedBkgd
void SaveTo(TDirectory *dir, const std::string &name) const
void DefineMultiverseSystematic(SystematicDef *syst, std::vector< Var > mv_weights)
static TH1 * Efficiency(TH1 *num, TH1 *denom, OptimizeMethod_t method)
Spectrum * fNominalSelectedSignal
void SetSpillCuts(const SpillCut &)
static void Integrate(TH1 *&result, const TH1 *tmp, OptimizeMethod_t method)
std::vector< float > Spectrum
Definition: Constants.h:570
std::map< SystematicDef *, UpDownPair< Spectrum > > fSelectedSignal
void save(bool IsData, bool Bad)
Definition: cellShifts.C:453
std::vector< double > POT
void Optimize(OptimizeMethod_t method, FOM_t fom, double data_pot, TDirectory *save_dir, std::string plot_dump=".", bool debug=false)
void OptimizedSigmaOverSigma(OptimizeMethod_t method, double data_pot, TDirectory *save_dir, std::string plot_dump=".", bool debug=false)
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
std::vector< SystematicDef * > GetSystDefs()
~CutOptimization()=default
TDirectory * dir
Definition: macro.C:5
void SaveToUpDownSpectra(const UpDownPair< Spectrum > &save, TDirectory *dir)
void DefineSystematic(SystematicDef *syst)
int num
Definition: f2_nu.C:119
std::map< SystematicDef *, Multiverse * > fMVSelectedSignal
std::map< SystematicDef *, UpDownPair< Spectrum > > fSelected
assert(nhit_max >=nhit_nbins)
std::vector< SystematicDef * > fSystDefs
UpDownPair< Spectrum > LoadFromUpDownSpectra(TDirectory *dir)
std::vector< SystematicDef * > GetAllSystDefs()
TH1 * AbsUncertaintySquared(const UpDownPair< TH1 > syst, const TH1 *nominal) const
std::map< SystematicDef *, Multiverse * > fMVSelected
std::vector< SystematicDef * > fMVSystDefs
SystematicDef * fNominalSystDef
Spectrum * fNominalSelectedBkgd
std::map< SystematicDef *, Multiverse * > fMVSignal
TH1 * AbsUncertainty(const UpDownPair< TH1 > syst, const TH1 *nominal) const