ROOT logo
// @(#)root/roostats:$Id: SamplingDistPlot.cxx 31276 2009-11-18 15:06:42Z moneta $

/*************************************************************************
 * Project: RooStats                                                     *
 * Package: RooFit/RooStats                                              *
 * Authors:                                                              *
 *   Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke       *
 *************************************************************************
 * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//____________________________________________________________________
/*
SamplingDistPlot : 

This class provides simple and straightforward utilities to plot SamplingDistribution
objects.
*/

#include "RooStats/SamplingDistPlot.h"

#include "RooRealVar.h"
#include "RooPlot.h"

#include <algorithm>
#include <iostream>

/// ClassImp for building the THtml documentation of the class 
ClassImp(RooStats::SamplingDistPlot);

using namespace RooStats;

//_______________________________________________________
SamplingDistPlot::SamplingDistPlot() :
 fhist(0) ,fItems()
{
  // SamplingDistPlot default constructor
  fIterator = fItems.MakeIterator();
  fbins = 100;
  fMarkerType = 20;
  fColor = 1;
}

//_______________________________________________________
SamplingDistPlot::SamplingDistPlot(const Int_t nbins) :
 fhist(0) ,fItems()
{
  // SamplingDistPlot default constructor with bin size
  fIterator = fItems.MakeIterator();
  fbins = nbins;
  fMarkerType = 20;
  fColor = 1;
}


//_______________________________________________________
SamplingDistPlot::SamplingDistPlot(const char* name, const char* title, Int_t nbins, Double_t xmin, Double_t xmax) :
 fhist(0) ,fItems()
{
  // SamplingDistPlot constructor
  fhist = new TH1F(name, title, nbins, xmin, xmax);
  fbins = nbins;
  fMarkerType = 20;
  fColor = 1;
}

//_______________________________________________________
SamplingDistPlot::~SamplingDistPlot()
{
  // SamplingDistPlot destructor

  fSamplingDistr.clear();
  fSampleWeights.clear();

  fItems.Clear();
}

//_______________________________________________________
void SamplingDistPlot::AddSamplingDistribution(const SamplingDistribution *samplingDist, Option_t *drawOptions)
{
  fSamplingDistr = samplingDist->GetSamplingDistribution();
  SetSampleWeights(samplingDist);

  // add option "SAME" if necessary
  TString options(drawOptions);
  options.ToUpper();
  if(!options.Contains("SAME")) options.Append("SAME");
  if(!options.Contains("E1")) options.Append("E1");

  const Double_t xlow = *(std::min_element(fSamplingDistr.begin(),fSamplingDistr.end()));
  const Double_t xup  = *(std::max_element(fSamplingDistr.begin(),fSamplingDistr.end()));

  fhist = new TH1F(samplingDist->GetName(),samplingDist->GetTitle(),fbins,xlow,xup);

  fhist->GetXaxis()->SetTitle(samplingDist->GetVarName().Data());

  fVarName = samplingDist->GetVarName().Data();

  std::vector<Double_t>::iterator valuesIt = fSamplingDistr.begin();

  for(int w_idx = 0; valuesIt != fSamplingDistr.end(); ++valuesIt, ++w_idx)
    {
      if(fIsWeighted) fhist->Fill(*valuesIt,fSampleWeights[w_idx]);
      else fhist->Fill(*valuesIt);
    }

  fhist->Sumw2() ;

  //some basic aesthetics
  fhist->SetMarkerStyle(fMarkerType);
  fhist->SetMarkerColor(fColor);
  fhist->SetLineColor(fColor);

  fMarkerType++;
  fColor++;

  fhist->SetStats(kFALSE);

  addObject(fhist,options.Data());

  return;
}

//_______________________________________________________
void SamplingDistPlot::SetSampleWeights(const SamplingDistribution* samplingDist)
{
  //Determine if the sampling distribution has weights and store them

  fIsWeighted = kFALSE;

  if(samplingDist->GetSampleWeights().size() != 0){
    fIsWeighted = kTRUE;
    fSampleWeights = samplingDist->GetSampleWeights();
  }  

  return;
}

void SamplingDistPlot::addObject(TObject *obj, Option_t *drawOptions) 
{
  // Add a generic object to this plot. The specified options will be
  // used to Draw() this object later. The caller transfers ownership
  // of the object with this call, and the object will be deleted
  // when its containing plot object is destroyed.

  if(0 == obj) {
    std::cerr << fName << "::addObject: called with a null pointer" << std::endl;
    return;
  }

  fItems.Add(obj,drawOptions);

  return;
}

//_____________________________________________________________________________
void SamplingDistPlot::Draw(const Option_t * /*options */ ) 
{
  // Draw this plot and all of the elements it contains. The specified options
  // only apply to the drawing of our frame. The options specified in our add...()
  // methods will be used to draw each object we contain.

  Float_t theMin(0.), theMax(0.), theYMax(0.);

  GetAbsoluteInterval(theMin,theMax,theYMax);

  RooRealVar xaxis("xaxis",fVarName.Data(),theMin,theMax);
  RooPlot* frame = xaxis.frame();
  frame->SetTitle("");
  frame->SetMaximum(theYMax);

  fIterator->Reset();
  TH1F *obj = 0;
  while((obj= (TH1F*)fIterator->Next()))
    //obj->Draw(fIterator->GetOption());
    frame->addTH1(obj,fIterator->GetOption());

  frame->Draw();

  return;
}

//_____________________________________________________________________________
void SamplingDistPlot::GetAbsoluteInterval(Float_t &theMin, Float_t &theMax, Float_t &theYMax) const
{
  Float_t tmpmin = 999.;
  Float_t tmpmax = -999.;
  Float_t tmpYmax = -999.;


  fIterator->Reset();
  TH1F *obj = 0;
  while((obj = (TH1F*)fIterator->Next())) {
    if(obj->GetXaxis()->GetXmin() < tmpmin) tmpmin = obj->GetXaxis()->GetXmin();
    if(obj->GetXaxis()->GetXmax() > tmpmax) tmpmax = obj->GetXaxis()->GetXmax();
    if(obj->GetMaximum() > tmpYmax) tmpYmax = obj->GetMaximum() + 0.1*obj->GetMaximum();
  }

  theMin = tmpmin;
  theMax = tmpmax;
  theYMax = tmpYmax;

  return;
}

//_____________________________________________________________________________
void SamplingDistPlot::SetLineColor(const Color_t color, const SamplingDistribution *samplDist)
{
  if(samplDist == 0){
    fhist->SetLineColor(color);
  }
  else{
    fIterator->Reset();
    TH1F *obj = 0;
    while((obj = (TH1F*)fIterator->Next())) {
      if(!strcmp(obj->GetName(),samplDist->GetName())){
	obj->SetLineColor(color);
	break;
      }
    }
  }

  return;
}

//_____________________________________________________________________________
void SamplingDistPlot::SetLineWidth(const Width_t lwidth, const SamplingDistribution *samplDist)
{
  if(samplDist == 0){
    fhist->SetLineWidth(lwidth);
  }
  else{
    fIterator->Reset();
    TH1F *obj = 0;
    while((obj = (TH1F*)fIterator->Next())) {
      if(!strcmp(obj->GetName(),samplDist->GetName())){
	obj->SetLineWidth(lwidth);
	break;
      }
    }
  }

  return;
}

//_____________________________________________________________________________
void SamplingDistPlot::SetLineStyle(const Style_t style, const SamplingDistribution *samplDist)
{
  if(samplDist == 0){
    fhist->SetLineStyle(style);
  }
  else{
    fIterator->Reset();
    TH1F *obj = 0;
    while((obj = (TH1F*)fIterator->Next())) {
      if(!strcmp(obj->GetName(),samplDist->GetName())){
	obj->SetLineStyle(style);
	break;
      }
    }
  }

  return;
}

//_____________________________________________________________________________
void SamplingDistPlot::SetMarkerStyle(const Style_t style, const SamplingDistribution *samplDist)
{
  if(samplDist == 0){
    fhist->SetMarkerStyle(style);
  }
  else{
    fIterator->Reset();
    TH1F *obj = 0;
    while((obj = (TH1F*)fIterator->Next())) {
      if(!strcmp(obj->GetName(),samplDist->GetName())){
	obj->SetMarkerStyle(style);
	break;
      }
    }
  }

  return;
}

//_____________________________________________________________________________
void SamplingDistPlot::SetMarkerColor(const Color_t color, const SamplingDistribution *samplDist)
{
  if(samplDist == 0){
    fhist->SetMarkerColor(color);
  }
  else{
    fIterator->Reset();
    TH1F *obj = 0;
    while((obj = (TH1F*)fIterator->Next())) {
      if(!strcmp(obj->GetName(),samplDist->GetName())){
	obj->SetMarkerColor(color);
	break;
      }
    }
  }

  return;
}

//_____________________________________________________________________________
void SamplingDistPlot::SetMarkerSize(const Size_t size, const SamplingDistribution *samplDist)
{
  if(samplDist == 0){
    fhist->SetMarkerSize(size);
  }
  else{
    fIterator->Reset();
    TH1F *obj = 0;
    while((obj = (TH1F*)fIterator->Next())) {
      if(!strcmp(obj->GetName(),samplDist->GetName())){
	obj->SetMarkerSize(size);
	break;
      }
    }
  }

  return;
}

//_____________________________________________________________________________
void SamplingDistPlot::RebinDistribution(const Int_t rebinFactor, const SamplingDistribution *samplDist)
{
  if(samplDist == 0){
    fhist->Rebin(rebinFactor);
  }
  else{
    fIterator->Reset();
    TH1F *obj = 0;
    while((obj = (TH1F*)fIterator->Next())) {
      if(!strcmp(obj->GetName(),samplDist->GetName())){
	obj->Rebin(rebinFactor);
	break;
      }
    }
  }

  return;
}
 SamplingDistPlot.cxx:1
 SamplingDistPlot.cxx:2
 SamplingDistPlot.cxx:3
 SamplingDistPlot.cxx:4
 SamplingDistPlot.cxx:5
 SamplingDistPlot.cxx:6
 SamplingDistPlot.cxx:7
 SamplingDistPlot.cxx:8
 SamplingDistPlot.cxx:9
 SamplingDistPlot.cxx:10
 SamplingDistPlot.cxx:11
 SamplingDistPlot.cxx:12
 SamplingDistPlot.cxx:13
 SamplingDistPlot.cxx:14
 SamplingDistPlot.cxx:15
 SamplingDistPlot.cxx:16
 SamplingDistPlot.cxx:17
 SamplingDistPlot.cxx:18
 SamplingDistPlot.cxx:19
 SamplingDistPlot.cxx:20
 SamplingDistPlot.cxx:21
 SamplingDistPlot.cxx:22
 SamplingDistPlot.cxx:23
 SamplingDistPlot.cxx:24
 SamplingDistPlot.cxx:25
 SamplingDistPlot.cxx:26
 SamplingDistPlot.cxx:27
 SamplingDistPlot.cxx:28
 SamplingDistPlot.cxx:29
 SamplingDistPlot.cxx:30
 SamplingDistPlot.cxx:31
 SamplingDistPlot.cxx:32
 SamplingDistPlot.cxx:33
 SamplingDistPlot.cxx:34
 SamplingDistPlot.cxx:35
 SamplingDistPlot.cxx:36
 SamplingDistPlot.cxx:37
 SamplingDistPlot.cxx:38
 SamplingDistPlot.cxx:39
 SamplingDistPlot.cxx:40
 SamplingDistPlot.cxx:41
 SamplingDistPlot.cxx:42
 SamplingDistPlot.cxx:43
 SamplingDistPlot.cxx:44
 SamplingDistPlot.cxx:45
 SamplingDistPlot.cxx:46
 SamplingDistPlot.cxx:47
 SamplingDistPlot.cxx:48
 SamplingDistPlot.cxx:49
 SamplingDistPlot.cxx:50
 SamplingDistPlot.cxx:51
 SamplingDistPlot.cxx:52
 SamplingDistPlot.cxx:53
 SamplingDistPlot.cxx:54
 SamplingDistPlot.cxx:55
 SamplingDistPlot.cxx:56
 SamplingDistPlot.cxx:57
 SamplingDistPlot.cxx:58
 SamplingDistPlot.cxx:59
 SamplingDistPlot.cxx:60
 SamplingDistPlot.cxx:61
 SamplingDistPlot.cxx:62
 SamplingDistPlot.cxx:63
 SamplingDistPlot.cxx:64
 SamplingDistPlot.cxx:65
 SamplingDistPlot.cxx:66
 SamplingDistPlot.cxx:67
 SamplingDistPlot.cxx:68
 SamplingDistPlot.cxx:69
 SamplingDistPlot.cxx:70
 SamplingDistPlot.cxx:71
 SamplingDistPlot.cxx:72
 SamplingDistPlot.cxx:73
 SamplingDistPlot.cxx:74
 SamplingDistPlot.cxx:75
 SamplingDistPlot.cxx:76
 SamplingDistPlot.cxx:77
 SamplingDistPlot.cxx:78
 SamplingDistPlot.cxx:79
 SamplingDistPlot.cxx:80
 SamplingDistPlot.cxx:81
 SamplingDistPlot.cxx:82
 SamplingDistPlot.cxx:83
 SamplingDistPlot.cxx:84
 SamplingDistPlot.cxx:85
 SamplingDistPlot.cxx:86
 SamplingDistPlot.cxx:87
 SamplingDistPlot.cxx:88
 SamplingDistPlot.cxx:89
 SamplingDistPlot.cxx:90
 SamplingDistPlot.cxx:91
 SamplingDistPlot.cxx:92
 SamplingDistPlot.cxx:93
 SamplingDistPlot.cxx:94
 SamplingDistPlot.cxx:95
 SamplingDistPlot.cxx:96
 SamplingDistPlot.cxx:97
 SamplingDistPlot.cxx:98
 SamplingDistPlot.cxx:99
 SamplingDistPlot.cxx:100
 SamplingDistPlot.cxx:101
 SamplingDistPlot.cxx:102
 SamplingDistPlot.cxx:103
 SamplingDistPlot.cxx:104
 SamplingDistPlot.cxx:105
 SamplingDistPlot.cxx:106
 SamplingDistPlot.cxx:107
 SamplingDistPlot.cxx:108
 SamplingDistPlot.cxx:109
 SamplingDistPlot.cxx:110
 SamplingDistPlot.cxx:111
 SamplingDistPlot.cxx:112
 SamplingDistPlot.cxx:113
 SamplingDistPlot.cxx:114
 SamplingDistPlot.cxx:115
 SamplingDistPlot.cxx:116
 SamplingDistPlot.cxx:117
 SamplingDistPlot.cxx:118
 SamplingDistPlot.cxx:119
 SamplingDistPlot.cxx:120
 SamplingDistPlot.cxx:121
 SamplingDistPlot.cxx:122
 SamplingDistPlot.cxx:123
 SamplingDistPlot.cxx:124
 SamplingDistPlot.cxx:125
 SamplingDistPlot.cxx:126
 SamplingDistPlot.cxx:127
 SamplingDistPlot.cxx:128
 SamplingDistPlot.cxx:129
 SamplingDistPlot.cxx:130
 SamplingDistPlot.cxx:131
 SamplingDistPlot.cxx:132
 SamplingDistPlot.cxx:133
 SamplingDistPlot.cxx:134
 SamplingDistPlot.cxx:135
 SamplingDistPlot.cxx:136
 SamplingDistPlot.cxx:137
 SamplingDistPlot.cxx:138
 SamplingDistPlot.cxx:139
 SamplingDistPlot.cxx:140
 SamplingDistPlot.cxx:141
 SamplingDistPlot.cxx:142
 SamplingDistPlot.cxx:143
 SamplingDistPlot.cxx:144
 SamplingDistPlot.cxx:145
 SamplingDistPlot.cxx:146
 SamplingDistPlot.cxx:147
 SamplingDistPlot.cxx:148
 SamplingDistPlot.cxx:149
 SamplingDistPlot.cxx:150
 SamplingDistPlot.cxx:151
 SamplingDistPlot.cxx:152
 SamplingDistPlot.cxx:153
 SamplingDistPlot.cxx:154
 SamplingDistPlot.cxx:155
 SamplingDistPlot.cxx:156
 SamplingDistPlot.cxx:157
 SamplingDistPlot.cxx:158
 SamplingDistPlot.cxx:159
 SamplingDistPlot.cxx:160
 SamplingDistPlot.cxx:161
 SamplingDistPlot.cxx:162
 SamplingDistPlot.cxx:163
 SamplingDistPlot.cxx:164
 SamplingDistPlot.cxx:165
 SamplingDistPlot.cxx:166
 SamplingDistPlot.cxx:167
 SamplingDistPlot.cxx:168
 SamplingDistPlot.cxx:169
 SamplingDistPlot.cxx:170
 SamplingDistPlot.cxx:171
 SamplingDistPlot.cxx:172
 SamplingDistPlot.cxx:173
 SamplingDistPlot.cxx:174
 SamplingDistPlot.cxx:175
 SamplingDistPlot.cxx:176
 SamplingDistPlot.cxx:177
 SamplingDistPlot.cxx:178
 SamplingDistPlot.cxx:179
 SamplingDistPlot.cxx:180
 SamplingDistPlot.cxx:181
 SamplingDistPlot.cxx:182
 SamplingDistPlot.cxx:183
 SamplingDistPlot.cxx:184
 SamplingDistPlot.cxx:185
 SamplingDistPlot.cxx:186
 SamplingDistPlot.cxx:187
 SamplingDistPlot.cxx:188
 SamplingDistPlot.cxx:189
 SamplingDistPlot.cxx:190
 SamplingDistPlot.cxx:191
 SamplingDistPlot.cxx:192
 SamplingDistPlot.cxx:193
 SamplingDistPlot.cxx:194
 SamplingDistPlot.cxx:195
 SamplingDistPlot.cxx:196
 SamplingDistPlot.cxx:197
 SamplingDistPlot.cxx:198
 SamplingDistPlot.cxx:199
 SamplingDistPlot.cxx:200
 SamplingDistPlot.cxx:201
 SamplingDistPlot.cxx:202
 SamplingDistPlot.cxx:203
 SamplingDistPlot.cxx:204
 SamplingDistPlot.cxx:205
 SamplingDistPlot.cxx:206
 SamplingDistPlot.cxx:207
 SamplingDistPlot.cxx:208
 SamplingDistPlot.cxx:209
 SamplingDistPlot.cxx:210
 SamplingDistPlot.cxx:211
 SamplingDistPlot.cxx:212
 SamplingDistPlot.cxx:213
 SamplingDistPlot.cxx:214
 SamplingDistPlot.cxx:215
 SamplingDistPlot.cxx:216
 SamplingDistPlot.cxx:217
 SamplingDistPlot.cxx:218
 SamplingDistPlot.cxx:219
 SamplingDistPlot.cxx:220
 SamplingDistPlot.cxx:221
 SamplingDistPlot.cxx:222
 SamplingDistPlot.cxx:223
 SamplingDistPlot.cxx:224
 SamplingDistPlot.cxx:225
 SamplingDistPlot.cxx:226
 SamplingDistPlot.cxx:227
 SamplingDistPlot.cxx:228
 SamplingDistPlot.cxx:229
 SamplingDistPlot.cxx:230
 SamplingDistPlot.cxx:231
 SamplingDistPlot.cxx:232
 SamplingDistPlot.cxx:233
 SamplingDistPlot.cxx:234
 SamplingDistPlot.cxx:235
 SamplingDistPlot.cxx:236
 SamplingDistPlot.cxx:237
 SamplingDistPlot.cxx:238
 SamplingDistPlot.cxx:239
 SamplingDistPlot.cxx:240
 SamplingDistPlot.cxx:241
 SamplingDistPlot.cxx:242
 SamplingDistPlot.cxx:243
 SamplingDistPlot.cxx:244
 SamplingDistPlot.cxx:245
 SamplingDistPlot.cxx:246
 SamplingDistPlot.cxx:247
 SamplingDistPlot.cxx:248
 SamplingDistPlot.cxx:249
 SamplingDistPlot.cxx:250
 SamplingDistPlot.cxx:251
 SamplingDistPlot.cxx:252
 SamplingDistPlot.cxx:253
 SamplingDistPlot.cxx:254
 SamplingDistPlot.cxx:255
 SamplingDistPlot.cxx:256
 SamplingDistPlot.cxx:257
 SamplingDistPlot.cxx:258
 SamplingDistPlot.cxx:259
 SamplingDistPlot.cxx:260
 SamplingDistPlot.cxx:261
 SamplingDistPlot.cxx:262
 SamplingDistPlot.cxx:263
 SamplingDistPlot.cxx:264
 SamplingDistPlot.cxx:265
 SamplingDistPlot.cxx:266
 SamplingDistPlot.cxx:267
 SamplingDistPlot.cxx:268
 SamplingDistPlot.cxx:269
 SamplingDistPlot.cxx:270
 SamplingDistPlot.cxx:271
 SamplingDistPlot.cxx:272
 SamplingDistPlot.cxx:273
 SamplingDistPlot.cxx:274
 SamplingDistPlot.cxx:275
 SamplingDistPlot.cxx:276
 SamplingDistPlot.cxx:277
 SamplingDistPlot.cxx:278
 SamplingDistPlot.cxx:279
 SamplingDistPlot.cxx:280
 SamplingDistPlot.cxx:281
 SamplingDistPlot.cxx:282
 SamplingDistPlot.cxx:283
 SamplingDistPlot.cxx:284
 SamplingDistPlot.cxx:285
 SamplingDistPlot.cxx:286
 SamplingDistPlot.cxx:287
 SamplingDistPlot.cxx:288
 SamplingDistPlot.cxx:289
 SamplingDistPlot.cxx:290
 SamplingDistPlot.cxx:291
 SamplingDistPlot.cxx:292
 SamplingDistPlot.cxx:293
 SamplingDistPlot.cxx:294
 SamplingDistPlot.cxx:295
 SamplingDistPlot.cxx:296
 SamplingDistPlot.cxx:297
 SamplingDistPlot.cxx:298
 SamplingDistPlot.cxx:299
 SamplingDistPlot.cxx:300
 SamplingDistPlot.cxx:301
 SamplingDistPlot.cxx:302
 SamplingDistPlot.cxx:303
 SamplingDistPlot.cxx:304
 SamplingDistPlot.cxx:305
 SamplingDistPlot.cxx:306
 SamplingDistPlot.cxx:307
 SamplingDistPlot.cxx:308
 SamplingDistPlot.cxx:309
 SamplingDistPlot.cxx:310
 SamplingDistPlot.cxx:311
 SamplingDistPlot.cxx:312
 SamplingDistPlot.cxx:313
 SamplingDistPlot.cxx:314
 SamplingDistPlot.cxx:315
 SamplingDistPlot.cxx:316
 SamplingDistPlot.cxx:317
 SamplingDistPlot.cxx:318
 SamplingDistPlot.cxx:319
 SamplingDistPlot.cxx:320
 SamplingDistPlot.cxx:321
 SamplingDistPlot.cxx:322
 SamplingDistPlot.cxx:323
 SamplingDistPlot.cxx:324
 SamplingDistPlot.cxx:325
 SamplingDistPlot.cxx:326
 SamplingDistPlot.cxx:327
 SamplingDistPlot.cxx:328
 SamplingDistPlot.cxx:329
 SamplingDistPlot.cxx:330
 SamplingDistPlot.cxx:331
 SamplingDistPlot.cxx:332
 SamplingDistPlot.cxx:333
 SamplingDistPlot.cxx:334
 SamplingDistPlot.cxx:335
 SamplingDistPlot.cxx:336
 SamplingDistPlot.cxx:337
 SamplingDistPlot.cxx:338
 SamplingDistPlot.cxx:339
 SamplingDistPlot.cxx:340
 SamplingDistPlot.cxx:341
 SamplingDistPlot.cxx:342
 SamplingDistPlot.cxx:343
 SamplingDistPlot.cxx:344
 SamplingDistPlot.cxx:345
 SamplingDistPlot.cxx:346
 SamplingDistPlot.cxx:347
 SamplingDistPlot.cxx:348