Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
cmf::ChiSqrCalculator Class Reference

#include "/cvmfs/nova-development.opensciencegrid.org/novasoft/releases/N20-12-03/CovarianceMatrixFit/utilities/ChiSqrCalculator.h"

Public Member Functions

void InitializeCovarianceMatrix (fhicl::ParameterSet const &pset)
 
double CovarianceMatrixEntry (size_t matrixPos, cmf::CovMat_t const &matType)
 
double ChiSqr (cmf::ChiSqrCalc_t const &chiSqrCalc, std::vector< std::string > const &pars, const double *vals)
 
double ChiSqr (cmf::ChiSqrCalc_t const &chiSqrCalc, cmf::OscillationParameterMap const &pars)
 
void SetSpectrum (std::vector< float > const &spectrum, bool isData)
 
void FillChiSqrHistograms (cmf::ChiSqrCalc_t const &chiSqrCalc, std::vector< float > const &dataSpectrum, std::vector< float > const &mcSpectrum, TH1D *binByBin, TH1D *cumulative)
 
void FillCovarianceMatrix (Eigen::MatrixXd &matrix, bool isSystOnly=false)
 

Static Public Member Functions

static cmf::ChiSqrCalculatorInstance ()
 

Private Member Functions

 ChiSqrCalculator ()
 
 ~ChiSqrCalculator ()
 
int FindReducedMatrixBin (std::set< long > const &keysToUse, int const &fullMatrixBin)
 
double ChiSqrCovMat ()
 
double ChiSqrPoisson ()
 
double BinPoissonChiSqr (double const &data, double const &MC)
 
double ChiSqrCNP ()
 
double BinCNPChiSqr (double const &data, double const &MC)
 
double ChiSqrGauss ()
 
double BinGaussChiSqr (double const &data, double const &MC)
 
double ChiSqrNuisance (std::vector< std::string > const &pars, const double *vals)
 

Private Attributes

std::vector< float > fDataSpectrum
 data spectrum in bins More...
 
std::vector< float > fMCSpectrum
 MC spectrum in bins. More...
 
double fSystScale
 scaling factor to apply to systematic matrix entries for tests More...
 
Eigen::VectorXd fDiffVec
 column-wise vector of the difference between data and MC More...
 
Eigen::MatrixXd fInputEigenMatrixSyst
 input matrix for systematics More...
 

Detailed Description

Definition at line 24 of file ChiSqrCalculator.h.

Constructor & Destructor Documentation

cmf::ChiSqrCalculator::ChiSqrCalculator ( )
private

Definition at line 29 of file ChiSqrCalculator.cxx.

References fDataSpectrum, fInputEigenMatrixSyst, fMCSpectrum, cmf::CovarianceBinUtility::Instance(), LOG_VERBATIM, and cmf::CovarianceBinUtility::TotalBins().

Referenced by Instance().

30  : fDiffVec(Eigen::VectorXd(cmf::CovarianceBinUtility::Instance()->TotalBins()))
33  {
34  fDataSpectrum.resize(cmf::CovarianceBinUtility::Instance()->TotalBins(), 0.);
35  fMCSpectrum .resize(cmf::CovarianceBinUtility::Instance()->TotalBins(), 0.);
36 
37  LOG_VERBATIM("ChiSqrCalculator")
38  << "The ChiSqrCalculator will use "
40  << " as the number of logical bins in a spectrum."
41  << " There are "
42  << fInputEigenMatrixSyst.rows()
43  << " rows and "
44  << fInputEigenMatrixSyst.cols()
45  << " colums in the matrix";
46  }
Eigen::VectorXd fDiffVec
column-wise vector of the difference between data and MC
std::vector< float > fMCSpectrum
MC spectrum in bins.
size_t TotalBins(bool allSels=false)
void Zero()
#define LOG_VERBATIM(category)
Eigen::MatrixXd fInputEigenMatrixSyst
input matrix for systematics
std::vector< float > fDataSpectrum
data spectrum in bins
static CovarianceBinUtility * Instance()
cmf::ChiSqrCalculator::~ChiSqrCalculator ( )
private

Definition at line 49 of file ChiSqrCalculator.cxx.

50  {
51  }

Member Function Documentation

double cmf::ChiSqrCalculator::BinCNPChiSqr ( double const &  data,
double const &  MC 
)
private

Definition at line 376 of file ChiSqrCalculator.cxx.

References chi2(), and cet::pow().

Referenced by ChiSqrCNP(), and FillChiSqrHistograms().

378  {
379  // strictly speaking, it is possible for both the amount of data
380  // and MC in a bin to be 0. In that case, the first part of this
381  // if statement would add 0 to the chi^2, so it is a no-op and
382  // I am leaving out the check on the amount of MC in the bin to
383  // reduce an extra conditional check
384  double chi2 = 0.;
385  if(data == 0.)
386  chi2 = 2. * MC;
387  else if(MC * data > 0.){
388  chi2 = std::pow(MC - data, 2.) / (3. / (1. / data + 2. / MC));
389  }
390 
391  return chi2;
392  }
constexpr T pow(T x)
Definition: pow.h:75
double chi2()
const XML_Char const XML_Char * data
Definition: expat.h:268
double cmf::ChiSqrCalculator::BinGaussChiSqr ( double const &  data,
double const &  MC 
)
private

Definition at line 395 of file ChiSqrCalculator.cxx.

Referenced by ChiSqrGauss(), and FillChiSqrHistograms().

397  {
398  // below I multiplied out (d - MC)^2 / MC, ie the Pearson
399  // construction value to speed up the calculation
400  double chiSqr = data * data / MC;
401  chiSqr += MC - 2. * data;
402 
403  return chiSqr;
404  }
const XML_Char const XML_Char * data
Definition: expat.h:268
double cmf::ChiSqrCalculator::BinPoissonChiSqr ( double const &  data,
double const &  MC 
)
private

Definition at line 407 of file ChiSqrCalculator.cxx.

References chi2(), and test_ParserArtEvents::log.

Referenced by ChiSqrCNP(), ChiSqrPoisson(), and FillChiSqrHistograms().

409  {
410  double chi2 = MC - data;
411  if(data * MC > 0.) chi2 += data * std::log(data / MC);
412 
413  return 2. * chi2;
414  }
double chi2()
const XML_Char const XML_Char * data
Definition: expat.h:268
double cmf::ChiSqrCalculator::ChiSqr ( cmf::ChiSqrCalc_t const &  chiSqrCalc,
std::vector< std::string > const &  pars,
const double *  vals 
)

Definition at line 237 of file ChiSqrCalculator.cxx.

References ChiSqrCNP(), ChiSqrCovMat(), ChiSqrGauss(), ChiSqrNuisance(), ChiSqrPoisson(), cmf::kCNP, cmf::kCovMat, cmf::kGauss, cmf::kPoisson, and LOG_DEBUG.

Referenced by ChiSqr(), cmf::ContourFromLibrary::FindChiSqrForPoint(), cmf::CovarianceFitHelper::FindInitialGuess(), cmf::FitFunction_ForROOT(), cmf::CovarianceFitHelper::MakeCovarianceMatrixHistogram(), cmf::CovarianceFitHelper::Minimize(), and cmf::FitFeldmanCousinsPoint::writeResults().

240  {
241  double chiSqrNP = this->ChiSqrNuisance(pars, vals);
242 
243  double chiSqr = 0;
244  if (chiSqrType == cmf::kCovMat ) chiSqr = this->ChiSqrCovMat ();
245  else if(chiSqrType == cmf::kPoisson) chiSqr = this->ChiSqrPoisson();
246  else if(chiSqrType == cmf::kCNP ) chiSqr = this->ChiSqrCNP ();
247  else if(chiSqrType == cmf::kGauss ) chiSqr = this->ChiSqrGauss ();
248 
249  LOG_DEBUG("CMFChiSqrCalculator")
250  << "chi^2 = "
251  << chiSqr
252  << " nuisance chi^2 = "
253  << chiSqrNP
254  << " total = "
255  << chiSqr + chiSqrNP;
256 
257  return chiSqr + chiSqrNP;
258  }
#define LOG_DEBUG(stream)
Definition: Messenger.h:149
double ChiSqrNuisance(std::vector< std::string > const &pars, const double *vals)
std::string pars("Th23Dmsq32")
double cmf::ChiSqrCalculator::ChiSqr ( cmf::ChiSqrCalc_t const &  chiSqrCalc,
cmf::OscillationParameterMap const &  pars 
)

Definition at line 224 of file ChiSqrCalculator.cxx.

References ChiSqr(), and cmf::cOscParams_Strings.

226  {
227  std::vector<std::string> parNames;
228  std::vector<double> parVals;
229  for(auto const& itr : pars){
230  parNames.emplace_back(cmf::cOscParams_Strings[itr.first]);
231  parVals .emplace_back(itr.second);
232  }
233  return this->ChiSqr(chiSqrType, parNames, parVals.data());
234  }
const std::string cOscParams_Strings[kNumOscParams]
Definition: Constants.h:257
double ChiSqr(cmf::ChiSqrCalc_t const &chiSqrCalc, std::vector< std::string > const &pars, const double *vals)
std::string pars("Th23Dmsq32")
double cmf::ChiSqrCalculator::ChiSqrCNP ( )
private

Definition at line 446 of file ChiSqrCalculator.cxx.

References BinCNPChiSqr(), BinPoissonChiSqr(), chi2(), fDataSpectrum, fMCSpectrum, MECModelEnuComparisons::i, and LOG_DEBUG.

Referenced by ChiSqr().

447  {
448  double chi2;
449  double sumChi2 = 0.;
450 
451  for(size_t i = 0; i < fDataSpectrum.size(); ++i){
452 
453  chi2 = this->BinCNPChiSqr(fDataSpectrum[i], fMCSpectrum[i]);
454 
455  sumChi2 += chi2;
456 
457  LOG_DEBUG("ChiSqrCalculator")
458  << "CNP bin "
459  << i
460  << " data: "
461  << fDataSpectrum[i]
462  << " MC: "
463  << fMCSpectrum[i]
464  << " chi^2: "
465  << chi2
466  << " total chi^2: "
467  << sumChi2
468  << " poisson chi^2: "
470 
471  }
472 
473  return sumChi2;
474  }
#define LOG_DEBUG(stream)
Definition: Messenger.h:149
double BinCNPChiSqr(double const &data, double const &MC)
double chi2()
std::vector< float > fMCSpectrum
MC spectrum in bins.
double BinPoissonChiSqr(double const &data, double const &MC)
std::vector< float > fDataSpectrum
data spectrum in bins
double cmf::ChiSqrCalculator::ChiSqrCovMat ( )
private

Definition at line 261 of file ChiSqrCalculator.cxx.

References fDataSpectrum, fDiffVec, FillCovarianceMatrix(), fMCSpectrum, MECModelEnuComparisons::i, Instance(), LOG_DEBUG, sum, and Zero().

Referenced by ChiSqr().

262  {
263  // make a matrix for the calculation
264  Eigen::MatrixXd covMatEigen = Eigen::MatrixXd::Zero(fDataSpectrum.size(),
265  fDataSpectrum.size());
266 
267  // update the covariance matrix based on the latest event counts for the current point
269 
270  for(size_t i = 0; i < fDataSpectrum.size(); ++i){
271 
273 
274  LOG_DEBUG("ChiSqrCalculator")
275  << i
276  << " data "
277  << fDataSpectrum[i]
278  << " mc "
279  << fMCSpectrum[i]
280  << " diffs: "
281  << fDiffVec(i)
282  << " "
283  << covMatEigen(i, i);
284  }
285 
286  // LOG_DEBUG("CMFChiSqrCalculatorEigen")
287  // << "difference vector \n"
288  // << fDiffVec
289  // << " \n\n transpose "
290  // << fDiffVec.transpose()
291  // << " \n\n intermediate matrix \n\n"
292  // << covMatEigen.inverse()
293  // << " \n\n eigen chiSqr matrix \n\n"
294  // << (fDiffVec.transpose() * fCovarianceMatrixEigen.inverse() * fDiffVec).sum();
295 
296  return (fDiffVec.transpose() * covMatEigen.inverse() * fDiffVec).sum();
297  }
Eigen::VectorXd fDiffVec
column-wise vector of the difference between data and MC
#define LOG_DEBUG(stream)
Definition: Messenger.h:149
std::vector< float > fMCSpectrum
MC spectrum in bins.
static cmf::ChiSqrCalculator * Instance()
void Zero()
Double_t sum
Definition: plot.C:31
void FillCovarianceMatrix(Eigen::MatrixXd &matrix, bool isSystOnly=false)
std::vector< float > fDataSpectrum
data spectrum in bins
double cmf::ChiSqrCalculator::ChiSqrGauss ( )
private

Definition at line 302 of file ChiSqrCalculator.cxx.

References BinGaussChiSqr(), fDataSpectrum, fMCSpectrum, MECModelEnuComparisons::i, and LOG_DEBUG.

Referenced by ChiSqr().

303  {
304  double chiSqr = 0.;
305 
306  for(size_t i = 0; i < fDataSpectrum.size(); ++i){
307  if(fMCSpectrum[i] > 0.)
308  chiSqr += this->BinGaussChiSqr(fDataSpectrum[i], fMCSpectrum[i]);
309 
310  LOG_DEBUG("ChiSqrCalculator")
311  << "Gaussian chi^2 "
312  << i
313  << " data "
314  << fDataSpectrum[i]
315  << " MC "
316  << fMCSpectrum[i]
317  << " chi^2 "
318  << chiSqr;
319  }
320 
321  return chiSqr;
322  }
#define LOG_DEBUG(stream)
Definition: Messenger.h:149
double BinGaussChiSqr(double const &data, double const &MC)
std::vector< float > fMCSpectrum
MC spectrum in bins.
std::vector< float > fDataSpectrum
data spectrum in bins
double cmf::ChiSqrCalculator::ChiSqrNuisance ( std::vector< std::string > const &  pars,
const double *  vals 
)
private

Definition at line 326 of file ChiSqrCalculator.cxx.

References fSystScale, cmf::ParameterUtility::Instance(), cmf::Parameter::IsNuisance(), LOG_DEBUG, cmf::ParameterUtility::ParameterInfo(), and cet::pow().

Referenced by ChiSqr().

328  {
329  // we want to get any chi^2 contribution from the nuisance parameters now
330  double chiSqrNP = 0.;
331  double curNPVal = 0.;
332 
333 // LOG_VERBATIM("ChiSqrCalculator")
334 // << "there are "
335 // << cmf::NuisanceParameters::Instance()->NuisanceParameterInfo().size()
336 // << " nuisance parameters";
337 // for(auto const& itr : npMap)
338 // LOG_VERBATIM("ChiSqrCalculator")
339 // << "input npMap "
340 // << itr.first.first
341 // << " "
342 // << cmf::cDetType_Strings[itr.first.second]
343 // << " "
344 // << itr.second;
345 
346  for(size_t p = 0; p < pars.size(); ++p){
348 
349  curNPVal = vals[p];
350 
351  auto const& np = cmf::ParameterUtility::Instance()->ParameterInfo(pars[p]);
352 
353  // if we are looking at a systematic parameter, so not an oscillation
354  // parameter, multiply the current value by the systematic scale factor
355  // for stats only fits this will zero curNPVal of systematic parameters
356  // TODO: this may not be the right thing to do to scale the uncertainties except for the stats only fit
357  if(!np.IsOscPar()) curNPVal *= fSystScale;
358 
359  chiSqrNP += std::pow((curNPVal - np.CentralValue()) / np.Sigma(), 2.);
360 
361  LOG_DEBUG("ChiSqrCalculator")
362  << pars[p]
363  << " "
364  << np.CentralValue()
365  << " "
366  << curNPVal
367  << " "
368  << np.Sigma();
369 
370  }
371 
372  return chiSqrNP;
373  }
#define LOG_DEBUG(stream)
Definition: Messenger.h:149
cmf::Parameter const & ParameterInfo(std::string const &parName)
const char * p
Definition: xmltok.h:285
constexpr T pow(T x)
Definition: pow.h:75
static ParameterUtility * Instance()
bool const & IsNuisance() const
Definition: Parameter.h:110
double fSystScale
scaling factor to apply to systematic matrix entries for tests
std::string pars("Th23Dmsq32")
double cmf::ChiSqrCalculator::ChiSqrPoisson ( )
private

Definition at line 419 of file ChiSqrCalculator.cxx.

References BinPoissonChiSqr(), chi2(), fDataSpectrum, fMCSpectrum, MECModelEnuComparisons::i, and LOG_DEBUG.

Referenced by ChiSqr().

420  {
421  double chi2 = 0.;
422 
423  // check that the data and MC are both non-zero before trying to add the 3rd term
424  // with the natural log
425  for(size_t i = 0; i < fDataSpectrum.size(); ++i){
426  chi2 += this->BinPoissonChiSqr(fDataSpectrum[i],
427  fMCSpectrum[i]);
428 
429  LOG_DEBUG("ChiSqrCalculator")
430  << "Poisson bin "
431  << i
432  << " data: "
433  << fDataSpectrum[i]
434  << " MC: "
435  << fMCSpectrum[i]
436  << " total chi^2: "
437  << chi2;
438  }
439 
440  return chi2;
441  }
#define LOG_DEBUG(stream)
Definition: Messenger.h:149
double chi2()
std::vector< float > fMCSpectrum
MC spectrum in bins.
double BinPoissonChiSqr(double const &data, double const &MC)
std::vector< float > fDataSpectrum
data spectrum in bins
double cmf::ChiSqrCalculator::CovarianceMatrixEntry ( size_t  matrixPos,
cmf::CovMat_t const &  matType 
)

Definition at line 54 of file ChiSqrCalculator.cxx.

References cmf::kGarbageDouble.

56  {
57  return cmf::kGarbageDouble;
58  }
static const double kGarbageDouble
Definition: Constants.h:22
void cmf::ChiSqrCalculator::FillChiSqrHistograms ( cmf::ChiSqrCalc_t const &  chiSqrCalc,
std::vector< float > const &  dataSpectrum,
std::vector< float > const &  mcSpectrum,
TH1D *  binByBin,
TH1D *  cumulative 
)

Definition at line 536 of file ChiSqrCalculator.cxx.

References b, BinCNPChiSqr(), BinGaussChiSqr(), BinPoissonChiSqr(), fDataSpectrum, fMCSpectrum, cmf::kCNP, cmf::kCovMat, cmf::kGauss, cmf::kPoisson, and SetSpectrum().

Referenced by cmf::CovarianceFitHelper::MakeResultPlots().

541  {
542  this->SetSpectrum(dataSpectrum, true );
543  this->SetSpectrum(mcSpectrum, false);
544 
545  // for the cmf::kCovMat chi^2 calc type, use the CNP bin by bin value
546  // because those give the same total chi^2
547 
548  // Fill histograms showing the chi^2 value for each bin and the cumulative chi^2
549 
550  double chiSqr;
551  double chiSqrSum = 0.;
552  for(size_t b = 0; b < fDataSpectrum.size(); ++b){
553 
554  if (chiSqrCalc == cmf::kCNP ||
555  chiSqrCalc == cmf::kCovMat) chiSqr = this->BinCNPChiSqr (fDataSpectrum[b], fMCSpectrum[b]);
556  else if(chiSqrCalc == cmf::kPoisson) chiSqr = this->BinPoissonChiSqr(fDataSpectrum[b], fMCSpectrum[b]);
557  else if(chiSqrCalc == cmf::kGauss) chiSqr = this->BinGaussChiSqr (fDataSpectrum[b], fMCSpectrum[b]);
558 
559  chiSqrSum += chiSqr;
560 
561  binByBin ->Fill(b, chiSqr);
562  cumulative->Fill(b, chiSqrSum);
563  }
564  }
double BinCNPChiSqr(double const &data, double const &MC)
double BinGaussChiSqr(double const &data, double const &MC)
std::vector< float > fMCSpectrum
MC spectrum in bins.
void SetSpectrum(std::vector< float > const &spectrum, bool isData)
const hit & b
Definition: hits.cxx:21
double BinPoissonChiSqr(double const &data, double const &MC)
std::vector< float > fDataSpectrum
data spectrum in bins
void cmf::ChiSqrCalculator::FillCovarianceMatrix ( Eigen::MatrixXd &  matrix,
bool  isSystOnly = false 
)

Definition at line 478 of file ChiSqrCalculator.cxx.

References b, fDataSpectrum, fInputEigenMatrixSyst, fMCSpectrum, fSystScale, calib::j, LOG_DEBUG, confusionMatrixTree::matrix, and stats().

Referenced by ChiSqrCovMat(), and cmf::CMFDecorrelator::writeResults().

480  {
481  // as in the CNPChiSqr function, use https://arxiv.org/pdf/1903.07185.pdf
482  // to determine the values for the statistical uncertainty matrix
483  // if we are not using some bins the count value will be 0, so set it to
484  // something just very small in that case
485 
486  // the data and MC spectra are the same size, just loop over the size of
487  // one of them
488  double stats;
489  for(size_t b = 0; b < fMCSpectrum.size(); ++b){
490  for(size_t j = 0; j < fMCSpectrum.size(); ++j){
491 
492  stats = 0.;
493  if(b == j && !isSystOnly){
494  // Default the value of stats to 1 on the diagonal to account for the possibility that
495  // we have neither data, nor MC in a bin. Since we do not remove those bins from the
496  // covariance matrix, we need to set the diagonal term to 1.
497  // That is true when we have removed a selection from the analysis, ie not including NC
498  // events in the 3 flavor analysis. There should never be a case where we have
499  // data events in a bin but no MC
500  if(fDataSpectrum[b] == 0. && fMCSpectrum[b] > 0.)
501  stats = fMCSpectrum[b] * 0.5;
502  else if(fMCSpectrum[b] * fDataSpectrum[b] > 0.)
503  stats = 3. / (1. / fDataSpectrum[b] + 2. / fMCSpectrum[b]);
504  else
505  stats = 1.;
506 
507  LOG_DEBUG("ChiSqrCalculator")
508  << b
509  << " "
510  << fMCSpectrum[b]
511  << " "
512  << fDataSpectrum[b]
513  << " "
514  << stats
515  << " "
517  << " "
518  << fSystScale;
519 
520  }
521 
523 
524  } // end loop over j bins
525 
526  } // end loop over b bins
527 
528  LOG_DEBUG("ChiSqrCalculator")
529  << matrix;
530 
531  } // end FillCovarianceMatrix
#define LOG_DEBUG(stream)
Definition: Messenger.h:149
int stats(TString inputFilePath, Int_t firstRun, Int_t lastRun, Float_t thresh, TString myDet)
Definition: stats.C:13
std::vector< float > fMCSpectrum
MC spectrum in bins.
const double j
Definition: BetheBloch.cxx:29
double fSystScale
scaling factor to apply to systematic matrix entries for tests
const hit & b
Definition: hits.cxx:21
Eigen::MatrixXd fInputEigenMatrixSyst
input matrix for systematics
std::vector< float > fDataSpectrum
data spectrum in bins
int cmf::ChiSqrCalculator::FindReducedMatrixBin ( std::set< long > const &  keysToUse,
int const &  fullMatrixBin 
)
private

Definition at line 61 of file ChiSqrCalculator.cxx.

References cmf::CovarianceBinUtility::BinToKey(), cmf::CovarianceBinUtility::Instance(), cmf::SelectionUtility::Instance(), cmf::IsNuESelected(), cmf::IsNuMuSelected(), cmf::KeyToBeamType(), cmf::KeyToDetectorType(), cmf::KeyToSelectionType(), cmf::KeyToString(), cmf::kNuESelection, cmf::kNuMuSelection, LOG_DEBUG, cmf::SelectionTypeKey(), cmf::SelectionUtility::UsesDetAndBeam(), and cmf::SelectionUtility::UsesSelection().

Referenced by InitializeCovarianceMatrix().

63  {
64  long fullMatrixKey = cmf::CovarianceBinUtility::Instance()->BinToKey(fullMatrixBin, true);
65  long condensedMatrixKey = fullMatrixKey;
66 
67  LOG_DEBUG("ChiSqrCalculator")
68  << "key is "
69  << fullMatrixKey
70  << " "
71  << cmf::KeyToString(fullMatrixKey);
72 
73  if(keysToUse.find(fullMatrixKey) == keysToUse.end()){
74 
75  // check that this beam type and detector is desired
77  cmf::KeyToBeamType(fullMatrixKey)))
78  return std::numeric_limits<int>::lowest();
79 
80  // maybe we want concatenated selections, so check those out
81  // use the fullMatrixKey to keep the detector and beam information, but
82  // remove the selection type information and then add back in the concat
83  // selection type
86  condensedMatrixKey = (fullMatrixKey -
89  }
92  condensedMatrixKey = (fullMatrixKey -
95  }
96  else
97  return std::numeric_limits<int>::lowest();
98  }
99 
100  // subtract off the offset from the all selections for this key and then add
101  // back the offset for the current configuration for the key
102  return (fullMatrixBin -
103  cmf::CovarianceBinUtility::Instance()->KeyToOffset(fullMatrixKey, true) +
104  cmf::CovarianceBinUtility::Instance()->KeyToOffset(condensedMatrixKey));
105  }
#define LOG_DEBUG(stream)
Definition: Messenger.h:149
static long SelectionTypeKey(cmf::SelectionType_t const &st)
Definition: StaticFuncs.h:23
static SelectionUtility * Instance()
long BinToKey(int const &bin, bool allSels=false)
static cmf::BeamType_t KeyToBeamType(long const &key)
Definition: StaticFuncs.h:136
static bool IsNuESelected(cmf::SelectionType_t const &sel)
Definition: StaticFuncs.h:376
static bool IsNuMuSelected(cmf::SelectionType_t const &sel)
Definition: StaticFuncs.h:362
static cmf::DetType_t KeyToDetectorType(long const &key)
Definition: StaticFuncs.h:70
bool UsesSelection(cmf::SelectionType_t const &s) const
bool UsesDetAndBeam(cmf::MetaData const &md) const
static cmf::SelectionType_t KeyToSelectionType(long const &key)
Definition: StaticFuncs.h:170
static std::string KeyToString(long const &key)
Definition: StaticFuncs.h:228
static CovarianceBinUtility * Instance()
void cmf::ChiSqrCalculator::InitializeCovarianceMatrix ( fhicl::ParameterSet const &  pset)

Definition at line 108 of file ChiSqrCalculator.cxx.

References FindReducedMatrixBin(), fInputEigenMatrixSyst, fSystScale, fhicl::ParameterSet::get(), MECModelEnuComparisons::i, cmf::CovarianceBinUtility::Instance(), cmf::SelectionUtility::Instance(), calib::j, findDuplicateFiles::key, caf_analysis::keys, LOG_DEBUG, LOG_VERBATIM, string, fhicl::ParameterSet::to_indented_string(), and cmf::CovarianceBinUtility::TotalBins().

Referenced by cmf::CovarianceFitHelper::Initialize(), and cmf::ContourFromLibrary::reconfigure().

109  {
110  LOG_VERBATIM("ChiSqrCalculator")
111  << pset.to_indented_string();
112 
113  fSystScale = pset.get<double>("SystScale", 1.);
114 
115  // if we are doing a stat only fit, we do nothing - the input covariance
116  // matrix was initialized with zeros in the constructor
117  if(pset.get<bool>("StatOnlyFit", false)) return;
118 
119  // get the information for where the covariance matrix is stored
120  // assume that the matrices for individual systematics have already
121  // been summed by this point
122  auto covarianceHistName = pset.get<std::string>("CovarianceMatrixHistName", "totalCovariance");
123 
124  // xrootd path to file in /pnfs/nova/scratch/
125  auto covarianceHistFileName = pset.get<std::string>("CovarianceMatrixHistFile");
126 
127  // get the covariance matrix from the input file - it is assumed to already be summed
128  // for the individual systematic uncertainties.
129 
130  // Load the covariance matrix from the input histogram file.
131  // Assume that the matrix has been properly normalized for the total
132  // number of shifts tested before we get it
133  TFile* covHistFile = TFile::Open(covarianceHistFileName.c_str());
134  TH2D* inputCovarianceSyst = dynamic_cast<TH2D*>(covHistFile->Get(covarianceHistName.c_str()));
135 
136  int numBins = inputCovarianceSyst->GetNbinsX();
137 
138  // The CovarianceBinUtility::TotalBins() returns the number for the full set of selections if
139  // the passed argument is true, the default is false but in this case we want to be sure the
140  // input covariance bin utility has everything we need
141  if(numBins != (int)cmf::CovarianceBinUtility::Instance()->TotalBins(true))
142  throw cet::exception("ChiSqrCalculator")
143  << "input matrix has different number of logical bins than CovarianceBinUtility "
144  << numBins
145  << " vs "
147 
148  LOG_DEBUG("ChiSqrCalculator")
149  << "numBins is "
150  << numBins
151  << " / "
153 
154  // in the following for loops we want to check that the bin to key values correspond
155  // to a selection we want. The returned value of SelectionsToUse is a set of
156  // DetBeamSels, a struct that holds a detector id, a beam type and a list of
157  // SelectionTypes
158  int iBin;
159  int jBin;
160 
161  std::set<long> keys;
162 
163  // get the keys for the selections we want
164  for(auto const& dbsItr : cmf::SelectionUtility::Instance()->SelectionsToUse()){
165  for(auto const& key: dbsItr.Keys()) keys.insert(key);
166  }
167 
168  for(int i = 0; i < numBins; ++i){
169 
170  iBin = this->FindReducedMatrixBin(keys, i);
171 
172  if(iBin == std::numeric_limits<int>::lowest()) continue;
173  LOG_DEBUG("ChiSqrCalculator")
174  << "i-th bin is "
175  << iBin
176  << " nominal i: "
177  << i;
178 
179  for(int j = 0; j < numBins; ++j){
180  jBin = this->FindReducedMatrixBin(keys, j);
181  if(jBin == std::numeric_limits<int>::lowest()) continue;
182 
183  LOG_DEBUG("ChiSqrCalculator")
184  << "j-th bin is "
185  << jBin
186  << " nominal j: "
187  << j;
188 
189  fInputEigenMatrixSyst(iBin, jBin) = inputCovarianceSyst->GetBinContent(i + 1, j + 1);
190  } // end loop over i bins
191  } // end loop over j bins
192 
193  covHistFile->Close();
194  }
#define LOG_DEBUG(stream)
Definition: Messenger.h:149
keys
Reco plots.
Definition: caf_analysis.py:46
static SelectionUtility * Instance()
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
int FindReducedMatrixBin(std::set< long > const &keysToUse, int const &fullMatrixBin)
const double j
Definition: BetheBloch.cxx:29
double fSystScale
scaling factor to apply to systematic matrix entries for tests
size_t TotalBins(bool allSels=false)
#define LOG_VERBATIM(category)
Eigen::MatrixXd fInputEigenMatrixSyst
input matrix for systematics
static CovarianceBinUtility * Instance()
enum BeamMode string
ChiSqrCalculator * cmf::ChiSqrCalculator::Instance ( )
static
void cmf::ChiSqrCalculator::SetSpectrum ( std::vector< float > const &  spectrum,
bool  isData 
)

Definition at line 197 of file ChiSqrCalculator.cxx.

References fDataSpectrum, fMCSpectrum, and LOG_DEBUG.

Referenced by cmf::ContourFromLibrary::CreateChiSqrMaps(), FillChiSqrHistograms(), cmf::CovarianceFitHelper::FillDataSpectrum(), cmf::CovarianceFitHelper::FillMCSpectrum(), cmf::ContourFromLibrary::FindChiSqrForPoint(), cmf::CovarianceFitHelper::FindInitialGuess(), cmf::CovarianceFitHelper::MakeCovarianceMatrixHistogram(), cmf::CMFDecorrelator::writeResults(), and cmf::FitFeldmanCousinsPoint::writeResults().

199  {
200  // std::vector<float> tmpVec(spectrum.size(), 0.);
201  // for(size_t b = 0; b < spectrum.size(); ++b){
202  // tmpVec[b] = spectrum[b];
203 
204  // LOG_DEBUG("ChiSqrCalculator")
205  // << "spectrum bin "
206  // << b
207  // << " "
208  // << tmpVec[b]
209  // << " / "
210  // << spectrum[b];
211  // }
212 
213  LOG_DEBUG("ChiSqrCalculator")
214  << "SetSpectrum, there are: "
215  << spectrum.size()
216  << " bins, isData: "
217  << isData;
218 
219  if(isData) fDataSpectrum = spectrum;
220  else fMCSpectrum = spectrum;
221  }
#define LOG_DEBUG(stream)
Definition: Messenger.h:149
std::vector< float > fMCSpectrum
MC spectrum in bins.
std::vector< float > fDataSpectrum
data spectrum in bins

Member Data Documentation

std::vector<float> cmf::ChiSqrCalculator::fDataSpectrum
private
Eigen::VectorXd cmf::ChiSqrCalculator::fDiffVec
private

column-wise vector of the difference between data and MC

Definition at line 77 of file ChiSqrCalculator.h.

Referenced by ChiSqrCovMat().

Eigen::MatrixXd cmf::ChiSqrCalculator::fInputEigenMatrixSyst
private

input matrix for systematics

Definition at line 78 of file ChiSqrCalculator.h.

Referenced by ChiSqrCalculator(), FillCovarianceMatrix(), and InitializeCovarianceMatrix().

std::vector<float> cmf::ChiSqrCalculator::fMCSpectrum
private
double cmf::ChiSqrCalculator::fSystScale
private

scaling factor to apply to systematic matrix entries for tests

Definition at line 76 of file ChiSqrCalculator.h.

Referenced by ChiSqrNuisance(), FillCovarianceMatrix(), and InitializeCovarianceMatrix().


The documentation for this class was generated from the following files: