All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MedianSurface.cxx
Go to the documentation of this file.
2 
4 
5 #include "TDirectory.h"
6 #include "TGraph.h"
7 #include "TH2.h"
8 #include "TObjString.h"
9 #include "TPad.h"
10 #include "TStyle.h"
11 
12 namespace ana
13 {
14  // --------------------------------------------------------------------------
15  MedianSurface::MedianSurface(const std::vector<Surface>& throws)
16  : fThrows(throws)
17  {
18  assert(!throws.empty());
19 
20  for(const Surface& s: throws){
21  assert(s.fHist->GetNbinsX() == throws[0].fHist->GetNbinsX());
22  assert(s.fHist->GetNbinsY() == throws[0].fHist->GetNbinsY());
23  // TODO check min and max match at least
24  }
25 
26  fLogX = throws[0].fLogX;
27  fLogY = throws[0].fLogY;
28 
29  // Think about what to do with these
30  fMinX = -1;
31  fMinY = -1;
32  // Though this is right
33  fMinChi = 0;
34 
35  fHist = new TH2F(*throws[0].fHist);
36  fHist->Reset();
37  fHistUp1 = new TH2F(*fHist);
38  fHistDn1 = new TH2F(*fHist);
39  fHistUp2 = new TH2F(*fHist);
40  fHistDn2 = new TH2F(*fHist);
41 
42  for(int ix = 0; ix < fHist->GetNbinsX()+2; ++ix){
43  for(int iy = 0; iy < fHist->GetNbinsY()+2; ++iy){
44  std::vector<float> chis;
45  chis.reserve(throws.size());
46  for(const Surface& s: throws){
47  chis.push_back(s.fHist->GetBinContent(ix, iy));
48  }
49  std::sort(chis.begin(), chis.end());
50 
51  // TODO think about off-by-one errors and interpolation here
52  // Median
53  fHist->SetBinContent(ix, iy, chis[throws.size()/2]);
54  // One sigma bounds
55  const double tail1 = (1-0.6827)/2;
56  fHistUp1->SetBinContent(ix, iy, chis[throws.size()*tail1]);
57  fHistDn1->SetBinContent(ix, iy, chis[throws.size()*(1-tail1)]);
58 
59  // Two sigma bounds
60  const double tail2 = (1-0.9545)/2;
61  fHistUp2->SetBinContent(ix, iy, chis[throws.size()*tail2]);
62  fHistDn2->SetBinContent(ix, iy, chis[(throws.size()-1)*(1-tail2)]);
63  } // end for iy
64  } // end for ix
65  }
66 
67  // --------------------------------------------------------------------------
68  void MedianSurface::DrawEnsemble(TH2* fc, Color_t color)
69  {
70  EnsureAxes();
71 
72  for(Surface& s: fThrows){
73  std::vector<TGraph*> gs = s.GetGraphs(fc, -1);
74 
75  for(TGraph* g: gs){
76  g->SetLineWidth(1);
77  g->SetLineColor(color);
78  g->Draw("l");
79  }
80  }
81 
82  gPad->Update();
83  }
84 
85  // --------------------------------------------------------------------------
87  {
88  EnsureAxes();
89 
90  TH2F surf1(*fHist);
91  TH2F surf2(*fHist);
92 
93  for(int ix = 0; ix < fHist->GetNbinsX()+2; ++ix){
94  for(int iy = 0; iy < fHist->GetNbinsY()+2; ++iy){
95  const double c = fc->GetBinContent(ix, iy);
96 
97  const double u1 = fHistUp1->GetBinContent(ix, iy);
98  const double d1 = fHistDn1->GetBinContent(ix, iy);
99  surf1.SetBinContent(ix, iy, std::max(u1-c, c-d1));
100 
101  const double u2 = fHistUp2->GetBinContent(ix, iy);
102  const double d2 = fHistDn2->GetBinContent(ix, iy);
103  surf2.SetBinContent(ix, iy, std::max(u2-c, c-d2));
104  }
105  }
106 
107  const double level = 0;
108 
109  // I haven't been able to figure out how to draw these filled properly. cont0 does it, but can't handle countours that reach the edge of the space
110  surf2.SetContour(1, &level);
111  surf2.SetLineColor(kYellow);
112  surf2.DrawCopy("cont3 same");
113 
114  surf1.SetContour(1, &level);
115  surf1.SetLineColor(kGreen);
116  surf1.SetFillColor(kGreen+2);
117  surf1.DrawCopy("cont3 same");
118 
119  gPad->Update();
120  }
121 
122  //----------------------------------------------------------------------
123  void MedianSurface::SaveTo(TDirectory* dir) const
124  {
125  TDirectory* tmp = gDirectory;
126  dir->cd();
127  TObjString("MedianSurface").Write("type");
128 
129  for(unsigned int i = 0; i < fThrows.size(); ++i){
130  fThrows[i].SaveTo(dir->mkdir(TString::Format("surf%d", i)));
131  }
132 
133  tmp->cd();
134  }
135 
136  //----------------------------------------------------------------------
137  std::unique_ptr<MedianSurface> MedianSurface::LoadFrom(TDirectory* dir)
138  {
139  DontAddDirectory guard;
140 
141  TObjString* tag = (TObjString*)dir->Get("type");
142  assert(tag);
143  assert(tag->GetString() == "MedianSurface");
144 
145  std::vector<Surface> surfs;
146  for(unsigned int i = 0; ; ++i){
147  TDirectory* surfdir = dir->GetDirectory(TString::Format("surf%d", i));
148  if(!surfdir) break; // we got all of them
149  surfs.push_back(*ana::LoadFrom<Surface>(surfdir));
150  }
151 
152  return std::make_unique<MedianSurface>(surfs);
153  }
154 
155 }
Log-likelihood scan across two parameters.
void DrawBand(TH2 *fc)
std::vector< Surface > fThrows
Definition: MedianSurface.h:20
std::size_t size(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:561
process_name opflashCryoW ana
BEGIN_PROLOG g
void DrawEnsemble(TH2 *fc, Color_t color=kGray)
void SaveTo(TDirectory *dir) const
static std::unique_ptr< MedianSurface > LoadFrom(TDirectory *dir)
tuple dir
Definition: dropbox.py:28
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
MedianSurface(const std::vector< Surface > &throws)
Prevent histograms being added to the current directory.
bool empty(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:555