SamplingDistPlot.cxx

Go to the documentation of this file.
00001 // @(#)root/roostats:$Id: SamplingDistPlot.cxx 37333 2010-12-06 14:26:57Z moneta $
00002 // Authors: Sven Kreiss    June 2010
00003 // Authors: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke
00004 /*************************************************************************
00005  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //____________________________________________________________________
00013 /*
00014 SamplingDistPlot : 
00015 
00016 This class provides simple and straightforward utilities to plot SamplingDistribution
00017 objects.
00018 */
00019 
00020 #include "RooStats/SamplingDistPlot.h"
00021 
00022 #include "RooRealVar.h"
00023 #include "TStyle.h"
00024 #include "TLine.h"
00025 #include "TFile.h"
00026 
00027 #include <algorithm>
00028 #include <iostream>
00029 
00030 
00031 #ifndef ROO_MSG_SERVICE
00032 #include "RooMsgService.h"
00033 #endif
00034 
00035 /// ClassImp for building the THtml documentation of the class 
00036 ClassImp(RooStats::SamplingDistPlot);
00037 
00038 using namespace RooStats;
00039 
00040 //_______________________________________________________
00041 SamplingDistPlot::SamplingDistPlot(Int_t nbins) :
00042    fHist(0),
00043    fLegend(NULL),
00044    fItems(),
00045    fOtherItems(),
00046    fRooPlot(NULL),
00047    fLogXaxis(kFALSE),
00048    fLogYaxis(kFALSE),
00049    fApplyStyle(kTRUE),
00050    fFillStyle(3004)
00051 {
00052   // SamplingDistPlot default constructor with bin size
00053   fIterator = fItems.MakeIterator();
00054   fIsWeighted = kFALSE;
00055   fBins = nbins;
00056   fMarkerType = 20;
00057   fColor = 1;
00058 }
00059 
00060 /*
00061 //_______________________________________________________
00062 SamplingDistPlot::SamplingDistPlot(const char* name, const char* title, Int_t nbins, Double_t xmin, Double_t xmax) :
00063    fHist(0),
00064    fLegend(NULL),
00065    fItems(),
00066    fOtherItems(),
00067    fRooPlot(NULL),
00068    fLogXaxis(kFALSE),
00069    fLogYaxis(kFALSE),
00070    fApplyStyle(kTRUE),
00071    fFillStyle(3004)
00072 {
00073   // SamplingDistPlot constructor
00074   fHist = new TH1F(name, title, nbins, xmin, xmax);
00075   fIterator = fItems.MakeIterator();
00076   fIsWeighted = kFALSE;
00077   fBins = nbins;
00078   fMarkerType = 20;
00079   fColor = 1;
00080 }
00081 */
00082 
00083 //_______________________________________________________
00084 Double_t SamplingDistPlot::AddSamplingDistribution(const SamplingDistribution *samplingDist, Option_t *drawOptions) {
00085    // adds sampling distribution (and normalizes if "NORMALIZE" is given as an option)
00086 
00087    fSamplingDistr = samplingDist->GetSamplingDistribution();
00088    SetSampleWeights(samplingDist);
00089 
00090    TString options(drawOptions);
00091    options.ToUpper();
00092 
00093    const Double_t xlow = *(std::min_element(fSamplingDistr.begin(), fSamplingDistr.end()));
00094    const Double_t xup = *(std::max_element(fSamplingDistr.begin(), fSamplingDistr.end()));
00095 
00096    fHist = new TH1F(samplingDist->GetName(), samplingDist->GetTitle(), fBins, xlow, xup);
00097 
00098    TString varName = samplingDist->GetVarName();
00099    fHist->GetXaxis()->SetTitle(varName.Data());
00100    if(varName.Length() > 0) fVarName = samplingDist->GetVarName().Data();
00101 
00102 
00103    std::vector<Double_t>::iterator valuesIt = fSamplingDistr.begin();
00104    for (int w_idx = 0; valuesIt != fSamplingDistr.end(); ++valuesIt, ++w_idx) {
00105       if (fIsWeighted) fHist->Fill(*valuesIt, fSampleWeights[w_idx]);
00106       else fHist->Fill(*valuesIt);
00107    }
00108 
00109    // NORMALIZATION
00110    fHist->Sumw2();
00111    double weightSum = 1.0;
00112    if(options.Contains("NORMALIZE")) {
00113       weightSum = fHist->Integral("width");
00114       fHist->Scale(1./weightSum);
00115 
00116       options.ReplaceAll("NORMALIZE", "");
00117       options.Strip();
00118    }
00119 
00120 
00121    //some basic aesthetics
00122    fHist->SetMarkerStyle(fMarkerType);
00123    fHist->SetMarkerColor(fColor);
00124    fHist->SetLineColor(fColor);
00125 
00126    fMarkerType++;
00127    fColor++;
00128 
00129    fHist->SetStats(kFALSE);
00130 
00131    addObject(fHist, options.Data());
00132 
00133    TString title = samplingDist->GetTitle();
00134    if(fLegend  &&  title.Length() > 0) fLegend->AddEntry(fHist, title, "L");
00135 
00136    return 1./weightSum;
00137 }
00138 
00139 //_______________________________________________________
00140 Double_t SamplingDistPlot::AddSamplingDistributionShaded(const SamplingDistribution *samplingDist, Double_t minShaded, Double_t maxShaded, Option_t *drawOptions) {
00141    Double_t scaleFactor = AddSamplingDistribution(samplingDist, drawOptions);
00142 
00143    TH1F *shaded = (TH1F*)fHist->Clone((string(samplingDist->GetName())+string("_shaded")).c_str());
00144    shaded->SetFillStyle(fFillStyle++);
00145    shaded->SetLineWidth(0);
00146 
00147    for (int i=0; i<shaded->GetNbinsX(); ++i) {
00148       if (shaded->GetBinCenter(i) < minShaded || shaded->GetBinCenter(i) > maxShaded){
00149          shaded->SetBinContent(i,0);
00150       }
00151    }
00152 
00153    TString options(drawOptions);
00154    options.ToUpper();
00155    if(options.Contains("NORMALIZE")) {
00156       options.ReplaceAll("NORMALIZE", "");
00157       options.Strip();
00158    }
00159    addObject(shaded, options.Data());
00160 
00161    return scaleFactor;
00162 }
00163 
00164 void SamplingDistPlot::AddLine(Double_t x1, Double_t y1, Double_t x2, Double_t y2, const char* title) {
00165    TLine *line = new TLine(x1, y1, x2, y2);
00166    line->SetLineWidth(3);
00167    line->SetLineColor(kBlack);
00168 
00169    if(fLegend  &&  title) fLegend->AddEntry(line, title, "L");
00170 
00171    addOtherObject(line, ""); // no options
00172 }
00173 
00174 
00175 //_______________________________________________________
00176 void SamplingDistPlot::SetSampleWeights(const SamplingDistribution* samplingDist)
00177 {
00178   //Determine if the sampling distribution has weights and store them
00179 
00180   fIsWeighted = kFALSE;
00181 
00182   if(samplingDist->GetSampleWeights().size() != 0){
00183     fIsWeighted = kTRUE;
00184     fSampleWeights = samplingDist->GetSampleWeights();
00185   }  
00186 
00187   return;
00188 }
00189 
00190 void SamplingDistPlot::addObject(TObject *obj, Option_t *drawOptions) 
00191 {
00192   // Add a generic object to this plot. The specified options will be
00193   // used to Draw() this object later. The caller transfers ownership
00194   // of the object with this call, and the object will be deleted
00195   // when its containing plot object is destroyed.
00196 
00197   if(0 == obj) {
00198     std::cerr << fName << "::addObject: called with a null pointer" << std::endl;
00199     return;
00200   }
00201 
00202   fItems.Add(obj,drawOptions);
00203 
00204   return;
00205 }
00206 void SamplingDistPlot::addOtherObject(TObject *obj, Option_t *drawOptions)
00207 {
00208   // Add a generic object to this plot. The specified options will be
00209   // used to Draw() this object later. The caller transfers ownership
00210   // of the object with this call, and the object will be deleted
00211   // when its containing plot object is destroyed.
00212 
00213   if(0 == obj) {
00214     std::cerr << fName << "::addOtherObject: called with a null pointer" << std::endl;
00215     return;
00216   }
00217 
00218   fOtherItems.Add(obj,drawOptions);
00219 
00220   return;
00221 }
00222 
00223 //_____________________________________________________________________________
00224 void SamplingDistPlot::Draw(Option_t * /*options */) {
00225    // Draw this plot and all of the elements it contains. The specified options
00226    // only apply to the drawing of our frame. The options specified in our add...()
00227    // methods will be used to draw each object we contain.
00228 
00229    ApplyDefaultStyle();
00230 
00231    Float_t theMin(0.), theMax(0.), theYMax(0.);
00232    GetAbsoluteInterval(theMin, theMax, theYMax);
00233 
00234    RooRealVar xaxis("xaxis", fVarName.Data(), theMin, theMax);
00235    fRooPlot = xaxis.frame();
00236    fRooPlot->SetTitle("");
00237    fRooPlot->SetMaximum(theYMax);
00238 
00239    fIterator->Reset();
00240    TH1F *obj = 0;
00241    while ((obj = (TH1F*) fIterator->Next())) {
00242       //obj->Draw(fIterator->GetOption());
00243       fRooPlot->addTH1(obj, fIterator->GetOption());
00244    }
00245 
00246    TIterator *otherIt = fOtherItems.MakeIterator();
00247    TObject *otherObj = NULL;
00248    while ((otherObj = otherIt->Next())) {
00249       fRooPlot->addObject(otherObj, otherIt->GetOption());
00250    }
00251    delete otherIt;
00252 
00253 
00254    if(fLegend) fRooPlot->addObject(fLegend);
00255 
00256    if(bool(gStyle->GetOptLogx()) != fLogXaxis) {
00257       if(!fApplyStyle) coutW(Plotting) << "gStyle will be changed to adjust SetOptLogx(...)";
00258       gStyle->SetOptLogx(fLogXaxis);
00259    }
00260    if(bool(gStyle->GetOptLogy()) != fLogYaxis) {
00261       if(!fApplyStyle) coutW(Plotting) << "gStyle will be changed to adjust SetOptLogy(...)";
00262       gStyle->SetOptLogy(fLogYaxis);
00263    }
00264    fRooPlot->Draw();
00265 
00266    return;
00267 }
00268 
00269 void SamplingDistPlot::ApplyDefaultStyle(void) {
00270    if(fApplyStyle) {
00271       // use plain black on white colors
00272       Int_t icol = 0;
00273       gStyle->SetFrameBorderMode( icol );
00274       gStyle->SetCanvasBorderMode( icol );
00275       gStyle->SetPadBorderMode( icol );
00276       gStyle->SetPadColor( icol );
00277       gStyle->SetCanvasColor( icol );
00278       gStyle->SetStatColor( icol );
00279       gStyle->SetFrameFillStyle( 0 );
00280 
00281       // set the paper & margin sizes
00282       gStyle->SetPaperSize( 20, 26 );
00283 
00284       if(fLegend) {
00285          fLegend->SetFillColor(0);
00286          fLegend->SetBorderSize(1);
00287       }
00288    }
00289 }
00290 
00291 //_____________________________________________________________________________
00292 void SamplingDistPlot::GetAbsoluteInterval(Float_t &theMin, Float_t &theMax, Float_t &theYMax) const
00293 {
00294   Float_t tmpmin = 999.;
00295   Float_t tmpmax = -999.;
00296   Float_t tmpYmax = -999.;
00297 
00298 
00299   fIterator->Reset();
00300   TH1F *obj = 0;
00301   while((obj = (TH1F*)fIterator->Next())) {
00302     if(obj->GetXaxis()->GetXmin() < tmpmin) tmpmin = obj->GetXaxis()->GetXmin();
00303     if(obj->GetXaxis()->GetXmax() > tmpmax) tmpmax = obj->GetXaxis()->GetXmax();
00304     if(obj->GetMaximum() > tmpYmax) tmpYmax = obj->GetMaximum() + 0.1*obj->GetMaximum();
00305   }
00306 
00307   theMin = tmpmin;
00308   theMax = tmpmax;
00309   theYMax = tmpYmax;
00310 
00311   return;
00312 }
00313 
00314 //_____________________________________________________________________________
00315 void SamplingDistPlot::SetLineColor(Color_t color, const SamplingDistribution *samplDist) {
00316    // Sets line color for given sampling distribution and
00317    // fill color for the associated shaded TH1F.
00318 
00319    if (samplDist == 0) {
00320       fHist->SetLineColor(color);
00321    } else {
00322       fIterator->Reset();
00323       TH1F *obj = 0;
00324 
00325       TString shadedName(samplDist->GetName());
00326       shadedName += "_shaded";
00327 
00328       while ((obj = (TH1F*) fIterator->Next())) {
00329          if (!strcmp(obj->GetName(), samplDist->GetName())) {
00330             obj->SetLineColor(color);
00331             //break;
00332          }
00333          if (!strcmp(obj->GetName(), shadedName.Data())) {
00334             obj->SetLineColor(color);
00335             obj->SetFillColor(color);
00336             //break;
00337          }
00338       }
00339    }
00340 
00341    return;
00342 }
00343 
00344 //_____________________________________________________________________________
00345 void SamplingDistPlot::SetLineWidth(Width_t lwidth, const SamplingDistribution *samplDist)
00346 {
00347   if(samplDist == 0){
00348     fHist->SetLineWidth(lwidth);
00349   }
00350   else{
00351     fIterator->Reset();
00352     TH1F *obj = 0;
00353     while((obj = (TH1F*)fIterator->Next())) {
00354       if(!strcmp(obj->GetName(),samplDist->GetName())){
00355         obj->SetLineWidth(lwidth);
00356         break;
00357       }
00358     }
00359   }
00360 
00361   return;
00362 }
00363 
00364 //_____________________________________________________________________________
00365 void SamplingDistPlot::SetLineStyle(Style_t style, const SamplingDistribution *samplDist)
00366 {
00367   if(samplDist == 0){
00368     fHist->SetLineStyle(style);
00369   }
00370   else{
00371     fIterator->Reset();
00372     TH1F *obj = 0;
00373     while((obj = (TH1F*)fIterator->Next())) {
00374       if(!strcmp(obj->GetName(),samplDist->GetName())){
00375         obj->SetLineStyle(style);
00376         break;
00377       }
00378     }
00379   }
00380 
00381   return;
00382 }
00383 
00384 //_____________________________________________________________________________
00385 void SamplingDistPlot::SetMarkerStyle(Style_t style, const SamplingDistribution *samplDist)
00386 {
00387   if(samplDist == 0){
00388     fHist->SetMarkerStyle(style);
00389   }
00390   else{
00391     fIterator->Reset();
00392     TH1F *obj = 0;
00393     while((obj = (TH1F*)fIterator->Next())) {
00394       if(!strcmp(obj->GetName(),samplDist->GetName())){
00395         obj->SetMarkerStyle(style);
00396         break;
00397       }
00398     }
00399   }
00400 
00401   return;
00402 }
00403 
00404 //_____________________________________________________________________________
00405 void SamplingDistPlot::SetMarkerColor(Color_t color, const SamplingDistribution *samplDist)
00406 {
00407   if(samplDist == 0){
00408     fHist->SetMarkerColor(color);
00409   }
00410   else{
00411     fIterator->Reset();
00412     TH1F *obj = 0;
00413     while((obj = (TH1F*)fIterator->Next())) {
00414       if(!strcmp(obj->GetName(),samplDist->GetName())){
00415         obj->SetMarkerColor(color);
00416         break;
00417       }
00418     }
00419   }
00420 
00421   return;
00422 }
00423 
00424 //_____________________________________________________________________________
00425 void SamplingDistPlot::SetMarkerSize(Size_t size, const SamplingDistribution *samplDist)
00426 {
00427   if(samplDist == 0){
00428     fHist->SetMarkerSize(size);
00429   }
00430   else{
00431     fIterator->Reset();
00432     TH1F *obj = 0;
00433     while((obj = (TH1F*)fIterator->Next())) {
00434       if(!strcmp(obj->GetName(),samplDist->GetName())){
00435         obj->SetMarkerSize(size);
00436         break;
00437       }
00438     }
00439   }
00440 
00441   return;
00442 }
00443 
00444 //_____________________________________________________________________________
00445 TH1F* SamplingDistPlot::GetTH1F(const SamplingDistribution *samplDist)
00446 {
00447   if(samplDist == NULL){
00448     return fHist;
00449   }else{
00450     fIterator->Reset();
00451     TH1F *obj = 0;
00452     while((obj = (TH1F*)fIterator->Next())) {
00453       if(!strcmp(obj->GetName(),samplDist->GetName())){
00454         return obj;
00455       }
00456     }
00457   }
00458 
00459   return NULL;
00460 }
00461 
00462 
00463 //_____________________________________________________________________________
00464 void SamplingDistPlot::RebinDistribution(Int_t rebinFactor, const SamplingDistribution *samplDist)
00465 {
00466   if(samplDist == 0){
00467     fHist->Rebin(rebinFactor);
00468   }
00469   else{
00470     fIterator->Reset();
00471     TH1F *obj = 0;
00472     while((obj = (TH1F*)fIterator->Next())) {
00473       if(!strcmp(obj->GetName(),samplDist->GetName())){
00474         obj->Rebin(rebinFactor);
00475         break;
00476       }
00477     }
00478   }
00479 
00480   return;
00481 }
00482 
00483 
00484 // TODO test
00485 void SamplingDistPlot::DumpToFile(const char* RootFileName, Option_t *option, const char *ftitle, Int_t compress) {
00486    // All the objects are written to rootfile
00487 
00488    if(!fRooPlot) {
00489       cout << "Plot was not drawn yet. Dump can only be saved after it was drawn with Draw()." << endl;
00490       return;
00491    }
00492 
00493    TFile ofile(RootFileName, option, ftitle, compress);
00494    ofile.cd();
00495    fRooPlot->Write();
00496    ofile.Close();
00497 }
00498 

Generated on Tue Jul 5 15:14:36 2011 for ROOT_528-00b_version by  doxygen 1.5.1