Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
HybridPlot.cxx
Go to the documentation of this file.
1// @(#)root/hist:$Id$
2
3/** \class RooStats::HybridPlot
4 \ingroup Roostats
5
6This class provides the plots for the result of a study performed with the
7HybridCalculatorOriginal class.
8
9Authors: D. Piparo, G. Schott - Universitaet Karlsruhe
10
11An example plot is available here:
12http://www-ekp.physik.uni-karlsruhe.de/~schott/roostats/hybridplot_example.png
13*/
14
15#include <cassert>
16#include <cmath>
17#include <iostream>
18#include <map>
19
20#include "RooStats/HybridPlot.h"
21#include "TStyle.h"
22#include "TF1.h"
23#include "TAxis.h"
24#include "TH1.h"
25#include "TLine.h"
26#include "TLegend.h"
27#include "TFile.h"
28#include "TVirtualPad.h"
29
30#include <algorithm>
31
32/// To build the THtml documentation
33
35
36using namespace RooStats;
37
38////////////////////////////////////////////////////////////////////////////////
39/// HybridPlot constructor
40
42 const char* title,
43 const std::vector<double> & sb_vals,
44 const std::vector<double> & b_vals,
45 double testStat_data,
46 int n_bins,
47 bool verbosity):
48 TNamed(name,title),
49 fSb_histo(nullptr),
50 fSb_histo_shaded(nullptr),
51 fB_histo(nullptr),
52 fB_histo_shaded(nullptr),
53 fData_testStat_line(nullptr),
54 fLegend(nullptr),
55 fPad(nullptr),
56 fVerbose(verbosity)
57{
58 int nToysSB = sb_vals.size();
59 int nToysB = sb_vals.size();
60 assert (nToysSB >0);
61 assert (nToysB >0);
62
63 // Get the max and the min of the plots
64 double min = *std::min_element(sb_vals.begin(), sb_vals.end());
65 double max = *std::max_element(sb_vals.begin(), sb_vals.end());
66
67 double min_b = *std::min_element(b_vals.begin(), b_vals.end());
68 double max_b = *std::max_element(b_vals.begin(), b_vals.end());
69
70
71 if ( min_b < min) min = min_b;
72 if ( max_b > max) max = max_b;
73
74 if (testStat_data<min) min = testStat_data;
75 if (testStat_data>max) max = testStat_data;
76
77 min *= 1.1;
78 max *= 1.1;
79
80 // Build the histos
81
82 fSb_histo = new TH1F ("SB_model",title,n_bins,min,max);
85 fSb_histo->GetXaxis()->SetTitle("test statistics");
87
88 fB_histo = new TH1F ("B_model",title,n_bins,min,max);
91 fB_histo->GetXaxis()->SetTitle("test statistics");
93
94 for (int i=0;i<nToysSB;++i) fSb_histo->Fill(sb_vals[i]);
95 for (int i=0;i<nToysB;++i) fB_histo->Fill(b_vals[i]);
96
97 double histos_max_y = fSb_histo->GetMaximum();
98 double lineHeight = histos_max_y/nToysSB;
99 if (histos_max_y<fB_histo->GetMaximum()) histos_max_y = fB_histo->GetMaximum()/nToysB;
100
101 // Build the line of the measured -2lnQ
102 fData_testStat_line = new TLine(testStat_data,0,testStat_data,lineHeight);
105
106 // The legend
107 double golden_section = (std::sqrt(5.)-1)/2;
108
109 fLegend = new TLegend(0.75,0.95-0.2*golden_section,0.95,0.95);
110 TString title_leg="test statistics distributions ";
111 title_leg+=sb_vals.size();
112 title_leg+=" toys";
113 fLegend->SetName(title_leg.Data());
114 fLegend->AddEntry(fSb_histo,"SB toy datasets");
115 fLegend->AddEntry(fB_histo,"B toy datasets");
116 fLegend->AddEntry((TLine*)fData_testStat_line,"test statistics on data","L");
118}
119
120////////////////////////////////////////////////////////////////////////////////
121/// destructor
122
124{
125
126 if (fSb_histo) delete fSb_histo;
127 if (fB_histo) delete fB_histo;
131 if (fLegend) delete fLegend;
132}
133
134////////////////////////////////////////////////////////////////////////////////
135/// draw the S+B and B histogram in the current canvas
136
137void HybridPlot::Draw(const char* )
138{
139 // We don't want the statistics of the histos
140 gStyle->SetOptStat(0);
141
142 // The histos
143
146 fB_histo->DrawNormalized("same");
147 }
148 else{
150 fSb_histo->DrawNormalized("same");
151 }
152
153 // Shaded
154 fB_histo_shaded = static_cast<TH1F*>(fB_histo->Clone("b_shaded"));
157
158 fSb_histo_shaded = static_cast<TH1F*>(fSb_histo->Clone("sb_shaded"));
161
162 // Empty the bins according to the data -2lnQ
163 double data_m2lnq= fData_testStat_line->GetX1();
164 for (int i=1;i<=fSb_histo->GetNbinsX();++i){
165 if (fSb_histo->GetBinCenter(i)<data_m2lnq){
168 }
169 else{
172 }
173 }
174
175 // Draw the shaded histos
176 fB_histo_shaded->Draw("same");
177 fSb_histo_shaded->Draw("same");
178
179 // The line
180 fData_testStat_line->Draw("same");
181
182 // The legend
183 fLegend->Draw("same");
184
185 if (gPad) {
186 gPad->SetName(GetName());
187 gPad->SetTitle(GetTitle() );
188 }
189
190 fPad = gPad;
191
192}
193
194////////////////////////////////////////////////////////////////////////////////
195/// All the objects are written to rootfile
196
197void HybridPlot::DumpToFile (const char* RootFileName, const char* options)
198{
199
200 TFile ofile(RootFileName,options);
201 ofile.cd();
202
203 // The histos
204 fSb_histo->Write();
205 fB_histo->Write();
206
207 // The shaded histos
208 if (fB_histo_shaded!=nullptr && fSb_histo_shaded!=nullptr){
211 }
212
213 // The line
214 fData_testStat_line->Write("Measured test statistics line tag");
215
216 // The legend
217 fLegend->Write();
218
219 ofile.Close();
220
221}
222
223////////////////////////////////////////////////////////////////////////////////
224
226 if (!fPad) {
227 Error("HybridPlot","Hybrid plot has not yet been drawn ");
228 return;
229 }
231}
232
233////////////////////////////////////////////////////////////////////////////////
234/// Perform 2 times a gaussian fit to fetch the center of the histo.
235/// To get the second fit range get an interval that tries to keep into account
236/// the skewness of the distribution.
237
238double HybridPlot::GetHistoCenter(TH1* histo_orig, double n_rms, bool display_result){
239 // Get the center of the histo
240
241 TString optfit = "Q0";
242 if (display_result) optfit = "Q";
243
244 TH1F* histo = static_cast<TH1F*>(histo_orig->Clone());
245
246 // get the histo x extremes
247 double x_min = histo->GetXaxis()->GetXmin();
248 double x_max = histo->GetXaxis()->GetXmax();
249
250 // First fit!
251
252 TF1* gauss = new TF1("mygauss", "gauss", x_min, x_max);
253
254 gauss->SetParameter("Constant",histo->GetEntries());
255 gauss->SetParameter("Mean",histo->GetMean());
256 gauss->SetParameter("Sigma",histo->GetRMS());
257
258 histo->Fit(gauss,optfit);
259
260 // Second fit!
261 double sigma = gauss->GetParameter("Sigma");
262 double mean = gauss->GetParameter("Mean");
263
264 delete gauss;
265
266 std::cout << "Center is 1st pass = " << mean << std::endl;
267
268 double skewness = histo->GetSkewness();
269
270 x_min = mean - n_rms*sigma - sigma*skewness/2;
271 x_max = mean + n_rms*sigma - sigma*skewness/2;
272
273 TF1* gauss2 = new TF1("mygauss2", "gauss", x_min, x_max);
274 gauss2->SetParameter("Mean",mean);
275
276 // second fit : likelihood fit
277 optfit += "L";
278 histo->Fit(gauss2,optfit,"", x_min, x_max);
279
280
281 double center = gauss2->GetParameter("Mean");
282
283 if (display_result) {
284 histo->Draw();
285 gauss2->Draw("same");
286 }
287 else {
288 delete histo;
289 }
290 delete gauss2;
291
292 return center;
293
294
295}
296
297////////////////////////////////////////////////////////////////////////////////
298/// We let an horizontal bar go down and we stop when we have the integral
299/// equal to the desired one.
300
301double* HybridPlot::GetHistoPvals (TH1* histo, double percentage){
302
303 if (percentage>1){
304 std::cerr << "Percentage greater or equal to 1!\n";
305 return nullptr;
306 }
307
308 // Get the integral of the histo
309 double* h_integral=histo->GetIntegral();
310
311 // Start the iteration
312 std::map<int,int> extremes_map;
313
314 for (int i=0;i<histo->GetNbinsX();++i){
315 for (int j=0;j<histo->GetNbinsX();++j){
316 double integral = h_integral[j]-h_integral[i];
317 if (integral>percentage){
318 extremes_map[i]=j;
319 break;
320 }
321 }
322 }
323
324 // Now select the couple of extremes which have the lower bin content diff
325 std::map<int,int>::iterator it;
326 int a;
327 int b;
328 double left_bin_center(0.);
329 double right_bin_center(0.);
330 double diff=10e40;
331 double current_diff;
332 for (it = extremes_map.begin();it != extremes_map.end();++it){
333 a=it->first;
334 b=it->second;
335 current_diff=std::abs(histo->GetBinContent(a)-histo->GetBinContent(b));
336 if (current_diff<diff){
337 //std::cout << "a=" << a << " b=" << b << std::endl;
338 diff=current_diff;
339 left_bin_center=histo->GetBinCenter(a);
340 right_bin_center=histo->GetBinCenter(b);
341 }
342 }
343
344 double* d = new double[2];
345 d[0]=left_bin_center;
346 d[1]=right_bin_center;
347 return d;
348}
349
350////////////////////////////////////////////////////////////////////////////////
351/// Get the median of an histogram.
352
354
355 //int xbin_median;
356 double* integral = histo->GetIntegral();
357 int median_i = 0;
358 for (int j = 0; j < histo->GetNbinsX(); j++) {
359 if (integral[j]<0.5)
360 median_i = j;
361 }
362
363 double median_x =
364 histo->GetBinCenter(median_i)+
365 (histo->GetBinCenter(median_i+1)-
366 histo->GetBinCenter(median_i))*
367 (0.5-integral[median_i])/(integral[median_i+1]-integral[median_i]);
368
369 return median_x;
370}
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
#define ClassImp(name)
Definition Rtypes.h:382
@ kRed
Definition Rtypes.h:66
@ kBlack
Definition Rtypes.h:65
@ kBlue
Definition Rtypes.h:66
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
char name[80]
Definition TGX11.cxx:110
R__EXTERN TStyle * gStyle
Definition TStyle.h:436
#define gPad
This class provides the plots for the result of a study performed with the HybridCalculatorOriginal c...
Definition HybridPlot.h:36
double GetMedian(TH1 *histo)
Get the median of an histogram.
TH1F * fB_histo_shaded
The b Histo shaded.
Definition HybridPlot.h:115
TH1F * fSb_histo_shaded
The sb Histo shaded.
Definition HybridPlot.h:113
TVirtualPad * fPad
The pad where it has been drawn.
Definition HybridPlot.h:118
HybridPlot(const char *name, const char *title, const std::vector< double > &sb_values, const std::vector< double > &b_values, double testStat_data, int n_bins, bool verbosity=true)
Constructor.
void Draw(const char *options="") override
Draw on current pad.
TH1F * fSb_histo
The sb Histo.
Definition HybridPlot.h:112
void DumpToImage(const char *filename)
Write an image on disk.
TLine * fData_testStat_line
The line for the data value of the test statistic.
Definition HybridPlot.h:116
TLegend * fLegend
The legend of the plot.
Definition HybridPlot.h:117
TH1F * fB_histo
The b Histo.
Definition HybridPlot.h:114
~HybridPlot() override
Destructor.
double * GetHistoPvals(TH1 *histo, double percentage)
Get the "effective sigmas" of the histo, call delete [] res to release memory.
double GetHistoCenter(TH1 *histo, double n_rms=1, bool display_result=false)
Get the center of the histo.
void DumpToFile(const char *RootFileName, const char *options)
All the objects are written to rootfile.
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition TAttFill.h:39
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
Double_t GetXmax() const
Definition TAxis.h:140
Double_t GetXmin() const
Definition TAxis.h:139
Bool_t cd() override
Change current directory to "this" directory.
1-Dim function class
Definition TF1.h:233
void Draw(Option_t *option="") override
Draw this function with its current attributes.
Definition TF1.cxx:1333
virtual void SetParameter(Int_t param, Double_t value)
Definition TF1.h:667
virtual Double_t GetParameter(Int_t ipar) const
Definition TF1.h:540
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
void Close(Option_t *option="") override
Close a file.
Definition TFile.cxx:950
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:622
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
virtual Double_t GetBinCenter(Int_t bin) const
Return bin center for 1D histogram.
Definition TH1.cxx:9162
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6739
virtual Double_t GetMean(Int_t axis=1) const
For axis = 1,2 or 3 returns the mean value of the histogram along X,Y or Z axis.
Definition TH1.cxx:7556
virtual Double_t GetSkewness(Int_t axis=1) const
Definition TH1.cxx:7692
virtual TH1 * DrawNormalized(Option_t *option="", Double_t norm=1) const
Draw a normalized copy of this histogram.
Definition TH1.cxx:3144
TAxis * GetXaxis()
Definition TH1.h:324
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
Fit histogram with function fname.
Definition TH1.cxx:3898
virtual Double_t GetMaximum(Double_t maxval=FLT_MAX) const
Return maximum value smaller than maxval of bins in the range, unless the value has been overridden b...
Definition TH1.cxx:8566
virtual Int_t GetNbinsX() const
Definition TH1.h:297
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3344
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
Double_t GetRMS(Int_t axis=1) const
This function returns the Standard Deviation (Sigma) of the distribution not the Root Mean Square (RM...
Definition TH1.h:319
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content see convention for numbering bins in TH1::GetBin In case the bin number is greater th...
Definition TH1.cxx:9243
virtual Double_t GetEntries() const
Return the current number of entries.
Definition TH1.cxx:4423
virtual Double_t * GetIntegral()
Return a pointer to the array of bins integral.
Definition TH1.cxx:2586
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition TH1.cxx:5082
TObject * Clone(const char *newname="") const override
Make a complete copy of the underlying object.
Definition TH1.cxx:2752
virtual Double_t GetSumOfWeights() const
Return the sum of weights excluding under/overflows.
Definition TH1.cxx:7938
This class displays a legend box (TPaveText) containing several legend entries.
Definition TLegend.h:23
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition TLegend.cxx:320
void Draw(Option_t *option="") override
Draw this legend with its current attributes.
Definition TLegend.cxx:425
Use the TLine constructor to create a simple line.
Definition TLine.h:22
Double_t GetX1() const
Definition TLine.h:50
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:48
virtual Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition TObject.cxx:898
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1005
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:292
virtual void SetName(const char *name="")
Definition TPave.h:79
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition TStyle.cxx:1640
void Print(const char *filename="") const override=0
This method must be overridden when a class wants to print itself.
const Double_t sigma
Namespace for the RooStats classes.
Definition Asimov.h:19