• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

BCMTFChannel.cxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008-2012, Daniel Kollar and Kevin Kroeninger.
00003  * All rights reserved.
00004  *
00005  * For the licensing terms see doc/COPYING.
00006  */
00007 
00008 // ---------------------------------------------------------
00009 
00010 #include <iostream>
00011 
00012 #include <TCanvas.h>
00013 #include <TH1D.h>
00014 #include <TH2D.h>
00015 #include <TMath.h>
00016 
00017 #include "BCMTFTemplate.h"
00018 #include "BCMTFSystematicVariation.h"
00019 
00020 #include "BCMTFChannel.h"
00021 
00022 // ---------------------------------------------------------
00023 BCMTFChannel::BCMTFChannel(const char * name)
00024  : fData(0)
00025  , fFlagChannelActive(true)
00026  , fHistUncertaintyBandExpectation(0)
00027  , fHistUncertaintyBandPoisson(0)
00028 {
00029    fName = name;
00030 }
00031 
00032 // ---------------------------------------------------------
00033 BCMTFChannel::~BCMTFChannel()
00034 {
00035    if (fData)
00036       delete fData;
00037 
00038    for (unsigned int i = 0; i < fTemplateContainer.size(); ++i)
00039       delete (fTemplateContainer.at(i));
00040 
00041    for (unsigned int i = 0; i < fSystematicVariationContainer.size(); ++i)
00042       delete (fSystematicVariationContainer.at(i));
00043 
00044     /*
00045     if (fHistUncertaintyBandExpectation)
00046        delete fHistUncertaintyBandExpectation;
00047 
00048     if (fHistUncertaintyBandPoisson)
00049        delete fHistUncertaintyBandPoisson;
00050     */
00051 }
00052 
00053 // ---------------------------------------------------------
00054 void BCMTFChannel::PrintTemplates(const char * filename)
00055 {
00056    // create new canvas
00057    TCanvas * c1 = new TCanvas();
00058    c1->Divide(2, 2);
00059 
00060    std::string f(filename);
00061 
00062    // get number of templates
00063    unsigned int ntemplates = fTemplateContainer.size();
00064 
00065    // calculate number of pages
00066    unsigned int npages =  ntemplates / 4;
00067    if (ntemplates % 4 > 0)
00068       npages++;
00069 
00070    // loop over pages
00071    for (unsigned int i = 0; i < npages; ++i) {
00072       // loop over pads
00073       for (unsigned int j = 0; j < 4; ++j) {
00074          // calculate template index
00075          unsigned int templateindex = 4 * i + j;
00076 
00077          if (templateindex < ntemplates && GetTemplate(templateindex)->GetHistogram()) {
00078             // cd into pad
00079             c1->cd(j+1);
00080 
00081             // get histogram
00082             TH1D * hist = new TH1D( *( (TH1D *) GetTemplate(templateindex)->GetHistogram()->Clone() ) );
00083 
00084             // get efficiency
00085             double efficiency = GetTemplate(templateindex)->GetEfficiency();
00086 
00087             // scale histogram
00088             hist->Scale(efficiency/hist->Integral());
00089 
00090             // draw histogram
00091             hist->Draw("HIST");
00092          }
00093          else {
00094             // clear pad
00095             c1->cd(j+1)->Clear();
00096          }
00097       }
00098 
00099       if (npages == 1)
00100          c1->Print(f.c_str());
00101       else if (i == 0) {
00102          c1->Print( (f+std::string("[")).c_str() );
00103          c1->Print(f.c_str());
00104       }
00105       else if (i < npages-1) {
00106          c1->Print(f.c_str());
00107       }
00108       else {
00109          c1->Print(f.c_str());
00110          c1->Print( (f+std::string("]")).c_str() );
00111       }
00112    }
00113 
00114    // free memory
00115    delete c1;
00116 }
00117 
00118 // ---------------------------------------------------------
00119 void BCMTFChannel::PrintTemplate(int index, const char * filename)
00120 {
00121    // create new canvas
00122    TCanvas * c1 = new TCanvas();
00123    c1->cd();
00124 
00125    // get number of systematics
00126    unsigned int nsystematics = fSystematicVariationContainer.size();
00127 
00128    // draw template
00129    GetTemplate(index)->GetHistogram()->Draw();
00130 
00131    // print to file
00132    if (nsystematics == 0)
00133       c1->Print(filename);
00134    else
00135       c1->Print( (std::string(filename)+std::string("(")).c_str() );
00136 
00137    // loop over systematics
00138    for (unsigned int isystematic = 0; isystematic < nsystematics; ++isystematic) {
00139       c1->cd();
00140 
00141       // check that histogram exists
00142       if (!GetSystematicVariation(isystematic)->GetHistogramUp(index) ||
00143             !GetSystematicVariation(isystematic)->GetHistogramDown(index))
00144          continue;
00145 
00146       // get histogram
00147       TH1D hist = TH1D(*GetTemplate(index)->GetHistogram());
00148       TH1D hist_up(hist);
00149       TH1D hist_down(hist);
00150 
00151       // set style
00152       hist.SetFillStyle(0);
00153       hist_up.SetFillStyle(0);
00154       hist_up.SetLineStyle(2);
00155       hist_down.SetFillStyle(0);
00156       hist_down.SetLineStyle(3);
00157 
00158       // get efficiency
00159       double efficiency = GetTemplate(index)->GetEfficiency();
00160 
00161       // scale histogram
00162       hist.Scale(efficiency/ hist.Integral());
00163 
00164       // loop over all bins
00165       for (int i = 1; i <= hist.GetNbinsX(); ++i) {
00166          hist.SetBinContent(i, GetTemplate(index)->GetHistogram()->GetBinContent(i));
00167          hist_up.SetBinContent(i, hist.GetBinContent(i) * (1.0 + GetSystematicVariation(isystematic)->GetHistogramUp(index)->GetBinContent(i)));
00168          hist_down.SetBinContent(i, hist.GetBinContent(i) * (1.0 - GetSystematicVariation(isystematic)->GetHistogramDown(index)->GetBinContent(i)));
00169       }
00170 
00171       // draw histogram
00172       hist_up.Draw("HIST");
00173       hist.Draw("HISTSAME");
00174       hist_down.Draw("HISTSAME");
00175 
00176       if (isystematic < nsystematics-1)
00177          c1->Print(filename);
00178       else
00179          c1->Print( (std::string(filename)+std::string(")")).c_str() );
00180    }
00181 
00182    // free memory
00183    delete c1;
00184 }
00185 
00186 // ---------------------------------------------------------
00187 void BCMTFChannel::PrintHistUncertaintyBandExpectation(const char* filename)
00188 {
00189    // create new canvas
00190    TCanvas * c1 = new TCanvas();
00191    c1->cd();
00192     
00193     // draw histogram
00194     fHistUncertaintyBandExpectation->Draw("COLZ");
00195     c1->Draw();
00196 
00197     // print
00198     c1->Print(filename);
00199 
00200     // free memory
00201     delete c1;
00202 }
00203 
00204 // ---------------------------------------------------------
00205 void BCMTFChannel::CalculateHistUncertaintyBandPoisson()
00206 {
00207    // calculate histogram
00208    int nbinsy_exp = fHistUncertaintyBandExpectation->GetNbinsY();
00209    int nbinsx_poisson = fHistUncertaintyBandPoisson->GetNbinsX();
00210    int nbinsy_poisson = fHistUncertaintyBandPoisson->GetNbinsY();
00211    
00212    // loop over x-axis of observation
00213    for (int ix = 1; ix <= nbinsx_poisson; ++ix) {
00214       double sum_w = 0;
00215       // loop over y-axis of expectation and calculate sum of weights
00216       for (int iy = 1; iy <= nbinsy_exp; ++iy) {
00217          double w = fHistUncertaintyBandExpectation->GetBinContent(ix, iy);
00218          sum_w += w;
00219       }
00220       // loop over y-axis of expectation
00221       for (int iy = 1; iy <= nbinsy_exp; ++iy) {
00222          double w = fHistUncertaintyBandExpectation->GetBinContent(ix, iy)/sum_w;
00223          double expectation = fHistUncertaintyBandExpectation->GetYaxis()->GetBinCenter(iy);
00224          // loop over y-axis of observation
00225          for (int jbin = 1; jbin <= nbinsy_poisson; ++jbin) {
00226             double p = TMath::Poisson(double(jbin-1), expectation);
00227             double bincontent = 0;
00228             if (iy>1)
00229                bincontent=fHistUncertaintyBandPoisson->GetBinContent(ix, jbin);
00230             fHistUncertaintyBandPoisson->SetBinContent(ix, jbin, bincontent+p*w);
00231          }
00232       }
00233    }
00234    
00235 }
00236 
00237 // ---------------------------------------------------------
00238 TH1D* BCMTFChannel::CalculateUncertaintyBandPoisson(double minimum, double maximum, int color)
00239 {
00240    TH1D* hist = new TH1D(*(fData->GetHistogram()));
00241    hist->SetMarkerSize(0);
00242    hist->SetFillColor(color);
00243    hist->SetFillStyle(1001);
00244 
00245    int nbinsx_poisson = fHistUncertaintyBandPoisson->GetNbinsX();
00246    int nbinsy_poisson = fHistUncertaintyBandPoisson->GetNbinsY();
00247 
00248    // loop over x-axis of observation
00249    for (int ix = 1; ix <= nbinsx_poisson; ++ix) {
00250       double sum_p = 0;  // sum of all probabilities inside the interval
00251       int limit_min = 0;
00252       int limit_max = nbinsx_poisson-1;
00253       
00254       // loop over y-axis of observation
00255       for (int jbin = 1; jbin <= nbinsy_poisson; ++jbin) {
00256          double p = fHistUncertaintyBandPoisson->GetBinContent(ix, jbin);
00257          sum_p+=p;
00258          if (sum_p < minimum)
00259             limit_min=jbin;
00260          if (sum_p > maximum && (sum_p - p) < maximum ) 
00261             limit_max=jbin-1;
00262       }
00263       hist->SetBinContent(ix, 0.5*double(limit_min+limit_max));
00264       hist->SetBinError(ix, 0.5*double(limit_max-limit_min));
00265    }
00266 
00267    return hist;
00268 }
00269 
00270 // ---------------------------------------------------------
00271 void BCMTFChannel::PrintHistCumulativeUncertaintyBandPoisson(const char* filename)
00272 {
00273    // create new canvas
00274    TCanvas * c1 = new TCanvas();
00275    c1->cd();
00276 
00277     // calculate error band
00278     this->CalculateHistUncertaintyBandPoisson();
00279 
00280     TH2D hist(*fHistUncertaintyBandPoisson);
00281 
00282    int nbinsx_poisson = hist.GetNbinsX();
00283    int nbinsy_poisson = hist.GetNbinsY();
00284 
00285    // loop over x-axis of observation
00286    for (int ix = 1; ix <= nbinsx_poisson; ++ix) {
00287       double sum_p = 0;  // sum of all probabilities inside the interval
00288       
00289       // loop over y-axis of observation
00290       for (int jbin = 1; jbin <= nbinsy_poisson; ++jbin) {
00291          double p = hist.GetBinContent(ix, jbin);
00292          sum_p+=p;
00293          hist.SetBinContent(ix, jbin, sum_p);
00294       }
00295    }
00296 
00297     // draw histogram
00298     hist.Draw("COLZ");
00299     c1->Draw();
00300 
00301     // print
00302     c1->Print(filename);
00303 
00304     // free memory
00305     delete c1;
00306 }
00307 
00308 // ---------------------------------------------------------
00309 void BCMTFChannel::PrintHistUncertaintyBandPoisson(const char* filename)
00310 {
00311    // create new canvas
00312    TCanvas * c1 = new TCanvas();
00313    c1->cd();
00314 
00315     // calculate error band
00316     this->CalculateHistUncertaintyBandPoisson();
00317 
00318     // draw histogram
00319     fHistUncertaintyBandPoisson->Draw("COLZ");
00320     c1->Draw();
00321 
00322     // print
00323     c1->Print(filename);
00324 
00325     // free memory
00326     delete c1;
00327 }
00328 
00329 // ---------------------------------------------------------

Generated by  doxygen 1.7.1