CMS 3D CMS Logo

List of all members | Public Member Functions | Protected Member Functions | Protected Attributes
HCovarianceVSxy Class Reference

#include <Histograms.h>

Inherits Histograms.

Public Member Functions

void Clear () override
 
void Fill (const double &x, const double &y, const double &a, const double &b) override
 
double Get (const double &x, const double &y) const
 
 HCovarianceVSxy (const TString &name, const TString &title, const int totBinsX, const double &xMin, const double &xMax, const int totBinsY, const double &yMin, const double &yMax, TDirectory *dir=0, bool varianceCheck=false)
 
 HCovarianceVSxy (TFile *inputFile, const TString &name, const TString &dirName)
 Contructor to read histograms from file. More...
 
void Write () override
 
 ~HCovarianceVSxy () override
 

Protected Member Functions

int getXindex (const double &x) const
 
int getYindex (const double &y) const
 

Protected Attributes

Covariance ** covariances_
 
double deltaX_
 
double deltaY_
 
TH2D * histoCovariance_
 
TH1D *** histoVarianceCheck_
 
bool readMode_
 
int totBinsX_
 
int totBinsY_
 
int totBinsZ_
 
bool varianceCheck_
 
double xMin_
 
double yMin_
 

Detailed Description

This class can be used to compute the covariance of two variables with respect to other two variables (to see e.g. how does the covariance of ptVSphi vary with respect to (pt,eta).

Definition at line 1818 of file Histograms.h.

Constructor & Destructor Documentation

HCovarianceVSxy::HCovarianceVSxy ( const TString &  name,
const TString &  title,
const int  totBinsX,
const double &  xMin,
const double &  xMax,
const int  totBinsY,
const double &  yMin,
const double &  yMax,
TDirectory *  dir = 0,
bool  varianceCheck = false 
)
inline

Definition at line 1821 of file Histograms.h.

References dir, mps_fire::i, and dataset::name.

1824  :
1825  totBinsX_(totBinsX), totBinsY_(totBinsY),
1827  readMode_(false),
1828  varianceCheck_(varianceCheck)
1829  {
1830  name_ = name;
1831  histoDir_ = dir;
1832  histoCovariance_ = new TH2D(name+"Covariance", title+" covariance", totBinsX, xMin, xMax, totBinsY, yMin, yMax);
1833 
1834  covariances_ = new Covariance*[totBinsX];
1835  for( int i=0; i<totBinsX; ++i ) {
1836  covariances_[i] = new Covariance[totBinsY];
1837  }
1838  if( varianceCheck_ ) {
1839  histoVarianceCheck_ = new TH1D**[totBinsX_];
1840  for( int i=0; i<totBinsX_; ++i ) {
1841  histoVarianceCheck_[i] = new TH1D*[totBinsY_];
1842  for( int j=0; j<totBinsY_; ++j ) {
1843  std::stringstream namei;
1844  std::stringstream namej;
1845  namei << i;
1846  namej << j;
1847  histoVarianceCheck_[i][j] = new TH1D(name+"_"+namei.str()+"_"+namej.str(), name, 10000, -1, 1);
1848  }
1849  }
1850  }
1851  }
TH2D * histoCovariance_
Definition: Histograms.h:1972
Covariance ** covariances_
Definition: Histograms.h:1973
TH1D *** histoVarianceCheck_
Definition: Histograms.h:1978
dbl *** dir
Definition: mlp_gen.cc:35
HCovarianceVSxy::HCovarianceVSxy ( TFile *  inputFile,
const TString &  name,
const TString &  dirName 
)
inline

Contructor to read histograms from file.

Definition at line 1853 of file Histograms.h.

References gather_cfg::cout, and cmsRelvalreport::exit.

1853  :
1854  readMode_(true)
1855  {
1856  histoDir_ = (TDirectory*)(inputFile->Get(dirName.Data()));
1857  if( histoDir_ == nullptr ) {
1858  std::cout << "Error: directory not found" << std::endl;
1859  exit(0);
1860  }
1861  histoCovariance_ = (TH2D*)(histoDir_->Get(name));
1862  totBinsX_ = histoCovariance_->GetNbinsX();
1863  xMin_ = histoCovariance_->GetXaxis()->GetBinLowEdge(1);
1864  deltaX_ = histoCovariance_->GetXaxis()->GetBinUpEdge(totBinsX_) - xMin_;
1865  totBinsY_ = histoCovariance_->GetNbinsY();
1866  yMin_ = histoCovariance_->GetYaxis()->GetBinLowEdge(1);
1867  deltaY_ = histoCovariance_->GetYaxis()->GetBinUpEdge(totBinsY_) - yMin_;
1868  }
TH2D * histoCovariance_
Definition: Histograms.h:1972
HCovarianceVSxy::~HCovarianceVSxy ( )
inlineoverride

Definition at line 1870 of file Histograms.h.

References mps_fire::i.

1870  {
1871  delete histoCovariance_;
1872  // Free covariances
1873  for(int i=0; i<totBinsX_; ++i) {
1874  delete[] covariances_[i];
1875  }
1876  delete[] covariances_;
1877  // Free variance check histograms
1878  if( varianceCheck_ ) {
1879  for( int i=0; i<totBinsX_; ++i ) {
1880  for( int j=0; j<totBinsY_; ++j ) {
1881  delete histoVarianceCheck_[i][j];
1882  }
1883  delete[] histoVarianceCheck_[i];
1884  }
1885  delete[] histoVarianceCheck_;
1886  }
1887  }
TH2D * histoCovariance_
Definition: Histograms.h:1972
Covariance ** covariances_
Definition: Histograms.h:1973
TH1D *** histoVarianceCheck_
Definition: Histograms.h:1978

Member Function Documentation

void HCovarianceVSxy::Clear ( )
inlineoverride

Definition at line 1955 of file Histograms.h.

References mps_fire::i.

1955  {
1956  histoCovariance_->Clear();
1957  if( varianceCheck_ ) {
1958  for( int i=0; i<totBinsX_; ++i ) {
1959  for( int j=0; j<totBinsY_; ++j ) {
1960  histoVarianceCheck_[i][j]->Clear();
1961  }
1962  }
1963  }
1964  }
TH2D * histoCovariance_
Definition: Histograms.h:1972
TH1D *** histoVarianceCheck_
Definition: Histograms.h:1978
void HCovarianceVSxy::Fill ( const double &  x,
const double &  y,
const double &  a,
const double &  b 
)
inlineoverride

x and y should be the variables VS which we are computing the covariance (pt and eta) a and b should be the variables OF which we are computing the covariance

Definition at line 1893 of file Histograms.h.

Referenced by ResolutionAnalyzer::analyze().

1893  {
1894  // Need to convert the (x,y) values to the array indeces
1895  int xIndex = getXindex(x);
1896  int yIndex = getYindex(y);
1897  // Only fill values if they are in the selected range
1898  if ( 0 <= xIndex && xIndex < totBinsX_ && 0 <= yIndex && yIndex < totBinsY_ ) {
1899  // if( TString(histoCovariance_->GetName()).Contains("CovarianceCotgTheta_Covariance") )
1900  covariances_[xIndex][yIndex].fill(a,b);
1901  // Should be used with the variance, in which case a==b
1902  if( varianceCheck_ ) histoVarianceCheck_[xIndex][yIndex]->Fill(a);
1903  }
1904  }
int getXindex(const double &x) const
Definition: Histograms.h:1966
void fill(const double &x, const double &y)
Definition: Histograms.h:1791
Covariance ** covariances_
Definition: Histograms.h:1973
double b
Definition: hdecay.h:120
int getYindex(const double &y) const
Definition: Histograms.h:1969
TH1D *** histoVarianceCheck_
Definition: Histograms.h:1978
double a
Definition: hdecay.h:121
double HCovarianceVSxy::Get ( const double &  x,
const double &  y 
) const
inline

Definition at line 1907 of file Histograms.h.

1907  {
1908  // Need to convert the (x,y) values to the array indeces
1909  int xIndex = getXindex(x);
1910  int yIndex = getYindex(y);
1911  // If the values exceed the limits of the histogram, return the border values
1912  if ( xIndex < 0 ) xIndex = 0;
1913  if ( xIndex >= totBinsX_ ) xIndex = totBinsX_-1;
1914  if ( yIndex < 0 ) yIndex = 0;
1915  if ( yIndex >= totBinsY_ ) yIndex = totBinsY_-1;
1916  return histoCovariance_->GetBinContent(xIndex+1, yIndex+1);
1917  }
int getXindex(const double &x) const
Definition: Histograms.h:1966
TH2D * histoCovariance_
Definition: Histograms.h:1972
int getYindex(const double &y) const
Definition: Histograms.h:1969
int HCovarianceVSxy::getXindex ( const double &  x) const
inlineprotected

Definition at line 1966 of file Histograms.h.

References createfilelist::int.

1966  {
1967  return int((x-xMin_)/deltaX_*totBinsX_);
1968  }
int HCovarianceVSxy::getYindex ( const double &  y) const
inlineprotected

Definition at line 1969 of file Histograms.h.

References createfilelist::int.

1969  {
1970  return int((y-yMin_)/deltaY_*totBinsY_);
1971  }
void HCovarianceVSxy::Write ( )
inlineoverride

Definition at line 1919 of file Histograms.h.

References svgfig::canvas(), gather_cfg::cout, and cuy::yBin.

Referenced by ResolutionAnalyzer::writeHistoMap().

1919  {
1920  if( !readMode_ ) {
1921  std::cout << "writing: " << histoCovariance_->GetName() << std::endl;
1922  for( int xBin=0; xBin<totBinsX_; ++xBin ) {
1923  for( int yBin=0; yBin<totBinsY_; ++yBin ) {
1924  double covariance = covariances_[xBin][yBin].covariance();
1925  // Histogram bins start from 1
1926  // std::cout << "covariance["<<xBin<<"]["<<yBin<<"] with N = "<<covariances_[xBin][yBin].getN()<<" is: " << covariance << std::endl;
1927  histoCovariance_->SetBinContent(xBin+1, yBin+1, covariance);
1928  }
1929  }
1930  if( histoDir_ != nullptr ) histoDir_->cd();
1931  TCanvas canvas(TString(histoCovariance_->GetName())+"_canvas", TString(histoCovariance_->GetTitle())+" canvas", 1000, 800);
1932  canvas.Divide(2);
1933  canvas.cd(1);
1934  histoCovariance_->Draw("lego");
1935  canvas.cd(2);
1936  histoCovariance_->Draw("surf5");
1937  canvas.Write();
1938  histoCovariance_->Write();
1939 
1940  TDirectory * binsDir = nullptr;
1941  if( varianceCheck_ ) {
1942  if ( histoDir_ != nullptr ) {
1943  histoDir_->cd();
1944  if( binsDir == nullptr ) binsDir = histoDir_->mkdir(name_+"Bins");
1945  binsDir->cd();
1946  }
1947  for( int xBin=0; xBin<totBinsX_; ++xBin ) {
1948  for( int yBin=0; yBin<totBinsY_; ++yBin ) {
1949  histoVarianceCheck_[xBin][yBin]->Write();
1950  }
1951  }
1952  }
1953  }
1954  }
double covariance()
Definition: Histograms.h:1797
TH2D * histoCovariance_
Definition: Histograms.h:1972
Covariance ** covariances_
Definition: Histograms.h:1973
TH1D *** histoVarianceCheck_
Definition: Histograms.h:1978
def canvas(sub, attr)
Definition: svgfig.py:482
yBin
Definition: cuy.py:893

Member Data Documentation

Covariance** HCovarianceVSxy::covariances_
protected

Definition at line 1973 of file Histograms.h.

double HCovarianceVSxy::deltaX_
protected

Definition at line 1975 of file Histograms.h.

double HCovarianceVSxy::deltaY_
protected

Definition at line 1975 of file Histograms.h.

TH2D* HCovarianceVSxy::histoCovariance_
protected

Definition at line 1972 of file Histograms.h.

TH1D*** HCovarianceVSxy::histoVarianceCheck_
protected

Definition at line 1978 of file Histograms.h.

bool HCovarianceVSxy::readMode_
protected

Definition at line 1976 of file Histograms.h.

int HCovarianceVSxy::totBinsX_
protected

Definition at line 1974 of file Histograms.h.

int HCovarianceVSxy::totBinsY_
protected

Definition at line 1974 of file Histograms.h.

int HCovarianceVSxy::totBinsZ_
protected

Definition at line 1974 of file Histograms.h.

bool HCovarianceVSxy::varianceCheck_
protected

Definition at line 1977 of file Histograms.h.

double HCovarianceVSxy::xMin_
protected

Definition at line 1975 of file Histograms.h.

double HCovarianceVSxy::yMin_
protected

Definition at line 1975 of file Histograms.h.