36 #define NaN std::numeric_limits<float>::quiet_NaN()
38 #define IsNaN(a) TMath::IsNaN(a)
40 ClassImp(RooStats::SamplingDistPlot);
42 using namespace RooStats;
48 SamplingDistPlot::SamplingDistPlot(Int_t nbins) :
56 fXMin(NaN), fXMax(NaN), fYMin(NaN), fYMax(NaN),
60 fIterator = fItems.MakeIterator();
70 SamplingDistPlot::SamplingDistPlot(Int_t nbins, Double_t min, Double_t max) :
78 fXMin(NaN), fXMax(NaN), fYMin(NaN), fYMax(NaN),
82 fIterator = fItems.MakeIterator();
88 SetXRange( min, max );
94 SamplingDistPlot::~SamplingDistPlot() {
97 if (fRooPlot)
delete fRooPlot;
104 Double_t SamplingDistPlot::AddSamplingDistribution(
const SamplingDistribution *samplingDist, Option_t *drawOptions) {
105 fSamplingDistr = samplingDist->GetSamplingDistribution();
106 if( fSamplingDistr.empty() ) {
107 coutW(Plotting) <<
"Empty sampling distribution given to plot. Skipping." << endl;
110 SetSampleWeights(samplingDist);
112 TString options(drawOptions);
115 Double_t xmin(TMath::Infinity()), xmax(-TMath::Infinity());
117 for(
unsigned int i=0; i < fSamplingDistr.size(); i++ ) {
118 if( fSamplingDistr[i] < xmin && fSamplingDistr[i] != -TMath::Infinity() ) {
119 xmin = fSamplingDistr[i];
121 if( fSamplingDistr[i] > xmax && fSamplingDistr[i] != TMath::Infinity() ) {
122 xmax = fSamplingDistr[i];
126 coutW(Plotting) <<
"Could not determine xmin and xmax of sampling distribution that was given to plot." << endl;
134 double binWidth = (xmax-xmin)/(fBins);
135 Double_t xlow = xmin - 1.5*binWidth;
136 Double_t xup = xmax + 1.5*binWidth;
137 if( !IsNaN(fXMin) ) xlow = fXMin;
138 if( !IsNaN(fXMax) ) xup = fXMax;
140 fHist =
new TH1F(samplingDist->GetName(), samplingDist->GetTitle(), fBins, xlow, xup);
141 fHist->SetDirectory(0);
143 if( fVarName.Length() == 0 ) fVarName = samplingDist->GetVarName();
144 fHist->GetXaxis()->SetTitle(fVarName.Data());
147 std::vector<Double_t>::iterator valuesIt = fSamplingDistr.begin();
148 for (
int w_idx = 0; valuesIt != fSamplingDistr.end(); ++valuesIt, ++w_idx) {
149 if (fIsWeighted) fHist->Fill(*valuesIt, fSampleWeights[w_idx]);
150 else fHist->Fill(*valuesIt);
155 double weightSum = 1.0;
156 if(options.Contains(
"NORMALIZE")) {
157 weightSum = fHist->Integral(
"width");
158 fHist->Scale(1./weightSum);
160 options.ReplaceAll(
"NORMALIZE",
"");
166 fHist->SetMarkerStyle(fMarkerType);
167 fHist->SetMarkerColor(fColor);
168 fHist->SetLineColor(fColor);
173 fHist->SetStats(kFALSE);
175 addObject(fHist, options.Data());
177 TString title = samplingDist->GetTitle();
178 if(fLegend && title.Length() > 0) fLegend->AddEntry(fHist, title,
"L");
185 Double_t SamplingDistPlot::AddSamplingDistributionShaded(
const SamplingDistribution *samplingDist, Double_t minShaded, Double_t maxShaded, Option_t *drawOptions) {
186 if( samplingDist->GetSamplingDistribution().empty() ) {
187 coutW(Plotting) <<
"Empty sampling distribution given to plot. Skipping." << endl;
190 Double_t scaleFactor = AddSamplingDistribution(samplingDist, drawOptions);
192 TH1F *shaded = (TH1F*)fHist->Clone((
string(samplingDist->GetName())+
string(
"_shaded")).c_str());
193 shaded->SetDirectory(0);
194 shaded->SetFillStyle(fFillStyle++);
195 shaded->SetLineWidth(1);
197 for (
int i=0; i<shaded->GetNbinsX(); ++i) {
198 if (shaded->GetBinCenter(i) < minShaded || shaded->GetBinCenter(i) > maxShaded){
199 shaded->SetBinContent(i,0);
203 TString options(drawOptions);
205 if(options.Contains(
"NORMALIZE")) {
206 options.ReplaceAll(
"NORMALIZE",
"");
209 addObject(shaded, options.Data());
216 void SamplingDistPlot::AddLine(Double_t x1, Double_t y1, Double_t x2, Double_t y2,
const char* title) {
217 TLine *line =
new TLine(x1, y1, x2, y2);
218 line->SetLineWidth(3);
219 line->SetLineColor(kBlack);
221 if(fLegend && title) fLegend->AddEntry(line, title,
"L");
223 addOtherObject(line,
"");
229 void SamplingDistPlot::AddTH1(TH1* h, Option_t *drawOptions) {
230 if(fLegend && h->GetTitle()) fLegend->AddEntry(h, h->GetTitle(),
"L");
231 TH1 * hcopy = (TH1*) h->Clone();
232 hcopy->SetDirectory(0);
233 addObject(hcopy, drawOptions);
235 void SamplingDistPlot::AddTF1(TF1* f,
const char* title, Option_t *drawOptions) {
236 if(fLegend && title) fLegend->AddEntry(f, title,
"L");
237 addOtherObject(f->Clone(), drawOptions);
243 void SamplingDistPlot::SetSampleWeights(
const SamplingDistribution* samplingDist)
245 fIsWeighted = kFALSE;
247 if(samplingDist->GetSampleWeights().size() != 0){
249 fSampleWeights = samplingDist->GetSampleWeights();
261 void SamplingDistPlot::addObject(TObject *obj, Option_t *drawOptions)
265 std::cerr << fName <<
"::addObject: called with a null pointer" << std::endl;
269 fItems.Add(obj,drawOptions);
280 void SamplingDistPlot::addOtherObject(TObject *obj, Option_t *drawOptions)
283 oocoutE(
this,InputArguments) << fName <<
"::addOtherObject: called with a null pointer" << std::endl;
287 fOtherItems.Add(obj,drawOptions);
297 void SamplingDistPlot::Draw(Option_t * ) {
300 Double_t theMin(0.), theMax(0.), theYMin(NaN), theYMax(0.);
301 GetAbsoluteInterval(theMin, theMax, theYMax);
302 if( !IsNaN(fXMin) ) theMin = fXMin;
303 if( !IsNaN(fXMax) ) theMax = fXMax;
304 if( !IsNaN(fYMin) ) theYMin = fYMin;
305 if( !IsNaN(fYMax) ) theYMax = fYMax;
307 RooRealVar xaxis(
"xaxis", fVarName.Data(), theMin, theMax);
310 if (fRooPlot)
delete fRooPlot;
312 bool dirStatus = RooPlot::addDirectoryStatus();
314 if (dirStatus) RooPlot::setAddDirectoryStatus(
false);
315 fRooPlot = xaxis.frame();
316 if (dirStatus) RooPlot::setAddDirectoryStatus(
true);
318 oocoutE(
this,InputArguments) <<
"invalid variable to plot" << std::endl;
321 fRooPlot->SetTitle(
"");
322 if( !IsNaN(theYMax) ) {
324 fRooPlot->SetMaximum(theYMax);
326 if( !IsNaN(theYMin) ) {
328 fRooPlot->SetMinimum(theYMin);
333 while ((obj = (TH1F*) fIterator->Next())) {
336 TH1 * cloneObj = (TH1*)obj->Clone();
337 if( !IsNaN(theYMax) ) {
339 cloneObj->SetMaximum(theYMax);
341 if( !IsNaN(theYMin) ) {
343 cloneObj->SetMinimum(theYMin);
345 cloneObj->SetDirectory(0);
346 fRooPlot->addTH1(cloneObj, fIterator->GetOption());
349 TIterator *otherIt = fOtherItems.MakeIterator();
350 TObject *otherObj = NULL;
351 while ((otherObj = otherIt->Next())) {
352 TObject * cloneObj = otherObj->Clone();
353 fRooPlot->addObject(cloneObj, otherIt->GetOption());
358 if(fLegend) fRooPlot->addObject(fLegend);
360 if(
bool(gStyle->GetOptLogx()) != fLogXaxis) {
361 if(!fApplyStyle) coutW(Plotting) <<
"gStyle will be changed to adjust SetOptLogx(...)" << endl;
362 gStyle->SetOptLogx(fLogXaxis);
364 if(
bool(gStyle->GetOptLogy()) != fLogYaxis) {
365 if(!fApplyStyle) coutW(Plotting) <<
"gStyle will be changed to adjust SetOptLogy(...)" << endl;
366 gStyle->SetOptLogy(fLogYaxis);
372 gPad->SetLogx(fLogXaxis);
373 gPad->SetLogy(fLogYaxis);
381 void SamplingDistPlot::ApplyDefaultStyle(
void) {
385 gStyle->SetFrameBorderMode( icol );
386 gStyle->SetCanvasBorderMode( icol );
387 gStyle->SetPadBorderMode( icol );
388 gStyle->SetPadColor( icol );
389 gStyle->SetCanvasColor( icol );
390 gStyle->SetStatColor( icol );
391 gStyle->SetFrameFillStyle( 0 );
394 gStyle->SetPaperSize( 20, 26 );
397 fLegend->SetFillColor(0);
398 fLegend->SetBorderSize(1);
405 void SamplingDistPlot::GetAbsoluteInterval(Double_t &theMin, Double_t &theMax, Double_t &theYMax)
const
407 Double_t tmpmin = TMath::Infinity();
408 Double_t tmpmax = -TMath::Infinity();
409 Double_t tmpYmax = -TMath::Infinity();
413 while((obj = (TH1F*)fIterator->Next())) {
414 if(obj->GetXaxis()->GetXmin() < tmpmin) tmpmin = obj->GetXaxis()->GetXmin();
415 if(obj->GetXaxis()->GetXmax() > tmpmax) tmpmax = obj->GetXaxis()->GetXmax();
416 if(obj->GetMaximum() > tmpYmax) tmpYmax = obj->GetMaximum() + 0.1*obj->GetMaximum();
430 void SamplingDistPlot::SetLineColor(Color_t color,
const SamplingDistribution *samplDist) {
431 if (samplDist == 0) {
432 fHist->SetLineColor(color);
437 TString shadedName(fHist->GetName());
438 shadedName +=
"_shaded";
440 while ((obj = (TH1F*) fIterator->Next())) {
441 if (!strcmp(obj->GetName(), shadedName.Data())) {
442 obj->SetLineColor(color);
443 obj->SetFillColor(color);
451 TString shadedName(samplDist->GetName());
452 shadedName +=
"_shaded";
454 while ((obj = (TH1F*) fIterator->Next())) {
455 if (!strcmp(obj->GetName(), samplDist->GetName())) {
456 obj->SetLineColor(color);
459 if (!strcmp(obj->GetName(), shadedName.Data())) {
460 obj->SetLineColor(color);
461 obj->SetFillColor(color);
472 void SamplingDistPlot::SetLineWidth(Width_t lwidth,
const SamplingDistribution *samplDist)
475 fHist->SetLineWidth(lwidth);
480 while((obj = (TH1F*)fIterator->Next())) {
481 if(!strcmp(obj->GetName(),samplDist->GetName())){
482 obj->SetLineWidth(lwidth);
493 void SamplingDistPlot::SetLineStyle(Style_t style,
const SamplingDistribution *samplDist)
496 fHist->SetLineStyle(style);
501 while((obj = (TH1F*)fIterator->Next())) {
502 if(!strcmp(obj->GetName(),samplDist->GetName())){
503 obj->SetLineStyle(style);
514 void SamplingDistPlot::SetMarkerStyle(Style_t style,
const SamplingDistribution *samplDist)
517 fHist->SetMarkerStyle(style);
522 while((obj = (TH1F*)fIterator->Next())) {
523 if(!strcmp(obj->GetName(),samplDist->GetName())){
524 obj->SetMarkerStyle(style);
535 void SamplingDistPlot::SetMarkerColor(Color_t color,
const SamplingDistribution *samplDist)
538 fHist->SetMarkerColor(color);
543 while((obj = (TH1F*)fIterator->Next())) {
544 if(!strcmp(obj->GetName(),samplDist->GetName())){
545 obj->SetMarkerColor(color);
556 void SamplingDistPlot::SetMarkerSize(Size_t size,
const SamplingDistribution *samplDist)
559 fHist->SetMarkerSize(size);
564 while((obj = (TH1F*)fIterator->Next())) {
565 if(!strcmp(obj->GetName(),samplDist->GetName())){
566 obj->SetMarkerSize(size);
577 TH1F* SamplingDistPlot::GetTH1F(
const SamplingDistribution *samplDist)
579 if(samplDist == NULL){
584 while((obj = (TH1F*)fIterator->Next())) {
585 if(!strcmp(obj->GetName(),samplDist->GetName())){
596 void SamplingDistPlot::RebinDistribution(Int_t rebinFactor,
const SamplingDistribution *samplDist)
599 fHist->Rebin(rebinFactor);
604 while((obj = (TH1F*)fIterator->Next())) {
605 if(!strcmp(obj->GetName(),samplDist->GetName())){
606 obj->Rebin(rebinFactor);
618 void SamplingDistPlot::DumpToFile(
const char* RootFileName, Option_t *option,
const char *ftitle, Int_t compress) {
622 cout <<
"Plot was not drawn yet. Dump can only be saved after it was drawn with Draw()." << endl;
626 TFile ofile(RootFileName, option, ftitle, compress);