Classes | Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
ana::SpectrumComponents Class Reference

#include "/cvmfs/nova-development.opensciencegrid.org/novasoft/releases/N20-12-02/CAFAna/Analysis/SpectrumComponents.h"

Classes

struct  Component
 

Public Member Functions

 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. More...
 
 SpectrumComponents (const ana::Cut &baseCut, const ana::MultiVarHistAxis &axis, ana::SpectrumLoaderBase &loader, const Var &wei=kUnweighted, const std::map< std::string, Component > &categories={})
 
 SpectrumComponents (Spectrum &&tot, std::map< std::string, Component > &&compDescrs, std::map< std::string, Spectrum > &&compSpecs)
 Constructor for use in LoadFrom() More...
 
 SpectrumComponents (const Spectrum &tot, const std::map< std::string, Component > &compDescrs, const std::map< std::string, Spectrum > &compSpecs)
 Constructor from copies... More...
 
TLegend * DrawLegend (double drawThreshold=0, TLegend *leg=nullptr) const
 Draw legend on plots. More...
 
void DrawComponents (bool stacked=true, EBinType bintype=kBinContent, double POT=0, bool sameAll=true) const
 Draw MC components distribution. More...
 
float Purity (const std::set< std::string > &signalCatNames={}) const
 Purity of the MC selection based on the MC subcategories. More...
 
void SaveTo (TDirectory *dir, const std::string &name) const
 
const ana::CutSel () const
 
SpectrumTot ()
 
const SpectrumTot () const
 
unsigned int NDimensions () const
 
std::vector< std::stringGetLabels () const
 
std::vector< BinningGetBinnings () const
 
const std::map< std::string, Component > & ComponentDescrs () const
 
std::map< std::string, Component > & ComponentDescrs ()
 
const std::map< std::string, Spectrum > & ComponentSpecs () const
 

Static Public Member Functions

static std::unique_ptr< SpectrumComponentsLoadFrom (TDirectory *dir, const std::string &name)
 

Private Attributes

ana::Cut fSel
 
Spectrum fTot
 
std::map< std::string, ComponentfComponentDescr
 
std::map< std::string, SpectrumfComponentSpecs
 

Detailed Description

Definition at line 16 of file SpectrumComponents.h.

Constructor & Destructor Documentation

ana::SpectrumComponents::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.

Parameters
baseCutthe selection cut shared between all components
axisHistAxis shared between all components
loaderDataSpectrumLoader for data
loaderMCSpectrumLoader for MC
weiWeight to apply to events
systsstd::vector of ISyst* for drawing plots with syst bands
bkgCut object to define background
mcCategoriesthe various Cut s defining each category

Definition at line 19 of file SpectrumComponents.cxx.

References fComponentDescr, fComponentSpecs, fSel, and ana::kNoShift.

24  :
25  fSel(baseCut),
26  fTot(loader, axis, fSel, kNoShift, wei)
27  {
28  if (!categories.empty())
29  {
30  for (const auto & componentPair : categories)
31  {
32  fComponentDescr.emplace(componentPair.first, componentPair.second);
33  fComponentSpecs.emplace(std::piecewise_construct,
34  std::forward_as_tuple(componentPair.first),
35  std::forward_as_tuple(loader, axis, fSel && componentPair.second.cut, kNoShift, wei));
36  }
37  }
38  }
std::map< std::string, Component > fComponentDescr
dictionary categories
Naming schema histograms should follow a logical naming convention: <category>-<observable>-<reconstr...
Definition: naming_schema.py:9
const SystShifts kNoShift
Definition: SystShifts.cxx:22
std::map< std::string, Spectrum > fComponentSpecs
ana::SpectrumComponents::SpectrumComponents ( const ana::Cut baseCut,
const ana::MultiVarHistAxis axis,
ana::SpectrumLoaderBase loader,
const Var wei = kUnweighted,
const std::map< std::string, Component > &  categories = {} 
)

Definition at line 41 of file SpectrumComponents.cxx.

References fComponentDescr, fComponentSpecs, fSel, and ana::kNoShift.

46  :
47  fSel(baseCut),
48  fTot(loader, axis, fSel, kNoShift, wei)
49  {
50  if (!categories.empty())
51  {
52  for (const auto & componentPair : categories)
53  {
54  fComponentDescr.emplace(componentPair.first, componentPair.second);
55  fComponentSpecs.emplace(std::piecewise_construct,
56  std::forward_as_tuple(componentPair.first),
57  std::forward_as_tuple(loader, axis, fSel && componentPair.second.cut, kNoShift, wei));
58  }
59  }
60  }
std::map< std::string, Component > fComponentDescr
dictionary categories
Naming schema histograms should follow a logical naming convention: <category>-<observable>-<reconstr...
Definition: naming_schema.py:9
const SystShifts kNoShift
Definition: SystShifts.cxx:22
std::map< std::string, Spectrum > fComponentSpecs
ana::SpectrumComponents::SpectrumComponents ( Spectrum &&  tot,
std::map< std::string, Component > &&  compDescrs,
std::map< std::string, Spectrum > &&  compSpecs 
)
inline

Constructor for use in LoadFrom()

Definition at line 53 of file SpectrumComponents.h.

56  : fSel(kNoCut),
57  fTot(tot),
58  fComponentDescr(compDescrs),
59  fComponentSpecs(compSpecs)
60  {}
std::map< std::string, Component > fComponentDescr
std::map< std::string, Spectrum > fComponentSpecs
const Cut kNoCut
The simplest possible cut: pass everything, used as a default.
Definition: Cut.h:109
ana::SpectrumComponents::SpectrumComponents ( const Spectrum tot,
const std::map< std::string, Component > &  compDescrs,
const std::map< std::string, Spectrum > &  compSpecs 
)
inline

Constructor from copies...

Definition at line 63 of file SpectrumComponents.h.

References dir, DrawComponents(), DrawLegend(), ana::kBinContent, MECModelEnuComparisons::leg, LoadFrom(), POT, Purity(), SaveTo(), and string.

66  : fSel(kNoCut),
67  fTot(tot),
68  fComponentDescr(compDescrs),
69  fComponentSpecs(compSpecs)
70  {}
std::map< std::string, Component > fComponentDescr
std::map< std::string, Spectrum > fComponentSpecs
const Cut kNoCut
The simplest possible cut: pass everything, used as a default.
Definition: Cut.h:109

Member Function Documentation

const std::map<std::string, Component>& ana::SpectrumComponents::ComponentDescrs ( ) const
inline

Definition at line 101 of file SpectrumComponents.h.

References fComponentDescr.

Referenced by ana::DataMCPair::DrawLegend(), ana::DataMCPair::SetComponentBlurb(), and ana::DataMCPair::SetComponentColor().

101 { return fComponentDescr; };
std::map< std::string, Component > fComponentDescr
std::map<std::string, Component>& ana::SpectrumComponents::ComponentDescrs ( )
inline

Definition at line 102 of file SpectrumComponents.h.

References fComponentDescr.

102 { return fComponentDescr; };
std::map< std::string, Component > fComponentDescr
const std::map<std::string, Spectrum>& ana::SpectrumComponents::ComponentSpecs ( ) const
inline

Definition at line 103 of file SpectrumComponents.h.

103 { return fComponentSpecs; };
std::map< std::string, Spectrum > fComponentSpecs
void ana::SpectrumComponents::DrawComponents ( bool  stacked = true,
EBinType  bintype = kBinContent,
double  POT = 0,
bool  sameAll = true 
) const

Draw MC components distribution.

Parameters
stackBy default components will be shown stacked. Pass false to show them unstacked instead.
bintypeRaw bin contents or bin-width normalized?
POTNormalize to specific POT? Otherwise use internal Spectrum POT
sameAllDraw all hists with "same" option?

Definition at line 103 of file SpectrumComponents.cxx.

References fComponentDescr, fComponentSpecs, make_syst_table_plots::h, hists, ana::kPOT, MECModelEnuComparisons::opt, and string.

Referenced by ana::DataMCPair::DrawMCComponents(), and SpectrumComponents().

104  {
105  if (POT == 0)
106  POT = fComponentSpecs.begin()->second.POT();
107 
108  TH1 * last = nullptr;
109  std::vector<TH1*> hists;
110 
111  // iterate backwards so that they're drawn in the specified order from top to bottom
112  for (auto it_comp = fComponentSpecs.rbegin(); it_comp != fComponentSpecs.rend(); it_comp++)
113  {
114  auto h = it_comp->second.ToTH1(POT, kBlack, kSolid, kPOT, bintype);
115  if (stacked)
116  {
117  if (last)
118  h->Add(last);
119  h->SetFillColor(fComponentDescr.at(it_comp->first).color);
120  h->SetLineWidth(1);
121  last = h;
122  }
123 
124  hists.push_back(h);
125  }
126  bool drawn = false;
127  for (auto it_hist = hists.rbegin(); it_hist != hists.rend(); it_hist++)
128  {
129  std::string opt = "hist";
130  if (drawn || sameAll)
131  opt += " same";
132  (*it_hist)->Draw(opt.c_str());
133  if (!drawn)
134  drawn = true;
135  }
136  }
std::map< std::string, Component > fComponentDescr
TString hists[nhists]
Definition: bdt_com.C:3
std::vector< double > POT
std::map< std::string, Spectrum > fComponentSpecs
enum BeamMode string
TLegend * ana::SpectrumComponents::DrawLegend ( double  drawThreshold = 0,
TLegend *  leg = nullptr 
) const

Draw legend on plots.

Parameters
drawThresholdOnly create entries for histograms whose integral is greater than this fraction of the total

Definition at line 63 of file SpectrumComponents.cxx.

References ana::assert(), ana::AutoPlaceLegend(), fComponentDescr, fComponentSpecs, fTot, ana::Spectrum::Integral(), MECModelEnuComparisons::leg, and ana::Spectrum::POT().

Referenced by ana::DataMCPair::DrawLegend(), and SpectrumComponents().

64  {
65  if (!leg)
66  leg = AutoPlaceLegend(0.25, 0.18 + 0.04*fComponentDescr.size());
67 
68  double thresh = 0;
69  if (drawThreshold >= 0 && drawThreshold <= 1.0)
70  thresh = fTot.Integral(fTot.POT()) * drawThreshold;
71  else
72  // this is supposed to be a fraction
73  assert(drawThreshold <= 1 && "SpectrumComponents::DrawLegend(): legend draw thresholds must be between 0.0 and 1.0");
74  for (const auto & mcSpecPair : fComponentDescr)
75  {
76  auto spec = fComponentSpecs.at(mcSpecPair.first);
77  if (spec.Integral(spec.POT()) < thresh)
78  continue;
79 
80  TH1F* comp = new TH1F();
81  comp->SetLineColor(kBlack);
82  comp->SetFillColor(mcSpecPair.second.color);
83  leg->AddEntry((TObject*)comp, mcSpecPair.second.blurb.c_str(), "f");
84  }
85 
86 
87  leg->SetFillColor(0);
88  leg->SetFillStyle(0);
89 
90  for(const auto& obj:*leg->GetListOfPrimitives())
91  {
92  if(obj->InheritsFrom("TAttFill")){
93  ((TAttFill*)obj)->SetFillStyle(0);
94  //((TAttFill*)obj)->SetFillColor(0);
95  }
96 
97  }
98  leg->Draw();
99  return leg;
100  }
std::map< std::string, Component > fComponentDescr
double Integral(double exposure, double *err=0, EExposureType expotype=kPOT) const
Return total number of events scaled to pot.
Definition: Spectrum.cxx:249
double POT() const
Definition: Spectrum.h:227
assert(nhit_max >=nhit_nbins)
std::map< std::string, Spectrum > fComponentSpecs
TLegend * AutoPlaceLegend(double dx, double dy, double yPin)
Create a legend, maximizing distance from all histograms.
Definition: Plots.cxx:715
std::vector<Binning> ana::SpectrumComponents::GetBinnings ( ) const
inline

Definition at line 99 of file SpectrumComponents.h.

References fTot, and ana::Spectrum::GetBinnings().

99 { return fTot.GetBinnings(); }
const std::vector< Binning > & GetBinnings() const
Definition: Spectrum.h:264
std::vector<std::string> ana::SpectrumComponents::GetLabels ( ) const
inline

Definition at line 98 of file SpectrumComponents.h.

References fTot, and ana::Spectrum::GetLabels().

98 { return fTot.GetLabels(); }
const std::vector< std::string > & GetLabels() const
Definition: Spectrum.h:263
std::unique_ptr< SpectrumComponents > ana::SpectrumComponents::LoadFrom ( TDirectory *  dir,
const std::string name 
)
static

Definition at line 185 of file SpectrumComponents.cxx.

References ana::assert(), dir, findDuplicateFiles::key, ana::kNoCut, ana::Spectrum::LoadFrom(), runNovaSAM::release, runNovaSAM::ret, cvnie::subdir, and PandAna.Demos.pi0_spectra::tot.

Referenced by ana::DataMCPair::LoadFrom(), and SpectrumComponents().

186  {
187  dir = dir->GetDirectory(name.c_str()); // switch to subdir
188  assert(dir);
189 
190  Spectrum tot = *Spectrum::LoadFrom(dir, "Total");
191 
192  std::map<std::string, Component> compDescrs;
193  std::map<std::string, Spectrum> compSpecs;
194  TDirectory * compDir = dynamic_cast<TDirectory*>(dir->Get("Components"));
195  for ( TObject * obj : *(compDir->GetListOfKeys()) )
196  {
197  TKey * key = dynamic_cast<TKey*>(obj);
198  TDirectory * subdir = dynamic_cast<TDirectory*>(key->ReadObj());
199  if (!subdir)
200  continue;
201 
202  compSpecs.emplace(key->GetName(), *(Spectrum::LoadFrom(compDir, key->GetName()).release()));
203  compDescrs.emplace(std::piecewise_construct,
204  std::forward_as_tuple(key->GetName()),
205  std::forward_as_tuple(kNoCut, // Cut object won't be re-used anyhow since Spectra are already made?
206  dynamic_cast<TObjString*>(subdir->Get("blurb"))->String().Data(),
207  dynamic_cast<TParameter<int>*>(subdir->Get("color"))->GetVal() )
208  );
209  }
210 
211  auto ret = std::make_unique<SpectrumComponents>(
212  std::move(tot),
213  std::move(compDescrs),
214  std::move(compSpecs)
215  );
216 
217  delete dir;
218 
219  return std::move(ret);
220 
221  }
const XML_Char * name
Definition: expat.h:151
subdir
Definition: cvnie.py:7
static std::unique_ptr< Spectrum > LoadFrom(TDirectory *dir, const std::string &name)
Definition: Spectrum.cxx:535
std::vector< float > Spectrum
Definition: Constants.h:610
TDirectory * dir
Definition: macro.C:5
assert(nhit_max >=nhit_nbins)
const Cut kNoCut
The simplest possible cut: pass everything, used as a default.
Definition: Cut.h:109
unsigned int ana::SpectrumComponents::NDimensions ( ) const
inline

Definition at line 97 of file SpectrumComponents.h.

References fTot, and ana::Spectrum::NDimensions().

97 { return fTot.NDimensions(); }
unsigned int NDimensions() const
Definition: Spectrum.h:262
float ana::SpectrumComponents::Purity ( const std::set< std::string > &  signalCatNames = {}) const

Purity of the MC selection based on the MC subcategories.

Parameters
signalCatNamesIf any of your subcategories are "signal", pass them here to avoid counting them as background

Definition at line 139 of file SpectrumComponents.cxx.

References fComponentSpecs, fTot, POT, and ana::Spectrum::ToTH1().

Referenced by ana::DataMCPair::Purity(), and SpectrumComponents().

140  {
141  double bkgCounts = 0;
142  double POT = fComponentSpecs.begin()->second.POT();
143  for (const auto & mcCatPair : fComponentSpecs)
144  {
145  if (signalCatNames.find(mcCatPair.first) != signalCatNames.end())
146  continue;
147  bkgCounts += mcCatPair.second.ToTH1(POT)->GetEntries();
148  }
149  return 1 - bkgCounts / fTot.ToTH1(POT)->GetEntries();
150  }
TH1D * ToTH1(double exposure, Color_t col=kBlack, Style_t style=kSolid, EExposureType expotype=kPOT, EBinType bintype=kBinContent) const
Histogram made from this Spectrum, scaled to some exposure.
Definition: Spectrum.cxx:148
std::vector< double > POT
std::map< std::string, Spectrum > fComponentSpecs
void ana::SpectrumComponents::SaveTo ( TDirectory *  dir,
const std::string name 
) const

Definition at line 153 of file SpectrumComponents.cxx.

References plot_validation_datamc_2018::color, dir, fComponentDescr, fComponentSpecs, fTot, ana::Spectrum::SaveTo(), and tmp.

Referenced by ana::DataMCPair::SaveTo(), and SpectrumComponents().

154  {
155  TDirectory* tmp = gDirectory;
156 
157  dir = dir->mkdir(name.c_str()); // switch to subdir
158  dir->cd();
159 
160  fTot.SaveTo(dir, "Total");
161 
162  TDirectory * compsDir = dir->mkdir("Components");
163  for (const auto & specPair : fComponentSpecs)
164  {
165  specPair.second.SaveTo(compsDir, specPair.first);
166 
167  TDirectory* compDir = compsDir->GetDirectory(specPair.first.c_str());
168  compDir->cd();
169  const auto & comp = fComponentDescr.at(specPair.first);
170  TObjString blurb(comp.blurb.c_str());
171  blurb.Write("blurb");
172 
173  TParameter<int> color;
174  color.SetVal(comp.color);
175  color.Write("color");
176  }
177 
178  dir->Write();
179  delete dir;
180 
181  tmp->cd();
182  }
const XML_Char * name
Definition: expat.h:151
std::map< std::string, Component > fComponentDescr
Float_t tmp
Definition: plot.C:36
void SaveTo(TDirectory *dir, const std::string &name) const
Definition: Spectrum.cxx:506
TDirectory * dir
Definition: macro.C:5
std::map< std::string, Spectrum > fComponentSpecs
const ana::Cut& ana::SpectrumComponents::Sel ( ) const
inline

Definition at line 92 of file SpectrumComponents.h.

References fSel.

92 { return fSel; };
Spectrum& ana::SpectrumComponents::Tot ( )
inline
const Spectrum& ana::SpectrumComponents::Tot ( ) const
inline

Definition at line 95 of file SpectrumComponents.h.

References fTot.

95 { return fTot; };

Member Data Documentation

std::map<std::string, Component> ana::SpectrumComponents::fComponentDescr
private
std::map<std::string, Spectrum> ana::SpectrumComponents::fComponentSpecs
private

Definition at line 109 of file SpectrumComponents.h.

Referenced by DrawComponents(), DrawLegend(), Purity(), SaveTo(), and SpectrumComponents().

ana::Cut ana::SpectrumComponents::fSel
private

Definition at line 103 of file SpectrumComponents.h.

Referenced by Sel(), and SpectrumComponents().

Spectrum ana::SpectrumComponents::fTot
private

Definition at line 107 of file SpectrumComponents.h.

Referenced by DrawLegend(), GetBinnings(), GetLabels(), NDimensions(), Purity(), SaveTo(), and Tot().


The documentation for this class was generated from the following files: