ISurface.cxx
Go to the documentation of this file.
1 #include "TCanvas.h"
2 #include "TGraph.h"
3 #include "TKey.h"
4 #include "TMarker.h"
5 #include "TH2.h"
6 #include "TPad.h"
7 #include "TROOT.h"
8 #include "TStyle.h"
9 #include "TVectorD.h"
10 
11 #include "OscLib/IOscCalc.h"
12 
13 #include "CAFAna/Core/Utilities.h"
14 #include "CAFAna/Fit/ISurface.h"
15 
16 #include <iostream>
17 
18 namespace ana
19 {
20  //---------------------------------------------------------------------
21  void ISurface::Draw() const
22  {
23  // Can be useful to draw a partial surface for debugging
24  // CheckMask();
25 
26  EnsureAxes();
27 
28  fHist->Draw("colz same");
29 
30  // colz obliterated them
31  gPad->RedrawAxis();
32 
33  gPad->Update();
34  }
35 
36  //---------------------------------------------------------------------
37  void ISurface::DrawBestFit(Color_t color, Int_t marker) const
38  {
39  CheckMask("DrawBestFit");
40  EnsureAxes();
41 
42  TMarker *mark = new TMarker(fBestFitX, fBestFitY, marker);
43  mark->SetMarkerSize(1.5);
44  mark->SetMarkerColor(color);
45  mark->Draw();
46  gPad->Update();
47  }
48 
49  //----------------------------------------------------------------------
50  void ISurface::DrawContour(TH2 *fc, Style_t style, Color_t color,
51  double minchi)
52  {
53  CheckMask("DrawContour");
54  EnsureAxes();
55 
56  std::vector<TGraph *> gs = GetGraphs(fc, minchi);
57 
58  for (TGraph *g: gs)
59  {
60  g->SetLineWidth(3);//2);
61  g->SetLineStyle(style);
62  g->SetLineColor(color);
63  g->Draw("l");
64  }
65 
66  gPad->Update();
67  }
68 
69  //---------------------------------------------------------------------
70  void ISurface::EnsureAxes() const
71  {
72  // Could have a file temporarily open
73  DontAddDirectory guard;
74 
75  // If this pad has already been drawn in, already has axes
76  if (gPad && !gPad->GetListOfPrimitives()->IsEmpty()) return;
77 
78  const TAxis *ax = fHist->GetXaxis();
79  const TAxis *ay = fHist->GetYaxis();
80  const double Nx = ax->GetNbins();
81  const double Ny = ay->GetNbins();
82 
83  // Axes with limits where the user originally requested them, which we
84  // adjusted to be the centres of the first and last bins.
85  TH2 *axes = new TH2C(UniqueName().c_str(),
86  TString::Format(";%s;%s",
87  ax->GetTitle(), ay->GetTitle()),
88  Nx - 1, BinCenterX(1), BinCenterX(Nx),
89  Ny - 1, BinCenterY(1), BinCenterY(Ny));
90  axes->Draw();
91 
92  if(fHist){
93  // "colz same" will reuse axis's min and max, so set them helpfully here
94  axes->SetMinimum(fHist->GetMinimum());
95  axes->SetMaximum(fHist->GetMaximum());
96  }
97 
98  axes->SetTitle(fHist->GetTitle());
99  axes->GetXaxis()->SetLabelSize(ax->GetLabelSize());
100  axes->GetYaxis()->SetLabelSize(ay->GetLabelSize());
101  axes->GetXaxis()->SetLabelOffset(ax->GetLabelOffset());
102  axes->GetYaxis()->SetLabelOffset(ay->GetLabelOffset());
103  axes->GetXaxis()->CenterTitle();
104  axes->GetYaxis()->CenterTitle();
105 
106  if(fLogX) gPad->SetLogx();
107  if(fLogY) gPad->SetLogy();
108 
109  gPad->Update();
110  }
111 
112  //----------------------------------------------------------------------
113  std::vector<TGraph*> ISurface::GetGraphs(TH2* fc, double minchi)
114  {
115  CheckMask("GetGraphs");
116 
117  std::vector<TGraph*> ret;
118 
119  if(minchi < 0) minchi = fBestLikelihood;
120  std::unique_ptr<TH2> surf((TH2*)fHist->Clone(UniqueName().c_str()));
121  surf->Add(fc, -1);
122 
123  TVirtualPad* bak = gPad;
124 
125  const bool wasbatch = gROOT->IsBatch();
126  gROOT->SetBatch(); // User doesn't want to see our temporary canvas
127  TCanvas tmp;
128 
129  gStyle->SetOptStat(0);
130 
131  const double level = minchi-fBestLikelihood;
132  surf->SetContour(1, &level);
133  surf->Draw("cont list");
134 
135  tmp.Update();
136  tmp.Paint();
137 
138  gROOT->SetBatch(wasbatch);
139  gPad = bak;
140 
141  // The graphs we need (contained inside TLists, contained inside
142  // TObjArrays) are in the list of specials. But we need to be careful about
143  // types, because other stuff can get in here too (TDatabasePDG for
144  // example).
145  TCollection* specs = gROOT->GetListOfSpecials();
146 
147  TIter nextSpec(specs);
148  while(TObject* spec = nextSpec()){
149  if(!spec->InheritsFrom(TObjArray::Class())) continue;
150  TObjArray* conts = (TObjArray*)spec;
151 
152  if(conts->IsEmpty()) continue;
153 
154  if(!conts->At(0)->InheritsFrom(TList::Class())) continue;
155  TList* cont = (TList*)conts->At(0);
156 
157  TIter nextObj(cont);
158  // Contour could be split into multiple pieces
159  while(TObject* obj = nextObj()){
160  if(!obj->InheritsFrom(TGraph::Class())) continue;
161 
162  ret.push_back((TGraph*)obj->Clone(UniqueName().c_str()));
163  } // end for obj
164  } // end for spec
165 
166  return ret;
167  }
168 
169  //----------------------------------------------------------------------
170  TH2 * ISurface::ToTH2(double minchi) const
171  {
172  // Could have a file temporarily open
173  DontAddDirectory guard;
174 
175  TH2 *ret = new TH2F(*fHist);
176 
177  if (minchi >= 0)
178  {
179  for (int x = 0; x < ret->GetNbinsX() + 2; ++x)
180  {
181  for (int y = 0; y < ret->GetNbinsY() + 2; ++y)
182  {
183  ret->SetBinContent(x, y, ret->GetBinContent(x, y) + fBestLikelihood - minchi);
184  }
185  }
186  }
187 
188  return ret;
189  }
190 
191  //----------------------------------------------------------------------
192  void ISurface::SetTitle(const char *str)
193  {
194  fHist->SetTitle(str);
195  }
196 
197  //----------------------------------------------------------------------
198  void ISurface::SaveToHelper(TDirectory *dir) const
199  {
200  TDirectory *oldDir = gDirectory;
201 
202  dir->cd();
203 
204  TVectorD v(3);
205  v[0] = fBestLikelihood;
206  v[1] = fBestFitX;
207  v[2] = fBestFitY;
208  v.Write("minValues");
209 
210  fHist->Write("hist");
211 
212  TVectorD s(fSeedValues.size(), &fSeedValues[0]);
213  s.Write("seeds");
214 
215  if (!fBinMask.empty()) {
216  std::vector<double> tmp(fBinMask.begin(), fBinMask.end());
217  TVectorD m(tmp.size(), &tmp[0]);
218  m.Write("mask");
219  }
220 
221  TObjString(fLogX ? "yes" : "no").Write("logx");
222  TObjString(fLogY ? "yes" : "no").Write("logy");
223 
224  if (oldDir)
225  oldDir->cd();
226  }
227 
228  //----------------------------------------------------------------------
230  {
231 
232  const TVectorD v = *(TVectorD *) dir->Get("minValues");
233  const TVectorD s = *(TVectorD *) dir->Get("seeds");
234  TVectorD* m = (TVectorD *) dir->Get("mask");
235 
236  surf.fHist = (TH2F *) dir->Get("hist");
237  surf.fBestLikelihood = v[0];
238  surf.fBestFitX = v[1];
239  surf.fBestFitY = v[2];
240 
241  for (int idx = 0; idx < s.GetNrows(); ++idx)
242  surf.fSeedValues.push_back(s[idx]);
243 
244  if (m) {
245  for (int idx = 0; idx < m->GetNrows(); ++idx)
246  surf.fBinMask.push_back((*m)[idx]);
247  }
248 
249  }
250 
251  //----------------------------------------------------------------------
252  /// Helper function for the gaussian approximation surfaces
253  TH2* Flat(double level, const ISurface& s)
254  {
255  TH2* h = new TH2F(*s.fHist);
256 
257  for(int x = 0; x < h->GetNbinsX()+2; ++x)
258  for(int y = 0; y < h->GetNbinsY()+2; ++y)
259  h->SetBinContent(x, y, level);
260 
261  return h;
262  }
263 
264  //----------------------------------------------------------------------
265  double ISurface::BinCenterX(int bin) const
266  {
267  const TAxis* ax = fHist->GetXaxis();
268  return fLogX ? ax->GetBinCenterLog(bin) : ax->GetBinCenter(bin);
269  }
270 
271  //----------------------------------------------------------------------
272  double ISurface::BinCenterY(int bin) const
273  {
274  const TAxis* ax = fHist->GetYaxis();
275  return fLogY ? ax->GetBinCenterLog(bin) : ax->GetBinCenter(bin);
276  }
277 
278  //----------------------------------------------------------------------
280  {
281  if(!fBinMask.empty()) {
282  std::cout << "Cannot call " << func << "() on a partial surface!" << std::endl;
283  abort();
284  }
285  }
286 
287 } // namespace ana
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
double BinCenterX(int bin) const
Definition: ISurface.cxx:265
void CheckMask(const std::string &func) const
Definition: ISurface.cxx:279
void SetTitle(const char *str)
Definition: ISurface.cxx:192
double BinCenterY(int bin) const
Definition: ISurface.cxx:272
Float_t tmp
Definition: plot.C:36
std::vector< TGraph * > GetGraphs(TH2 *fc, double minchi=-1)
For expert use, custom painting of contours.
Definition: ISurface.cxx:113
const XML_Char * s
Definition: expat.h:262
void DrawBestFit(Color_t color, Int_t marker=kFullCircle) const
Draw the best fit point.
Definition: ISurface.cxx:37
TH2F * fHist
Definition: ISurface.h:68
void Draw() const
Draw the surface itself.
Definition: ISurface.cxx:21
void DrawContour(TH2 *fc, Style_t style, Color_t color, double minchi=-1)
Definition: ISurface.cxx:50
bool fLogX
Definition: ISurface.h:69
double fBestFitY
Definition: ISurface.h:67
double func(double x, double y)
void SaveToHelper(TDirectory *dir) const
dir should already be the appropriate sub-directory
Definition: ISurface.cxx:198
float bin[41]
Definition: plottest35.C:14
bool fLogY
Definition: ISurface.h:69
OStream cout
Definition: OStream.cxx:6
std::vector< double > fSeedValues
Definition: ISurface.h:70
TH2 * ToTH2(double minchi=-1) const
Definition: ISurface.cxx:170
void EnsureAxes() const
Definition: ISurface.cxx:70
TDirectory * dir
Definition: macro.C:5
double fBestFitX
Definition: ISurface.h:66
friend TH2 * Flat(double, const ISurface &)
Helper function for the gaussian approximation surfaces.
Definition: ISurface.cxx:253
std::vector< int > fBinMask
Definition: ISurface.h:71
void Format(TGraph *gr, int lcol, int lsty, int lwid, int mcol, int msty, double msiz)
Definition: Style.cxx:154
static void FillSurfObj(ISurface &surf, TDirectory *dir)
Definition: ISurface.cxx:229
Prevent histograms being added to the current directory.
Definition: UtilsExt.h:46
double fBestLikelihood
Definition: ISurface.h:65
std::string UniqueName()
Return a different string each time, for creating histograms.
Definition: Utilities.cxx:29
surf
Definition: demo5.py:35
enum BeamMode string