Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Plot.h File Reference

#include <iostream>
#include <map>
#include <string>
#include <vector>
#include "PhysicsNtuple/DataBlock.h"

Go to the source code of this file.

Namespaces

namespace  Plot

Classes

class  Plot::Style
class  Plot::Band
class  Plot::Hist
class  Plot::Range

Functions

double GetChi2 (TH1 *h1, TH1 *h2, const std::string &option="", const std::string &tag="")
double GetChi2 (const Hist &lhs, const Hist &rhs, const std::string &option="", std::ostream &o=std::cout)
const std::string Format (double v, int w, int p)
bool operator== (const Band &lhs, const Band &rhs)
void TestHist (TH1 *hO, TH1 *hC, std::string option="")
void TestError (TH1 *hI, std::string option="")


Function Documentation

const string Plot::Format double  v,
int  w,
int  p
 

Definition at line 241 of file Plot.cxx.

Referenced by add_projection(), NCExtrapolation::AddEvent(), AxesForNCParameter(), AxesForNCParameters(), NCExtrapolationModule::DefaultConfig(), NCDataQualityModule::DefaultConfig(), NCBeam::Info::GetDescription(), NCDataQualityModule::MarkPrelim(), PIDSpectrum::ProjectHist(), and NC::Utility::ReportProgress().

00242 {
00243    stringstream ss;
00244    ss << std::fixed << std::setw(w) << std::setprecision(p) << v;
00245    return ss.str();
00246 }

double Plot::GetChi2 const Hist &  lhs,
const Hist &  rhs,
const std::string &  option = "",
std::ostream &  o = std::cout
 

Definition at line 32 of file Plot.cxx.

References Plot::Band::Bin(), Plot::Hist::GetErrorBand(), Plot::Hist::GetHist(), Plot::Band::High(), Plot::Band::Low(), and option.

00033 {
00034    TH1 *hL = lhs.GetHist();
00035    TH1 *hR = rhs.GetHist();
00036 
00037    if(!hL || !hR)
00038    {
00039       cerr << "GetChi2() - null histogram pointers" << endl;
00040       return 0.0;
00041    }
00042    if(hL -> GetNbinsX() != hR -> GetNbinsX())
00043    {
00044       cerr << "GetChi2() - histograms have unequal number of bins" << endl;
00045       return 0.0;
00046    }
00047 
00048    unsigned int ndof = 0;
00049    long double chi2 = 0.0;
00050 
00051    int lbin = 1, hbin = hL -> GetNbinsX();
00052 
00053    if(option.find("U") != string::npos || option.find("u") != string::npos)
00054    {
00055       --lbin;
00056    }
00057    if(option.find("O") != string::npos || option.find("o") != string::npos)
00058    {
00059       ++hbin;
00060    }
00061    if(!(lbin < hbin))
00062    {
00063       cerr << "GetChi2() - invalid range of histogram bins" << endl;
00064       return 0.0;
00065    }
00066 
00067    const map<int, pair<Band, Band> >& bandL = lhs.GetErrorBand();
00068    const map<int, pair<Band, Band> >& bandR = rhs.GetErrorBand();
00069 
00070    for(int ibin = lbin; ibin <= hbin; ++ibin)
00071    {
00072       const double valueL = hL -> GetBinContent(ibin);
00073       const double valueR = hR -> GetBinContent(ibin);   
00074     
00075       double num = 0.0, den = 0.0;
00076       if(valueL > 0.0 && valueR > 0.0)
00077       {
00078          num = (valueL - valueR)*(valueL - valueR);
00079          den = valueL + valueR;
00080       }
00081       else if(valueL > 0.0)
00082       {
00083          num = valueL*valueL;
00084          den = valueL + 1;
00085       }
00086       else if(valueR > 0.0)
00087       {
00088          num = valueR*valueR;
00089          den = valueR + 1;
00090       }
00091       else
00092       {
00093          continue;
00094       }
00095 
00096       const map<int, pair<Band, Band> >::const_iterator lit = bandL.find(ibin);
00097       if(false && lit != bandL.end())
00098       {
00099          const Band &band = (lit -> second).second;
00100          assert(band.Bin() == ibin && "Mismatched bin number");
00101 
00102          if(band.Low() > band.High())
00103          {
00104             den += band.Low();
00105          }
00106          else
00107          {
00108             den += band.High();
00109          }
00110       }
00111       
00112       const map<int, pair<Band, Band> >::const_iterator rit = bandR.find(ibin);
00113       if(false && rit != bandR.end())
00114       {
00115          const Band &band = (rit -> second).second;
00116          assert(band.Bin() == ibin && "Mismatched bin number");
00117 
00118          if(band.Low() > band.High())
00119          {
00120             den += band.Low();
00121          }
00122          else
00123          {
00124             den += band.High();
00125          }
00126       }
00127 
00128       if(!(den > 0.0))
00129       {
00130          cerr << "Plot::GetChi2 - bin error is zero" << endl;
00131          continue;
00132       }
00133 
00134       ++ndof;
00135       chi2 += num/den;
00136    }
00137 
00138    if(!(ndof > 0))
00139    {
00140       return 0.0;
00141    }
00142 
00143    if(option.find("P") != string::npos || option.find("V") != string::npos)
00144    {
00145       o << "chi2/ndof = " << chi2 << "/" << ndof << " = " << chi2/ndof << endl;
00146    }
00147 
00148    if(option.find("chi2") != string::npos)
00149    {
00150       return chi2;
00151    }
00152    else if(option.find("ndof") != string::npos)
00153    {
00154       return ndof;
00155    }
00156 
00157    return chi2/ndof;
00158 }

double Plot::GetChi2 TH1 *  h1,
TH1 *  h2,
const std::string &  option = "",
const std::string &  tag = ""
 

Definition at line 161 of file Plot.cxx.

References option.

00162 {
00163    if(!h1 || !h2)
00164    {
00165       cerr << "GetChi2() - invalid histogram pointers" << endl;
00166       return 0.0;
00167    }
00168    if(h1 -> GetNbinsX() != h2 -> GetNbinsX())
00169    {
00170       cerr << "GetChi2() - histograms have unequal number of bins" << endl;
00171       return 0.0;
00172    }
00173 
00174    int lbin = 1, hbin = h1 -> GetNbinsX();
00175 
00176    if(option.find("U") != string::npos || option.find("u") != string::npos)
00177    {
00178       --lbin;
00179    }
00180    if(option.find("O") != string::npos || option.find("o") != string::npos)
00181    {
00182       ++hbin;
00183    }
00184    if(!(lbin < hbin))
00185    {
00186       cerr << "GetChi2() - invalid range of histogram bins" << endl;
00187       return 0.0;
00188    }
00189 
00190    double ndof = 0.0, chi2 = 0.0;
00191    for(int ibin = lbin; ibin <= hbin; ++ibin)
00192    {
00193       const double valueL = h1 -> GetBinContent(ibin);
00194       const double valueR = h2 -> GetBinContent(ibin);
00195     
00196       double num = 0.0, den = 0.0;
00197       if(valueL > 0.0 && valueR > 0.0)
00198       {
00199          num = (valueL - valueR)*(valueL - valueR);
00200          den = valueL + valueR;
00201       }
00202       else if(valueL > 0.0)
00203       {
00204          num = valueL*valueL;
00205          den = valueL + 1;
00206       }
00207       else if(valueR > 0.0)
00208       {
00209          num = valueR*valueR;
00210          den = valueR + 1;
00211       }
00212       else
00213       {
00214          continue;
00215       }
00216 
00217       ++ndof;
00218    }
00219 
00220    if(!(ndof > 0.0))
00221    {
00222       return 0.0;
00223    }
00224 
00225    if(option.find("P") != string::npos || option.find("V") != string::npos || !tag.empty())
00226    {
00227       if(tag.empty())
00228       {
00229          cout << "chi2/ndof = " << chi2 << "/" << ndof << " = " << chi2/ndof << endl;
00230       }
00231       else
00232       {
00233          cout << tag << ": chi2/ndof = " << chi2 << "/" << ndof << " = " << chi2/ndof << endl;
00234       }
00235    }
00236 
00237    return chi2/ndof;
00238 }

bool Plot::operator== const Band &  lhs,
const Band &  rhs
 

Definition at line 1552 of file Plot.cxx.

References Plot::Band::Bin().

01553 {
01554    return (lhs.Bin() == rhs.Bin());
01555 }

void Plot::TestError TH1 *  hI,
std::string  option = ""
 

Definition at line 1707 of file Plot.cxx.

References Draw(), and option.

01708 {
01709    cout << "Plot::TestError - option = " << option << endl;
01710 
01711    TH1 *hO = dynamic_cast<TH1 *>(hI -> Clone("hO"));
01712    TH1 *hN = dynamic_cast<TH1 *>(hI -> Clone("hN"));
01713 
01714    hO -> SetDirectory(0);
01715    hN -> SetDirectory(0);
01716 
01717    const int nbin = hN -> GetNbinsX();
01718 
01719    for(int ibin = 1; ibin <= nbin; ++ibin)
01720    {
01721       const double value = hN -> GetBinContent(ibin);
01722       
01723       if(option.find("half") != std::string::npos)
01724       {
01725          hN -> SetBinError(ibin, 0.5*std::sqrt(value));
01726       }
01727       else
01728       {
01729          hN -> SetBinError(ibin, std::sqrt(value));
01730       }
01731    }
01732 
01733    hO -> SetLineColor(2);
01734    hN -> SetLineColor(4);
01735 
01736    TCanvas *canvas = new TCanvas("ctest", "ctest", 0, 0, 720, 450);
01737    canvas -> SetGrid();
01738    canvas -> Draw();
01739    
01740    hO -> Draw("E1");
01741    hN -> Draw("E1 sames");  
01742 }

void Plot::TestHist TH1 *  hO,
TH1 *  hC,
std::string  option = ""
 

Definition at line 1558 of file Plot.cxx.

References Anp::Bin< T >::add(), Draw(), Anp::Bin< T >::edge(), Anp::Hist1d< T >::Fill(), Anp::Hist1d< T >::Merge(), option, Anp::Hist1d< T >::Print(), Anp::Hist1d< T >::Reset(), and Anp::Bin< T >::sum().

01559 {
01560    const double nmin = 1000;
01561       
01562    vector<Anp::Bin<double> > bvec;
01563    
01564    map<int, double> bmap;
01565    
01566    Anp::Hist1d<double> histO(*hO);
01567    Anp::Hist1d<double> histC(*hC);
01568    
01569    cout << "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO" << endl;
01570    histO.Print();
01571    const unsigned int nmergeO = histO.Merge(1000.0);
01572    histO.Print();
01573    
01574    cout << "Merged " << nmergeO << " bins" << endl;
01575    
01576    cout << "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" << endl;
01577    histC.Print();
01578    const unsigned int nmergeC = histC.Merge(1000.0);
01579    histC.Print();
01580    
01581    cout << "Merged " << nmergeC << " bins" << endl;
01582    
01583    cout << "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN" << endl;
01584    histO.Reset();
01585    histO.Fill(*hC);
01586    histO.Print();
01587 
01588    cout << "Attempting draw test..." << endl;
01589 
01590    TH1 *h1 = Anp::CreateTH1<double>(histO, "h1");
01591    TH1 *h2 = Anp::CreateTH1<double>(histC, "h2");
01592    
01593    hC -> SetLineColor(1);
01594    hC -> SetLineWidth(2);
01595    hC -> SetFillColor(0);
01596    h1 -> SetLineColor(2);
01597    h2 -> SetLineColor(4);
01598 
01599    if(!h1)
01600    {
01601       cerr << "Failed to create h1" << endl;
01602       return;
01603    }
01604    if(!h2)
01605    {
01606       cerr << "Failed to create h2" << endl;
01607       return;
01608    }
01609 
01610    TCanvas *canvas = new TCanvas("ctest", "ctest", 0, 0, 650, 400);
01611    canvas -> SetGrid();
01612    canvas -> Draw();
01613    
01614    hC -> Draw("HIST");
01615    h1 -> Draw("sames");   
01616    h2 -> Draw("sames");
01617 
01618    cout << "Finished draw test" << endl;
01619 
01620    return;
01621 
01622    const int nbin = hC -> GetNbinsX();
01623 
01624    for(int ibin = 1; ibin <= nbin; ++ibin)
01625    {
01626       const double value = hO -> GetBinContent(ibin);
01627       const double edge  = hO -> GetBinLowEdge(ibin);
01628       
01629       Anp::Bin<double> bin(ibin, edge);
01630       bin.add(value);
01631       
01632       bvec.push_back(bin);
01633    }
01634    
01635    unsigned int osize = bvec.size();
01636    double osum = 0.0;
01637    
01638    if(option.find("V") != string::npos)
01639    {
01640       for(unsigned int ibin = 0; ibin < bvec.size(); ++ibin)
01641       {
01642          const Anp::Bin<double> &bin = bvec[ibin];
01643          cout << "ibin = " << ibin << ", edge = " << bin.edge() << ", sum = " << bin.sum() << endl; 
01644          osum += bin.sum();
01645       }
01646    }
01647 
01648    std::sort(bvec.begin(), bvec.end());
01649    
01650    vector<Anp::Bin<double> >::iterator bit = bvec.begin();
01651    while(bit != bvec.end())
01652    {
01653       if(bit -> sum() > nmin || bit == bvec.begin() || bit + 1 == bvec.end())
01654       {
01655          ++bit;
01656          continue;
01657       }
01658       
01659       vector<Anp::Bin<double> >::iterator lit = bit - 1;
01660       vector<Anp::Bin<double> >::iterator hit = bit + 1;
01661       
01662       if(lit -> sum() < hit -> sum())
01663       {
01664          Anp::Bin<double> bin(0, lit -> edge());
01665          
01666          bin.add(bit -> sum());
01667          bin.add(lit -> sum());
01668          
01669          bvec.erase(lit, hit + 1);
01670          
01671          bvec.push_back(bin);
01672       }
01673       else
01674       {
01675          Anp::Bin<double> bin(0, bit -> edge());
01676          
01677          bin.add(bit -> sum());
01678          bin.add(hit -> sum());
01679          
01680          bvec.erase(bit, hit + 1);
01681          
01682          bvec.push_back(bin);       
01683       }
01684       
01685       std::sort(bvec.begin(), bvec.end());
01686       
01687          bit = bvec.begin();
01688    }
01689    
01690    if(option.find("V") != string::npos)
01691    {
01692       double nsum = 0.0;
01693       for(unsigned int ibin = 0; ibin < bvec.size(); ++ibin)
01694       {
01695          const Anp::Bin<double> &bin = bvec[ibin];
01696          cout << "ibin = " << ibin << ", edge = " << bin.edge() << ", sum = " << bin.sum() << endl; 
01697          nsum += bin.sum();
01698       }
01699       
01700       cout << "old bvec size = " << osize << endl;
01701       cout << "new bvec size = " << bvec.size() << endl;
01702       cout << "osum - nsum = " << osum << " - " << nsum << " = " << osum - nsum << endl;
01703    }
01704 }


Generated on Mon Feb 15 11:08:09 2010 for loon by  doxygen 1.3.9.1